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.34 2005/09/05 09:08:04 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
72 #include "sunatmpos.h"
78 #define offsetof(s, e) ((size_t)&((s *)0)->e)
82 #include <netdb.h> /* for "struct addrinfo" */
85 #include <pcap-namedb.h>
90 #define IPPROTO_SCTP 132
93 #ifdef HAVE_OS_PROTO_H
97 #define JMP(c) ((c)|BPF_JMP|BPF_K)
100 static jmp_buf top_ctx;
101 static pcap_t *bpf_pcap;
103 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
104 static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
108 static int pcap_fddipad;
113 bpf_error(const char *fmt, ...)
118 if (bpf_pcap != NULL)
119 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
126 static void init_linktype(pcap_t *);
128 static int alloc_reg(void);
129 static void free_reg(int);
131 static struct block *root;
134 * Value passed to gen_load_a() to indicate what the offset argument
138 OR_PACKET, /* relative to the beginning of the packet */
139 OR_LINK, /* relative to the link-layer header */
140 OR_NET, /* relative to the network-layer header */
141 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
142 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
143 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
147 * We divy out chunks of memory rather than call malloc each time so
148 * we don't have to worry about leaking memory. It's probably
149 * not a big deal if all this memory was wasted but if this ever
150 * goes into a library that would probably not be a good idea.
152 * XXX - this *is* in a library....
155 #define CHUNK0SIZE 1024
161 static struct chunk chunks[NCHUNKS];
162 static int cur_chunk;
164 static void *newchunk(u_int);
165 static void freechunks(void);
166 static inline struct block *new_block(int);
167 static inline struct slist *new_stmt(int);
168 static struct block *gen_retblk(int);
169 static inline void syntax(void);
171 static void backpatch(struct block *, struct block *);
172 static void merge(struct block *, struct block *);
173 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
174 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
175 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
176 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
177 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
178 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
180 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
181 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
182 bpf_u_int32, bpf_u_int32, int, bpf_int32);
183 static struct slist *gen_load_llrel(u_int, u_int);
184 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
185 static struct slist *gen_loadx_iphdrlen(void);
186 static struct block *gen_uncond(int);
187 static inline struct block *gen_true(void);
188 static inline struct block *gen_false(void);
189 static struct block *gen_ether_linktype(int);
190 static struct block *gen_linux_sll_linktype(int);
191 static void insert_radiotap_load_llprefixlen(struct block *);
192 static void insert_load_llprefixlen(struct block *);
193 static struct slist *gen_llprefixlen(void);
194 static struct block *gen_linktype(int);
195 static struct block *gen_snap(bpf_u_int32, bpf_u_int32, u_int);
196 static struct block *gen_llc_linktype(int);
197 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
199 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
201 static struct block *gen_ahostop(const u_char *, int);
202 static struct block *gen_ehostop(const u_char *, int);
203 static struct block *gen_fhostop(const u_char *, int);
204 static struct block *gen_thostop(const u_char *, int);
205 static struct block *gen_wlanhostop(const u_char *, int);
206 static struct block *gen_ipfchostop(const u_char *, int);
207 static struct block *gen_dnhostop(bpf_u_int32, int);
208 static struct block *gen_mpls_linktype(int);
209 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int);
211 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int);
214 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
216 static struct block *gen_ipfrag(void);
217 static struct block *gen_portatom(int, bpf_int32);
218 static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
220 static struct block *gen_portatom6(int, bpf_int32);
221 static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
223 struct block *gen_portop(int, int, int);
224 static struct block *gen_port(int, int, int);
225 struct block *gen_portrangeop(int, int, int, int);
226 static struct block *gen_portrange(int, int, int, int);
228 struct block *gen_portop6(int, int, int);
229 static struct block *gen_port6(int, int, int);
230 struct block *gen_portrangeop6(int, int, int, int);
231 static struct block *gen_portrange6(int, int, int, int);
233 static int lookup_proto(const char *, int);
234 static struct block *gen_protochain(int, int, int);
235 static struct block *gen_proto(int, int, int);
236 static struct slist *xfer_to_x(struct arth *);
237 static struct slist *xfer_to_a(struct arth *);
238 static struct block *gen_mac_multicast(int);
239 static struct block *gen_len(int, int);
241 static struct block *gen_msg_abbrev(int type);
252 /* XXX Round up to nearest long. */
253 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
255 /* XXX Round up to structure boundary. */
259 cp = &chunks[cur_chunk];
260 if (n > cp->n_left) {
261 ++cp, k = ++cur_chunk;
263 bpf_error("out of memory");
264 size = CHUNK0SIZE << k;
265 cp->m = (void *)malloc(size);
267 bpf_error("out of memory");
268 memset((char *)cp->m, 0, size);
271 bpf_error("out of memory");
274 return (void *)((char *)cp->m + cp->n_left);
283 for (i = 0; i < NCHUNKS; ++i)
284 if (chunks[i].m != NULL) {
291 * A strdup whose allocations are freed after code generation is over.
295 register const char *s;
297 int n = strlen(s) + 1;
298 char *cp = newchunk(n);
304 static inline struct block *
310 p = (struct block *)newchunk(sizeof(*p));
317 static inline struct slist *
323 p = (struct slist *)newchunk(sizeof(*p));
329 static struct block *
333 struct block *b = new_block(BPF_RET|BPF_K);
342 bpf_error("syntax error in filter expression");
345 static bpf_u_int32 netmask;
350 pcap_compile(pcap_t *p, struct bpf_program *program,
351 char *buf, int optimize, bpf_u_int32 mask)
360 if (setjmp(top_ctx)) {
368 snaplen = pcap_snapshot(p);
370 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
371 "snaplen of 0 rejects all packets");
375 lex_init(buf ? buf : "");
383 root = gen_retblk(snaplen);
385 if (optimize && !no_optimize) {
388 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
389 bpf_error("expression rejects all packets");
391 program->bf_insns = icode_to_fcode(root, &len);
392 program->bf_len = len;
400 * entry point for using the compiler with no pcap open
401 * pass in all the stuff that is needed explicitly instead.
404 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
405 struct bpf_program *program,
406 char *buf, int optimize, bpf_u_int32 mask)
411 p = pcap_open_dead(linktype_arg, snaplen_arg);
414 ret = pcap_compile(p, program, buf, optimize, mask);
420 * Clean up a "struct bpf_program" by freeing all the memory allocated
424 pcap_freecode(struct bpf_program *program)
427 if (program->bf_insns != NULL) {
428 free((char *)program->bf_insns);
429 program->bf_insns = NULL;
434 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
435 * which of the jt and jf fields has been resolved and which is a pointer
436 * back to another unresolved block (or nil). At least one of the fields
437 * in each block is already resolved.
440 backpatch(list, target)
441 struct block *list, *target;
458 * Merge the lists in b0 and b1, using the 'sense' field to indicate
459 * which of jt and jf is the link.
463 struct block *b0, *b1;
465 register struct block **p = &b0;
467 /* Find end of list. */
469 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
471 /* Concatenate the lists. */
479 backpatch(p, gen_retblk(snaplen));
480 p->sense = !p->sense;
481 backpatch(p, gen_retblk(0));
485 * Insert before the statements of the first (root) block any
486 * statements needed to load the lengths of any variable-length
487 * headers into registers.
489 * XXX - a fancier strategy would be to insert those before the
490 * statements of all blocks that use those lengths and that
491 * have no predecessors that use them, so that we only compute
492 * the lengths if we need them. There might be even better
493 * approaches than that. However, as we're currently only
494 * handling variable-length radiotap headers, and as all
495 * filtering expressions other than raw link[M:N] tests
496 * require the length of that header, doing more for that
497 * header length isn't really worth the effort.
499 insert_load_llprefixlen(root);
504 struct block *b0, *b1;
506 backpatch(b0, b1->head);
507 b0->sense = !b0->sense;
508 b1->sense = !b1->sense;
510 b1->sense = !b1->sense;
516 struct block *b0, *b1;
518 b0->sense = !b0->sense;
519 backpatch(b0, b1->head);
520 b0->sense = !b0->sense;
529 b->sense = !b->sense;
532 static struct block *
533 gen_cmp(offrel, offset, size, v)
534 enum e_offrel offrel;
538 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
541 static struct block *
542 gen_cmp_gt(offrel, offset, size, v)
543 enum e_offrel offrel;
547 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
550 static struct block *
551 gen_cmp_ge(offrel, offset, size, v)
552 enum e_offrel offrel;
556 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
559 static struct block *
560 gen_cmp_lt(offrel, offset, size, v)
561 enum e_offrel offrel;
565 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
568 static struct block *
569 gen_cmp_le(offrel, offset, size, v)
570 enum e_offrel offrel;
574 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
577 static struct block *
578 gen_mcmp(offrel, offset, size, v, mask)
579 enum e_offrel offrel;
584 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
587 static struct block *
588 gen_bcmp(offrel, offset, size, v)
589 enum e_offrel offrel;
590 register u_int offset, size;
591 register const u_char *v;
593 register struct block *b, *tmp;
597 register const u_char *p = &v[size - 4];
598 bpf_int32 w = ((bpf_int32)p[0] << 24) |
599 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
601 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
608 register const u_char *p = &v[size - 2];
609 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
611 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
618 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
627 * AND the field of size "size" at offset "offset" relative to the header
628 * specified by "offrel" with "mask", and compare it with the value "v"
629 * with the test specified by "jtype"; if "reverse" is true, the test
630 * should test the opposite of "jtype".
632 static struct block *
633 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
634 enum e_offrel offrel;
636 bpf_u_int32 offset, size, mask, jtype;
639 struct slist *s, *s2;
642 s = gen_load_a(offrel, offset, size);
644 if (mask != 0xffffffff) {
645 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
650 b = new_block(JMP(jtype));
653 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
659 * Various code constructs need to know the layout of the data link
660 * layer. These variables give the necessary offsets from the beginning
661 * of the packet data.
663 * If the link layer has variable_length headers, the offsets are offsets
664 * from the end of the link-link-layer header, and "reg_ll_size" is
665 * the register number for a register containing the length of the
666 * link-layer header. Otherwise, "reg_ll_size" is -1.
668 static int reg_ll_size;
671 * This is the offset of the beginning of the link-layer header.
672 * It's usually 0, except for 802.11 with a fixed-length radio header.
677 * This is the offset of the beginning of the MAC-layer header.
678 * It's usually 0, except for ATM LANE.
680 static u_int off_mac;
683 * "off_linktype" is the offset to information in the link-layer header
684 * giving the packet type.
686 * For Ethernet, it's the offset of the Ethernet type field.
688 * For link-layer types that always use 802.2 headers, it's the
689 * offset of the LLC header.
691 * For PPP, it's the offset of the PPP type field.
693 * For Cisco HDLC, it's the offset of the CHDLC type field.
695 * For BSD loopback, it's the offset of the AF_ value.
697 * For Linux cooked sockets, it's the offset of the type field.
699 * It's set to -1 for no encapsulation, in which case, IP is assumed.
701 static u_int off_linktype;
704 * TRUE if the link layer includes an ATM pseudo-header.
706 static int is_atm = 0;
709 * TRUE if "lane" appeared in the filter; it causes us to generate
710 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
712 static int is_lane = 0;
715 * These are offsets for the ATM pseudo-header.
717 static u_int off_vpi;
718 static u_int off_vci;
719 static u_int off_proto;
722 * These are offsets for the MTP3 fields.
724 static u_int off_sio;
725 static u_int off_opc;
726 static u_int off_dpc;
727 static u_int off_sls;
730 * This is the offset of the first byte after the ATM pseudo_header,
731 * or -1 if there is no ATM pseudo-header.
733 static u_int off_payload;
736 * These are offsets to the beginning of the network-layer header.
738 * If the link layer never uses 802.2 LLC:
740 * "off_nl" and "off_nl_nosnap" are the same.
742 * If the link layer always uses 802.2 LLC:
744 * "off_nl" is the offset if there's a SNAP header following
747 * "off_nl_nosnap" is the offset if there's no SNAP header.
749 * If the link layer is Ethernet:
751 * "off_nl" is the offset if the packet is an Ethernet II packet
752 * (we assume no 802.3+802.2+SNAP);
754 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
755 * with an 802.2 header following it.
758 static u_int off_nl_nosnap;
766 linktype = pcap_datalink(p);
768 pcap_fddipad = p->fddipad;
772 * Assume it's not raw ATM with a pseudo-header, for now.
783 * And assume we're not doing SS7.
791 * Also assume it's not 802.11 with a fixed-length radio header.
797 label_stack_depth = 0;
805 off_nl = 6; /* XXX in reality, variable! */
806 off_nl_nosnap = 6; /* no 802.2 LLC */
809 case DLT_ARCNET_LINUX:
811 off_nl = 8; /* XXX in reality, variable! */
812 off_nl_nosnap = 8; /* no 802.2 LLC */
817 off_nl = 14; /* Ethernet II */
818 off_nl_nosnap = 17; /* 802.3+802.2 */
823 * SLIP doesn't have a link level type. The 16 byte
824 * header is hacked into our SLIP driver.
828 off_nl_nosnap = 16; /* no 802.2 LLC */
832 /* XXX this may be the same as the DLT_PPP_BSDOS case */
836 off_nl_nosnap = 24; /* no 802.2 LLC */
843 off_nl_nosnap = 4; /* no 802.2 LLC */
849 off_nl_nosnap = 12; /* no 802.2 LLC */
854 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
855 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
858 off_nl_nosnap = 4; /* no 802.2 LLC */
863 * This does no include the Ethernet header, and
864 * only covers session state.
868 off_nl_nosnap = 8; /* no 802.2 LLC */
874 off_nl_nosnap = 24; /* no 802.2 LLC */
879 * FDDI doesn't really have a link-level type field.
880 * We set "off_linktype" to the offset of the LLC header.
882 * To check for Ethernet types, we assume that SSAP = SNAP
883 * is being used and pick out the encapsulated Ethernet type.
884 * XXX - should we generate code to check for SNAP?
888 off_linktype += pcap_fddipad;
890 off_nl = 21; /* FDDI+802.2+SNAP */
891 off_nl_nosnap = 16; /* FDDI+802.2 */
893 off_nl += pcap_fddipad;
894 off_nl_nosnap += pcap_fddipad;
900 * Token Ring doesn't really have a link-level type field.
901 * We set "off_linktype" to the offset of the LLC header.
903 * To check for Ethernet types, we assume that SSAP = SNAP
904 * is being used and pick out the encapsulated Ethernet type.
905 * XXX - should we generate code to check for SNAP?
907 * XXX - the header is actually variable-length.
908 * Some various Linux patched versions gave 38
909 * as "off_linktype" and 40 as "off_nl"; however,
910 * if a token ring packet has *no* routing
911 * information, i.e. is not source-routed, the correct
912 * values are 20 and 22, as they are in the vanilla code.
914 * A packet is source-routed iff the uppermost bit
915 * of the first byte of the source address, at an
916 * offset of 8, has the uppermost bit set. If the
917 * packet is source-routed, the total number of bytes
918 * of routing information is 2 plus bits 0x1F00 of
919 * the 16-bit value at an offset of 14 (shifted right
920 * 8 - figure out which byte that is).
923 off_nl = 22; /* Token Ring+802.2+SNAP */
924 off_nl_nosnap = 17; /* Token Ring+802.2 */
929 * 802.11 doesn't really have a link-level type field.
930 * We set "off_linktype" to the offset of the LLC header.
932 * To check for Ethernet types, we assume that SSAP = SNAP
933 * is being used and pick out the encapsulated Ethernet type.
934 * XXX - should we generate code to check for SNAP?
936 * XXX - the header is actually variable-length. We
937 * assume a 24-byte link-layer header, as appears in
938 * data frames in networks with no bridges. If the
939 * fromds and tods 802.11 header bits are both set,
940 * it's actually supposed to be 30 bytes.
943 off_nl = 32; /* 802.11+802.2+SNAP */
944 off_nl_nosnap = 27; /* 802.11+802.2 */
947 case DLT_PRISM_HEADER:
949 * Same as 802.11, but with an additional header before
950 * the 802.11 header, containing a bunch of additional
951 * information including radio-level information.
953 * The header is 144 bytes long.
955 * XXX - same variable-length header problem; at least
956 * the Prism header is fixed-length.
959 off_linktype = 144+24;
960 off_nl = 144+32; /* Prism+802.11+802.2+SNAP */
961 off_nl_nosnap = 144+27; /* Prism+802.11+802.2 */
964 case DLT_IEEE802_11_RADIO_AVS:
966 * Same as 802.11, but with an additional header before
967 * the 802.11 header, containing a bunch of additional
968 * information including radio-level information.
970 * The header is 64 bytes long, at least in its
971 * current incarnation.
973 * XXX - same variable-length header problem, only
974 * more so; this header is also variable-length,
975 * with the length being the 32-bit big-endian
976 * number at an offset of 4 from the beginning
977 * of the radio header.
980 off_linktype = 64+24;
981 off_nl = 64+32; /* Radio+802.11+802.2+SNAP */
982 off_nl_nosnap = 64+27; /* Radio+802.11+802.2 */
985 case DLT_IEEE802_11_RADIO:
987 * Same as 802.11, but with an additional header before
988 * the 802.11 header, containing a bunch of additional
989 * information including radio-level information.
991 * The radiotap header is variable length, and we
992 * generate code to compute its length and store it
993 * in a register. These offsets are relative to the
994 * beginning of the 802.11 header.
997 off_nl = 32; /* 802.11+802.2+SNAP */
998 off_nl_nosnap = 27; /* 802.11+802.2 */
1001 case DLT_ATM_RFC1483:
1002 case DLT_ATM_CLIP: /* Linux ATM defines this */
1004 * assume routed, non-ISO PDUs
1005 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1007 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1008 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1009 * latter would presumably be treated the way PPPoE
1010 * should be, so you can do "pppoe and udp port 2049"
1011 * or "pppoa and tcp port 80" and have it check for
1012 * PPPo{A,E} and a PPP protocol of IP and....
1015 off_nl = 8; /* 802.2+SNAP */
1016 off_nl_nosnap = 3; /* 802.2 */
1021 * Full Frontal ATM; you get AALn PDUs with an ATM
1025 off_vpi = SUNATM_VPI_POS;
1026 off_vci = SUNATM_VCI_POS;
1027 off_proto = PROTO_POS;
1028 off_mac = -1; /* LLC-encapsulated, so no MAC-layer header */
1029 off_payload = SUNATM_PKT_BEGIN_POS;
1030 off_linktype = off_payload;
1031 off_nl = off_payload+8; /* 802.2+SNAP */
1032 off_nl_nosnap = off_payload+3; /* 802.2 */
1038 off_nl_nosnap = 0; /* no 802.2 LLC */
1041 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1044 off_nl_nosnap = 16; /* no 802.2 LLC */
1049 * LocalTalk does have a 1-byte type field in the LLAP header,
1050 * but really it just indicates whether there is a "short" or
1051 * "long" DDP packet following.
1055 off_nl_nosnap = 0; /* no 802.2 LLC */
1058 case DLT_IP_OVER_FC:
1060 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1061 * link-level type field. We set "off_linktype" to the
1062 * offset of the LLC header.
1064 * To check for Ethernet types, we assume that SSAP = SNAP
1065 * is being used and pick out the encapsulated Ethernet type.
1066 * XXX - should we generate code to check for SNAP? RFC
1067 * 2625 says SNAP should be used.
1070 off_nl = 24; /* IPFC+802.2+SNAP */
1071 off_nl_nosnap = 19; /* IPFC+802.2 */
1076 * XXX - we should set this to handle SNAP-encapsulated
1077 * frames (NLPID of 0x80).
1081 off_nl_nosnap = 0; /* no 802.2 LLC */
1084 case DLT_APPLE_IP_OVER_IEEE1394:
1087 off_nl_nosnap = 18; /* no 802.2 LLC */
1090 case DLT_LINUX_IRDA:
1092 * Currently, only raw "link[N:M]" filtering is supported.
1101 * Currently, only raw "link[N:M]" filtering is supported.
1108 case DLT_SYMANTEC_FIREWALL:
1110 off_nl = 44; /* Ethernet II */
1111 off_nl_nosnap = 44; /* XXX - what does it do with 802.3 packets? */
1116 /* XXX read this from pf.h? */
1117 off_nl = PFLOG_HDRLEN;
1118 off_nl_nosnap = PFLOG_HDRLEN; /* no 802.2 LLC */
1121 case DLT_JUNIPER_MFR:
1122 case DLT_JUNIPER_MLFR:
1123 case DLT_JUNIPER_MLPPP:
1124 case DLT_JUNIPER_PPP:
1125 case DLT_JUNIPER_CHDLC:
1126 case DLT_JUNIPER_FRELAY:
1129 off_nl_nosnap = -1; /* no 802.2 LLC */
1132 case DLT_JUNIPER_ATM1:
1133 off_linktype = 4; /* in reality variable between 4-8 */
1138 case DLT_JUNIPER_ATM2:
1139 off_linktype = 8; /* in reality variable between 8-12 */
1144 /* frames captured on a Juniper PPPoE service PIC
1145 * contain raw ethernet frames */
1146 case DLT_JUNIPER_PPPOE:
1147 case DLT_JUNIPER_ETHER:
1149 off_nl = 18; /* Ethernet II */
1150 off_nl_nosnap = 21; /* 802.3+802.2 */
1153 case DLT_JUNIPER_PPPOE_ATM:
1156 off_nl_nosnap = -1; /* no 802.2 LLC */
1159 case DLT_JUNIPER_GGSN:
1162 off_nl_nosnap = -1; /* no 802.2 LLC */
1165 case DLT_JUNIPER_ES:
1167 off_nl = -1; /* not really a network layer but raw IP adresses */
1168 off_nl_nosnap = -1; /* no 802.2 LLC */
1171 case DLT_JUNIPER_MONITOR:
1173 off_nl = 12; /* raw IP/IP6 header */
1174 off_nl_nosnap = -1; /* no 802.2 LLC */
1177 case DLT_JUNIPER_SERVICES:
1179 off_nl = -1; /* L3 proto location dep. on cookie type */
1180 off_nl_nosnap = -1; /* no 802.2 LLC */
1201 case DLT_LINUX_LAPD:
1203 * Currently, only raw "link[N:M]" filtering is supported.
1210 bpf_error("unknown data link type %d", linktype);
1215 * Load a value relative to the beginning of the link-layer header.
1216 * The link-layer header doesn't necessarily begin at the beginning
1217 * of the packet data; there might be a variable-length prefix containing
1218 * radio information.
1220 static struct slist *
1221 gen_load_llrel(offset, size)
1224 struct slist *s, *s2;
1226 s = gen_llprefixlen();
1229 * If "s" is non-null, it has code to arrange that the X register
1230 * contains the length of the prefix preceding the link-layer
1234 s2 = new_stmt(BPF_LD|BPF_IND|size);
1238 s = new_stmt(BPF_LD|BPF_ABS|size);
1245 * Load a value relative to the beginning of the specified header.
1247 static struct slist *
1248 gen_load_a(offrel, offset, size)
1249 enum e_offrel offrel;
1252 struct slist *s, *s2;
1257 s = gen_load_llrel(offset, size);
1261 s = gen_load_llrel(off_ll + offset, size);
1265 s = gen_load_llrel(off_nl + offset, size);
1269 s = gen_load_llrel(off_nl_nosnap + offset, size);
1274 * Load the X register with the length of the IPv4 header,
1277 s = gen_loadx_iphdrlen();
1280 * Load the item at {length of the link-layer header} +
1281 * {length of the IPv4 header} + {specified offset}.
1283 s2 = new_stmt(BPF_LD|BPF_IND|size);
1284 s2->s.k = off_nl + offset;
1289 s = gen_load_llrel(off_nl + 40 + offset, size);
1300 * Generate code to load into the X register the sum of the length of
1301 * the IPv4 header and any variable-length header preceding the link-layer
1304 static struct slist *
1305 gen_loadx_iphdrlen()
1307 struct slist *s, *s2;
1309 s = gen_llprefixlen();
1312 * There's a variable-length prefix preceding the
1313 * link-layer header. "s" points to a list of statements
1314 * that put the length of that prefix into the X register.
1315 * The 4*([k]&0xf) addressing mode can't be used, as we
1316 * don't have a constant offset, so we have to load the
1317 * value in question into the A register and add to it
1318 * the value from the X register.
1320 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1323 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1326 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1331 * The A register now contains the length of the
1332 * IP header. We need to add to it the length
1333 * of the prefix preceding the link-layer
1334 * header, which is still in the X register, and
1335 * move the result into the X register.
1337 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1338 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1341 * There is no variable-length header preceding the
1342 * link-layer header; if there's a fixed-length
1343 * header preceding it, its length is included in
1344 * the off_ variables, so it doesn't need to be added.
1346 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1352 static struct block *
1359 s = new_stmt(BPF_LD|BPF_IMM);
1361 b = new_block(JMP(BPF_JEQ));
1367 static inline struct block *
1370 return gen_uncond(1);
1373 static inline struct block *
1376 return gen_uncond(0);
1380 * Byte-swap a 32-bit number.
1381 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1382 * big-endian platforms.)
1384 #define SWAPLONG(y) \
1385 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1388 * Generate code to match a particular packet type.
1390 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1391 * value, if <= ETHERMTU. We use that to determine whether to
1392 * match the type/length field or to check the type/length field for
1393 * a value <= ETHERMTU to see whether it's a type field and then do
1394 * the appropriate test.
1396 static struct block *
1397 gen_ether_linktype(proto)
1400 struct block *b0, *b1;
1406 case LLCSAP_NETBEUI:
1408 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1409 * so we check the DSAP and SSAP.
1411 * LLCSAP_IP checks for IP-over-802.2, rather
1412 * than IP-over-Ethernet or IP-over-SNAP.
1414 * XXX - should we check both the DSAP and the
1415 * SSAP, like this, or should we check just the
1416 * DSAP, as we do for other types <= ETHERMTU
1417 * (i.e., other SAP values)?
1419 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1421 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1422 ((proto << 8) | proto));
1430 * Ethernet_II frames, which are Ethernet
1431 * frames with a frame type of ETHERTYPE_IPX;
1433 * Ethernet_802.3 frames, which are 802.3
1434 * frames (i.e., the type/length field is
1435 * a length field, <= ETHERMTU, rather than
1436 * a type field) with the first two bytes
1437 * after the Ethernet/802.3 header being
1440 * Ethernet_802.2 frames, which are 802.3
1441 * frames with an 802.2 LLC header and
1442 * with the IPX LSAP as the DSAP in the LLC
1445 * Ethernet_SNAP frames, which are 802.3
1446 * frames with an LLC header and a SNAP
1447 * header and with an OUI of 0x000000
1448 * (encapsulated Ethernet) and a protocol
1449 * ID of ETHERTYPE_IPX in the SNAP header.
1451 * XXX - should we generate the same code both
1452 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1456 * This generates code to check both for the
1457 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1459 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1460 (bpf_int32)LLCSAP_IPX);
1461 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H,
1466 * Now we add code to check for SNAP frames with
1467 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1469 b0 = gen_snap(0x000000, ETHERTYPE_IPX, 14);
1473 * Now we generate code to check for 802.3
1474 * frames in general.
1476 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1480 * Now add the check for 802.3 frames before the
1481 * check for Ethernet_802.2 and Ethernet_802.3,
1482 * as those checks should only be done on 802.3
1483 * frames, not on Ethernet frames.
1488 * Now add the check for Ethernet_II frames, and
1489 * do that before checking for the other frame
1492 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1493 (bpf_int32)ETHERTYPE_IPX);
1497 case ETHERTYPE_ATALK:
1498 case ETHERTYPE_AARP:
1500 * EtherTalk (AppleTalk protocols on Ethernet link
1501 * layer) may use 802.2 encapsulation.
1505 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1506 * we check for an Ethernet type field less than
1507 * 1500, which means it's an 802.3 length field.
1509 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1513 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1514 * SNAP packets with an organization code of
1515 * 0x080007 (Apple, for Appletalk) and a protocol
1516 * type of ETHERTYPE_ATALK (Appletalk).
1518 * 802.2-encapsulated ETHERTYPE_AARP packets are
1519 * SNAP packets with an organization code of
1520 * 0x000000 (encapsulated Ethernet) and a protocol
1521 * type of ETHERTYPE_AARP (Appletalk ARP).
1523 if (proto == ETHERTYPE_ATALK)
1524 b1 = gen_snap(0x080007, ETHERTYPE_ATALK, 14);
1525 else /* proto == ETHERTYPE_AARP */
1526 b1 = gen_snap(0x000000, ETHERTYPE_AARP, 14);
1530 * Check for Ethernet encapsulation (Ethertalk
1531 * phase 1?); we just check for the Ethernet
1534 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1540 if (proto <= ETHERMTU) {
1542 * This is an LLC SAP value, so the frames
1543 * that match would be 802.2 frames.
1544 * Check that the frame is an 802.2 frame
1545 * (i.e., that the length/type field is
1546 * a length field, <= ETHERMTU) and
1547 * then check the DSAP.
1549 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1551 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1557 * This is an Ethernet type, so compare
1558 * the length/type field with it (if
1559 * the frame is an 802.2 frame, the length
1560 * field will be <= ETHERMTU, and, as
1561 * "proto" is > ETHERMTU, this test
1562 * will fail and the frame won't match,
1563 * which is what we want).
1565 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1572 * Generate code to match a particular packet type.
1574 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1575 * value, if <= ETHERMTU. We use that to determine whether to
1576 * match the type field or to check the type field for the special
1577 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1579 static struct block *
1580 gen_linux_sll_linktype(proto)
1583 struct block *b0, *b1;
1589 case LLCSAP_NETBEUI:
1591 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1592 * so we check the DSAP and SSAP.
1594 * LLCSAP_IP checks for IP-over-802.2, rather
1595 * than IP-over-Ethernet or IP-over-SNAP.
1597 * XXX - should we check both the DSAP and the
1598 * SSAP, like this, or should we check just the
1599 * DSAP, as we do for other types <= ETHERMTU
1600 * (i.e., other SAP values)?
1602 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1603 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1604 ((proto << 8) | proto));
1610 * Ethernet_II frames, which are Ethernet
1611 * frames with a frame type of ETHERTYPE_IPX;
1613 * Ethernet_802.3 frames, which have a frame
1614 * type of LINUX_SLL_P_802_3;
1616 * Ethernet_802.2 frames, which are 802.3
1617 * frames with an 802.2 LLC header (i.e, have
1618 * a frame type of LINUX_SLL_P_802_2) and
1619 * with the IPX LSAP as the DSAP in the LLC
1622 * Ethernet_SNAP frames, which are 802.3
1623 * frames with an LLC header and a SNAP
1624 * header and with an OUI of 0x000000
1625 * (encapsulated Ethernet) and a protocol
1626 * ID of ETHERTYPE_IPX in the SNAP header.
1628 * First, do the checks on LINUX_SLL_P_802_2
1629 * frames; generate the check for either
1630 * Ethernet_802.2 or Ethernet_SNAP frames, and
1631 * then put a check for LINUX_SLL_P_802_2 frames
1634 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1635 (bpf_int32)LLCSAP_IPX);
1636 b1 = gen_snap(0x000000, ETHERTYPE_IPX,
1639 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1643 * Now check for 802.3 frames and OR that with
1644 * the previous test.
1646 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1650 * Now add the check for Ethernet_II frames, and
1651 * do that before checking for the other frame
1654 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1655 (bpf_int32)ETHERTYPE_IPX);
1659 case ETHERTYPE_ATALK:
1660 case ETHERTYPE_AARP:
1662 * EtherTalk (AppleTalk protocols on Ethernet link
1663 * layer) may use 802.2 encapsulation.
1667 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1668 * we check for the 802.2 protocol type in the
1669 * "Ethernet type" field.
1671 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1674 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1675 * SNAP packets with an organization code of
1676 * 0x080007 (Apple, for Appletalk) and a protocol
1677 * type of ETHERTYPE_ATALK (Appletalk).
1679 * 802.2-encapsulated ETHERTYPE_AARP packets are
1680 * SNAP packets with an organization code of
1681 * 0x000000 (encapsulated Ethernet) and a protocol
1682 * type of ETHERTYPE_AARP (Appletalk ARP).
1684 if (proto == ETHERTYPE_ATALK)
1685 b1 = gen_snap(0x080007, ETHERTYPE_ATALK,
1687 else /* proto == ETHERTYPE_AARP */
1688 b1 = gen_snap(0x000000, ETHERTYPE_AARP,
1693 * Check for Ethernet encapsulation (Ethertalk
1694 * phase 1?); we just check for the Ethernet
1697 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1703 if (proto <= ETHERMTU) {
1705 * This is an LLC SAP value, so the frames
1706 * that match would be 802.2 frames.
1707 * Check for the 802.2 protocol type
1708 * in the "Ethernet type" field, and
1709 * then check the DSAP.
1711 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1713 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1719 * This is an Ethernet type, so compare
1720 * the length/type field with it (if
1721 * the frame is an 802.2 frame, the length
1722 * field will be <= ETHERMTU, and, as
1723 * "proto" is > ETHERMTU, this test
1724 * will fail and the frame won't match,
1725 * which is what we want).
1727 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1734 insert_radiotap_load_llprefixlen(b)
1737 struct slist *s1, *s2;
1740 * Prepend to the statements in this block code to load the
1741 * length of the radiotap header into the register assigned
1742 * to hold that length, if one has been assigned.
1744 if (reg_ll_size != -1) {
1746 * The 2 bytes at offsets of 2 and 3 from the beginning
1747 * of the radiotap header are the length of the radiotap
1748 * header; unfortunately, it's little-endian, so we have
1749 * to load it a byte at a time and construct the value.
1753 * Load the high-order byte, at an offset of 3, shift it
1754 * left a byte, and put the result in the X register.
1756 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1758 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1761 s2 = new_stmt(BPF_MISC|BPF_TAX);
1765 * Load the next byte, at an offset of 2, and OR the
1766 * value from the X register into it.
1768 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1771 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1775 * Now allocate a register to hold that value and store
1778 s2 = new_stmt(BPF_ST);
1779 s2->s.k = reg_ll_size;
1783 * Now move it into the X register.
1785 s2 = new_stmt(BPF_MISC|BPF_TAX);
1789 * Now append all the existing statements in this
1790 * block to these statements.
1792 sappend(s1, b->stmts);
1799 insert_load_llprefixlen(b)
1804 case DLT_IEEE802_11_RADIO:
1805 insert_radiotap_load_llprefixlen(b);
1810 static struct slist *
1811 gen_radiotap_llprefixlen(void)
1815 if (reg_ll_size == -1) {
1817 * We haven't yet assigned a register for the length
1818 * of the radiotap header; allocate one.
1820 reg_ll_size = alloc_reg();
1824 * Load the register containing the radiotap length
1825 * into the X register.
1827 s = new_stmt(BPF_LDX|BPF_MEM);
1828 s->s.k = reg_ll_size;
1833 * Generate code to compute the link-layer header length, if necessary,
1834 * putting it into the X register, and to return either a pointer to a
1835 * "struct slist" for the list of statements in that code, or NULL if
1836 * no code is necessary.
1838 static struct slist *
1839 gen_llprefixlen(void)
1843 case DLT_IEEE802_11_RADIO:
1844 return gen_radiotap_llprefixlen();
1852 * Generate code to match a particular packet type by matching the
1853 * link-layer type field or fields in the 802.2 LLC header.
1855 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1856 * value, if <= ETHERMTU.
1858 static struct block *
1862 struct block *b0, *b1, *b2;
1864 /* are we checking MPLS-encapsulated packets? */
1865 if (label_stack_depth > 0) {
1869 /* FIXME add other L3 proto IDs */
1870 return gen_mpls_linktype(Q_IP);
1872 case ETHERTYPE_IPV6:
1874 /* FIXME add other L3 proto IDs */
1875 return gen_mpls_linktype(Q_IPV6);
1878 bpf_error("unsupported protocol over mpls");
1886 return gen_ether_linktype(proto);
1894 proto = (proto << 8 | LLCSAP_ISONS);
1898 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1907 case DLT_IEEE802_11:
1908 case DLT_IEEE802_11_RADIO_AVS:
1909 case DLT_IEEE802_11_RADIO:
1910 case DLT_PRISM_HEADER:
1911 case DLT_ATM_RFC1483:
1913 case DLT_IP_OVER_FC:
1914 return gen_llc_linktype(proto);
1920 * If "is_lane" is set, check for a LANE-encapsulated
1921 * version of this protocol, otherwise check for an
1922 * LLC-encapsulated version of this protocol.
1924 * We assume LANE means Ethernet, not Token Ring.
1928 * Check that the packet doesn't begin with an
1929 * LE Control marker. (We've already generated
1932 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
1937 * Now generate an Ethernet test.
1939 b1 = gen_ether_linktype(proto);
1944 * Check for LLC encapsulation and then check the
1947 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
1948 b1 = gen_llc_linktype(proto);
1956 return gen_linux_sll_linktype(proto);
1961 case DLT_SLIP_BSDOS:
1964 * These types don't provide any type field; packets
1967 * XXX - for IPv4, check for a version number of 4, and,
1968 * for IPv6, check for a version number of 6?
1974 case ETHERTYPE_IPV6:
1976 return gen_true(); /* always true */
1979 return gen_false(); /* always false */
1986 case DLT_PPP_SERIAL:
1989 * We use Ethernet protocol types inside libpcap;
1990 * map them to the corresponding PPP protocol types.
1999 case ETHERTYPE_IPV6:
2008 case ETHERTYPE_ATALK:
2022 * I'm assuming the "Bridging PDU"s that go
2023 * over PPP are Spanning Tree Protocol
2037 * We use Ethernet protocol types inside libpcap;
2038 * map them to the corresponding PPP protocol types.
2043 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
2044 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
2046 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
2051 case ETHERTYPE_IPV6:
2061 case ETHERTYPE_ATALK:
2075 * I'm assuming the "Bridging PDU"s that go
2076 * over PPP are Spanning Tree Protocol
2092 * For DLT_NULL, the link-layer header is a 32-bit
2093 * word containing an AF_ value in *host* byte order,
2094 * and for DLT_ENC, the link-layer header begins
2095 * with a 32-bit work containing an AF_ value in
2098 * In addition, if we're reading a saved capture file,
2099 * the host byte order in the capture may not be the
2100 * same as the host byte order on this machine.
2102 * For DLT_LOOP, the link-layer header is a 32-bit
2103 * word containing an AF_ value in *network* byte order.
2105 * XXX - AF_ values may, unfortunately, be platform-
2106 * dependent; for example, FreeBSD's AF_INET6 is 24
2107 * whilst NetBSD's and OpenBSD's is 26.
2109 * This means that, when reading a capture file, just
2110 * checking for our AF_INET6 value won't work if the
2111 * capture file came from another OS.
2120 case ETHERTYPE_IPV6:
2127 * Not a type on which we support filtering.
2128 * XXX - support those that have AF_ values
2129 * #defined on this platform, at least?
2134 if (linktype == DLT_NULL || linktype == DLT_ENC) {
2136 * The AF_ value is in host byte order, but
2137 * the BPF interpreter will convert it to
2138 * network byte order.
2140 * If this is a save file, and it's from a
2141 * machine with the opposite byte order to
2142 * ours, we byte-swap the AF_ value.
2144 * Then we run it through "htonl()", and
2145 * generate code to compare against the result.
2147 if (bpf_pcap->sf.rfile != NULL &&
2148 bpf_pcap->sf.swapped)
2149 proto = SWAPLONG(proto);
2150 proto = htonl(proto);
2152 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
2156 * af field is host byte order in contrast to the rest of
2159 if (proto == ETHERTYPE_IP)
2160 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2161 BPF_B, (bpf_int32)AF_INET));
2163 else if (proto == ETHERTYPE_IPV6)
2164 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2165 BPF_B, (bpf_int32)AF_INET6));
2173 case DLT_ARCNET_LINUX:
2175 * XXX should we check for first fragment if the protocol
2184 case ETHERTYPE_IPV6:
2185 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2186 (bpf_int32)ARCTYPE_INET6));
2190 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2191 (bpf_int32)ARCTYPE_IP);
2192 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2193 (bpf_int32)ARCTYPE_IP_OLD);
2198 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2199 (bpf_int32)ARCTYPE_ARP);
2200 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2201 (bpf_int32)ARCTYPE_ARP_OLD);
2205 case ETHERTYPE_REVARP:
2206 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2207 (bpf_int32)ARCTYPE_REVARP));
2209 case ETHERTYPE_ATALK:
2210 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2211 (bpf_int32)ARCTYPE_ATALK));
2218 case ETHERTYPE_ATALK:
2228 * XXX - assumes a 2-byte Frame Relay header with
2229 * DLCI and flags. What if the address is longer?
2235 * Check for the special NLPID for IP.
2237 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
2240 case ETHERTYPE_IPV6:
2242 * Check for the special NLPID for IPv6.
2244 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
2249 * Check for several OSI protocols.
2251 * Frame Relay packets typically have an OSI
2252 * NLPID at the beginning; we check for each
2255 * What we check for is the NLPID and a frame
2256 * control field of UI, i.e. 0x03 followed
2259 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
2260 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
2261 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
2272 case DLT_JUNIPER_MFR:
2273 case DLT_JUNIPER_MLFR:
2274 case DLT_JUNIPER_MLPPP:
2275 case DLT_JUNIPER_ATM1:
2276 case DLT_JUNIPER_ATM2:
2277 case DLT_JUNIPER_PPPOE:
2278 case DLT_JUNIPER_PPPOE_ATM:
2279 case DLT_JUNIPER_GGSN:
2280 case DLT_JUNIPER_ES:
2281 case DLT_JUNIPER_MONITOR:
2282 case DLT_JUNIPER_SERVICES:
2283 case DLT_JUNIPER_ETHER:
2284 case DLT_JUNIPER_PPP:
2285 case DLT_JUNIPER_FRELAY:
2286 case DLT_JUNIPER_CHDLC:
2287 /* just lets verify the magic number for now -
2288 * on ATM we may have up to 6 different encapsulations on the wire
2289 * and need a lot of heuristics to figure out that the payload
2292 * FIXME encapsulation specific BPF_ filters
2294 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
2296 case DLT_LINUX_IRDA:
2297 bpf_error("IrDA link-layer type filtering not implemented");
2300 bpf_error("DOCSIS link-layer type filtering not implemented");
2302 case DLT_LINUX_LAPD:
2303 bpf_error("LAPD link-layer type filtering not implemented");
2307 * All the types that have no encapsulation should either be
2308 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2309 * all packets are IP packets, or should be handled in some
2310 * special case, if none of them are (if some are and some
2311 * aren't, the lack of encapsulation is a problem, as we'd
2312 * have to find some other way of determining the packet type).
2314 * Therefore, if "off_linktype" is -1, there's an error.
2316 if (off_linktype == (u_int)-1)
2320 * Any type not handled above should always have an Ethernet
2321 * type at an offset of "off_linktype". (PPP is partially
2322 * handled above - the protocol type is mapped from the
2323 * Ethernet and LLC types we use internally to the corresponding
2324 * PPP type - but the PPP type is always specified by a value
2325 * at "off_linktype", so we don't have to do the code generation
2328 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2332 * Check for an LLC SNAP packet with a given organization code and
2333 * protocol type; we check the entire contents of the 802.2 LLC and
2334 * snap headers, checking for DSAP and SSAP of SNAP and a control
2335 * field of 0x03 in the LLC header, and for the specified organization
2336 * code and protocol type in the SNAP header.
2338 static struct block *
2339 gen_snap(orgcode, ptype, offset)
2340 bpf_u_int32 orgcode;
2344 u_char snapblock[8];
2346 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
2347 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
2348 snapblock[2] = 0x03; /* control = UI */
2349 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
2350 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
2351 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
2352 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
2353 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
2354 return gen_bcmp(OR_LINK, offset, 8, snapblock);
2358 * Generate code to match a particular packet type, for link-layer types
2359 * using 802.2 LLC headers.
2361 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2362 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2364 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2365 * value, if <= ETHERMTU. We use that to determine whether to
2366 * match the DSAP or both DSAP and LSAP or to check the OUI and
2367 * protocol ID in a SNAP header.
2369 static struct block *
2370 gen_llc_linktype(proto)
2374 * XXX - handle token-ring variable-length header.
2380 case LLCSAP_NETBEUI:
2382 * XXX - should we check both the DSAP and the
2383 * SSAP, like this, or should we check just the
2384 * DSAP, as we do for other types <= ETHERMTU
2385 * (i.e., other SAP values)?
2387 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_u_int32)
2388 ((proto << 8) | proto));
2392 * XXX - are there ever SNAP frames for IPX on
2393 * non-Ethernet 802.x networks?
2395 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2396 (bpf_int32)LLCSAP_IPX);
2398 case ETHERTYPE_ATALK:
2400 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2401 * SNAP packets with an organization code of
2402 * 0x080007 (Apple, for Appletalk) and a protocol
2403 * type of ETHERTYPE_ATALK (Appletalk).
2405 * XXX - check for an organization code of
2406 * encapsulated Ethernet as well?
2408 return gen_snap(0x080007, ETHERTYPE_ATALK, off_linktype);
2412 * XXX - we don't have to check for IPX 802.3
2413 * here, but should we check for the IPX Ethertype?
2415 if (proto <= ETHERMTU) {
2417 * This is an LLC SAP value, so check
2420 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2424 * This is an Ethernet type; we assume that it's
2425 * unlikely that it'll appear in the right place
2426 * at random, and therefore check only the
2427 * location that would hold the Ethernet type
2428 * in a SNAP frame with an organization code of
2429 * 0x000000 (encapsulated Ethernet).
2431 * XXX - if we were to check for the SNAP DSAP and
2432 * LSAP, as per XXX, and were also to check for an
2433 * organization code of 0x000000 (encapsulated
2434 * Ethernet), we'd do
2436 * return gen_snap(0x000000, proto,
2439 * here; for now, we don't, as per the above.
2440 * I don't know whether it's worth the extra CPU
2441 * time to do the right check or not.
2443 return gen_cmp(OR_LINK, off_linktype+6, BPF_H,
2449 static struct block *
2450 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
2454 u_int src_off, dst_off;
2456 struct block *b0, *b1;
2470 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2471 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2477 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2478 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2485 b0 = gen_linktype(proto);
2486 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
2492 static struct block *
2493 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
2494 struct in6_addr *addr;
2495 struct in6_addr *mask;
2497 u_int src_off, dst_off;
2499 struct block *b0, *b1;
2514 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2515 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2521 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2522 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2529 /* this order is important */
2530 a = (u_int32_t *)addr;
2531 m = (u_int32_t *)mask;
2532 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
2533 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
2535 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
2537 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
2539 b0 = gen_linktype(proto);
2545 static struct block *
2546 gen_ehostop(eaddr, dir)
2547 register const u_char *eaddr;
2550 register struct block *b0, *b1;
2554 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
2557 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
2560 b0 = gen_ehostop(eaddr, Q_SRC);
2561 b1 = gen_ehostop(eaddr, Q_DST);
2567 b0 = gen_ehostop(eaddr, Q_SRC);
2568 b1 = gen_ehostop(eaddr, Q_DST);
2577 * Like gen_ehostop, but for DLT_FDDI
2579 static struct block *
2580 gen_fhostop(eaddr, dir)
2581 register const u_char *eaddr;
2584 struct block *b0, *b1;
2589 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
2591 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
2596 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
2598 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
2602 b0 = gen_fhostop(eaddr, Q_SRC);
2603 b1 = gen_fhostop(eaddr, Q_DST);
2609 b0 = gen_fhostop(eaddr, Q_SRC);
2610 b1 = gen_fhostop(eaddr, Q_DST);
2619 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2621 static struct block *
2622 gen_thostop(eaddr, dir)
2623 register const u_char *eaddr;
2626 register struct block *b0, *b1;
2630 return gen_bcmp(OR_LINK, 8, 6, eaddr);
2633 return gen_bcmp(OR_LINK, 2, 6, eaddr);
2636 b0 = gen_thostop(eaddr, Q_SRC);
2637 b1 = gen_thostop(eaddr, Q_DST);
2643 b0 = gen_thostop(eaddr, Q_SRC);
2644 b1 = gen_thostop(eaddr, Q_DST);
2653 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2655 static struct block *
2656 gen_wlanhostop(eaddr, dir)
2657 register const u_char *eaddr;
2660 register struct block *b0, *b1, *b2;
2661 register struct slist *s;
2668 * For control frames, there is no SA.
2670 * For management frames, SA is at an
2671 * offset of 10 from the beginning of
2674 * For data frames, SA is at an offset
2675 * of 10 from the beginning of the packet
2676 * if From DS is clear, at an offset of
2677 * 16 from the beginning of the packet
2678 * if From DS is set and To DS is clear,
2679 * and an offset of 24 from the beginning
2680 * of the packet if From DS is set and To DS
2685 * Generate the tests to be done for data frames
2688 * First, check for To DS set, i.e. check "link[1] & 0x01".
2690 s = gen_load_a(OR_LINK, 1, BPF_B);
2691 b1 = new_block(JMP(BPF_JSET));
2692 b1->s.k = 0x01; /* To DS */
2696 * If To DS is set, the SA is at 24.
2698 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
2702 * Now, check for To DS not set, i.e. check
2703 * "!(link[1] & 0x01)".
2705 s = gen_load_a(OR_LINK, 1, BPF_B);
2706 b2 = new_block(JMP(BPF_JSET));
2707 b2->s.k = 0x01; /* To DS */
2712 * If To DS is not set, the SA is at 16.
2714 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
2718 * Now OR together the last two checks. That gives
2719 * the complete set of checks for data frames with
2725 * Now check for From DS being set, and AND that with
2726 * the ORed-together checks.
2728 s = gen_load_a(OR_LINK, 1, BPF_B);
2729 b1 = new_block(JMP(BPF_JSET));
2730 b1->s.k = 0x02; /* From DS */
2735 * Now check for data frames with From DS not set.
2737 s = gen_load_a(OR_LINK, 1, BPF_B);
2738 b2 = new_block(JMP(BPF_JSET));
2739 b2->s.k = 0x02; /* From DS */
2744 * If From DS isn't set, the SA is at 10.
2746 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
2750 * Now OR together the checks for data frames with
2751 * From DS not set and for data frames with From DS
2752 * set; that gives the checks done for data frames.
2757 * Now check for a data frame.
2758 * I.e, check "link[0] & 0x08".
2760 gen_load_a(OR_LINK, 0, BPF_B);
2761 b1 = new_block(JMP(BPF_JSET));
2766 * AND that with the checks done for data frames.
2771 * If the high-order bit of the type value is 0, this
2772 * is a management frame.
2773 * I.e, check "!(link[0] & 0x08)".
2775 s = gen_load_a(OR_LINK, 0, BPF_B);
2776 b2 = new_block(JMP(BPF_JSET));
2782 * For management frames, the SA is at 10.
2784 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
2788 * OR that with the checks done for data frames.
2789 * That gives the checks done for management and
2795 * If the low-order bit of the type value is 1,
2796 * this is either a control frame or a frame
2797 * with a reserved type, and thus not a
2800 * I.e., check "!(link[0] & 0x04)".
2802 s = gen_load_a(OR_LINK, 0, BPF_B);
2803 b1 = new_block(JMP(BPF_JSET));
2809 * AND that with the checks for data and management
2819 * For control frames, there is no DA.
2821 * For management frames, DA is at an
2822 * offset of 4 from the beginning of
2825 * For data frames, DA is at an offset
2826 * of 4 from the beginning of the packet
2827 * if To DS is clear and at an offset of
2828 * 16 from the beginning of the packet
2833 * Generate the tests to be done for data frames.
2835 * First, check for To DS set, i.e. "link[1] & 0x01".
2837 s = gen_load_a(OR_LINK, 1, BPF_B);
2838 b1 = new_block(JMP(BPF_JSET));
2839 b1->s.k = 0x01; /* To DS */
2843 * If To DS is set, the DA is at 16.
2845 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
2849 * Now, check for To DS not set, i.e. check
2850 * "!(link[1] & 0x01)".
2852 s = gen_load_a(OR_LINK, 1, BPF_B);
2853 b2 = new_block(JMP(BPF_JSET));
2854 b2->s.k = 0x01; /* To DS */
2859 * If To DS is not set, the DA is at 4.
2861 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
2865 * Now OR together the last two checks. That gives
2866 * the complete set of checks for data frames.
2871 * Now check for a data frame.
2872 * I.e, check "link[0] & 0x08".
2874 s = gen_load_a(OR_LINK, 0, BPF_B);
2875 b1 = new_block(JMP(BPF_JSET));
2880 * AND that with the checks done for data frames.
2885 * If the high-order bit of the type value is 0, this
2886 * is a management frame.
2887 * I.e, check "!(link[0] & 0x08)".
2889 s = gen_load_a(OR_LINK, 0, BPF_B);
2890 b2 = new_block(JMP(BPF_JSET));
2896 * For management frames, the DA is at 4.
2898 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
2902 * OR that with the checks done for data frames.
2903 * That gives the checks done for management and
2909 * If the low-order bit of the type value is 1,
2910 * this is either a control frame or a frame
2911 * with a reserved type, and thus not a
2914 * I.e., check "!(link[0] & 0x04)".
2916 s = gen_load_a(OR_LINK, 0, BPF_B);
2917 b1 = new_block(JMP(BPF_JSET));
2923 * AND that with the checks for data and management
2930 b0 = gen_wlanhostop(eaddr, Q_SRC);
2931 b1 = gen_wlanhostop(eaddr, Q_DST);
2937 b0 = gen_wlanhostop(eaddr, Q_SRC);
2938 b1 = gen_wlanhostop(eaddr, Q_DST);
2947 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2948 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2949 * as the RFC states.)
2951 static struct block *
2952 gen_ipfchostop(eaddr, dir)
2953 register const u_char *eaddr;
2956 register struct block *b0, *b1;
2960 return gen_bcmp(OR_LINK, 10, 6, eaddr);
2963 return gen_bcmp(OR_LINK, 2, 6, eaddr);
2966 b0 = gen_ipfchostop(eaddr, Q_SRC);
2967 b1 = gen_ipfchostop(eaddr, Q_DST);
2973 b0 = gen_ipfchostop(eaddr, Q_SRC);
2974 b1 = gen_ipfchostop(eaddr, Q_DST);
2983 * This is quite tricky because there may be pad bytes in front of the
2984 * DECNET header, and then there are two possible data packet formats that
2985 * carry both src and dst addresses, plus 5 packet types in a format that
2986 * carries only the src node, plus 2 types that use a different format and
2987 * also carry just the src node.
2991 * Instead of doing those all right, we just look for data packets with
2992 * 0 or 1 bytes of padding. If you want to look at other packets, that
2993 * will require a lot more hacking.
2995 * To add support for filtering on DECNET "areas" (network numbers)
2996 * one would want to add a "mask" argument to this routine. That would
2997 * make the filter even more inefficient, although one could be clever
2998 * and not generate masking instructions if the mask is 0xFFFF.
3000 static struct block *
3001 gen_dnhostop(addr, dir)
3005 struct block *b0, *b1, *b2, *tmp;
3006 u_int offset_lh; /* offset if long header is received */
3007 u_int offset_sh; /* offset if short header is received */
3012 offset_sh = 1; /* follows flags */
3013 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
3017 offset_sh = 3; /* follows flags, dstnode */
3018 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3022 /* Inefficient because we do our Calvinball dance twice */
3023 b0 = gen_dnhostop(addr, Q_SRC);
3024 b1 = gen_dnhostop(addr, Q_DST);
3030 /* Inefficient because we do our Calvinball dance twice */
3031 b0 = gen_dnhostop(addr, Q_SRC);
3032 b1 = gen_dnhostop(addr, Q_DST);
3037 bpf_error("ISO host filtering not implemented");
3042 b0 = gen_linktype(ETHERTYPE_DN);
3043 /* Check for pad = 1, long header case */
3044 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3045 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
3046 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
3047 BPF_H, (bpf_int32)ntohs(addr));
3049 /* Check for pad = 0, long header case */
3050 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
3051 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs(addr));
3054 /* Check for pad = 1, short header case */
3055 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3056 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
3057 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
3060 /* Check for pad = 0, short header case */
3061 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
3062 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
3066 /* Combine with test for linktype */
3072 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3073 * test the bottom-of-stack bit, and then check the version number
3074 * field in the IP header.
3076 static struct block *
3077 gen_mpls_linktype(proto)
3080 struct block *b0, *b1;
3085 /* match the bottom-of-stack bit */
3086 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3087 /* match the IPv4 version number */
3088 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
3093 /* match the bottom-of-stack bit */
3094 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3095 /* match the IPv4 version number */
3096 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
3105 static struct block *
3106 gen_host(addr, mask, proto, dir)
3112 struct block *b0, *b1;
3117 b0 = gen_host(addr, mask, Q_IP, dir);
3119 * Only check for non-IPv4 addresses if we're not
3120 * checking MPLS-encapsulated packets.
3122 if (label_stack_depth == 0) {
3123 b1 = gen_host(addr, mask, Q_ARP, dir);
3125 b0 = gen_host(addr, mask, Q_RARP, dir);
3131 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
3134 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
3137 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
3140 bpf_error("'tcp' modifier applied to host");
3143 bpf_error("'sctp' modifier applied to host");
3146 bpf_error("'udp' modifier applied to host");
3149 bpf_error("'icmp' modifier applied to host");
3152 bpf_error("'igmp' modifier applied to host");
3155 bpf_error("'igrp' modifier applied to host");
3158 bpf_error("'pim' modifier applied to host");
3161 bpf_error("'vrrp' modifier applied to host");
3164 bpf_error("ATALK host filtering not implemented");
3167 bpf_error("AARP host filtering not implemented");
3170 return gen_dnhostop(addr, dir);
3173 bpf_error("SCA host filtering not implemented");
3176 bpf_error("LAT host filtering not implemented");
3179 bpf_error("MOPDL host filtering not implemented");
3182 bpf_error("MOPRC host filtering not implemented");
3186 bpf_error("'ip6' modifier applied to ip host");
3189 bpf_error("'icmp6' modifier applied to host");
3193 bpf_error("'ah' modifier applied to host");
3196 bpf_error("'esp' modifier applied to host");
3199 bpf_error("ISO host filtering not implemented");
3202 bpf_error("'esis' modifier applied to host");
3205 bpf_error("'isis' modifier applied to host");
3208 bpf_error("'clnp' modifier applied to host");
3211 bpf_error("'stp' modifier applied to host");
3214 bpf_error("IPX host filtering not implemented");
3217 bpf_error("'netbeui' modifier applied to host");
3220 bpf_error("'radio' modifier applied to host");
3229 static struct block *
3230 gen_host6(addr, mask, proto, dir)
3231 struct in6_addr *addr;
3232 struct in6_addr *mask;
3239 return gen_host6(addr, mask, Q_IPV6, dir);
3242 bpf_error("'ip' modifier applied to ip6 host");
3245 bpf_error("'rarp' modifier applied to ip6 host");
3248 bpf_error("'arp' modifier applied to ip6 host");
3251 bpf_error("'sctp' modifier applied to host");
3254 bpf_error("'tcp' modifier applied to host");
3257 bpf_error("'udp' modifier applied to host");
3260 bpf_error("'icmp' modifier applied to host");
3263 bpf_error("'igmp' modifier applied to host");
3266 bpf_error("'igrp' modifier applied to host");
3269 bpf_error("'pim' modifier applied to host");
3272 bpf_error("'vrrp' modifier applied to host");
3275 bpf_error("ATALK host filtering not implemented");
3278 bpf_error("AARP host filtering not implemented");
3281 bpf_error("'decnet' modifier applied to ip6 host");
3284 bpf_error("SCA host filtering not implemented");
3287 bpf_error("LAT host filtering not implemented");
3290 bpf_error("MOPDL host filtering not implemented");
3293 bpf_error("MOPRC host filtering not implemented");
3296 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
3299 bpf_error("'icmp6' modifier applied to host");
3302 bpf_error("'ah' modifier applied to host");
3305 bpf_error("'esp' modifier applied to host");
3308 bpf_error("ISO host filtering not implemented");
3311 bpf_error("'esis' modifier applied to host");
3314 bpf_error("'isis' modifier applied to host");
3317 bpf_error("'clnp' modifier applied to host");
3320 bpf_error("'stp' modifier applied to host");
3323 bpf_error("IPX host filtering not implemented");
3326 bpf_error("'netbeui' modifier applied to host");
3329 bpf_error("'radio' modifier applied to host");
3339 static struct block *
3340 gen_gateway(eaddr, alist, proto, dir)
3341 const u_char *eaddr;
3342 bpf_u_int32 **alist;
3346 struct block *b0, *b1, *tmp;
3349 bpf_error("direction applied to 'gateway'");
3356 if (linktype == DLT_EN10MB)
3357 b0 = gen_ehostop(eaddr, Q_OR);
3358 else if (linktype == DLT_FDDI)
3359 b0 = gen_fhostop(eaddr, Q_OR);
3360 else if (linktype == DLT_IEEE802)
3361 b0 = gen_thostop(eaddr, Q_OR);
3362 else if (linktype == DLT_IEEE802_11 ||
3363 linktype == DLT_IEEE802_11_RADIO_AVS ||
3364 linktype == DLT_IEEE802_11_RADIO ||
3365 linktype == DLT_PRISM_HEADER)
3366 b0 = gen_wlanhostop(eaddr, Q_OR);
3367 else if (linktype == DLT_SUNATM && is_lane) {
3369 * Check that the packet doesn't begin with an
3370 * LE Control marker. (We've already generated
3373 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3378 * Now check the MAC address.
3380 b0 = gen_ehostop(eaddr, Q_OR);
3382 } else if (linktype == DLT_IP_OVER_FC)
3383 b0 = gen_ipfchostop(eaddr, Q_OR);
3386 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3388 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR);
3390 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR);
3398 bpf_error("illegal modifier of 'gateway'");
3404 gen_proto_abbrev(proto)
3413 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
3415 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
3421 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
3423 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
3429 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
3431 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
3437 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
3440 #ifndef IPPROTO_IGMP
3441 #define IPPROTO_IGMP 2
3445 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
3448 #ifndef IPPROTO_IGRP
3449 #define IPPROTO_IGRP 9
3452 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
3456 #define IPPROTO_PIM 103
3460 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
3462 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
3467 #ifndef IPPROTO_VRRP
3468 #define IPPROTO_VRRP 112
3472 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
3476 b1 = gen_linktype(ETHERTYPE_IP);
3480 b1 = gen_linktype(ETHERTYPE_ARP);
3484 b1 = gen_linktype(ETHERTYPE_REVARP);
3488 bpf_error("link layer applied in wrong context");
3491 b1 = gen_linktype(ETHERTYPE_ATALK);
3495 b1 = gen_linktype(ETHERTYPE_AARP);
3499 b1 = gen_linktype(ETHERTYPE_DN);
3503 b1 = gen_linktype(ETHERTYPE_SCA);
3507 b1 = gen_linktype(ETHERTYPE_LAT);
3511 b1 = gen_linktype(ETHERTYPE_MOPDL);
3515 b1 = gen_linktype(ETHERTYPE_MOPRC);
3520 b1 = gen_linktype(ETHERTYPE_IPV6);
3523 #ifndef IPPROTO_ICMPV6
3524 #define IPPROTO_ICMPV6 58
3527 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
3532 #define IPPROTO_AH 51
3535 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
3537 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
3543 #define IPPROTO_ESP 50
3546 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
3548 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
3554 b1 = gen_linktype(LLCSAP_ISONS);
3558 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
3562 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
3565 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
3566 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3567 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3569 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3571 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3573 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3577 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
3578 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3579 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3581 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3583 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3585 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3589 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
3590 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3591 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3593 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
3598 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3599 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3604 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3605 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3607 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3609 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3614 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3615 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3620 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3621 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3626 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
3630 b1 = gen_linktype(LLCSAP_8021D);
3634 b1 = gen_linktype(LLCSAP_IPX);
3638 b1 = gen_linktype(LLCSAP_NETBEUI);
3642 bpf_error("'radio' is not a valid protocol type");
3650 static struct block *
3657 s = gen_load_a(OR_NET, 6, BPF_H);
3658 b = new_block(JMP(BPF_JSET));
3667 * Generate a comparison to a port value in the transport-layer header
3668 * at the specified offset from the beginning of that header.
3670 * XXX - this handles a variable-length prefix preceding the link-layer
3671 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3672 * variable-length link-layer headers (such as Token Ring or 802.11
3675 static struct block *
3676 gen_portatom(off, v)
3680 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
3684 static struct block *
3685 gen_portatom6(off, v)
3689 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
3694 gen_portop(port, proto, dir)
3695 int port, proto, dir;
3697 struct block *b0, *b1, *tmp;
3699 /* ip proto 'proto' */
3700 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
3706 b1 = gen_portatom(0, (bpf_int32)port);
3710 b1 = gen_portatom(2, (bpf_int32)port);
3715 tmp = gen_portatom(0, (bpf_int32)port);
3716 b1 = gen_portatom(2, (bpf_int32)port);
3721 tmp = gen_portatom(0, (bpf_int32)port);
3722 b1 = gen_portatom(2, (bpf_int32)port);
3734 static struct block *
3735 gen_port(port, ip_proto, dir)
3740 struct block *b0, *b1, *tmp;
3745 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3746 * not LLC encapsulation with LLCSAP_IP.
3748 * For IEEE 802 networks - which includes 802.5 token ring
3749 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3750 * says that SNAP encapsulation is used, not LLC encapsulation
3753 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3754 * RFC 2225 say that SNAP encapsulation is used, not LLC
3755 * encapsulation with LLCSAP_IP.
3757 * So we always check for ETHERTYPE_IP.
3759 b0 = gen_linktype(ETHERTYPE_IP);
3765 b1 = gen_portop(port, ip_proto, dir);
3769 tmp = gen_portop(port, IPPROTO_TCP, dir);
3770 b1 = gen_portop(port, IPPROTO_UDP, dir);
3772 tmp = gen_portop(port, IPPROTO_SCTP, dir);
3785 gen_portop6(port, proto, dir)
3786 int port, proto, dir;
3788 struct block *b0, *b1, *tmp;
3790 /* ip6 proto 'proto' */
3791 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
3795 b1 = gen_portatom6(0, (bpf_int32)port);
3799 b1 = gen_portatom6(2, (bpf_int32)port);
3804 tmp = gen_portatom6(0, (bpf_int32)port);
3805 b1 = gen_portatom6(2, (bpf_int32)port);
3810 tmp = gen_portatom6(0, (bpf_int32)port);
3811 b1 = gen_portatom6(2, (bpf_int32)port);
3823 static struct block *
3824 gen_port6(port, ip_proto, dir)
3829 struct block *b0, *b1, *tmp;
3831 /* link proto ip6 */
3832 b0 = gen_linktype(ETHERTYPE_IPV6);
3838 b1 = gen_portop6(port, ip_proto, dir);
3842 tmp = gen_portop6(port, IPPROTO_TCP, dir);
3843 b1 = gen_portop6(port, IPPROTO_UDP, dir);
3845 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
3857 /* gen_portrange code */
3858 static struct block *
3859 gen_portrangeatom(off, v1, v2)
3863 struct block *b1, *b2;
3867 * Reverse the order of the ports, so v1 is the lower one.
3876 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
3877 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
3885 gen_portrangeop(port1, port2, proto, dir)
3890 struct block *b0, *b1, *tmp;
3892 /* ip proto 'proto' */
3893 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
3899 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3903 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
3908 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3909 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
3914 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3915 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
3927 static struct block *
3928 gen_portrange(port1, port2, ip_proto, dir)
3933 struct block *b0, *b1, *tmp;
3936 b0 = gen_linktype(ETHERTYPE_IP);
3942 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
3946 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
3947 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
3949 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
3961 static struct block *
3962 gen_portrangeatom6(off, v1, v2)
3966 struct block *b1, *b2;
3970 * Reverse the order of the ports, so v1 is the lower one.
3979 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
3980 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
3988 gen_portrangeop6(port1, port2, proto, dir)
3993 struct block *b0, *b1, *tmp;
3995 /* ip6 proto 'proto' */
3996 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
4000 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4004 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4009 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4010 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4015 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4016 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4028 static struct block *
4029 gen_portrange6(port1, port2, ip_proto, dir)
4034 struct block *b0, *b1, *tmp;
4036 /* link proto ip6 */
4037 b0 = gen_linktype(ETHERTYPE_IPV6);
4043 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
4047 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
4048 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
4050 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
4063 lookup_proto(name, proto)
4064 register const char *name;
4074 v = pcap_nametoproto(name);
4075 if (v == PROTO_UNDEF)
4076 bpf_error("unknown ip proto '%s'", name);
4080 /* XXX should look up h/w protocol type based on linktype */
4081 v = pcap_nametoeproto(name);
4082 if (v == PROTO_UNDEF) {
4083 v = pcap_nametollc(name);
4084 if (v == PROTO_UNDEF)
4085 bpf_error("unknown ether proto '%s'", name);
4090 if (strcmp(name, "esis") == 0)
4092 else if (strcmp(name, "isis") == 0)
4094 else if (strcmp(name, "clnp") == 0)
4097 bpf_error("unknown osi proto '%s'", name);
4117 static struct block *
4118 gen_protochain(v, proto, dir)
4123 #ifdef NO_PROTOCHAIN
4124 return gen_proto(v, proto, dir);
4126 struct block *b0, *b;
4127 struct slist *s[100];
4128 int fix2, fix3, fix4, fix5;
4129 int ahcheck, again, end;
4131 int reg2 = alloc_reg();
4133 memset(s, 0, sizeof(s));
4134 fix2 = fix3 = fix4 = fix5 = 0;
4141 b0 = gen_protochain(v, Q_IP, dir);
4142 b = gen_protochain(v, Q_IPV6, dir);
4146 bpf_error("bad protocol applied for 'protochain'");
4151 * We don't handle variable-length radiotap here headers yet.
4152 * We might want to add BPF instructions to do the protochain
4153 * work, to simplify that and, on platforms that have a BPF
4154 * interpreter with the new instructions, let the filtering
4155 * be done in the kernel. (We already require a modified BPF
4156 * engine to do the protochain stuff, to support backward
4157 * branches, and backward branch support is unlikely to appear
4158 * in kernel BPF engines.)
4160 if (linktype == DLT_IEEE802_11_RADIO)
4161 bpf_error("'protochain' not supported with radiotap headers");
4163 no_optimize = 1; /*this code is not compatible with optimzer yet */
4166 * s[0] is a dummy entry to protect other BPF insn from damage
4167 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4168 * hard to find interdependency made by jump table fixup.
4171 s[i] = new_stmt(0); /*dummy*/
4176 b0 = gen_linktype(ETHERTYPE_IP);
4179 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4180 s[i]->s.k = off_nl + 9;
4182 /* X = ip->ip_hl << 2 */
4183 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
4189 b0 = gen_linktype(ETHERTYPE_IPV6);
4191 /* A = ip6->ip_nxt */
4192 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4193 s[i]->s.k = off_nl + 6;
4195 /* X = sizeof(struct ip6_hdr) */
4196 s[i] = new_stmt(BPF_LDX|BPF_IMM);
4202 bpf_error("unsupported proto to gen_protochain");
4206 /* again: if (A == v) goto end; else fall through; */
4208 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4210 s[i]->s.jt = NULL; /*later*/
4211 s[i]->s.jf = NULL; /*update in next stmt*/
4215 #ifndef IPPROTO_NONE
4216 #define IPPROTO_NONE 59
4218 /* if (A == IPPROTO_NONE) goto end */
4219 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4220 s[i]->s.jt = NULL; /*later*/
4221 s[i]->s.jf = NULL; /*update in next stmt*/
4222 s[i]->s.k = IPPROTO_NONE;
4223 s[fix5]->s.jf = s[i];
4228 if (proto == Q_IPV6) {
4229 int v6start, v6end, v6advance, j;
4232 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4233 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4234 s[i]->s.jt = NULL; /*later*/
4235 s[i]->s.jf = NULL; /*update in next stmt*/
4236 s[i]->s.k = IPPROTO_HOPOPTS;
4237 s[fix2]->s.jf = s[i];
4239 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4240 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4241 s[i]->s.jt = NULL; /*later*/
4242 s[i]->s.jf = NULL; /*update in next stmt*/
4243 s[i]->s.k = IPPROTO_DSTOPTS;
4245 /* if (A == IPPROTO_ROUTING) goto v6advance */
4246 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4247 s[i]->s.jt = NULL; /*later*/
4248 s[i]->s.jf = NULL; /*update in next stmt*/
4249 s[i]->s.k = IPPROTO_ROUTING;
4251 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4252 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4253 s[i]->s.jt = NULL; /*later*/
4254 s[i]->s.jf = NULL; /*later*/
4255 s[i]->s.k = IPPROTO_FRAGMENT;
4266 * X = X + (P[X + 1] + 1) * 8;
4269 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4271 /* A = P[X + packet head] */
4272 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4276 s[i] = new_stmt(BPF_ST);
4280 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4283 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4287 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4289 /* A = P[X + packet head]; */
4290 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4294 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4298 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4302 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4305 s[i] = new_stmt(BPF_LD|BPF_MEM);
4309 /* goto again; (must use BPF_JA for backward jump) */
4310 s[i] = new_stmt(BPF_JMP|BPF_JA);
4311 s[i]->s.k = again - i - 1;
4312 s[i - 1]->s.jf = s[i];
4316 for (j = v6start; j <= v6end; j++)
4317 s[j]->s.jt = s[v6advance];
4322 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4324 s[fix2]->s.jf = s[i];
4330 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4331 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4332 s[i]->s.jt = NULL; /*later*/
4333 s[i]->s.jf = NULL; /*later*/
4334 s[i]->s.k = IPPROTO_AH;
4336 s[fix3]->s.jf = s[ahcheck];
4343 * X = X + (P[X + 1] + 2) * 4;
4346 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4348 /* A = P[X + packet head]; */
4349 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4353 s[i] = new_stmt(BPF_ST);
4357 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4360 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4364 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4366 /* A = P[X + packet head] */
4367 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4371 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4375 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4379 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4382 s[i] = new_stmt(BPF_LD|BPF_MEM);
4386 /* goto again; (must use BPF_JA for backward jump) */
4387 s[i] = new_stmt(BPF_JMP|BPF_JA);
4388 s[i]->s.k = again - i - 1;
4393 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4395 s[fix2]->s.jt = s[end];
4396 s[fix4]->s.jf = s[end];
4397 s[fix5]->s.jt = s[end];
4404 for (i = 0; i < max - 1; i++)
4405 s[i]->next = s[i + 1];
4406 s[max - 1]->next = NULL;
4411 b = new_block(JMP(BPF_JEQ));
4412 b->stmts = s[1]; /*remember, s[0] is dummy*/
4423 * Generate code that checks whether the packet is a packet for protocol
4424 * <proto> and whether the type field in that protocol's header has
4425 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4426 * IP packet and checks the protocol number in the IP header against <v>.
4428 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4429 * against Q_IP and Q_IPV6.
4431 static struct block *
4432 gen_proto(v, proto, dir)
4437 struct block *b0, *b1;
4439 if (dir != Q_DEFAULT)
4440 bpf_error("direction applied to 'proto'");
4445 b0 = gen_proto(v, Q_IP, dir);
4446 b1 = gen_proto(v, Q_IPV6, dir);
4454 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4455 * not LLC encapsulation with LLCSAP_IP.
4457 * For IEEE 802 networks - which includes 802.5 token ring
4458 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4459 * says that SNAP encapsulation is used, not LLC encapsulation
4462 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4463 * RFC 2225 say that SNAP encapsulation is used, not LLC
4464 * encapsulation with LLCSAP_IP.
4466 * So we always check for ETHERTYPE_IP.
4469 b0 = gen_linktype(ETHERTYPE_IP);
4471 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
4473 b1 = gen_protochain(v, Q_IP);
4483 * Frame Relay packets typically have an OSI
4484 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4485 * generates code to check for all the OSI
4486 * NLPIDs, so calling it and then adding a check
4487 * for the particular NLPID for which we're
4488 * looking is bogus, as we can just check for
4491 * What we check for is the NLPID and a frame
4492 * control field value of UI, i.e. 0x03 followed
4495 * XXX - assumes a 2-byte Frame Relay header with
4496 * DLCI and flags. What if the address is longer?
4498 * XXX - what about SNAP-encapsulated frames?
4500 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
4506 * Cisco uses an Ethertype lookalike - for OSI,
4509 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
4510 /* OSI in C-HDLC is stuffed with a fudge byte */
4511 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
4516 b0 = gen_linktype(LLCSAP_ISONS);
4517 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
4523 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4525 * 4 is the offset of the PDU type relative to the IS-IS
4528 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
4533 bpf_error("arp does not encapsulate another protocol");
4537 bpf_error("rarp does not encapsulate another protocol");
4541 bpf_error("atalk encapsulation is not specifiable");
4545 bpf_error("decnet encapsulation is not specifiable");
4549 bpf_error("sca does not encapsulate another protocol");
4553 bpf_error("lat does not encapsulate another protocol");
4557 bpf_error("moprc does not encapsulate another protocol");
4561 bpf_error("mopdl does not encapsulate another protocol");
4565 return gen_linktype(v);
4568 bpf_error("'udp proto' is bogus");
4572 bpf_error("'tcp proto' is bogus");
4576 bpf_error("'sctp proto' is bogus");
4580 bpf_error("'icmp proto' is bogus");
4584 bpf_error("'igmp proto' is bogus");
4588 bpf_error("'igrp proto' is bogus");
4592 bpf_error("'pim proto' is bogus");
4596 bpf_error("'vrrp proto' is bogus");
4601 b0 = gen_linktype(ETHERTYPE_IPV6);
4603 b1 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
4605 b1 = gen_protochain(v, Q_IPV6);
4611 bpf_error("'icmp6 proto' is bogus");
4615 bpf_error("'ah proto' is bogus");
4618 bpf_error("'ah proto' is bogus");
4621 bpf_error("'stp proto' is bogus");
4624 bpf_error("'ipx proto' is bogus");
4627 bpf_error("'netbeui proto' is bogus");
4630 bpf_error("'radio proto' is bogus");
4641 register const char *name;
4644 int proto = q.proto;
4648 bpf_u_int32 mask, addr;
4650 bpf_u_int32 **alist;
4653 struct sockaddr_in *sin;
4654 struct sockaddr_in6 *sin6;
4655 struct addrinfo *res, *res0;
4656 struct in6_addr mask128;
4658 struct block *b, *tmp;
4659 int port, real_proto;
4665 addr = pcap_nametonetaddr(name);
4667 bpf_error("unknown network '%s'", name);
4668 /* Left justify network addr and calculate its network mask */
4670 while (addr && (addr & 0xff000000) == 0) {
4674 return gen_host(addr, mask, proto, dir);
4678 if (proto == Q_LINK) {
4682 eaddr = pcap_ether_hostton(name);
4685 "unknown ether host '%s'", name);
4686 b = gen_ehostop(eaddr, dir);
4691 eaddr = pcap_ether_hostton(name);
4694 "unknown FDDI host '%s'", name);
4695 b = gen_fhostop(eaddr, dir);
4700 eaddr = pcap_ether_hostton(name);
4703 "unknown token ring host '%s'", name);
4704 b = gen_thostop(eaddr, dir);
4708 case DLT_IEEE802_11:
4709 case DLT_IEEE802_11_RADIO_AVS:
4710 case DLT_IEEE802_11_RADIO:
4711 case DLT_PRISM_HEADER:
4712 eaddr = pcap_ether_hostton(name);
4715 "unknown 802.11 host '%s'", name);
4716 b = gen_wlanhostop(eaddr, dir);
4720 case DLT_IP_OVER_FC:
4721 eaddr = pcap_ether_hostton(name);
4724 "unknown Fibre Channel host '%s'", name);
4725 b = gen_ipfchostop(eaddr, dir);
4734 * Check that the packet doesn't begin
4735 * with an LE Control marker. (We've
4736 * already generated a test for LANE.)
4738 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
4742 eaddr = pcap_ether_hostton(name);
4745 "unknown ether host '%s'", name);
4746 b = gen_ehostop(eaddr, dir);
4752 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4753 } else if (proto == Q_DECNET) {
4754 unsigned short dn_addr = __pcap_nametodnaddr(name);
4756 * I don't think DECNET hosts can be multihomed, so
4757 * there is no need to build up a list of addresses
4759 return (gen_host(dn_addr, 0, proto, dir));
4762 alist = pcap_nametoaddr(name);
4763 if (alist == NULL || *alist == NULL)
4764 bpf_error("unknown host '%s'", name);
4766 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
4768 b = gen_host(**alist++, 0xffffffff, tproto, dir);
4770 tmp = gen_host(**alist++, 0xffffffff,
4777 memset(&mask128, 0xff, sizeof(mask128));
4778 res0 = res = pcap_nametoaddrinfo(name);
4780 bpf_error("unknown host '%s'", name);
4782 tproto = tproto6 = proto;
4783 if (off_linktype == -1 && tproto == Q_DEFAULT) {
4787 for (res = res0; res; res = res->ai_next) {
4788 switch (res->ai_family) {
4790 if (tproto == Q_IPV6)
4793 sin = (struct sockaddr_in *)
4795 tmp = gen_host(ntohl(sin->sin_addr.s_addr),
4796 0xffffffff, tproto, dir);
4799 if (tproto6 == Q_IP)
4802 sin6 = (struct sockaddr_in6 *)
4804 tmp = gen_host6(&sin6->sin6_addr,
4805 &mask128, tproto6, dir);
4816 bpf_error("unknown host '%s'%s", name,
4817 (proto == Q_DEFAULT)
4819 : " for specified address family");
4826 if (proto != Q_DEFAULT &&
4827 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
4828 bpf_error("illegal qualifier of 'port'");
4829 if (pcap_nametoport(name, &port, &real_proto) == 0)
4830 bpf_error("unknown port '%s'", name);
4831 if (proto == Q_UDP) {
4832 if (real_proto == IPPROTO_TCP)
4833 bpf_error("port '%s' is tcp", name);
4834 else if (real_proto == IPPROTO_SCTP)
4835 bpf_error("port '%s' is sctp", name);
4837 /* override PROTO_UNDEF */
4838 real_proto = IPPROTO_UDP;
4840 if (proto == Q_TCP) {
4841 if (real_proto == IPPROTO_UDP)
4842 bpf_error("port '%s' is udp", name);
4844 else if (real_proto == IPPROTO_SCTP)
4845 bpf_error("port '%s' is sctp", name);
4847 /* override PROTO_UNDEF */
4848 real_proto = IPPROTO_TCP;
4850 if (proto == Q_SCTP) {
4851 if (real_proto == IPPROTO_UDP)
4852 bpf_error("port '%s' is udp", name);
4854 else if (real_proto == IPPROTO_TCP)
4855 bpf_error("port '%s' is tcp", name);
4857 /* override PROTO_UNDEF */
4858 real_proto = IPPROTO_SCTP;
4861 return gen_port(port, real_proto, dir);
4865 b = gen_port(port, real_proto, dir);
4866 gen_or(gen_port6(port, real_proto, dir), b);
4872 if (proto != Q_DEFAULT &&
4873 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
4874 bpf_error("illegal qualifier of 'portrange'");
4875 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
4876 bpf_error("unknown port in range '%s'", name);
4877 if (proto == Q_UDP) {
4878 if (real_proto == IPPROTO_TCP)
4879 bpf_error("port in range '%s' is tcp", name);
4880 else if (real_proto == IPPROTO_SCTP)
4881 bpf_error("port in range '%s' is sctp", name);
4883 /* override PROTO_UNDEF */
4884 real_proto = IPPROTO_UDP;
4886 if (proto == Q_TCP) {
4887 if (real_proto == IPPROTO_UDP)
4888 bpf_error("port in range '%s' is udp", name);
4889 else if (real_proto == IPPROTO_SCTP)
4890 bpf_error("port in range '%s' is sctp", name);
4892 /* override PROTO_UNDEF */
4893 real_proto = IPPROTO_TCP;
4895 if (proto == Q_SCTP) {
4896 if (real_proto == IPPROTO_UDP)
4897 bpf_error("port in range '%s' is udp", name);
4898 else if (real_proto == IPPROTO_TCP)
4899 bpf_error("port in range '%s' is tcp", name);
4901 /* override PROTO_UNDEF */
4902 real_proto = IPPROTO_SCTP;
4905 return gen_portrange(port1, port2, real_proto, dir);
4909 b = gen_portrange(port1, port2, real_proto, dir);
4910 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
4917 eaddr = pcap_ether_hostton(name);
4919 bpf_error("unknown ether host: %s", name);
4921 alist = pcap_nametoaddr(name);
4922 if (alist == NULL || *alist == NULL)
4923 bpf_error("unknown host '%s'", name);
4924 b = gen_gateway(eaddr, alist, proto, dir);
4928 bpf_error("'gateway' not supported in this configuration");
4932 real_proto = lookup_proto(name, proto);
4933 if (real_proto >= 0)
4934 return gen_proto(real_proto, proto, dir);
4936 bpf_error("unknown protocol: %s", name);
4939 real_proto = lookup_proto(name, proto);
4940 if (real_proto >= 0)
4941 return gen_protochain(real_proto, proto, dir);
4943 bpf_error("unknown protocol: %s", name);
4955 gen_mcode(s1, s2, masklen, q)
4956 register const char *s1, *s2;
4957 register int masklen;
4960 register int nlen, mlen;
4963 nlen = __pcap_atoin(s1, &n);
4964 /* Promote short ipaddr */
4968 mlen = __pcap_atoin(s2, &m);
4969 /* Promote short ipaddr */
4972 bpf_error("non-network bits set in \"%s mask %s\"",
4975 /* Convert mask len to mask */
4977 bpf_error("mask length must be <= 32");
4978 m = 0xffffffff << (32 - masklen);
4980 bpf_error("non-network bits set in \"%s/%d\"",
4987 return gen_host(n, m, q.proto, q.dir);
4990 bpf_error("Mask syntax for networks only");
4998 register const char *s;
5003 int proto = q.proto;
5009 else if (q.proto == Q_DECNET)
5010 vlen = __pcap_atodn(s, &v);
5012 vlen = __pcap_atoin(s, &v);
5019 if (proto == Q_DECNET)
5020 return gen_host(v, 0, proto, dir);
5021 else if (proto == Q_LINK) {
5022 bpf_error("illegal link layer address");
5025 if (s == NULL && q.addr == Q_NET) {
5026 /* Promote short net number */
5027 while (v && (v & 0xff000000) == 0) {
5032 /* Promote short ipaddr */
5036 return gen_host(v, mask, proto, dir);
5041 proto = IPPROTO_UDP;
5042 else if (proto == Q_TCP)
5043 proto = IPPROTO_TCP;
5044 else if (proto == Q_SCTP)
5045 proto = IPPROTO_SCTP;
5046 else if (proto == Q_DEFAULT)
5047 proto = PROTO_UNDEF;
5049 bpf_error("illegal qualifier of 'port'");
5052 return gen_port((int)v, proto, dir);
5056 b = gen_port((int)v, proto, dir);
5057 gen_or(gen_port6((int)v, proto, dir), b);
5064 proto = IPPROTO_UDP;
5065 else if (proto == Q_TCP)
5066 proto = IPPROTO_TCP;
5067 else if (proto == Q_SCTP)
5068 proto = IPPROTO_SCTP;
5069 else if (proto == Q_DEFAULT)
5070 proto = PROTO_UNDEF;
5072 bpf_error("illegal qualifier of 'portrange'");
5075 return gen_portrange((int)v, (int)v, proto, dir);
5079 b = gen_portrange((int)v, (int)v, proto, dir);
5080 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
5086 bpf_error("'gateway' requires a name");
5090 return gen_proto((int)v, proto, dir);
5093 return gen_protochain((int)v, proto, dir);
5108 gen_mcode6(s1, s2, masklen, q)
5109 register const char *s1, *s2;
5110 register int masklen;
5113 struct addrinfo *res;
5114 struct in6_addr *addr;
5115 struct in6_addr mask;
5120 bpf_error("no mask %s supported", s2);
5122 res = pcap_nametoaddrinfo(s1);
5124 bpf_error("invalid ip6 address %s", s1);
5126 bpf_error("%s resolved to multiple address", s1);
5127 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
5129 if (sizeof(mask) * 8 < masklen)
5130 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
5131 memset(&mask, 0, sizeof(mask));
5132 memset(&mask, 0xff, masklen / 8);
5134 mask.s6_addr[masklen / 8] =
5135 (0xff << (8 - masklen % 8)) & 0xff;
5138 a = (u_int32_t *)addr;
5139 m = (u_int32_t *)&mask;
5140 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
5141 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
5142 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
5150 bpf_error("Mask syntax for networks only");
5154 b = gen_host6(addr, &mask, q.proto, q.dir);
5159 bpf_error("invalid qualifier against IPv6 address");
5167 register const u_char *eaddr;
5170 struct block *b, *tmp;
5172 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
5173 if (linktype == DLT_EN10MB)
5174 return gen_ehostop(eaddr, (int)q.dir);
5175 if (linktype == DLT_FDDI)
5176 return gen_fhostop(eaddr, (int)q.dir);
5177 if (linktype == DLT_IEEE802)
5178 return gen_thostop(eaddr, (int)q.dir);
5179 if (linktype == DLT_IEEE802_11 ||
5180 linktype == DLT_IEEE802_11_RADIO_AVS ||
5181 linktype == DLT_IEEE802_11_RADIO ||
5182 linktype == DLT_PRISM_HEADER)
5183 return gen_wlanhostop(eaddr, (int)q.dir);
5184 if (linktype == DLT_SUNATM && is_lane) {
5186 * Check that the packet doesn't begin with an
5187 * LE Control marker. (We've already generated
5190 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5195 * Now check the MAC address.
5197 b = gen_ehostop(eaddr, (int)q.dir);
5201 if (linktype == DLT_IP_OVER_FC)
5202 return gen_ipfchostop(eaddr, (int)q.dir);
5203 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5205 bpf_error("ethernet address used in non-ether expression");
5211 struct slist *s0, *s1;
5214 * This is definitely not the best way to do this, but the
5215 * lists will rarely get long.
5222 static struct slist *
5228 s = new_stmt(BPF_LDX|BPF_MEM);
5233 static struct slist *
5239 s = new_stmt(BPF_LD|BPF_MEM);
5245 * Modify "index" to use the value stored into its register as an
5246 * offset relative to the beginning of the header for the protocol
5247 * "proto", and allocate a register and put an item "size" bytes long
5248 * (1, 2, or 4) at that offset into that register, making it the register
5252 gen_load(proto, index, size)
5257 struct slist *s, *tmp;
5259 int regno = alloc_reg();
5261 free_reg(index->regno);
5265 bpf_error("data size must be 1, 2, or 4");
5281 bpf_error("unsupported index operation");
5285 * The offset is relative to the beginning of the packet
5286 * data, if we have a radio header. (If we don't, this
5289 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
5290 linktype != DLT_IEEE802_11_RADIO &&
5291 linktype != DLT_PRISM_HEADER)
5292 bpf_error("radio information not present in capture");
5295 * Load into the X register the offset computed into the
5296 * register specifed by "index".
5298 s = xfer_to_x(index);
5301 * Load the item at that offset.
5303 tmp = new_stmt(BPF_LD|BPF_IND|size);
5305 sappend(index->s, s);
5310 * The offset is relative to the beginning of
5311 * the link-layer header.
5313 * XXX - what about ATM LANE? Should the index be
5314 * relative to the beginning of the AAL5 frame, so
5315 * that 0 refers to the beginning of the LE Control
5316 * field, or relative to the beginning of the LAN
5317 * frame, so that 0 refers, for Ethernet LANE, to
5318 * the beginning of the destination address?
5320 s = gen_llprefixlen();
5323 * If "s" is non-null, it has code to arrange that the
5324 * X register contains the length of the prefix preceding
5325 * the link-layer header. Add to it the offset computed
5326 * into the register specified by "index", and move that
5327 * into the X register. Otherwise, just load into the X
5328 * register the offset computed into the register specifed
5332 sappend(s, xfer_to_a(index));
5333 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5334 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5336 s = xfer_to_x(index);
5339 * Load the item at the sum of the offset we've put in the
5340 * X register and the offset of the start of the link
5341 * layer header (which is 0 if the radio header is
5342 * variable-length; that header length is what we put
5343 * into the X register and then added to the index).
5345 tmp = new_stmt(BPF_LD|BPF_IND|size);
5348 sappend(index->s, s);
5364 * The offset is relative to the beginning of
5365 * the network-layer header.
5366 * XXX - are there any cases where we want
5369 s = gen_llprefixlen();
5372 * If "s" is non-null, it has code to arrange that the
5373 * X register contains the length of the prefix preceding
5374 * the link-layer header. Add to it the offset computed
5375 * into the register specified by "index", and move that
5376 * into the X register. Otherwise, just load into the X
5377 * register the offset computed into the register specifed
5381 sappend(s, xfer_to_a(index));
5382 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5383 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5385 s = xfer_to_x(index);
5388 * Load the item at the sum of the offset we've put in the
5389 * X register and the offset of the start of the network
5392 tmp = new_stmt(BPF_LD|BPF_IND|size);
5395 sappend(index->s, s);
5398 * Do the computation only if the packet contains
5399 * the protocol in question.
5401 b = gen_proto_abbrev(proto);
5403 gen_and(index->b, b);
5416 * The offset is relative to the beginning of
5417 * the transport-layer header.
5418 * XXX - are there any cases where we want
5420 * XXX - we should, if we're built with
5421 * IPv6 support, generate code to load either
5422 * IPv4, IPv6, or both, as appropriate.
5424 s = gen_loadx_iphdrlen();
5427 * The X register now contains the sum of the offset
5428 * of the beginning of the link-layer header and
5429 * the length of the network-layer header. Load
5430 * into the A register the offset relative to
5431 * the beginning of the transport layer header,
5432 * add the X register to that, move that to the
5433 * X register, and load with an offset from the
5434 * X register equal to the offset of the network
5435 * layer header relative to the beginning of
5436 * the link-layer header.
5438 sappend(s, xfer_to_a(index));
5439 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5440 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5441 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
5443 sappend(index->s, s);
5446 * Do the computation only if the packet contains
5447 * the protocol in question - which is true only
5448 * if this is an IP datagram and is the first or
5449 * only fragment of that datagram.
5451 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
5453 gen_and(index->b, b);
5455 gen_and(gen_proto_abbrev(Q_IP), b);
5461 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5465 index->regno = regno;
5466 s = new_stmt(BPF_ST);
5468 sappend(index->s, s);
5474 gen_relation(code, a0, a1, reversed)
5476 struct arth *a0, *a1;
5479 struct slist *s0, *s1, *s2;
5480 struct block *b, *tmp;
5484 if (code == BPF_JEQ) {
5485 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
5486 b = new_block(JMP(code));
5490 b = new_block(BPF_JMP|code|BPF_X);
5496 sappend(a0->s, a1->s);
5500 free_reg(a0->regno);
5501 free_reg(a1->regno);
5503 /* 'and' together protocol checks */
5506 gen_and(a0->b, tmp = a1->b);
5522 int regno = alloc_reg();
5523 struct arth *a = (struct arth *)newchunk(sizeof(*a));
5526 s = new_stmt(BPF_LD|BPF_LEN);
5527 s->next = new_stmt(BPF_ST);
5528 s->next->s.k = regno;
5543 a = (struct arth *)newchunk(sizeof(*a));
5547 s = new_stmt(BPF_LD|BPF_IMM);
5549 s->next = new_stmt(BPF_ST);
5565 s = new_stmt(BPF_ALU|BPF_NEG);
5568 s = new_stmt(BPF_ST);
5576 gen_arth(code, a0, a1)
5578 struct arth *a0, *a1;
5580 struct slist *s0, *s1, *s2;
5584 s2 = new_stmt(BPF_ALU|BPF_X|code);
5589 sappend(a0->s, a1->s);
5591 free_reg(a0->regno);
5592 free_reg(a1->regno);
5594 s0 = new_stmt(BPF_ST);
5595 a0->regno = s0->s.k = alloc_reg();
5602 * Here we handle simple allocation of the scratch registers.
5603 * If too many registers are alloc'd, the allocator punts.
5605 static int regused[BPF_MEMWORDS];
5609 * Return the next free register.
5614 int n = BPF_MEMWORDS;
5617 if (regused[curreg])
5618 curreg = (curreg + 1) % BPF_MEMWORDS;
5620 regused[curreg] = 1;
5624 bpf_error("too many registers needed to evaluate expression");
5629 * Return a register to the table so it can
5639 static struct block *
5646 s = new_stmt(BPF_LD|BPF_LEN);
5647 b = new_block(JMP(jmp));
5658 return gen_len(BPF_JGE, n);
5662 * Actually, this is less than or equal.
5670 b = gen_len(BPF_JGT, n);
5677 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5678 * the beginning of the link-layer header.
5679 * XXX - that means you can't test values in the radiotap header, but
5680 * as that header is difficult if not impossible to parse generally
5681 * without a loop, that might not be a severe problem. A new keyword
5682 * "radio" could be added for that, although what you'd really want
5683 * would be a way of testing particular radio header values, which
5684 * would generate code appropriate to the radio header in question.
5687 gen_byteop(op, idx, val)
5698 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5701 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5705 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5709 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
5713 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
5717 b = new_block(JMP(BPF_JEQ));
5724 static u_char abroadcast[] = { 0x0 };
5727 gen_broadcast(proto)
5730 bpf_u_int32 hostmask;
5731 struct block *b0, *b1, *b2;
5732 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5738 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
5739 return gen_ahostop(abroadcast, Q_DST);
5740 if (linktype == DLT_EN10MB)
5741 return gen_ehostop(ebroadcast, Q_DST);
5742 if (linktype == DLT_FDDI)
5743 return gen_fhostop(ebroadcast, Q_DST);
5744 if (linktype == DLT_IEEE802)
5745 return gen_thostop(ebroadcast, Q_DST);
5746 if (linktype == DLT_IEEE802_11 ||
5747 linktype == DLT_IEEE802_11_RADIO_AVS ||
5748 linktype == DLT_IEEE802_11_RADIO ||
5749 linktype == DLT_PRISM_HEADER)
5750 return gen_wlanhostop(ebroadcast, Q_DST);
5751 if (linktype == DLT_IP_OVER_FC)
5752 return gen_ipfchostop(ebroadcast, Q_DST);
5753 if (linktype == DLT_SUNATM && is_lane) {
5755 * Check that the packet doesn't begin with an
5756 * LE Control marker. (We've already generated
5759 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5764 * Now check the MAC address.
5766 b0 = gen_ehostop(ebroadcast, Q_DST);
5770 bpf_error("not a broadcast link");
5774 b0 = gen_linktype(ETHERTYPE_IP);
5775 hostmask = ~netmask;
5776 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
5777 b2 = gen_mcmp(OR_NET, 16, BPF_W,
5778 (bpf_int32)(~0 & hostmask), hostmask);
5783 bpf_error("only link-layer/IP broadcast filters supported");
5788 * Generate code to test the low-order bit of a MAC address (that's
5789 * the bottom bit of the *first* byte).
5791 static struct block *
5792 gen_mac_multicast(offset)
5795 register struct block *b0;
5796 register struct slist *s;
5798 /* link[offset] & 1 != 0 */
5799 s = gen_load_a(OR_LINK, offset, BPF_B);
5800 b0 = new_block(JMP(BPF_JSET));
5807 gen_multicast(proto)
5810 register struct block *b0, *b1, *b2;
5811 register struct slist *s;
5817 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
5818 /* all ARCnet multicasts use the same address */
5819 return gen_ahostop(abroadcast, Q_DST);
5821 if (linktype == DLT_EN10MB) {
5822 /* ether[0] & 1 != 0 */
5823 return gen_mac_multicast(0);
5826 if (linktype == DLT_FDDI) {
5828 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5830 * XXX - was that referring to bit-order issues?
5832 /* fddi[1] & 1 != 0 */
5833 return gen_mac_multicast(1);
5836 if (linktype == DLT_IEEE802) {
5837 /* tr[2] & 1 != 0 */
5838 return gen_mac_multicast(2);
5841 if (linktype == DLT_IEEE802_11 ||
5842 linktype == DLT_IEEE802_11_RADIO_AVS ||
5843 linktype == DLT_IEEE802_11_RADIO ||
5844 linktype == DLT_PRISM_HEADER) {
5848 * For control frames, there is no DA.
5850 * For management frames, DA is at an
5851 * offset of 4 from the beginning of
5854 * For data frames, DA is at an offset
5855 * of 4 from the beginning of the packet
5856 * if To DS is clear and at an offset of
5857 * 16 from the beginning of the packet
5862 * Generate the tests to be done for data frames.
5864 * First, check for To DS set, i.e. "link[1] & 0x01".
5866 s = gen_load_a(OR_LINK, 1, BPF_B);
5867 b1 = new_block(JMP(BPF_JSET));
5868 b1->s.k = 0x01; /* To DS */
5872 * If To DS is set, the DA is at 16.
5874 b0 = gen_mac_multicast(16);
5878 * Now, check for To DS not set, i.e. check
5879 * "!(link[1] & 0x01)".
5881 s = gen_load_a(OR_LINK, 1, BPF_B);
5882 b2 = new_block(JMP(BPF_JSET));
5883 b2->s.k = 0x01; /* To DS */
5888 * If To DS is not set, the DA is at 4.
5890 b1 = gen_mac_multicast(4);
5894 * Now OR together the last two checks. That gives
5895 * the complete set of checks for data frames.
5900 * Now check for a data frame.
5901 * I.e, check "link[0] & 0x08".
5903 s = gen_load_a(OR_LINK, 0, BPF_B);
5904 b1 = new_block(JMP(BPF_JSET));
5909 * AND that with the checks done for data frames.
5914 * If the high-order bit of the type value is 0, this
5915 * is a management frame.
5916 * I.e, check "!(link[0] & 0x08)".
5918 s = gen_load_a(OR_LINK, 0, BPF_B);
5919 b2 = new_block(JMP(BPF_JSET));
5925 * For management frames, the DA is at 4.
5927 b1 = gen_mac_multicast(4);
5931 * OR that with the checks done for data frames.
5932 * That gives the checks done for management and
5938 * If the low-order bit of the type value is 1,
5939 * this is either a control frame or a frame
5940 * with a reserved type, and thus not a
5943 * I.e., check "!(link[0] & 0x04)".
5945 s = gen_load_a(OR_LINK, 0, BPF_B);
5946 b1 = new_block(JMP(BPF_JSET));
5952 * AND that with the checks for data and management
5959 if (linktype == DLT_IP_OVER_FC) {
5960 b0 = gen_mac_multicast(2);
5964 if (linktype == DLT_SUNATM && is_lane) {
5966 * Check that the packet doesn't begin with an
5967 * LE Control marker. (We've already generated
5970 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5974 /* ether[off_mac] & 1 != 0 */
5975 b0 = gen_mac_multicast(off_mac);
5980 /* Link not known to support multicasts */
5984 b0 = gen_linktype(ETHERTYPE_IP);
5985 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
5991 b0 = gen_linktype(ETHERTYPE_IPV6);
5992 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
5997 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6002 * generate command for inbound/outbound. It's here so we can
6003 * make it link-type specific. 'dir' = 0 implies "inbound",
6004 * = 1 implies "outbound".
6010 register struct block *b0;
6013 * Only some data link types support inbound/outbound qualifiers.
6017 b0 = gen_relation(BPF_JEQ,
6018 gen_load(Q_LINK, gen_loadi(0), 1),
6026 * Match packets sent by this machine.
6028 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
6031 * Match packets sent to this machine.
6032 * (No broadcast or multicast packets, or
6033 * packets sent to some other machine and
6034 * received promiscuously.)
6036 * XXX - packets sent to other machines probably
6037 * shouldn't be matched, but what about broadcast
6038 * or multicast packets we received?
6040 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_HOST);
6045 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
6046 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
6051 /* match outgoing packets */
6052 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
6054 /* match incoming packets */
6055 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
6059 case DLT_JUNIPER_MFR:
6060 case DLT_JUNIPER_MLFR:
6061 case DLT_JUNIPER_MLPPP:
6062 case DLT_JUNIPER_ATM1:
6063 case DLT_JUNIPER_ATM2:
6064 case DLT_JUNIPER_PPPOE:
6065 case DLT_JUNIPER_PPPOE_ATM:
6066 case DLT_JUNIPER_GGSN:
6067 case DLT_JUNIPER_ES:
6068 case DLT_JUNIPER_MONITOR:
6069 case DLT_JUNIPER_SERVICES:
6070 case DLT_JUNIPER_ETHER:
6071 case DLT_JUNIPER_PPP:
6072 case DLT_JUNIPER_FRELAY:
6073 case DLT_JUNIPER_CHDLC:
6074 /* juniper flags (including direction) are stored
6075 * the byte after the 3-byte magic number */
6077 /* match outgoing packets */
6078 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
6080 /* match incoming packets */
6081 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
6086 bpf_error("inbound/outbound not supported on linktype %d",
6094 /* PF firewall log matched interface */
6096 gen_pf_ifname(const char *ifname)
6101 if (linktype == DLT_PFLOG) {
6102 len = sizeof(((struct pfloghdr *)0)->ifname);
6103 off = offsetof(struct pfloghdr, ifname);
6105 bpf_error("ifname not supported on linktype 0x%x", linktype);
6108 if (strlen(ifname) >= len) {
6109 bpf_error("ifname interface names can only be %d characters",
6113 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
6117 /* PF firewall log ruleset name */
6119 gen_pf_ruleset(char *ruleset)
6123 if (linktype != DLT_PFLOG) {
6124 bpf_error("ruleset not supported on linktype 0x%x", linktype);
6127 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
6128 bpf_error("ruleset names can only be %ld characters",
6129 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
6132 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
6133 strlen(ruleset), (const u_char *)ruleset);
6137 /* PF firewall log rule number */
6143 if (linktype == DLT_PFLOG) {
6144 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
6147 bpf_error("rnr not supported on linktype 0x%x", linktype);
6154 /* PF firewall log sub-rule number */
6156 gen_pf_srnr(int srnr)
6160 if (linktype != DLT_PFLOG) {
6161 bpf_error("srnr not supported on linktype 0x%x", linktype);
6165 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
6170 /* PF firewall log reason code */
6172 gen_pf_reason(int reason)
6176 if (linktype == DLT_PFLOG) {
6177 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
6180 bpf_error("reason not supported on linktype 0x%x", linktype);
6187 /* PF firewall log action */
6189 gen_pf_action(int action)
6193 if (linktype == DLT_PFLOG) {
6194 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
6197 bpf_error("action not supported on linktype 0x%x", linktype);
6206 register const u_char *eaddr;
6209 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6210 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
6211 return gen_ahostop(eaddr, (int)q.dir);
6213 bpf_error("ARCnet address used in non-arc expression");
6217 static struct block *
6218 gen_ahostop(eaddr, dir)
6219 register const u_char *eaddr;
6222 register struct block *b0, *b1;
6225 /* src comes first, different from Ethernet */
6227 return gen_bcmp(OR_LINK, 0, 1, eaddr);
6230 return gen_bcmp(OR_LINK, 1, 1, eaddr);
6233 b0 = gen_ahostop(eaddr, Q_SRC);
6234 b1 = gen_ahostop(eaddr, Q_DST);
6240 b0 = gen_ahostop(eaddr, Q_SRC);
6241 b1 = gen_ahostop(eaddr, Q_DST);
6250 * support IEEE 802.1Q VLAN trunk over ethernet
6256 struct block *b0, *b1;
6258 /* can't check for VLAN-encapsulated packets inside MPLS */
6259 if (label_stack_depth > 0)
6260 bpf_error("no VLAN match after MPLS");
6263 * Change the offsets to point to the type and data fields within
6264 * the VLAN packet. Just increment the offsets, so that we
6265 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6266 * capture VLAN 200 encapsulated within VLAN 100.
6268 * XXX - this is a bit of a kludge. If we were to split the
6269 * compiler into a parser that parses an expression and
6270 * generates an expression tree, and a code generator that
6271 * takes an expression tree (which could come from our
6272 * parser or from some other parser) and generates BPF code,
6273 * we could perhaps make the offsets parameters of routines
6274 * and, in the handler for an "AND" node, pass to subnodes
6275 * other than the VLAN node the adjusted offsets.
6277 * This would mean that "vlan" would, instead of changing the
6278 * behavior of *all* tests after it, change only the behavior
6279 * of tests ANDed with it. That would change the documented
6280 * semantics of "vlan", which might break some expressions.
6281 * However, it would mean that "(vlan and ip) or ip" would check
6282 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6283 * checking only for VLAN-encapsulated IP, so that could still
6284 * be considered worth doing; it wouldn't break expressions
6285 * that are of the form "vlan and ..." or "vlan N and ...",
6286 * which I suspect are the most common expressions involving
6287 * "vlan". "vlan or ..." doesn't necessarily do what the user
6288 * would really want, now, as all the "or ..." tests would
6289 * be done assuming a VLAN, even though the "or" could be viewed
6290 * as meaning "or, if this isn't a VLAN packet...".
6292 orig_linktype = off_linktype; /* save original values */
6304 bpf_error("no VLAN support for data link type %d",
6309 /* check for VLAN */
6310 b0 = gen_cmp(OR_LINK, orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q);
6312 /* If a specific VLAN is requested, check VLAN id */
6313 if (vlan_num >= 0) {
6314 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_H, (bpf_int32)vlan_num,
6330 struct block *b0,*b1;
6333 * Change the offsets to point to the type and data fields within
6334 * the MPLS packet. Just increment the offsets, so that we
6335 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6336 * capture packets with an outer label of 100000 and an inner
6339 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6343 if (label_stack_depth > 0) {
6344 /* just match the bottom-of-stack bit clear */
6345 b0 = gen_mcmp(OR_LINK, orig_nl-2, BPF_B, 0, 0x01);
6348 * Indicate that we're checking MPLS-encapsulated headers,
6349 * to make sure higher level code generators don't try to
6350 * match against IP-related protocols such as Q_ARP, Q_RARP
6355 case DLT_C_HDLC: /* fall through */
6357 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
6358 (bpf_int32)ETHERTYPE_MPLS);
6362 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
6363 (bpf_int32)PPP_MPLS_UCAST);
6366 /* FIXME add other DLT_s ...
6367 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6368 * leave it for now */
6371 bpf_error("no MPLS support for data link type %d",
6379 /* If a specific MPLS label is requested, check it */
6380 if (label_num >= 0) {
6381 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
6382 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_W, (bpf_int32)label_num,
6383 0xfffff000); /* only compare the first 20 bits */
6390 label_stack_depth++;
6395 * Support PPPOE discovery and session.
6400 /* check for PPPoE discovery */
6401 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
6410 * Test against the PPPoE session link-layer type.
6412 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
6415 * Change the offsets to point to the type and data fields within
6418 * XXX - this is a bit of a kludge. If we were to split the
6419 * compiler into a parser that parses an expression and
6420 * generates an expression tree, and a code generator that
6421 * takes an expression tree (which could come from our
6422 * parser or from some other parser) and generates BPF code,
6423 * we could perhaps make the offsets parameters of routines
6424 * and, in the handler for an "AND" node, pass to subnodes
6425 * other than the PPPoE node the adjusted offsets.
6427 * This would mean that "pppoes" would, instead of changing the
6428 * behavior of *all* tests after it, change only the behavior
6429 * of tests ANDed with it. That would change the documented
6430 * semantics of "pppoes", which might break some expressions.
6431 * However, it would mean that "(pppoes and ip) or ip" would check
6432 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6433 * checking only for VLAN-encapsulated IP, so that could still
6434 * be considered worth doing; it wouldn't break expressions
6435 * that are of the form "pppoes and ..." which I suspect are the
6436 * most common expressions involving "pppoes". "pppoes or ..."
6437 * doesn't necessarily do what the user would really want, now,
6438 * as all the "or ..." tests would be done assuming PPPoE, even
6439 * though the "or" could be viewed as meaning "or, if this isn't
6440 * a PPPoE packet...".
6442 orig_linktype = off_linktype; /* save original values */
6446 * The "network-layer" protocol is PPPoE, which has a 6-byte
6447 * PPPoE header, followed by PPP payload, so we set the
6448 * offsets to the network layer offset plus 6 bytes for
6449 * the PPPoE header plus the values appropriate for PPP when
6450 * encapsulated in Ethernet (which means there's no HDLC
6453 off_linktype = orig_nl + 6;
6454 off_nl = orig_nl + 6 + 2;
6455 off_nl_nosnap = orig_nl + 6 + 2;
6458 * Set the link-layer type to PPP, as all subsequent tests will
6459 * be on the encapsulated PPP header.
6467 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
6479 bpf_error("'vpi' supported only on raw ATM");
6480 if (off_vpi == (u_int)-1)
6482 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
6488 bpf_error("'vci' supported only on raw ATM");
6489 if (off_vci == (u_int)-1)
6491 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
6496 if (off_proto == (u_int)-1)
6497 abort(); /* XXX - this isn't on FreeBSD */
6498 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
6503 if (off_payload == (u_int)-1)
6505 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
6506 0xffffffff, jtype, reverse, jvalue);
6511 bpf_error("'callref' supported only on raw ATM");
6512 if (off_proto == (u_int)-1)
6514 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
6515 jtype, reverse, jvalue);
6525 gen_atmtype_abbrev(type)
6528 struct block *b0, *b1;
6533 /* Get all packets in Meta signalling Circuit */
6535 bpf_error("'metac' supported only on raw ATM");
6536 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6537 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
6542 /* Get all packets in Broadcast Circuit*/
6544 bpf_error("'bcc' supported only on raw ATM");
6545 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6546 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
6551 /* Get all cells in Segment OAM F4 circuit*/
6553 bpf_error("'oam4sc' supported only on raw ATM");
6554 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6555 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6560 /* Get all cells in End-to-End OAM F4 Circuit*/
6562 bpf_error("'oam4ec' supported only on raw ATM");
6563 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6564 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6569 /* Get all packets in connection Signalling Circuit */
6571 bpf_error("'sc' supported only on raw ATM");
6572 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6573 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
6578 /* Get all packets in ILMI Circuit */
6580 bpf_error("'ilmic' supported only on raw ATM");
6581 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6582 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
6587 /* Get all LANE packets */
6589 bpf_error("'lane' supported only on raw ATM");
6590 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
6593 * Arrange that all subsequent tests assume LANE
6594 * rather than LLC-encapsulated packets, and set
6595 * the offsets appropriately for LANE-encapsulated
6598 * "off_mac" is the offset of the Ethernet header,
6599 * which is 2 bytes past the ATM pseudo-header
6600 * (skipping the pseudo-header and 2-byte LE Client
6601 * field). The other offsets are Ethernet offsets
6602 * relative to "off_mac".
6605 off_mac = off_payload + 2; /* MAC header */
6606 off_linktype = off_mac + 12;
6607 off_nl = off_mac + 14; /* Ethernet II */
6608 off_nl_nosnap = off_mac + 17; /* 802.3+802.2 */
6612 /* Get all LLC-encapsulated packets */
6614 bpf_error("'llc' supported only on raw ATM");
6615 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
6626 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
6633 bpf_u_int32 val1 , val2 , val3;
6635 switch (mtp3field) {
6638 if (off_sio == (u_int)-1)
6639 bpf_error("'sio' supported only on SS7");
6640 /* sio coded on 1 byte so max value 255 */
6642 bpf_error("sio value %u too big; max value = 255",
6644 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
6645 (u_int)jtype, reverse, (u_int)jvalue);
6649 if (off_opc == (u_int)-1)
6650 bpf_error("'opc' supported only on SS7");
6651 /* opc coded on 14 bits so max value 16383 */
6653 bpf_error("opc value %u too big; max value = 16383",
6655 /* the following instructions are made to convert jvalue
6656 * to the form used to write opc in an ss7 message*/
6657 val1 = jvalue & 0x00003c00;
6659 val2 = jvalue & 0x000003fc;
6661 val3 = jvalue & 0x00000003;
6663 jvalue = val1 + val2 + val3;
6664 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
6665 (u_int)jtype, reverse, (u_int)jvalue);
6669 if (off_dpc == (u_int)-1)
6670 bpf_error("'dpc' supported only on SS7");
6671 /* dpc coded on 14 bits so max value 16383 */
6673 bpf_error("dpc value %u too big; max value = 16383",
6675 /* the following instructions are made to convert jvalue
6676 * to the forme used to write dpc in an ss7 message*/
6677 val1 = jvalue & 0x000000ff;
6679 val2 = jvalue & 0x00003f00;
6681 jvalue = val1 + val2;
6682 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
6683 (u_int)jtype, reverse, (u_int)jvalue);
6687 if (off_sls == (u_int)-1)
6688 bpf_error("'sls' supported only on SS7");
6689 /* sls coded on 4 bits so max value 15 */
6691 bpf_error("sls value %u too big; max value = 15",
6693 /* the following instruction is made to convert jvalue
6694 * to the forme used to write sls in an ss7 message*/
6695 jvalue = jvalue << 4;
6696 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
6697 (u_int)jtype,reverse, (u_int)jvalue);
6706 static struct block *
6707 gen_msg_abbrev(type)
6713 * Q.2931 signalling protocol messages for handling virtual circuits
6714 * establishment and teardown
6719 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
6723 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
6727 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
6731 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
6735 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
6738 case A_RELEASE_DONE:
6739 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
6749 gen_atmmulti_abbrev(type)
6752 struct block *b0, *b1;
6758 bpf_error("'oam' supported only on raw ATM");
6759 b1 = gen_atmmulti_abbrev(A_OAMF4);
6764 bpf_error("'oamf4' supported only on raw ATM");
6766 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6767 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6769 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6775 * Get Q.2931 signalling messages for switched
6776 * virtual connection
6779 bpf_error("'connectmsg' supported only on raw ATM");
6780 b0 = gen_msg_abbrev(A_SETUP);
6781 b1 = gen_msg_abbrev(A_CALLPROCEED);
6783 b0 = gen_msg_abbrev(A_CONNECT);
6785 b0 = gen_msg_abbrev(A_CONNECTACK);
6787 b0 = gen_msg_abbrev(A_RELEASE);
6789 b0 = gen_msg_abbrev(A_RELEASE_DONE);
6791 b0 = gen_atmtype_abbrev(A_SC);
6797 bpf_error("'metaconnect' supported only on raw ATM");
6798 b0 = gen_msg_abbrev(A_SETUP);
6799 b1 = gen_msg_abbrev(A_CALLPROCEED);
6801 b0 = gen_msg_abbrev(A_CONNECT);
6803 b0 = gen_msg_abbrev(A_RELEASE);
6805 b0 = gen_msg_abbrev(A_RELEASE_DONE);
6807 b0 = gen_atmtype_abbrev(A_METAC);