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.43 2006/09/13 07:36:19 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, int);
211 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, 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 from
672 * the beginning of the raw packet data.
674 * It's usually 0, except for 802.11 with a fixed-length radio header.
675 * (For 802.11 with a variable-length radio header, we have to generate
676 * code to compute that offset; off_ll is 0 in that case.)
681 * This is the offset of the beginning of the MAC-layer header.
682 * It's usually 0, except for ATM LANE, where it's the offset, relative
683 * to the beginning of the raw packet data, of the Ethernet header.
685 static u_int off_mac;
688 * "off_linktype" is the offset to information in the link-layer header
689 * giving the packet type. This offset is relative to the beginning
690 * of the link-layer header (i.e., it doesn't include off_ll).
692 * For Ethernet, it's the offset of the Ethernet type field.
694 * For link-layer types that always use 802.2 headers, it's the
695 * offset of the LLC header.
697 * For PPP, it's the offset of the PPP type field.
699 * For Cisco HDLC, it's the offset of the CHDLC type field.
701 * For BSD loopback, it's the offset of the AF_ value.
703 * For Linux cooked sockets, it's the offset of the type field.
705 * It's set to -1 for no encapsulation, in which case, IP is assumed.
707 static u_int off_linktype;
710 * TRUE if the link layer includes an ATM pseudo-header.
712 static int is_atm = 0;
715 * TRUE if "lane" appeared in the filter; it causes us to generate
716 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
718 static int is_lane = 0;
721 * These are offsets for the ATM pseudo-header.
723 static u_int off_vpi;
724 static u_int off_vci;
725 static u_int off_proto;
728 * These are offsets for the MTP3 fields.
730 static u_int off_sio;
731 static u_int off_opc;
732 static u_int off_dpc;
733 static u_int off_sls;
736 * This is the offset of the first byte after the ATM pseudo_header,
737 * or -1 if there is no ATM pseudo-header.
739 static u_int off_payload;
742 * These are offsets to the beginning of the network-layer header.
743 * They are relative to the beginning of the link-layer header (i.e.,
744 * they don't include off_ll).
746 * If the link layer never uses 802.2 LLC:
748 * "off_nl" and "off_nl_nosnap" are the same.
750 * If the link layer always uses 802.2 LLC:
752 * "off_nl" is the offset if there's a SNAP header following
755 * "off_nl_nosnap" is the offset if there's no SNAP header.
757 * If the link layer is Ethernet:
759 * "off_nl" is the offset if the packet is an Ethernet II packet
760 * (we assume no 802.3+802.2+SNAP);
762 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
763 * with an 802.2 header following it.
766 static u_int off_nl_nosnap;
774 linktype = pcap_datalink(p);
776 pcap_fddipad = p->fddipad;
780 * Assume it's not raw ATM with a pseudo-header, for now.
791 * And assume we're not doing SS7.
799 * Also assume it's not 802.11 with a fixed-length radio header.
805 label_stack_depth = 0;
813 off_nl = 6; /* XXX in reality, variable! */
814 off_nl_nosnap = 6; /* no 802.2 LLC */
817 case DLT_ARCNET_LINUX:
819 off_nl = 8; /* XXX in reality, variable! */
820 off_nl_nosnap = 8; /* no 802.2 LLC */
825 off_nl = 14; /* Ethernet II */
826 off_nl_nosnap = 17; /* 802.3+802.2 */
831 * SLIP doesn't have a link level type. The 16 byte
832 * header is hacked into our SLIP driver.
836 off_nl_nosnap = 16; /* no 802.2 LLC */
840 /* XXX this may be the same as the DLT_PPP_BSDOS case */
844 off_nl_nosnap = 24; /* no 802.2 LLC */
851 off_nl_nosnap = 4; /* no 802.2 LLC */
857 off_nl_nosnap = 12; /* no 802.2 LLC */
862 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
863 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
866 off_nl_nosnap = 4; /* no 802.2 LLC */
871 * This does no include the Ethernet header, and
872 * only covers session state.
876 off_nl_nosnap = 8; /* no 802.2 LLC */
882 off_nl_nosnap = 24; /* no 802.2 LLC */
887 * FDDI doesn't really have a link-level type field.
888 * We set "off_linktype" to the offset of the LLC header.
890 * To check for Ethernet types, we assume that SSAP = SNAP
891 * is being used and pick out the encapsulated Ethernet type.
892 * XXX - should we generate code to check for SNAP?
896 off_linktype += pcap_fddipad;
898 off_nl = 21; /* FDDI+802.2+SNAP */
899 off_nl_nosnap = 16; /* FDDI+802.2 */
901 off_nl += pcap_fddipad;
902 off_nl_nosnap += pcap_fddipad;
908 * Token Ring doesn't really have a link-level type field.
909 * We set "off_linktype" to the offset of the LLC header.
911 * To check for Ethernet types, we assume that SSAP = SNAP
912 * is being used and pick out the encapsulated Ethernet type.
913 * XXX - should we generate code to check for SNAP?
915 * XXX - the header is actually variable-length.
916 * Some various Linux patched versions gave 38
917 * as "off_linktype" and 40 as "off_nl"; however,
918 * if a token ring packet has *no* routing
919 * information, i.e. is not source-routed, the correct
920 * values are 20 and 22, as they are in the vanilla code.
922 * A packet is source-routed iff the uppermost bit
923 * of the first byte of the source address, at an
924 * offset of 8, has the uppermost bit set. If the
925 * packet is source-routed, the total number of bytes
926 * of routing information is 2 plus bits 0x1F00 of
927 * the 16-bit value at an offset of 14 (shifted right
928 * 8 - figure out which byte that is).
931 off_nl = 22; /* Token Ring+802.2+SNAP */
932 off_nl_nosnap = 17; /* Token Ring+802.2 */
937 * 802.11 doesn't really have a link-level type field.
938 * We set "off_linktype" to the offset of the LLC header.
940 * To check for Ethernet types, we assume that SSAP = SNAP
941 * is being used and pick out the encapsulated Ethernet type.
942 * XXX - should we generate code to check for SNAP?
944 * XXX - the header is actually variable-length. We
945 * assume a 24-byte link-layer header, as appears in
946 * data frames in networks with no bridges. If the
947 * fromds and tods 802.11 header bits are both set,
948 * it's actually supposed to be 30 bytes.
951 off_nl = 32; /* 802.11+802.2+SNAP */
952 off_nl_nosnap = 27; /* 802.11+802.2 */
955 case DLT_PRISM_HEADER:
957 * Same as 802.11, but with an additional header before
958 * the 802.11 header, containing a bunch of additional
959 * information including radio-level information.
961 * The header is 144 bytes long.
963 * XXX - same variable-length header problem; at least
964 * the Prism header is fixed-length.
968 off_nl = 32; /* Prism+802.11+802.2+SNAP */
969 off_nl_nosnap = 27; /* Prism+802.11+802.2 */
972 case DLT_IEEE802_11_RADIO_AVS:
974 * Same as 802.11, but with an additional header before
975 * the 802.11 header, containing a bunch of additional
976 * information including radio-level information.
978 * The header is 64 bytes long, at least in its
979 * current incarnation.
981 * XXX - same variable-length header problem, only
982 * more so; this header is also variable-length,
983 * with the length being the 32-bit big-endian
984 * number at an offset of 4 from the beginning
985 * of the radio header. We should handle that the
986 * same way we handle the length at the beginning
987 * of the radiotap header.
989 * XXX - in Linux, do any drivers that supply an AVS
990 * header supply a link-layer type other than
991 * ARPHRD_IEEE80211_PRISM? If so, we should map that
992 * to DLT_IEEE802_11_RADIO_AVS; if not, or if there are
993 * any drivers that supply an AVS header but supply
994 * an ARPHRD value of ARPHRD_IEEE80211_PRISM, we'll
995 * have to check the header in the generated code to
996 * determine whether it's Prism or AVS.
1000 off_nl = 32; /* Radio+802.11+802.2+SNAP */
1001 off_nl_nosnap = 27; /* Radio+802.11+802.2 */
1004 case DLT_IEEE802_11_RADIO:
1006 * Same as 802.11, but with an additional header before
1007 * the 802.11 header, containing a bunch of additional
1008 * information including radio-level information.
1010 * The radiotap header is variable length, and we
1011 * generate code to compute its length and store it
1012 * in a register. These offsets are relative to the
1013 * beginning of the 802.11 header.
1016 off_nl = 32; /* 802.11+802.2+SNAP */
1017 off_nl_nosnap = 27; /* 802.11+802.2 */
1020 case DLT_ATM_RFC1483:
1021 case DLT_ATM_CLIP: /* Linux ATM defines this */
1023 * assume routed, non-ISO PDUs
1024 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1026 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1027 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1028 * latter would presumably be treated the way PPPoE
1029 * should be, so you can do "pppoe and udp port 2049"
1030 * or "pppoa and tcp port 80" and have it check for
1031 * PPPo{A,E} and a PPP protocol of IP and....
1034 off_nl = 8; /* 802.2+SNAP */
1035 off_nl_nosnap = 3; /* 802.2 */
1040 * Full Frontal ATM; you get AALn PDUs with an ATM
1044 off_vpi = SUNATM_VPI_POS;
1045 off_vci = SUNATM_VCI_POS;
1046 off_proto = PROTO_POS;
1047 off_mac = -1; /* LLC-encapsulated, so no MAC-layer header */
1048 off_payload = SUNATM_PKT_BEGIN_POS;
1049 off_linktype = off_payload;
1050 off_nl = off_payload+8; /* 802.2+SNAP */
1051 off_nl_nosnap = off_payload+3; /* 802.2 */
1057 off_nl_nosnap = 0; /* no 802.2 LLC */
1060 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1063 off_nl_nosnap = 16; /* no 802.2 LLC */
1068 * LocalTalk does have a 1-byte type field in the LLAP header,
1069 * but really it just indicates whether there is a "short" or
1070 * "long" DDP packet following.
1074 off_nl_nosnap = 0; /* no 802.2 LLC */
1077 case DLT_IP_OVER_FC:
1079 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1080 * link-level type field. We set "off_linktype" to the
1081 * offset of the LLC header.
1083 * To check for Ethernet types, we assume that SSAP = SNAP
1084 * is being used and pick out the encapsulated Ethernet type.
1085 * XXX - should we generate code to check for SNAP? RFC
1086 * 2625 says SNAP should be used.
1089 off_nl = 24; /* IPFC+802.2+SNAP */
1090 off_nl_nosnap = 19; /* IPFC+802.2 */
1095 * XXX - we should set this to handle SNAP-encapsulated
1096 * frames (NLPID of 0x80).
1100 off_nl_nosnap = 0; /* no 802.2 LLC */
1104 * the only BPF-interesting FRF.16 frames are non-control frames;
1105 * Frame Relay has a variable length link-layer
1106 * so lets start with offset 4 for now and increments later on (FIXME);
1111 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1114 case DLT_APPLE_IP_OVER_IEEE1394:
1117 off_nl_nosnap = 18; /* no 802.2 LLC */
1120 case DLT_LINUX_IRDA:
1122 * Currently, only raw "link[N:M]" filtering is supported.
1131 * Currently, only raw "link[N:M]" filtering is supported.
1138 case DLT_SYMANTEC_FIREWALL:
1140 off_nl = 44; /* Ethernet II */
1141 off_nl_nosnap = 44; /* XXX - what does it do with 802.3 packets? */
1146 /* XXX read this from pf.h? */
1147 off_nl = PFLOG_HDRLEN;
1148 off_nl_nosnap = PFLOG_HDRLEN; /* no 802.2 LLC */
1151 case DLT_JUNIPER_MFR:
1152 case DLT_JUNIPER_MLFR:
1153 case DLT_JUNIPER_MLPPP:
1154 case DLT_JUNIPER_PPP:
1155 case DLT_JUNIPER_CHDLC:
1156 case DLT_JUNIPER_FRELAY:
1159 off_nl_nosnap = -1; /* no 802.2 LLC */
1162 case DLT_JUNIPER_ATM1:
1163 off_linktype = 4; /* in reality variable between 4-8 */
1168 case DLT_JUNIPER_ATM2:
1169 off_linktype = 8; /* in reality variable between 8-12 */
1174 /* frames captured on a Juniper PPPoE service PIC
1175 * contain raw ethernet frames */
1176 case DLT_JUNIPER_PPPOE:
1177 case DLT_JUNIPER_ETHER:
1179 off_nl = 18; /* Ethernet II */
1180 off_nl_nosnap = 21; /* 802.3+802.2 */
1183 case DLT_JUNIPER_PPPOE_ATM:
1186 off_nl_nosnap = -1; /* no 802.2 LLC */
1189 case DLT_JUNIPER_GGSN:
1192 off_nl_nosnap = -1; /* no 802.2 LLC */
1195 case DLT_JUNIPER_ES:
1197 off_nl = -1; /* not really a network layer but raw IP adresses */
1198 off_nl_nosnap = -1; /* no 802.2 LLC */
1201 case DLT_JUNIPER_MONITOR:
1203 off_nl = 12; /* raw IP/IP6 header */
1204 off_nl_nosnap = -1; /* no 802.2 LLC */
1207 case DLT_JUNIPER_SERVICES:
1209 off_nl = -1; /* L3 proto location dep. on cookie type */
1210 off_nl_nosnap = -1; /* no 802.2 LLC */
1231 case DLT_LINUX_LAPD:
1233 * Currently, only raw "link[N:M]" filtering is supported.
1240 bpf_error("unknown data link type %d", linktype);
1245 * Load a value relative to the beginning of the link-layer header.
1246 * The link-layer header doesn't necessarily begin at the beginning
1247 * of the packet data; there might be a variable-length prefix containing
1248 * radio information.
1250 static struct slist *
1251 gen_load_llrel(offset, size)
1254 struct slist *s, *s2;
1256 s = gen_llprefixlen();
1259 * If "s" is non-null, it has code to arrange that the X register
1260 * contains the length of the prefix preceding the link-layer
1263 * Otherwise, the length of the prefix preceding the link-layer
1264 * header is "off_ll".
1268 * There's a variable-length prefix preceding the
1269 * link-layer header. "s" points to a list of statements
1270 * that put the length of that prefix into the X register.
1271 * do an indirect load, to use the X register as an offset.
1273 s2 = new_stmt(BPF_LD|BPF_IND|size);
1278 * There is no variable-length header preceding the
1279 * link-layer header; add in off_ll, which, if there's
1280 * a fixed-length header preceding the link-layer header,
1281 * is the length of that header.
1283 s = new_stmt(BPF_LD|BPF_ABS|size);
1284 s->s.k = offset + off_ll;
1290 * Load a value relative to the beginning of the specified header.
1292 static struct slist *
1293 gen_load_a(offrel, offset, size)
1294 enum e_offrel offrel;
1297 struct slist *s, *s2;
1302 s = new_stmt(BPF_LD|BPF_ABS|size);
1307 s = gen_load_llrel(offset, size);
1311 s = gen_load_llrel(off_nl + offset, size);
1315 s = gen_load_llrel(off_nl_nosnap + offset, size);
1320 * Load the X register with the length of the IPv4 header
1321 * (plus the offset of the link-layer header, if it's
1322 * preceded by a variable-length header such as a radio
1323 * header), in bytes.
1325 s = gen_loadx_iphdrlen();
1328 * Load the item at {offset of the link-layer header} +
1329 * {offset, relative to the start of the link-layer
1330 * header, of the IPv4 header} + {length of the IPv4 header} +
1331 * {specified offset}.
1333 * (If the link-layer is variable-length, it's included
1334 * in the value in the X register, and off_ll is 0.)
1336 s2 = new_stmt(BPF_LD|BPF_IND|size);
1337 s2->s.k = off_ll + off_nl + offset;
1342 s = gen_load_llrel(off_nl + 40 + offset, size);
1353 * Generate code to load into the X register the sum of the length of
1354 * the IPv4 header and any variable-length header preceding the link-layer
1357 static struct slist *
1358 gen_loadx_iphdrlen()
1360 struct slist *s, *s2;
1362 s = gen_llprefixlen();
1365 * There's a variable-length prefix preceding the
1366 * link-layer header. "s" points to a list of statements
1367 * that put the length of that prefix into the X register.
1368 * The 4*([k]&0xf) addressing mode can't be used, as we
1369 * don't have a constant offset, so we have to load the
1370 * value in question into the A register and add to it
1371 * the value from the X register.
1373 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1376 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1379 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1384 * The A register now contains the length of the
1385 * IP header. We need to add to it the length
1386 * of the prefix preceding the link-layer
1387 * header, which is still in the X register, and
1388 * move the result into the X register.
1390 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1391 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1394 * There is no variable-length header preceding the
1395 * link-layer header; add in off_ll, which, if there's
1396 * a fixed-length header preceding the link-layer header,
1397 * is the length of that header.
1399 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1400 s->s.k = off_ll + off_nl;
1405 static struct block *
1412 s = new_stmt(BPF_LD|BPF_IMM);
1414 b = new_block(JMP(BPF_JEQ));
1420 static inline struct block *
1423 return gen_uncond(1);
1426 static inline struct block *
1429 return gen_uncond(0);
1433 * Byte-swap a 32-bit number.
1434 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1435 * big-endian platforms.)
1437 #define SWAPLONG(y) \
1438 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1441 * Generate code to match a particular packet type.
1443 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1444 * value, if <= ETHERMTU. We use that to determine whether to
1445 * match the type/length field or to check the type/length field for
1446 * a value <= ETHERMTU to see whether it's a type field and then do
1447 * the appropriate test.
1449 static struct block *
1450 gen_ether_linktype(proto)
1453 struct block *b0, *b1;
1459 case LLCSAP_NETBEUI:
1461 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1462 * so we check the DSAP and SSAP.
1464 * LLCSAP_IP checks for IP-over-802.2, rather
1465 * than IP-over-Ethernet or IP-over-SNAP.
1467 * XXX - should we check both the DSAP and the
1468 * SSAP, like this, or should we check just the
1469 * DSAP, as we do for other types <= ETHERMTU
1470 * (i.e., other SAP values)?
1472 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1474 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1475 ((proto << 8) | proto));
1483 * Ethernet_II frames, which are Ethernet
1484 * frames with a frame type of ETHERTYPE_IPX;
1486 * Ethernet_802.3 frames, which are 802.3
1487 * frames (i.e., the type/length field is
1488 * a length field, <= ETHERMTU, rather than
1489 * a type field) with the first two bytes
1490 * after the Ethernet/802.3 header being
1493 * Ethernet_802.2 frames, which are 802.3
1494 * frames with an 802.2 LLC header and
1495 * with the IPX LSAP as the DSAP in the LLC
1498 * Ethernet_SNAP frames, which are 802.3
1499 * frames with an LLC header and a SNAP
1500 * header and with an OUI of 0x000000
1501 * (encapsulated Ethernet) and a protocol
1502 * ID of ETHERTYPE_IPX in the SNAP header.
1504 * XXX - should we generate the same code both
1505 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1509 * This generates code to check both for the
1510 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1512 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1513 (bpf_int32)LLCSAP_IPX);
1514 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H,
1519 * Now we add code to check for SNAP frames with
1520 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1522 b0 = gen_snap(0x000000, ETHERTYPE_IPX, 14);
1526 * Now we generate code to check for 802.3
1527 * frames in general.
1529 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1533 * Now add the check for 802.3 frames before the
1534 * check for Ethernet_802.2 and Ethernet_802.3,
1535 * as those checks should only be done on 802.3
1536 * frames, not on Ethernet frames.
1541 * Now add the check for Ethernet_II frames, and
1542 * do that before checking for the other frame
1545 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1546 (bpf_int32)ETHERTYPE_IPX);
1550 case ETHERTYPE_ATALK:
1551 case ETHERTYPE_AARP:
1553 * EtherTalk (AppleTalk protocols on Ethernet link
1554 * layer) may use 802.2 encapsulation.
1558 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1559 * we check for an Ethernet type field less than
1560 * 1500, which means it's an 802.3 length field.
1562 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1566 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1567 * SNAP packets with an organization code of
1568 * 0x080007 (Apple, for Appletalk) and a protocol
1569 * type of ETHERTYPE_ATALK (Appletalk).
1571 * 802.2-encapsulated ETHERTYPE_AARP packets are
1572 * SNAP packets with an organization code of
1573 * 0x000000 (encapsulated Ethernet) and a protocol
1574 * type of ETHERTYPE_AARP (Appletalk ARP).
1576 if (proto == ETHERTYPE_ATALK)
1577 b1 = gen_snap(0x080007, ETHERTYPE_ATALK, 14);
1578 else /* proto == ETHERTYPE_AARP */
1579 b1 = gen_snap(0x000000, ETHERTYPE_AARP, 14);
1583 * Check for Ethernet encapsulation (Ethertalk
1584 * phase 1?); we just check for the Ethernet
1587 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1593 if (proto <= ETHERMTU) {
1595 * This is an LLC SAP value, so the frames
1596 * that match would be 802.2 frames.
1597 * Check that the frame is an 802.2 frame
1598 * (i.e., that the length/type field is
1599 * a length field, <= ETHERMTU) and
1600 * then check the DSAP.
1602 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1604 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1610 * This is an Ethernet type, so compare
1611 * the length/type field with it (if
1612 * the frame is an 802.2 frame, the length
1613 * field will be <= ETHERMTU, and, as
1614 * "proto" is > ETHERMTU, this test
1615 * will fail and the frame won't match,
1616 * which is what we want).
1618 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1625 * Generate code to match a particular packet type.
1627 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1628 * value, if <= ETHERMTU. We use that to determine whether to
1629 * match the type field or to check the type field for the special
1630 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1632 static struct block *
1633 gen_linux_sll_linktype(proto)
1636 struct block *b0, *b1;
1642 case LLCSAP_NETBEUI:
1644 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1645 * so we check the DSAP and SSAP.
1647 * LLCSAP_IP checks for IP-over-802.2, rather
1648 * than IP-over-Ethernet or IP-over-SNAP.
1650 * XXX - should we check both the DSAP and the
1651 * SSAP, like this, or should we check just the
1652 * DSAP, as we do for other types <= ETHERMTU
1653 * (i.e., other SAP values)?
1655 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1656 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1657 ((proto << 8) | proto));
1663 * Ethernet_II frames, which are Ethernet
1664 * frames with a frame type of ETHERTYPE_IPX;
1666 * Ethernet_802.3 frames, which have a frame
1667 * type of LINUX_SLL_P_802_3;
1669 * Ethernet_802.2 frames, which are 802.3
1670 * frames with an 802.2 LLC header (i.e, have
1671 * a frame type of LINUX_SLL_P_802_2) and
1672 * with the IPX LSAP as the DSAP in the LLC
1675 * Ethernet_SNAP frames, which are 802.3
1676 * frames with an LLC header and a SNAP
1677 * header and with an OUI of 0x000000
1678 * (encapsulated Ethernet) and a protocol
1679 * ID of ETHERTYPE_IPX in the SNAP header.
1681 * First, do the checks on LINUX_SLL_P_802_2
1682 * frames; generate the check for either
1683 * Ethernet_802.2 or Ethernet_SNAP frames, and
1684 * then put a check for LINUX_SLL_P_802_2 frames
1687 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1688 (bpf_int32)LLCSAP_IPX);
1689 b1 = gen_snap(0x000000, ETHERTYPE_IPX,
1692 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1696 * Now check for 802.3 frames and OR that with
1697 * the previous test.
1699 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1703 * Now add the check for Ethernet_II frames, and
1704 * do that before checking for the other frame
1707 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1708 (bpf_int32)ETHERTYPE_IPX);
1712 case ETHERTYPE_ATALK:
1713 case ETHERTYPE_AARP:
1715 * EtherTalk (AppleTalk protocols on Ethernet link
1716 * layer) may use 802.2 encapsulation.
1720 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1721 * we check for the 802.2 protocol type in the
1722 * "Ethernet type" field.
1724 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1727 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1728 * SNAP packets with an organization code of
1729 * 0x080007 (Apple, for Appletalk) and a protocol
1730 * type of ETHERTYPE_ATALK (Appletalk).
1732 * 802.2-encapsulated ETHERTYPE_AARP packets are
1733 * SNAP packets with an organization code of
1734 * 0x000000 (encapsulated Ethernet) and a protocol
1735 * type of ETHERTYPE_AARP (Appletalk ARP).
1737 if (proto == ETHERTYPE_ATALK)
1738 b1 = gen_snap(0x080007, ETHERTYPE_ATALK,
1740 else /* proto == ETHERTYPE_AARP */
1741 b1 = gen_snap(0x000000, ETHERTYPE_AARP,
1746 * Check for Ethernet encapsulation (Ethertalk
1747 * phase 1?); we just check for the Ethernet
1750 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1756 if (proto <= ETHERMTU) {
1758 * This is an LLC SAP value, so the frames
1759 * that match would be 802.2 frames.
1760 * Check for the 802.2 protocol type
1761 * in the "Ethernet type" field, and
1762 * then check the DSAP.
1764 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1766 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1772 * This is an Ethernet type, so compare
1773 * the length/type field with it (if
1774 * the frame is an 802.2 frame, the length
1775 * field will be <= ETHERMTU, and, as
1776 * "proto" is > ETHERMTU, this test
1777 * will fail and the frame won't match,
1778 * which is what we want).
1780 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1787 insert_radiotap_load_llprefixlen(b)
1790 struct slist *s1, *s2;
1793 * Prepend to the statements in this block code to load the
1794 * length of the radiotap header into the register assigned
1795 * to hold that length, if one has been assigned.
1797 if (reg_ll_size != -1) {
1799 * The 2 bytes at offsets of 2 and 3 from the beginning
1800 * of the radiotap header are the length of the radiotap
1801 * header; unfortunately, it's little-endian, so we have
1802 * to load it a byte at a time and construct the value.
1806 * Load the high-order byte, at an offset of 3, shift it
1807 * left a byte, and put the result in the X register.
1809 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1811 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1814 s2 = new_stmt(BPF_MISC|BPF_TAX);
1818 * Load the next byte, at an offset of 2, and OR the
1819 * value from the X register into it.
1821 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1824 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1828 * Now allocate a register to hold that value and store
1831 s2 = new_stmt(BPF_ST);
1832 s2->s.k = reg_ll_size;
1836 * Now move it into the X register.
1838 s2 = new_stmt(BPF_MISC|BPF_TAX);
1842 * Now append all the existing statements in this
1843 * block to these statements.
1845 sappend(s1, b->stmts);
1852 insert_load_llprefixlen(b)
1857 case DLT_IEEE802_11_RADIO:
1858 insert_radiotap_load_llprefixlen(b);
1863 static struct slist *
1864 gen_radiotap_llprefixlen(void)
1868 if (reg_ll_size == -1) {
1870 * We haven't yet assigned a register for the length
1871 * of the radiotap header; allocate one.
1873 reg_ll_size = alloc_reg();
1877 * Load the register containing the radiotap length
1878 * into the X register.
1880 s = new_stmt(BPF_LDX|BPF_MEM);
1881 s->s.k = reg_ll_size;
1886 * Generate code to compute the link-layer header length, if necessary,
1887 * putting it into the X register, and to return either a pointer to a
1888 * "struct slist" for the list of statements in that code, or NULL if
1889 * no code is necessary.
1891 static struct slist *
1892 gen_llprefixlen(void)
1896 case DLT_IEEE802_11_RADIO:
1897 return gen_radiotap_llprefixlen();
1905 * Generate code to match a particular packet type by matching the
1906 * link-layer type field or fields in the 802.2 LLC header.
1908 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1909 * value, if <= ETHERMTU.
1911 static struct block *
1915 struct block *b0, *b1, *b2;
1917 /* are we checking MPLS-encapsulated packets? */
1918 if (label_stack_depth > 0) {
1922 /* FIXME add other L3 proto IDs */
1923 return gen_mpls_linktype(Q_IP);
1925 case ETHERTYPE_IPV6:
1927 /* FIXME add other L3 proto IDs */
1928 return gen_mpls_linktype(Q_IPV6);
1931 bpf_error("unsupported protocol over mpls");
1939 return gen_ether_linktype(proto);
1947 proto = (proto << 8 | LLCSAP_ISONS);
1951 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1960 case DLT_IEEE802_11:
1961 case DLT_IEEE802_11_RADIO_AVS:
1962 case DLT_IEEE802_11_RADIO:
1963 case DLT_PRISM_HEADER:
1964 case DLT_ATM_RFC1483:
1966 case DLT_IP_OVER_FC:
1967 return gen_llc_linktype(proto);
1973 * If "is_lane" is set, check for a LANE-encapsulated
1974 * version of this protocol, otherwise check for an
1975 * LLC-encapsulated version of this protocol.
1977 * We assume LANE means Ethernet, not Token Ring.
1981 * Check that the packet doesn't begin with an
1982 * LE Control marker. (We've already generated
1985 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
1990 * Now generate an Ethernet test.
1992 b1 = gen_ether_linktype(proto);
1997 * Check for LLC encapsulation and then check the
2000 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
2001 b1 = gen_llc_linktype(proto);
2009 return gen_linux_sll_linktype(proto);
2014 case DLT_SLIP_BSDOS:
2017 * These types don't provide any type field; packets
2020 * XXX - for IPv4, check for a version number of 4, and,
2021 * for IPv6, check for a version number of 6?
2027 case ETHERTYPE_IPV6:
2029 return gen_true(); /* always true */
2032 return gen_false(); /* always false */
2039 case DLT_PPP_SERIAL:
2042 * We use Ethernet protocol types inside libpcap;
2043 * map them to the corresponding PPP protocol types.
2052 case ETHERTYPE_IPV6:
2061 case ETHERTYPE_ATALK:
2075 * I'm assuming the "Bridging PDU"s that go
2076 * over PPP are Spanning Tree Protocol
2090 * We use Ethernet protocol types inside libpcap;
2091 * map them to the corresponding PPP protocol types.
2096 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
2097 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
2099 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
2104 case ETHERTYPE_IPV6:
2114 case ETHERTYPE_ATALK:
2128 * I'm assuming the "Bridging PDU"s that go
2129 * over PPP are Spanning Tree Protocol
2145 * For DLT_NULL, the link-layer header is a 32-bit
2146 * word containing an AF_ value in *host* byte order,
2147 * and for DLT_ENC, the link-layer header begins
2148 * with a 32-bit work containing an AF_ value in
2151 * In addition, if we're reading a saved capture file,
2152 * the host byte order in the capture may not be the
2153 * same as the host byte order on this machine.
2155 * For DLT_LOOP, the link-layer header is a 32-bit
2156 * word containing an AF_ value in *network* byte order.
2158 * XXX - AF_ values may, unfortunately, be platform-
2159 * dependent; for example, FreeBSD's AF_INET6 is 24
2160 * whilst NetBSD's and OpenBSD's is 26.
2162 * This means that, when reading a capture file, just
2163 * checking for our AF_INET6 value won't work if the
2164 * capture file came from another OS.
2173 case ETHERTYPE_IPV6:
2180 * Not a type on which we support filtering.
2181 * XXX - support those that have AF_ values
2182 * #defined on this platform, at least?
2187 if (linktype == DLT_NULL || linktype == DLT_ENC) {
2189 * The AF_ value is in host byte order, but
2190 * the BPF interpreter will convert it to
2191 * network byte order.
2193 * If this is a save file, and it's from a
2194 * machine with the opposite byte order to
2195 * ours, we byte-swap the AF_ value.
2197 * Then we run it through "htonl()", and
2198 * generate code to compare against the result.
2200 if (bpf_pcap->sf.rfile != NULL &&
2201 bpf_pcap->sf.swapped)
2202 proto = SWAPLONG(proto);
2203 proto = htonl(proto);
2205 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
2209 * af field is host byte order in contrast to the rest of
2212 if (proto == ETHERTYPE_IP)
2213 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2214 BPF_B, (bpf_int32)AF_INET));
2216 else if (proto == ETHERTYPE_IPV6)
2217 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2218 BPF_B, (bpf_int32)AF_INET6));
2226 case DLT_ARCNET_LINUX:
2228 * XXX should we check for first fragment if the protocol
2237 case ETHERTYPE_IPV6:
2238 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2239 (bpf_int32)ARCTYPE_INET6));
2243 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2244 (bpf_int32)ARCTYPE_IP);
2245 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2246 (bpf_int32)ARCTYPE_IP_OLD);
2251 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2252 (bpf_int32)ARCTYPE_ARP);
2253 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2254 (bpf_int32)ARCTYPE_ARP_OLD);
2258 case ETHERTYPE_REVARP:
2259 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2260 (bpf_int32)ARCTYPE_REVARP));
2262 case ETHERTYPE_ATALK:
2263 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2264 (bpf_int32)ARCTYPE_ATALK));
2271 case ETHERTYPE_ATALK:
2281 * XXX - assumes a 2-byte Frame Relay header with
2282 * DLCI and flags. What if the address is longer?
2288 * Check for the special NLPID for IP.
2290 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
2293 case ETHERTYPE_IPV6:
2295 * Check for the special NLPID for IPv6.
2297 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
2302 * Check for several OSI protocols.
2304 * Frame Relay packets typically have an OSI
2305 * NLPID at the beginning; we check for each
2308 * What we check for is the NLPID and a frame
2309 * control field of UI, i.e. 0x03 followed
2312 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
2313 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
2314 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
2325 case DLT_JUNIPER_MFR:
2326 case DLT_JUNIPER_MLFR:
2327 case DLT_JUNIPER_MLPPP:
2328 case DLT_JUNIPER_ATM1:
2329 case DLT_JUNIPER_ATM2:
2330 case DLT_JUNIPER_PPPOE:
2331 case DLT_JUNIPER_PPPOE_ATM:
2332 case DLT_JUNIPER_GGSN:
2333 case DLT_JUNIPER_ES:
2334 case DLT_JUNIPER_MONITOR:
2335 case DLT_JUNIPER_SERVICES:
2336 case DLT_JUNIPER_ETHER:
2337 case DLT_JUNIPER_PPP:
2338 case DLT_JUNIPER_FRELAY:
2339 case DLT_JUNIPER_CHDLC:
2340 /* just lets verify the magic number for now -
2341 * on ATM we may have up to 6 different encapsulations on the wire
2342 * and need a lot of heuristics to figure out that the payload
2345 * FIXME encapsulation specific BPF_ filters
2347 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
2349 case DLT_LINUX_IRDA:
2350 bpf_error("IrDA link-layer type filtering not implemented");
2353 bpf_error("DOCSIS link-layer type filtering not implemented");
2355 case DLT_LINUX_LAPD:
2356 bpf_error("LAPD link-layer type filtering not implemented");
2360 * All the types that have no encapsulation should either be
2361 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2362 * all packets are IP packets, or should be handled in some
2363 * special case, if none of them are (if some are and some
2364 * aren't, the lack of encapsulation is a problem, as we'd
2365 * have to find some other way of determining the packet type).
2367 * Therefore, if "off_linktype" is -1, there's an error.
2369 if (off_linktype == (u_int)-1)
2373 * Any type not handled above should always have an Ethernet
2374 * type at an offset of "off_linktype". (PPP is partially
2375 * handled above - the protocol type is mapped from the
2376 * Ethernet and LLC types we use internally to the corresponding
2377 * PPP type - but the PPP type is always specified by a value
2378 * at "off_linktype", so we don't have to do the code generation
2381 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2385 * Check for an LLC SNAP packet with a given organization code and
2386 * protocol type; we check the entire contents of the 802.2 LLC and
2387 * snap headers, checking for DSAP and SSAP of SNAP and a control
2388 * field of 0x03 in the LLC header, and for the specified organization
2389 * code and protocol type in the SNAP header.
2391 static struct block *
2392 gen_snap(orgcode, ptype, offset)
2393 bpf_u_int32 orgcode;
2397 u_char snapblock[8];
2399 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
2400 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
2401 snapblock[2] = 0x03; /* control = UI */
2402 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
2403 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
2404 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
2405 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
2406 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
2407 return gen_bcmp(OR_LINK, offset, 8, snapblock);
2411 * Generate code to match a particular packet type, for link-layer types
2412 * using 802.2 LLC headers.
2414 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2415 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2417 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2418 * value, if <= ETHERMTU. We use that to determine whether to
2419 * match the DSAP or both DSAP and LSAP or to check the OUI and
2420 * protocol ID in a SNAP header.
2422 static struct block *
2423 gen_llc_linktype(proto)
2427 * XXX - handle token-ring variable-length header.
2433 case LLCSAP_NETBEUI:
2435 * XXX - should we check both the DSAP and the
2436 * SSAP, like this, or should we check just the
2437 * DSAP, as we do for other types <= ETHERMTU
2438 * (i.e., other SAP values)?
2440 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_u_int32)
2441 ((proto << 8) | proto));
2445 * XXX - are there ever SNAP frames for IPX on
2446 * non-Ethernet 802.x networks?
2448 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2449 (bpf_int32)LLCSAP_IPX);
2451 case ETHERTYPE_ATALK:
2453 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2454 * SNAP packets with an organization code of
2455 * 0x080007 (Apple, for Appletalk) and a protocol
2456 * type of ETHERTYPE_ATALK (Appletalk).
2458 * XXX - check for an organization code of
2459 * encapsulated Ethernet as well?
2461 return gen_snap(0x080007, ETHERTYPE_ATALK, off_linktype);
2465 * XXX - we don't have to check for IPX 802.3
2466 * here, but should we check for the IPX Ethertype?
2468 if (proto <= ETHERMTU) {
2470 * This is an LLC SAP value, so check
2473 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2477 * This is an Ethernet type; we assume that it's
2478 * unlikely that it'll appear in the right place
2479 * at random, and therefore check only the
2480 * location that would hold the Ethernet type
2481 * in a SNAP frame with an organization code of
2482 * 0x000000 (encapsulated Ethernet).
2484 * XXX - if we were to check for the SNAP DSAP and
2485 * LSAP, as per XXX, and were also to check for an
2486 * organization code of 0x000000 (encapsulated
2487 * Ethernet), we'd do
2489 * return gen_snap(0x000000, proto,
2492 * here; for now, we don't, as per the above.
2493 * I don't know whether it's worth the extra CPU
2494 * time to do the right check or not.
2496 return gen_cmp(OR_LINK, off_linktype+6, BPF_H,
2502 static struct block *
2503 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
2507 u_int src_off, dst_off;
2509 struct block *b0, *b1;
2523 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2524 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2530 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2531 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2538 b0 = gen_linktype(proto);
2539 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
2545 static struct block *
2546 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
2547 struct in6_addr *addr;
2548 struct in6_addr *mask;
2550 u_int src_off, dst_off;
2552 struct block *b0, *b1;
2567 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2568 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2574 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2575 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2582 /* this order is important */
2583 a = (u_int32_t *)addr;
2584 m = (u_int32_t *)mask;
2585 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
2586 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
2588 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
2590 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
2592 b0 = gen_linktype(proto);
2598 static struct block *
2599 gen_ehostop(eaddr, dir)
2600 register const u_char *eaddr;
2603 register struct block *b0, *b1;
2607 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
2610 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
2613 b0 = gen_ehostop(eaddr, Q_SRC);
2614 b1 = gen_ehostop(eaddr, Q_DST);
2620 b0 = gen_ehostop(eaddr, Q_SRC);
2621 b1 = gen_ehostop(eaddr, Q_DST);
2630 * Like gen_ehostop, but for DLT_FDDI
2632 static struct block *
2633 gen_fhostop(eaddr, dir)
2634 register const u_char *eaddr;
2637 struct block *b0, *b1;
2642 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
2644 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
2649 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
2651 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
2655 b0 = gen_fhostop(eaddr, Q_SRC);
2656 b1 = gen_fhostop(eaddr, Q_DST);
2662 b0 = gen_fhostop(eaddr, Q_SRC);
2663 b1 = gen_fhostop(eaddr, Q_DST);
2672 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2674 static struct block *
2675 gen_thostop(eaddr, dir)
2676 register const u_char *eaddr;
2679 register struct block *b0, *b1;
2683 return gen_bcmp(OR_LINK, 8, 6, eaddr);
2686 return gen_bcmp(OR_LINK, 2, 6, eaddr);
2689 b0 = gen_thostop(eaddr, Q_SRC);
2690 b1 = gen_thostop(eaddr, Q_DST);
2696 b0 = gen_thostop(eaddr, Q_SRC);
2697 b1 = gen_thostop(eaddr, Q_DST);
2706 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2708 static struct block *
2709 gen_wlanhostop(eaddr, dir)
2710 register const u_char *eaddr;
2713 register struct block *b0, *b1, *b2;
2714 register struct slist *s;
2721 * For control frames, there is no SA.
2723 * For management frames, SA is at an
2724 * offset of 10 from the beginning of
2727 * For data frames, SA is at an offset
2728 * of 10 from the beginning of the packet
2729 * if From DS is clear, at an offset of
2730 * 16 from the beginning of the packet
2731 * if From DS is set and To DS is clear,
2732 * and an offset of 24 from the beginning
2733 * of the packet if From DS is set and To DS
2738 * Generate the tests to be done for data frames
2741 * First, check for To DS set, i.e. check "link[1] & 0x01".
2743 s = gen_load_a(OR_LINK, 1, BPF_B);
2744 b1 = new_block(JMP(BPF_JSET));
2745 b1->s.k = 0x01; /* To DS */
2749 * If To DS is set, the SA is at 24.
2751 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
2755 * Now, check for To DS not set, i.e. check
2756 * "!(link[1] & 0x01)".
2758 s = gen_load_a(OR_LINK, 1, BPF_B);
2759 b2 = new_block(JMP(BPF_JSET));
2760 b2->s.k = 0x01; /* To DS */
2765 * If To DS is not set, the SA is at 16.
2767 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
2771 * Now OR together the last two checks. That gives
2772 * the complete set of checks for data frames with
2778 * Now check for From DS being set, and AND that with
2779 * the ORed-together checks.
2781 s = gen_load_a(OR_LINK, 1, BPF_B);
2782 b1 = new_block(JMP(BPF_JSET));
2783 b1->s.k = 0x02; /* From DS */
2788 * Now check for data frames with From DS not set.
2790 s = gen_load_a(OR_LINK, 1, BPF_B);
2791 b2 = new_block(JMP(BPF_JSET));
2792 b2->s.k = 0x02; /* From DS */
2797 * If From DS isn't set, the SA is at 10.
2799 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
2803 * Now OR together the checks for data frames with
2804 * From DS not set and for data frames with From DS
2805 * set; that gives the checks done for data frames.
2810 * Now check for a data frame.
2811 * I.e, check "link[0] & 0x08".
2813 gen_load_a(OR_LINK, 0, BPF_B);
2814 b1 = new_block(JMP(BPF_JSET));
2819 * AND that with the checks done for data frames.
2824 * If the high-order bit of the type value is 0, this
2825 * is a management frame.
2826 * I.e, check "!(link[0] & 0x08)".
2828 s = gen_load_a(OR_LINK, 0, BPF_B);
2829 b2 = new_block(JMP(BPF_JSET));
2835 * For management frames, the SA is at 10.
2837 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
2841 * OR that with the checks done for data frames.
2842 * That gives the checks done for management and
2848 * If the low-order bit of the type value is 1,
2849 * this is either a control frame or a frame
2850 * with a reserved type, and thus not a
2853 * I.e., check "!(link[0] & 0x04)".
2855 s = gen_load_a(OR_LINK, 0, BPF_B);
2856 b1 = new_block(JMP(BPF_JSET));
2862 * AND that with the checks for data and management
2872 * For control frames, there is no DA.
2874 * For management frames, DA is at an
2875 * offset of 4 from the beginning of
2878 * For data frames, DA is at an offset
2879 * of 4 from the beginning of the packet
2880 * if To DS is clear and at an offset of
2881 * 16 from the beginning of the packet
2886 * Generate the tests to be done for data frames.
2888 * First, check for To DS set, i.e. "link[1] & 0x01".
2890 s = gen_load_a(OR_LINK, 1, BPF_B);
2891 b1 = new_block(JMP(BPF_JSET));
2892 b1->s.k = 0x01; /* To DS */
2896 * If To DS is set, the DA is at 16.
2898 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
2902 * Now, check for To DS not set, i.e. check
2903 * "!(link[1] & 0x01)".
2905 s = gen_load_a(OR_LINK, 1, BPF_B);
2906 b2 = new_block(JMP(BPF_JSET));
2907 b2->s.k = 0x01; /* To DS */
2912 * If To DS is not set, the DA is at 4.
2914 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
2918 * Now OR together the last two checks. That gives
2919 * the complete set of checks for data frames.
2924 * Now check for a data frame.
2925 * I.e, check "link[0] & 0x08".
2927 s = gen_load_a(OR_LINK, 0, BPF_B);
2928 b1 = new_block(JMP(BPF_JSET));
2933 * AND that with the checks done for data frames.
2938 * If the high-order bit of the type value is 0, this
2939 * is a management frame.
2940 * I.e, check "!(link[0] & 0x08)".
2942 s = gen_load_a(OR_LINK, 0, BPF_B);
2943 b2 = new_block(JMP(BPF_JSET));
2949 * For management frames, the DA is at 4.
2951 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
2955 * OR that with the checks done for data frames.
2956 * That gives the checks done for management and
2962 * If the low-order bit of the type value is 1,
2963 * this is either a control frame or a frame
2964 * with a reserved type, and thus not a
2967 * I.e., check "!(link[0] & 0x04)".
2969 s = gen_load_a(OR_LINK, 0, BPF_B);
2970 b1 = new_block(JMP(BPF_JSET));
2976 * AND that with the checks for data and management
2983 b0 = gen_wlanhostop(eaddr, Q_SRC);
2984 b1 = gen_wlanhostop(eaddr, Q_DST);
2990 b0 = gen_wlanhostop(eaddr, Q_SRC);
2991 b1 = gen_wlanhostop(eaddr, Q_DST);
3000 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3001 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3002 * as the RFC states.)
3004 static struct block *
3005 gen_ipfchostop(eaddr, dir)
3006 register const u_char *eaddr;
3009 register struct block *b0, *b1;
3013 return gen_bcmp(OR_LINK, 10, 6, eaddr);
3016 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3019 b0 = gen_ipfchostop(eaddr, Q_SRC);
3020 b1 = gen_ipfchostop(eaddr, Q_DST);
3026 b0 = gen_ipfchostop(eaddr, Q_SRC);
3027 b1 = gen_ipfchostop(eaddr, Q_DST);
3036 * This is quite tricky because there may be pad bytes in front of the
3037 * DECNET header, and then there are two possible data packet formats that
3038 * carry both src and dst addresses, plus 5 packet types in a format that
3039 * carries only the src node, plus 2 types that use a different format and
3040 * also carry just the src node.
3044 * Instead of doing those all right, we just look for data packets with
3045 * 0 or 1 bytes of padding. If you want to look at other packets, that
3046 * will require a lot more hacking.
3048 * To add support for filtering on DECNET "areas" (network numbers)
3049 * one would want to add a "mask" argument to this routine. That would
3050 * make the filter even more inefficient, although one could be clever
3051 * and not generate masking instructions if the mask is 0xFFFF.
3053 static struct block *
3054 gen_dnhostop(addr, dir)
3058 struct block *b0, *b1, *b2, *tmp;
3059 u_int offset_lh; /* offset if long header is received */
3060 u_int offset_sh; /* offset if short header is received */
3065 offset_sh = 1; /* follows flags */
3066 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
3070 offset_sh = 3; /* follows flags, dstnode */
3071 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3075 /* Inefficient because we do our Calvinball dance twice */
3076 b0 = gen_dnhostop(addr, Q_SRC);
3077 b1 = gen_dnhostop(addr, Q_DST);
3083 /* Inefficient because we do our Calvinball dance twice */
3084 b0 = gen_dnhostop(addr, Q_SRC);
3085 b1 = gen_dnhostop(addr, Q_DST);
3090 bpf_error("ISO host filtering not implemented");
3095 b0 = gen_linktype(ETHERTYPE_DN);
3096 /* Check for pad = 1, long header case */
3097 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3098 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
3099 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
3100 BPF_H, (bpf_int32)ntohs(addr));
3102 /* Check for pad = 0, long header case */
3103 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
3104 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs(addr));
3107 /* Check for pad = 1, short header case */
3108 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3109 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
3110 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
3113 /* Check for pad = 0, short header case */
3114 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
3115 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
3119 /* Combine with test for linktype */
3125 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3126 * test the bottom-of-stack bit, and then check the version number
3127 * field in the IP header.
3129 static struct block *
3130 gen_mpls_linktype(proto)
3133 struct block *b0, *b1;
3138 /* match the bottom-of-stack bit */
3139 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3140 /* match the IPv4 version number */
3141 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
3146 /* match the bottom-of-stack bit */
3147 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3148 /* match the IPv4 version number */
3149 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
3158 static struct block *
3159 gen_host(addr, mask, proto, dir, type)
3166 struct block *b0, *b1;
3167 const char *typestr;
3177 b0 = gen_host(addr, mask, Q_IP, dir, type);
3179 * Only check for non-IPv4 addresses if we're not
3180 * checking MPLS-encapsulated packets.
3182 if (label_stack_depth == 0) {
3183 b1 = gen_host(addr, mask, Q_ARP, dir, type);
3185 b0 = gen_host(addr, mask, Q_RARP, dir, type);
3191 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
3194 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
3197 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
3200 bpf_error("'tcp' modifier applied to %s", typestr);
3203 bpf_error("'sctp' modifier applied to %s", typestr);
3206 bpf_error("'udp' modifier applied to %s", typestr);
3209 bpf_error("'icmp' modifier applied to %s", typestr);
3212 bpf_error("'igmp' modifier applied to %s", typestr);
3215 bpf_error("'igrp' modifier applied to %s", typestr);
3218 bpf_error("'pim' modifier applied to %s", typestr);
3221 bpf_error("'vrrp' modifier applied to %s", typestr);
3224 bpf_error("ATALK host filtering not implemented");
3227 bpf_error("AARP host filtering not implemented");
3230 return gen_dnhostop(addr, dir);
3233 bpf_error("SCA host filtering not implemented");
3236 bpf_error("LAT host filtering not implemented");
3239 bpf_error("MOPDL host filtering not implemented");
3242 bpf_error("MOPRC host filtering not implemented");
3246 bpf_error("'ip6' modifier applied to ip host");
3249 bpf_error("'icmp6' modifier applied to %s", typestr);
3253 bpf_error("'ah' modifier applied to %s", typestr);
3256 bpf_error("'esp' modifier applied to %s", typestr);
3259 bpf_error("ISO host filtering not implemented");
3262 bpf_error("'esis' modifier applied to %s", typestr);
3265 bpf_error("'isis' modifier applied to %s", typestr);
3268 bpf_error("'clnp' modifier applied to %s", typestr);
3271 bpf_error("'stp' modifier applied to %s", typestr);
3274 bpf_error("IPX host filtering not implemented");
3277 bpf_error("'netbeui' modifier applied to %s", typestr);
3280 bpf_error("'radio' modifier applied to %s", typestr);
3289 static struct block *
3290 gen_host6(addr, mask, proto, dir, type)
3291 struct in6_addr *addr;
3292 struct in6_addr *mask;
3297 const char *typestr;
3307 return gen_host6(addr, mask, Q_IPV6, dir, type);
3310 bpf_error("'ip' modifier applied to ip6 %s", typestr);
3313 bpf_error("'rarp' modifier applied to ip6 %s", typestr);
3316 bpf_error("'arp' modifier applied to ip6 %s", typestr);
3319 bpf_error("'sctp' modifier applied to %s", typestr);
3322 bpf_error("'tcp' modifier applied to %s", typestr);
3325 bpf_error("'udp' modifier applied to %s", typestr);
3328 bpf_error("'icmp' modifier applied to %s", typestr);
3331 bpf_error("'igmp' modifier applied to %s", typestr);
3334 bpf_error("'igrp' modifier applied to %s", typestr);
3337 bpf_error("'pim' modifier applied to %s", typestr);
3340 bpf_error("'vrrp' modifier applied to %s", typestr);
3343 bpf_error("ATALK host filtering not implemented");
3346 bpf_error("AARP host filtering not implemented");
3349 bpf_error("'decnet' modifier applied to ip6 %s", typestr);
3352 bpf_error("SCA host filtering not implemented");
3355 bpf_error("LAT host filtering not implemented");
3358 bpf_error("MOPDL host filtering not implemented");
3361 bpf_error("MOPRC host filtering not implemented");
3364 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
3367 bpf_error("'icmp6' modifier applied to %s", typestr);
3370 bpf_error("'ah' modifier applied to %s", typestr);
3373 bpf_error("'esp' modifier applied to %s", typestr);
3376 bpf_error("ISO host filtering not implemented");
3379 bpf_error("'esis' modifier applied to %s", typestr);
3382 bpf_error("'isis' modifier applied to %s", typestr);
3385 bpf_error("'clnp' modifier applied to %s", typestr);
3388 bpf_error("'stp' modifier applied to %s", typestr);
3391 bpf_error("IPX host filtering not implemented");
3394 bpf_error("'netbeui' modifier applied to %s", typestr);
3397 bpf_error("'radio' modifier applied to %s", typestr);
3407 static struct block *
3408 gen_gateway(eaddr, alist, proto, dir)
3409 const u_char *eaddr;
3410 bpf_u_int32 **alist;
3414 struct block *b0, *b1, *tmp;
3417 bpf_error("direction applied to 'gateway'");
3426 b0 = gen_ehostop(eaddr, Q_OR);
3429 b0 = gen_fhostop(eaddr, Q_OR);
3432 b0 = gen_thostop(eaddr, Q_OR);
3434 case DLT_IEEE802_11:
3435 case DLT_IEEE802_11_RADIO_AVS:
3436 case DLT_IEEE802_11_RADIO:
3437 case DLT_PRISM_HEADER:
3438 b0 = gen_wlanhostop(eaddr, Q_OR);
3443 * Check that the packet doesn't begin with an
3444 * LE Control marker. (We've already generated
3447 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3452 * Now check the MAC address.
3454 b0 = gen_ehostop(eaddr, Q_OR);
3458 case DLT_IP_OVER_FC:
3459 b0 = gen_ipfchostop(eaddr, Q_OR);
3463 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3465 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST);
3467 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR,
3476 bpf_error("illegal modifier of 'gateway'");
3482 gen_proto_abbrev(proto)
3491 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
3493 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
3499 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
3501 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
3507 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
3509 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
3515 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
3518 #ifndef IPPROTO_IGMP
3519 #define IPPROTO_IGMP 2
3523 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
3526 #ifndef IPPROTO_IGRP
3527 #define IPPROTO_IGRP 9
3530 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
3534 #define IPPROTO_PIM 103
3538 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
3540 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
3545 #ifndef IPPROTO_VRRP
3546 #define IPPROTO_VRRP 112
3550 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
3554 b1 = gen_linktype(ETHERTYPE_IP);
3558 b1 = gen_linktype(ETHERTYPE_ARP);
3562 b1 = gen_linktype(ETHERTYPE_REVARP);
3566 bpf_error("link layer applied in wrong context");
3569 b1 = gen_linktype(ETHERTYPE_ATALK);
3573 b1 = gen_linktype(ETHERTYPE_AARP);
3577 b1 = gen_linktype(ETHERTYPE_DN);
3581 b1 = gen_linktype(ETHERTYPE_SCA);
3585 b1 = gen_linktype(ETHERTYPE_LAT);
3589 b1 = gen_linktype(ETHERTYPE_MOPDL);
3593 b1 = gen_linktype(ETHERTYPE_MOPRC);
3598 b1 = gen_linktype(ETHERTYPE_IPV6);
3601 #ifndef IPPROTO_ICMPV6
3602 #define IPPROTO_ICMPV6 58
3605 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
3610 #define IPPROTO_AH 51
3613 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
3615 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
3621 #define IPPROTO_ESP 50
3624 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
3626 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
3632 b1 = gen_linktype(LLCSAP_ISONS);
3636 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
3640 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
3643 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
3644 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3645 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3647 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3649 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3651 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3655 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
3656 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3657 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3659 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3661 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3663 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3667 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
3668 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3669 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3671 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
3676 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3677 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3682 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3683 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3685 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3687 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3692 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3693 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3698 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3699 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3704 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
3708 b1 = gen_linktype(LLCSAP_8021D);
3712 b1 = gen_linktype(LLCSAP_IPX);
3716 b1 = gen_linktype(LLCSAP_NETBEUI);
3720 bpf_error("'radio' is not a valid protocol type");
3728 static struct block *
3735 s = gen_load_a(OR_NET, 6, BPF_H);
3736 b = new_block(JMP(BPF_JSET));
3745 * Generate a comparison to a port value in the transport-layer header
3746 * at the specified offset from the beginning of that header.
3748 * XXX - this handles a variable-length prefix preceding the link-layer
3749 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3750 * variable-length link-layer headers (such as Token Ring or 802.11
3753 static struct block *
3754 gen_portatom(off, v)
3758 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
3762 static struct block *
3763 gen_portatom6(off, v)
3767 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
3772 gen_portop(port, proto, dir)
3773 int port, proto, dir;
3775 struct block *b0, *b1, *tmp;
3777 /* ip proto 'proto' */
3778 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
3784 b1 = gen_portatom(0, (bpf_int32)port);
3788 b1 = gen_portatom(2, (bpf_int32)port);
3793 tmp = gen_portatom(0, (bpf_int32)port);
3794 b1 = gen_portatom(2, (bpf_int32)port);
3799 tmp = gen_portatom(0, (bpf_int32)port);
3800 b1 = gen_portatom(2, (bpf_int32)port);
3812 static struct block *
3813 gen_port(port, ip_proto, dir)
3818 struct block *b0, *b1, *tmp;
3823 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3824 * not LLC encapsulation with LLCSAP_IP.
3826 * For IEEE 802 networks - which includes 802.5 token ring
3827 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3828 * says that SNAP encapsulation is used, not LLC encapsulation
3831 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3832 * RFC 2225 say that SNAP encapsulation is used, not LLC
3833 * encapsulation with LLCSAP_IP.
3835 * So we always check for ETHERTYPE_IP.
3837 b0 = gen_linktype(ETHERTYPE_IP);
3843 b1 = gen_portop(port, ip_proto, dir);
3847 tmp = gen_portop(port, IPPROTO_TCP, dir);
3848 b1 = gen_portop(port, IPPROTO_UDP, dir);
3850 tmp = gen_portop(port, IPPROTO_SCTP, dir);
3863 gen_portop6(port, proto, dir)
3864 int port, proto, dir;
3866 struct block *b0, *b1, *tmp;
3868 /* ip6 proto 'proto' */
3869 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
3873 b1 = gen_portatom6(0, (bpf_int32)port);
3877 b1 = gen_portatom6(2, (bpf_int32)port);
3882 tmp = gen_portatom6(0, (bpf_int32)port);
3883 b1 = gen_portatom6(2, (bpf_int32)port);
3888 tmp = gen_portatom6(0, (bpf_int32)port);
3889 b1 = gen_portatom6(2, (bpf_int32)port);
3901 static struct block *
3902 gen_port6(port, ip_proto, dir)
3907 struct block *b0, *b1, *tmp;
3909 /* link proto ip6 */
3910 b0 = gen_linktype(ETHERTYPE_IPV6);
3916 b1 = gen_portop6(port, ip_proto, dir);
3920 tmp = gen_portop6(port, IPPROTO_TCP, dir);
3921 b1 = gen_portop6(port, IPPROTO_UDP, dir);
3923 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
3935 /* gen_portrange code */
3936 static struct block *
3937 gen_portrangeatom(off, v1, v2)
3941 struct block *b1, *b2;
3945 * Reverse the order of the ports, so v1 is the lower one.
3954 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
3955 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
3963 gen_portrangeop(port1, port2, proto, dir)
3968 struct block *b0, *b1, *tmp;
3970 /* ip proto 'proto' */
3971 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
3977 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3981 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
3986 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3987 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
3992 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3993 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4005 static struct block *
4006 gen_portrange(port1, port2, ip_proto, dir)
4011 struct block *b0, *b1, *tmp;
4014 b0 = gen_linktype(ETHERTYPE_IP);
4020 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
4024 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
4025 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
4027 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
4039 static struct block *
4040 gen_portrangeatom6(off, v1, v2)
4044 struct block *b1, *b2;
4048 * Reverse the order of the ports, so v1 is the lower one.
4057 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
4058 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
4066 gen_portrangeop6(port1, port2, proto, dir)
4071 struct block *b0, *b1, *tmp;
4073 /* ip6 proto 'proto' */
4074 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
4078 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4082 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4087 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4088 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4093 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4094 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4106 static struct block *
4107 gen_portrange6(port1, port2, ip_proto, dir)
4112 struct block *b0, *b1, *tmp;
4114 /* link proto ip6 */
4115 b0 = gen_linktype(ETHERTYPE_IPV6);
4121 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
4125 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
4126 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
4128 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
4141 lookup_proto(name, proto)
4142 register const char *name;
4152 v = pcap_nametoproto(name);
4153 if (v == PROTO_UNDEF)
4154 bpf_error("unknown ip proto '%s'", name);
4158 /* XXX should look up h/w protocol type based on linktype */
4159 v = pcap_nametoeproto(name);
4160 if (v == PROTO_UNDEF) {
4161 v = pcap_nametollc(name);
4162 if (v == PROTO_UNDEF)
4163 bpf_error("unknown ether proto '%s'", name);
4168 if (strcmp(name, "esis") == 0)
4170 else if (strcmp(name, "isis") == 0)
4172 else if (strcmp(name, "clnp") == 0)
4175 bpf_error("unknown osi proto '%s'", name);
4195 static struct block *
4196 gen_protochain(v, proto, dir)
4201 #ifdef NO_PROTOCHAIN
4202 return gen_proto(v, proto, dir);
4204 struct block *b0, *b;
4205 struct slist *s[100];
4206 int fix2, fix3, fix4, fix5;
4207 int ahcheck, again, end;
4209 int reg2 = alloc_reg();
4211 memset(s, 0, sizeof(s));
4212 fix2 = fix3 = fix4 = fix5 = 0;
4219 b0 = gen_protochain(v, Q_IP, dir);
4220 b = gen_protochain(v, Q_IPV6, dir);
4224 bpf_error("bad protocol applied for 'protochain'");
4229 * We don't handle variable-length radiotap here headers yet.
4230 * We might want to add BPF instructions to do the protochain
4231 * work, to simplify that and, on platforms that have a BPF
4232 * interpreter with the new instructions, let the filtering
4233 * be done in the kernel. (We already require a modified BPF
4234 * engine to do the protochain stuff, to support backward
4235 * branches, and backward branch support is unlikely to appear
4236 * in kernel BPF engines.)
4238 if (linktype == DLT_IEEE802_11_RADIO)
4239 bpf_error("'protochain' not supported with radiotap headers");
4241 no_optimize = 1; /*this code is not compatible with optimzer yet */
4244 * s[0] is a dummy entry to protect other BPF insn from damage
4245 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4246 * hard to find interdependency made by jump table fixup.
4249 s[i] = new_stmt(0); /*dummy*/
4254 b0 = gen_linktype(ETHERTYPE_IP);
4257 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4258 s[i]->s.k = off_ll + off_nl + 9;
4260 /* X = ip->ip_hl << 2 */
4261 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
4262 s[i]->s.k = off_ll + off_nl;
4267 b0 = gen_linktype(ETHERTYPE_IPV6);
4269 /* A = ip6->ip_nxt */
4270 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4271 s[i]->s.k = off_ll + off_nl + 6;
4273 /* X = sizeof(struct ip6_hdr) */
4274 s[i] = new_stmt(BPF_LDX|BPF_IMM);
4280 bpf_error("unsupported proto to gen_protochain");
4284 /* again: if (A == v) goto end; else fall through; */
4286 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4288 s[i]->s.jt = NULL; /*later*/
4289 s[i]->s.jf = NULL; /*update in next stmt*/
4293 #ifndef IPPROTO_NONE
4294 #define IPPROTO_NONE 59
4296 /* if (A == IPPROTO_NONE) goto end */
4297 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4298 s[i]->s.jt = NULL; /*later*/
4299 s[i]->s.jf = NULL; /*update in next stmt*/
4300 s[i]->s.k = IPPROTO_NONE;
4301 s[fix5]->s.jf = s[i];
4306 if (proto == Q_IPV6) {
4307 int v6start, v6end, v6advance, j;
4310 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4311 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4312 s[i]->s.jt = NULL; /*later*/
4313 s[i]->s.jf = NULL; /*update in next stmt*/
4314 s[i]->s.k = IPPROTO_HOPOPTS;
4315 s[fix2]->s.jf = s[i];
4317 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4318 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4319 s[i]->s.jt = NULL; /*later*/
4320 s[i]->s.jf = NULL; /*update in next stmt*/
4321 s[i]->s.k = IPPROTO_DSTOPTS;
4323 /* if (A == IPPROTO_ROUTING) goto v6advance */
4324 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4325 s[i]->s.jt = NULL; /*later*/
4326 s[i]->s.jf = NULL; /*update in next stmt*/
4327 s[i]->s.k = IPPROTO_ROUTING;
4329 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4330 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4331 s[i]->s.jt = NULL; /*later*/
4332 s[i]->s.jf = NULL; /*later*/
4333 s[i]->s.k = IPPROTO_FRAGMENT;
4344 * X = X + (P[X + 1] + 1) * 8;
4347 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4349 /* A = P[X + packet head] */
4350 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4351 s[i]->s.k = off_ll + off_nl;
4354 s[i] = new_stmt(BPF_ST);
4358 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4361 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4365 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4367 /* A = P[X + packet head]; */
4368 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4369 s[i]->s.k = off_ll + off_nl;
4372 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4376 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4380 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4383 s[i] = new_stmt(BPF_LD|BPF_MEM);
4387 /* goto again; (must use BPF_JA for backward jump) */
4388 s[i] = new_stmt(BPF_JMP|BPF_JA);
4389 s[i]->s.k = again - i - 1;
4390 s[i - 1]->s.jf = s[i];
4394 for (j = v6start; j <= v6end; j++)
4395 s[j]->s.jt = s[v6advance];
4400 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4402 s[fix2]->s.jf = s[i];
4408 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4409 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4410 s[i]->s.jt = NULL; /*later*/
4411 s[i]->s.jf = NULL; /*later*/
4412 s[i]->s.k = IPPROTO_AH;
4414 s[fix3]->s.jf = s[ahcheck];
4421 * X = X + (P[X + 1] + 2) * 4;
4424 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4426 /* A = P[X + packet head]; */
4427 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4428 s[i]->s.k = off_ll + off_nl;
4431 s[i] = new_stmt(BPF_ST);
4435 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4438 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4442 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4444 /* A = P[X + packet head] */
4445 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4446 s[i]->s.k = off_ll + off_nl;
4449 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4453 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4457 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4460 s[i] = new_stmt(BPF_LD|BPF_MEM);
4464 /* goto again; (must use BPF_JA for backward jump) */
4465 s[i] = new_stmt(BPF_JMP|BPF_JA);
4466 s[i]->s.k = again - i - 1;
4471 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4473 s[fix2]->s.jt = s[end];
4474 s[fix4]->s.jf = s[end];
4475 s[fix5]->s.jt = s[end];
4482 for (i = 0; i < max - 1; i++)
4483 s[i]->next = s[i + 1];
4484 s[max - 1]->next = NULL;
4489 b = new_block(JMP(BPF_JEQ));
4490 b->stmts = s[1]; /*remember, s[0] is dummy*/
4501 * Generate code that checks whether the packet is a packet for protocol
4502 * <proto> and whether the type field in that protocol's header has
4503 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4504 * IP packet and checks the protocol number in the IP header against <v>.
4506 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4507 * against Q_IP and Q_IPV6.
4509 static struct block *
4510 gen_proto(v, proto, dir)
4515 struct block *b0, *b1;
4517 if (dir != Q_DEFAULT)
4518 bpf_error("direction applied to 'proto'");
4523 b0 = gen_proto(v, Q_IP, dir);
4524 b1 = gen_proto(v, Q_IPV6, dir);
4532 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4533 * not LLC encapsulation with LLCSAP_IP.
4535 * For IEEE 802 networks - which includes 802.5 token ring
4536 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4537 * says that SNAP encapsulation is used, not LLC encapsulation
4540 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4541 * RFC 2225 say that SNAP encapsulation is used, not LLC
4542 * encapsulation with LLCSAP_IP.
4544 * So we always check for ETHERTYPE_IP.
4547 b0 = gen_linktype(ETHERTYPE_IP);
4549 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
4551 b1 = gen_protochain(v, Q_IP);
4561 * Frame Relay packets typically have an OSI
4562 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4563 * generates code to check for all the OSI
4564 * NLPIDs, so calling it and then adding a check
4565 * for the particular NLPID for which we're
4566 * looking is bogus, as we can just check for
4569 * What we check for is the NLPID and a frame
4570 * control field value of UI, i.e. 0x03 followed
4573 * XXX - assumes a 2-byte Frame Relay header with
4574 * DLCI and flags. What if the address is longer?
4576 * XXX - what about SNAP-encapsulated frames?
4578 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
4584 * Cisco uses an Ethertype lookalike - for OSI,
4587 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
4588 /* OSI in C-HDLC is stuffed with a fudge byte */
4589 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
4594 b0 = gen_linktype(LLCSAP_ISONS);
4595 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
4601 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4603 * 4 is the offset of the PDU type relative to the IS-IS
4606 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
4611 bpf_error("arp does not encapsulate another protocol");
4615 bpf_error("rarp does not encapsulate another protocol");
4619 bpf_error("atalk encapsulation is not specifiable");
4623 bpf_error("decnet encapsulation is not specifiable");
4627 bpf_error("sca does not encapsulate another protocol");
4631 bpf_error("lat does not encapsulate another protocol");
4635 bpf_error("moprc does not encapsulate another protocol");
4639 bpf_error("mopdl does not encapsulate another protocol");
4643 return gen_linktype(v);
4646 bpf_error("'udp proto' is bogus");
4650 bpf_error("'tcp proto' is bogus");
4654 bpf_error("'sctp proto' is bogus");
4658 bpf_error("'icmp proto' is bogus");
4662 bpf_error("'igmp proto' is bogus");
4666 bpf_error("'igrp proto' is bogus");
4670 bpf_error("'pim proto' is bogus");
4674 bpf_error("'vrrp proto' is bogus");
4679 b0 = gen_linktype(ETHERTYPE_IPV6);
4681 b1 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
4683 b1 = gen_protochain(v, Q_IPV6);
4689 bpf_error("'icmp6 proto' is bogus");
4693 bpf_error("'ah proto' is bogus");
4696 bpf_error("'ah proto' is bogus");
4699 bpf_error("'stp proto' is bogus");
4702 bpf_error("'ipx proto' is bogus");
4705 bpf_error("'netbeui proto' is bogus");
4708 bpf_error("'radio proto' is bogus");
4719 register const char *name;
4722 int proto = q.proto;
4726 bpf_u_int32 mask, addr;
4728 bpf_u_int32 **alist;
4731 struct sockaddr_in *sin;
4732 struct sockaddr_in6 *sin6;
4733 struct addrinfo *res, *res0;
4734 struct in6_addr mask128;
4736 struct block *b, *tmp;
4737 int port, real_proto;
4743 addr = pcap_nametonetaddr(name);
4745 bpf_error("unknown network '%s'", name);
4746 /* Left justify network addr and calculate its network mask */
4748 while (addr && (addr & 0xff000000) == 0) {
4752 return gen_host(addr, mask, proto, dir, q.addr);
4756 if (proto == Q_LINK) {
4760 eaddr = pcap_ether_hostton(name);
4763 "unknown ether host '%s'", name);
4764 b = gen_ehostop(eaddr, dir);
4769 eaddr = pcap_ether_hostton(name);
4772 "unknown FDDI host '%s'", name);
4773 b = gen_fhostop(eaddr, dir);
4778 eaddr = pcap_ether_hostton(name);
4781 "unknown token ring host '%s'", name);
4782 b = gen_thostop(eaddr, dir);
4786 case DLT_IEEE802_11:
4787 case DLT_IEEE802_11_RADIO_AVS:
4788 case DLT_IEEE802_11_RADIO:
4789 case DLT_PRISM_HEADER:
4790 eaddr = pcap_ether_hostton(name);
4793 "unknown 802.11 host '%s'", name);
4794 b = gen_wlanhostop(eaddr, dir);
4798 case DLT_IP_OVER_FC:
4799 eaddr = pcap_ether_hostton(name);
4802 "unknown Fibre Channel host '%s'", name);
4803 b = gen_ipfchostop(eaddr, dir);
4812 * Check that the packet doesn't begin
4813 * with an LE Control marker. (We've
4814 * already generated a test for LANE.)
4816 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
4820 eaddr = pcap_ether_hostton(name);
4823 "unknown ether host '%s'", name);
4824 b = gen_ehostop(eaddr, dir);
4830 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4831 } else if (proto == Q_DECNET) {
4832 unsigned short dn_addr = __pcap_nametodnaddr(name);
4834 * I don't think DECNET hosts can be multihomed, so
4835 * there is no need to build up a list of addresses
4837 return (gen_host(dn_addr, 0, proto, dir, q.addr));
4840 alist = pcap_nametoaddr(name);
4841 if (alist == NULL || *alist == NULL)
4842 bpf_error("unknown host '%s'", name);
4844 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
4846 b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
4848 tmp = gen_host(**alist++, 0xffffffff,
4849 tproto, dir, q.addr);
4855 memset(&mask128, 0xff, sizeof(mask128));
4856 res0 = res = pcap_nametoaddrinfo(name);
4858 bpf_error("unknown host '%s'", name);
4860 tproto = tproto6 = proto;
4861 if (off_linktype == -1 && tproto == Q_DEFAULT) {
4865 for (res = res0; res; res = res->ai_next) {
4866 switch (res->ai_family) {
4868 if (tproto == Q_IPV6)
4871 sin = (struct sockaddr_in *)
4873 tmp = gen_host(ntohl(sin->sin_addr.s_addr),
4874 0xffffffff, tproto, dir, q.addr);
4877 if (tproto6 == Q_IP)
4880 sin6 = (struct sockaddr_in6 *)
4882 tmp = gen_host6(&sin6->sin6_addr,
4883 &mask128, tproto6, dir, q.addr);
4894 bpf_error("unknown host '%s'%s", name,
4895 (proto == Q_DEFAULT)
4897 : " for specified address family");
4904 if (proto != Q_DEFAULT &&
4905 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
4906 bpf_error("illegal qualifier of 'port'");
4907 if (pcap_nametoport(name, &port, &real_proto) == 0)
4908 bpf_error("unknown port '%s'", name);
4909 if (proto == Q_UDP) {
4910 if (real_proto == IPPROTO_TCP)
4911 bpf_error("port '%s' is tcp", name);
4912 else if (real_proto == IPPROTO_SCTP)
4913 bpf_error("port '%s' is sctp", name);
4915 /* override PROTO_UNDEF */
4916 real_proto = IPPROTO_UDP;
4918 if (proto == Q_TCP) {
4919 if (real_proto == IPPROTO_UDP)
4920 bpf_error("port '%s' is udp", name);
4922 else if (real_proto == IPPROTO_SCTP)
4923 bpf_error("port '%s' is sctp", name);
4925 /* override PROTO_UNDEF */
4926 real_proto = IPPROTO_TCP;
4928 if (proto == Q_SCTP) {
4929 if (real_proto == IPPROTO_UDP)
4930 bpf_error("port '%s' is udp", name);
4932 else if (real_proto == IPPROTO_TCP)
4933 bpf_error("port '%s' is tcp", name);
4935 /* override PROTO_UNDEF */
4936 real_proto = IPPROTO_SCTP;
4939 return gen_port(port, real_proto, dir);
4943 b = gen_port(port, real_proto, dir);
4944 gen_or(gen_port6(port, real_proto, dir), b);
4950 if (proto != Q_DEFAULT &&
4951 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
4952 bpf_error("illegal qualifier of 'portrange'");
4953 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
4954 bpf_error("unknown port in range '%s'", name);
4955 if (proto == Q_UDP) {
4956 if (real_proto == IPPROTO_TCP)
4957 bpf_error("port in range '%s' is tcp", name);
4958 else if (real_proto == IPPROTO_SCTP)
4959 bpf_error("port in range '%s' is sctp", name);
4961 /* override PROTO_UNDEF */
4962 real_proto = IPPROTO_UDP;
4964 if (proto == Q_TCP) {
4965 if (real_proto == IPPROTO_UDP)
4966 bpf_error("port in range '%s' is udp", name);
4967 else if (real_proto == IPPROTO_SCTP)
4968 bpf_error("port in range '%s' is sctp", name);
4970 /* override PROTO_UNDEF */
4971 real_proto = IPPROTO_TCP;
4973 if (proto == Q_SCTP) {
4974 if (real_proto == IPPROTO_UDP)
4975 bpf_error("port in range '%s' is udp", name);
4976 else if (real_proto == IPPROTO_TCP)
4977 bpf_error("port in range '%s' is tcp", name);
4979 /* override PROTO_UNDEF */
4980 real_proto = IPPROTO_SCTP;
4983 return gen_portrange(port1, port2, real_proto, dir);
4987 b = gen_portrange(port1, port2, real_proto, dir);
4988 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
4995 eaddr = pcap_ether_hostton(name);
4997 bpf_error("unknown ether host: %s", name);
4999 alist = pcap_nametoaddr(name);
5000 if (alist == NULL || *alist == NULL)
5001 bpf_error("unknown host '%s'", name);
5002 b = gen_gateway(eaddr, alist, proto, dir);
5006 bpf_error("'gateway' not supported in this configuration");
5010 real_proto = lookup_proto(name, proto);
5011 if (real_proto >= 0)
5012 return gen_proto(real_proto, proto, dir);
5014 bpf_error("unknown protocol: %s", name);
5017 real_proto = lookup_proto(name, proto);
5018 if (real_proto >= 0)
5019 return gen_protochain(real_proto, proto, dir);
5021 bpf_error("unknown protocol: %s", name);
5033 gen_mcode(s1, s2, masklen, q)
5034 register const char *s1, *s2;
5035 register int masklen;
5038 register int nlen, mlen;
5041 nlen = __pcap_atoin(s1, &n);
5042 /* Promote short ipaddr */
5046 mlen = __pcap_atoin(s2, &m);
5047 /* Promote short ipaddr */
5050 bpf_error("non-network bits set in \"%s mask %s\"",
5053 /* Convert mask len to mask */
5055 bpf_error("mask length must be <= 32");
5058 * X << 32 is not guaranteed by C to be 0; it's
5063 m = 0xffffffff << (32 - masklen);
5065 bpf_error("non-network bits set in \"%s/%d\"",
5072 return gen_host(n, m, q.proto, q.dir, q.addr);
5075 bpf_error("Mask syntax for networks only");
5083 register const char *s;
5088 int proto = q.proto;
5094 else if (q.proto == Q_DECNET)
5095 vlen = __pcap_atodn(s, &v);
5097 vlen = __pcap_atoin(s, &v);
5104 if (proto == Q_DECNET)
5105 return gen_host(v, 0, proto, dir, q.addr);
5106 else if (proto == Q_LINK) {
5107 bpf_error("illegal link layer address");
5110 if (s == NULL && q.addr == Q_NET) {
5111 /* Promote short net number */
5112 while (v && (v & 0xff000000) == 0) {
5117 /* Promote short ipaddr */
5121 return gen_host(v, mask, proto, dir, q.addr);
5126 proto = IPPROTO_UDP;
5127 else if (proto == Q_TCP)
5128 proto = IPPROTO_TCP;
5129 else if (proto == Q_SCTP)
5130 proto = IPPROTO_SCTP;
5131 else if (proto == Q_DEFAULT)
5132 proto = PROTO_UNDEF;
5134 bpf_error("illegal qualifier of 'port'");
5137 return gen_port((int)v, proto, dir);
5141 b = gen_port((int)v, proto, dir);
5142 gen_or(gen_port6((int)v, proto, dir), b);
5149 proto = IPPROTO_UDP;
5150 else if (proto == Q_TCP)
5151 proto = IPPROTO_TCP;
5152 else if (proto == Q_SCTP)
5153 proto = IPPROTO_SCTP;
5154 else if (proto == Q_DEFAULT)
5155 proto = PROTO_UNDEF;
5157 bpf_error("illegal qualifier of 'portrange'");
5160 return gen_portrange((int)v, (int)v, proto, dir);
5164 b = gen_portrange((int)v, (int)v, proto, dir);
5165 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
5171 bpf_error("'gateway' requires a name");
5175 return gen_proto((int)v, proto, dir);
5178 return gen_protochain((int)v, proto, dir);
5193 gen_mcode6(s1, s2, masklen, q)
5194 register const char *s1, *s2;
5195 register int masklen;
5198 struct addrinfo *res;
5199 struct in6_addr *addr;
5200 struct in6_addr mask;
5205 bpf_error("no mask %s supported", s2);
5207 res = pcap_nametoaddrinfo(s1);
5209 bpf_error("invalid ip6 address %s", s1);
5211 bpf_error("%s resolved to multiple address", s1);
5212 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
5214 if (sizeof(mask) * 8 < masklen)
5215 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
5216 memset(&mask, 0, sizeof(mask));
5217 memset(&mask, 0xff, masklen / 8);
5219 mask.s6_addr[masklen / 8] =
5220 (0xff << (8 - masklen % 8)) & 0xff;
5223 a = (u_int32_t *)addr;
5224 m = (u_int32_t *)&mask;
5225 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
5226 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
5227 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
5235 bpf_error("Mask syntax for networks only");
5239 b = gen_host6(addr, &mask, q.proto, q.dir, q.addr);
5244 bpf_error("invalid qualifier against IPv6 address");
5252 register const u_char *eaddr;
5255 struct block *b, *tmp;
5257 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
5260 return gen_ehostop(eaddr, (int)q.dir);
5262 return gen_fhostop(eaddr, (int)q.dir);
5264 return gen_thostop(eaddr, (int)q.dir);
5265 case DLT_IEEE802_11:
5266 case DLT_IEEE802_11_RADIO_AVS:
5267 case DLT_IEEE802_11_RADIO:
5268 case DLT_PRISM_HEADER:
5269 return gen_wlanhostop(eaddr, (int)q.dir);
5273 * Check that the packet doesn't begin with an
5274 * LE Control marker. (We've already generated
5277 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5282 * Now check the MAC address.
5284 b = gen_ehostop(eaddr, (int)q.dir);
5289 case DLT_IP_OVER_FC:
5290 return gen_ipfchostop(eaddr, (int)q.dir);
5292 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5296 bpf_error("ethernet address used in non-ether expression");
5302 struct slist *s0, *s1;
5305 * This is definitely not the best way to do this, but the
5306 * lists will rarely get long.
5313 static struct slist *
5319 s = new_stmt(BPF_LDX|BPF_MEM);
5324 static struct slist *
5330 s = new_stmt(BPF_LD|BPF_MEM);
5336 * Modify "index" to use the value stored into its register as an
5337 * offset relative to the beginning of the header for the protocol
5338 * "proto", and allocate a register and put an item "size" bytes long
5339 * (1, 2, or 4) at that offset into that register, making it the register
5343 gen_load(proto, index, size)
5348 struct slist *s, *tmp;
5350 int regno = alloc_reg();
5352 free_reg(index->regno);
5356 bpf_error("data size must be 1, 2, or 4");
5372 bpf_error("unsupported index operation");
5376 * The offset is relative to the beginning of the packet
5377 * data, if we have a radio header. (If we don't, this
5380 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
5381 linktype != DLT_IEEE802_11_RADIO &&
5382 linktype != DLT_PRISM_HEADER)
5383 bpf_error("radio information not present in capture");
5386 * Load into the X register the offset computed into the
5387 * register specifed by "index".
5389 s = xfer_to_x(index);
5392 * Load the item at that offset.
5394 tmp = new_stmt(BPF_LD|BPF_IND|size);
5396 sappend(index->s, s);
5401 * The offset is relative to the beginning of
5402 * the link-layer header.
5404 * XXX - what about ATM LANE? Should the index be
5405 * relative to the beginning of the AAL5 frame, so
5406 * that 0 refers to the beginning of the LE Control
5407 * field, or relative to the beginning of the LAN
5408 * frame, so that 0 refers, for Ethernet LANE, to
5409 * the beginning of the destination address?
5411 s = gen_llprefixlen();
5414 * If "s" is non-null, it has code to arrange that the
5415 * X register contains the length of the prefix preceding
5416 * the link-layer header. Add to it the offset computed
5417 * into the register specified by "index", and move that
5418 * into the X register. Otherwise, just load into the X
5419 * register the offset computed into the register specifed
5423 sappend(s, xfer_to_a(index));
5424 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5425 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5427 s = xfer_to_x(index);
5430 * Load the item at the sum of the offset we've put in the
5431 * X register and the offset of the start of the link
5432 * layer header (which is 0 if the radio header is
5433 * variable-length; that header length is what we put
5434 * into the X register and then added to the index).
5436 tmp = new_stmt(BPF_LD|BPF_IND|size);
5439 sappend(index->s, s);
5455 * The offset is relative to the beginning of
5456 * the network-layer header.
5457 * XXX - are there any cases where we want
5460 s = gen_llprefixlen();
5463 * If "s" is non-null, it has code to arrange that the
5464 * X register contains the length of the prefix preceding
5465 * the link-layer header. Add to it the offset computed
5466 * into the register specified by "index", and move that
5467 * into the X register. Otherwise, just load into the X
5468 * register the offset computed into the register specifed
5472 sappend(s, xfer_to_a(index));
5473 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5474 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5476 s = xfer_to_x(index);
5479 * Load the item at the sum of the offset we've put in the
5480 * X register, the offset of the start of the network
5481 * layer header, and the offset of the start of the link
5482 * layer header (which is 0 if the radio header is
5483 * variable-length; that header length is what we put
5484 * into the X register and then added to the index).
5486 tmp = new_stmt(BPF_LD|BPF_IND|size);
5487 tmp->s.k = off_ll + off_nl;
5489 sappend(index->s, s);
5492 * Do the computation only if the packet contains
5493 * the protocol in question.
5495 b = gen_proto_abbrev(proto);
5497 gen_and(index->b, b);
5510 * The offset is relative to the beginning of
5511 * the transport-layer header.
5513 * Load the X register with the length of the IPv4 header
5514 * (plus the offset of the link-layer header, if it's
5515 * a variable-length header), in bytes.
5517 * XXX - are there any cases where we want
5519 * XXX - we should, if we're built with
5520 * IPv6 support, generate code to load either
5521 * IPv4, IPv6, or both, as appropriate.
5523 s = gen_loadx_iphdrlen();
5526 * The X register now contains the sum of the length
5527 * of any variable-length header preceding the link-layer
5528 * header and the length of the network-layer header.
5529 * Load into the A register the offset relative to
5530 * the beginning of the transport layer header,
5531 * add the X register to that, move that to the
5532 * X register, and load with an offset from the
5533 * X register equal to the offset of the network
5534 * layer header relative to the beginning of
5535 * the link-layer header plus the length of any
5536 * fixed-length header preceding the link-layer
5539 sappend(s, xfer_to_a(index));
5540 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5541 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5542 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
5543 tmp->s.k = off_ll + off_nl;
5544 sappend(index->s, s);
5547 * Do the computation only if the packet contains
5548 * the protocol in question - which is true only
5549 * if this is an IP datagram and is the first or
5550 * only fragment of that datagram.
5552 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
5554 gen_and(index->b, b);
5556 gen_and(gen_proto_abbrev(Q_IP), b);
5562 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5566 index->regno = regno;
5567 s = new_stmt(BPF_ST);
5569 sappend(index->s, s);
5575 gen_relation(code, a0, a1, reversed)
5577 struct arth *a0, *a1;
5580 struct slist *s0, *s1, *s2;
5581 struct block *b, *tmp;
5585 if (code == BPF_JEQ) {
5586 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
5587 b = new_block(JMP(code));
5591 b = new_block(BPF_JMP|code|BPF_X);
5597 sappend(a0->s, a1->s);
5601 free_reg(a0->regno);
5602 free_reg(a1->regno);
5604 /* 'and' together protocol checks */
5607 gen_and(a0->b, tmp = a1->b);
5623 int regno = alloc_reg();
5624 struct arth *a = (struct arth *)newchunk(sizeof(*a));
5627 s = new_stmt(BPF_LD|BPF_LEN);
5628 s->next = new_stmt(BPF_ST);
5629 s->next->s.k = regno;
5644 a = (struct arth *)newchunk(sizeof(*a));
5648 s = new_stmt(BPF_LD|BPF_IMM);
5650 s->next = new_stmt(BPF_ST);
5666 s = new_stmt(BPF_ALU|BPF_NEG);
5669 s = new_stmt(BPF_ST);
5677 gen_arth(code, a0, a1)
5679 struct arth *a0, *a1;
5681 struct slist *s0, *s1, *s2;
5685 s2 = new_stmt(BPF_ALU|BPF_X|code);
5690 sappend(a0->s, a1->s);
5692 free_reg(a0->regno);
5693 free_reg(a1->regno);
5695 s0 = new_stmt(BPF_ST);
5696 a0->regno = s0->s.k = alloc_reg();
5703 * Here we handle simple allocation of the scratch registers.
5704 * If too many registers are alloc'd, the allocator punts.
5706 static int regused[BPF_MEMWORDS];
5710 * Return the next free register.
5715 int n = BPF_MEMWORDS;
5718 if (regused[curreg])
5719 curreg = (curreg + 1) % BPF_MEMWORDS;
5721 regused[curreg] = 1;
5725 bpf_error("too many registers needed to evaluate expression");
5730 * Return a register to the table so it can
5740 static struct block *
5747 s = new_stmt(BPF_LD|BPF_LEN);
5748 b = new_block(JMP(jmp));
5759 return gen_len(BPF_JGE, n);
5763 * Actually, this is less than or equal.
5771 b = gen_len(BPF_JGT, n);
5778 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5779 * the beginning of the link-layer header.
5780 * XXX - that means you can't test values in the radiotap header, but
5781 * as that header is difficult if not impossible to parse generally
5782 * without a loop, that might not be a severe problem. A new keyword
5783 * "radio" could be added for that, although what you'd really want
5784 * would be a way of testing particular radio header values, which
5785 * would generate code appropriate to the radio header in question.
5788 gen_byteop(op, idx, val)
5799 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5802 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5806 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5810 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
5814 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
5818 b = new_block(JMP(BPF_JEQ));
5825 static u_char abroadcast[] = { 0x0 };
5828 gen_broadcast(proto)
5831 bpf_u_int32 hostmask;
5832 struct block *b0, *b1, *b2;
5833 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5841 case DLT_ARCNET_LINUX:
5842 return gen_ahostop(abroadcast, Q_DST);
5844 return gen_ehostop(ebroadcast, Q_DST);
5846 return gen_fhostop(ebroadcast, Q_DST);
5848 return gen_thostop(ebroadcast, Q_DST);
5849 case DLT_IEEE802_11:
5850 case DLT_IEEE802_11_RADIO_AVS:
5851 case DLT_IEEE802_11_RADIO:
5852 case DLT_PRISM_HEADER:
5853 return gen_wlanhostop(ebroadcast, Q_DST);
5854 case DLT_IP_OVER_FC:
5855 return gen_ipfchostop(ebroadcast, Q_DST);
5859 * Check that the packet doesn't begin with an
5860 * LE Control marker. (We've already generated
5863 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5868 * Now check the MAC address.
5870 b0 = gen_ehostop(ebroadcast, Q_DST);
5876 bpf_error("not a broadcast link");
5881 b0 = gen_linktype(ETHERTYPE_IP);
5882 hostmask = ~netmask;
5883 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
5884 b2 = gen_mcmp(OR_NET, 16, BPF_W,
5885 (bpf_int32)(~0 & hostmask), hostmask);
5890 bpf_error("only link-layer/IP broadcast filters supported");
5895 * Generate code to test the low-order bit of a MAC address (that's
5896 * the bottom bit of the *first* byte).
5898 static struct block *
5899 gen_mac_multicast(offset)
5902 register struct block *b0;
5903 register struct slist *s;
5905 /* link[offset] & 1 != 0 */
5906 s = gen_load_a(OR_LINK, offset, BPF_B);
5907 b0 = new_block(JMP(BPF_JSET));
5914 gen_multicast(proto)
5917 register struct block *b0, *b1, *b2;
5918 register struct slist *s;
5926 case DLT_ARCNET_LINUX:
5927 /* all ARCnet multicasts use the same address */
5928 return gen_ahostop(abroadcast, Q_DST);
5930 /* ether[0] & 1 != 0 */
5931 return gen_mac_multicast(0);
5934 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5936 * XXX - was that referring to bit-order issues?
5938 /* fddi[1] & 1 != 0 */
5939 return gen_mac_multicast(1);
5941 /* tr[2] & 1 != 0 */
5942 return gen_mac_multicast(2);
5943 case DLT_IEEE802_11:
5944 case DLT_IEEE802_11_RADIO_AVS:
5945 case DLT_IEEE802_11_RADIO:
5946 case DLT_PRISM_HEADER:
5950 * For control frames, there is no DA.
5952 * For management frames, DA is at an
5953 * offset of 4 from the beginning of
5956 * For data frames, DA is at an offset
5957 * of 4 from the beginning of the packet
5958 * if To DS is clear and at an offset of
5959 * 16 from the beginning of the packet
5964 * Generate the tests to be done for data frames.
5966 * First, check for To DS set, i.e. "link[1] & 0x01".
5968 s = gen_load_a(OR_LINK, 1, BPF_B);
5969 b1 = new_block(JMP(BPF_JSET));
5970 b1->s.k = 0x01; /* To DS */
5974 * If To DS is set, the DA is at 16.
5976 b0 = gen_mac_multicast(16);
5980 * Now, check for To DS not set, i.e. check
5981 * "!(link[1] & 0x01)".
5983 s = gen_load_a(OR_LINK, 1, BPF_B);
5984 b2 = new_block(JMP(BPF_JSET));
5985 b2->s.k = 0x01; /* To DS */
5990 * If To DS is not set, the DA is at 4.
5992 b1 = gen_mac_multicast(4);
5996 * Now OR together the last two checks. That gives
5997 * the complete set of checks for data frames.
6002 * Now check for a data frame.
6003 * I.e, check "link[0] & 0x08".
6005 s = gen_load_a(OR_LINK, 0, BPF_B);
6006 b1 = new_block(JMP(BPF_JSET));
6011 * AND that with the checks done for data frames.
6016 * If the high-order bit of the type value is 0, this
6017 * is a management frame.
6018 * I.e, check "!(link[0] & 0x08)".
6020 s = gen_load_a(OR_LINK, 0, BPF_B);
6021 b2 = new_block(JMP(BPF_JSET));
6027 * For management frames, the DA is at 4.
6029 b1 = gen_mac_multicast(4);
6033 * OR that with the checks done for data frames.
6034 * That gives the checks done for management and
6040 * If the low-order bit of the type value is 1,
6041 * this is either a control frame or a frame
6042 * with a reserved type, and thus not a
6045 * I.e., check "!(link[0] & 0x04)".
6047 s = gen_load_a(OR_LINK, 0, BPF_B);
6048 b1 = new_block(JMP(BPF_JSET));
6054 * AND that with the checks for data and management
6059 case DLT_IP_OVER_FC:
6060 b0 = gen_mac_multicast(2);
6065 * Check that the packet doesn't begin with an
6066 * LE Control marker. (We've already generated
6069 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6073 /* ether[off_mac] & 1 != 0 */
6074 b0 = gen_mac_multicast(off_mac);
6082 /* Link not known to support multicasts */
6086 b0 = gen_linktype(ETHERTYPE_IP);
6087 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
6093 b0 = gen_linktype(ETHERTYPE_IPV6);
6094 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
6099 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6104 * generate command for inbound/outbound. It's here so we can
6105 * make it link-type specific. 'dir' = 0 implies "inbound",
6106 * = 1 implies "outbound".
6112 register struct block *b0;
6115 * Only some data link types support inbound/outbound qualifiers.
6119 b0 = gen_relation(BPF_JEQ,
6120 gen_load(Q_LINK, gen_loadi(0), 1),
6128 * Match packets sent by this machine.
6130 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
6133 * Match packets sent to this machine.
6134 * (No broadcast or multicast packets, or
6135 * packets sent to some other machine and
6136 * received promiscuously.)
6138 * XXX - packets sent to other machines probably
6139 * shouldn't be matched, but what about broadcast
6140 * or multicast packets we received?
6142 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_HOST);
6147 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
6148 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
6153 /* match outgoing packets */
6154 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
6156 /* match incoming packets */
6157 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
6161 case DLT_JUNIPER_MFR:
6162 case DLT_JUNIPER_MLFR:
6163 case DLT_JUNIPER_MLPPP:
6164 case DLT_JUNIPER_ATM1:
6165 case DLT_JUNIPER_ATM2:
6166 case DLT_JUNIPER_PPPOE:
6167 case DLT_JUNIPER_PPPOE_ATM:
6168 case DLT_JUNIPER_GGSN:
6169 case DLT_JUNIPER_ES:
6170 case DLT_JUNIPER_MONITOR:
6171 case DLT_JUNIPER_SERVICES:
6172 case DLT_JUNIPER_ETHER:
6173 case DLT_JUNIPER_PPP:
6174 case DLT_JUNIPER_FRELAY:
6175 case DLT_JUNIPER_CHDLC:
6176 /* juniper flags (including direction) are stored
6177 * the byte after the 3-byte magic number */
6179 /* match outgoing packets */
6180 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
6182 /* match incoming packets */
6183 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
6188 bpf_error("inbound/outbound not supported on linktype %d",
6196 /* PF firewall log matched interface */
6198 gen_pf_ifname(const char *ifname)
6203 if (linktype == DLT_PFLOG) {
6204 len = sizeof(((struct pfloghdr *)0)->ifname);
6205 off = offsetof(struct pfloghdr, ifname);
6207 bpf_error("ifname not supported on linktype 0x%x", linktype);
6210 if (strlen(ifname) >= len) {
6211 bpf_error("ifname interface names can only be %d characters",
6215 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
6219 /* PF firewall log ruleset name */
6221 gen_pf_ruleset(char *ruleset)
6225 if (linktype != DLT_PFLOG) {
6226 bpf_error("ruleset not supported on linktype 0x%x", linktype);
6229 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
6230 bpf_error("ruleset names can only be %ld characters",
6231 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
6234 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
6235 strlen(ruleset), (const u_char *)ruleset);
6239 /* PF firewall log rule number */
6245 if (linktype == DLT_PFLOG) {
6246 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
6249 bpf_error("rnr not supported on linktype 0x%x", linktype);
6256 /* PF firewall log sub-rule number */
6258 gen_pf_srnr(int srnr)
6262 if (linktype != DLT_PFLOG) {
6263 bpf_error("srnr not supported on linktype 0x%x", linktype);
6267 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
6272 /* PF firewall log reason code */
6274 gen_pf_reason(int reason)
6278 if (linktype == DLT_PFLOG) {
6279 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
6282 bpf_error("reason not supported on linktype 0x%x", linktype);
6289 /* PF firewall log action */
6291 gen_pf_action(int action)
6295 if (linktype == DLT_PFLOG) {
6296 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
6299 bpf_error("action not supported on linktype 0x%x", linktype);
6308 register const u_char *eaddr;
6311 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6312 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
6313 return gen_ahostop(eaddr, (int)q.dir);
6315 bpf_error("ARCnet address used in non-arc expression");
6319 static struct block *
6320 gen_ahostop(eaddr, dir)
6321 register const u_char *eaddr;
6324 register struct block *b0, *b1;
6327 /* src comes first, different from Ethernet */
6329 return gen_bcmp(OR_LINK, 0, 1, eaddr);
6332 return gen_bcmp(OR_LINK, 1, 1, eaddr);
6335 b0 = gen_ahostop(eaddr, Q_SRC);
6336 b1 = gen_ahostop(eaddr, Q_DST);
6342 b0 = gen_ahostop(eaddr, Q_SRC);
6343 b1 = gen_ahostop(eaddr, Q_DST);
6352 * support IEEE 802.1Q VLAN trunk over ethernet
6358 struct block *b0, *b1;
6360 /* can't check for VLAN-encapsulated packets inside MPLS */
6361 if (label_stack_depth > 0)
6362 bpf_error("no VLAN match after MPLS");
6365 * Change the offsets to point to the type and data fields within
6366 * the VLAN packet. Just increment the offsets, so that we
6367 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6368 * capture VLAN 200 encapsulated within VLAN 100.
6370 * XXX - this is a bit of a kludge. If we were to split the
6371 * compiler into a parser that parses an expression and
6372 * generates an expression tree, and a code generator that
6373 * takes an expression tree (which could come from our
6374 * parser or from some other parser) and generates BPF code,
6375 * we could perhaps make the offsets parameters of routines
6376 * and, in the handler for an "AND" node, pass to subnodes
6377 * other than the VLAN node the adjusted offsets.
6379 * This would mean that "vlan" would, instead of changing the
6380 * behavior of *all* tests after it, change only the behavior
6381 * of tests ANDed with it. That would change the documented
6382 * semantics of "vlan", which might break some expressions.
6383 * However, it would mean that "(vlan and ip) or ip" would check
6384 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6385 * checking only for VLAN-encapsulated IP, so that could still
6386 * be considered worth doing; it wouldn't break expressions
6387 * that are of the form "vlan and ..." or "vlan N and ...",
6388 * which I suspect are the most common expressions involving
6389 * "vlan". "vlan or ..." doesn't necessarily do what the user
6390 * would really want, now, as all the "or ..." tests would
6391 * be done assuming a VLAN, even though the "or" could be viewed
6392 * as meaning "or, if this isn't a VLAN packet...".
6394 orig_linktype = off_linktype; /* save original values */
6406 bpf_error("no VLAN support for data link type %d",
6411 /* check for VLAN */
6412 b0 = gen_cmp(OR_LINK, orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q);
6414 /* If a specific VLAN is requested, check VLAN id */
6415 if (vlan_num >= 0) {
6416 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_H, (bpf_int32)vlan_num,
6432 struct block *b0,*b1;
6435 * Change the offsets to point to the type and data fields within
6436 * the MPLS packet. Just increment the offsets, so that we
6437 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6438 * capture packets with an outer label of 100000 and an inner
6441 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6445 if (label_stack_depth > 0) {
6446 /* just match the bottom-of-stack bit clear */
6447 b0 = gen_mcmp(OR_LINK, orig_nl-2, BPF_B, 0, 0x01);
6450 * Indicate that we're checking MPLS-encapsulated headers,
6451 * to make sure higher level code generators don't try to
6452 * match against IP-related protocols such as Q_ARP, Q_RARP
6457 case DLT_C_HDLC: /* fall through */
6459 b0 = gen_linktype(ETHERTYPE_MPLS);
6463 b0 = gen_linktype(PPP_MPLS_UCAST);
6466 /* FIXME add other DLT_s ...
6467 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6468 * leave it for now */
6471 bpf_error("no MPLS support for data link type %d",
6479 /* If a specific MPLS label is requested, check it */
6480 if (label_num >= 0) {
6481 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
6482 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_W, (bpf_int32)label_num,
6483 0xfffff000); /* only compare the first 20 bits */
6490 label_stack_depth++;
6495 * Support PPPOE discovery and session.
6500 /* check for PPPoE discovery */
6501 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
6510 * Test against the PPPoE session link-layer type.
6512 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
6515 * Change the offsets to point to the type and data fields within
6518 * XXX - this is a bit of a kludge. If we were to split the
6519 * compiler into a parser that parses an expression and
6520 * generates an expression tree, and a code generator that
6521 * takes an expression tree (which could come from our
6522 * parser or from some other parser) and generates BPF code,
6523 * we could perhaps make the offsets parameters of routines
6524 * and, in the handler for an "AND" node, pass to subnodes
6525 * other than the PPPoE node the adjusted offsets.
6527 * This would mean that "pppoes" would, instead of changing the
6528 * behavior of *all* tests after it, change only the behavior
6529 * of tests ANDed with it. That would change the documented
6530 * semantics of "pppoes", which might break some expressions.
6531 * However, it would mean that "(pppoes and ip) or ip" would check
6532 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6533 * checking only for VLAN-encapsulated IP, so that could still
6534 * be considered worth doing; it wouldn't break expressions
6535 * that are of the form "pppoes and ..." which I suspect are the
6536 * most common expressions involving "pppoes". "pppoes or ..."
6537 * doesn't necessarily do what the user would really want, now,
6538 * as all the "or ..." tests would be done assuming PPPoE, even
6539 * though the "or" could be viewed as meaning "or, if this isn't
6540 * a PPPoE packet...".
6542 orig_linktype = off_linktype; /* save original values */
6546 * The "network-layer" protocol is PPPoE, which has a 6-byte
6547 * PPPoE header, followed by PPP payload, so we set the
6548 * offsets to the network layer offset plus 6 bytes for
6549 * the PPPoE header plus the values appropriate for PPP when
6550 * encapsulated in Ethernet (which means there's no HDLC
6553 off_linktype = orig_nl + 6;
6554 off_nl = orig_nl + 6 + 2;
6555 off_nl_nosnap = orig_nl + 6 + 2;
6558 * Set the link-layer type to PPP, as all subsequent tests will
6559 * be on the encapsulated PPP header.
6567 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
6579 bpf_error("'vpi' supported only on raw ATM");
6580 if (off_vpi == (u_int)-1)
6582 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
6588 bpf_error("'vci' supported only on raw ATM");
6589 if (off_vci == (u_int)-1)
6591 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
6596 if (off_proto == (u_int)-1)
6597 abort(); /* XXX - this isn't on FreeBSD */
6598 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
6603 if (off_payload == (u_int)-1)
6605 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
6606 0xffffffff, jtype, reverse, jvalue);
6611 bpf_error("'callref' supported only on raw ATM");
6612 if (off_proto == (u_int)-1)
6614 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
6615 jtype, reverse, jvalue);
6625 gen_atmtype_abbrev(type)
6628 struct block *b0, *b1;
6633 /* Get all packets in Meta signalling Circuit */
6635 bpf_error("'metac' supported only on raw ATM");
6636 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6637 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
6642 /* Get all packets in Broadcast Circuit*/
6644 bpf_error("'bcc' supported only on raw ATM");
6645 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6646 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
6651 /* Get all cells in Segment OAM F4 circuit*/
6653 bpf_error("'oam4sc' supported only on raw ATM");
6654 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6655 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6660 /* Get all cells in End-to-End OAM F4 Circuit*/
6662 bpf_error("'oam4ec' supported only on raw ATM");
6663 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6664 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6669 /* Get all packets in connection Signalling Circuit */
6671 bpf_error("'sc' supported only on raw ATM");
6672 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6673 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
6678 /* Get all packets in ILMI Circuit */
6680 bpf_error("'ilmic' supported only on raw ATM");
6681 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6682 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
6687 /* Get all LANE packets */
6689 bpf_error("'lane' supported only on raw ATM");
6690 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
6693 * Arrange that all subsequent tests assume LANE
6694 * rather than LLC-encapsulated packets, and set
6695 * the offsets appropriately for LANE-encapsulated
6698 * "off_mac" is the offset of the Ethernet header,
6699 * which is 2 bytes past the ATM pseudo-header
6700 * (skipping the pseudo-header and 2-byte LE Client
6701 * field). The other offsets are Ethernet offsets
6702 * relative to "off_mac".
6705 off_mac = off_payload + 2; /* MAC header */
6706 off_linktype = off_mac + 12;
6707 off_nl = off_mac + 14; /* Ethernet II */
6708 off_nl_nosnap = off_mac + 17; /* 802.3+802.2 */
6712 /* Get all LLC-encapsulated packets */
6714 bpf_error("'llc' supported only on raw ATM");
6715 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
6726 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
6733 bpf_u_int32 val1 , val2 , val3;
6735 switch (mtp3field) {
6738 if (off_sio == (u_int)-1)
6739 bpf_error("'sio' supported only on SS7");
6740 /* sio coded on 1 byte so max value 255 */
6742 bpf_error("sio value %u too big; max value = 255",
6744 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
6745 (u_int)jtype, reverse, (u_int)jvalue);
6749 if (off_opc == (u_int)-1)
6750 bpf_error("'opc' supported only on SS7");
6751 /* opc coded on 14 bits so max value 16383 */
6753 bpf_error("opc value %u too big; max value = 16383",
6755 /* the following instructions are made to convert jvalue
6756 * to the form used to write opc in an ss7 message*/
6757 val1 = jvalue & 0x00003c00;
6759 val2 = jvalue & 0x000003fc;
6761 val3 = jvalue & 0x00000003;
6763 jvalue = val1 + val2 + val3;
6764 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
6765 (u_int)jtype, reverse, (u_int)jvalue);
6769 if (off_dpc == (u_int)-1)
6770 bpf_error("'dpc' supported only on SS7");
6771 /* dpc coded on 14 bits so max value 16383 */
6773 bpf_error("dpc value %u too big; max value = 16383",
6775 /* the following instructions are made to convert jvalue
6776 * to the forme used to write dpc in an ss7 message*/
6777 val1 = jvalue & 0x000000ff;
6779 val2 = jvalue & 0x00003f00;
6781 jvalue = val1 + val2;
6782 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
6783 (u_int)jtype, reverse, (u_int)jvalue);
6787 if (off_sls == (u_int)-1)
6788 bpf_error("'sls' supported only on SS7");
6789 /* sls coded on 4 bits so max value 15 */
6791 bpf_error("sls value %u too big; max value = 15",
6793 /* the following instruction is made to convert jvalue
6794 * to the forme used to write sls in an ss7 message*/
6795 jvalue = jvalue << 4;
6796 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
6797 (u_int)jtype,reverse, (u_int)jvalue);
6806 static struct block *
6807 gen_msg_abbrev(type)
6813 * Q.2931 signalling protocol messages for handling virtual circuits
6814 * establishment and teardown
6819 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
6823 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
6827 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
6831 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
6835 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
6838 case A_RELEASE_DONE:
6839 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
6849 gen_atmmulti_abbrev(type)
6852 struct block *b0, *b1;
6858 bpf_error("'oam' supported only on raw ATM");
6859 b1 = gen_atmmulti_abbrev(A_OAMF4);
6864 bpf_error("'oamf4' supported only on raw ATM");
6866 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6867 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6869 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6875 * Get Q.2931 signalling messages for switched
6876 * virtual connection
6879 bpf_error("'connectmsg' supported only on raw ATM");
6880 b0 = gen_msg_abbrev(A_SETUP);
6881 b1 = gen_msg_abbrev(A_CALLPROCEED);
6883 b0 = gen_msg_abbrev(A_CONNECT);
6885 b0 = gen_msg_abbrev(A_CONNECTACK);
6887 b0 = gen_msg_abbrev(A_RELEASE);
6889 b0 = gen_msg_abbrev(A_RELEASE_DONE);
6891 b0 = gen_atmtype_abbrev(A_SC);
6897 bpf_error("'metaconnect' supported only on raw ATM");
6898 b0 = gen_msg_abbrev(A_SETUP);
6899 b1 = gen_msg_abbrev(A_CALLPROCEED);
6901 b0 = gen_msg_abbrev(A_CONNECT);
6903 b0 = gen_msg_abbrev(A_RELEASE);
6905 b0 = gen_msg_abbrev(A_RELEASE_DONE);
6907 b0 = gen_atmtype_abbrev(A_METAC);