From ec76742caa03344ae755a34caf49f87004590b7a Mon Sep 17 00:00:00 2001 From: Eric Leblond Date: Mon, 30 Jul 2012 12:12:34 +0200 Subject: [PATCH] af-packet: reorder socket operation. This patch moves raw socket binding at the end of init code to avoid to have a flow of packets reaching the socket before we start to read them. The socket creation is now made in the loop function to avoid any timing issue between init function and the call of the loop. --- src/source-af-packet.c | 44 ++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/source-af-packet.c b/src/source-af-packet.c index ac8a45e3ce..a128243262 100644 --- a/src/source-af-packet.c +++ b/src/source-af-packet.c @@ -832,6 +832,7 @@ TmEcode ReceiveAFPLoop(ThreadVars *tv, void *data, void *slot) ptv->afp_state = AFP_STATE_DOWN; continue; case AFP_FAILURE: + ptv->afp_state = AFP_STATE_DOWN; SCReturnInt(TM_ECODE_FAILED); break; case AFP_READ_OK: @@ -1045,23 +1046,6 @@ static int AFPCreateSocket(AFPThreadVars *ptv, char *devname, int verbose) ptv->frame_offset = 0; } - r = bind(ptv->socket, (struct sockaddr *)&bind_address, sizeof(bind_address)); - if (r < 0) { - if (verbose) { - if (errno == ENETDOWN) { - SCLogError(SC_ERR_AFP_CREATE, - "Couldn't bind AF_PACKET socket, iface %s is down", - devname); - } else { - SCLogError(SC_ERR_AFP_CREATE, - "Couldn't bind AF_PACKET socket to iface %s, error %s", - devname, - strerror(errno)); - } - } - close(ptv->socket); - return -1; - } if (ptv->promisc != 0) { /* Force promiscuous mode */ memset(&sock_params, 0, sizeof(sock_params)); @@ -1071,8 +1055,7 @@ static int AFPCreateSocket(AFPThreadVars *ptv, char *devname, int verbose) if (r < 0) { SCLogError(SC_ERR_AFP_CREATE, "Couldn't switch iface %s to promiscuous, error %s", - devname, - strerror(errno)); + devname, strerror(errno)); close(ptv->socket); return -1; } @@ -1099,14 +1082,29 @@ static int AFPCreateSocket(AFPThreadVars *ptv, char *devname, int verbose) sizeof(ptv->buffer_size)) == -1) { SCLogError(SC_ERR_AFP_CREATE, "Couldn't set buffer size to %d on iface %s, error %s", - ptv->buffer_size, - devname, - strerror(errno)); + ptv->buffer_size, devname, strerror(errno)); close(ptv->socket); return -1; } } + r = bind(ptv->socket, (struct sockaddr *)&bind_address, sizeof(bind_address)); + if (r < 0) { + if (verbose) { + if (errno == ENETDOWN) { + SCLogError(SC_ERR_AFP_CREATE, + "Couldn't bind AF_PACKET socket, iface %s is down", + devname); + } else { + SCLogError(SC_ERR_AFP_CREATE, + "Couldn't bind AF_PACKET socket to iface %s, error %s", + devname, strerror(errno)); + } + } + close(ptv->socket); + return -1; + } + #ifdef HAVE_PACKET_FANOUT /* add binded socket to fanout group */ if (ptv->threads > 1) { @@ -1292,6 +1290,7 @@ TmEcode ReceiveAFPThreadInit(ThreadVars *tv, void *initdata, void **data) { SCReturnInt(TM_ECODE_FAILED); } + ptv->copy_mode = afpconfig->copy_mode; if (ptv->copy_mode != AFP_COPY_MODE_NONE) { strlcpy(ptv->out_iface, afpconfig->out_iface, AFP_IFACE_NAME_LENGTH); @@ -1303,7 +1302,6 @@ TmEcode ReceiveAFPThreadInit(ThreadVars *tv, void *initdata, void **data) { } } - #define T_DATA_SIZE 70000 ptv->data = SCMalloc(T_DATA_SIZE); if (ptv->data == NULL) {