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[] =
26 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.140.2.1 2001/01/14 06:48:35 guy Exp $ (LBL)";
33 #include <sys/types.h>
34 #include <sys/socket.h>
37 #include <sys/param.h>
44 #include <netinet/in.h>
54 #include "ethertype.h"
59 #include <pcap-namedb.h>
62 #include <sys/socket.h>
65 #define LLC_SNAP_LSAP 0xaa
66 #define LLC_ISO_LSAP 0xfe
71 #ifdef HAVE_OS_PROTO_H
75 #define JMP(c) ((c)|BPF_JMP|BPF_K)
78 static jmp_buf top_ctx;
79 static pcap_t *bpf_pcap;
83 int pcap_fddipad = PCAP_FDDIPAD;
90 bpf_error(const char *fmt, ...)
97 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
104 static void init_linktype(int);
106 static int alloc_reg(void);
107 static void free_reg(int);
109 static struct block *root;
112 * We divy out chunks of memory rather than call malloc each time so
113 * we don't have to worry about leaking memory. It's probably
114 * not a big deal if all this memory was wasted but it this ever
115 * goes into a library that would probably not be a good idea.
118 #define CHUNK0SIZE 1024
124 static struct chunk chunks[NCHUNKS];
125 static int cur_chunk;
127 static void *newchunk(u_int);
128 static void freechunks(void);
129 static inline struct block *new_block(int);
130 static inline struct slist *new_stmt(int);
131 static struct block *gen_retblk(int);
132 static inline void syntax(void);
134 static void backpatch(struct block *, struct block *);
135 static void merge(struct block *, struct block *);
136 static struct block *gen_cmp(u_int, u_int, bpf_int32);
137 static struct block *gen_cmp_gt(u_int, u_int, bpf_int32);
138 static struct block *gen_mcmp(u_int, u_int, bpf_int32, bpf_u_int32);
139 static struct block *gen_bcmp(u_int, u_int, const u_char *);
140 static struct block *gen_uncond(int);
141 static inline struct block *gen_true(void);
142 static inline struct block *gen_false(void);
143 static struct block *gen_linktype(int);
144 static struct block *gen_snap(bpf_u_int32, bpf_u_int32, u_int);
145 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
147 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
149 static struct block *gen_ehostop(const u_char *, int);
150 static struct block *gen_fhostop(const u_char *, int);
151 static struct block *gen_thostop(const u_char *, int);
152 static struct block *gen_dnhostop(bpf_u_int32, int, u_int);
153 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int);
155 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int);
158 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
160 static struct block *gen_ipfrag(void);
161 static struct block *gen_portatom(int, bpf_int32);
163 static struct block *gen_portatom6(int, bpf_int32);
165 struct block *gen_portop(int, int, int);
166 static struct block *gen_port(int, int, int);
168 struct block *gen_portop6(int, int, int);
169 static struct block *gen_port6(int, int, int);
171 static int lookup_proto(const char *, int);
172 static struct block *gen_protochain(int, int, int);
173 static struct block *gen_proto(int, int, int);
174 static struct slist *xfer_to_x(struct arth *);
175 static struct slist *xfer_to_a(struct arth *);
176 static struct block *gen_len(int, int);
186 /* XXX Round up to nearest long. */
187 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
189 /* XXX Round up to structure boundary. */
193 cp = &chunks[cur_chunk];
194 if (n > cp->n_left) {
195 ++cp, k = ++cur_chunk;
197 bpf_error("out of memory");
198 size = CHUNK0SIZE << k;
199 cp->m = (void *)malloc(size);
200 memset((char *)cp->m, 0, size);
203 bpf_error("out of memory");
206 return (void *)((char *)cp->m + cp->n_left);
215 for (i = 0; i < NCHUNKS; ++i)
216 if (chunks[i].m != NULL) {
223 * A strdup whose allocations are freed after code generation is over.
227 register const char *s;
229 int n = strlen(s) + 1;
230 char *cp = newchunk(n);
236 static inline struct block *
242 p = (struct block *)newchunk(sizeof(*p));
249 static inline struct slist *
255 p = (struct slist *)newchunk(sizeof(*p));
261 static struct block *
265 struct block *b = new_block(BPF_RET|BPF_K);
274 bpf_error("syntax error in filter expression");
277 static bpf_u_int32 netmask;
282 pcap_compile(pcap_t *p, struct bpf_program *program,
283 char *buf, int optimize, bpf_u_int32 mask)
292 if (setjmp(top_ctx)) {
300 snaplen = pcap_snapshot(p);
302 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
303 "snaplen of 0 rejects all packets");
307 lex_init(buf ? buf : "");
308 init_linktype(pcap_datalink(p));
315 root = gen_retblk(snaplen);
317 if (optimize && !no_optimize) {
320 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
321 bpf_error("expression rejects all packets");
323 program->bf_insns = icode_to_fcode(root, &len);
324 program->bf_len = len;
332 * entry point for using the compiler with no pcap open
333 * pass in all the stuff that is needed explicitly instead.
336 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
337 struct bpf_program *program,
338 char *buf, int optimize, bpf_u_int32 mask)
343 p = pcap_open_dead(linktype_arg, snaplen_arg);
346 ret = pcap_compile(p, program, buf, optimize, mask);
352 * Clean up a "struct bpf_program" by freeing all the memory allocated
356 pcap_freecode(struct bpf_program *program)
359 if (program->bf_insns != NULL) {
360 free((char *)program->bf_insns);
361 program->bf_insns = NULL;
366 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
367 * which of the jt and jf fields has been resolved and which is a pointer
368 * back to another unresolved block (or nil). At least one of the fields
369 * in each block is already resolved.
372 backpatch(list, target)
373 struct block *list, *target;
390 * Merge the lists in b0 and b1, using the 'sense' field to indicate
391 * which of jt and jf is the link.
395 struct block *b0, *b1;
397 register struct block **p = &b0;
399 /* Find end of list. */
401 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
403 /* Concatenate the lists. */
411 backpatch(p, gen_retblk(snaplen));
412 p->sense = !p->sense;
413 backpatch(p, gen_retblk(0));
419 struct block *b0, *b1;
421 backpatch(b0, b1->head);
422 b0->sense = !b0->sense;
423 b1->sense = !b1->sense;
425 b1->sense = !b1->sense;
431 struct block *b0, *b1;
433 b0->sense = !b0->sense;
434 backpatch(b0, b1->head);
435 b0->sense = !b0->sense;
444 b->sense = !b->sense;
447 static struct block *
448 gen_cmp(offset, size, v)
455 s = new_stmt(BPF_LD|BPF_ABS|size);
458 b = new_block(JMP(BPF_JEQ));
465 static struct block *
466 gen_cmp_gt(offset, size, v)
473 s = new_stmt(BPF_LD|BPF_ABS|size);
476 b = new_block(JMP(BPF_JGT));
483 static struct block *
484 gen_mcmp(offset, size, v, mask)
489 struct block *b = gen_cmp(offset, size, v);
492 if (mask != 0xffffffff) {
493 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
500 static struct block *
501 gen_bcmp(offset, size, v)
502 register u_int offset, size;
503 register const u_char *v;
505 register struct block *b, *tmp;
509 register const u_char *p = &v[size - 4];
510 bpf_int32 w = ((bpf_int32)p[0] << 24) |
511 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
513 tmp = gen_cmp(offset + size - 4, BPF_W, w);
520 register const u_char *p = &v[size - 2];
521 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
523 tmp = gen_cmp(offset + size - 2, BPF_H, w);
530 tmp = gen_cmp(offset, BPF_B, (bpf_int32)v[0]);
539 * Various code constructs need to know the layout of the data link
540 * layer. These variables give the necessary offsets. off_linktype
541 * is set to -1 for no encapsulation, in which case, IP is assumed.
543 static u_int off_linktype;
562 * SLIP doesn't have a link level type. The 16 byte
563 * header is hacked into our SLIP driver.
570 /* XXX this may be the same as the DLT_PPP_BSDOS case */
596 * FDDI doesn't really have a link-level type field.
597 * We assume that SSAP = SNAP is being used and pick
598 * out the encapsulated Ethernet type.
600 * XXX - should we generate code to check for SNAP?
604 off_linktype += pcap_fddipad;
608 off_nl += pcap_fddipad;
614 * Token Ring doesn't really have a link-level type field.
615 * We assume that SSAP = SNAP is being used and pick
616 * out the encapsulated Ethernet type.
618 * XXX - should we generate code to check for SNAP?
620 * XXX - the header is actually variable-length.
621 * Some various Linux patched versions gave 38
622 * as "off_linktype" and 40 as "off_nl"; however,
623 * if a token ring packet has *no* routing
624 * information, i.e. is not source-routed, the correct
625 * values are 20 and 22, as they are in the vanilla code.
627 * A packet is source-routed iff the uppermost bit
628 * of the first byte of the source address, at an
629 * offset of 8, has the uppermost bit set. If the
630 * packet is source-routed, the total number of bytes
631 * of routing information is 2 plus bits 0x1F00 of
632 * the 16-bit value at an offset of 14 (shifted right
633 * 8 - figure out which byte that is).
639 case DLT_ATM_RFC1483:
641 * assume routed, non-ISO PDUs
642 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
653 case DLT_ATM_CLIP: /* Linux ATM defines this */
658 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
663 bpf_error("unknown data link type %d", linktype);
667 static struct block *
674 s = new_stmt(BPF_LD|BPF_IMM);
676 b = new_block(JMP(BPF_JEQ));
682 static inline struct block *
685 return gen_uncond(1);
688 static inline struct block *
691 return gen_uncond(0);
695 * Byte-swap a 32-bit number.
696 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
697 * big-endian platforms.)
699 #define SWAPLONG(y) \
700 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
702 static struct block *
706 struct block *b0, *b1;
708 /* If we're not using encapsulation, we're done */
709 if (off_linktype == -1)
716 * XXX - handle other LLC-encapsulated protocols here
723 * OSI protocols always use 802.2 encapsulation.
725 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU);
727 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)
728 ((LLC_ISO_LSAP << 8) | LLC_ISO_LSAP));
732 case ETHERTYPE_ATALK:
735 * EtherTalk (AppleTalk protocols on Ethernet link
736 * layer) may use 802.2 encapsulation.
740 * Check for 802.2 encapsulation (EtherTalk phase 2?);
741 * we check for an Ethernet type field less than
742 * 1500, which means it's an 802.3 length field.
744 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU);
748 * 802.2-encapsulated ETHERTYPE_ATALK packets are
749 * SNAP packets with an organization code of
750 * 0x080007 (Apple, for Appletalk) and a protocol
751 * type of ETHERTYPE_ATALK (Appletalk).
753 * 802.2-encapsulated ETHERTYPE_AARP packets are
754 * SNAP packets with an organization code of
755 * 0x000000 (encapsulated Ethernet) and a protocol
756 * type of ETHERTYPE_AARP (Appletalk ARP).
758 if (proto == ETHERTYPE_ATALK)
759 b1 = gen_snap(0x080007, ETHERTYPE_ATALK, 14);
760 else /* proto == ETHERTYPE_AARP */
761 b1 = gen_snap(0x000000, ETHERTYPE_AARP, 14);
765 * Check for Ethernet encapsulation (Ethertalk
766 * phase 1?); we just check for the Ethernet
769 b0 = gen_cmp(off_linktype, BPF_H, (bpf_int32)proto);
782 * We use Ethernet protocol types inside libpcap;
783 * map them to the corresponding PPP protocol types.
788 proto = PPP_IP; /* XXX was 0x21 */
801 case ETHERTYPE_ATALK:
817 * We use Ethernet protocol types inside libpcap;
818 * map them to the corresponding PPP protocol types.
823 b0 = gen_cmp(off_linktype, BPF_H, PPP_IP);
824 b1 = gen_cmp(off_linktype, BPF_H, PPP_VJC);
826 b0 = gen_cmp(off_linktype, BPF_H, PPP_VJNC);
841 case ETHERTYPE_ATALK:
858 * For DLT_NULL, the link-layer header is a 32-bit
859 * word containing an AF_ value in *host* byte order.
861 * In addition, if we're reading a saved capture file,
862 * the host byte order in the capture may not be the
863 * same as the host byte order on this machine.
865 * For DLT_LOOP, the link-layer header is a 32-bit
866 * word containing an AF_ value in *network* byte order.
868 * XXX - AF_ values may, unfortunately, be platform-
869 * dependent; for example, FreeBSD's AF_INET6 is 24
870 * whilst NetBSD's and OpenBSD's is 26.
872 * This means that, when reading a capture file, just
873 * checking for our AF_INET6 value won't work if the
874 * capture file came from another OS.
890 * Not a type on which we support filtering.
891 * XXX - support those that have AF_ values
892 * #defined on this platform, at least?
897 if (linktype == DLT_NULL) {
899 * The AF_ value is in host byte order, but
900 * the BPF interpreter will convert it to
901 * network byte order.
903 * If this is a save file, and it's from a
904 * machine with the opposite byte order to
905 * ours, we byte-swap the AF_ value.
907 * Then we run it through "htonl()", and
908 * generate code to compare against the result.
910 if (bpf_pcap->sf.rfile != NULL &&
911 bpf_pcap->sf.swapped)
912 proto = SWAPLONG(proto);
913 proto = htonl(proto);
915 return (gen_cmp(0, BPF_W, (bpf_int32)proto));
917 return gen_cmp(off_linktype, BPF_H, (bpf_int32)proto);
921 * Check for an LLC SNAP packet with a given organization code and
922 * protocol type; we check the entire contents of the 802.2 LLC and
923 * snap headers, checking for DSAP and SSAP of SNAP and a control
924 * field of 0x03 in the LLC header, and for the specified organization
925 * code and protocol type in the SNAP header.
927 static struct block *
928 gen_snap(orgcode, ptype, offset)
935 snapblock[0] = LLC_SNAP_LSAP; /* DSAP = SNAP */
936 snapblock[1] = LLC_SNAP_LSAP; /* SSAP = SNAP */
937 snapblock[2] = 0x03; /* control = UI */
938 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
939 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
940 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
941 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
942 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
943 return gen_bcmp(offset, 8, snapblock);
946 static struct block *
947 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
951 u_int src_off, dst_off;
953 struct block *b0, *b1;
967 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
968 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
974 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
975 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
982 b0 = gen_linktype(proto);
983 b1 = gen_mcmp(offset, BPF_W, (bpf_int32)addr, mask);
989 static struct block *
990 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
991 struct in6_addr *addr;
992 struct in6_addr *mask;
994 u_int src_off, dst_off;
996 struct block *b0, *b1;
1011 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
1012 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
1018 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
1019 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
1026 /* this order is important */
1027 a = (u_int32_t *)addr;
1028 m = (u_int32_t *)mask;
1029 b1 = gen_mcmp(offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
1030 b0 = gen_mcmp(offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
1032 b0 = gen_mcmp(offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
1034 b0 = gen_mcmp(offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
1036 b0 = gen_linktype(proto);
1042 static struct block *
1043 gen_ehostop(eaddr, dir)
1044 register const u_char *eaddr;
1047 register struct block *b0, *b1;
1051 return gen_bcmp(6, 6, eaddr);
1054 return gen_bcmp(0, 6, eaddr);
1057 b0 = gen_ehostop(eaddr, Q_SRC);
1058 b1 = gen_ehostop(eaddr, Q_DST);
1064 b0 = gen_ehostop(eaddr, Q_SRC);
1065 b1 = gen_ehostop(eaddr, Q_DST);
1074 * Like gen_ehostop, but for DLT_FDDI
1076 static struct block *
1077 gen_fhostop(eaddr, dir)
1078 register const u_char *eaddr;
1081 struct block *b0, *b1;
1086 return gen_bcmp(6 + 1 + pcap_fddipad, 6, eaddr);
1088 return gen_bcmp(6 + 1, 6, eaddr);
1093 return gen_bcmp(0 + 1 + pcap_fddipad, 6, eaddr);
1095 return gen_bcmp(0 + 1, 6, eaddr);
1099 b0 = gen_fhostop(eaddr, Q_SRC);
1100 b1 = gen_fhostop(eaddr, Q_DST);
1106 b0 = gen_fhostop(eaddr, Q_SRC);
1107 b1 = gen_fhostop(eaddr, Q_DST);
1116 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1118 static struct block *
1119 gen_thostop(eaddr, dir)
1120 register const u_char *eaddr;
1123 register struct block *b0, *b1;
1127 return gen_bcmp(8, 6, eaddr);
1130 return gen_bcmp(2, 6, eaddr);
1133 b0 = gen_thostop(eaddr, Q_SRC);
1134 b1 = gen_thostop(eaddr, Q_DST);
1140 b0 = gen_thostop(eaddr, Q_SRC);
1141 b1 = gen_thostop(eaddr, Q_DST);
1150 * This is quite tricky because there may be pad bytes in front of the
1151 * DECNET header, and then there are two possible data packet formats that
1152 * carry both src and dst addresses, plus 5 packet types in a format that
1153 * carries only the src node, plus 2 types that use a different format and
1154 * also carry just the src node.
1158 * Instead of doing those all right, we just look for data packets with
1159 * 0 or 1 bytes of padding. If you want to look at other packets, that
1160 * will require a lot more hacking.
1162 * To add support for filtering on DECNET "areas" (network numbers)
1163 * one would want to add a "mask" argument to this routine. That would
1164 * make the filter even more inefficient, although one could be clever
1165 * and not generate masking instructions if the mask is 0xFFFF.
1167 static struct block *
1168 gen_dnhostop(addr, dir, base_off)
1173 struct block *b0, *b1, *b2, *tmp;
1174 u_int offset_lh; /* offset if long header is received */
1175 u_int offset_sh; /* offset if short header is received */
1180 offset_sh = 1; /* follows flags */
1181 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
1185 offset_sh = 3; /* follows flags, dstnode */
1186 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1190 /* Inefficient because we do our Calvinball dance twice */
1191 b0 = gen_dnhostop(addr, Q_SRC, base_off);
1192 b1 = gen_dnhostop(addr, Q_DST, base_off);
1198 /* Inefficient because we do our Calvinball dance twice */
1199 b0 = gen_dnhostop(addr, Q_SRC, base_off);
1200 b1 = gen_dnhostop(addr, Q_DST, base_off);
1205 bpf_error("ISO host filtering not implemented");
1210 b0 = gen_linktype(ETHERTYPE_DN);
1211 /* Check for pad = 1, long header case */
1212 tmp = gen_mcmp(base_off + 2, BPF_H,
1213 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
1214 b1 = gen_cmp(base_off + 2 + 1 + offset_lh,
1215 BPF_H, (bpf_int32)ntohs(addr));
1217 /* Check for pad = 0, long header case */
1218 tmp = gen_mcmp(base_off + 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
1219 b2 = gen_cmp(base_off + 2 + offset_lh, BPF_H, (bpf_int32)ntohs(addr));
1222 /* Check for pad = 1, short header case */
1223 tmp = gen_mcmp(base_off + 2, BPF_H,
1224 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
1225 b2 = gen_cmp(base_off + 2 + 1 + offset_sh,
1226 BPF_H, (bpf_int32)ntohs(addr));
1229 /* Check for pad = 0, short header case */
1230 tmp = gen_mcmp(base_off + 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
1231 b2 = gen_cmp(base_off + 2 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
1235 /* Combine with test for linktype */
1240 static struct block *
1241 gen_host(addr, mask, proto, dir)
1247 struct block *b0, *b1;
1252 b0 = gen_host(addr, mask, Q_IP, dir);
1253 if (off_linktype != -1) {
1254 b1 = gen_host(addr, mask, Q_ARP, dir);
1256 b0 = gen_host(addr, mask, Q_RARP, dir);
1262 return gen_hostop(addr, mask, dir, ETHERTYPE_IP,
1263 off_nl + 12, off_nl + 16);
1266 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP,
1267 off_nl + 14, off_nl + 24);
1270 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP,
1271 off_nl + 14, off_nl + 24);
1274 bpf_error("'tcp' modifier applied to host");
1277 bpf_error("'udp' modifier applied to host");
1280 bpf_error("'icmp' modifier applied to host");
1283 bpf_error("'igmp' modifier applied to host");
1286 bpf_error("'igrp' modifier applied to host");
1289 bpf_error("'pim' modifier applied to host");
1292 bpf_error("ATALK host filtering not implemented");
1295 bpf_error("AARP host filtering not implemented");
1298 return gen_dnhostop(addr, dir, off_nl);
1301 bpf_error("SCA host filtering not implemented");
1304 bpf_error("LAT host filtering not implemented");
1307 bpf_error("MOPDL host filtering not implemented");
1310 bpf_error("MOPRC host filtering not implemented");
1314 bpf_error("'ip6' modifier applied to ip host");
1317 bpf_error("'icmp6' modifier applied to host");
1321 bpf_error("'ah' modifier applied to host");
1324 bpf_error("'esp' modifier applied to host");
1327 bpf_error("ISO host filtering not implemented");
1330 bpf_error("'esis' modifier applied to host");
1333 bpf_error("'isis' modifier applied to host");
1336 bpf_error("'clnp' modifier applied to host");
1345 static struct block *
1346 gen_host6(addr, mask, proto, dir)
1347 struct in6_addr *addr;
1348 struct in6_addr *mask;
1355 return gen_host6(addr, mask, Q_IPV6, dir);
1358 bpf_error("'ip' modifier applied to ip6 host");
1361 bpf_error("'rarp' modifier applied to ip6 host");
1364 bpf_error("'arp' modifier applied to ip6 host");
1367 bpf_error("'tcp' modifier applied to host");
1370 bpf_error("'udp' modifier applied to host");
1373 bpf_error("'icmp' modifier applied to host");
1376 bpf_error("'igmp' modifier applied to host");
1379 bpf_error("'igrp' modifier applied to host");
1382 bpf_error("'pim' modifier applied to host");
1385 bpf_error("ATALK host filtering not implemented");
1388 bpf_error("AARP host filtering not implemented");
1391 bpf_error("'decnet' modifier applied to ip6 host");
1394 bpf_error("SCA host filtering not implemented");
1397 bpf_error("LAT host filtering not implemented");
1400 bpf_error("MOPDL host filtering not implemented");
1403 bpf_error("MOPRC host filtering not implemented");
1406 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6,
1407 off_nl + 8, off_nl + 24);
1410 bpf_error("'icmp6' modifier applied to host");
1413 bpf_error("'ah' modifier applied to host");
1416 bpf_error("'esp' modifier applied to host");
1419 bpf_error("ISO host filtering not implemented");
1422 bpf_error("'esis' modifier applied to host");
1425 bpf_error("'isis' modifier applied to host");
1428 bpf_error("'clnp' modifier applied to host");
1438 static struct block *
1439 gen_gateway(eaddr, alist, proto, dir)
1440 const u_char *eaddr;
1441 bpf_u_int32 **alist;
1445 struct block *b0, *b1, *tmp;
1448 bpf_error("direction applied to 'gateway'");
1455 if (linktype == DLT_EN10MB)
1456 b0 = gen_ehostop(eaddr, Q_OR);
1457 else if (linktype == DLT_FDDI)
1458 b0 = gen_fhostop(eaddr, Q_OR);
1459 else if (linktype == DLT_IEEE802)
1460 b0 = gen_thostop(eaddr, Q_OR);
1463 "'gateway' supported only on ethernet, FDDI or token ring");
1465 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR);
1467 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR);
1475 bpf_error("illegal modifier of 'gateway'");
1481 gen_proto_abbrev(proto)
1492 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
1494 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
1500 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
1502 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
1508 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
1511 #ifndef IPPROTO_IGMP
1512 #define IPPROTO_IGMP 2
1516 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
1519 #ifndef IPPROTO_IGRP
1520 #define IPPROTO_IGRP 9
1523 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
1527 #define IPPROTO_PIM 103
1531 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
1533 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
1539 b1 = gen_linktype(ETHERTYPE_IP);
1543 b1 = gen_linktype(ETHERTYPE_ARP);
1547 b1 = gen_linktype(ETHERTYPE_REVARP);
1551 bpf_error("link layer applied in wrong context");
1554 b1 = gen_linktype(ETHERTYPE_ATALK);
1558 b1 = gen_linktype(ETHERTYPE_AARP);
1562 b1 = gen_linktype(ETHERTYPE_DN);
1566 b1 = gen_linktype(ETHERTYPE_SCA);
1570 b1 = gen_linktype(ETHERTYPE_LAT);
1574 b1 = gen_linktype(ETHERTYPE_MOPDL);
1578 b1 = gen_linktype(ETHERTYPE_MOPRC);
1583 b1 = gen_linktype(ETHERTYPE_IPV6);
1586 #ifndef IPPROTO_ICMPV6
1587 #define IPPROTO_ICMPV6 58
1590 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
1595 #define IPPROTO_AH 51
1598 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
1600 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
1606 #define IPPROTO_ESP 50
1609 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
1611 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
1617 b1 = gen_linktype(LLC_ISO_LSAP);
1621 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
1625 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
1629 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
1638 static struct block *
1645 s = new_stmt(BPF_LD|BPF_H|BPF_ABS);
1646 s->s.k = off_nl + 6;
1647 b = new_block(JMP(BPF_JSET));
1655 static struct block *
1656 gen_portatom(off, v)
1663 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1666 s->next = new_stmt(BPF_LD|BPF_IND|BPF_H);
1667 s->next->s.k = off_nl + off;
1669 b = new_block(JMP(BPF_JEQ));
1677 static struct block *
1678 gen_portatom6(off, v)
1682 return gen_cmp(off_nl + 40 + off, BPF_H, v);
1687 gen_portop(port, proto, dir)
1688 int port, proto, dir;
1690 struct block *b0, *b1, *tmp;
1692 /* ip proto 'proto' */
1693 tmp = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)proto);
1699 b1 = gen_portatom(0, (bpf_int32)port);
1703 b1 = gen_portatom(2, (bpf_int32)port);
1708 tmp = gen_portatom(0, (bpf_int32)port);
1709 b1 = gen_portatom(2, (bpf_int32)port);
1714 tmp = gen_portatom(0, (bpf_int32)port);
1715 b1 = gen_portatom(2, (bpf_int32)port);
1727 static struct block *
1728 gen_port(port, ip_proto, dir)
1733 struct block *b0, *b1, *tmp;
1735 /* ether proto ip */
1736 b0 = gen_linktype(ETHERTYPE_IP);
1741 b1 = gen_portop(port, ip_proto, dir);
1745 tmp = gen_portop(port, IPPROTO_TCP, dir);
1746 b1 = gen_portop(port, IPPROTO_UDP, dir);
1759 gen_portop6(port, proto, dir)
1760 int port, proto, dir;
1762 struct block *b0, *b1, *tmp;
1764 /* ip proto 'proto' */
1765 b0 = gen_cmp(off_nl + 6, BPF_B, (bpf_int32)proto);
1769 b1 = gen_portatom6(0, (bpf_int32)port);
1773 b1 = gen_portatom6(2, (bpf_int32)port);
1778 tmp = gen_portatom6(0, (bpf_int32)port);
1779 b1 = gen_portatom6(2, (bpf_int32)port);
1784 tmp = gen_portatom6(0, (bpf_int32)port);
1785 b1 = gen_portatom6(2, (bpf_int32)port);
1797 static struct block *
1798 gen_port6(port, ip_proto, dir)
1803 struct block *b0, *b1, *tmp;
1805 /* ether proto ip */
1806 b0 = gen_linktype(ETHERTYPE_IPV6);
1811 b1 = gen_portop6(port, ip_proto, dir);
1815 tmp = gen_portop6(port, IPPROTO_TCP, dir);
1816 b1 = gen_portop6(port, IPPROTO_UDP, dir);
1829 lookup_proto(name, proto)
1830 register const char *name;
1839 v = pcap_nametoproto(name);
1840 if (v == PROTO_UNDEF)
1841 bpf_error("unknown ip proto '%s'", name);
1845 /* XXX should look up h/w protocol type based on linktype */
1846 v = pcap_nametoeproto(name);
1847 if (v == PROTO_UNDEF)
1848 bpf_error("unknown ether proto '%s'", name);
1852 if (strcmp(name, "esis") == 0)
1854 else if (strcmp(name, "isis") == 0)
1856 else if (strcmp(name, "clnp") == 0)
1859 bpf_error("unknown osi proto '%s'", name);
1879 static struct block *
1880 gen_protochain(v, proto, dir)
1885 #ifdef NO_PROTOCHAIN
1886 return gen_proto(v, proto, dir);
1888 struct block *b0, *b;
1889 struct slist *s[100];
1890 int fix2, fix3, fix4, fix5;
1891 int ahcheck, again, end;
1893 int reg2 = alloc_reg();
1895 memset(s, 0, sizeof(s));
1896 fix2 = fix3 = fix4 = fix5 = 0;
1903 b0 = gen_protochain(v, Q_IP, dir);
1904 b = gen_protochain(v, Q_IPV6, dir);
1908 bpf_error("bad protocol applied for 'protochain'");
1912 no_optimize = 1; /*this code is not compatible with optimzer yet */
1915 * s[0] is a dummy entry to protect other BPF insn from damaged
1916 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1917 * hard to find interdependency made by jump table fixup.
1920 s[i] = new_stmt(0); /*dummy*/
1925 b0 = gen_linktype(ETHERTYPE_IP);
1928 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
1929 s[i]->s.k = off_nl + 9;
1931 /* X = ip->ip_hl << 2 */
1932 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1938 b0 = gen_linktype(ETHERTYPE_IPV6);
1940 /* A = ip6->ip_nxt */
1941 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
1942 s[i]->s.k = off_nl + 6;
1944 /* X = sizeof(struct ip6_hdr) */
1945 s[i] = new_stmt(BPF_LDX|BPF_IMM);
1951 bpf_error("unsupported proto to gen_protochain");
1955 /* again: if (A == v) goto end; else fall through; */
1957 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1959 s[i]->s.jt = NULL; /*later*/
1960 s[i]->s.jf = NULL; /*update in next stmt*/
1964 #ifndef IPPROTO_NONE
1965 #define IPPROTO_NONE 59
1967 /* if (A == IPPROTO_NONE) goto end */
1968 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1969 s[i]->s.jt = NULL; /*later*/
1970 s[i]->s.jf = NULL; /*update in next stmt*/
1971 s[i]->s.k = IPPROTO_NONE;
1972 s[fix5]->s.jf = s[i];
1977 if (proto == Q_IPV6) {
1978 int v6start, v6end, v6advance, j;
1981 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1982 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1983 s[i]->s.jt = NULL; /*later*/
1984 s[i]->s.jf = NULL; /*update in next stmt*/
1985 s[i]->s.k = IPPROTO_HOPOPTS;
1986 s[fix2]->s.jf = s[i];
1988 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1989 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1990 s[i]->s.jt = NULL; /*later*/
1991 s[i]->s.jf = NULL; /*update in next stmt*/
1992 s[i]->s.k = IPPROTO_DSTOPTS;
1994 /* if (A == IPPROTO_ROUTING) goto v6advance */
1995 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1996 s[i]->s.jt = NULL; /*later*/
1997 s[i]->s.jf = NULL; /*update in next stmt*/
1998 s[i]->s.k = IPPROTO_ROUTING;
2000 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
2001 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
2002 s[i]->s.jt = NULL; /*later*/
2003 s[i]->s.jf = NULL; /*later*/
2004 s[i]->s.k = IPPROTO_FRAGMENT;
2015 * X = X + (P[X + 1] + 1) * 8;
2018 s[i] = new_stmt(BPF_MISC|BPF_TXA);
2020 /* A = P[X + packet head] */
2021 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
2025 s[i] = new_stmt(BPF_ST);
2029 s[i] = new_stmt(BPF_MISC|BPF_TXA);
2032 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2036 s[i] = new_stmt(BPF_MISC|BPF_TAX);
2038 /* A = P[X + packet head]; */
2039 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
2043 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2047 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
2051 s[i] = new_stmt(BPF_MISC|BPF_TAX);
2054 s[i] = new_stmt(BPF_LD|BPF_MEM);
2058 /* goto again; (must use BPF_JA for backward jump) */
2059 s[i] = new_stmt(BPF_JMP|BPF_JA);
2060 s[i]->s.k = again - i - 1;
2061 s[i - 1]->s.jf = s[i];
2065 for (j = v6start; j <= v6end; j++)
2066 s[j]->s.jt = s[v6advance];
2071 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2073 s[fix2]->s.jf = s[i];
2079 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2080 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
2081 s[i]->s.jt = NULL; /*later*/
2082 s[i]->s.jf = NULL; /*later*/
2083 s[i]->s.k = IPPROTO_AH;
2085 s[fix3]->s.jf = s[ahcheck];
2092 * X = X + (P[X + 1] + 2) * 4;
2095 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
2097 /* A = P[X + packet head]; */
2098 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
2102 s[i] = new_stmt(BPF_ST);
2106 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
2109 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2113 s[i] = new_stmt(BPF_MISC|BPF_TAX);
2115 /* A = P[X + packet head] */
2116 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
2120 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2124 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
2128 s[i] = new_stmt(BPF_MISC|BPF_TAX);
2131 s[i] = new_stmt(BPF_LD|BPF_MEM);
2135 /* goto again; (must use BPF_JA for backward jump) */
2136 s[i] = new_stmt(BPF_JMP|BPF_JA);
2137 s[i]->s.k = again - i - 1;
2142 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2144 s[fix2]->s.jt = s[end];
2145 s[fix4]->s.jf = s[end];
2146 s[fix5]->s.jt = s[end];
2153 for (i = 0; i < max - 1; i++)
2154 s[i]->next = s[i + 1];
2155 s[max - 1]->next = NULL;
2160 b = new_block(JMP(BPF_JEQ));
2161 b->stmts = s[1]; /*remember, s[0] is dummy*/
2171 static struct block *
2172 gen_proto(v, proto, dir)
2177 struct block *b0, *b1;
2179 if (dir != Q_DEFAULT)
2180 bpf_error("direction applied to 'proto'");
2185 b0 = gen_proto(v, Q_IP, dir);
2186 b1 = gen_proto(v, Q_IPV6, dir);
2193 b0 = gen_linktype(ETHERTYPE_IP);
2195 b1 = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)v);
2197 b1 = gen_protochain(v, Q_IP);
2203 b0 = gen_linktype(LLC_ISO_LSAP);
2204 b1 = gen_cmp(off_nl + 3, BPF_B, (long)v);
2209 bpf_error("arp does not encapsulate another protocol");
2213 bpf_error("rarp does not encapsulate another protocol");
2217 bpf_error("atalk encapsulation is not specifiable");
2221 bpf_error("decnet encapsulation is not specifiable");
2225 bpf_error("sca does not encapsulate another protocol");
2229 bpf_error("lat does not encapsulate another protocol");
2233 bpf_error("moprc does not encapsulate another protocol");
2237 bpf_error("mopdl does not encapsulate another protocol");
2241 return gen_linktype(v);
2244 bpf_error("'udp proto' is bogus");
2248 bpf_error("'tcp proto' is bogus");
2252 bpf_error("'icmp proto' is bogus");
2256 bpf_error("'igmp proto' is bogus");
2260 bpf_error("'igrp proto' is bogus");
2264 bpf_error("'pim proto' is bogus");
2269 b0 = gen_linktype(ETHERTYPE_IPV6);
2271 b1 = gen_cmp(off_nl + 6, BPF_B, (bpf_int32)v);
2273 b1 = gen_protochain(v, Q_IPV6);
2279 bpf_error("'icmp6 proto' is bogus");
2283 bpf_error("'ah proto' is bogus");
2286 bpf_error("'ah proto' is bogus");
2297 register const char *name;
2300 int proto = q.proto;
2304 bpf_u_int32 mask, addr;
2306 bpf_u_int32 **alist;
2309 struct sockaddr_in *sin;
2310 struct sockaddr_in6 *sin6;
2311 struct addrinfo *res, *res0;
2312 struct in6_addr mask128;
2314 struct block *b, *tmp;
2315 int port, real_proto;
2320 addr = pcap_nametonetaddr(name);
2322 bpf_error("unknown network '%s'", name);
2323 /* Left justify network addr and calculate its network mask */
2325 while (addr && (addr & 0xff000000) == 0) {
2329 return gen_host(addr, mask, proto, dir);
2333 if (proto == Q_LINK) {
2337 eaddr = pcap_ether_hostton(name);
2340 "unknown ether host '%s'", name);
2341 return gen_ehostop(eaddr, dir);
2344 eaddr = pcap_ether_hostton(name);
2347 "unknown FDDI host '%s'", name);
2348 return gen_fhostop(eaddr, dir);
2351 eaddr = pcap_ether_hostton(name);
2354 "unknown token ring host '%s'", name);
2355 return gen_thostop(eaddr, dir);
2359 "only ethernet/FDDI/token ring supports link-level host name");
2362 } else if (proto == Q_DECNET) {
2363 unsigned short dn_addr = __pcap_nametodnaddr(name);
2365 * I don't think DECNET hosts can be multihomed, so
2366 * there is no need to build up a list of addresses
2368 return (gen_host(dn_addr, 0, proto, dir));
2371 alist = pcap_nametoaddr(name);
2372 if (alist == NULL || *alist == NULL)
2373 bpf_error("unknown host '%s'", name);
2375 if (off_linktype == -1 && tproto == Q_DEFAULT)
2377 b = gen_host(**alist++, 0xffffffff, tproto, dir);
2379 tmp = gen_host(**alist++, 0xffffffff,
2386 memset(&mask128, 0xff, sizeof(mask128));
2387 res0 = res = pcap_nametoaddrinfo(name);
2389 bpf_error("unknown host '%s'", name);
2391 tproto = tproto6 = proto;
2392 if (off_linktype == -1 && tproto == Q_DEFAULT) {
2396 for (res = res0; res; res = res->ai_next) {
2397 switch (res->ai_family) {
2399 if (tproto == Q_IPV6)
2402 sin = (struct sockaddr_in *)
2404 tmp = gen_host(ntohl(sin->sin_addr.s_addr),
2405 0xffffffff, tproto, dir);
2408 if (tproto6 == Q_IP)
2411 sin6 = (struct sockaddr_in6 *)
2413 tmp = gen_host6(&sin6->sin6_addr,
2414 &mask128, tproto6, dir);
2423 bpf_error("unknown host '%s'%s", name,
2424 (proto == Q_DEFAULT)
2426 : " for specified address family");
2433 if (proto != Q_DEFAULT && proto != Q_UDP && proto != Q_TCP)
2434 bpf_error("illegal qualifier of 'port'");
2435 if (pcap_nametoport(name, &port, &real_proto) == 0)
2436 bpf_error("unknown port '%s'", name);
2437 if (proto == Q_UDP) {
2438 if (real_proto == IPPROTO_TCP)
2439 bpf_error("port '%s' is tcp", name);
2441 /* override PROTO_UNDEF */
2442 real_proto = IPPROTO_UDP;
2444 if (proto == Q_TCP) {
2445 if (real_proto == IPPROTO_UDP)
2446 bpf_error("port '%s' is udp", name);
2448 /* override PROTO_UNDEF */
2449 real_proto = IPPROTO_TCP;
2452 return gen_port(port, real_proto, dir);
2456 b = gen_port(port, real_proto, dir);
2457 gen_or(gen_port6(port, real_proto, dir), b);
2464 eaddr = pcap_ether_hostton(name);
2466 bpf_error("unknown ether host: %s", name);
2468 alist = pcap_nametoaddr(name);
2469 if (alist == NULL || *alist == NULL)
2470 bpf_error("unknown host '%s'", name);
2471 return gen_gateway(eaddr, alist, proto, dir);
2473 bpf_error("'gateway' not supported in this configuration");
2477 real_proto = lookup_proto(name, proto);
2478 if (real_proto >= 0)
2479 return gen_proto(real_proto, proto, dir);
2481 bpf_error("unknown protocol: %s", name);
2484 real_proto = lookup_proto(name, proto);
2485 if (real_proto >= 0)
2486 return gen_protochain(real_proto, proto, dir);
2488 bpf_error("unknown protocol: %s", name);
2500 gen_mcode(s1, s2, masklen, q)
2501 register const char *s1, *s2;
2502 register int masklen;
2505 register int nlen, mlen;
2508 nlen = __pcap_atoin(s1, &n);
2509 /* Promote short ipaddr */
2513 mlen = __pcap_atoin(s2, &m);
2514 /* Promote short ipaddr */
2517 bpf_error("non-network bits set in \"%s mask %s\"",
2520 /* Convert mask len to mask */
2522 bpf_error("mask length must be <= 32");
2523 m = 0xffffffff << (32 - masklen);
2525 bpf_error("non-network bits set in \"%s/%d\"",
2532 return gen_host(n, m, q.proto, q.dir);
2535 bpf_error("Mask syntax for networks only");
2542 register const char *s;
2547 int proto = q.proto;
2553 else if (q.proto == Q_DECNET)
2554 vlen = __pcap_atodn(s, &v);
2556 vlen = __pcap_atoin(s, &v);
2563 if (proto == Q_DECNET)
2564 return gen_host(v, 0, proto, dir);
2565 else if (proto == Q_LINK) {
2566 bpf_error("illegal link layer address");
2569 if (s == NULL && q.addr == Q_NET) {
2570 /* Promote short net number */
2571 while (v && (v & 0xff000000) == 0) {
2576 /* Promote short ipaddr */
2580 return gen_host(v, mask, proto, dir);
2585 proto = IPPROTO_UDP;
2586 else if (proto == Q_TCP)
2587 proto = IPPROTO_TCP;
2588 else if (proto == Q_DEFAULT)
2589 proto = PROTO_UNDEF;
2591 bpf_error("illegal qualifier of 'port'");
2594 return gen_port((int)v, proto, dir);
2598 b = gen_port((int)v, proto, dir);
2599 gen_or(gen_port6((int)v, proto, dir), b);
2605 bpf_error("'gateway' requires a name");
2609 return gen_proto((int)v, proto, dir);
2612 return gen_protochain((int)v, proto, dir);
2627 gen_mcode6(s1, s2, masklen, q)
2628 register const char *s1, *s2;
2629 register int masklen;
2632 struct addrinfo *res;
2633 struct in6_addr *addr;
2634 struct in6_addr mask;
2639 bpf_error("no mask %s supported", s2);
2641 res = pcap_nametoaddrinfo(s1);
2643 bpf_error("invalid ip6 address %s", s1);
2645 bpf_error("%s resolved to multiple address", s1);
2646 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
2648 if (sizeof(mask) * 8 < masklen)
2649 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
2650 memset(&mask, 0xff, masklen / 8);
2652 mask.s6_addr[masklen / 8] =
2653 (0xff << (8 - masklen % 8)) & 0xff;
2656 a = (u_int32_t *)addr;
2657 m = (u_int32_t *)&mask;
2658 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
2659 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
2660 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
2668 bpf_error("Mask syntax for networks only");
2672 b = gen_host6(addr, &mask, q.proto, q.dir);
2677 bpf_error("invalid qualifier against IPv6 address");
2685 register const u_char *eaddr;
2688 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
2689 if (linktype == DLT_EN10MB)
2690 return gen_ehostop(eaddr, (int)q.dir);
2691 if (linktype == DLT_FDDI)
2692 return gen_fhostop(eaddr, (int)q.dir);
2693 if (linktype == DLT_IEEE802)
2694 return gen_thostop(eaddr, (int)q.dir);
2695 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
2697 bpf_error("ethernet address used in non-ether expression");
2703 struct slist *s0, *s1;
2706 * This is definitely not the best way to do this, but the
2707 * lists will rarely get long.
2714 static struct slist *
2720 s = new_stmt(BPF_LDX|BPF_MEM);
2725 static struct slist *
2731 s = new_stmt(BPF_LD|BPF_MEM);
2737 gen_load(proto, index, size)
2742 struct slist *s, *tmp;
2744 int regno = alloc_reg();
2746 free_reg(index->regno);
2750 bpf_error("data size must be 1, 2, or 4");
2766 bpf_error("unsupported index operation");
2769 s = xfer_to_x(index);
2770 tmp = new_stmt(BPF_LD|BPF_IND|size);
2772 sappend(index->s, s);
2787 /* XXX Note that we assume a fixed link header here. */
2788 s = xfer_to_x(index);
2789 tmp = new_stmt(BPF_LD|BPF_IND|size);
2792 sappend(index->s, s);
2794 b = gen_proto_abbrev(proto);
2796 gen_and(index->b, b);
2806 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
2808 sappend(s, xfer_to_a(index));
2809 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
2810 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
2811 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
2813 sappend(index->s, s);
2815 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
2817 gen_and(index->b, b);
2819 gen_and(gen_proto_abbrev(Q_IP), b);
2825 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2829 index->regno = regno;
2830 s = new_stmt(BPF_ST);
2832 sappend(index->s, s);
2838 gen_relation(code, a0, a1, reversed)
2840 struct arth *a0, *a1;
2843 struct slist *s0, *s1, *s2;
2844 struct block *b, *tmp;
2848 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
2849 b = new_block(JMP(code));
2850 if (code == BPF_JGT || code == BPF_JGE) {
2851 reversed = !reversed;
2852 b->s.k = 0x80000000;
2860 sappend(a0->s, a1->s);
2864 free_reg(a0->regno);
2865 free_reg(a1->regno);
2867 /* 'and' together protocol checks */
2870 gen_and(a0->b, tmp = a1->b);
2886 int regno = alloc_reg();
2887 struct arth *a = (struct arth *)newchunk(sizeof(*a));
2890 s = new_stmt(BPF_LD|BPF_LEN);
2891 s->next = new_stmt(BPF_ST);
2892 s->next->s.k = regno;
2907 a = (struct arth *)newchunk(sizeof(*a));
2911 s = new_stmt(BPF_LD|BPF_IMM);
2913 s->next = new_stmt(BPF_ST);
2929 s = new_stmt(BPF_ALU|BPF_NEG);
2932 s = new_stmt(BPF_ST);
2940 gen_arth(code, a0, a1)
2942 struct arth *a0, *a1;
2944 struct slist *s0, *s1, *s2;
2948 s2 = new_stmt(BPF_ALU|BPF_X|code);
2953 sappend(a0->s, a1->s);
2955 free_reg(a1->regno);
2957 s0 = new_stmt(BPF_ST);
2958 a0->regno = s0->s.k = alloc_reg();
2965 * Here we handle simple allocation of the scratch registers.
2966 * If too many registers are alloc'd, the allocator punts.
2968 static int regused[BPF_MEMWORDS];
2972 * Return the next free register.
2977 int n = BPF_MEMWORDS;
2980 if (regused[curreg])
2981 curreg = (curreg + 1) % BPF_MEMWORDS;
2983 regused[curreg] = 1;
2987 bpf_error("too many registers needed to evaluate expression");
2992 * Return a register to the table so it can
3002 static struct block *
3009 s = new_stmt(BPF_LD|BPF_LEN);
3010 b = new_block(JMP(jmp));
3021 return gen_len(BPF_JGE, n);
3025 * Actually, this is less than or equal.
3033 b = gen_len(BPF_JGT, n);
3040 gen_byteop(op, idx, val)
3051 return gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
3054 b = gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
3055 b->s.code = JMP(BPF_JGE);
3060 b = gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
3061 b->s.code = JMP(BPF_JGT);
3065 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
3069 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
3073 b = new_block(JMP(BPF_JEQ));
3081 gen_broadcast(proto)
3084 bpf_u_int32 hostmask;
3085 struct block *b0, *b1, *b2;
3086 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3092 if (linktype == DLT_EN10MB)
3093 return gen_ehostop(ebroadcast, Q_DST);
3094 if (linktype == DLT_FDDI)
3095 return gen_fhostop(ebroadcast, Q_DST);
3096 if (linktype == DLT_IEEE802)
3097 return gen_thostop(ebroadcast, Q_DST);
3098 bpf_error("not a broadcast link");
3102 b0 = gen_linktype(ETHERTYPE_IP);
3103 hostmask = ~netmask;
3104 b1 = gen_mcmp(off_nl + 16, BPF_W, (bpf_int32)0, hostmask);
3105 b2 = gen_mcmp(off_nl + 16, BPF_W,
3106 (bpf_int32)(~0 & hostmask), hostmask);
3111 bpf_error("only ether/ip broadcast filters supported");
3115 gen_multicast(proto)
3118 register struct block *b0, *b1;
3119 register struct slist *s;
3125 if (linktype == DLT_EN10MB) {
3126 /* ether[0] & 1 != 0 */
3127 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
3129 b0 = new_block(JMP(BPF_JSET));
3135 if (linktype == DLT_FDDI) {
3136 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3137 /* fddi[1] & 1 != 0 */
3138 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
3140 b0 = new_block(JMP(BPF_JSET));
3146 /* TODO - check how token ring handles multicast */
3147 /* if (linktype == DLT_IEEE802) ... */
3149 /* Link not known to support multicasts */
3153 b0 = gen_linktype(ETHERTYPE_IP);
3154 b1 = gen_cmp(off_nl + 16, BPF_B, (bpf_int32)224);
3155 b1->s.code = JMP(BPF_JGE);
3161 b0 = gen_linktype(ETHERTYPE_IPV6);
3162 b1 = gen_cmp(off_nl + 24, BPF_B, (bpf_int32)255);
3167 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3171 * generate command for inbound/outbound. It's here so we can
3172 * make it link-type specific. 'dir' = 0 implies "inbound",
3173 * = 1 implies "outbound".
3179 register struct block *b0;
3182 * Only some data link types support inbound/outbound qualifiers.
3187 b0 = gen_relation(BPF_JEQ,
3188 gen_load(Q_LINK, gen_loadi(0), 1),
3194 bpf_error("inbound/outbound not supported on linktype %d\n",
3203 * support IEEE 802.1Q VLAN trunk over ethernet
3209 static u_int orig_linktype = -1, orig_nl = -1;
3213 * Change the offsets to point to the type and data fields within
3214 * the VLAN packet. This is somewhat of a kludge.
3216 if (orig_nl == (u_int)-1) {
3217 orig_linktype = off_linktype; /* save original values */
3228 bpf_error("no VLAN support for data link type %d",
3234 /* check for VLAN */
3235 b0 = gen_cmp(orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q);
3237 /* If a specific VLAN is requested, check VLAN id */
3238 if (vlan_num >= 0) {
3241 b1 = gen_cmp(orig_nl, BPF_H, (bpf_int32)vlan_num);