Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libpcap Add support for VLAN (IEEE 802.1Q) frames.



details:   https://anonhg.NetBSD.org/src/rev/61a76b60023b
branches:  trunk
changeset: 540597:61a76b60023b
user:      hannken <hannken%NetBSD.org@localhost>
date:      Thu Dec 19 16:33:47 2002 +0000

description:
Add support for VLAN (IEEE 802.1Q) frames.
Built after libpcap-0.7 from tcpdump-3.7.1.

Approved by: Jason R. Thorpe <thorpej%netbsd.org@localhost>

diffstat:

 lib/libpcap/ethertype.h |   5 +++-
 lib/libpcap/gencode.c   |  56 +++++++++++++++++++++++++++++++++++++++++++++++-
 lib/libpcap/gencode.h   |   3 +-
 lib/libpcap/grammar.y   |   7 ++++-
 lib/libpcap/scanner.l   |   5 ++-
 5 files changed, 68 insertions(+), 8 deletions(-)

diffs (187 lines):

diff -r 1778a4b5fa3e -r 61a76b60023b lib/libpcap/ethertype.h
--- a/lib/libpcap/ethertype.h   Thu Dec 19 16:31:38 2002 +0000
+++ b/lib/libpcap/ethertype.h   Thu Dec 19 16:33:47 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ethertype.h,v 1.5 2000/04/14 14:26:35 itojun Exp $     */
+/*     $NetBSD: ethertype.h,v 1.6 2002/12/19 16:33:47 hannken Exp $    */
 
 /*
  * Copyright (c) 1993, 1994, 1996
@@ -73,6 +73,9 @@
 #ifndef ETHERTYPE_AARP
 #define ETHERTYPE_AARP         0x80f3
 #endif
+#ifndef ETHERTYPE_8021Q
+#define ETHERTYPE_8021Q                0x8100
+#endif
 #ifndef ETHERTYPE_IPV6
 #define ETHERTYPE_IPV6         0x80f3
 #endif
diff -r 1778a4b5fa3e -r 61a76b60023b lib/libpcap/gencode.c
--- a/lib/libpcap/gencode.c     Thu Dec 19 16:31:38 2002 +0000
+++ b/lib/libpcap/gencode.c     Thu Dec 19 16:33:47 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gencode.c,v 1.32 2002/09/22 16:13:01 thorpej Exp $     */
+/*     $NetBSD: gencode.c,v 1.33 2002/12/19 16:33:47 hannken Exp $     */
 
 /*
  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
@@ -26,7 +26,7 @@
 static const char rcsid[] =
     "@(#) Header: gencode.c,v 1.93 97/06/12 14:22:47 leres Exp  (LBL)";
 #else
-__RCSID("$NetBSD: gencode.c,v 1.32 2002/09/22 16:13:01 thorpej Exp $");
+__RCSID("$NetBSD: gencode.c,v 1.33 2002/12/19 16:33:47 hannken Exp $");
 #endif
 #endif
 
@@ -80,6 +80,10 @@
 static jmp_buf top_ctx;
 static pcap_t *bpf_pcap;
 
+/* Hack for updating VLAN offsets. */
+static u_int   orig_linktype = (u_int)-1;
+static u_int   orig_nl = (u_int)-1;
+
 /* XXX */
 #ifdef PCAP_FDDIPAD
 int    pcap_fddipad = PCAP_FDDIPAD;
@@ -547,6 +551,8 @@
        int type;
 {
        linktype = type;
+       orig_linktype = -1;
+       orig_nl = -1;
 
        if (DLT_IS_RAWAF(type)) {
                off_linktype = -1;
@@ -3039,3 +3045,49 @@
        abort();
        /* NOTREACHED */
 }
+
+/*
+ * support IEEE 802.1Q VLAN trunk over ethernet
+ */
+struct block *
+gen_vlan(vlan_num)
+       int vlan_num;
+{
+       struct  block   *b0;
+
+       /*
+        * Change the offsets to point to the type and data fields within
+        * the VLAN packet.  This is somewhat of a kludge.
+        */
+       if (orig_nl == (u_int)-1) {
+               orig_linktype = off_linktype;   /* save original values */
+               orig_nl = off_nl;
+
+               switch (linktype) {
+
+               case DLT_EN10MB:
+                       off_linktype = 16;
+                       off_nl = 18;
+                       break;
+
+               default:
+                       bpf_error("no VLAN support for data link type %d",
+                                 linktype);
+                       /*NOTREACHED*/
+               }
+       }
+
+       /* check for VLAN */
+       b0 = gen_cmp(orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q);
+
+       /* If a specific VLAN is requested, check VLAN id */
+       if (vlan_num >= 0) {
+               struct block *b1;
+
+               b1 = gen_cmp(orig_nl, BPF_H, (bpf_int32)vlan_num);
+               gen_and(b0, b1);
+               b0 = b1;
+       }
+
+       return (b0);
+}
diff -r 1778a4b5fa3e -r 61a76b60023b lib/libpcap/gencode.h
--- a/lib/libpcap/gencode.h     Thu Dec 19 16:31:38 2002 +0000
+++ b/lib/libpcap/gencode.h     Thu Dec 19 16:33:47 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gencode.h,v 1.11 2001/01/19 09:02:40 kleink Exp $      */
+/*     $NetBSD: gencode.h,v 1.12 2002/12/19 16:33:48 hannken Exp $     */
 
 /*
  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
@@ -179,6 +179,7 @@
 struct block *gen_broadcast(int);
 struct block *gen_multicast(int);
 struct block *gen_inbound(int);
+struct block *gen_vlan(int);
 
 void bpf_optimize(struct block **);
 #if __STDC__
diff -r 1778a4b5fa3e -r 61a76b60023b lib/libpcap/grammar.y
--- a/lib/libpcap/grammar.y     Thu Dec 19 16:31:38 2002 +0000
+++ b/lib/libpcap/grammar.y     Thu Dec 19 16:33:47 2002 +0000
@@ -1,5 +1,5 @@
 %{
-/*     $NetBSD: grammar.y,v 1.8 2001/01/19 09:02:40 kleink Exp $       */
+/*     $NetBSD: grammar.y,v 1.9 2002/12/19 16:33:48 hannken Exp $      */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
@@ -28,7 +28,7 @@
 static const char rcsid[] =
     "@(#) Header: grammar.y,v 1.56 96/11/02 21:54:55 leres Exp  (LBL)";
 #else
-__RCSID("$NetBSD: grammar.y,v 1.8 2001/01/19 09:02:40 kleink Exp $");
+__RCSID("$NetBSD: grammar.y,v 1.9 2002/12/19 16:33:48 hannken Exp $");
 #endif
 #endif
 
@@ -124,6 +124,7 @@
 %token LSH RSH
 %token  LEN
 %token  IPV6 ICMPV6 AH ESP
+%token VLAN
 
 %type  <s> ID
 %type  <e> EID
@@ -282,6 +283,8 @@
        | BYTE NUM byteop NUM   { $$ = gen_byteop($3, $2, $4); }
        | INBOUND               { $$ = gen_inbound(0); }
        | OUTBOUND              { $$ = gen_inbound(1); }
+       | VLAN pnum             { $$ = gen_vlan($2); }
+       | VLAN                  { $$ = gen_vlan(-1); }
        ;
 relop:   '>'                   { $$ = BPF_JGT; }
        | GEQ                   { $$ = BPF_JGE; }
diff -r 1778a4b5fa3e -r 61a76b60023b lib/libpcap/scanner.l
--- a/lib/libpcap/scanner.l     Thu Dec 19 16:31:38 2002 +0000
+++ b/lib/libpcap/scanner.l     Thu Dec 19 16:33:47 2002 +0000
@@ -1,5 +1,5 @@
 %{
-/*     $NetBSD: scanner.l,v 1.14 2001/01/19 09:02:40 kleink Exp $      */
+/*     $NetBSD: scanner.l,v 1.15 2002/12/19 16:33:48 hannken Exp $     */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
@@ -28,7 +28,7 @@
 static const char rcsid[] =
     "@(#) Header: scanner.l,v 1.56 97/07/21 13:31:50 leres Exp  (LBL)";
 #else
-__RCSID("$NetBSD: scanner.l,v 1.14 2001/01/19 09:02:40 kleink Exp $");
+__RCSID("$NetBSD: scanner.l,v 1.15 2002/12/19 16:33:48 hannken Exp $");
 #endif
 #endif
 
@@ -229,6 +229,7 @@
 len|length     return LEN;
 inbound                return INBOUND;
 outbound       return OUTBOUND;
+vlan           return VLAN;
 
 [ \n\t]                        ;
 [+\-*/:\[\]!<>()&|=]   return yytext[0];



Home | Main Index | Thread Index | Old Index