]>
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.145 2001-01-14 21:26:52 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"
58 #include <pcap-namedb.h>
61 #include <sys/socket.h>
66 #ifdef HAVE_OS_PROTO_H
70 #define JMP(c) ((c)|BPF_JMP|BPF_K)
73 static jmp_buf top_ctx
;
74 static pcap_t
*bpf_pcap
;
78 int pcap_fddipad
= PCAP_FDDIPAD
;
85 bpf_error(const char *fmt
, ...)
92 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
99 static void init_linktype(int);
101 static int alloc_reg(void);
102 static void free_reg(int);
104 static struct block
*root
;
107 * We divy out chunks of memory rather than call malloc each time so
108 * we don't have to worry about leaking memory. It's probably
109 * not a big deal if all this memory was wasted but it this ever
110 * goes into a library that would probably not be a good idea.
113 #define CHUNK0SIZE 1024
119 static struct chunk chunks
[NCHUNKS
];
120 static int cur_chunk
;
122 static void *newchunk(u_int
);
123 static void freechunks(void);
124 static inline struct block
*new_block(int);
125 static inline struct slist
*new_stmt(int);
126 static struct block
*gen_retblk(int);
127 static inline void syntax(void);
129 static void backpatch(struct block
*, struct block
*);
130 static void merge(struct block
*, struct block
*);
131 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
132 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
133 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
134 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
135 static struct block
*gen_uncond(int);
136 static inline struct block
*gen_true(void);
137 static inline struct block
*gen_false(void);
138 static struct block
*gen_linktype(int);
139 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
140 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
142 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
144 static struct block
*gen_ehostop(const u_char
*, int);
145 static struct block
*gen_fhostop(const u_char
*, int);
146 static struct block
*gen_thostop(const u_char
*, int);
147 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
148 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
150 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
153 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
155 static struct block
*gen_ipfrag(void);
156 static struct block
*gen_portatom(int, bpf_int32
);
158 static struct block
*gen_portatom6(int, bpf_int32
);
160 struct block
*gen_portop(int, int, int);
161 static struct block
*gen_port(int, int, int);
163 struct block
*gen_portop6(int, int, int);
164 static struct block
*gen_port6(int, int, int);
166 static int lookup_proto(const char *, int);
167 static struct block
*gen_protochain(int, int, int);
168 static struct block
*gen_proto(int, int, int);
169 static struct slist
*xfer_to_x(struct arth
*);
170 static struct slist
*xfer_to_a(struct arth
*);
171 static struct block
*gen_len(int, int);
181 /* XXX Round up to nearest long. */
182 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
184 /* XXX Round up to structure boundary. */
188 cp
= &chunks
[cur_chunk
];
189 if (n
> cp
->n_left
) {
190 ++cp
, k
= ++cur_chunk
;
192 bpf_error("out of memory");
193 size
= CHUNK0SIZE
<< k
;
194 cp
->m
= (void *)malloc(size
);
195 memset((char *)cp
->m
, 0, size
);
198 bpf_error("out of memory");
201 return (void *)((char *)cp
->m
+ cp
->n_left
);
210 for (i
= 0; i
< NCHUNKS
; ++i
)
211 if (chunks
[i
].m
!= NULL
) {
218 * A strdup whose allocations are freed after code generation is over.
222 register const char *s
;
224 int n
= strlen(s
) + 1;
225 char *cp
= newchunk(n
);
231 static inline struct block
*
237 p
= (struct block
*)newchunk(sizeof(*p
));
244 static inline struct slist
*
250 p
= (struct slist
*)newchunk(sizeof(*p
));
256 static struct block
*
260 struct block
*b
= new_block(BPF_RET
|BPF_K
);
269 bpf_error("syntax error in filter expression");
272 static bpf_u_int32 netmask
;
277 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
278 char *buf
, int optimize
, bpf_u_int32 mask
)
287 if (setjmp(top_ctx
)) {
295 snaplen
= pcap_snapshot(p
);
297 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
298 "snaplen of 0 rejects all packets");
302 lex_init(buf
? buf
: "");
303 init_linktype(pcap_datalink(p
));
310 root
= gen_retblk(snaplen
);
312 if (optimize
&& !no_optimize
) {
315 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
316 bpf_error("expression rejects all packets");
318 program
->bf_insns
= icode_to_fcode(root
, &len
);
319 program
->bf_len
= len
;
327 * entry point for using the compiler with no pcap open
328 * pass in all the stuff that is needed explicitly instead.
331 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
332 struct bpf_program
*program
,
333 char *buf
, int optimize
, bpf_u_int32 mask
)
338 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
341 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
347 * Clean up a "struct bpf_program" by freeing all the memory allocated
351 pcap_freecode(struct bpf_program
*program
)
354 if (program
->bf_insns
!= NULL
) {
355 free((char *)program
->bf_insns
);
356 program
->bf_insns
= NULL
;
361 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
362 * which of the jt and jf fields has been resolved and which is a pointer
363 * back to another unresolved block (or nil). At least one of the fields
364 * in each block is already resolved.
367 backpatch(list
, target
)
368 struct block
*list
, *target
;
385 * Merge the lists in b0 and b1, using the 'sense' field to indicate
386 * which of jt and jf is the link.
390 struct block
*b0
, *b1
;
392 register struct block
**p
= &b0
;
394 /* Find end of list. */
396 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
398 /* Concatenate the lists. */
406 backpatch(p
, gen_retblk(snaplen
));
407 p
->sense
= !p
->sense
;
408 backpatch(p
, gen_retblk(0));
414 struct block
*b0
, *b1
;
416 backpatch(b0
, b1
->head
);
417 b0
->sense
= !b0
->sense
;
418 b1
->sense
= !b1
->sense
;
420 b1
->sense
= !b1
->sense
;
426 struct block
*b0
, *b1
;
428 b0
->sense
= !b0
->sense
;
429 backpatch(b0
, b1
->head
);
430 b0
->sense
= !b0
->sense
;
439 b
->sense
= !b
->sense
;
442 static struct block
*
443 gen_cmp(offset
, size
, v
)
450 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
453 b
= new_block(JMP(BPF_JEQ
));
460 static struct block
*
461 gen_cmp_gt(offset
, size
, v
)
468 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
471 b
= new_block(JMP(BPF_JGT
));
478 static struct block
*
479 gen_mcmp(offset
, size
, v
, mask
)
484 struct block
*b
= gen_cmp(offset
, size
, v
);
487 if (mask
!= 0xffffffff) {
488 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
495 static struct block
*
496 gen_bcmp(offset
, size
, v
)
497 register u_int offset
, size
;
498 register const u_char
*v
;
500 register struct block
*b
, *tmp
;
504 register const u_char
*p
= &v
[size
- 4];
505 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
506 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
508 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
515 register const u_char
*p
= &v
[size
- 2];
516 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
518 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
525 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
534 * Various code constructs need to know the layout of the data link
535 * layer. These variables give the necessary offsets. off_linktype
536 * is set to -1 for no encapsulation, in which case, IP is assumed.
538 static u_int off_linktype
;
557 * SLIP doesn't have a link level type. The 16 byte
558 * header is hacked into our SLIP driver.
565 /* XXX this may be the same as the DLT_PPP_BSDOS case */
591 * FDDI doesn't really have a link-level type field.
592 * We set "off_linktype" to the offset of the LLC header.
594 * To check for Ethernet types, we assume that SSAP = SNAP
595 * is being used and pick out the encapsulated Ethernet type.
596 * XXX - should we generate code to check for SNAP?
600 off_linktype
+= pcap_fddipad
;
604 off_nl
+= pcap_fddipad
;
610 * Token Ring doesn't really have a link-level type field.
611 * We set "off_linktype" to the offset of the LLC header.
613 * To check for Ethernet types, we assume that SSAP = SNAP
614 * is being used and pick out the encapsulated Ethernet type.
615 * XXX - should we generate code to check for SNAP?
617 * XXX - the header is actually variable-length.
618 * Some various Linux patched versions gave 38
619 * as "off_linktype" and 40 as "off_nl"; however,
620 * if a token ring packet has *no* routing
621 * information, i.e. is not source-routed, the correct
622 * values are 20 and 22, as they are in the vanilla code.
624 * A packet is source-routed iff the uppermost bit
625 * of the first byte of the source address, at an
626 * offset of 8, has the uppermost bit set. If the
627 * packet is source-routed, the total number of bytes
628 * of routing information is 2 plus bits 0x1F00 of
629 * the 16-bit value at an offset of 14 (shifted right
630 * 8 - figure out which byte that is).
636 case DLT_ATM_RFC1483
:
638 * assume routed, non-ISO PDUs
639 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
650 case DLT_ATM_CLIP
: /* Linux ATM defines this */
655 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
660 bpf_error("unknown data link type %d", linktype
);
664 static struct block
*
671 s
= new_stmt(BPF_LD
|BPF_IMM
);
673 b
= new_block(JMP(BPF_JEQ
));
679 static inline struct block
*
682 return gen_uncond(1);
685 static inline struct block
*
688 return gen_uncond(0);
692 * Byte-swap a 32-bit number.
693 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
694 * big-endian platforms.)
696 #define SWAPLONG(y) \
697 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
699 static struct block
*
703 struct block
*b0
, *b1
;
712 * OSI protocols always use 802.2 encapsulation.
713 * XXX - should we check both the DSAP and the
714 * SSAP, like this, or should we check just the
717 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
719 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
720 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
728 * Ethernet_II frames, which are Ethernet
729 * frames with a frame type of ETHERTYPE_IPX;
731 * Ethernet_802.3 frames, which are 802.3
732 * frames (i.e., the type/length field is
733 * a length field, <= ETHERMTU, rather than
734 * a type field) with the first two bytes
735 * after the Ethernet/802.3 header being
738 * Ethernet_802.2 frames, which are 802.3
739 * frames with an 802.2 LLC header and
740 * with the IPX LSAP as the DSAP in the LLC
743 * Ethernet_SNAP frames, which are 802.3
744 * frames with an LLC header and a SNAP
745 * header and with an OUI of 0x000000
746 * (encapsulated Ethernet) and a protocol
747 * ID of ETHERTYPE_IPX in the SNAP header.
749 * XXX - should we generate the same code both
750 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
754 * This generates code to check both for the
755 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
757 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
758 (bpf_int32
)LLCSAP_IPX
);
759 b1
= gen_cmp(off_linktype
+ 2, BPF_H
,
764 * Now we add code to check for SNAP frames with
765 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
767 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
771 * Now we generate code to check for 802.3
774 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
778 * Now add the check for 802.3 frames before the
779 * check for Ethernet_802.2 and Ethernet_802.3,
780 * as those checks should only be done on 802.3
781 * frames, not on Ethernet frames.
786 * Now add the check for Ethernet_II frames, and
787 * do that before checking for the other frame
790 b0
= gen_cmp(off_linktype
, BPF_H
,
791 (bpf_int32
)ETHERTYPE_IPX
);
795 case ETHERTYPE_ATALK
:
798 * EtherTalk (AppleTalk protocols on Ethernet link
799 * layer) may use 802.2 encapsulation.
803 * Check for 802.2 encapsulation (EtherTalk phase 2?);
804 * we check for an Ethernet type field less than
805 * 1500, which means it's an 802.3 length field.
807 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
811 * 802.2-encapsulated ETHERTYPE_ATALK packets are
812 * SNAP packets with an organization code of
813 * 0x080007 (Apple, for Appletalk) and a protocol
814 * type of ETHERTYPE_ATALK (Appletalk).
816 * 802.2-encapsulated ETHERTYPE_AARP packets are
817 * SNAP packets with an organization code of
818 * 0x000000 (encapsulated Ethernet) and a protocol
819 * type of ETHERTYPE_AARP (Appletalk ARP).
821 if (proto
== ETHERTYPE_ATALK
)
822 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
823 else /* proto == ETHERTYPE_AARP */
824 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
828 * Check for Ethernet encapsulation (Ethertalk
829 * phase 1?); we just check for the Ethernet
832 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
838 if (proto
<= ETHERMTU
) {
840 * This is an LLC SAP value, so the frames
841 * that match would be 802.2 frames.
842 * Check that the frame is an 802.2 frame
843 * (i.e., that the length/type field is
844 * a length field, <= ETHERMTU) and
845 * then check the DSAP.
847 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
849 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
855 * This is an Ethernet type, so compare
856 * the length/type field with it (if
857 * the frame is an 802.2 frame, the length
858 * field will be <= ETHERMTU, and, as
859 * "proto" is > ETHERMTU, this test
860 * will fail and the frame won't match,
861 * which is what we want).
863 return gen_cmp(off_linktype
, BPF_H
,
871 case DLT_ATM_RFC1483
:
874 * XXX - handle token-ring variable-length header.
879 return gen_cmp(off_linktype
, BPF_H
, (long)
880 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
882 case ETHERTYPE_ATALK
:
884 * 802.2-encapsulated ETHERTYPE_ATALK packets are
885 * SNAP packets with an organization code of
886 * 0x080007 (Apple, for Appletalk) and a protocol
887 * type of ETHERTYPE_ATALK (Appletalk).
889 * XXX - check for an organization code of
890 * encapsulated Ethernet as well?
892 return gen_snap(0x080007, ETHERTYPE_ATALK
,
898 * XXX - we don't have to check for IPX 802.3
899 * here, but should we check for the IPX Ethertype?
901 if (proto
<= ETHERMTU
) {
903 * This is an LLC SAP value, so check
906 return gen_cmp(off_linktype
, BPF_B
,
910 * This is an Ethernet type; we assume
911 * that it's unlikely that it'll
912 * appear in the right place at random,
913 * and therefore check only the
914 * location that would hold the Ethernet
915 * type in a SNAP frame with an organization
916 * code of 0x000000 (encapsulated Ethernet).
918 * XXX - if we were to check for the SNAP DSAP
919 * and LSAP, as per XXX, and were also to check
920 * for an organization code of 0x000000
921 * (encapsulated Ethernet), we'd do
923 * return gen_snap(0x000000, proto,
926 * here; for now, we don't, as per the above.
927 * I don't know whether it's worth the
928 * extra CPU time to do the right check
931 return gen_cmp(off_linktype
+6, BPF_H
,
942 * OSI protocols always use 802.2 encapsulation.
943 * XXX - should we check both the DSAP and the
944 * LSAP, like this, or should we check just the
947 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
948 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
949 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
955 * Check both for 802.2 frames with the IPX LSAP as
956 * the DSAP and for Netware 802.3 frames.
958 * This generates code to check for 802.2 frames
961 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
962 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
963 (bpf_int32
)LLCSAP_IPX
);
967 * Now check for 802.3 frames and OR that with
970 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
974 case ETHERTYPE_ATALK
:
977 * EtherTalk (AppleTalk protocols on Ethernet link
978 * layer) may use 802.2 encapsulation.
982 * Check for 802.2 encapsulation (EtherTalk phase 2?);
983 * we check for the 802.2 protocol type in the
984 * "Ethernet type" field.
986 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
989 * 802.2-encapsulated ETHERTYPE_ATALK packets are
990 * SNAP packets with an organization code of
991 * 0x080007 (Apple, for Appletalk) and a protocol
992 * type of ETHERTYPE_ATALK (Appletalk).
994 * 802.2-encapsulated ETHERTYPE_AARP packets are
995 * SNAP packets with an organization code of
996 * 0x000000 (encapsulated Ethernet) and a protocol
997 * type of ETHERTYPE_AARP (Appletalk ARP).
999 if (proto
== ETHERTYPE_ATALK
)
1000 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1002 else /* proto == ETHERTYPE_AARP */
1003 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1008 * Check for Ethernet encapsulation (Ethertalk
1009 * phase 1?); we just check for the Ethernet
1012 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1018 if (proto
<= ETHERMTU
) {
1020 * This is an LLC SAP value, so the frames
1021 * that match would be 802.2 frames.
1022 * Check for the 802.2 protocol type
1023 * in the "Ethernet type" field, and
1024 * then check the DSAP.
1026 b0
= gen_cmp(off_linktype
, BPF_H
,
1028 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1034 * This is an Ethernet type, so compare
1035 * the length/type field with it (if
1036 * the frame is an 802.2 frame, the length
1037 * field will be <= ETHERMTU, and, as
1038 * "proto" is > ETHERMTU, this test
1039 * will fail and the frame won't match,
1040 * which is what we want).
1042 return gen_cmp(off_linktype
, BPF_H
,
1049 case DLT_SLIP_BSDOS
:
1052 * These types don't provide any type field; packets
1055 * XXX - for IPv4, check for a version number of 4, and,
1056 * for IPv6, check for a version number of 6?
1062 case ETHERTYPE_IPV6
:
1064 return gen_true(); /* always true */
1067 return gen_false(); /* always false */
1072 case DLT_PPP_SERIAL
:
1074 * We use Ethernet protocol types inside libpcap;
1075 * map them to the corresponding PPP protocol types.
1080 proto
= PPP_IP
; /* XXX was 0x21 */
1084 case ETHERTYPE_IPV6
:
1093 case ETHERTYPE_ATALK
:
1107 * I'm assuming the "Bridging PDU"s that go
1108 * over PPP are Spanning Tree Protocol
1122 * We use Ethernet protocol types inside libpcap;
1123 * map them to the corresponding PPP protocol types.
1128 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1129 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1131 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1136 case ETHERTYPE_IPV6
:
1146 case ETHERTYPE_ATALK
:
1160 * I'm assuming the "Bridging PDU"s that go
1161 * over PPP are Spanning Tree Protocol
1176 * For DLT_NULL, the link-layer header is a 32-bit
1177 * word containing an AF_ value in *host* byte order.
1179 * In addition, if we're reading a saved capture file,
1180 * the host byte order in the capture may not be the
1181 * same as the host byte order on this machine.
1183 * For DLT_LOOP, the link-layer header is a 32-bit
1184 * word containing an AF_ value in *network* byte order.
1186 * XXX - AF_ values may, unfortunately, be platform-
1187 * dependent; for example, FreeBSD's AF_INET6 is 24
1188 * whilst NetBSD's and OpenBSD's is 26.
1190 * This means that, when reading a capture file, just
1191 * checking for our AF_INET6 value won't work if the
1192 * capture file came from another OS.
1201 case ETHERTYPE_IPV6
:
1208 * Not a type on which we support filtering.
1209 * XXX - support those that have AF_ values
1210 * #defined on this platform, at least?
1215 if (linktype
== DLT_NULL
) {
1217 * The AF_ value is in host byte order, but
1218 * the BPF interpreter will convert it to
1219 * network byte order.
1221 * If this is a save file, and it's from a
1222 * machine with the opposite byte order to
1223 * ours, we byte-swap the AF_ value.
1225 * Then we run it through "htonl()", and
1226 * generate code to compare against the result.
1228 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1229 bpf_pcap
->sf
.swapped
)
1230 proto
= SWAPLONG(proto
);
1231 proto
= htonl(proto
);
1233 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1237 * All the types that have no encapsulation should either be
1238 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1239 * all packets are IP packets, or should be handled in some
1240 * special case, if none of them are (if some are and some
1241 * aren't, the lack of encapsulation is a problem, as we'd
1242 * have to find some other way of determining the packet type).
1244 * Therefore, if "off_linktype" is -1, there's an error.
1246 if (off_linktype
== -1)
1250 * Any type not handled above should always have an Ethernet
1251 * type at an offset of "off_linktype". (PPP is partially
1252 * handled above - the protocol type is mapped from the
1253 * Ethernet and LLC types we use internally to the corresponding
1254 * PPP type - but the PPP type is always specified by a value
1255 * at "off_linktype", so we don't have to do the code generation
1258 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1262 * Check for an LLC SNAP packet with a given organization code and
1263 * protocol type; we check the entire contents of the 802.2 LLC and
1264 * snap headers, checking for DSAP and SSAP of SNAP and a control
1265 * field of 0x03 in the LLC header, and for the specified organization
1266 * code and protocol type in the SNAP header.
1268 static struct block
*
1269 gen_snap(orgcode
, ptype
, offset
)
1270 bpf_u_int32 orgcode
;
1274 u_char snapblock
[8];
1276 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1277 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1278 snapblock
[2] = 0x03; /* control = UI */
1279 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1280 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1281 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1282 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1283 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1284 return gen_bcmp(offset
, 8, snapblock
);
1287 static struct block
*
1288 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1292 u_int src_off
, dst_off
;
1294 struct block
*b0
, *b1
;
1308 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1309 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1315 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1316 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1323 b0
= gen_linktype(proto
);
1324 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1330 static struct block
*
1331 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1332 struct in6_addr
*addr
;
1333 struct in6_addr
*mask
;
1335 u_int src_off
, dst_off
;
1337 struct block
*b0
, *b1
;
1352 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1353 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1359 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1360 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1367 /* this order is important */
1368 a
= (u_int32_t
*)addr
;
1369 m
= (u_int32_t
*)mask
;
1370 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1371 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1373 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1375 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1377 b0
= gen_linktype(proto
);
1383 static struct block
*
1384 gen_ehostop(eaddr
, dir
)
1385 register const u_char
*eaddr
;
1388 register struct block
*b0
, *b1
;
1392 return gen_bcmp(6, 6, eaddr
);
1395 return gen_bcmp(0, 6, eaddr
);
1398 b0
= gen_ehostop(eaddr
, Q_SRC
);
1399 b1
= gen_ehostop(eaddr
, Q_DST
);
1405 b0
= gen_ehostop(eaddr
, Q_SRC
);
1406 b1
= gen_ehostop(eaddr
, Q_DST
);
1415 * Like gen_ehostop, but for DLT_FDDI
1417 static struct block
*
1418 gen_fhostop(eaddr
, dir
)
1419 register const u_char
*eaddr
;
1422 struct block
*b0
, *b1
;
1427 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1429 return gen_bcmp(6 + 1, 6, eaddr
);
1434 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1436 return gen_bcmp(0 + 1, 6, eaddr
);
1440 b0
= gen_fhostop(eaddr
, Q_SRC
);
1441 b1
= gen_fhostop(eaddr
, Q_DST
);
1447 b0
= gen_fhostop(eaddr
, Q_SRC
);
1448 b1
= gen_fhostop(eaddr
, Q_DST
);
1457 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1459 static struct block
*
1460 gen_thostop(eaddr
, dir
)
1461 register const u_char
*eaddr
;
1464 register struct block
*b0
, *b1
;
1468 return gen_bcmp(8, 6, eaddr
);
1471 return gen_bcmp(2, 6, eaddr
);
1474 b0
= gen_thostop(eaddr
, Q_SRC
);
1475 b1
= gen_thostop(eaddr
, Q_DST
);
1481 b0
= gen_thostop(eaddr
, Q_SRC
);
1482 b1
= gen_thostop(eaddr
, Q_DST
);
1491 * This is quite tricky because there may be pad bytes in front of the
1492 * DECNET header, and then there are two possible data packet formats that
1493 * carry both src and dst addresses, plus 5 packet types in a format that
1494 * carries only the src node, plus 2 types that use a different format and
1495 * also carry just the src node.
1499 * Instead of doing those all right, we just look for data packets with
1500 * 0 or 1 bytes of padding. If you want to look at other packets, that
1501 * will require a lot more hacking.
1503 * To add support for filtering on DECNET "areas" (network numbers)
1504 * one would want to add a "mask" argument to this routine. That would
1505 * make the filter even more inefficient, although one could be clever
1506 * and not generate masking instructions if the mask is 0xFFFF.
1508 static struct block
*
1509 gen_dnhostop(addr
, dir
, base_off
)
1514 struct block
*b0
, *b1
, *b2
, *tmp
;
1515 u_int offset_lh
; /* offset if long header is received */
1516 u_int offset_sh
; /* offset if short header is received */
1521 offset_sh
= 1; /* follows flags */
1522 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1526 offset_sh
= 3; /* follows flags, dstnode */
1527 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1531 /* Inefficient because we do our Calvinball dance twice */
1532 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1533 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1539 /* Inefficient because we do our Calvinball dance twice */
1540 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1541 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1546 bpf_error("ISO host filtering not implemented");
1551 b0
= gen_linktype(ETHERTYPE_DN
);
1552 /* Check for pad = 1, long header case */
1553 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1554 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1555 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1556 BPF_H
, (bpf_int32
)ntohs(addr
));
1558 /* Check for pad = 0, long header case */
1559 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1560 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1563 /* Check for pad = 1, short header case */
1564 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1565 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1566 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1567 BPF_H
, (bpf_int32
)ntohs(addr
));
1570 /* Check for pad = 0, short header case */
1571 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1572 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1576 /* Combine with test for linktype */
1581 static struct block
*
1582 gen_host(addr
, mask
, proto
, dir
)
1588 struct block
*b0
, *b1
;
1593 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1594 if (off_linktype
!= -1) {
1595 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1597 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1603 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1604 off_nl
+ 12, off_nl
+ 16);
1607 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1608 off_nl
+ 14, off_nl
+ 24);
1611 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1612 off_nl
+ 14, off_nl
+ 24);
1615 bpf_error("'tcp' modifier applied to host");
1618 bpf_error("'udp' modifier applied to host");
1621 bpf_error("'icmp' modifier applied to host");
1624 bpf_error("'igmp' modifier applied to host");
1627 bpf_error("'igrp' modifier applied to host");
1630 bpf_error("'pim' modifier applied to host");
1633 bpf_error("ATALK host filtering not implemented");
1636 bpf_error("AARP host filtering not implemented");
1639 return gen_dnhostop(addr
, dir
, off_nl
);
1642 bpf_error("SCA host filtering not implemented");
1645 bpf_error("LAT host filtering not implemented");
1648 bpf_error("MOPDL host filtering not implemented");
1651 bpf_error("MOPRC host filtering not implemented");
1655 bpf_error("'ip6' modifier applied to ip host");
1658 bpf_error("'icmp6' modifier applied to host");
1662 bpf_error("'ah' modifier applied to host");
1665 bpf_error("'esp' modifier applied to host");
1668 bpf_error("ISO host filtering not implemented");
1671 bpf_error("'esis' modifier applied to host");
1674 bpf_error("'isis' modifier applied to host");
1677 bpf_error("'clnp' modifier applied to host");
1680 bpf_error("'stp' modifier applied to host");
1683 bpf_error("IPX host filtering not implemented");
1692 static struct block
*
1693 gen_host6(addr
, mask
, proto
, dir
)
1694 struct in6_addr
*addr
;
1695 struct in6_addr
*mask
;
1702 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1705 bpf_error("'ip' modifier applied to ip6 host");
1708 bpf_error("'rarp' modifier applied to ip6 host");
1711 bpf_error("'arp' modifier applied to ip6 host");
1714 bpf_error("'tcp' modifier applied to host");
1717 bpf_error("'udp' modifier applied to host");
1720 bpf_error("'icmp' modifier applied to host");
1723 bpf_error("'igmp' modifier applied to host");
1726 bpf_error("'igrp' modifier applied to host");
1729 bpf_error("'pim' modifier applied to host");
1732 bpf_error("ATALK host filtering not implemented");
1735 bpf_error("AARP host filtering not implemented");
1738 bpf_error("'decnet' modifier applied to ip6 host");
1741 bpf_error("SCA host filtering not implemented");
1744 bpf_error("LAT host filtering not implemented");
1747 bpf_error("MOPDL host filtering not implemented");
1750 bpf_error("MOPRC host filtering not implemented");
1753 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1754 off_nl
+ 8, off_nl
+ 24);
1757 bpf_error("'icmp6' modifier applied to host");
1760 bpf_error("'ah' modifier applied to host");
1763 bpf_error("'esp' modifier applied to host");
1766 bpf_error("ISO host filtering not implemented");
1769 bpf_error("'esis' modifier applied to host");
1772 bpf_error("'isis' modifier applied to host");
1775 bpf_error("'clnp' modifier applied to host");
1778 bpf_error("'stp' modifier applied to host");
1781 bpf_error("IPX host filtering not implemented");
1791 static struct block
*
1792 gen_gateway(eaddr
, alist
, proto
, dir
)
1793 const u_char
*eaddr
;
1794 bpf_u_int32
**alist
;
1798 struct block
*b0
, *b1
, *tmp
;
1801 bpf_error("direction applied to 'gateway'");
1808 if (linktype
== DLT_EN10MB
)
1809 b0
= gen_ehostop(eaddr
, Q_OR
);
1810 else if (linktype
== DLT_FDDI
)
1811 b0
= gen_fhostop(eaddr
, Q_OR
);
1812 else if (linktype
== DLT_IEEE802
)
1813 b0
= gen_thostop(eaddr
, Q_OR
);
1816 "'gateway' supported only on ethernet, FDDI or token ring");
1818 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1820 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1828 bpf_error("illegal modifier of 'gateway'");
1834 gen_proto_abbrev(proto
)
1845 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1847 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1853 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1855 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1861 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1864 #ifndef IPPROTO_IGMP
1865 #define IPPROTO_IGMP 2
1869 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1872 #ifndef IPPROTO_IGRP
1873 #define IPPROTO_IGRP 9
1876 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1880 #define IPPROTO_PIM 103
1884 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1886 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1892 b1
= gen_linktype(ETHERTYPE_IP
);
1896 b1
= gen_linktype(ETHERTYPE_ARP
);
1900 b1
= gen_linktype(ETHERTYPE_REVARP
);
1904 bpf_error("link layer applied in wrong context");
1907 b1
= gen_linktype(ETHERTYPE_ATALK
);
1911 b1
= gen_linktype(ETHERTYPE_AARP
);
1915 b1
= gen_linktype(ETHERTYPE_DN
);
1919 b1
= gen_linktype(ETHERTYPE_SCA
);
1923 b1
= gen_linktype(ETHERTYPE_LAT
);
1927 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1931 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1936 b1
= gen_linktype(ETHERTYPE_IPV6
);
1939 #ifndef IPPROTO_ICMPV6
1940 #define IPPROTO_ICMPV6 58
1943 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1948 #define IPPROTO_AH 51
1951 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1953 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1959 #define IPPROTO_ESP 50
1962 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1964 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1970 b1
= gen_linktype(LLCSAP_ISONS
);
1974 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
1978 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
1982 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
1986 b1
= gen_linktype(LLCSAP_8021D
);
1990 b1
= gen_linktype(LLCSAP_IPX
);
1999 static struct block
*
2006 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2007 s
->s
.k
= off_nl
+ 6;
2008 b
= new_block(JMP(BPF_JSET
));
2016 static struct block
*
2017 gen_portatom(off
, v
)
2024 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2027 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2028 s
->next
->s
.k
= off_nl
+ off
;
2030 b
= new_block(JMP(BPF_JEQ
));
2038 static struct block
*
2039 gen_portatom6(off
, v
)
2043 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2048 gen_portop(port
, proto
, dir
)
2049 int port
, proto
, dir
;
2051 struct block
*b0
, *b1
, *tmp
;
2053 /* ip proto 'proto' */
2054 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2060 b1
= gen_portatom(0, (bpf_int32
)port
);
2064 b1
= gen_portatom(2, (bpf_int32
)port
);
2069 tmp
= gen_portatom(0, (bpf_int32
)port
);
2070 b1
= gen_portatom(2, (bpf_int32
)port
);
2075 tmp
= gen_portatom(0, (bpf_int32
)port
);
2076 b1
= gen_portatom(2, (bpf_int32
)port
);
2088 static struct block
*
2089 gen_port(port
, ip_proto
, dir
)
2094 struct block
*b0
, *b1
, *tmp
;
2096 /* ether proto ip */
2097 b0
= gen_linktype(ETHERTYPE_IP
);
2102 b1
= gen_portop(port
, ip_proto
, dir
);
2106 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2107 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2120 gen_portop6(port
, proto
, dir
)
2121 int port
, proto
, dir
;
2123 struct block
*b0
, *b1
, *tmp
;
2125 /* ip proto 'proto' */
2126 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2130 b1
= gen_portatom6(0, (bpf_int32
)port
);
2134 b1
= gen_portatom6(2, (bpf_int32
)port
);
2139 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2140 b1
= gen_portatom6(2, (bpf_int32
)port
);
2145 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2146 b1
= gen_portatom6(2, (bpf_int32
)port
);
2158 static struct block
*
2159 gen_port6(port
, ip_proto
, dir
)
2164 struct block
*b0
, *b1
, *tmp
;
2166 /* ether proto ip */
2167 b0
= gen_linktype(ETHERTYPE_IPV6
);
2172 b1
= gen_portop6(port
, ip_proto
, dir
);
2176 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2177 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2190 lookup_proto(name
, proto
)
2191 register const char *name
;
2200 v
= pcap_nametoproto(name
);
2201 if (v
== PROTO_UNDEF
)
2202 bpf_error("unknown ip proto '%s'", name
);
2206 /* XXX should look up h/w protocol type based on linktype */
2207 v
= pcap_nametoeproto(name
);
2208 if (v
== PROTO_UNDEF
)
2209 bpf_error("unknown ether proto '%s'", name
);
2213 if (strcmp(name
, "esis") == 0)
2215 else if (strcmp(name
, "isis") == 0)
2217 else if (strcmp(name
, "clnp") == 0)
2220 bpf_error("unknown osi proto '%s'", name
);
2240 static struct block
*
2241 gen_protochain(v
, proto
, dir
)
2246 #ifdef NO_PROTOCHAIN
2247 return gen_proto(v
, proto
, dir
);
2249 struct block
*b0
, *b
;
2250 struct slist
*s
[100];
2251 int fix2
, fix3
, fix4
, fix5
;
2252 int ahcheck
, again
, end
;
2254 int reg2
= alloc_reg();
2256 memset(s
, 0, sizeof(s
));
2257 fix2
= fix3
= fix4
= fix5
= 0;
2264 b0
= gen_protochain(v
, Q_IP
, dir
);
2265 b
= gen_protochain(v
, Q_IPV6
, dir
);
2269 bpf_error("bad protocol applied for 'protochain'");
2273 no_optimize
= 1; /*this code is not compatible with optimzer yet */
2276 * s[0] is a dummy entry to protect other BPF insn from damaged
2277 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
2278 * hard to find interdependency made by jump table fixup.
2281 s
[i
] = new_stmt(0); /*dummy*/
2286 b0
= gen_linktype(ETHERTYPE_IP
);
2289 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2290 s
[i
]->s
.k
= off_nl
+ 9;
2292 /* X = ip->ip_hl << 2 */
2293 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2299 b0
= gen_linktype(ETHERTYPE_IPV6
);
2301 /* A = ip6->ip_nxt */
2302 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2303 s
[i
]->s
.k
= off_nl
+ 6;
2305 /* X = sizeof(struct ip6_hdr) */
2306 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
2312 bpf_error("unsupported proto to gen_protochain");
2316 /* again: if (A == v) goto end; else fall through; */
2318 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2320 s
[i
]->s
.jt
= NULL
; /*later*/
2321 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2325 #ifndef IPPROTO_NONE
2326 #define IPPROTO_NONE 59
2328 /* if (A == IPPROTO_NONE) goto end */
2329 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2330 s
[i
]->s
.jt
= NULL
; /*later*/
2331 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2332 s
[i
]->s
.k
= IPPROTO_NONE
;
2333 s
[fix5
]->s
.jf
= s
[i
];
2338 if (proto
== Q_IPV6
) {
2339 int v6start
, v6end
, v6advance
, j
;
2342 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
2343 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2344 s
[i
]->s
.jt
= NULL
; /*later*/
2345 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2346 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
2347 s
[fix2
]->s
.jf
= s
[i
];
2349 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
2350 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2351 s
[i
]->s
.jt
= NULL
; /*later*/
2352 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2353 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
2355 /* if (A == IPPROTO_ROUTING) goto v6advance */
2356 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2357 s
[i
]->s
.jt
= NULL
; /*later*/
2358 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2359 s
[i
]->s
.k
= IPPROTO_ROUTING
;
2361 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
2362 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2363 s
[i
]->s
.jt
= NULL
; /*later*/
2364 s
[i
]->s
.jf
= NULL
; /*later*/
2365 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
2376 * X = X + (P[X + 1] + 1) * 8;
2379 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2381 /* A = P[X + packet head] */
2382 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2386 s
[i
] = new_stmt(BPF_ST
);
2390 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2393 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2397 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2399 /* A = P[X + packet head]; */
2400 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2404 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2408 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2412 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2415 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2419 /* goto again; (must use BPF_JA for backward jump) */
2420 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2421 s
[i
]->s
.k
= again
- i
- 1;
2422 s
[i
- 1]->s
.jf
= s
[i
];
2426 for (j
= v6start
; j
<= v6end
; j
++)
2427 s
[j
]->s
.jt
= s
[v6advance
];
2432 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2434 s
[fix2
]->s
.jf
= s
[i
];
2440 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2441 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2442 s
[i
]->s
.jt
= NULL
; /*later*/
2443 s
[i
]->s
.jf
= NULL
; /*later*/
2444 s
[i
]->s
.k
= IPPROTO_AH
;
2446 s
[fix3
]->s
.jf
= s
[ahcheck
];
2453 * X = X + (P[X + 1] + 2) * 4;
2456 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2458 /* A = P[X + packet head]; */
2459 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2463 s
[i
] = new_stmt(BPF_ST
);
2467 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2470 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2474 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2476 /* A = P[X + packet head] */
2477 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2481 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2485 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2489 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2492 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2496 /* goto again; (must use BPF_JA for backward jump) */
2497 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2498 s
[i
]->s
.k
= again
- i
- 1;
2503 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2505 s
[fix2
]->s
.jt
= s
[end
];
2506 s
[fix4
]->s
.jf
= s
[end
];
2507 s
[fix5
]->s
.jt
= s
[end
];
2514 for (i
= 0; i
< max
- 1; i
++)
2515 s
[i
]->next
= s
[i
+ 1];
2516 s
[max
- 1]->next
= NULL
;
2521 b
= new_block(JMP(BPF_JEQ
));
2522 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2532 static struct block
*
2533 gen_proto(v
, proto
, dir
)
2538 struct block
*b0
, *b1
;
2540 if (dir
!= Q_DEFAULT
)
2541 bpf_error("direction applied to 'proto'");
2546 b0
= gen_proto(v
, Q_IP
, dir
);
2547 b1
= gen_proto(v
, Q_IPV6
, dir
);
2554 b0
= gen_linktype(ETHERTYPE_IP
);
2556 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2558 b1
= gen_protochain(v
, Q_IP
);
2564 b0
= gen_linktype(LLCSAP_ISONS
);
2565 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2570 bpf_error("arp does not encapsulate another protocol");
2574 bpf_error("rarp does not encapsulate another protocol");
2578 bpf_error("atalk encapsulation is not specifiable");
2582 bpf_error("decnet encapsulation is not specifiable");
2586 bpf_error("sca does not encapsulate another protocol");
2590 bpf_error("lat does not encapsulate another protocol");
2594 bpf_error("moprc does not encapsulate another protocol");
2598 bpf_error("mopdl does not encapsulate another protocol");
2602 return gen_linktype(v
);
2605 bpf_error("'udp proto' is bogus");
2609 bpf_error("'tcp proto' is bogus");
2613 bpf_error("'icmp proto' is bogus");
2617 bpf_error("'igmp proto' is bogus");
2621 bpf_error("'igrp proto' is bogus");
2625 bpf_error("'pim proto' is bogus");
2630 b0
= gen_linktype(ETHERTYPE_IPV6
);
2632 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2634 b1
= gen_protochain(v
, Q_IPV6
);
2640 bpf_error("'icmp6 proto' is bogus");
2644 bpf_error("'ah proto' is bogus");
2647 bpf_error("'ah proto' is bogus");
2650 bpf_error("'stp proto' is bogus");
2653 bpf_error("'ipx proto' is bogus");
2664 register const char *name
;
2667 int proto
= q
.proto
;
2671 bpf_u_int32 mask
, addr
;
2673 bpf_u_int32
**alist
;
2676 struct sockaddr_in
*sin
;
2677 struct sockaddr_in6
*sin6
;
2678 struct addrinfo
*res
, *res0
;
2679 struct in6_addr mask128
;
2681 struct block
*b
, *tmp
;
2682 int port
, real_proto
;
2687 addr
= pcap_nametonetaddr(name
);
2689 bpf_error("unknown network '%s'", name
);
2690 /* Left justify network addr and calculate its network mask */
2692 while (addr
&& (addr
& 0xff000000) == 0) {
2696 return gen_host(addr
, mask
, proto
, dir
);
2700 if (proto
== Q_LINK
) {
2704 eaddr
= pcap_ether_hostton(name
);
2707 "unknown ether host '%s'", name
);
2708 return gen_ehostop(eaddr
, dir
);
2711 eaddr
= pcap_ether_hostton(name
);
2714 "unknown FDDI host '%s'", name
);
2715 return gen_fhostop(eaddr
, dir
);
2718 eaddr
= pcap_ether_hostton(name
);
2721 "unknown token ring host '%s'", name
);
2722 return gen_thostop(eaddr
, dir
);
2726 "only ethernet/FDDI/token ring supports link-level host name");
2729 } else if (proto
== Q_DECNET
) {
2730 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2732 * I don't think DECNET hosts can be multihomed, so
2733 * there is no need to build up a list of addresses
2735 return (gen_host(dn_addr
, 0, proto
, dir
));
2738 alist
= pcap_nametoaddr(name
);
2739 if (alist
== NULL
|| *alist
== NULL
)
2740 bpf_error("unknown host '%s'", name
);
2742 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2744 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2746 tmp
= gen_host(**alist
++, 0xffffffff,
2753 memset(&mask128
, 0xff, sizeof(mask128
));
2754 res0
= res
= pcap_nametoaddrinfo(name
);
2756 bpf_error("unknown host '%s'", name
);
2758 tproto
= tproto6
= proto
;
2759 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2763 for (res
= res0
; res
; res
= res
->ai_next
) {
2764 switch (res
->ai_family
) {
2766 if (tproto
== Q_IPV6
)
2769 sin
= (struct sockaddr_in
*)
2771 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2772 0xffffffff, tproto
, dir
);
2775 if (tproto6
== Q_IP
)
2778 sin6
= (struct sockaddr_in6
*)
2780 tmp
= gen_host6(&sin6
->sin6_addr
,
2781 &mask128
, tproto6
, dir
);
2790 bpf_error("unknown host '%s'%s", name
,
2791 (proto
== Q_DEFAULT
)
2793 : " for specified address family");
2800 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2801 bpf_error("illegal qualifier of 'port'");
2802 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2803 bpf_error("unknown port '%s'", name
);
2804 if (proto
== Q_UDP
) {
2805 if (real_proto
== IPPROTO_TCP
)
2806 bpf_error("port '%s' is tcp", name
);
2808 /* override PROTO_UNDEF */
2809 real_proto
= IPPROTO_UDP
;
2811 if (proto
== Q_TCP
) {
2812 if (real_proto
== IPPROTO_UDP
)
2813 bpf_error("port '%s' is udp", name
);
2815 /* override PROTO_UNDEF */
2816 real_proto
= IPPROTO_TCP
;
2819 return gen_port(port
, real_proto
, dir
);
2823 b
= gen_port(port
, real_proto
, dir
);
2824 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2831 eaddr
= pcap_ether_hostton(name
);
2833 bpf_error("unknown ether host: %s", name
);
2835 alist
= pcap_nametoaddr(name
);
2836 if (alist
== NULL
|| *alist
== NULL
)
2837 bpf_error("unknown host '%s'", name
);
2838 return gen_gateway(eaddr
, alist
, proto
, dir
);
2840 bpf_error("'gateway' not supported in this configuration");
2844 real_proto
= lookup_proto(name
, proto
);
2845 if (real_proto
>= 0)
2846 return gen_proto(real_proto
, proto
, dir
);
2848 bpf_error("unknown protocol: %s", name
);
2851 real_proto
= lookup_proto(name
, proto
);
2852 if (real_proto
>= 0)
2853 return gen_protochain(real_proto
, proto
, dir
);
2855 bpf_error("unknown protocol: %s", name
);
2867 gen_mcode(s1
, s2
, masklen
, q
)
2868 register const char *s1
, *s2
;
2869 register int masklen
;
2872 register int nlen
, mlen
;
2875 nlen
= __pcap_atoin(s1
, &n
);
2876 /* Promote short ipaddr */
2880 mlen
= __pcap_atoin(s2
, &m
);
2881 /* Promote short ipaddr */
2884 bpf_error("non-network bits set in \"%s mask %s\"",
2887 /* Convert mask len to mask */
2889 bpf_error("mask length must be <= 32");
2890 m
= 0xffffffff << (32 - masklen
);
2892 bpf_error("non-network bits set in \"%s/%d\"",
2899 return gen_host(n
, m
, q
.proto
, q
.dir
);
2902 bpf_error("Mask syntax for networks only");
2909 register const char *s
;
2914 int proto
= q
.proto
;
2920 else if (q
.proto
== Q_DECNET
)
2921 vlen
= __pcap_atodn(s
, &v
);
2923 vlen
= __pcap_atoin(s
, &v
);
2930 if (proto
== Q_DECNET
)
2931 return gen_host(v
, 0, proto
, dir
);
2932 else if (proto
== Q_LINK
) {
2933 bpf_error("illegal link layer address");
2936 if (s
== NULL
&& q
.addr
== Q_NET
) {
2937 /* Promote short net number */
2938 while (v
&& (v
& 0xff000000) == 0) {
2943 /* Promote short ipaddr */
2947 return gen_host(v
, mask
, proto
, dir
);
2952 proto
= IPPROTO_UDP
;
2953 else if (proto
== Q_TCP
)
2954 proto
= IPPROTO_TCP
;
2955 else if (proto
== Q_DEFAULT
)
2956 proto
= PROTO_UNDEF
;
2958 bpf_error("illegal qualifier of 'port'");
2961 return gen_port((int)v
, proto
, dir
);
2965 b
= gen_port((int)v
, proto
, dir
);
2966 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2972 bpf_error("'gateway' requires a name");
2976 return gen_proto((int)v
, proto
, dir
);
2979 return gen_protochain((int)v
, proto
, dir
);
2994 gen_mcode6(s1
, s2
, masklen
, q
)
2995 register const char *s1
, *s2
;
2996 register int masklen
;
2999 struct addrinfo
*res
;
3000 struct in6_addr
*addr
;
3001 struct in6_addr mask
;
3006 bpf_error("no mask %s supported", s2
);
3008 res
= pcap_nametoaddrinfo(s1
);
3010 bpf_error("invalid ip6 address %s", s1
);
3012 bpf_error("%s resolved to multiple address", s1
);
3013 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3015 if (sizeof(mask
) * 8 < masklen
)
3016 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3017 memset(&mask
, 0xff, masklen
/ 8);
3019 mask
.s6_addr
[masklen
/ 8] =
3020 (0xff << (8 - masklen
% 8)) & 0xff;
3023 a
= (u_int32_t
*)addr
;
3024 m
= (u_int32_t
*)&mask
;
3025 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3026 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3027 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
3035 bpf_error("Mask syntax for networks only");
3039 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
3044 bpf_error("invalid qualifier against IPv6 address");
3052 register const u_char
*eaddr
;
3055 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3056 if (linktype
== DLT_EN10MB
)
3057 return gen_ehostop(eaddr
, (int)q
.dir
);
3058 if (linktype
== DLT_FDDI
)
3059 return gen_fhostop(eaddr
, (int)q
.dir
);
3060 if (linktype
== DLT_IEEE802
)
3061 return gen_thostop(eaddr
, (int)q
.dir
);
3062 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
3064 bpf_error("ethernet address used in non-ether expression");
3070 struct slist
*s0
, *s1
;
3073 * This is definitely not the best way to do this, but the
3074 * lists will rarely get long.
3081 static struct slist
*
3087 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3092 static struct slist
*
3098 s
= new_stmt(BPF_LD
|BPF_MEM
);
3104 gen_load(proto
, index
, size
)
3109 struct slist
*s
, *tmp
;
3111 int regno
= alloc_reg();
3113 free_reg(index
->regno
);
3117 bpf_error("data size must be 1, 2, or 4");
3133 bpf_error("unsupported index operation");
3136 s
= xfer_to_x(index
);
3137 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3139 sappend(index
->s
, s
);
3154 /* XXX Note that we assume a fixed link header here. */
3155 s
= xfer_to_x(index
);
3156 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3159 sappend(index
->s
, s
);
3161 b
= gen_proto_abbrev(proto
);
3163 gen_and(index
->b
, b
);
3173 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3175 sappend(s
, xfer_to_a(index
));
3176 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3177 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3178 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3180 sappend(index
->s
, s
);
3182 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
3184 gen_and(index
->b
, b
);
3186 gen_and(gen_proto_abbrev(Q_IP
), b
);
3192 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
3196 index
->regno
= regno
;
3197 s
= new_stmt(BPF_ST
);
3199 sappend(index
->s
, s
);
3205 gen_relation(code
, a0
, a1
, reversed
)
3207 struct arth
*a0
, *a1
;
3210 struct slist
*s0
, *s1
, *s2
;
3211 struct block
*b
, *tmp
;
3215 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
3216 b
= new_block(JMP(code
));
3217 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
3218 reversed
= !reversed
;
3219 b
->s
.k
= 0x80000000;
3227 sappend(a0
->s
, a1
->s
);
3231 free_reg(a0
->regno
);
3232 free_reg(a1
->regno
);
3234 /* 'and' together protocol checks */
3237 gen_and(a0
->b
, tmp
= a1
->b
);
3253 int regno
= alloc_reg();
3254 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
3257 s
= new_stmt(BPF_LD
|BPF_LEN
);
3258 s
->next
= new_stmt(BPF_ST
);
3259 s
->next
->s
.k
= regno
;
3274 a
= (struct arth
*)newchunk(sizeof(*a
));
3278 s
= new_stmt(BPF_LD
|BPF_IMM
);
3280 s
->next
= new_stmt(BPF_ST
);
3296 s
= new_stmt(BPF_ALU
|BPF_NEG
);
3299 s
= new_stmt(BPF_ST
);
3307 gen_arth(code
, a0
, a1
)
3309 struct arth
*a0
, *a1
;
3311 struct slist
*s0
, *s1
, *s2
;
3315 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
3320 sappend(a0
->s
, a1
->s
);
3322 free_reg(a1
->regno
);
3324 s0
= new_stmt(BPF_ST
);
3325 a0
->regno
= s0
->s
.k
= alloc_reg();
3332 * Here we handle simple allocation of the scratch registers.
3333 * If too many registers are alloc'd, the allocator punts.
3335 static int regused
[BPF_MEMWORDS
];
3339 * Return the next free register.
3344 int n
= BPF_MEMWORDS
;
3347 if (regused
[curreg
])
3348 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
3350 regused
[curreg
] = 1;
3354 bpf_error("too many registers needed to evaluate expression");
3359 * Return a register to the table so it can
3369 static struct block
*
3376 s
= new_stmt(BPF_LD
|BPF_LEN
);
3377 b
= new_block(JMP(jmp
));
3388 return gen_len(BPF_JGE
, n
);
3392 * Actually, this is less than or equal.
3400 b
= gen_len(BPF_JGT
, n
);
3407 gen_byteop(op
, idx
, val
)
3418 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3421 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3422 b
->s
.code
= JMP(BPF_JGE
);
3427 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3428 b
->s
.code
= JMP(BPF_JGT
);
3432 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3436 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3440 b
= new_block(JMP(BPF_JEQ
));
3448 gen_broadcast(proto
)
3451 bpf_u_int32 hostmask
;
3452 struct block
*b0
, *b1
, *b2
;
3453 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3459 if (linktype
== DLT_EN10MB
)
3460 return gen_ehostop(ebroadcast
, Q_DST
);
3461 if (linktype
== DLT_FDDI
)
3462 return gen_fhostop(ebroadcast
, Q_DST
);
3463 if (linktype
== DLT_IEEE802
)
3464 return gen_thostop(ebroadcast
, Q_DST
);
3465 bpf_error("not a broadcast link");
3469 b0
= gen_linktype(ETHERTYPE_IP
);
3470 hostmask
= ~netmask
;
3471 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3472 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3473 (bpf_int32
)(~0 & hostmask
), hostmask
);
3478 bpf_error("only ether/ip broadcast filters supported");
3482 gen_multicast(proto
)
3485 register struct block
*b0
, *b1
;
3486 register struct slist
*s
;
3492 if (linktype
== DLT_EN10MB
) {
3493 /* ether[0] & 1 != 0 */
3494 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3496 b0
= new_block(JMP(BPF_JSET
));
3502 if (linktype
== DLT_FDDI
) {
3503 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3504 /* fddi[1] & 1 != 0 */
3505 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3507 b0
= new_block(JMP(BPF_JSET
));
3513 /* TODO - check how token ring handles multicast */
3514 /* if (linktype == DLT_IEEE802) ... */
3516 /* Link not known to support multicasts */
3520 b0
= gen_linktype(ETHERTYPE_IP
);
3521 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3522 b1
->s
.code
= JMP(BPF_JGE
);
3528 b0
= gen_linktype(ETHERTYPE_IPV6
);
3529 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3534 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3538 * generate command for inbound/outbound. It's here so we can
3539 * make it link-type specific. 'dir' = 0 implies "inbound",
3540 * = 1 implies "outbound".
3546 register struct block
*b0
;
3549 * Only some data link types support inbound/outbound qualifiers.
3554 b0
= gen_relation(BPF_JEQ
,
3555 gen_load(Q_LINK
, gen_loadi(0), 1),
3561 bpf_error("inbound/outbound not supported on linktype %d\n",
3570 * support IEEE 802.1Q VLAN trunk over ethernet
3576 static u_int orig_linktype
= -1, orig_nl
= -1;
3580 * Change the offsets to point to the type and data fields within
3581 * the VLAN packet. This is somewhat of a kludge.
3583 if (orig_nl
== (u_int
)-1) {
3584 orig_linktype
= off_linktype
; /* save original values */
3595 bpf_error("no VLAN support for data link type %d",
3601 /* check for VLAN */
3602 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3604 /* If a specific VLAN is requested, check VLAN id */
3605 if (vlan_num
>= 0) {
3608 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);