]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid
[] =
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.143 2001-01-14 07:57:47 guy Exp $ (LBL)";
31 #include <sys/types.h>
32 #include <sys/socket.h>
35 #include <sys/param.h>
42 #include <netinet/in.h>
52 #include "ethertype.h"
57 #include <pcap-namedb.h>
60 #include <sys/socket.h>
65 * Note that these fit in one byte, and are thus less than 1500, and
66 * are thus distinguishable from ETHERTYPE_ values, so we can use them
67 * as protocol types values.
69 #define LLC_SNAP_LSAP 0xaa
70 #define LLC_ISO_LSAP 0xfe
71 #define LLC_STP_LSAP 0x42
72 #define LLC_IPX_LSAP 0xe0
76 #ifdef HAVE_OS_PROTO_H
80 #define JMP(c) ((c)|BPF_JMP|BPF_K)
83 static jmp_buf top_ctx
;
84 static pcap_t
*bpf_pcap
;
88 int pcap_fddipad
= PCAP_FDDIPAD
;
95 bpf_error(const char *fmt
, ...)
101 if (bpf_pcap
!= NULL
)
102 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
109 static void init_linktype(int);
111 static int alloc_reg(void);
112 static void free_reg(int);
114 static struct block
*root
;
117 * We divy out chunks of memory rather than call malloc each time so
118 * we don't have to worry about leaking memory. It's probably
119 * not a big deal if all this memory was wasted but it this ever
120 * goes into a library that would probably not be a good idea.
123 #define CHUNK0SIZE 1024
129 static struct chunk chunks
[NCHUNKS
];
130 static int cur_chunk
;
132 static void *newchunk(u_int
);
133 static void freechunks(void);
134 static inline struct block
*new_block(int);
135 static inline struct slist
*new_stmt(int);
136 static struct block
*gen_retblk(int);
137 static inline void syntax(void);
139 static void backpatch(struct block
*, struct block
*);
140 static void merge(struct block
*, struct block
*);
141 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
142 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
143 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
144 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
145 static struct block
*gen_uncond(int);
146 static inline struct block
*gen_true(void);
147 static inline struct block
*gen_false(void);
148 static struct block
*gen_linktype(int);
149 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
150 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
152 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
154 static struct block
*gen_ehostop(const u_char
*, int);
155 static struct block
*gen_fhostop(const u_char
*, int);
156 static struct block
*gen_thostop(const u_char
*, int);
157 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
158 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
160 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
163 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
165 static struct block
*gen_ipfrag(void);
166 static struct block
*gen_portatom(int, bpf_int32
);
168 static struct block
*gen_portatom6(int, bpf_int32
);
170 struct block
*gen_portop(int, int, int);
171 static struct block
*gen_port(int, int, int);
173 struct block
*gen_portop6(int, int, int);
174 static struct block
*gen_port6(int, int, int);
176 static int lookup_proto(const char *, int);
177 static struct block
*gen_protochain(int, int, int);
178 static struct block
*gen_proto(int, int, int);
179 static struct slist
*xfer_to_x(struct arth
*);
180 static struct slist
*xfer_to_a(struct arth
*);
181 static struct block
*gen_len(int, int);
191 /* XXX Round up to nearest long. */
192 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
194 /* XXX Round up to structure boundary. */
198 cp
= &chunks
[cur_chunk
];
199 if (n
> cp
->n_left
) {
200 ++cp
, k
= ++cur_chunk
;
202 bpf_error("out of memory");
203 size
= CHUNK0SIZE
<< k
;
204 cp
->m
= (void *)malloc(size
);
205 memset((char *)cp
->m
, 0, size
);
208 bpf_error("out of memory");
211 return (void *)((char *)cp
->m
+ cp
->n_left
);
220 for (i
= 0; i
< NCHUNKS
; ++i
)
221 if (chunks
[i
].m
!= NULL
) {
228 * A strdup whose allocations are freed after code generation is over.
232 register const char *s
;
234 int n
= strlen(s
) + 1;
235 char *cp
= newchunk(n
);
241 static inline struct block
*
247 p
= (struct block
*)newchunk(sizeof(*p
));
254 static inline struct slist
*
260 p
= (struct slist
*)newchunk(sizeof(*p
));
266 static struct block
*
270 struct block
*b
= new_block(BPF_RET
|BPF_K
);
279 bpf_error("syntax error in filter expression");
282 static bpf_u_int32 netmask
;
287 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
288 char *buf
, int optimize
, bpf_u_int32 mask
)
297 if (setjmp(top_ctx
)) {
305 snaplen
= pcap_snapshot(p
);
307 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
308 "snaplen of 0 rejects all packets");
312 lex_init(buf
? buf
: "");
313 init_linktype(pcap_datalink(p
));
320 root
= gen_retblk(snaplen
);
322 if (optimize
&& !no_optimize
) {
325 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
326 bpf_error("expression rejects all packets");
328 program
->bf_insns
= icode_to_fcode(root
, &len
);
329 program
->bf_len
= len
;
337 * entry point for using the compiler with no pcap open
338 * pass in all the stuff that is needed explicitly instead.
341 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
342 struct bpf_program
*program
,
343 char *buf
, int optimize
, bpf_u_int32 mask
)
348 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
351 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
357 * Clean up a "struct bpf_program" by freeing all the memory allocated
361 pcap_freecode(struct bpf_program
*program
)
364 if (program
->bf_insns
!= NULL
) {
365 free((char *)program
->bf_insns
);
366 program
->bf_insns
= NULL
;
371 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
372 * which of the jt and jf fields has been resolved and which is a pointer
373 * back to another unresolved block (or nil). At least one of the fields
374 * in each block is already resolved.
377 backpatch(list
, target
)
378 struct block
*list
, *target
;
395 * Merge the lists in b0 and b1, using the 'sense' field to indicate
396 * which of jt and jf is the link.
400 struct block
*b0
, *b1
;
402 register struct block
**p
= &b0
;
404 /* Find end of list. */
406 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
408 /* Concatenate the lists. */
416 backpatch(p
, gen_retblk(snaplen
));
417 p
->sense
= !p
->sense
;
418 backpatch(p
, gen_retblk(0));
424 struct block
*b0
, *b1
;
426 backpatch(b0
, b1
->head
);
427 b0
->sense
= !b0
->sense
;
428 b1
->sense
= !b1
->sense
;
430 b1
->sense
= !b1
->sense
;
436 struct block
*b0
, *b1
;
438 b0
->sense
= !b0
->sense
;
439 backpatch(b0
, b1
->head
);
440 b0
->sense
= !b0
->sense
;
449 b
->sense
= !b
->sense
;
452 static struct block
*
453 gen_cmp(offset
, size
, v
)
460 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
463 b
= new_block(JMP(BPF_JEQ
));
470 static struct block
*
471 gen_cmp_gt(offset
, size
, v
)
478 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
481 b
= new_block(JMP(BPF_JGT
));
488 static struct block
*
489 gen_mcmp(offset
, size
, v
, mask
)
494 struct block
*b
= gen_cmp(offset
, size
, v
);
497 if (mask
!= 0xffffffff) {
498 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
505 static struct block
*
506 gen_bcmp(offset
, size
, v
)
507 register u_int offset
, size
;
508 register const u_char
*v
;
510 register struct block
*b
, *tmp
;
514 register const u_char
*p
= &v
[size
- 4];
515 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
516 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
518 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
525 register const u_char
*p
= &v
[size
- 2];
526 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
528 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
535 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
544 * Various code constructs need to know the layout of the data link
545 * layer. These variables give the necessary offsets. off_linktype
546 * is set to -1 for no encapsulation, in which case, IP is assumed.
548 static u_int off_linktype
;
567 * SLIP doesn't have a link level type. The 16 byte
568 * header is hacked into our SLIP driver.
575 /* XXX this may be the same as the DLT_PPP_BSDOS case */
601 * FDDI doesn't really have a link-level type field.
602 * We set "off_linktype" to the offset of the LLC header.
604 * To check for Ethernet types, we assume that SSAP = SNAP
605 * is being used and pick out the encapsulated Ethernet type.
606 * XXX - should we generate code to check for SNAP?
610 off_linktype
+= pcap_fddipad
;
614 off_nl
+= pcap_fddipad
;
620 * Token Ring doesn't really have a link-level type field.
621 * We set "off_linktype" to the offset of the LLC header.
623 * To check for Ethernet types, we assume that SSAP = SNAP
624 * is being used and pick out the encapsulated Ethernet type.
625 * XXX - should we generate code to check for SNAP?
627 * XXX - the header is actually variable-length.
628 * Some various Linux patched versions gave 38
629 * as "off_linktype" and 40 as "off_nl"; however,
630 * if a token ring packet has *no* routing
631 * information, i.e. is not source-routed, the correct
632 * values are 20 and 22, as they are in the vanilla code.
634 * A packet is source-routed iff the uppermost bit
635 * of the first byte of the source address, at an
636 * offset of 8, has the uppermost bit set. If the
637 * packet is source-routed, the total number of bytes
638 * of routing information is 2 plus bits 0x1F00 of
639 * the 16-bit value at an offset of 14 (shifted right
640 * 8 - figure out which byte that is).
646 case DLT_ATM_RFC1483
:
648 * assume routed, non-ISO PDUs
649 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
660 case DLT_ATM_CLIP
: /* Linux ATM defines this */
665 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
670 bpf_error("unknown data link type %d", linktype
);
674 static struct block
*
681 s
= new_stmt(BPF_LD
|BPF_IMM
);
683 b
= new_block(JMP(BPF_JEQ
));
689 static inline struct block
*
692 return gen_uncond(1);
695 static inline struct block
*
698 return gen_uncond(0);
702 * Byte-swap a 32-bit number.
703 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
704 * big-endian platforms.)
706 #define SWAPLONG(y) \
707 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
709 static struct block
*
713 struct block
*b0
, *b1
;
722 * OSI protocols always use 802.2 encapsulation.
723 * XXX - should we check both the DSAP and the
724 * SSAP, like this, or should we check just the
727 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
729 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
730 ((LLC_ISO_LSAP
<< 8) | LLC_ISO_LSAP
));
736 * Check both for the IPX LSAP as the DSAP and
737 * for Netware 802.3, where the type/length
738 * field is a length field (i.e., <= ETHERMTU)
739 * and the first two bytes after the LLC header
742 * XXX - check for the IPX Ethertype, 0x8137,
745 * This generates code to check both for the
746 * IPX LSAP and for Netware 802.3.
748 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
749 (bpf_int32
)LLC_IPX_LSAP
);
750 b1
= gen_cmp(off_linktype
+ 2, BPF_H
,
755 * Now we generate code to check for 802.3
758 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
762 * Now check for 802.3 frames and, if that passes,
763 * check for either of the flavors of IPX.
768 case ETHERTYPE_ATALK
:
771 * EtherTalk (AppleTalk protocols on Ethernet link
772 * layer) may use 802.2 encapsulation.
776 * Check for 802.2 encapsulation (EtherTalk phase 2?);
777 * we check for an Ethernet type field less than
778 * 1500, which means it's an 802.3 length field.
780 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
784 * 802.2-encapsulated ETHERTYPE_ATALK packets are
785 * SNAP packets with an organization code of
786 * 0x080007 (Apple, for Appletalk) and a protocol
787 * type of ETHERTYPE_ATALK (Appletalk).
789 * 802.2-encapsulated ETHERTYPE_AARP packets are
790 * SNAP packets with an organization code of
791 * 0x000000 (encapsulated Ethernet) and a protocol
792 * type of ETHERTYPE_AARP (Appletalk ARP).
794 if (proto
== ETHERTYPE_ATALK
)
795 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
796 else /* proto == ETHERTYPE_AARP */
797 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
801 * Check for Ethernet encapsulation (Ethertalk
802 * phase 1?); we just check for the Ethernet
805 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
811 if (proto
<= ETHERMTU
) {
813 * This is an LLC SAP value, so the frames
814 * that match would be 802.2 frames.
815 * Check that the frame is an 802.2 frame
816 * (i.e., that the length/type field is
817 * a length field, <= ETHERMTU) and
818 * then check the DSAP.
820 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
822 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
828 * This is an Ethernet type, so compare
829 * the length/type field with it (if
830 * the frame is an 802.2 frame, the length
831 * field will be <= ETHERMTU, and, as
832 * "proto" is > ETHERMTU, this test
833 * will fail and the frame won't match,
834 * which is what we want).
836 return gen_cmp(off_linktype
, BPF_H
,
844 case DLT_ATM_RFC1483
:
847 * XXX - handle token-ring variable-length header.
852 return gen_cmp(off_linktype
, BPF_H
, (long)
853 ((LLC_ISO_LSAP
<< 8) | LLC_ISO_LSAP
));
855 case ETHERTYPE_ATALK
:
857 * 802.2-encapsulated ETHERTYPE_ATALK packets are
858 * SNAP packets with an organization code of
859 * 0x080007 (Apple, for Appletalk) and a protocol
860 * type of ETHERTYPE_ATALK (Appletalk).
862 * XXX - check for an organization code of
863 * encapsulated Ethernet as well?
865 return gen_snap(0x080007, ETHERTYPE_ATALK
,
871 * XXX - we don't have to check for IPX 802.3
872 * here, but should we check for the IPX Ethertype?
874 if (proto
<= ETHERMTU
) {
876 * This is an LLC SAP value, so check
879 return gen_cmp(off_linktype
, BPF_B
,
883 * This is an Ethernet type; we assume
884 * that it's unlikely that it'll
885 * appear in the right place at random,
886 * and therefore check only the
887 * location that would hold the Ethernet
888 * type in a SNAP frame with an organization
889 * code of 0x000000 (encapsulated Ethernet).
891 * XXX - if we were to check for the SNAP DSAP
892 * and LSAP, as per XXX, and were also to check
893 * for an organization code of 0x000000
894 * (encapsulated Ethernet), we'd do
896 * return gen_snap(0x000000, proto,
899 * here; for now, we don't, as per the above.
900 * I don't know whether it's worth the
901 * extra CPU time to do the right check
904 return gen_cmp(off_linktype
+6, BPF_H
,
915 * OSI protocols always use 802.2 encapsulation.
916 * XXX - should we check both the DSAP and the
917 * LSAP, like this, or should we check just the
920 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
921 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
922 ((LLC_ISO_LSAP
<< 8) | LLC_ISO_LSAP
));
928 * Check both for 802.2 frames with the IPX LSAP as
929 * the DSAP and for Netware 802.3 frames.
931 * This generates code to check for 802.2 frames
934 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
935 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
936 (bpf_int32
)LLC_IPX_LSAP
);
940 * Now check for 802.3 frames and OR that with
943 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
947 case ETHERTYPE_ATALK
:
950 * EtherTalk (AppleTalk protocols on Ethernet link
951 * layer) may use 802.2 encapsulation.
955 * Check for 802.2 encapsulation (EtherTalk phase 2?);
956 * we check for the 802.2 protocol type in the
957 * "Ethernet type" field.
959 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
962 * 802.2-encapsulated ETHERTYPE_ATALK packets are
963 * SNAP packets with an organization code of
964 * 0x080007 (Apple, for Appletalk) and a protocol
965 * type of ETHERTYPE_ATALK (Appletalk).
967 * 802.2-encapsulated ETHERTYPE_AARP packets are
968 * SNAP packets with an organization code of
969 * 0x000000 (encapsulated Ethernet) and a protocol
970 * type of ETHERTYPE_AARP (Appletalk ARP).
972 if (proto
== ETHERTYPE_ATALK
)
973 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
975 else /* proto == ETHERTYPE_AARP */
976 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
981 * Check for Ethernet encapsulation (Ethertalk
982 * phase 1?); we just check for the Ethernet
985 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
991 if (proto
<= ETHERMTU
) {
993 * This is an LLC SAP value, so the frames
994 * that match would be 802.2 frames.
995 * Check for the 802.2 protocol type
996 * in the "Ethernet type" field, and
997 * then check the DSAP.
999 b0
= gen_cmp(off_linktype
, BPF_H
,
1001 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1007 * This is an Ethernet type, so compare
1008 * the length/type field with it (if
1009 * the frame is an 802.2 frame, the length
1010 * field will be <= ETHERMTU, and, as
1011 * "proto" is > ETHERMTU, this test
1012 * will fail and the frame won't match,
1013 * which is what we want).
1015 return gen_cmp(off_linktype
, BPF_H
,
1022 case DLT_SLIP_BSDOS
:
1025 * These types don't provide any type field; packets
1028 * XXX - for IPv4, check for a version number of 4, and,
1029 * for IPv6, check for a version number of 6?
1035 case ETHERTYPE_IPV6
:
1037 return gen_true(); /* always true */
1040 return gen_false(); /* always false */
1045 case DLT_PPP_SERIAL
:
1047 * We use Ethernet protocol types inside libpcap;
1048 * map them to the corresponding PPP protocol types.
1053 proto
= PPP_IP
; /* XXX was 0x21 */
1057 case ETHERTYPE_IPV6
:
1066 case ETHERTYPE_ATALK
:
1082 * We use Ethernet protocol types inside libpcap;
1083 * map them to the corresponding PPP protocol types.
1088 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1089 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1091 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1096 case ETHERTYPE_IPV6
:
1106 case ETHERTYPE_ATALK
:
1123 * For DLT_NULL, the link-layer header is a 32-bit
1124 * word containing an AF_ value in *host* byte order.
1126 * In addition, if we're reading a saved capture file,
1127 * the host byte order in the capture may not be the
1128 * same as the host byte order on this machine.
1130 * For DLT_LOOP, the link-layer header is a 32-bit
1131 * word containing an AF_ value in *network* byte order.
1133 * XXX - AF_ values may, unfortunately, be platform-
1134 * dependent; for example, FreeBSD's AF_INET6 is 24
1135 * whilst NetBSD's and OpenBSD's is 26.
1137 * This means that, when reading a capture file, just
1138 * checking for our AF_INET6 value won't work if the
1139 * capture file came from another OS.
1148 case ETHERTYPE_IPV6
:
1155 * Not a type on which we support filtering.
1156 * XXX - support those that have AF_ values
1157 * #defined on this platform, at least?
1162 if (linktype
== DLT_NULL
) {
1164 * The AF_ value is in host byte order, but
1165 * the BPF interpreter will convert it to
1166 * network byte order.
1168 * If this is a save file, and it's from a
1169 * machine with the opposite byte order to
1170 * ours, we byte-swap the AF_ value.
1172 * Then we run it through "htonl()", and
1173 * generate code to compare against the result.
1175 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1176 bpf_pcap
->sf
.swapped
)
1177 proto
= SWAPLONG(proto
);
1178 proto
= htonl(proto
);
1180 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1184 * All the types that have no encapsulation should either be
1185 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1186 * all packets are IP packets, or should be handled in some
1187 * special case, if none of them are (if some are and some
1188 * aren't, the lack of encapsulation is a problem, as we'd
1189 * have to find some other way of determining the packet type).
1191 * Therefore, if "off_linktype" is -1, there's an error.
1193 if (off_linktype
== -1)
1197 * Any type not handled above should always have an Ethernet
1198 * type at an offset of "off_linktype".
1200 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1204 * Check for an LLC SNAP packet with a given organization code and
1205 * protocol type; we check the entire contents of the 802.2 LLC and
1206 * snap headers, checking for DSAP and SSAP of SNAP and a control
1207 * field of 0x03 in the LLC header, and for the specified organization
1208 * code and protocol type in the SNAP header.
1210 static struct block
*
1211 gen_snap(orgcode
, ptype
, offset
)
1212 bpf_u_int32 orgcode
;
1216 u_char snapblock
[8];
1218 snapblock
[0] = LLC_SNAP_LSAP
; /* DSAP = SNAP */
1219 snapblock
[1] = LLC_SNAP_LSAP
; /* SSAP = SNAP */
1220 snapblock
[2] = 0x03; /* control = UI */
1221 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1222 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1223 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1224 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1225 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1226 return gen_bcmp(offset
, 8, snapblock
);
1229 static struct block
*
1230 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1234 u_int src_off
, dst_off
;
1236 struct block
*b0
, *b1
;
1250 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1251 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1257 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1258 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1265 b0
= gen_linktype(proto
);
1266 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1272 static struct block
*
1273 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1274 struct in6_addr
*addr
;
1275 struct in6_addr
*mask
;
1277 u_int src_off
, dst_off
;
1279 struct block
*b0
, *b1
;
1294 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1295 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1301 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1302 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1309 /* this order is important */
1310 a
= (u_int32_t
*)addr
;
1311 m
= (u_int32_t
*)mask
;
1312 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1313 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1315 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1317 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1319 b0
= gen_linktype(proto
);
1325 static struct block
*
1326 gen_ehostop(eaddr
, dir
)
1327 register const u_char
*eaddr
;
1330 register struct block
*b0
, *b1
;
1334 return gen_bcmp(6, 6, eaddr
);
1337 return gen_bcmp(0, 6, eaddr
);
1340 b0
= gen_ehostop(eaddr
, Q_SRC
);
1341 b1
= gen_ehostop(eaddr
, Q_DST
);
1347 b0
= gen_ehostop(eaddr
, Q_SRC
);
1348 b1
= gen_ehostop(eaddr
, Q_DST
);
1357 * Like gen_ehostop, but for DLT_FDDI
1359 static struct block
*
1360 gen_fhostop(eaddr
, dir
)
1361 register const u_char
*eaddr
;
1364 struct block
*b0
, *b1
;
1369 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1371 return gen_bcmp(6 + 1, 6, eaddr
);
1376 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1378 return gen_bcmp(0 + 1, 6, eaddr
);
1382 b0
= gen_fhostop(eaddr
, Q_SRC
);
1383 b1
= gen_fhostop(eaddr
, Q_DST
);
1389 b0
= gen_fhostop(eaddr
, Q_SRC
);
1390 b1
= gen_fhostop(eaddr
, Q_DST
);
1399 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1401 static struct block
*
1402 gen_thostop(eaddr
, dir
)
1403 register const u_char
*eaddr
;
1406 register struct block
*b0
, *b1
;
1410 return gen_bcmp(8, 6, eaddr
);
1413 return gen_bcmp(2, 6, eaddr
);
1416 b0
= gen_thostop(eaddr
, Q_SRC
);
1417 b1
= gen_thostop(eaddr
, Q_DST
);
1423 b0
= gen_thostop(eaddr
, Q_SRC
);
1424 b1
= gen_thostop(eaddr
, Q_DST
);
1433 * This is quite tricky because there may be pad bytes in front of the
1434 * DECNET header, and then there are two possible data packet formats that
1435 * carry both src and dst addresses, plus 5 packet types in a format that
1436 * carries only the src node, plus 2 types that use a different format and
1437 * also carry just the src node.
1441 * Instead of doing those all right, we just look for data packets with
1442 * 0 or 1 bytes of padding. If you want to look at other packets, that
1443 * will require a lot more hacking.
1445 * To add support for filtering on DECNET "areas" (network numbers)
1446 * one would want to add a "mask" argument to this routine. That would
1447 * make the filter even more inefficient, although one could be clever
1448 * and not generate masking instructions if the mask is 0xFFFF.
1450 static struct block
*
1451 gen_dnhostop(addr
, dir
, base_off
)
1456 struct block
*b0
, *b1
, *b2
, *tmp
;
1457 u_int offset_lh
; /* offset if long header is received */
1458 u_int offset_sh
; /* offset if short header is received */
1463 offset_sh
= 1; /* follows flags */
1464 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1468 offset_sh
= 3; /* follows flags, dstnode */
1469 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1473 /* Inefficient because we do our Calvinball dance twice */
1474 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1475 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1481 /* Inefficient because we do our Calvinball dance twice */
1482 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1483 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1488 bpf_error("ISO host filtering not implemented");
1493 b0
= gen_linktype(ETHERTYPE_DN
);
1494 /* Check for pad = 1, long header case */
1495 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1496 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1497 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1498 BPF_H
, (bpf_int32
)ntohs(addr
));
1500 /* Check for pad = 0, long header case */
1501 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1502 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1505 /* Check for pad = 1, short header case */
1506 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1507 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1508 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1509 BPF_H
, (bpf_int32
)ntohs(addr
));
1512 /* Check for pad = 0, short header case */
1513 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1514 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1518 /* Combine with test for linktype */
1523 static struct block
*
1524 gen_host(addr
, mask
, proto
, dir
)
1530 struct block
*b0
, *b1
;
1535 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1536 if (off_linktype
!= -1) {
1537 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1539 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1545 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1546 off_nl
+ 12, off_nl
+ 16);
1549 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1550 off_nl
+ 14, off_nl
+ 24);
1553 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1554 off_nl
+ 14, off_nl
+ 24);
1557 bpf_error("'tcp' modifier applied to host");
1560 bpf_error("'udp' modifier applied to host");
1563 bpf_error("'icmp' modifier applied to host");
1566 bpf_error("'igmp' modifier applied to host");
1569 bpf_error("'igrp' modifier applied to host");
1572 bpf_error("'pim' modifier applied to host");
1575 bpf_error("ATALK host filtering not implemented");
1578 bpf_error("AARP host filtering not implemented");
1581 return gen_dnhostop(addr
, dir
, off_nl
);
1584 bpf_error("SCA host filtering not implemented");
1587 bpf_error("LAT host filtering not implemented");
1590 bpf_error("MOPDL host filtering not implemented");
1593 bpf_error("MOPRC host filtering not implemented");
1597 bpf_error("'ip6' modifier applied to ip host");
1600 bpf_error("'icmp6' modifier applied to host");
1604 bpf_error("'ah' modifier applied to host");
1607 bpf_error("'esp' modifier applied to host");
1610 bpf_error("ISO host filtering not implemented");
1613 bpf_error("'esis' modifier applied to host");
1616 bpf_error("'isis' modifier applied to host");
1619 bpf_error("'clnp' modifier applied to host");
1622 bpf_error("'stp' modifier applied to host");
1625 bpf_error("IPX host filtering not implemented");
1634 static struct block
*
1635 gen_host6(addr
, mask
, proto
, dir
)
1636 struct in6_addr
*addr
;
1637 struct in6_addr
*mask
;
1644 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1647 bpf_error("'ip' modifier applied to ip6 host");
1650 bpf_error("'rarp' modifier applied to ip6 host");
1653 bpf_error("'arp' modifier applied to ip6 host");
1656 bpf_error("'tcp' modifier applied to host");
1659 bpf_error("'udp' modifier applied to host");
1662 bpf_error("'icmp' modifier applied to host");
1665 bpf_error("'igmp' modifier applied to host");
1668 bpf_error("'igrp' modifier applied to host");
1671 bpf_error("'pim' modifier applied to host");
1674 bpf_error("ATALK host filtering not implemented");
1677 bpf_error("AARP host filtering not implemented");
1680 bpf_error("'decnet' modifier applied to ip6 host");
1683 bpf_error("SCA host filtering not implemented");
1686 bpf_error("LAT host filtering not implemented");
1689 bpf_error("MOPDL host filtering not implemented");
1692 bpf_error("MOPRC host filtering not implemented");
1695 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1696 off_nl
+ 8, off_nl
+ 24);
1699 bpf_error("'icmp6' modifier applied to host");
1702 bpf_error("'ah' modifier applied to host");
1705 bpf_error("'esp' modifier applied to host");
1708 bpf_error("ISO host filtering not implemented");
1711 bpf_error("'esis' modifier applied to host");
1714 bpf_error("'isis' modifier applied to host");
1717 bpf_error("'clnp' modifier applied to host");
1720 bpf_error("'stp' modifier applied to host");
1723 bpf_error("IPX host filtering not implemented");
1733 static struct block
*
1734 gen_gateway(eaddr
, alist
, proto
, dir
)
1735 const u_char
*eaddr
;
1736 bpf_u_int32
**alist
;
1740 struct block
*b0
, *b1
, *tmp
;
1743 bpf_error("direction applied to 'gateway'");
1750 if (linktype
== DLT_EN10MB
)
1751 b0
= gen_ehostop(eaddr
, Q_OR
);
1752 else if (linktype
== DLT_FDDI
)
1753 b0
= gen_fhostop(eaddr
, Q_OR
);
1754 else if (linktype
== DLT_IEEE802
)
1755 b0
= gen_thostop(eaddr
, Q_OR
);
1758 "'gateway' supported only on ethernet, FDDI or token ring");
1760 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1762 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1770 bpf_error("illegal modifier of 'gateway'");
1776 gen_proto_abbrev(proto
)
1787 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1789 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1795 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1797 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1803 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1806 #ifndef IPPROTO_IGMP
1807 #define IPPROTO_IGMP 2
1811 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1814 #ifndef IPPROTO_IGRP
1815 #define IPPROTO_IGRP 9
1818 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1822 #define IPPROTO_PIM 103
1826 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1828 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1834 b1
= gen_linktype(ETHERTYPE_IP
);
1838 b1
= gen_linktype(ETHERTYPE_ARP
);
1842 b1
= gen_linktype(ETHERTYPE_REVARP
);
1846 bpf_error("link layer applied in wrong context");
1849 b1
= gen_linktype(ETHERTYPE_ATALK
);
1853 b1
= gen_linktype(ETHERTYPE_AARP
);
1857 b1
= gen_linktype(ETHERTYPE_DN
);
1861 b1
= gen_linktype(ETHERTYPE_SCA
);
1865 b1
= gen_linktype(ETHERTYPE_LAT
);
1869 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1873 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1878 b1
= gen_linktype(ETHERTYPE_IPV6
);
1881 #ifndef IPPROTO_ICMPV6
1882 #define IPPROTO_ICMPV6 58
1885 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1890 #define IPPROTO_AH 51
1893 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1895 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1901 #define IPPROTO_ESP 50
1904 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1906 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1912 b1
= gen_linktype(LLC_ISO_LSAP
);
1916 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
1920 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
1924 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
1928 b1
= gen_linktype(LLC_STP_LSAP
);
1932 b1
= gen_linktype(LLC_IPX_LSAP
);
1941 static struct block
*
1948 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1949 s
->s
.k
= off_nl
+ 6;
1950 b
= new_block(JMP(BPF_JSET
));
1958 static struct block
*
1959 gen_portatom(off
, v
)
1966 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1969 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1970 s
->next
->s
.k
= off_nl
+ off
;
1972 b
= new_block(JMP(BPF_JEQ
));
1980 static struct block
*
1981 gen_portatom6(off
, v
)
1985 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1990 gen_portop(port
, proto
, dir
)
1991 int port
, proto
, dir
;
1993 struct block
*b0
, *b1
, *tmp
;
1995 /* ip proto 'proto' */
1996 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2002 b1
= gen_portatom(0, (bpf_int32
)port
);
2006 b1
= gen_portatom(2, (bpf_int32
)port
);
2011 tmp
= gen_portatom(0, (bpf_int32
)port
);
2012 b1
= gen_portatom(2, (bpf_int32
)port
);
2017 tmp
= gen_portatom(0, (bpf_int32
)port
);
2018 b1
= gen_portatom(2, (bpf_int32
)port
);
2030 static struct block
*
2031 gen_port(port
, ip_proto
, dir
)
2036 struct block
*b0
, *b1
, *tmp
;
2038 /* ether proto ip */
2039 b0
= gen_linktype(ETHERTYPE_IP
);
2044 b1
= gen_portop(port
, ip_proto
, dir
);
2048 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2049 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2062 gen_portop6(port
, proto
, dir
)
2063 int port
, proto
, dir
;
2065 struct block
*b0
, *b1
, *tmp
;
2067 /* ip proto 'proto' */
2068 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2072 b1
= gen_portatom6(0, (bpf_int32
)port
);
2076 b1
= gen_portatom6(2, (bpf_int32
)port
);
2081 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2082 b1
= gen_portatom6(2, (bpf_int32
)port
);
2087 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2088 b1
= gen_portatom6(2, (bpf_int32
)port
);
2100 static struct block
*
2101 gen_port6(port
, ip_proto
, dir
)
2106 struct block
*b0
, *b1
, *tmp
;
2108 /* ether proto ip */
2109 b0
= gen_linktype(ETHERTYPE_IPV6
);
2114 b1
= gen_portop6(port
, ip_proto
, dir
);
2118 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2119 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2132 lookup_proto(name
, proto
)
2133 register const char *name
;
2142 v
= pcap_nametoproto(name
);
2143 if (v
== PROTO_UNDEF
)
2144 bpf_error("unknown ip proto '%s'", name
);
2148 /* XXX should look up h/w protocol type based on linktype */
2149 v
= pcap_nametoeproto(name
);
2150 if (v
== PROTO_UNDEF
)
2151 bpf_error("unknown ether proto '%s'", name
);
2155 if (strcmp(name
, "esis") == 0)
2157 else if (strcmp(name
, "isis") == 0)
2159 else if (strcmp(name
, "clnp") == 0)
2162 bpf_error("unknown osi proto '%s'", name
);
2182 static struct block
*
2183 gen_protochain(v
, proto
, dir
)
2188 #ifdef NO_PROTOCHAIN
2189 return gen_proto(v
, proto
, dir
);
2191 struct block
*b0
, *b
;
2192 struct slist
*s
[100];
2193 int fix2
, fix3
, fix4
, fix5
;
2194 int ahcheck
, again
, end
;
2196 int reg2
= alloc_reg();
2198 memset(s
, 0, sizeof(s
));
2199 fix2
= fix3
= fix4
= fix5
= 0;
2206 b0
= gen_protochain(v
, Q_IP
, dir
);
2207 b
= gen_protochain(v
, Q_IPV6
, dir
);
2211 bpf_error("bad protocol applied for 'protochain'");
2215 no_optimize
= 1; /*this code is not compatible with optimzer yet */
2218 * s[0] is a dummy entry to protect other BPF insn from damaged
2219 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
2220 * hard to find interdependency made by jump table fixup.
2223 s
[i
] = new_stmt(0); /*dummy*/
2228 b0
= gen_linktype(ETHERTYPE_IP
);
2231 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2232 s
[i
]->s
.k
= off_nl
+ 9;
2234 /* X = ip->ip_hl << 2 */
2235 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2241 b0
= gen_linktype(ETHERTYPE_IPV6
);
2243 /* A = ip6->ip_nxt */
2244 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2245 s
[i
]->s
.k
= off_nl
+ 6;
2247 /* X = sizeof(struct ip6_hdr) */
2248 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
2254 bpf_error("unsupported proto to gen_protochain");
2258 /* again: if (A == v) goto end; else fall through; */
2260 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2262 s
[i
]->s
.jt
= NULL
; /*later*/
2263 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2267 #ifndef IPPROTO_NONE
2268 #define IPPROTO_NONE 59
2270 /* if (A == IPPROTO_NONE) goto end */
2271 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2272 s
[i
]->s
.jt
= NULL
; /*later*/
2273 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2274 s
[i
]->s
.k
= IPPROTO_NONE
;
2275 s
[fix5
]->s
.jf
= s
[i
];
2280 if (proto
== Q_IPV6
) {
2281 int v6start
, v6end
, v6advance
, j
;
2284 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
2285 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2286 s
[i
]->s
.jt
= NULL
; /*later*/
2287 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2288 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
2289 s
[fix2
]->s
.jf
= s
[i
];
2291 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
2292 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2293 s
[i
]->s
.jt
= NULL
; /*later*/
2294 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2295 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
2297 /* if (A == IPPROTO_ROUTING) goto v6advance */
2298 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2299 s
[i
]->s
.jt
= NULL
; /*later*/
2300 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2301 s
[i
]->s
.k
= IPPROTO_ROUTING
;
2303 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
2304 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2305 s
[i
]->s
.jt
= NULL
; /*later*/
2306 s
[i
]->s
.jf
= NULL
; /*later*/
2307 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
2318 * X = X + (P[X + 1] + 1) * 8;
2321 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2323 /* A = P[X + packet head] */
2324 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2328 s
[i
] = new_stmt(BPF_ST
);
2332 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2335 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2339 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2341 /* A = P[X + packet head]; */
2342 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2346 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2350 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2354 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2357 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2361 /* goto again; (must use BPF_JA for backward jump) */
2362 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2363 s
[i
]->s
.k
= again
- i
- 1;
2364 s
[i
- 1]->s
.jf
= s
[i
];
2368 for (j
= v6start
; j
<= v6end
; j
++)
2369 s
[j
]->s
.jt
= s
[v6advance
];
2374 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2376 s
[fix2
]->s
.jf
= s
[i
];
2382 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2383 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2384 s
[i
]->s
.jt
= NULL
; /*later*/
2385 s
[i
]->s
.jf
= NULL
; /*later*/
2386 s
[i
]->s
.k
= IPPROTO_AH
;
2388 s
[fix3
]->s
.jf
= s
[ahcheck
];
2395 * X = X + (P[X + 1] + 2) * 4;
2398 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2400 /* A = P[X + packet head]; */
2401 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2405 s
[i
] = new_stmt(BPF_ST
);
2409 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2412 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2416 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2418 /* A = P[X + packet head] */
2419 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2423 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2427 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2431 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2434 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2438 /* goto again; (must use BPF_JA for backward jump) */
2439 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2440 s
[i
]->s
.k
= again
- i
- 1;
2445 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2447 s
[fix2
]->s
.jt
= s
[end
];
2448 s
[fix4
]->s
.jf
= s
[end
];
2449 s
[fix5
]->s
.jt
= s
[end
];
2456 for (i
= 0; i
< max
- 1; i
++)
2457 s
[i
]->next
= s
[i
+ 1];
2458 s
[max
- 1]->next
= NULL
;
2463 b
= new_block(JMP(BPF_JEQ
));
2464 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2474 static struct block
*
2475 gen_proto(v
, proto
, dir
)
2480 struct block
*b0
, *b1
;
2482 if (dir
!= Q_DEFAULT
)
2483 bpf_error("direction applied to 'proto'");
2488 b0
= gen_proto(v
, Q_IP
, dir
);
2489 b1
= gen_proto(v
, Q_IPV6
, dir
);
2496 b0
= gen_linktype(ETHERTYPE_IP
);
2498 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2500 b1
= gen_protochain(v
, Q_IP
);
2506 b0
= gen_linktype(LLC_ISO_LSAP
);
2507 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2512 bpf_error("arp does not encapsulate another protocol");
2516 bpf_error("rarp does not encapsulate another protocol");
2520 bpf_error("atalk encapsulation is not specifiable");
2524 bpf_error("decnet encapsulation is not specifiable");
2528 bpf_error("sca does not encapsulate another protocol");
2532 bpf_error("lat does not encapsulate another protocol");
2536 bpf_error("moprc does not encapsulate another protocol");
2540 bpf_error("mopdl does not encapsulate another protocol");
2544 return gen_linktype(v
);
2547 bpf_error("'udp proto' is bogus");
2551 bpf_error("'tcp proto' is bogus");
2555 bpf_error("'icmp proto' is bogus");
2559 bpf_error("'igmp proto' is bogus");
2563 bpf_error("'igrp proto' is bogus");
2567 bpf_error("'pim proto' is bogus");
2572 b0
= gen_linktype(ETHERTYPE_IPV6
);
2574 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2576 b1
= gen_protochain(v
, Q_IPV6
);
2582 bpf_error("'icmp6 proto' is bogus");
2586 bpf_error("'ah proto' is bogus");
2589 bpf_error("'ah proto' is bogus");
2592 bpf_error("'stp proto' is bogus");
2595 bpf_error("'ipx proto' is bogus");
2606 register const char *name
;
2609 int proto
= q
.proto
;
2613 bpf_u_int32 mask
, addr
;
2615 bpf_u_int32
**alist
;
2618 struct sockaddr_in
*sin
;
2619 struct sockaddr_in6
*sin6
;
2620 struct addrinfo
*res
, *res0
;
2621 struct in6_addr mask128
;
2623 struct block
*b
, *tmp
;
2624 int port
, real_proto
;
2629 addr
= pcap_nametonetaddr(name
);
2631 bpf_error("unknown network '%s'", name
);
2632 /* Left justify network addr and calculate its network mask */
2634 while (addr
&& (addr
& 0xff000000) == 0) {
2638 return gen_host(addr
, mask
, proto
, dir
);
2642 if (proto
== Q_LINK
) {
2646 eaddr
= pcap_ether_hostton(name
);
2649 "unknown ether host '%s'", name
);
2650 return gen_ehostop(eaddr
, dir
);
2653 eaddr
= pcap_ether_hostton(name
);
2656 "unknown FDDI host '%s'", name
);
2657 return gen_fhostop(eaddr
, dir
);
2660 eaddr
= pcap_ether_hostton(name
);
2663 "unknown token ring host '%s'", name
);
2664 return gen_thostop(eaddr
, dir
);
2668 "only ethernet/FDDI/token ring supports link-level host name");
2671 } else if (proto
== Q_DECNET
) {
2672 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2674 * I don't think DECNET hosts can be multihomed, so
2675 * there is no need to build up a list of addresses
2677 return (gen_host(dn_addr
, 0, proto
, dir
));
2680 alist
= pcap_nametoaddr(name
);
2681 if (alist
== NULL
|| *alist
== NULL
)
2682 bpf_error("unknown host '%s'", name
);
2684 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2686 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2688 tmp
= gen_host(**alist
++, 0xffffffff,
2695 memset(&mask128
, 0xff, sizeof(mask128
));
2696 res0
= res
= pcap_nametoaddrinfo(name
);
2698 bpf_error("unknown host '%s'", name
);
2700 tproto
= tproto6
= proto
;
2701 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2705 for (res
= res0
; res
; res
= res
->ai_next
) {
2706 switch (res
->ai_family
) {
2708 if (tproto
== Q_IPV6
)
2711 sin
= (struct sockaddr_in
*)
2713 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2714 0xffffffff, tproto
, dir
);
2717 if (tproto6
== Q_IP
)
2720 sin6
= (struct sockaddr_in6
*)
2722 tmp
= gen_host6(&sin6
->sin6_addr
,
2723 &mask128
, tproto6
, dir
);
2732 bpf_error("unknown host '%s'%s", name
,
2733 (proto
== Q_DEFAULT
)
2735 : " for specified address family");
2742 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2743 bpf_error("illegal qualifier of 'port'");
2744 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2745 bpf_error("unknown port '%s'", name
);
2746 if (proto
== Q_UDP
) {
2747 if (real_proto
== IPPROTO_TCP
)
2748 bpf_error("port '%s' is tcp", name
);
2750 /* override PROTO_UNDEF */
2751 real_proto
= IPPROTO_UDP
;
2753 if (proto
== Q_TCP
) {
2754 if (real_proto
== IPPROTO_UDP
)
2755 bpf_error("port '%s' is udp", name
);
2757 /* override PROTO_UNDEF */
2758 real_proto
= IPPROTO_TCP
;
2761 return gen_port(port
, real_proto
, dir
);
2765 b
= gen_port(port
, real_proto
, dir
);
2766 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2773 eaddr
= pcap_ether_hostton(name
);
2775 bpf_error("unknown ether host: %s", name
);
2777 alist
= pcap_nametoaddr(name
);
2778 if (alist
== NULL
|| *alist
== NULL
)
2779 bpf_error("unknown host '%s'", name
);
2780 return gen_gateway(eaddr
, alist
, proto
, dir
);
2782 bpf_error("'gateway' not supported in this configuration");
2786 real_proto
= lookup_proto(name
, proto
);
2787 if (real_proto
>= 0)
2788 return gen_proto(real_proto
, proto
, dir
);
2790 bpf_error("unknown protocol: %s", name
);
2793 real_proto
= lookup_proto(name
, proto
);
2794 if (real_proto
>= 0)
2795 return gen_protochain(real_proto
, proto
, dir
);
2797 bpf_error("unknown protocol: %s", name
);
2809 gen_mcode(s1
, s2
, masklen
, q
)
2810 register const char *s1
, *s2
;
2811 register int masklen
;
2814 register int nlen
, mlen
;
2817 nlen
= __pcap_atoin(s1
, &n
);
2818 /* Promote short ipaddr */
2822 mlen
= __pcap_atoin(s2
, &m
);
2823 /* Promote short ipaddr */
2826 bpf_error("non-network bits set in \"%s mask %s\"",
2829 /* Convert mask len to mask */
2831 bpf_error("mask length must be <= 32");
2832 m
= 0xffffffff << (32 - masklen
);
2834 bpf_error("non-network bits set in \"%s/%d\"",
2841 return gen_host(n
, m
, q
.proto
, q
.dir
);
2844 bpf_error("Mask syntax for networks only");
2851 register const char *s
;
2856 int proto
= q
.proto
;
2862 else if (q
.proto
== Q_DECNET
)
2863 vlen
= __pcap_atodn(s
, &v
);
2865 vlen
= __pcap_atoin(s
, &v
);
2872 if (proto
== Q_DECNET
)
2873 return gen_host(v
, 0, proto
, dir
);
2874 else if (proto
== Q_LINK
) {
2875 bpf_error("illegal link layer address");
2878 if (s
== NULL
&& q
.addr
== Q_NET
) {
2879 /* Promote short net number */
2880 while (v
&& (v
& 0xff000000) == 0) {
2885 /* Promote short ipaddr */
2889 return gen_host(v
, mask
, proto
, dir
);
2894 proto
= IPPROTO_UDP
;
2895 else if (proto
== Q_TCP
)
2896 proto
= IPPROTO_TCP
;
2897 else if (proto
== Q_DEFAULT
)
2898 proto
= PROTO_UNDEF
;
2900 bpf_error("illegal qualifier of 'port'");
2903 return gen_port((int)v
, proto
, dir
);
2907 b
= gen_port((int)v
, proto
, dir
);
2908 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2914 bpf_error("'gateway' requires a name");
2918 return gen_proto((int)v
, proto
, dir
);
2921 return gen_protochain((int)v
, proto
, dir
);
2936 gen_mcode6(s1
, s2
, masklen
, q
)
2937 register const char *s1
, *s2
;
2938 register int masklen
;
2941 struct addrinfo
*res
;
2942 struct in6_addr
*addr
;
2943 struct in6_addr mask
;
2948 bpf_error("no mask %s supported", s2
);
2950 res
= pcap_nametoaddrinfo(s1
);
2952 bpf_error("invalid ip6 address %s", s1
);
2954 bpf_error("%s resolved to multiple address", s1
);
2955 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2957 if (sizeof(mask
) * 8 < masklen
)
2958 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2959 memset(&mask
, 0xff, masklen
/ 8);
2961 mask
.s6_addr
[masklen
/ 8] =
2962 (0xff << (8 - masklen
% 8)) & 0xff;
2965 a
= (u_int32_t
*)addr
;
2966 m
= (u_int32_t
*)&mask
;
2967 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2968 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2969 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2977 bpf_error("Mask syntax for networks only");
2981 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2986 bpf_error("invalid qualifier against IPv6 address");
2994 register const u_char
*eaddr
;
2997 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2998 if (linktype
== DLT_EN10MB
)
2999 return gen_ehostop(eaddr
, (int)q
.dir
);
3000 if (linktype
== DLT_FDDI
)
3001 return gen_fhostop(eaddr
, (int)q
.dir
);
3002 if (linktype
== DLT_IEEE802
)
3003 return gen_thostop(eaddr
, (int)q
.dir
);
3004 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
3006 bpf_error("ethernet address used in non-ether expression");
3012 struct slist
*s0
, *s1
;
3015 * This is definitely not the best way to do this, but the
3016 * lists will rarely get long.
3023 static struct slist
*
3029 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3034 static struct slist
*
3040 s
= new_stmt(BPF_LD
|BPF_MEM
);
3046 gen_load(proto
, index
, size
)
3051 struct slist
*s
, *tmp
;
3053 int regno
= alloc_reg();
3055 free_reg(index
->regno
);
3059 bpf_error("data size must be 1, 2, or 4");
3075 bpf_error("unsupported index operation");
3078 s
= xfer_to_x(index
);
3079 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3081 sappend(index
->s
, s
);
3096 /* XXX Note that we assume a fixed link header here. */
3097 s
= xfer_to_x(index
);
3098 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3101 sappend(index
->s
, s
);
3103 b
= gen_proto_abbrev(proto
);
3105 gen_and(index
->b
, b
);
3115 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3117 sappend(s
, xfer_to_a(index
));
3118 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3119 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3120 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3122 sappend(index
->s
, s
);
3124 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
3126 gen_and(index
->b
, b
);
3128 gen_and(gen_proto_abbrev(Q_IP
), b
);
3134 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
3138 index
->regno
= regno
;
3139 s
= new_stmt(BPF_ST
);
3141 sappend(index
->s
, s
);
3147 gen_relation(code
, a0
, a1
, reversed
)
3149 struct arth
*a0
, *a1
;
3152 struct slist
*s0
, *s1
, *s2
;
3153 struct block
*b
, *tmp
;
3157 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
3158 b
= new_block(JMP(code
));
3159 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
3160 reversed
= !reversed
;
3161 b
->s
.k
= 0x80000000;
3169 sappend(a0
->s
, a1
->s
);
3173 free_reg(a0
->regno
);
3174 free_reg(a1
->regno
);
3176 /* 'and' together protocol checks */
3179 gen_and(a0
->b
, tmp
= a1
->b
);
3195 int regno
= alloc_reg();
3196 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
3199 s
= new_stmt(BPF_LD
|BPF_LEN
);
3200 s
->next
= new_stmt(BPF_ST
);
3201 s
->next
->s
.k
= regno
;
3216 a
= (struct arth
*)newchunk(sizeof(*a
));
3220 s
= new_stmt(BPF_LD
|BPF_IMM
);
3222 s
->next
= new_stmt(BPF_ST
);
3238 s
= new_stmt(BPF_ALU
|BPF_NEG
);
3241 s
= new_stmt(BPF_ST
);
3249 gen_arth(code
, a0
, a1
)
3251 struct arth
*a0
, *a1
;
3253 struct slist
*s0
, *s1
, *s2
;
3257 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
3262 sappend(a0
->s
, a1
->s
);
3264 free_reg(a1
->regno
);
3266 s0
= new_stmt(BPF_ST
);
3267 a0
->regno
= s0
->s
.k
= alloc_reg();
3274 * Here we handle simple allocation of the scratch registers.
3275 * If too many registers are alloc'd, the allocator punts.
3277 static int regused
[BPF_MEMWORDS
];
3281 * Return the next free register.
3286 int n
= BPF_MEMWORDS
;
3289 if (regused
[curreg
])
3290 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
3292 regused
[curreg
] = 1;
3296 bpf_error("too many registers needed to evaluate expression");
3301 * Return a register to the table so it can
3311 static struct block
*
3318 s
= new_stmt(BPF_LD
|BPF_LEN
);
3319 b
= new_block(JMP(jmp
));
3330 return gen_len(BPF_JGE
, n
);
3334 * Actually, this is less than or equal.
3342 b
= gen_len(BPF_JGT
, n
);
3349 gen_byteop(op
, idx
, val
)
3360 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3363 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3364 b
->s
.code
= JMP(BPF_JGE
);
3369 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3370 b
->s
.code
= JMP(BPF_JGT
);
3374 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3378 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3382 b
= new_block(JMP(BPF_JEQ
));
3390 gen_broadcast(proto
)
3393 bpf_u_int32 hostmask
;
3394 struct block
*b0
, *b1
, *b2
;
3395 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3401 if (linktype
== DLT_EN10MB
)
3402 return gen_ehostop(ebroadcast
, Q_DST
);
3403 if (linktype
== DLT_FDDI
)
3404 return gen_fhostop(ebroadcast
, Q_DST
);
3405 if (linktype
== DLT_IEEE802
)
3406 return gen_thostop(ebroadcast
, Q_DST
);
3407 bpf_error("not a broadcast link");
3411 b0
= gen_linktype(ETHERTYPE_IP
);
3412 hostmask
= ~netmask
;
3413 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3414 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3415 (bpf_int32
)(~0 & hostmask
), hostmask
);
3420 bpf_error("only ether/ip broadcast filters supported");
3424 gen_multicast(proto
)
3427 register struct block
*b0
, *b1
;
3428 register struct slist
*s
;
3434 if (linktype
== DLT_EN10MB
) {
3435 /* ether[0] & 1 != 0 */
3436 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3438 b0
= new_block(JMP(BPF_JSET
));
3444 if (linktype
== DLT_FDDI
) {
3445 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3446 /* fddi[1] & 1 != 0 */
3447 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3449 b0
= new_block(JMP(BPF_JSET
));
3455 /* TODO - check how token ring handles multicast */
3456 /* if (linktype == DLT_IEEE802) ... */
3458 /* Link not known to support multicasts */
3462 b0
= gen_linktype(ETHERTYPE_IP
);
3463 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3464 b1
->s
.code
= JMP(BPF_JGE
);
3470 b0
= gen_linktype(ETHERTYPE_IPV6
);
3471 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3476 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3480 * generate command for inbound/outbound. It's here so we can
3481 * make it link-type specific. 'dir' = 0 implies "inbound",
3482 * = 1 implies "outbound".
3488 register struct block
*b0
;
3491 * Only some data link types support inbound/outbound qualifiers.
3496 b0
= gen_relation(BPF_JEQ
,
3497 gen_load(Q_LINK
, gen_loadi(0), 1),
3503 bpf_error("inbound/outbound not supported on linktype %d\n",
3512 * support IEEE 802.1Q VLAN trunk over ethernet
3518 static u_int orig_linktype
= -1, orig_nl
= -1;
3522 * Change the offsets to point to the type and data fields within
3523 * the VLAN packet. This is somewhat of a kludge.
3525 if (orig_nl
== (u_int
)-1) {
3526 orig_linktype
= off_linktype
; /* save original values */
3537 bpf_error("no VLAN support for data link type %d",
3543 /* check for VLAN */
3544 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3546 /* If a specific VLAN is requested, check VLAN id */
3547 if (vlan_num
>= 0) {
3550 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);