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.53 2007/09/12 19:17:24 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"
76 #ifdef HAVE_NET_PFVAR_H
77 #include <sys/socket.h>
79 #include <net/pfvar.h>
80 #include <net/if_pflog.h>
83 #define offsetof(s, e) ((size_t)&((s *)0)->e)
87 #include <netdb.h> /* for "struct addrinfo" */
90 #include <pcap-namedb.h>
92 #include <netproto/802_11/ieee80211.h>
93 #include <netproto/802_11/ieee80211_radiotap.h>
98 #define IPPROTO_SCTP 132
101 #ifdef HAVE_OS_PROTO_H
102 #include "os-proto.h"
105 #define JMP(c) ((c)|BPF_JMP|BPF_K)
108 static jmp_buf top_ctx;
109 static pcap_t *bpf_pcap;
112 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
113 static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
115 static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
120 static int pcap_fddipad;
125 bpf_error(const char *fmt, ...)
130 if (bpf_pcap != NULL)
131 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
138 static void init_linktype(pcap_t *);
140 static int alloc_reg(void);
141 static void free_reg(int);
143 static struct block *root;
146 * Value passed to gen_load_a() to indicate what the offset argument
150 OR_PACKET, /* relative to the beginning of the packet */
151 OR_LINK, /* relative to the link-layer header */
152 OR_NET, /* relative to the network-layer header */
153 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
154 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
155 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
159 * We divy out chunks of memory rather than call malloc each time so
160 * we don't have to worry about leaking memory. It's probably
161 * not a big deal if all this memory was wasted but if this ever
162 * goes into a library that would probably not be a good idea.
164 * XXX - this *is* in a library....
167 #define CHUNK0SIZE 1024
173 static struct chunk chunks[NCHUNKS];
174 static int cur_chunk;
176 static void *newchunk(u_int);
177 static void freechunks(void);
178 static inline struct block *new_block(int);
179 static inline struct slist *new_stmt(int);
180 static struct block *gen_retblk(int);
181 static inline void syntax(void);
183 static void backpatch(struct block *, struct block *);
184 static void merge(struct block *, struct block *);
185 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
186 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
187 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
188 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
189 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
190 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
192 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
193 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
194 bpf_u_int32, bpf_u_int32, int, bpf_int32);
195 static struct slist *gen_load_llrel(u_int, u_int);
196 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
197 static struct slist *gen_loadx_iphdrlen(void);
198 static struct block *gen_uncond(int);
199 static inline struct block *gen_true(void);
200 static inline struct block *gen_false(void);
201 static struct block *gen_ether_linktype(int);
202 static struct block *gen_linux_sll_linktype(int);
203 static void insert_radiotap_load_llprefixlen(struct block *);
204 static void insert_ppi_load_llprefixlen(struct block *);
205 static void insert_load_llprefixlen(struct block *);
206 static struct slist *gen_llprefixlen(void);
207 static struct block *gen_linktype(int);
208 static struct block *gen_snap(bpf_u_int32, bpf_u_int32, u_int);
209 static struct block *gen_llc_linktype(int);
210 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
212 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
214 static struct block *gen_ahostop(const u_char *, int);
215 static struct block *gen_ehostop(const u_char *, int);
216 static struct block *gen_fhostop(const u_char *, int);
217 static struct block *gen_thostop(const u_char *, int);
218 static struct block *gen_wlanhostop(const u_char *, int);
219 static struct block *gen_ipfchostop(const u_char *, int);
220 static struct block *gen_dnhostop(bpf_u_int32, int);
221 static struct block *gen_mpls_linktype(int);
222 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
224 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
227 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
229 static struct block *gen_ipfrag(void);
230 static struct block *gen_portatom(int, bpf_int32);
231 static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
233 static struct block *gen_portatom6(int, bpf_int32);
234 static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
236 struct block *gen_portop(int, int, int);
237 static struct block *gen_port(int, int, int);
238 struct block *gen_portrangeop(int, int, int, int);
239 static struct block *gen_portrange(int, int, int, int);
241 struct block *gen_portop6(int, int, int);
242 static struct block *gen_port6(int, int, int);
243 struct block *gen_portrangeop6(int, int, int, int);
244 static struct block *gen_portrange6(int, int, int, int);
246 static int lookup_proto(const char *, int);
247 static struct block *gen_protochain(int, int, int);
248 static struct block *gen_proto(int, int, int);
249 static struct slist *xfer_to_x(struct arth *);
250 static struct slist *xfer_to_a(struct arth *);
251 static struct block *gen_mac_multicast(int);
252 static struct block *gen_len(int, int);
254 static struct block *gen_ppi_dlt_check(void);
255 static struct block *gen_msg_abbrev(int type);
266 /* XXX Round up to nearest long. */
267 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
269 /* XXX Round up to structure boundary. */
273 cp = &chunks[cur_chunk];
274 if (n > cp->n_left) {
275 ++cp, k = ++cur_chunk;
277 bpf_error("out of memory");
278 size = CHUNK0SIZE << k;
279 cp->m = (void *)malloc(size);
281 bpf_error("out of memory");
282 memset((char *)cp->m, 0, size);
285 bpf_error("out of memory");
288 return (void *)((char *)cp->m + cp->n_left);
297 for (i = 0; i < NCHUNKS; ++i)
298 if (chunks[i].m != NULL) {
305 * A strdup whose allocations are freed after code generation is over.
309 register const char *s;
311 int n = strlen(s) + 1;
312 char *cp = newchunk(n);
318 static inline struct block *
324 p = (struct block *)newchunk(sizeof(*p));
331 static inline struct slist *
337 p = (struct slist *)newchunk(sizeof(*p));
343 static struct block *
347 struct block *b = new_block(BPF_RET|BPF_K);
356 bpf_error("syntax error in filter expression");
359 static bpf_u_int32 netmask;
364 pcap_compile(pcap_t *p, struct bpf_program *program,
365 const char *buf, int optimize, bpf_u_int32 mask)
368 const char * volatile xbuf = buf;
375 if (setjmp(top_ctx)) {
383 snaplen = pcap_snapshot(p);
385 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
386 "snaplen of 0 rejects all packets");
390 lex_init(xbuf ? xbuf : "");
398 root = gen_retblk(snaplen);
400 if (optimize && !no_optimize) {
403 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
404 bpf_error("expression rejects all packets");
406 program->bf_insns = icode_to_fcode(root, &len);
407 program->bf_len = len;
415 * entry point for using the compiler with no pcap open
416 * pass in all the stuff that is needed explicitly instead.
419 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
420 struct bpf_program *program,
421 const char *buf, int optimize, bpf_u_int32 mask)
426 p = pcap_open_dead(linktype_arg, snaplen_arg);
429 ret = pcap_compile(p, program, buf, optimize, mask);
435 * Clean up a "struct bpf_program" by freeing all the memory allocated
439 pcap_freecode(struct bpf_program *program)
442 if (program->bf_insns != NULL) {
443 free((char *)program->bf_insns);
444 program->bf_insns = NULL;
449 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
450 * which of the jt and jf fields has been resolved and which is a pointer
451 * back to another unresolved block (or nil). At least one of the fields
452 * in each block is already resolved.
455 backpatch(list, target)
456 struct block *list, *target;
473 * Merge the lists in b0 and b1, using the 'sense' field to indicate
474 * which of jt and jf is the link.
478 struct block *b0, *b1;
480 register struct block **p = &b0;
482 /* Find end of list. */
484 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
486 /* Concatenate the lists. */
495 struct block *ppi_dlt_check;
497 ppi_dlt_check = gen_ppi_dlt_check();
499 if (ppi_dlt_check != NULL)
501 gen_and(ppi_dlt_check, p);
504 backpatch(p, gen_retblk(snaplen));
505 p->sense = !p->sense;
506 backpatch(p, gen_retblk(0));
510 * Insert before the statements of the first (root) block any
511 * statements needed to load the lengths of any variable-length
512 * headers into registers.
514 * XXX - a fancier strategy would be to insert those before the
515 * statements of all blocks that use those lengths and that
516 * have no predecessors that use them, so that we only compute
517 * the lengths if we need them. There might be even better
518 * approaches than that. However, as we're currently only
519 * handling variable-length radiotap headers, and as all
520 * filtering expressions other than raw link[M:N] tests
521 * require the length of that header, doing more for that
522 * header length isn't really worth the effort.
525 insert_load_llprefixlen(root);
530 struct block *b0, *b1;
532 backpatch(b0, b1->head);
533 b0->sense = !b0->sense;
534 b1->sense = !b1->sense;
536 b1->sense = !b1->sense;
542 struct block *b0, *b1;
544 b0->sense = !b0->sense;
545 backpatch(b0, b1->head);
546 b0->sense = !b0->sense;
555 b->sense = !b->sense;
558 static struct block *
559 gen_cmp(offrel, offset, size, v)
560 enum e_offrel offrel;
564 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
567 static struct block *
568 gen_cmp_gt(offrel, offset, size, v)
569 enum e_offrel offrel;
573 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
576 static struct block *
577 gen_cmp_ge(offrel, offset, size, v)
578 enum e_offrel offrel;
582 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
585 static struct block *
586 gen_cmp_lt(offrel, offset, size, v)
587 enum e_offrel offrel;
591 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
594 static struct block *
595 gen_cmp_le(offrel, offset, size, v)
596 enum e_offrel offrel;
600 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
603 static struct block *
604 gen_mcmp(offrel, offset, size, v, mask)
605 enum e_offrel offrel;
610 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
613 static struct block *
614 gen_bcmp(offrel, offset, size, v)
615 enum e_offrel offrel;
616 register u_int offset, size;
617 register const u_char *v;
619 register struct block *b, *tmp;
623 register const u_char *p = &v[size - 4];
624 bpf_int32 w = ((bpf_int32)p[0] << 24) |
625 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
627 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
634 register const u_char *p = &v[size - 2];
635 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
637 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
644 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
653 * AND the field of size "size" at offset "offset" relative to the header
654 * specified by "offrel" with "mask", and compare it with the value "v"
655 * with the test specified by "jtype"; if "reverse" is true, the test
656 * should test the opposite of "jtype".
658 static struct block *
659 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
660 enum e_offrel offrel;
662 bpf_u_int32 offset, size, mask, jtype;
665 struct slist *s, *s2;
668 s = gen_load_a(offrel, offset, size);
670 if (mask != 0xffffffff) {
671 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
676 b = new_block(JMP(jtype));
679 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
685 * Various code constructs need to know the layout of the data link
686 * layer. These variables give the necessary offsets from the beginning
687 * of the packet data.
689 * If the link layer has variable_length headers, the offsets are offsets
690 * from the end of the link-link-layer header, and "reg_ll_size" is
691 * the register number for a register containing the length of the
692 * link-layer header. Otherwise, "reg_ll_size" is -1.
694 static int reg_ll_size;
697 * This is the offset of the beginning of the link-layer header from
698 * the beginning of the raw packet data.
700 * It's usually 0, except for 802.11 with a fixed-length radio header.
701 * (For 802.11 with a variable-length radio header, we have to generate
702 * code to compute that offset; off_ll is 0 in that case.)
707 * This is the offset of the beginning of the MAC-layer header.
708 * It's usually 0, except for ATM LANE, where it's the offset, relative
709 * to the beginning of the raw packet data, of the Ethernet header.
711 static u_int off_mac;
714 * "off_linktype" is the offset to information in the link-layer header
715 * giving the packet type. This offset is relative to the beginning
716 * of the link-layer header (i.e., it doesn't include off_ll).
718 * For Ethernet, it's the offset of the Ethernet type field.
720 * For link-layer types that always use 802.2 headers, it's the
721 * offset of the LLC header.
723 * For PPP, it's the offset of the PPP type field.
725 * For Cisco HDLC, it's the offset of the CHDLC type field.
727 * For BSD loopback, it's the offset of the AF_ value.
729 * For Linux cooked sockets, it's the offset of the type field.
731 * It's set to -1 for no encapsulation, in which case, IP is assumed.
733 static u_int off_linktype;
736 * TRUE if the link layer includes an ATM pseudo-header.
738 static int is_atm = 0;
741 * TRUE if "lane" appeared in the filter; it causes us to generate
742 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
744 static int is_lane = 0;
747 * These are offsets for the ATM pseudo-header.
749 static u_int off_vpi;
750 static u_int off_vci;
751 static u_int off_proto;
754 * These are offsets for the MTP2 fields.
759 * These are offsets for the MTP3 fields.
761 static u_int off_sio;
762 static u_int off_opc;
763 static u_int off_dpc;
764 static u_int off_sls;
767 * This is the offset of the first byte after the ATM pseudo_header,
768 * or -1 if there is no ATM pseudo-header.
770 static u_int off_payload;
773 * These are offsets to the beginning of the network-layer header.
774 * They are relative to the beginning of the link-layer header (i.e.,
775 * they don't include off_ll).
777 * If the link layer never uses 802.2 LLC:
779 * "off_nl" and "off_nl_nosnap" are the same.
781 * If the link layer always uses 802.2 LLC:
783 * "off_nl" is the offset if there's a SNAP header following
786 * "off_nl_nosnap" is the offset if there's no SNAP header.
788 * If the link layer is Ethernet:
790 * "off_nl" is the offset if the packet is an Ethernet II packet
791 * (we assume no 802.3+802.2+SNAP);
793 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
794 * with an 802.2 header following it.
797 static u_int off_nl_nosnap;
805 linktype = pcap_datalink(p);
807 pcap_fddipad = p->fddipad;
811 * Assume it's not raw ATM with a pseudo-header, for now.
822 * And assume we're not doing SS7.
831 * Also assume it's not 802.11 with a fixed-length radio header.
837 label_stack_depth = 0;
845 off_nl = 6; /* XXX in reality, variable! */
846 off_nl_nosnap = 6; /* no 802.2 LLC */
849 case DLT_ARCNET_LINUX:
851 off_nl = 8; /* XXX in reality, variable! */
852 off_nl_nosnap = 8; /* no 802.2 LLC */
857 off_nl = 14; /* Ethernet II */
858 off_nl_nosnap = 17; /* 802.3+802.2 */
863 * SLIP doesn't have a link level type. The 16 byte
864 * header is hacked into our SLIP driver.
868 off_nl_nosnap = 16; /* no 802.2 LLC */
872 /* XXX this may be the same as the DLT_PPP_BSDOS case */
876 off_nl_nosnap = 24; /* no 802.2 LLC */
883 off_nl_nosnap = 4; /* no 802.2 LLC */
889 off_nl_nosnap = 12; /* no 802.2 LLC */
894 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
895 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
898 off_nl_nosnap = 4; /* no 802.2 LLC */
903 * This does no include the Ethernet header, and
904 * only covers session state.
908 off_nl_nosnap = 8; /* no 802.2 LLC */
914 off_nl_nosnap = 24; /* no 802.2 LLC */
919 * FDDI doesn't really have a link-level type field.
920 * We set "off_linktype" to the offset of the LLC header.
922 * To check for Ethernet types, we assume that SSAP = SNAP
923 * is being used and pick out the encapsulated Ethernet type.
924 * XXX - should we generate code to check for SNAP?
928 off_linktype += pcap_fddipad;
930 off_nl = 21; /* FDDI+802.2+SNAP */
931 off_nl_nosnap = 16; /* FDDI+802.2 */
933 off_nl += pcap_fddipad;
934 off_nl_nosnap += pcap_fddipad;
940 * Token Ring doesn't really have a link-level type field.
941 * We set "off_linktype" to the offset of the LLC header.
943 * To check for Ethernet types, we assume that SSAP = SNAP
944 * is being used and pick out the encapsulated Ethernet type.
945 * XXX - should we generate code to check for SNAP?
947 * XXX - the header is actually variable-length.
948 * Some various Linux patched versions gave 38
949 * as "off_linktype" and 40 as "off_nl"; however,
950 * if a token ring packet has *no* routing
951 * information, i.e. is not source-routed, the correct
952 * values are 20 and 22, as they are in the vanilla code.
954 * A packet is source-routed iff the uppermost bit
955 * of the first byte of the source address, at an
956 * offset of 8, has the uppermost bit set. If the
957 * packet is source-routed, the total number of bytes
958 * of routing information is 2 plus bits 0x1F00 of
959 * the 16-bit value at an offset of 14 (shifted right
960 * 8 - figure out which byte that is).
963 off_nl = 22; /* Token Ring+802.2+SNAP */
964 off_nl_nosnap = 17; /* Token Ring+802.2 */
969 * 802.11 doesn't really have a link-level type field.
970 * We set "off_linktype" to the offset of the LLC header.
972 * To check for Ethernet types, we assume that SSAP = SNAP
973 * is being used and pick out the encapsulated Ethernet type.
974 * XXX - should we generate code to check for SNAP?
976 * XXX - the header is actually variable-length. We
977 * assume a 24-byte link-layer header, as appears in
978 * data frames in networks with no bridges. If the
979 * fromds and tods 802.11 header bits are both set,
980 * it's actually supposed to be 30 bytes.
983 off_nl = 32; /* 802.11+802.2+SNAP */
984 off_nl_nosnap = 27; /* 802.11+802.2 */
987 case DLT_PRISM_HEADER:
989 * Same as 802.11, but with an additional header before
990 * the 802.11 header, containing a bunch of additional
991 * information including radio-level information.
993 * The header is 144 bytes long.
995 * XXX - same variable-length header problem; at least
996 * the Prism header is fixed-length.
1000 off_nl = 32; /* Prism+802.11+802.2+SNAP */
1001 off_nl_nosnap = 27; /* Prism+802.11+802.2 */
1004 case DLT_IEEE802_11_RADIO_AVS:
1006 * Same as 802.11, but with an additional header before
1007 * the 802.11 header, containing a bunch of additional
1008 * information including radio-level information.
1010 * The header is 64 bytes long, at least in its
1011 * current incarnation.
1013 * XXX - same variable-length header problem, only
1014 * more so; this header is also variable-length,
1015 * with the length being the 32-bit big-endian
1016 * number at an offset of 4 from the beginning
1017 * of the radio header. We should handle that the
1018 * same way we handle the length at the beginning
1019 * of the radiotap header.
1021 * XXX - in Linux, do any drivers that supply an AVS
1022 * header supply a link-layer type other than
1023 * ARPHRD_IEEE80211_PRISM? If so, we should map that
1024 * to DLT_IEEE802_11_RADIO_AVS; if not, or if there are
1025 * any drivers that supply an AVS header but supply
1026 * an ARPHRD value of ARPHRD_IEEE80211_PRISM, we'll
1027 * have to check the header in the generated code to
1028 * determine whether it's Prism or AVS.
1032 off_nl = 32; /* Radio+802.11+802.2+SNAP */
1033 off_nl_nosnap = 27; /* Radio+802.11+802.2 */
1038 * At the moment we treat PPI as normal Radiotap encoded
1039 * packets. The difference is in the function that generates
1040 * the code at the beginning to compute the header length.
1041 * Since this code generator of PPI supports bare 802.11
1042 * encapsulation only (i.e. the encapsulated DLT should be
1043 * DLT_IEEE802_11) we generate code to check for this too.
1046 case DLT_IEEE802_11_RADIO:
1048 * Same as 802.11, but with an additional header before
1049 * the 802.11 header, containing a bunch of additional
1050 * information including radio-level information.
1052 * The radiotap header is variable length, and we
1053 * generate code to compute its length and store it
1054 * in a register. These offsets are relative to the
1055 * beginning of the 802.11 header.
1058 off_nl = 32; /* 802.11+802.2+SNAP */
1059 off_nl_nosnap = 27; /* 802.11+802.2 */
1062 case DLT_ATM_RFC1483:
1063 case DLT_ATM_CLIP: /* Linux ATM defines this */
1065 * assume routed, non-ISO PDUs
1066 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1068 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1069 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1070 * latter would presumably be treated the way PPPoE
1071 * should be, so you can do "pppoe and udp port 2049"
1072 * or "pppoa and tcp port 80" and have it check for
1073 * PPPo{A,E} and a PPP protocol of IP and....
1076 off_nl = 8; /* 802.2+SNAP */
1077 off_nl_nosnap = 3; /* 802.2 */
1082 * Full Frontal ATM; you get AALn PDUs with an ATM
1086 off_vpi = SUNATM_VPI_POS;
1087 off_vci = SUNATM_VCI_POS;
1088 off_proto = PROTO_POS;
1089 off_mac = -1; /* LLC-encapsulated, so no MAC-layer header */
1090 off_payload = SUNATM_PKT_BEGIN_POS;
1091 off_linktype = off_payload;
1092 off_nl = off_payload+8; /* 802.2+SNAP */
1093 off_nl_nosnap = off_payload+3; /* 802.2 */
1099 off_nl_nosnap = 0; /* no 802.2 LLC */
1102 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1105 off_nl_nosnap = 16; /* no 802.2 LLC */
1110 * LocalTalk does have a 1-byte type field in the LLAP header,
1111 * but really it just indicates whether there is a "short" or
1112 * "long" DDP packet following.
1116 off_nl_nosnap = 0; /* no 802.2 LLC */
1119 case DLT_IP_OVER_FC:
1121 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1122 * link-level type field. We set "off_linktype" to the
1123 * offset of the LLC header.
1125 * To check for Ethernet types, we assume that SSAP = SNAP
1126 * is being used and pick out the encapsulated Ethernet type.
1127 * XXX - should we generate code to check for SNAP? RFC
1128 * 2625 says SNAP should be used.
1131 off_nl = 24; /* IPFC+802.2+SNAP */
1132 off_nl_nosnap = 19; /* IPFC+802.2 */
1137 * XXX - we should set this to handle SNAP-encapsulated
1138 * frames (NLPID of 0x80).
1142 off_nl_nosnap = 0; /* no 802.2 LLC */
1146 * the only BPF-interesting FRF.16 frames are non-control frames;
1147 * Frame Relay has a variable length link-layer
1148 * so lets start with offset 4 for now and increments later on (FIXME);
1153 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1156 case DLT_APPLE_IP_OVER_IEEE1394:
1159 off_nl_nosnap = 18; /* no 802.2 LLC */
1162 case DLT_LINUX_IRDA:
1164 * Currently, only raw "link[N:M]" filtering is supported.
1173 * Currently, only raw "link[N:M]" filtering is supported.
1180 case DLT_SYMANTEC_FIREWALL:
1182 off_nl = 44; /* Ethernet II */
1183 off_nl_nosnap = 44; /* XXX - what does it do with 802.3 packets? */
1186 #ifdef HAVE_NET_PFVAR_H
1189 off_nl = PFLOG_HDRLEN;
1190 off_nl_nosnap = PFLOG_HDRLEN; /* no 802.2 LLC */
1194 case DLT_JUNIPER_MFR:
1195 case DLT_JUNIPER_MLFR:
1196 case DLT_JUNIPER_MLPPP:
1197 case DLT_JUNIPER_PPP:
1198 case DLT_JUNIPER_CHDLC:
1199 case DLT_JUNIPER_FRELAY:
1202 off_nl_nosnap = -1; /* no 802.2 LLC */
1205 case DLT_JUNIPER_ATM1:
1206 off_linktype = 4; /* in reality variable between 4-8 */
1211 case DLT_JUNIPER_ATM2:
1212 off_linktype = 8; /* in reality variable between 8-12 */
1217 /* frames captured on a Juniper PPPoE service PIC
1218 * contain raw ethernet frames */
1219 case DLT_JUNIPER_PPPOE:
1220 case DLT_JUNIPER_ETHER:
1222 off_nl = 18; /* Ethernet II */
1223 off_nl_nosnap = 21; /* 802.3+802.2 */
1226 case DLT_JUNIPER_PPPOE_ATM:
1229 off_nl_nosnap = -1; /* no 802.2 LLC */
1232 case DLT_JUNIPER_GGSN:
1235 off_nl_nosnap = -1; /* no 802.2 LLC */
1238 case DLT_JUNIPER_ES:
1240 off_nl = -1; /* not really a network layer but raw IP adresses */
1241 off_nl_nosnap = -1; /* no 802.2 LLC */
1244 case DLT_JUNIPER_MONITOR:
1246 off_nl = 12; /* raw IP/IP6 header */
1247 off_nl_nosnap = -1; /* no 802.2 LLC */
1250 case DLT_JUNIPER_SERVICES:
1252 off_nl = -1; /* L3 proto location dep. on cookie type */
1253 off_nl_nosnap = -1; /* no 802.2 LLC */
1256 case DLT_JUNIPER_VP:
1273 case DLT_MTP2_WITH_PHDR:
1292 case DLT_LINUX_LAPD:
1294 * Currently, only raw "link[N:M]" filtering is supported.
1303 * Currently, only raw "link[N:M]" filtering is supported.
1310 case DLT_BLUETOOTH_HCI_H4:
1312 * Currently, only raw "link[N:M]" filtering is supported.
1319 bpf_error("unknown data link type %d", linktype);
1324 * Load a value relative to the beginning of the link-layer header.
1325 * The link-layer header doesn't necessarily begin at the beginning
1326 * of the packet data; there might be a variable-length prefix containing
1327 * radio information.
1329 static struct slist *
1330 gen_load_llrel(offset, size)
1333 struct slist *s, *s2;
1335 s = gen_llprefixlen();
1338 * If "s" is non-null, it has code to arrange that the X register
1339 * contains the length of the prefix preceding the link-layer
1342 * Otherwise, the length of the prefix preceding the link-layer
1343 * header is "off_ll".
1347 * There's a variable-length prefix preceding the
1348 * link-layer header. "s" points to a list of statements
1349 * that put the length of that prefix into the X register.
1350 * do an indirect load, to use the X register as an offset.
1352 s2 = new_stmt(BPF_LD|BPF_IND|size);
1357 * There is no variable-length header preceding the
1358 * link-layer header; add in off_ll, which, if there's
1359 * a fixed-length header preceding the link-layer header,
1360 * is the length of that header.
1362 s = new_stmt(BPF_LD|BPF_ABS|size);
1363 s->s.k = offset + off_ll;
1370 * Load a value relative to the beginning of the specified header.
1372 static struct slist *
1373 gen_load_a(offrel, offset, size)
1374 enum e_offrel offrel;
1377 struct slist *s, *s2;
1382 s = new_stmt(BPF_LD|BPF_ABS|size);
1387 s = gen_load_llrel(offset, size);
1391 s = gen_load_llrel(off_nl + offset, size);
1395 s = gen_load_llrel(off_nl_nosnap + offset, size);
1400 * Load the X register with the length of the IPv4 header
1401 * (plus the offset of the link-layer header, if it's
1402 * preceded by a variable-length header such as a radio
1403 * header), in bytes.
1405 s = gen_loadx_iphdrlen();
1408 * Load the item at {offset of the link-layer header} +
1409 * {offset, relative to the start of the link-layer
1410 * header, of the IPv4 header} + {length of the IPv4 header} +
1411 * {specified offset}.
1413 * (If the link-layer is variable-length, it's included
1414 * in the value in the X register, and off_ll is 0.)
1416 s2 = new_stmt(BPF_LD|BPF_IND|size);
1417 s2->s.k = off_ll + off_nl + offset;
1422 s = gen_load_llrel(off_nl + 40 + offset, size);
1433 * Generate code to load into the X register the sum of the length of
1434 * the IPv4 header and any variable-length header preceding the link-layer
1437 static struct slist *
1438 gen_loadx_iphdrlen()
1440 struct slist *s, *s2;
1442 s = gen_llprefixlen();
1445 * There's a variable-length prefix preceding the
1446 * link-layer header. "s" points to a list of statements
1447 * that put the length of that prefix into the X register.
1448 * The 4*([k]&0xf) addressing mode can't be used, as we
1449 * don't have a constant offset, so we have to load the
1450 * value in question into the A register and add to it
1451 * the value from the X register.
1453 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1456 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1459 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1464 * The A register now contains the length of the
1465 * IP header. We need to add to it the length
1466 * of the prefix preceding the link-layer
1467 * header, which is still in the X register, and
1468 * move the result into the X register.
1470 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1471 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1474 * There is no variable-length header preceding the
1475 * link-layer header; add in off_ll, which, if there's
1476 * a fixed-length header preceding the link-layer header,
1477 * is the length of that header.
1479 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1480 s->s.k = off_ll + off_nl;
1485 static struct block *
1492 s = new_stmt(BPF_LD|BPF_IMM);
1494 b = new_block(JMP(BPF_JEQ));
1500 static inline struct block *
1503 return gen_uncond(1);
1506 static inline struct block *
1509 return gen_uncond(0);
1513 * Byte-swap a 32-bit number.
1514 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1515 * big-endian platforms.)
1517 #define SWAPLONG(y) \
1518 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1521 * Generate code to match a particular packet type.
1523 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1524 * value, if <= ETHERMTU. We use that to determine whether to
1525 * match the type/length field or to check the type/length field for
1526 * a value <= ETHERMTU to see whether it's a type field and then do
1527 * the appropriate test.
1529 static struct block *
1530 gen_ether_linktype(proto)
1533 struct block *b0, *b1;
1539 case LLCSAP_NETBEUI:
1541 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1542 * so we check the DSAP and SSAP.
1544 * LLCSAP_IP checks for IP-over-802.2, rather
1545 * than IP-over-Ethernet or IP-over-SNAP.
1547 * XXX - should we check both the DSAP and the
1548 * SSAP, like this, or should we check just the
1549 * DSAP, as we do for other types <= ETHERMTU
1550 * (i.e., other SAP values)?
1552 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1554 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1555 ((proto << 8) | proto));
1563 * Ethernet_II frames, which are Ethernet
1564 * frames with a frame type of ETHERTYPE_IPX;
1566 * Ethernet_802.3 frames, which are 802.3
1567 * frames (i.e., the type/length field is
1568 * a length field, <= ETHERMTU, rather than
1569 * a type field) with the first two bytes
1570 * after the Ethernet/802.3 header being
1573 * Ethernet_802.2 frames, which are 802.3
1574 * frames with an 802.2 LLC header and
1575 * with the IPX LSAP as the DSAP in the LLC
1578 * Ethernet_SNAP frames, which are 802.3
1579 * frames with an LLC header and a SNAP
1580 * header and with an OUI of 0x000000
1581 * (encapsulated Ethernet) and a protocol
1582 * ID of ETHERTYPE_IPX in the SNAP header.
1584 * XXX - should we generate the same code both
1585 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1589 * This generates code to check both for the
1590 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1592 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1593 (bpf_int32)LLCSAP_IPX);
1594 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H,
1599 * Now we add code to check for SNAP frames with
1600 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1602 b0 = gen_snap(0x000000, ETHERTYPE_IPX, 14);
1606 * Now we generate code to check for 802.3
1607 * frames in general.
1609 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1613 * Now add the check for 802.3 frames before the
1614 * check for Ethernet_802.2 and Ethernet_802.3,
1615 * as those checks should only be done on 802.3
1616 * frames, not on Ethernet frames.
1621 * Now add the check for Ethernet_II frames, and
1622 * do that before checking for the other frame
1625 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1626 (bpf_int32)ETHERTYPE_IPX);
1630 case ETHERTYPE_ATALK:
1631 case ETHERTYPE_AARP:
1633 * EtherTalk (AppleTalk protocols on Ethernet link
1634 * layer) may use 802.2 encapsulation.
1638 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1639 * we check for an Ethernet type field less than
1640 * 1500, which means it's an 802.3 length field.
1642 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1646 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1647 * SNAP packets with an organization code of
1648 * 0x080007 (Apple, for Appletalk) and a protocol
1649 * type of ETHERTYPE_ATALK (Appletalk).
1651 * 802.2-encapsulated ETHERTYPE_AARP packets are
1652 * SNAP packets with an organization code of
1653 * 0x000000 (encapsulated Ethernet) and a protocol
1654 * type of ETHERTYPE_AARP (Appletalk ARP).
1656 if (proto == ETHERTYPE_ATALK)
1657 b1 = gen_snap(0x080007, ETHERTYPE_ATALK, 14);
1658 else /* proto == ETHERTYPE_AARP */
1659 b1 = gen_snap(0x000000, ETHERTYPE_AARP, 14);
1663 * Check for Ethernet encapsulation (Ethertalk
1664 * phase 1?); we just check for the Ethernet
1667 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1673 if (proto <= ETHERMTU) {
1675 * This is an LLC SAP value, so the frames
1676 * that match would be 802.2 frames.
1677 * Check that the frame is an 802.2 frame
1678 * (i.e., that the length/type field is
1679 * a length field, <= ETHERMTU) and
1680 * then check the DSAP.
1682 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1684 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1690 * This is an Ethernet type, so compare
1691 * the length/type field with it (if
1692 * the frame is an 802.2 frame, the length
1693 * field will be <= ETHERMTU, and, as
1694 * "proto" is > ETHERMTU, this test
1695 * will fail and the frame won't match,
1696 * which is what we want).
1698 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1705 * Generate code to match a particular packet type.
1707 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1708 * value, if <= ETHERMTU. We use that to determine whether to
1709 * match the type field or to check the type field for the special
1710 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1712 static struct block *
1713 gen_linux_sll_linktype(proto)
1716 struct block *b0, *b1;
1722 case LLCSAP_NETBEUI:
1724 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1725 * so we check the DSAP and SSAP.
1727 * LLCSAP_IP checks for IP-over-802.2, rather
1728 * than IP-over-Ethernet or IP-over-SNAP.
1730 * XXX - should we check both the DSAP and the
1731 * SSAP, like this, or should we check just the
1732 * DSAP, as we do for other types <= ETHERMTU
1733 * (i.e., other SAP values)?
1735 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1736 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1737 ((proto << 8) | proto));
1743 * Ethernet_II frames, which are Ethernet
1744 * frames with a frame type of ETHERTYPE_IPX;
1746 * Ethernet_802.3 frames, which have a frame
1747 * type of LINUX_SLL_P_802_3;
1749 * Ethernet_802.2 frames, which are 802.3
1750 * frames with an 802.2 LLC header (i.e, have
1751 * a frame type of LINUX_SLL_P_802_2) and
1752 * with the IPX LSAP as the DSAP in the LLC
1755 * Ethernet_SNAP frames, which are 802.3
1756 * frames with an LLC header and a SNAP
1757 * header and with an OUI of 0x000000
1758 * (encapsulated Ethernet) and a protocol
1759 * ID of ETHERTYPE_IPX in the SNAP header.
1761 * First, do the checks on LINUX_SLL_P_802_2
1762 * frames; generate the check for either
1763 * Ethernet_802.2 or Ethernet_SNAP frames, and
1764 * then put a check for LINUX_SLL_P_802_2 frames
1767 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1768 (bpf_int32)LLCSAP_IPX);
1769 b1 = gen_snap(0x000000, ETHERTYPE_IPX,
1772 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1776 * Now check for 802.3 frames and OR that with
1777 * the previous test.
1779 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1783 * Now add the check for Ethernet_II frames, and
1784 * do that before checking for the other frame
1787 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1788 (bpf_int32)ETHERTYPE_IPX);
1792 case ETHERTYPE_ATALK:
1793 case ETHERTYPE_AARP:
1795 * EtherTalk (AppleTalk protocols on Ethernet link
1796 * layer) may use 802.2 encapsulation.
1800 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1801 * we check for the 802.2 protocol type in the
1802 * "Ethernet type" field.
1804 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1807 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1808 * SNAP packets with an organization code of
1809 * 0x080007 (Apple, for Appletalk) and a protocol
1810 * type of ETHERTYPE_ATALK (Appletalk).
1812 * 802.2-encapsulated ETHERTYPE_AARP packets are
1813 * SNAP packets with an organization code of
1814 * 0x000000 (encapsulated Ethernet) and a protocol
1815 * type of ETHERTYPE_AARP (Appletalk ARP).
1817 if (proto == ETHERTYPE_ATALK)
1818 b1 = gen_snap(0x080007, ETHERTYPE_ATALK,
1820 else /* proto == ETHERTYPE_AARP */
1821 b1 = gen_snap(0x000000, ETHERTYPE_AARP,
1826 * Check for Ethernet encapsulation (Ethertalk
1827 * phase 1?); we just check for the Ethernet
1830 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1836 if (proto <= ETHERMTU) {
1838 * This is an LLC SAP value, so the frames
1839 * that match would be 802.2 frames.
1840 * Check for the 802.2 protocol type
1841 * in the "Ethernet type" field, and
1842 * then check the DSAP.
1844 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1846 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1852 * This is an Ethernet type, so compare
1853 * the length/type field with it (if
1854 * the frame is an 802.2 frame, the length
1855 * field will be <= ETHERMTU, and, as
1856 * "proto" is > ETHERMTU, this test
1857 * will fail and the frame won't match,
1858 * which is what we want).
1860 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1867 insert_radiotap_load_llprefixlen(b)
1870 struct slist *s1, *s2;
1873 * Prepend to the statements in this block code to load the
1874 * length of the radiotap header into the register assigned
1875 * to hold that length, if one has been assigned.
1877 if (reg_ll_size != -1) {
1879 * The 2 bytes at offsets of 2 and 3 from the beginning
1880 * of the radiotap header are the length of the radiotap
1881 * header; unfortunately, it's little-endian, so we have
1882 * to load it a byte at a time and construct the value.
1886 * Load the high-order byte, at an offset of 3, shift it
1887 * left a byte, and put the result in the X register.
1889 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1891 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1894 s2 = new_stmt(BPF_MISC|BPF_TAX);
1898 * Load the next byte, at an offset of 2, and OR the
1899 * value from the X register into it.
1901 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1904 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1908 * Now allocate a register to hold that value and store
1911 s2 = new_stmt(BPF_ST);
1912 s2->s.k = reg_ll_size;
1916 * Now move it into the X register.
1918 s2 = new_stmt(BPF_MISC|BPF_TAX);
1922 * Now append all the existing statements in this
1923 * block to these statements.
1925 sappend(s1, b->stmts);
1931 * At the moment we treat PPI as normal Radiotap encoded
1932 * packets. The difference is in the function that generates
1933 * the code at the beginning to compute the header length.
1934 * Since this code generator of PPI supports bare 802.11
1935 * encapsulation only (i.e. the encapsulated DLT should be
1936 * DLT_IEEE802_11) we generate code to check for this too.
1939 insert_ppi_load_llprefixlen(b)
1942 struct slist *s1, *s2;
1945 * Prepend to the statements in this block code to load the
1946 * length of the radiotap header into the register assigned
1947 * to hold that length, if one has been assigned.
1949 if (reg_ll_size != -1) {
1951 * The 2 bytes at offsets of 2 and 3 from the beginning
1952 * of the radiotap header are the length of the radiotap
1953 * header; unfortunately, it's little-endian, so we have
1954 * to load it a byte at a time and construct the value.
1958 * Load the high-order byte, at an offset of 3, shift it
1959 * left a byte, and put the result in the X register.
1961 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1963 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1966 s2 = new_stmt(BPF_MISC|BPF_TAX);
1970 * Load the next byte, at an offset of 2, and OR the
1971 * value from the X register into it.
1973 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1976 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1980 * Now allocate a register to hold that value and store
1983 s2 = new_stmt(BPF_ST);
1984 s2->s.k = reg_ll_size;
1988 * Now move it into the X register.
1990 s2 = new_stmt(BPF_MISC|BPF_TAX);
1994 * Now append all the existing statements in this
1995 * block to these statements.
1997 sappend(s1, b->stmts);
2003 static struct block *
2004 gen_ppi_dlt_check(void)
2006 struct slist *s_load_dlt;
2009 if (linktype == DLT_PPI)
2011 /* Create the statements that check for the DLT
2013 s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2014 s_load_dlt->s.k = 4;
2016 b = new_block(JMP(BPF_JEQ));
2018 b->stmts = s_load_dlt;
2019 b->s.k = SWAPLONG(DLT_IEEE802_11);
2030 insert_load_llprefixlen(b)
2036 * At the moment we treat PPI as normal Radiotap encoded
2037 * packets. The difference is in the function that generates
2038 * the code at the beginning to compute the header length.
2039 * Since this code generator of PPI supports bare 802.11
2040 * encapsulation only (i.e. the encapsulated DLT should be
2041 * DLT_IEEE802_11) we generate code to check for this too.
2044 insert_ppi_load_llprefixlen(b);
2047 case DLT_IEEE802_11_RADIO:
2048 insert_radiotap_load_llprefixlen(b);
2054 static struct slist *
2055 gen_radiotap_llprefixlen(void)
2059 if (reg_ll_size == -1) {
2061 * We haven't yet assigned a register for the length
2062 * of the radiotap header; allocate one.
2064 reg_ll_size = alloc_reg();
2068 * Load the register containing the radiotap length
2069 * into the X register.
2071 s = new_stmt(BPF_LDX|BPF_MEM);
2072 s->s.k = reg_ll_size;
2077 * At the moment we treat PPI as normal Radiotap encoded
2078 * packets. The difference is in the function that generates
2079 * the code at the beginning to compute the header length.
2080 * Since this code generator of PPI supports bare 802.11
2081 * encapsulation only (i.e. the encapsulated DLT should be
2082 * DLT_IEEE802_11) we generate code to check for this too.
2084 static struct slist *
2085 gen_ppi_llprefixlen(void)
2089 if (reg_ll_size == -1) {
2091 * We haven't yet assigned a register for the length
2092 * of the radiotap header; allocate one.
2094 reg_ll_size = alloc_reg();
2098 * Load the register containing the radiotap length
2099 * into the X register.
2101 s = new_stmt(BPF_LDX|BPF_MEM);
2102 s->s.k = reg_ll_size;
2109 * Generate code to compute the link-layer header length, if necessary,
2110 * putting it into the X register, and to return either a pointer to a
2111 * "struct slist" for the list of statements in that code, or NULL if
2112 * no code is necessary.
2114 static struct slist *
2115 gen_llprefixlen(void)
2120 return gen_ppi_llprefixlen();
2123 case DLT_IEEE802_11_RADIO:
2124 return gen_radiotap_llprefixlen();
2132 * Generate code to match a particular packet type by matching the
2133 * link-layer type field or fields in the 802.2 LLC header.
2135 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2136 * value, if <= ETHERMTU.
2138 static struct block *
2142 struct block *b0, *b1, *b2;
2144 /* are we checking MPLS-encapsulated packets? */
2145 if (label_stack_depth > 0) {
2149 /* FIXME add other L3 proto IDs */
2150 return gen_mpls_linktype(Q_IP);
2152 case ETHERTYPE_IPV6:
2154 /* FIXME add other L3 proto IDs */
2155 return gen_mpls_linktype(Q_IPV6);
2158 bpf_error("unsupported protocol over mpls");
2166 return gen_ether_linktype(proto);
2174 proto = (proto << 8 | LLCSAP_ISONS);
2178 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2188 case DLT_IEEE802_11:
2189 case DLT_IEEE802_11_RADIO_AVS:
2190 case DLT_IEEE802_11_RADIO:
2191 case DLT_PRISM_HEADER:
2192 case DLT_ATM_RFC1483:
2194 case DLT_IP_OVER_FC:
2195 return gen_llc_linktype(proto);
2201 * If "is_lane" is set, check for a LANE-encapsulated
2202 * version of this protocol, otherwise check for an
2203 * LLC-encapsulated version of this protocol.
2205 * We assume LANE means Ethernet, not Token Ring.
2209 * Check that the packet doesn't begin with an
2210 * LE Control marker. (We've already generated
2213 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
2218 * Now generate an Ethernet test.
2220 b1 = gen_ether_linktype(proto);
2225 * Check for LLC encapsulation and then check the
2228 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
2229 b1 = gen_llc_linktype(proto);
2237 return gen_linux_sll_linktype(proto);
2242 case DLT_SLIP_BSDOS:
2245 * These types don't provide any type field; packets
2246 * are always IPv4 or IPv6.
2248 * XXX - for IPv4, check for a version number of 4, and,
2249 * for IPv6, check for a version number of 6?
2254 /* Check for a version number of 4. */
2255 return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
2257 case ETHERTYPE_IPV6:
2258 /* Check for a version number of 6. */
2259 return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
2263 return gen_false(); /* always false */
2270 case DLT_PPP_SERIAL:
2273 * We use Ethernet protocol types inside libpcap;
2274 * map them to the corresponding PPP protocol types.
2283 case ETHERTYPE_IPV6:
2292 case ETHERTYPE_ATALK:
2306 * I'm assuming the "Bridging PDU"s that go
2307 * over PPP are Spanning Tree Protocol
2321 * We use Ethernet protocol types inside libpcap;
2322 * map them to the corresponding PPP protocol types.
2327 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
2328 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
2330 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
2335 case ETHERTYPE_IPV6:
2345 case ETHERTYPE_ATALK:
2359 * I'm assuming the "Bridging PDU"s that go
2360 * over PPP are Spanning Tree Protocol
2376 * For DLT_NULL, the link-layer header is a 32-bit
2377 * word containing an AF_ value in *host* byte order,
2378 * and for DLT_ENC, the link-layer header begins
2379 * with a 32-bit work containing an AF_ value in
2382 * In addition, if we're reading a saved capture file,
2383 * the host byte order in the capture may not be the
2384 * same as the host byte order on this machine.
2386 * For DLT_LOOP, the link-layer header is a 32-bit
2387 * word containing an AF_ value in *network* byte order.
2389 * XXX - AF_ values may, unfortunately, be platform-
2390 * dependent; for example, FreeBSD's AF_INET6 is 24
2391 * whilst NetBSD's and OpenBSD's is 26.
2393 * This means that, when reading a capture file, just
2394 * checking for our AF_INET6 value won't work if the
2395 * capture file came from another OS.
2404 case ETHERTYPE_IPV6:
2411 * Not a type on which we support filtering.
2412 * XXX - support those that have AF_ values
2413 * #defined on this platform, at least?
2418 if (linktype == DLT_NULL || linktype == DLT_ENC) {
2420 * The AF_ value is in host byte order, but
2421 * the BPF interpreter will convert it to
2422 * network byte order.
2424 * If this is a save file, and it's from a
2425 * machine with the opposite byte order to
2426 * ours, we byte-swap the AF_ value.
2428 * Then we run it through "htonl()", and
2429 * generate code to compare against the result.
2431 if (bpf_pcap->sf.rfile != NULL &&
2432 bpf_pcap->sf.swapped)
2433 proto = SWAPLONG(proto);
2434 proto = htonl(proto);
2436 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
2438 #ifdef HAVE_NET_PFVAR_H
2441 * af field is host byte order in contrast to the rest of
2444 if (proto == ETHERTYPE_IP)
2445 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2446 BPF_B, (bpf_int32)AF_INET));
2448 else if (proto == ETHERTYPE_IPV6)
2449 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2450 BPF_B, (bpf_int32)AF_INET6));
2456 #endif /* HAVE_NET_PFVAR_H */
2459 case DLT_ARCNET_LINUX:
2461 * XXX should we check for first fragment if the protocol
2470 case ETHERTYPE_IPV6:
2471 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2472 (bpf_int32)ARCTYPE_INET6));
2476 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2477 (bpf_int32)ARCTYPE_IP);
2478 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2479 (bpf_int32)ARCTYPE_IP_OLD);
2484 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2485 (bpf_int32)ARCTYPE_ARP);
2486 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2487 (bpf_int32)ARCTYPE_ARP_OLD);
2491 case ETHERTYPE_REVARP:
2492 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2493 (bpf_int32)ARCTYPE_REVARP));
2495 case ETHERTYPE_ATALK:
2496 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2497 (bpf_int32)ARCTYPE_ATALK));
2504 case ETHERTYPE_ATALK:
2514 * XXX - assumes a 2-byte Frame Relay header with
2515 * DLCI and flags. What if the address is longer?
2521 * Check for the special NLPID for IP.
2523 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
2526 case ETHERTYPE_IPV6:
2528 * Check for the special NLPID for IPv6.
2530 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
2535 * Check for several OSI protocols.
2537 * Frame Relay packets typically have an OSI
2538 * NLPID at the beginning; we check for each
2541 * What we check for is the NLPID and a frame
2542 * control field of UI, i.e. 0x03 followed
2545 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
2546 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
2547 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
2558 case DLT_JUNIPER_MFR:
2559 case DLT_JUNIPER_MLFR:
2560 case DLT_JUNIPER_MLPPP:
2561 case DLT_JUNIPER_ATM1:
2562 case DLT_JUNIPER_ATM2:
2563 case DLT_JUNIPER_PPPOE:
2564 case DLT_JUNIPER_PPPOE_ATM:
2565 case DLT_JUNIPER_GGSN:
2566 case DLT_JUNIPER_ES:
2567 case DLT_JUNIPER_MONITOR:
2568 case DLT_JUNIPER_SERVICES:
2569 case DLT_JUNIPER_ETHER:
2570 case DLT_JUNIPER_PPP:
2571 case DLT_JUNIPER_FRELAY:
2572 case DLT_JUNIPER_CHDLC:
2573 case DLT_JUNIPER_VP:
2574 /* just lets verify the magic number for now -
2575 * on ATM we may have up to 6 different encapsulations on the wire
2576 * and need a lot of heuristics to figure out that the payload
2579 * FIXME encapsulation specific BPF_ filters
2581 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
2583 case DLT_LINUX_IRDA:
2584 bpf_error("IrDA link-layer type filtering not implemented");
2587 bpf_error("DOCSIS link-layer type filtering not implemented");
2589 case DLT_LINUX_LAPD:
2590 bpf_error("LAPD link-layer type filtering not implemented");
2594 * All the types that have no encapsulation should either be
2595 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2596 * all packets are IP packets, or should be handled in some
2597 * special case, if none of them are (if some are and some
2598 * aren't, the lack of encapsulation is a problem, as we'd
2599 * have to find some other way of determining the packet type).
2601 * Therefore, if "off_linktype" is -1, there's an error.
2603 if (off_linktype == (u_int)-1)
2607 * Any type not handled above should always have an Ethernet
2608 * type at an offset of "off_linktype". (PPP is partially
2609 * handled above - the protocol type is mapped from the
2610 * Ethernet and LLC types we use internally to the corresponding
2611 * PPP type - but the PPP type is always specified by a value
2612 * at "off_linktype", so we don't have to do the code generation
2615 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2619 * Check for an LLC SNAP packet with a given organization code and
2620 * protocol type; we check the entire contents of the 802.2 LLC and
2621 * snap headers, checking for DSAP and SSAP of SNAP and a control
2622 * field of 0x03 in the LLC header, and for the specified organization
2623 * code and protocol type in the SNAP header.
2625 static struct block *
2626 gen_snap(orgcode, ptype, offset)
2627 bpf_u_int32 orgcode;
2631 u_char snapblock[8];
2633 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
2634 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
2635 snapblock[2] = 0x03; /* control = UI */
2636 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
2637 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
2638 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
2639 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
2640 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
2641 return gen_bcmp(OR_LINK, offset, 8, snapblock);
2645 * Generate code to match a particular packet type, for link-layer types
2646 * using 802.2 LLC headers.
2648 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2649 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2651 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2652 * value, if <= ETHERMTU. We use that to determine whether to
2653 * match the DSAP or both DSAP and LSAP or to check the OUI and
2654 * protocol ID in a SNAP header.
2656 static struct block *
2657 gen_llc_linktype(proto)
2661 * XXX - handle token-ring variable-length header.
2667 case LLCSAP_NETBEUI:
2669 * XXX - should we check both the DSAP and the
2670 * SSAP, like this, or should we check just the
2671 * DSAP, as we do for other types <= ETHERMTU
2672 * (i.e., other SAP values)?
2674 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_u_int32)
2675 ((proto << 8) | proto));
2679 * XXX - are there ever SNAP frames for IPX on
2680 * non-Ethernet 802.x networks?
2682 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2683 (bpf_int32)LLCSAP_IPX);
2685 case ETHERTYPE_ATALK:
2687 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2688 * SNAP packets with an organization code of
2689 * 0x080007 (Apple, for Appletalk) and a protocol
2690 * type of ETHERTYPE_ATALK (Appletalk).
2692 * XXX - check for an organization code of
2693 * encapsulated Ethernet as well?
2695 return gen_snap(0x080007, ETHERTYPE_ATALK, off_linktype);
2699 * XXX - we don't have to check for IPX 802.3
2700 * here, but should we check for the IPX Ethertype?
2702 if (proto <= ETHERMTU) {
2704 * This is an LLC SAP value, so check
2707 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2711 * This is an Ethernet type; we assume that it's
2712 * unlikely that it'll appear in the right place
2713 * at random, and therefore check only the
2714 * location that would hold the Ethernet type
2715 * in a SNAP frame with an organization code of
2716 * 0x000000 (encapsulated Ethernet).
2718 * XXX - if we were to check for the SNAP DSAP and
2719 * LSAP, as per XXX, and were also to check for an
2720 * organization code of 0x000000 (encapsulated
2721 * Ethernet), we'd do
2723 * return gen_snap(0x000000, proto,
2726 * here; for now, we don't, as per the above.
2727 * I don't know whether it's worth the extra CPU
2728 * time to do the right check or not.
2730 return gen_cmp(OR_LINK, off_linktype+6, BPF_H,
2736 static struct block *
2737 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
2741 u_int src_off, dst_off;
2743 struct block *b0, *b1;
2757 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2758 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2764 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2765 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2772 b0 = gen_linktype(proto);
2773 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
2779 static struct block *
2780 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
2781 struct in6_addr *addr;
2782 struct in6_addr *mask;
2784 u_int src_off, dst_off;
2786 struct block *b0, *b1;
2801 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2802 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2808 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2809 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2816 /* this order is important */
2817 a = (u_int32_t *)addr;
2818 m = (u_int32_t *)mask;
2819 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
2820 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
2822 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
2824 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
2826 b0 = gen_linktype(proto);
2832 static struct block *
2833 gen_ehostop(eaddr, dir)
2834 register const u_char *eaddr;
2837 register struct block *b0, *b1;
2841 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
2844 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
2847 b0 = gen_ehostop(eaddr, Q_SRC);
2848 b1 = gen_ehostop(eaddr, Q_DST);
2854 b0 = gen_ehostop(eaddr, Q_SRC);
2855 b1 = gen_ehostop(eaddr, Q_DST);
2864 * Like gen_ehostop, but for DLT_FDDI
2866 static struct block *
2867 gen_fhostop(eaddr, dir)
2868 register const u_char *eaddr;
2871 struct block *b0, *b1;
2876 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
2878 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
2883 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
2885 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
2889 b0 = gen_fhostop(eaddr, Q_SRC);
2890 b1 = gen_fhostop(eaddr, Q_DST);
2896 b0 = gen_fhostop(eaddr, Q_SRC);
2897 b1 = gen_fhostop(eaddr, Q_DST);
2906 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2908 static struct block *
2909 gen_thostop(eaddr, dir)
2910 register const u_char *eaddr;
2913 register struct block *b0, *b1;
2917 return gen_bcmp(OR_LINK, 8, 6, eaddr);
2920 return gen_bcmp(OR_LINK, 2, 6, eaddr);
2923 b0 = gen_thostop(eaddr, Q_SRC);
2924 b1 = gen_thostop(eaddr, Q_DST);
2930 b0 = gen_thostop(eaddr, Q_SRC);
2931 b1 = gen_thostop(eaddr, Q_DST);
2940 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2942 static struct block *
2943 gen_wlanhostop(eaddr, dir)
2944 register const u_char *eaddr;
2947 register struct block *b0, *b1, *b2;
2948 register struct slist *s;
2955 * For control frames, there is no SA.
2957 * For management frames, SA is at an
2958 * offset of 10 from the beginning of
2961 * For data frames, SA is at an offset
2962 * of 10 from the beginning of the packet
2963 * if From DS is clear, at an offset of
2964 * 16 from the beginning of the packet
2965 * if From DS is set and To DS is clear,
2966 * and an offset of 24 from the beginning
2967 * of the packet if From DS is set and To DS
2972 * Generate the tests to be done for data frames
2975 * First, check for To DS set, i.e. check "link[1] & 0x01".
2977 s = gen_load_a(OR_LINK, 1, BPF_B);
2978 b1 = new_block(JMP(BPF_JSET));
2979 b1->s.k = 0x01; /* To DS */
2983 * If To DS is set, the SA is at 24.
2985 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
2989 * Now, check for To DS not set, i.e. check
2990 * "!(link[1] & 0x01)".
2992 s = gen_load_a(OR_LINK, 1, BPF_B);
2993 b2 = new_block(JMP(BPF_JSET));
2994 b2->s.k = 0x01; /* To DS */
2999 * If To DS is not set, the SA is at 16.
3001 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3005 * Now OR together the last two checks. That gives
3006 * the complete set of checks for data frames with
3012 * Now check for From DS being set, and AND that with
3013 * the ORed-together checks.
3015 s = gen_load_a(OR_LINK, 1, BPF_B);
3016 b1 = new_block(JMP(BPF_JSET));
3017 b1->s.k = 0x02; /* From DS */
3022 * Now check for data frames with From DS not set.
3024 s = gen_load_a(OR_LINK, 1, BPF_B);
3025 b2 = new_block(JMP(BPF_JSET));
3026 b2->s.k = 0x02; /* From DS */
3031 * If From DS isn't set, the SA is at 10.
3033 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3037 * Now OR together the checks for data frames with
3038 * From DS not set and for data frames with From DS
3039 * set; that gives the checks done for data frames.
3044 * Now check for a data frame.
3045 * I.e, check "link[0] & 0x08".
3047 gen_load_a(OR_LINK, 0, BPF_B);
3048 b1 = new_block(JMP(BPF_JSET));
3053 * AND that with the checks done for data frames.
3058 * If the high-order bit of the type value is 0, this
3059 * is a management frame.
3060 * I.e, check "!(link[0] & 0x08)".
3062 s = gen_load_a(OR_LINK, 0, BPF_B);
3063 b2 = new_block(JMP(BPF_JSET));
3069 * For management frames, the SA is at 10.
3071 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3075 * OR that with the checks done for data frames.
3076 * That gives the checks done for management and
3082 * If the low-order bit of the type value is 1,
3083 * this is either a control frame or a frame
3084 * with a reserved type, and thus not a
3087 * I.e., check "!(link[0] & 0x04)".
3089 s = gen_load_a(OR_LINK, 0, BPF_B);
3090 b1 = new_block(JMP(BPF_JSET));
3096 * AND that with the checks for data and management
3106 * For control frames, there is no DA.
3108 * For management frames, DA is at an
3109 * offset of 4 from the beginning of
3112 * For data frames, DA is at an offset
3113 * of 4 from the beginning of the packet
3114 * if To DS is clear and at an offset of
3115 * 16 from the beginning of the packet
3120 * Generate the tests to be done for data frames.
3122 * First, check for To DS set, i.e. "link[1] & 0x01".
3124 s = gen_load_a(OR_LINK, 1, BPF_B);
3125 b1 = new_block(JMP(BPF_JSET));
3126 b1->s.k = 0x01; /* To DS */
3130 * If To DS is set, the DA is at 16.
3132 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3136 * Now, check for To DS not set, i.e. check
3137 * "!(link[1] & 0x01)".
3139 s = gen_load_a(OR_LINK, 1, BPF_B);
3140 b2 = new_block(JMP(BPF_JSET));
3141 b2->s.k = 0x01; /* To DS */
3146 * If To DS is not set, the DA is at 4.
3148 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
3152 * Now OR together the last two checks. That gives
3153 * the complete set of checks for data frames.
3158 * Now check for a data frame.
3159 * I.e, check "link[0] & 0x08".
3161 s = gen_load_a(OR_LINK, 0, BPF_B);
3162 b1 = new_block(JMP(BPF_JSET));
3167 * AND that with the checks done for data frames.
3172 * If the high-order bit of the type value is 0, this
3173 * is a management frame.
3174 * I.e, check "!(link[0] & 0x08)".
3176 s = gen_load_a(OR_LINK, 0, BPF_B);
3177 b2 = new_block(JMP(BPF_JSET));
3183 * For management frames, the DA is at 4.
3185 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
3189 * OR that with the checks done for data frames.
3190 * That gives the checks done for management and
3196 * If the low-order bit of the type value is 1,
3197 * this is either a control frame or a frame
3198 * with a reserved type, and thus not a
3201 * I.e., check "!(link[0] & 0x04)".
3203 s = gen_load_a(OR_LINK, 0, BPF_B);
3204 b1 = new_block(JMP(BPF_JSET));
3210 * AND that with the checks for data and management
3217 b0 = gen_wlanhostop(eaddr, Q_SRC);
3218 b1 = gen_wlanhostop(eaddr, Q_DST);
3224 b0 = gen_wlanhostop(eaddr, Q_SRC);
3225 b1 = gen_wlanhostop(eaddr, Q_DST);
3234 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3235 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3236 * as the RFC states.)
3238 static struct block *
3239 gen_ipfchostop(eaddr, dir)
3240 register const u_char *eaddr;
3243 register struct block *b0, *b1;
3247 return gen_bcmp(OR_LINK, 10, 6, eaddr);
3250 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3253 b0 = gen_ipfchostop(eaddr, Q_SRC);
3254 b1 = gen_ipfchostop(eaddr, Q_DST);
3260 b0 = gen_ipfchostop(eaddr, Q_SRC);
3261 b1 = gen_ipfchostop(eaddr, Q_DST);
3270 * This is quite tricky because there may be pad bytes in front of the
3271 * DECNET header, and then there are two possible data packet formats that
3272 * carry both src and dst addresses, plus 5 packet types in a format that
3273 * carries only the src node, plus 2 types that use a different format and
3274 * also carry just the src node.
3278 * Instead of doing those all right, we just look for data packets with
3279 * 0 or 1 bytes of padding. If you want to look at other packets, that
3280 * will require a lot more hacking.
3282 * To add support for filtering on DECNET "areas" (network numbers)
3283 * one would want to add a "mask" argument to this routine. That would
3284 * make the filter even more inefficient, although one could be clever
3285 * and not generate masking instructions if the mask is 0xFFFF.
3287 static struct block *
3288 gen_dnhostop(addr, dir)
3292 struct block *b0, *b1, *b2, *tmp;
3293 u_int offset_lh; /* offset if long header is received */
3294 u_int offset_sh; /* offset if short header is received */
3299 offset_sh = 1; /* follows flags */
3300 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
3304 offset_sh = 3; /* follows flags, dstnode */
3305 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3309 /* Inefficient because we do our Calvinball dance twice */
3310 b0 = gen_dnhostop(addr, Q_SRC);
3311 b1 = gen_dnhostop(addr, Q_DST);
3317 /* Inefficient because we do our Calvinball dance twice */
3318 b0 = gen_dnhostop(addr, Q_SRC);
3319 b1 = gen_dnhostop(addr, Q_DST);
3324 bpf_error("ISO host filtering not implemented");
3329 b0 = gen_linktype(ETHERTYPE_DN);
3330 /* Check for pad = 1, long header case */
3331 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3332 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
3333 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
3334 BPF_H, (bpf_int32)ntohs((u_short)addr));
3336 /* Check for pad = 0, long header case */
3337 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
3338 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3341 /* Check for pad = 1, short header case */
3342 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3343 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
3344 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3347 /* Check for pad = 0, short header case */
3348 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
3349 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3353 /* Combine with test for linktype */
3359 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3360 * test the bottom-of-stack bit, and then check the version number
3361 * field in the IP header.
3363 static struct block *
3364 gen_mpls_linktype(proto)
3367 struct block *b0, *b1;
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, 0x40, 0xf0);
3380 /* match the bottom-of-stack bit */
3381 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3382 /* match the IPv4 version number */
3383 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
3392 static struct block *
3393 gen_host(addr, mask, proto, dir, type)
3400 struct block *b0, *b1;
3401 const char *typestr;
3411 b0 = gen_host(addr, mask, Q_IP, dir, type);
3413 * Only check for non-IPv4 addresses if we're not
3414 * checking MPLS-encapsulated packets.
3416 if (label_stack_depth == 0) {
3417 b1 = gen_host(addr, mask, Q_ARP, dir, type);
3419 b0 = gen_host(addr, mask, Q_RARP, dir, type);
3425 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
3428 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
3431 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
3434 bpf_error("'tcp' modifier applied to %s", typestr);
3437 bpf_error("'sctp' modifier applied to %s", typestr);
3440 bpf_error("'udp' modifier applied to %s", typestr);
3443 bpf_error("'icmp' modifier applied to %s", typestr);
3446 bpf_error("'igmp' modifier applied to %s", typestr);
3449 bpf_error("'igrp' modifier applied to %s", typestr);
3452 bpf_error("'pim' modifier applied to %s", typestr);
3455 bpf_error("'vrrp' modifier applied to %s", typestr);
3458 bpf_error("ATALK host filtering not implemented");
3461 bpf_error("AARP host filtering not implemented");
3464 return gen_dnhostop(addr, dir);
3467 bpf_error("SCA host filtering not implemented");
3470 bpf_error("LAT host filtering not implemented");
3473 bpf_error("MOPDL host filtering not implemented");
3476 bpf_error("MOPRC host filtering not implemented");
3480 bpf_error("'ip6' modifier applied to ip host");
3483 bpf_error("'icmp6' modifier applied to %s", typestr);
3487 bpf_error("'ah' modifier applied to %s", typestr);
3490 bpf_error("'esp' modifier applied to %s", typestr);
3493 bpf_error("ISO host filtering not implemented");
3496 bpf_error("'esis' modifier applied to %s", typestr);
3499 bpf_error("'isis' modifier applied to %s", typestr);
3502 bpf_error("'clnp' modifier applied to %s", typestr);
3505 bpf_error("'stp' modifier applied to %s", typestr);
3508 bpf_error("IPX host filtering not implemented");
3511 bpf_error("'netbeui' modifier applied to %s", typestr);
3514 bpf_error("'radio' modifier applied to %s", typestr);
3523 static struct block *
3524 gen_host6(addr, mask, proto, dir, type)
3525 struct in6_addr *addr;
3526 struct in6_addr *mask;
3531 const char *typestr;
3541 return gen_host6(addr, mask, Q_IPV6, dir, type);
3544 bpf_error("'ip' modifier applied to ip6 %s", typestr);
3547 bpf_error("'rarp' modifier applied to ip6 %s", typestr);
3550 bpf_error("'arp' modifier applied to ip6 %s", typestr);
3553 bpf_error("'sctp' modifier applied to %s", typestr);
3556 bpf_error("'tcp' modifier applied to %s", typestr);
3559 bpf_error("'udp' modifier applied to %s", typestr);
3562 bpf_error("'icmp' modifier applied to %s", typestr);
3565 bpf_error("'igmp' modifier applied to %s", typestr);
3568 bpf_error("'igrp' modifier applied to %s", typestr);
3571 bpf_error("'pim' modifier applied to %s", typestr);
3574 bpf_error("'vrrp' modifier applied to %s", typestr);
3577 bpf_error("ATALK host filtering not implemented");
3580 bpf_error("AARP host filtering not implemented");
3583 bpf_error("'decnet' modifier applied to ip6 %s", typestr);
3586 bpf_error("SCA host filtering not implemented");
3589 bpf_error("LAT host filtering not implemented");
3592 bpf_error("MOPDL host filtering not implemented");
3595 bpf_error("MOPRC host filtering not implemented");
3598 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
3601 bpf_error("'icmp6' modifier applied to %s", typestr);
3604 bpf_error("'ah' modifier applied to %s", typestr);
3607 bpf_error("'esp' modifier applied to %s", typestr);
3610 bpf_error("ISO host filtering not implemented");
3613 bpf_error("'esis' modifier applied to %s", typestr);
3616 bpf_error("'isis' modifier applied to %s", typestr);
3619 bpf_error("'clnp' modifier applied to %s", typestr);
3622 bpf_error("'stp' modifier applied to %s", typestr);
3625 bpf_error("IPX host filtering not implemented");
3628 bpf_error("'netbeui' modifier applied to %s", typestr);
3631 bpf_error("'radio' modifier applied to %s", typestr);
3641 static struct block *
3642 gen_gateway(eaddr, alist, proto, dir)
3643 const u_char *eaddr;
3644 bpf_u_int32 **alist;
3648 struct block *b0, *b1, *tmp;
3651 bpf_error("direction applied to 'gateway'");
3660 b0 = gen_ehostop(eaddr, Q_OR);
3663 b0 = gen_fhostop(eaddr, Q_OR);
3666 b0 = gen_thostop(eaddr, Q_OR);
3668 case DLT_IEEE802_11:
3669 case DLT_IEEE802_11_RADIO_AVS:
3671 case DLT_IEEE802_11_RADIO:
3672 case DLT_PRISM_HEADER:
3673 b0 = gen_wlanhostop(eaddr, Q_OR);
3678 * Check that the packet doesn't begin with an
3679 * LE Control marker. (We've already generated
3682 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3687 * Now check the MAC address.
3689 b0 = gen_ehostop(eaddr, Q_OR);
3693 case DLT_IP_OVER_FC:
3694 b0 = gen_ipfchostop(eaddr, Q_OR);
3698 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3700 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST);
3702 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR,
3711 bpf_error("illegal modifier of 'gateway'");
3717 gen_proto_abbrev(proto)
3726 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
3728 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
3734 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
3736 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
3742 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
3744 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
3750 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
3753 #ifndef IPPROTO_IGMP
3754 #define IPPROTO_IGMP 2
3758 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
3761 #ifndef IPPROTO_IGRP
3762 #define IPPROTO_IGRP 9
3765 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
3769 #define IPPROTO_PIM 103
3773 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
3775 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
3780 #ifndef IPPROTO_VRRP
3781 #define IPPROTO_VRRP 112
3785 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
3789 b1 = gen_linktype(ETHERTYPE_IP);
3793 b1 = gen_linktype(ETHERTYPE_ARP);
3797 b1 = gen_linktype(ETHERTYPE_REVARP);
3801 bpf_error("link layer applied in wrong context");
3804 b1 = gen_linktype(ETHERTYPE_ATALK);
3808 b1 = gen_linktype(ETHERTYPE_AARP);
3812 b1 = gen_linktype(ETHERTYPE_DN);
3816 b1 = gen_linktype(ETHERTYPE_SCA);
3820 b1 = gen_linktype(ETHERTYPE_LAT);
3824 b1 = gen_linktype(ETHERTYPE_MOPDL);
3828 b1 = gen_linktype(ETHERTYPE_MOPRC);
3833 b1 = gen_linktype(ETHERTYPE_IPV6);
3836 #ifndef IPPROTO_ICMPV6
3837 #define IPPROTO_ICMPV6 58
3840 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
3845 #define IPPROTO_AH 51
3848 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
3850 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
3856 #define IPPROTO_ESP 50
3859 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
3861 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
3867 b1 = gen_linktype(LLCSAP_ISONS);
3871 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
3875 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
3878 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
3879 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3880 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3882 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3884 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3886 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3890 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
3891 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3892 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3894 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3896 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3898 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3902 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
3903 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3904 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3906 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
3911 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3912 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3917 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3918 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3920 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3922 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3927 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3928 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3933 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3934 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3939 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
3943 b1 = gen_linktype(LLCSAP_8021D);
3947 b1 = gen_linktype(LLCSAP_IPX);
3951 b1 = gen_linktype(LLCSAP_NETBEUI);
3955 bpf_error("'radio' is not a valid protocol type");
3963 static struct block *
3970 s = gen_load_a(OR_NET, 6, BPF_H);
3971 b = new_block(JMP(BPF_JSET));
3980 * Generate a comparison to a port value in the transport-layer header
3981 * at the specified offset from the beginning of that header.
3983 * XXX - this handles a variable-length prefix preceding the link-layer
3984 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3985 * variable-length link-layer headers (such as Token Ring or 802.11
3988 static struct block *
3989 gen_portatom(off, v)
3993 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
3997 static struct block *
3998 gen_portatom6(off, v)
4002 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
4007 gen_portop(port, proto, dir)
4008 int port, proto, dir;
4010 struct block *b0, *b1, *tmp;
4012 /* ip proto 'proto' */
4013 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
4019 b1 = gen_portatom(0, (bpf_int32)port);
4023 b1 = gen_portatom(2, (bpf_int32)port);
4028 tmp = gen_portatom(0, (bpf_int32)port);
4029 b1 = gen_portatom(2, (bpf_int32)port);
4034 tmp = gen_portatom(0, (bpf_int32)port);
4035 b1 = gen_portatom(2, (bpf_int32)port);
4047 static struct block *
4048 gen_port(port, ip_proto, dir)
4053 struct block *b0, *b1, *tmp;
4058 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4059 * not LLC encapsulation with LLCSAP_IP.
4061 * For IEEE 802 networks - which includes 802.5 token ring
4062 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4063 * says that SNAP encapsulation is used, not LLC encapsulation
4066 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4067 * RFC 2225 say that SNAP encapsulation is used, not LLC
4068 * encapsulation with LLCSAP_IP.
4070 * So we always check for ETHERTYPE_IP.
4072 b0 = gen_linktype(ETHERTYPE_IP);
4078 b1 = gen_portop(port, ip_proto, dir);
4082 tmp = gen_portop(port, IPPROTO_TCP, dir);
4083 b1 = gen_portop(port, IPPROTO_UDP, dir);
4085 tmp = gen_portop(port, IPPROTO_SCTP, dir);
4098 gen_portop6(port, proto, dir)
4099 int port, proto, dir;
4101 struct block *b0, *b1, *tmp;
4103 /* ip6 proto 'proto' */
4104 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
4108 b1 = gen_portatom6(0, (bpf_int32)port);
4112 b1 = gen_portatom6(2, (bpf_int32)port);
4117 tmp = gen_portatom6(0, (bpf_int32)port);
4118 b1 = gen_portatom6(2, (bpf_int32)port);
4123 tmp = gen_portatom6(0, (bpf_int32)port);
4124 b1 = gen_portatom6(2, (bpf_int32)port);
4136 static struct block *
4137 gen_port6(port, ip_proto, dir)
4142 struct block *b0, *b1, *tmp;
4144 /* link proto ip6 */
4145 b0 = gen_linktype(ETHERTYPE_IPV6);
4151 b1 = gen_portop6(port, ip_proto, dir);
4155 tmp = gen_portop6(port, IPPROTO_TCP, dir);
4156 b1 = gen_portop6(port, IPPROTO_UDP, dir);
4158 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
4170 /* gen_portrange code */
4171 static struct block *
4172 gen_portrangeatom(off, v1, v2)
4176 struct block *b1, *b2;
4180 * Reverse the order of the ports, so v1 is the lower one.
4189 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
4190 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
4198 gen_portrangeop(port1, port2, proto, dir)
4203 struct block *b0, *b1, *tmp;
4205 /* ip proto 'proto' */
4206 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
4212 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4216 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4221 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4222 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4227 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4228 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4240 static struct block *
4241 gen_portrange(port1, port2, ip_proto, dir)
4246 struct block *b0, *b1, *tmp;
4249 b0 = gen_linktype(ETHERTYPE_IP);
4255 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
4259 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
4260 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
4262 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
4274 static struct block *
4275 gen_portrangeatom6(off, v1, v2)
4279 struct block *b1, *b2;
4283 * Reverse the order of the ports, so v1 is the lower one.
4292 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
4293 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
4301 gen_portrangeop6(port1, port2, proto, dir)
4306 struct block *b0, *b1, *tmp;
4308 /* ip6 proto 'proto' */
4309 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
4313 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4317 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4322 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4323 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4328 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4329 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4341 static struct block *
4342 gen_portrange6(port1, port2, ip_proto, dir)
4347 struct block *b0, *b1, *tmp;
4349 /* link proto ip6 */
4350 b0 = gen_linktype(ETHERTYPE_IPV6);
4356 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
4360 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
4361 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
4363 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
4376 lookup_proto(name, proto)
4377 register const char *name;
4387 v = pcap_nametoproto(name);
4388 if (v == PROTO_UNDEF)
4389 bpf_error("unknown ip proto '%s'", name);
4393 /* XXX should look up h/w protocol type based on linktype */
4394 v = pcap_nametoeproto(name);
4395 if (v == PROTO_UNDEF) {
4396 v = pcap_nametollc(name);
4397 if (v == PROTO_UNDEF)
4398 bpf_error("unknown ether proto '%s'", name);
4403 if (strcmp(name, "esis") == 0)
4405 else if (strcmp(name, "isis") == 0)
4407 else if (strcmp(name, "clnp") == 0)
4410 bpf_error("unknown osi proto '%s'", name);
4430 static struct block *
4431 gen_protochain(v, proto, dir)
4436 #ifdef NO_PROTOCHAIN
4437 return gen_proto(v, proto, dir);
4439 struct block *b0, *b;
4440 struct slist *s[100];
4441 int fix2, fix3, fix4, fix5;
4442 int ahcheck, again, end;
4444 int reg2 = alloc_reg();
4446 memset(s, 0, sizeof(s));
4447 fix2 = fix3 = fix4 = fix5 = 0;
4454 b0 = gen_protochain(v, Q_IP, dir);
4455 b = gen_protochain(v, Q_IPV6, dir);
4459 bpf_error("bad protocol applied for 'protochain'");
4464 * We don't handle variable-length radiotap here headers yet.
4465 * We might want to add BPF instructions to do the protochain
4466 * work, to simplify that and, on platforms that have a BPF
4467 * interpreter with the new instructions, let the filtering
4468 * be done in the kernel. (We already require a modified BPF
4469 * engine to do the protochain stuff, to support backward
4470 * branches, and backward branch support is unlikely to appear
4471 * in kernel BPF engines.)
4473 if (linktype == DLT_IEEE802_11_RADIO)
4474 bpf_error("'protochain' not supported with radiotap headers");
4476 if (linktype == DLT_PPI)
4477 bpf_error("'protochain' not supported with PPI headers");
4479 no_optimize = 1; /*this code is not compatible with optimzer yet */
4482 * s[0] is a dummy entry to protect other BPF insn from damage
4483 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4484 * hard to find interdependency made by jump table fixup.
4487 s[i] = new_stmt(0); /*dummy*/
4492 b0 = gen_linktype(ETHERTYPE_IP);
4495 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4496 s[i]->s.k = off_ll + off_nl + 9;
4498 /* X = ip->ip_hl << 2 */
4499 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
4500 s[i]->s.k = off_ll + off_nl;
4505 b0 = gen_linktype(ETHERTYPE_IPV6);
4507 /* A = ip6->ip_nxt */
4508 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4509 s[i]->s.k = off_ll + off_nl + 6;
4511 /* X = sizeof(struct ip6_hdr) */
4512 s[i] = new_stmt(BPF_LDX|BPF_IMM);
4518 bpf_error("unsupported proto to gen_protochain");
4522 /* again: if (A == v) goto end; else fall through; */
4524 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4526 s[i]->s.jt = NULL; /*later*/
4527 s[i]->s.jf = NULL; /*update in next stmt*/
4531 #ifndef IPPROTO_NONE
4532 #define IPPROTO_NONE 59
4534 /* if (A == IPPROTO_NONE) goto end */
4535 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4536 s[i]->s.jt = NULL; /*later*/
4537 s[i]->s.jf = NULL; /*update in next stmt*/
4538 s[i]->s.k = IPPROTO_NONE;
4539 s[fix5]->s.jf = s[i];
4544 if (proto == Q_IPV6) {
4545 int v6start, v6end, v6advance, j;
4548 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4549 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4550 s[i]->s.jt = NULL; /*later*/
4551 s[i]->s.jf = NULL; /*update in next stmt*/
4552 s[i]->s.k = IPPROTO_HOPOPTS;
4553 s[fix2]->s.jf = s[i];
4555 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4556 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4557 s[i]->s.jt = NULL; /*later*/
4558 s[i]->s.jf = NULL; /*update in next stmt*/
4559 s[i]->s.k = IPPROTO_DSTOPTS;
4561 /* if (A == IPPROTO_ROUTING) goto v6advance */
4562 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4563 s[i]->s.jt = NULL; /*later*/
4564 s[i]->s.jf = NULL; /*update in next stmt*/
4565 s[i]->s.k = IPPROTO_ROUTING;
4567 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4568 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4569 s[i]->s.jt = NULL; /*later*/
4570 s[i]->s.jf = NULL; /*later*/
4571 s[i]->s.k = IPPROTO_FRAGMENT;
4582 * X = X + (P[X + 1] + 1) * 8;
4585 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4587 /* A = P[X + packet head] */
4588 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4589 s[i]->s.k = off_ll + off_nl;
4592 s[i] = new_stmt(BPF_ST);
4596 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4599 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4603 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4605 /* A = P[X + packet head]; */
4606 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4607 s[i]->s.k = off_ll + off_nl;
4610 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4614 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4618 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4621 s[i] = new_stmt(BPF_LD|BPF_MEM);
4625 /* goto again; (must use BPF_JA for backward jump) */
4626 s[i] = new_stmt(BPF_JMP|BPF_JA);
4627 s[i]->s.k = again - i - 1;
4628 s[i - 1]->s.jf = s[i];
4632 for (j = v6start; j <= v6end; j++)
4633 s[j]->s.jt = s[v6advance];
4638 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4640 s[fix2]->s.jf = s[i];
4646 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4647 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4648 s[i]->s.jt = NULL; /*later*/
4649 s[i]->s.jf = NULL; /*later*/
4650 s[i]->s.k = IPPROTO_AH;
4652 s[fix3]->s.jf = s[ahcheck];
4659 * X = X + (P[X + 1] + 2) * 4;
4662 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4664 /* A = P[X + packet head]; */
4665 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4666 s[i]->s.k = off_ll + off_nl;
4669 s[i] = new_stmt(BPF_ST);
4673 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4676 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4680 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4682 /* A = P[X + packet head] */
4683 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4684 s[i]->s.k = off_ll + off_nl;
4687 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4691 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4695 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4698 s[i] = new_stmt(BPF_LD|BPF_MEM);
4702 /* goto again; (must use BPF_JA for backward jump) */
4703 s[i] = new_stmt(BPF_JMP|BPF_JA);
4704 s[i]->s.k = again - i - 1;
4709 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4711 s[fix2]->s.jt = s[end];
4712 s[fix4]->s.jf = s[end];
4713 s[fix5]->s.jt = s[end];
4720 for (i = 0; i < max - 1; i++)
4721 s[i]->next = s[i + 1];
4722 s[max - 1]->next = NULL;
4727 b = new_block(JMP(BPF_JEQ));
4728 b->stmts = s[1]; /*remember, s[0] is dummy*/
4740 * Generate code that checks whether the packet is a packet for protocol
4741 * <proto> and whether the type field in that protocol's header has
4742 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4743 * IP packet and checks the protocol number in the IP header against <v>.
4745 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4746 * against Q_IP and Q_IPV6.
4748 static struct block *
4749 gen_proto(v, proto, dir)
4754 struct block *b0, *b1;
4756 if (dir != Q_DEFAULT)
4757 bpf_error("direction applied to 'proto'");
4762 b0 = gen_proto(v, Q_IP, dir);
4763 b1 = gen_proto(v, Q_IPV6, dir);
4771 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4772 * not LLC encapsulation with LLCSAP_IP.
4774 * For IEEE 802 networks - which includes 802.5 token ring
4775 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4776 * says that SNAP encapsulation is used, not LLC encapsulation
4779 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4780 * RFC 2225 say that SNAP encapsulation is used, not LLC
4781 * encapsulation with LLCSAP_IP.
4783 * So we always check for ETHERTYPE_IP.
4785 b0 = gen_linktype(ETHERTYPE_IP);
4787 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
4789 b1 = gen_protochain(v, Q_IP);
4799 * Frame Relay packets typically have an OSI
4800 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4801 * generates code to check for all the OSI
4802 * NLPIDs, so calling it and then adding a check
4803 * for the particular NLPID for which we're
4804 * looking is bogus, as we can just check for
4807 * What we check for is the NLPID and a frame
4808 * control field value of UI, i.e. 0x03 followed
4811 * XXX - assumes a 2-byte Frame Relay header with
4812 * DLCI and flags. What if the address is longer?
4814 * XXX - what about SNAP-encapsulated frames?
4816 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
4822 * Cisco uses an Ethertype lookalike - for OSI,
4825 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
4826 /* OSI in C-HDLC is stuffed with a fudge byte */
4827 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
4832 b0 = gen_linktype(LLCSAP_ISONS);
4833 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
4839 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4841 * 4 is the offset of the PDU type relative to the IS-IS
4844 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
4849 bpf_error("arp does not encapsulate another protocol");
4853 bpf_error("rarp does not encapsulate another protocol");
4857 bpf_error("atalk encapsulation is not specifiable");
4861 bpf_error("decnet encapsulation is not specifiable");
4865 bpf_error("sca does not encapsulate another protocol");
4869 bpf_error("lat does not encapsulate another protocol");
4873 bpf_error("moprc does not encapsulate another protocol");
4877 bpf_error("mopdl does not encapsulate another protocol");
4881 return gen_linktype(v);
4884 bpf_error("'udp proto' is bogus");
4888 bpf_error("'tcp proto' is bogus");
4892 bpf_error("'sctp proto' is bogus");
4896 bpf_error("'icmp proto' is bogus");
4900 bpf_error("'igmp proto' is bogus");
4904 bpf_error("'igrp proto' is bogus");
4908 bpf_error("'pim proto' is bogus");
4912 bpf_error("'vrrp proto' is bogus");
4917 b0 = gen_linktype(ETHERTYPE_IPV6);
4919 b1 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
4921 b1 = gen_protochain(v, Q_IPV6);
4927 bpf_error("'icmp6 proto' is bogus");
4931 bpf_error("'ah proto' is bogus");
4934 bpf_error("'ah proto' is bogus");
4937 bpf_error("'stp proto' is bogus");
4940 bpf_error("'ipx proto' is bogus");
4943 bpf_error("'netbeui proto' is bogus");
4946 bpf_error("'radio proto' is bogus");
4957 register const char *name;
4960 int proto = q.proto;
4964 bpf_u_int32 mask, addr;
4966 bpf_u_int32 **alist;
4969 struct sockaddr_in *sin4;
4970 struct sockaddr_in6 *sin6;
4971 struct addrinfo *res, *res0;
4972 struct in6_addr mask128;
4974 struct block *b, *tmp;
4975 int port, real_proto;
4981 addr = pcap_nametonetaddr(name);
4983 bpf_error("unknown network '%s'", name);
4984 /* Left justify network addr and calculate its network mask */
4986 while (addr && (addr & 0xff000000) == 0) {
4990 return gen_host(addr, mask, proto, dir, q.addr);
4994 if (proto == Q_LINK) {
4998 eaddr = pcap_ether_hostton(name);
5001 "unknown ether host '%s'", name);
5002 b = gen_ehostop(eaddr, dir);
5007 eaddr = pcap_ether_hostton(name);
5010 "unknown FDDI host '%s'", name);
5011 b = gen_fhostop(eaddr, dir);
5016 eaddr = pcap_ether_hostton(name);
5019 "unknown token ring host '%s'", name);
5020 b = gen_thostop(eaddr, dir);
5024 case DLT_IEEE802_11:
5025 case DLT_IEEE802_11_RADIO_AVS:
5026 case DLT_IEEE802_11_RADIO:
5027 case DLT_PRISM_HEADER:
5029 eaddr = pcap_ether_hostton(name);
5032 "unknown 802.11 host '%s'", name);
5033 b = gen_wlanhostop(eaddr, dir);
5037 case DLT_IP_OVER_FC:
5038 eaddr = pcap_ether_hostton(name);
5041 "unknown Fibre Channel host '%s'", name);
5042 b = gen_ipfchostop(eaddr, dir);
5051 * Check that the packet doesn't begin
5052 * with an LE Control marker. (We've
5053 * already generated a test for LANE.)
5055 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
5059 eaddr = pcap_ether_hostton(name);
5062 "unknown ether host '%s'", name);
5063 b = gen_ehostop(eaddr, dir);
5069 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5070 } else if (proto == Q_DECNET) {
5071 unsigned short dn_addr = __pcap_nametodnaddr(name);
5073 * I don't think DECNET hosts can be multihomed, so
5074 * there is no need to build up a list of addresses
5076 return (gen_host(dn_addr, 0, proto, dir, q.addr));
5079 alist = pcap_nametoaddr(name);
5080 if (alist == NULL || *alist == NULL)
5081 bpf_error("unknown host '%s'", name);
5083 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
5085 b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
5087 tmp = gen_host(**alist++, 0xffffffff,
5088 tproto, dir, q.addr);
5094 memset(&mask128, 0xff, sizeof(mask128));
5095 res0 = res = pcap_nametoaddrinfo(name);
5097 bpf_error("unknown host '%s'", name);
5099 tproto = tproto6 = proto;
5100 if (off_linktype == -1 && tproto == Q_DEFAULT) {
5104 for (res = res0; res; res = res->ai_next) {
5105 switch (res->ai_family) {
5107 if (tproto == Q_IPV6)
5110 sin4 = (struct sockaddr_in *)
5112 tmp = gen_host(ntohl(sin4->sin_addr.s_addr),
5113 0xffffffff, tproto, dir, q.addr);
5116 if (tproto6 == Q_IP)
5119 sin6 = (struct sockaddr_in6 *)
5121 tmp = gen_host6(&sin6->sin6_addr,
5122 &mask128, tproto6, dir, q.addr);
5133 bpf_error("unknown host '%s'%s", name,
5134 (proto == Q_DEFAULT)
5136 : " for specified address family");
5143 if (proto != Q_DEFAULT &&
5144 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
5145 bpf_error("illegal qualifier of 'port'");
5146 if (pcap_nametoport(name, &port, &real_proto) == 0)
5147 bpf_error("unknown port '%s'", name);
5148 if (proto == Q_UDP) {
5149 if (real_proto == IPPROTO_TCP)
5150 bpf_error("port '%s' is tcp", name);
5151 else if (real_proto == IPPROTO_SCTP)
5152 bpf_error("port '%s' is sctp", name);
5154 /* override PROTO_UNDEF */
5155 real_proto = IPPROTO_UDP;
5157 if (proto == Q_TCP) {
5158 if (real_proto == IPPROTO_UDP)
5159 bpf_error("port '%s' is udp", name);
5161 else if (real_proto == IPPROTO_SCTP)
5162 bpf_error("port '%s' is sctp", name);
5164 /* override PROTO_UNDEF */
5165 real_proto = IPPROTO_TCP;
5167 if (proto == Q_SCTP) {
5168 if (real_proto == IPPROTO_UDP)
5169 bpf_error("port '%s' is udp", name);
5171 else if (real_proto == IPPROTO_TCP)
5172 bpf_error("port '%s' is tcp", name);
5174 /* override PROTO_UNDEF */
5175 real_proto = IPPROTO_SCTP;
5178 return gen_port(port, real_proto, dir);
5180 b = gen_port(port, real_proto, dir);
5181 gen_or(gen_port6(port, real_proto, dir), b);
5186 if (proto != Q_DEFAULT &&
5187 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
5188 bpf_error("illegal qualifier of 'portrange'");
5189 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
5190 bpf_error("unknown port in range '%s'", name);
5191 if (proto == Q_UDP) {
5192 if (real_proto == IPPROTO_TCP)
5193 bpf_error("port in range '%s' is tcp", name);
5194 else if (real_proto == IPPROTO_SCTP)
5195 bpf_error("port in range '%s' is sctp", name);
5197 /* override PROTO_UNDEF */
5198 real_proto = IPPROTO_UDP;
5200 if (proto == Q_TCP) {
5201 if (real_proto == IPPROTO_UDP)
5202 bpf_error("port in range '%s' is udp", name);
5203 else if (real_proto == IPPROTO_SCTP)
5204 bpf_error("port in range '%s' is sctp", name);
5206 /* override PROTO_UNDEF */
5207 real_proto = IPPROTO_TCP;
5209 if (proto == Q_SCTP) {
5210 if (real_proto == IPPROTO_UDP)
5211 bpf_error("port in range '%s' is udp", name);
5212 else if (real_proto == IPPROTO_TCP)
5213 bpf_error("port in range '%s' is tcp", name);
5215 /* override PROTO_UNDEF */
5216 real_proto = IPPROTO_SCTP;
5219 return gen_portrange(port1, port2, real_proto, dir);
5221 b = gen_portrange(port1, port2, real_proto, dir);
5222 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
5228 eaddr = pcap_ether_hostton(name);
5230 bpf_error("unknown ether host: %s", name);
5232 alist = pcap_nametoaddr(name);
5233 if (alist == NULL || *alist == NULL)
5234 bpf_error("unknown host '%s'", name);
5235 b = gen_gateway(eaddr, alist, proto, dir);
5239 bpf_error("'gateway' not supported in this configuration");
5243 real_proto = lookup_proto(name, proto);
5244 if (real_proto >= 0)
5245 return gen_proto(real_proto, proto, dir);
5247 bpf_error("unknown protocol: %s", name);
5250 real_proto = lookup_proto(name, proto);
5251 if (real_proto >= 0)
5252 return gen_protochain(real_proto, proto, dir);
5254 bpf_error("unknown protocol: %s", name);
5266 gen_mcode(s1, s2, masklen, q)
5267 register const char *s1, *s2;
5268 register int masklen;
5271 register int nlen, mlen;
5274 nlen = __pcap_atoin(s1, &n);
5275 /* Promote short ipaddr */
5279 mlen = __pcap_atoin(s2, &m);
5280 /* Promote short ipaddr */
5283 bpf_error("non-network bits set in \"%s mask %s\"",
5286 /* Convert mask len to mask */
5288 bpf_error("mask length must be <= 32");
5291 * X << 32 is not guaranteed by C to be 0; it's
5296 m = 0xffffffff << (32 - masklen);
5298 bpf_error("non-network bits set in \"%s/%d\"",
5305 return gen_host(n, m, q.proto, q.dir, q.addr);
5308 bpf_error("Mask syntax for networks only");
5317 register const char *s;
5322 int proto = q.proto;
5328 else if (q.proto == Q_DECNET)
5329 vlen = __pcap_atodn(s, &v);
5331 vlen = __pcap_atoin(s, &v);
5338 if (proto == Q_DECNET)
5339 return gen_host(v, 0, proto, dir, q.addr);
5340 else if (proto == Q_LINK) {
5341 bpf_error("illegal link layer address");
5344 if (s == NULL && q.addr == Q_NET) {
5345 /* Promote short net number */
5346 while (v && (v & 0xff000000) == 0) {
5351 /* Promote short ipaddr */
5355 return gen_host(v, mask, proto, dir, q.addr);
5360 proto = IPPROTO_UDP;
5361 else if (proto == Q_TCP)
5362 proto = IPPROTO_TCP;
5363 else if (proto == Q_SCTP)
5364 proto = IPPROTO_SCTP;
5365 else if (proto == Q_DEFAULT)
5366 proto = PROTO_UNDEF;
5368 bpf_error("illegal qualifier of 'port'");
5371 return gen_port((int)v, proto, dir);
5375 b = gen_port((int)v, proto, dir);
5376 gen_or(gen_port6((int)v, proto, dir), b);
5383 proto = IPPROTO_UDP;
5384 else if (proto == Q_TCP)
5385 proto = IPPROTO_TCP;
5386 else if (proto == Q_SCTP)
5387 proto = IPPROTO_SCTP;
5388 else if (proto == Q_DEFAULT)
5389 proto = PROTO_UNDEF;
5391 bpf_error("illegal qualifier of 'portrange'");
5394 return gen_portrange((int)v, (int)v, proto, dir);
5398 b = gen_portrange((int)v, (int)v, proto, dir);
5399 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
5405 bpf_error("'gateway' requires a name");
5409 return gen_proto((int)v, proto, dir);
5412 return gen_protochain((int)v, proto, dir);
5427 gen_mcode6(s1, s2, masklen, q)
5428 register const char *s1, *s2;
5429 register int masklen;
5432 struct addrinfo *res;
5433 struct in6_addr *addr;
5434 struct in6_addr mask;
5439 bpf_error("no mask %s supported", s2);
5441 res = pcap_nametoaddrinfo(s1);
5443 bpf_error("invalid ip6 address %s", s1);
5445 bpf_error("%s resolved to multiple address", s1);
5446 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
5448 if (sizeof(mask) * 8 < masklen)
5449 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
5450 memset(&mask, 0, sizeof(mask));
5451 memset(&mask, 0xff, masklen / 8);
5453 mask.s6_addr[masklen / 8] =
5454 (0xff << (8 - masklen % 8)) & 0xff;
5457 a = (u_int32_t *)addr;
5458 m = (u_int32_t *)&mask;
5459 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
5460 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
5461 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
5469 bpf_error("Mask syntax for networks only");
5473 b = gen_host6(addr, &mask, q.proto, q.dir, q.addr);
5478 bpf_error("invalid qualifier against IPv6 address");
5487 register const u_char *eaddr;
5490 struct block *b, *tmp;
5492 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
5495 return gen_ehostop(eaddr, (int)q.dir);
5497 return gen_fhostop(eaddr, (int)q.dir);
5499 return gen_thostop(eaddr, (int)q.dir);
5500 case DLT_IEEE802_11:
5501 case DLT_IEEE802_11_RADIO_AVS:
5502 case DLT_IEEE802_11_RADIO:
5503 case DLT_PRISM_HEADER:
5505 return gen_wlanhostop(eaddr, (int)q.dir);
5509 * Check that the packet doesn't begin with an
5510 * LE Control marker. (We've already generated
5513 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5518 * Now check the MAC address.
5520 b = gen_ehostop(eaddr, (int)q.dir);
5525 case DLT_IP_OVER_FC:
5526 return gen_ipfchostop(eaddr, (int)q.dir);
5528 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5532 bpf_error("ethernet address used in non-ether expression");
5539 struct slist *s0, *s1;
5542 * This is definitely not the best way to do this, but the
5543 * lists will rarely get long.
5550 static struct slist *
5556 s = new_stmt(BPF_LDX|BPF_MEM);
5561 static struct slist *
5567 s = new_stmt(BPF_LD|BPF_MEM);
5573 * Modify "index" to use the value stored into its register as an
5574 * offset relative to the beginning of the header for the protocol
5575 * "proto", and allocate a register and put an item "size" bytes long
5576 * (1, 2, or 4) at that offset into that register, making it the register
5580 gen_load(proto, inst, size)
5585 struct slist *s, *tmp;
5587 int regno = alloc_reg();
5589 free_reg(inst->regno);
5593 bpf_error("data size must be 1, 2, or 4");
5609 bpf_error("unsupported index operation");
5613 * The offset is relative to the beginning of the packet
5614 * data, if we have a radio header. (If we don't, this
5617 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
5618 linktype != DLT_IEEE802_11_RADIO &&
5619 linktype != DLT_PRISM_HEADER)
5620 bpf_error("radio information not present in capture");
5623 * Load into the X register the offset computed into the
5624 * register specifed by "index".
5626 s = xfer_to_x(inst);
5629 * Load the item at that offset.
5631 tmp = new_stmt(BPF_LD|BPF_IND|size);
5633 sappend(inst->s, s);
5638 * The offset is relative to the beginning of
5639 * the link-layer header.
5641 * XXX - what about ATM LANE? Should the index be
5642 * relative to the beginning of the AAL5 frame, so
5643 * that 0 refers to the beginning of the LE Control
5644 * field, or relative to the beginning of the LAN
5645 * frame, so that 0 refers, for Ethernet LANE, to
5646 * the beginning of the destination address?
5648 s = gen_llprefixlen();
5651 * If "s" is non-null, it has code to arrange that the
5652 * X register contains the length of the prefix preceding
5653 * the link-layer header. Add to it the offset computed
5654 * into the register specified by "index", and move that
5655 * into the X register. Otherwise, just load into the X
5656 * register the offset computed into the register specifed
5660 sappend(s, xfer_to_a(inst));
5661 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5662 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5664 s = xfer_to_x(inst);
5667 * Load the item at the sum of the offset we've put in the
5668 * X register and the offset of the start of the link
5669 * layer header (which is 0 if the radio header is
5670 * variable-length; that header length is what we put
5671 * into the X register and then added to the index).
5673 tmp = new_stmt(BPF_LD|BPF_IND|size);
5676 sappend(inst->s, s);
5692 * The offset is relative to the beginning of
5693 * the network-layer header.
5694 * XXX - are there any cases where we want
5697 s = gen_llprefixlen();
5700 * If "s" is non-null, it has code to arrange that the
5701 * X register contains the length of the prefix preceding
5702 * the link-layer header. Add to it the offset computed
5703 * into the register specified by "index", and move that
5704 * into the X register. Otherwise, just load into the X
5705 * register the offset computed into the register specifed
5709 sappend(s, xfer_to_a(inst));
5710 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5711 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5713 s = xfer_to_x(inst);
5716 * Load the item at the sum of the offset we've put in the
5717 * X register, the offset of the start of the network
5718 * layer header, and the offset of the start of the link
5719 * layer header (which is 0 if the radio header is
5720 * variable-length; that header length is what we put
5721 * into the X register and then added to the index).
5723 tmp = new_stmt(BPF_LD|BPF_IND|size);
5724 tmp->s.k = off_ll + off_nl;
5726 sappend(inst->s, s);
5729 * Do the computation only if the packet contains
5730 * the protocol in question.
5732 b = gen_proto_abbrev(proto);
5734 gen_and(inst->b, b);
5747 * The offset is relative to the beginning of
5748 * the transport-layer header.
5750 * Load the X register with the length of the IPv4 header
5751 * (plus the offset of the link-layer header, if it's
5752 * a variable-length header), in bytes.
5754 * XXX - are there any cases where we want
5756 * XXX - we should, if we're built with
5757 * IPv6 support, generate code to load either
5758 * IPv4, IPv6, or both, as appropriate.
5760 s = gen_loadx_iphdrlen();
5763 * The X register now contains the sum of the length
5764 * of any variable-length header preceding the link-layer
5765 * header and the length of the network-layer header.
5766 * Load into the A register the offset relative to
5767 * the beginning of the transport layer header,
5768 * add the X register to that, move that to the
5769 * X register, and load with an offset from the
5770 * X register equal to the offset of the network
5771 * layer header relative to the beginning of
5772 * the link-layer header plus the length of any
5773 * fixed-length header preceding the link-layer
5776 sappend(s, xfer_to_a(inst));
5777 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5778 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5779 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
5780 tmp->s.k = off_ll + off_nl;
5781 sappend(inst->s, s);
5784 * Do the computation only if the packet contains
5785 * the protocol in question - which is true only
5786 * if this is an IP datagram and is the first or
5787 * only fragment of that datagram.
5789 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
5791 gen_and(inst->b, b);
5793 gen_and(gen_proto_abbrev(Q_IP), b);
5799 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5803 inst->regno = regno;
5804 s = new_stmt(BPF_ST);
5806 sappend(inst->s, s);
5812 gen_relation(code, a0, a1, reversed)
5814 struct arth *a0, *a1;
5817 struct slist *s0, *s1, *s2;
5818 struct block *b, *tmp;
5822 if (code == BPF_JEQ) {
5823 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
5824 b = new_block(JMP(code));
5828 b = new_block(BPF_JMP|code|BPF_X);
5834 sappend(a0->s, a1->s);
5838 free_reg(a0->regno);
5839 free_reg(a1->regno);
5841 /* 'and' together protocol checks */
5844 gen_and(a0->b, tmp = a1->b);
5860 int regno = alloc_reg();
5861 struct arth *a = (struct arth *)newchunk(sizeof(*a));
5864 s = new_stmt(BPF_LD|BPF_LEN);
5865 s->next = new_stmt(BPF_ST);
5866 s->next->s.k = regno;
5881 a = (struct arth *)newchunk(sizeof(*a));
5885 s = new_stmt(BPF_LD|BPF_IMM);
5887 s->next = new_stmt(BPF_ST);
5903 s = new_stmt(BPF_ALU|BPF_NEG);
5906 s = new_stmt(BPF_ST);
5914 gen_arth(code, a0, a1)
5916 struct arth *a0, *a1;
5918 struct slist *s0, *s1, *s2;
5922 s2 = new_stmt(BPF_ALU|BPF_X|code);
5927 sappend(a0->s, a1->s);
5929 free_reg(a0->regno);
5930 free_reg(a1->regno);
5932 s0 = new_stmt(BPF_ST);
5933 a0->regno = s0->s.k = alloc_reg();
5940 * Here we handle simple allocation of the scratch registers.
5941 * If too many registers are alloc'd, the allocator punts.
5943 static int regused[BPF_MEMWORDS];
5947 * Return the next free register.
5952 int n = BPF_MEMWORDS;
5955 if (regused[curreg])
5956 curreg = (curreg + 1) % BPF_MEMWORDS;
5958 regused[curreg] = 1;
5962 bpf_error("too many registers needed to evaluate expression");
5968 * Return a register to the table so it can
5978 static struct block *
5985 s = new_stmt(BPF_LD|BPF_LEN);
5986 b = new_block(JMP(jmp));
5997 return gen_len(BPF_JGE, n);
6001 * Actually, this is less than or equal.
6009 b = gen_len(BPF_JGT, n);
6016 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6017 * the beginning of the link-layer header.
6018 * XXX - that means you can't test values in the radiotap header, but
6019 * as that header is difficult if not impossible to parse generally
6020 * without a loop, that might not be a severe problem. A new keyword
6021 * "radio" could be added for that, although what you'd really want
6022 * would be a way of testing particular radio header values, which
6023 * would generate code appropriate to the radio header in question.
6026 gen_byteop(op, idx, val)
6037 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6040 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6044 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6048 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
6052 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
6056 b = new_block(JMP(BPF_JEQ));
6063 static u_char abroadcast[] = { 0x0 };
6066 gen_broadcast(proto)
6069 bpf_u_int32 hostmask;
6070 struct block *b0, *b1, *b2;
6071 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6079 case DLT_ARCNET_LINUX:
6080 return gen_ahostop(abroadcast, Q_DST);
6082 return gen_ehostop(ebroadcast, Q_DST);
6084 return gen_fhostop(ebroadcast, Q_DST);
6086 return gen_thostop(ebroadcast, Q_DST);
6087 case DLT_IEEE802_11:
6088 case DLT_IEEE802_11_RADIO_AVS:
6089 case DLT_IEEE802_11_RADIO:
6091 case DLT_PRISM_HEADER:
6092 return gen_wlanhostop(ebroadcast, Q_DST);
6093 case DLT_IP_OVER_FC:
6094 return gen_ipfchostop(ebroadcast, Q_DST);
6098 * Check that the packet doesn't begin with an
6099 * LE Control marker. (We've already generated
6102 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6107 * Now check the MAC address.
6109 b0 = gen_ehostop(ebroadcast, Q_DST);
6115 bpf_error("not a broadcast link");
6120 b0 = gen_linktype(ETHERTYPE_IP);
6121 hostmask = ~netmask;
6122 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
6123 b2 = gen_mcmp(OR_NET, 16, BPF_W,
6124 (bpf_int32)(~0 & hostmask), hostmask);
6129 bpf_error("only link-layer/IP broadcast filters supported");
6135 * Generate code to test the low-order bit of a MAC address (that's
6136 * the bottom bit of the *first* byte).
6138 static struct block *
6139 gen_mac_multicast(offset)
6142 register struct block *b0;
6143 register struct slist *s;
6145 /* link[offset] & 1 != 0 */
6146 s = gen_load_a(OR_LINK, offset, BPF_B);
6147 b0 = new_block(JMP(BPF_JSET));
6154 gen_multicast(proto)
6157 register struct block *b0, *b1, *b2;
6158 register struct slist *s;
6166 case DLT_ARCNET_LINUX:
6167 /* all ARCnet multicasts use the same address */
6168 return gen_ahostop(abroadcast, Q_DST);
6170 /* ether[0] & 1 != 0 */
6171 return gen_mac_multicast(0);
6174 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
6176 * XXX - was that referring to bit-order issues?
6178 /* fddi[1] & 1 != 0 */
6179 return gen_mac_multicast(1);
6181 /* tr[2] & 1 != 0 */
6182 return gen_mac_multicast(2);
6183 case DLT_IEEE802_11:
6184 case DLT_IEEE802_11_RADIO_AVS:
6186 case DLT_IEEE802_11_RADIO:
6187 case DLT_PRISM_HEADER:
6191 * For control frames, there is no DA.
6193 * For management frames, DA is at an
6194 * offset of 4 from the beginning of
6197 * For data frames, DA is at an offset
6198 * of 4 from the beginning of the packet
6199 * if To DS is clear and at an offset of
6200 * 16 from the beginning of the packet
6205 * Generate the tests to be done for data frames.
6207 * First, check for To DS set, i.e. "link[1] & 0x01".
6209 s = gen_load_a(OR_LINK, 1, BPF_B);
6210 b1 = new_block(JMP(BPF_JSET));
6211 b1->s.k = 0x01; /* To DS */
6215 * If To DS is set, the DA is at 16.
6217 b0 = gen_mac_multicast(16);
6221 * Now, check for To DS not set, i.e. check
6222 * "!(link[1] & 0x01)".
6224 s = gen_load_a(OR_LINK, 1, BPF_B);
6225 b2 = new_block(JMP(BPF_JSET));
6226 b2->s.k = 0x01; /* To DS */
6231 * If To DS is not set, the DA is at 4.
6233 b1 = gen_mac_multicast(4);
6237 * Now OR together the last two checks. That gives
6238 * the complete set of checks for data frames.
6243 * Now check for a data frame.
6244 * I.e, check "link[0] & 0x08".
6246 s = gen_load_a(OR_LINK, 0, BPF_B);
6247 b1 = new_block(JMP(BPF_JSET));
6252 * AND that with the checks done for data frames.
6257 * If the high-order bit of the type value is 0, this
6258 * is a management frame.
6259 * I.e, check "!(link[0] & 0x08)".
6261 s = gen_load_a(OR_LINK, 0, BPF_B);
6262 b2 = new_block(JMP(BPF_JSET));
6268 * For management frames, the DA is at 4.
6270 b1 = gen_mac_multicast(4);
6274 * OR that with the checks done for data frames.
6275 * That gives the checks done for management and
6281 * If the low-order bit of the type value is 1,
6282 * this is either a control frame or a frame
6283 * with a reserved type, and thus not a
6286 * I.e., check "!(link[0] & 0x04)".
6288 s = gen_load_a(OR_LINK, 0, BPF_B);
6289 b1 = new_block(JMP(BPF_JSET));
6295 * AND that with the checks for data and management
6300 case DLT_IP_OVER_FC:
6301 b0 = gen_mac_multicast(2);
6306 * Check that the packet doesn't begin with an
6307 * LE Control marker. (We've already generated
6310 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6314 /* ether[off_mac] & 1 != 0 */
6315 b0 = gen_mac_multicast(off_mac);
6323 /* Link not known to support multicasts */
6327 b0 = gen_linktype(ETHERTYPE_IP);
6328 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
6334 b0 = gen_linktype(ETHERTYPE_IPV6);
6335 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
6340 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6346 * generate command for inbound/outbound. It's here so we can
6347 * make it link-type specific. 'dir' = 0 implies "inbound",
6348 * = 1 implies "outbound".
6354 register struct block *b0;
6357 * Only some data link types support inbound/outbound qualifiers.
6361 b0 = gen_relation(BPF_JEQ,
6362 gen_load(Q_LINK, gen_loadi(0), 1),
6370 * Match packets sent by this machine.
6372 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
6375 * Match packets sent to this machine.
6376 * (No broadcast or multicast packets, or
6377 * packets sent to some other machine and
6378 * received promiscuously.)
6380 * XXX - packets sent to other machines probably
6381 * shouldn't be matched, but what about broadcast
6382 * or multicast packets we received?
6384 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_HOST);
6388 #ifdef HAVE_NET_PFVAR_H
6390 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
6391 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
6397 /* match outgoing packets */
6398 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
6400 /* match incoming packets */
6401 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
6405 case DLT_JUNIPER_MFR:
6406 case DLT_JUNIPER_MLFR:
6407 case DLT_JUNIPER_MLPPP:
6408 case DLT_JUNIPER_ATM1:
6409 case DLT_JUNIPER_ATM2:
6410 case DLT_JUNIPER_PPPOE:
6411 case DLT_JUNIPER_PPPOE_ATM:
6412 case DLT_JUNIPER_GGSN:
6413 case DLT_JUNIPER_ES:
6414 case DLT_JUNIPER_MONITOR:
6415 case DLT_JUNIPER_SERVICES:
6416 case DLT_JUNIPER_ETHER:
6417 case DLT_JUNIPER_PPP:
6418 case DLT_JUNIPER_FRELAY:
6419 case DLT_JUNIPER_CHDLC:
6420 case DLT_JUNIPER_VP:
6421 /* juniper flags (including direction) are stored
6422 * the byte after the 3-byte magic number */
6424 /* match outgoing packets */
6425 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
6427 /* match incoming packets */
6428 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
6433 bpf_error("inbound/outbound not supported on linktype %d",
6441 #ifdef HAVE_NET_PFVAR_H
6442 /* PF firewall log matched interface */
6444 gen_pf_ifname(const char *ifname)
6449 if (linktype == DLT_PFLOG) {
6450 len = sizeof(((struct pfloghdr *)0)->ifname);
6451 off = offsetof(struct pfloghdr, ifname);
6453 bpf_error("ifname not supported on linktype 0x%x", linktype);
6456 if (strlen(ifname) >= len) {
6457 bpf_error("ifname interface names can only be %d characters",
6461 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
6465 /* PF firewall log ruleset name */
6467 gen_pf_ruleset(char *ruleset)
6471 if (linktype != DLT_PFLOG) {
6472 bpf_error("ruleset not supported on linktype 0x%x", linktype);
6475 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
6476 bpf_error("ruleset names can only be %ld characters",
6477 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
6480 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
6481 strlen(ruleset), (const u_char *)ruleset);
6485 /* PF firewall log rule number */
6491 if (linktype == DLT_PFLOG) {
6492 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
6495 bpf_error("rnr not supported on linktype 0x%x", linktype);
6502 /* PF firewall log sub-rule number */
6504 gen_pf_srnr(int srnr)
6508 if (linktype != DLT_PFLOG) {
6509 bpf_error("srnr not supported on linktype 0x%x", linktype);
6513 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
6518 /* PF firewall log reason code */
6520 gen_pf_reason(int reason)
6524 if (linktype == DLT_PFLOG) {
6525 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
6528 bpf_error("reason not supported on linktype 0x%x", linktype);
6535 /* PF firewall log action */
6537 gen_pf_action(int action)
6541 if (linktype == DLT_PFLOG) {
6542 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
6545 bpf_error("action not supported on linktype 0x%x", linktype);
6551 #else /* !HAVE_NET_PFVAR_H */
6553 gen_pf_ifname(const char *ifname)
6555 bpf_error("libpcap was compiled without pf support");
6561 gen_pf_ruleset(char *ruleset)
6563 bpf_error("libpcap was compiled on a machine without pf support");
6571 bpf_error("libpcap was compiled on a machine without pf support");
6577 gen_pf_srnr(int srnr)
6579 bpf_error("libpcap was compiled on a machine without pf support");
6585 gen_pf_reason(int reason)
6587 bpf_error("libpcap was compiled on a machine without pf support");
6593 gen_pf_action(int action)
6595 bpf_error("libpcap was compiled on a machine without pf support");
6599 #endif /* HAVE_NET_PFVAR_H */
6601 /* IEEE 802.11 wireless header */
6603 gen_p80211_type(int type, int mask)
6607 if (linktype != DLT_IEEE802_11 && linktype != DLT_IEEE802_11_RADIO) {
6608 bpf_error("action not supported on linktype 0x%x\n", linktype);
6611 b0 = gen_mcmp(OR_LINK, offsetof(struct ieee80211_frame, i_fc[0]),
6612 BPF_B, (bpf_int32)type, (bpf_int32)mask);
6618 register const u_char *eaddr;
6621 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6622 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
6623 return gen_ahostop(eaddr, (int)q.dir);
6625 bpf_error("ARCnet address used in non-arc expression");
6630 static struct block *
6631 gen_ahostop(eaddr, dir)
6632 register const u_char *eaddr;
6635 register struct block *b0, *b1;
6638 /* src comes first, different from Ethernet */
6640 return gen_bcmp(OR_LINK, 0, 1, eaddr);
6643 return gen_bcmp(OR_LINK, 1, 1, eaddr);
6646 b0 = gen_ahostop(eaddr, Q_SRC);
6647 b1 = gen_ahostop(eaddr, Q_DST);
6653 b0 = gen_ahostop(eaddr, Q_SRC);
6654 b1 = gen_ahostop(eaddr, Q_DST);
6663 * support IEEE 802.1Q VLAN trunk over ethernet
6669 struct block *b0, *b1;
6671 /* can't check for VLAN-encapsulated packets inside MPLS */
6672 if (label_stack_depth > 0)
6673 bpf_error("no VLAN match after MPLS");
6676 * Change the offsets to point to the type and data fields within
6677 * the VLAN packet. Just increment the offsets, so that we
6678 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6679 * capture VLAN 200 encapsulated within VLAN 100.
6681 * XXX - this is a bit of a kludge. If we were to split the
6682 * compiler into a parser that parses an expression and
6683 * generates an expression tree, and a code generator that
6684 * takes an expression tree (which could come from our
6685 * parser or from some other parser) and generates BPF code,
6686 * we could perhaps make the offsets parameters of routines
6687 * and, in the handler for an "AND" node, pass to subnodes
6688 * other than the VLAN node the adjusted offsets.
6690 * This would mean that "vlan" would, instead of changing the
6691 * behavior of *all* tests after it, change only the behavior
6692 * of tests ANDed with it. That would change the documented
6693 * semantics of "vlan", which might break some expressions.
6694 * However, it would mean that "(vlan and ip) or ip" would check
6695 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6696 * checking only for VLAN-encapsulated IP, so that could still
6697 * be considered worth doing; it wouldn't break expressions
6698 * that are of the form "vlan and ..." or "vlan N and ...",
6699 * which I suspect are the most common expressions involving
6700 * "vlan". "vlan or ..." doesn't necessarily do what the user
6701 * would really want, now, as all the "or ..." tests would
6702 * be done assuming a VLAN, even though the "or" could be viewed
6703 * as meaning "or, if this isn't a VLAN packet...".
6705 orig_linktype = off_linktype; /* save original values */
6717 bpf_error("no VLAN support for data link type %d",
6722 /* check for VLAN */
6723 b0 = gen_cmp(OR_LINK, orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q);
6725 /* If a specific VLAN is requested, check VLAN id */
6726 if (vlan_num >= 0) {
6727 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_H, (bpf_int32)vlan_num,
6743 struct block *b0,*b1;
6746 * Change the offsets to point to the type and data fields within
6747 * the MPLS packet. Just increment the offsets, so that we
6748 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6749 * capture packets with an outer label of 100000 and an inner
6752 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6756 if (label_stack_depth > 0) {
6757 /* just match the bottom-of-stack bit clear */
6758 b0 = gen_mcmp(OR_LINK, orig_nl-2, BPF_B, 0, 0x01);
6761 * Indicate that we're checking MPLS-encapsulated headers,
6762 * to make sure higher level code generators don't try to
6763 * match against IP-related protocols such as Q_ARP, Q_RARP
6768 case DLT_C_HDLC: /* fall through */
6770 b0 = gen_linktype(ETHERTYPE_MPLS);
6774 b0 = gen_linktype(PPP_MPLS_UCAST);
6777 /* FIXME add other DLT_s ...
6778 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6779 * leave it for now */
6782 bpf_error("no MPLS support for data link type %d",
6790 /* If a specific MPLS label is requested, check it */
6791 if (label_num >= 0) {
6792 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
6793 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_W, (bpf_int32)label_num,
6794 0xfffff000); /* only compare the first 20 bits */
6801 label_stack_depth++;
6806 * Support PPPOE discovery and session.
6811 /* check for PPPoE discovery */
6812 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
6821 * Test against the PPPoE session link-layer type.
6823 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
6826 * Change the offsets to point to the type and data fields within
6829 * XXX - this is a bit of a kludge. If we were to split the
6830 * compiler into a parser that parses an expression and
6831 * generates an expression tree, and a code generator that
6832 * takes an expression tree (which could come from our
6833 * parser or from some other parser) and generates BPF code,
6834 * we could perhaps make the offsets parameters of routines
6835 * and, in the handler for an "AND" node, pass to subnodes
6836 * other than the PPPoE node the adjusted offsets.
6838 * This would mean that "pppoes" would, instead of changing the
6839 * behavior of *all* tests after it, change only the behavior
6840 * of tests ANDed with it. That would change the documented
6841 * semantics of "pppoes", which might break some expressions.
6842 * However, it would mean that "(pppoes and ip) or ip" would check
6843 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6844 * checking only for VLAN-encapsulated IP, so that could still
6845 * be considered worth doing; it wouldn't break expressions
6846 * that are of the form "pppoes and ..." which I suspect are the
6847 * most common expressions involving "pppoes". "pppoes or ..."
6848 * doesn't necessarily do what the user would really want, now,
6849 * as all the "or ..." tests would be done assuming PPPoE, even
6850 * though the "or" could be viewed as meaning "or, if this isn't
6851 * a PPPoE packet...".
6853 orig_linktype = off_linktype; /* save original values */
6857 * The "network-layer" protocol is PPPoE, which has a 6-byte
6858 * PPPoE header, followed by PPP payload, so we set the
6859 * offsets to the network layer offset plus 6 bytes for
6860 * the PPPoE header plus the values appropriate for PPP when
6861 * encapsulated in Ethernet (which means there's no HDLC
6864 off_linktype = orig_nl + 6;
6865 off_nl = orig_nl + 6 + 2;
6866 off_nl_nosnap = orig_nl + 6 + 2;
6869 * Set the link-layer type to PPP, as all subsequent tests will
6870 * be on the encapsulated PPP header.
6878 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
6890 bpf_error("'vpi' supported only on raw ATM");
6891 if (off_vpi == (u_int)-1)
6893 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
6899 bpf_error("'vci' supported only on raw ATM");
6900 if (off_vci == (u_int)-1)
6902 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
6907 if (off_proto == (u_int)-1)
6908 abort(); /* XXX - this isn't on FreeBSD */
6909 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
6914 if (off_payload == (u_int)-1)
6916 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
6917 0xffffffff, jtype, reverse, jvalue);
6922 bpf_error("'callref' supported only on raw ATM");
6923 if (off_proto == (u_int)-1)
6925 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
6926 jtype, reverse, jvalue);
6936 gen_atmtype_abbrev(type)
6939 struct block *b0, *b1;
6944 /* Get all packets in Meta signalling Circuit */
6946 bpf_error("'metac' supported only on raw ATM");
6947 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6948 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
6953 /* Get all packets in Broadcast Circuit*/
6955 bpf_error("'bcc' supported only on raw ATM");
6956 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6957 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
6962 /* Get all cells in Segment OAM F4 circuit*/
6964 bpf_error("'oam4sc' supported only on raw ATM");
6965 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6966 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6971 /* Get all cells in End-to-End OAM F4 Circuit*/
6973 bpf_error("'oam4ec' supported only on raw ATM");
6974 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6975 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6980 /* Get all packets in connection Signalling Circuit */
6982 bpf_error("'sc' supported only on raw ATM");
6983 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6984 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
6989 /* Get all packets in ILMI Circuit */
6991 bpf_error("'ilmic' supported only on raw ATM");
6992 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6993 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
6998 /* Get all LANE packets */
7000 bpf_error("'lane' supported only on raw ATM");
7001 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
7004 * Arrange that all subsequent tests assume LANE
7005 * rather than LLC-encapsulated packets, and set
7006 * the offsets appropriately for LANE-encapsulated
7009 * "off_mac" is the offset of the Ethernet header,
7010 * which is 2 bytes past the ATM pseudo-header
7011 * (skipping the pseudo-header and 2-byte LE Client
7012 * field). The other offsets are Ethernet offsets
7013 * relative to "off_mac".
7016 off_mac = off_payload + 2; /* MAC header */
7017 off_linktype = off_mac + 12;
7018 off_nl = off_mac + 14; /* Ethernet II */
7019 off_nl_nosnap = off_mac + 17; /* 802.3+802.2 */
7023 /* Get all LLC-encapsulated packets */
7025 bpf_error("'llc' supported only on raw ATM");
7026 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
7037 * Filtering for MTP2 messages based on li value
7038 * FISU, length is null
7039 * LSSU, length is 1 or 2
7040 * MSU, length is 3 or more
7043 gen_mtp2type_abbrev(type)
7046 struct block *b0, *b1;
7051 if ( (linktype != DLT_MTP2) &&
7052 (linktype != DLT_MTP2_WITH_PHDR) )
7053 bpf_error("'fisu' supported only on MTP2");
7054 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
7055 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
7059 if ( (linktype != DLT_MTP2) &&
7060 (linktype != DLT_MTP2_WITH_PHDR) )
7061 bpf_error("'lssu' supported only on MTP2");
7062 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
7063 b1 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
7068 if ( (linktype != DLT_MTP2) &&
7069 (linktype != DLT_MTP2_WITH_PHDR) )
7070 bpf_error("'msu' supported only on MTP2");
7071 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
7081 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
7088 bpf_u_int32 val1 , val2 , val3;
7090 switch (mtp3field) {
7093 if (off_sio == (u_int)-1)
7094 bpf_error("'sio' supported only on SS7");
7095 /* sio coded on 1 byte so max value 255 */
7097 bpf_error("sio value %u too big; max value = 255",
7099 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
7100 (u_int)jtype, reverse, (u_int)jvalue);
7104 if (off_opc == (u_int)-1)
7105 bpf_error("'opc' supported only on SS7");
7106 /* opc coded on 14 bits so max value 16383 */
7108 bpf_error("opc value %u too big; max value = 16383",
7110 /* the following instructions are made to convert jvalue
7111 * to the form used to write opc in an ss7 message*/
7112 val1 = jvalue & 0x00003c00;
7114 val2 = jvalue & 0x000003fc;
7116 val3 = jvalue & 0x00000003;
7118 jvalue = val1 + val2 + val3;
7119 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
7120 (u_int)jtype, reverse, (u_int)jvalue);
7124 if (off_dpc == (u_int)-1)
7125 bpf_error("'dpc' supported only on SS7");
7126 /* dpc coded on 14 bits so max value 16383 */
7128 bpf_error("dpc value %u too big; max value = 16383",
7130 /* the following instructions are made to convert jvalue
7131 * to the forme used to write dpc in an ss7 message*/
7132 val1 = jvalue & 0x000000ff;
7134 val2 = jvalue & 0x00003f00;
7136 jvalue = val1 + val2;
7137 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
7138 (u_int)jtype, reverse, (u_int)jvalue);
7142 if (off_sls == (u_int)-1)
7143 bpf_error("'sls' supported only on SS7");
7144 /* sls coded on 4 bits so max value 15 */
7146 bpf_error("sls value %u too big; max value = 15",
7148 /* the following instruction is made to convert jvalue
7149 * to the forme used to write sls in an ss7 message*/
7150 jvalue = jvalue << 4;
7151 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
7152 (u_int)jtype,reverse, (u_int)jvalue);
7161 static struct block *
7162 gen_msg_abbrev(type)
7168 * Q.2931 signalling protocol messages for handling virtual circuits
7169 * establishment and teardown
7174 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
7178 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
7182 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
7186 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
7190 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
7193 case A_RELEASE_DONE:
7194 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
7204 gen_atmmulti_abbrev(type)
7207 struct block *b0, *b1;
7213 bpf_error("'oam' supported only on raw ATM");
7214 b1 = gen_atmmulti_abbrev(A_OAMF4);
7219 bpf_error("'oamf4' supported only on raw ATM");
7221 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
7222 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
7224 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
7230 * Get Q.2931 signalling messages for switched
7231 * virtual connection
7234 bpf_error("'connectmsg' supported only on raw ATM");
7235 b0 = gen_msg_abbrev(A_SETUP);
7236 b1 = gen_msg_abbrev(A_CALLPROCEED);
7238 b0 = gen_msg_abbrev(A_CONNECT);
7240 b0 = gen_msg_abbrev(A_CONNECTACK);
7242 b0 = gen_msg_abbrev(A_RELEASE);
7244 b0 = gen_msg_abbrev(A_RELEASE_DONE);
7246 b0 = gen_atmtype_abbrev(A_SC);
7252 bpf_error("'metaconnect' supported only on raw ATM");
7253 b0 = gen_msg_abbrev(A_SETUP);
7254 b1 = gen_msg_abbrev(A_CALLPROCEED);
7256 b0 = gen_msg_abbrev(A_CONNECT);
7258 b0 = gen_msg_abbrev(A_RELEASE);
7260 b0 = gen_msg_abbrev(A_RELEASE_DONE);
7262 b0 = gen_atmtype_abbrev(A_METAC);