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.
25 static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.221.2.53 2007/09/12 19:17:24 guy Exp $ (LBL)";
34 #include <pcap-stdinc.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
41 * XXX - why was this included even on UNIX?
50 #include <sys/param.h>
53 #include <netinet/in.h>
69 #include "ethertype.h"
74 #include "sunatmpos.h"
78 #ifdef HAVE_NET_PFVAR_H
79 #include <sys/socket.h>
81 #include <net/pfvar.h>
82 #include <net/if_pflog.h>
85 #define offsetof(s, e) ((size_t)&((s *)0)->e)
89 #include <netdb.h> /* for "struct addrinfo" */
92 #include <pcap-namedb.h>
97 #define IPPROTO_SCTP 132
100 #ifdef HAVE_OS_PROTO_H
101 #include "os-proto.h"
104 #define JMP(c) ((c)|BPF_JMP|BPF_K)
107 static jmp_buf top_ctx;
108 static pcap_t *bpf_pcap;
111 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
112 static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
114 static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
119 static int pcap_fddipad;
124 bpf_error(const char *fmt, ...)
129 if (bpf_pcap != NULL)
130 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
137 static void init_linktype(pcap_t *);
139 static int alloc_reg(void);
140 static void free_reg(int);
142 static struct block *root;
145 * Value passed to gen_load_a() to indicate what the offset argument
149 OR_PACKET, /* relative to the beginning of the packet */
150 OR_LINK, /* relative to the link-layer header */
151 OR_NET, /* relative to the network-layer header */
152 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
153 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
154 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
158 * We divy out chunks of memory rather than call malloc each time so
159 * we don't have to worry about leaking memory. It's probably
160 * not a big deal if all this memory was wasted but if this ever
161 * goes into a library that would probably not be a good idea.
163 * XXX - this *is* in a library....
166 #define CHUNK0SIZE 1024
172 static struct chunk chunks[NCHUNKS];
173 static int cur_chunk;
175 static void *newchunk(u_int);
176 static void freechunks(void);
177 static inline struct block *new_block(int);
178 static inline struct slist *new_stmt(int);
179 static struct block *gen_retblk(int);
180 static inline void syntax(void);
182 static void backpatch(struct block *, struct block *);
183 static void merge(struct block *, struct block *);
184 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
185 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
186 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
187 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
188 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
189 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
191 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
192 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
193 bpf_u_int32, bpf_u_int32, int, bpf_int32);
194 static struct slist *gen_load_llrel(u_int, u_int);
195 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
196 static struct slist *gen_loadx_iphdrlen(void);
197 static struct block *gen_uncond(int);
198 static inline struct block *gen_true(void);
199 static inline struct block *gen_false(void);
200 static struct block *gen_ether_linktype(int);
201 static struct block *gen_linux_sll_linktype(int);
202 static void insert_radiotap_load_llprefixlen(struct block *);
203 static void insert_ppi_load_llprefixlen(struct block *);
204 static void insert_load_llprefixlen(struct block *);
205 static struct slist *gen_llprefixlen(void);
206 static struct block *gen_linktype(int);
207 static struct block *gen_snap(bpf_u_int32, bpf_u_int32, u_int);
208 static struct block *gen_llc_linktype(int);
209 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
211 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
213 static struct block *gen_ahostop(const u_char *, int);
214 static struct block *gen_ehostop(const u_char *, int);
215 static struct block *gen_fhostop(const u_char *, int);
216 static struct block *gen_thostop(const u_char *, int);
217 static struct block *gen_wlanhostop(const u_char *, int);
218 static struct block *gen_ipfchostop(const u_char *, int);
219 static struct block *gen_dnhostop(bpf_u_int32, int);
220 static struct block *gen_mpls_linktype(int);
221 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
223 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
226 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
228 static struct block *gen_ipfrag(void);
229 static struct block *gen_portatom(int, bpf_int32);
230 static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
232 static struct block *gen_portatom6(int, bpf_int32);
233 static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
235 struct block *gen_portop(int, int, int);
236 static struct block *gen_port(int, int, int);
237 struct block *gen_portrangeop(int, int, int, int);
238 static struct block *gen_portrange(int, int, int, int);
240 struct block *gen_portop6(int, int, int);
241 static struct block *gen_port6(int, int, int);
242 struct block *gen_portrangeop6(int, int, int, int);
243 static struct block *gen_portrange6(int, int, int, int);
245 static int lookup_proto(const char *, int);
246 static struct block *gen_protochain(int, int, int);
247 static struct block *gen_proto(int, int, int);
248 static struct slist *xfer_to_x(struct arth *);
249 static struct slist *xfer_to_a(struct arth *);
250 static struct block *gen_mac_multicast(int);
251 static struct block *gen_len(int, int);
253 static struct block *gen_ppi_dlt_check(void);
254 static struct block *gen_msg_abbrev(int type);
265 /* XXX Round up to nearest long. */
266 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
268 /* XXX Round up to structure boundary. */
272 cp = &chunks[cur_chunk];
273 if (n > cp->n_left) {
274 ++cp, k = ++cur_chunk;
276 bpf_error("out of memory");
277 size = CHUNK0SIZE << k;
278 cp->m = (void *)malloc(size);
280 bpf_error("out of memory");
281 memset((char *)cp->m, 0, size);
284 bpf_error("out of memory");
287 return (void *)((char *)cp->m + cp->n_left);
296 for (i = 0; i < NCHUNKS; ++i)
297 if (chunks[i].m != NULL) {
304 * A strdup whose allocations are freed after code generation is over.
308 register const char *s;
310 int n = strlen(s) + 1;
311 char *cp = newchunk(n);
317 static inline struct block *
323 p = (struct block *)newchunk(sizeof(*p));
330 static inline struct slist *
336 p = (struct slist *)newchunk(sizeof(*p));
342 static struct block *
346 struct block *b = new_block(BPF_RET|BPF_K);
355 bpf_error("syntax error in filter expression");
358 static bpf_u_int32 netmask;
363 pcap_compile(pcap_t *p, struct bpf_program *program,
364 const char *buf, int optimize, bpf_u_int32 mask)
367 const char * volatile xbuf = buf;
374 if (setjmp(top_ctx)) {
382 snaplen = pcap_snapshot(p);
384 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
385 "snaplen of 0 rejects all packets");
389 lex_init(xbuf ? xbuf : "");
397 root = gen_retblk(snaplen);
399 if (optimize && !no_optimize) {
402 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
403 bpf_error("expression rejects all packets");
405 program->bf_insns = icode_to_fcode(root, &len);
406 program->bf_len = len;
414 * entry point for using the compiler with no pcap open
415 * pass in all the stuff that is needed explicitly instead.
418 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
419 struct bpf_program *program,
420 const char *buf, int optimize, bpf_u_int32 mask)
425 p = pcap_open_dead(linktype_arg, snaplen_arg);
428 ret = pcap_compile(p, program, buf, optimize, mask);
434 * Clean up a "struct bpf_program" by freeing all the memory allocated
438 pcap_freecode(struct bpf_program *program)
441 if (program->bf_insns != NULL) {
442 free((char *)program->bf_insns);
443 program->bf_insns = NULL;
448 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
449 * which of the jt and jf fields has been resolved and which is a pointer
450 * back to another unresolved block (or nil). At least one of the fields
451 * in each block is already resolved.
454 backpatch(list, target)
455 struct block *list, *target;
472 * Merge the lists in b0 and b1, using the 'sense' field to indicate
473 * which of jt and jf is the link.
477 struct block *b0, *b1;
479 register struct block **p = &b0;
481 /* Find end of list. */
483 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
485 /* Concatenate the lists. */
494 struct block *ppi_dlt_check;
496 ppi_dlt_check = gen_ppi_dlt_check();
498 if (ppi_dlt_check != NULL)
500 gen_and(ppi_dlt_check, p);
503 backpatch(p, gen_retblk(snaplen));
504 p->sense = !p->sense;
505 backpatch(p, gen_retblk(0));
509 * Insert before the statements of the first (root) block any
510 * statements needed to load the lengths of any variable-length
511 * headers into registers.
513 * XXX - a fancier strategy would be to insert those before the
514 * statements of all blocks that use those lengths and that
515 * have no predecessors that use them, so that we only compute
516 * the lengths if we need them. There might be even better
517 * approaches than that. However, as we're currently only
518 * handling variable-length radiotap headers, and as all
519 * filtering expressions other than raw link[M:N] tests
520 * require the length of that header, doing more for that
521 * header length isn't really worth the effort.
524 insert_load_llprefixlen(root);
529 struct block *b0, *b1;
531 backpatch(b0, b1->head);
532 b0->sense = !b0->sense;
533 b1->sense = !b1->sense;
535 b1->sense = !b1->sense;
541 struct block *b0, *b1;
543 b0->sense = !b0->sense;
544 backpatch(b0, b1->head);
545 b0->sense = !b0->sense;
554 b->sense = !b->sense;
557 static struct block *
558 gen_cmp(offrel, offset, size, v)
559 enum e_offrel offrel;
563 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
566 static struct block *
567 gen_cmp_gt(offrel, offset, size, v)
568 enum e_offrel offrel;
572 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
575 static struct block *
576 gen_cmp_ge(offrel, offset, size, v)
577 enum e_offrel offrel;
581 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
584 static struct block *
585 gen_cmp_lt(offrel, offset, size, v)
586 enum e_offrel offrel;
590 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
593 static struct block *
594 gen_cmp_le(offrel, offset, size, v)
595 enum e_offrel offrel;
599 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
602 static struct block *
603 gen_mcmp(offrel, offset, size, v, mask)
604 enum e_offrel offrel;
609 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
612 static struct block *
613 gen_bcmp(offrel, offset, size, v)
614 enum e_offrel offrel;
615 register u_int offset, size;
616 register const u_char *v;
618 register struct block *b, *tmp;
622 register const u_char *p = &v[size - 4];
623 bpf_int32 w = ((bpf_int32)p[0] << 24) |
624 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
626 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
633 register const u_char *p = &v[size - 2];
634 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
636 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
643 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
652 * AND the field of size "size" at offset "offset" relative to the header
653 * specified by "offrel" with "mask", and compare it with the value "v"
654 * with the test specified by "jtype"; if "reverse" is true, the test
655 * should test the opposite of "jtype".
657 static struct block *
658 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
659 enum e_offrel offrel;
661 bpf_u_int32 offset, size, mask, jtype;
664 struct slist *s, *s2;
667 s = gen_load_a(offrel, offset, size);
669 if (mask != 0xffffffff) {
670 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
675 b = new_block(JMP(jtype));
678 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
684 * Various code constructs need to know the layout of the data link
685 * layer. These variables give the necessary offsets from the beginning
686 * of the packet data.
688 * If the link layer has variable_length headers, the offsets are offsets
689 * from the end of the link-link-layer header, and "reg_ll_size" is
690 * the register number for a register containing the length of the
691 * link-layer header. Otherwise, "reg_ll_size" is -1.
693 static int reg_ll_size;
696 * This is the offset of the beginning of the link-layer header from
697 * the beginning of the raw packet data.
699 * It's usually 0, except for 802.11 with a fixed-length radio header.
700 * (For 802.11 with a variable-length radio header, we have to generate
701 * code to compute that offset; off_ll is 0 in that case.)
706 * This is the offset of the beginning of the MAC-layer header.
707 * It's usually 0, except for ATM LANE, where it's the offset, relative
708 * to the beginning of the raw packet data, of the Ethernet header.
710 static u_int off_mac;
713 * "off_linktype" is the offset to information in the link-layer header
714 * giving the packet type. This offset is relative to the beginning
715 * of the link-layer header (i.e., it doesn't include off_ll).
717 * For Ethernet, it's the offset of the Ethernet type field.
719 * For link-layer types that always use 802.2 headers, it's the
720 * offset of the LLC header.
722 * For PPP, it's the offset of the PPP type field.
724 * For Cisco HDLC, it's the offset of the CHDLC type field.
726 * For BSD loopback, it's the offset of the AF_ value.
728 * For Linux cooked sockets, it's the offset of the type field.
730 * It's set to -1 for no encapsulation, in which case, IP is assumed.
732 static u_int off_linktype;
735 * TRUE if the link layer includes an ATM pseudo-header.
737 static int is_atm = 0;
740 * TRUE if "lane" appeared in the filter; it causes us to generate
741 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
743 static int is_lane = 0;
746 * These are offsets for the ATM pseudo-header.
748 static u_int off_vpi;
749 static u_int off_vci;
750 static u_int off_proto;
753 * These are offsets for the MTP2 fields.
758 * These are offsets for the MTP3 fields.
760 static u_int off_sio;
761 static u_int off_opc;
762 static u_int off_dpc;
763 static u_int off_sls;
766 * This is the offset of the first byte after the ATM pseudo_header,
767 * or -1 if there is no ATM pseudo-header.
769 static u_int off_payload;
772 * These are offsets to the beginning of the network-layer header.
773 * They are relative to the beginning of the link-layer header (i.e.,
774 * they don't include off_ll).
776 * If the link layer never uses 802.2 LLC:
778 * "off_nl" and "off_nl_nosnap" are the same.
780 * If the link layer always uses 802.2 LLC:
782 * "off_nl" is the offset if there's a SNAP header following
785 * "off_nl_nosnap" is the offset if there's no SNAP header.
787 * If the link layer is Ethernet:
789 * "off_nl" is the offset if the packet is an Ethernet II packet
790 * (we assume no 802.3+802.2+SNAP);
792 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
793 * with an 802.2 header following it.
796 static u_int off_nl_nosnap;
804 linktype = pcap_datalink(p);
806 pcap_fddipad = p->fddipad;
810 * Assume it's not raw ATM with a pseudo-header, for now.
821 * And assume we're not doing SS7.
830 * Also assume it's not 802.11 with a fixed-length radio header.
836 label_stack_depth = 0;
844 off_nl = 6; /* XXX in reality, variable! */
845 off_nl_nosnap = 6; /* no 802.2 LLC */
848 case DLT_ARCNET_LINUX:
850 off_nl = 8; /* XXX in reality, variable! */
851 off_nl_nosnap = 8; /* no 802.2 LLC */
856 off_nl = 14; /* Ethernet II */
857 off_nl_nosnap = 17; /* 802.3+802.2 */
862 * SLIP doesn't have a link level type. The 16 byte
863 * header is hacked into our SLIP driver.
867 off_nl_nosnap = 16; /* no 802.2 LLC */
871 /* XXX this may be the same as the DLT_PPP_BSDOS case */
875 off_nl_nosnap = 24; /* no 802.2 LLC */
882 off_nl_nosnap = 4; /* no 802.2 LLC */
888 off_nl_nosnap = 12; /* no 802.2 LLC */
893 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
894 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
897 off_nl_nosnap = 4; /* no 802.2 LLC */
902 * This does no include the Ethernet header, and
903 * only covers session state.
907 off_nl_nosnap = 8; /* no 802.2 LLC */
913 off_nl_nosnap = 24; /* no 802.2 LLC */
918 * FDDI doesn't really have a link-level type field.
919 * We set "off_linktype" to the offset of the LLC header.
921 * To check for Ethernet types, we assume that SSAP = SNAP
922 * is being used and pick out the encapsulated Ethernet type.
923 * XXX - should we generate code to check for SNAP?
927 off_linktype += pcap_fddipad;
929 off_nl = 21; /* FDDI+802.2+SNAP */
930 off_nl_nosnap = 16; /* FDDI+802.2 */
932 off_nl += pcap_fddipad;
933 off_nl_nosnap += pcap_fddipad;
939 * Token Ring doesn't really have a link-level type field.
940 * We set "off_linktype" to the offset of the LLC header.
942 * To check for Ethernet types, we assume that SSAP = SNAP
943 * is being used and pick out the encapsulated Ethernet type.
944 * XXX - should we generate code to check for SNAP?
946 * XXX - the header is actually variable-length.
947 * Some various Linux patched versions gave 38
948 * as "off_linktype" and 40 as "off_nl"; however,
949 * if a token ring packet has *no* routing
950 * information, i.e. is not source-routed, the correct
951 * values are 20 and 22, as they are in the vanilla code.
953 * A packet is source-routed iff the uppermost bit
954 * of the first byte of the source address, at an
955 * offset of 8, has the uppermost bit set. If the
956 * packet is source-routed, the total number of bytes
957 * of routing information is 2 plus bits 0x1F00 of
958 * the 16-bit value at an offset of 14 (shifted right
959 * 8 - figure out which byte that is).
962 off_nl = 22; /* Token Ring+802.2+SNAP */
963 off_nl_nosnap = 17; /* Token Ring+802.2 */
968 * 802.11 doesn't really have a link-level type field.
969 * We set "off_linktype" to the offset of the LLC header.
971 * To check for Ethernet types, we assume that SSAP = SNAP
972 * is being used and pick out the encapsulated Ethernet type.
973 * XXX - should we generate code to check for SNAP?
975 * XXX - the header is actually variable-length. We
976 * assume a 24-byte link-layer header, as appears in
977 * data frames in networks with no bridges. If the
978 * fromds and tods 802.11 header bits are both set,
979 * it's actually supposed to be 30 bytes.
982 off_nl = 32; /* 802.11+802.2+SNAP */
983 off_nl_nosnap = 27; /* 802.11+802.2 */
986 case DLT_PRISM_HEADER:
988 * Same as 802.11, but with an additional header before
989 * the 802.11 header, containing a bunch of additional
990 * information including radio-level information.
992 * The header is 144 bytes long.
994 * XXX - same variable-length header problem; at least
995 * the Prism header is fixed-length.
999 off_nl = 32; /* Prism+802.11+802.2+SNAP */
1000 off_nl_nosnap = 27; /* Prism+802.11+802.2 */
1003 case DLT_IEEE802_11_RADIO_AVS:
1005 * Same as 802.11, but with an additional header before
1006 * the 802.11 header, containing a bunch of additional
1007 * information including radio-level information.
1009 * The header is 64 bytes long, at least in its
1010 * current incarnation.
1012 * XXX - same variable-length header problem, only
1013 * more so; this header is also variable-length,
1014 * with the length being the 32-bit big-endian
1015 * number at an offset of 4 from the beginning
1016 * of the radio header. We should handle that the
1017 * same way we handle the length at the beginning
1018 * of the radiotap header.
1020 * XXX - in Linux, do any drivers that supply an AVS
1021 * header supply a link-layer type other than
1022 * ARPHRD_IEEE80211_PRISM? If so, we should map that
1023 * to DLT_IEEE802_11_RADIO_AVS; if not, or if there are
1024 * any drivers that supply an AVS header but supply
1025 * an ARPHRD value of ARPHRD_IEEE80211_PRISM, we'll
1026 * have to check the header in the generated code to
1027 * determine whether it's Prism or AVS.
1031 off_nl = 32; /* Radio+802.11+802.2+SNAP */
1032 off_nl_nosnap = 27; /* Radio+802.11+802.2 */
1037 * At the moment we treat PPI as normal Radiotap encoded
1038 * packets. The difference is in the function that generates
1039 * the code at the beginning to compute the header length.
1040 * Since this code generator of PPI supports bare 802.11
1041 * encapsulation only (i.e. the encapsulated DLT should be
1042 * DLT_IEEE802_11) we generate code to check for this too.
1045 case DLT_IEEE802_11_RADIO:
1047 * Same as 802.11, but with an additional header before
1048 * the 802.11 header, containing a bunch of additional
1049 * information including radio-level information.
1051 * The radiotap header is variable length, and we
1052 * generate code to compute its length and store it
1053 * in a register. These offsets are relative to the
1054 * beginning of the 802.11 header.
1057 off_nl = 32; /* 802.11+802.2+SNAP */
1058 off_nl_nosnap = 27; /* 802.11+802.2 */
1061 case DLT_ATM_RFC1483:
1062 case DLT_ATM_CLIP: /* Linux ATM defines this */
1064 * assume routed, non-ISO PDUs
1065 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1067 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1068 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1069 * latter would presumably be treated the way PPPoE
1070 * should be, so you can do "pppoe and udp port 2049"
1071 * or "pppoa and tcp port 80" and have it check for
1072 * PPPo{A,E} and a PPP protocol of IP and....
1075 off_nl = 8; /* 802.2+SNAP */
1076 off_nl_nosnap = 3; /* 802.2 */
1081 * Full Frontal ATM; you get AALn PDUs with an ATM
1085 off_vpi = SUNATM_VPI_POS;
1086 off_vci = SUNATM_VCI_POS;
1087 off_proto = PROTO_POS;
1088 off_mac = -1; /* LLC-encapsulated, so no MAC-layer header */
1089 off_payload = SUNATM_PKT_BEGIN_POS;
1090 off_linktype = off_payload;
1091 off_nl = off_payload+8; /* 802.2+SNAP */
1092 off_nl_nosnap = off_payload+3; /* 802.2 */
1098 off_nl_nosnap = 0; /* no 802.2 LLC */
1101 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1104 off_nl_nosnap = 16; /* no 802.2 LLC */
1109 * LocalTalk does have a 1-byte type field in the LLAP header,
1110 * but really it just indicates whether there is a "short" or
1111 * "long" DDP packet following.
1115 off_nl_nosnap = 0; /* no 802.2 LLC */
1118 case DLT_IP_OVER_FC:
1120 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1121 * link-level type field. We set "off_linktype" to the
1122 * offset of the LLC header.
1124 * To check for Ethernet types, we assume that SSAP = SNAP
1125 * is being used and pick out the encapsulated Ethernet type.
1126 * XXX - should we generate code to check for SNAP? RFC
1127 * 2625 says SNAP should be used.
1130 off_nl = 24; /* IPFC+802.2+SNAP */
1131 off_nl_nosnap = 19; /* IPFC+802.2 */
1136 * XXX - we should set this to handle SNAP-encapsulated
1137 * frames (NLPID of 0x80).
1141 off_nl_nosnap = 0; /* no 802.2 LLC */
1145 * the only BPF-interesting FRF.16 frames are non-control frames;
1146 * Frame Relay has a variable length link-layer
1147 * so lets start with offset 4 for now and increments later on (FIXME);
1152 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1155 case DLT_APPLE_IP_OVER_IEEE1394:
1158 off_nl_nosnap = 18; /* no 802.2 LLC */
1161 case DLT_LINUX_IRDA:
1163 * Currently, only raw "link[N:M]" filtering is supported.
1172 * Currently, only raw "link[N:M]" filtering is supported.
1179 case DLT_SYMANTEC_FIREWALL:
1181 off_nl = 44; /* Ethernet II */
1182 off_nl_nosnap = 44; /* XXX - what does it do with 802.3 packets? */
1185 #ifdef HAVE_NET_PFVAR_H
1188 off_nl = PFLOG_HDRLEN;
1189 off_nl_nosnap = PFLOG_HDRLEN; /* no 802.2 LLC */
1193 case DLT_JUNIPER_MFR:
1194 case DLT_JUNIPER_MLFR:
1195 case DLT_JUNIPER_MLPPP:
1196 case DLT_JUNIPER_PPP:
1197 case DLT_JUNIPER_CHDLC:
1198 case DLT_JUNIPER_FRELAY:
1201 off_nl_nosnap = -1; /* no 802.2 LLC */
1204 case DLT_JUNIPER_ATM1:
1205 off_linktype = 4; /* in reality variable between 4-8 */
1210 case DLT_JUNIPER_ATM2:
1211 off_linktype = 8; /* in reality variable between 8-12 */
1216 /* frames captured on a Juniper PPPoE service PIC
1217 * contain raw ethernet frames */
1218 case DLT_JUNIPER_PPPOE:
1219 case DLT_JUNIPER_ETHER:
1221 off_nl = 18; /* Ethernet II */
1222 off_nl_nosnap = 21; /* 802.3+802.2 */
1225 case DLT_JUNIPER_PPPOE_ATM:
1228 off_nl_nosnap = -1; /* no 802.2 LLC */
1231 case DLT_JUNIPER_GGSN:
1234 off_nl_nosnap = -1; /* no 802.2 LLC */
1237 case DLT_JUNIPER_ES:
1239 off_nl = -1; /* not really a network layer but raw IP adresses */
1240 off_nl_nosnap = -1; /* no 802.2 LLC */
1243 case DLT_JUNIPER_MONITOR:
1245 off_nl = 12; /* raw IP/IP6 header */
1246 off_nl_nosnap = -1; /* no 802.2 LLC */
1249 case DLT_JUNIPER_SERVICES:
1251 off_nl = -1; /* L3 proto location dep. on cookie type */
1252 off_nl_nosnap = -1; /* no 802.2 LLC */
1255 case DLT_JUNIPER_VP:
1272 case DLT_MTP2_WITH_PHDR:
1291 case DLT_LINUX_LAPD:
1293 * Currently, only raw "link[N:M]" filtering is supported.
1302 * Currently, only raw "link[N:M]" filtering is supported.
1309 case DLT_BLUETOOTH_HCI_H4:
1311 * Currently, only raw "link[N:M]" filtering is supported.
1318 bpf_error("unknown data link type %d", linktype);
1323 * Load a value relative to the beginning of the link-layer header.
1324 * The link-layer header doesn't necessarily begin at the beginning
1325 * of the packet data; there might be a variable-length prefix containing
1326 * radio information.
1328 static struct slist *
1329 gen_load_llrel(offset, size)
1332 struct slist *s, *s2;
1334 s = gen_llprefixlen();
1337 * If "s" is non-null, it has code to arrange that the X register
1338 * contains the length of the prefix preceding the link-layer
1341 * Otherwise, the length of the prefix preceding the link-layer
1342 * header is "off_ll".
1346 * There's a variable-length prefix preceding the
1347 * link-layer header. "s" points to a list of statements
1348 * that put the length of that prefix into the X register.
1349 * do an indirect load, to use the X register as an offset.
1351 s2 = new_stmt(BPF_LD|BPF_IND|size);
1356 * There is no variable-length header preceding the
1357 * link-layer header; add in off_ll, which, if there's
1358 * a fixed-length header preceding the link-layer header,
1359 * is the length of that header.
1361 s = new_stmt(BPF_LD|BPF_ABS|size);
1362 s->s.k = offset + off_ll;
1369 * Load a value relative to the beginning of the specified header.
1371 static struct slist *
1372 gen_load_a(offrel, offset, size)
1373 enum e_offrel offrel;
1376 struct slist *s, *s2;
1381 s = new_stmt(BPF_LD|BPF_ABS|size);
1386 s = gen_load_llrel(offset, size);
1390 s = gen_load_llrel(off_nl + offset, size);
1394 s = gen_load_llrel(off_nl_nosnap + offset, size);
1399 * Load the X register with the length of the IPv4 header
1400 * (plus the offset of the link-layer header, if it's
1401 * preceded by a variable-length header such as a radio
1402 * header), in bytes.
1404 s = gen_loadx_iphdrlen();
1407 * Load the item at {offset of the link-layer header} +
1408 * {offset, relative to the start of the link-layer
1409 * header, of the IPv4 header} + {length of the IPv4 header} +
1410 * {specified offset}.
1412 * (If the link-layer is variable-length, it's included
1413 * in the value in the X register, and off_ll is 0.)
1415 s2 = new_stmt(BPF_LD|BPF_IND|size);
1416 s2->s.k = off_ll + off_nl + offset;
1421 s = gen_load_llrel(off_nl + 40 + offset, size);
1432 * Generate code to load into the X register the sum of the length of
1433 * the IPv4 header and any variable-length header preceding the link-layer
1436 static struct slist *
1437 gen_loadx_iphdrlen()
1439 struct slist *s, *s2;
1441 s = gen_llprefixlen();
1444 * There's a variable-length prefix preceding the
1445 * link-layer header. "s" points to a list of statements
1446 * that put the length of that prefix into the X register.
1447 * The 4*([k]&0xf) addressing mode can't be used, as we
1448 * don't have a constant offset, so we have to load the
1449 * value in question into the A register and add to it
1450 * the value from the X register.
1452 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1455 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1458 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1463 * The A register now contains the length of the
1464 * IP header. We need to add to it the length
1465 * of the prefix preceding the link-layer
1466 * header, which is still in the X register, and
1467 * move the result into the X register.
1469 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1470 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1473 * There is no variable-length header preceding the
1474 * link-layer header; add in off_ll, which, if there's
1475 * a fixed-length header preceding the link-layer header,
1476 * is the length of that header.
1478 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1479 s->s.k = off_ll + off_nl;
1484 static struct block *
1491 s = new_stmt(BPF_LD|BPF_IMM);
1493 b = new_block(JMP(BPF_JEQ));
1499 static inline struct block *
1502 return gen_uncond(1);
1505 static inline struct block *
1508 return gen_uncond(0);
1512 * Byte-swap a 32-bit number.
1513 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1514 * big-endian platforms.)
1516 #define SWAPLONG(y) \
1517 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1520 * Generate code to match a particular packet type.
1522 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1523 * value, if <= ETHERMTU. We use that to determine whether to
1524 * match the type/length field or to check the type/length field for
1525 * a value <= ETHERMTU to see whether it's a type field and then do
1526 * the appropriate test.
1528 static struct block *
1529 gen_ether_linktype(proto)
1532 struct block *b0, *b1;
1538 case LLCSAP_NETBEUI:
1540 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1541 * so we check the DSAP and SSAP.
1543 * LLCSAP_IP checks for IP-over-802.2, rather
1544 * than IP-over-Ethernet or IP-over-SNAP.
1546 * XXX - should we check both the DSAP and the
1547 * SSAP, like this, or should we check just the
1548 * DSAP, as we do for other types <= ETHERMTU
1549 * (i.e., other SAP values)?
1551 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1553 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1554 ((proto << 8) | proto));
1562 * Ethernet_II frames, which are Ethernet
1563 * frames with a frame type of ETHERTYPE_IPX;
1565 * Ethernet_802.3 frames, which are 802.3
1566 * frames (i.e., the type/length field is
1567 * a length field, <= ETHERMTU, rather than
1568 * a type field) with the first two bytes
1569 * after the Ethernet/802.3 header being
1572 * Ethernet_802.2 frames, which are 802.3
1573 * frames with an 802.2 LLC header and
1574 * with the IPX LSAP as the DSAP in the LLC
1577 * Ethernet_SNAP frames, which are 802.3
1578 * frames with an LLC header and a SNAP
1579 * header and with an OUI of 0x000000
1580 * (encapsulated Ethernet) and a protocol
1581 * ID of ETHERTYPE_IPX in the SNAP header.
1583 * XXX - should we generate the same code both
1584 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1588 * This generates code to check both for the
1589 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1591 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1592 (bpf_int32)LLCSAP_IPX);
1593 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H,
1598 * Now we add code to check for SNAP frames with
1599 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1601 b0 = gen_snap(0x000000, ETHERTYPE_IPX, 14);
1605 * Now we generate code to check for 802.3
1606 * frames in general.
1608 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1612 * Now add the check for 802.3 frames before the
1613 * check for Ethernet_802.2 and Ethernet_802.3,
1614 * as those checks should only be done on 802.3
1615 * frames, not on Ethernet frames.
1620 * Now add the check for Ethernet_II frames, and
1621 * do that before checking for the other frame
1624 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1625 (bpf_int32)ETHERTYPE_IPX);
1629 case ETHERTYPE_ATALK:
1630 case ETHERTYPE_AARP:
1632 * EtherTalk (AppleTalk protocols on Ethernet link
1633 * layer) may use 802.2 encapsulation.
1637 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1638 * we check for an Ethernet type field less than
1639 * 1500, which means it's an 802.3 length field.
1641 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1645 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1646 * SNAP packets with an organization code of
1647 * 0x080007 (Apple, for Appletalk) and a protocol
1648 * type of ETHERTYPE_ATALK (Appletalk).
1650 * 802.2-encapsulated ETHERTYPE_AARP packets are
1651 * SNAP packets with an organization code of
1652 * 0x000000 (encapsulated Ethernet) and a protocol
1653 * type of ETHERTYPE_AARP (Appletalk ARP).
1655 if (proto == ETHERTYPE_ATALK)
1656 b1 = gen_snap(0x080007, ETHERTYPE_ATALK, 14);
1657 else /* proto == ETHERTYPE_AARP */
1658 b1 = gen_snap(0x000000, ETHERTYPE_AARP, 14);
1662 * Check for Ethernet encapsulation (Ethertalk
1663 * phase 1?); we just check for the Ethernet
1666 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1672 if (proto <= ETHERMTU) {
1674 * This is an LLC SAP value, so the frames
1675 * that match would be 802.2 frames.
1676 * Check that the frame is an 802.2 frame
1677 * (i.e., that the length/type field is
1678 * a length field, <= ETHERMTU) and
1679 * then check the DSAP.
1681 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1683 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1689 * This is an Ethernet type, so compare
1690 * the length/type field with it (if
1691 * the frame is an 802.2 frame, the length
1692 * field will be <= ETHERMTU, and, as
1693 * "proto" is > ETHERMTU, this test
1694 * will fail and the frame won't match,
1695 * which is what we want).
1697 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1704 * Generate code to match a particular packet type.
1706 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1707 * value, if <= ETHERMTU. We use that to determine whether to
1708 * match the type field or to check the type field for the special
1709 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1711 static struct block *
1712 gen_linux_sll_linktype(proto)
1715 struct block *b0, *b1;
1721 case LLCSAP_NETBEUI:
1723 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1724 * so we check the DSAP and SSAP.
1726 * LLCSAP_IP checks for IP-over-802.2, rather
1727 * than IP-over-Ethernet or IP-over-SNAP.
1729 * XXX - should we check both the DSAP and the
1730 * SSAP, like this, or should we check just the
1731 * DSAP, as we do for other types <= ETHERMTU
1732 * (i.e., other SAP values)?
1734 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1735 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1736 ((proto << 8) | proto));
1742 * Ethernet_II frames, which are Ethernet
1743 * frames with a frame type of ETHERTYPE_IPX;
1745 * Ethernet_802.3 frames, which have a frame
1746 * type of LINUX_SLL_P_802_3;
1748 * Ethernet_802.2 frames, which are 802.3
1749 * frames with an 802.2 LLC header (i.e, have
1750 * a frame type of LINUX_SLL_P_802_2) and
1751 * with the IPX LSAP as the DSAP in the LLC
1754 * Ethernet_SNAP frames, which are 802.3
1755 * frames with an LLC header and a SNAP
1756 * header and with an OUI of 0x000000
1757 * (encapsulated Ethernet) and a protocol
1758 * ID of ETHERTYPE_IPX in the SNAP header.
1760 * First, do the checks on LINUX_SLL_P_802_2
1761 * frames; generate the check for either
1762 * Ethernet_802.2 or Ethernet_SNAP frames, and
1763 * then put a check for LINUX_SLL_P_802_2 frames
1766 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1767 (bpf_int32)LLCSAP_IPX);
1768 b1 = gen_snap(0x000000, ETHERTYPE_IPX,
1771 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1775 * Now check for 802.3 frames and OR that with
1776 * the previous test.
1778 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1782 * Now add the check for Ethernet_II frames, and
1783 * do that before checking for the other frame
1786 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1787 (bpf_int32)ETHERTYPE_IPX);
1791 case ETHERTYPE_ATALK:
1792 case ETHERTYPE_AARP:
1794 * EtherTalk (AppleTalk protocols on Ethernet link
1795 * layer) may use 802.2 encapsulation.
1799 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1800 * we check for the 802.2 protocol type in the
1801 * "Ethernet type" field.
1803 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1806 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1807 * SNAP packets with an organization code of
1808 * 0x080007 (Apple, for Appletalk) and a protocol
1809 * type of ETHERTYPE_ATALK (Appletalk).
1811 * 802.2-encapsulated ETHERTYPE_AARP packets are
1812 * SNAP packets with an organization code of
1813 * 0x000000 (encapsulated Ethernet) and a protocol
1814 * type of ETHERTYPE_AARP (Appletalk ARP).
1816 if (proto == ETHERTYPE_ATALK)
1817 b1 = gen_snap(0x080007, ETHERTYPE_ATALK,
1819 else /* proto == ETHERTYPE_AARP */
1820 b1 = gen_snap(0x000000, ETHERTYPE_AARP,
1825 * Check for Ethernet encapsulation (Ethertalk
1826 * phase 1?); we just check for the Ethernet
1829 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1835 if (proto <= ETHERMTU) {
1837 * This is an LLC SAP value, so the frames
1838 * that match would be 802.2 frames.
1839 * Check for the 802.2 protocol type
1840 * in the "Ethernet type" field, and
1841 * then check the DSAP.
1843 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1845 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1851 * This is an Ethernet type, so compare
1852 * the length/type field with it (if
1853 * the frame is an 802.2 frame, the length
1854 * field will be <= ETHERMTU, and, as
1855 * "proto" is > ETHERMTU, this test
1856 * will fail and the frame won't match,
1857 * which is what we want).
1859 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1866 insert_radiotap_load_llprefixlen(b)
1869 struct slist *s1, *s2;
1872 * Prepend to the statements in this block code to load the
1873 * length of the radiotap header into the register assigned
1874 * to hold that length, if one has been assigned.
1876 if (reg_ll_size != -1) {
1878 * The 2 bytes at offsets of 2 and 3 from the beginning
1879 * of the radiotap header are the length of the radiotap
1880 * header; unfortunately, it's little-endian, so we have
1881 * to load it a byte at a time and construct the value.
1885 * Load the high-order byte, at an offset of 3, shift it
1886 * left a byte, and put the result in the X register.
1888 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1890 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1893 s2 = new_stmt(BPF_MISC|BPF_TAX);
1897 * Load the next byte, at an offset of 2, and OR the
1898 * value from the X register into it.
1900 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1903 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1907 * Now allocate a register to hold that value and store
1910 s2 = new_stmt(BPF_ST);
1911 s2->s.k = reg_ll_size;
1915 * Now move it into the X register.
1917 s2 = new_stmt(BPF_MISC|BPF_TAX);
1921 * Now append all the existing statements in this
1922 * block to these statements.
1924 sappend(s1, b->stmts);
1930 * At the moment we treat PPI as normal Radiotap encoded
1931 * packets. The difference is in the function that generates
1932 * the code at the beginning to compute the header length.
1933 * Since this code generator of PPI supports bare 802.11
1934 * encapsulation only (i.e. the encapsulated DLT should be
1935 * DLT_IEEE802_11) we generate code to check for this too.
1938 insert_ppi_load_llprefixlen(b)
1941 struct slist *s1, *s2;
1944 * Prepend to the statements in this block code to load the
1945 * length of the radiotap header into the register assigned
1946 * to hold that length, if one has been assigned.
1948 if (reg_ll_size != -1) {
1950 * The 2 bytes at offsets of 2 and 3 from the beginning
1951 * of the radiotap header are the length of the radiotap
1952 * header; unfortunately, it's little-endian, so we have
1953 * to load it a byte at a time and construct the value.
1957 * Load the high-order byte, at an offset of 3, shift it
1958 * left a byte, and put the result in the X register.
1960 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1962 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1965 s2 = new_stmt(BPF_MISC|BPF_TAX);
1969 * Load the next byte, at an offset of 2, and OR the
1970 * value from the X register into it.
1972 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1975 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1979 * Now allocate a register to hold that value and store
1982 s2 = new_stmt(BPF_ST);
1983 s2->s.k = reg_ll_size;
1987 * Now move it into the X register.
1989 s2 = new_stmt(BPF_MISC|BPF_TAX);
1993 * Now append all the existing statements in this
1994 * block to these statements.
1996 sappend(s1, b->stmts);
2002 static struct block *
2003 gen_ppi_dlt_check(void)
2005 struct slist *s_load_dlt;
2008 if (linktype == DLT_PPI)
2010 /* Create the statements that check for the DLT
2012 s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2013 s_load_dlt->s.k = 4;
2015 b = new_block(JMP(BPF_JEQ));
2017 b->stmts = s_load_dlt;
2018 b->s.k = SWAPLONG(DLT_IEEE802_11);
2029 insert_load_llprefixlen(b)
2035 * At the moment we treat PPI as normal Radiotap encoded
2036 * packets. The difference is in the function that generates
2037 * the code at the beginning to compute the header length.
2038 * Since this code generator of PPI supports bare 802.11
2039 * encapsulation only (i.e. the encapsulated DLT should be
2040 * DLT_IEEE802_11) we generate code to check for this too.
2043 insert_ppi_load_llprefixlen(b);
2046 case DLT_IEEE802_11_RADIO:
2047 insert_radiotap_load_llprefixlen(b);
2053 static struct slist *
2054 gen_radiotap_llprefixlen(void)
2058 if (reg_ll_size == -1) {
2060 * We haven't yet assigned a register for the length
2061 * of the radiotap header; allocate one.
2063 reg_ll_size = alloc_reg();
2067 * Load the register containing the radiotap length
2068 * into the X register.
2070 s = new_stmt(BPF_LDX|BPF_MEM);
2071 s->s.k = reg_ll_size;
2076 * At the moment we treat PPI as normal Radiotap encoded
2077 * packets. The difference is in the function that generates
2078 * the code at the beginning to compute the header length.
2079 * Since this code generator of PPI supports bare 802.11
2080 * encapsulation only (i.e. the encapsulated DLT should be
2081 * DLT_IEEE802_11) we generate code to check for this too.
2083 static struct slist *
2084 gen_ppi_llprefixlen(void)
2088 if (reg_ll_size == -1) {
2090 * We haven't yet assigned a register for the length
2091 * of the radiotap header; allocate one.
2093 reg_ll_size = alloc_reg();
2097 * Load the register containing the radiotap length
2098 * into the X register.
2100 s = new_stmt(BPF_LDX|BPF_MEM);
2101 s->s.k = reg_ll_size;
2108 * Generate code to compute the link-layer header length, if necessary,
2109 * putting it into the X register, and to return either a pointer to a
2110 * "struct slist" for the list of statements in that code, or NULL if
2111 * no code is necessary.
2113 static struct slist *
2114 gen_llprefixlen(void)
2119 return gen_ppi_llprefixlen();
2122 case DLT_IEEE802_11_RADIO:
2123 return gen_radiotap_llprefixlen();
2131 * Generate code to match a particular packet type by matching the
2132 * link-layer type field or fields in the 802.2 LLC header.
2134 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2135 * value, if <= ETHERMTU.
2137 static struct block *
2141 struct block *b0, *b1, *b2;
2143 /* are we checking MPLS-encapsulated packets? */
2144 if (label_stack_depth > 0) {
2148 /* FIXME add other L3 proto IDs */
2149 return gen_mpls_linktype(Q_IP);
2151 case ETHERTYPE_IPV6:
2153 /* FIXME add other L3 proto IDs */
2154 return gen_mpls_linktype(Q_IPV6);
2157 bpf_error("unsupported protocol over mpls");
2165 return gen_ether_linktype(proto);
2173 proto = (proto << 8 | LLCSAP_ISONS);
2177 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2187 case DLT_IEEE802_11:
2188 case DLT_IEEE802_11_RADIO_AVS:
2189 case DLT_IEEE802_11_RADIO:
2190 case DLT_PRISM_HEADER:
2191 case DLT_ATM_RFC1483:
2193 case DLT_IP_OVER_FC:
2194 return gen_llc_linktype(proto);
2200 * If "is_lane" is set, check for a LANE-encapsulated
2201 * version of this protocol, otherwise check for an
2202 * LLC-encapsulated version of this protocol.
2204 * We assume LANE means Ethernet, not Token Ring.
2208 * Check that the packet doesn't begin with an
2209 * LE Control marker. (We've already generated
2212 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
2217 * Now generate an Ethernet test.
2219 b1 = gen_ether_linktype(proto);
2224 * Check for LLC encapsulation and then check the
2227 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
2228 b1 = gen_llc_linktype(proto);
2236 return gen_linux_sll_linktype(proto);
2241 case DLT_SLIP_BSDOS:
2244 * These types don't provide any type field; packets
2245 * are always IPv4 or IPv6.
2247 * XXX - for IPv4, check for a version number of 4, and,
2248 * for IPv6, check for a version number of 6?
2253 /* Check for a version number of 4. */
2254 return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
2256 case ETHERTYPE_IPV6:
2257 /* Check for a version number of 6. */
2258 return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
2262 return gen_false(); /* always false */
2269 case DLT_PPP_SERIAL:
2272 * We use Ethernet protocol types inside libpcap;
2273 * map them to the corresponding PPP protocol types.
2282 case ETHERTYPE_IPV6:
2291 case ETHERTYPE_ATALK:
2305 * I'm assuming the "Bridging PDU"s that go
2306 * over PPP are Spanning Tree Protocol
2320 * We use Ethernet protocol types inside libpcap;
2321 * map them to the corresponding PPP protocol types.
2326 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
2327 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
2329 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
2334 case ETHERTYPE_IPV6:
2344 case ETHERTYPE_ATALK:
2358 * I'm assuming the "Bridging PDU"s that go
2359 * over PPP are Spanning Tree Protocol
2375 * For DLT_NULL, the link-layer header is a 32-bit
2376 * word containing an AF_ value in *host* byte order,
2377 * and for DLT_ENC, the link-layer header begins
2378 * with a 32-bit work containing an AF_ value in
2381 * In addition, if we're reading a saved capture file,
2382 * the host byte order in the capture may not be the
2383 * same as the host byte order on this machine.
2385 * For DLT_LOOP, the link-layer header is a 32-bit
2386 * word containing an AF_ value in *network* byte order.
2388 * XXX - AF_ values may, unfortunately, be platform-
2389 * dependent; for example, FreeBSD's AF_INET6 is 24
2390 * whilst NetBSD's and OpenBSD's is 26.
2392 * This means that, when reading a capture file, just
2393 * checking for our AF_INET6 value won't work if the
2394 * capture file came from another OS.
2403 case ETHERTYPE_IPV6:
2410 * Not a type on which we support filtering.
2411 * XXX - support those that have AF_ values
2412 * #defined on this platform, at least?
2417 if (linktype == DLT_NULL || linktype == DLT_ENC) {
2419 * The AF_ value is in host byte order, but
2420 * the BPF interpreter will convert it to
2421 * network byte order.
2423 * If this is a save file, and it's from a
2424 * machine with the opposite byte order to
2425 * ours, we byte-swap the AF_ value.
2427 * Then we run it through "htonl()", and
2428 * generate code to compare against the result.
2430 if (bpf_pcap->sf.rfile != NULL &&
2431 bpf_pcap->sf.swapped)
2432 proto = SWAPLONG(proto);
2433 proto = htonl(proto);
2435 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
2437 #ifdef HAVE_NET_PFVAR_H
2440 * af field is host byte order in contrast to the rest of
2443 if (proto == ETHERTYPE_IP)
2444 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2445 BPF_B, (bpf_int32)AF_INET));
2447 else if (proto == ETHERTYPE_IPV6)
2448 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2449 BPF_B, (bpf_int32)AF_INET6));
2455 #endif /* HAVE_NET_PFVAR_H */
2458 case DLT_ARCNET_LINUX:
2460 * XXX should we check for first fragment if the protocol
2469 case ETHERTYPE_IPV6:
2470 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2471 (bpf_int32)ARCTYPE_INET6));
2475 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2476 (bpf_int32)ARCTYPE_IP);
2477 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2478 (bpf_int32)ARCTYPE_IP_OLD);
2483 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2484 (bpf_int32)ARCTYPE_ARP);
2485 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2486 (bpf_int32)ARCTYPE_ARP_OLD);
2490 case ETHERTYPE_REVARP:
2491 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2492 (bpf_int32)ARCTYPE_REVARP));
2494 case ETHERTYPE_ATALK:
2495 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2496 (bpf_int32)ARCTYPE_ATALK));
2503 case ETHERTYPE_ATALK:
2513 * XXX - assumes a 2-byte Frame Relay header with
2514 * DLCI and flags. What if the address is longer?
2520 * Check for the special NLPID for IP.
2522 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
2525 case ETHERTYPE_IPV6:
2527 * Check for the special NLPID for IPv6.
2529 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
2534 * Check for several OSI protocols.
2536 * Frame Relay packets typically have an OSI
2537 * NLPID at the beginning; we check for each
2540 * What we check for is the NLPID and a frame
2541 * control field of UI, i.e. 0x03 followed
2544 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
2545 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
2546 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
2557 case DLT_JUNIPER_MFR:
2558 case DLT_JUNIPER_MLFR:
2559 case DLT_JUNIPER_MLPPP:
2560 case DLT_JUNIPER_ATM1:
2561 case DLT_JUNIPER_ATM2:
2562 case DLT_JUNIPER_PPPOE:
2563 case DLT_JUNIPER_PPPOE_ATM:
2564 case DLT_JUNIPER_GGSN:
2565 case DLT_JUNIPER_ES:
2566 case DLT_JUNIPER_MONITOR:
2567 case DLT_JUNIPER_SERVICES:
2568 case DLT_JUNIPER_ETHER:
2569 case DLT_JUNIPER_PPP:
2570 case DLT_JUNIPER_FRELAY:
2571 case DLT_JUNIPER_CHDLC:
2572 case DLT_JUNIPER_VP:
2573 /* just lets verify the magic number for now -
2574 * on ATM we may have up to 6 different encapsulations on the wire
2575 * and need a lot of heuristics to figure out that the payload
2578 * FIXME encapsulation specific BPF_ filters
2580 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
2582 case DLT_LINUX_IRDA:
2583 bpf_error("IrDA link-layer type filtering not implemented");
2586 bpf_error("DOCSIS link-layer type filtering not implemented");
2588 case DLT_LINUX_LAPD:
2589 bpf_error("LAPD link-layer type filtering not implemented");
2593 * All the types that have no encapsulation should either be
2594 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2595 * all packets are IP packets, or should be handled in some
2596 * special case, if none of them are (if some are and some
2597 * aren't, the lack of encapsulation is a problem, as we'd
2598 * have to find some other way of determining the packet type).
2600 * Therefore, if "off_linktype" is -1, there's an error.
2602 if (off_linktype == (u_int)-1)
2606 * Any type not handled above should always have an Ethernet
2607 * type at an offset of "off_linktype". (PPP is partially
2608 * handled above - the protocol type is mapped from the
2609 * Ethernet and LLC types we use internally to the corresponding
2610 * PPP type - but the PPP type is always specified by a value
2611 * at "off_linktype", so we don't have to do the code generation
2614 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2618 * Check for an LLC SNAP packet with a given organization code and
2619 * protocol type; we check the entire contents of the 802.2 LLC and
2620 * snap headers, checking for DSAP and SSAP of SNAP and a control
2621 * field of 0x03 in the LLC header, and for the specified organization
2622 * code and protocol type in the SNAP header.
2624 static struct block *
2625 gen_snap(orgcode, ptype, offset)
2626 bpf_u_int32 orgcode;
2630 u_char snapblock[8];
2632 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
2633 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
2634 snapblock[2] = 0x03; /* control = UI */
2635 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
2636 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
2637 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
2638 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
2639 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
2640 return gen_bcmp(OR_LINK, offset, 8, snapblock);
2644 * Generate code to match a particular packet type, for link-layer types
2645 * using 802.2 LLC headers.
2647 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2648 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2650 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2651 * value, if <= ETHERMTU. We use that to determine whether to
2652 * match the DSAP or both DSAP and LSAP or to check the OUI and
2653 * protocol ID in a SNAP header.
2655 static struct block *
2656 gen_llc_linktype(proto)
2660 * XXX - handle token-ring variable-length header.
2666 case LLCSAP_NETBEUI:
2668 * XXX - should we check both the DSAP and the
2669 * SSAP, like this, or should we check just the
2670 * DSAP, as we do for other types <= ETHERMTU
2671 * (i.e., other SAP values)?
2673 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_u_int32)
2674 ((proto << 8) | proto));
2678 * XXX - are there ever SNAP frames for IPX on
2679 * non-Ethernet 802.x networks?
2681 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2682 (bpf_int32)LLCSAP_IPX);
2684 case ETHERTYPE_ATALK:
2686 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2687 * SNAP packets with an organization code of
2688 * 0x080007 (Apple, for Appletalk) and a protocol
2689 * type of ETHERTYPE_ATALK (Appletalk).
2691 * XXX - check for an organization code of
2692 * encapsulated Ethernet as well?
2694 return gen_snap(0x080007, ETHERTYPE_ATALK, off_linktype);
2698 * XXX - we don't have to check for IPX 802.3
2699 * here, but should we check for the IPX Ethertype?
2701 if (proto <= ETHERMTU) {
2703 * This is an LLC SAP value, so check
2706 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2710 * This is an Ethernet type; we assume that it's
2711 * unlikely that it'll appear in the right place
2712 * at random, and therefore check only the
2713 * location that would hold the Ethernet type
2714 * in a SNAP frame with an organization code of
2715 * 0x000000 (encapsulated Ethernet).
2717 * XXX - if we were to check for the SNAP DSAP and
2718 * LSAP, as per XXX, and were also to check for an
2719 * organization code of 0x000000 (encapsulated
2720 * Ethernet), we'd do
2722 * return gen_snap(0x000000, proto,
2725 * here; for now, we don't, as per the above.
2726 * I don't know whether it's worth the extra CPU
2727 * time to do the right check or not.
2729 return gen_cmp(OR_LINK, off_linktype+6, BPF_H,
2735 static struct block *
2736 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
2740 u_int src_off, dst_off;
2742 struct block *b0, *b1;
2756 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2757 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2763 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2764 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2771 b0 = gen_linktype(proto);
2772 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
2778 static struct block *
2779 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
2780 struct in6_addr *addr;
2781 struct in6_addr *mask;
2783 u_int src_off, dst_off;
2785 struct block *b0, *b1;
2800 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2801 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2807 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2808 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2815 /* this order is important */
2816 a = (u_int32_t *)addr;
2817 m = (u_int32_t *)mask;
2818 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
2819 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
2821 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
2823 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
2825 b0 = gen_linktype(proto);
2831 static struct block *
2832 gen_ehostop(eaddr, dir)
2833 register const u_char *eaddr;
2836 register struct block *b0, *b1;
2840 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
2843 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
2846 b0 = gen_ehostop(eaddr, Q_SRC);
2847 b1 = gen_ehostop(eaddr, Q_DST);
2853 b0 = gen_ehostop(eaddr, Q_SRC);
2854 b1 = gen_ehostop(eaddr, Q_DST);
2863 * Like gen_ehostop, but for DLT_FDDI
2865 static struct block *
2866 gen_fhostop(eaddr, dir)
2867 register const u_char *eaddr;
2870 struct block *b0, *b1;
2875 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
2877 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
2882 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
2884 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
2888 b0 = gen_fhostop(eaddr, Q_SRC);
2889 b1 = gen_fhostop(eaddr, Q_DST);
2895 b0 = gen_fhostop(eaddr, Q_SRC);
2896 b1 = gen_fhostop(eaddr, Q_DST);
2905 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2907 static struct block *
2908 gen_thostop(eaddr, dir)
2909 register const u_char *eaddr;
2912 register struct block *b0, *b1;
2916 return gen_bcmp(OR_LINK, 8, 6, eaddr);
2919 return gen_bcmp(OR_LINK, 2, 6, eaddr);
2922 b0 = gen_thostop(eaddr, Q_SRC);
2923 b1 = gen_thostop(eaddr, Q_DST);
2929 b0 = gen_thostop(eaddr, Q_SRC);
2930 b1 = gen_thostop(eaddr, Q_DST);
2939 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2941 static struct block *
2942 gen_wlanhostop(eaddr, dir)
2943 register const u_char *eaddr;
2946 register struct block *b0, *b1, *b2;
2947 register struct slist *s;
2954 * For control frames, there is no SA.
2956 * For management frames, SA is at an
2957 * offset of 10 from the beginning of
2960 * For data frames, SA is at an offset
2961 * of 10 from the beginning of the packet
2962 * if From DS is clear, at an offset of
2963 * 16 from the beginning of the packet
2964 * if From DS is set and To DS is clear,
2965 * and an offset of 24 from the beginning
2966 * of the packet if From DS is set and To DS
2971 * Generate the tests to be done for data frames
2974 * First, check for To DS set, i.e. check "link[1] & 0x01".
2976 s = gen_load_a(OR_LINK, 1, BPF_B);
2977 b1 = new_block(JMP(BPF_JSET));
2978 b1->s.k = 0x01; /* To DS */
2982 * If To DS is set, the SA is at 24.
2984 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
2988 * Now, check for To DS not set, i.e. check
2989 * "!(link[1] & 0x01)".
2991 s = gen_load_a(OR_LINK, 1, BPF_B);
2992 b2 = new_block(JMP(BPF_JSET));
2993 b2->s.k = 0x01; /* To DS */
2998 * If To DS is not set, the SA is at 16.
3000 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3004 * Now OR together the last two checks. That gives
3005 * the complete set of checks for data frames with
3011 * Now check for From DS being set, and AND that with
3012 * the ORed-together checks.
3014 s = gen_load_a(OR_LINK, 1, BPF_B);
3015 b1 = new_block(JMP(BPF_JSET));
3016 b1->s.k = 0x02; /* From DS */
3021 * Now check for data frames with From DS not set.
3023 s = gen_load_a(OR_LINK, 1, BPF_B);
3024 b2 = new_block(JMP(BPF_JSET));
3025 b2->s.k = 0x02; /* From DS */
3030 * If From DS isn't set, the SA is at 10.
3032 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3036 * Now OR together the checks for data frames with
3037 * From DS not set and for data frames with From DS
3038 * set; that gives the checks done for data frames.
3043 * Now check for a data frame.
3044 * I.e, check "link[0] & 0x08".
3046 gen_load_a(OR_LINK, 0, BPF_B);
3047 b1 = new_block(JMP(BPF_JSET));
3052 * AND that with the checks done for data frames.
3057 * If the high-order bit of the type value is 0, this
3058 * is a management frame.
3059 * I.e, check "!(link[0] & 0x08)".
3061 s = gen_load_a(OR_LINK, 0, BPF_B);
3062 b2 = new_block(JMP(BPF_JSET));
3068 * For management frames, the SA is at 10.
3070 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3074 * OR that with the checks done for data frames.
3075 * That gives the checks done for management and
3081 * If the low-order bit of the type value is 1,
3082 * this is either a control frame or a frame
3083 * with a reserved type, and thus not a
3086 * I.e., check "!(link[0] & 0x04)".
3088 s = gen_load_a(OR_LINK, 0, BPF_B);
3089 b1 = new_block(JMP(BPF_JSET));
3095 * AND that with the checks for data and management
3105 * For control frames, there is no DA.
3107 * For management frames, DA is at an
3108 * offset of 4 from the beginning of
3111 * For data frames, DA is at an offset
3112 * of 4 from the beginning of the packet
3113 * if To DS is clear and at an offset of
3114 * 16 from the beginning of the packet
3119 * Generate the tests to be done for data frames.
3121 * First, check for To DS set, i.e. "link[1] & 0x01".
3123 s = gen_load_a(OR_LINK, 1, BPF_B);
3124 b1 = new_block(JMP(BPF_JSET));
3125 b1->s.k = 0x01; /* To DS */
3129 * If To DS is set, the DA is at 16.
3131 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3135 * Now, check for To DS not set, i.e. check
3136 * "!(link[1] & 0x01)".
3138 s = gen_load_a(OR_LINK, 1, BPF_B);
3139 b2 = new_block(JMP(BPF_JSET));
3140 b2->s.k = 0x01; /* To DS */
3145 * If To DS is not set, the DA is at 4.
3147 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
3151 * Now OR together the last two checks. That gives
3152 * the complete set of checks for data frames.
3157 * Now check for a data frame.
3158 * I.e, check "link[0] & 0x08".
3160 s = gen_load_a(OR_LINK, 0, BPF_B);
3161 b1 = new_block(JMP(BPF_JSET));
3166 * AND that with the checks done for data frames.
3171 * If the high-order bit of the type value is 0, this
3172 * is a management frame.
3173 * I.e, check "!(link[0] & 0x08)".
3175 s = gen_load_a(OR_LINK, 0, BPF_B);
3176 b2 = new_block(JMP(BPF_JSET));
3182 * For management frames, the DA is at 4.
3184 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
3188 * OR that with the checks done for data frames.
3189 * That gives the checks done for management and
3195 * If the low-order bit of the type value is 1,
3196 * this is either a control frame or a frame
3197 * with a reserved type, and thus not a
3200 * I.e., check "!(link[0] & 0x04)".
3202 s = gen_load_a(OR_LINK, 0, BPF_B);
3203 b1 = new_block(JMP(BPF_JSET));
3209 * AND that with the checks for data and management
3216 b0 = gen_wlanhostop(eaddr, Q_SRC);
3217 b1 = gen_wlanhostop(eaddr, Q_DST);
3223 b0 = gen_wlanhostop(eaddr, Q_SRC);
3224 b1 = gen_wlanhostop(eaddr, Q_DST);
3233 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3234 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3235 * as the RFC states.)
3237 static struct block *
3238 gen_ipfchostop(eaddr, dir)
3239 register const u_char *eaddr;
3242 register struct block *b0, *b1;
3246 return gen_bcmp(OR_LINK, 10, 6, eaddr);
3249 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3252 b0 = gen_ipfchostop(eaddr, Q_SRC);
3253 b1 = gen_ipfchostop(eaddr, Q_DST);
3259 b0 = gen_ipfchostop(eaddr, Q_SRC);
3260 b1 = gen_ipfchostop(eaddr, Q_DST);
3269 * This is quite tricky because there may be pad bytes in front of the
3270 * DECNET header, and then there are two possible data packet formats that
3271 * carry both src and dst addresses, plus 5 packet types in a format that
3272 * carries only the src node, plus 2 types that use a different format and
3273 * also carry just the src node.
3277 * Instead of doing those all right, we just look for data packets with
3278 * 0 or 1 bytes of padding. If you want to look at other packets, that
3279 * will require a lot more hacking.
3281 * To add support for filtering on DECNET "areas" (network numbers)
3282 * one would want to add a "mask" argument to this routine. That would
3283 * make the filter even more inefficient, although one could be clever
3284 * and not generate masking instructions if the mask is 0xFFFF.
3286 static struct block *
3287 gen_dnhostop(addr, dir)
3291 struct block *b0, *b1, *b2, *tmp;
3292 u_int offset_lh; /* offset if long header is received */
3293 u_int offset_sh; /* offset if short header is received */
3298 offset_sh = 1; /* follows flags */
3299 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
3303 offset_sh = 3; /* follows flags, dstnode */
3304 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3308 /* Inefficient because we do our Calvinball dance twice */
3309 b0 = gen_dnhostop(addr, Q_SRC);
3310 b1 = gen_dnhostop(addr, Q_DST);
3316 /* Inefficient because we do our Calvinball dance twice */
3317 b0 = gen_dnhostop(addr, Q_SRC);
3318 b1 = gen_dnhostop(addr, Q_DST);
3323 bpf_error("ISO host filtering not implemented");
3328 b0 = gen_linktype(ETHERTYPE_DN);
3329 /* Check for pad = 1, long header case */
3330 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3331 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
3332 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
3333 BPF_H, (bpf_int32)ntohs((u_short)addr));
3335 /* Check for pad = 0, long header case */
3336 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
3337 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3340 /* Check for pad = 1, short header case */
3341 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3342 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
3343 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3346 /* Check for pad = 0, short header case */
3347 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
3348 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3352 /* Combine with test for linktype */
3358 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3359 * test the bottom-of-stack bit, and then check the version number
3360 * field in the IP header.
3362 static struct block *
3363 gen_mpls_linktype(proto)
3366 struct block *b0, *b1;
3371 /* match the bottom-of-stack bit */
3372 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3373 /* match the IPv4 version number */
3374 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
3379 /* match the bottom-of-stack bit */
3380 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3381 /* match the IPv4 version number */
3382 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
3391 static struct block *
3392 gen_host(addr, mask, proto, dir, type)
3399 struct block *b0, *b1;
3400 const char *typestr;
3410 b0 = gen_host(addr, mask, Q_IP, dir, type);
3412 * Only check for non-IPv4 addresses if we're not
3413 * checking MPLS-encapsulated packets.
3415 if (label_stack_depth == 0) {
3416 b1 = gen_host(addr, mask, Q_ARP, dir, type);
3418 b0 = gen_host(addr, mask, Q_RARP, dir, type);
3424 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
3427 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
3430 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
3433 bpf_error("'tcp' modifier applied to %s", typestr);
3436 bpf_error("'sctp' modifier applied to %s", typestr);
3439 bpf_error("'udp' modifier applied to %s", typestr);
3442 bpf_error("'icmp' modifier applied to %s", typestr);
3445 bpf_error("'igmp' modifier applied to %s", typestr);
3448 bpf_error("'igrp' modifier applied to %s", typestr);
3451 bpf_error("'pim' modifier applied to %s", typestr);
3454 bpf_error("'vrrp' modifier applied to %s", typestr);
3457 bpf_error("ATALK host filtering not implemented");
3460 bpf_error("AARP host filtering not implemented");
3463 return gen_dnhostop(addr, dir);
3466 bpf_error("SCA host filtering not implemented");
3469 bpf_error("LAT host filtering not implemented");
3472 bpf_error("MOPDL host filtering not implemented");
3475 bpf_error("MOPRC host filtering not implemented");
3479 bpf_error("'ip6' modifier applied to ip host");
3482 bpf_error("'icmp6' modifier applied to %s", typestr);
3486 bpf_error("'ah' modifier applied to %s", typestr);
3489 bpf_error("'esp' modifier applied to %s", typestr);
3492 bpf_error("ISO host filtering not implemented");
3495 bpf_error("'esis' modifier applied to %s", typestr);
3498 bpf_error("'isis' modifier applied to %s", typestr);
3501 bpf_error("'clnp' modifier applied to %s", typestr);
3504 bpf_error("'stp' modifier applied to %s", typestr);
3507 bpf_error("IPX host filtering not implemented");
3510 bpf_error("'netbeui' modifier applied to %s", typestr);
3513 bpf_error("'radio' modifier applied to %s", typestr);
3522 static struct block *
3523 gen_host6(addr, mask, proto, dir, type)
3524 struct in6_addr *addr;
3525 struct in6_addr *mask;
3530 const char *typestr;
3540 return gen_host6(addr, mask, Q_IPV6, dir, type);
3543 bpf_error("'ip' modifier applied to ip6 %s", typestr);
3546 bpf_error("'rarp' modifier applied to ip6 %s", typestr);
3549 bpf_error("'arp' modifier applied to ip6 %s", typestr);
3552 bpf_error("'sctp' modifier applied to %s", typestr);
3555 bpf_error("'tcp' modifier applied to %s", typestr);
3558 bpf_error("'udp' modifier applied to %s", typestr);
3561 bpf_error("'icmp' modifier applied to %s", typestr);
3564 bpf_error("'igmp' modifier applied to %s", typestr);
3567 bpf_error("'igrp' modifier applied to %s", typestr);
3570 bpf_error("'pim' modifier applied to %s", typestr);
3573 bpf_error("'vrrp' modifier applied to %s", typestr);
3576 bpf_error("ATALK host filtering not implemented");
3579 bpf_error("AARP host filtering not implemented");
3582 bpf_error("'decnet' modifier applied to ip6 %s", typestr);
3585 bpf_error("SCA host filtering not implemented");
3588 bpf_error("LAT host filtering not implemented");
3591 bpf_error("MOPDL host filtering not implemented");
3594 bpf_error("MOPRC host filtering not implemented");
3597 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
3600 bpf_error("'icmp6' modifier applied to %s", typestr);
3603 bpf_error("'ah' modifier applied to %s", typestr);
3606 bpf_error("'esp' modifier applied to %s", typestr);
3609 bpf_error("ISO host filtering not implemented");
3612 bpf_error("'esis' modifier applied to %s", typestr);
3615 bpf_error("'isis' modifier applied to %s", typestr);
3618 bpf_error("'clnp' modifier applied to %s", typestr);
3621 bpf_error("'stp' modifier applied to %s", typestr);
3624 bpf_error("IPX host filtering not implemented");
3627 bpf_error("'netbeui' modifier applied to %s", typestr);
3630 bpf_error("'radio' modifier applied to %s", typestr);
3640 static struct block *
3641 gen_gateway(eaddr, alist, proto, dir)
3642 const u_char *eaddr;
3643 bpf_u_int32 **alist;
3647 struct block *b0, *b1, *tmp;
3650 bpf_error("direction applied to 'gateway'");
3659 b0 = gen_ehostop(eaddr, Q_OR);
3662 b0 = gen_fhostop(eaddr, Q_OR);
3665 b0 = gen_thostop(eaddr, Q_OR);
3667 case DLT_IEEE802_11:
3668 case DLT_IEEE802_11_RADIO_AVS:
3670 case DLT_IEEE802_11_RADIO:
3671 case DLT_PRISM_HEADER:
3672 b0 = gen_wlanhostop(eaddr, Q_OR);
3677 * Check that the packet doesn't begin with an
3678 * LE Control marker. (We've already generated
3681 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3686 * Now check the MAC address.
3688 b0 = gen_ehostop(eaddr, Q_OR);
3692 case DLT_IP_OVER_FC:
3693 b0 = gen_ipfchostop(eaddr, Q_OR);
3697 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3699 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST);
3701 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR,
3710 bpf_error("illegal modifier of 'gateway'");
3716 gen_proto_abbrev(proto)
3725 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
3727 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
3733 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
3735 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
3741 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
3743 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
3749 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
3752 #ifndef IPPROTO_IGMP
3753 #define IPPROTO_IGMP 2
3757 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
3760 #ifndef IPPROTO_IGRP
3761 #define IPPROTO_IGRP 9
3764 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
3768 #define IPPROTO_PIM 103
3772 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
3774 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
3779 #ifndef IPPROTO_VRRP
3780 #define IPPROTO_VRRP 112
3784 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
3788 b1 = gen_linktype(ETHERTYPE_IP);
3792 b1 = gen_linktype(ETHERTYPE_ARP);
3796 b1 = gen_linktype(ETHERTYPE_REVARP);
3800 bpf_error("link layer applied in wrong context");
3803 b1 = gen_linktype(ETHERTYPE_ATALK);
3807 b1 = gen_linktype(ETHERTYPE_AARP);
3811 b1 = gen_linktype(ETHERTYPE_DN);
3815 b1 = gen_linktype(ETHERTYPE_SCA);
3819 b1 = gen_linktype(ETHERTYPE_LAT);
3823 b1 = gen_linktype(ETHERTYPE_MOPDL);
3827 b1 = gen_linktype(ETHERTYPE_MOPRC);
3832 b1 = gen_linktype(ETHERTYPE_IPV6);
3835 #ifndef IPPROTO_ICMPV6
3836 #define IPPROTO_ICMPV6 58
3839 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
3844 #define IPPROTO_AH 51
3847 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
3849 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
3855 #define IPPROTO_ESP 50
3858 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
3860 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
3866 b1 = gen_linktype(LLCSAP_ISONS);
3870 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
3874 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
3877 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
3878 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3879 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3881 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3883 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3885 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3889 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
3890 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3891 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3893 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3895 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3897 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3901 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
3902 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3903 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3905 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
3910 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3911 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3916 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3917 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3919 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3921 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3926 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3927 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3932 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3933 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3938 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
3942 b1 = gen_linktype(LLCSAP_8021D);
3946 b1 = gen_linktype(LLCSAP_IPX);
3950 b1 = gen_linktype(LLCSAP_NETBEUI);
3954 bpf_error("'radio' is not a valid protocol type");
3962 static struct block *
3969 s = gen_load_a(OR_NET, 6, BPF_H);
3970 b = new_block(JMP(BPF_JSET));
3979 * Generate a comparison to a port value in the transport-layer header
3980 * at the specified offset from the beginning of that header.
3982 * XXX - this handles a variable-length prefix preceding the link-layer
3983 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3984 * variable-length link-layer headers (such as Token Ring or 802.11
3987 static struct block *
3988 gen_portatom(off, v)
3992 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
3996 static struct block *
3997 gen_portatom6(off, v)
4001 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
4006 gen_portop(port, proto, dir)
4007 int port, proto, dir;
4009 struct block *b0, *b1, *tmp;
4011 /* ip proto 'proto' */
4012 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
4018 b1 = gen_portatom(0, (bpf_int32)port);
4022 b1 = gen_portatom(2, (bpf_int32)port);
4027 tmp = gen_portatom(0, (bpf_int32)port);
4028 b1 = gen_portatom(2, (bpf_int32)port);
4033 tmp = gen_portatom(0, (bpf_int32)port);
4034 b1 = gen_portatom(2, (bpf_int32)port);
4046 static struct block *
4047 gen_port(port, ip_proto, dir)
4052 struct block *b0, *b1, *tmp;
4057 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4058 * not LLC encapsulation with LLCSAP_IP.
4060 * For IEEE 802 networks - which includes 802.5 token ring
4061 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4062 * says that SNAP encapsulation is used, not LLC encapsulation
4065 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4066 * RFC 2225 say that SNAP encapsulation is used, not LLC
4067 * encapsulation with LLCSAP_IP.
4069 * So we always check for ETHERTYPE_IP.
4071 b0 = gen_linktype(ETHERTYPE_IP);
4077 b1 = gen_portop(port, ip_proto, dir);
4081 tmp = gen_portop(port, IPPROTO_TCP, dir);
4082 b1 = gen_portop(port, IPPROTO_UDP, dir);
4084 tmp = gen_portop(port, IPPROTO_SCTP, dir);
4097 gen_portop6(port, proto, dir)
4098 int port, proto, dir;
4100 struct block *b0, *b1, *tmp;
4102 /* ip6 proto 'proto' */
4103 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
4107 b1 = gen_portatom6(0, (bpf_int32)port);
4111 b1 = gen_portatom6(2, (bpf_int32)port);
4116 tmp = gen_portatom6(0, (bpf_int32)port);
4117 b1 = gen_portatom6(2, (bpf_int32)port);
4122 tmp = gen_portatom6(0, (bpf_int32)port);
4123 b1 = gen_portatom6(2, (bpf_int32)port);
4135 static struct block *
4136 gen_port6(port, ip_proto, dir)
4141 struct block *b0, *b1, *tmp;
4143 /* link proto ip6 */
4144 b0 = gen_linktype(ETHERTYPE_IPV6);
4150 b1 = gen_portop6(port, ip_proto, dir);
4154 tmp = gen_portop6(port, IPPROTO_TCP, dir);
4155 b1 = gen_portop6(port, IPPROTO_UDP, dir);
4157 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
4169 /* gen_portrange code */
4170 static struct block *
4171 gen_portrangeatom(off, v1, v2)
4175 struct block *b1, *b2;
4179 * Reverse the order of the ports, so v1 is the lower one.
4188 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
4189 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
4197 gen_portrangeop(port1, port2, proto, dir)
4202 struct block *b0, *b1, *tmp;
4204 /* ip proto 'proto' */
4205 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
4211 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4215 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4220 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4221 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4226 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4227 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4239 static struct block *
4240 gen_portrange(port1, port2, ip_proto, dir)
4245 struct block *b0, *b1, *tmp;
4248 b0 = gen_linktype(ETHERTYPE_IP);
4254 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
4258 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
4259 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
4261 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
4273 static struct block *
4274 gen_portrangeatom6(off, v1, v2)
4278 struct block *b1, *b2;
4282 * Reverse the order of the ports, so v1 is the lower one.
4291 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
4292 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
4300 gen_portrangeop6(port1, port2, proto, dir)
4305 struct block *b0, *b1, *tmp;
4307 /* ip6 proto 'proto' */
4308 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
4312 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4316 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4321 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4322 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4327 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4328 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4340 static struct block *
4341 gen_portrange6(port1, port2, ip_proto, dir)
4346 struct block *b0, *b1, *tmp;
4348 /* link proto ip6 */
4349 b0 = gen_linktype(ETHERTYPE_IPV6);
4355 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
4359 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
4360 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
4362 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
4375 lookup_proto(name, proto)
4376 register const char *name;
4386 v = pcap_nametoproto(name);
4387 if (v == PROTO_UNDEF)
4388 bpf_error("unknown ip proto '%s'", name);
4392 /* XXX should look up h/w protocol type based on linktype */
4393 v = pcap_nametoeproto(name);
4394 if (v == PROTO_UNDEF) {
4395 v = pcap_nametollc(name);
4396 if (v == PROTO_UNDEF)
4397 bpf_error("unknown ether proto '%s'", name);
4402 if (strcmp(name, "esis") == 0)
4404 else if (strcmp(name, "isis") == 0)
4406 else if (strcmp(name, "clnp") == 0)
4409 bpf_error("unknown osi proto '%s'", name);
4429 static struct block *
4430 gen_protochain(v, proto, dir)
4435 #ifdef NO_PROTOCHAIN
4436 return gen_proto(v, proto, dir);
4438 struct block *b0, *b;
4439 struct slist *s[100];
4440 int fix2, fix3, fix4, fix5;
4441 int ahcheck, again, end;
4443 int reg2 = alloc_reg();
4445 memset(s, 0, sizeof(s));
4446 fix2 = fix3 = fix4 = fix5 = 0;
4453 b0 = gen_protochain(v, Q_IP, dir);
4454 b = gen_protochain(v, Q_IPV6, dir);
4458 bpf_error("bad protocol applied for 'protochain'");
4463 * We don't handle variable-length radiotap here headers yet.
4464 * We might want to add BPF instructions to do the protochain
4465 * work, to simplify that and, on platforms that have a BPF
4466 * interpreter with the new instructions, let the filtering
4467 * be done in the kernel. (We already require a modified BPF
4468 * engine to do the protochain stuff, to support backward
4469 * branches, and backward branch support is unlikely to appear
4470 * in kernel BPF engines.)
4472 if (linktype == DLT_IEEE802_11_RADIO)
4473 bpf_error("'protochain' not supported with radiotap headers");
4475 if (linktype == DLT_PPI)
4476 bpf_error("'protochain' not supported with PPI headers");
4478 no_optimize = 1; /*this code is not compatible with optimzer yet */
4481 * s[0] is a dummy entry to protect other BPF insn from damage
4482 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4483 * hard to find interdependency made by jump table fixup.
4486 s[i] = new_stmt(0); /*dummy*/
4491 b0 = gen_linktype(ETHERTYPE_IP);
4494 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4495 s[i]->s.k = off_ll + off_nl + 9;
4497 /* X = ip->ip_hl << 2 */
4498 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
4499 s[i]->s.k = off_ll + off_nl;
4504 b0 = gen_linktype(ETHERTYPE_IPV6);
4506 /* A = ip6->ip_nxt */
4507 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4508 s[i]->s.k = off_ll + off_nl + 6;
4510 /* X = sizeof(struct ip6_hdr) */
4511 s[i] = new_stmt(BPF_LDX|BPF_IMM);
4517 bpf_error("unsupported proto to gen_protochain");
4521 /* again: if (A == v) goto end; else fall through; */
4523 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4525 s[i]->s.jt = NULL; /*later*/
4526 s[i]->s.jf = NULL; /*update in next stmt*/
4530 #ifndef IPPROTO_NONE
4531 #define IPPROTO_NONE 59
4533 /* if (A == IPPROTO_NONE) goto end */
4534 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4535 s[i]->s.jt = NULL; /*later*/
4536 s[i]->s.jf = NULL; /*update in next stmt*/
4537 s[i]->s.k = IPPROTO_NONE;
4538 s[fix5]->s.jf = s[i];
4543 if (proto == Q_IPV6) {
4544 int v6start, v6end, v6advance, j;
4547 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4548 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4549 s[i]->s.jt = NULL; /*later*/
4550 s[i]->s.jf = NULL; /*update in next stmt*/
4551 s[i]->s.k = IPPROTO_HOPOPTS;
4552 s[fix2]->s.jf = s[i];
4554 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4555 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4556 s[i]->s.jt = NULL; /*later*/
4557 s[i]->s.jf = NULL; /*update in next stmt*/
4558 s[i]->s.k = IPPROTO_DSTOPTS;
4560 /* if (A == IPPROTO_ROUTING) goto v6advance */
4561 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4562 s[i]->s.jt = NULL; /*later*/
4563 s[i]->s.jf = NULL; /*update in next stmt*/
4564 s[i]->s.k = IPPROTO_ROUTING;
4566 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4567 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4568 s[i]->s.jt = NULL; /*later*/
4569 s[i]->s.jf = NULL; /*later*/
4570 s[i]->s.k = IPPROTO_FRAGMENT;
4581 * X = X + (P[X + 1] + 1) * 8;
4584 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4586 /* A = P[X + packet head] */
4587 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4588 s[i]->s.k = off_ll + off_nl;
4591 s[i] = new_stmt(BPF_ST);
4595 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4598 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4602 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4604 /* A = P[X + packet head]; */
4605 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4606 s[i]->s.k = off_ll + off_nl;
4609 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4613 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4617 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4620 s[i] = new_stmt(BPF_LD|BPF_MEM);
4624 /* goto again; (must use BPF_JA for backward jump) */
4625 s[i] = new_stmt(BPF_JMP|BPF_JA);
4626 s[i]->s.k = again - i - 1;
4627 s[i - 1]->s.jf = s[i];
4631 for (j = v6start; j <= v6end; j++)
4632 s[j]->s.jt = s[v6advance];
4637 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4639 s[fix2]->s.jf = s[i];
4645 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4646 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4647 s[i]->s.jt = NULL; /*later*/
4648 s[i]->s.jf = NULL; /*later*/
4649 s[i]->s.k = IPPROTO_AH;
4651 s[fix3]->s.jf = s[ahcheck];
4658 * X = X + (P[X + 1] + 2) * 4;
4661 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4663 /* A = P[X + packet head]; */
4664 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4665 s[i]->s.k = off_ll + off_nl;
4668 s[i] = new_stmt(BPF_ST);
4672 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4675 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4679 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4681 /* A = P[X + packet head] */
4682 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4683 s[i]->s.k = off_ll + off_nl;
4686 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4690 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4694 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4697 s[i] = new_stmt(BPF_LD|BPF_MEM);
4701 /* goto again; (must use BPF_JA for backward jump) */
4702 s[i] = new_stmt(BPF_JMP|BPF_JA);
4703 s[i]->s.k = again - i - 1;
4708 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4710 s[fix2]->s.jt = s[end];
4711 s[fix4]->s.jf = s[end];
4712 s[fix5]->s.jt = s[end];
4719 for (i = 0; i < max - 1; i++)
4720 s[i]->next = s[i + 1];
4721 s[max - 1]->next = NULL;
4726 b = new_block(JMP(BPF_JEQ));
4727 b->stmts = s[1]; /*remember, s[0] is dummy*/
4739 * Generate code that checks whether the packet is a packet for protocol
4740 * <proto> and whether the type field in that protocol's header has
4741 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4742 * IP packet and checks the protocol number in the IP header against <v>.
4744 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4745 * against Q_IP and Q_IPV6.
4747 static struct block *
4748 gen_proto(v, proto, dir)
4753 struct block *b0, *b1;
4755 if (dir != Q_DEFAULT)
4756 bpf_error("direction applied to 'proto'");
4761 b0 = gen_proto(v, Q_IP, dir);
4762 b1 = gen_proto(v, Q_IPV6, dir);
4770 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4771 * not LLC encapsulation with LLCSAP_IP.
4773 * For IEEE 802 networks - which includes 802.5 token ring
4774 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4775 * says that SNAP encapsulation is used, not LLC encapsulation
4778 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4779 * RFC 2225 say that SNAP encapsulation is used, not LLC
4780 * encapsulation with LLCSAP_IP.
4782 * So we always check for ETHERTYPE_IP.
4784 b0 = gen_linktype(ETHERTYPE_IP);
4786 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
4788 b1 = gen_protochain(v, Q_IP);
4798 * Frame Relay packets typically have an OSI
4799 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4800 * generates code to check for all the OSI
4801 * NLPIDs, so calling it and then adding a check
4802 * for the particular NLPID for which we're
4803 * looking is bogus, as we can just check for
4806 * What we check for is the NLPID and a frame
4807 * control field value of UI, i.e. 0x03 followed
4810 * XXX - assumes a 2-byte Frame Relay header with
4811 * DLCI and flags. What if the address is longer?
4813 * XXX - what about SNAP-encapsulated frames?
4815 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
4821 * Cisco uses an Ethertype lookalike - for OSI,
4824 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
4825 /* OSI in C-HDLC is stuffed with a fudge byte */
4826 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
4831 b0 = gen_linktype(LLCSAP_ISONS);
4832 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
4838 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4840 * 4 is the offset of the PDU type relative to the IS-IS
4843 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
4848 bpf_error("arp does not encapsulate another protocol");
4852 bpf_error("rarp does not encapsulate another protocol");
4856 bpf_error("atalk encapsulation is not specifiable");
4860 bpf_error("decnet encapsulation is not specifiable");
4864 bpf_error("sca does not encapsulate another protocol");
4868 bpf_error("lat does not encapsulate another protocol");
4872 bpf_error("moprc does not encapsulate another protocol");
4876 bpf_error("mopdl does not encapsulate another protocol");
4880 return gen_linktype(v);
4883 bpf_error("'udp proto' is bogus");
4887 bpf_error("'tcp proto' is bogus");
4891 bpf_error("'sctp proto' is bogus");
4895 bpf_error("'icmp proto' is bogus");
4899 bpf_error("'igmp proto' is bogus");
4903 bpf_error("'igrp proto' is bogus");
4907 bpf_error("'pim proto' is bogus");
4911 bpf_error("'vrrp proto' is bogus");
4916 b0 = gen_linktype(ETHERTYPE_IPV6);
4918 b1 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
4920 b1 = gen_protochain(v, Q_IPV6);
4926 bpf_error("'icmp6 proto' is bogus");
4930 bpf_error("'ah proto' is bogus");
4933 bpf_error("'ah proto' is bogus");
4936 bpf_error("'stp proto' is bogus");
4939 bpf_error("'ipx proto' is bogus");
4942 bpf_error("'netbeui proto' is bogus");
4945 bpf_error("'radio proto' is bogus");
4956 register const char *name;
4959 int proto = q.proto;
4963 bpf_u_int32 mask, addr;
4965 bpf_u_int32 **alist;
4968 struct sockaddr_in *sin4;
4969 struct sockaddr_in6 *sin6;
4970 struct addrinfo *res, *res0;
4971 struct in6_addr mask128;
4973 struct block *b, *tmp;
4974 int port, real_proto;
4980 addr = pcap_nametonetaddr(name);
4982 bpf_error("unknown network '%s'", name);
4983 /* Left justify network addr and calculate its network mask */
4985 while (addr && (addr & 0xff000000) == 0) {
4989 return gen_host(addr, mask, proto, dir, q.addr);
4993 if (proto == Q_LINK) {
4997 eaddr = pcap_ether_hostton(name);
5000 "unknown ether host '%s'", name);
5001 b = gen_ehostop(eaddr, dir);
5006 eaddr = pcap_ether_hostton(name);
5009 "unknown FDDI host '%s'", name);
5010 b = gen_fhostop(eaddr, dir);
5015 eaddr = pcap_ether_hostton(name);
5018 "unknown token ring host '%s'", name);
5019 b = gen_thostop(eaddr, dir);
5023 case DLT_IEEE802_11:
5024 case DLT_IEEE802_11_RADIO_AVS:
5025 case DLT_IEEE802_11_RADIO:
5026 case DLT_PRISM_HEADER:
5028 eaddr = pcap_ether_hostton(name);
5031 "unknown 802.11 host '%s'", name);
5032 b = gen_wlanhostop(eaddr, dir);
5036 case DLT_IP_OVER_FC:
5037 eaddr = pcap_ether_hostton(name);
5040 "unknown Fibre Channel host '%s'", name);
5041 b = gen_ipfchostop(eaddr, dir);
5050 * Check that the packet doesn't begin
5051 * with an LE Control marker. (We've
5052 * already generated a test for LANE.)
5054 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
5058 eaddr = pcap_ether_hostton(name);
5061 "unknown ether host '%s'", name);
5062 b = gen_ehostop(eaddr, dir);
5068 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5069 } else if (proto == Q_DECNET) {
5070 unsigned short dn_addr = __pcap_nametodnaddr(name);
5072 * I don't think DECNET hosts can be multihomed, so
5073 * there is no need to build up a list of addresses
5075 return (gen_host(dn_addr, 0, proto, dir, q.addr));
5078 alist = pcap_nametoaddr(name);
5079 if (alist == NULL || *alist == NULL)
5080 bpf_error("unknown host '%s'", name);
5082 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
5084 b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
5086 tmp = gen_host(**alist++, 0xffffffff,
5087 tproto, dir, q.addr);
5093 memset(&mask128, 0xff, sizeof(mask128));
5094 res0 = res = pcap_nametoaddrinfo(name);
5096 bpf_error("unknown host '%s'", name);
5098 tproto = tproto6 = proto;
5099 if (off_linktype == -1 && tproto == Q_DEFAULT) {
5103 for (res = res0; res; res = res->ai_next) {
5104 switch (res->ai_family) {
5106 if (tproto == Q_IPV6)
5109 sin4 = (struct sockaddr_in *)
5111 tmp = gen_host(ntohl(sin4->sin_addr.s_addr),
5112 0xffffffff, tproto, dir, q.addr);
5115 if (tproto6 == Q_IP)
5118 sin6 = (struct sockaddr_in6 *)
5120 tmp = gen_host6(&sin6->sin6_addr,
5121 &mask128, tproto6, dir, q.addr);
5132 bpf_error("unknown host '%s'%s", name,
5133 (proto == Q_DEFAULT)
5135 : " for specified address family");
5142 if (proto != Q_DEFAULT &&
5143 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
5144 bpf_error("illegal qualifier of 'port'");
5145 if (pcap_nametoport(name, &port, &real_proto) == 0)
5146 bpf_error("unknown port '%s'", name);
5147 if (proto == Q_UDP) {
5148 if (real_proto == IPPROTO_TCP)
5149 bpf_error("port '%s' is tcp", name);
5150 else if (real_proto == IPPROTO_SCTP)
5151 bpf_error("port '%s' is sctp", name);
5153 /* override PROTO_UNDEF */
5154 real_proto = IPPROTO_UDP;
5156 if (proto == Q_TCP) {
5157 if (real_proto == IPPROTO_UDP)
5158 bpf_error("port '%s' is udp", name);
5160 else if (real_proto == IPPROTO_SCTP)
5161 bpf_error("port '%s' is sctp", name);
5163 /* override PROTO_UNDEF */
5164 real_proto = IPPROTO_TCP;
5166 if (proto == Q_SCTP) {
5167 if (real_proto == IPPROTO_UDP)
5168 bpf_error("port '%s' is udp", name);
5170 else if (real_proto == IPPROTO_TCP)
5171 bpf_error("port '%s' is tcp", name);
5173 /* override PROTO_UNDEF */
5174 real_proto = IPPROTO_SCTP;
5177 return gen_port(port, real_proto, dir);
5179 b = gen_port(port, real_proto, dir);
5180 gen_or(gen_port6(port, real_proto, dir), b);
5185 if (proto != Q_DEFAULT &&
5186 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
5187 bpf_error("illegal qualifier of 'portrange'");
5188 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
5189 bpf_error("unknown port in range '%s'", name);
5190 if (proto == Q_UDP) {
5191 if (real_proto == IPPROTO_TCP)
5192 bpf_error("port in range '%s' is tcp", name);
5193 else if (real_proto == IPPROTO_SCTP)
5194 bpf_error("port in range '%s' is sctp", name);
5196 /* override PROTO_UNDEF */
5197 real_proto = IPPROTO_UDP;
5199 if (proto == Q_TCP) {
5200 if (real_proto == IPPROTO_UDP)
5201 bpf_error("port in range '%s' is udp", name);
5202 else if (real_proto == IPPROTO_SCTP)
5203 bpf_error("port in range '%s' is sctp", name);
5205 /* override PROTO_UNDEF */
5206 real_proto = IPPROTO_TCP;
5208 if (proto == Q_SCTP) {
5209 if (real_proto == IPPROTO_UDP)
5210 bpf_error("port in range '%s' is udp", name);
5211 else if (real_proto == IPPROTO_TCP)
5212 bpf_error("port in range '%s' is tcp", name);
5214 /* override PROTO_UNDEF */
5215 real_proto = IPPROTO_SCTP;
5218 return gen_portrange(port1, port2, real_proto, dir);
5220 b = gen_portrange(port1, port2, real_proto, dir);
5221 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
5227 eaddr = pcap_ether_hostton(name);
5229 bpf_error("unknown ether host: %s", name);
5231 alist = pcap_nametoaddr(name);
5232 if (alist == NULL || *alist == NULL)
5233 bpf_error("unknown host '%s'", name);
5234 b = gen_gateway(eaddr, alist, proto, dir);
5238 bpf_error("'gateway' not supported in this configuration");
5242 real_proto = lookup_proto(name, proto);
5243 if (real_proto >= 0)
5244 return gen_proto(real_proto, proto, dir);
5246 bpf_error("unknown protocol: %s", name);
5249 real_proto = lookup_proto(name, proto);
5250 if (real_proto >= 0)
5251 return gen_protochain(real_proto, proto, dir);
5253 bpf_error("unknown protocol: %s", name);
5265 gen_mcode(s1, s2, masklen, q)
5266 register const char *s1, *s2;
5267 register int masklen;
5270 register int nlen, mlen;
5273 nlen = __pcap_atoin(s1, &n);
5274 /* Promote short ipaddr */
5278 mlen = __pcap_atoin(s2, &m);
5279 /* Promote short ipaddr */
5282 bpf_error("non-network bits set in \"%s mask %s\"",
5285 /* Convert mask len to mask */
5287 bpf_error("mask length must be <= 32");
5290 * X << 32 is not guaranteed by C to be 0; it's
5295 m = 0xffffffff << (32 - masklen);
5297 bpf_error("non-network bits set in \"%s/%d\"",
5304 return gen_host(n, m, q.proto, q.dir, q.addr);
5307 bpf_error("Mask syntax for networks only");
5316 register const char *s;
5321 int proto = q.proto;
5327 else if (q.proto == Q_DECNET)
5328 vlen = __pcap_atodn(s, &v);
5330 vlen = __pcap_atoin(s, &v);
5337 if (proto == Q_DECNET)
5338 return gen_host(v, 0, proto, dir, q.addr);
5339 else if (proto == Q_LINK) {
5340 bpf_error("illegal link layer address");
5343 if (s == NULL && q.addr == Q_NET) {
5344 /* Promote short net number */
5345 while (v && (v & 0xff000000) == 0) {
5350 /* Promote short ipaddr */
5354 return gen_host(v, mask, proto, dir, q.addr);
5359 proto = IPPROTO_UDP;
5360 else if (proto == Q_TCP)
5361 proto = IPPROTO_TCP;
5362 else if (proto == Q_SCTP)
5363 proto = IPPROTO_SCTP;
5364 else if (proto == Q_DEFAULT)
5365 proto = PROTO_UNDEF;
5367 bpf_error("illegal qualifier of 'port'");
5370 return gen_port((int)v, proto, dir);
5374 b = gen_port((int)v, proto, dir);
5375 gen_or(gen_port6((int)v, proto, dir), b);
5382 proto = IPPROTO_UDP;
5383 else if (proto == Q_TCP)
5384 proto = IPPROTO_TCP;
5385 else if (proto == Q_SCTP)
5386 proto = IPPROTO_SCTP;
5387 else if (proto == Q_DEFAULT)
5388 proto = PROTO_UNDEF;
5390 bpf_error("illegal qualifier of 'portrange'");
5393 return gen_portrange((int)v, (int)v, proto, dir);
5397 b = gen_portrange((int)v, (int)v, proto, dir);
5398 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
5404 bpf_error("'gateway' requires a name");
5408 return gen_proto((int)v, proto, dir);
5411 return gen_protochain((int)v, proto, dir);
5426 gen_mcode6(s1, s2, masklen, q)
5427 register const char *s1, *s2;
5428 register int masklen;
5431 struct addrinfo *res;
5432 struct in6_addr *addr;
5433 struct in6_addr mask;
5438 bpf_error("no mask %s supported", s2);
5440 res = pcap_nametoaddrinfo(s1);
5442 bpf_error("invalid ip6 address %s", s1);
5444 bpf_error("%s resolved to multiple address", s1);
5445 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
5447 if (sizeof(mask) * 8 < masklen)
5448 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
5449 memset(&mask, 0, sizeof(mask));
5450 memset(&mask, 0xff, masklen / 8);
5452 mask.s6_addr[masklen / 8] =
5453 (0xff << (8 - masklen % 8)) & 0xff;
5456 a = (u_int32_t *)addr;
5457 m = (u_int32_t *)&mask;
5458 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
5459 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
5460 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
5468 bpf_error("Mask syntax for networks only");
5472 b = gen_host6(addr, &mask, q.proto, q.dir, q.addr);
5477 bpf_error("invalid qualifier against IPv6 address");
5486 register const u_char *eaddr;
5489 struct block *b, *tmp;
5491 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
5494 return gen_ehostop(eaddr, (int)q.dir);
5496 return gen_fhostop(eaddr, (int)q.dir);
5498 return gen_thostop(eaddr, (int)q.dir);
5499 case DLT_IEEE802_11:
5500 case DLT_IEEE802_11_RADIO_AVS:
5501 case DLT_IEEE802_11_RADIO:
5502 case DLT_PRISM_HEADER:
5504 return gen_wlanhostop(eaddr, (int)q.dir);
5508 * Check that the packet doesn't begin with an
5509 * LE Control marker. (We've already generated
5512 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5517 * Now check the MAC address.
5519 b = gen_ehostop(eaddr, (int)q.dir);
5524 case DLT_IP_OVER_FC:
5525 return gen_ipfchostop(eaddr, (int)q.dir);
5527 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5531 bpf_error("ethernet address used in non-ether expression");
5538 struct slist *s0, *s1;
5541 * This is definitely not the best way to do this, but the
5542 * lists will rarely get long.
5549 static struct slist *
5555 s = new_stmt(BPF_LDX|BPF_MEM);
5560 static struct slist *
5566 s = new_stmt(BPF_LD|BPF_MEM);
5572 * Modify "index" to use the value stored into its register as an
5573 * offset relative to the beginning of the header for the protocol
5574 * "proto", and allocate a register and put an item "size" bytes long
5575 * (1, 2, or 4) at that offset into that register, making it the register
5579 gen_load(proto, inst, size)
5584 struct slist *s, *tmp;
5586 int regno = alloc_reg();
5588 free_reg(inst->regno);
5592 bpf_error("data size must be 1, 2, or 4");
5608 bpf_error("unsupported index operation");
5612 * The offset is relative to the beginning of the packet
5613 * data, if we have a radio header. (If we don't, this
5616 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
5617 linktype != DLT_IEEE802_11_RADIO &&
5618 linktype != DLT_PRISM_HEADER)
5619 bpf_error("radio information not present in capture");
5622 * Load into the X register the offset computed into the
5623 * register specifed by "index".
5625 s = xfer_to_x(inst);
5628 * Load the item at that offset.
5630 tmp = new_stmt(BPF_LD|BPF_IND|size);
5632 sappend(inst->s, s);
5637 * The offset is relative to the beginning of
5638 * the link-layer header.
5640 * XXX - what about ATM LANE? Should the index be
5641 * relative to the beginning of the AAL5 frame, so
5642 * that 0 refers to the beginning of the LE Control
5643 * field, or relative to the beginning of the LAN
5644 * frame, so that 0 refers, for Ethernet LANE, to
5645 * the beginning of the destination address?
5647 s = gen_llprefixlen();
5650 * If "s" is non-null, it has code to arrange that the
5651 * X register contains the length of the prefix preceding
5652 * the link-layer header. Add to it the offset computed
5653 * into the register specified by "index", and move that
5654 * into the X register. Otherwise, just load into the X
5655 * register the offset computed into the register specifed
5659 sappend(s, xfer_to_a(inst));
5660 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5661 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5663 s = xfer_to_x(inst);
5666 * Load the item at the sum of the offset we've put in the
5667 * X register and the offset of the start of the link
5668 * layer header (which is 0 if the radio header is
5669 * variable-length; that header length is what we put
5670 * into the X register and then added to the index).
5672 tmp = new_stmt(BPF_LD|BPF_IND|size);
5675 sappend(inst->s, s);
5691 * The offset is relative to the beginning of
5692 * the network-layer header.
5693 * XXX - are there any cases where we want
5696 s = gen_llprefixlen();
5699 * If "s" is non-null, it has code to arrange that the
5700 * X register contains the length of the prefix preceding
5701 * the link-layer header. Add to it the offset computed
5702 * into the register specified by "index", and move that
5703 * into the X register. Otherwise, just load into the X
5704 * register the offset computed into the register specifed
5708 sappend(s, xfer_to_a(inst));
5709 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5710 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5712 s = xfer_to_x(inst);
5715 * Load the item at the sum of the offset we've put in the
5716 * X register, the offset of the start of the network
5717 * layer header, and the offset of the start of the link
5718 * layer header (which is 0 if the radio header is
5719 * variable-length; that header length is what we put
5720 * into the X register and then added to the index).
5722 tmp = new_stmt(BPF_LD|BPF_IND|size);
5723 tmp->s.k = off_ll + off_nl;
5725 sappend(inst->s, s);
5728 * Do the computation only if the packet contains
5729 * the protocol in question.
5731 b = gen_proto_abbrev(proto);
5733 gen_and(inst->b, b);
5746 * The offset is relative to the beginning of
5747 * the transport-layer header.
5749 * Load the X register with the length of the IPv4 header
5750 * (plus the offset of the link-layer header, if it's
5751 * a variable-length header), in bytes.
5753 * XXX - are there any cases where we want
5755 * XXX - we should, if we're built with
5756 * IPv6 support, generate code to load either
5757 * IPv4, IPv6, or both, as appropriate.
5759 s = gen_loadx_iphdrlen();
5762 * The X register now contains the sum of the length
5763 * of any variable-length header preceding the link-layer
5764 * header and the length of the network-layer header.
5765 * Load into the A register the offset relative to
5766 * the beginning of the transport layer header,
5767 * add the X register to that, move that to the
5768 * X register, and load with an offset from the
5769 * X register equal to the offset of the network
5770 * layer header relative to the beginning of
5771 * the link-layer header plus the length of any
5772 * fixed-length header preceding the link-layer
5775 sappend(s, xfer_to_a(inst));
5776 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5777 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5778 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
5779 tmp->s.k = off_ll + off_nl;
5780 sappend(inst->s, s);
5783 * Do the computation only if the packet contains
5784 * the protocol in question - which is true only
5785 * if this is an IP datagram and is the first or
5786 * only fragment of that datagram.
5788 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
5790 gen_and(inst->b, b);
5792 gen_and(gen_proto_abbrev(Q_IP), b);
5798 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5802 inst->regno = regno;
5803 s = new_stmt(BPF_ST);
5805 sappend(inst->s, s);
5811 gen_relation(code, a0, a1, reversed)
5813 struct arth *a0, *a1;
5816 struct slist *s0, *s1, *s2;
5817 struct block *b, *tmp;
5821 if (code == BPF_JEQ) {
5822 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
5823 b = new_block(JMP(code));
5827 b = new_block(BPF_JMP|code|BPF_X);
5833 sappend(a0->s, a1->s);
5837 free_reg(a0->regno);
5838 free_reg(a1->regno);
5840 /* 'and' together protocol checks */
5843 gen_and(a0->b, tmp = a1->b);
5859 int regno = alloc_reg();
5860 struct arth *a = (struct arth *)newchunk(sizeof(*a));
5863 s = new_stmt(BPF_LD|BPF_LEN);
5864 s->next = new_stmt(BPF_ST);
5865 s->next->s.k = regno;
5880 a = (struct arth *)newchunk(sizeof(*a));
5884 s = new_stmt(BPF_LD|BPF_IMM);
5886 s->next = new_stmt(BPF_ST);
5902 s = new_stmt(BPF_ALU|BPF_NEG);
5905 s = new_stmt(BPF_ST);
5913 gen_arth(code, a0, a1)
5915 struct arth *a0, *a1;
5917 struct slist *s0, *s1, *s2;
5921 s2 = new_stmt(BPF_ALU|BPF_X|code);
5926 sappend(a0->s, a1->s);
5928 free_reg(a0->regno);
5929 free_reg(a1->regno);
5931 s0 = new_stmt(BPF_ST);
5932 a0->regno = s0->s.k = alloc_reg();
5939 * Here we handle simple allocation of the scratch registers.
5940 * If too many registers are alloc'd, the allocator punts.
5942 static int regused[BPF_MEMWORDS];
5946 * Return the next free register.
5951 int n = BPF_MEMWORDS;
5954 if (regused[curreg])
5955 curreg = (curreg + 1) % BPF_MEMWORDS;
5957 regused[curreg] = 1;
5961 bpf_error("too many registers needed to evaluate expression");
5967 * Return a register to the table so it can
5977 static struct block *
5984 s = new_stmt(BPF_LD|BPF_LEN);
5985 b = new_block(JMP(jmp));
5996 return gen_len(BPF_JGE, n);
6000 * Actually, this is less than or equal.
6008 b = gen_len(BPF_JGT, n);
6015 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6016 * the beginning of the link-layer header.
6017 * XXX - that means you can't test values in the radiotap header, but
6018 * as that header is difficult if not impossible to parse generally
6019 * without a loop, that might not be a severe problem. A new keyword
6020 * "radio" could be added for that, although what you'd really want
6021 * would be a way of testing particular radio header values, which
6022 * would generate code appropriate to the radio header in question.
6025 gen_byteop(op, idx, val)
6036 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6039 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6043 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6047 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
6051 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
6055 b = new_block(JMP(BPF_JEQ));
6062 static u_char abroadcast[] = { 0x0 };
6065 gen_broadcast(proto)
6068 bpf_u_int32 hostmask;
6069 struct block *b0, *b1, *b2;
6070 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6078 case DLT_ARCNET_LINUX:
6079 return gen_ahostop(abroadcast, Q_DST);
6081 return gen_ehostop(ebroadcast, Q_DST);
6083 return gen_fhostop(ebroadcast, Q_DST);
6085 return gen_thostop(ebroadcast, Q_DST);
6086 case DLT_IEEE802_11:
6087 case DLT_IEEE802_11_RADIO_AVS:
6088 case DLT_IEEE802_11_RADIO:
6090 case DLT_PRISM_HEADER:
6091 return gen_wlanhostop(ebroadcast, Q_DST);
6092 case DLT_IP_OVER_FC:
6093 return gen_ipfchostop(ebroadcast, Q_DST);
6097 * Check that the packet doesn't begin with an
6098 * LE Control marker. (We've already generated
6101 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6106 * Now check the MAC address.
6108 b0 = gen_ehostop(ebroadcast, Q_DST);
6114 bpf_error("not a broadcast link");
6119 b0 = gen_linktype(ETHERTYPE_IP);
6120 hostmask = ~netmask;
6121 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
6122 b2 = gen_mcmp(OR_NET, 16, BPF_W,
6123 (bpf_int32)(~0 & hostmask), hostmask);
6128 bpf_error("only link-layer/IP broadcast filters supported");
6134 * Generate code to test the low-order bit of a MAC address (that's
6135 * the bottom bit of the *first* byte).
6137 static struct block *
6138 gen_mac_multicast(offset)
6141 register struct block *b0;
6142 register struct slist *s;
6144 /* link[offset] & 1 != 0 */
6145 s = gen_load_a(OR_LINK, offset, BPF_B);
6146 b0 = new_block(JMP(BPF_JSET));
6153 gen_multicast(proto)
6156 register struct block *b0, *b1, *b2;
6157 register struct slist *s;
6165 case DLT_ARCNET_LINUX:
6166 /* all ARCnet multicasts use the same address */
6167 return gen_ahostop(abroadcast, Q_DST);
6169 /* ether[0] & 1 != 0 */
6170 return gen_mac_multicast(0);
6173 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
6175 * XXX - was that referring to bit-order issues?
6177 /* fddi[1] & 1 != 0 */
6178 return gen_mac_multicast(1);
6180 /* tr[2] & 1 != 0 */
6181 return gen_mac_multicast(2);
6182 case DLT_IEEE802_11:
6183 case DLT_IEEE802_11_RADIO_AVS:
6185 case DLT_IEEE802_11_RADIO:
6186 case DLT_PRISM_HEADER:
6190 * For control frames, there is no DA.
6192 * For management frames, DA is at an
6193 * offset of 4 from the beginning of
6196 * For data frames, DA is at an offset
6197 * of 4 from the beginning of the packet
6198 * if To DS is clear and at an offset of
6199 * 16 from the beginning of the packet
6204 * Generate the tests to be done for data frames.
6206 * First, check for To DS set, i.e. "link[1] & 0x01".
6208 s = gen_load_a(OR_LINK, 1, BPF_B);
6209 b1 = new_block(JMP(BPF_JSET));
6210 b1->s.k = 0x01; /* To DS */
6214 * If To DS is set, the DA is at 16.
6216 b0 = gen_mac_multicast(16);
6220 * Now, check for To DS not set, i.e. check
6221 * "!(link[1] & 0x01)".
6223 s = gen_load_a(OR_LINK, 1, BPF_B);
6224 b2 = new_block(JMP(BPF_JSET));
6225 b2->s.k = 0x01; /* To DS */
6230 * If To DS is not set, the DA is at 4.
6232 b1 = gen_mac_multicast(4);
6236 * Now OR together the last two checks. That gives
6237 * the complete set of checks for data frames.
6242 * Now check for a data frame.
6243 * I.e, check "link[0] & 0x08".
6245 s = gen_load_a(OR_LINK, 0, BPF_B);
6246 b1 = new_block(JMP(BPF_JSET));
6251 * AND that with the checks done for data frames.
6256 * If the high-order bit of the type value is 0, this
6257 * is a management frame.
6258 * I.e, check "!(link[0] & 0x08)".
6260 s = gen_load_a(OR_LINK, 0, BPF_B);
6261 b2 = new_block(JMP(BPF_JSET));
6267 * For management frames, the DA is at 4.
6269 b1 = gen_mac_multicast(4);
6273 * OR that with the checks done for data frames.
6274 * That gives the checks done for management and
6280 * If the low-order bit of the type value is 1,
6281 * this is either a control frame or a frame
6282 * with a reserved type, and thus not a
6285 * I.e., check "!(link[0] & 0x04)".
6287 s = gen_load_a(OR_LINK, 0, BPF_B);
6288 b1 = new_block(JMP(BPF_JSET));
6294 * AND that with the checks for data and management
6299 case DLT_IP_OVER_FC:
6300 b0 = gen_mac_multicast(2);
6305 * Check that the packet doesn't begin with an
6306 * LE Control marker. (We've already generated
6309 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6313 /* ether[off_mac] & 1 != 0 */
6314 b0 = gen_mac_multicast(off_mac);
6322 /* Link not known to support multicasts */
6326 b0 = gen_linktype(ETHERTYPE_IP);
6327 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
6333 b0 = gen_linktype(ETHERTYPE_IPV6);
6334 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
6339 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6345 * generate command for inbound/outbound. It's here so we can
6346 * make it link-type specific. 'dir' = 0 implies "inbound",
6347 * = 1 implies "outbound".
6353 register struct block *b0;
6356 * Only some data link types support inbound/outbound qualifiers.
6360 b0 = gen_relation(BPF_JEQ,
6361 gen_load(Q_LINK, gen_loadi(0), 1),
6369 * Match packets sent by this machine.
6371 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
6374 * Match packets sent to this machine.
6375 * (No broadcast or multicast packets, or
6376 * packets sent to some other machine and
6377 * received promiscuously.)
6379 * XXX - packets sent to other machines probably
6380 * shouldn't be matched, but what about broadcast
6381 * or multicast packets we received?
6383 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_HOST);
6387 #ifdef HAVE_NET_PFVAR_H
6389 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
6390 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
6396 /* match outgoing packets */
6397 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
6399 /* match incoming packets */
6400 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
6404 case DLT_JUNIPER_MFR:
6405 case DLT_JUNIPER_MLFR:
6406 case DLT_JUNIPER_MLPPP:
6407 case DLT_JUNIPER_ATM1:
6408 case DLT_JUNIPER_ATM2:
6409 case DLT_JUNIPER_PPPOE:
6410 case DLT_JUNIPER_PPPOE_ATM:
6411 case DLT_JUNIPER_GGSN:
6412 case DLT_JUNIPER_ES:
6413 case DLT_JUNIPER_MONITOR:
6414 case DLT_JUNIPER_SERVICES:
6415 case DLT_JUNIPER_ETHER:
6416 case DLT_JUNIPER_PPP:
6417 case DLT_JUNIPER_FRELAY:
6418 case DLT_JUNIPER_CHDLC:
6419 case DLT_JUNIPER_VP:
6420 /* juniper flags (including direction) are stored
6421 * the byte after the 3-byte magic number */
6423 /* match outgoing packets */
6424 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
6426 /* match incoming packets */
6427 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
6432 bpf_error("inbound/outbound not supported on linktype %d",
6440 #ifdef HAVE_NET_PFVAR_H
6441 /* PF firewall log matched interface */
6443 gen_pf_ifname(const char *ifname)
6448 if (linktype == DLT_PFLOG) {
6449 len = sizeof(((struct pfloghdr *)0)->ifname);
6450 off = offsetof(struct pfloghdr, ifname);
6452 bpf_error("ifname not supported on linktype 0x%x", linktype);
6455 if (strlen(ifname) >= len) {
6456 bpf_error("ifname interface names can only be %d characters",
6460 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
6464 /* PF firewall log ruleset name */
6466 gen_pf_ruleset(char *ruleset)
6470 if (linktype != DLT_PFLOG) {
6471 bpf_error("ruleset not supported on linktype 0x%x", linktype);
6474 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
6475 bpf_error("ruleset names can only be %ld characters",
6476 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
6479 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
6480 strlen(ruleset), (const u_char *)ruleset);
6484 /* PF firewall log rule number */
6490 if (linktype == DLT_PFLOG) {
6491 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
6494 bpf_error("rnr not supported on linktype 0x%x", linktype);
6501 /* PF firewall log sub-rule number */
6503 gen_pf_srnr(int srnr)
6507 if (linktype != DLT_PFLOG) {
6508 bpf_error("srnr not supported on linktype 0x%x", linktype);
6512 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
6517 /* PF firewall log reason code */
6519 gen_pf_reason(int reason)
6523 if (linktype == DLT_PFLOG) {
6524 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
6527 bpf_error("reason not supported on linktype 0x%x", linktype);
6534 /* PF firewall log action */
6536 gen_pf_action(int action)
6540 if (linktype == DLT_PFLOG) {
6541 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
6544 bpf_error("action not supported on linktype 0x%x", linktype);
6550 #else /* !HAVE_NET_PFVAR_H */
6552 gen_pf_ifname(const char *ifname)
6554 bpf_error("libpcap was compiled without pf support");
6560 gen_pf_ruleset(char *ruleset)
6562 bpf_error("libpcap was compiled on a machine without pf support");
6570 bpf_error("libpcap was compiled on a machine without pf support");
6576 gen_pf_srnr(int srnr)
6578 bpf_error("libpcap was compiled on a machine without pf support");
6584 gen_pf_reason(int reason)
6586 bpf_error("libpcap was compiled on a machine without pf support");
6592 gen_pf_action(int action)
6594 bpf_error("libpcap was compiled on a machine without pf support");
6598 #endif /* HAVE_NET_PFVAR_H */
6602 register const u_char *eaddr;
6605 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6606 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
6607 return gen_ahostop(eaddr, (int)q.dir);
6609 bpf_error("ARCnet address used in non-arc expression");
6614 static struct block *
6615 gen_ahostop(eaddr, dir)
6616 register const u_char *eaddr;
6619 register struct block *b0, *b1;
6622 /* src comes first, different from Ethernet */
6624 return gen_bcmp(OR_LINK, 0, 1, eaddr);
6627 return gen_bcmp(OR_LINK, 1, 1, eaddr);
6630 b0 = gen_ahostop(eaddr, Q_SRC);
6631 b1 = gen_ahostop(eaddr, Q_DST);
6637 b0 = gen_ahostop(eaddr, Q_SRC);
6638 b1 = gen_ahostop(eaddr, Q_DST);
6647 * support IEEE 802.1Q VLAN trunk over ethernet
6653 struct block *b0, *b1;
6655 /* can't check for VLAN-encapsulated packets inside MPLS */
6656 if (label_stack_depth > 0)
6657 bpf_error("no VLAN match after MPLS");
6660 * Change the offsets to point to the type and data fields within
6661 * the VLAN packet. Just increment the offsets, so that we
6662 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6663 * capture VLAN 200 encapsulated within VLAN 100.
6665 * XXX - this is a bit of a kludge. If we were to split the
6666 * compiler into a parser that parses an expression and
6667 * generates an expression tree, and a code generator that
6668 * takes an expression tree (which could come from our
6669 * parser or from some other parser) and generates BPF code,
6670 * we could perhaps make the offsets parameters of routines
6671 * and, in the handler for an "AND" node, pass to subnodes
6672 * other than the VLAN node the adjusted offsets.
6674 * This would mean that "vlan" would, instead of changing the
6675 * behavior of *all* tests after it, change only the behavior
6676 * of tests ANDed with it. That would change the documented
6677 * semantics of "vlan", which might break some expressions.
6678 * However, it would mean that "(vlan and ip) or ip" would check
6679 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6680 * checking only for VLAN-encapsulated IP, so that could still
6681 * be considered worth doing; it wouldn't break expressions
6682 * that are of the form "vlan and ..." or "vlan N and ...",
6683 * which I suspect are the most common expressions involving
6684 * "vlan". "vlan or ..." doesn't necessarily do what the user
6685 * would really want, now, as all the "or ..." tests would
6686 * be done assuming a VLAN, even though the "or" could be viewed
6687 * as meaning "or, if this isn't a VLAN packet...".
6689 orig_linktype = off_linktype; /* save original values */
6701 bpf_error("no VLAN support for data link type %d",
6706 /* check for VLAN */
6707 b0 = gen_cmp(OR_LINK, orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q);
6709 /* If a specific VLAN is requested, check VLAN id */
6710 if (vlan_num >= 0) {
6711 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_H, (bpf_int32)vlan_num,
6727 struct block *b0,*b1;
6730 * Change the offsets to point to the type and data fields within
6731 * the MPLS packet. Just increment the offsets, so that we
6732 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6733 * capture packets with an outer label of 100000 and an inner
6736 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6740 if (label_stack_depth > 0) {
6741 /* just match the bottom-of-stack bit clear */
6742 b0 = gen_mcmp(OR_LINK, orig_nl-2, BPF_B, 0, 0x01);
6745 * Indicate that we're checking MPLS-encapsulated headers,
6746 * to make sure higher level code generators don't try to
6747 * match against IP-related protocols such as Q_ARP, Q_RARP
6752 case DLT_C_HDLC: /* fall through */
6754 b0 = gen_linktype(ETHERTYPE_MPLS);
6758 b0 = gen_linktype(PPP_MPLS_UCAST);
6761 /* FIXME add other DLT_s ...
6762 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6763 * leave it for now */
6766 bpf_error("no MPLS support for data link type %d",
6774 /* If a specific MPLS label is requested, check it */
6775 if (label_num >= 0) {
6776 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
6777 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_W, (bpf_int32)label_num,
6778 0xfffff000); /* only compare the first 20 bits */
6785 label_stack_depth++;
6790 * Support PPPOE discovery and session.
6795 /* check for PPPoE discovery */
6796 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
6805 * Test against the PPPoE session link-layer type.
6807 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
6810 * Change the offsets to point to the type and data fields within
6813 * XXX - this is a bit of a kludge. If we were to split the
6814 * compiler into a parser that parses an expression and
6815 * generates an expression tree, and a code generator that
6816 * takes an expression tree (which could come from our
6817 * parser or from some other parser) and generates BPF code,
6818 * we could perhaps make the offsets parameters of routines
6819 * and, in the handler for an "AND" node, pass to subnodes
6820 * other than the PPPoE node the adjusted offsets.
6822 * This would mean that "pppoes" would, instead of changing the
6823 * behavior of *all* tests after it, change only the behavior
6824 * of tests ANDed with it. That would change the documented
6825 * semantics of "pppoes", which might break some expressions.
6826 * However, it would mean that "(pppoes and ip) or ip" would check
6827 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6828 * checking only for VLAN-encapsulated IP, so that could still
6829 * be considered worth doing; it wouldn't break expressions
6830 * that are of the form "pppoes and ..." which I suspect are the
6831 * most common expressions involving "pppoes". "pppoes or ..."
6832 * doesn't necessarily do what the user would really want, now,
6833 * as all the "or ..." tests would be done assuming PPPoE, even
6834 * though the "or" could be viewed as meaning "or, if this isn't
6835 * a PPPoE packet...".
6837 orig_linktype = off_linktype; /* save original values */
6841 * The "network-layer" protocol is PPPoE, which has a 6-byte
6842 * PPPoE header, followed by PPP payload, so we set the
6843 * offsets to the network layer offset plus 6 bytes for
6844 * the PPPoE header plus the values appropriate for PPP when
6845 * encapsulated in Ethernet (which means there's no HDLC
6848 off_linktype = orig_nl + 6;
6849 off_nl = orig_nl + 6 + 2;
6850 off_nl_nosnap = orig_nl + 6 + 2;
6853 * Set the link-layer type to PPP, as all subsequent tests will
6854 * be on the encapsulated PPP header.
6862 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
6874 bpf_error("'vpi' supported only on raw ATM");
6875 if (off_vpi == (u_int)-1)
6877 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
6883 bpf_error("'vci' supported only on raw ATM");
6884 if (off_vci == (u_int)-1)
6886 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
6891 if (off_proto == (u_int)-1)
6892 abort(); /* XXX - this isn't on FreeBSD */
6893 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
6898 if (off_payload == (u_int)-1)
6900 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
6901 0xffffffff, jtype, reverse, jvalue);
6906 bpf_error("'callref' supported only on raw ATM");
6907 if (off_proto == (u_int)-1)
6909 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
6910 jtype, reverse, jvalue);
6920 gen_atmtype_abbrev(type)
6923 struct block *b0, *b1;
6928 /* Get all packets in Meta signalling Circuit */
6930 bpf_error("'metac' supported only on raw ATM");
6931 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6932 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
6937 /* Get all packets in Broadcast Circuit*/
6939 bpf_error("'bcc' supported only on raw ATM");
6940 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6941 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
6946 /* Get all cells in Segment OAM F4 circuit*/
6948 bpf_error("'oam4sc' supported only on raw ATM");
6949 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6950 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6955 /* Get all cells in End-to-End OAM F4 Circuit*/
6957 bpf_error("'oam4ec' supported only on raw ATM");
6958 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6959 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6964 /* Get all packets in connection Signalling Circuit */
6966 bpf_error("'sc' supported only on raw ATM");
6967 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6968 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
6973 /* Get all packets in ILMI Circuit */
6975 bpf_error("'ilmic' supported only on raw ATM");
6976 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6977 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
6982 /* Get all LANE packets */
6984 bpf_error("'lane' supported only on raw ATM");
6985 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
6988 * Arrange that all subsequent tests assume LANE
6989 * rather than LLC-encapsulated packets, and set
6990 * the offsets appropriately for LANE-encapsulated
6993 * "off_mac" is the offset of the Ethernet header,
6994 * which is 2 bytes past the ATM pseudo-header
6995 * (skipping the pseudo-header and 2-byte LE Client
6996 * field). The other offsets are Ethernet offsets
6997 * relative to "off_mac".
7000 off_mac = off_payload + 2; /* MAC header */
7001 off_linktype = off_mac + 12;
7002 off_nl = off_mac + 14; /* Ethernet II */
7003 off_nl_nosnap = off_mac + 17; /* 802.3+802.2 */
7007 /* Get all LLC-encapsulated packets */
7009 bpf_error("'llc' supported only on raw ATM");
7010 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
7021 * Filtering for MTP2 messages based on li value
7022 * FISU, length is null
7023 * LSSU, length is 1 or 2
7024 * MSU, length is 3 or more
7027 gen_mtp2type_abbrev(type)
7030 struct block *b0, *b1;
7035 if ( (linktype != DLT_MTP2) &&
7036 (linktype != DLT_MTP2_WITH_PHDR) )
7037 bpf_error("'fisu' supported only on MTP2");
7038 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
7039 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
7043 if ( (linktype != DLT_MTP2) &&
7044 (linktype != DLT_MTP2_WITH_PHDR) )
7045 bpf_error("'lssu' supported only on MTP2");
7046 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
7047 b1 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
7052 if ( (linktype != DLT_MTP2) &&
7053 (linktype != DLT_MTP2_WITH_PHDR) )
7054 bpf_error("'msu' supported only on MTP2");
7055 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
7065 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
7072 bpf_u_int32 val1 , val2 , val3;
7074 switch (mtp3field) {
7077 if (off_sio == (u_int)-1)
7078 bpf_error("'sio' supported only on SS7");
7079 /* sio coded on 1 byte so max value 255 */
7081 bpf_error("sio value %u too big; max value = 255",
7083 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
7084 (u_int)jtype, reverse, (u_int)jvalue);
7088 if (off_opc == (u_int)-1)
7089 bpf_error("'opc' supported only on SS7");
7090 /* opc coded on 14 bits so max value 16383 */
7092 bpf_error("opc value %u too big; max value = 16383",
7094 /* the following instructions are made to convert jvalue
7095 * to the form used to write opc in an ss7 message*/
7096 val1 = jvalue & 0x00003c00;
7098 val2 = jvalue & 0x000003fc;
7100 val3 = jvalue & 0x00000003;
7102 jvalue = val1 + val2 + val3;
7103 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
7104 (u_int)jtype, reverse, (u_int)jvalue);
7108 if (off_dpc == (u_int)-1)
7109 bpf_error("'dpc' supported only on SS7");
7110 /* dpc coded on 14 bits so max value 16383 */
7112 bpf_error("dpc value %u too big; max value = 16383",
7114 /* the following instructions are made to convert jvalue
7115 * to the forme used to write dpc in an ss7 message*/
7116 val1 = jvalue & 0x000000ff;
7118 val2 = jvalue & 0x00003f00;
7120 jvalue = val1 + val2;
7121 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
7122 (u_int)jtype, reverse, (u_int)jvalue);
7126 if (off_sls == (u_int)-1)
7127 bpf_error("'sls' supported only on SS7");
7128 /* sls coded on 4 bits so max value 15 */
7130 bpf_error("sls value %u too big; max value = 15",
7132 /* the following instruction is made to convert jvalue
7133 * to the forme used to write sls in an ss7 message*/
7134 jvalue = jvalue << 4;
7135 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
7136 (u_int)jtype,reverse, (u_int)jvalue);
7145 static struct block *
7146 gen_msg_abbrev(type)
7152 * Q.2931 signalling protocol messages for handling virtual circuits
7153 * establishment and teardown
7158 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
7162 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
7166 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
7170 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
7174 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
7177 case A_RELEASE_DONE:
7178 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
7188 gen_atmmulti_abbrev(type)
7191 struct block *b0, *b1;
7197 bpf_error("'oam' supported only on raw ATM");
7198 b1 = gen_atmmulti_abbrev(A_OAMF4);
7203 bpf_error("'oamf4' supported only on raw ATM");
7205 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
7206 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
7208 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
7214 * Get Q.2931 signalling messages for switched
7215 * virtual connection
7218 bpf_error("'connectmsg' supported only on raw ATM");
7219 b0 = gen_msg_abbrev(A_SETUP);
7220 b1 = gen_msg_abbrev(A_CALLPROCEED);
7222 b0 = gen_msg_abbrev(A_CONNECT);
7224 b0 = gen_msg_abbrev(A_CONNECTACK);
7226 b0 = gen_msg_abbrev(A_RELEASE);
7228 b0 = gen_msg_abbrev(A_RELEASE_DONE);
7230 b0 = gen_atmtype_abbrev(A_SC);
7236 bpf_error("'metaconnect' supported only on raw ATM");
7237 b0 = gen_msg_abbrev(A_SETUP);
7238 b1 = gen_msg_abbrev(A_CALLPROCEED);
7240 b0 = gen_msg_abbrev(A_CONNECT);
7242 b0 = gen_msg_abbrev(A_RELEASE);
7244 b0 = gen_msg_abbrev(A_RELEASE_DONE);
7246 b0 = gen_atmtype_abbrev(A_METAC);