Skip to content

Commit

Permalink
fixed outgoing ip addresses for opensips
Browse files Browse the repository at this point in the history
  • Loading branch information
adubovikov committed Mar 27, 2024
1 parent 49bd616 commit 0ae5a8e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 25 deletions.
62 changes: 41 additions & 21 deletions kern/opensips.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#include "rtcagent.h"

#define _SYS_SOCKET_H 1
#define IP4_MAX_STR_SIZE 15
#define IP6_MAX_STR_SIZE 45
#define IP_ADDR_MAX_STR_SIZE 6 /* ip62ascii + \0*/
#define SS_MAXSIZE 128 /* Implementation specific max size */
#define MAX_DATA_SIZE_SIP 1024*4
#define MAX_DATA_SIZE_SIP 1024 * 4

typedef unsigned short sa_family_t;

Expand Down Expand Up @@ -107,29 +106,50 @@ typedef union sockaddr_union
typedef struct socket_info
{
int socket;
int gindex; /* global index in the lists of all sockets */
str name; /* name - eg.: foo.bar or 10.0.0.1 */
struct ip_addr address; /* ip address */
str address_str; /*ip address converted to string -- optimization*/
str port_no_str; /* port number converted to string -- optimization*/
enum si_flags flags; /* SI_IS_IP | SI_IS_LO | SI_IS_MCAST */
str name; /*!< name - eg.: foo.bar or 10.0.0.1 */
str tag; /* the tag of the interface, use only in OpenSIPS ecosystem */
struct ip_addr address; /*!< ip address */
str address_str; /*!< ip address converted to string -- optimization*/
unsigned short port_no; /*!< port number */
str port_no_str; /*!< port number converted to string -- optimization*/
enum si_flags flags; /*!< SI_IS_IP | SI_IS_LO | SI_IS_MCAST | SI_IS_ANYCAST */
union sockaddr_union su;
int proto; /*!< tcp or udp*/
str sock_str;
str adv_sock_str;
str tag_sock_str;
str adv_name_str; /* Advertised name of this interface */
str adv_port_str; /* Advertised port of this interface */
struct ip_addr adv_address; /* Advertised address in ip_addr form (for find_si) */
unsigned short adv_port; /* optimization for grep_sock_info() */
unsigned short workers;
} socket_info_t;

/*
typedef struct socket_info
{
int socket;
int gindex;
str name;
struct ip_addr address;
str address_str;
str port_no_str;
enum si_flags flags;
union sockaddr_union su;
struct socket_info *next;
struct socket_info *prev;
unsigned short port_no; /* port number */
char proto; /* tcp or udp*/
char proto_pad0; /* padding field */
short proto_pad1; /* padding field */
str sock_str; /* Socket proto, ip, and port as string */
struct addr_info *addr_info_lst; /* extra addresses (e.g. SCTP mh) */
int workers; /* number of worker processes for this socket */
int workers_tcpidx; /* index of workers in tcp children array */
str sockname; /* socket name given in config listen value */
struct advertise_info useinfo; /* details to be used in SIP msg */
#ifdef USE_MCAST
str mcast; /* name of interface that should join multicast group*/
#endif /* USE_MCAST */
unsigned short port_no;
char proto;
char proto_pad0;
short proto_pad1;
str sock_str;
struct addr_info *addr_info_lst;
int workers;
int workers_tcpidx;
str sockname;
struct advertise_info useinfo;
} socket_info_t;
*/

typedef struct dest_info
{
Expand Down
19 changes: 15 additions & 4 deletions kern/opensips_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct data_t
const char *buf;
u64 len;
const void *rcvinfo;
const void *socketun;
char comm[TASK_COMM_LEN];
s8 retval; // dispatch_command return value
};
Expand Down Expand Up @@ -142,7 +143,7 @@ static __inline struct sip_data_event_t *create_sip_data_event(u64 current_pid_t

static int process_sip_data(struct pt_regs *ctx, u64 id, enum sip_data_event_type type, struct data_t *data)
{
char fmt2[] = "TIMESTAMP: %lld\n";
char fmt2[] = "TIMESTAMP2: %lld\n";
bpf_trace_printk(fmt2, sizeof(fmt2), data->timestamp);

if (data->len < 0)
Expand All @@ -169,7 +170,8 @@ static int process_sip_data(struct pt_regs *ctx, u64 id, enum sip_data_event_typ
else
{
dest_info_t dest;
bpf_probe_read(&dest, sizeof(dest_info_t), data->rcvinfo);
bpf_probe_read(&dest.to, sizeof(union sockaddr_union), data->socketun);
//bpf_probe_read(&dest.send_sock, sizeof(struct socket_info), data->rcvinfo);

u16 dst_port = su_getport(&dest.to);
char fmt1[] = "PORT DST: %d\n";
Expand All @@ -178,7 +180,7 @@ static int process_sip_data(struct pt_regs *ctx, u64 id, enum sip_data_event_typ
event->rcinfo.dst_port = dst_port;
su2ip_addr(&event->rcinfo.dst_ip, &dest.to);

data->rcvinfo = dest.send_sock;
//data->rcvinfo = &dest.send_sock;

u32 kZero = 0;
struct socket_info *send_sock = bpf_map_lookup_elem(&socket_info_heap, &kZero);
Expand Down Expand Up @@ -299,9 +301,14 @@ SEC("uprobe/msg_send_udp")
int msg_send_udp(struct pt_regs *ctx)
{
// int run_onsend(sip_msg_t *orig_msg, dest_info_t *dst, char *buf, int len)
// debug_bpf_printk("opensips ======================================= d\n");
//debug_bpf_printk("opensips ======================================= d\n");
// static inline int msg_send_buffer(struct dest_info *dst, char *buf, int len, int flags)
// int udp_send(struct dest_info* dst, char *buf, unsigned len);
//OPENSIPS
//static int proto_udp_send(struct socket_info* source,
// char* buf, unsigned int len, union sockaddr_union* to,
// unsigned int id)


u64 timestamp = bpf_ktime_get_ns();
u64 current_pid_tgid = bpf_get_current_pid_tgid();
Expand Down Expand Up @@ -342,9 +349,13 @@ int msg_send_udp(struct pt_regs *ctx)
const char *buf = (const char *)PT_REGS_PARM2(ctx);
data.buf = buf;

const void *socketun = (void *)PT_REGS_PARM4(ctx);
data.socketun = socketun;

const void *rcvinfo = (void *)PT_REGS_PARM1(ctx);
data.rcvinfo = rcvinfo;


bpf_map_update_elem(&sip_hash_send, &current_pid_tgid, &data, BPF_ANY);

// struct socket_info send_sock = {};
Expand Down

0 comments on commit 0ae5a8e

Please sign in to comment.