1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25 static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.221.2.34 2005/09/05 09:08:04 guy Exp $ (LBL)";
34 #include <pcap-stdinc.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
41 * XXX - why was this included even on UNIX?
50 #include <sys/param.h>
53 #include <netinet/in.h>
69 #include "ethertype.h"
74 #include "sunatmpos.h"
80 #define offsetof(s, e) ((size_t)&((s *)0)->e)
84 #include <netdb.h> /* for "struct addrinfo" */
87 #include <pcap-namedb.h>
92 #define IPPROTO_SCTP 132
95 #ifdef HAVE_OS_PROTO_H
99 #define JMP(c) ((c)|BPF_JMP|BPF_K)
102 static jmp_buf top_ctx;
103 static pcap_t *bpf_pcap;
105 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
106 static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
110 static int pcap_fddipad;
115 bpf_error(const char *fmt, ...)
120 if (bpf_pcap != NULL)
121 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
128 static void init_linktype(pcap_t *);
130 static int alloc_reg(void);
131 static void free_reg(int);
133 static struct block *root;
136 * Value passed to gen_load_a() to indicate what the offset argument
140 OR_PACKET, /* relative to the beginning of the packet */
141 OR_LINK, /* relative to the link-layer header */
142 OR_NET, /* relative to the network-layer header */
143 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
144 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
145 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
149 * We divy out chunks of memory rather than call malloc each time so
150 * we don't have to worry about leaking memory. It's probably
151 * not a big deal if all this memory was wasted but if this ever
152 * goes into a library that would probably not be a good idea.
154 * XXX - this *is* in a library....
157 #define CHUNK0SIZE 1024
163 static struct chunk chunks[NCHUNKS];
164 static int cur_chunk;
166 static void *newchunk(u_int);
167 static void freechunks(void);
168 static inline struct block *new_block(int);
169 static inline struct slist *new_stmt(int);
170 static struct block *gen_retblk(int);
171 static inline void syntax(void);
173 static void backpatch(struct block *, struct block *);
174 static void merge(struct block *, struct block *);
175 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
176 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
177 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
178 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
179 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
180 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
182 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
183 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
184 bpf_u_int32, bpf_u_int32, int, bpf_int32);
185 static struct slist *gen_load_llrel(u_int, u_int);
186 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
187 static struct slist *gen_loadx_iphdrlen(void);
188 static struct block *gen_uncond(int);
189 static inline struct block *gen_true(void);
190 static inline struct block *gen_false(void);
191 static struct block *gen_ether_linktype(int);
192 static struct block *gen_linux_sll_linktype(int);
193 static void insert_radiotap_load_llprefixlen(struct block *);
194 static void insert_load_llprefixlen(struct block *);
195 static struct slist *gen_llprefixlen(void);
196 static struct block *gen_linktype(int);
197 static struct block *gen_snap(bpf_u_int32, bpf_u_int32, u_int);
198 static struct block *gen_llc_linktype(int);
199 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
201 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
203 static struct block *gen_ahostop(const u_char *, int);
204 static struct block *gen_ehostop(const u_char *, int);
205 static struct block *gen_fhostop(const u_char *, int);
206 static struct block *gen_thostop(const u_char *, int);
207 static struct block *gen_wlanhostop(const u_char *, int);
208 static struct block *gen_ipfchostop(const u_char *, int);
209 static struct block *gen_dnhostop(bpf_u_int32, int);
210 static struct block *gen_mpls_linktype(int);
211 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int);
213 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int);
216 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
218 static struct block *gen_ipfrag(void);
219 static struct block *gen_portatom(int, bpf_int32);
220 static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
222 static struct block *gen_portatom6(int, bpf_int32);
223 static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
225 struct block *gen_portop(int, int, int);
226 static struct block *gen_port(int, int, int);
227 struct block *gen_portrangeop(int, int, int, int);
228 static struct block *gen_portrange(int, int, int, int);
230 struct block *gen_portop6(int, int, int);
231 static struct block *gen_port6(int, int, int);
232 struct block *gen_portrangeop6(int, int, int, int);
233 static struct block *gen_portrange6(int, int, int, int);
235 static int lookup_proto(const char *, int);
236 static struct block *gen_protochain(int, int, int);
237 static struct block *gen_proto(int, int, int);
238 static struct slist *xfer_to_x(struct arth *);
239 static struct slist *xfer_to_a(struct arth *);
240 static struct block *gen_mac_multicast(int);
241 static struct block *gen_len(int, int);
243 static struct block *gen_msg_abbrev(int type);
254 /* XXX Round up to nearest long. */
255 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
257 /* XXX Round up to structure boundary. */
261 cp = &chunks[cur_chunk];
262 if (n > cp->n_left) {
263 ++cp, k = ++cur_chunk;
265 bpf_error("out of memory");
266 size = CHUNK0SIZE << k;
267 cp->m = (void *)malloc(size);
269 bpf_error("out of memory");
270 memset((char *)cp->m, 0, size);
273 bpf_error("out of memory");
276 return (void *)((char *)cp->m + cp->n_left);
285 for (i = 0; i < NCHUNKS; ++i)
286 if (chunks[i].m != NULL) {
293 * A strdup whose allocations are freed after code generation is over.
297 register const char *s;
299 int n = strlen(s) + 1;
300 char *cp = newchunk(n);
306 static inline struct block *
312 p = (struct block *)newchunk(sizeof(*p));
319 static inline struct slist *
325 p = (struct slist *)newchunk(sizeof(*p));
331 static struct block *
335 struct block *b = new_block(BPF_RET|BPF_K);
344 bpf_error("syntax error in filter expression");
347 static bpf_u_int32 netmask;
352 pcap_compile(pcap_t *p, struct bpf_program *program,
353 char *buf, int optimize, bpf_u_int32 mask)
362 if (setjmp(top_ctx)) {
370 snaplen = pcap_snapshot(p);
372 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
373 "snaplen of 0 rejects all packets");
377 lex_init(buf ? buf : "");
385 root = gen_retblk(snaplen);
387 if (optimize && !no_optimize) {
390 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
391 bpf_error("expression rejects all packets");
393 program->bf_insns = icode_to_fcode(root, &len);
394 program->bf_len = len;
402 * entry point for using the compiler with no pcap open
403 * pass in all the stuff that is needed explicitly instead.
406 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
407 struct bpf_program *program,
408 char *buf, int optimize, bpf_u_int32 mask)
413 p = pcap_open_dead(linktype_arg, snaplen_arg);
416 ret = pcap_compile(p, program, buf, optimize, mask);
422 * Clean up a "struct bpf_program" by freeing all the memory allocated
426 pcap_freecode(struct bpf_program *program)
429 if (program->bf_insns != NULL) {
430 free((char *)program->bf_insns);
431 program->bf_insns = NULL;
436 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
437 * which of the jt and jf fields has been resolved and which is a pointer
438 * back to another unresolved block (or nil). At least one of the fields
439 * in each block is already resolved.
442 backpatch(list, target)
443 struct block *list, *target;
460 * Merge the lists in b0 and b1, using the 'sense' field to indicate
461 * which of jt and jf is the link.
465 struct block *b0, *b1;
467 register struct block **p = &b0;
469 /* Find end of list. */
471 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
473 /* Concatenate the lists. */
481 backpatch(p, gen_retblk(snaplen));
482 p->sense = !p->sense;
483 backpatch(p, gen_retblk(0));
487 * Insert before the statements of the first (root) block any
488 * statements needed to load the lengths of any variable-length
489 * headers into registers.
491 * XXX - a fancier strategy would be to insert those before the
492 * statements of all blocks that use those lengths and that
493 * have no predecessors that use them, so that we only compute
494 * the lengths if we need them. There might be even better
495 * approaches than that. However, as we're currently only
496 * handling variable-length radiotap headers, and as all
497 * filtering expressions other than raw link[M:N] tests
498 * require the length of that header, doing more for that
499 * header length isn't really worth the effort.
501 insert_load_llprefixlen(root);
506 struct block *b0, *b1;
508 backpatch(b0, b1->head);
509 b0->sense = !b0->sense;
510 b1->sense = !b1->sense;
512 b1->sense = !b1->sense;
518 struct block *b0, *b1;
520 b0->sense = !b0->sense;
521 backpatch(b0, b1->head);
522 b0->sense = !b0->sense;
531 b->sense = !b->sense;
534 static struct block *
535 gen_cmp(offrel, offset, size, v)
536 enum e_offrel offrel;
540 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
543 static struct block *
544 gen_cmp_gt(offrel, offset, size, v)
545 enum e_offrel offrel;
549 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
552 static struct block *
553 gen_cmp_ge(offrel, offset, size, v)
554 enum e_offrel offrel;
558 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
561 static struct block *
562 gen_cmp_lt(offrel, offset, size, v)
563 enum e_offrel offrel;
567 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
570 static struct block *
571 gen_cmp_le(offrel, offset, size, v)
572 enum e_offrel offrel;
576 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
579 static struct block *
580 gen_mcmp(offrel, offset, size, v, mask)
581 enum e_offrel offrel;
586 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
589 static struct block *
590 gen_bcmp(offrel, offset, size, v)
591 enum e_offrel offrel;
592 register u_int offset, size;
593 register const u_char *v;
595 register struct block *b, *tmp;
599 register const u_char *p = &v[size - 4];
600 bpf_int32 w = ((bpf_int32)p[0] << 24) |
601 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
603 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
610 register const u_char *p = &v[size - 2];
611 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
613 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
620 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
629 * AND the field of size "size" at offset "offset" relative to the header
630 * specified by "offrel" with "mask", and compare it with the value "v"
631 * with the test specified by "jtype"; if "reverse" is true, the test
632 * should test the opposite of "jtype".
634 static struct block *
635 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
636 enum e_offrel offrel;
638 bpf_u_int32 offset, size, mask, jtype;
641 struct slist *s, *s2;
644 s = gen_load_a(offrel, offset, size);
646 if (mask != 0xffffffff) {
647 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
652 b = new_block(JMP(jtype));
655 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
661 * Various code constructs need to know the layout of the data link
662 * layer. These variables give the necessary offsets from the beginning
663 * of the packet data.
665 * If the link layer has variable_length headers, the offsets are offsets
666 * from the end of the link-link-layer header, and "reg_ll_size" is
667 * the register number for a register containing the length of the
668 * link-layer header. Otherwise, "reg_ll_size" is -1.
670 static int reg_ll_size;
673 * This is the offset of the beginning of the link-layer header.
674 * It's usually 0, except for 802.11 with a fixed-length radio header.
679 * This is the offset of the beginning of the MAC-layer header.
680 * It's usually 0, except for ATM LANE.
682 static u_int off_mac;
685 * "off_linktype" is the offset to information in the link-layer header
686 * giving the packet type.
688 * For Ethernet, it's the offset of the Ethernet type field.
690 * For link-layer types that always use 802.2 headers, it's the
691 * offset of the LLC header.
693 * For PPP, it's the offset of the PPP type field.
695 * For Cisco HDLC, it's the offset of the CHDLC type field.
697 * For BSD loopback, it's the offset of the AF_ value.
699 * For Linux cooked sockets, it's the offset of the type field.
701 * It's set to -1 for no encapsulation, in which case, IP is assumed.
703 static u_int off_linktype;
706 * TRUE if the link layer includes an ATM pseudo-header.
708 static int is_atm = 0;
711 * TRUE if "lane" appeared in the filter; it causes us to generate
712 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
714 static int is_lane = 0;
717 * These are offsets for the ATM pseudo-header.
719 static u_int off_vpi;
720 static u_int off_vci;
721 static u_int off_proto;
724 * These are offsets for the MTP3 fields.
726 static u_int off_sio;
727 static u_int off_opc;
728 static u_int off_dpc;
729 static u_int off_sls;
732 * This is the offset of the first byte after the ATM pseudo_header,
733 * or -1 if there is no ATM pseudo-header.
735 static u_int off_payload;
738 * These are offsets to the beginning of the network-layer header.
740 * If the link layer never uses 802.2 LLC:
742 * "off_nl" and "off_nl_nosnap" are the same.
744 * If the link layer always uses 802.2 LLC:
746 * "off_nl" is the offset if there's a SNAP header following
749 * "off_nl_nosnap" is the offset if there's no SNAP header.
751 * If the link layer is Ethernet:
753 * "off_nl" is the offset if the packet is an Ethernet II packet
754 * (we assume no 802.3+802.2+SNAP);
756 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
757 * with an 802.2 header following it.
760 static u_int off_nl_nosnap;
768 linktype = pcap_datalink(p);
770 pcap_fddipad = p->fddipad;
774 * Assume it's not raw ATM with a pseudo-header, for now.
785 * And assume we're not doing SS7.
793 * Also assume it's not 802.11 with a fixed-length radio header.
799 label_stack_depth = 0;
807 off_nl = 6; /* XXX in reality, variable! */
808 off_nl_nosnap = 6; /* no 802.2 LLC */
811 case DLT_ARCNET_LINUX:
813 off_nl = 8; /* XXX in reality, variable! */
814 off_nl_nosnap = 8; /* no 802.2 LLC */
819 off_nl = 14; /* Ethernet II */
820 off_nl_nosnap = 17; /* 802.3+802.2 */
825 * SLIP doesn't have a link level type. The 16 byte
826 * header is hacked into our SLIP driver.
830 off_nl_nosnap = 16; /* no 802.2 LLC */
834 /* XXX this may be the same as the DLT_PPP_BSDOS case */
838 off_nl_nosnap = 24; /* no 802.2 LLC */
845 off_nl_nosnap = 4; /* no 802.2 LLC */
851 off_nl_nosnap = 12; /* no 802.2 LLC */
856 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
857 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
860 off_nl_nosnap = 4; /* no 802.2 LLC */
865 * This does no include the Ethernet header, and
866 * only covers session state.
870 off_nl_nosnap = 8; /* no 802.2 LLC */
876 off_nl_nosnap = 24; /* no 802.2 LLC */
881 * FDDI doesn't really have a link-level type field.
882 * We set "off_linktype" to the offset of the LLC header.
884 * To check for Ethernet types, we assume that SSAP = SNAP
885 * is being used and pick out the encapsulated Ethernet type.
886 * XXX - should we generate code to check for SNAP?
890 off_linktype += pcap_fddipad;
892 off_nl = 21; /* FDDI+802.2+SNAP */
893 off_nl_nosnap = 16; /* FDDI+802.2 */
895 off_nl += pcap_fddipad;
896 off_nl_nosnap += pcap_fddipad;
902 * Token Ring doesn't really have a link-level type field.
903 * We set "off_linktype" to the offset of the LLC header.
905 * To check for Ethernet types, we assume that SSAP = SNAP
906 * is being used and pick out the encapsulated Ethernet type.
907 * XXX - should we generate code to check for SNAP?
909 * XXX - the header is actually variable-length.
910 * Some various Linux patched versions gave 38
911 * as "off_linktype" and 40 as "off_nl"; however,
912 * if a token ring packet has *no* routing
913 * information, i.e. is not source-routed, the correct
914 * values are 20 and 22, as they are in the vanilla code.
916 * A packet is source-routed iff the uppermost bit
917 * of the first byte of the source address, at an
918 * offset of 8, has the uppermost bit set. If the
919 * packet is source-routed, the total number of bytes
920 * of routing information is 2 plus bits 0x1F00 of
921 * the 16-bit value at an offset of 14 (shifted right
922 * 8 - figure out which byte that is).
925 off_nl = 22; /* Token Ring+802.2+SNAP */
926 off_nl_nosnap = 17; /* Token Ring+802.2 */
931 * 802.11 doesn't really have a link-level type field.
932 * We set "off_linktype" to the offset of the LLC header.
934 * To check for Ethernet types, we assume that SSAP = SNAP
935 * is being used and pick out the encapsulated Ethernet type.
936 * XXX - should we generate code to check for SNAP?
938 * XXX - the header is actually variable-length. We
939 * assume a 24-byte link-layer header, as appears in
940 * data frames in networks with no bridges. If the
941 * fromds and tods 802.11 header bits are both set,
942 * it's actually supposed to be 30 bytes.
945 off_nl = 32; /* 802.11+802.2+SNAP */
946 off_nl_nosnap = 27; /* 802.11+802.2 */
949 case DLT_PRISM_HEADER:
951 * Same as 802.11, but with an additional header before
952 * the 802.11 header, containing a bunch of additional
953 * information including radio-level information.
955 * The header is 144 bytes long.
957 * XXX - same variable-length header problem; at least
958 * the Prism header is fixed-length.
961 off_linktype = 144+24;
962 off_nl = 144+32; /* Prism+802.11+802.2+SNAP */
963 off_nl_nosnap = 144+27; /* Prism+802.11+802.2 */
966 case DLT_IEEE802_11_RADIO_AVS:
968 * Same as 802.11, but with an additional header before
969 * the 802.11 header, containing a bunch of additional
970 * information including radio-level information.
972 * The header is 64 bytes long, at least in its
973 * current incarnation.
975 * XXX - same variable-length header problem, only
976 * more so; this header is also variable-length,
977 * with the length being the 32-bit big-endian
978 * number at an offset of 4 from the beginning
979 * of the radio header.
982 off_linktype = 64+24;
983 off_nl = 64+32; /* Radio+802.11+802.2+SNAP */
984 off_nl_nosnap = 64+27; /* Radio+802.11+802.2 */
987 case DLT_IEEE802_11_RADIO:
989 * Same as 802.11, but with an additional header before
990 * the 802.11 header, containing a bunch of additional
991 * information including radio-level information.
993 * The radiotap header is variable length, and we
994 * generate code to compute its length and store it
995 * in a register. These offsets are relative to the
996 * beginning of the 802.11 header.
999 off_nl = 32; /* 802.11+802.2+SNAP */
1000 off_nl_nosnap = 27; /* 802.11+802.2 */
1003 case DLT_ATM_RFC1483:
1004 case DLT_ATM_CLIP: /* Linux ATM defines this */
1006 * assume routed, non-ISO PDUs
1007 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1009 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1010 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1011 * latter would presumably be treated the way PPPoE
1012 * should be, so you can do "pppoe and udp port 2049"
1013 * or "pppoa and tcp port 80" and have it check for
1014 * PPPo{A,E} and a PPP protocol of IP and....
1017 off_nl = 8; /* 802.2+SNAP */
1018 off_nl_nosnap = 3; /* 802.2 */
1023 * Full Frontal ATM; you get AALn PDUs with an ATM
1027 off_vpi = SUNATM_VPI_POS;
1028 off_vci = SUNATM_VCI_POS;
1029 off_proto = PROTO_POS;
1030 off_mac = -1; /* LLC-encapsulated, so no MAC-layer header */
1031 off_payload = SUNATM_PKT_BEGIN_POS;
1032 off_linktype = off_payload;
1033 off_nl = off_payload+8; /* 802.2+SNAP */
1034 off_nl_nosnap = off_payload+3; /* 802.2 */
1040 off_nl_nosnap = 0; /* no 802.2 LLC */
1043 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1046 off_nl_nosnap = 16; /* no 802.2 LLC */
1051 * LocalTalk does have a 1-byte type field in the LLAP header,
1052 * but really it just indicates whether there is a "short" or
1053 * "long" DDP packet following.
1057 off_nl_nosnap = 0; /* no 802.2 LLC */
1060 case DLT_IP_OVER_FC:
1062 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1063 * link-level type field. We set "off_linktype" to the
1064 * offset of the LLC header.
1066 * To check for Ethernet types, we assume that SSAP = SNAP
1067 * is being used and pick out the encapsulated Ethernet type.
1068 * XXX - should we generate code to check for SNAP? RFC
1069 * 2625 says SNAP should be used.
1072 off_nl = 24; /* IPFC+802.2+SNAP */
1073 off_nl_nosnap = 19; /* IPFC+802.2 */
1078 * XXX - we should set this to handle SNAP-encapsulated
1079 * frames (NLPID of 0x80).
1083 off_nl_nosnap = 0; /* no 802.2 LLC */
1086 case DLT_APPLE_IP_OVER_IEEE1394:
1089 off_nl_nosnap = 18; /* no 802.2 LLC */
1092 case DLT_LINUX_IRDA:
1094 * Currently, only raw "link[N:M]" filtering is supported.
1103 * Currently, only raw "link[N:M]" filtering is supported.
1110 case DLT_SYMANTEC_FIREWALL:
1112 off_nl = 44; /* Ethernet II */
1113 off_nl_nosnap = 44; /* XXX - what does it do with 802.3 packets? */
1118 /* XXX read this from pf.h? */
1119 off_nl = PFLOG_HDRLEN;
1120 off_nl_nosnap = PFLOG_HDRLEN; /* no 802.2 LLC */
1123 case DLT_JUNIPER_MFR:
1124 case DLT_JUNIPER_MLFR:
1125 case DLT_JUNIPER_MLPPP:
1126 case DLT_JUNIPER_PPP:
1127 case DLT_JUNIPER_CHDLC:
1128 case DLT_JUNIPER_FRELAY:
1131 off_nl_nosnap = -1; /* no 802.2 LLC */
1134 case DLT_JUNIPER_ATM1:
1135 off_linktype = 4; /* in reality variable between 4-8 */
1140 case DLT_JUNIPER_ATM2:
1141 off_linktype = 8; /* in reality variable between 8-12 */
1146 /* frames captured on a Juniper PPPoE service PIC
1147 * contain raw ethernet frames */
1148 case DLT_JUNIPER_PPPOE:
1149 case DLT_JUNIPER_ETHER:
1151 off_nl = 18; /* Ethernet II */
1152 off_nl_nosnap = 21; /* 802.3+802.2 */
1155 case DLT_JUNIPER_PPPOE_ATM:
1158 off_nl_nosnap = -1; /* no 802.2 LLC */
1161 case DLT_JUNIPER_GGSN:
1164 off_nl_nosnap = -1; /* no 802.2 LLC */
1167 case DLT_JUNIPER_ES:
1169 off_nl = -1; /* not really a network layer but raw IP adresses */
1170 off_nl_nosnap = -1; /* no 802.2 LLC */
1173 case DLT_JUNIPER_MONITOR:
1175 off_nl = 12; /* raw IP/IP6 header */
1176 off_nl_nosnap = -1; /* no 802.2 LLC */
1179 case DLT_JUNIPER_SERVICES:
1181 off_nl = -1; /* L3 proto location dep. on cookie type */
1182 off_nl_nosnap = -1; /* no 802.2 LLC */
1203 case DLT_LINUX_LAPD:
1205 * Currently, only raw "link[N:M]" filtering is supported.
1212 bpf_error("unknown data link type %d", linktype);
1217 * Load a value relative to the beginning of the link-layer header.
1218 * The link-layer header doesn't necessarily begin at the beginning
1219 * of the packet data; there might be a variable-length prefix containing
1220 * radio information.
1222 static struct slist *
1223 gen_load_llrel(offset, size)
1226 struct slist *s, *s2;
1228 s = gen_llprefixlen();
1231 * If "s" is non-null, it has code to arrange that the X register
1232 * contains the length of the prefix preceding the link-layer
1236 s2 = new_stmt(BPF_LD|BPF_IND|size);
1240 s = new_stmt(BPF_LD|BPF_ABS|size);
1247 * Load a value relative to the beginning of the specified header.
1249 static struct slist *
1250 gen_load_a(offrel, offset, size)
1251 enum e_offrel offrel;
1254 struct slist *s, *s2;
1259 s = gen_load_llrel(offset, size);
1263 s = gen_load_llrel(off_ll + offset, size);
1267 s = gen_load_llrel(off_nl + offset, size);
1271 s = gen_load_llrel(off_nl_nosnap + offset, size);
1276 * Load the X register with the length of the IPv4 header,
1279 s = gen_loadx_iphdrlen();
1282 * Load the item at {length of the link-layer header} +
1283 * {length of the IPv4 header} + {specified offset}.
1285 s2 = new_stmt(BPF_LD|BPF_IND|size);
1286 s2->s.k = off_nl + offset;
1291 s = gen_load_llrel(off_nl + 40 + offset, size);
1302 * Generate code to load into the X register the sum of the length of
1303 * the IPv4 header and any variable-length header preceding the link-layer
1306 static struct slist *
1307 gen_loadx_iphdrlen()
1309 struct slist *s, *s2;
1311 s = gen_llprefixlen();
1314 * There's a variable-length prefix preceding the
1315 * link-layer header. "s" points to a list of statements
1316 * that put the length of that prefix into the X register.
1317 * The 4*([k]&0xf) addressing mode can't be used, as we
1318 * don't have a constant offset, so we have to load the
1319 * value in question into the A register and add to it
1320 * the value from the X register.
1322 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1325 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1328 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1333 * The A register now contains the length of the
1334 * IP header. We need to add to it the length
1335 * of the prefix preceding the link-layer
1336 * header, which is still in the X register, and
1337 * move the result into the X register.
1339 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1340 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1343 * There is no variable-length header preceding the
1344 * link-layer header; if there's a fixed-length
1345 * header preceding it, its length is included in
1346 * the off_ variables, so it doesn't need to be added.
1348 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1354 static struct block *
1361 s = new_stmt(BPF_LD|BPF_IMM);
1363 b = new_block(JMP(BPF_JEQ));
1369 static inline struct block *
1372 return gen_uncond(1);
1375 static inline struct block *
1378 return gen_uncond(0);
1382 * Byte-swap a 32-bit number.
1383 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1384 * big-endian platforms.)
1386 #define SWAPLONG(y) \
1387 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1390 * Generate code to match a particular packet type.
1392 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1393 * value, if <= ETHERMTU. We use that to determine whether to
1394 * match the type/length field or to check the type/length field for
1395 * a value <= ETHERMTU to see whether it's a type field and then do
1396 * the appropriate test.
1398 static struct block *
1399 gen_ether_linktype(proto)
1402 struct block *b0, *b1;
1408 case LLCSAP_NETBEUI:
1410 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1411 * so we check the DSAP and SSAP.
1413 * LLCSAP_IP checks for IP-over-802.2, rather
1414 * than IP-over-Ethernet or IP-over-SNAP.
1416 * XXX - should we check both the DSAP and the
1417 * SSAP, like this, or should we check just the
1418 * DSAP, as we do for other types <= ETHERMTU
1419 * (i.e., other SAP values)?
1421 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1423 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1424 ((proto << 8) | proto));
1432 * Ethernet_II frames, which are Ethernet
1433 * frames with a frame type of ETHERTYPE_IPX;
1435 * Ethernet_802.3 frames, which are 802.3
1436 * frames (i.e., the type/length field is
1437 * a length field, <= ETHERMTU, rather than
1438 * a type field) with the first two bytes
1439 * after the Ethernet/802.3 header being
1442 * Ethernet_802.2 frames, which are 802.3
1443 * frames with an 802.2 LLC header and
1444 * with the IPX LSAP as the DSAP in the LLC
1447 * Ethernet_SNAP frames, which are 802.3
1448 * frames with an LLC header and a SNAP
1449 * header and with an OUI of 0x000000
1450 * (encapsulated Ethernet) and a protocol
1451 * ID of ETHERTYPE_IPX in the SNAP header.
1453 * XXX - should we generate the same code both
1454 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1458 * This generates code to check both for the
1459 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1461 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1462 (bpf_int32)LLCSAP_IPX);
1463 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H,
1468 * Now we add code to check for SNAP frames with
1469 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1471 b0 = gen_snap(0x000000, ETHERTYPE_IPX, 14);
1475 * Now we generate code to check for 802.3
1476 * frames in general.
1478 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1482 * Now add the check for 802.3 frames before the
1483 * check for Ethernet_802.2 and Ethernet_802.3,
1484 * as those checks should only be done on 802.3
1485 * frames, not on Ethernet frames.
1490 * Now add the check for Ethernet_II frames, and
1491 * do that before checking for the other frame
1494 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1495 (bpf_int32)ETHERTYPE_IPX);
1499 case ETHERTYPE_ATALK:
1500 case ETHERTYPE_AARP:
1502 * EtherTalk (AppleTalk protocols on Ethernet link
1503 * layer) may use 802.2 encapsulation.
1507 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1508 * we check for an Ethernet type field less than
1509 * 1500, which means it's an 802.3 length field.
1511 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1515 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1516 * SNAP packets with an organization code of
1517 * 0x080007 (Apple, for Appletalk) and a protocol
1518 * type of ETHERTYPE_ATALK (Appletalk).
1520 * 802.2-encapsulated ETHERTYPE_AARP packets are
1521 * SNAP packets with an organization code of
1522 * 0x000000 (encapsulated Ethernet) and a protocol
1523 * type of ETHERTYPE_AARP (Appletalk ARP).
1525 if (proto == ETHERTYPE_ATALK)
1526 b1 = gen_snap(0x080007, ETHERTYPE_ATALK, 14);
1527 else /* proto == ETHERTYPE_AARP */
1528 b1 = gen_snap(0x000000, ETHERTYPE_AARP, 14);
1532 * Check for Ethernet encapsulation (Ethertalk
1533 * phase 1?); we just check for the Ethernet
1536 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1542 if (proto <= ETHERMTU) {
1544 * This is an LLC SAP value, so the frames
1545 * that match would be 802.2 frames.
1546 * Check that the frame is an 802.2 frame
1547 * (i.e., that the length/type field is
1548 * a length field, <= ETHERMTU) and
1549 * then check the DSAP.
1551 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1553 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1559 * This is an Ethernet type, so compare
1560 * the length/type field with it (if
1561 * the frame is an 802.2 frame, the length
1562 * field will be <= ETHERMTU, and, as
1563 * "proto" is > ETHERMTU, this test
1564 * will fail and the frame won't match,
1565 * which is what we want).
1567 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1574 * Generate code to match a particular packet type.
1576 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1577 * value, if <= ETHERMTU. We use that to determine whether to
1578 * match the type field or to check the type field for the special
1579 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1581 static struct block *
1582 gen_linux_sll_linktype(proto)
1585 struct block *b0, *b1;
1591 case LLCSAP_NETBEUI:
1593 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1594 * so we check the DSAP and SSAP.
1596 * LLCSAP_IP checks for IP-over-802.2, rather
1597 * than IP-over-Ethernet or IP-over-SNAP.
1599 * XXX - should we check both the DSAP and the
1600 * SSAP, like this, or should we check just the
1601 * DSAP, as we do for other types <= ETHERMTU
1602 * (i.e., other SAP values)?
1604 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1605 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1606 ((proto << 8) | proto));
1612 * Ethernet_II frames, which are Ethernet
1613 * frames with a frame type of ETHERTYPE_IPX;
1615 * Ethernet_802.3 frames, which have a frame
1616 * type of LINUX_SLL_P_802_3;
1618 * Ethernet_802.2 frames, which are 802.3
1619 * frames with an 802.2 LLC header (i.e, have
1620 * a frame type of LINUX_SLL_P_802_2) and
1621 * with the IPX LSAP as the DSAP in the LLC
1624 * Ethernet_SNAP frames, which are 802.3
1625 * frames with an LLC header and a SNAP
1626 * header and with an OUI of 0x000000
1627 * (encapsulated Ethernet) and a protocol
1628 * ID of ETHERTYPE_IPX in the SNAP header.
1630 * First, do the checks on LINUX_SLL_P_802_2
1631 * frames; generate the check for either
1632 * Ethernet_802.2 or Ethernet_SNAP frames, and
1633 * then put a check for LINUX_SLL_P_802_2 frames
1636 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1637 (bpf_int32)LLCSAP_IPX);
1638 b1 = gen_snap(0x000000, ETHERTYPE_IPX,
1641 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1645 * Now check for 802.3 frames and OR that with
1646 * the previous test.
1648 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1652 * Now add the check for Ethernet_II frames, and
1653 * do that before checking for the other frame
1656 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1657 (bpf_int32)ETHERTYPE_IPX);
1661 case ETHERTYPE_ATALK:
1662 case ETHERTYPE_AARP:
1664 * EtherTalk (AppleTalk protocols on Ethernet link
1665 * layer) may use 802.2 encapsulation.
1669 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1670 * we check for the 802.2 protocol type in the
1671 * "Ethernet type" field.
1673 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1676 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1677 * SNAP packets with an organization code of
1678 * 0x080007 (Apple, for Appletalk) and a protocol
1679 * type of ETHERTYPE_ATALK (Appletalk).
1681 * 802.2-encapsulated ETHERTYPE_AARP packets are
1682 * SNAP packets with an organization code of
1683 * 0x000000 (encapsulated Ethernet) and a protocol
1684 * type of ETHERTYPE_AARP (Appletalk ARP).
1686 if (proto == ETHERTYPE_ATALK)
1687 b1 = gen_snap(0x080007, ETHERTYPE_ATALK,
1689 else /* proto == ETHERTYPE_AARP */
1690 b1 = gen_snap(0x000000, ETHERTYPE_AARP,
1695 * Check for Ethernet encapsulation (Ethertalk
1696 * phase 1?); we just check for the Ethernet
1699 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1705 if (proto <= ETHERMTU) {
1707 * This is an LLC SAP value, so the frames
1708 * that match would be 802.2 frames.
1709 * Check for the 802.2 protocol type
1710 * in the "Ethernet type" field, and
1711 * then check the DSAP.
1713 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1715 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1721 * This is an Ethernet type, so compare
1722 * the length/type field with it (if
1723 * the frame is an 802.2 frame, the length
1724 * field will be <= ETHERMTU, and, as
1725 * "proto" is > ETHERMTU, this test
1726 * will fail and the frame won't match,
1727 * which is what we want).
1729 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1736 insert_radiotap_load_llprefixlen(b)
1739 struct slist *s1, *s2;
1742 * Prepend to the statements in this block code to load the
1743 * length of the radiotap header into the register assigned
1744 * to hold that length, if one has been assigned.
1746 if (reg_ll_size != -1) {
1748 * The 2 bytes at offsets of 2 and 3 from the beginning
1749 * of the radiotap header are the length of the radiotap
1750 * header; unfortunately, it's little-endian, so we have
1751 * to load it a byte at a time and construct the value.
1755 * Load the high-order byte, at an offset of 3, shift it
1756 * left a byte, and put the result in the X register.
1758 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1760 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1763 s2 = new_stmt(BPF_MISC|BPF_TAX);
1767 * Load the next byte, at an offset of 2, and OR the
1768 * value from the X register into it.
1770 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1773 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1777 * Now allocate a register to hold that value and store
1780 s2 = new_stmt(BPF_ST);
1781 s2->s.k = reg_ll_size;
1785 * Now move it into the X register.
1787 s2 = new_stmt(BPF_MISC|BPF_TAX);
1791 * Now append all the existing statements in this
1792 * block to these statements.
1794 sappend(s1, b->stmts);
1801 insert_load_llprefixlen(b)
1806 case DLT_IEEE802_11_RADIO:
1807 insert_radiotap_load_llprefixlen(b);
1812 static struct slist *
1813 gen_radiotap_llprefixlen(void)
1817 if (reg_ll_size == -1) {
1819 * We haven't yet assigned a register for the length
1820 * of the radiotap header; allocate one.
1822 reg_ll_size = alloc_reg();
1826 * Load the register containing the radiotap length
1827 * into the X register.
1829 s = new_stmt(BPF_LDX|BPF_MEM);
1830 s->s.k = reg_ll_size;
1835 * Generate code to compute the link-layer header length, if necessary,
1836 * putting it into the X register, and to return either a pointer to a
1837 * "struct slist" for the list of statements in that code, or NULL if
1838 * no code is necessary.
1840 static struct slist *
1841 gen_llprefixlen(void)
1845 case DLT_IEEE802_11_RADIO:
1846 return gen_radiotap_llprefixlen();
1854 * Generate code to match a particular packet type by matching the
1855 * link-layer type field or fields in the 802.2 LLC header.
1857 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1858 * value, if <= ETHERMTU.
1860 static struct block *
1864 struct block *b0, *b1, *b2;
1866 /* are we checking MPLS-encapsulated packets? */
1867 if (label_stack_depth > 0) {
1871 /* FIXME add other L3 proto IDs */
1872 return gen_mpls_linktype(Q_IP);
1874 case ETHERTYPE_IPV6:
1876 /* FIXME add other L3 proto IDs */
1877 return gen_mpls_linktype(Q_IPV6);
1880 bpf_error("unsupported protocol over mpls");
1888 return gen_ether_linktype(proto);
1896 proto = (proto << 8 | LLCSAP_ISONS);
1900 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1909 case DLT_IEEE802_11:
1910 case DLT_IEEE802_11_RADIO_AVS:
1911 case DLT_IEEE802_11_RADIO:
1912 case DLT_PRISM_HEADER:
1913 case DLT_ATM_RFC1483:
1915 case DLT_IP_OVER_FC:
1916 return gen_llc_linktype(proto);
1922 * If "is_lane" is set, check for a LANE-encapsulated
1923 * version of this protocol, otherwise check for an
1924 * LLC-encapsulated version of this protocol.
1926 * We assume LANE means Ethernet, not Token Ring.
1930 * Check that the packet doesn't begin with an
1931 * LE Control marker. (We've already generated
1934 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
1939 * Now generate an Ethernet test.
1941 b1 = gen_ether_linktype(proto);
1946 * Check for LLC encapsulation and then check the
1949 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
1950 b1 = gen_llc_linktype(proto);
1958 return gen_linux_sll_linktype(proto);
1963 case DLT_SLIP_BSDOS:
1966 * These types don't provide any type field; packets
1969 * XXX - for IPv4, check for a version number of 4, and,
1970 * for IPv6, check for a version number of 6?
1976 case ETHERTYPE_IPV6:
1978 return gen_true(); /* always true */
1981 return gen_false(); /* always false */
1988 case DLT_PPP_SERIAL:
1991 * We use Ethernet protocol types inside libpcap;
1992 * map them to the corresponding PPP protocol types.
2001 case ETHERTYPE_IPV6:
2010 case ETHERTYPE_ATALK:
2024 * I'm assuming the "Bridging PDU"s that go
2025 * over PPP are Spanning Tree Protocol
2039 * We use Ethernet protocol types inside libpcap;
2040 * map them to the corresponding PPP protocol types.
2045 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
2046 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
2048 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
2053 case ETHERTYPE_IPV6:
2063 case ETHERTYPE_ATALK:
2077 * I'm assuming the "Bridging PDU"s that go
2078 * over PPP are Spanning Tree Protocol
2094 * For DLT_NULL, the link-layer header is a 32-bit
2095 * word containing an AF_ value in *host* byte order,
2096 * and for DLT_ENC, the link-layer header begins
2097 * with a 32-bit work containing an AF_ value in
2100 * In addition, if we're reading a saved capture file,
2101 * the host byte order in the capture may not be the
2102 * same as the host byte order on this machine.
2104 * For DLT_LOOP, the link-layer header is a 32-bit
2105 * word containing an AF_ value in *network* byte order.
2107 * XXX - AF_ values may, unfortunately, be platform-
2108 * dependent; for example, FreeBSD's AF_INET6 is 24
2109 * whilst NetBSD's and OpenBSD's is 26.
2111 * This means that, when reading a capture file, just
2112 * checking for our AF_INET6 value won't work if the
2113 * capture file came from another OS.
2122 case ETHERTYPE_IPV6:
2129 * Not a type on which we support filtering.
2130 * XXX - support those that have AF_ values
2131 * #defined on this platform, at least?
2136 if (linktype == DLT_NULL || linktype == DLT_ENC) {
2138 * The AF_ value is in host byte order, but
2139 * the BPF interpreter will convert it to
2140 * network byte order.
2142 * If this is a save file, and it's from a
2143 * machine with the opposite byte order to
2144 * ours, we byte-swap the AF_ value.
2146 * Then we run it through "htonl()", and
2147 * generate code to compare against the result.
2149 if (bpf_pcap->sf.rfile != NULL &&
2150 bpf_pcap->sf.swapped)
2151 proto = SWAPLONG(proto);
2152 proto = htonl(proto);
2154 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
2158 * af field is host byte order in contrast to the rest of
2161 if (proto == ETHERTYPE_IP)
2162 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2163 BPF_B, (bpf_int32)AF_INET));
2165 else if (proto == ETHERTYPE_IPV6)
2166 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2167 BPF_B, (bpf_int32)AF_INET6));
2175 case DLT_ARCNET_LINUX:
2177 * XXX should we check for first fragment if the protocol
2186 case ETHERTYPE_IPV6:
2187 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2188 (bpf_int32)ARCTYPE_INET6));
2192 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2193 (bpf_int32)ARCTYPE_IP);
2194 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2195 (bpf_int32)ARCTYPE_IP_OLD);
2200 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2201 (bpf_int32)ARCTYPE_ARP);
2202 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2203 (bpf_int32)ARCTYPE_ARP_OLD);
2207 case ETHERTYPE_REVARP:
2208 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2209 (bpf_int32)ARCTYPE_REVARP));
2211 case ETHERTYPE_ATALK:
2212 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2213 (bpf_int32)ARCTYPE_ATALK));
2220 case ETHERTYPE_ATALK:
2230 * XXX - assumes a 2-byte Frame Relay header with
2231 * DLCI and flags. What if the address is longer?
2237 * Check for the special NLPID for IP.
2239 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
2242 case ETHERTYPE_IPV6:
2244 * Check for the special NLPID for IPv6.
2246 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
2251 * Check for several OSI protocols.
2253 * Frame Relay packets typically have an OSI
2254 * NLPID at the beginning; we check for each
2257 * What we check for is the NLPID and a frame
2258 * control field of UI, i.e. 0x03 followed
2261 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
2262 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
2263 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
2274 case DLT_JUNIPER_MFR:
2275 case DLT_JUNIPER_MLFR:
2276 case DLT_JUNIPER_MLPPP:
2277 case DLT_JUNIPER_ATM1:
2278 case DLT_JUNIPER_ATM2:
2279 case DLT_JUNIPER_PPPOE:
2280 case DLT_JUNIPER_PPPOE_ATM:
2281 case DLT_JUNIPER_GGSN:
2282 case DLT_JUNIPER_ES:
2283 case DLT_JUNIPER_MONITOR:
2284 case DLT_JUNIPER_SERVICES:
2285 case DLT_JUNIPER_ETHER:
2286 case DLT_JUNIPER_PPP:
2287 case DLT_JUNIPER_FRELAY:
2288 case DLT_JUNIPER_CHDLC:
2289 /* just lets verify the magic number for now -
2290 * on ATM we may have up to 6 different encapsulations on the wire
2291 * and need a lot of heuristics to figure out that the payload
2294 * FIXME encapsulation specific BPF_ filters
2296 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
2298 case DLT_LINUX_IRDA:
2299 bpf_error("IrDA link-layer type filtering not implemented");
2302 bpf_error("DOCSIS link-layer type filtering not implemented");
2304 case DLT_LINUX_LAPD:
2305 bpf_error("LAPD link-layer type filtering not implemented");
2309 * All the types that have no encapsulation should either be
2310 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2311 * all packets are IP packets, or should be handled in some
2312 * special case, if none of them are (if some are and some
2313 * aren't, the lack of encapsulation is a problem, as we'd
2314 * have to find some other way of determining the packet type).
2316 * Therefore, if "off_linktype" is -1, there's an error.
2318 if (off_linktype == (u_int)-1)
2322 * Any type not handled above should always have an Ethernet
2323 * type at an offset of "off_linktype". (PPP is partially
2324 * handled above - the protocol type is mapped from the
2325 * Ethernet and LLC types we use internally to the corresponding
2326 * PPP type - but the PPP type is always specified by a value
2327 * at "off_linktype", so we don't have to do the code generation
2330 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2334 * Check for an LLC SNAP packet with a given organization code and
2335 * protocol type; we check the entire contents of the 802.2 LLC and
2336 * snap headers, checking for DSAP and SSAP of SNAP and a control
2337 * field of 0x03 in the LLC header, and for the specified organization
2338 * code and protocol type in the SNAP header.
2340 static struct block *
2341 gen_snap(orgcode, ptype, offset)
2342 bpf_u_int32 orgcode;
2346 u_char snapblock[8];
2348 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
2349 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
2350 snapblock[2] = 0x03; /* control = UI */
2351 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
2352 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
2353 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
2354 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
2355 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
2356 return gen_bcmp(OR_LINK, offset, 8, snapblock);
2360 * Generate code to match a particular packet type, for link-layer types
2361 * using 802.2 LLC headers.
2363 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2364 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2366 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2367 * value, if <= ETHERMTU. We use that to determine whether to
2368 * match the DSAP or both DSAP and LSAP or to check the OUI and
2369 * protocol ID in a SNAP header.
2371 static struct block *
2372 gen_llc_linktype(proto)
2376 * XXX - handle token-ring variable-length header.
2382 case LLCSAP_NETBEUI:
2384 * XXX - should we check both the DSAP and the
2385 * SSAP, like this, or should we check just the
2386 * DSAP, as we do for other types <= ETHERMTU
2387 * (i.e., other SAP values)?
2389 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_u_int32)
2390 ((proto << 8) | proto));
2394 * XXX - are there ever SNAP frames for IPX on
2395 * non-Ethernet 802.x networks?
2397 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2398 (bpf_int32)LLCSAP_IPX);
2400 case ETHERTYPE_ATALK:
2402 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2403 * SNAP packets with an organization code of
2404 * 0x080007 (Apple, for Appletalk) and a protocol
2405 * type of ETHERTYPE_ATALK (Appletalk).
2407 * XXX - check for an organization code of
2408 * encapsulated Ethernet as well?
2410 return gen_snap(0x080007, ETHERTYPE_ATALK, off_linktype);
2414 * XXX - we don't have to check for IPX 802.3
2415 * here, but should we check for the IPX Ethertype?
2417 if (proto <= ETHERMTU) {
2419 * This is an LLC SAP value, so check
2422 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2426 * This is an Ethernet type; we assume that it's
2427 * unlikely that it'll appear in the right place
2428 * at random, and therefore check only the
2429 * location that would hold the Ethernet type
2430 * in a SNAP frame with an organization code of
2431 * 0x000000 (encapsulated Ethernet).
2433 * XXX - if we were to check for the SNAP DSAP and
2434 * LSAP, as per XXX, and were also to check for an
2435 * organization code of 0x000000 (encapsulated
2436 * Ethernet), we'd do
2438 * return gen_snap(0x000000, proto,
2441 * here; for now, we don't, as per the above.
2442 * I don't know whether it's worth the extra CPU
2443 * time to do the right check or not.
2445 return gen_cmp(OR_LINK, off_linktype+6, BPF_H,
2451 static struct block *
2452 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
2456 u_int src_off, dst_off;
2458 struct block *b0, *b1;
2472 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2473 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2479 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2480 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2487 b0 = gen_linktype(proto);
2488 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
2494 static struct block *
2495 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
2496 struct in6_addr *addr;
2497 struct in6_addr *mask;
2499 u_int src_off, dst_off;
2501 struct block *b0, *b1;
2516 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2517 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2523 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2524 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2531 /* this order is important */
2532 a = (u_int32_t *)addr;
2533 m = (u_int32_t *)mask;
2534 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
2535 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
2537 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
2539 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
2541 b0 = gen_linktype(proto);
2547 static struct block *
2548 gen_ehostop(eaddr, dir)
2549 register const u_char *eaddr;
2552 register struct block *b0, *b1;
2556 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
2559 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
2562 b0 = gen_ehostop(eaddr, Q_SRC);
2563 b1 = gen_ehostop(eaddr, Q_DST);
2569 b0 = gen_ehostop(eaddr, Q_SRC);
2570 b1 = gen_ehostop(eaddr, Q_DST);
2579 * Like gen_ehostop, but for DLT_FDDI
2581 static struct block *
2582 gen_fhostop(eaddr, dir)
2583 register const u_char *eaddr;
2586 struct block *b0, *b1;
2591 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
2593 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
2598 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
2600 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
2604 b0 = gen_fhostop(eaddr, Q_SRC);
2605 b1 = gen_fhostop(eaddr, Q_DST);
2611 b0 = gen_fhostop(eaddr, Q_SRC);
2612 b1 = gen_fhostop(eaddr, Q_DST);
2621 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2623 static struct block *
2624 gen_thostop(eaddr, dir)
2625 register const u_char *eaddr;
2628 register struct block *b0, *b1;
2632 return gen_bcmp(OR_LINK, 8, 6, eaddr);
2635 return gen_bcmp(OR_LINK, 2, 6, eaddr);
2638 b0 = gen_thostop(eaddr, Q_SRC);
2639 b1 = gen_thostop(eaddr, Q_DST);
2645 b0 = gen_thostop(eaddr, Q_SRC);
2646 b1 = gen_thostop(eaddr, Q_DST);
2655 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2657 static struct block *
2658 gen_wlanhostop(eaddr, dir)
2659 register const u_char *eaddr;
2662 register struct block *b0, *b1, *b2;
2663 register struct slist *s;
2670 * For control frames, there is no SA.
2672 * For management frames, SA is at an
2673 * offset of 10 from the beginning of
2676 * For data frames, SA is at an offset
2677 * of 10 from the beginning of the packet
2678 * if From DS is clear, at an offset of
2679 * 16 from the beginning of the packet
2680 * if From DS is set and To DS is clear,
2681 * and an offset of 24 from the beginning
2682 * of the packet if From DS is set and To DS
2687 * Generate the tests to be done for data frames
2690 * First, check for To DS set, i.e. check "link[1] & 0x01".
2692 s = gen_load_a(OR_LINK, 1, BPF_B);
2693 b1 = new_block(JMP(BPF_JSET));
2694 b1->s.k = 0x01; /* To DS */
2698 * If To DS is set, the SA is at 24.
2700 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
2704 * Now, check for To DS not set, i.e. check
2705 * "!(link[1] & 0x01)".
2707 s = gen_load_a(OR_LINK, 1, BPF_B);
2708 b2 = new_block(JMP(BPF_JSET));
2709 b2->s.k = 0x01; /* To DS */
2714 * If To DS is not set, the SA is at 16.
2716 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
2720 * Now OR together the last two checks. That gives
2721 * the complete set of checks for data frames with
2727 * Now check for From DS being set, and AND that with
2728 * the ORed-together checks.
2730 s = gen_load_a(OR_LINK, 1, BPF_B);
2731 b1 = new_block(JMP(BPF_JSET));
2732 b1->s.k = 0x02; /* From DS */
2737 * Now check for data frames with From DS not set.
2739 s = gen_load_a(OR_LINK, 1, BPF_B);
2740 b2 = new_block(JMP(BPF_JSET));
2741 b2->s.k = 0x02; /* From DS */
2746 * If From DS isn't set, the SA is at 10.
2748 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
2752 * Now OR together the checks for data frames with
2753 * From DS not set and for data frames with From DS
2754 * set; that gives the checks done for data frames.
2759 * Now check for a data frame.
2760 * I.e, check "link[0] & 0x08".
2762 gen_load_a(OR_LINK, 0, BPF_B);
2763 b1 = new_block(JMP(BPF_JSET));
2768 * AND that with the checks done for data frames.
2773 * If the high-order bit of the type value is 0, this
2774 * is a management frame.
2775 * I.e, check "!(link[0] & 0x08)".
2777 s = gen_load_a(OR_LINK, 0, BPF_B);
2778 b2 = new_block(JMP(BPF_JSET));
2784 * For management frames, the SA is at 10.
2786 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
2790 * OR that with the checks done for data frames.
2791 * That gives the checks done for management and
2797 * If the low-order bit of the type value is 1,
2798 * this is either a control frame or a frame
2799 * with a reserved type, and thus not a
2802 * I.e., check "!(link[0] & 0x04)".
2804 s = gen_load_a(OR_LINK, 0, BPF_B);
2805 b1 = new_block(JMP(BPF_JSET));
2811 * AND that with the checks for data and management
2821 * For control frames, there is no DA.
2823 * For management frames, DA is at an
2824 * offset of 4 from the beginning of
2827 * For data frames, DA is at an offset
2828 * of 4 from the beginning of the packet
2829 * if To DS is clear and at an offset of
2830 * 16 from the beginning of the packet
2835 * Generate the tests to be done for data frames.
2837 * First, check for To DS set, i.e. "link[1] & 0x01".
2839 s = gen_load_a(OR_LINK, 1, BPF_B);
2840 b1 = new_block(JMP(BPF_JSET));
2841 b1->s.k = 0x01; /* To DS */
2845 * If To DS is set, the DA is at 16.
2847 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
2851 * Now, check for To DS not set, i.e. check
2852 * "!(link[1] & 0x01)".
2854 s = gen_load_a(OR_LINK, 1, BPF_B);
2855 b2 = new_block(JMP(BPF_JSET));
2856 b2->s.k = 0x01; /* To DS */
2861 * If To DS is not set, the DA is at 4.
2863 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
2867 * Now OR together the last two checks. That gives
2868 * the complete set of checks for data frames.
2873 * Now check for a data frame.
2874 * I.e, check "link[0] & 0x08".
2876 s = gen_load_a(OR_LINK, 0, BPF_B);
2877 b1 = new_block(JMP(BPF_JSET));
2882 * AND that with the checks done for data frames.
2887 * If the high-order bit of the type value is 0, this
2888 * is a management frame.
2889 * I.e, check "!(link[0] & 0x08)".
2891 s = gen_load_a(OR_LINK, 0, BPF_B);
2892 b2 = new_block(JMP(BPF_JSET));
2898 * For management frames, the DA is at 4.
2900 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
2904 * OR that with the checks done for data frames.
2905 * That gives the checks done for management and
2911 * If the low-order bit of the type value is 1,
2912 * this is either a control frame or a frame
2913 * with a reserved type, and thus not a
2916 * I.e., check "!(link[0] & 0x04)".
2918 s = gen_load_a(OR_LINK, 0, BPF_B);
2919 b1 = new_block(JMP(BPF_JSET));
2925 * AND that with the checks for data and management
2932 b0 = gen_wlanhostop(eaddr, Q_SRC);
2933 b1 = gen_wlanhostop(eaddr, Q_DST);
2939 b0 = gen_wlanhostop(eaddr, Q_SRC);
2940 b1 = gen_wlanhostop(eaddr, Q_DST);
2949 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2950 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2951 * as the RFC states.)
2953 static struct block *
2954 gen_ipfchostop(eaddr, dir)
2955 register const u_char *eaddr;
2958 register struct block *b0, *b1;
2962 return gen_bcmp(OR_LINK, 10, 6, eaddr);
2965 return gen_bcmp(OR_LINK, 2, 6, eaddr);
2968 b0 = gen_ipfchostop(eaddr, Q_SRC);
2969 b1 = gen_ipfchostop(eaddr, Q_DST);
2975 b0 = gen_ipfchostop(eaddr, Q_SRC);
2976 b1 = gen_ipfchostop(eaddr, Q_DST);
2985 * This is quite tricky because there may be pad bytes in front of the
2986 * DECNET header, and then there are two possible data packet formats that
2987 * carry both src and dst addresses, plus 5 packet types in a format that
2988 * carries only the src node, plus 2 types that use a different format and
2989 * also carry just the src node.
2993 * Instead of doing those all right, we just look for data packets with
2994 * 0 or 1 bytes of padding. If you want to look at other packets, that
2995 * will require a lot more hacking.
2997 * To add support for filtering on DECNET "areas" (network numbers)
2998 * one would want to add a "mask" argument to this routine. That would
2999 * make the filter even more inefficient, although one could be clever
3000 * and not generate masking instructions if the mask is 0xFFFF.
3002 static struct block *
3003 gen_dnhostop(addr, dir)
3007 struct block *b0, *b1, *b2, *tmp;
3008 u_int offset_lh; /* offset if long header is received */
3009 u_int offset_sh; /* offset if short header is received */
3014 offset_sh = 1; /* follows flags */
3015 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
3019 offset_sh = 3; /* follows flags, dstnode */
3020 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3024 /* Inefficient because we do our Calvinball dance twice */
3025 b0 = gen_dnhostop(addr, Q_SRC);
3026 b1 = gen_dnhostop(addr, Q_DST);
3032 /* Inefficient because we do our Calvinball dance twice */
3033 b0 = gen_dnhostop(addr, Q_SRC);
3034 b1 = gen_dnhostop(addr, Q_DST);
3039 bpf_error("ISO host filtering not implemented");
3044 b0 = gen_linktype(ETHERTYPE_DN);
3045 /* Check for pad = 1, long header case */
3046 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3047 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
3048 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
3049 BPF_H, (bpf_int32)ntohs(addr));
3051 /* Check for pad = 0, long header case */
3052 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
3053 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs(addr));
3056 /* Check for pad = 1, short header case */
3057 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3058 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
3059 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
3062 /* Check for pad = 0, short header case */
3063 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
3064 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
3068 /* Combine with test for linktype */
3074 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3075 * test the bottom-of-stack bit, and then check the version number
3076 * field in the IP header.
3078 static struct block *
3079 gen_mpls_linktype(proto)
3082 struct block *b0, *b1;
3087 /* match the bottom-of-stack bit */
3088 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3089 /* match the IPv4 version number */
3090 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
3095 /* match the bottom-of-stack bit */
3096 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
3097 /* match the IPv4 version number */
3098 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
3107 static struct block *
3108 gen_host(addr, mask, proto, dir)
3114 struct block *b0, *b1;
3119 b0 = gen_host(addr, mask, Q_IP, dir);
3121 * Only check for non-IPv4 addresses if we're not
3122 * checking MPLS-encapsulated packets.
3124 if (label_stack_depth == 0) {
3125 b1 = gen_host(addr, mask, Q_ARP, dir);
3127 b0 = gen_host(addr, mask, Q_RARP, dir);
3133 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
3136 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
3139 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
3142 bpf_error("'tcp' modifier applied to host");
3145 bpf_error("'sctp' modifier applied to host");
3148 bpf_error("'udp' modifier applied to host");
3151 bpf_error("'icmp' modifier applied to host");
3154 bpf_error("'igmp' modifier applied to host");
3157 bpf_error("'igrp' modifier applied to host");
3160 bpf_error("'pim' modifier applied to host");
3163 bpf_error("'vrrp' modifier applied to host");
3166 bpf_error("ATALK host filtering not implemented");
3169 bpf_error("AARP host filtering not implemented");
3172 return gen_dnhostop(addr, dir);
3175 bpf_error("SCA host filtering not implemented");
3178 bpf_error("LAT host filtering not implemented");
3181 bpf_error("MOPDL host filtering not implemented");
3184 bpf_error("MOPRC host filtering not implemented");
3188 bpf_error("'ip6' modifier applied to ip host");
3191 bpf_error("'icmp6' modifier applied to host");
3195 bpf_error("'ah' modifier applied to host");
3198 bpf_error("'esp' modifier applied to host");
3201 bpf_error("ISO host filtering not implemented");
3204 bpf_error("'esis' modifier applied to host");
3207 bpf_error("'isis' modifier applied to host");
3210 bpf_error("'clnp' modifier applied to host");
3213 bpf_error("'stp' modifier applied to host");
3216 bpf_error("IPX host filtering not implemented");
3219 bpf_error("'netbeui' modifier applied to host");
3222 bpf_error("'radio' modifier applied to host");
3231 static struct block *
3232 gen_host6(addr, mask, proto, dir)
3233 struct in6_addr *addr;
3234 struct in6_addr *mask;
3241 return gen_host6(addr, mask, Q_IPV6, dir);
3244 bpf_error("'ip' modifier applied to ip6 host");
3247 bpf_error("'rarp' modifier applied to ip6 host");
3250 bpf_error("'arp' modifier applied to ip6 host");
3253 bpf_error("'sctp' modifier applied to host");
3256 bpf_error("'tcp' modifier applied to host");
3259 bpf_error("'udp' modifier applied to host");
3262 bpf_error("'icmp' modifier applied to host");
3265 bpf_error("'igmp' modifier applied to host");
3268 bpf_error("'igrp' modifier applied to host");
3271 bpf_error("'pim' modifier applied to host");
3274 bpf_error("'vrrp' modifier applied to host");
3277 bpf_error("ATALK host filtering not implemented");
3280 bpf_error("AARP host filtering not implemented");
3283 bpf_error("'decnet' modifier applied to ip6 host");
3286 bpf_error("SCA host filtering not implemented");
3289 bpf_error("LAT host filtering not implemented");
3292 bpf_error("MOPDL host filtering not implemented");
3295 bpf_error("MOPRC host filtering not implemented");
3298 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
3301 bpf_error("'icmp6' modifier applied to host");
3304 bpf_error("'ah' modifier applied to host");
3307 bpf_error("'esp' modifier applied to host");
3310 bpf_error("ISO host filtering not implemented");
3313 bpf_error("'esis' modifier applied to host");
3316 bpf_error("'isis' modifier applied to host");
3319 bpf_error("'clnp' modifier applied to host");
3322 bpf_error("'stp' modifier applied to host");
3325 bpf_error("IPX host filtering not implemented");
3328 bpf_error("'netbeui' modifier applied to host");
3331 bpf_error("'radio' modifier applied to host");
3341 static struct block *
3342 gen_gateway(eaddr, alist, proto, dir)
3343 const u_char *eaddr;
3344 bpf_u_int32 **alist;
3348 struct block *b0, *b1, *tmp;
3351 bpf_error("direction applied to 'gateway'");
3358 if (linktype == DLT_EN10MB)
3359 b0 = gen_ehostop(eaddr, Q_OR);
3360 else if (linktype == DLT_FDDI)
3361 b0 = gen_fhostop(eaddr, Q_OR);
3362 else if (linktype == DLT_IEEE802)
3363 b0 = gen_thostop(eaddr, Q_OR);
3364 else if (linktype == DLT_IEEE802_11 ||
3365 linktype == DLT_IEEE802_11_RADIO_AVS ||
3366 linktype == DLT_IEEE802_11_RADIO ||
3367 linktype == DLT_PRISM_HEADER)
3368 b0 = gen_wlanhostop(eaddr, Q_OR);
3369 else if (linktype == DLT_SUNATM && is_lane) {
3371 * Check that the packet doesn't begin with an
3372 * LE Control marker. (We've already generated
3375 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3380 * Now check the MAC address.
3382 b0 = gen_ehostop(eaddr, Q_OR);
3384 } else if (linktype == DLT_IP_OVER_FC)
3385 b0 = gen_ipfchostop(eaddr, Q_OR);
3388 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3390 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR);
3392 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR);
3400 bpf_error("illegal modifier of 'gateway'");
3406 gen_proto_abbrev(proto)
3415 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
3417 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
3423 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
3425 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
3431 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
3433 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
3439 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
3442 #ifndef IPPROTO_IGMP
3443 #define IPPROTO_IGMP 2
3447 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
3450 #ifndef IPPROTO_IGRP
3451 #define IPPROTO_IGRP 9
3454 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
3458 #define IPPROTO_PIM 103
3462 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
3464 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
3469 #ifndef IPPROTO_VRRP
3470 #define IPPROTO_VRRP 112
3474 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
3478 b1 = gen_linktype(ETHERTYPE_IP);
3482 b1 = gen_linktype(ETHERTYPE_ARP);
3486 b1 = gen_linktype(ETHERTYPE_REVARP);
3490 bpf_error("link layer applied in wrong context");
3493 b1 = gen_linktype(ETHERTYPE_ATALK);
3497 b1 = gen_linktype(ETHERTYPE_AARP);
3501 b1 = gen_linktype(ETHERTYPE_DN);
3505 b1 = gen_linktype(ETHERTYPE_SCA);
3509 b1 = gen_linktype(ETHERTYPE_LAT);
3513 b1 = gen_linktype(ETHERTYPE_MOPDL);
3517 b1 = gen_linktype(ETHERTYPE_MOPRC);
3522 b1 = gen_linktype(ETHERTYPE_IPV6);
3525 #ifndef IPPROTO_ICMPV6
3526 #define IPPROTO_ICMPV6 58
3529 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
3534 #define IPPROTO_AH 51
3537 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
3539 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
3545 #define IPPROTO_ESP 50
3548 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
3550 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
3556 b1 = gen_linktype(LLCSAP_ISONS);
3560 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
3564 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
3567 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
3568 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3569 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3571 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3573 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3575 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3579 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
3580 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3581 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3583 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3585 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3587 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3591 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
3592 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3593 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3595 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
3600 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3601 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3606 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3607 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3609 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3611 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3616 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3617 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3622 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3623 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3628 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
3632 b1 = gen_linktype(LLCSAP_8021D);
3636 b1 = gen_linktype(LLCSAP_IPX);
3640 b1 = gen_linktype(LLCSAP_NETBEUI);
3644 bpf_error("'radio' is not a valid protocol type");
3652 static struct block *
3659 s = gen_load_a(OR_NET, 6, BPF_H);
3660 b = new_block(JMP(BPF_JSET));
3669 * Generate a comparison to a port value in the transport-layer header
3670 * at the specified offset from the beginning of that header.
3672 * XXX - this handles a variable-length prefix preceding the link-layer
3673 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3674 * variable-length link-layer headers (such as Token Ring or 802.11
3677 static struct block *
3678 gen_portatom(off, v)
3682 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
3686 static struct block *
3687 gen_portatom6(off, v)
3691 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
3696 gen_portop(port, proto, dir)
3697 int port, proto, dir;
3699 struct block *b0, *b1, *tmp;
3701 /* ip proto 'proto' */
3702 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
3708 b1 = gen_portatom(0, (bpf_int32)port);
3712 b1 = gen_portatom(2, (bpf_int32)port);
3717 tmp = gen_portatom(0, (bpf_int32)port);
3718 b1 = gen_portatom(2, (bpf_int32)port);
3723 tmp = gen_portatom(0, (bpf_int32)port);
3724 b1 = gen_portatom(2, (bpf_int32)port);
3736 static struct block *
3737 gen_port(port, ip_proto, dir)
3742 struct block *b0, *b1, *tmp;
3747 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3748 * not LLC encapsulation with LLCSAP_IP.
3750 * For IEEE 802 networks - which includes 802.5 token ring
3751 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3752 * says that SNAP encapsulation is used, not LLC encapsulation
3755 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3756 * RFC 2225 say that SNAP encapsulation is used, not LLC
3757 * encapsulation with LLCSAP_IP.
3759 * So we always check for ETHERTYPE_IP.
3761 b0 = gen_linktype(ETHERTYPE_IP);
3767 b1 = gen_portop(port, ip_proto, dir);
3771 tmp = gen_portop(port, IPPROTO_TCP, dir);
3772 b1 = gen_portop(port, IPPROTO_UDP, dir);
3774 tmp = gen_portop(port, IPPROTO_SCTP, dir);
3787 gen_portop6(port, proto, dir)
3788 int port, proto, dir;
3790 struct block *b0, *b1, *tmp;
3792 /* ip6 proto 'proto' */
3793 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
3797 b1 = gen_portatom6(0, (bpf_int32)port);
3801 b1 = gen_portatom6(2, (bpf_int32)port);
3806 tmp = gen_portatom6(0, (bpf_int32)port);
3807 b1 = gen_portatom6(2, (bpf_int32)port);
3812 tmp = gen_portatom6(0, (bpf_int32)port);
3813 b1 = gen_portatom6(2, (bpf_int32)port);
3825 static struct block *
3826 gen_port6(port, ip_proto, dir)
3831 struct block *b0, *b1, *tmp;
3833 /* link proto ip6 */
3834 b0 = gen_linktype(ETHERTYPE_IPV6);
3840 b1 = gen_portop6(port, ip_proto, dir);
3844 tmp = gen_portop6(port, IPPROTO_TCP, dir);
3845 b1 = gen_portop6(port, IPPROTO_UDP, dir);
3847 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
3859 /* gen_portrange code */
3860 static struct block *
3861 gen_portrangeatom(off, v1, v2)
3865 struct block *b1, *b2;
3869 * Reverse the order of the ports, so v1 is the lower one.
3878 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
3879 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
3887 gen_portrangeop(port1, port2, proto, dir)
3892 struct block *b0, *b1, *tmp;
3894 /* ip proto 'proto' */
3895 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
3901 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3905 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
3910 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3911 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
3916 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3917 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
3929 static struct block *
3930 gen_portrange(port1, port2, ip_proto, dir)
3935 struct block *b0, *b1, *tmp;
3938 b0 = gen_linktype(ETHERTYPE_IP);
3944 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
3948 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
3949 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
3951 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
3963 static struct block *
3964 gen_portrangeatom6(off, v1, v2)
3968 struct block *b1, *b2;
3972 * Reverse the order of the ports, so v1 is the lower one.
3981 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
3982 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
3990 gen_portrangeop6(port1, port2, proto, dir)
3995 struct block *b0, *b1, *tmp;
3997 /* ip6 proto 'proto' */
3998 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
4002 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4006 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4011 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4012 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4017 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
4018 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
4030 static struct block *
4031 gen_portrange6(port1, port2, ip_proto, dir)
4036 struct block *b0, *b1, *tmp;
4038 /* link proto ip6 */
4039 b0 = gen_linktype(ETHERTYPE_IPV6);
4045 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
4049 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
4050 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
4052 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
4065 lookup_proto(name, proto)
4066 register const char *name;
4076 v = pcap_nametoproto(name);
4077 if (v == PROTO_UNDEF)
4078 bpf_error("unknown ip proto '%s'", name);
4082 /* XXX should look up h/w protocol type based on linktype */
4083 v = pcap_nametoeproto(name);
4084 if (v == PROTO_UNDEF) {
4085 v = pcap_nametollc(name);
4086 if (v == PROTO_UNDEF)
4087 bpf_error("unknown ether proto '%s'", name);
4092 if (strcmp(name, "esis") == 0)
4094 else if (strcmp(name, "isis") == 0)
4096 else if (strcmp(name, "clnp") == 0)
4099 bpf_error("unknown osi proto '%s'", name);
4119 static struct block *
4120 gen_protochain(v, proto, dir)
4125 #ifdef NO_PROTOCHAIN
4126 return gen_proto(v, proto, dir);
4128 struct block *b0, *b;
4129 struct slist *s[100];
4130 int fix2, fix3, fix4, fix5;
4131 int ahcheck, again, end;
4133 int reg2 = alloc_reg();
4135 memset(s, 0, sizeof(s));
4136 fix2 = fix3 = fix4 = fix5 = 0;
4143 b0 = gen_protochain(v, Q_IP, dir);
4144 b = gen_protochain(v, Q_IPV6, dir);
4148 bpf_error("bad protocol applied for 'protochain'");
4153 * We don't handle variable-length radiotap here headers yet.
4154 * We might want to add BPF instructions to do the protochain
4155 * work, to simplify that and, on platforms that have a BPF
4156 * interpreter with the new instructions, let the filtering
4157 * be done in the kernel. (We already require a modified BPF
4158 * engine to do the protochain stuff, to support backward
4159 * branches, and backward branch support is unlikely to appear
4160 * in kernel BPF engines.)
4162 if (linktype == DLT_IEEE802_11_RADIO)
4163 bpf_error("'protochain' not supported with radiotap headers");
4165 no_optimize = 1; /*this code is not compatible with optimzer yet */
4168 * s[0] is a dummy entry to protect other BPF insn from damage
4169 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4170 * hard to find interdependency made by jump table fixup.
4173 s[i] = new_stmt(0); /*dummy*/
4178 b0 = gen_linktype(ETHERTYPE_IP);
4181 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4182 s[i]->s.k = off_nl + 9;
4184 /* X = ip->ip_hl << 2 */
4185 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
4191 b0 = gen_linktype(ETHERTYPE_IPV6);
4193 /* A = ip6->ip_nxt */
4194 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4195 s[i]->s.k = off_nl + 6;
4197 /* X = sizeof(struct ip6_hdr) */
4198 s[i] = new_stmt(BPF_LDX|BPF_IMM);
4204 bpf_error("unsupported proto to gen_protochain");
4208 /* again: if (A == v) goto end; else fall through; */
4210 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4212 s[i]->s.jt = NULL; /*later*/
4213 s[i]->s.jf = NULL; /*update in next stmt*/
4217 #ifndef IPPROTO_NONE
4218 #define IPPROTO_NONE 59
4220 /* if (A == IPPROTO_NONE) goto end */
4221 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4222 s[i]->s.jt = NULL; /*later*/
4223 s[i]->s.jf = NULL; /*update in next stmt*/
4224 s[i]->s.k = IPPROTO_NONE;
4225 s[fix5]->s.jf = s[i];
4230 if (proto == Q_IPV6) {
4231 int v6start, v6end, v6advance, j;
4234 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4235 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4236 s[i]->s.jt = NULL; /*later*/
4237 s[i]->s.jf = NULL; /*update in next stmt*/
4238 s[i]->s.k = IPPROTO_HOPOPTS;
4239 s[fix2]->s.jf = s[i];
4241 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4242 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4243 s[i]->s.jt = NULL; /*later*/
4244 s[i]->s.jf = NULL; /*update in next stmt*/
4245 s[i]->s.k = IPPROTO_DSTOPTS;
4247 /* if (A == IPPROTO_ROUTING) goto v6advance */
4248 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4249 s[i]->s.jt = NULL; /*later*/
4250 s[i]->s.jf = NULL; /*update in next stmt*/
4251 s[i]->s.k = IPPROTO_ROUTING;
4253 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4254 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4255 s[i]->s.jt = NULL; /*later*/
4256 s[i]->s.jf = NULL; /*later*/
4257 s[i]->s.k = IPPROTO_FRAGMENT;
4268 * X = X + (P[X + 1] + 1) * 8;
4271 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4273 /* A = P[X + packet head] */
4274 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4278 s[i] = new_stmt(BPF_ST);
4282 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4285 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4289 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4291 /* A = P[X + packet head]; */
4292 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4296 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4300 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4304 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4307 s[i] = new_stmt(BPF_LD|BPF_MEM);
4311 /* goto again; (must use BPF_JA for backward jump) */
4312 s[i] = new_stmt(BPF_JMP|BPF_JA);
4313 s[i]->s.k = again - i - 1;
4314 s[i - 1]->s.jf = s[i];
4318 for (j = v6start; j <= v6end; j++)
4319 s[j]->s.jt = s[v6advance];
4324 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4326 s[fix2]->s.jf = s[i];
4332 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4333 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4334 s[i]->s.jt = NULL; /*later*/
4335 s[i]->s.jf = NULL; /*later*/
4336 s[i]->s.k = IPPROTO_AH;
4338 s[fix3]->s.jf = s[ahcheck];
4345 * X = X + (P[X + 1] + 2) * 4;
4348 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4350 /* A = P[X + packet head]; */
4351 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4355 s[i] = new_stmt(BPF_ST);
4359 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4362 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4366 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4368 /* A = P[X + packet head] */
4369 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4373 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4377 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4381 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4384 s[i] = new_stmt(BPF_LD|BPF_MEM);
4388 /* goto again; (must use BPF_JA for backward jump) */
4389 s[i] = new_stmt(BPF_JMP|BPF_JA);
4390 s[i]->s.k = again - i - 1;
4395 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4397 s[fix2]->s.jt = s[end];
4398 s[fix4]->s.jf = s[end];
4399 s[fix5]->s.jt = s[end];
4406 for (i = 0; i < max - 1; i++)
4407 s[i]->next = s[i + 1];
4408 s[max - 1]->next = NULL;
4413 b = new_block(JMP(BPF_JEQ));
4414 b->stmts = s[1]; /*remember, s[0] is dummy*/
4425 * Generate code that checks whether the packet is a packet for protocol
4426 * <proto> and whether the type field in that protocol's header has
4427 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4428 * IP packet and checks the protocol number in the IP header against <v>.
4430 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4431 * against Q_IP and Q_IPV6.
4433 static struct block *
4434 gen_proto(v, proto, dir)
4439 struct block *b0, *b1;
4441 if (dir != Q_DEFAULT)
4442 bpf_error("direction applied to 'proto'");
4447 b0 = gen_proto(v, Q_IP, dir);
4448 b1 = gen_proto(v, Q_IPV6, dir);
4456 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4457 * not LLC encapsulation with LLCSAP_IP.
4459 * For IEEE 802 networks - which includes 802.5 token ring
4460 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4461 * says that SNAP encapsulation is used, not LLC encapsulation
4464 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4465 * RFC 2225 say that SNAP encapsulation is used, not LLC
4466 * encapsulation with LLCSAP_IP.
4468 * So we always check for ETHERTYPE_IP.
4471 b0 = gen_linktype(ETHERTYPE_IP);
4473 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
4475 b1 = gen_protochain(v, Q_IP);
4485 * Frame Relay packets typically have an OSI
4486 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4487 * generates code to check for all the OSI
4488 * NLPIDs, so calling it and then adding a check
4489 * for the particular NLPID for which we're
4490 * looking is bogus, as we can just check for
4493 * What we check for is the NLPID and a frame
4494 * control field value of UI, i.e. 0x03 followed
4497 * XXX - assumes a 2-byte Frame Relay header with
4498 * DLCI and flags. What if the address is longer?
4500 * XXX - what about SNAP-encapsulated frames?
4502 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
4508 * Cisco uses an Ethertype lookalike - for OSI,
4511 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
4512 /* OSI in C-HDLC is stuffed with a fudge byte */
4513 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
4518 b0 = gen_linktype(LLCSAP_ISONS);
4519 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
4525 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4527 * 4 is the offset of the PDU type relative to the IS-IS
4530 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
4535 bpf_error("arp does not encapsulate another protocol");
4539 bpf_error("rarp does not encapsulate another protocol");
4543 bpf_error("atalk encapsulation is not specifiable");
4547 bpf_error("decnet encapsulation is not specifiable");
4551 bpf_error("sca does not encapsulate another protocol");
4555 bpf_error("lat does not encapsulate another protocol");
4559 bpf_error("moprc does not encapsulate another protocol");
4563 bpf_error("mopdl does not encapsulate another protocol");
4567 return gen_linktype(v);
4570 bpf_error("'udp proto' is bogus");
4574 bpf_error("'tcp proto' is bogus");
4578 bpf_error("'sctp proto' is bogus");
4582 bpf_error("'icmp proto' is bogus");
4586 bpf_error("'igmp proto' is bogus");
4590 bpf_error("'igrp proto' is bogus");
4594 bpf_error("'pim proto' is bogus");
4598 bpf_error("'vrrp proto' is bogus");
4603 b0 = gen_linktype(ETHERTYPE_IPV6);
4605 b1 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
4607 b1 = gen_protochain(v, Q_IPV6);
4613 bpf_error("'icmp6 proto' is bogus");
4617 bpf_error("'ah proto' is bogus");
4620 bpf_error("'ah proto' is bogus");
4623 bpf_error("'stp proto' is bogus");
4626 bpf_error("'ipx proto' is bogus");
4629 bpf_error("'netbeui proto' is bogus");
4632 bpf_error("'radio proto' is bogus");
4643 register const char *name;
4646 int proto = q.proto;
4650 bpf_u_int32 mask, addr;
4652 bpf_u_int32 **alist;
4655 struct sockaddr_in *sin;
4656 struct sockaddr_in6 *sin6;
4657 struct addrinfo *res, *res0;
4658 struct in6_addr mask128;
4660 struct block *b, *tmp;
4661 int port, real_proto;
4667 addr = pcap_nametonetaddr(name);
4669 bpf_error("unknown network '%s'", name);
4670 /* Left justify network addr and calculate its network mask */
4672 while (addr && (addr & 0xff000000) == 0) {
4676 return gen_host(addr, mask, proto, dir);
4680 if (proto == Q_LINK) {
4684 eaddr = pcap_ether_hostton(name);
4687 "unknown ether host '%s'", name);
4688 b = gen_ehostop(eaddr, dir);
4693 eaddr = pcap_ether_hostton(name);
4696 "unknown FDDI host '%s'", name);
4697 b = gen_fhostop(eaddr, dir);
4702 eaddr = pcap_ether_hostton(name);
4705 "unknown token ring host '%s'", name);
4706 b = gen_thostop(eaddr, dir);
4710 case DLT_IEEE802_11:
4711 case DLT_IEEE802_11_RADIO_AVS:
4712 case DLT_IEEE802_11_RADIO:
4713 case DLT_PRISM_HEADER:
4714 eaddr = pcap_ether_hostton(name);
4717 "unknown 802.11 host '%s'", name);
4718 b = gen_wlanhostop(eaddr, dir);
4722 case DLT_IP_OVER_FC:
4723 eaddr = pcap_ether_hostton(name);
4726 "unknown Fibre Channel host '%s'", name);
4727 b = gen_ipfchostop(eaddr, dir);
4736 * Check that the packet doesn't begin
4737 * with an LE Control marker. (We've
4738 * already generated a test for LANE.)
4740 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
4744 eaddr = pcap_ether_hostton(name);
4747 "unknown ether host '%s'", name);
4748 b = gen_ehostop(eaddr, dir);
4754 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4755 } else if (proto == Q_DECNET) {
4756 unsigned short dn_addr = __pcap_nametodnaddr(name);
4758 * I don't think DECNET hosts can be multihomed, so
4759 * there is no need to build up a list of addresses
4761 return (gen_host(dn_addr, 0, proto, dir));
4764 alist = pcap_nametoaddr(name);
4765 if (alist == NULL || *alist == NULL)
4766 bpf_error("unknown host '%s'", name);
4768 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
4770 b = gen_host(**alist++, 0xffffffff, tproto, dir);
4772 tmp = gen_host(**alist++, 0xffffffff,
4779 memset(&mask128, 0xff, sizeof(mask128));
4780 res0 = res = pcap_nametoaddrinfo(name);
4782 bpf_error("unknown host '%s'", name);
4784 tproto = tproto6 = proto;
4785 if (off_linktype == -1 && tproto == Q_DEFAULT) {
4789 for (res = res0; res; res = res->ai_next) {
4790 switch (res->ai_family) {
4792 if (tproto == Q_IPV6)
4795 sin = (struct sockaddr_in *)
4797 tmp = gen_host(ntohl(sin->sin_addr.s_addr),
4798 0xffffffff, tproto, dir);
4801 if (tproto6 == Q_IP)
4804 sin6 = (struct sockaddr_in6 *)
4806 tmp = gen_host6(&sin6->sin6_addr,
4807 &mask128, tproto6, dir);
4818 bpf_error("unknown host '%s'%s", name,
4819 (proto == Q_DEFAULT)
4821 : " for specified address family");
4828 if (proto != Q_DEFAULT &&
4829 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
4830 bpf_error("illegal qualifier of 'port'");
4831 if (pcap_nametoport(name, &port, &real_proto) == 0)
4832 bpf_error("unknown port '%s'", name);
4833 if (proto == Q_UDP) {
4834 if (real_proto == IPPROTO_TCP)
4835 bpf_error("port '%s' is tcp", name);
4836 else if (real_proto == IPPROTO_SCTP)
4837 bpf_error("port '%s' is sctp", name);
4839 /* override PROTO_UNDEF */
4840 real_proto = IPPROTO_UDP;
4842 if (proto == Q_TCP) {
4843 if (real_proto == IPPROTO_UDP)
4844 bpf_error("port '%s' is udp", name);
4846 else if (real_proto == IPPROTO_SCTP)
4847 bpf_error("port '%s' is sctp", name);
4849 /* override PROTO_UNDEF */
4850 real_proto = IPPROTO_TCP;
4852 if (proto == Q_SCTP) {
4853 if (real_proto == IPPROTO_UDP)
4854 bpf_error("port '%s' is udp", name);
4856 else if (real_proto == IPPROTO_TCP)
4857 bpf_error("port '%s' is tcp", name);
4859 /* override PROTO_UNDEF */
4860 real_proto = IPPROTO_SCTP;
4863 return gen_port(port, real_proto, dir);
4867 b = gen_port(port, real_proto, dir);
4868 gen_or(gen_port6(port, real_proto, dir), b);
4874 if (proto != Q_DEFAULT &&
4875 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
4876 bpf_error("illegal qualifier of 'portrange'");
4877 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
4878 bpf_error("unknown port in range '%s'", name);
4879 if (proto == Q_UDP) {
4880 if (real_proto == IPPROTO_TCP)
4881 bpf_error("port in range '%s' is tcp", name);
4882 else if (real_proto == IPPROTO_SCTP)
4883 bpf_error("port in range '%s' is sctp", name);
4885 /* override PROTO_UNDEF */
4886 real_proto = IPPROTO_UDP;
4888 if (proto == Q_TCP) {
4889 if (real_proto == IPPROTO_UDP)
4890 bpf_error("port in range '%s' is udp", name);
4891 else if (real_proto == IPPROTO_SCTP)
4892 bpf_error("port in range '%s' is sctp", name);
4894 /* override PROTO_UNDEF */
4895 real_proto = IPPROTO_TCP;
4897 if (proto == Q_SCTP) {
4898 if (real_proto == IPPROTO_UDP)
4899 bpf_error("port in range '%s' is udp", name);
4900 else if (real_proto == IPPROTO_TCP)
4901 bpf_error("port in range '%s' is tcp", name);
4903 /* override PROTO_UNDEF */
4904 real_proto = IPPROTO_SCTP;
4907 return gen_portrange(port1, port2, real_proto, dir);
4911 b = gen_portrange(port1, port2, real_proto, dir);
4912 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
4919 eaddr = pcap_ether_hostton(name);
4921 bpf_error("unknown ether host: %s", name);
4923 alist = pcap_nametoaddr(name);
4924 if (alist == NULL || *alist == NULL)
4925 bpf_error("unknown host '%s'", name);
4926 b = gen_gateway(eaddr, alist, proto, dir);
4930 bpf_error("'gateway' not supported in this configuration");
4934 real_proto = lookup_proto(name, proto);
4935 if (real_proto >= 0)
4936 return gen_proto(real_proto, proto, dir);
4938 bpf_error("unknown protocol: %s", name);
4941 real_proto = lookup_proto(name, proto);
4942 if (real_proto >= 0)
4943 return gen_protochain(real_proto, proto, dir);
4945 bpf_error("unknown protocol: %s", name);
4957 gen_mcode(s1, s2, masklen, q)
4958 register const char *s1, *s2;
4959 register int masklen;
4962 register int nlen, mlen;
4965 nlen = __pcap_atoin(s1, &n);
4966 /* Promote short ipaddr */
4970 mlen = __pcap_atoin(s2, &m);
4971 /* Promote short ipaddr */
4974 bpf_error("non-network bits set in \"%s mask %s\"",
4977 /* Convert mask len to mask */
4979 bpf_error("mask length must be <= 32");
4980 m = 0xffffffff << (32 - masklen);
4982 bpf_error("non-network bits set in \"%s/%d\"",
4989 return gen_host(n, m, q.proto, q.dir);
4992 bpf_error("Mask syntax for networks only");
5000 register const char *s;
5005 int proto = q.proto;
5011 else if (q.proto == Q_DECNET)
5012 vlen = __pcap_atodn(s, &v);
5014 vlen = __pcap_atoin(s, &v);
5021 if (proto == Q_DECNET)
5022 return gen_host(v, 0, proto, dir);
5023 else if (proto == Q_LINK) {
5024 bpf_error("illegal link layer address");
5027 if (s == NULL && q.addr == Q_NET) {
5028 /* Promote short net number */
5029 while (v && (v & 0xff000000) == 0) {
5034 /* Promote short ipaddr */
5038 return gen_host(v, mask, proto, dir);
5043 proto = IPPROTO_UDP;
5044 else if (proto == Q_TCP)
5045 proto = IPPROTO_TCP;
5046 else if (proto == Q_SCTP)
5047 proto = IPPROTO_SCTP;
5048 else if (proto == Q_DEFAULT)
5049 proto = PROTO_UNDEF;
5051 bpf_error("illegal qualifier of 'port'");
5054 return gen_port((int)v, proto, dir);
5058 b = gen_port((int)v, proto, dir);
5059 gen_or(gen_port6((int)v, proto, dir), b);
5066 proto = IPPROTO_UDP;
5067 else if (proto == Q_TCP)
5068 proto = IPPROTO_TCP;
5069 else if (proto == Q_SCTP)
5070 proto = IPPROTO_SCTP;
5071 else if (proto == Q_DEFAULT)
5072 proto = PROTO_UNDEF;
5074 bpf_error("illegal qualifier of 'portrange'");
5077 return gen_portrange((int)v, (int)v, proto, dir);
5081 b = gen_portrange((int)v, (int)v, proto, dir);
5082 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
5088 bpf_error("'gateway' requires a name");
5092 return gen_proto((int)v, proto, dir);
5095 return gen_protochain((int)v, proto, dir);
5110 gen_mcode6(s1, s2, masklen, q)
5111 register const char *s1, *s2;
5112 register int masklen;
5115 struct addrinfo *res;
5116 struct in6_addr *addr;
5117 struct in6_addr mask;
5122 bpf_error("no mask %s supported", s2);
5124 res = pcap_nametoaddrinfo(s1);
5126 bpf_error("invalid ip6 address %s", s1);
5128 bpf_error("%s resolved to multiple address", s1);
5129 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
5131 if (sizeof(mask) * 8 < masklen)
5132 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
5133 memset(&mask, 0, sizeof(mask));
5134 memset(&mask, 0xff, masklen / 8);
5136 mask.s6_addr[masklen / 8] =
5137 (0xff << (8 - masklen % 8)) & 0xff;
5140 a = (u_int32_t *)addr;
5141 m = (u_int32_t *)&mask;
5142 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
5143 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
5144 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
5152 bpf_error("Mask syntax for networks only");
5156 b = gen_host6(addr, &mask, q.proto, q.dir);
5161 bpf_error("invalid qualifier against IPv6 address");
5169 register const u_char *eaddr;
5172 struct block *b, *tmp;
5174 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
5175 if (linktype == DLT_EN10MB)
5176 return gen_ehostop(eaddr, (int)q.dir);
5177 if (linktype == DLT_FDDI)
5178 return gen_fhostop(eaddr, (int)q.dir);
5179 if (linktype == DLT_IEEE802)
5180 return gen_thostop(eaddr, (int)q.dir);
5181 if (linktype == DLT_IEEE802_11 ||
5182 linktype == DLT_IEEE802_11_RADIO_AVS ||
5183 linktype == DLT_IEEE802_11_RADIO ||
5184 linktype == DLT_PRISM_HEADER)
5185 return gen_wlanhostop(eaddr, (int)q.dir);
5186 if (linktype == DLT_SUNATM && is_lane) {
5188 * Check that the packet doesn't begin with an
5189 * LE Control marker. (We've already generated
5192 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5197 * Now check the MAC address.
5199 b = gen_ehostop(eaddr, (int)q.dir);
5203 if (linktype == DLT_IP_OVER_FC)
5204 return gen_ipfchostop(eaddr, (int)q.dir);
5205 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5207 bpf_error("ethernet address used in non-ether expression");
5213 struct slist *s0, *s1;
5216 * This is definitely not the best way to do this, but the
5217 * lists will rarely get long.
5224 static struct slist *
5230 s = new_stmt(BPF_LDX|BPF_MEM);
5235 static struct slist *
5241 s = new_stmt(BPF_LD|BPF_MEM);
5247 * Modify "index" to use the value stored into its register as an
5248 * offset relative to the beginning of the header for the protocol
5249 * "proto", and allocate a register and put an item "size" bytes long
5250 * (1, 2, or 4) at that offset into that register, making it the register
5254 gen_load(proto, index, size)
5259 struct slist *s, *tmp;
5261 int regno = alloc_reg();
5263 free_reg(index->regno);
5267 bpf_error("data size must be 1, 2, or 4");
5283 bpf_error("unsupported index operation");
5287 * The offset is relative to the beginning of the packet
5288 * data, if we have a radio header. (If we don't, this
5291 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
5292 linktype != DLT_IEEE802_11_RADIO &&
5293 linktype != DLT_PRISM_HEADER)
5294 bpf_error("radio information not present in capture");
5297 * Load into the X register the offset computed into the
5298 * register specifed by "index".
5300 s = xfer_to_x(index);
5303 * Load the item at that offset.
5305 tmp = new_stmt(BPF_LD|BPF_IND|size);
5307 sappend(index->s, s);
5312 * The offset is relative to the beginning of
5313 * the link-layer header.
5315 * XXX - what about ATM LANE? Should the index be
5316 * relative to the beginning of the AAL5 frame, so
5317 * that 0 refers to the beginning of the LE Control
5318 * field, or relative to the beginning of the LAN
5319 * frame, so that 0 refers, for Ethernet LANE, to
5320 * the beginning of the destination address?
5322 s = gen_llprefixlen();
5325 * If "s" is non-null, it has code to arrange that the
5326 * X register contains the length of the prefix preceding
5327 * the link-layer header. Add to it the offset computed
5328 * into the register specified by "index", and move that
5329 * into the X register. Otherwise, just load into the X
5330 * register the offset computed into the register specifed
5334 sappend(s, xfer_to_a(index));
5335 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5336 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5338 s = xfer_to_x(index);
5341 * Load the item at the sum of the offset we've put in the
5342 * X register and the offset of the start of the link
5343 * layer header (which is 0 if the radio header is
5344 * variable-length; that header length is what we put
5345 * into the X register and then added to the index).
5347 tmp = new_stmt(BPF_LD|BPF_IND|size);
5350 sappend(index->s, s);
5366 * The offset is relative to the beginning of
5367 * the network-layer header.
5368 * XXX - are there any cases where we want
5371 s = gen_llprefixlen();
5374 * If "s" is non-null, it has code to arrange that the
5375 * X register contains the length of the prefix preceding
5376 * the link-layer header. Add to it the offset computed
5377 * into the register specified by "index", and move that
5378 * into the X register. Otherwise, just load into the X
5379 * register the offset computed into the register specifed
5383 sappend(s, xfer_to_a(index));
5384 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5385 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5387 s = xfer_to_x(index);
5390 * Load the item at the sum of the offset we've put in the
5391 * X register and the offset of the start of the network
5394 tmp = new_stmt(BPF_LD|BPF_IND|size);
5397 sappend(index->s, s);
5400 * Do the computation only if the packet contains
5401 * the protocol in question.
5403 b = gen_proto_abbrev(proto);
5405 gen_and(index->b, b);
5418 * The offset is relative to the beginning of
5419 * the transport-layer header.
5420 * XXX - are there any cases where we want
5422 * XXX - we should, if we're built with
5423 * IPv6 support, generate code to load either
5424 * IPv4, IPv6, or both, as appropriate.
5426 s = gen_loadx_iphdrlen();
5429 * The X register now contains the sum of the offset
5430 * of the beginning of the link-layer header and
5431 * the length of the network-layer header. Load
5432 * into the A register the offset relative to
5433 * the beginning of the transport layer header,
5434 * add the X register to that, move that to the
5435 * X register, and load with an offset from the
5436 * X register equal to the offset of the network
5437 * layer header relative to the beginning of
5438 * the link-layer header.
5440 sappend(s, xfer_to_a(index));
5441 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5442 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5443 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
5445 sappend(index->s, s);
5448 * Do the computation only if the packet contains
5449 * the protocol in question - which is true only
5450 * if this is an IP datagram and is the first or
5451 * only fragment of that datagram.
5453 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
5455 gen_and(index->b, b);
5457 gen_and(gen_proto_abbrev(Q_IP), b);
5463 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5467 index->regno = regno;
5468 s = new_stmt(BPF_ST);
5470 sappend(index->s, s);
5476 gen_relation(code, a0, a1, reversed)
5478 struct arth *a0, *a1;
5481 struct slist *s0, *s1, *s2;
5482 struct block *b, *tmp;
5486 if (code == BPF_JEQ) {
5487 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
5488 b = new_block(JMP(code));
5492 b = new_block(BPF_JMP|code|BPF_X);
5498 sappend(a0->s, a1->s);
5502 free_reg(a0->regno);
5503 free_reg(a1->regno);
5505 /* 'and' together protocol checks */
5508 gen_and(a0->b, tmp = a1->b);
5524 int regno = alloc_reg();
5525 struct arth *a = (struct arth *)newchunk(sizeof(*a));
5528 s = new_stmt(BPF_LD|BPF_LEN);
5529 s->next = new_stmt(BPF_ST);
5530 s->next->s.k = regno;
5545 a = (struct arth *)newchunk(sizeof(*a));
5549 s = new_stmt(BPF_LD|BPF_IMM);
5551 s->next = new_stmt(BPF_ST);
5567 s = new_stmt(BPF_ALU|BPF_NEG);
5570 s = new_stmt(BPF_ST);
5578 gen_arth(code, a0, a1)
5580 struct arth *a0, *a1;
5582 struct slist *s0, *s1, *s2;
5586 s2 = new_stmt(BPF_ALU|BPF_X|code);
5591 sappend(a0->s, a1->s);
5593 free_reg(a0->regno);
5594 free_reg(a1->regno);
5596 s0 = new_stmt(BPF_ST);
5597 a0->regno = s0->s.k = alloc_reg();
5604 * Here we handle simple allocation of the scratch registers.
5605 * If too many registers are alloc'd, the allocator punts.
5607 static int regused[BPF_MEMWORDS];
5611 * Return the next free register.
5616 int n = BPF_MEMWORDS;
5619 if (regused[curreg])
5620 curreg = (curreg + 1) % BPF_MEMWORDS;
5622 regused[curreg] = 1;
5626 bpf_error("too many registers needed to evaluate expression");
5631 * Return a register to the table so it can
5641 static struct block *
5648 s = new_stmt(BPF_LD|BPF_LEN);
5649 b = new_block(JMP(jmp));
5660 return gen_len(BPF_JGE, n);
5664 * Actually, this is less than or equal.
5672 b = gen_len(BPF_JGT, n);
5679 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5680 * the beginning of the link-layer header.
5681 * XXX - that means you can't test values in the radiotap header, but
5682 * as that header is difficult if not impossible to parse generally
5683 * without a loop, that might not be a severe problem. A new keyword
5684 * "radio" could be added for that, although what you'd really want
5685 * would be a way of testing particular radio header values, which
5686 * would generate code appropriate to the radio header in question.
5689 gen_byteop(op, idx, val)
5700 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5703 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5707 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5711 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
5715 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
5719 b = new_block(JMP(BPF_JEQ));
5726 static u_char abroadcast[] = { 0x0 };
5729 gen_broadcast(proto)
5732 bpf_u_int32 hostmask;
5733 struct block *b0, *b1, *b2;
5734 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5740 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
5741 return gen_ahostop(abroadcast, Q_DST);
5742 if (linktype == DLT_EN10MB)
5743 return gen_ehostop(ebroadcast, Q_DST);
5744 if (linktype == DLT_FDDI)
5745 return gen_fhostop(ebroadcast, Q_DST);
5746 if (linktype == DLT_IEEE802)
5747 return gen_thostop(ebroadcast, Q_DST);
5748 if (linktype == DLT_IEEE802_11 ||
5749 linktype == DLT_IEEE802_11_RADIO_AVS ||
5750 linktype == DLT_IEEE802_11_RADIO ||
5751 linktype == DLT_PRISM_HEADER)
5752 return gen_wlanhostop(ebroadcast, Q_DST);
5753 if (linktype == DLT_IP_OVER_FC)
5754 return gen_ipfchostop(ebroadcast, Q_DST);
5755 if (linktype == DLT_SUNATM && is_lane) {
5757 * Check that the packet doesn't begin with an
5758 * LE Control marker. (We've already generated
5761 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5766 * Now check the MAC address.
5768 b0 = gen_ehostop(ebroadcast, Q_DST);
5772 bpf_error("not a broadcast link");
5776 b0 = gen_linktype(ETHERTYPE_IP);
5777 hostmask = ~netmask;
5778 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
5779 b2 = gen_mcmp(OR_NET, 16, BPF_W,
5780 (bpf_int32)(~0 & hostmask), hostmask);
5785 bpf_error("only link-layer/IP broadcast filters supported");
5790 * Generate code to test the low-order bit of a MAC address (that's
5791 * the bottom bit of the *first* byte).
5793 static struct block *
5794 gen_mac_multicast(offset)
5797 register struct block *b0;
5798 register struct slist *s;
5800 /* link[offset] & 1 != 0 */
5801 s = gen_load_a(OR_LINK, offset, BPF_B);
5802 b0 = new_block(JMP(BPF_JSET));
5809 gen_multicast(proto)
5812 register struct block *b0, *b1, *b2;
5813 register struct slist *s;
5819 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
5820 /* all ARCnet multicasts use the same address */
5821 return gen_ahostop(abroadcast, Q_DST);
5823 if (linktype == DLT_EN10MB) {
5824 /* ether[0] & 1 != 0 */
5825 return gen_mac_multicast(0);
5828 if (linktype == DLT_FDDI) {
5830 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5832 * XXX - was that referring to bit-order issues?
5834 /* fddi[1] & 1 != 0 */
5835 return gen_mac_multicast(1);
5838 if (linktype == DLT_IEEE802) {
5839 /* tr[2] & 1 != 0 */
5840 return gen_mac_multicast(2);
5843 if (linktype == DLT_IEEE802_11 ||
5844 linktype == DLT_IEEE802_11_RADIO_AVS ||
5845 linktype == DLT_IEEE802_11_RADIO ||
5846 linktype == DLT_PRISM_HEADER) {
5850 * For control frames, there is no DA.
5852 * For management frames, DA is at an
5853 * offset of 4 from the beginning of
5856 * For data frames, DA is at an offset
5857 * of 4 from the beginning of the packet
5858 * if To DS is clear and at an offset of
5859 * 16 from the beginning of the packet
5864 * Generate the tests to be done for data frames.
5866 * First, check for To DS set, i.e. "link[1] & 0x01".
5868 s = gen_load_a(OR_LINK, 1, BPF_B);
5869 b1 = new_block(JMP(BPF_JSET));
5870 b1->s.k = 0x01; /* To DS */
5874 * If To DS is set, the DA is at 16.
5876 b0 = gen_mac_multicast(16);
5880 * Now, check for To DS not set, i.e. check
5881 * "!(link[1] & 0x01)".
5883 s = gen_load_a(OR_LINK, 1, BPF_B);
5884 b2 = new_block(JMP(BPF_JSET));
5885 b2->s.k = 0x01; /* To DS */
5890 * If To DS is not set, the DA is at 4.
5892 b1 = gen_mac_multicast(4);
5896 * Now OR together the last two checks. That gives
5897 * the complete set of checks for data frames.
5902 * Now check for a data frame.
5903 * I.e, check "link[0] & 0x08".
5905 s = gen_load_a(OR_LINK, 0, BPF_B);
5906 b1 = new_block(JMP(BPF_JSET));
5911 * AND that with the checks done for data frames.
5916 * If the high-order bit of the type value is 0, this
5917 * is a management frame.
5918 * I.e, check "!(link[0] & 0x08)".
5920 s = gen_load_a(OR_LINK, 0, BPF_B);
5921 b2 = new_block(JMP(BPF_JSET));
5927 * For management frames, the DA is at 4.
5929 b1 = gen_mac_multicast(4);
5933 * OR that with the checks done for data frames.
5934 * That gives the checks done for management and
5940 * If the low-order bit of the type value is 1,
5941 * this is either a control frame or a frame
5942 * with a reserved type, and thus not a
5945 * I.e., check "!(link[0] & 0x04)".
5947 s = gen_load_a(OR_LINK, 0, BPF_B);
5948 b1 = new_block(JMP(BPF_JSET));
5954 * AND that with the checks for data and management
5961 if (linktype == DLT_IP_OVER_FC) {
5962 b0 = gen_mac_multicast(2);
5966 if (linktype == DLT_SUNATM && is_lane) {
5968 * Check that the packet doesn't begin with an
5969 * LE Control marker. (We've already generated
5972 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5976 /* ether[off_mac] & 1 != 0 */
5977 b0 = gen_mac_multicast(off_mac);
5982 /* Link not known to support multicasts */
5986 b0 = gen_linktype(ETHERTYPE_IP);
5987 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
5993 b0 = gen_linktype(ETHERTYPE_IPV6);
5994 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
5999 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6004 * generate command for inbound/outbound. It's here so we can
6005 * make it link-type specific. 'dir' = 0 implies "inbound",
6006 * = 1 implies "outbound".
6012 register struct block *b0;
6015 * Only some data link types support inbound/outbound qualifiers.
6019 b0 = gen_relation(BPF_JEQ,
6020 gen_load(Q_LINK, gen_loadi(0), 1),
6028 * Match packets sent by this machine.
6030 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
6033 * Match packets sent to this machine.
6034 * (No broadcast or multicast packets, or
6035 * packets sent to some other machine and
6036 * received promiscuously.)
6038 * XXX - packets sent to other machines probably
6039 * shouldn't be matched, but what about broadcast
6040 * or multicast packets we received?
6042 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_HOST);
6047 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
6048 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
6053 /* match outgoing packets */
6054 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
6056 /* match incoming packets */
6057 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
6061 case DLT_JUNIPER_MFR:
6062 case DLT_JUNIPER_MLFR:
6063 case DLT_JUNIPER_MLPPP:
6064 case DLT_JUNIPER_ATM1:
6065 case DLT_JUNIPER_ATM2:
6066 case DLT_JUNIPER_PPPOE:
6067 case DLT_JUNIPER_PPPOE_ATM:
6068 case DLT_JUNIPER_GGSN:
6069 case DLT_JUNIPER_ES:
6070 case DLT_JUNIPER_MONITOR:
6071 case DLT_JUNIPER_SERVICES:
6072 case DLT_JUNIPER_ETHER:
6073 case DLT_JUNIPER_PPP:
6074 case DLT_JUNIPER_FRELAY:
6075 case DLT_JUNIPER_CHDLC:
6076 /* juniper flags (including direction) are stored
6077 * the byte after the 3-byte magic number */
6079 /* match outgoing packets */
6080 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
6082 /* match incoming packets */
6083 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
6088 bpf_error("inbound/outbound not supported on linktype %d",
6096 /* PF firewall log matched interface */
6098 gen_pf_ifname(const char *ifname)
6103 if (linktype == DLT_PFLOG) {
6104 len = sizeof(((struct pfloghdr *)0)->ifname);
6105 off = offsetof(struct pfloghdr, ifname);
6107 bpf_error("ifname not supported on linktype 0x%x", linktype);
6110 if (strlen(ifname) >= len) {
6111 bpf_error("ifname interface names can only be %d characters",
6115 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
6119 /* PF firewall log ruleset name */
6121 gen_pf_ruleset(char *ruleset)
6125 if (linktype != DLT_PFLOG) {
6126 bpf_error("ruleset not supported on linktype 0x%x", linktype);
6129 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
6130 bpf_error("ruleset names can only be %ld characters",
6131 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
6134 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
6135 strlen(ruleset), (const u_char *)ruleset);
6139 /* PF firewall log rule number */
6145 if (linktype == DLT_PFLOG) {
6146 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
6149 bpf_error("rnr not supported on linktype 0x%x", linktype);
6156 /* PF firewall log sub-rule number */
6158 gen_pf_srnr(int srnr)
6162 if (linktype != DLT_PFLOG) {
6163 bpf_error("srnr not supported on linktype 0x%x", linktype);
6167 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
6172 /* PF firewall log reason code */
6174 gen_pf_reason(int reason)
6178 if (linktype == DLT_PFLOG) {
6179 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
6182 bpf_error("reason not supported on linktype 0x%x", linktype);
6189 /* PF firewall log action */
6191 gen_pf_action(int action)
6195 if (linktype == DLT_PFLOG) {
6196 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
6199 bpf_error("action not supported on linktype 0x%x", linktype);
6208 register const u_char *eaddr;
6211 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6212 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
6213 return gen_ahostop(eaddr, (int)q.dir);
6215 bpf_error("ARCnet address used in non-arc expression");
6219 static struct block *
6220 gen_ahostop(eaddr, dir)
6221 register const u_char *eaddr;
6224 register struct block *b0, *b1;
6227 /* src comes first, different from Ethernet */
6229 return gen_bcmp(OR_LINK, 0, 1, eaddr);
6232 return gen_bcmp(OR_LINK, 1, 1, eaddr);
6235 b0 = gen_ahostop(eaddr, Q_SRC);
6236 b1 = gen_ahostop(eaddr, Q_DST);
6242 b0 = gen_ahostop(eaddr, Q_SRC);
6243 b1 = gen_ahostop(eaddr, Q_DST);
6252 * support IEEE 802.1Q VLAN trunk over ethernet
6258 struct block *b0, *b1;
6260 /* can't check for VLAN-encapsulated packets inside MPLS */
6261 if (label_stack_depth > 0)
6262 bpf_error("no VLAN match after MPLS");
6265 * Change the offsets to point to the type and data fields within
6266 * the VLAN packet. Just increment the offsets, so that we
6267 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6268 * capture VLAN 200 encapsulated within VLAN 100.
6270 * XXX - this is a bit of a kludge. If we were to split the
6271 * compiler into a parser that parses an expression and
6272 * generates an expression tree, and a code generator that
6273 * takes an expression tree (which could come from our
6274 * parser or from some other parser) and generates BPF code,
6275 * we could perhaps make the offsets parameters of routines
6276 * and, in the handler for an "AND" node, pass to subnodes
6277 * other than the VLAN node the adjusted offsets.
6279 * This would mean that "vlan" would, instead of changing the
6280 * behavior of *all* tests after it, change only the behavior
6281 * of tests ANDed with it. That would change the documented
6282 * semantics of "vlan", which might break some expressions.
6283 * However, it would mean that "(vlan and ip) or ip" would check
6284 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6285 * checking only for VLAN-encapsulated IP, so that could still
6286 * be considered worth doing; it wouldn't break expressions
6287 * that are of the form "vlan and ..." or "vlan N and ...",
6288 * which I suspect are the most common expressions involving
6289 * "vlan". "vlan or ..." doesn't necessarily do what the user
6290 * would really want, now, as all the "or ..." tests would
6291 * be done assuming a VLAN, even though the "or" could be viewed
6292 * as meaning "or, if this isn't a VLAN packet...".
6294 orig_linktype = off_linktype; /* save original values */
6306 bpf_error("no VLAN support for data link type %d",
6311 /* check for VLAN */
6312 b0 = gen_cmp(OR_LINK, orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q);
6314 /* If a specific VLAN is requested, check VLAN id */
6315 if (vlan_num >= 0) {
6316 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_H, (bpf_int32)vlan_num,
6332 struct block *b0,*b1;
6335 * Change the offsets to point to the type and data fields within
6336 * the MPLS packet. Just increment the offsets, so that we
6337 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6338 * capture packets with an outer label of 100000 and an inner
6341 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6345 if (label_stack_depth > 0) {
6346 /* just match the bottom-of-stack bit clear */
6347 b0 = gen_mcmp(OR_LINK, orig_nl-2, BPF_B, 0, 0x01);
6350 * Indicate that we're checking MPLS-encapsulated headers,
6351 * to make sure higher level code generators don't try to
6352 * match against IP-related protocols such as Q_ARP, Q_RARP
6357 case DLT_C_HDLC: /* fall through */
6359 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
6360 (bpf_int32)ETHERTYPE_MPLS);
6364 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
6365 (bpf_int32)PPP_MPLS_UCAST);
6368 /* FIXME add other DLT_s ...
6369 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6370 * leave it for now */
6373 bpf_error("no MPLS support for data link type %d",
6381 /* If a specific MPLS label is requested, check it */
6382 if (label_num >= 0) {
6383 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
6384 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_W, (bpf_int32)label_num,
6385 0xfffff000); /* only compare the first 20 bits */
6392 label_stack_depth++;
6397 * Support PPPOE discovery and session.
6402 /* check for PPPoE discovery */
6403 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
6412 * Test against the PPPoE session link-layer type.
6414 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
6417 * Change the offsets to point to the type and data fields within
6420 * XXX - this is a bit of a kludge. If we were to split the
6421 * compiler into a parser that parses an expression and
6422 * generates an expression tree, and a code generator that
6423 * takes an expression tree (which could come from our
6424 * parser or from some other parser) and generates BPF code,
6425 * we could perhaps make the offsets parameters of routines
6426 * and, in the handler for an "AND" node, pass to subnodes
6427 * other than the PPPoE node the adjusted offsets.
6429 * This would mean that "pppoes" would, instead of changing the
6430 * behavior of *all* tests after it, change only the behavior
6431 * of tests ANDed with it. That would change the documented
6432 * semantics of "pppoes", which might break some expressions.
6433 * However, it would mean that "(pppoes and ip) or ip" would check
6434 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6435 * checking only for VLAN-encapsulated IP, so that could still
6436 * be considered worth doing; it wouldn't break expressions
6437 * that are of the form "pppoes and ..." which I suspect are the
6438 * most common expressions involving "pppoes". "pppoes or ..."
6439 * doesn't necessarily do what the user would really want, now,
6440 * as all the "or ..." tests would be done assuming PPPoE, even
6441 * though the "or" could be viewed as meaning "or, if this isn't
6442 * a PPPoE packet...".
6444 orig_linktype = off_linktype; /* save original values */
6448 * The "network-layer" protocol is PPPoE, which has a 6-byte
6449 * PPPoE header, followed by PPP payload, so we set the
6450 * offsets to the network layer offset plus 6 bytes for
6451 * the PPPoE header plus the values appropriate for PPP when
6452 * encapsulated in Ethernet (which means there's no HDLC
6455 off_linktype = orig_nl + 6;
6456 off_nl = orig_nl + 6 + 2;
6457 off_nl_nosnap = orig_nl + 6 + 2;
6460 * Set the link-layer type to PPP, as all subsequent tests will
6461 * be on the encapsulated PPP header.
6469 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
6481 bpf_error("'vpi' supported only on raw ATM");
6482 if (off_vpi == (u_int)-1)
6484 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
6490 bpf_error("'vci' supported only on raw ATM");
6491 if (off_vci == (u_int)-1)
6493 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
6498 if (off_proto == (u_int)-1)
6499 abort(); /* XXX - this isn't on FreeBSD */
6500 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
6505 if (off_payload == (u_int)-1)
6507 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
6508 0xffffffff, jtype, reverse, jvalue);
6513 bpf_error("'callref' supported only on raw ATM");
6514 if (off_proto == (u_int)-1)
6516 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
6517 jtype, reverse, jvalue);
6527 gen_atmtype_abbrev(type)
6530 struct block *b0, *b1;
6535 /* Get all packets in Meta signalling Circuit */
6537 bpf_error("'metac' supported only on raw ATM");
6538 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6539 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
6544 /* Get all packets in Broadcast Circuit*/
6546 bpf_error("'bcc' supported only on raw ATM");
6547 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6548 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
6553 /* Get all cells in Segment OAM F4 circuit*/
6555 bpf_error("'oam4sc' supported only on raw ATM");
6556 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6557 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6562 /* Get all cells in End-to-End OAM F4 Circuit*/
6564 bpf_error("'oam4ec' supported only on raw ATM");
6565 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6566 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6571 /* Get all packets in connection Signalling Circuit */
6573 bpf_error("'sc' supported only on raw ATM");
6574 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6575 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
6580 /* Get all packets in ILMI Circuit */
6582 bpf_error("'ilmic' supported only on raw ATM");
6583 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6584 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
6589 /* Get all LANE packets */
6591 bpf_error("'lane' supported only on raw ATM");
6592 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
6595 * Arrange that all subsequent tests assume LANE
6596 * rather than LLC-encapsulated packets, and set
6597 * the offsets appropriately for LANE-encapsulated
6600 * "off_mac" is the offset of the Ethernet header,
6601 * which is 2 bytes past the ATM pseudo-header
6602 * (skipping the pseudo-header and 2-byte LE Client
6603 * field). The other offsets are Ethernet offsets
6604 * relative to "off_mac".
6607 off_mac = off_payload + 2; /* MAC header */
6608 off_linktype = off_mac + 12;
6609 off_nl = off_mac + 14; /* Ethernet II */
6610 off_nl_nosnap = off_mac + 17; /* 802.3+802.2 */
6614 /* Get all LLC-encapsulated packets */
6616 bpf_error("'llc' supported only on raw ATM");
6617 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
6628 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
6635 bpf_u_int32 val1 , val2 , val3;
6637 switch (mtp3field) {
6640 if (off_sio == (u_int)-1)
6641 bpf_error("'sio' supported only on SS7");
6642 /* sio coded on 1 byte so max value 255 */
6644 bpf_error("sio value %u too big; max value = 255",
6646 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
6647 (u_int)jtype, reverse, (u_int)jvalue);
6651 if (off_opc == (u_int)-1)
6652 bpf_error("'opc' supported only on SS7");
6653 /* opc coded on 14 bits so max value 16383 */
6655 bpf_error("opc value %u too big; max value = 16383",
6657 /* the following instructions are made to convert jvalue
6658 * to the form used to write opc in an ss7 message*/
6659 val1 = jvalue & 0x00003c00;
6661 val2 = jvalue & 0x000003fc;
6663 val3 = jvalue & 0x00000003;
6665 jvalue = val1 + val2 + val3;
6666 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
6667 (u_int)jtype, reverse, (u_int)jvalue);
6671 if (off_dpc == (u_int)-1)
6672 bpf_error("'dpc' supported only on SS7");
6673 /* dpc coded on 14 bits so max value 16383 */
6675 bpf_error("dpc value %u too big; max value = 16383",
6677 /* the following instructions are made to convert jvalue
6678 * to the forme used to write dpc in an ss7 message*/
6679 val1 = jvalue & 0x000000ff;
6681 val2 = jvalue & 0x00003f00;
6683 jvalue = val1 + val2;
6684 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
6685 (u_int)jtype, reverse, (u_int)jvalue);
6689 if (off_sls == (u_int)-1)
6690 bpf_error("'sls' supported only on SS7");
6691 /* sls coded on 4 bits so max value 15 */
6693 bpf_error("sls value %u too big; max value = 15",
6695 /* the following instruction is made to convert jvalue
6696 * to the forme used to write sls in an ss7 message*/
6697 jvalue = jvalue << 4;
6698 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
6699 (u_int)jtype,reverse, (u_int)jvalue);
6708 static struct block *
6709 gen_msg_abbrev(type)
6715 * Q.2931 signalling protocol messages for handling virtual circuits
6716 * establishment and teardown
6721 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
6725 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
6729 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
6733 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
6737 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
6740 case A_RELEASE_DONE:
6741 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
6751 gen_atmmulti_abbrev(type)
6754 struct block *b0, *b1;
6760 bpf_error("'oam' supported only on raw ATM");
6761 b1 = gen_atmmulti_abbrev(A_OAMF4);
6766 bpf_error("'oamf4' supported only on raw ATM");
6768 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6769 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6771 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6777 * Get Q.2931 signalling messages for switched
6778 * virtual connection
6781 bpf_error("'connectmsg' supported only on raw ATM");
6782 b0 = gen_msg_abbrev(A_SETUP);
6783 b1 = gen_msg_abbrev(A_CALLPROCEED);
6785 b0 = gen_msg_abbrev(A_CONNECT);
6787 b0 = gen_msg_abbrev(A_CONNECTACK);
6789 b0 = gen_msg_abbrev(A_RELEASE);
6791 b0 = gen_msg_abbrev(A_RELEASE_DONE);
6793 b0 = gen_atmtype_abbrev(A_SC);
6799 bpf_error("'metaconnect' supported only on raw ATM");
6800 b0 = gen_msg_abbrev(A_SETUP);
6801 b1 = gen_msg_abbrev(A_CALLPROCEED);
6803 b0 = gen_msg_abbrev(A_CONNECT);
6805 b0 = gen_msg_abbrev(A_RELEASE);
6807 b0 = gen_msg_abbrev(A_RELEASE_DONE);
6809 b0 = gen_atmtype_abbrev(A_METAC);