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[] _U_ =
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.221.2.52 2007/06/22 06:43:58 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
72 #include "sunatmpos.h"
78 #define offsetof(s, e) ((size_t)&((s *)0)->e)
82 #include <netdb.h> /* for "struct addrinfo" */
85 #include <pcap-namedb.h>
87 #include <netproto/802_11/ieee80211.h>
88 #include <netproto/802_11/ieee80211_radiotap.h>
93 #define IPPROTO_SCTP 132
96 #ifdef HAVE_OS_PROTO_H
100 #define JMP(c) ((c)|BPF_JMP|BPF_K)
103 static jmp_buf top_ctx;
104 static pcap_t *bpf_pcap;
107 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
108 static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
110 static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
115 static int pcap_fddipad;
120 bpf_error(const char *fmt, ...)
125 if (bpf_pcap != NULL)
126 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
133 static void init_linktype(pcap_t *);
135 static int alloc_reg(void);
136 static void free_reg(int);
138 static struct block *root;
141 * Value passed to gen_load_a() to indicate what the offset argument
145 OR_PACKET, /* relative to the beginning of the packet */
146 OR_LINK, /* relative to the link-layer header */
147 OR_NET, /* relative to the network-layer header */
148 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
149 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
150 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
154 * We divy out chunks of memory rather than call malloc each time so
155 * we don't have to worry about leaking memory. It's probably
156 * not a big deal if all this memory was wasted but if this ever
157 * goes into a library that would probably not be a good idea.
159 * XXX - this *is* in a library....
162 #define CHUNK0SIZE 1024
168 static struct chunk chunks[NCHUNKS];
169 static int cur_chunk;
171 static void *newchunk(u_int);
172 static void freechunks(void);
173 static inline struct block *new_block(int);
174 static inline struct slist *new_stmt(int);
175 static struct block *gen_retblk(int);
176 static inline void syntax(void);
178 static void backpatch(struct block *, struct block *);
179 static void merge(struct block *, struct block *);
180 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
181 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
182 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
183 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
184 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
185 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
187 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
188 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
189 bpf_u_int32, bpf_u_int32, int, bpf_int32);
190 static struct slist *gen_load_llrel(u_int, u_int);
191 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
192 static struct slist *gen_loadx_iphdrlen(void);
193 static struct block *gen_uncond(int);
194 static inline struct block *gen_true(void);
195 static inline struct block *gen_false(void);
196 static struct block *gen_ether_linktype(int);
197 static struct block *gen_linux_sll_linktype(int);
198 static void insert_radiotap_load_llprefixlen(struct block *);
199 static void insert_ppi_load_llprefixlen(struct block *);
200 static void insert_load_llprefixlen(struct block *);
201 static struct slist *gen_llprefixlen(void);
202 static struct block *gen_linktype(int);
203 static struct block *gen_snap(bpf_u_int32, bpf_u_int32, u_int);
204 static struct block *gen_llc_linktype(int);
205 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
207 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
209 static struct block *gen_ahostop(const u_char *, int);
210 static struct block *gen_ehostop(const u_char *, int);
211 static struct block *gen_fhostop(const u_char *, int);
212 static struct block *gen_thostop(const u_char *, int);
213 static struct block *gen_wlanhostop(const u_char *, int);
214 static struct block *gen_ipfchostop(const u_char *, int);
215 static struct block *gen_dnhostop(bpf_u_int32, int);
216 static struct block *gen_mpls_linktype(int);
217 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
219 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
222 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
224 static struct block *gen_ipfrag(void);
225 static struct block *gen_portatom(int, bpf_int32);
226 static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
228 static struct block *gen_portatom6(int, bpf_int32);
229 static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
231 struct block *gen_portop(int, int, int);
232 static struct block *gen_port(int, int, int);
233 struct block *gen_portrangeop(int, int, int, int);
234 static struct block *gen_portrange(int, int, int, int);
236 struct block *gen_portop6(int, int, int);
237 static struct block *gen_port6(int, int, int);
238 struct block *gen_portrangeop6(int, int, int, int);
239 static struct block *gen_portrange6(int, int, int, int);
241 static int lookup_proto(const char *, int);
242 static struct block *gen_protochain(int, int, int);
243 static struct block *gen_proto(int, int, int);
244 static struct slist *xfer_to_x(struct arth *);
245 static struct slist *xfer_to_a(struct arth *);
246 static struct block *gen_mac_multicast(int);
247 static struct block *gen_len(int, int);
249 static struct block *gen_ppi_dlt_check(void);
250 static struct block *gen_msg_abbrev(int type);
261 /* XXX Round up to nearest long. */
262 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
264 /* XXX Round up to structure boundary. */
268 cp = &chunks[cur_chunk];
269 if (n > cp->n_left) {
270 ++cp, k = ++cur_chunk;
272 bpf_error("out of memory");
273 size = CHUNK0SIZE << k;
274 cp->m = (void *)malloc(size);
276 bpf_error("out of memory");
277 memset((char *)cp->m, 0, size);
280 bpf_error("out of memory");
283 return (void *)((char *)cp->m + cp->n_left);
292 for (i = 0; i < NCHUNKS; ++i)
293 if (chunks[i].m != NULL) {
300 * A strdup whose allocations are freed after code generation is over.
304 register const char *s;
306 int n = strlen(s) + 1;
307 char *cp = newchunk(n);
313 static inline struct block *
319 p = (struct block *)newchunk(sizeof(*p));
326 static inline struct slist *
332 p = (struct slist *)newchunk(sizeof(*p));
338 static struct block *
342 struct block *b = new_block(BPF_RET|BPF_K);
351 bpf_error("syntax error in filter expression");
354 static bpf_u_int32 netmask;
359 pcap_compile(pcap_t *p, struct bpf_program *program,
360 const char *buf, int optimize, bpf_u_int32 mask)
363 const char * volatile xbuf = buf;
370 if (setjmp(top_ctx)) {
378 snaplen = pcap_snapshot(p);
380 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
381 "snaplen of 0 rejects all packets");
385 lex_init(xbuf ? xbuf : "");
393 root = gen_retblk(snaplen);
395 if (optimize && !no_optimize) {
398 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
399 bpf_error("expression rejects all packets");
401 program->bf_insns = icode_to_fcode(root, &len);
402 program->bf_len = len;
410 * entry point for using the compiler with no pcap open
411 * pass in all the stuff that is needed explicitly instead.
414 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
415 struct bpf_program *program,
416 const char *buf, int optimize, bpf_u_int32 mask)
421 p = pcap_open_dead(linktype_arg, snaplen_arg);
424 ret = pcap_compile(p, program, buf, optimize, mask);
430 * Clean up a "struct bpf_program" by freeing all the memory allocated
434 pcap_freecode(struct bpf_program *program)
437 if (program->bf_insns != NULL) {
438 free((char *)program->bf_insns);
439 program->bf_insns = NULL;
444 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
445 * which of the jt and jf fields has been resolved and which is a pointer
446 * back to another unresolved block (or nil). At least one of the fields
447 * in each block is already resolved.
450 backpatch(list, target)
451 struct block *list, *target;
468 * Merge the lists in b0 and b1, using the 'sense' field to indicate
469 * which of jt and jf is the link.
473 struct block *b0, *b1;
475 register struct block **p = &b0;
477 /* Find end of list. */
479 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
481 /* Concatenate the lists. */
490 struct block *ppi_dlt_check;
492 ppi_dlt_check = gen_ppi_dlt_check();
494 if (ppi_dlt_check != NULL)
496 gen_and(ppi_dlt_check, p);
499 backpatch(p, gen_retblk(snaplen));
500 p->sense = !p->sense;
501 backpatch(p, gen_retblk(0));
505 * Insert before the statements of the first (root) block any
506 * statements needed to load the lengths of any variable-length
507 * headers into registers.
509 * XXX - a fancier strategy would be to insert those before the
510 * statements of all blocks that use those lengths and that
511 * have no predecessors that use them, so that we only compute
512 * the lengths if we need them. There might be even better
513 * approaches than that. However, as we're currently only
514 * handling variable-length radiotap headers, and as all
515 * filtering expressions other than raw link[M:N] tests
516 * require the length of that header, doing more for that
517 * header length isn't really worth the effort.
520 insert_load_llprefixlen(root);
525 struct block *b0, *b1;
527 backpatch(b0, b1->head);
528 b0->sense = !b0->sense;
529 b1->sense = !b1->sense;
531 b1->sense = !b1->sense;
537 struct block *b0, *b1;
539 b0->sense = !b0->sense;
540 backpatch(b0, b1->head);
541 b0->sense = !b0->sense;
550 b->sense = !b->sense;
553 static struct block *
554 gen_cmp(offrel, offset, size, v)
555 enum e_offrel offrel;
559 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
562 static struct block *
563 gen_cmp_gt(offrel, offset, size, v)
564 enum e_offrel offrel;
568 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
571 static struct block *
572 gen_cmp_ge(offrel, offset, size, v)
573 enum e_offrel offrel;
577 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
580 static struct block *
581 gen_cmp_lt(offrel, offset, size, v)
582 enum e_offrel offrel;
586 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
589 static struct block *
590 gen_cmp_le(offrel, offset, size, v)
591 enum e_offrel offrel;
595 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
598 static struct block *
599 gen_mcmp(offrel, offset, size, v, mask)
600 enum e_offrel offrel;
605 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
608 static struct block *
609 gen_bcmp(offrel, offset, size, v)
610 enum e_offrel offrel;
611 register u_int offset, size;
612 register const u_char *v;
614 register struct block *b, *tmp;
618 register const u_char *p = &v[size - 4];
619 bpf_int32 w = ((bpf_int32)p[0] << 24) |
620 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
622 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
629 register const u_char *p = &v[size - 2];
630 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
632 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
639 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
648 * AND the field of size "size" at offset "offset" relative to the header
649 * specified by "offrel" with "mask", and compare it with the value "v"
650 * with the test specified by "jtype"; if "reverse" is true, the test
651 * should test the opposite of "jtype".
653 static struct block *
654 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
655 enum e_offrel offrel;
657 bpf_u_int32 offset, size, mask, jtype;
660 struct slist *s, *s2;
663 s = gen_load_a(offrel, offset, size);
665 if (mask != 0xffffffff) {
666 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
671 b = new_block(JMP(jtype));
674 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
680 * Various code constructs need to know the layout of the data link
681 * layer. These variables give the necessary offsets from the beginning
682 * of the packet data.
684 * If the link layer has variable_length headers, the offsets are offsets
685 * from the end of the link-link-layer header, and "reg_ll_size" is
686 * the register number for a register containing the length of the
687 * link-layer header. Otherwise, "reg_ll_size" is -1.
689 static int reg_ll_size;
692 * This is the offset of the beginning of the link-layer header from
693 * the beginning of the raw packet data.
695 * It's usually 0, except for 802.11 with a fixed-length radio header.
696 * (For 802.11 with a variable-length radio header, we have to generate
697 * code to compute that offset; off_ll is 0 in that case.)
702 * This is the offset of the beginning of the MAC-layer header.
703 * It's usually 0, except for ATM LANE, where it's the offset, relative
704 * to the beginning of the raw packet data, of the Ethernet header.
706 static u_int off_mac;
709 * "off_linktype" is the offset to information in the link-layer header
710 * giving the packet type. This offset is relative to the beginning
711 * of the link-layer header (i.e., it doesn't include off_ll).
713 * For Ethernet, it's the offset of the Ethernet type field.
715 * For link-layer types that always use 802.2 headers, it's the
716 * offset of the LLC header.
718 * For PPP, it's the offset of the PPP type field.
720 * For Cisco HDLC, it's the offset of the CHDLC type field.
722 * For BSD loopback, it's the offset of the AF_ value.
724 * For Linux cooked sockets, it's the offset of the type field.
726 * It's set to -1 for no encapsulation, in which case, IP is assumed.
728 static u_int off_linktype;
731 * TRUE if the link layer includes an ATM pseudo-header.
733 static int is_atm = 0;
736 * TRUE if "lane" appeared in the filter; it causes us to generate
737 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
739 static int is_lane = 0;
742 * These are offsets for the ATM pseudo-header.
744 static u_int off_vpi;
745 static u_int off_vci;
746 static u_int off_proto;
749 * These are offsets for the MTP2 fields.
754 * These are offsets for the MTP3 fields.
756 static u_int off_sio;
757 static u_int off_opc;
758 static u_int off_dpc;
759 static u_int off_sls;
762 * This is the offset of the first byte after the ATM pseudo_header,
763 * or -1 if there is no ATM pseudo-header.
765 static u_int off_payload;
768 * These are offsets to the beginning of the network-layer header.
769 * They are relative to the beginning of the link-layer header (i.e.,
770 * they don't include off_ll).
772 * If the link layer never uses 802.2 LLC:
774 * "off_nl" and "off_nl_nosnap" are the same.
776 * If the link layer always uses 802.2 LLC:
778 * "off_nl" is the offset if there's a SNAP header following
781 * "off_nl_nosnap" is the offset if there's no SNAP header.
783 * If the link layer is Ethernet:
785 * "off_nl" is the offset if the packet is an Ethernet II packet
786 * (we assume no 802.3+802.2+SNAP);
788 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
789 * with an 802.2 header following it.
792 static u_int off_nl_nosnap;
800 linktype = pcap_datalink(p);
802 pcap_fddipad = p->fddipad;
806 * Assume it's not raw ATM with a pseudo-header, for now.
817 * And assume we're not doing SS7.
826 * Also assume it's not 802.11 with a fixed-length radio header.
832 label_stack_depth = 0;
840 off_nl = 6; /* XXX in reality, variable! */
841 off_nl_nosnap = 6; /* no 802.2 LLC */
844 case DLT_ARCNET_LINUX:
846 off_nl = 8; /* XXX in reality, variable! */
847 off_nl_nosnap = 8; /* no 802.2 LLC */
852 off_nl = 14; /* Ethernet II */
853 off_nl_nosnap = 17; /* 802.3+802.2 */
858 * SLIP doesn't have a link level type. The 16 byte
859 * header is hacked into our SLIP driver.
863 off_nl_nosnap = 16; /* no 802.2 LLC */
867 /* XXX this may be the same as the DLT_PPP_BSDOS case */
871 off_nl_nosnap = 24; /* no 802.2 LLC */
878 off_nl_nosnap = 4; /* no 802.2 LLC */
884 off_nl_nosnap = 12; /* no 802.2 LLC */
889 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
890 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
893 off_nl_nosnap = 4; /* no 802.2 LLC */
898 * This does no include the Ethernet header, and
899 * only covers session state.
903 off_nl_nosnap = 8; /* no 802.2 LLC */
909 off_nl_nosnap = 24; /* no 802.2 LLC */
914 * FDDI doesn't really have a link-level type field.
915 * We set "off_linktype" to the offset of the LLC header.
917 * To check for Ethernet types, we assume that SSAP = SNAP
918 * is being used and pick out the encapsulated Ethernet type.
919 * XXX - should we generate code to check for SNAP?
923 off_linktype += pcap_fddipad;
925 off_nl = 21; /* FDDI+802.2+SNAP */
926 off_nl_nosnap = 16; /* FDDI+802.2 */
928 off_nl += pcap_fddipad;
929 off_nl_nosnap += pcap_fddipad;
935 * Token Ring doesn't really have a link-level type field.
936 * We set "off_linktype" to the offset of the LLC header.
938 * To check for Ethernet types, we assume that SSAP = SNAP
939 * is being used and pick out the encapsulated Ethernet type.
940 * XXX - should we generate code to check for SNAP?
942 * XXX - the header is actually variable-length.
943 * Some various Linux patched versions gave 38
944 * as "off_linktype" and 40 as "off_nl"; however,
945 * if a token ring packet has *no* routing
946 * information, i.e. is not source-routed, the correct
947 * values are 20 and 22, as they are in the vanilla code.
949 * A packet is source-routed iff the uppermost bit
950 * of the first byte of the source address, at an
951 * offset of 8, has the uppermost bit set. If the
952 * packet is source-routed, the total number of bytes
953 * of routing information is 2 plus bits 0x1F00 of
954 * the 16-bit value at an offset of 14 (shifted right
955 * 8 - figure out which byte that is).
958 off_nl = 22; /* Token Ring+802.2+SNAP */
959 off_nl_nosnap = 17; /* Token Ring+802.2 */
964 * 802.11 doesn't really have a link-level type field.
965 * We set "off_linktype" to the offset of the LLC header.
967 * To check for Ethernet types, we assume that SSAP = SNAP
968 * is being used and pick out the encapsulated Ethernet type.
969 * XXX - should we generate code to check for SNAP?
971 * XXX - the header is actually variable-length. We
972 * assume a 24-byte link-layer header, as appears in
973 * data frames in networks with no bridges. If the
974 * fromds and tods 802.11 header bits are both set,
975 * it's actually supposed to be 30 bytes.
978 off_nl = 32; /* 802.11+802.2+SNAP */
979 off_nl_nosnap = 27; /* 802.11+802.2 */
982 case DLT_PRISM_HEADER:
984 * Same as 802.11, but with an additional header before
985 * the 802.11 header, containing a bunch of additional
986 * information including radio-level information.
988 * The header is 144 bytes long.
990 * XXX - same variable-length header problem; at least
991 * the Prism header is fixed-length.
995 off_nl = 32; /* Prism+802.11+802.2+SNAP */
996 off_nl_nosnap = 27; /* Prism+802.11+802.2 */
999 case DLT_IEEE802_11_RADIO_AVS:
1001 * Same as 802.11, but with an additional header before
1002 * the 802.11 header, containing a bunch of additional
1003 * information including radio-level information.
1005 * The header is 64 bytes long, at least in its
1006 * current incarnation.
1008 * XXX - same variable-length header problem, only
1009 * more so; this header is also variable-length,
1010 * with the length being the 32-bit big-endian
1011 * number at an offset of 4 from the beginning
1012 * of the radio header. We should handle that the
1013 * same way we handle the length at the beginning
1014 * of the radiotap header.
1016 * XXX - in Linux, do any drivers that supply an AVS
1017 * header supply a link-layer type other than
1018 * ARPHRD_IEEE80211_PRISM? If so, we should map that
1019 * to DLT_IEEE802_11_RADIO_AVS; if not, or if there are
1020 * any drivers that supply an AVS header but supply
1021 * an ARPHRD value of ARPHRD_IEEE80211_PRISM, we'll
1022 * have to check the header in the generated code to
1023 * determine whether it's Prism or AVS.
1027 off_nl = 32; /* Radio+802.11+802.2+SNAP */
1028 off_nl_nosnap = 27; /* Radio+802.11+802.2 */
1033 * At the moment we treat PPI as normal Radiotap encoded
1034 * packets. The difference is in the function that generates
1035 * the code at the beginning to compute the header length.
1036 * Since this code generator of PPI supports bare 802.11
1037 * encapsulation only (i.e. the encapsulated DLT should be
1038 * DLT_IEEE802_11) we generate code to check for this too.
1041 case DLT_IEEE802_11_RADIO:
1043 * Same as 802.11, but with an additional header before
1044 * the 802.11 header, containing a bunch of additional
1045 * information including radio-level information.
1047 * The radiotap header is variable length, and we
1048 * generate code to compute its length and store it
1049 * in a register. These offsets are relative to the
1050 * beginning of the 802.11 header.
1053 off_nl = 32; /* 802.11+802.2+SNAP */
1054 off_nl_nosnap = 27; /* 802.11+802.2 */
1057 case DLT_ATM_RFC1483:
1058 case DLT_ATM_CLIP: /* Linux ATM defines this */
1060 * assume routed, non-ISO PDUs
1061 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1063 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1064 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1065 * latter would presumably be treated the way PPPoE
1066 * should be, so you can do "pppoe and udp port 2049"
1067 * or "pppoa and tcp port 80" and have it check for
1068 * PPPo{A,E} and a PPP protocol of IP and....
1071 off_nl = 8; /* 802.2+SNAP */
1072 off_nl_nosnap = 3; /* 802.2 */
1077 * Full Frontal ATM; you get AALn PDUs with an ATM
1081 off_vpi = SUNATM_VPI_POS;
1082 off_vci = SUNATM_VCI_POS;
1083 off_proto = PROTO_POS;
1084 off_mac = -1; /* LLC-encapsulated, so no MAC-layer header */
1085 off_payload = SUNATM_PKT_BEGIN_POS;
1086 off_linktype = off_payload;
1087 off_nl = off_payload+8; /* 802.2+SNAP */
1088 off_nl_nosnap = off_payload+3; /* 802.2 */
1094 off_nl_nosnap = 0; /* no 802.2 LLC */
1097 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1100 off_nl_nosnap = 16; /* no 802.2 LLC */
1105 * LocalTalk does have a 1-byte type field in the LLAP header,
1106 * but really it just indicates whether there is a "short" or
1107 * "long" DDP packet following.
1111 off_nl_nosnap = 0; /* no 802.2 LLC */
1114 case DLT_IP_OVER_FC:
1116 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1117 * link-level type field. We set "off_linktype" to the
1118 * offset of the LLC header.
1120 * To check for Ethernet types, we assume that SSAP = SNAP
1121 * is being used and pick out the encapsulated Ethernet type.
1122 * XXX - should we generate code to check for SNAP? RFC
1123 * 2625 says SNAP should be used.
1126 off_nl = 24; /* IPFC+802.2+SNAP */
1127 off_nl_nosnap = 19; /* IPFC+802.2 */
1132 * XXX - we should set this to handle SNAP-encapsulated
1133 * frames (NLPID of 0x80).
1137 off_nl_nosnap = 0; /* no 802.2 LLC */
1141 * the only BPF-interesting FRF.16 frames are non-control frames;
1142 * Frame Relay has a variable length link-layer
1143 * so lets start with offset 4 for now and increments later on (FIXME);
1148 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1151 case DLT_APPLE_IP_OVER_IEEE1394:
1154 off_nl_nosnap = 18; /* no 802.2 LLC */
1157 case DLT_LINUX_IRDA:
1159 * Currently, only raw "link[N:M]" filtering is supported.
1168 * Currently, only raw "link[N:M]" filtering is supported.
1175 case DLT_SYMANTEC_FIREWALL:
1177 off_nl = 44; /* Ethernet II */
1178 off_nl_nosnap = 44; /* XXX - what does it do with 802.3 packets? */
1183 /* XXX read this from pf.h? */
1184 off_nl = PFLOG_HDRLEN;
1185 off_nl_nosnap = PFLOG_HDRLEN; /* no 802.2 LLC */
1188 case DLT_JUNIPER_MFR:
1189 case DLT_JUNIPER_MLFR:
1190 case DLT_JUNIPER_MLPPP:
1191 case DLT_JUNIPER_PPP:
1192 case DLT_JUNIPER_CHDLC:
1193 case DLT_JUNIPER_FRELAY:
1196 off_nl_nosnap = -1; /* no 802.2 LLC */
1199 case DLT_JUNIPER_ATM1:
1200 off_linktype = 4; /* in reality variable between 4-8 */
1205 case DLT_JUNIPER_ATM2:
1206 off_linktype = 8; /* in reality variable between 8-12 */
1211 /* frames captured on a Juniper PPPoE service PIC
1212 * contain raw ethernet frames */
1213 case DLT_JUNIPER_PPPOE:
1214 case DLT_JUNIPER_ETHER:
1216 off_nl = 18; /* Ethernet II */
1217 off_nl_nosnap = 21; /* 802.3+802.2 */
1220 case DLT_JUNIPER_PPPOE_ATM:
1223 off_nl_nosnap = -1; /* no 802.2 LLC */
1226 case DLT_JUNIPER_GGSN:
1229 off_nl_nosnap = -1; /* no 802.2 LLC */
1232 case DLT_JUNIPER_ES:
1234 off_nl = -1; /* not really a network layer but raw IP adresses */
1235 off_nl_nosnap = -1; /* no 802.2 LLC */
1238 case DLT_JUNIPER_MONITOR:
1240 off_nl = 12; /* raw IP/IP6 header */
1241 off_nl_nosnap = -1; /* no 802.2 LLC */
1244 case DLT_JUNIPER_SERVICES:
1246 off_nl = -1; /* L3 proto location dep. on cookie type */
1247 off_nl_nosnap = -1; /* no 802.2 LLC */
1250 case DLT_JUNIPER_VP:
1267 case DLT_MTP2_WITH_PHDR:
1286 case DLT_LINUX_LAPD:
1288 * Currently, only raw "link[N:M]" filtering is supported.
1297 * Currently, only raw "link[N:M]" filtering is supported.
1304 case DLT_BLUETOOTH_HCI_H4:
1306 * Currently, only raw "link[N:M]" filtering is supported.
1313 bpf_error("unknown data link type %d", linktype);
1318 * Load a value relative to the beginning of the link-layer header.
1319 * The link-layer header doesn't necessarily begin at the beginning
1320 * of the packet data; there might be a variable-length prefix containing
1321 * radio information.
1323 static struct slist *
1324 gen_load_llrel(offset, size)
1327 struct slist *s, *s2;
1329 s = gen_llprefixlen();
1332 * If "s" is non-null, it has code to arrange that the X register
1333 * contains the length of the prefix preceding the link-layer
1336 * Otherwise, the length of the prefix preceding the link-layer
1337 * header is "off_ll".
1341 * There's a variable-length prefix preceding the
1342 * link-layer header. "s" points to a list of statements
1343 * that put the length of that prefix into the X register.
1344 * do an indirect load, to use the X register as an offset.
1346 s2 = new_stmt(BPF_LD|BPF_IND|size);
1351 * There is no variable-length header preceding the
1352 * link-layer header; add in off_ll, which, if there's
1353 * a fixed-length header preceding the link-layer header,
1354 * is the length of that header.
1356 s = new_stmt(BPF_LD|BPF_ABS|size);
1357 s->s.k = offset + off_ll;
1364 * Load a value relative to the beginning of the specified header.
1366 static struct slist *
1367 gen_load_a(offrel, offset, size)
1368 enum e_offrel offrel;
1371 struct slist *s, *s2;
1376 s = new_stmt(BPF_LD|BPF_ABS|size);
1381 s = gen_load_llrel(offset, size);
1385 s = gen_load_llrel(off_nl + offset, size);
1389 s = gen_load_llrel(off_nl_nosnap + offset, size);
1394 * Load the X register with the length of the IPv4 header
1395 * (plus the offset of the link-layer header, if it's
1396 * preceded by a variable-length header such as a radio
1397 * header), in bytes.
1399 s = gen_loadx_iphdrlen();
1402 * Load the item at {offset of the link-layer header} +
1403 * {offset, relative to the start of the link-layer
1404 * header, of the IPv4 header} + {length of the IPv4 header} +
1405 * {specified offset}.
1407 * (If the link-layer is variable-length, it's included
1408 * in the value in the X register, and off_ll is 0.)
1410 s2 = new_stmt(BPF_LD|BPF_IND|size);
1411 s2->s.k = off_ll + off_nl + offset;
1416 s = gen_load_llrel(off_nl + 40 + offset, size);
1427 * Generate code to load into the X register the sum of the length of
1428 * the IPv4 header and any variable-length header preceding the link-layer
1431 static struct slist *
1432 gen_loadx_iphdrlen()
1434 struct slist *s, *s2;
1436 s = gen_llprefixlen();
1439 * There's a variable-length prefix preceding the
1440 * link-layer header. "s" points to a list of statements
1441 * that put the length of that prefix into the X register.
1442 * The 4*([k]&0xf) addressing mode can't be used, as we
1443 * don't have a constant offset, so we have to load the
1444 * value in question into the A register and add to it
1445 * the value from the X register.
1447 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1450 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1453 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1458 * The A register now contains the length of the
1459 * IP header. We need to add to it the length
1460 * of the prefix preceding the link-layer
1461 * header, which is still in the X register, and
1462 * move the result into the X register.
1464 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1465 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1468 * There is no variable-length header preceding the
1469 * link-layer header; add in off_ll, which, if there's
1470 * a fixed-length header preceding the link-layer header,
1471 * is the length of that header.
1473 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1474 s->s.k = off_ll + off_nl;
1479 static struct block *
1486 s = new_stmt(BPF_LD|BPF_IMM);
1488 b = new_block(JMP(BPF_JEQ));
1494 static inline struct block *
1497 return gen_uncond(1);
1500 static inline struct block *
1503 return gen_uncond(0);
1507 * Byte-swap a 32-bit number.
1508 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1509 * big-endian platforms.)
1511 #define SWAPLONG(y) \
1512 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1515 * Generate code to match a particular packet type.
1517 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1518 * value, if <= ETHERMTU. We use that to determine whether to
1519 * match the type/length field or to check the type/length field for
1520 * a value <= ETHERMTU to see whether it's a type field and then do
1521 * the appropriate test.
1523 static struct block *
1524 gen_ether_linktype(proto)
1527 struct block *b0, *b1;
1533 case LLCSAP_NETBEUI:
1535 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1536 * so we check the DSAP and SSAP.
1538 * LLCSAP_IP checks for IP-over-802.2, rather
1539 * than IP-over-Ethernet or IP-over-SNAP.
1541 * XXX - should we check both the DSAP and the
1542 * SSAP, like this, or should we check just the
1543 * DSAP, as we do for other types <= ETHERMTU
1544 * (i.e., other SAP values)?
1546 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1548 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1549 ((proto << 8) | proto));
1557 * Ethernet_II frames, which are Ethernet
1558 * frames with a frame type of ETHERTYPE_IPX;
1560 * Ethernet_802.3 frames, which are 802.3
1561 * frames (i.e., the type/length field is
1562 * a length field, <= ETHERMTU, rather than
1563 * a type field) with the first two bytes
1564 * after the Ethernet/802.3 header being
1567 * Ethernet_802.2 frames, which are 802.3
1568 * frames with an 802.2 LLC header and
1569 * with the IPX LSAP as the DSAP in the LLC
1572 * Ethernet_SNAP frames, which are 802.3
1573 * frames with an LLC header and a SNAP
1574 * header and with an OUI of 0x000000
1575 * (encapsulated Ethernet) and a protocol
1576 * ID of ETHERTYPE_IPX in the SNAP header.
1578 * XXX - should we generate the same code both
1579 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1583 * This generates code to check both for the
1584 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1586 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1587 (bpf_int32)LLCSAP_IPX);
1588 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H,
1593 * Now we add code to check for SNAP frames with
1594 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1596 b0 = gen_snap(0x000000, ETHERTYPE_IPX, 14);
1600 * Now we generate code to check for 802.3
1601 * frames in general.
1603 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1607 * Now add the check for 802.3 frames before the
1608 * check for Ethernet_802.2 and Ethernet_802.3,
1609 * as those checks should only be done on 802.3
1610 * frames, not on Ethernet frames.
1615 * Now add the check for Ethernet_II frames, and
1616 * do that before checking for the other frame
1619 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1620 (bpf_int32)ETHERTYPE_IPX);
1624 case ETHERTYPE_ATALK:
1625 case ETHERTYPE_AARP:
1627 * EtherTalk (AppleTalk protocols on Ethernet link
1628 * layer) may use 802.2 encapsulation.
1632 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1633 * we check for an Ethernet type field less than
1634 * 1500, which means it's an 802.3 length field.
1636 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1640 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1641 * SNAP packets with an organization code of
1642 * 0x080007 (Apple, for Appletalk) and a protocol
1643 * type of ETHERTYPE_ATALK (Appletalk).
1645 * 802.2-encapsulated ETHERTYPE_AARP packets are
1646 * SNAP packets with an organization code of
1647 * 0x000000 (encapsulated Ethernet) and a protocol
1648 * type of ETHERTYPE_AARP (Appletalk ARP).
1650 if (proto == ETHERTYPE_ATALK)
1651 b1 = gen_snap(0x080007, ETHERTYPE_ATALK, 14);
1652 else /* proto == ETHERTYPE_AARP */
1653 b1 = gen_snap(0x000000, ETHERTYPE_AARP, 14);
1657 * Check for Ethernet encapsulation (Ethertalk
1658 * phase 1?); we just check for the Ethernet
1661 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1667 if (proto <= ETHERMTU) {
1669 * This is an LLC SAP value, so the frames
1670 * that match would be 802.2 frames.
1671 * Check that the frame is an 802.2 frame
1672 * (i.e., that the length/type field is
1673 * a length field, <= ETHERMTU) and
1674 * then check the DSAP.
1676 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1678 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1684 * This is an Ethernet type, so compare
1685 * the length/type field with it (if
1686 * the frame is an 802.2 frame, the length
1687 * field will be <= ETHERMTU, and, as
1688 * "proto" is > ETHERMTU, this test
1689 * will fail and the frame won't match,
1690 * which is what we want).
1692 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1699 * Generate code to match a particular packet type.
1701 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1702 * value, if <= ETHERMTU. We use that to determine whether to
1703 * match the type field or to check the type field for the special
1704 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1706 static struct block *
1707 gen_linux_sll_linktype(proto)
1710 struct block *b0, *b1;
1716 case LLCSAP_NETBEUI:
1718 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1719 * so we check the DSAP and SSAP.
1721 * LLCSAP_IP checks for IP-over-802.2, rather
1722 * than IP-over-Ethernet or IP-over-SNAP.
1724 * XXX - should we check both the DSAP and the
1725 * SSAP, like this, or should we check just the
1726 * DSAP, as we do for other types <= ETHERMTU
1727 * (i.e., other SAP values)?
1729 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1730 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1731 ((proto << 8) | proto));
1737 * Ethernet_II frames, which are Ethernet
1738 * frames with a frame type of ETHERTYPE_IPX;
1740 * Ethernet_802.3 frames, which have a frame
1741 * type of LINUX_SLL_P_802_3;
1743 * Ethernet_802.2 frames, which are 802.3
1744 * frames with an 802.2 LLC header (i.e, have
1745 * a frame type of LINUX_SLL_P_802_2) and
1746 * with the IPX LSAP as the DSAP in the LLC
1749 * Ethernet_SNAP frames, which are 802.3
1750 * frames with an LLC header and a SNAP
1751 * header and with an OUI of 0x000000
1752 * (encapsulated Ethernet) and a protocol
1753 * ID of ETHERTYPE_IPX in the SNAP header.
1755 * First, do the checks on LINUX_SLL_P_802_2
1756 * frames; generate the check for either
1757 * Ethernet_802.2 or Ethernet_SNAP frames, and
1758 * then put a check for LINUX_SLL_P_802_2 frames
1761 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1762 (bpf_int32)LLCSAP_IPX);
1763 b1 = gen_snap(0x000000, ETHERTYPE_IPX,
1766 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1770 * Now check for 802.3 frames and OR that with
1771 * the previous test.
1773 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1777 * Now add the check for Ethernet_II frames, and
1778 * do that before checking for the other frame
1781 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1782 (bpf_int32)ETHERTYPE_IPX);
1786 case ETHERTYPE_ATALK:
1787 case ETHERTYPE_AARP:
1789 * EtherTalk (AppleTalk protocols on Ethernet link
1790 * layer) may use 802.2 encapsulation.
1794 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1795 * we check for the 802.2 protocol type in the
1796 * "Ethernet type" field.
1798 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1801 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1802 * SNAP packets with an organization code of
1803 * 0x080007 (Apple, for Appletalk) and a protocol
1804 * type of ETHERTYPE_ATALK (Appletalk).
1806 * 802.2-encapsulated ETHERTYPE_AARP packets are
1807 * SNAP packets with an organization code of
1808 * 0x000000 (encapsulated Ethernet) and a protocol
1809 * type of ETHERTYPE_AARP (Appletalk ARP).
1811 if (proto == ETHERTYPE_ATALK)
1812 b1 = gen_snap(0x080007, ETHERTYPE_ATALK,
1814 else /* proto == ETHERTYPE_AARP */
1815 b1 = gen_snap(0x000000, ETHERTYPE_AARP,
1820 * Check for Ethernet encapsulation (Ethertalk
1821 * phase 1?); we just check for the Ethernet
1824 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1830 if (proto <= ETHERMTU) {
1832 * This is an LLC SAP value, so the frames
1833 * that match would be 802.2 frames.
1834 * Check for the 802.2 protocol type
1835 * in the "Ethernet type" field, and
1836 * then check the DSAP.
1838 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1840 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1846 * This is an Ethernet type, so compare
1847 * the length/type field with it (if
1848 * the frame is an 802.2 frame, the length
1849 * field will be <= ETHERMTU, and, as
1850 * "proto" is > ETHERMTU, this test
1851 * will fail and the frame won't match,
1852 * which is what we want).
1854 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1861 insert_radiotap_load_llprefixlen(b)
1864 struct slist *s1, *s2;
1867 * Prepend to the statements in this block code to load the
1868 * length of the radiotap header into the register assigned
1869 * to hold that length, if one has been assigned.
1871 if (reg_ll_size != -1) {
1873 * The 2 bytes at offsets of 2 and 3 from the beginning
1874 * of the radiotap header are the length of the radiotap
1875 * header; unfortunately, it's little-endian, so we have
1876 * to load it a byte at a time and construct the value.
1880 * Load the high-order byte, at an offset of 3, shift it
1881 * left a byte, and put the result in the X register.
1883 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1885 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1888 s2 = new_stmt(BPF_MISC|BPF_TAX);
1892 * Load the next byte, at an offset of 2, and OR the
1893 * value from the X register into it.
1895 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1898 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1902 * Now allocate a register to hold that value and store
1905 s2 = new_stmt(BPF_ST);
1906 s2->s.k = reg_ll_size;
1910 * Now move it into the X register.
1912 s2 = new_stmt(BPF_MISC|BPF_TAX);
1916 * Now append all the existing statements in this
1917 * block to these statements.
1919 sappend(s1, b->stmts);
1925 * At the moment we treat PPI as normal Radiotap encoded
1926 * packets. The difference is in the function that generates
1927 * the code at the beginning to compute the header length.
1928 * Since this code generator of PPI supports bare 802.11
1929 * encapsulation only (i.e. the encapsulated DLT should be
1930 * DLT_IEEE802_11) we generate code to check for this too.
1933 insert_ppi_load_llprefixlen(b)
1936 struct slist *s1, *s2;
1939 * Prepend to the statements in this block code to load the
1940 * length of the radiotap header into the register assigned
1941 * to hold that length, if one has been assigned.
1943 if (reg_ll_size != -1) {
1945 * The 2 bytes at offsets of 2 and 3 from the beginning
1946 * of the radiotap header are the length of the radiotap
1947 * header; unfortunately, it's little-endian, so we have
1948 * to load it a byte at a time and construct the value.
1952 * Load the high-order byte, at an offset of 3, shift it
1953 * left a byte, and put the result in the X register.
1955 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1957 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1960 s2 = new_stmt(BPF_MISC|BPF_TAX);
1964 * Load the next byte, at an offset of 2, and OR the
1965 * value from the X register into it.
1967 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1970 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1974 * Now allocate a register to hold that value and store
1977 s2 = new_stmt(BPF_ST);
1978 s2->s.k = reg_ll_size;
1982 * Now move it into the X register.
1984 s2 = new_stmt(BPF_MISC|BPF_TAX);
1988 * Now append all the existing statements in this
1989 * block to these statements.
1991 sappend(s1, b->stmts);
1997 static struct block *
1998 gen_ppi_dlt_check(void)
2000 struct slist *s_load_dlt;
2003 if (linktype == DLT_PPI)
2005 /* Create the statements that check for the DLT
2007 s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2008 s_load_dlt->s.k = 4;
2010 b = new_block(JMP(BPF_JEQ));
2012 b->stmts = s_load_dlt;
2013 b->s.k = SWAPLONG(DLT_IEEE802_11);
2024 insert_load_llprefixlen(b)
2030 * At the moment we treat PPI as normal Radiotap encoded
2031 * packets. The difference is in the function that generates
2032 * the code at the beginning to compute the header length.
2033 * Since this code generator of PPI supports bare 802.11
2034 * encapsulation only (i.e. the encapsulated DLT should be
2035 * DLT_IEEE802_11) we generate code to check for this too.
2038 insert_ppi_load_llprefixlen(b);
2041 case DLT_IEEE802_11_RADIO:
2042 insert_radiotap_load_llprefixlen(b);
2048 static struct slist *
2049 gen_radiotap_llprefixlen(void)
2053 if (reg_ll_size == -1) {
2055 * We haven't yet assigned a register for the length
2056 * of the radiotap header; allocate one.
2058 reg_ll_size = alloc_reg();
2062 * Load the register containing the radiotap length
2063 * into the X register.
2065 s = new_stmt(BPF_LDX|BPF_MEM);
2066 s->s.k = reg_ll_size;
2071 * At the moment we treat PPI as normal Radiotap encoded
2072 * packets. The difference is in the function that generates
2073 * the code at the beginning to compute the header length.
2074 * Since this code generator of PPI supports bare 802.11
2075 * encapsulation only (i.e. the encapsulated DLT should be
2076 * DLT_IEEE802_11) we generate code to check for this too.
2078 static struct slist *
2079 gen_ppi_llprefixlen(void)
2083 if (reg_ll_size == -1) {
2085 * We haven't yet assigned a register for the length
2086 * of the radiotap header; allocate one.
2088 reg_ll_size = alloc_reg();
2092 * Load the register containing the radiotap length
2093 * into the X register.
2095 s = new_stmt(BPF_LDX|BPF_MEM);
2096 s->s.k = reg_ll_size;
2103 * Generate code to compute the link-layer header length, if necessary,
2104 * putting it into the X register, and to return either a pointer to a
2105 * "struct slist" for the list of statements in that code, or NULL if
2106 * no code is necessary.
2108 static struct slist *
2109 gen_llprefixlen(void)
2114 return gen_ppi_llprefixlen();
2117 case DLT_IEEE802_11_RADIO:
2118 return gen_radiotap_llprefixlen();
2126 * Generate code to match a particular packet type by matching the
2127 * link-layer type field or fields in the 802.2 LLC header.
2129 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2130 * value, if <= ETHERMTU.
2132 static struct block *
2136 struct block *b0, *b1, *b2;
2138 /* are we checking MPLS-encapsulated packets? */
2139 if (label_stack_depth > 0) {
2143 /* FIXME add other L3 proto IDs */
2144 return gen_mpls_linktype(Q_IP);
2146 case ETHERTYPE_IPV6:
2148 /* FIXME add other L3 proto IDs */
2149 return gen_mpls_linktype(Q_IPV6);
2152 bpf_error("unsupported protocol over mpls");
2160 return gen_ether_linktype(proto);
2168 proto = (proto << 8 | LLCSAP_ISONS);
2172 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2182 case DLT_IEEE802_11:
2183 case DLT_IEEE802_11_RADIO_AVS:
2184 case DLT_IEEE802_11_RADIO:
2185 case DLT_PRISM_HEADER:
2186 case DLT_ATM_RFC1483:
2188 case DLT_IP_OVER_FC:
2189 return gen_llc_linktype(proto);
2195 * If "is_lane" is set, check for a LANE-encapsulated
2196 * version of this protocol, otherwise check for an
2197 * LLC-encapsulated version of this protocol.
2199 * We assume LANE means Ethernet, not Token Ring.
2203 * Check that the packet doesn't begin with an
2204 * LE Control marker. (We've already generated
2207 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
2212 * Now generate an Ethernet test.
2214 b1 = gen_ether_linktype(proto);
2219 * Check for LLC encapsulation and then check the
2222 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
2223 b1 = gen_llc_linktype(proto);
2231 return gen_linux_sll_linktype(proto);
2236 case DLT_SLIP_BSDOS:
2239 * These types don't provide any type field; packets
2240 * are always IPv4 or IPv6.
2242 * XXX - for IPv4, check for a version number of 4, and,
2243 * for IPv6, check for a version number of 6?
2248 /* Check for a version number of 4. */
2249 return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
2251 case ETHERTYPE_IPV6:
2252 /* Check for a version number of 6. */
2253 return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
2257 return gen_false(); /* always false */
2264 case DLT_PPP_SERIAL:
2267 * We use Ethernet protocol types inside libpcap;
2268 * map them to the corresponding PPP protocol types.
2277 case ETHERTYPE_IPV6:
2286 case ETHERTYPE_ATALK:
2300 * I'm assuming the "Bridging PDU"s that go
2301 * over PPP are Spanning Tree Protocol
2315 * We use Ethernet protocol types inside libpcap;
2316 * map them to the corresponding PPP protocol types.
2321 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
2322 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
2324 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
2329 case ETHERTYPE_IPV6:
2339 case ETHERTYPE_ATALK:
2353 * I'm assuming the "Bridging PDU"s that go
2354 * over PPP are Spanning Tree Protocol
2370 * For DLT_NULL, the link-layer header is a 32-bit
2371 * word containing an AF_ value in *host* byte order,
2372 * and for DLT_ENC, the link-layer header begins
2373 * with a 32-bit work containing an AF_ value in
2376 * In addition, if we're reading a saved capture file,
2377 * the host byte order in the capture may not be the
2378 * same as the host byte order on this machine.
2380 * For DLT_LOOP, the link-layer header is a 32-bit
2381 * word containing an AF_ value in *network* byte order.
2383 * XXX - AF_ values may, unfortunately, be platform-
2384 * dependent; for example, FreeBSD's AF_INET6 is 24
2385 * whilst NetBSD's and OpenBSD's is 26.
2387 * This means that, when reading a capture file, just
2388 * checking for our AF_INET6 value won't work if the
2389 * capture file came from another OS.
2398 case ETHERTYPE_IPV6:
2405 * Not a type on which we support filtering.
2406 * XXX - support those that have AF_ values
2407 * #defined on this platform, at least?
2412 if (linktype == DLT_NULL || linktype == DLT_ENC) {
2414 * The AF_ value is in host byte order, but
2415 * the BPF interpreter will convert it to
2416 * network byte order.
2418 * If this is a save file, and it's from a
2419 * machine with the opposite byte order to
2420 * ours, we byte-swap the AF_ value.
2422 * Then we run it through "htonl()", and
2423 * generate code to compare against the result.
2425 if (bpf_pcap->sf.rfile != NULL &&
2426 bpf_pcap->sf.swapped)
2427 proto = SWAPLONG(proto);
2428 proto = htonl(proto);
2430 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
2434 * af field is host byte order in contrast to the rest of
2437 if (proto == ETHERTYPE_IP)
2438 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2439 BPF_B, (bpf_int32)AF_INET));
2441 else if (proto == ETHERTYPE_IPV6)
2442 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2443 BPF_B, (bpf_int32)AF_INET6));
2451 case DLT_ARCNET_LINUX:
2453 * XXX should we check for first fragment if the protocol
2462 case ETHERTYPE_IPV6:
2463 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2464 (bpf_int32)ARCTYPE_INET6));
2468 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2469 (bpf_int32)ARCTYPE_IP);
2470 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2471 (bpf_int32)ARCTYPE_IP_OLD);
2476 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2477 (bpf_int32)ARCTYPE_ARP);
2478 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2479 (bpf_int32)ARCTYPE_ARP_OLD);
2483 case ETHERTYPE_REVARP:
2484 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2485 (bpf_int32)ARCTYPE_REVARP));
2487 case ETHERTYPE_ATALK:
2488 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2489 (bpf_int32)ARCTYPE_ATALK));
2496 case ETHERTYPE_ATALK:
2506 * XXX - assumes a 2-byte Frame Relay header with
2507 * DLCI and flags. What if the address is longer?
2513 * Check for the special NLPID for IP.
2515 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
2518 case ETHERTYPE_IPV6:
2520 * Check for the special NLPID for IPv6.
2522 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
2527 * Check for several OSI protocols.
2529 * Frame Relay packets typically have an OSI
2530 * NLPID at the beginning; we check for each
2533 * What we check for is the NLPID and a frame
2534 * control field of UI, i.e. 0x03 followed
2537 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
2538 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
2539 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
2550 case DLT_JUNIPER_MFR:
2551 case DLT_JUNIPER_MLFR:
2552 case DLT_JUNIPER_MLPPP:
2553 case DLT_JUNIPER_ATM1:
2554 case DLT_JUNIPER_ATM2:
2555 case DLT_JUNIPER_PPPOE:
2556 case DLT_JUNIPER_PPPOE_ATM:
2557 case DLT_JUNIPER_GGSN:
2558 case DLT_JUNIPER_ES:
2559 case DLT_JUNIPER_MONITOR:
2560 case DLT_JUNIPER_SERVICES:
2561 case DLT_JUNIPER_ETHER:
2562 case DLT_JUNIPER_PPP:
2563 case DLT_JUNIPER_FRELAY:
2564 case DLT_JUNIPER_CHDLC:
2565 case DLT_JUNIPER_VP:
2566 /* just lets verify the magic number for now -
2567 * on ATM we may have up to 6 different encapsulations on the wire
2568 * and need a lot of heuristics to figure out that the payload
2571 * FIXME encapsulation specific BPF_ filters
2573 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
2575 case DLT_LINUX_IRDA:
2576 bpf_error("IrDA link-layer type filtering not implemented");
2579 bpf_error("DOCSIS link-layer type filtering not implemented");
2581 case DLT_LINUX_LAPD:
2582 bpf_error("LAPD link-layer type filtering not implemented");
2586 * All the types that have no encapsulation should either be
2587 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2588 * all packets are IP packets, or should be handled in some
2589 * special case, if none of them are (if some are and some
2590 * aren't, the lack of encapsulation is a problem, as we'd
2591 * have to find some other way of determining the packet type).
2593 * Therefore, if "off_linktype" is -1, there's an error.
2595 if (off_linktype == (u_int)-1)
2599 * Any type not handled above should always have an Ethernet
2600 * type at an offset of "off_linktype". (PPP is partially
2601 * handled above - the protocol type is mapped from the
2602 * Ethernet and LLC types we use internally to the corresponding
2603 * PPP type - but the PPP type is always specified by a value
2604 * at "off_linktype", so we don't have to do the code generation
2607 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2611 * Check for an LLC SNAP packet with a given organization code and
2612 * protocol type; we check the entire contents of the 802.2 LLC and
2613 * snap headers, checking for DSAP and SSAP of SNAP and a control
2614 * field of 0x03 in the LLC header, and for the specified organization
2615 * code and protocol type in the SNAP header.
2617 static struct block *
2618 gen_snap(orgcode, ptype, offset)
2619 bpf_u_int32 orgcode;
2623 u_char snapblock[8];
2625 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
2626 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
2627 snapblock[2] = 0x03; /* control = UI */
2628 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
2629 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
2630 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
2631 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
2632 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
2633 return gen_bcmp(OR_LINK, offset, 8, snapblock);
2637 * Generate code to match a particular packet type, for link-layer types
2638 * using 802.2 LLC headers.
2640 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2641 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2643 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2644 * value, if <= ETHERMTU. We use that to determine whether to
2645 * match the DSAP or both DSAP and LSAP or to check the OUI and
2646 * protocol ID in a SNAP header.
2648 static struct block *
2649 gen_llc_linktype(proto)
2653 * XXX - handle token-ring variable-length header.
2659 case LLCSAP_NETBEUI:
2661 * XXX - should we check both the DSAP and the
2662 * SSAP, like this, or should we check just the
2663 * DSAP, as we do for other types <= ETHERMTU
2664 * (i.e., other SAP values)?
2666 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_u_int32)
2667 ((proto << 8) | proto));
2671 * XXX - are there ever SNAP frames for IPX on
2672 * non-Ethernet 802.x networks?
2674 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2675 (bpf_int32)LLCSAP_IPX);
2677 case ETHERTYPE_ATALK:
2679 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2680 * SNAP packets with an organization code of
2681 * 0x080007 (Apple, for Appletalk) and a protocol
2682 * type of ETHERTYPE_ATALK (Appletalk).
2684 * XXX - check for an organization code of
2685 * encapsulated Ethernet as well?
2687 return gen_snap(0x080007, ETHERTYPE_ATALK, off_linktype);
2691 * XXX - we don't have to check for IPX 802.3
2692 * here, but should we check for the IPX Ethertype?
2694 if (proto <= ETHERMTU) {
2696 * This is an LLC SAP value, so check
2699 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2703 * This is an Ethernet type; we assume that it's
2704 * unlikely that it'll appear in the right place
2705 * at random, and therefore check only the
2706 * location that would hold the Ethernet type
2707 * in a SNAP frame with an organization code of
2708 * 0x000000 (encapsulated Ethernet).
2710 * XXX - if we were to check for the SNAP DSAP and
2711 * LSAP, as per XXX, and were also to check for an
2712 * organization code of 0x000000 (encapsulated
2713 * Ethernet), we'd do
2715 * return gen_snap(0x000000, proto,
2718 * here; for now, we don't, as per the above.
2719 * I don't know whether it's worth the extra CPU
2720 * time to do the right check or not.
2722 return gen_cmp(OR_LINK, off_linktype+6, BPF_H,
2728 static struct block *
2729 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
2733 u_int src_off, dst_off;
2735 struct block *b0, *b1;
2749 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2750 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2756 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2757 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2764 b0 = gen_linktype(proto);
2765 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
2771 static struct block *
2772 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
2773 struct in6_addr *addr;
2774 struct in6_addr *mask;
2776 u_int src_off, dst_off;
2778 struct block *b0, *b1;
2793 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2794 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2800 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2801 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2808 /* this order is important */
2809 a = (u_int32_t *)addr;
2810 m = (u_int32_t *)mask;
2811 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
2812 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
2814 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
2816 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
2818 b0 = gen_linktype(proto);
2824 static struct block *
2825 gen_ehostop(eaddr, dir)
2826 register const u_char *eaddr;
2829 register struct block *b0, *b1;
2833 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
2836 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
2839 b0 = gen_ehostop(eaddr, Q_SRC);
2840 b1 = gen_ehostop(eaddr, Q_DST);
2846 b0 = gen_ehostop(eaddr, Q_SRC);
2847 b1 = gen_ehostop(eaddr, Q_DST);
2856 * Like gen_ehostop, but for DLT_FDDI
2858 static struct block *
2859 gen_fhostop(eaddr, dir)
2860 register const u_char *eaddr;
2863 struct block *b0, *b1;
2868 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
2870 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
2875 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
2877 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
2881 b0 = gen_fhostop(eaddr, Q_SRC);
2882 b1 = gen_fhostop(eaddr, Q_DST);
2888 b0 = gen_fhostop(eaddr, Q_SRC);
2889 b1 = gen_fhostop(eaddr, Q_DST);
2898 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2900 static struct block *
2901 gen_thostop(eaddr, dir)
2902 register const u_char *eaddr;
2905 register struct block *b0, *b1;
2909 return gen_bcmp(OR_LINK, 8, 6, eaddr);
2912 return gen_bcmp(OR_LINK, 2, 6, eaddr);
2915 b0 = gen_thostop(eaddr, Q_SRC);
2916 b1 = gen_thostop(eaddr, Q_DST);
2922 b0 = gen_thostop(eaddr, Q_SRC);
2923 b1 = gen_thostop(eaddr, Q_DST);
2932 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2934 static struct block *
2935 gen_wlanhostop(eaddr, dir)
2936 register const u_char *eaddr;
2939 register struct block *b0, *b1, *b2;
2940 register struct slist *s;
2947 * For control frames, there is no SA.
2949 * For management frames, SA is at an
2950 * offset of 10 from the beginning of
2953 * For data frames, SA is at an offset
2954 * of 10 from the beginning of the packet
2955 * if From DS is clear, at an offset of
2956 * 16 from the beginning of the packet
2957 * if From DS is set and To DS is clear,
2958 * and an offset of 24 from the beginning
2959 * of the packet if From DS is set and To DS
2964 * Generate the tests to be done for data frames
2967 * First, check for To DS set, i.e. check "link[1] & 0x01".
2969 s = gen_load_a(OR_LINK, 1, BPF_B);
2970 b1 = new_block(JMP(BPF_JSET));
2971 b1->s.k = 0x01; /* To DS */
2975 * If To DS is set, the SA is at 24.
2977 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
2981 * Now, check for To DS not set, i.e. check
2982 * "!(link[1] & 0x01)".
2984 s = gen_load_a(OR_LINK, 1, BPF_B);
2985 b2 = new_block(JMP(BPF_JSET));
2986 b2->s.k = 0x01; /* To DS */
2991 * If To DS is not set, the SA is at 16.
2993 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
2997 * Now OR together the last two checks. That gives
2998 * the complete set of checks for data frames with
3004 * Now check for From DS being set, and AND that with
3005 * the ORed-together checks.
3007 s = gen_load_a(OR_LINK, 1, BPF_B);
3008 b1 = new_block(JMP(BPF_JSET));
3009 b1->s.k = 0x02; /* From DS */
3014 * Now check for data frames with From DS not set.
3016 s = gen_load_a(OR_LINK, 1, BPF_B);
3017 b2 = new_block(JMP(BPF_JSET));
3018 b2->s.k = 0x02; /* From DS */
3023 * If From DS isn't set, the SA is at 10.
3025 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3029 * Now OR together the checks for data frames with
3030 * From DS not set and for data frames with From DS
3031 * set; that gives the checks done for data frames.
3036 * Now check for a data frame.
3037 * I.e, check "link[0] & 0x08".
3039 gen_load_a(OR_LINK, 0, BPF_B);
3040 b1 = new_block(JMP(BPF_JSET));
3045 * AND that with the checks done for data frames.
3050 * If the high-order bit of the type value is 0, this
3051 * is a management frame.
3052 * I.e, check "!(link[0] & 0x08)".
3054 s = gen_load_a(OR_LINK, 0, BPF_B);
3055 b2 = new_block(JMP(BPF_JSET));
3061 * For management frames, the SA is at 10.
3063 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3067 * OR that with the checks done for data frames.
3068 * That gives the checks done for management and
3074 * If the low-order bit of the type value is 1,
3075 * this is either a control frame or a frame
3076 * with a reserved type, and thus not a
3079 * I.e., check "!(link[0] & 0x04)".
3081 s = gen_load_a(OR_LINK, 0, BPF_B);
3082 b1 = new_block(JMP(BPF_JSET));
3088 * AND that with the checks for data and management
3098 * For control frames, there is no DA.
3100 * For management frames, DA is at an
3101 * offset of 4 from the beginning of
3104 * For data frames, DA is at an offset
3105 * of 4 from the beginning of the packet
3106 * if To DS is clear and at an offset of
3107 * 16 from the beginning of the packet
3112 * Generate the tests to be done for data frames.
3114 * First, check for To DS set, i.e. "link[1] & 0x01".
3116 s = gen_load_a(OR_LINK, 1, BPF_B);
3117 b1 = new_block(JMP(BPF_JSET));
3118 b1->s.k = 0x01; /* To DS */
3122 * If To DS is set, the DA is at 16.
3124 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3128 * Now, check for To DS not set, i.e. check
3129 * "!(link[1] & 0x01)".
3131 s = gen_load_a(OR_LINK, 1, BPF_B);
3132 b2 = new_block(JMP(BPF_JSET));
3133 b2->s.k = 0x01; /* To DS */
3138 * If To DS is not set, the DA is at 4.
3140 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
3144 * Now OR together the last two checks. That gives
3145 * the complete set of checks for data frames.
3150 * Now check for a data frame.
3151 * I.e, check "link[0] & 0x08".
3153 s = gen_load_a(OR_LINK, 0, BPF_B);
3154 b1 = new_block(JMP(BPF_JSET));
3159 * AND that with the checks done for data frames.
3164 * If the high-order bit of the type value is 0, this
3165 * is a management frame.
3166 * I.e, check "!(link[0] & 0x08)".
3168 s = gen_load_a(OR_LINK, 0, BPF_B);
3169 b2 = new_block(JMP(BPF_JSET));
3175 * For management frames, the DA is at 4.
3177 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
3181 * OR that with the checks done for data frames.
3182 * That gives the checks done for management and
3188 * If the low-order bit of the type value is 1,
3189 * this is either a control frame or a frame
3190 * with a reserved type, and thus not a
3193 * I.e., check "!(link[0] & 0x04)".
3195 s = gen_load_a(OR_LINK, 0, BPF_B);
3196 b1 = new_block(JMP(BPF_JSET));
3202 * AND that with the checks for data and management
3209 b0 = gen_wlanhostop(eaddr, Q_SRC);
3210 b1 = gen_wlanhostop(eaddr, Q_DST);
3216 b0 = gen_wlanhostop(eaddr, Q_SRC);
3217 b1 = gen_wlanhostop(eaddr, Q_DST);
3226 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3227 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3228 * as the RFC states.)
3230 static struct block *
3231 gen_ipfchostop(eaddr, dir)
3232 register const u_char *eaddr;
3235 register struct block *b0, *b1;
3239 return gen_bcmp(OR_LINK, 10, 6, eaddr);
3242 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3245 b0 = gen_ipfchostop(eaddr, Q_SRC);
3246 b1 = gen_ipfchostop(eaddr, Q_DST);
3252 b0 = gen_ipfchostop(eaddr, Q_SRC);
3253 b1 = gen_ipfchostop(eaddr, Q_DST);
3262 * This is quite tricky because there may be pad bytes in front of the
3263 * DECNET header, and then there are two possible data packet formats that
3264 * carry both src and dst addresses, plus 5 packet types in a format that
3265 * carries only the src node, plus 2 types that use a different format and
3266 * also carry just the src node.
3270 * Instead of doing those all right, we just look for data packets with
3271 * 0 or 1 bytes of padding. If you want to look at other packets, that
3272 * will require a lot more hacking.
3274 * To add support for filtering on DECNET "areas" (network numbers)
3275 * one would want to add a "mask" argument to this routine. That would
3276 * make the filter even more inefficient, although one could be clever
3277 * and not generate masking instructions if the mask is 0xFFFF.
3279 static struct block *
3280 gen_dnhostop(addr, dir)
3284 struct block *b0, *b1, *b2, *tmp;
3285 u_int offset_lh; /* offset if long header is received */
3286 u_int offset_sh; /* offset if short header is received */
3291 offset_sh = 1; /* follows flags */
3292 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
3296 offset_sh = 3; /* follows flags, dstnode */
3297 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3301 /* Inefficient because we do our Calvinball dance twice */
3302 b0 = gen_dnhostop(addr, Q_SRC);
3303 b1 = gen_dnhostop(addr, Q_DST);
3309 /* Inefficient because we do our Calvinball dance twice */
3310 b0 = gen_dnhostop(addr, Q_SRC);
3311 b1 = gen_dnhostop(addr, Q_DST);
3316 bpf_error("ISO host filtering not implemented");
3321 b0 = gen_linktype(ETHERTYPE_DN);
3322 /* Check for pad = 1, long header case */
3323 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3324 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
3325 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
3326 BPF_H, (bpf_int32)ntohs((u_short)addr));
3328 /* Check for pad = 0, long header case */
3329 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
3330 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3333 /* Check for pad = 1, short header case */
3334 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3335 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
3336 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3339 /* Check for pad = 0, short header case */
3340 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
3341 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3345 /* Combine with test for linktype */
3351 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3352 * test the bottom-of-stack bit, and then check the version number
3353 * field in the IP header.
3355 static struct block *
3356 gen_mpls_linktype(proto)
3359 struct block *b0, *b1;
3364 /* match the bottom-of-stack bit */
3365 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3366 /* match the IPv4 version number */
3367 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
3372 /* match the bottom-of-stack bit */
3373 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3374 /* match the IPv4 version number */
3375 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
3384 static struct block *
3385 gen_host(addr, mask, proto, dir, type)
3392 struct block *b0, *b1;
3393 const char *typestr;
3403 b0 = gen_host(addr, mask, Q_IP, dir, type);
3405 * Only check for non-IPv4 addresses if we're not
3406 * checking MPLS-encapsulated packets.
3408 if (label_stack_depth == 0) {
3409 b1 = gen_host(addr, mask, Q_ARP, dir, type);
3411 b0 = gen_host(addr, mask, Q_RARP, dir, type);
3417 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
3420 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
3423 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
3426 bpf_error("'tcp' modifier applied to %s", typestr);
3429 bpf_error("'sctp' modifier applied to %s", typestr);
3432 bpf_error("'udp' modifier applied to %s", typestr);
3435 bpf_error("'icmp' modifier applied to %s", typestr);
3438 bpf_error("'igmp' modifier applied to %s", typestr);
3441 bpf_error("'igrp' modifier applied to %s", typestr);
3444 bpf_error("'pim' modifier applied to %s", typestr);
3447 bpf_error("'vrrp' modifier applied to %s", typestr);
3450 bpf_error("ATALK host filtering not implemented");
3453 bpf_error("AARP host filtering not implemented");
3456 return gen_dnhostop(addr, dir);
3459 bpf_error("SCA host filtering not implemented");
3462 bpf_error("LAT host filtering not implemented");
3465 bpf_error("MOPDL host filtering not implemented");
3468 bpf_error("MOPRC host filtering not implemented");
3472 bpf_error("'ip6' modifier applied to ip host");
3475 bpf_error("'icmp6' modifier applied to %s", typestr);
3479 bpf_error("'ah' modifier applied to %s", typestr);
3482 bpf_error("'esp' modifier applied to %s", typestr);
3485 bpf_error("ISO host filtering not implemented");
3488 bpf_error("'esis' modifier applied to %s", typestr);
3491 bpf_error("'isis' modifier applied to %s", typestr);
3494 bpf_error("'clnp' modifier applied to %s", typestr);
3497 bpf_error("'stp' modifier applied to %s", typestr);
3500 bpf_error("IPX host filtering not implemented");
3503 bpf_error("'netbeui' modifier applied to %s", typestr);
3506 bpf_error("'radio' modifier applied to %s", typestr);
3515 static struct block *
3516 gen_host6(addr, mask, proto, dir, type)
3517 struct in6_addr *addr;
3518 struct in6_addr *mask;
3523 const char *typestr;
3533 return gen_host6(addr, mask, Q_IPV6, dir, type);
3536 bpf_error("'ip' modifier applied to ip6 %s", typestr);
3539 bpf_error("'rarp' modifier applied to ip6 %s", typestr);
3542 bpf_error("'arp' modifier applied to ip6 %s", typestr);
3545 bpf_error("'sctp' modifier applied to %s", typestr);
3548 bpf_error("'tcp' modifier applied to %s", typestr);
3551 bpf_error("'udp' modifier applied to %s", typestr);
3554 bpf_error("'icmp' modifier applied to %s", typestr);
3557 bpf_error("'igmp' modifier applied to %s", typestr);
3560 bpf_error("'igrp' modifier applied to %s", typestr);
3563 bpf_error("'pim' modifier applied to %s", typestr);
3566 bpf_error("'vrrp' modifier applied to %s", typestr);
3569 bpf_error("ATALK host filtering not implemented");
3572 bpf_error("AARP host filtering not implemented");
3575 bpf_error("'decnet' modifier applied to ip6 %s", typestr);
3578 bpf_error("SCA host filtering not implemented");
3581 bpf_error("LAT host filtering not implemented");
3584 bpf_error("MOPDL host filtering not implemented");
3587 bpf_error("MOPRC host filtering not implemented");
3590 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
3593 bpf_error("'icmp6' modifier applied to %s", typestr);
3596 bpf_error("'ah' modifier applied to %s", typestr);
3599 bpf_error("'esp' modifier applied to %s", typestr);
3602 bpf_error("ISO host filtering not implemented");
3605 bpf_error("'esis' modifier applied to %s", typestr);
3608 bpf_error("'isis' modifier applied to %s", typestr);
3611 bpf_error("'clnp' modifier applied to %s", typestr);
3614 bpf_error("'stp' modifier applied to %s", typestr);
3617 bpf_error("IPX host filtering not implemented");
3620 bpf_error("'netbeui' modifier applied to %s", typestr);
3623 bpf_error("'radio' modifier applied to %s", typestr);
3633 static struct block *
3634 gen_gateway(eaddr, alist, proto, dir)
3635 const u_char *eaddr;
3636 bpf_u_int32 **alist;
3640 struct block *b0, *b1, *tmp;
3643 bpf_error("direction applied to 'gateway'");
3652 b0 = gen_ehostop(eaddr, Q_OR);
3655 b0 = gen_fhostop(eaddr, Q_OR);
3658 b0 = gen_thostop(eaddr, Q_OR);
3660 case DLT_IEEE802_11:
3661 case DLT_IEEE802_11_RADIO_AVS:
3663 case DLT_IEEE802_11_RADIO:
3664 case DLT_PRISM_HEADER:
3665 b0 = gen_wlanhostop(eaddr, Q_OR);
3670 * Check that the packet doesn't begin with an
3671 * LE Control marker. (We've already generated
3674 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3679 * Now check the MAC address.
3681 b0 = gen_ehostop(eaddr, Q_OR);
3685 case DLT_IP_OVER_FC:
3686 b0 = gen_ipfchostop(eaddr, Q_OR);
3690 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3692 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST);
3694 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR,
3703 bpf_error("illegal modifier of 'gateway'");
3709 gen_proto_abbrev(proto)
3718 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
3720 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
3726 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
3728 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
3734 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
3736 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
3742 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
3745 #ifndef IPPROTO_IGMP
3746 #define IPPROTO_IGMP 2
3750 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
3753 #ifndef IPPROTO_IGRP
3754 #define IPPROTO_IGRP 9
3757 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
3761 #define IPPROTO_PIM 103
3765 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
3767 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
3772 #ifndef IPPROTO_VRRP
3773 #define IPPROTO_VRRP 112
3777 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
3781 b1 = gen_linktype(ETHERTYPE_IP);
3785 b1 = gen_linktype(ETHERTYPE_ARP);
3789 b1 = gen_linktype(ETHERTYPE_REVARP);
3793 bpf_error("link layer applied in wrong context");
3796 b1 = gen_linktype(ETHERTYPE_ATALK);
3800 b1 = gen_linktype(ETHERTYPE_AARP);
3804 b1 = gen_linktype(ETHERTYPE_DN);
3808 b1 = gen_linktype(ETHERTYPE_SCA);
3812 b1 = gen_linktype(ETHERTYPE_LAT);
3816 b1 = gen_linktype(ETHERTYPE_MOPDL);
3820 b1 = gen_linktype(ETHERTYPE_MOPRC);
3825 b1 = gen_linktype(ETHERTYPE_IPV6);
3828 #ifndef IPPROTO_ICMPV6
3829 #define IPPROTO_ICMPV6 58
3832 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
3837 #define IPPROTO_AH 51
3840 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
3842 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
3848 #define IPPROTO_ESP 50
3851 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
3853 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
3859 b1 = gen_linktype(LLCSAP_ISONS);
3863 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
3867 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
3870 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
3871 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3872 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3874 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3876 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3878 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3882 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
3883 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3884 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3886 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3888 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3890 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3894 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
3895 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3896 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3898 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
3903 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3904 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3909 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3910 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3912 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3914 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3919 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3920 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3925 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3926 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3931 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
3935 b1 = gen_linktype(LLCSAP_8021D);
3939 b1 = gen_linktype(LLCSAP_IPX);
3943 b1 = gen_linktype(LLCSAP_NETBEUI);
3947 bpf_error("'radio' is not a valid protocol type");
3955 static struct block *
3962 s = gen_load_a(OR_NET, 6, BPF_H);
3963 b = new_block(JMP(BPF_JSET));
3972 * Generate a comparison to a port value in the transport-layer header
3973 * at the specified offset from the beginning of that header.
3975 * XXX - this handles a variable-length prefix preceding the link-layer
3976 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3977 * variable-length link-layer headers (such as Token Ring or 802.11
3980 static struct block *
3981 gen_portatom(off, v)
3985 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
3989 static struct block *
3990 gen_portatom6(off, v)
3994 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
3999 gen_portop(port, proto, dir)
4000 int port, proto, dir;
4002 struct block *b0, *b1, *tmp;
4004 /* ip proto 'proto' */
4005 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
4011 b1 = gen_portatom(0, (bpf_int32)port);
4015 b1 = gen_portatom(2, (bpf_int32)port);
4020 tmp = gen_portatom(0, (bpf_int32)port);
4021 b1 = gen_portatom(2, (bpf_int32)port);
4026 tmp = gen_portatom(0, (bpf_int32)port);
4027 b1 = gen_portatom(2, (bpf_int32)port);
4039 static struct block *
4040 gen_port(port, ip_proto, dir)
4045 struct block *b0, *b1, *tmp;
4050 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4051 * not LLC encapsulation with LLCSAP_IP.
4053 * For IEEE 802 networks - which includes 802.5 token ring
4054 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4055 * says that SNAP encapsulation is used, not LLC encapsulation
4058 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4059 * RFC 2225 say that SNAP encapsulation is used, not LLC
4060 * encapsulation with LLCSAP_IP.
4062 * So we always check for ETHERTYPE_IP.
4064 b0 = gen_linktype(ETHERTYPE_IP);
4070 b1 = gen_portop(port, ip_proto, dir);
4074 tmp = gen_portop(port, IPPROTO_TCP, dir);
4075 b1 = gen_portop(port, IPPROTO_UDP, dir);
4077 tmp = gen_portop(port, IPPROTO_SCTP, dir);
4090 gen_portop6(port, proto, dir)
4091 int port, proto, dir;
4093 struct block *b0, *b1, *tmp;
4095 /* ip6 proto 'proto' */
4096 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
4100 b1 = gen_portatom6(0, (bpf_int32)port);
4104 b1 = gen_portatom6(2, (bpf_int32)port);
4109 tmp = gen_portatom6(0, (bpf_int32)port);
4110 b1 = gen_portatom6(2, (bpf_int32)port);
4115 tmp = gen_portatom6(0, (bpf_int32)port);
4116 b1 = gen_portatom6(2, (bpf_int32)port);
4128 static struct block *
4129 gen_port6(port, ip_proto, dir)
4134 struct block *b0, *b1, *tmp;
4136 /* link proto ip6 */
4137 b0 = gen_linktype(ETHERTYPE_IPV6);
4143 b1 = gen_portop6(port, ip_proto, dir);
4147 tmp = gen_portop6(port, IPPROTO_TCP, dir);
4148 b1 = gen_portop6(port, IPPROTO_UDP, dir);
4150 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
4162 /* gen_portrange code */
4163 static struct block *
4164 gen_portrangeatom(off, v1, v2)
4168 struct block *b1, *b2;
4172 * Reverse the order of the ports, so v1 is the lower one.
4181 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
4182 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
4190 gen_portrangeop(port1, port2, proto, dir)
4195 struct block *b0, *b1, *tmp;
4197 /* ip proto 'proto' */
4198 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
4204 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4208 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4213 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4214 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4219 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4220 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4232 static struct block *
4233 gen_portrange(port1, port2, ip_proto, dir)
4238 struct block *b0, *b1, *tmp;
4241 b0 = gen_linktype(ETHERTYPE_IP);
4247 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
4251 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
4252 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
4254 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
4266 static struct block *
4267 gen_portrangeatom6(off, v1, v2)
4271 struct block *b1, *b2;
4275 * Reverse the order of the ports, so v1 is the lower one.
4284 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
4285 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
4293 gen_portrangeop6(port1, port2, proto, dir)
4298 struct block *b0, *b1, *tmp;
4300 /* ip6 proto 'proto' */
4301 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
4305 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4309 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4314 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4315 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4320 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4321 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4333 static struct block *
4334 gen_portrange6(port1, port2, ip_proto, dir)
4339 struct block *b0, *b1, *tmp;
4341 /* link proto ip6 */
4342 b0 = gen_linktype(ETHERTYPE_IPV6);
4348 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
4352 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
4353 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
4355 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
4368 lookup_proto(name, proto)
4369 register const char *name;
4379 v = pcap_nametoproto(name);
4380 if (v == PROTO_UNDEF)
4381 bpf_error("unknown ip proto '%s'", name);
4385 /* XXX should look up h/w protocol type based on linktype */
4386 v = pcap_nametoeproto(name);
4387 if (v == PROTO_UNDEF) {
4388 v = pcap_nametollc(name);
4389 if (v == PROTO_UNDEF)
4390 bpf_error("unknown ether proto '%s'", name);
4395 if (strcmp(name, "esis") == 0)
4397 else if (strcmp(name, "isis") == 0)
4399 else if (strcmp(name, "clnp") == 0)
4402 bpf_error("unknown osi proto '%s'", name);
4422 static struct block *
4423 gen_protochain(v, proto, dir)
4428 #ifdef NO_PROTOCHAIN
4429 return gen_proto(v, proto, dir);
4431 struct block *b0, *b;
4432 struct slist *s[100];
4433 int fix2, fix3, fix4, fix5;
4434 int ahcheck, again, end;
4436 int reg2 = alloc_reg();
4438 memset(s, 0, sizeof(s));
4439 fix2 = fix3 = fix4 = fix5 = 0;
4446 b0 = gen_protochain(v, Q_IP, dir);
4447 b = gen_protochain(v, Q_IPV6, dir);
4451 bpf_error("bad protocol applied for 'protochain'");
4456 * We don't handle variable-length radiotap here headers yet.
4457 * We might want to add BPF instructions to do the protochain
4458 * work, to simplify that and, on platforms that have a BPF
4459 * interpreter with the new instructions, let the filtering
4460 * be done in the kernel. (We already require a modified BPF
4461 * engine to do the protochain stuff, to support backward
4462 * branches, and backward branch support is unlikely to appear
4463 * in kernel BPF engines.)
4465 if (linktype == DLT_IEEE802_11_RADIO)
4466 bpf_error("'protochain' not supported with radiotap headers");
4468 if (linktype == DLT_PPI)
4469 bpf_error("'protochain' not supported with PPI headers");
4471 no_optimize = 1; /*this code is not compatible with optimzer yet */
4474 * s[0] is a dummy entry to protect other BPF insn from damage
4475 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4476 * hard to find interdependency made by jump table fixup.
4479 s[i] = new_stmt(0); /*dummy*/
4484 b0 = gen_linktype(ETHERTYPE_IP);
4487 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4488 s[i]->s.k = off_ll + off_nl + 9;
4490 /* X = ip->ip_hl << 2 */
4491 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
4492 s[i]->s.k = off_ll + off_nl;
4497 b0 = gen_linktype(ETHERTYPE_IPV6);
4499 /* A = ip6->ip_nxt */
4500 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4501 s[i]->s.k = off_ll + off_nl + 6;
4503 /* X = sizeof(struct ip6_hdr) */
4504 s[i] = new_stmt(BPF_LDX|BPF_IMM);
4510 bpf_error("unsupported proto to gen_protochain");
4514 /* again: if (A == v) goto end; else fall through; */
4516 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4518 s[i]->s.jt = NULL; /*later*/
4519 s[i]->s.jf = NULL; /*update in next stmt*/
4523 #ifndef IPPROTO_NONE
4524 #define IPPROTO_NONE 59
4526 /* if (A == IPPROTO_NONE) goto end */
4527 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4528 s[i]->s.jt = NULL; /*later*/
4529 s[i]->s.jf = NULL; /*update in next stmt*/
4530 s[i]->s.k = IPPROTO_NONE;
4531 s[fix5]->s.jf = s[i];
4536 if (proto == Q_IPV6) {
4537 int v6start, v6end, v6advance, j;
4540 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4541 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4542 s[i]->s.jt = NULL; /*later*/
4543 s[i]->s.jf = NULL; /*update in next stmt*/
4544 s[i]->s.k = IPPROTO_HOPOPTS;
4545 s[fix2]->s.jf = s[i];
4547 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4548 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4549 s[i]->s.jt = NULL; /*later*/
4550 s[i]->s.jf = NULL; /*update in next stmt*/
4551 s[i]->s.k = IPPROTO_DSTOPTS;
4553 /* if (A == IPPROTO_ROUTING) goto v6advance */
4554 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4555 s[i]->s.jt = NULL; /*later*/
4556 s[i]->s.jf = NULL; /*update in next stmt*/
4557 s[i]->s.k = IPPROTO_ROUTING;
4559 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4560 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4561 s[i]->s.jt = NULL; /*later*/
4562 s[i]->s.jf = NULL; /*later*/
4563 s[i]->s.k = IPPROTO_FRAGMENT;
4574 * X = X + (P[X + 1] + 1) * 8;
4577 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4579 /* A = P[X + packet head] */
4580 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4581 s[i]->s.k = off_ll + off_nl;
4584 s[i] = new_stmt(BPF_ST);
4588 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4591 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4595 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4597 /* A = P[X + packet head]; */
4598 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4599 s[i]->s.k = off_ll + off_nl;
4602 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4606 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4610 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4613 s[i] = new_stmt(BPF_LD|BPF_MEM);
4617 /* goto again; (must use BPF_JA for backward jump) */
4618 s[i] = new_stmt(BPF_JMP|BPF_JA);
4619 s[i]->s.k = again - i - 1;
4620 s[i - 1]->s.jf = s[i];
4624 for (j = v6start; j <= v6end; j++)
4625 s[j]->s.jt = s[v6advance];
4630 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4632 s[fix2]->s.jf = s[i];
4638 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4639 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4640 s[i]->s.jt = NULL; /*later*/
4641 s[i]->s.jf = NULL; /*later*/
4642 s[i]->s.k = IPPROTO_AH;
4644 s[fix3]->s.jf = s[ahcheck];
4651 * X = X + (P[X + 1] + 2) * 4;
4654 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4656 /* A = P[X + packet head]; */
4657 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4658 s[i]->s.k = off_ll + off_nl;
4661 s[i] = new_stmt(BPF_ST);
4665 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4668 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4672 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4674 /* A = P[X + packet head] */
4675 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4676 s[i]->s.k = off_ll + off_nl;
4679 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4683 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4687 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4690 s[i] = new_stmt(BPF_LD|BPF_MEM);
4694 /* goto again; (must use BPF_JA for backward jump) */
4695 s[i] = new_stmt(BPF_JMP|BPF_JA);
4696 s[i]->s.k = again - i - 1;
4701 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4703 s[fix2]->s.jt = s[end];
4704 s[fix4]->s.jf = s[end];
4705 s[fix5]->s.jt = s[end];
4712 for (i = 0; i < max - 1; i++)
4713 s[i]->next = s[i + 1];
4714 s[max - 1]->next = NULL;
4719 b = new_block(JMP(BPF_JEQ));
4720 b->stmts = s[1]; /*remember, s[0] is dummy*/
4732 * Generate code that checks whether the packet is a packet for protocol
4733 * <proto> and whether the type field in that protocol's header has
4734 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4735 * IP packet and checks the protocol number in the IP header against <v>.
4737 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4738 * against Q_IP and Q_IPV6.
4740 static struct block *
4741 gen_proto(v, proto, dir)
4746 struct block *b0, *b1;
4748 if (dir != Q_DEFAULT)
4749 bpf_error("direction applied to 'proto'");
4754 b0 = gen_proto(v, Q_IP, dir);
4755 b1 = gen_proto(v, Q_IPV6, dir);
4763 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4764 * not LLC encapsulation with LLCSAP_IP.
4766 * For IEEE 802 networks - which includes 802.5 token ring
4767 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4768 * says that SNAP encapsulation is used, not LLC encapsulation
4771 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4772 * RFC 2225 say that SNAP encapsulation is used, not LLC
4773 * encapsulation with LLCSAP_IP.
4775 * So we always check for ETHERTYPE_IP.
4777 b0 = gen_linktype(ETHERTYPE_IP);
4779 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
4781 b1 = gen_protochain(v, Q_IP);
4791 * Frame Relay packets typically have an OSI
4792 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4793 * generates code to check for all the OSI
4794 * NLPIDs, so calling it and then adding a check
4795 * for the particular NLPID for which we're
4796 * looking is bogus, as we can just check for
4799 * What we check for is the NLPID and a frame
4800 * control field value of UI, i.e. 0x03 followed
4803 * XXX - assumes a 2-byte Frame Relay header with
4804 * DLCI and flags. What if the address is longer?
4806 * XXX - what about SNAP-encapsulated frames?
4808 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
4814 * Cisco uses an Ethertype lookalike - for OSI,
4817 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
4818 /* OSI in C-HDLC is stuffed with a fudge byte */
4819 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
4824 b0 = gen_linktype(LLCSAP_ISONS);
4825 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
4831 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4833 * 4 is the offset of the PDU type relative to the IS-IS
4836 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
4841 bpf_error("arp does not encapsulate another protocol");
4845 bpf_error("rarp does not encapsulate another protocol");
4849 bpf_error("atalk encapsulation is not specifiable");
4853 bpf_error("decnet encapsulation is not specifiable");
4857 bpf_error("sca does not encapsulate another protocol");
4861 bpf_error("lat does not encapsulate another protocol");
4865 bpf_error("moprc does not encapsulate another protocol");
4869 bpf_error("mopdl does not encapsulate another protocol");
4873 return gen_linktype(v);
4876 bpf_error("'udp proto' is bogus");
4880 bpf_error("'tcp proto' is bogus");
4884 bpf_error("'sctp proto' is bogus");
4888 bpf_error("'icmp proto' is bogus");
4892 bpf_error("'igmp proto' is bogus");
4896 bpf_error("'igrp proto' is bogus");
4900 bpf_error("'pim proto' is bogus");
4904 bpf_error("'vrrp proto' is bogus");
4909 b0 = gen_linktype(ETHERTYPE_IPV6);
4911 b1 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
4913 b1 = gen_protochain(v, Q_IPV6);
4919 bpf_error("'icmp6 proto' is bogus");
4923 bpf_error("'ah proto' is bogus");
4926 bpf_error("'ah proto' is bogus");
4929 bpf_error("'stp proto' is bogus");
4932 bpf_error("'ipx proto' is bogus");
4935 bpf_error("'netbeui proto' is bogus");
4938 bpf_error("'radio proto' is bogus");
4949 register const char *name;
4952 int proto = q.proto;
4956 bpf_u_int32 mask, addr;
4958 bpf_u_int32 **alist;
4961 struct sockaddr_in *sin4;
4962 struct sockaddr_in6 *sin6;
4963 struct addrinfo *res, *res0;
4964 struct in6_addr mask128;
4966 struct block *b, *tmp;
4967 int port, real_proto;
4973 addr = pcap_nametonetaddr(name);
4975 bpf_error("unknown network '%s'", name);
4976 /* Left justify network addr and calculate its network mask */
4978 while (addr && (addr & 0xff000000) == 0) {
4982 return gen_host(addr, mask, proto, dir, q.addr);
4986 if (proto == Q_LINK) {
4990 eaddr = pcap_ether_hostton(name);
4993 "unknown ether host '%s'", name);
4994 b = gen_ehostop(eaddr, dir);
4999 eaddr = pcap_ether_hostton(name);
5002 "unknown FDDI host '%s'", name);
5003 b = gen_fhostop(eaddr, dir);
5008 eaddr = pcap_ether_hostton(name);
5011 "unknown token ring host '%s'", name);
5012 b = gen_thostop(eaddr, dir);
5016 case DLT_IEEE802_11:
5017 case DLT_IEEE802_11_RADIO_AVS:
5018 case DLT_IEEE802_11_RADIO:
5019 case DLT_PRISM_HEADER:
5021 eaddr = pcap_ether_hostton(name);
5024 "unknown 802.11 host '%s'", name);
5025 b = gen_wlanhostop(eaddr, dir);
5029 case DLT_IP_OVER_FC:
5030 eaddr = pcap_ether_hostton(name);
5033 "unknown Fibre Channel host '%s'", name);
5034 b = gen_ipfchostop(eaddr, dir);
5043 * Check that the packet doesn't begin
5044 * with an LE Control marker. (We've
5045 * already generated a test for LANE.)
5047 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
5051 eaddr = pcap_ether_hostton(name);
5054 "unknown ether host '%s'", name);
5055 b = gen_ehostop(eaddr, dir);
5061 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5062 } else if (proto == Q_DECNET) {
5063 unsigned short dn_addr = __pcap_nametodnaddr(name);
5065 * I don't think DECNET hosts can be multihomed, so
5066 * there is no need to build up a list of addresses
5068 return (gen_host(dn_addr, 0, proto, dir, q.addr));
5071 alist = pcap_nametoaddr(name);
5072 if (alist == NULL || *alist == NULL)
5073 bpf_error("unknown host '%s'", name);
5075 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
5077 b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
5079 tmp = gen_host(**alist++, 0xffffffff,
5080 tproto, dir, q.addr);
5086 memset(&mask128, 0xff, sizeof(mask128));
5087 res0 = res = pcap_nametoaddrinfo(name);
5089 bpf_error("unknown host '%s'", name);
5091 tproto = tproto6 = proto;
5092 if (off_linktype == -1 && tproto == Q_DEFAULT) {
5096 for (res = res0; res; res = res->ai_next) {
5097 switch (res->ai_family) {
5099 if (tproto == Q_IPV6)
5102 sin4 = (struct sockaddr_in *)
5104 tmp = gen_host(ntohl(sin4->sin_addr.s_addr),
5105 0xffffffff, tproto, dir, q.addr);
5108 if (tproto6 == Q_IP)
5111 sin6 = (struct sockaddr_in6 *)
5113 tmp = gen_host6(&sin6->sin6_addr,
5114 &mask128, tproto6, dir, q.addr);
5125 bpf_error("unknown host '%s'%s", name,
5126 (proto == Q_DEFAULT)
5128 : " for specified address family");
5135 if (proto != Q_DEFAULT &&
5136 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
5137 bpf_error("illegal qualifier of 'port'");
5138 if (pcap_nametoport(name, &port, &real_proto) == 0)
5139 bpf_error("unknown port '%s'", name);
5140 if (proto == Q_UDP) {
5141 if (real_proto == IPPROTO_TCP)
5142 bpf_error("port '%s' is tcp", name);
5143 else if (real_proto == IPPROTO_SCTP)
5144 bpf_error("port '%s' is sctp", name);
5146 /* override PROTO_UNDEF */
5147 real_proto = IPPROTO_UDP;
5149 if (proto == Q_TCP) {
5150 if (real_proto == IPPROTO_UDP)
5151 bpf_error("port '%s' is udp", name);
5153 else if (real_proto == IPPROTO_SCTP)
5154 bpf_error("port '%s' is sctp", name);
5156 /* override PROTO_UNDEF */
5157 real_proto = IPPROTO_TCP;
5159 if (proto == Q_SCTP) {
5160 if (real_proto == IPPROTO_UDP)
5161 bpf_error("port '%s' is udp", name);
5163 else if (real_proto == IPPROTO_TCP)
5164 bpf_error("port '%s' is tcp", name);
5166 /* override PROTO_UNDEF */
5167 real_proto = IPPROTO_SCTP;
5170 return gen_port(port, real_proto, dir);
5172 b = gen_port(port, real_proto, dir);
5173 gen_or(gen_port6(port, real_proto, dir), b);
5178 if (proto != Q_DEFAULT &&
5179 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
5180 bpf_error("illegal qualifier of 'portrange'");
5181 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
5182 bpf_error("unknown port in range '%s'", name);
5183 if (proto == Q_UDP) {
5184 if (real_proto == IPPROTO_TCP)
5185 bpf_error("port in range '%s' is tcp", name);
5186 else if (real_proto == IPPROTO_SCTP)
5187 bpf_error("port in range '%s' is sctp", name);
5189 /* override PROTO_UNDEF */
5190 real_proto = IPPROTO_UDP;
5192 if (proto == Q_TCP) {
5193 if (real_proto == IPPROTO_UDP)
5194 bpf_error("port in range '%s' is udp", name);
5195 else if (real_proto == IPPROTO_SCTP)
5196 bpf_error("port in range '%s' is sctp", name);
5198 /* override PROTO_UNDEF */
5199 real_proto = IPPROTO_TCP;
5201 if (proto == Q_SCTP) {
5202 if (real_proto == IPPROTO_UDP)
5203 bpf_error("port in range '%s' is udp", name);
5204 else if (real_proto == IPPROTO_TCP)
5205 bpf_error("port in range '%s' is tcp", name);
5207 /* override PROTO_UNDEF */
5208 real_proto = IPPROTO_SCTP;
5211 return gen_portrange(port1, port2, real_proto, dir);
5213 b = gen_portrange(port1, port2, real_proto, dir);
5214 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
5220 eaddr = pcap_ether_hostton(name);
5222 bpf_error("unknown ether host: %s", name);
5224 alist = pcap_nametoaddr(name);
5225 if (alist == NULL || *alist == NULL)
5226 bpf_error("unknown host '%s'", name);
5227 b = gen_gateway(eaddr, alist, proto, dir);
5231 bpf_error("'gateway' not supported in this configuration");
5235 real_proto = lookup_proto(name, proto);
5236 if (real_proto >= 0)
5237 return gen_proto(real_proto, proto, dir);
5239 bpf_error("unknown protocol: %s", name);
5242 real_proto = lookup_proto(name, proto);
5243 if (real_proto >= 0)
5244 return gen_protochain(real_proto, proto, dir);
5246 bpf_error("unknown protocol: %s", name);
5258 gen_mcode(s1, s2, masklen, q)
5259 register const char *s1, *s2;
5260 register int masklen;
5263 register int nlen, mlen;
5266 nlen = __pcap_atoin(s1, &n);
5267 /* Promote short ipaddr */
5271 mlen = __pcap_atoin(s2, &m);
5272 /* Promote short ipaddr */
5275 bpf_error("non-network bits set in \"%s mask %s\"",
5278 /* Convert mask len to mask */
5280 bpf_error("mask length must be <= 32");
5283 * X << 32 is not guaranteed by C to be 0; it's
5288 m = 0xffffffff << (32 - masklen);
5290 bpf_error("non-network bits set in \"%s/%d\"",
5297 return gen_host(n, m, q.proto, q.dir, q.addr);
5300 bpf_error("Mask syntax for networks only");
5309 register const char *s;
5314 int proto = q.proto;
5320 else if (q.proto == Q_DECNET)
5321 vlen = __pcap_atodn(s, &v);
5323 vlen = __pcap_atoin(s, &v);
5330 if (proto == Q_DECNET)
5331 return gen_host(v, 0, proto, dir, q.addr);
5332 else if (proto == Q_LINK) {
5333 bpf_error("illegal link layer address");
5336 if (s == NULL && q.addr == Q_NET) {
5337 /* Promote short net number */
5338 while (v && (v & 0xff000000) == 0) {
5343 /* Promote short ipaddr */
5347 return gen_host(v, mask, proto, dir, q.addr);
5352 proto = IPPROTO_UDP;
5353 else if (proto == Q_TCP)
5354 proto = IPPROTO_TCP;
5355 else if (proto == Q_SCTP)
5356 proto = IPPROTO_SCTP;
5357 else if (proto == Q_DEFAULT)
5358 proto = PROTO_UNDEF;
5360 bpf_error("illegal qualifier of 'port'");
5363 return gen_port((int)v, proto, dir);
5367 b = gen_port((int)v, proto, dir);
5368 gen_or(gen_port6((int)v, proto, dir), b);
5375 proto = IPPROTO_UDP;
5376 else if (proto == Q_TCP)
5377 proto = IPPROTO_TCP;
5378 else if (proto == Q_SCTP)
5379 proto = IPPROTO_SCTP;
5380 else if (proto == Q_DEFAULT)
5381 proto = PROTO_UNDEF;
5383 bpf_error("illegal qualifier of 'portrange'");
5386 return gen_portrange((int)v, (int)v, proto, dir);
5390 b = gen_portrange((int)v, (int)v, proto, dir);
5391 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
5397 bpf_error("'gateway' requires a name");
5401 return gen_proto((int)v, proto, dir);
5404 return gen_protochain((int)v, proto, dir);
5419 gen_mcode6(s1, s2, masklen, q)
5420 register const char *s1, *s2;
5421 register int masklen;
5424 struct addrinfo *res;
5425 struct in6_addr *addr;
5426 struct in6_addr mask;
5431 bpf_error("no mask %s supported", s2);
5433 res = pcap_nametoaddrinfo(s1);
5435 bpf_error("invalid ip6 address %s", s1);
5437 bpf_error("%s resolved to multiple address", s1);
5438 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
5440 if (sizeof(mask) * 8 < masklen)
5441 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
5442 memset(&mask, 0, sizeof(mask));
5443 memset(&mask, 0xff, masklen / 8);
5445 mask.s6_addr[masklen / 8] =
5446 (0xff << (8 - masklen % 8)) & 0xff;
5449 a = (u_int32_t *)addr;
5450 m = (u_int32_t *)&mask;
5451 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
5452 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
5453 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
5461 bpf_error("Mask syntax for networks only");
5465 b = gen_host6(addr, &mask, q.proto, q.dir, q.addr);
5470 bpf_error("invalid qualifier against IPv6 address");
5479 register const u_char *eaddr;
5482 struct block *b, *tmp;
5484 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
5487 return gen_ehostop(eaddr, (int)q.dir);
5489 return gen_fhostop(eaddr, (int)q.dir);
5491 return gen_thostop(eaddr, (int)q.dir);
5492 case DLT_IEEE802_11:
5493 case DLT_IEEE802_11_RADIO_AVS:
5494 case DLT_IEEE802_11_RADIO:
5495 case DLT_PRISM_HEADER:
5497 return gen_wlanhostop(eaddr, (int)q.dir);
5501 * Check that the packet doesn't begin with an
5502 * LE Control marker. (We've already generated
5505 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5510 * Now check the MAC address.
5512 b = gen_ehostop(eaddr, (int)q.dir);
5517 case DLT_IP_OVER_FC:
5518 return gen_ipfchostop(eaddr, (int)q.dir);
5520 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5524 bpf_error("ethernet address used in non-ether expression");
5531 struct slist *s0, *s1;
5534 * This is definitely not the best way to do this, but the
5535 * lists will rarely get long.
5542 static struct slist *
5548 s = new_stmt(BPF_LDX|BPF_MEM);
5553 static struct slist *
5559 s = new_stmt(BPF_LD|BPF_MEM);
5565 * Modify "index" to use the value stored into its register as an
5566 * offset relative to the beginning of the header for the protocol
5567 * "proto", and allocate a register and put an item "size" bytes long
5568 * (1, 2, or 4) at that offset into that register, making it the register
5572 gen_load(proto, inst, size)
5577 struct slist *s, *tmp;
5579 int regno = alloc_reg();
5581 free_reg(inst->regno);
5585 bpf_error("data size must be 1, 2, or 4");
5601 bpf_error("unsupported index operation");
5605 * The offset is relative to the beginning of the packet
5606 * data, if we have a radio header. (If we don't, this
5609 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
5610 linktype != DLT_IEEE802_11_RADIO &&
5611 linktype != DLT_PRISM_HEADER)
5612 bpf_error("radio information not present in capture");
5615 * Load into the X register the offset computed into the
5616 * register specifed by "index".
5618 s = xfer_to_x(inst);
5621 * Load the item at that offset.
5623 tmp = new_stmt(BPF_LD|BPF_IND|size);
5625 sappend(inst->s, s);
5630 * The offset is relative to the beginning of
5631 * the link-layer header.
5633 * XXX - what about ATM LANE? Should the index be
5634 * relative to the beginning of the AAL5 frame, so
5635 * that 0 refers to the beginning of the LE Control
5636 * field, or relative to the beginning of the LAN
5637 * frame, so that 0 refers, for Ethernet LANE, to
5638 * the beginning of the destination address?
5640 s = gen_llprefixlen();
5643 * If "s" is non-null, it has code to arrange that the
5644 * X register contains the length of the prefix preceding
5645 * the link-layer header. Add to it the offset computed
5646 * into the register specified by "index", and move that
5647 * into the X register. Otherwise, just load into the X
5648 * register the offset computed into the register specifed
5652 sappend(s, xfer_to_a(inst));
5653 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5654 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5656 s = xfer_to_x(inst);
5659 * Load the item at the sum of the offset we've put in the
5660 * X register and the offset of the start of the link
5661 * layer header (which is 0 if the radio header is
5662 * variable-length; that header length is what we put
5663 * into the X register and then added to the index).
5665 tmp = new_stmt(BPF_LD|BPF_IND|size);
5668 sappend(inst->s, s);
5684 * The offset is relative to the beginning of
5685 * the network-layer header.
5686 * XXX - are there any cases where we want
5689 s = gen_llprefixlen();
5692 * If "s" is non-null, it has code to arrange that the
5693 * X register contains the length of the prefix preceding
5694 * the link-layer header. Add to it the offset computed
5695 * into the register specified by "index", and move that
5696 * into the X register. Otherwise, just load into the X
5697 * register the offset computed into the register specifed
5701 sappend(s, xfer_to_a(inst));
5702 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5703 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5705 s = xfer_to_x(inst);
5708 * Load the item at the sum of the offset we've put in the
5709 * X register, the offset of the start of the network
5710 * layer header, and the offset of the start of the link
5711 * layer header (which is 0 if the radio header is
5712 * variable-length; that header length is what we put
5713 * into the X register and then added to the index).
5715 tmp = new_stmt(BPF_LD|BPF_IND|size);
5716 tmp->s.k = off_ll + off_nl;
5718 sappend(inst->s, s);
5721 * Do the computation only if the packet contains
5722 * the protocol in question.
5724 b = gen_proto_abbrev(proto);
5726 gen_and(inst->b, b);
5739 * The offset is relative to the beginning of
5740 * the transport-layer header.
5742 * Load the X register with the length of the IPv4 header
5743 * (plus the offset of the link-layer header, if it's
5744 * a variable-length header), in bytes.
5746 * XXX - are there any cases where we want
5748 * XXX - we should, if we're built with
5749 * IPv6 support, generate code to load either
5750 * IPv4, IPv6, or both, as appropriate.
5752 s = gen_loadx_iphdrlen();
5755 * The X register now contains the sum of the length
5756 * of any variable-length header preceding the link-layer
5757 * header and the length of the network-layer header.
5758 * Load into the A register the offset relative to
5759 * the beginning of the transport layer header,
5760 * add the X register to that, move that to the
5761 * X register, and load with an offset from the
5762 * X register equal to the offset of the network
5763 * layer header relative to the beginning of
5764 * the link-layer header plus the length of any
5765 * fixed-length header preceding the link-layer
5768 sappend(s, xfer_to_a(inst));
5769 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5770 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5771 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
5772 tmp->s.k = off_ll + off_nl;
5773 sappend(inst->s, s);
5776 * Do the computation only if the packet contains
5777 * the protocol in question - which is true only
5778 * if this is an IP datagram and is the first or
5779 * only fragment of that datagram.
5781 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
5783 gen_and(inst->b, b);
5785 gen_and(gen_proto_abbrev(Q_IP), b);
5791 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5795 inst->regno = regno;
5796 s = new_stmt(BPF_ST);
5798 sappend(inst->s, s);
5804 gen_relation(code, a0, a1, reversed)
5806 struct arth *a0, *a1;
5809 struct slist *s0, *s1, *s2;
5810 struct block *b, *tmp;
5814 if (code == BPF_JEQ) {
5815 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
5816 b = new_block(JMP(code));
5820 b = new_block(BPF_JMP|code|BPF_X);
5826 sappend(a0->s, a1->s);
5830 free_reg(a0->regno);
5831 free_reg(a1->regno);
5833 /* 'and' together protocol checks */
5836 gen_and(a0->b, tmp = a1->b);
5852 int regno = alloc_reg();
5853 struct arth *a = (struct arth *)newchunk(sizeof(*a));
5856 s = new_stmt(BPF_LD|BPF_LEN);
5857 s->next = new_stmt(BPF_ST);
5858 s->next->s.k = regno;
5873 a = (struct arth *)newchunk(sizeof(*a));
5877 s = new_stmt(BPF_LD|BPF_IMM);
5879 s->next = new_stmt(BPF_ST);
5895 s = new_stmt(BPF_ALU|BPF_NEG);
5898 s = new_stmt(BPF_ST);
5906 gen_arth(code, a0, a1)
5908 struct arth *a0, *a1;
5910 struct slist *s0, *s1, *s2;
5914 s2 = new_stmt(BPF_ALU|BPF_X|code);
5919 sappend(a0->s, a1->s);
5921 free_reg(a0->regno);
5922 free_reg(a1->regno);
5924 s0 = new_stmt(BPF_ST);
5925 a0->regno = s0->s.k = alloc_reg();
5932 * Here we handle simple allocation of the scratch registers.
5933 * If too many registers are alloc'd, the allocator punts.
5935 static int regused[BPF_MEMWORDS];
5939 * Return the next free register.
5944 int n = BPF_MEMWORDS;
5947 if (regused[curreg])
5948 curreg = (curreg + 1) % BPF_MEMWORDS;
5950 regused[curreg] = 1;
5954 bpf_error("too many registers needed to evaluate expression");
5960 * Return a register to the table so it can
5970 static struct block *
5977 s = new_stmt(BPF_LD|BPF_LEN);
5978 b = new_block(JMP(jmp));
5989 return gen_len(BPF_JGE, n);
5993 * Actually, this is less than or equal.
6001 b = gen_len(BPF_JGT, n);
6008 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6009 * the beginning of the link-layer header.
6010 * XXX - that means you can't test values in the radiotap header, but
6011 * as that header is difficult if not impossible to parse generally
6012 * without a loop, that might not be a severe problem. A new keyword
6013 * "radio" could be added for that, although what you'd really want
6014 * would be a way of testing particular radio header values, which
6015 * would generate code appropriate to the radio header in question.
6018 gen_byteop(op, idx, val)
6029 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6032 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6036 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6040 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
6044 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
6048 b = new_block(JMP(BPF_JEQ));
6055 static u_char abroadcast[] = { 0x0 };
6058 gen_broadcast(proto)
6061 bpf_u_int32 hostmask;
6062 struct block *b0, *b1, *b2;
6063 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6071 case DLT_ARCNET_LINUX:
6072 return gen_ahostop(abroadcast, Q_DST);
6074 return gen_ehostop(ebroadcast, Q_DST);
6076 return gen_fhostop(ebroadcast, Q_DST);
6078 return gen_thostop(ebroadcast, Q_DST);
6079 case DLT_IEEE802_11:
6080 case DLT_IEEE802_11_RADIO_AVS:
6081 case DLT_IEEE802_11_RADIO:
6083 case DLT_PRISM_HEADER:
6084 return gen_wlanhostop(ebroadcast, Q_DST);
6085 case DLT_IP_OVER_FC:
6086 return gen_ipfchostop(ebroadcast, Q_DST);
6090 * Check that the packet doesn't begin with an
6091 * LE Control marker. (We've already generated
6094 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6099 * Now check the MAC address.
6101 b0 = gen_ehostop(ebroadcast, Q_DST);
6107 bpf_error("not a broadcast link");
6112 b0 = gen_linktype(ETHERTYPE_IP);
6113 hostmask = ~netmask;
6114 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
6115 b2 = gen_mcmp(OR_NET, 16, BPF_W,
6116 (bpf_int32)(~0 & hostmask), hostmask);
6121 bpf_error("only link-layer/IP broadcast filters supported");
6127 * Generate code to test the low-order bit of a MAC address (that's
6128 * the bottom bit of the *first* byte).
6130 static struct block *
6131 gen_mac_multicast(offset)
6134 register struct block *b0;
6135 register struct slist *s;
6137 /* link[offset] & 1 != 0 */
6138 s = gen_load_a(OR_LINK, offset, BPF_B);
6139 b0 = new_block(JMP(BPF_JSET));
6146 gen_multicast(proto)
6149 register struct block *b0, *b1, *b2;
6150 register struct slist *s;
6158 case DLT_ARCNET_LINUX:
6159 /* all ARCnet multicasts use the same address */
6160 return gen_ahostop(abroadcast, Q_DST);
6162 /* ether[0] & 1 != 0 */
6163 return gen_mac_multicast(0);
6166 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
6168 * XXX - was that referring to bit-order issues?
6170 /* fddi[1] & 1 != 0 */
6171 return gen_mac_multicast(1);
6173 /* tr[2] & 1 != 0 */
6174 return gen_mac_multicast(2);
6175 case DLT_IEEE802_11:
6176 case DLT_IEEE802_11_RADIO_AVS:
6178 case DLT_IEEE802_11_RADIO:
6179 case DLT_PRISM_HEADER:
6183 * For control frames, there is no DA.
6185 * For management frames, DA is at an
6186 * offset of 4 from the beginning of
6189 * For data frames, DA is at an offset
6190 * of 4 from the beginning of the packet
6191 * if To DS is clear and at an offset of
6192 * 16 from the beginning of the packet
6197 * Generate the tests to be done for data frames.
6199 * First, check for To DS set, i.e. "link[1] & 0x01".
6201 s = gen_load_a(OR_LINK, 1, BPF_B);
6202 b1 = new_block(JMP(BPF_JSET));
6203 b1->s.k = 0x01; /* To DS */
6207 * If To DS is set, the DA is at 16.
6209 b0 = gen_mac_multicast(16);
6213 * Now, check for To DS not set, i.e. check
6214 * "!(link[1] & 0x01)".
6216 s = gen_load_a(OR_LINK, 1, BPF_B);
6217 b2 = new_block(JMP(BPF_JSET));
6218 b2->s.k = 0x01; /* To DS */
6223 * If To DS is not set, the DA is at 4.
6225 b1 = gen_mac_multicast(4);
6229 * Now OR together the last two checks. That gives
6230 * the complete set of checks for data frames.
6235 * Now check for a data frame.
6236 * I.e, check "link[0] & 0x08".
6238 s = gen_load_a(OR_LINK, 0, BPF_B);
6239 b1 = new_block(JMP(BPF_JSET));
6244 * AND that with the checks done for data frames.
6249 * If the high-order bit of the type value is 0, this
6250 * is a management frame.
6251 * I.e, check "!(link[0] & 0x08)".
6253 s = gen_load_a(OR_LINK, 0, BPF_B);
6254 b2 = new_block(JMP(BPF_JSET));
6260 * For management frames, the DA is at 4.
6262 b1 = gen_mac_multicast(4);
6266 * OR that with the checks done for data frames.
6267 * That gives the checks done for management and
6273 * If the low-order bit of the type value is 1,
6274 * this is either a control frame or a frame
6275 * with a reserved type, and thus not a
6278 * I.e., check "!(link[0] & 0x04)".
6280 s = gen_load_a(OR_LINK, 0, BPF_B);
6281 b1 = new_block(JMP(BPF_JSET));
6287 * AND that with the checks for data and management
6292 case DLT_IP_OVER_FC:
6293 b0 = gen_mac_multicast(2);
6298 * Check that the packet doesn't begin with an
6299 * LE Control marker. (We've already generated
6302 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6306 /* ether[off_mac] & 1 != 0 */
6307 b0 = gen_mac_multicast(off_mac);
6315 /* Link not known to support multicasts */
6319 b0 = gen_linktype(ETHERTYPE_IP);
6320 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
6326 b0 = gen_linktype(ETHERTYPE_IPV6);
6327 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
6332 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6338 * generate command for inbound/outbound. It's here so we can
6339 * make it link-type specific. 'dir' = 0 implies "inbound",
6340 * = 1 implies "outbound".
6346 register struct block *b0;
6349 * Only some data link types support inbound/outbound qualifiers.
6353 b0 = gen_relation(BPF_JEQ,
6354 gen_load(Q_LINK, gen_loadi(0), 1),
6362 * Match packets sent by this machine.
6364 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
6367 * Match packets sent to this machine.
6368 * (No broadcast or multicast packets, or
6369 * packets sent to some other machine and
6370 * received promiscuously.)
6372 * XXX - packets sent to other machines probably
6373 * shouldn't be matched, but what about broadcast
6374 * or multicast packets we received?
6376 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_HOST);
6381 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
6382 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
6387 /* match outgoing packets */
6388 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
6390 /* match incoming packets */
6391 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
6395 case DLT_JUNIPER_MFR:
6396 case DLT_JUNIPER_MLFR:
6397 case DLT_JUNIPER_MLPPP:
6398 case DLT_JUNIPER_ATM1:
6399 case DLT_JUNIPER_ATM2:
6400 case DLT_JUNIPER_PPPOE:
6401 case DLT_JUNIPER_PPPOE_ATM:
6402 case DLT_JUNIPER_GGSN:
6403 case DLT_JUNIPER_ES:
6404 case DLT_JUNIPER_MONITOR:
6405 case DLT_JUNIPER_SERVICES:
6406 case DLT_JUNIPER_ETHER:
6407 case DLT_JUNIPER_PPP:
6408 case DLT_JUNIPER_FRELAY:
6409 case DLT_JUNIPER_CHDLC:
6410 case DLT_JUNIPER_VP:
6411 /* juniper flags (including direction) are stored
6412 * the byte after the 3-byte magic number */
6414 /* match outgoing packets */
6415 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
6417 /* match incoming packets */
6418 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
6423 bpf_error("inbound/outbound not supported on linktype %d",
6431 /* PF firewall log matched interface */
6433 gen_pf_ifname(const char *ifname)
6438 if (linktype == DLT_PFLOG) {
6439 len = sizeof(((struct pfloghdr *)0)->ifname);
6440 off = offsetof(struct pfloghdr, ifname);
6442 bpf_error("ifname not supported on linktype 0x%x", linktype);
6445 if (strlen(ifname) >= len) {
6446 bpf_error("ifname interface names can only be %d characters",
6450 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
6454 /* PF firewall log ruleset name */
6456 gen_pf_ruleset(char *ruleset)
6460 if (linktype != DLT_PFLOG) {
6461 bpf_error("ruleset not supported on linktype 0x%x", linktype);
6464 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
6465 bpf_error("ruleset names can only be %ld characters",
6466 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
6469 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
6470 strlen(ruleset), (const u_char *)ruleset);
6474 /* PF firewall log rule number */
6480 if (linktype == DLT_PFLOG) {
6481 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
6484 bpf_error("rnr not supported on linktype 0x%x", linktype);
6491 /* PF firewall log sub-rule number */
6493 gen_pf_srnr(int srnr)
6497 if (linktype != DLT_PFLOG) {
6498 bpf_error("srnr not supported on linktype 0x%x", linktype);
6502 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
6507 /* PF firewall log reason code */
6509 gen_pf_reason(int reason)
6513 if (linktype == DLT_PFLOG) {
6514 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
6517 bpf_error("reason not supported on linktype 0x%x", linktype);
6524 /* PF firewall log action */
6526 gen_pf_action(int action)
6530 if (linktype == DLT_PFLOG) {
6531 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
6534 bpf_error("action not supported on linktype 0x%x", linktype);
6541 /* IEEE 802.11 wireless header */
6543 gen_p80211_type(int type, int mask)
6547 if (linktype != DLT_IEEE802_11 && linktype != DLT_IEEE802_11_RADIO) {
6548 bpf_error("action not supported on linktype 0x%x\n", linktype);
6551 b0 = gen_mcmp(OR_LINK, offsetof(struct ieee80211_frame, i_fc[0]),
6552 BPF_B, (bpf_int32)type, (bpf_int32)mask);
6558 register const u_char *eaddr;
6561 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6562 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
6563 return gen_ahostop(eaddr, (int)q.dir);
6565 bpf_error("ARCnet address used in non-arc expression");
6570 static struct block *
6571 gen_ahostop(eaddr, dir)
6572 register const u_char *eaddr;
6575 register struct block *b0, *b1;
6578 /* src comes first, different from Ethernet */
6580 return gen_bcmp(OR_LINK, 0, 1, eaddr);
6583 return gen_bcmp(OR_LINK, 1, 1, eaddr);
6586 b0 = gen_ahostop(eaddr, Q_SRC);
6587 b1 = gen_ahostop(eaddr, Q_DST);
6593 b0 = gen_ahostop(eaddr, Q_SRC);
6594 b1 = gen_ahostop(eaddr, Q_DST);
6603 * support IEEE 802.1Q VLAN trunk over ethernet
6609 struct block *b0, *b1;
6611 /* can't check for VLAN-encapsulated packets inside MPLS */
6612 if (label_stack_depth > 0)
6613 bpf_error("no VLAN match after MPLS");
6616 * Change the offsets to point to the type and data fields within
6617 * the VLAN packet. Just increment the offsets, so that we
6618 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6619 * capture VLAN 200 encapsulated within VLAN 100.
6621 * XXX - this is a bit of a kludge. If we were to split the
6622 * compiler into a parser that parses an expression and
6623 * generates an expression tree, and a code generator that
6624 * takes an expression tree (which could come from our
6625 * parser or from some other parser) and generates BPF code,
6626 * we could perhaps make the offsets parameters of routines
6627 * and, in the handler for an "AND" node, pass to subnodes
6628 * other than the VLAN node the adjusted offsets.
6630 * This would mean that "vlan" would, instead of changing the
6631 * behavior of *all* tests after it, change only the behavior
6632 * of tests ANDed with it. That would change the documented
6633 * semantics of "vlan", which might break some expressions.
6634 * However, it would mean that "(vlan and ip) or ip" would check
6635 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6636 * checking only for VLAN-encapsulated IP, so that could still
6637 * be considered worth doing; it wouldn't break expressions
6638 * that are of the form "vlan and ..." or "vlan N and ...",
6639 * which I suspect are the most common expressions involving
6640 * "vlan". "vlan or ..." doesn't necessarily do what the user
6641 * would really want, now, as all the "or ..." tests would
6642 * be done assuming a VLAN, even though the "or" could be viewed
6643 * as meaning "or, if this isn't a VLAN packet...".
6645 orig_linktype = off_linktype; /* save original values */
6657 bpf_error("no VLAN support for data link type %d",
6662 /* check for VLAN */
6663 b0 = gen_cmp(OR_LINK, orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q);
6665 /* If a specific VLAN is requested, check VLAN id */
6666 if (vlan_num >= 0) {
6667 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_H, (bpf_int32)vlan_num,
6683 struct block *b0,*b1;
6686 * Change the offsets to point to the type and data fields within
6687 * the MPLS packet. Just increment the offsets, so that we
6688 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6689 * capture packets with an outer label of 100000 and an inner
6692 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6696 if (label_stack_depth > 0) {
6697 /* just match the bottom-of-stack bit clear */
6698 b0 = gen_mcmp(OR_LINK, orig_nl-2, BPF_B, 0, 0x01);
6701 * Indicate that we're checking MPLS-encapsulated headers,
6702 * to make sure higher level code generators don't try to
6703 * match against IP-related protocols such as Q_ARP, Q_RARP
6708 case DLT_C_HDLC: /* fall through */
6710 b0 = gen_linktype(ETHERTYPE_MPLS);
6714 b0 = gen_linktype(PPP_MPLS_UCAST);
6717 /* FIXME add other DLT_s ...
6718 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6719 * leave it for now */
6722 bpf_error("no MPLS support for data link type %d",
6730 /* If a specific MPLS label is requested, check it */
6731 if (label_num >= 0) {
6732 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
6733 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_W, (bpf_int32)label_num,
6734 0xfffff000); /* only compare the first 20 bits */
6741 label_stack_depth++;
6746 * Support PPPOE discovery and session.
6751 /* check for PPPoE discovery */
6752 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
6761 * Test against the PPPoE session link-layer type.
6763 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
6766 * Change the offsets to point to the type and data fields within
6769 * XXX - this is a bit of a kludge. If we were to split the
6770 * compiler into a parser that parses an expression and
6771 * generates an expression tree, and a code generator that
6772 * takes an expression tree (which could come from our
6773 * parser or from some other parser) and generates BPF code,
6774 * we could perhaps make the offsets parameters of routines
6775 * and, in the handler for an "AND" node, pass to subnodes
6776 * other than the PPPoE node the adjusted offsets.
6778 * This would mean that "pppoes" would, instead of changing the
6779 * behavior of *all* tests after it, change only the behavior
6780 * of tests ANDed with it. That would change the documented
6781 * semantics of "pppoes", which might break some expressions.
6782 * However, it would mean that "(pppoes and ip) or ip" would check
6783 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6784 * checking only for VLAN-encapsulated IP, so that could still
6785 * be considered worth doing; it wouldn't break expressions
6786 * that are of the form "pppoes and ..." which I suspect are the
6787 * most common expressions involving "pppoes". "pppoes or ..."
6788 * doesn't necessarily do what the user would really want, now,
6789 * as all the "or ..." tests would be done assuming PPPoE, even
6790 * though the "or" could be viewed as meaning "or, if this isn't
6791 * a PPPoE packet...".
6793 orig_linktype = off_linktype; /* save original values */
6797 * The "network-layer" protocol is PPPoE, which has a 6-byte
6798 * PPPoE header, followed by PPP payload, so we set the
6799 * offsets to the network layer offset plus 6 bytes for
6800 * the PPPoE header plus the values appropriate for PPP when
6801 * encapsulated in Ethernet (which means there's no HDLC
6804 off_linktype = orig_nl + 6;
6805 off_nl = orig_nl + 6 + 2;
6806 off_nl_nosnap = orig_nl + 6 + 2;
6809 * Set the link-layer type to PPP, as all subsequent tests will
6810 * be on the encapsulated PPP header.
6818 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
6830 bpf_error("'vpi' supported only on raw ATM");
6831 if (off_vpi == (u_int)-1)
6833 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
6839 bpf_error("'vci' supported only on raw ATM");
6840 if (off_vci == (u_int)-1)
6842 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
6847 if (off_proto == (u_int)-1)
6848 abort(); /* XXX - this isn't on FreeBSD */
6849 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
6854 if (off_payload == (u_int)-1)
6856 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
6857 0xffffffff, jtype, reverse, jvalue);
6862 bpf_error("'callref' supported only on raw ATM");
6863 if (off_proto == (u_int)-1)
6865 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
6866 jtype, reverse, jvalue);
6876 gen_atmtype_abbrev(type)
6879 struct block *b0, *b1;
6884 /* Get all packets in Meta signalling Circuit */
6886 bpf_error("'metac' supported only on raw ATM");
6887 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6888 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
6893 /* Get all packets in Broadcast Circuit*/
6895 bpf_error("'bcc' supported only on raw ATM");
6896 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6897 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
6902 /* Get all cells in Segment OAM F4 circuit*/
6904 bpf_error("'oam4sc' supported only on raw ATM");
6905 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6906 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6911 /* Get all cells in End-to-End OAM F4 Circuit*/
6913 bpf_error("'oam4ec' supported only on raw ATM");
6914 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6915 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6920 /* Get all packets in connection Signalling Circuit */
6922 bpf_error("'sc' supported only on raw ATM");
6923 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6924 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
6929 /* Get all packets in ILMI Circuit */
6931 bpf_error("'ilmic' supported only on raw ATM");
6932 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6933 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
6938 /* Get all LANE packets */
6940 bpf_error("'lane' supported only on raw ATM");
6941 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
6944 * Arrange that all subsequent tests assume LANE
6945 * rather than LLC-encapsulated packets, and set
6946 * the offsets appropriately for LANE-encapsulated
6949 * "off_mac" is the offset of the Ethernet header,
6950 * which is 2 bytes past the ATM pseudo-header
6951 * (skipping the pseudo-header and 2-byte LE Client
6952 * field). The other offsets are Ethernet offsets
6953 * relative to "off_mac".
6956 off_mac = off_payload + 2; /* MAC header */
6957 off_linktype = off_mac + 12;
6958 off_nl = off_mac + 14; /* Ethernet II */
6959 off_nl_nosnap = off_mac + 17; /* 802.3+802.2 */
6963 /* Get all LLC-encapsulated packets */
6965 bpf_error("'llc' supported only on raw ATM");
6966 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
6977 * Filtering for MTP2 messages based on li value
6978 * FISU, length is null
6979 * LSSU, length is 1 or 2
6980 * MSU, length is 3 or more
6983 gen_mtp2type_abbrev(type)
6986 struct block *b0, *b1;
6991 if ( (linktype != DLT_MTP2) &&
6992 (linktype != DLT_MTP2_WITH_PHDR) )
6993 bpf_error("'fisu' supported only on MTP2");
6994 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
6995 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
6999 if ( (linktype != DLT_MTP2) &&
7000 (linktype != DLT_MTP2_WITH_PHDR) )
7001 bpf_error("'lssu' supported only on MTP2");
7002 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
7003 b1 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
7008 if ( (linktype != DLT_MTP2) &&
7009 (linktype != DLT_MTP2_WITH_PHDR) )
7010 bpf_error("'msu' supported only on MTP2");
7011 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
7021 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
7028 bpf_u_int32 val1 , val2 , val3;
7030 switch (mtp3field) {
7033 if (off_sio == (u_int)-1)
7034 bpf_error("'sio' supported only on SS7");
7035 /* sio coded on 1 byte so max value 255 */
7037 bpf_error("sio value %u too big; max value = 255",
7039 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
7040 (u_int)jtype, reverse, (u_int)jvalue);
7044 if (off_opc == (u_int)-1)
7045 bpf_error("'opc' supported only on SS7");
7046 /* opc coded on 14 bits so max value 16383 */
7048 bpf_error("opc value %u too big; max value = 16383",
7050 /* the following instructions are made to convert jvalue
7051 * to the form used to write opc in an ss7 message*/
7052 val1 = jvalue & 0x00003c00;
7054 val2 = jvalue & 0x000003fc;
7056 val3 = jvalue & 0x00000003;
7058 jvalue = val1 + val2 + val3;
7059 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
7060 (u_int)jtype, reverse, (u_int)jvalue);
7064 if (off_dpc == (u_int)-1)
7065 bpf_error("'dpc' supported only on SS7");
7066 /* dpc coded on 14 bits so max value 16383 */
7068 bpf_error("dpc value %u too big; max value = 16383",
7070 /* the following instructions are made to convert jvalue
7071 * to the forme used to write dpc in an ss7 message*/
7072 val1 = jvalue & 0x000000ff;
7074 val2 = jvalue & 0x00003f00;
7076 jvalue = val1 + val2;
7077 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
7078 (u_int)jtype, reverse, (u_int)jvalue);
7082 if (off_sls == (u_int)-1)
7083 bpf_error("'sls' supported only on SS7");
7084 /* sls coded on 4 bits so max value 15 */
7086 bpf_error("sls value %u too big; max value = 15",
7088 /* the following instruction is made to convert jvalue
7089 * to the forme used to write sls in an ss7 message*/
7090 jvalue = jvalue << 4;
7091 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
7092 (u_int)jtype,reverse, (u_int)jvalue);
7101 static struct block *
7102 gen_msg_abbrev(type)
7108 * Q.2931 signalling protocol messages for handling virtual circuits
7109 * establishment and teardown
7114 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
7118 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
7122 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
7126 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
7130 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
7133 case A_RELEASE_DONE:
7134 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
7144 gen_atmmulti_abbrev(type)
7147 struct block *b0, *b1;
7153 bpf_error("'oam' supported only on raw ATM");
7154 b1 = gen_atmmulti_abbrev(A_OAMF4);
7159 bpf_error("'oamf4' supported only on raw ATM");
7161 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
7162 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
7164 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
7170 * Get Q.2931 signalling messages for switched
7171 * virtual connection
7174 bpf_error("'connectmsg' supported only on raw ATM");
7175 b0 = gen_msg_abbrev(A_SETUP);
7176 b1 = gen_msg_abbrev(A_CALLPROCEED);
7178 b0 = gen_msg_abbrev(A_CONNECT);
7180 b0 = gen_msg_abbrev(A_CONNECTACK);
7182 b0 = gen_msg_abbrev(A_RELEASE);
7184 b0 = gen_msg_abbrev(A_RELEASE_DONE);
7186 b0 = gen_atmtype_abbrev(A_SC);
7192 bpf_error("'metaconnect' supported only on raw ATM");
7193 b0 = gen_msg_abbrev(A_SETUP);
7194 b1 = gen_msg_abbrev(A_CALLPROCEED);
7196 b0 = gen_msg_abbrev(A_CONNECT);
7198 b0 = gen_msg_abbrev(A_RELEASE);
7200 b0 = gen_msg_abbrev(A_RELEASE_DONE);
7202 b0 = gen_atmtype_abbrev(A_METAC);