Blame SOURCES/0005-Move-the-socket-ops-out-of-gencode.c.patch

2cd29a
From 06553e55d6ff2b203c4ab1dda2d6fb15c45e2896 Mon Sep 17 00:00:00 2001
2cd29a
From: Guy Harris <guy@alum.mit.edu>
2cd29a
Date: Wed, 5 Nov 2014 14:33:14 -0800
2cd29a
Subject: [PATCH 5/5] Move the socket ops out of gencode.c.
2cd29a
2cd29a
Instead, do it in pcap-linux.c, and have it set a flag in the pcap_t
2cd29a
structure to indicate to the code in gencode.c that it needs to generate
2cd29a
special VLAN-handling code.
2cd29a
2cd29a
(cherry picked from 612503bb0801a49a1c6ebe9c82591289b1d2e5c9)
2cd29a
2cd29a
Conflicts:
2cd29a
        pcap-linux.c
2cd29a
---
2cd29a
 gencode.c    | 37 ++++++++++++++-----------------------
2cd29a
 pcap-int.h   | 10 ++++++++++
2cd29a
 pcap-linux.c | 24 ++++++++++++++++++++++++
2cd29a
 pcap.c       | 12 ++++++++++++
2cd29a
 savefile.c   |  5 +++++
2cd29a
 5 files changed, 65 insertions(+), 23 deletions(-)
2cd29a
2cd29a
diff --git a/gencode.c b/gencode.c
2cd29a
index 9c1d17b..6b3772f 100644
2cd29a
--- a/gencode.c
2cd29a
+++ b/gencode.c
2cd29a
@@ -58,7 +58,6 @@ static const char rcsid[] _U_ =
2cd29a
 
2cd29a
 #include <netinet/in.h>
2cd29a
 #include <arpa/inet.h>
2cd29a
-#include <errno.h>
2cd29a
 
2cd29a
 #endif /* WIN32 */
2cd29a
 
2cd29a
@@ -7864,23 +7863,11 @@ gen_ahostop(eaddr, dir)
2cd29a
 }
2cd29a
 
2cd29a
 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
2cd29a
-static int skf_ad_vlan_tag_present_supported(int bpf_extensions) {
2cd29a
-        return bpf_extensions >= SKF_AD_VLAN_TAG_PRESENT;
2cd29a
-}
2cd29a
-
2cd29a
 static struct block *
2cd29a
-gen_vlan_bpf_extensions(int vlan_num) {
2cd29a
+gen_vlan_bpf_extensions(int vlan_num)
2cd29a
+{
2cd29a
         struct block *b0, *b1;
2cd29a
         struct slist *s;
2cd29a
-        int val = 0, len, r;
2cd29a
-
2cd29a
-        len = sizeof(val);
2cd29a
-        r = getsockopt(bpf_pcap->fd, SOL_SOCKET, SO_BPF_EXTENSIONS, &val, &len;;
2cd29a
-        if (r < 0)
2cd29a
-                return NULL;
2cd29a
-
2cd29a
-        if (!skf_ad_vlan_tag_present_supported(val))
2cd29a
-                return NULL;
2cd29a
 
2cd29a
         /* generate new filter code based on extracting packet
2cd29a
          * metadata */
2cd29a
@@ -7908,7 +7895,8 @@ gen_vlan_bpf_extensions(int vlan_num) {
2cd29a
 #endif
2cd29a
 
2cd29a
 static struct block *
2cd29a
-gen_vlan_no_bpf_extensions(int vlan_num) {
2cd29a
+gen_vlan_no_bpf_extensions(int vlan_num)
2cd29a
+{
2cd29a
         struct block *b0, *b1;
2cd29a
 
2cd29a
         /* check for VLAN, including QinQ */
2cd29a
@@ -7985,14 +7973,17 @@ gen_vlan(vlan_num)
2cd29a
 	case DLT_NETANALYZER:
2cd29a
 	case DLT_NETANALYZER_TRANSPARENT:
2cd29a
 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
2cd29a
-                if (!vlan_stack_depth) {
2cd29a
-                        b0 = gen_vlan_bpf_extensions(vlan_num);
2cd29a
-                        if (!b0)
2cd29a
-                                b0 = gen_vlan_no_bpf_extensions(vlan_num);
2cd29a
-                }
2cd29a
-                else
2cd29a
+		if (vlan_stack_depth == 0) {
2cd29a
+			/*
2cd29a
+			 * Do we need special VLAN handling?
2cd29a
+			 */
2cd29a
+			if (bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING)
2cd29a
+				b0 = gen_vlan_bpf_extensions(vlan_num);
2cd29a
+			else
2cd29a
+				b0 = gen_vlan_no_bpf_extensions(vlan_num);
2cd29a
+		} else
2cd29a
 #endif
2cd29a
-                        b0 = gen_vlan_no_bpf_extensions(vlan_num);
2cd29a
+			b0 = gen_vlan_no_bpf_extensions(vlan_num);
2cd29a
                 break;
2cd29a
 	default:
2cd29a
 		bpf_error("no VLAN support for data link type %d",
2cd29a
diff --git a/pcap-int.h b/pcap-int.h
2cd29a
index 0c27ec7..c9dbb5f 100644
2cd29a
--- a/pcap-int.h
2cd29a
+++ b/pcap-int.h
2cd29a
@@ -182,6 +182,11 @@ struct pcap {
2cd29a
 	pcap_direction_t direction;
2cd29a
 
2cd29a
 	/*
2cd29a
+	 * Flags to affect BPF code generation.
2cd29a
+	 */
2cd29a
+	int bpf_codegen_flags;
2cd29a
+
2cd29a
+	/*
2cd29a
 	 * Placeholder for filter code if bpf not in kernel.
2cd29a
 	 */
2cd29a
 	struct bpf_program fcode;
2cd29a
@@ -228,6 +233,11 @@ struct pcap {
2cd29a
 };
2cd29a
 
2cd29a
 /*
2cd29a
+ * BPF code generation flags.
2cd29a
+ */
2cd29a
+#define BPF_SPECIAL_VLAN_HANDLING	0x00000001	/* special VLAN handling for Linux */
2cd29a
+
2cd29a
+/*
2cd29a
  * This is a timeval as stored in a savefile.
2cd29a
  * It has to use the same types everywhere, independent of the actual
2cd29a
  * `struct timeval'; `struct timeval' has 32-bit tv_sec values on some
2cd29a
diff --git a/pcap-linux.c b/pcap-linux.c
2cd29a
index a370858..a82f4eb 100644
2cd29a
--- a/pcap-linux.c
2cd29a
+++ b/pcap-linux.c
2cd29a
@@ -3024,6 +3024,10 @@ activate_new(pcap_t *handle)
2cd29a
 #endif
2cd29a
 	int			err = 0;
2cd29a
 	struct packet_mreq	mr;
2cd29a
+#ifdef SO_BPF_EXTENSIONS
2cd29a
+	int			bpf_extensions;
2cd29a
+	socklen_t		len;
2cd29a
+#endif
2cd29a
 
2cd29a
 	/*
2cd29a
 	 * Open a socket with protocol family packet. If the
2cd29a
@@ -3342,6 +3346,26 @@ activate_new(pcap_t *handle)
2cd29a
 	/* Save the socket FD in the pcap structure */
2cd29a
 	handle->fd = sock_fd;
2cd29a
 
2cd29a
+#ifdef SO_BPF_EXTENSIONS
2cd29a
+	/*
2cd29a
+	 * Can we generate special code for VLAN checks?
2cd29a
+	 * (XXX - what if we need the special code but it's not supported
2cd29a
+	 * by the OS?  Is that possible?)
2cd29a
+	 */
2cd29a
+	len = sizeof(bpf_extensions);
2cd29a
+
2cd29a
+	if (getsockopt(sock_fd, SOL_SOCKET, SO_BPF_EXTENSIONS, &bpf_extensions, &len) == 0) {
2cd29a
+		if (bpf_extensions >= SKF_AD_VLAN_TAG_PRESENT) {
2cd29a
+			/*
2cd29a
+			 * Yes, we can.  Request that we do so.
2cd29a
+			 */
2cd29a
+			handle->bpf_codegen_flags |= BPF_SPECIAL_VLAN_HANDLING;
2cd29a
+		}
2cd29a
+	}
2cd29a
+#endif /* SO_BPF_EXTENSIONS */
2cd29a
+
2cd29a
+
2cd29a
+
2cd29a
 #if defined(SIOCGSTAMPNS) && defined(SO_TIMESTAMPNS)
2cd29a
 	if (handle->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO) {
2cd29a
 		int nsec_tstamps = 1;
2cd29a
diff --git a/pcap.c b/pcap.c
2cd29a
index 6b16cea..74dc708 100644
2cd29a
--- a/pcap.c
2cd29a
+++ b/pcap.c
2cd29a
@@ -558,6 +558,12 @@ pcap_create_common(const char *source, char *ebuf, size_t size)
2cd29a
 	p->opt.immediate = 0;
2cd29a
 	p->opt.tstamp_type = -1;	/* default to not setting time stamp type */
2cd29a
 	p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
2cd29a
+
2cd29a
+	/*
2cd29a
+	 * Start out with no BPF code generation flags set.
2cd29a
+	 */
2cd29a
+	p->bpf_codegen_flags = 0;
2cd29a
+
2cd29a
 	return (p);
2cd29a
 }
2cd29a
 
2cd29a
@@ -1810,6 +1816,12 @@ pcap_open_dead_with_tstamp_precision(int linktype, int snaplen, u_int precision)
2cd29a
 	p->setmintocopy_op = pcap_setmintocopy_dead;
2cd29a
 #endif
2cd29a
 	p->cleanup_op = pcap_cleanup_dead;
2cd29a
+
2cd29a
+	/*
2cd29a
+	 * A "dead" pcap_t never requires special BPF code generation.
2cd29a
+	 */
2cd29a
+	p->bpf_codegen_flags = 0;
2cd29a
+
2cd29a
 	p->activated = 1;
2cd29a
 	return (p);
2cd29a
 }
2cd29a
diff --git a/savefile.c b/savefile.c
2cd29a
index 73e3ea9..98f9c82 100644
2cd29a
--- a/savefile.c
2cd29a
+++ b/savefile.c
2cd29a
@@ -349,6 +349,11 @@ found:
2cd29a
 	 */
2cd29a
 	p->oneshot_callback = pcap_oneshot;
2cd29a
 
2cd29a
+	/*
2cd29a
+	 * Savefiles never require special BPF code generation.
2cd29a
+	 */
2cd29a
+	p->bpf_codegen_flags = 0;
2cd29a
+
2cd29a
 	p->activated = 1;
2cd29a
 
2cd29a
 	return (p);
2cd29a
-- 
2cd29a
2.4.3
2cd29a