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.24 2005/06/20 21:52:53 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>
93 #define IPPROTO_SCTP 132
96 #ifdef HAVE_OS_PROTO_H
100 #define JMP(c) ((c)|BPF_JMP|BPF_K)
103 static jmp_buf top_ctx;
104 static pcap_t *bpf_pcap;
106 /* Hack for updating VLAN, MPLS offsets. */
107 static u_int orig_linktype = -1U, orig_nl = -1U;
111 static int pcap_fddipad;
116 bpf_error(const char *fmt, ...)
121 if (bpf_pcap != NULL)
122 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
129 static void init_linktype(pcap_t *);
131 static int alloc_reg(void);
132 static void free_reg(int);
134 static struct block *root;
137 * Value passed to gen_load_a() to indicate what the offset argument
141 OR_PACKET, /* relative to the beginning of the packet */
142 OR_LINK, /* relative to the link-layer header */
143 OR_NET, /* relative to the network-layer header */
144 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
145 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
146 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
150 * We divy out chunks of memory rather than call malloc each time so
151 * we don't have to worry about leaking memory. It's probably
152 * not a big deal if all this memory was wasted but if this ever
153 * goes into a library that would probably not be a good idea.
155 * XXX - this *is* in a library....
158 #define CHUNK0SIZE 1024
164 static struct chunk chunks[NCHUNKS];
165 static int cur_chunk;
167 static void *newchunk(u_int);
168 static void freechunks(void);
169 static inline struct block *new_block(int);
170 static inline struct slist *new_stmt(int);
171 static struct block *gen_retblk(int);
172 static inline void syntax(void);
174 static void backpatch(struct block *, struct block *);
175 static void merge(struct block *, struct block *);
176 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
177 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
178 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
179 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
180 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
181 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
183 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
184 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
185 bpf_u_int32, bpf_u_int32, int, bpf_int32);
186 static struct slist *gen_load_llrel(u_int, u_int);
187 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
188 static struct slist *gen_loadx_iphdrlen(void);
189 static struct block *gen_uncond(int);
190 static inline struct block *gen_true(void);
191 static inline struct block *gen_false(void);
192 static struct block *gen_ether_linktype(int);
193 static struct block *gen_linux_sll_linktype(int);
194 static void insert_radiotap_load_llprefixlen(struct block *);
195 static void insert_load_llprefixlen(struct block *);
196 static struct slist *gen_llprefixlen(void);
197 static struct block *gen_linktype(int);
198 static struct block *gen_snap(bpf_u_int32, bpf_u_int32, u_int);
199 static struct block *gen_llc_linktype(int);
200 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
202 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
204 static struct block *gen_ahostop(const u_char *, int);
205 static struct block *gen_ehostop(const u_char *, int);
206 static struct block *gen_fhostop(const u_char *, int);
207 static struct block *gen_thostop(const u_char *, int);
208 static struct block *gen_wlanhostop(const u_char *, int);
209 static struct block *gen_ipfchostop(const u_char *, int);
210 static struct block *gen_dnhostop(bpf_u_int32, 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.
790 * Also assume it's not 802.11 with a fixed-length radio header.
803 off_nl = 6; /* XXX in reality, variable! */
804 off_nl_nosnap = 6; /* no 802.2 LLC */
807 case DLT_ARCNET_LINUX:
809 off_nl = 8; /* XXX in reality, variable! */
810 off_nl_nosnap = 8; /* no 802.2 LLC */
815 off_nl = 14; /* Ethernet II */
816 off_nl_nosnap = 17; /* 802.3+802.2 */
821 * SLIP doesn't have a link level type. The 16 byte
822 * header is hacked into our SLIP driver.
826 off_nl_nosnap = 16; /* no 802.2 LLC */
830 /* XXX this may be the same as the DLT_PPP_BSDOS case */
834 off_nl_nosnap = 24; /* no 802.2 LLC */
841 off_nl_nosnap = 4; /* no 802.2 LLC */
847 off_nl_nosnap = 12; /* no 802.2 LLC */
852 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
853 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
856 off_nl_nosnap = 4; /* no 802.2 LLC */
861 * This does no include the Ethernet header, and
862 * only covers session state.
866 off_nl_nosnap = 8; /* no 802.2 LLC */
872 off_nl_nosnap = 24; /* no 802.2 LLC */
877 * FDDI doesn't really have a link-level type field.
878 * We set "off_linktype" to the offset of the LLC header.
880 * To check for Ethernet types, we assume that SSAP = SNAP
881 * is being used and pick out the encapsulated Ethernet type.
882 * XXX - should we generate code to check for SNAP?
886 off_linktype += pcap_fddipad;
888 off_nl = 21; /* FDDI+802.2+SNAP */
889 off_nl_nosnap = 16; /* FDDI+802.2 */
891 off_nl += pcap_fddipad;
892 off_nl_nosnap += pcap_fddipad;
898 * Token Ring doesn't really have a link-level type field.
899 * We set "off_linktype" to the offset of the LLC header.
901 * To check for Ethernet types, we assume that SSAP = SNAP
902 * is being used and pick out the encapsulated Ethernet type.
903 * XXX - should we generate code to check for SNAP?
905 * XXX - the header is actually variable-length.
906 * Some various Linux patched versions gave 38
907 * as "off_linktype" and 40 as "off_nl"; however,
908 * if a token ring packet has *no* routing
909 * information, i.e. is not source-routed, the correct
910 * values are 20 and 22, as they are in the vanilla code.
912 * A packet is source-routed iff the uppermost bit
913 * of the first byte of the source address, at an
914 * offset of 8, has the uppermost bit set. If the
915 * packet is source-routed, the total number of bytes
916 * of routing information is 2 plus bits 0x1F00 of
917 * the 16-bit value at an offset of 14 (shifted right
918 * 8 - figure out which byte that is).
921 off_nl = 22; /* Token Ring+802.2+SNAP */
922 off_nl_nosnap = 17; /* Token Ring+802.2 */
927 * 802.11 doesn't really have a link-level type field.
928 * We set "off_linktype" to the offset of the LLC header.
930 * To check for Ethernet types, we assume that SSAP = SNAP
931 * is being used and pick out the encapsulated Ethernet type.
932 * XXX - should we generate code to check for SNAP?
934 * XXX - the header is actually variable-length. We
935 * assume a 24-byte link-layer header, as appears in
936 * data frames in networks with no bridges. If the
937 * fromds and tods 802.11 header bits are both set,
938 * it's actually supposed to be 30 bytes.
941 off_nl = 32; /* 802.11+802.2+SNAP */
942 off_nl_nosnap = 27; /* 802.11+802.2 */
945 case DLT_PRISM_HEADER:
947 * Same as 802.11, but with an additional header before
948 * the 802.11 header, containing a bunch of additional
949 * information including radio-level information.
951 * The header is 144 bytes long.
953 * XXX - same variable-length header problem; at least
954 * the Prism header is fixed-length.
957 off_linktype = 144+24;
958 off_nl = 144+32; /* Prism+802.11+802.2+SNAP */
959 off_nl_nosnap = 144+27; /* Prism+802.11+802.2 */
962 case DLT_IEEE802_11_RADIO_AVS:
964 * Same as 802.11, but with an additional header before
965 * the 802.11 header, containing a bunch of additional
966 * information including radio-level information.
968 * The header is 64 bytes long, at least in its
969 * current incarnation.
971 * XXX - same variable-length header problem, only
972 * more so; this header is also variable-length,
973 * with the length being the 32-bit big-endian
974 * number at an offset of 4 from the beginning
975 * of the radio header.
978 off_linktype = 64+24;
979 off_nl = 64+32; /* Radio+802.11+802.2+SNAP */
980 off_nl_nosnap = 64+27; /* Radio+802.11+802.2 */
983 case DLT_IEEE802_11_RADIO:
985 * Same as 802.11, but with an additional header before
986 * the 802.11 header, containing a bunch of additional
987 * information including radio-level information.
989 * The radiotap header is variable length, and we
990 * generate code to compute its length and store it
991 * in a register. These offsets are relative to the
992 * beginning of the 802.11 header.
995 off_nl = 32; /* 802.11+802.2+SNAP */
996 off_nl_nosnap = 27; /* 802.11+802.2 */
999 case DLT_ATM_RFC1483:
1000 case DLT_ATM_CLIP: /* Linux ATM defines this */
1002 * assume routed, non-ISO PDUs
1003 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1005 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1006 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1007 * latter would presumably be treated the way PPPoE
1008 * should be, so you can do "pppoe and udp port 2049"
1009 * or "pppoa and tcp port 80" and have it check for
1010 * PPPo{A,E} and a PPP protocol of IP and....
1013 off_nl = 8; /* 802.2+SNAP */
1014 off_nl_nosnap = 3; /* 802.2 */
1019 * Full Frontal ATM; you get AALn PDUs with an ATM
1023 off_vpi = SUNATM_VPI_POS;
1024 off_vci = SUNATM_VCI_POS;
1025 off_proto = PROTO_POS;
1026 off_mac = -1; /* LLC-encapsulated, so no MAC-layer header */
1027 off_payload = SUNATM_PKT_BEGIN_POS;
1028 off_linktype = off_payload;
1029 off_nl = off_payload+8; /* 802.2+SNAP */
1030 off_nl_nosnap = off_payload+3; /* 802.2 */
1036 off_nl_nosnap = 0; /* no 802.2 LLC */
1039 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1042 off_nl_nosnap = 16; /* no 802.2 LLC */
1047 * LocalTalk does have a 1-byte type field in the LLAP header,
1048 * but really it just indicates whether there is a "short" or
1049 * "long" DDP packet following.
1053 off_nl_nosnap = 0; /* no 802.2 LLC */
1056 case DLT_IP_OVER_FC:
1058 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1059 * link-level type field. We set "off_linktype" to the
1060 * offset of the LLC header.
1062 * To check for Ethernet types, we assume that SSAP = SNAP
1063 * is being used and pick out the encapsulated Ethernet type.
1064 * XXX - should we generate code to check for SNAP? RFC
1065 * 2625 says SNAP should be used.
1068 off_nl = 24; /* IPFC+802.2+SNAP */
1069 off_nl_nosnap = 19; /* IPFC+802.2 */
1074 * XXX - we should set this to handle SNAP-encapsulated
1075 * frames (NLPID of 0x80).
1079 off_nl_nosnap = 0; /* no 802.2 LLC */
1082 case DLT_APPLE_IP_OVER_IEEE1394:
1085 off_nl_nosnap = 18; /* no 802.2 LLC */
1088 case DLT_LINUX_IRDA:
1090 * Currently, only raw "link[N:M]" filtering is supported.
1099 * Currently, only raw "link[N:M]" filtering is supported.
1106 case DLT_SYMANTEC_FIREWALL:
1108 off_nl = 44; /* Ethernet II */
1109 off_nl_nosnap = 44; /* XXX - what does it do with 802.3 packets? */
1114 /* XXX read this from pf.h? */
1115 off_nl = PFLOG_HDRLEN;
1116 off_nl_nosnap = PFLOG_HDRLEN; /* no 802.2 LLC */
1119 case DLT_JUNIPER_MLFR:
1120 case DLT_JUNIPER_MLPPP:
1123 off_nl_nosnap = -1; /* no 802.2 LLC */
1126 case DLT_JUNIPER_ATM1:
1127 off_linktype = 4; /* in reality variable between 4-8 */
1132 case DLT_JUNIPER_ATM2:
1133 off_linktype = 8; /* in reality variable between 8-12 */
1138 /* frames captured on a Juniper PPPoE service PIC
1139 * contain raw ethernet frames */
1140 case DLT_JUNIPER_PPPOE:
1142 off_nl = 18; /* Ethernet II */
1143 off_nl_nosnap = 21; /* 802.3+802.2 */
1146 case DLT_JUNIPER_PPPOE_ATM:
1149 off_nl_nosnap = -1; /* no 802.2 LLC */
1152 case DLT_JUNIPER_GGSN:
1155 off_nl_nosnap = -1; /* no 802.2 LLC */
1158 case DLT_JUNIPER_ES:
1160 off_nl = -1; /* not really a network layer but raw IP adresses */
1161 off_nl_nosnap = -1; /* no 802.2 LLC */
1164 case DLT_JUNIPER_MONITOR:
1166 off_nl = 12; /* raw IP/IP6 header */
1167 off_nl_nosnap = -1; /* no 802.2 LLC */
1170 case DLT_JUNIPER_SERVICES:
1172 off_nl = -1; /* L3 proto location dep. on cookie type */
1173 off_nl_nosnap = -1; /* no 802.2 LLC */
1194 case DLT_LINUX_LAPD:
1196 * Currently, only raw "link[N:M]" filtering is supported.
1203 bpf_error("unknown data link type %d", linktype);
1208 * Load a value relative to the beginning of the link-layer header.
1209 * The link-layer header doesn't necessarily begin at the beginning
1210 * of the packet data; there might be a variable-length prefix containing
1211 * radio information.
1213 static struct slist *
1214 gen_load_llrel(offset, size)
1217 struct slist *s, *s2;
1219 s = gen_llprefixlen();
1222 * If "s" is non-null, it has code to arrange that the X register
1223 * contains the length of the prefix preceding the link-layer
1227 s2 = new_stmt(BPF_LD|BPF_IND|size);
1231 s = new_stmt(BPF_LD|BPF_ABS|size);
1238 * Load a value relative to the beginning of the specified header.
1240 static struct slist *
1241 gen_load_a(offrel, offset, size)
1242 enum e_offrel offrel;
1245 struct slist *s, *s2;
1250 s = gen_load_llrel(offset, size);
1254 s = gen_load_llrel(off_ll + offset, size);
1258 s = gen_load_llrel(off_nl + offset, size);
1262 s = gen_load_llrel(off_nl_nosnap + offset, size);
1267 * Load the X register with the length of the IPv4 header,
1270 s = gen_loadx_iphdrlen();
1273 * Load the item at {length of the link-layer header} +
1274 * {length of the IPv4 header} + {specified offset}.
1276 s2 = new_stmt(BPF_LD|BPF_IND|size);
1277 s2->s.k = off_nl + offset;
1282 s = gen_load_llrel(off_nl + 40 + offset, size);
1293 * Generate code to load into the X register the sum of the length of
1294 * the IPv4 header and any variable-length header preceding the link-layer
1297 static struct slist *
1298 gen_loadx_iphdrlen()
1300 struct slist *s, *s2;
1302 s = gen_llprefixlen();
1305 * There's a variable-length prefix preceding the
1306 * link-layer header. "s" points to a list of statements
1307 * that put the length of that prefix into the X register.
1308 * The 4*([k]&0xf) addressing mode can't be used, as we
1309 * don't have a constant offset, so we have to load the
1310 * value in question into the A register and add to it
1311 * the value from the X register.
1313 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1316 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1319 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1324 * The A register now contains the length of the
1325 * IP header. We need to add to it the length
1326 * of the prefix preceding the link-layer
1327 * header, which is still in the X register, and
1328 * move the result into the X register.
1330 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1331 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1334 * There is no variable-length header preceding the
1335 * link-layer header; if there's a fixed-length
1336 * header preceding it, its length is included in
1337 * the off_ variables, so it doesn't need to be added.
1339 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1345 static struct block *
1352 s = new_stmt(BPF_LD|BPF_IMM);
1354 b = new_block(JMP(BPF_JEQ));
1360 static inline struct block *
1363 return gen_uncond(1);
1366 static inline struct block *
1369 return gen_uncond(0);
1373 * Byte-swap a 32-bit number.
1374 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1375 * big-endian platforms.)
1377 #define SWAPLONG(y) \
1378 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1381 * Generate code to match a particular packet type.
1383 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1384 * value, if <= ETHERMTU. We use that to determine whether to
1385 * match the type/length field or to check the type/length field for
1386 * a value <= ETHERMTU to see whether it's a type field and then do
1387 * the appropriate test.
1389 static struct block *
1390 gen_ether_linktype(proto)
1393 struct block *b0, *b1;
1399 case LLCSAP_NETBEUI:
1401 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1402 * so we check the DSAP and SSAP.
1404 * LLCSAP_IP checks for IP-over-802.2, rather
1405 * than IP-over-Ethernet or IP-over-SNAP.
1407 * XXX - should we check both the DSAP and the
1408 * SSAP, like this, or should we check just the
1409 * DSAP, as we do for other types <= ETHERMTU
1410 * (i.e., other SAP values)?
1412 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1414 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1415 ((proto << 8) | proto));
1423 * Ethernet_II frames, which are Ethernet
1424 * frames with a frame type of ETHERTYPE_IPX;
1426 * Ethernet_802.3 frames, which are 802.3
1427 * frames (i.e., the type/length field is
1428 * a length field, <= ETHERMTU, rather than
1429 * a type field) with the first two bytes
1430 * after the Ethernet/802.3 header being
1433 * Ethernet_802.2 frames, which are 802.3
1434 * frames with an 802.2 LLC header and
1435 * with the IPX LSAP as the DSAP in the LLC
1438 * Ethernet_SNAP frames, which are 802.3
1439 * frames with an LLC header and a SNAP
1440 * header and with an OUI of 0x000000
1441 * (encapsulated Ethernet) and a protocol
1442 * ID of ETHERTYPE_IPX in the SNAP header.
1444 * XXX - should we generate the same code both
1445 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1449 * This generates code to check both for the
1450 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1452 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1453 (bpf_int32)LLCSAP_IPX);
1454 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H,
1459 * Now we add code to check for SNAP frames with
1460 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1462 b0 = gen_snap(0x000000, ETHERTYPE_IPX, 14);
1466 * Now we generate code to check for 802.3
1467 * frames in general.
1469 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1473 * Now add the check for 802.3 frames before the
1474 * check for Ethernet_802.2 and Ethernet_802.3,
1475 * as those checks should only be done on 802.3
1476 * frames, not on Ethernet frames.
1481 * Now add the check for Ethernet_II frames, and
1482 * do that before checking for the other frame
1485 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1486 (bpf_int32)ETHERTYPE_IPX);
1490 case ETHERTYPE_ATALK:
1491 case ETHERTYPE_AARP:
1493 * EtherTalk (AppleTalk protocols on Ethernet link
1494 * layer) may use 802.2 encapsulation.
1498 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1499 * we check for an Ethernet type field less than
1500 * 1500, which means it's an 802.3 length field.
1502 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1506 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1507 * SNAP packets with an organization code of
1508 * 0x080007 (Apple, for Appletalk) and a protocol
1509 * type of ETHERTYPE_ATALK (Appletalk).
1511 * 802.2-encapsulated ETHERTYPE_AARP packets are
1512 * SNAP packets with an organization code of
1513 * 0x000000 (encapsulated Ethernet) and a protocol
1514 * type of ETHERTYPE_AARP (Appletalk ARP).
1516 if (proto == ETHERTYPE_ATALK)
1517 b1 = gen_snap(0x080007, ETHERTYPE_ATALK, 14);
1518 else /* proto == ETHERTYPE_AARP */
1519 b1 = gen_snap(0x000000, ETHERTYPE_AARP, 14);
1523 * Check for Ethernet encapsulation (Ethertalk
1524 * phase 1?); we just check for the Ethernet
1527 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1533 if (proto <= ETHERMTU) {
1535 * This is an LLC SAP value, so the frames
1536 * that match would be 802.2 frames.
1537 * Check that the frame is an 802.2 frame
1538 * (i.e., that the length/type field is
1539 * a length field, <= ETHERMTU) and
1540 * then check the DSAP.
1542 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1544 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1550 * This is an Ethernet type, so compare
1551 * the length/type field with it (if
1552 * the frame is an 802.2 frame, the length
1553 * field will be <= ETHERMTU, and, as
1554 * "proto" is > ETHERMTU, this test
1555 * will fail and the frame won't match,
1556 * which is what we want).
1558 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1565 * Generate code to match a particular packet type.
1567 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1568 * value, if <= ETHERMTU. We use that to determine whether to
1569 * match the type field or to check the type field for the special
1570 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1572 static struct block *
1573 gen_linux_sll_linktype(proto)
1576 struct block *b0, *b1;
1582 case LLCSAP_NETBEUI:
1584 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1585 * so we check the DSAP and SSAP.
1587 * LLCSAP_IP checks for IP-over-802.2, rather
1588 * than IP-over-Ethernet or IP-over-SNAP.
1590 * XXX - should we check both the DSAP and the
1591 * SSAP, like this, or should we check just the
1592 * DSAP, as we do for other types <= ETHERMTU
1593 * (i.e., other SAP values)?
1595 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1596 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_H, (bpf_int32)
1597 ((proto << 8) | proto));
1603 * Ethernet_II frames, which are Ethernet
1604 * frames with a frame type of ETHERTYPE_IPX;
1606 * Ethernet_802.3 frames, which have a frame
1607 * type of LINUX_SLL_P_802_3;
1609 * Ethernet_802.2 frames, which are 802.3
1610 * frames with an 802.2 LLC header (i.e, have
1611 * a frame type of LINUX_SLL_P_802_2) and
1612 * with the IPX LSAP as the DSAP in the LLC
1615 * Ethernet_SNAP frames, which are 802.3
1616 * frames with an LLC header and a SNAP
1617 * header and with an OUI of 0x000000
1618 * (encapsulated Ethernet) and a protocol
1619 * ID of ETHERTYPE_IPX in the SNAP header.
1621 * First, do the checks on LINUX_SLL_P_802_2
1622 * frames; generate the check for either
1623 * Ethernet_802.2 or Ethernet_SNAP frames, and
1624 * then put a check for LINUX_SLL_P_802_2 frames
1627 b0 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1628 (bpf_int32)LLCSAP_IPX);
1629 b1 = gen_snap(0x000000, ETHERTYPE_IPX,
1632 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1636 * Now check for 802.3 frames and OR that with
1637 * the previous test.
1639 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1643 * Now add the check for Ethernet_II frames, and
1644 * do that before checking for the other frame
1647 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1648 (bpf_int32)ETHERTYPE_IPX);
1652 case ETHERTYPE_ATALK:
1653 case ETHERTYPE_AARP:
1655 * EtherTalk (AppleTalk protocols on Ethernet link
1656 * layer) may use 802.2 encapsulation.
1660 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1661 * we check for the 802.2 protocol type in the
1662 * "Ethernet type" field.
1664 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1667 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1668 * SNAP packets with an organization code of
1669 * 0x080007 (Apple, for Appletalk) and a protocol
1670 * type of ETHERTYPE_ATALK (Appletalk).
1672 * 802.2-encapsulated ETHERTYPE_AARP packets are
1673 * SNAP packets with an organization code of
1674 * 0x000000 (encapsulated Ethernet) and a protocol
1675 * type of ETHERTYPE_AARP (Appletalk ARP).
1677 if (proto == ETHERTYPE_ATALK)
1678 b1 = gen_snap(0x080007, ETHERTYPE_ATALK,
1680 else /* proto == ETHERTYPE_AARP */
1681 b1 = gen_snap(0x000000, ETHERTYPE_AARP,
1686 * Check for Ethernet encapsulation (Ethertalk
1687 * phase 1?); we just check for the Ethernet
1690 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1696 if (proto <= ETHERMTU) {
1698 * This is an LLC SAP value, so the frames
1699 * that match would be 802.2 frames.
1700 * Check for the 802.2 protocol type
1701 * in the "Ethernet type" field, and
1702 * then check the DSAP.
1704 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1706 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1712 * This is an Ethernet type, so compare
1713 * the length/type field with it (if
1714 * the frame is an 802.2 frame, the length
1715 * field will be <= ETHERMTU, and, as
1716 * "proto" is > ETHERMTU, this test
1717 * will fail and the frame won't match,
1718 * which is what we want).
1720 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1727 insert_radiotap_load_llprefixlen(b)
1730 struct slist *s1, *s2;
1733 * Prepend to the statements in this block code to load the
1734 * length of the radiotap header into the register assigned
1735 * to hold that length, if one has been assigned.
1737 if (reg_ll_size != -1) {
1739 * The 2 bytes at offsets of 2 and 3 from the beginning
1740 * of the radiotap header are the length of the radiotap
1741 * header; unfortunately, it's little-endian, so we have
1742 * to load it a byte at a time and construct the value.
1746 * Load the high-order byte, at an offset of 3, shift it
1747 * left a byte, and put the result in the X register.
1749 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1751 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1754 s2 = new_stmt(BPF_MISC|BPF_TAX);
1758 * Load the next byte, at an offset of 2, and OR the
1759 * value from the X register into it.
1761 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
1764 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
1768 * Now allocate a register to hold that value and store
1771 s2 = new_stmt(BPF_ST);
1772 s2->s.k = reg_ll_size;
1776 * Now move it into the X register.
1778 s2 = new_stmt(BPF_MISC|BPF_TAX);
1782 * Now append all the existing statements in this
1783 * block to these statements.
1785 sappend(s1, b->stmts);
1792 insert_load_llprefixlen(b)
1797 case DLT_IEEE802_11_RADIO:
1798 insert_radiotap_load_llprefixlen(b);
1803 static struct slist *
1804 gen_radiotap_llprefixlen(void)
1808 if (reg_ll_size == -1) {
1810 * We haven't yet assigned a register for the length
1811 * of the radiotap header; allocate one.
1813 reg_ll_size = alloc_reg();
1817 * Load the register containing the radiotap length
1818 * into the X register.
1820 s = new_stmt(BPF_LDX|BPF_MEM);
1821 s->s.k = reg_ll_size;
1826 * Generate code to compute the link-layer header length, if necessary,
1827 * putting it into the X register, and to return either a pointer to a
1828 * "struct slist" for the list of statements in that code, or NULL if
1829 * no code is necessary.
1831 static struct slist *
1832 gen_llprefixlen(void)
1836 case DLT_IEEE802_11_RADIO:
1837 return gen_radiotap_llprefixlen();
1845 * Generate code to match a particular packet type by matching the
1846 * link-layer type field or fields in the 802.2 LLC header.
1848 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1849 * value, if <= ETHERMTU.
1851 static struct block *
1855 struct block *b0, *b1, *b2;
1860 return gen_ether_linktype(proto);
1868 proto = (proto << 8 | LLCSAP_ISONS);
1872 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1881 case DLT_IEEE802_11:
1882 case DLT_IEEE802_11_RADIO_AVS:
1883 case DLT_IEEE802_11_RADIO:
1884 case DLT_PRISM_HEADER:
1885 case DLT_ATM_RFC1483:
1887 case DLT_IP_OVER_FC:
1888 return gen_llc_linktype(proto);
1894 * If "is_lane" is set, check for a LANE-encapsulated
1895 * version of this protocol, otherwise check for an
1896 * LLC-encapsulated version of this protocol.
1898 * We assume LANE means Ethernet, not Token Ring.
1902 * Check that the packet doesn't begin with an
1903 * LE Control marker. (We've already generated
1906 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
1911 * Now generate an Ethernet test.
1913 b1 = gen_ether_linktype(proto);
1918 * Check for LLC encapsulation and then check the
1921 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
1922 b1 = gen_llc_linktype(proto);
1930 return gen_linux_sll_linktype(proto);
1935 case DLT_SLIP_BSDOS:
1938 * These types don't provide any type field; packets
1941 * XXX - for IPv4, check for a version number of 4, and,
1942 * for IPv6, check for a version number of 6?
1948 case ETHERTYPE_IPV6:
1950 return gen_true(); /* always true */
1953 return gen_false(); /* always false */
1960 case DLT_PPP_SERIAL:
1963 * We use Ethernet protocol types inside libpcap;
1964 * map them to the corresponding PPP protocol types.
1973 case ETHERTYPE_IPV6:
1982 case ETHERTYPE_ATALK:
1996 * I'm assuming the "Bridging PDU"s that go
1997 * over PPP are Spanning Tree Protocol
2011 * We use Ethernet protocol types inside libpcap;
2012 * map them to the corresponding PPP protocol types.
2017 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
2018 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
2020 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
2025 case ETHERTYPE_IPV6:
2035 case ETHERTYPE_ATALK:
2049 * I'm assuming the "Bridging PDU"s that go
2050 * over PPP are Spanning Tree Protocol
2066 * For DLT_NULL, the link-layer header is a 32-bit
2067 * word containing an AF_ value in *host* byte order,
2068 * and for DLT_ENC, the link-layer header begins
2069 * with a 32-bit work containing an AF_ value in
2072 * In addition, if we're reading a saved capture file,
2073 * the host byte order in the capture may not be the
2074 * same as the host byte order on this machine.
2076 * For DLT_LOOP, the link-layer header is a 32-bit
2077 * word containing an AF_ value in *network* byte order.
2079 * XXX - AF_ values may, unfortunately, be platform-
2080 * dependent; for example, FreeBSD's AF_INET6 is 24
2081 * whilst NetBSD's and OpenBSD's is 26.
2083 * This means that, when reading a capture file, just
2084 * checking for our AF_INET6 value won't work if the
2085 * capture file came from another OS.
2094 case ETHERTYPE_IPV6:
2101 * Not a type on which we support filtering.
2102 * XXX - support those that have AF_ values
2103 * #defined on this platform, at least?
2108 if (linktype == DLT_NULL || linktype == DLT_ENC) {
2110 * The AF_ value is in host byte order, but
2111 * the BPF interpreter will convert it to
2112 * network byte order.
2114 * If this is a save file, and it's from a
2115 * machine with the opposite byte order to
2116 * ours, we byte-swap the AF_ value.
2118 * Then we run it through "htonl()", and
2119 * generate code to compare against the result.
2121 if (bpf_pcap->sf.rfile != NULL &&
2122 bpf_pcap->sf.swapped)
2123 proto = SWAPLONG(proto);
2124 proto = htonl(proto);
2126 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
2130 * af field is host byte order in contrast to the rest of
2133 if (proto == ETHERTYPE_IP)
2134 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2135 BPF_B, (bpf_int32)AF_INET));
2137 else if (proto == ETHERTYPE_IPV6)
2138 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
2139 BPF_B, (bpf_int32)AF_INET6));
2147 case DLT_ARCNET_LINUX:
2149 * XXX should we check for first fragment if the protocol
2158 case ETHERTYPE_IPV6:
2159 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2160 (bpf_int32)ARCTYPE_INET6));
2164 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2165 (bpf_int32)ARCTYPE_IP);
2166 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2167 (bpf_int32)ARCTYPE_IP_OLD);
2172 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2173 (bpf_int32)ARCTYPE_ARP);
2174 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
2175 (bpf_int32)ARCTYPE_ARP_OLD);
2179 case ETHERTYPE_REVARP:
2180 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2181 (bpf_int32)ARCTYPE_REVARP));
2183 case ETHERTYPE_ATALK:
2184 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
2185 (bpf_int32)ARCTYPE_ATALK));
2192 case ETHERTYPE_ATALK:
2202 * XXX - assumes a 2-byte Frame Relay header with
2203 * DLCI and flags. What if the address is longer?
2209 * Check for the special NLPID for IP.
2211 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
2214 case ETHERTYPE_IPV6:
2216 * Check for the special NLPID for IPv6.
2218 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
2223 * Check for several OSI protocols.
2225 * Frame Relay packets typically have an OSI
2226 * NLPID at the beginning; we check for each
2229 * What we check for is the NLPID and a frame
2230 * control field of UI, i.e. 0x03 followed
2233 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
2234 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
2235 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
2246 case DLT_JUNIPER_MLFR:
2247 case DLT_JUNIPER_MLPPP:
2248 case DLT_JUNIPER_ATM1:
2249 case DLT_JUNIPER_ATM2:
2250 case DLT_JUNIPER_PPPOE:
2251 case DLT_JUNIPER_PPPOE_ATM:
2252 case DLT_JUNIPER_GGSN:
2253 case DLT_JUNIPER_ES:
2254 case DLT_JUNIPER_MONITOR:
2255 case DLT_JUNIPER_SERVICES:
2256 /* just lets verify the magic number for now -
2257 * on ATM we may have up to 6 different encapsulations on the wire
2258 * and need a lot of heuristics to figure out that the payload
2261 * FIXME encapsulation specific BPF_ filters
2263 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
2265 case DLT_LINUX_IRDA:
2266 bpf_error("IrDA link-layer type filtering not implemented");
2269 bpf_error("DOCSIS link-layer type filtering not implemented");
2271 case DLT_LINUX_LAPD:
2272 bpf_error("LAPD link-layer type filtering not implemented");
2276 * All the types that have no encapsulation should either be
2277 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2278 * all packets are IP packets, or should be handled in some
2279 * special case, if none of them are (if some are and some
2280 * aren't, the lack of encapsulation is a problem, as we'd
2281 * have to find some other way of determining the packet type).
2283 * Therefore, if "off_linktype" is -1, there's an error.
2285 if (off_linktype == (u_int)-1)
2289 * Any type not handled above should always have an Ethernet
2290 * type at an offset of "off_linktype". (PPP is partially
2291 * handled above - the protocol type is mapped from the
2292 * Ethernet and LLC types we use internally to the corresponding
2293 * PPP type - but the PPP type is always specified by a value
2294 * at "off_linktype", so we don't have to do the code generation
2297 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2301 * Check for an LLC SNAP packet with a given organization code and
2302 * protocol type; we check the entire contents of the 802.2 LLC and
2303 * snap headers, checking for DSAP and SSAP of SNAP and a control
2304 * field of 0x03 in the LLC header, and for the specified organization
2305 * code and protocol type in the SNAP header.
2307 static struct block *
2308 gen_snap(orgcode, ptype, offset)
2309 bpf_u_int32 orgcode;
2313 u_char snapblock[8];
2315 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
2316 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
2317 snapblock[2] = 0x03; /* control = UI */
2318 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
2319 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
2320 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
2321 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
2322 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
2323 return gen_bcmp(OR_LINK, offset, 8, snapblock);
2327 * Generate code to match a particular packet type, for link-layer types
2328 * using 802.2 LLC headers.
2330 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2331 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2333 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2334 * value, if <= ETHERMTU. We use that to determine whether to
2335 * match the DSAP or both DSAP and LSAP or to check the OUI and
2336 * protocol ID in a SNAP header.
2338 static struct block *
2339 gen_llc_linktype(proto)
2343 * XXX - handle token-ring variable-length header.
2349 case LLCSAP_NETBEUI:
2351 * XXX - should we check both the DSAP and the
2352 * SSAP, like this, or should we check just the
2353 * DSAP, as we do for other types <= ETHERMTU
2354 * (i.e., other SAP values)?
2356 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_u_int32)
2357 ((proto << 8) | proto));
2361 * XXX - are there ever SNAP frames for IPX on
2362 * non-Ethernet 802.x networks?
2364 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2365 (bpf_int32)LLCSAP_IPX);
2367 case ETHERTYPE_ATALK:
2369 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2370 * SNAP packets with an organization code of
2371 * 0x080007 (Apple, for Appletalk) and a protocol
2372 * type of ETHERTYPE_ATALK (Appletalk).
2374 * XXX - check for an organization code of
2375 * encapsulated Ethernet as well?
2377 return gen_snap(0x080007, ETHERTYPE_ATALK, off_linktype);
2381 * XXX - we don't have to check for IPX 802.3
2382 * here, but should we check for the IPX Ethertype?
2384 if (proto <= ETHERMTU) {
2386 * This is an LLC SAP value, so check
2389 return gen_cmp(OR_LINK, off_linktype, BPF_B,
2393 * This is an Ethernet type; we assume that it's
2394 * unlikely that it'll appear in the right place
2395 * at random, and therefore check only the
2396 * location that would hold the Ethernet type
2397 * in a SNAP frame with an organization code of
2398 * 0x000000 (encapsulated Ethernet).
2400 * XXX - if we were to check for the SNAP DSAP and
2401 * LSAP, as per XXX, and were also to check for an
2402 * organization code of 0x000000 (encapsulated
2403 * Ethernet), we'd do
2405 * return gen_snap(0x000000, proto,
2408 * here; for now, we don't, as per the above.
2409 * I don't know whether it's worth the extra CPU
2410 * time to do the right check or not.
2412 return gen_cmp(OR_LINK, off_linktype+6, BPF_H,
2418 static struct block *
2419 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
2423 u_int src_off, dst_off;
2425 struct block *b0, *b1;
2439 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2440 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2446 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
2447 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
2454 b0 = gen_linktype(proto);
2455 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
2461 static struct block *
2462 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
2463 struct in6_addr *addr;
2464 struct in6_addr *mask;
2466 u_int src_off, dst_off;
2468 struct block *b0, *b1;
2483 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2484 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2490 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2491 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2498 /* this order is important */
2499 a = (u_int32_t *)addr;
2500 m = (u_int32_t *)mask;
2501 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
2502 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
2504 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
2506 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
2508 b0 = gen_linktype(proto);
2514 static struct block *
2515 gen_ehostop(eaddr, dir)
2516 register const u_char *eaddr;
2519 register struct block *b0, *b1;
2523 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
2526 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
2529 b0 = gen_ehostop(eaddr, Q_SRC);
2530 b1 = gen_ehostop(eaddr, Q_DST);
2536 b0 = gen_ehostop(eaddr, Q_SRC);
2537 b1 = gen_ehostop(eaddr, Q_DST);
2546 * Like gen_ehostop, but for DLT_FDDI
2548 static struct block *
2549 gen_fhostop(eaddr, dir)
2550 register const u_char *eaddr;
2553 struct block *b0, *b1;
2558 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
2560 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
2565 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
2567 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
2571 b0 = gen_fhostop(eaddr, Q_SRC);
2572 b1 = gen_fhostop(eaddr, Q_DST);
2578 b0 = gen_fhostop(eaddr, Q_SRC);
2579 b1 = gen_fhostop(eaddr, Q_DST);
2588 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2590 static struct block *
2591 gen_thostop(eaddr, dir)
2592 register const u_char *eaddr;
2595 register struct block *b0, *b1;
2599 return gen_bcmp(OR_LINK, 8, 6, eaddr);
2602 return gen_bcmp(OR_LINK, 2, 6, eaddr);
2605 b0 = gen_thostop(eaddr, Q_SRC);
2606 b1 = gen_thostop(eaddr, Q_DST);
2612 b0 = gen_thostop(eaddr, Q_SRC);
2613 b1 = gen_thostop(eaddr, Q_DST);
2622 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2624 static struct block *
2625 gen_wlanhostop(eaddr, dir)
2626 register const u_char *eaddr;
2629 register struct block *b0, *b1, *b2;
2630 register struct slist *s;
2637 * For control frames, there is no SA.
2639 * For management frames, SA is at an
2640 * offset of 10 from the beginning of
2643 * For data frames, SA is at an offset
2644 * of 10 from the beginning of the packet
2645 * if From DS is clear, at an offset of
2646 * 16 from the beginning of the packet
2647 * if From DS is set and To DS is clear,
2648 * and an offset of 24 from the beginning
2649 * of the packet if From DS is set and To DS
2654 * Generate the tests to be done for data frames
2657 * First, check for To DS set, i.e. check "link[1] & 0x01".
2659 s = gen_load_a(OR_LINK, 1, BPF_B);
2660 b1 = new_block(JMP(BPF_JSET));
2661 b1->s.k = 0x01; /* To DS */
2665 * If To DS is set, the SA is at 24.
2667 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
2671 * Now, check for To DS not set, i.e. check
2672 * "!(link[1] & 0x01)".
2674 s = gen_load_a(OR_LINK, 1, BPF_B);
2675 b2 = new_block(JMP(BPF_JSET));
2676 b2->s.k = 0x01; /* To DS */
2681 * If To DS is not set, the SA is at 16.
2683 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
2687 * Now OR together the last two checks. That gives
2688 * the complete set of checks for data frames with
2694 * Now check for From DS being set, and AND that with
2695 * the ORed-together checks.
2697 s = gen_load_a(OR_LINK, 1, BPF_B);
2698 b1 = new_block(JMP(BPF_JSET));
2699 b1->s.k = 0x02; /* From DS */
2704 * Now check for data frames with From DS not set.
2706 s = gen_load_a(OR_LINK, 1, BPF_B);
2707 b2 = new_block(JMP(BPF_JSET));
2708 b2->s.k = 0x02; /* From DS */
2713 * If From DS isn't set, the SA is at 10.
2715 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
2719 * Now OR together the checks for data frames with
2720 * From DS not set and for data frames with From DS
2721 * set; that gives the checks done for data frames.
2726 * Now check for a data frame.
2727 * I.e, check "link[0] & 0x08".
2729 gen_load_a(OR_LINK, 0, BPF_B);
2730 b1 = new_block(JMP(BPF_JSET));
2735 * AND that with the checks done for data frames.
2740 * If the high-order bit of the type value is 0, this
2741 * is a management frame.
2742 * I.e, check "!(link[0] & 0x08)".
2744 s = gen_load_a(OR_LINK, 0, BPF_B);
2745 b2 = new_block(JMP(BPF_JSET));
2751 * For management frames, the SA is at 10.
2753 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
2757 * OR that with the checks done for data frames.
2758 * That gives the checks done for management and
2764 * If the low-order bit of the type value is 1,
2765 * this is either a control frame or a frame
2766 * with a reserved type, and thus not a
2769 * I.e., check "!(link[0] & 0x04)".
2771 s = gen_load_a(OR_LINK, 0, BPF_B);
2772 b1 = new_block(JMP(BPF_JSET));
2778 * AND that with the checks for data and management
2788 * For control frames, there is no DA.
2790 * For management frames, DA is at an
2791 * offset of 4 from the beginning of
2794 * For data frames, DA is at an offset
2795 * of 4 from the beginning of the packet
2796 * if To DS is clear and at an offset of
2797 * 16 from the beginning of the packet
2802 * Generate the tests to be done for data frames.
2804 * First, check for To DS set, i.e. "link[1] & 0x01".
2806 s = gen_load_a(OR_LINK, 1, BPF_B);
2807 b1 = new_block(JMP(BPF_JSET));
2808 b1->s.k = 0x01; /* To DS */
2812 * If To DS is set, the DA is at 16.
2814 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
2818 * Now, check for To DS not set, i.e. check
2819 * "!(link[1] & 0x01)".
2821 s = gen_load_a(OR_LINK, 1, BPF_B);
2822 b2 = new_block(JMP(BPF_JSET));
2823 b2->s.k = 0x01; /* To DS */
2828 * If To DS is not set, the DA is at 4.
2830 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
2834 * Now OR together the last two checks. That gives
2835 * the complete set of checks for data frames.
2840 * Now check for a data frame.
2841 * I.e, check "link[0] & 0x08".
2843 s = gen_load_a(OR_LINK, 0, BPF_B);
2844 b1 = new_block(JMP(BPF_JSET));
2849 * AND that with the checks done for data frames.
2854 * If the high-order bit of the type value is 0, this
2855 * is a management frame.
2856 * I.e, check "!(link[0] & 0x08)".
2858 s = gen_load_a(OR_LINK, 0, BPF_B);
2859 b2 = new_block(JMP(BPF_JSET));
2865 * For management frames, the DA is at 4.
2867 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
2871 * OR that with the checks done for data frames.
2872 * That gives the checks done for management and
2878 * If the low-order bit of the type value is 1,
2879 * this is either a control frame or a frame
2880 * with a reserved type, and thus not a
2883 * I.e., check "!(link[0] & 0x04)".
2885 s = gen_load_a(OR_LINK, 0, BPF_B);
2886 b1 = new_block(JMP(BPF_JSET));
2892 * AND that with the checks for data and management
2899 b0 = gen_wlanhostop(eaddr, Q_SRC);
2900 b1 = gen_wlanhostop(eaddr, Q_DST);
2906 b0 = gen_wlanhostop(eaddr, Q_SRC);
2907 b1 = gen_wlanhostop(eaddr, Q_DST);
2916 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2917 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2918 * as the RFC states.)
2920 static struct block *
2921 gen_ipfchostop(eaddr, dir)
2922 register const u_char *eaddr;
2925 register struct block *b0, *b1;
2929 return gen_bcmp(OR_LINK, 10, 6, eaddr);
2932 return gen_bcmp(OR_LINK, 2, 6, eaddr);
2935 b0 = gen_ipfchostop(eaddr, Q_SRC);
2936 b1 = gen_ipfchostop(eaddr, Q_DST);
2942 b0 = gen_ipfchostop(eaddr, Q_SRC);
2943 b1 = gen_ipfchostop(eaddr, Q_DST);
2952 * This is quite tricky because there may be pad bytes in front of the
2953 * DECNET header, and then there are two possible data packet formats that
2954 * carry both src and dst addresses, plus 5 packet types in a format that
2955 * carries only the src node, plus 2 types that use a different format and
2956 * also carry just the src node.
2960 * Instead of doing those all right, we just look for data packets with
2961 * 0 or 1 bytes of padding. If you want to look at other packets, that
2962 * will require a lot more hacking.
2964 * To add support for filtering on DECNET "areas" (network numbers)
2965 * one would want to add a "mask" argument to this routine. That would
2966 * make the filter even more inefficient, although one could be clever
2967 * and not generate masking instructions if the mask is 0xFFFF.
2969 static struct block *
2970 gen_dnhostop(addr, dir)
2974 struct block *b0, *b1, *b2, *tmp;
2975 u_int offset_lh; /* offset if long header is received */
2976 u_int offset_sh; /* offset if short header is received */
2981 offset_sh = 1; /* follows flags */
2982 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
2986 offset_sh = 3; /* follows flags, dstnode */
2987 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2991 /* Inefficient because we do our Calvinball dance twice */
2992 b0 = gen_dnhostop(addr, Q_SRC);
2993 b1 = gen_dnhostop(addr, Q_DST);
2999 /* Inefficient because we do our Calvinball dance twice */
3000 b0 = gen_dnhostop(addr, Q_SRC);
3001 b1 = gen_dnhostop(addr, Q_DST);
3006 bpf_error("ISO host filtering not implemented");
3011 b0 = gen_linktype(ETHERTYPE_DN);
3012 /* Check for pad = 1, long header case */
3013 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3014 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
3015 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
3016 BPF_H, (bpf_int32)ntohs(addr));
3018 /* Check for pad = 0, long header case */
3019 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
3020 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs(addr));
3023 /* Check for pad = 1, short header case */
3024 tmp = gen_mcmp(OR_NET, 2, BPF_H,
3025 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
3026 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
3029 /* Check for pad = 0, short header case */
3030 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
3031 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
3035 /* Combine with test for linktype */
3040 static struct block *
3041 gen_host(addr, mask, proto, dir)
3047 struct block *b0, *b1;
3052 b0 = gen_host(addr, mask, Q_IP, dir);
3053 if (off_linktype != (u_int)-1) {
3054 b1 = gen_host(addr, mask, Q_ARP, dir);
3056 b0 = gen_host(addr, mask, Q_RARP, dir);
3062 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
3065 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
3068 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
3071 bpf_error("'tcp' modifier applied to host");
3074 bpf_error("'sctp' modifier applied to host");
3077 bpf_error("'udp' modifier applied to host");
3080 bpf_error("'icmp' modifier applied to host");
3083 bpf_error("'igmp' modifier applied to host");
3086 bpf_error("'igrp' modifier applied to host");
3089 bpf_error("'pim' modifier applied to host");
3092 bpf_error("'vrrp' modifier applied to host");
3095 bpf_error("ATALK host filtering not implemented");
3098 bpf_error("AARP host filtering not implemented");
3101 return gen_dnhostop(addr, dir);
3104 bpf_error("SCA host filtering not implemented");
3107 bpf_error("LAT host filtering not implemented");
3110 bpf_error("MOPDL host filtering not implemented");
3113 bpf_error("MOPRC host filtering not implemented");
3117 bpf_error("'ip6' modifier applied to ip host");
3120 bpf_error("'icmp6' modifier applied to host");
3124 bpf_error("'ah' modifier applied to host");
3127 bpf_error("'esp' modifier applied to host");
3130 bpf_error("ISO host filtering not implemented");
3133 bpf_error("'esis' modifier applied to host");
3136 bpf_error("'isis' modifier applied to host");
3139 bpf_error("'clnp' modifier applied to host");
3142 bpf_error("'stp' modifier applied to host");
3145 bpf_error("IPX host filtering not implemented");
3148 bpf_error("'netbeui' modifier applied to host");
3151 bpf_error("'radio' modifier applied to host");
3160 static struct block *
3161 gen_host6(addr, mask, proto, dir)
3162 struct in6_addr *addr;
3163 struct in6_addr *mask;
3170 return gen_host6(addr, mask, Q_IPV6, dir);
3173 bpf_error("'ip' modifier applied to ip6 host");
3176 bpf_error("'rarp' modifier applied to ip6 host");
3179 bpf_error("'arp' modifier applied to ip6 host");
3182 bpf_error("'sctp' modifier applied to host");
3185 bpf_error("'tcp' modifier applied to host");
3188 bpf_error("'udp' modifier applied to host");
3191 bpf_error("'icmp' modifier applied to host");
3194 bpf_error("'igmp' modifier applied to host");
3197 bpf_error("'igrp' modifier applied to host");
3200 bpf_error("'pim' modifier applied to host");
3203 bpf_error("'vrrp' modifier applied to host");
3206 bpf_error("ATALK host filtering not implemented");
3209 bpf_error("AARP host filtering not implemented");
3212 bpf_error("'decnet' modifier applied to ip6 host");
3215 bpf_error("SCA host filtering not implemented");
3218 bpf_error("LAT host filtering not implemented");
3221 bpf_error("MOPDL host filtering not implemented");
3224 bpf_error("MOPRC host filtering not implemented");
3227 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
3230 bpf_error("'icmp6' modifier applied to host");
3233 bpf_error("'ah' modifier applied to host");
3236 bpf_error("'esp' modifier applied to host");
3239 bpf_error("ISO host filtering not implemented");
3242 bpf_error("'esis' modifier applied to host");
3245 bpf_error("'isis' modifier applied to host");
3248 bpf_error("'clnp' modifier applied to host");
3251 bpf_error("'stp' modifier applied to host");
3254 bpf_error("IPX host filtering not implemented");
3257 bpf_error("'netbeui' modifier applied to host");
3260 bpf_error("'radio' modifier applied to host");
3270 static struct block *
3271 gen_gateway(eaddr, alist, proto, dir)
3272 const u_char *eaddr;
3273 bpf_u_int32 **alist;
3277 struct block *b0, *b1, *tmp;
3280 bpf_error("direction applied to 'gateway'");
3287 if (linktype == DLT_EN10MB)
3288 b0 = gen_ehostop(eaddr, Q_OR);
3289 else if (linktype == DLT_FDDI)
3290 b0 = gen_fhostop(eaddr, Q_OR);
3291 else if (linktype == DLT_IEEE802)
3292 b0 = gen_thostop(eaddr, Q_OR);
3293 else if (linktype == DLT_IEEE802_11 ||
3294 linktype == DLT_IEEE802_11_RADIO_AVS ||
3295 linktype == DLT_IEEE802_11_RADIO ||
3296 linktype == DLT_PRISM_HEADER)
3297 b0 = gen_wlanhostop(eaddr, Q_OR);
3298 else if (linktype == DLT_SUNATM && is_lane) {
3300 * Check that the packet doesn't begin with an
3301 * LE Control marker. (We've already generated
3304 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3309 * Now check the MAC address.
3311 b0 = gen_ehostop(eaddr, Q_OR);
3313 } else if (linktype == DLT_IP_OVER_FC)
3314 b0 = gen_ipfchostop(eaddr, Q_OR);
3317 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3319 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR);
3321 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR);
3329 bpf_error("illegal modifier of 'gateway'");
3335 gen_proto_abbrev(proto)
3344 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
3346 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
3352 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
3354 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
3360 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
3362 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
3368 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
3371 #ifndef IPPROTO_IGMP
3372 #define IPPROTO_IGMP 2
3376 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
3379 #ifndef IPPROTO_IGRP
3380 #define IPPROTO_IGRP 9
3383 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
3387 #define IPPROTO_PIM 103
3391 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
3393 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
3398 #ifndef IPPROTO_VRRP
3399 #define IPPROTO_VRRP 112
3403 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
3407 b1 = gen_linktype(ETHERTYPE_IP);
3411 b1 = gen_linktype(ETHERTYPE_ARP);
3415 b1 = gen_linktype(ETHERTYPE_REVARP);
3419 bpf_error("link layer applied in wrong context");
3422 b1 = gen_linktype(ETHERTYPE_ATALK);
3426 b1 = gen_linktype(ETHERTYPE_AARP);
3430 b1 = gen_linktype(ETHERTYPE_DN);
3434 b1 = gen_linktype(ETHERTYPE_SCA);
3438 b1 = gen_linktype(ETHERTYPE_LAT);
3442 b1 = gen_linktype(ETHERTYPE_MOPDL);
3446 b1 = gen_linktype(ETHERTYPE_MOPRC);
3451 b1 = gen_linktype(ETHERTYPE_IPV6);
3454 #ifndef IPPROTO_ICMPV6
3455 #define IPPROTO_ICMPV6 58
3458 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
3463 #define IPPROTO_AH 51
3466 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
3468 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
3474 #define IPPROTO_ESP 50
3477 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
3479 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
3485 b1 = gen_linktype(LLCSAP_ISONS);
3489 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
3493 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
3496 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
3497 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3498 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3500 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3502 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3504 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3508 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
3509 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3510 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3512 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3514 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3516 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3520 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
3521 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3522 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3524 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
3529 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3530 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3535 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3536 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3538 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3540 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3545 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3546 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3551 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3552 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3557 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
3561 b1 = gen_linktype(LLCSAP_8021D);
3565 b1 = gen_linktype(LLCSAP_IPX);
3569 b1 = gen_linktype(LLCSAP_NETBEUI);
3573 bpf_error("'radio' is not a valid protocol type");
3581 static struct block *
3588 s = gen_load_a(OR_NET, 6, BPF_H);
3589 b = new_block(JMP(BPF_JSET));
3598 * Generate a comparison to a port value in the transport-layer header
3599 * at the specified offset from the beginning of that header.
3601 * XXX - this handles a variable-length prefix preceding the link-layer
3602 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3603 * variable-length link-layer headers (such as Token Ring or 802.11
3606 static struct block *
3607 gen_portatom(off, v)
3611 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
3615 static struct block *
3616 gen_portatom6(off, v)
3620 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
3625 gen_portop(port, proto, dir)
3626 int port, proto, dir;
3628 struct block *b0, *b1, *tmp;
3630 /* ip proto 'proto' */
3631 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
3637 b1 = gen_portatom(0, (bpf_int32)port);
3641 b1 = gen_portatom(2, (bpf_int32)port);
3646 tmp = gen_portatom(0, (bpf_int32)port);
3647 b1 = gen_portatom(2, (bpf_int32)port);
3652 tmp = gen_portatom(0, (bpf_int32)port);
3653 b1 = gen_portatom(2, (bpf_int32)port);
3665 static struct block *
3666 gen_port(port, ip_proto, dir)
3671 struct block *b0, *b1, *tmp;
3676 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3677 * not LLC encapsulation with LLCSAP_IP.
3679 * For IEEE 802 networks - which includes 802.5 token ring
3680 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3681 * says that SNAP encapsulation is used, not LLC encapsulation
3684 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3685 * RFC 2225 say that SNAP encapsulation is used, not LLC
3686 * encapsulation with LLCSAP_IP.
3688 * So we always check for ETHERTYPE_IP.
3690 b0 = gen_linktype(ETHERTYPE_IP);
3696 b1 = gen_portop(port, ip_proto, dir);
3700 tmp = gen_portop(port, IPPROTO_TCP, dir);
3701 b1 = gen_portop(port, IPPROTO_UDP, dir);
3703 tmp = gen_portop(port, IPPROTO_SCTP, dir);
3716 gen_portop6(port, proto, dir)
3717 int port, proto, dir;
3719 struct block *b0, *b1, *tmp;
3721 /* ip6 proto 'proto' */
3722 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
3726 b1 = gen_portatom6(0, (bpf_int32)port);
3730 b1 = gen_portatom6(2, (bpf_int32)port);
3735 tmp = gen_portatom6(0, (bpf_int32)port);
3736 b1 = gen_portatom6(2, (bpf_int32)port);
3741 tmp = gen_portatom6(0, (bpf_int32)port);
3742 b1 = gen_portatom6(2, (bpf_int32)port);
3754 static struct block *
3755 gen_port6(port, ip_proto, dir)
3760 struct block *b0, *b1, *tmp;
3762 /* link proto ip6 */
3763 b0 = gen_linktype(ETHERTYPE_IPV6);
3769 b1 = gen_portop6(port, ip_proto, dir);
3773 tmp = gen_portop6(port, IPPROTO_TCP, dir);
3774 b1 = gen_portop6(port, IPPROTO_UDP, dir);
3776 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
3788 /* gen_portrange code */
3789 static struct block *
3790 gen_portrangeatom(off, v1, v2)
3794 struct block *b1, *b2;
3798 * Reverse the order of the ports, so v1 is the lower one.
3807 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
3808 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
3816 gen_portrangeop(port1, port2, proto, dir)
3821 struct block *b0, *b1, *tmp;
3823 /* ip proto 'proto' */
3824 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
3830 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3834 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
3839 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3840 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
3845 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
3846 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
3858 static struct block *
3859 gen_portrange(port1, port2, ip_proto, dir)
3864 struct block *b0, *b1, *tmp;
3867 b0 = gen_linktype(ETHERTYPE_IP);
3873 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
3877 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
3878 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
3880 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
3892 static struct block *
3893 gen_portrangeatom6(off, v1, v2)
3897 struct block *b1, *b2;
3901 * Reverse the order of the ports, so v1 is the lower one.
3910 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
3911 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
3919 gen_portrangeop6(port1, port2, proto, dir)
3924 struct block *b0, *b1, *tmp;
3926 /* ip6 proto 'proto' */
3927 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
3931 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
3935 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
3940 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
3941 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
3946 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
3947 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
3959 static struct block *
3960 gen_portrange6(port1, port2, ip_proto, dir)
3965 struct block *b0, *b1, *tmp;
3967 /* link proto ip6 */
3968 b0 = gen_linktype(ETHERTYPE_IPV6);
3974 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
3978 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
3979 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
3981 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
3994 lookup_proto(name, proto)
3995 register const char *name;
4005 v = pcap_nametoproto(name);
4006 if (v == PROTO_UNDEF)
4007 bpf_error("unknown ip proto '%s'", name);
4011 /* XXX should look up h/w protocol type based on linktype */
4012 v = pcap_nametoeproto(name);
4013 if (v == PROTO_UNDEF) {
4014 v = pcap_nametollc(name);
4015 if (v == PROTO_UNDEF)
4016 bpf_error("unknown ether proto '%s'", name);
4021 if (strcmp(name, "esis") == 0)
4023 else if (strcmp(name, "isis") == 0)
4025 else if (strcmp(name, "clnp") == 0)
4028 bpf_error("unknown osi proto '%s'", name);
4048 static struct block *
4049 gen_protochain(v, proto, dir)
4054 #ifdef NO_PROTOCHAIN
4055 return gen_proto(v, proto, dir);
4057 struct block *b0, *b;
4058 struct slist *s[100];
4059 int fix2, fix3, fix4, fix5;
4060 int ahcheck, again, end;
4062 int reg2 = alloc_reg();
4064 memset(s, 0, sizeof(s));
4065 fix2 = fix3 = fix4 = fix5 = 0;
4072 b0 = gen_protochain(v, Q_IP, dir);
4073 b = gen_protochain(v, Q_IPV6, dir);
4077 bpf_error("bad protocol applied for 'protochain'");
4082 * We don't handle variable-length radiotap here headers yet.
4083 * We might want to add BPF instructions to do the protochain
4084 * work, to simplify that and, on platforms that have a BPF
4085 * interpreter with the new instructions, let the filtering
4086 * be done in the kernel. (We already require a modified BPF
4087 * engine to do the protochain stuff, to support backward
4088 * branches, and backward branch support is unlikely to appear
4089 * in kernel BPF engines.)
4091 if (linktype == DLT_IEEE802_11_RADIO)
4092 bpf_error("'protochain' not supported with radiotap headers");
4094 no_optimize = 1; /*this code is not compatible with optimzer yet */
4097 * s[0] is a dummy entry to protect other BPF insn from damage
4098 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4099 * hard to find interdependency made by jump table fixup.
4102 s[i] = new_stmt(0); /*dummy*/
4107 b0 = gen_linktype(ETHERTYPE_IP);
4110 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4111 s[i]->s.k = off_nl + 9;
4113 /* X = ip->ip_hl << 2 */
4114 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
4120 b0 = gen_linktype(ETHERTYPE_IPV6);
4122 /* A = ip6->ip_nxt */
4123 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
4124 s[i]->s.k = off_nl + 6;
4126 /* X = sizeof(struct ip6_hdr) */
4127 s[i] = new_stmt(BPF_LDX|BPF_IMM);
4133 bpf_error("unsupported proto to gen_protochain");
4137 /* again: if (A == v) goto end; else fall through; */
4139 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4141 s[i]->s.jt = NULL; /*later*/
4142 s[i]->s.jf = NULL; /*update in next stmt*/
4146 #ifndef IPPROTO_NONE
4147 #define IPPROTO_NONE 59
4149 /* if (A == IPPROTO_NONE) goto end */
4150 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4151 s[i]->s.jt = NULL; /*later*/
4152 s[i]->s.jf = NULL; /*update in next stmt*/
4153 s[i]->s.k = IPPROTO_NONE;
4154 s[fix5]->s.jf = s[i];
4159 if (proto == Q_IPV6) {
4160 int v6start, v6end, v6advance, j;
4163 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4164 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4165 s[i]->s.jt = NULL; /*later*/
4166 s[i]->s.jf = NULL; /*update in next stmt*/
4167 s[i]->s.k = IPPROTO_HOPOPTS;
4168 s[fix2]->s.jf = s[i];
4170 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4171 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4172 s[i]->s.jt = NULL; /*later*/
4173 s[i]->s.jf = NULL; /*update in next stmt*/
4174 s[i]->s.k = IPPROTO_DSTOPTS;
4176 /* if (A == IPPROTO_ROUTING) goto v6advance */
4177 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4178 s[i]->s.jt = NULL; /*later*/
4179 s[i]->s.jf = NULL; /*update in next stmt*/
4180 s[i]->s.k = IPPROTO_ROUTING;
4182 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4183 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4184 s[i]->s.jt = NULL; /*later*/
4185 s[i]->s.jf = NULL; /*later*/
4186 s[i]->s.k = IPPROTO_FRAGMENT;
4197 * X = X + (P[X + 1] + 1) * 8;
4200 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4202 /* A = P[X + packet head] */
4203 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4207 s[i] = new_stmt(BPF_ST);
4211 s[i] = new_stmt(BPF_MISC|BPF_TXA);
4214 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4218 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4220 /* A = P[X + packet head]; */
4221 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4225 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4229 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4233 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4236 s[i] = new_stmt(BPF_LD|BPF_MEM);
4240 /* goto again; (must use BPF_JA for backward jump) */
4241 s[i] = new_stmt(BPF_JMP|BPF_JA);
4242 s[i]->s.k = again - i - 1;
4243 s[i - 1]->s.jf = s[i];
4247 for (j = v6start; j <= v6end; j++)
4248 s[j]->s.jt = s[v6advance];
4253 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4255 s[fix2]->s.jf = s[i];
4261 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4262 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
4263 s[i]->s.jt = NULL; /*later*/
4264 s[i]->s.jf = NULL; /*later*/
4265 s[i]->s.k = IPPROTO_AH;
4267 s[fix3]->s.jf = s[ahcheck];
4274 * X = X + (P[X + 1] + 2) * 4;
4277 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4279 /* A = P[X + packet head]; */
4280 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4284 s[i] = new_stmt(BPF_ST);
4288 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
4291 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4295 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4297 /* A = P[X + packet head] */
4298 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
4302 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4306 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
4310 s[i] = new_stmt(BPF_MISC|BPF_TAX);
4313 s[i] = new_stmt(BPF_LD|BPF_MEM);
4317 /* goto again; (must use BPF_JA for backward jump) */
4318 s[i] = new_stmt(BPF_JMP|BPF_JA);
4319 s[i]->s.k = again - i - 1;
4324 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
4326 s[fix2]->s.jt = s[end];
4327 s[fix4]->s.jf = s[end];
4328 s[fix5]->s.jt = s[end];
4335 for (i = 0; i < max - 1; i++)
4336 s[i]->next = s[i + 1];
4337 s[max - 1]->next = NULL;
4342 b = new_block(JMP(BPF_JEQ));
4343 b->stmts = s[1]; /*remember, s[0] is dummy*/
4354 * Generate code that checks whether the packet is a packet for protocol
4355 * <proto> and whether the type field in that protocol's header has
4356 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4357 * IP packet and checks the protocol number in the IP header against <v>.
4359 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4360 * against Q_IP and Q_IPV6.
4362 static struct block *
4363 gen_proto(v, proto, dir)
4368 struct block *b0, *b1;
4370 if (dir != Q_DEFAULT)
4371 bpf_error("direction applied to 'proto'");
4376 b0 = gen_proto(v, Q_IP, dir);
4377 b1 = gen_proto(v, Q_IPV6, dir);
4385 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4386 * not LLC encapsulation with LLCSAP_IP.
4388 * For IEEE 802 networks - which includes 802.5 token ring
4389 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4390 * says that SNAP encapsulation is used, not LLC encapsulation
4393 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4394 * RFC 2225 say that SNAP encapsulation is used, not LLC
4395 * encapsulation with LLCSAP_IP.
4397 * So we always check for ETHERTYPE_IP.
4399 b0 = gen_linktype(ETHERTYPE_IP);
4401 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
4403 b1 = gen_protochain(v, Q_IP);
4413 * Frame Relay packets typically have an OSI
4414 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4415 * generates code to check for all the OSI
4416 * NLPIDs, so calling it and then adding a check
4417 * for the particular NLPID for which we're
4418 * looking is bogus, as we can just check for
4421 * What we check for is the NLPID and a frame
4422 * control field value of UI, i.e. 0x03 followed
4425 * XXX - assumes a 2-byte Frame Relay header with
4426 * DLCI and flags. What if the address is longer?
4428 * XXX - what about SNAP-encapsulated frames?
4430 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
4436 * Cisco uses an Ethertype lookalike - for OSI,
4439 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
4440 /* OSI in C-HDLC is stuffed with a fudge byte */
4441 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
4446 b0 = gen_linktype(LLCSAP_ISONS);
4447 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
4453 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4455 * 4 is the offset of the PDU type relative to the IS-IS
4458 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
4463 bpf_error("arp does not encapsulate another protocol");
4467 bpf_error("rarp does not encapsulate another protocol");
4471 bpf_error("atalk encapsulation is not specifiable");
4475 bpf_error("decnet encapsulation is not specifiable");
4479 bpf_error("sca does not encapsulate another protocol");
4483 bpf_error("lat does not encapsulate another protocol");
4487 bpf_error("moprc does not encapsulate another protocol");
4491 bpf_error("mopdl does not encapsulate another protocol");
4495 return gen_linktype(v);
4498 bpf_error("'udp proto' is bogus");
4502 bpf_error("'tcp proto' is bogus");
4506 bpf_error("'sctp proto' is bogus");
4510 bpf_error("'icmp proto' is bogus");
4514 bpf_error("'igmp proto' is bogus");
4518 bpf_error("'igrp proto' is bogus");
4522 bpf_error("'pim proto' is bogus");
4526 bpf_error("'vrrp proto' is bogus");
4531 b0 = gen_linktype(ETHERTYPE_IPV6);
4533 b1 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
4535 b1 = gen_protochain(v, Q_IPV6);
4541 bpf_error("'icmp6 proto' is bogus");
4545 bpf_error("'ah proto' is bogus");
4548 bpf_error("'ah proto' is bogus");
4551 bpf_error("'stp proto' is bogus");
4554 bpf_error("'ipx proto' is bogus");
4557 bpf_error("'netbeui proto' is bogus");
4560 bpf_error("'radio proto' is bogus");
4571 register const char *name;
4574 int proto = q.proto;
4578 bpf_u_int32 mask, addr;
4580 bpf_u_int32 **alist;
4583 struct sockaddr_in *sin;
4584 struct sockaddr_in6 *sin6;
4585 struct addrinfo *res, *res0;
4586 struct in6_addr mask128;
4588 struct block *b, *tmp;
4589 int port, real_proto;
4595 addr = pcap_nametonetaddr(name);
4597 bpf_error("unknown network '%s'", name);
4598 /* Left justify network addr and calculate its network mask */
4600 while (addr && (addr & 0xff000000) == 0) {
4604 return gen_host(addr, mask, proto, dir);
4608 if (proto == Q_LINK) {
4612 eaddr = pcap_ether_hostton(name);
4615 "unknown ether host '%s'", name);
4616 b = gen_ehostop(eaddr, dir);
4621 eaddr = pcap_ether_hostton(name);
4624 "unknown FDDI host '%s'", name);
4625 b = gen_fhostop(eaddr, dir);
4630 eaddr = pcap_ether_hostton(name);
4633 "unknown token ring host '%s'", name);
4634 b = gen_thostop(eaddr, dir);
4638 case DLT_IEEE802_11:
4639 case DLT_IEEE802_11_RADIO_AVS:
4640 case DLT_IEEE802_11_RADIO:
4641 case DLT_PRISM_HEADER:
4642 eaddr = pcap_ether_hostton(name);
4645 "unknown 802.11 host '%s'", name);
4646 b = gen_wlanhostop(eaddr, dir);
4650 case DLT_IP_OVER_FC:
4651 eaddr = pcap_ether_hostton(name);
4654 "unknown Fibre Channel host '%s'", name);
4655 b = gen_ipfchostop(eaddr, dir);
4664 * Check that the packet doesn't begin
4665 * with an LE Control marker. (We've
4666 * already generated a test for LANE.)
4668 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
4672 eaddr = pcap_ether_hostton(name);
4675 "unknown ether host '%s'", name);
4676 b = gen_ehostop(eaddr, dir);
4682 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4683 } else if (proto == Q_DECNET) {
4684 unsigned short dn_addr = __pcap_nametodnaddr(name);
4686 * I don't think DECNET hosts can be multihomed, so
4687 * there is no need to build up a list of addresses
4689 return (gen_host(dn_addr, 0, proto, dir));
4692 alist = pcap_nametoaddr(name);
4693 if (alist == NULL || *alist == NULL)
4694 bpf_error("unknown host '%s'", name);
4696 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
4698 b = gen_host(**alist++, 0xffffffff, tproto, dir);
4700 tmp = gen_host(**alist++, 0xffffffff,
4707 memset(&mask128, 0xff, sizeof(mask128));
4708 res0 = res = pcap_nametoaddrinfo(name);
4710 bpf_error("unknown host '%s'", name);
4712 tproto = tproto6 = proto;
4713 if (off_linktype == -1 && tproto == Q_DEFAULT) {
4717 for (res = res0; res; res = res->ai_next) {
4718 switch (res->ai_family) {
4720 if (tproto == Q_IPV6)
4723 sin = (struct sockaddr_in *)
4725 tmp = gen_host(ntohl(sin->sin_addr.s_addr),
4726 0xffffffff, tproto, dir);
4729 if (tproto6 == Q_IP)
4732 sin6 = (struct sockaddr_in6 *)
4734 tmp = gen_host6(&sin6->sin6_addr,
4735 &mask128, tproto6, dir);
4746 bpf_error("unknown host '%s'%s", name,
4747 (proto == Q_DEFAULT)
4749 : " for specified address family");
4756 if (proto != Q_DEFAULT &&
4757 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
4758 bpf_error("illegal qualifier of 'port'");
4759 if (pcap_nametoport(name, &port, &real_proto) == 0)
4760 bpf_error("unknown port '%s'", name);
4761 if (proto == Q_UDP) {
4762 if (real_proto == IPPROTO_TCP)
4763 bpf_error("port '%s' is tcp", name);
4764 else if (real_proto == IPPROTO_SCTP)
4765 bpf_error("port '%s' is sctp", name);
4767 /* override PROTO_UNDEF */
4768 real_proto = IPPROTO_UDP;
4770 if (proto == Q_TCP) {
4771 if (real_proto == IPPROTO_UDP)
4772 bpf_error("port '%s' is udp", name);
4774 else if (real_proto == IPPROTO_SCTP)
4775 bpf_error("port '%s' is sctp", name);
4777 /* override PROTO_UNDEF */
4778 real_proto = IPPROTO_TCP;
4780 if (proto == Q_SCTP) {
4781 if (real_proto == IPPROTO_UDP)
4782 bpf_error("port '%s' is udp", name);
4784 else if (real_proto == IPPROTO_TCP)
4785 bpf_error("port '%s' is tcp", name);
4787 /* override PROTO_UNDEF */
4788 real_proto = IPPROTO_SCTP;
4791 return gen_port(port, real_proto, dir);
4795 b = gen_port(port, real_proto, dir);
4796 gen_or(gen_port6(port, real_proto, dir), b);
4802 if (proto != Q_DEFAULT &&
4803 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
4804 bpf_error("illegal qualifier of 'portrange'");
4805 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
4806 bpf_error("unknown port in range '%s'", name);
4807 if (proto == Q_UDP) {
4808 if (real_proto == IPPROTO_TCP)
4809 bpf_error("port in range '%s' is tcp", name);
4810 else if (real_proto == IPPROTO_SCTP)
4811 bpf_error("port in range '%s' is sctp", name);
4813 /* override PROTO_UNDEF */
4814 real_proto = IPPROTO_UDP;
4816 if (proto == Q_TCP) {
4817 if (real_proto == IPPROTO_UDP)
4818 bpf_error("port in range '%s' is udp", name);
4819 else if (real_proto == IPPROTO_SCTP)
4820 bpf_error("port in range '%s' is sctp", name);
4822 /* override PROTO_UNDEF */
4823 real_proto = IPPROTO_TCP;
4825 if (proto == Q_SCTP) {
4826 if (real_proto == IPPROTO_UDP)
4827 bpf_error("port in range '%s' is udp", name);
4828 else if (real_proto == IPPROTO_TCP)
4829 bpf_error("port in range '%s' is tcp", name);
4831 /* override PROTO_UNDEF */
4832 real_proto = IPPROTO_SCTP;
4835 return gen_portrange(port1, port2, real_proto, dir);
4839 b = gen_portrange(port1, port2, real_proto, dir);
4840 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
4847 eaddr = pcap_ether_hostton(name);
4849 bpf_error("unknown ether host: %s", name);
4851 alist = pcap_nametoaddr(name);
4852 if (alist == NULL || *alist == NULL)
4853 bpf_error("unknown host '%s'", name);
4854 b = gen_gateway(eaddr, alist, proto, dir);
4858 bpf_error("'gateway' not supported in this configuration");
4862 real_proto = lookup_proto(name, proto);
4863 if (real_proto >= 0)
4864 return gen_proto(real_proto, proto, dir);
4866 bpf_error("unknown protocol: %s", name);
4869 real_proto = lookup_proto(name, proto);
4870 if (real_proto >= 0)
4871 return gen_protochain(real_proto, proto, dir);
4873 bpf_error("unknown protocol: %s", name);
4885 gen_mcode(s1, s2, masklen, q)
4886 register const char *s1, *s2;
4887 register int masklen;
4890 register int nlen, mlen;
4893 nlen = __pcap_atoin(s1, &n);
4894 /* Promote short ipaddr */
4898 mlen = __pcap_atoin(s2, &m);
4899 /* Promote short ipaddr */
4902 bpf_error("non-network bits set in \"%s mask %s\"",
4905 /* Convert mask len to mask */
4907 bpf_error("mask length must be <= 32");
4908 m = 0xffffffff << (32 - masklen);
4910 bpf_error("non-network bits set in \"%s/%d\"",
4917 return gen_host(n, m, q.proto, q.dir);
4920 bpf_error("Mask syntax for networks only");
4928 register const char *s;
4933 int proto = q.proto;
4939 else if (q.proto == Q_DECNET)
4940 vlen = __pcap_atodn(s, &v);
4942 vlen = __pcap_atoin(s, &v);
4949 if (proto == Q_DECNET)
4950 return gen_host(v, 0, proto, dir);
4951 else if (proto == Q_LINK) {
4952 bpf_error("illegal link layer address");
4955 if (s == NULL && q.addr == Q_NET) {
4956 /* Promote short net number */
4957 while (v && (v & 0xff000000) == 0) {
4962 /* Promote short ipaddr */
4966 return gen_host(v, mask, proto, dir);
4971 proto = IPPROTO_UDP;
4972 else if (proto == Q_TCP)
4973 proto = IPPROTO_TCP;
4974 else if (proto == Q_SCTP)
4975 proto = IPPROTO_SCTP;
4976 else if (proto == Q_DEFAULT)
4977 proto = PROTO_UNDEF;
4979 bpf_error("illegal qualifier of 'port'");
4982 return gen_port((int)v, proto, dir);
4986 b = gen_port((int)v, proto, dir);
4987 gen_or(gen_port6((int)v, proto, dir), b);
4994 proto = IPPROTO_UDP;
4995 else if (proto == Q_TCP)
4996 proto = IPPROTO_TCP;
4997 else if (proto == Q_SCTP)
4998 proto = IPPROTO_SCTP;
4999 else if (proto == Q_DEFAULT)
5000 proto = PROTO_UNDEF;
5002 bpf_error("illegal qualifier of 'portrange'");
5005 return gen_portrange((int)v, (int)v, proto, dir);
5009 b = gen_portrange((int)v, (int)v, proto, dir);
5010 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
5016 bpf_error("'gateway' requires a name");
5020 return gen_proto((int)v, proto, dir);
5023 return gen_protochain((int)v, proto, dir);
5038 gen_mcode6(s1, s2, masklen, q)
5039 register const char *s1, *s2;
5040 register int masklen;
5043 struct addrinfo *res;
5044 struct in6_addr *addr;
5045 struct in6_addr mask;
5050 bpf_error("no mask %s supported", s2);
5052 res = pcap_nametoaddrinfo(s1);
5054 bpf_error("invalid ip6 address %s", s1);
5056 bpf_error("%s resolved to multiple address", s1);
5057 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
5059 if (sizeof(mask) * 8 < masklen)
5060 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
5061 memset(&mask, 0, sizeof(mask));
5062 memset(&mask, 0xff, masklen / 8);
5064 mask.s6_addr[masklen / 8] =
5065 (0xff << (8 - masklen % 8)) & 0xff;
5068 a = (u_int32_t *)addr;
5069 m = (u_int32_t *)&mask;
5070 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
5071 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
5072 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
5080 bpf_error("Mask syntax for networks only");
5084 b = gen_host6(addr, &mask, q.proto, q.dir);
5089 bpf_error("invalid qualifier against IPv6 address");
5097 register const u_char *eaddr;
5100 struct block *b, *tmp;
5102 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
5103 if (linktype == DLT_EN10MB)
5104 return gen_ehostop(eaddr, (int)q.dir);
5105 if (linktype == DLT_FDDI)
5106 return gen_fhostop(eaddr, (int)q.dir);
5107 if (linktype == DLT_IEEE802)
5108 return gen_thostop(eaddr, (int)q.dir);
5109 if (linktype == DLT_IEEE802_11 ||
5110 linktype == DLT_IEEE802_11_RADIO_AVS ||
5111 linktype == DLT_IEEE802_11_RADIO ||
5112 linktype == DLT_PRISM_HEADER)
5113 return gen_wlanhostop(eaddr, (int)q.dir);
5114 if (linktype == DLT_SUNATM && is_lane) {
5116 * Check that the packet doesn't begin with an
5117 * LE Control marker. (We've already generated
5120 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5125 * Now check the MAC address.
5127 b = gen_ehostop(eaddr, (int)q.dir);
5131 if (linktype == DLT_IP_OVER_FC)
5132 return gen_ipfchostop(eaddr, (int)q.dir);
5133 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5135 bpf_error("ethernet address used in non-ether expression");
5141 struct slist *s0, *s1;
5144 * This is definitely not the best way to do this, but the
5145 * lists will rarely get long.
5152 static struct slist *
5158 s = new_stmt(BPF_LDX|BPF_MEM);
5163 static struct slist *
5169 s = new_stmt(BPF_LD|BPF_MEM);
5175 * Modify "index" to use the value stored into its register as an
5176 * offset relative to the beginning of the header for the protocol
5177 * "proto", and allocate a register and put an item "size" bytes long
5178 * (1, 2, or 4) at that offset into that register, making it the register
5182 gen_load(proto, index, size)
5187 struct slist *s, *tmp;
5189 int regno = alloc_reg();
5191 free_reg(index->regno);
5195 bpf_error("data size must be 1, 2, or 4");
5211 bpf_error("unsupported index operation");
5215 * The offset is relative to the beginning of the packet
5216 * data, if we have a radio header. (If we don't, this
5219 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
5220 linktype != DLT_IEEE802_11_RADIO &&
5221 linktype != DLT_PRISM_HEADER)
5222 bpf_error("radio information not present in capture");
5225 * Load into the X register the offset computed into the
5226 * register specifed by "index".
5228 s = xfer_to_x(index);
5231 * Load the item at that offset.
5233 tmp = new_stmt(BPF_LD|BPF_IND|size);
5235 sappend(index->s, s);
5240 * The offset is relative to the beginning of
5241 * the link-layer header.
5243 * XXX - what about ATM LANE? Should the index be
5244 * relative to the beginning of the AAL5 frame, so
5245 * that 0 refers to the beginning of the LE Control
5246 * field, or relative to the beginning of the LAN
5247 * frame, so that 0 refers, for Ethernet LANE, to
5248 * the beginning of the destination address?
5250 s = gen_llprefixlen();
5253 * If "s" is non-null, it has code to arrange that the
5254 * X register contains the length of the prefix preceding
5255 * the link-layer header. Add to it the offset computed
5256 * into the register specified by "index", and move that
5257 * into the X register. Otherwise, just load into the X
5258 * register the offset computed into the register specifed
5262 sappend(s, xfer_to_a(index));
5263 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5264 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5266 s = xfer_to_x(index);
5269 * Load the item at the sum of the offset we've put in the
5270 * X register and the offset of the start of the link
5271 * layer header (which is 0 if the radio header is
5272 * variable-length; that header length is what we put
5273 * into the X register and then added to the index).
5275 tmp = new_stmt(BPF_LD|BPF_IND|size);
5278 sappend(index->s, s);
5294 * The offset is relative to the beginning of
5295 * the network-layer header.
5296 * XXX - are there any cases where we want
5299 s = gen_llprefixlen();
5302 * If "s" is non-null, it has code to arrange that the
5303 * X register contains the length of the prefix preceding
5304 * the link-layer header. Add to it the offset computed
5305 * into the register specified by "index", and move that
5306 * into the X register. Otherwise, just load into the X
5307 * register the offset computed into the register specifed
5311 sappend(s, xfer_to_a(index));
5312 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5313 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5315 s = xfer_to_x(index);
5318 * Load the item at the sum of the offset we've put in the
5319 * X register and the offset of the start of the network
5322 tmp = new_stmt(BPF_LD|BPF_IND|size);
5325 sappend(index->s, s);
5328 * Do the computation only if the packet contains
5329 * the protocol in question.
5331 b = gen_proto_abbrev(proto);
5333 gen_and(index->b, b);
5346 * The offset is relative to the beginning of
5347 * the transport-layer header.
5348 * XXX - are there any cases where we want
5350 * XXX - we should, if we're built with
5351 * IPv6 support, generate code to load either
5352 * IPv4, IPv6, or both, as appropriate.
5354 s = gen_loadx_iphdrlen();
5357 * The X register now contains the sum of the offset
5358 * of the beginning of the link-layer header and
5359 * the length of the network-layer header. Load
5360 * into the A register the offset relative to
5361 * the beginning of the transport layer header,
5362 * add the X register to that, move that to the
5363 * X register, and load with an offset from the
5364 * X register equal to the offset of the network
5365 * layer header relative to the beginning of
5366 * the link-layer header.
5368 sappend(s, xfer_to_a(index));
5369 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
5370 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
5371 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
5373 sappend(index->s, s);
5376 * Do the computation only if the packet contains
5377 * the protocol in question - which is true only
5378 * if this is an IP datagram and is the first or
5379 * only fragment of that datagram.
5381 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
5383 gen_and(index->b, b);
5385 gen_and(gen_proto_abbrev(Q_IP), b);
5391 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5395 index->regno = regno;
5396 s = new_stmt(BPF_ST);
5398 sappend(index->s, s);
5404 gen_relation(code, a0, a1, reversed)
5406 struct arth *a0, *a1;
5409 struct slist *s0, *s1, *s2;
5410 struct block *b, *tmp;
5414 if (code == BPF_JEQ) {
5415 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
5416 b = new_block(JMP(code));
5420 b = new_block(BPF_JMP|code|BPF_X);
5426 sappend(a0->s, a1->s);
5430 free_reg(a0->regno);
5431 free_reg(a1->regno);
5433 /* 'and' together protocol checks */
5436 gen_and(a0->b, tmp = a1->b);
5452 int regno = alloc_reg();
5453 struct arth *a = (struct arth *)newchunk(sizeof(*a));
5456 s = new_stmt(BPF_LD|BPF_LEN);
5457 s->next = new_stmt(BPF_ST);
5458 s->next->s.k = regno;
5473 a = (struct arth *)newchunk(sizeof(*a));
5477 s = new_stmt(BPF_LD|BPF_IMM);
5479 s->next = new_stmt(BPF_ST);
5495 s = new_stmt(BPF_ALU|BPF_NEG);
5498 s = new_stmt(BPF_ST);
5506 gen_arth(code, a0, a1)
5508 struct arth *a0, *a1;
5510 struct slist *s0, *s1, *s2;
5514 s2 = new_stmt(BPF_ALU|BPF_X|code);
5519 sappend(a0->s, a1->s);
5521 free_reg(a0->regno);
5522 free_reg(a1->regno);
5524 s0 = new_stmt(BPF_ST);
5525 a0->regno = s0->s.k = alloc_reg();
5532 * Here we handle simple allocation of the scratch registers.
5533 * If too many registers are alloc'd, the allocator punts.
5535 static int regused[BPF_MEMWORDS];
5539 * Return the next free register.
5544 int n = BPF_MEMWORDS;
5547 if (regused[curreg])
5548 curreg = (curreg + 1) % BPF_MEMWORDS;
5550 regused[curreg] = 1;
5554 bpf_error("too many registers needed to evaluate expression");
5559 * Return a register to the table so it can
5569 static struct block *
5576 s = new_stmt(BPF_LD|BPF_LEN);
5577 b = new_block(JMP(jmp));
5588 return gen_len(BPF_JGE, n);
5592 * Actually, this is less than or equal.
5600 b = gen_len(BPF_JGT, n);
5607 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5608 * the beginning of the link-layer header.
5609 * XXX - that means you can't test values in the radiotap header, but
5610 * as that header is difficult if not impossible to parse generally
5611 * without a loop, that might not be a severe problem. A new keyword
5612 * "radio" could be added for that, although what you'd really want
5613 * would be a way of testing particular radio header values, which
5614 * would generate code appropriate to the radio header in question.
5617 gen_byteop(op, idx, val)
5628 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5631 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5635 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
5639 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
5643 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
5647 b = new_block(JMP(BPF_JEQ));
5654 static u_char abroadcast[] = { 0x0 };
5657 gen_broadcast(proto)
5660 bpf_u_int32 hostmask;
5661 struct block *b0, *b1, *b2;
5662 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5668 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
5669 return gen_ahostop(abroadcast, Q_DST);
5670 if (linktype == DLT_EN10MB)
5671 return gen_ehostop(ebroadcast, Q_DST);
5672 if (linktype == DLT_FDDI)
5673 return gen_fhostop(ebroadcast, Q_DST);
5674 if (linktype == DLT_IEEE802)
5675 return gen_thostop(ebroadcast, Q_DST);
5676 if (linktype == DLT_IEEE802_11 ||
5677 linktype == DLT_IEEE802_11_RADIO_AVS ||
5678 linktype == DLT_IEEE802_11_RADIO ||
5679 linktype == DLT_PRISM_HEADER)
5680 return gen_wlanhostop(ebroadcast, Q_DST);
5681 if (linktype == DLT_IP_OVER_FC)
5682 return gen_ipfchostop(ebroadcast, Q_DST);
5683 if (linktype == DLT_SUNATM && is_lane) {
5685 * Check that the packet doesn't begin with an
5686 * LE Control marker. (We've already generated
5689 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5694 * Now check the MAC address.
5696 b0 = gen_ehostop(ebroadcast, Q_DST);
5700 bpf_error("not a broadcast link");
5704 b0 = gen_linktype(ETHERTYPE_IP);
5705 hostmask = ~netmask;
5706 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
5707 b2 = gen_mcmp(OR_NET, 16, BPF_W,
5708 (bpf_int32)(~0 & hostmask), hostmask);
5713 bpf_error("only link-layer/IP broadcast filters supported");
5718 * Generate code to test the low-order bit of a MAC address (that's
5719 * the bottom bit of the *first* byte).
5721 static struct block *
5722 gen_mac_multicast(offset)
5725 register struct block *b0;
5726 register struct slist *s;
5728 /* link[offset] & 1 != 0 */
5729 s = gen_load_a(OR_LINK, offset, BPF_B);
5730 b0 = new_block(JMP(BPF_JSET));
5737 gen_multicast(proto)
5740 register struct block *b0, *b1, *b2;
5741 register struct slist *s;
5747 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
5748 /* all ARCnet multicasts use the same address */
5749 return gen_ahostop(abroadcast, Q_DST);
5751 if (linktype == DLT_EN10MB) {
5752 /* ether[0] & 1 != 0 */
5753 return gen_mac_multicast(0);
5756 if (linktype == DLT_FDDI) {
5758 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5760 * XXX - was that referring to bit-order issues?
5762 /* fddi[1] & 1 != 0 */
5763 return gen_mac_multicast(1);
5766 if (linktype == DLT_IEEE802) {
5767 /* tr[2] & 1 != 0 */
5768 return gen_mac_multicast(2);
5771 if (linktype == DLT_IEEE802_11 ||
5772 linktype == DLT_IEEE802_11_RADIO_AVS ||
5773 linktype == DLT_IEEE802_11_RADIO ||
5774 linktype == DLT_PRISM_HEADER) {
5778 * For control frames, there is no DA.
5780 * For management frames, DA is at an
5781 * offset of 4 from the beginning of
5784 * For data frames, DA is at an offset
5785 * of 4 from the beginning of the packet
5786 * if To DS is clear and at an offset of
5787 * 16 from the beginning of the packet
5792 * Generate the tests to be done for data frames.
5794 * First, check for To DS set, i.e. "link[1] & 0x01".
5796 s = gen_load_a(OR_LINK, 1, BPF_B);
5797 b1 = new_block(JMP(BPF_JSET));
5798 b1->s.k = 0x01; /* To DS */
5802 * If To DS is set, the DA is at 16.
5804 b0 = gen_mac_multicast(16);
5808 * Now, check for To DS not set, i.e. check
5809 * "!(link[1] & 0x01)".
5811 s = gen_load_a(OR_LINK, 1, BPF_B);
5812 b2 = new_block(JMP(BPF_JSET));
5813 b2->s.k = 0x01; /* To DS */
5818 * If To DS is not set, the DA is at 4.
5820 b1 = gen_mac_multicast(4);
5824 * Now OR together the last two checks. That gives
5825 * the complete set of checks for data frames.
5830 * Now check for a data frame.
5831 * I.e, check "link[0] & 0x08".
5833 s = gen_load_a(OR_LINK, 0, BPF_B);
5834 b1 = new_block(JMP(BPF_JSET));
5839 * AND that with the checks done for data frames.
5844 * If the high-order bit of the type value is 0, this
5845 * is a management frame.
5846 * I.e, check "!(link[0] & 0x08)".
5848 s = gen_load_a(OR_LINK, 0, BPF_B);
5849 b2 = new_block(JMP(BPF_JSET));
5855 * For management frames, the DA is at 4.
5857 b1 = gen_mac_multicast(4);
5861 * OR that with the checks done for data frames.
5862 * That gives the checks done for management and
5868 * If the low-order bit of the type value is 1,
5869 * this is either a control frame or a frame
5870 * with a reserved type, and thus not a
5873 * I.e., check "!(link[0] & 0x04)".
5875 s = gen_load_a(OR_LINK, 0, BPF_B);
5876 b1 = new_block(JMP(BPF_JSET));
5882 * AND that with the checks for data and management
5889 if (linktype == DLT_IP_OVER_FC) {
5890 b0 = gen_mac_multicast(2);
5894 if (linktype == DLT_SUNATM && is_lane) {
5896 * Check that the packet doesn't begin with an
5897 * LE Control marker. (We've already generated
5900 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
5904 /* ether[off_mac] & 1 != 0 */
5905 b0 = gen_mac_multicast(off_mac);
5910 /* Link not known to support multicasts */
5914 b0 = gen_linktype(ETHERTYPE_IP);
5915 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
5921 b0 = gen_linktype(ETHERTYPE_IPV6);
5922 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
5927 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5932 * generate command for inbound/outbound. It's here so we can
5933 * make it link-type specific. 'dir' = 0 implies "inbound",
5934 * = 1 implies "outbound".
5940 register struct block *b0;
5943 * Only some data link types support inbound/outbound qualifiers.
5947 b0 = gen_relation(BPF_JEQ,
5948 gen_load(Q_LINK, gen_loadi(0), 1),
5956 * Match packets sent by this machine.
5958 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
5961 * Match packets sent to this machine.
5962 * (No broadcast or multicast packets, or
5963 * packets sent to some other machine and
5964 * received promiscuously.)
5966 * XXX - packets sent to other machines probably
5967 * shouldn't be matched, but what about broadcast
5968 * or multicast packets we received?
5970 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_HOST);
5975 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
5976 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
5981 /* match outgoing packets */
5982 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
5984 /* match incoming packets */
5985 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
5989 case DLT_JUNIPER_MLFR:
5990 case DLT_JUNIPER_MLPPP:
5991 case DLT_JUNIPER_ATM1:
5992 case DLT_JUNIPER_ATM2:
5993 case DLT_JUNIPER_PPPOE:
5994 case DLT_JUNIPER_PPPOE_ATM:
5995 case DLT_JUNIPER_GGSN:
5996 case DLT_JUNIPER_ES:
5997 case DLT_JUNIPER_MONITOR:
5998 case DLT_JUNIPER_SERVICES:
5999 /* juniper flags (including direction) are stored
6000 * the byte after the 3-byte magic number */
6002 /* match outgoing packets */
6003 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
6005 /* match incoming packets */
6006 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
6011 bpf_error("inbound/outbound not supported on linktype %d",
6019 /* PF firewall log matched interface */
6021 gen_pf_ifname(const char *ifname)
6026 if (linktype == DLT_PFLOG) {
6027 len = sizeof(((struct pfloghdr *)0)->ifname);
6028 off = offsetof(struct pfloghdr, ifname);
6030 bpf_error("ifname not supported on linktype 0x%x", linktype);
6033 if (strlen(ifname) >= len) {
6034 bpf_error("ifname interface names can only be %d characters",
6038 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
6042 /* PF firewall log matched interface */
6044 gen_pf_ruleset(char *ruleset)
6048 if (linktype != DLT_PFLOG) {
6049 bpf_error("ruleset not supported on linktype 0x%x", linktype);
6052 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
6053 bpf_error("ruleset names can only be %ld characters",
6054 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
6057 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
6058 strlen(ruleset), (const u_char *)ruleset);
6062 /* PF firewall log rule number */
6068 if (linktype == DLT_PFLOG) {
6069 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
6072 bpf_error("rnr not supported on linktype 0x%x", linktype);
6079 /* PF firewall log sub-rule number */
6081 gen_pf_srnr(int srnr)
6085 if (linktype != DLT_PFLOG) {
6086 bpf_error("srnr not supported on linktype 0x%x", linktype);
6090 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
6095 /* PF firewall log reason code */
6097 gen_pf_reason(int reason)
6101 if (linktype == DLT_PFLOG) {
6102 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
6105 bpf_error("reason not supported on linktype 0x%x", linktype);
6112 /* PF firewall log action */
6114 gen_pf_action(int action)
6118 if (linktype == DLT_PFLOG) {
6119 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
6122 bpf_error("action not supported on linktype 0x%x", linktype);
6131 register const u_char *eaddr;
6134 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6135 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
6136 return gen_ahostop(eaddr, (int)q.dir);
6138 bpf_error("ARCnet address used in non-arc expression");
6142 static struct block *
6143 gen_ahostop(eaddr, dir)
6144 register const u_char *eaddr;
6147 register struct block *b0, *b1;
6150 /* src comes first, different from Ethernet */
6152 return gen_bcmp(OR_LINK, 0, 1, eaddr);
6155 return gen_bcmp(OR_LINK, 1, 1, eaddr);
6158 b0 = gen_ahostop(eaddr, Q_SRC);
6159 b1 = gen_ahostop(eaddr, Q_DST);
6165 b0 = gen_ahostop(eaddr, Q_SRC);
6166 b1 = gen_ahostop(eaddr, Q_DST);
6175 * support IEEE 802.1Q VLAN trunk over ethernet
6184 * Change the offsets to point to the type and data fields within
6185 * the VLAN packet. Just increment the offsets, so that we
6186 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6187 * capture VLAN 200 encapsulated within VLAN 100.
6189 * XXX - this is a bit of a kludge. If we were to split the
6190 * compiler into a parser that parses an expression and
6191 * generates an expression tree, and a code generator that
6192 * takes an expression tree (which could come from our
6193 * parser or from some other parser) and generates BPF code,
6194 * we could perhaps make the offsets parameters of routines
6195 * and, in the handler for an "AND" node, pass to subnodes
6196 * other than the VLAN node the adjusted offsets.
6198 * This would mean that "vlan" would, instead of changing the
6199 * behavior of *all* tests after it, change only the behavior
6200 * of tests ANDed with it. That would change the documented
6201 * semantics of "vlan", which might break some expressions.
6202 * However, it would mean that "(vlan and ip) or ip" would check
6203 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6204 * checking only for VLAN-encapsulated IP, so that could still
6205 * be considered worth doing; it wouldn't break expressions
6206 * that are of the form "vlan and ..." or "vlan N and ...",
6207 * which I suspect are the most common expressions involving
6208 * "vlan". "vlan or ..." doesn't necessarily do what the user
6209 * would really want, now, as all the "or ..." tests would
6210 * be done assuming a VLAN, even though the "or" could be viewed
6211 * as meaning "or, if this isn't a VLAN packet...".
6213 orig_linktype = off_linktype; /* save original values */
6225 bpf_error("no VLAN support for data link type %d",
6230 /* check for VLAN */
6231 b0 = gen_cmp(OR_LINK, orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q);
6233 /* If a specific VLAN is requested, check VLAN id */
6234 if (vlan_num >= 0) {
6237 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_H, (bpf_int32)vlan_num,
6256 * Change the offsets to point to the type and data fields within
6257 * the MPLS packet. Just increment the offsets, so that we
6258 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6259 * capture packets with an outer label of 100000 and an inner
6262 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6264 orig_linktype = off_linktype; /* save original values */
6269 case DLT_C_HDLC: /* fall through */
6274 b0 = gen_cmp(OR_LINK, orig_linktype, BPF_H,
6275 (bpf_int32)ETHERTYPE_MPLS);
6282 b0 = gen_cmp(OR_LINK, orig_linktype, BPF_H,
6283 (bpf_int32)PPP_MPLS_UCAST);
6286 /* FIXME add other DLT_s ...
6287 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6288 * leave it for now */
6291 bpf_error("no MPLS support for data link type %d",
6298 /* If a specific MPLS label is requested, check it */
6299 if (label_num >= 0) {
6302 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
6303 b1 = gen_mcmp(OR_LINK, orig_nl, BPF_W, (bpf_int32)label_num,
6304 0xfffff000); /* only compare the first 20 bits */
6313 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
6325 bpf_error("'vpi' supported only on raw ATM");
6326 if (off_vpi == (u_int)-1)
6328 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
6334 bpf_error("'vci' supported only on raw ATM");
6335 if (off_vci == (u_int)-1)
6337 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
6342 if (off_proto == (u_int)-1)
6343 abort(); /* XXX - this isn't on FreeBSD */
6344 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
6349 if (off_payload == (u_int)-1)
6351 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
6352 0xffffffff, jtype, reverse, jvalue);
6357 bpf_error("'callref' supported only on raw ATM");
6358 if (off_proto == (u_int)-1)
6360 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
6361 jtype, reverse, jvalue);
6371 gen_atmtype_abbrev(type)
6374 struct block *b0, *b1;
6379 /* Get all packets in Meta signalling Circuit */
6381 bpf_error("'metac' supported only on raw ATM");
6382 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6383 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
6388 /* Get all packets in Broadcast Circuit*/
6390 bpf_error("'bcc' supported only on raw ATM");
6391 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6392 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
6397 /* Get all cells in Segment OAM F4 circuit*/
6399 bpf_error("'oam4sc' supported only on raw ATM");
6400 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6401 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6406 /* Get all cells in End-to-End OAM F4 Circuit*/
6408 bpf_error("'oam4ec' supported only on raw ATM");
6409 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6410 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6415 /* Get all packets in connection Signalling Circuit */
6417 bpf_error("'sc' supported only on raw ATM");
6418 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6419 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
6424 /* Get all packets in ILMI Circuit */
6426 bpf_error("'ilmic' supported only on raw ATM");
6427 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6428 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
6433 /* Get all LANE packets */
6435 bpf_error("'lane' supported only on raw ATM");
6436 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
6439 * Arrange that all subsequent tests assume LANE
6440 * rather than LLC-encapsulated packets, and set
6441 * the offsets appropriately for LANE-encapsulated
6444 * "off_mac" is the offset of the Ethernet header,
6445 * which is 2 bytes past the ATM pseudo-header
6446 * (skipping the pseudo-header and 2-byte LE Client
6447 * field). The other offsets are Ethernet offsets
6448 * relative to "off_mac".
6451 off_mac = off_payload + 2; /* MAC header */
6452 off_linktype = off_mac + 12;
6453 off_nl = off_mac + 14; /* Ethernet II */
6454 off_nl_nosnap = off_mac + 17; /* 802.3+802.2 */
6458 /* Get all LLC-encapsulated packets */
6460 bpf_error("'llc' supported only on raw ATM");
6461 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
6472 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
6479 bpf_u_int32 val1 , val2 , val3;
6481 switch (mtp3field) {
6484 if (off_sio == (u_int)-1)
6485 bpf_error("'sio' supported only on SS7");
6486 /* sio coded on 1 byte so max value 255 */
6488 bpf_error("sio value %u too big; max value = 255",
6490 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
6491 (u_int)jtype, reverse, (u_int)jvalue);
6495 if (off_opc == (u_int)-1)
6496 bpf_error("'opc' supported only on SS7");
6497 /* opc coded on 14 bits so max value 16383 */
6499 bpf_error("opc value %u too big; max value = 16383",
6501 /* the following instructions are made to convert jvalue
6502 * to the form used to write opc in an ss7 message*/
6503 val1 = jvalue & 0x00003c00;
6505 val2 = jvalue & 0x000003fc;
6507 val3 = jvalue & 0x00000003;
6509 jvalue = val1 + val2 + val3;
6510 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
6511 (u_int)jtype, reverse, (u_int)jvalue);
6515 if (off_dpc == (u_int)-1)
6516 bpf_error("'dpc' supported only on SS7");
6517 /* dpc coded on 14 bits so max value 16383 */
6519 bpf_error("dpc value %u too big; max value = 16383",
6521 /* the following instructions are made to convert jvalue
6522 * to the forme used to write dpc in an ss7 message*/
6523 val1 = jvalue & 0x000000ff;
6525 val2 = jvalue & 0x00003f00;
6527 jvalue = val1 + val2;
6528 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
6529 (u_int)jtype, reverse, (u_int)jvalue);
6533 if (off_sls == (u_int)-1)
6534 bpf_error("'sls' supported only on SS7");
6535 /* sls coded on 4 bits so max value 15 */
6537 bpf_error("sls value %u too big; max value = 15",
6539 /* the following instruction is made to convert jvalue
6540 * to the forme used to write sls in an ss7 message*/
6541 jvalue = jvalue << 4;
6542 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
6543 (u_int)jtype,reverse, (u_int)jvalue);
6552 static struct block *
6553 gen_msg_abbrev(type)
6559 * Q.2931 signalling protocol messages for handling virtual circuits
6560 * establishment and teardown
6565 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
6569 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
6573 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
6577 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
6581 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
6584 case A_RELEASE_DONE:
6585 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
6595 gen_atmmulti_abbrev(type)
6598 struct block *b0, *b1;
6604 bpf_error("'oam' supported only on raw ATM");
6605 b1 = gen_atmmulti_abbrev(A_OAMF4);
6610 bpf_error("'oamf4' supported only on raw ATM");
6612 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
6613 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
6615 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
6621 * Get Q.2931 signalling messages for switched
6622 * virtual connection
6625 bpf_error("'connectmsg' supported only on raw ATM");
6626 b0 = gen_msg_abbrev(A_SETUP);
6627 b1 = gen_msg_abbrev(A_CALLPROCEED);
6629 b0 = gen_msg_abbrev(A_CONNECT);
6631 b0 = gen_msg_abbrev(A_CONNECTACK);
6633 b0 = gen_msg_abbrev(A_RELEASE);
6635 b0 = gen_msg_abbrev(A_RELEASE_DONE);
6637 b0 = gen_atmtype_abbrev(A_SC);
6643 bpf_error("'metaconnect' supported only on raw ATM");
6644 b0 = gen_msg_abbrev(A_SETUP);
6645 b1 = gen_msg_abbrev(A_CALLPROCEED);
6647 b0 = gen_msg_abbrev(A_CONNECT);
6649 b0 = gen_msg_abbrev(A_RELEASE);
6651 b0 = gen_msg_abbrev(A_RELEASE_DONE);
6653 b0 = gen_atmtype_abbrev(A_METAC);