From 9417652b46c648e0b4f10a1bf82ad9fb2ec27889 Mon Sep 17 00:00:00 2001 From: hannken Date: Thu, 19 Dec 2002 16:33:47 +0000 Subject: [PATCH] Add support for VLAN (IEEE 802.1Q) frames. Built after libpcap-0.7 from tcpdump-3.7.1. Approved by: Jason R. Thorpe --- 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(-) diff --git a/lib/libpcap/ethertype.h b/lib/libpcap/ethertype.h index 4e89508f1d53..487b66db79c3 100644 --- a/lib/libpcap/ethertype.h +++ b/lib/libpcap/ethertype.h @@ -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 --git a/lib/libpcap/gencode.c b/lib/libpcap/gencode.c index 2867dd99d621..12379613cbf6 100644 --- a/lib/libpcap/gencode.c +++ b/lib/libpcap/gencode.c @@ -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 @@ struct rtentry; 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 @@ init_linktype(type) int type; { linktype = type; + orig_linktype = -1; + orig_nl = -1; if (DLT_IS_RAWAF(type)) { off_linktype = -1; @@ -3039,3 +3045,49 @@ gen_ahostop(eaddr, dir) 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 --git a/lib/libpcap/gencode.h b/lib/libpcap/gencode.h index 51cdb8e76201..78b990be7047 100644 --- a/lib/libpcap/gencode.h +++ b/lib/libpcap/gencode.h @@ -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_byteop(int, int, int); 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 --git a/lib/libpcap/grammar.y b/lib/libpcap/grammar.y index 13ed0122b8ed..86fdcb5fedb6 100644 --- a/lib/libpcap/grammar.y +++ b/lib/libpcap/grammar.y @@ -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 @@ pcap_parse() %token LSH RSH %token LEN %token IPV6 ICMPV6 AH ESP +%token VLAN %type ID %type EID @@ -282,6 +283,8 @@ other: pqual TK_BROADCAST { $$ = gen_broadcast($1); } | 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 --git a/lib/libpcap/scanner.l b/lib/libpcap/scanner.l index 474d638eb589..58c3d559f302 100644 --- a/lib/libpcap/scanner.l +++ b/lib/libpcap/scanner.l @@ -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 @@ not return '!'; len|length return LEN; inbound return INBOUND; outbound return OUTBOUND; +vlan return VLAN; [ \n\t] ; [+\-*/:\[\]!<>()&|=] return yytext[0];