Skip to content

Commit

Permalink
Bind SCTP socket in a specific IP. Fixed #43, fixed #45
Browse files Browse the repository at this point in the history
  • Loading branch information
nhnghia committed Feb 16, 2024
1 parent f62adbe commit bb5d7f8
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 5 deletions.
3 changes: 2 additions & 1 deletion mmt-5greplay.conf
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ forward
default = DROP #default action when packets are not selected/satisfied by any rule
# either FORWARD to forward the packets or DROP to drop the packets

#forward packets to a target using SCTP protocol: MMT will be a SCTP client,
#forward packets to a target using SCTP protocol: 5Greplay will be a SCTP client,
# - it connects to the given "sctp-host" at "sctp-port"
# - the SCTP packets' payload will be sent to the target using this SCTP connection
# target-protocols = { SCTP, UDP, HTTP2}
# target-hosts = { "127.0.0.5", "127.0.0.7", "127.0.0.7" }
# target-ports = { 38412, 2152, 8080 }
bind-ip = "127.0.0.1" #bind sockets on a specific NIC which is given via its IP
}
2 changes: 2 additions & 0 deletions src/engine/configure.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ static inline cfg_t *_load_cfg_from_file(const char *filename) {
CFG_INT("snap-len", 0, CFGF_NONE),
CFG_INT("nb-copies", 1, CFGF_NONE),
CFG_INT("promisc", 0, CFGF_NONE),
CFG_STR("bind-ip", "127.0.0.1", CFGF_NONE),
CFG_INT_CB("default", ACTION_DROP, CFGF_NONE, _conf_parse_forward_default_action),
CFG_STR_LIST("target-protocols", "{}", CFGF_NONE),
CFG_STR_LIST("target-hosts", "{}", CFGF_NONE),
Expand Down Expand Up @@ -265,6 +266,7 @@ static inline forward_packet_conf_t *_parse_forward_packet( cfg_t *cfg ){
ret->nb_copies = _cfg_getint( cfg, "nb-copies", 1, UINT32_MAX, 1 );
ret->promisc = _cfg_getint( cfg, "promisc", 0, 1, 1 );
ret->default_action = cfg_getint( cfg, "default" );
ret->bind_ip = _cfg_get_str(cfg, "bind-ip");

ret->target_size = cfg_size( cfg, "target-protocols");
ASSERT( ret->target_size == cfg_size( cfg, "target-hosts"), "Number of elements in target-protocols and target-hosts are different");
Expand Down
1 change: 1 addition & 0 deletions src/engine/configure.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ typedef struct forward_packet_conf_struct{
uint16_t promisc;
uint32_t nb_copies;
forward_action_t default_action;
char *bind_ip;

forward_packet_target_conf_t *targets;
uint16_t target_size;
Expand Down
2 changes: 2 additions & 0 deletions src/engine/configure_override.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ DECLARE_CONF_ATT(
(CONF_ATT__FORWARD__SNAP_LEN, "forward.snap-len", &conf->forward->snap_len, UINT16_T),
(CONF_ATT__FORWARD__NB_COPIES, "forward.nb-copies", &conf->forward->nb_copies, UINT32_T),
(CONF_ATT__FORWARD__DEFAULT, "forward.default", &conf->forward->default_action, UINT32_T),
(CONF_ATT__FORWARD__BIND_IP, "forward.bind-ip", &conf->forward->bind_ip, CHAR_STAR),

(CONF_ATT__FORWARD__TARGET_PROTOCOLS, "forward.target-protocols", &conf->forward->targets, LIST),
(CONF_ATT__FORWARD__TARGET_HOSTS, "forward.target-hosts", &conf->forward->targets, LIST),
(CONF_ATT__FORWARD__TARGET_PORTS, "forward.target-ports", &conf->forward->targets, LIST),
Expand Down
2 changes: 1 addition & 1 deletion src/forward/proto/inject_proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ inject_proto_context_t* inject_proto_alloc( const config_t *config ){
target = & conf->targets[i];
switch( target->protocol ){
case FORWARD_PACKET_PROTO_SCTP:
context->sctp = inject_sctp_alloc(target, conf->nb_copies );
context->sctp = inject_sctp_alloc(target, conf->nb_copies, config->forward->bind_ip );
break;
case FORWARD_PACKET_PROTO_UDP:
context->udp = inject_udp_alloc(target, conf->nb_copies );
Expand Down
28 changes: 27 additions & 1 deletion src/forward/proto/inject_sctp.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ struct inject_sctp_context_struct{
const char* host;
uint16_t port;

const char *bind_host;
uint16_t bind_port;

// the parameters to pass to sctp_sendmsg function to send sctp packets
// see more: https://linux.die.net/man/3/sctp_sendmsg
sctp_param_t sctp_param;
Expand All @@ -57,9 +60,29 @@ void _sctp_connect( inject_sctp_context_t *context ){
.sin_addr.s_addr = inet_addr( context->host ),
};

struct sockaddr_in bind_addr = {
.sin_family = AF_INET
};

conn_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
ASSERT( conn_fd >= 0, "Cannot create SCTP socket" );

//bind to a specific NIC
if( context->bind_host ){
bind_addr.sin_port = htons( context->bind_port );
bind_addr.sin_addr.s_addr = inet_addr( context->bind_host );
ret = sctp_bindx( conn_fd, (struct sockaddr *) &bind_addr,
1, //number of addresses
SCTP_BINDX_ADD_ADDR //to associate additional addresses
);
if( ret )
log_write( LOG_WARNING, "Cannot bind socket to %s:%d using SCTP: %s. Ignore it.",
context->bind_host, context->bind_port, strerror(errno) );
else
log_write( LOG_INFO, "Binded successfully socket to %s:%d using SCTP.",
context->bind_host, context->bind_port );
}

opt = 1;
ret = setsockopt(conn_fd, IPPROTO_SCTP, SCTP_NODELAY, &opt, sizeof(opt));
ASSERT( ret >= 0, "Cannot set SCTP_NODELAY to SCTP socket" );
Expand All @@ -77,14 +100,17 @@ void _sctp_connect( inject_sctp_context_t *context ){
context->shown_error = false;
}

inject_sctp_context_t* inject_sctp_alloc( const forward_packet_target_conf_t *conf, uint32_t nb_copies ){
inject_sctp_context_t* inject_sctp_alloc( const forward_packet_target_conf_t *conf, uint32_t nb_copies, const char *bind_host ){


inject_sctp_context_t *context = mmt_mem_alloc_and_init_zero( sizeof( struct inject_sctp_context_struct ));
context->host = conf->host;
context->port = conf->port;
context->nb_copies = nb_copies;
context->total_sent_pkt = 0;
context->bind_host = bind_host;
context->bind_port = 0; //auto

//sctp parameters to send packets
inject_sctp_update_param( context,
60, //payload protocol id of NGAP protcol
Expand Down
3 changes: 1 addition & 2 deletions src/forward/proto/inject_sctp.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@

typedef struct inject_sctp_context_struct inject_sctp_context_t;

inject_sctp_context_t* inject_sctp_alloc( const forward_packet_target_conf_t *conf, uint32_t nb_copies );
inject_sctp_context_t* inject_sctp_alloc( const forward_packet_target_conf_t *conf, uint32_t nb_copies, const char *bind_host );
int inject_sctp_send_packet( inject_sctp_context_t *context, const uint8_t *packet_data, uint16_t packet_size );
void inject_sctp_release( inject_sctp_context_t *context );

void inject_sctp_update_param( inject_sctp_context_t *injector_context, uint32_t ppid, uint32_t flags, uint16_t stream_no, uint32_t timetolive, uint32_t ctx );

#endif /* SRC_MODULES_SECURITY_FORWARD_PROTO_INJECT_SCTP_H_ */

0 comments on commit bb5d7f8

Please sign in to comment.