1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid[] _U_ =
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.221.2.53 2007/09/12 19:17:24 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
72 #include "sunatmpos.h"
76 #ifdef HAVE_NET_PFVAR_H
77 #include <sys/socket.h>
79 #include <net/pfvar.h>
80 #include <net/if_pflog.h>
83 #define offsetof(s, e) ((size_t)&((s *)0)->e)
87 #include <netdb.h> /* for "struct addrinfo" */
90 #include <pcap-namedb.h>
95 #define IPPROTO_SCTP 132
98 #ifdef HAVE_OS_PROTO_H
102 #define JMP(c) ((c)|BPF_JMP|BPF_K)
105 static jmp_buf top_ctx;
106 static pcap_t *bpf_pcap;
109 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
110 static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
112 static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
117 static int pcap_fddipad;
122 bpf_error(const char *fmt, ...)
127 if (bpf_pcap != NULL)
128 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
135 static void init_linktype(pcap_t *);
137 static int alloc_reg(void);
138 static void free_reg(int);
140 static struct block *root;
143 * Value passed to gen_load_a() to indicate what the offset argument
147 OR_PACKET, /* relative to the beginning of the packet */
148 OR_LINK, /* relative to the link-layer header */
149 OR_NET, /* relative to the network-layer header */
150 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
151 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
152 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
156 * We divy out chunks of memory rather than call malloc each time so
157 * we don't have to worry about leaking memory. It's probably
158 * not a big deal if all this memory was wasted but if this ever
159 * goes into a library that would probably not be a good idea.
161 * XXX - this *is* in a library....
164 #define CHUNK0SIZE 1024
170 static struct chunk chunks[NCHUNKS];
171 static int cur_chunk;
173 static void *newchunk(u_int);
174 static void freechunks(void);
175 static inline struct block *new_block(int);
176 static inline struct slist *new_stmt(int);
177 static struct block *gen_retblk(int);
178 static inline void syntax(void);
180 static void backpatch(struct block *, struct block *);
181 static void merge(struct block *, struct block *);
182 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
183 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
184 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
185 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
186 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
187 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
189 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
190 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
191 bpf_u_int32, bpf_u_int32, int, bpf_int32);
192 static struct slist *gen_load_llrel(u_int, u_int);
193 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
194 static struct slist *gen_loadx_iphdrlen(void);
195 static struct block *gen_uncond(int);
196 static inline struct block *gen_true(void);
197 static inline struct block *gen_false(void);
198 static struct block *gen_ether_linktype(int);
199 static struct block *gen_linux_sll_linktype(int);
200 static void insert_radiotap_load_llprefixlen(struct block *);
201 static void insert_ppi_load_llprefixlen(struct block *);
202 static void insert_load_llprefixlen(struct block *);
203 static struct slist *gen_llprefixlen(void);
204 static struct block *gen_linktype(int);
205 static struct block *gen_snap(bpf_u_int32, bpf_u_int32, u_int);
206 static struct block *gen_llc_linktype(int);
207 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
209 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
211 static struct block *gen_ahostop(const u_char *, int);
212 static struct block *gen_ehostop(const u_char *, int);
213 static struct block *gen_fhostop(const u_char *, int);
214 static struct block *gen_thostop(const u_char *, int);
215 static struct block *gen_wlanhostop(const u_char *, int);
216 static struct block *gen_ipfchostop(const u_char *, int);
217 static struct block *gen_dnhostop(bpf_u_int32, int);
218 static struct block *gen_mpls_linktype(int);
219 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
221 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
224 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
226 static struct block *gen_ipfrag(void);
227 static struct block *gen_portatom(int, bpf_int32);
228 static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
230 static struct block *gen_portatom6(int, bpf_int32);
231 static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
233 struct block *gen_portop(int, int, int);
234 static struct block *gen_port(int, int, int);
235 struct block *gen_portrangeop(int, int, int, int);
236 static struct block *gen_portrange(int, int, int, int);
238 struct block *gen_portop6(int, int, int);
239 static struct block *gen_port6(int, int, int);
240 struct block *gen_portrangeop6(int, int, int, int);
241 static struct block *gen_portrange6(int, int, int, int);
243 static int lookup_proto(const char *, int);
244 static struct block *gen_protochain(int, int, int);
245 static struct block *gen_proto(int, int, int);
246 static struct slist *xfer_to_x(struct arth *);
247 static struct slist *xfer_to_a(struct arth *);
248 static struct block *gen_mac_multicast(int);
249 static struct block *gen_len(int, int);
251 static struct block *gen_ppi_dlt_check(void);
252 static struct block *gen_msg_abbrev(int type);
263 /* XXX Round up to nearest long. */
264 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
266 /* XXX Round up to structure boundary. */
270 cp = &chunks[cur_chunk];
271 if (n > cp->n_left) {
272 ++cp, k = ++cur_chunk;
274 bpf_error("out of memory");
275 size = CHUNK0SIZE << k;
276 cp->m = (void *)malloc(size);
278 bpf_error("out of memory");
279 memset((char *)cp->m, 0, size);
282 bpf_error("out of memory");
285 return (void *)((char *)cp->m + cp->n_left);
294 for (i = 0; i < NCHUNKS; ++i)
295 if (chunks[i].m != NULL) {
302 * A strdup whose allocations are freed after code generation is over.
306 register const char *s;
308 int n = strlen(s) + 1;
309 char *cp = newchunk(n);
315 static inline struct block *
321 p = (struct block *)newchunk(sizeof(*p));
328 static inline struct slist *
334 p = (struct slist *)newchunk(sizeof(*p));
340 static struct block *
344 struct block *b = new_block(BPF_RET|BPF_K);
353 bpf_error("syntax error in filter expression");
356 static bpf_u_int32 netmask;
361 pcap_compile(pcap_t *p, struct bpf_program *program,
362 const char *buf, int optimize, bpf_u_int32 mask)
365 const char * volatile xbuf = buf;
372 if (setjmp(top_ctx)) {
380 snaplen = pcap_snapshot(p);
382 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
383 "snaplen of 0 rejects all packets");
387 lex_init(xbuf ? xbuf : "");
395 root = gen_retblk(snaplen);
397 if (optimize && !no_optimize) {
400 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
401 bpf_error("expression rejects all packets");
403 program->bf_insns = icode_to_fcode(root, &len);
404 program->bf_len = len;
412 * entry point for using the compiler with no pcap open
413 * pass in all the stuff that is needed explicitly instead.
416 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
417 struct bpf_program *program,
418 const char *buf, int optimize, bpf_u_int32 mask)
423 p = pcap_open_dead(linktype_arg, snaplen_arg);
426 ret = pcap_compile(p, program, buf, optimize, mask);
432 * Clean up a "struct bpf_program" by freeing all the memory allocated
436 pcap_freecode(struct bpf_program *program)
439 if (program->bf_insns != NULL) {
440 free((char *)program->bf_insns);
441 program->bf_insns = NULL;
446 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
447 * which of the jt and jf fields has been resolved and which is a pointer
448 * back to another unresolved block (or nil). At least one of the fields
449 * in each block is already resolved.
452 backpatch(list, target)
453 struct block *list, *target;
470 * Merge the lists in b0 and b1, using the 'sense' field to indicate
471 * which of jt and jf is the link.
475 struct block *b0, *b1;
477 register struct block **p = &b0;
479 /* Find end of list. */
481 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
483 /* Concatenate the lists. */
492 struct block *ppi_dlt_check;
494 ppi_dlt_check = gen_ppi_dlt_check();
496 if (ppi_dlt_check != NULL)
498 gen_and(ppi_dlt_check, p);
501 backpatch(p, gen_retblk(snaplen));
502 p->sense = !p->sense;
503 backpatch(p, gen_retblk(0));
507 * Insert before the statements of the first (root) block any
508 * statements needed to load the lengths of any variable-length
509 * headers into registers.
511 * XXX - a fancier strategy would be to insert those before the
512 * statements of all blocks that use those lengths and that
513 * have no predecessors that use them, so that we only compute
514 * the lengths if we need them. There might be even better
515 * approaches than that. However, as we're currently only
516 * handling variable-length radiotap headers, and as all
517 * filtering expressions other than raw link[M:N] tests
518 * require the length of that header, doing more for that
519 * header length isn't really worth the effort.
522 insert_load_llprefixlen(root);
527 struct block *b0, *b1;
529 backpatch(b0, b1->head);
530 b0->sense = !b0->sense;
531 b1->sense = !b1->sense;
533 b1->sense = !b1->sense;
539 struct block *b0, *b1;
541 b0->sense = !b0->sense;
542 backpatch(b0, b1->head);
543 b0->sense = !b0->sense;
552 b->sense = !b->sense;
555 static struct block *
556 gen_cmp(offrel, offset, size, v)
557 enum e_offrel offrel;
561 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
564 static struct block *
565 gen_cmp_gt(offrel, offset, size, v)
566 enum e_offrel offrel;
570 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
573 static struct block *
574 gen_cmp_ge(offrel, offset, size, v)
575 enum e_offrel offrel;
579 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
582 static struct block *
583 gen_cmp_lt(offrel, offset, size, v)
584 enum e_offrel offrel;
588 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
591 static struct block *
592 gen_cmp_le(offrel, offset, size, v)
593 enum e_offrel offrel;
597 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
600 static struct block *
601 gen_mcmp(offrel, offset, size, v, mask)
602 enum e_offrel offrel;
607 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
610 static struct block *
611 gen_bcmp(offrel, offset, size, v)
612 enum e_offrel offrel;
613 register u_int offset, size;
614 register const u_char *v;
616 register struct block *b, *tmp;
620 register const u_char *p = &v[size - 4];
621 bpf_int32 w = ((bpf_int32)p[0] << 24) |
622 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
624 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
631 register const u_char *p = &v[size - 2];
632 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
634 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
641 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
650 * AND the field of size "size" at offset "offset" relative to the header
651 * specified by "offrel" with "mask", and compare it with the value "v"
652 * with the test specified by "jtype"; if "reverse" is true, the test
653 * should test the opposite of "jtype".
655 static struct block *
656 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
657 enum e_offrel offrel;
659 bpf_u_int32 offset, size, mask, jtype;
662 struct slist *s, *s2;
665 s = gen_load_a(offrel, offset, size);
667 if (mask != 0xffffffff) {
668 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
673 b = new_block(JMP(jtype));
676 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
682 * Various code constructs need to know the layout of the data link
683 * layer. These variables give the necessary offsets from the beginning
684 * of the packet data.
686 * If the link layer has variable_length headers, the offsets are offsets
687 * from the end of the link-link-layer header, and "reg_ll_size" is
688 * the register number for a register containing the length of the
689 * link-layer header. Otherwise, "reg_ll_size" is -1.
691 static int reg_ll_size;
694 * This is the offset of the beginning of the link-layer header from
695 * the beginning of the raw packet data.
697 * It's usually 0, except for 802.11 with a fixed-length radio header.
698 * (For 802.11 with a variable-length radio header, we have to generate
699 * code to compute that offset; off_ll is 0 in that case.)
704 * This is the offset of the beginning of the MAC-layer header.
705 * It's usually 0, except for ATM LANE, where it's the offset, relative
706 * to the beginning of the raw packet data, of the Ethernet header.
708 static u_int off_mac;
711 * "off_linktype" is the offset to information in the link-layer header
712 * giving the packet type. This offset is relative to the beginning
713 * of the link-layer header (i.e., it doesn't include off_ll).
715 * For Ethernet, it's the offset of the Ethernet type field.
717 * For link-layer types that always use 802.2 headers, it's the
718 * offset of the LLC header.
720 * For PPP, it's the offset of the PPP type field.
722 * For Cisco HDLC, it's the offset of the CHDLC type field.
724 * For BSD loopback, it's the offset of the AF_ value.
726 * For Linux cooked sockets, it's the offset of the type field.
728 * It's set to -1 for no encapsulation, in which case, IP is assumed.
730 static u_int off_linktype;
733 * TRUE if the link layer includes an ATM pseudo-header.
735 static int is_atm = 0;
738 * TRUE if "lane" appeared in the filter; it causes us to generate
739 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
741 static int is_lane = 0;
744 * These are offsets for the ATM pseudo-header.
746 static u_int off_vpi;
747 static u_int off_vci;
748 static u_int off_proto;
751 * These are offsets for the MTP2 fields.
756 * These are offsets for the MTP3 fields.
758 static u_int off_sio;
759 static u_int off_opc;
760 static u_int off_dpc;
761 static u_int off_sls;
764 * This is the offset of the first byte after the ATM pseudo_header,
765 * or -1 if there is no ATM pseudo-header.
767 static u_int off_payload;
770 * These are offsets to the beginning of the network-layer header.
771 * They are relative to the beginning of the link-layer header (i.e.,
772 * they don't include off_ll).
774 * If the link layer never uses 802.2 LLC:
776 * "off_nl" and "off_nl_nosnap" are the same.
778 * If the link layer always uses 802.2 LLC:
780 * "off_nl" is the offset if there's a SNAP header following
783 * "off_nl_nosnap" is the offset if there's no SNAP header.
785 * If the link layer is Ethernet:
787 * "off_nl" is the offset if the packet is an Ethernet II packet
788 * (we assume no 802.3+802.2+SNAP);
790 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
791 * with an 802.2 header following it.
794 static u_int off_nl_nosnap;
802 linktype = pcap_datalink(p);
804 pcap_fddipad = p->fddipad;
808 * Assume it's not raw ATM with a pseudo-header, for now.
819 * And assume we're not doing SS7.
828 * Also assume it's not 802.11 with a fixed-length radio header.
834 label_stack_depth = 0;
842 off_nl = 6; /* XXX in reality, variable! */
843 off_nl_nosnap = 6; /* no 802.2 LLC */
846 case DLT_ARCNET_LINUX:
848 off_nl = 8; /* XXX in reality, variable! */
849 off_nl_nosnap = 8; /* no 802.2 LLC */
854 off_nl = 14; /* Ethernet II */
855 off_nl_nosnap = 17; /* 802.3+802.2 */
860 * SLIP doesn't have a link level type. The 16 byte
861 * header is hacked into our SLIP driver.
865 off_nl_nosnap = 16; /* no 802.2 LLC */
869 /* XXX this may be the same as the DLT_PPP_BSDOS case */
873 off_nl_nosnap = 24; /* no 802.2 LLC */
880 off_nl_nosnap = 4; /* no 802.2 LLC */
886 off_nl_nosnap = 12; /* no 802.2 LLC */
891 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
892 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
895 off_nl_nosnap = 4; /* no 802.2 LLC */
900 * This does no include the Ethernet header, and
901 * only covers session state.
905 off_nl_nosnap = 8; /* no 802.2 LLC */
911 off_nl_nosnap = 24; /* no 802.2 LLC */
916 * FDDI doesn't really have a link-level type field.
917 * We set "off_linktype" to the offset of the LLC header.
919 * To check for Ethernet types, we assume that SSAP = SNAP
920 * is being used and pick out the encapsulated Ethernet type.
921 * XXX - should we generate code to check for SNAP?
925 off_linktype += pcap_fddipad;
927 off_nl = 21; /* FDDI+802.2+SNAP */
928 off_nl_nosnap = 16; /* FDDI+802.2 */
930 off_nl += pcap_fddipad;
931 off_nl_nosnap += pcap_fddipad;
937 * Token Ring doesn't really have a link-level type field.
938 * We set "off_linktype" to the offset of the LLC header.
940 * To check for Ethernet types, we assume that SSAP = SNAP
941 * is being used and pick out the encapsulated Ethernet type.
942 * XXX - should we generate code to check for SNAP?
944 * XXX - the header is actually variable-length.
945 * Some various Linux patched versions gave 38
946 * as "off_linktype" and 40 as "off_nl"; however,
947 * if a token ring packet has *no* routing
948 * information, i.e. is not source-routed, the correct
949 * values are 20 and 22, as they are in the vanilla code.
951 * A packet is source-routed iff the uppermost bit
952 * of the first byte of the source address, at an
953 * offset of 8, has the uppermost bit set. If the
954 * packet is source-routed, the total number of bytes
955 * of routing information is 2 plus bits 0x1F00 of
956 * the 16-bit value at an offset of 14 (shifted right
957 * 8 - figure out which byte that is).
960 off_nl = 22; /* Token Ring+802.2+SNAP */
961 off_nl_nosnap = 17; /* Token Ring+802.2 */
966 * 802.11 doesn't really have a link-level type field.
967 * We set "off_linktype" to the offset of the LLC header.
969 * To check for Ethernet types, we assume that SSAP = SNAP
970 * is being used and pick out the encapsulated Ethernet type.
971 * XXX - should we generate code to check for SNAP?
973 * XXX - the header is actually variable-length. We
974 * assume a 24-byte link-layer header, as appears in
975 * data frames in networks with no bridges. If the
976 * fromds and tods 802.11 header bits are both set,
977 * it's actually supposed to be 30 bytes.
980 off_nl = 32; /* 802.11+802.2+SNAP */
981 off_nl_nosnap = 27; /* 802.11+802.2 */
984 case DLT_PRISM_HEADER:
986 * Same as 802.11, but with an additional header before
987 * the 802.11 header, containing a bunch of additional
988 * information including radio-level information.
990 * The header is 144 bytes long.
992 * XXX - same variable-length header problem; at least
993 * the Prism header is fixed-length.
997 off_nl = 32; /* Prism+802.11+802.2+SNAP */
998 off_nl_nosnap = 27; /* Prism+802.11+802.2 */
1001 case DLT_IEEE802_11_RADIO_AVS:
1003 * Same as 802.11, but with an additional header before
1004 * the 802.11 header, containing a bunch of additional
1005 * information including radio-level information.
1007 * The header is 64 bytes long, at least in its
1008 * current incarnation.
1010 * XXX - same variable-length header problem, only
1011 * more so; this header is also variable-length,
1012 * with the length being the 32-bit big-endian
1013 * number at an offset of 4 from the beginning
1014 * of the radio header. We should handle that the
1015 * same way we handle the length at the beginning
1016 * of the radiotap header.
1018 * XXX - in Linux, do any drivers that supply an AVS
1019 * header supply a link-layer type other than
1020 * ARPHRD_IEEE80211_PRISM? If so, we should map that
1021 * to DLT_IEEE802_11_RADIO_AVS; if not, or if there are
1022 * any drivers that supply an AVS header but supply
1023 * an ARPHRD value of ARPHRD_IEEE80211_PRISM, we'll
1024 * have to check the header in the generated code to
1025 * determine whether it's Prism or AVS.
1029 off_nl = 32; /* Radio+802.11+802.2+SNAP */
1030 off_nl_nosnap = 27; /* Radio+802.11+802.2 */
1035 * At the moment we treat PPI as normal Radiotap encoded
1036 * packets. The difference is in the function that generates
1037 * the code at the beginning to compute the header length.
1038 * Since this code generator of PPI supports bare 802.11
1039 * encapsulation only (i.e. the encapsulated DLT should be
1040 * DLT_IEEE802_11) we generate code to check for this too.
1043 case DLT_IEEE802_11_RADIO:
1045 * Same as 802.11, but with an additional header before
1046 * the 802.11 header, containing a bunch of additional
1047 * information including radio-level information.
1049 * The radiotap header is variable length, and we
1050 * generate code to compute its length and store it
1051 * in a register. These offsets are relative to the
1052 * beginning of the 802.11 header.
1055 off_nl = 32; /* 802.11+802.2+SNAP */
1056 off_nl_nosnap = 27; /* 802.11+802.2 */
1059 case DLT_ATM_RFC1483:
1060 case DLT_ATM_CLIP: /* Linux ATM defines this */
1062 * assume routed, non-ISO PDUs
1063 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1065 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1066 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1067 * latter would presumably be treated the way PPPoE
1068 * should be, so you can do "pppoe and udp port 2049"
1069 * or "pppoa and tcp port 80" and have it check for
1070 * PPPo{A,E} and a PPP protocol of IP and....
1073 off_nl = 8; /* 802.2+SNAP */
1074 off_nl_nosnap = 3; /* 802.2 */
1079 * Full Frontal ATM; you get AALn PDUs with an ATM
1083 off_vpi = SUNATM_VPI_POS;
1084 off_vci = SUNATM_VCI_POS;
1085 off_proto = PROTO_POS;
1086 off_mac = -1; /* LLC-encapsulated, so no MAC-layer header */
1087 off_payload = SUNATM_PKT_BEGIN_POS;
1088 off_linktype = off_payload;
1089 off_nl = off_payload+8; /* 802.2+SNAP */
1090 off_nl_nosnap = off_payload+3; /* 802.2 */
1096 off_nl_nosnap = 0; /* no 802.2 LLC */
1099 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1102 off_nl_nosnap = 16; /* no 802.2 LLC */
1107 * LocalTalk does have a 1-byte type field in the LLAP header,
1108 * but really it just indicates whether there is a "short" or
1109 * "long" DDP packet following.
1113 off_nl_nosnap = 0; /* no 802.2 LLC */
1116 case DLT_IP_OVER_FC:
1118 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1119 * link-level type field. We set "off_linktype" to the
1120 * offset of the LLC header.
1122 * To check for Ethernet types, we assume that SSAP = SNAP
1123 * is being used and pick out the encapsulated Ethernet type.
1124 * XXX - should we generate code to check for SNAP? RFC
1125 * 2625 says SNAP should be used.
1128 off_nl = 24; /* IPFC+802.2+SNAP */
1129 off_nl_nosnap = 19; /* IPFC+802.2 */
1134 * XXX - we should set this to handle SNAP-encapsulated
1135 * frames (NLPID of 0x80).
1139 off_nl_nosnap = 0; /* no 802.2 LLC */
1143 * the only BPF-interesting FRF.16 frames are non-control frames;
1144 * Frame Relay has a variable length link-layer
1145 * so lets start with offset 4 for now and increments later on (FIXME);
1150 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1153 case DLT_APPLE_IP_OVER_IEEE1394:
1156 off_nl_nosnap = 18; /* no 802.2 LLC */
1159 case DLT_LINUX_IRDA:
1161 * Currently, only raw "link[N:M]" filtering is supported.
1170 * Currently, only raw "link[N:M]" filtering is supported.
1177 case DLT_SYMANTEC_FIREWALL:
1179 off_nl = 44; /* Ethernet II */
1180 off_nl_nosnap = 44; /* XXX - what does it do with 802.3 packets? */
1183 #ifdef HAVE_NET_PFVAR_H
1186 off_nl = PFLOG_HDRLEN;
1187 off_nl_nosnap = PFLOG_HDRLEN; /* no 802.2 LLC */
1191 case DLT_JUNIPER_MFR:
1192 case DLT_JUNIPER_MLFR:
1193 case DLT_JUNIPER_MLPPP:
1194 case DLT_JUNIPER_PPP:
1195 case DLT_JUNIPER_CHDLC:
1196 case DLT_JUNIPER_FRELAY:
1199 off_nl_nosnap = -1; /* no 802.2 LLC */
1202 case DLT_JUNIPER_ATM1:
1203 off_linktype = 4; /* in reality variable between 4-8 */
1208 case DLT_JUNIPER_ATM2:
1209 off_linktype = 8; /* in reality variable between 8-12 */
1214 /* frames captured on a Juniper PPPoE service PIC
1215 * contain raw ethernet frames */
1216 case DLT_JUNIPER_PPPOE:
1217 case DLT_JUNIPER_ETHER:
1219 off_nl = 18; /* Ethernet II */
1220 off_nl_nosnap = 21; /* 802.3+802.2 */
1223 case DLT_JUNIPER_PPPOE_ATM:
1226 off_nl_nosnap = -1; /* no 802.2 LLC */
1229 case DLT_JUNIPER_GGSN:
1232 off_nl_nosnap = -1; /* no 802.2 LLC */
1235 case DLT_JUNIPER_ES:
1237 off_nl = -1; /* not really a network layer but raw IP adresses */
1238 off_nl_nosnap = -1; /* no 802.2 LLC */
1241 case DLT_JUNIPER_MONITOR:
1243 off_nl = 12; /* raw IP/IP6 header */
1244 off_nl_nosnap = -1; /* no 802.2 LLC */
1247 case DLT_JUNIPER_SERVICES:
1249 off_nl = -1; /* L3 proto location dep. on cookie type */
1250 off_nl_nosnap = -1; /* no 802.2 LLC */
1253 case DLT_JUNIPER_VP:
1270 case DLT_MTP2_WITH_PHDR:
1289 case DLT_LINUX_LAPD:
1291 * Currently, only raw "link[N:M]" filtering is supported.
1300 * Currently, only raw "link[N:M]" filtering is supported.
1307 case DLT_BLUETOOTH_HCI_H4:
1309 * Currently, only raw "link[N:M]" filtering is supported.
1316 bpf_error("unknown data link type %d", linktype);
1321 * Load a value relative to the beginning of the link-layer header.
1322 * The link-layer header doesn't necessarily begin at the beginning
1323 * of the packet data; there might be a variable-length prefix containing
1324 * radio information.
1326 static struct slist *
1327 gen_load_llrel(offset, size)
1330 struct slist *s, *s2;
1332 s = gen_llprefixlen();
1335 * If "s" is non-null, it has code to arrange that the X register
1336 * contains the length of the prefix preceding the link-layer
1339 * Otherwise, the length of the prefix preceding the link-layer
1340 * header is "off_ll".
1344 * There's a variable-length prefix preceding the
1345 * link-layer header. "s" points to a list of statements
1346 * that put the length of that prefix into the X register.
1347 * do an indirect load, to use the X register as an offset.
1349 s2 = new_stmt(BPF_LD|BPF_IND|size);
1354 * There is no variable-length header preceding the
1355 * link-layer header; add in off_ll, which, if there's
1356 * a fixed-length header preceding the link-layer header,
1357 * is the length of that header.
1359 s = new_stmt(BPF_LD|BPF_ABS|size);
1360 s->s.k = offset + off_ll;
1367 * Load a value relative to the beginning of the specified header.
1369 static struct slist *
1370 gen_load_a(offrel, offset, size)
1371 enum e_offrel offrel;
1374 struct slist *s, *s2;
1379 s = new_stmt(BPF_LD|BPF_ABS|size);
1384 s = gen_load_llrel(offset, size);
1388 s = gen_load_llrel(off_nl + offset, size);
1392 s = gen_load_llrel(off_nl_nosnap + offset, size);
1397 * Load the X register with the length of the IPv4 header
1398 * (plus the offset of the link-layer header, if it's
1399 * preceded by a variable-length header such as a radio
1400 * header), in bytes.
1402 s = gen_loadx_iphdrlen();
1405 * Load the item at {offset of the link-layer header} +
1406 * {offset, relative to the start of the link-layer
1407 * header, of the IPv4 header} + {length of the IPv4 header} +
1408 * {specified offset}.
1410 * (If the link-layer is variable-length, it's included
1411 * in the value in the X register, and off_ll is 0.)
1413 s2 = new_stmt(BPF_LD|BPF_IND|size);
1414 s2->s.k = off_ll + off_nl + offset;
1419 s = gen_load_llrel(off_nl + 40 + offset, size);
1430 * Generate code to load into the X register the sum of the length of
1431 * the IPv4 header and any variable-length header preceding the link-layer
1434 static struct slist *
1435 gen_loadx_iphdrlen()
1437 struct slist *s, *s2;
1439 s = gen_llprefixlen();
1442 * There's a variable-length prefix preceding the
1443 * link-layer header. "s" points to a list of statements
1444 * that put the length of that prefix into the X register.
1445 * The 4*([k]&0xf) addressing mode can't be used, as we
1446 * don't have a constant offset, so we have to load the
1447 * value in question into the A register and add to it
1448 * the value from the X register.
1450 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1453 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1456 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1461 * The A register now contains the length of the
1462 * IP header. We need to add to it the length
1463 * of the prefix preceding the link-layer
1464 * header, which is still in the X register, and
1465 * move the result into the X register.
1467 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1468 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1471 * There is no variable-length header preceding the
1472 * link-layer header; add in off_ll, which, if there's
1473 * a fixed-length header preceding the link-layer header,
1474 * is the length of that header.
1476 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1477 s->s.k = off_ll + off_nl;
1482 static struct block *
1489 s = new_stmt(BPF_LD|BPF_IMM);
1491 b = new_block(JMP(BPF_JEQ));
1497 static inline struct block *
1500 return gen_uncond(1);
1503 static inline struct block *
1506 return gen_uncond(0);
1510 * Byte-swap a 32-bit number.
1511 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1512 * big-endian platforms.)
1514 #define SWAPLONG(y) \
1515 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1518 * Generate code to match a particular packet type.
1520 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1521 * value, if <= ETHERMTU. We use that to determine whether to
1522 * match the type/length field or to check the type/length field for
1523 * a value <= ETHERMTU to see whether it's a type field and then do
1524 * the appropriate test.
1526 static struct block *
1527 gen_ether_linktype(proto)
1530 struct block *b0, *b1;
1536 case LLCSAP_NETBEUI:
1538 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1539 * so we check the DSAP and SSAP.
1541 * LLCSAP_IP checks for IP-over-802.2, rather
1542 * than IP-over-Ethernet or IP-over-SNAP.
1544 * XXX - should we check both the DSAP and the
1545 * SSAP, like this, or should we check just the
1546 * DSAP, as we do for other types <= ETHERMTU
1547 * (i.e., other SAP values)?
1549 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1551 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1552 ((proto << 8) | proto));
1560 * Ethernet_II frames, which are Ethernet
1561 * frames with a frame type of ETHERTYPE_IPX;
1563 * Ethernet_802.3 frames, which are 802.3
1564 * frames (i.e., the type/length field is
1565 * a length field, <= ETHERMTU, rather than
1566 * a type field) with the first two bytes
1567 * after the Ethernet/802.3 header being
1570 * Ethernet_802.2 frames, which are 802.3
1571 * frames with an 802.2 LLC header and
1572 * with the IPX LSAP as the DSAP in the LLC
1575 * Ethernet_SNAP frames, which are 802.3
1576 * frames with an LLC header and a SNAP
1577 * header and with an OUI of 0x000000
1578 * (encapsulated Ethernet) and a protocol
1579 * ID of ETHERTYPE_IPX in the SNAP header.
1581 * XXX - should we generate the same code both
1582 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1586 * This generates code to check both for the
1587 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1589 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1590 (bpf_int32)LLCSAP_IPX);
1591 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H,
1596 * Now we add code to check for SNAP frames with
1597 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1599 b0 = gen_snap(0x000000, ETHERTYPE_IPX, 14);
1603 * Now we generate code to check for 802.3
1604 * frames in general.
1606 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1610 * Now add the check for 802.3 frames before the
1611 * check for Ethernet_802.2 and Ethernet_802.3,
1612 * as those checks should only be done on 802.3
1613 * frames, not on Ethernet frames.
1618 * Now add the check for Ethernet_II frames, and
1619 * do that before checking for the other frame
1622 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1623 (bpf_int32)ETHERTYPE_IPX);
1627 case ETHERTYPE_ATALK:
1628 case ETHERTYPE_AARP:
1630 * EtherTalk (AppleTalk protocols on Ethernet link
1631 * layer) may use 802.2 encapsulation.
1635 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1636 * we check for an Ethernet type field less than
1637 * 1500, which means it's an 802.3 length field.
1639 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1643 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1644 * SNAP packets with an organization code of
1645 * 0x080007 (Apple, for Appletalk) and a protocol
1646 * type of ETHERTYPE_ATALK (Appletalk).
1648 * 802.2-encapsulated ETHERTYPE_AARP packets are
1649 * SNAP packets with an organization code of
1650 * 0x000000 (encapsulated Ethernet) and a protocol
1651 * type of ETHERTYPE_AARP (Appletalk ARP).
1653 if (proto == ETHERTYPE_ATALK)
1654 b1 = gen_snap(0x080007, ETHERTYPE_ATALK, 14);
1655 else /* proto == ETHERTYPE_AARP */
1656 b1 = gen_snap(0x000000, ETHERTYPE_AARP, 14);
1660 * Check for Ethernet encapsulation (Ethertalk
1661 * phase 1?); we just check for the Ethernet
1664 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1670 if (proto <= ETHERMTU) {
1672 * This is an LLC SAP value, so the frames
1673 * that match would be 802.2 frames.
1674 * Check that the frame is an 802.2 frame
1675 * (i.e., that the length/type field is
1676 * a length field, <= ETHERMTU) and
1677 * then check the DSAP.
1679 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1681 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1687 * This is an Ethernet type, so compare
1688 * the length/type field with it (if
1689 * the frame is an 802.2 frame, the length
1690 * field will be <= ETHERMTU, and, as
1691 * "proto" is > ETHERMTU, this test
1692 * will fail and the frame won't match,
1693 * which is what we want).
1695 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1702 * Generate code to match a particular packet type.
1704 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1705 * value, if <= ETHERMTU. We use that to determine whether to
1706 * match the type field or to check the type field for the special
1707 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1709 static struct block *
1710 gen_linux_sll_linktype(proto)
1713 struct block *b0, *b1;
1719 case LLCSAP_NETBEUI:
1721 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1722 * so we check the DSAP and SSAP.
1724 * LLCSAP_IP checks for IP-over-802.2, rather
1725 * than IP-over-Ethernet or IP-over-SNAP.
1727 * XXX - should we check both the DSAP and the
1728 * SSAP, like this, or should we check just the
1729 * DSAP, as we do for other types <= ETHERMTU
1730 * (i.e., other SAP values)?
1732 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1733 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1734 ((proto << 8) | proto));
1740 * Ethernet_II frames, which are Ethernet
1741 * frames with a frame type of ETHERTYPE_IPX;
1743 * Ethernet_802.3 frames, which have a frame
1744 * type of LINUX_SLL_P_802_3;
1746 * Ethernet_802.2 frames, which are 802.3
1747 * frames with an 802.2 LLC header (i.e, have
1748 * a frame type of LINUX_SLL_P_802_2) and
1749 * with the IPX LSAP as the DSAP in the LLC
1752 * Ethernet_SNAP frames, which are 802.3
1753 * frames with an LLC header and a SNAP
1754 * header and with an OUI of 0x000000
1755 * (encapsulated Ethernet) and a protocol
1756 * ID of ETHERTYPE_IPX in the SNAP header.
1758 * First, do the checks on LINUX_SLL_P_802_2
1759 * frames; generate the check for either
1760 * Ethernet_802.2 or Ethernet_SNAP frames, and
1761 * then put a check for LINUX_SLL_P_802_2 frames
1764 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1765 (bpf_int32)LLCSAP_IPX);
1766 b1 = gen_snap(0x000000, ETHERTYPE_IPX,
1769 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1773 * Now check for 802.3 frames and OR that with
1774 * the previous test.
1776 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1780 * Now add the check for Ethernet_II frames, and
1781 * do that before checking for the other frame
1784 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1785 (bpf_int32)ETHERTYPE_IPX);
1789 case ETHERTYPE_ATALK:
1790 case ETHERTYPE_AARP:
1792 * EtherTalk (AppleTalk protocols on Ethernet link
1793 * layer) may use 802.2 encapsulation.
1797 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1798 * we check for the 802.2 protocol type in the
1799 * "Ethernet type" field.
1801 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1804 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1805 * SNAP packets with an organization code of
1806 * 0x080007 (Apple, for Appletalk) and a protocol
1807 * type of ETHERTYPE_ATALK (Appletalk).
1809 * 802.2-encapsulated ETHERTYPE_AARP packets are
1810 * SNAP packets with an organization code of
1811 * 0x000000 (encapsulated Ethernet) and a protocol
1812 * type of ETHERTYPE_AARP (Appletalk ARP).
1814 if (proto == ETHERTYPE_ATALK)
1815 b1 = gen_snap(0x080007, ETHERTYPE_ATALK,
1817 else /* proto == ETHERTYPE_AARP */
1818 b1 = gen_snap(0x000000, ETHERTYPE_AARP,
1823 * Check for Ethernet encapsulation (Ethertalk
1824 * phase 1?); we just check for the Ethernet
1827 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1833 if (proto <= ETHERMTU) {
1835 * This is an LLC SAP value, so the frames
1836 * that match would be 802.2 frames.
1837 * Check for the 802.2 protocol type
1838 * in the "Ethernet type" field, and
1839 * then check the DSAP.
1841 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1843 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1849 * This is an Ethernet type, so compare
1850 * the length/type field with it (if
1851 * the frame is an 802.2 frame, the length
1852 * field will be <= ETHERMTU, and, as
1853 * "proto" is > ETHERMTU, this test
1854 * will fail and the frame won't match,
1855 * which is what we want).
1857 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1864 insert_radiotap_load_llprefixlen(b)
1867 struct slist *s1, *s2;
1870 * Prepend to the statements in this block code to load the
1871 * length of the radiotap header into the register assigned
1872 * to hold that length, if one has been assigned.
1874 if (reg_ll_size != -1) {
1876 * The 2 bytes at offsets of 2 and 3 from the beginning
1877 * of the radiotap header are the length of the radiotap
1878 * header; unfortunately, it's little-endian, so we have
1879 * to load it a byte at a time and construct the value.
1883 * Load the high-order byte, at an offset of 3, shift it
1884 * left a byte, and put the result in the X register.
1886 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1888 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1891 s2 = new_stmt(BPF_MISC|BPF_TAX);
1895 * Load the next byte, at an offset of 2, and OR the
1896 * value from the X register into it.
1898 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1901 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1905 * Now allocate a register to hold that value and store
1908 s2 = new_stmt(BPF_ST);
1909 s2->s.k = reg_ll_size;
1913 * Now move it into the X register.
1915 s2 = new_stmt(BPF_MISC|BPF_TAX);
1919 * Now append all the existing statements in this
1920 * block to these statements.
1922 sappend(s1, b->stmts);
1928 * At the moment we treat PPI as normal Radiotap encoded
1929 * packets. The difference is in the function that generates
1930 * the code at the beginning to compute the header length.
1931 * Since this code generator of PPI supports bare 802.11
1932 * encapsulation only (i.e. the encapsulated DLT should be
1933 * DLT_IEEE802_11) we generate code to check for this too.
1936 insert_ppi_load_llprefixlen(b)
1939 struct slist *s1, *s2;
1942 * Prepend to the statements in this block code to load the
1943 * length of the radiotap header into the register assigned
1944 * to hold that length, if one has been assigned.
1946 if (reg_ll_size != -1) {
1948 * The 2 bytes at offsets of 2 and 3 from the beginning
1949 * of the radiotap header are the length of the radiotap
1950 * header; unfortunately, it's little-endian, so we have
1951 * to load it a byte at a time and construct the value.
1955 * Load the high-order byte, at an offset of 3, shift it
1956 * left a byte, and put the result in the X register.
1958 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1960 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1963 s2 = new_stmt(BPF_MISC|BPF_TAX);
1967 * Load the next byte, at an offset of 2, and OR the
1968 * value from the X register into it.
1970 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1973 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1977 * Now allocate a register to hold that value and store
1980 s2 = new_stmt(BPF_ST);
1981 s2->s.k = reg_ll_size;
1985 * Now move it into the X register.
1987 s2 = new_stmt(BPF_MISC|BPF_TAX);
1991 * Now append all the existing statements in this
1992 * block to these statements.
1994 sappend(s1, b->stmts);
2000 static struct block *
2001 gen_ppi_dlt_check(void)
2003 struct slist *s_load_dlt;
2006 if (linktype == DLT_PPI)
2008 /* Create the statements that check for the DLT
2010 s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2011 s_load_dlt->s.k = 4;
2013 b = new_block(JMP(BPF_JEQ));
2015 b->stmts = s_load_dlt;
2016 b->s.k = SWAPLONG(DLT_IEEE802_11);
2027 insert_load_llprefixlen(b)
2033 * At the moment we treat PPI as normal Radiotap encoded
2034 * packets. The difference is in the function that generates
2035 * the code at the beginning to compute the header length.
2036 * Since this code generator of PPI supports bare 802.11
2037 * encapsulation only (i.e. the encapsulated DLT should be
2038 * DLT_IEEE802_11) we generate code to check for this too.
2041 insert_ppi_load_llprefixlen(b);
2044 case DLT_IEEE802_11_RADIO:
2045 insert_radiotap_load_llprefixlen(b);
2051 static struct slist *
2052 gen_radiotap_llprefixlen(void)
2056 if (reg_ll_size == -1) {
2058 * We haven't yet assigned a register for the length
2059 * of the radiotap header; allocate one.
2061 reg_ll_size = alloc_reg();
2065 * Load the register containing the radiotap length
2066 * into the X register.
2068 s = new_stmt(BPF_LDX|BPF_MEM);
2069 s->s.k = reg_ll_size;
2074 * At the moment we treat PPI as normal Radiotap encoded
2075 * packets. The difference is in the function that generates
2076 * the code at the beginning to compute the header length.
2077 * Since this code generator of PPI supports bare 802.11
2078 * encapsulation only (i.e. the encapsulated DLT should be
2079 * DLT_IEEE802_11) we generate code to check for this too.
2081 static struct slist *
2082 gen_ppi_llprefixlen(void)
2086 if (reg_ll_size == -1) {
2088 * We haven't yet assigned a register for the length
2089 * of the radiotap header; allocate one.
2091 reg_ll_size = alloc_reg();
2095 * Load the register containing the radiotap length
2096 * into the X register.
2098 s = new_stmt(BPF_LDX|BPF_MEM);
2099 s->s.k = reg_ll_size;
2106 * Generate code to compute the link-layer header length, if necessary,
2107 * putting it into the X register, and to return either a pointer to a
2108 * "struct slist" for the list of statements in that code, or NULL if
2109 * no code is necessary.
2111 static struct slist *
2112 gen_llprefixlen(void)
2117 return gen_ppi_llprefixlen();
2120 case DLT_IEEE802_11_RADIO:
2121 return gen_radiotap_llprefixlen();
2129 * Generate code to match a particular packet type by matching the
2130 * link-layer type field or fields in the 802.2 LLC header.
2132 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2133 * value, if <= ETHERMTU.
2135 static struct block *
2139 struct block *b0, *b1, *b2;
2141 /* are we checking MPLS-encapsulated packets? */
2142 if (label_stack_depth > 0) {
2146 /* FIXME add other L3 proto IDs */
2147 return gen_mpls_linktype(Q_IP);
2149 case ETHERTYPE_IPV6:
2151 /* FIXME add other L3 proto IDs */
2152 return gen_mpls_linktype(Q_IPV6);
2155 bpf_error("unsupported protocol over mpls");
2163 return gen_ether_linktype(proto);
2171 proto = (proto << 8 | LLCSAP_ISONS);
2175 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2185 case DLT_IEEE802_11:
2186 case DLT_IEEE802_11_RADIO_AVS:
2187 case DLT_IEEE802_11_RADIO:
2188 case DLT_PRISM_HEADER:
2189 case DLT_ATM_RFC1483:
2191 case DLT_IP_OVER_FC:
2192 return gen_llc_linktype(proto);
2198 * If "is_lane" is set, check for a LANE-encapsulated
2199 * version of this protocol, otherwise check for an
2200 * LLC-encapsulated version of this protocol.
2202 * We assume LANE means Ethernet, not Token Ring.
2206 * Check that the packet doesn't begin with an
2207 * LE Control marker. (We've already generated
2210 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
2215 * Now generate an Ethernet test.
2217 b1 = gen_ether_linktype(proto);
2222 * Check for LLC encapsulation and then check the
2225 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
2226 b1 = gen_llc_linktype(proto);
2234 return gen_linux_sll_linktype(proto);
2239 case DLT_SLIP_BSDOS:
2242 * These types don't provide any type field; packets
2243 * are always IPv4 or IPv6.
2245 * XXX - for IPv4, check for a version number of 4, and,
2246 * for IPv6, check for a version number of 6?
2251 /* Check for a version number of 4. */
2252 return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
2254 case ETHERTYPE_IPV6:
2255 /* Check for a version number of 6. */
2256 return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
2260 return gen_false(); /* always false */
2267 case DLT_PPP_SERIAL:
2270 * We use Ethernet protocol types inside libpcap;
2271 * map them to the corresponding PPP protocol types.
2280 case ETHERTYPE_IPV6:
2289 case ETHERTYPE_ATALK:
2303 * I'm assuming the "Bridging PDU"s that go
2304 * over PPP are Spanning Tree Protocol
2318 * We use Ethernet protocol types inside libpcap;
2319 * map them to the corresponding PPP protocol types.
2324 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
2325 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
2327 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
2332 case ETHERTYPE_IPV6:
2342 case ETHERTYPE_ATALK:
2356 * I'm assuming the "Bridging PDU"s that go
2357 * over PPP are Spanning Tree Protocol
2373 * For DLT_NULL, the link-layer header is a 32-bit
2374 * word containing an AF_ value in *host* byte order,
2375 * and for DLT_ENC, the link-layer header begins
2376 * with a 32-bit work containing an AF_ value in
2379 * In addition, if we're reading a saved capture file,
2380 * the host byte order in the capture may not be the
2381 * same as the host byte order on this machine.
2383 * For DLT_LOOP, the link-layer header is a 32-bit
2384 * word containing an AF_ value in *network* byte order.
2386 * XXX - AF_ values may, unfortunately, be platform-
2387 * dependent; for example, FreeBSD's AF_INET6 is 24
2388 * whilst NetBSD's and OpenBSD's is 26.
2390 * This means that, when reading a capture file, just
2391 * checking for our AF_INET6 value won't work if the
2392 * capture file came from another OS.
2401 case ETHERTYPE_IPV6:
2408 * Not a type on which we support filtering.
2409 * XXX - support those that have AF_ values
2410 * #defined on this platform, at least?
2415 if (linktype == DLT_NULL || linktype == DLT_ENC) {
2417 * The AF_ value is in host byte order, but
2418 * the BPF interpreter will convert it to
2419 * network byte order.
2421 * If this is a save file, and it's from a
2422 * machine with the opposite byte order to
2423 * ours, we byte-swap the AF_ value.
2425 * Then we run it through "htonl()", and
2426 * generate code to compare against the result.
2428 if (bpf_pcap->sf.rfile != NULL &&
2429 bpf_pcap->sf.swapped)
2430 proto = SWAPLONG(proto);
2431 proto = htonl(proto);
2433 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
2435 #ifdef HAVE_NET_PFVAR_H
2438 * af field is host byte order in contrast to the rest of
2441 if (proto == ETHERTYPE_IP)
2442 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2443 BPF_B, (bpf_int32)AF_INET));
2445 else if (proto == ETHERTYPE_IPV6)
2446 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2447 BPF_B, (bpf_int32)AF_INET6));
2453 #endif /* HAVE_NET_PFVAR_H */
2456 case DLT_ARCNET_LINUX:
2458 * XXX should we check for first fragment if the protocol
2467 case ETHERTYPE_IPV6:
2468 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2469 (bpf_int32)ARCTYPE_INET6));
2473 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2474 (bpf_int32)ARCTYPE_IP);
2475 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2476 (bpf_int32)ARCTYPE_IP_OLD);
2481 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2482 (bpf_int32)ARCTYPE_ARP);
2483 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2484 (bpf_int32)ARCTYPE_ARP_OLD);
2488 case ETHERTYPE_REVARP:
2489 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2490 (bpf_int32)ARCTYPE_REVARP));
2492 case ETHERTYPE_ATALK:
2493 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2494 (bpf_int32)ARCTYPE_ATALK));
2501 case ETHERTYPE_ATALK:
2511 * XXX - assumes a 2-byte Frame Relay header with
2512 * DLCI and flags. What if the address is longer?
2518 * Check for the special NLPID for IP.
2520 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
2523 case ETHERTYPE_IPV6:
2525 * Check for the special NLPID for IPv6.
2527 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
2532 * Check for several OSI protocols.
2534 * Frame Relay packets typically have an OSI
2535 * NLPID at the beginning; we check for each
2538 * What we check for is the NLPID and a frame
2539 * control field of UI, i.e. 0x03 followed
2542 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
2543 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
2544 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
2555 case DLT_JUNIPER_MFR:
2556 case DLT_JUNIPER_MLFR:
2557 case DLT_JUNIPER_MLPPP:
2558 case DLT_JUNIPER_ATM1:
2559 case DLT_JUNIPER_ATM2:
2560 case DLT_JUNIPER_PPPOE:
2561 case DLT_JUNIPER_PPPOE_ATM:
2562 case DLT_JUNIPER_GGSN:
2563 case DLT_JUNIPER_ES:
2564 case DLT_JUNIPER_MONITOR:
2565 case DLT_JUNIPER_SERVICES:
2566 case DLT_JUNIPER_ETHER:
2567 case DLT_JUNIPER_PPP:
2568 case DLT_JUNIPER_FRELAY:
2569 case DLT_JUNIPER_CHDLC:
2570 case DLT_JUNIPER_VP:
2571 /* just lets verify the magic number for now -
2572 * on ATM we may have up to 6 different encapsulations on the wire
2573 * and need a lot of heuristics to figure out that the payload
2576 * FIXME encapsulation specific BPF_ filters
2578 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
2580 case DLT_LINUX_IRDA:
2581 bpf_error("IrDA link-layer type filtering not implemented");
2584 bpf_error("DOCSIS link-layer type filtering not implemented");
2586 case DLT_LINUX_LAPD:
2587 bpf_error("LAPD link-layer type filtering not implemented");
2591 * All the types that have no encapsulation should either be
2592 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2593 * all packets are IP packets, or should be handled in some
2594 * special case, if none of them are (if some are and some
2595 * aren't, the lack of encapsulation is a problem, as we'd
2596 * have to find some other way of determining the packet type).
2598 * Therefore, if "off_linktype" is -1, there's an error.
2600 if (off_linktype == (u_int)-1)
2604 * Any type not handled above should always have an Ethernet
2605 * type at an offset of "off_linktype". (PPP is partially
2606 * handled above - the protocol type is mapped from the
2607 * Ethernet and LLC types we use internally to the corresponding
2608 * PPP type - but the PPP type is always specified by a value
2609 * at "off_linktype", so we don't have to do the code generation
2612 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2616 * Check for an LLC SNAP packet with a given organization code and
2617 * protocol type; we check the entire contents of the 802.2 LLC and
2618 * snap headers, checking for DSAP and SSAP of SNAP and a control
2619 * field of 0x03 in the LLC header, and for the specified organization
2620 * code and protocol type in the SNAP header.
2622 static struct block *
2623 gen_snap(orgcode, ptype, offset)
2624 bpf_u_int32 orgcode;
2628 u_char snapblock[8];
2630 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
2631 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
2632 snapblock[2] = 0x03; /* control = UI */
2633 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
2634 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
2635 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
2636 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
2637 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
2638 return gen_bcmp(OR_LINK, offset, 8, snapblock);
2642 * Generate code to match a particular packet type, for link-layer types
2643 * using 802.2 LLC headers.
2645 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2646 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2648 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2649 * value, if <= ETHERMTU. We use that to determine whether to
2650 * match the DSAP or both DSAP and LSAP or to check the OUI and
2651 * protocol ID in a SNAP header.
2653 static struct block *
2654 gen_llc_linktype(proto)
2658 * XXX - handle token-ring variable-length header.
2664 case LLCSAP_NETBEUI:
2666 * XXX - should we check both the DSAP and the
2667 * SSAP, like this, or should we check just the
2668 * DSAP, as we do for other types <= ETHERMTU
2669 * (i.e., other SAP values)?
2671 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_u_int32)
2672 ((proto << 8) | proto));
2676 * XXX - are there ever SNAP frames for IPX on
2677 * non-Ethernet 802.x networks?
2679 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2680 (bpf_int32)LLCSAP_IPX);
2682 case ETHERTYPE_ATALK:
2684 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2685 * SNAP packets with an organization code of
2686 * 0x080007 (Apple, for Appletalk) and a protocol
2687 * type of ETHERTYPE_ATALK (Appletalk).
2689 * XXX - check for an organization code of
2690 * encapsulated Ethernet as well?
2692 return gen_snap(0x080007, ETHERTYPE_ATALK, off_linktype);
2696 * XXX - we don't have to check for IPX 802.3
2697 * here, but should we check for the IPX Ethertype?
2699 if (proto <= ETHERMTU) {
2701 * This is an LLC SAP value, so check
2704 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2708 * This is an Ethernet type; we assume that it's
2709 * unlikely that it'll appear in the right place
2710 * at random, and therefore check only the
2711 * location that would hold the Ethernet type
2712 * in a SNAP frame with an organization code of
2713 * 0x000000 (encapsulated Ethernet).
2715 * XXX - if we were to check for the SNAP DSAP and
2716 * LSAP, as per XXX, and were also to check for an
2717 * organization code of 0x000000 (encapsulated
2718 * Ethernet), we'd do
2720 * return gen_snap(0x000000, proto,
2723 * here; for now, we don't, as per the above.
2724 * I don't know whether it's worth the extra CPU
2725 * time to do the right check or not.
2727 return gen_cmp(OR_LINK, off_linktype+6, BPF_H,
2733 static struct block *
2734 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
2738 u_int src_off, dst_off;
2740 struct block *b0, *b1;
2754 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2755 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2761 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2762 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2769 b0 = gen_linktype(proto);
2770 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
2776 static struct block *
2777 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
2778 struct in6_addr *addr;
2779 struct in6_addr *mask;
2781 u_int src_off, dst_off;
2783 struct block *b0, *b1;
2798 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2799 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2805 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2806 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2813 /* this order is important */
2814 a = (u_int32_t *)addr;
2815 m = (u_int32_t *)mask;
2816 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
2817 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
2819 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
2821 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
2823 b0 = gen_linktype(proto);
2829 static struct block *
2830 gen_ehostop(eaddr, dir)
2831 register const u_char *eaddr;
2834 register struct block *b0, *b1;
2838 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
2841 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
2844 b0 = gen_ehostop(eaddr, Q_SRC);
2845 b1 = gen_ehostop(eaddr, Q_DST);
2851 b0 = gen_ehostop(eaddr, Q_SRC);
2852 b1 = gen_ehostop(eaddr, Q_DST);
2861 * Like gen_ehostop, but for DLT_FDDI
2863 static struct block *
2864 gen_fhostop(eaddr, dir)
2865 register const u_char *eaddr;
2868 struct block *b0, *b1;
2873 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
2875 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
2880 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
2882 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
2886 b0 = gen_fhostop(eaddr, Q_SRC);
2887 b1 = gen_fhostop(eaddr, Q_DST);
2893 b0 = gen_fhostop(eaddr, Q_SRC);
2894 b1 = gen_fhostop(eaddr, Q_DST);
2903 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2905 static struct block *
2906 gen_thostop(eaddr, dir)
2907 register const u_char *eaddr;
2910 register struct block *b0, *b1;
2914 return gen_bcmp(OR_LINK, 8, 6, eaddr);
2917 return gen_bcmp(OR_LINK, 2, 6, eaddr);
2920 b0 = gen_thostop(eaddr, Q_SRC);
2921 b1 = gen_thostop(eaddr, Q_DST);
2927 b0 = gen_thostop(eaddr, Q_SRC);
2928 b1 = gen_thostop(eaddr, Q_DST);
2937 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2939 static struct block *
2940 gen_wlanhostop(eaddr, dir)
2941 register const u_char *eaddr;
2944 register struct block *b0, *b1, *b2;
2945 register struct slist *s;
2952 * For control frames, there is no SA.
2954 * For management frames, SA is at an
2955 * offset of 10 from the beginning of
2958 * For data frames, SA is at an offset
2959 * of 10 from the beginning of the packet
2960 * if From DS is clear, at an offset of
2961 * 16 from the beginning of the packet
2962 * if From DS is set and To DS is clear,
2963 * and an offset of 24 from the beginning
2964 * of the packet if From DS is set and To DS
2969 * Generate the tests to be done for data frames
2972 * First, check for To DS set, i.e. check "link[1] & 0x01".
2974 s = gen_load_a(OR_LINK, 1, BPF_B);
2975 b1 = new_block(JMP(BPF_JSET));
2976 b1->s.k = 0x01; /* To DS */
2980 * If To DS is set, the SA is at 24.
2982 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
2986 * Now, check for To DS not set, i.e. check
2987 * "!(link[1] & 0x01)".
2989 s = gen_load_a(OR_LINK, 1, BPF_B);
2990 b2 = new_block(JMP(BPF_JSET));
2991 b2->s.k = 0x01; /* To DS */
2996 * If To DS is not set, the SA is at 16.
2998 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3002 * Now OR together the last two checks. That gives
3003 * the complete set of checks for data frames with
3009 * Now check for From DS being set, and AND that with
3010 * the ORed-together checks.
3012 s = gen_load_a(OR_LINK, 1, BPF_B);
3013 b1 = new_block(JMP(BPF_JSET));
3014 b1->s.k = 0x02; /* From DS */
3019 * Now check for data frames with From DS not set.
3021 s = gen_load_a(OR_LINK, 1, BPF_B);
3022 b2 = new_block(JMP(BPF_JSET));
3023 b2->s.k = 0x02; /* From DS */
3028 * If From DS isn't set, the SA is at 10.
3030 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3034 * Now OR together the checks for data frames with
3035 * From DS not set and for data frames with From DS
3036 * set; that gives the checks done for data frames.
3041 * Now check for a data frame.
3042 * I.e, check "link[0] & 0x08".
3044 gen_load_a(OR_LINK, 0, BPF_B);
3045 b1 = new_block(JMP(BPF_JSET));
3050 * AND that with the checks done for data frames.
3055 * If the high-order bit of the type value is 0, this
3056 * is a management frame.
3057 * I.e, check "!(link[0] & 0x08)".
3059 s = gen_load_a(OR_LINK, 0, BPF_B);
3060 b2 = new_block(JMP(BPF_JSET));
3066 * For management frames, the SA is at 10.
3068 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3072 * OR that with the checks done for data frames.
3073 * That gives the checks done for management and
3079 * If the low-order bit of the type value is 1,
3080 * this is either a control frame or a frame
3081 * with a reserved type, and thus not a
3084 * I.e., check "!(link[0] & 0x04)".
3086 s = gen_load_a(OR_LINK, 0, BPF_B);
3087 b1 = new_block(JMP(BPF_JSET));
3093 * AND that with the checks for data and management
3103 * For control frames, there is no DA.
3105 * For management frames, DA is at an
3106 * offset of 4 from the beginning of
3109 * For data frames, DA is at an offset
3110 * of 4 from the beginning of the packet
3111 * if To DS is clear and at an offset of
3112 * 16 from the beginning of the packet
3117 * Generate the tests to be done for data frames.
3119 * First, check for To DS set, i.e. "link[1] & 0x01".
3121 s = gen_load_a(OR_LINK, 1, BPF_B);
3122 b1 = new_block(JMP(BPF_JSET));
3123 b1->s.k = 0x01; /* To DS */
3127 * If To DS is set, the DA is at 16.
3129 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3133 * Now, check for To DS not set, i.e. check
3134 * "!(link[1] & 0x01)".
3136 s = gen_load_a(OR_LINK, 1, BPF_B);
3137 b2 = new_block(JMP(BPF_JSET));
3138 b2->s.k = 0x01; /* To DS */
3143 * If To DS is not set, the DA is at 4.
3145 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
3149 * Now OR together the last two checks. That gives
3150 * the complete set of checks for data frames.
3155 * Now check for a data frame.
3156 * I.e, check "link[0] & 0x08".
3158 s = gen_load_a(OR_LINK, 0, BPF_B);
3159 b1 = new_block(JMP(BPF_JSET));
3164 * AND that with the checks done for data frames.
3169 * If the high-order bit of the type value is 0, this
3170 * is a management frame.
3171 * I.e, check "!(link[0] & 0x08)".
3173 s = gen_load_a(OR_LINK, 0, BPF_B);
3174 b2 = new_block(JMP(BPF_JSET));
3180 * For management frames, the DA is at 4.
3182 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
3186 * OR that with the checks done for data frames.
3187 * That gives the checks done for management and
3193 * If the low-order bit of the type value is 1,
3194 * this is either a control frame or a frame
3195 * with a reserved type, and thus not a
3198 * I.e., check "!(link[0] & 0x04)".
3200 s = gen_load_a(OR_LINK, 0, BPF_B);
3201 b1 = new_block(JMP(BPF_JSET));
3207 * AND that with the checks for data and management
3214 b0 = gen_wlanhostop(eaddr, Q_SRC);
3215 b1 = gen_wlanhostop(eaddr, Q_DST);
3221 b0 = gen_wlanhostop(eaddr, Q_SRC);
3222 b1 = gen_wlanhostop(eaddr, Q_DST);
3231 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3232 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3233 * as the RFC states.)
3235 static struct block *
3236 gen_ipfchostop(eaddr, dir)
3237 register const u_char *eaddr;
3240 register struct block *b0, *b1;
3244 return gen_bcmp(OR_LINK, 10, 6, eaddr);
3247 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3250 b0 = gen_ipfchostop(eaddr, Q_SRC);
3251 b1 = gen_ipfchostop(eaddr, Q_DST);
3257 b0 = gen_ipfchostop(eaddr, Q_SRC);
3258 b1 = gen_ipfchostop(eaddr, Q_DST);
3267 * This is quite tricky because there may be pad bytes in front of the
3268 * DECNET header, and then there are two possible data packet formats that
3269 * carry both src and dst addresses, plus 5 packet types in a format that
3270 * carries only the src node, plus 2 types that use a different format and
3271 * also carry just the src node.
3275 * Instead of doing those all right, we just look for data packets with
3276 * 0 or 1 bytes of padding. If you want to look at other packets, that
3277 * will require a lot more hacking.
3279 * To add support for filtering on DECNET "areas" (network numbers)
3280 * one would want to add a "mask" argument to this routine. That would
3281 * make the filter even more inefficient, although one could be clever
3282 * and not generate masking instructions if the mask is 0xFFFF.
3284 static struct block *
3285 gen_dnhostop(addr, dir)
3289 struct block *b0, *b1, *b2, *tmp;
3290 u_int offset_lh; /* offset if long header is received */
3291 u_int offset_sh; /* offset if short header is received */
3296 offset_sh = 1; /* follows flags */
3297 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
3301 offset_sh = 3; /* follows flags, dstnode */
3302 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3306 /* Inefficient because we do our Calvinball dance twice */
3307 b0 = gen_dnhostop(addr, Q_SRC);
3308 b1 = gen_dnhostop(addr, Q_DST);
3314 /* Inefficient because we do our Calvinball dance twice */
3315 b0 = gen_dnhostop(addr, Q_SRC);
3316 b1 = gen_dnhostop(addr, Q_DST);
3321 bpf_error("ISO host filtering not implemented");
3326 b0 = gen_linktype(ETHERTYPE_DN);
3327 /* Check for pad = 1, long header case */
3328 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3329 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
3330 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
3331 BPF_H, (bpf_int32)ntohs((u_short)addr));
3333 /* Check for pad = 0, long header case */
3334 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
3335 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3338 /* Check for pad = 1, short header case */
3339 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3340 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
3341 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3344 /* Check for pad = 0, short header case */
3345 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
3346 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
3350 /* Combine with test for linktype */
3356 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3357 * test the bottom-of-stack bit, and then check the version number
3358 * field in the IP header.
3360 static struct block *
3361 gen_mpls_linktype(proto)
3364 struct block *b0, *b1;
3369 /* match the bottom-of-stack bit */
3370 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3371 /* match the IPv4 version number */
3372 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
3377 /* match the bottom-of-stack bit */
3378 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3379 /* match the IPv4 version number */
3380 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
3389 static struct block *
3390 gen_host(addr, mask, proto, dir, type)
3397 struct block *b0, *b1;
3398 const char *typestr;
3408 b0 = gen_host(addr, mask, Q_IP, dir, type);
3410 * Only check for non-IPv4 addresses if we're not
3411 * checking MPLS-encapsulated packets.
3413 if (label_stack_depth == 0) {
3414 b1 = gen_host(addr, mask, Q_ARP, dir, type);
3416 b0 = gen_host(addr, mask, Q_RARP, dir, type);
3422 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
3425 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
3428 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
3431 bpf_error("'tcp' modifier applied to %s", typestr);
3434 bpf_error("'sctp' modifier applied to %s", typestr);
3437 bpf_error("'udp' modifier applied to %s", typestr);
3440 bpf_error("'icmp' modifier applied to %s", typestr);
3443 bpf_error("'igmp' modifier applied to %s", typestr);
3446 bpf_error("'igrp' modifier applied to %s", typestr);
3449 bpf_error("'pim' modifier applied to %s", typestr);
3452 bpf_error("'vrrp' modifier applied to %s", typestr);
3455 bpf_error("ATALK host filtering not implemented");
3458 bpf_error("AARP host filtering not implemented");
3461 return gen_dnhostop(addr, dir);
3464 bpf_error("SCA host filtering not implemented");
3467 bpf_error("LAT host filtering not implemented");
3470 bpf_error("MOPDL host filtering not implemented");
3473 bpf_error("MOPRC host filtering not implemented");
3477 bpf_error("'ip6' modifier applied to ip host");
3480 bpf_error("'icmp6' modifier applied to %s", typestr);
3484 bpf_error("'ah' modifier applied to %s", typestr);
3487 bpf_error("'esp' modifier applied to %s", typestr);
3490 bpf_error("ISO host filtering not implemented");
3493 bpf_error("'esis' modifier applied to %s", typestr);
3496 bpf_error("'isis' modifier applied to %s", typestr);
3499 bpf_error("'clnp' modifier applied to %s", typestr);
3502 bpf_error("'stp' modifier applied to %s", typestr);
3505 bpf_error("IPX host filtering not implemented");
3508 bpf_error("'netbeui' modifier applied to %s", typestr);
3511 bpf_error("'radio' modifier applied to %s", typestr);
3520 static struct block *
3521 gen_host6(addr, mask, proto, dir, type)
3522 struct in6_addr *addr;
3523 struct in6_addr *mask;
3528 const char *typestr;
3538 return gen_host6(addr, mask, Q_IPV6, dir, type);
3541 bpf_error("'ip' modifier applied to ip6 %s", typestr);
3544 bpf_error("'rarp' modifier applied to ip6 %s", typestr);
3547 bpf_error("'arp' modifier applied to ip6 %s", typestr);
3550 bpf_error("'sctp' modifier applied to %s", typestr);
3553 bpf_error("'tcp' modifier applied to %s", typestr);
3556 bpf_error("'udp' modifier applied to %s", typestr);
3559 bpf_error("'icmp' modifier applied to %s", typestr);
3562 bpf_error("'igmp' modifier applied to %s", typestr);
3565 bpf_error("'igrp' modifier applied to %s", typestr);
3568 bpf_error("'pim' modifier applied to %s", typestr);
3571 bpf_error("'vrrp' modifier applied to %s", typestr);
3574 bpf_error("ATALK host filtering not implemented");
3577 bpf_error("AARP host filtering not implemented");
3580 bpf_error("'decnet' modifier applied to ip6 %s", typestr);
3583 bpf_error("SCA host filtering not implemented");
3586 bpf_error("LAT host filtering not implemented");
3589 bpf_error("MOPDL host filtering not implemented");
3592 bpf_error("MOPRC host filtering not implemented");
3595 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
3598 bpf_error("'icmp6' modifier applied to %s", typestr);
3601 bpf_error("'ah' modifier applied to %s", typestr);
3604 bpf_error("'esp' modifier applied to %s", typestr);
3607 bpf_error("ISO host filtering not implemented");
3610 bpf_error("'esis' modifier applied to %s", typestr);
3613 bpf_error("'isis' modifier applied to %s", typestr);
3616 bpf_error("'clnp' modifier applied to %s", typestr);
3619 bpf_error("'stp' modifier applied to %s", typestr);
3622 bpf_error("IPX host filtering not implemented");
3625 bpf_error("'netbeui' modifier applied to %s", typestr);
3628 bpf_error("'radio' modifier applied to %s", typestr);
3638 static struct block *
3639 gen_gateway(eaddr, alist, proto, dir)
3640 const u_char *eaddr;
3641 bpf_u_int32 **alist;
3645 struct block *b0, *b1, *tmp;
3648 bpf_error("direction applied to 'gateway'");
3657 b0 = gen_ehostop(eaddr, Q_OR);
3660 b0 = gen_fhostop(eaddr, Q_OR);
3663 b0 = gen_thostop(eaddr, Q_OR);
3665 case DLT_IEEE802_11:
3666 case DLT_IEEE802_11_RADIO_AVS:
3668 case DLT_IEEE802_11_RADIO:
3669 case DLT_PRISM_HEADER:
3670 b0 = gen_wlanhostop(eaddr, Q_OR);
3675 * Check that the packet doesn't begin with an
3676 * LE Control marker. (We've already generated
3679 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3684 * Now check the MAC address.
3686 b0 = gen_ehostop(eaddr, Q_OR);
3690 case DLT_IP_OVER_FC:
3691 b0 = gen_ipfchostop(eaddr, Q_OR);
3695 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3697 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST);
3699 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR,
3708 bpf_error("illegal modifier of 'gateway'");
3714 gen_proto_abbrev(proto)
3723 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
3725 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
3731 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
3733 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
3739 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
3741 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
3747 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
3750 #ifndef IPPROTO_IGMP
3751 #define IPPROTO_IGMP 2
3755 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
3758 #ifndef IPPROTO_IGRP
3759 #define IPPROTO_IGRP 9
3762 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
3766 #define IPPROTO_PIM 103
3770 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
3772 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
3777 #ifndef IPPROTO_VRRP
3778 #define IPPROTO_VRRP 112
3782 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
3786 b1 = gen_linktype(ETHERTYPE_IP);
3790 b1 = gen_linktype(ETHERTYPE_ARP);
3794 b1 = gen_linktype(ETHERTYPE_REVARP);
3798 bpf_error("link layer applied in wrong context");
3801 b1 = gen_linktype(ETHERTYPE_ATALK);
3805 b1 = gen_linktype(ETHERTYPE_AARP);
3809 b1 = gen_linktype(ETHERTYPE_DN);
3813 b1 = gen_linktype(ETHERTYPE_SCA);
3817 b1 = gen_linktype(ETHERTYPE_LAT);
3821 b1 = gen_linktype(ETHERTYPE_MOPDL);
3825 b1 = gen_linktype(ETHERTYPE_MOPRC);
3830 b1 = gen_linktype(ETHERTYPE_IPV6);
3833 #ifndef IPPROTO_ICMPV6
3834 #define IPPROTO_ICMPV6 58
3837 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
3842 #define IPPROTO_AH 51
3845 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
3847 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
3853 #define IPPROTO_ESP 50
3856 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
3858 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
3864 b1 = gen_linktype(LLCSAP_ISONS);
3868 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
3872 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
3875 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
3876 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3877 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3879 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3881 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3883 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3887 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
3888 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3889 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3891 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3893 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3895 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3899 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
3900 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3901 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3903 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
3908 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3909 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3914 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3915 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3917 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3919 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3924 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3925 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3930 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3931 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3936 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
3940 b1 = gen_linktype(LLCSAP_8021D);
3944 b1 = gen_linktype(LLCSAP_IPX);
3948 b1 = gen_linktype(LLCSAP_NETBEUI);
3952 bpf_error("'radio' is not a valid protocol type");
3960 static struct block *
3967 s = gen_load_a(OR_NET, 6, BPF_H);
3968 b = new_block(JMP(BPF_JSET));
3977 * Generate a comparison to a port value in the transport-layer header
3978 * at the specified offset from the beginning of that header.
3980 * XXX - this handles a variable-length prefix preceding the link-layer
3981 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3982 * variable-length link-layer headers (such as Token Ring or 802.11
3985 static struct block *
3986 gen_portatom(off, v)
3990 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
3994 static struct block *
3995 gen_portatom6(off, v)
3999 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
4004 gen_portop(port, proto, dir)
4005 int port, proto, dir;
4007 struct block *b0, *b1, *tmp;
4009 /* ip proto 'proto' */
4010 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
4016 b1 = gen_portatom(0, (bpf_int32)port);
4020 b1 = gen_portatom(2, (bpf_int32)port);
4025 tmp = gen_portatom(0, (bpf_int32)port);
4026 b1 = gen_portatom(2, (bpf_int32)port);
4031 tmp = gen_portatom(0, (bpf_int32)port);
4032 b1 = gen_portatom(2, (bpf_int32)port);
4044 static struct block *
4045 gen_port(port, ip_proto, dir)
4050 struct block *b0, *b1, *tmp;
4055 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4056 * not LLC encapsulation with LLCSAP_IP.
4058 * For IEEE 802 networks - which includes 802.5 token ring
4059 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4060 * says that SNAP encapsulation is used, not LLC encapsulation
4063 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4064 * RFC 2225 say that SNAP encapsulation is used, not LLC
4065 * encapsulation with LLCSAP_IP.
4067 * So we always check for ETHERTYPE_IP.
4069 b0 = gen_linktype(ETHERTYPE_IP);
4075 b1 = gen_portop(port, ip_proto, dir);
4079 tmp = gen_portop(port, IPPROTO_TCP, dir);
4080 b1 = gen_portop(port, IPPROTO_UDP, dir);
4082 tmp = gen_portop(port, IPPROTO_SCTP, dir);
4095 gen_portop6(port, proto, dir)
4096 int port, proto, dir;
4098 struct block *b0, *b1, *tmp;
4100 /* ip6 proto 'proto' */
4101 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
4105 b1 = gen_portatom6(0, (bpf_int32)port);
4109 b1 = gen_portatom6(2, (bpf_int32)port);
4114 tmp = gen_portatom6(0, (bpf_int32)port);
4115 b1 = gen_portatom6(2, (bpf_int32)port);
4120 tmp = gen_portatom6(0, (bpf_int32)port);
4121 b1 = gen_portatom6(2, (bpf_int32)port);
4133 static struct block *
4134 gen_port6(port, ip_proto, dir)
4139 struct block *b0, *b1, *tmp;
4141 /* link proto ip6 */
4142 b0 = gen_linktype(ETHERTYPE_IPV6);
4148 b1 = gen_portop6(port, ip_proto, dir);
4152 tmp = gen_portop6(port, IPPROTO_TCP, dir);
4153 b1 = gen_portop6(port, IPPROTO_UDP, dir);
4155 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
4167 /* gen_portrange code */
4168 static struct block *
4169 gen_portrangeatom(off, v1, v2)
4173 struct block *b1, *b2;
4177 * Reverse the order of the ports, so v1 is the lower one.
4186 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
4187 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
4195 gen_portrangeop(port1, port2, proto, dir)
4200 struct block *b0, *b1, *tmp;
4202 /* ip proto 'proto' */
4203 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
4209 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4213 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4218 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4219 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4224 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
4225 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
4237 static struct block *
4238 gen_portrange(port1, port2, ip_proto, dir)
4243 struct block *b0, *b1, *tmp;
4246 b0 = gen_linktype(ETHERTYPE_IP);
4252 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
4256 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
4257 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
4259 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
4271 static struct block *
4272 gen_portrangeatom6(off, v1, v2)
4276 struct block *b1, *b2;
4280 * Reverse the order of the ports, so v1 is the lower one.
4289 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
4290 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
4298 gen_portrangeop6(port1, port2, proto, dir)
4303 struct block *b0, *b1, *tmp;
4305 /* ip6 proto 'proto' */
4306 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
4310 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4314 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4319 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4320 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4325 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4326 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4338 static struct block *
4339 gen_portrange6(port1, port2, ip_proto, dir)
4344 struct block *b0, *b1, *tmp;
4346 /* link proto ip6 */
4347 b0 = gen_linktype(ETHERTYPE_IPV6);
4353 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
4357 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
4358 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
4360 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
4373 lookup_proto(name, proto)
4374 register const char *name;
4384 v = pcap_nametoproto(name);
4385 if (v == PROTO_UNDEF)
4386 bpf_error("unknown ip proto '%s'", name);
4390 /* XXX should look up h/w protocol type based on linktype */
4391 v = pcap_nametoeproto(name);
4392 if (v == PROTO_UNDEF) {
4393 v = pcap_nametollc(name);
4394 if (v == PROTO_UNDEF)
4395 bpf_error("unknown ether proto '%s'", name);
4400 if (strcmp(name, "esis") == 0)
4402 else if (strcmp(name, "isis") == 0)
4404 else if (strcmp(name, "clnp") == 0)
4407 bpf_error("unknown osi proto '%s'", name);
4427 static struct block *
4428 gen_protochain(v, proto, dir)
4433 #ifdef NO_PROTOCHAIN
4434 return gen_proto(v, proto, dir);
4436 struct block *b0, *b;
4437 struct slist *s[100];
4438 int fix2, fix3, fix4, fix5;
4439 int ahcheck, again, end;
4441 int reg2 = alloc_reg();
4443 memset(s, 0, sizeof(s));
4444 fix2 = fix3 = fix4 = fix5 = 0;
4451 b0 = gen_protochain(v, Q_IP, dir);
4452 b = gen_protochain(v, Q_IPV6, dir);
4456 bpf_error("bad protocol applied for 'protochain'");
4461 * We don't handle variable-length radiotap here headers yet.
4462 * We might want to add BPF instructions to do the protochain
4463 * work, to simplify that and, on platforms that have a BPF
4464 * interpreter with the new instructions, let the filtering
4465 * be done in the kernel. (We already require a modified BPF
4466 * engine to do the protochain stuff, to support backward
4467 * branches, and backward branch support is unlikely to appear
4468 * in kernel BPF engines.)
4470 if (linktype == DLT_IEEE802_11_RADIO)
4471 bpf_error("'protochain' not supported with radiotap headers");
4473 if (linktype == DLT_PPI)
4474 bpf_error("'protochain' not supported with PPI headers");
4476 no_optimize = 1; /*this code is not compatible with optimzer yet */
4479 * s[0] is a dummy entry to protect other BPF insn from damage
4480 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4481 * hard to find interdependency made by jump table fixup.
4484 s[i] = new_stmt(0); /*dummy*/
4489 b0 = gen_linktype(ETHERTYPE_IP);
4492 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4493 s[i]->s.k = off_ll + off_nl + 9;
4495 /* X = ip->ip_hl << 2 */
4496 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
4497 s[i]->s.k = off_ll + off_nl;
4502 b0 = gen_linktype(ETHERTYPE_IPV6);
4504 /* A = ip6->ip_nxt */
4505 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4506 s[i]->s.k = off_ll + off_nl + 6;
4508 /* X = sizeof(struct ip6_hdr) */
4509 s[i] = new_stmt(BPF_LDX|BPF_IMM);
4515 bpf_error("unsupported proto to gen_protochain");
4519 /* again: if (A == v) goto end; else fall through; */
4521 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4523 s[i]->s.jt = NULL; /*later*/
4524 s[i]->s.jf = NULL; /*update in next stmt*/
4528 #ifndef IPPROTO_NONE
4529 #define IPPROTO_NONE 59
4531 /* if (A == IPPROTO_NONE) goto end */
4532 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4533 s[i]->s.jt = NULL; /*later*/
4534 s[i]->s.jf = NULL; /*update in next stmt*/
4535 s[i]->s.k = IPPROTO_NONE;
4536 s[fix5]->s.jf = s[i];
4541 if (proto == Q_IPV6) {
4542 int v6start, v6end, v6advance, j;
4545 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4546 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4547 s[i]->s.jt = NULL; /*later*/
4548 s[i]->s.jf = NULL; /*update in next stmt*/
4549 s[i]->s.k = IPPROTO_HOPOPTS;
4550 s[fix2]->s.jf = s[i];
4552 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4553 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4554 s[i]->s.jt = NULL; /*later*/
4555 s[i]->s.jf = NULL; /*update in next stmt*/
4556 s[i]->s.k = IPPROTO_DSTOPTS;
4558 /* if (A == IPPROTO_ROUTING) goto v6advance */
4559 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4560 s[i]->s.jt = NULL; /*later*/
4561 s[i]->s.jf = NULL; /*update in next stmt*/
4562 s[i]->s.k = IPPROTO_ROUTING;
4564 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4565 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4566 s[i]->s.jt = NULL; /*later*/
4567 s[i]->s.jf = NULL; /*later*/
4568 s[i]->s.k = IPPROTO_FRAGMENT;
4579 * X = X + (P[X + 1] + 1) * 8;
4582 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4584 /* A = P[X + packet head] */
4585 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4586 s[i]->s.k = off_ll + off_nl;
4589 s[i] = new_stmt(BPF_ST);
4593 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4596 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4600 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4602 /* A = P[X + packet head]; */
4603 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4604 s[i]->s.k = off_ll + off_nl;
4607 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4611 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4615 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4618 s[i] = new_stmt(BPF_LD|BPF_MEM);
4622 /* goto again; (must use BPF_JA for backward jump) */
4623 s[i] = new_stmt(BPF_JMP|BPF_JA);
4624 s[i]->s.k = again - i - 1;
4625 s[i - 1]->s.jf = s[i];
4629 for (j = v6start; j <= v6end; j++)
4630 s[j]->s.jt = s[v6advance];
4635 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4637 s[fix2]->s.jf = s[i];
4643 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4644 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4645 s[i]->s.jt = NULL; /*later*/
4646 s[i]->s.jf = NULL; /*later*/
4647 s[i]->s.k = IPPROTO_AH;
4649 s[fix3]->s.jf = s[ahcheck];
4656 * X = X + (P[X + 1] + 2) * 4;
4659 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4661 /* A = P[X + packet head]; */
4662 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4663 s[i]->s.k = off_ll + off_nl;
4666 s[i] = new_stmt(BPF_ST);
4670 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4673 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4677 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4679 /* A = P[X + packet head] */
4680 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4681 s[i]->s.k = off_ll + off_nl;
4684 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4688 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4692 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4695 s[i] = new_stmt(BPF_LD|BPF_MEM);
4699 /* goto again; (must use BPF_JA for backward jump) */
4700 s[i] = new_stmt(BPF_JMP|BPF_JA);
4701 s[i]->s.k = again - i - 1;
4706 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4708 s[fix2]->s.jt = s[end];
4709 s[fix4]->s.jf = s[end];
4710 s[fix5]->s.jt = s[end];
4717 for (i = 0; i < max - 1; i++)
4718 s[i]->next = s[i + 1];
4719 s[max - 1]->next = NULL;
4724 b = new_block(JMP(BPF_JEQ));
4725 b->stmts = s[1]; /*remember, s[0] is dummy*/
4737 * Generate code that checks whether the packet is a packet for protocol
4738 * <proto> and whether the type field in that protocol's header has
4739 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4740 * IP packet and checks the protocol number in the IP header against <v>.
4742 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4743 * against Q_IP and Q_IPV6.
4745 static struct block *
4746 gen_proto(v, proto, dir)
4751 struct block *b0, *b1;
4753 if (dir != Q_DEFAULT)
4754 bpf_error("direction applied to 'proto'");
4759 b0 = gen_proto(v, Q_IP, dir);
4760 b1 = gen_proto(v, Q_IPV6, dir);
4768 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4769 * not LLC encapsulation with LLCSAP_IP.
4771 * For IEEE 802 networks - which includes 802.5 token ring
4772 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4773 * says that SNAP encapsulation is used, not LLC encapsulation
4776 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4777 * RFC 2225 say that SNAP encapsulation is used, not LLC
4778 * encapsulation with LLCSAP_IP.
4780 * So we always check for ETHERTYPE_IP.
4782 b0 = gen_linktype(ETHERTYPE_IP);
4784 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
4786 b1 = gen_protochain(v, Q_IP);
4796 * Frame Relay packets typically have an OSI
4797 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4798 * generates code to check for all the OSI
4799 * NLPIDs, so calling it and then adding a check
4800 * for the particular NLPID for which we're
4801 * looking is bogus, as we can just check for
4804 * What we check for is the NLPID and a frame
4805 * control field value of UI, i.e. 0x03 followed
4808 * XXX - assumes a 2-byte Frame Relay header with
4809 * DLCI and flags. What if the address is longer?
4811 * XXX - what about SNAP-encapsulated frames?
4813 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
4819 * Cisco uses an Ethertype lookalike - for OSI,
4822 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
4823 /* OSI in C-HDLC is stuffed with a fudge byte */
4824 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
4829 b0 = gen_linktype(LLCSAP_ISONS);
4830 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
4836 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4838 * 4 is the offset of the PDU type relative to the IS-IS
4841 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
4846 bpf_error("arp does not encapsulate another protocol");
4850 bpf_error("rarp does not encapsulate another protocol");
4854 bpf_error("atalk encapsulation is not specifiable");
4858 bpf_error("decnet encapsulation is not specifiable");
4862 bpf_error("sca does not encapsulate another protocol");
4866 bpf_error("lat does not encapsulate another protocol");
4870 bpf_error("moprc does not encapsulate another protocol");
4874 bpf_error("mopdl does not encapsulate another protocol");
4878 return gen_linktype(v);
4881 bpf_error("'udp proto' is bogus");
4885 bpf_error("'tcp proto' is bogus");
4889 bpf_error("'sctp proto' is bogus");
4893 bpf_error("'icmp proto' is bogus");
4897 bpf_error("'igmp proto' is bogus");
4901 bpf_error("'igrp proto' is bogus");
4905 bpf_error("'pim proto' is bogus");
4909 bpf_error("'vrrp proto' is bogus");
4914 b0 = gen_linktype(ETHERTYPE_IPV6);
4916 b1 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
4918 b1 = gen_protochain(v, Q_IPV6);
4924 bpf_error("'icmp6 proto' is bogus");
4928 bpf_error("'ah proto' is bogus");
4931 bpf_error("'ah proto' is bogus");
4934 bpf_error("'stp proto' is bogus");
4937 bpf_error("'ipx proto' is bogus");
4940 bpf_error("'netbeui proto' is bogus");
4943 bpf_error("'radio proto' is bogus");
4954 register const char *name;
4957 int proto = q.proto;
4961 bpf_u_int32 mask, addr;
4963 bpf_u_int32 **alist;
4966 struct sockaddr_in *sin4;
4967 struct sockaddr_in6 *sin6;
4968 struct addrinfo *res, *res0;
4969 struct in6_addr mask128;
4971 struct block *b, *tmp;
4972 int port, real_proto;
4978 addr = pcap_nametonetaddr(name);
4980 bpf_error("unknown network '%s'", name);
4981 /* Left justify network addr and calculate its network mask */
4983 while (addr && (addr & 0xff000000) == 0) {
4987 return gen_host(addr, mask, proto, dir, q.addr);
4991 if (proto == Q_LINK) {
4995 eaddr = pcap_ether_hostton(name);
4998 "unknown ether host '%s'", name);
4999 b = gen_ehostop(eaddr, dir);
5004 eaddr = pcap_ether_hostton(name);
5007 "unknown FDDI host '%s'", name);
5008 b = gen_fhostop(eaddr, dir);
5013 eaddr = pcap_ether_hostton(name);
5016 "unknown token ring host '%s'", name);
5017 b = gen_thostop(eaddr, dir);
5021 case DLT_IEEE802_11:
5022 case DLT_IEEE802_11_RADIO_AVS:
5023 case DLT_IEEE802_11_RADIO:
5024 case DLT_PRISM_HEADER:
5026 eaddr = pcap_ether_hostton(name);
5029 "unknown 802.11 host '%s'", name);
5030 b = gen_wlanhostop(eaddr, dir);
5034 case DLT_IP_OVER_FC:
5035 eaddr = pcap_ether_hostton(name);
5038 "unknown Fibre Channel host '%s'", name);
5039 b = gen_ipfchostop(eaddr, dir);
5048 * Check that the packet doesn't begin
5049 * with an LE Control marker. (We've
5050 * already generated a test for LANE.)
5052 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
5056 eaddr = pcap_ether_hostton(name);
5059 "unknown ether host '%s'", name);
5060 b = gen_ehostop(eaddr, dir);
5066 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5067 } else if (proto == Q_DECNET) {
5068 unsigned short dn_addr = __pcap_nametodnaddr(name);
5070 * I don't think DECNET hosts can be multihomed, so
5071 * there is no need to build up a list of addresses
5073 return (gen_host(dn_addr, 0, proto, dir, q.addr));
5076 alist = pcap_nametoaddr(name);
5077 if (alist == NULL || *alist == NULL)
5078 bpf_error("unknown host '%s'", name);
5080 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
5082 b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
5084 tmp = gen_host(**alist++, 0xffffffff,
5085 tproto, dir, q.addr);
5091 memset(&mask128, 0xff, sizeof(mask128));
5092 res0 = res = pcap_nametoaddrinfo(name);
5094 bpf_error("unknown host '%s'", name);
5096 tproto = tproto6 = proto;
5097 if (off_linktype == -1 && tproto == Q_DEFAULT) {
5101 for (res = res0; res; res = res->ai_next) {
5102 switch (res->ai_family) {
5104 if (tproto == Q_IPV6)
5107 sin4 = (struct sockaddr_in *)
5109 tmp = gen_host(ntohl(sin4->sin_addr.s_addr),
5110 0xffffffff, tproto, dir, q.addr);
5113 if (tproto6 == Q_IP)
5116 sin6 = (struct sockaddr_in6 *)
5118 tmp = gen_host6(&sin6->sin6_addr,
5119 &mask128, tproto6, dir, q.addr);
5130 bpf_error("unknown host '%s'%s", name,
5131 (proto == Q_DEFAULT)
5133 : " for specified address family");
5140 if (proto != Q_DEFAULT &&
5141 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
5142 bpf_error("illegal qualifier of 'port'");
5143 if (pcap_nametoport(name, &port, &real_proto) == 0)
5144 bpf_error("unknown port '%s'", name);
5145 if (proto == Q_UDP) {
5146 if (real_proto == IPPROTO_TCP)
5147 bpf_error("port '%s' is tcp", name);
5148 else if (real_proto == IPPROTO_SCTP)
5149 bpf_error("port '%s' is sctp", name);
5151 /* override PROTO_UNDEF */
5152 real_proto = IPPROTO_UDP;
5154 if (proto == Q_TCP) {
5155 if (real_proto == IPPROTO_UDP)
5156 bpf_error("port '%s' is udp", name);
5158 else if (real_proto == IPPROTO_SCTP)
5159 bpf_error("port '%s' is sctp", name);
5161 /* override PROTO_UNDEF */
5162 real_proto = IPPROTO_TCP;
5164 if (proto == Q_SCTP) {
5165 if (real_proto == IPPROTO_UDP)
5166 bpf_error("port '%s' is udp", name);
5168 else if (real_proto == IPPROTO_TCP)
5169 bpf_error("port '%s' is tcp", name);
5171 /* override PROTO_UNDEF */
5172 real_proto = IPPROTO_SCTP;
5175 return gen_port(port, real_proto, dir);
5177 b = gen_port(port, real_proto, dir);
5178 gen_or(gen_port6(port, real_proto, dir), b);
5183 if (proto != Q_DEFAULT &&
5184 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
5185 bpf_error("illegal qualifier of 'portrange'");
5186 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
5187 bpf_error("unknown port in range '%s'", name);
5188 if (proto == Q_UDP) {
5189 if (real_proto == IPPROTO_TCP)
5190 bpf_error("port in range '%s' is tcp", name);
5191 else if (real_proto == IPPROTO_SCTP)
5192 bpf_error("port in range '%s' is sctp", name);
5194 /* override PROTO_UNDEF */
5195 real_proto = IPPROTO_UDP;
5197 if (proto == Q_TCP) {
5198 if (real_proto == IPPROTO_UDP)
5199 bpf_error("port in range '%s' is udp", name);
5200 else if (real_proto == IPPROTO_SCTP)
5201 bpf_error("port in range '%s' is sctp", name);
5203 /* override PROTO_UNDEF */
5204 real_proto = IPPROTO_TCP;
5206 if (proto == Q_SCTP) {
5207 if (real_proto == IPPROTO_UDP)
5208 bpf_error("port in range '%s' is udp", name);
5209 else if (real_proto == IPPROTO_TCP)
5210 bpf_error("port in range '%s' is tcp", name);
5212 /* override PROTO_UNDEF */
5213 real_proto = IPPROTO_SCTP;
5216 return gen_portrange(port1, port2, real_proto, dir);
5218 b = gen_portrange(port1, port2, real_proto, dir);
5219 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
5225 eaddr = pcap_ether_hostton(name);
5227 bpf_error("unknown ether host: %s", name);
5229 alist = pcap_nametoaddr(name);
5230 if (alist == NULL || *alist == NULL)
5231 bpf_error("unknown host '%s'", name);
5232 b = gen_gateway(eaddr, alist, proto, dir);
5236 bpf_error("'gateway' not supported in this configuration");
5240 real_proto = lookup_proto(name, proto);
5241 if (real_proto >= 0)
5242 return gen_proto(real_proto, proto, dir);
5244 bpf_error("unknown protocol: %s", name);
5247 real_proto = lookup_proto(name, proto);
5248 if (real_proto >= 0)
5249 return gen_protochain(real_proto, proto, dir);
5251 bpf_error("unknown protocol: %s", name);
5263 gen_mcode(s1, s2, masklen, q)
5264 register const char *s1, *s2;
5265 register int masklen;
5268 register int nlen, mlen;
5271 nlen = __pcap_atoin(s1, &n);
5272 /* Promote short ipaddr */
5276 mlen = __pcap_atoin(s2, &m);
5277 /* Promote short ipaddr */
5280 bpf_error("non-network bits set in \"%s mask %s\"",
5283 /* Convert mask len to mask */
5285 bpf_error("mask length must be <= 32");
5288 * X << 32 is not guaranteed by C to be 0; it's
5293 m = 0xffffffff << (32 - masklen);
5295 bpf_error("non-network bits set in \"%s/%d\"",
5302 return gen_host(n, m, q.proto, q.dir, q.addr);
5305 bpf_error("Mask syntax for networks only");
5314 register const char *s;
5319 int proto = q.proto;
5325 else if (q.proto == Q_DECNET)
5326 vlen = __pcap_atodn(s, &v);
5328 vlen = __pcap_atoin(s, &v);
5335 if (proto == Q_DECNET)
5336 return gen_host(v, 0, proto, dir, q.addr);
5337 else if (proto == Q_LINK) {
5338 bpf_error("illegal link layer address");
5341 if (s == NULL && q.addr == Q_NET) {
5342 /* Promote short net number */
5343 while (v && (v & 0xff000000) == 0) {
5348 /* Promote short ipaddr */
5352 return gen_host(v, mask, proto, dir, q.addr);
5357 proto = IPPROTO_UDP;
5358 else if (proto == Q_TCP)
5359 proto = IPPROTO_TCP;
5360 else if (proto == Q_SCTP)
5361 proto = IPPROTO_SCTP;
5362 else if (proto == Q_DEFAULT)
5363 proto = PROTO_UNDEF;
5365 bpf_error("illegal qualifier of 'port'");
5368 return gen_port((int)v, proto, dir);
5372 b = gen_port((int)v, proto, dir);
5373 gen_or(gen_port6((int)v, proto, dir), b);
5380 proto = IPPROTO_UDP;
5381 else if (proto == Q_TCP)
5382 proto = IPPROTO_TCP;
5383 else if (proto == Q_SCTP)
5384 proto = IPPROTO_SCTP;
5385 else if (proto == Q_DEFAULT)
5386 proto = PROTO_UNDEF;
5388 bpf_error("illegal qualifier of 'portrange'");
5391 return gen_portrange((int)v, (int)v, proto, dir);
5395 b = gen_portrange((int)v, (int)v, proto, dir);
5396 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
5402 bpf_error("'gateway' requires a name");
5406 return gen_proto((int)v, proto, dir);
5409 return gen_protochain((int)v, proto, dir);
5424 gen_mcode6(s1, s2, masklen, q)
5425 register const char *s1, *s2;
5426 register int masklen;
5429 struct addrinfo *res;
5430 struct in6_addr *addr;
5431 struct in6_addr mask;
5436 bpf_error("no mask %s supported", s2);
5438 res = pcap_nametoaddrinfo(s1);
5440 bpf_error("invalid ip6 address %s", s1);
5442 bpf_error("%s resolved to multiple address", s1);
5443 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
5445 if (sizeof(mask) * 8 < masklen)
5446 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
5447 memset(&mask, 0, sizeof(mask));
5448 memset(&mask, 0xff, masklen / 8);
5450 mask.s6_addr[masklen / 8] =
5451 (0xff << (8 - masklen % 8)) & 0xff;
5454 a = (u_int32_t *)addr;
5455 m = (u_int32_t *)&mask;
5456 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
5457 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
5458 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
5466 bpf_error("Mask syntax for networks only");
5470 b = gen_host6(addr, &mask, q.proto, q.dir, q.addr);
5475 bpf_error("invalid qualifier against IPv6 address");
5484 register const u_char *eaddr;
5487 struct block *b, *tmp;
5489 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
5492 return gen_ehostop(eaddr, (int)q.dir);
5494 return gen_fhostop(eaddr, (int)q.dir);
5496 return gen_thostop(eaddr, (int)q.dir);
5497 case DLT_IEEE802_11:
5498 case DLT_IEEE802_11_RADIO_AVS:
5499 case DLT_IEEE802_11_RADIO:
5500 case DLT_PRISM_HEADER:
5502 return gen_wlanhostop(eaddr, (int)q.dir);
5506 * Check that the packet doesn't begin with an
5507 * LE Control marker. (We've already generated
5510 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5515 * Now check the MAC address.
5517 b = gen_ehostop(eaddr, (int)q.dir);
5522 case DLT_IP_OVER_FC:
5523 return gen_ipfchostop(eaddr, (int)q.dir);
5525 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5529 bpf_error("ethernet address used in non-ether expression");
5536 struct slist *s0, *s1;
5539 * This is definitely not the best way to do this, but the
5540 * lists will rarely get long.
5547 static struct slist *
5553 s = new_stmt(BPF_LDX|BPF_MEM);
5558 static struct slist *
5564 s = new_stmt(BPF_LD|BPF_MEM);
5570 * Modify "index" to use the value stored into its register as an
5571 * offset relative to the beginning of the header for the protocol
5572 * "proto", and allocate a register and put an item "size" bytes long
5573 * (1, 2, or 4) at that offset into that register, making it the register
5577 gen_load(proto, inst, size)
5582 struct slist *s, *tmp;
5584 int regno = alloc_reg();
5586 free_reg(inst->regno);
5590 bpf_error("data size must be 1, 2, or 4");
5606 bpf_error("unsupported index operation");
5610 * The offset is relative to the beginning of the packet
5611 * data, if we have a radio header. (If we don't, this
5614 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
5615 linktype != DLT_IEEE802_11_RADIO &&
5616 linktype != DLT_PRISM_HEADER)
5617 bpf_error("radio information not present in capture");
5620 * Load into the X register the offset computed into the
5621 * register specifed by "index".
5623 s = xfer_to_x(inst);
5626 * Load the item at that offset.
5628 tmp = new_stmt(BPF_LD|BPF_IND|size);
5630 sappend(inst->s, s);
5635 * The offset is relative to the beginning of
5636 * the link-layer header.
5638 * XXX - what about ATM LANE? Should the index be
5639 * relative to the beginning of the AAL5 frame, so
5640 * that 0 refers to the beginning of the LE Control
5641 * field, or relative to the beginning of the LAN
5642 * frame, so that 0 refers, for Ethernet LANE, to
5643 * the beginning of the destination address?
5645 s = gen_llprefixlen();
5648 * If "s" is non-null, it has code to arrange that the
5649 * X register contains the length of the prefix preceding
5650 * the link-layer header. Add to it the offset computed
5651 * into the register specified by "index", and move that
5652 * into the X register. Otherwise, just load into the X
5653 * register the offset computed into the register specifed
5657 sappend(s, xfer_to_a(inst));
5658 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5659 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5661 s = xfer_to_x(inst);
5664 * Load the item at the sum of the offset we've put in the
5665 * X register and the offset of the start of the link
5666 * layer header (which is 0 if the radio header is
5667 * variable-length; that header length is what we put
5668 * into the X register and then added to the index).
5670 tmp = new_stmt(BPF_LD|BPF_IND|size);
5673 sappend(inst->s, s);
5689 * The offset is relative to the beginning of
5690 * the network-layer header.
5691 * XXX - are there any cases where we want
5694 s = gen_llprefixlen();
5697 * If "s" is non-null, it has code to arrange that the
5698 * X register contains the length of the prefix preceding
5699 * the link-layer header. Add to it the offset computed
5700 * into the register specified by "index", and move that
5701 * into the X register. Otherwise, just load into the X
5702 * register the offset computed into the register specifed
5706 sappend(s, xfer_to_a(inst));
5707 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5708 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5710 s = xfer_to_x(inst);
5713 * Load the item at the sum of the offset we've put in the
5714 * X register, the offset of the start of the network
5715 * layer header, and the offset of the start of the link
5716 * layer header (which is 0 if the radio header is
5717 * variable-length; that header length is what we put
5718 * into the X register and then added to the index).
5720 tmp = new_stmt(BPF_LD|BPF_IND|size);
5721 tmp->s.k = off_ll + off_nl;
5723 sappend(inst->s, s);
5726 * Do the computation only if the packet contains
5727 * the protocol in question.
5729 b = gen_proto_abbrev(proto);
5731 gen_and(inst->b, b);
5744 * The offset is relative to the beginning of
5745 * the transport-layer header.
5747 * Load the X register with the length of the IPv4 header
5748 * (plus the offset of the link-layer header, if it's
5749 * a variable-length header), in bytes.
5751 * XXX - are there any cases where we want
5753 * XXX - we should, if we're built with
5754 * IPv6 support, generate code to load either
5755 * IPv4, IPv6, or both, as appropriate.
5757 s = gen_loadx_iphdrlen();
5760 * The X register now contains the sum of the length
5761 * of any variable-length header preceding the link-layer
5762 * header and the length of the network-layer header.
5763 * Load into the A register the offset relative to
5764 * the beginning of the transport layer header,
5765 * add the X register to that, move that to the
5766 * X register, and load with an offset from the
5767 * X register equal to the offset of the network
5768 * layer header relative to the beginning of
5769 * the link-layer header plus the length of any
5770 * fixed-length header preceding the link-layer
5773 sappend(s, xfer_to_a(inst));
5774 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5775 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5776 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
5777 tmp->s.k = off_ll + off_nl;
5778 sappend(inst->s, s);
5781 * Do the computation only if the packet contains
5782 * the protocol in question - which is true only
5783 * if this is an IP datagram and is the first or
5784 * only fragment of that datagram.
5786 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
5788 gen_and(inst->b, b);
5790 gen_and(gen_proto_abbrev(Q_IP), b);
5796 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5800 inst->regno = regno;
5801 s = new_stmt(BPF_ST);
5803 sappend(inst->s, s);
5809 gen_relation(code, a0, a1, reversed)
5811 struct arth *a0, *a1;
5814 struct slist *s0, *s1, *s2;
5815 struct block *b, *tmp;
5819 if (code == BPF_JEQ) {
5820 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
5821 b = new_block(JMP(code));
5825 b = new_block(BPF_JMP|code|BPF_X);
5831 sappend(a0->s, a1->s);
5835 free_reg(a0->regno);
5836 free_reg(a1->regno);
5838 /* 'and' together protocol checks */
5841 gen_and(a0->b, tmp = a1->b);
5857 int regno = alloc_reg();
5858 struct arth *a = (struct arth *)newchunk(sizeof(*a));
5861 s = new_stmt(BPF_LD|BPF_LEN);
5862 s->next = new_stmt(BPF_ST);
5863 s->next->s.k = regno;
5878 a = (struct arth *)newchunk(sizeof(*a));
5882 s = new_stmt(BPF_LD|BPF_IMM);
5884 s->next = new_stmt(BPF_ST);
5900 s = new_stmt(BPF_ALU|BPF_NEG);
5903 s = new_stmt(BPF_ST);
5911 gen_arth(code, a0, a1)
5913 struct arth *a0, *a1;
5915 struct slist *s0, *s1, *s2;
5919 s2 = new_stmt(BPF_ALU|BPF_X|code);
5924 sappend(a0->s, a1->s);
5926 free_reg(a0->regno);
5927 free_reg(a1->regno);
5929 s0 = new_stmt(BPF_ST);
5930 a0->regno = s0->s.k = alloc_reg();
5937 * Here we handle simple allocation of the scratch registers.
5938 * If too many registers are alloc'd, the allocator punts.
5940 static int regused[BPF_MEMWORDS];
5944 * Return the next free register.
5949 int n = BPF_MEMWORDS;
5952 if (regused[curreg])
5953 curreg = (curreg + 1) % BPF_MEMWORDS;
5955 regused[curreg] = 1;
5959 bpf_error("too many registers needed to evaluate expression");
5965 * Return a register to the table so it can
5975 static struct block *
5982 s = new_stmt(BPF_LD|BPF_LEN);
5983 b = new_block(JMP(jmp));
5994 return gen_len(BPF_JGE, n);
5998 * Actually, this is less than or equal.
6006 b = gen_len(BPF_JGT, n);
6013 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6014 * the beginning of the link-layer header.
6015 * XXX - that means you can't test values in the radiotap header, but
6016 * as that header is difficult if not impossible to parse generally
6017 * without a loop, that might not be a severe problem. A new keyword
6018 * "radio" could be added for that, although what you'd really want
6019 * would be a way of testing particular radio header values, which
6020 * would generate code appropriate to the radio header in question.
6023 gen_byteop(op, idx, val)
6034 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6037 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6041 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
6045 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
6049 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
6053 b = new_block(JMP(BPF_JEQ));
6060 static u_char abroadcast[] = { 0x0 };
6063 gen_broadcast(proto)
6066 bpf_u_int32 hostmask;
6067 struct block *b0, *b1, *b2;
6068 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6076 case DLT_ARCNET_LINUX:
6077 return gen_ahostop(abroadcast, Q_DST);
6079 return gen_ehostop(ebroadcast, Q_DST);
6081 return gen_fhostop(ebroadcast, Q_DST);
6083 return gen_thostop(ebroadcast, Q_DST);
6084 case DLT_IEEE802_11:
6085 case DLT_IEEE802_11_RADIO_AVS:
6086 case DLT_IEEE802_11_RADIO:
6088 case DLT_PRISM_HEADER:
6089 return gen_wlanhostop(ebroadcast, Q_DST);
6090 case DLT_IP_OVER_FC:
6091 return gen_ipfchostop(ebroadcast, Q_DST);
6095 * Check that the packet doesn't begin with an
6096 * LE Control marker. (We've already generated
6099 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6104 * Now check the MAC address.
6106 b0 = gen_ehostop(ebroadcast, Q_DST);
6112 bpf_error("not a broadcast link");
6117 b0 = gen_linktype(ETHERTYPE_IP);
6118 hostmask = ~netmask;
6119 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
6120 b2 = gen_mcmp(OR_NET, 16, BPF_W,
6121 (bpf_int32)(~0 & hostmask), hostmask);
6126 bpf_error("only link-layer/IP broadcast filters supported");
6132 * Generate code to test the low-order bit of a MAC address (that's
6133 * the bottom bit of the *first* byte).
6135 static struct block *
6136 gen_mac_multicast(offset)
6139 register struct block *b0;
6140 register struct slist *s;
6142 /* link[offset] & 1 != 0 */
6143 s = gen_load_a(OR_LINK, offset, BPF_B);
6144 b0 = new_block(JMP(BPF_JSET));
6151 gen_multicast(proto)
6154 register struct block *b0, *b1, *b2;
6155 register struct slist *s;
6163 case DLT_ARCNET_LINUX:
6164 /* all ARCnet multicasts use the same address */
6165 return gen_ahostop(abroadcast, Q_DST);
6167 /* ether[0] & 1 != 0 */
6168 return gen_mac_multicast(0);
6171 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
6173 * XXX - was that referring to bit-order issues?
6175 /* fddi[1] & 1 != 0 */
6176 return gen_mac_multicast(1);
6178 /* tr[2] & 1 != 0 */
6179 return gen_mac_multicast(2);
6180 case DLT_IEEE802_11:
6181 case DLT_IEEE802_11_RADIO_AVS:
6183 case DLT_IEEE802_11_RADIO:
6184 case DLT_PRISM_HEADER:
6188 * For control frames, there is no DA.
6190 * For management frames, DA is at an
6191 * offset of 4 from the beginning of
6194 * For data frames, DA is at an offset
6195 * of 4 from the beginning of the packet
6196 * if To DS is clear and at an offset of
6197 * 16 from the beginning of the packet
6202 * Generate the tests to be done for data frames.
6204 * First, check for To DS set, i.e. "link[1] & 0x01".
6206 s = gen_load_a(OR_LINK, 1, BPF_B);
6207 b1 = new_block(JMP(BPF_JSET));
6208 b1->s.k = 0x01; /* To DS */
6212 * If To DS is set, the DA is at 16.
6214 b0 = gen_mac_multicast(16);
6218 * Now, check for To DS not set, i.e. check
6219 * "!(link[1] & 0x01)".
6221 s = gen_load_a(OR_LINK, 1, BPF_B);
6222 b2 = new_block(JMP(BPF_JSET));
6223 b2->s.k = 0x01; /* To DS */
6228 * If To DS is not set, the DA is at 4.
6230 b1 = gen_mac_multicast(4);
6234 * Now OR together the last two checks. That gives
6235 * the complete set of checks for data frames.
6240 * Now check for a data frame.
6241 * I.e, check "link[0] & 0x08".
6243 s = gen_load_a(OR_LINK, 0, BPF_B);
6244 b1 = new_block(JMP(BPF_JSET));
6249 * AND that with the checks done for data frames.
6254 * If the high-order bit of the type value is 0, this
6255 * is a management frame.
6256 * I.e, check "!(link[0] & 0x08)".
6258 s = gen_load_a(OR_LINK, 0, BPF_B);
6259 b2 = new_block(JMP(BPF_JSET));
6265 * For management frames, the DA is at 4.
6267 b1 = gen_mac_multicast(4);
6271 * OR that with the checks done for data frames.
6272 * That gives the checks done for management and
6278 * If the low-order bit of the type value is 1,
6279 * this is either a control frame or a frame
6280 * with a reserved type, and thus not a
6283 * I.e., check "!(link[0] & 0x04)".
6285 s = gen_load_a(OR_LINK, 0, BPF_B);
6286 b1 = new_block(JMP(BPF_JSET));
6292 * AND that with the checks for data and management
6297 case DLT_IP_OVER_FC:
6298 b0 = gen_mac_multicast(2);
6303 * Check that the packet doesn't begin with an
6304 * LE Control marker. (We've already generated
6307 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6311 /* ether[off_mac] & 1 != 0 */
6312 b0 = gen_mac_multicast(off_mac);
6320 /* Link not known to support multicasts */
6324 b0 = gen_linktype(ETHERTYPE_IP);
6325 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
6331 b0 = gen_linktype(ETHERTYPE_IPV6);
6332 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
6337 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6343 * generate command for inbound/outbound. It's here so we can
6344 * make it link-type specific. 'dir' = 0 implies "inbound",
6345 * = 1 implies "outbound".
6351 register struct block *b0;
6354 * Only some data link types support inbound/outbound qualifiers.
6358 b0 = gen_relation(BPF_JEQ,
6359 gen_load(Q_LINK, gen_loadi(0), 1),
6367 * Match packets sent by this machine.
6369 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
6372 * Match packets sent to this machine.
6373 * (No broadcast or multicast packets, or
6374 * packets sent to some other machine and
6375 * received promiscuously.)
6377 * XXX - packets sent to other machines probably
6378 * shouldn't be matched, but what about broadcast
6379 * or multicast packets we received?
6381 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_HOST);
6385 #ifdef HAVE_NET_PFVAR_H
6387 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
6388 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
6394 /* match outgoing packets */
6395 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
6397 /* match incoming packets */
6398 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
6402 case DLT_JUNIPER_MFR:
6403 case DLT_JUNIPER_MLFR:
6404 case DLT_JUNIPER_MLPPP:
6405 case DLT_JUNIPER_ATM1:
6406 case DLT_JUNIPER_ATM2:
6407 case DLT_JUNIPER_PPPOE:
6408 case DLT_JUNIPER_PPPOE_ATM:
6409 case DLT_JUNIPER_GGSN:
6410 case DLT_JUNIPER_ES:
6411 case DLT_JUNIPER_MONITOR:
6412 case DLT_JUNIPER_SERVICES:
6413 case DLT_JUNIPER_ETHER:
6414 case DLT_JUNIPER_PPP:
6415 case DLT_JUNIPER_FRELAY:
6416 case DLT_JUNIPER_CHDLC:
6417 case DLT_JUNIPER_VP:
6418 /* juniper flags (including direction) are stored
6419 * the byte after the 3-byte magic number */
6421 /* match outgoing packets */
6422 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
6424 /* match incoming packets */
6425 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
6430 bpf_error("inbound/outbound not supported on linktype %d",
6438 #ifdef HAVE_NET_PFVAR_H
6439 /* PF firewall log matched interface */
6441 gen_pf_ifname(const char *ifname)
6446 if (linktype == DLT_PFLOG) {
6447 len = sizeof(((struct pfloghdr *)0)->ifname);
6448 off = offsetof(struct pfloghdr, ifname);
6450 bpf_error("ifname not supported on linktype 0x%x", linktype);
6453 if (strlen(ifname) >= len) {
6454 bpf_error("ifname interface names can only be %d characters",
6458 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
6462 /* PF firewall log ruleset name */
6464 gen_pf_ruleset(char *ruleset)
6468 if (linktype != DLT_PFLOG) {
6469 bpf_error("ruleset not supported on linktype 0x%x", linktype);
6472 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
6473 bpf_error("ruleset names can only be %ld characters",
6474 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
6477 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
6478 strlen(ruleset), (const u_char *)ruleset);
6482 /* PF firewall log rule number */
6488 if (linktype == DLT_PFLOG) {
6489 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
6492 bpf_error("rnr not supported on linktype 0x%x", linktype);
6499 /* PF firewall log sub-rule number */
6501 gen_pf_srnr(int srnr)
6505 if (linktype != DLT_PFLOG) {
6506 bpf_error("srnr not supported on linktype 0x%x", linktype);
6510 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
6515 /* PF firewall log reason code */
6517 gen_pf_reason(int reason)
6521 if (linktype == DLT_PFLOG) {
6522 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
6525 bpf_error("reason not supported on linktype 0x%x", linktype);
6532 /* PF firewall log action */
6534 gen_pf_action(int action)
6538 if (linktype == DLT_PFLOG) {
6539 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
6542 bpf_error("action not supported on linktype 0x%x", linktype);
6548 #else /* !HAVE_NET_PFVAR_H */
6550 gen_pf_ifname(const char *ifname)
6552 bpf_error("libpcap was compiled without pf support");
6558 gen_pf_ruleset(char *ruleset)
6560 bpf_error("libpcap was compiled on a machine without pf support");
6568 bpf_error("libpcap was compiled on a machine without pf support");
6574 gen_pf_srnr(int srnr)
6576 bpf_error("libpcap was compiled on a machine without pf support");
6582 gen_pf_reason(int reason)
6584 bpf_error("libpcap was compiled on a machine without pf support");
6590 gen_pf_action(int action)
6592 bpf_error("libpcap was compiled on a machine without pf support");
6596 #endif /* HAVE_NET_PFVAR_H */
6600 register const u_char *eaddr;
6603 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6604 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
6605 return gen_ahostop(eaddr, (int)q.dir);
6607 bpf_error("ARCnet address used in non-arc expression");
6612 static struct block *
6613 gen_ahostop(eaddr, dir)
6614 register const u_char *eaddr;
6617 register struct block *b0, *b1;
6620 /* src comes first, different from Ethernet */
6622 return gen_bcmp(OR_LINK, 0, 1, eaddr);
6625 return gen_bcmp(OR_LINK, 1, 1, eaddr);
6628 b0 = gen_ahostop(eaddr, Q_SRC);
6629 b1 = gen_ahostop(eaddr, Q_DST);
6635 b0 = gen_ahostop(eaddr, Q_SRC);
6636 b1 = gen_ahostop(eaddr, Q_DST);
6645 * support IEEE 802.1Q VLAN trunk over ethernet
6651 struct block *b0, *b1;
6653 /* can't check for VLAN-encapsulated packets inside MPLS */
6654 if (label_stack_depth > 0)
6655 bpf_error("no VLAN match after MPLS");
6658 * Change the offsets to point to the type and data fields within
6659 * the VLAN packet. Just increment the offsets, so that we
6660 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6661 * capture VLAN 200 encapsulated within VLAN 100.
6663 * XXX - this is a bit of a kludge. If we were to split the
6664 * compiler into a parser that parses an expression and
6665 * generates an expression tree, and a code generator that
6666 * takes an expression tree (which could come from our
6667 * parser or from some other parser) and generates BPF code,
6668 * we could perhaps make the offsets parameters of routines
6669 * and, in the handler for an "AND" node, pass to subnodes
6670 * other than the VLAN node the adjusted offsets.
6672 * This would mean that "vlan" would, instead of changing the
6673 * behavior of *all* tests after it, change only the behavior
6674 * of tests ANDed with it. That would change the documented
6675 * semantics of "vlan", which might break some expressions.
6676 * However, it would mean that "(vlan and ip) or ip" would check
6677 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6678 * checking only for VLAN-encapsulated IP, so that could still
6679 * be considered worth doing; it wouldn't break expressions
6680 * that are of the form "vlan and ..." or "vlan N and ...",
6681 * which I suspect are the most common expressions involving
6682 * "vlan". "vlan or ..." doesn't necessarily do what the user
6683 * would really want, now, as all the "or ..." tests would
6684 * be done assuming a VLAN, even though the "or" could be viewed
6685 * as meaning "or, if this isn't a VLAN packet...".
6687 orig_linktype = off_linktype; /* save original values */
6699 bpf_error("no VLAN support for data link type %d",
6704 /* check for VLAN */
6705 b0 = gen_cmp(OR_LINK, orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q);
6707 /* If a specific VLAN is requested, check VLAN id */
6708 if (vlan_num >= 0) {
6709 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_H, (bpf_int32)vlan_num,
6725 struct block *b0,*b1;
6728 * Change the offsets to point to the type and data fields within
6729 * the MPLS packet. Just increment the offsets, so that we
6730 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6731 * capture packets with an outer label of 100000 and an inner
6734 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6738 if (label_stack_depth > 0) {
6739 /* just match the bottom-of-stack bit clear */
6740 b0 = gen_mcmp(OR_LINK, orig_nl-2, BPF_B, 0, 0x01);
6743 * Indicate that we're checking MPLS-encapsulated headers,
6744 * to make sure higher level code generators don't try to
6745 * match against IP-related protocols such as Q_ARP, Q_RARP
6750 case DLT_C_HDLC: /* fall through */
6752 b0 = gen_linktype(ETHERTYPE_MPLS);
6756 b0 = gen_linktype(PPP_MPLS_UCAST);
6759 /* FIXME add other DLT_s ...
6760 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6761 * leave it for now */
6764 bpf_error("no MPLS support for data link type %d",
6772 /* If a specific MPLS label is requested, check it */
6773 if (label_num >= 0) {
6774 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
6775 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_W, (bpf_int32)label_num,
6776 0xfffff000); /* only compare the first 20 bits */
6783 label_stack_depth++;
6788 * Support PPPOE discovery and session.
6793 /* check for PPPoE discovery */
6794 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
6803 * Test against the PPPoE session link-layer type.
6805 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
6808 * Change the offsets to point to the type and data fields within
6811 * XXX - this is a bit of a kludge. If we were to split the
6812 * compiler into a parser that parses an expression and
6813 * generates an expression tree, and a code generator that
6814 * takes an expression tree (which could come from our
6815 * parser or from some other parser) and generates BPF code,
6816 * we could perhaps make the offsets parameters of routines
6817 * and, in the handler for an "AND" node, pass to subnodes
6818 * other than the PPPoE node the adjusted offsets.
6820 * This would mean that "pppoes" would, instead of changing the
6821 * behavior of *all* tests after it, change only the behavior
6822 * of tests ANDed with it. That would change the documented
6823 * semantics of "pppoes", which might break some expressions.
6824 * However, it would mean that "(pppoes and ip) or ip" would check
6825 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6826 * checking only for VLAN-encapsulated IP, so that could still
6827 * be considered worth doing; it wouldn't break expressions
6828 * that are of the form "pppoes and ..." which I suspect are the
6829 * most common expressions involving "pppoes". "pppoes or ..."
6830 * doesn't necessarily do what the user would really want, now,
6831 * as all the "or ..." tests would be done assuming PPPoE, even
6832 * though the "or" could be viewed as meaning "or, if this isn't
6833 * a PPPoE packet...".
6835 orig_linktype = off_linktype; /* save original values */
6839 * The "network-layer" protocol is PPPoE, which has a 6-byte
6840 * PPPoE header, followed by PPP payload, so we set the
6841 * offsets to the network layer offset plus 6 bytes for
6842 * the PPPoE header plus the values appropriate for PPP when
6843 * encapsulated in Ethernet (which means there's no HDLC
6846 off_linktype = orig_nl + 6;
6847 off_nl = orig_nl + 6 + 2;
6848 off_nl_nosnap = orig_nl + 6 + 2;
6851 * Set the link-layer type to PPP, as all subsequent tests will
6852 * be on the encapsulated PPP header.
6860 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
6872 bpf_error("'vpi' supported only on raw ATM");
6873 if (off_vpi == (u_int)-1)
6875 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
6881 bpf_error("'vci' supported only on raw ATM");
6882 if (off_vci == (u_int)-1)
6884 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
6889 if (off_proto == (u_int)-1)
6890 abort(); /* XXX - this isn't on FreeBSD */
6891 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
6896 if (off_payload == (u_int)-1)
6898 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
6899 0xffffffff, jtype, reverse, jvalue);
6904 bpf_error("'callref' supported only on raw ATM");
6905 if (off_proto == (u_int)-1)
6907 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
6908 jtype, reverse, jvalue);
6918 gen_atmtype_abbrev(type)
6921 struct block *b0, *b1;
6926 /* Get all packets in Meta signalling Circuit */
6928 bpf_error("'metac' supported only on raw ATM");
6929 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6930 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
6935 /* Get all packets in Broadcast Circuit*/
6937 bpf_error("'bcc' supported only on raw ATM");
6938 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6939 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
6944 /* Get all cells in Segment OAM F4 circuit*/
6946 bpf_error("'oam4sc' supported only on raw ATM");
6947 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6948 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6953 /* Get all cells in End-to-End OAM F4 Circuit*/
6955 bpf_error("'oam4ec' supported only on raw ATM");
6956 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6957 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6962 /* Get all packets in connection Signalling Circuit */
6964 bpf_error("'sc' supported only on raw ATM");
6965 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6966 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
6971 /* Get all packets in ILMI Circuit */
6973 bpf_error("'ilmic' supported only on raw ATM");
6974 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6975 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
6980 /* Get all LANE packets */
6982 bpf_error("'lane' supported only on raw ATM");
6983 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
6986 * Arrange that all subsequent tests assume LANE
6987 * rather than LLC-encapsulated packets, and set
6988 * the offsets appropriately for LANE-encapsulated
6991 * "off_mac" is the offset of the Ethernet header,
6992 * which is 2 bytes past the ATM pseudo-header
6993 * (skipping the pseudo-header and 2-byte LE Client
6994 * field). The other offsets are Ethernet offsets
6995 * relative to "off_mac".
6998 off_mac = off_payload + 2; /* MAC header */
6999 off_linktype = off_mac + 12;
7000 off_nl = off_mac + 14; /* Ethernet II */
7001 off_nl_nosnap = off_mac + 17; /* 802.3+802.2 */
7005 /* Get all LLC-encapsulated packets */
7007 bpf_error("'llc' supported only on raw ATM");
7008 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
7019 * Filtering for MTP2 messages based on li value
7020 * FISU, length is null
7021 * LSSU, length is 1 or 2
7022 * MSU, length is 3 or more
7025 gen_mtp2type_abbrev(type)
7028 struct block *b0, *b1;
7033 if ( (linktype != DLT_MTP2) &&
7034 (linktype != DLT_MTP2_WITH_PHDR) )
7035 bpf_error("'fisu' supported only on MTP2");
7036 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
7037 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
7041 if ( (linktype != DLT_MTP2) &&
7042 (linktype != DLT_MTP2_WITH_PHDR) )
7043 bpf_error("'lssu' supported only on MTP2");
7044 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
7045 b1 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
7050 if ( (linktype != DLT_MTP2) &&
7051 (linktype != DLT_MTP2_WITH_PHDR) )
7052 bpf_error("'msu' supported only on MTP2");
7053 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
7063 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
7070 bpf_u_int32 val1 , val2 , val3;
7072 switch (mtp3field) {
7075 if (off_sio == (u_int)-1)
7076 bpf_error("'sio' supported only on SS7");
7077 /* sio coded on 1 byte so max value 255 */
7079 bpf_error("sio value %u too big; max value = 255",
7081 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
7082 (u_int)jtype, reverse, (u_int)jvalue);
7086 if (off_opc == (u_int)-1)
7087 bpf_error("'opc' supported only on SS7");
7088 /* opc coded on 14 bits so max value 16383 */
7090 bpf_error("opc value %u too big; max value = 16383",
7092 /* the following instructions are made to convert jvalue
7093 * to the form used to write opc in an ss7 message*/
7094 val1 = jvalue & 0x00003c00;
7096 val2 = jvalue & 0x000003fc;
7098 val3 = jvalue & 0x00000003;
7100 jvalue = val1 + val2 + val3;
7101 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
7102 (u_int)jtype, reverse, (u_int)jvalue);
7106 if (off_dpc == (u_int)-1)
7107 bpf_error("'dpc' supported only on SS7");
7108 /* dpc coded on 14 bits so max value 16383 */
7110 bpf_error("dpc value %u too big; max value = 16383",
7112 /* the following instructions are made to convert jvalue
7113 * to the forme used to write dpc in an ss7 message*/
7114 val1 = jvalue & 0x000000ff;
7116 val2 = jvalue & 0x00003f00;
7118 jvalue = val1 + val2;
7119 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
7120 (u_int)jtype, reverse, (u_int)jvalue);
7124 if (off_sls == (u_int)-1)
7125 bpf_error("'sls' supported only on SS7");
7126 /* sls coded on 4 bits so max value 15 */
7128 bpf_error("sls value %u too big; max value = 15",
7130 /* the following instruction is made to convert jvalue
7131 * to the forme used to write sls in an ss7 message*/
7132 jvalue = jvalue << 4;
7133 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
7134 (u_int)jtype,reverse, (u_int)jvalue);
7143 static struct block *
7144 gen_msg_abbrev(type)
7150 * Q.2931 signalling protocol messages for handling virtual circuits
7151 * establishment and teardown
7156 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
7160 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
7164 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
7168 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
7172 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
7175 case A_RELEASE_DONE:
7176 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
7186 gen_atmmulti_abbrev(type)
7189 struct block *b0, *b1;
7195 bpf_error("'oam' supported only on raw ATM");
7196 b1 = gen_atmmulti_abbrev(A_OAMF4);
7201 bpf_error("'oamf4' supported only on raw ATM");
7203 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
7204 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
7206 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
7212 * Get Q.2931 signalling messages for switched
7213 * virtual connection
7216 bpf_error("'connectmsg' supported only on raw ATM");
7217 b0 = gen_msg_abbrev(A_SETUP);
7218 b1 = gen_msg_abbrev(A_CALLPROCEED);
7220 b0 = gen_msg_abbrev(A_CONNECT);
7222 b0 = gen_msg_abbrev(A_CONNECTACK);
7224 b0 = gen_msg_abbrev(A_RELEASE);
7226 b0 = gen_msg_abbrev(A_RELEASE_DONE);
7228 b0 = gen_atmtype_abbrev(A_SC);
7234 bpf_error("'metaconnect' supported only on raw ATM");
7235 b0 = gen_msg_abbrev(A_SETUP);
7236 b1 = gen_msg_abbrev(A_CALLPROCEED);
7238 b0 = gen_msg_abbrev(A_CONNECT);
7240 b0 = gen_msg_abbrev(A_RELEASE);
7242 b0 = gen_msg_abbrev(A_RELEASE_DONE);
7244 b0 = gen_atmtype_abbrev(A_METAC);