]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
af-packet: reorder socket operation.
authorEric Leblond <eric@regit.org>
Mon, 30 Jul 2012 10:12:34 +0000 (12:12 +0200)
committerVictor Julien <victor@inliniac.net>
Mon, 3 Sep 2012 13:27:38 +0000 (15:27 +0200)
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

index ac8a45e3cec83af404c38dd4f8df48fd95955eed..a128243262c2046ffeae518156a73aa17d4b7dfd 100644 (file)
@@ -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) {