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.100.2.1 2000/03/01 14:12:54 itojun Exp $ (LBL)";
29 #include <sys/types.h>
30 #include <sys/socket.h>
33 #include <sys/param.h>
42 #include <net/ethernet.h>
44 #include <netinet/in.h>
49 #include <net/if_llc.h>
58 #include "ethertype.h"
62 #include <pcap-namedb.h>
65 #include <sys/socket.h>
69 #ifdef HAVE_OS_PROTO_H
73 #define JMP(c) ((c)|BPF_JMP|BPF_K)
76 static jmp_buf top_ctx;
77 static pcap_t *bpf_pcap;
81 int pcap_fddipad = PCAP_FDDIPAD;
89 bpf_error(const char *fmt, ...)
91 bpf_error(fmt, va_alist)
103 if (bpf_pcap != NULL)
104 (void)vsprintf(pcap_geterr(bpf_pcap), fmt, ap);
110 static void init_linktype(int);
112 static int alloc_reg(void);
113 static void free_reg(int);
115 static struct block *root;
118 * We divy out chunks of memory rather than call malloc each time so
119 * we don't have to worry about leaking memory. It's probably
120 * not a big deal if all this memory was wasted but it this ever
121 * goes into a library that would probably not be a good idea.
124 #define CHUNK0SIZE 1024
130 static struct chunk chunks[NCHUNKS];
131 static int cur_chunk;
133 static void *newchunk(u_int);
134 static void freechunks(void);
135 static inline struct block *new_block(int);
136 static inline struct slist *new_stmt(int);
137 static struct block *gen_retblk(int);
138 static inline void syntax(void);
140 static void backpatch(struct block *, struct block *);
141 static void merge(struct block *, struct block *);
142 static struct block *gen_cmp(u_int, u_int, bpf_int32);
143 static struct block *gen_mcmp(u_int, u_int, bpf_int32, bpf_u_int32);
144 static struct block *gen_bcmp(u_int, u_int, const u_char *);
145 static struct block *gen_uncond(int);
146 static inline struct block *gen_true(void);
147 static inline struct block *gen_false(void);
148 static struct block *gen_linktype(int);
149 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
151 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
153 static struct block *gen_ehostop(const u_char *, int);
154 static struct block *gen_fhostop(const u_char *, int);
155 static struct block *gen_dnhostop(bpf_u_int32, int, u_int);
156 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int);
158 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int);
160 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
161 static struct block *gen_ipfrag(void);
162 static struct block *gen_portatom(int, bpf_int32);
164 static struct block *gen_portatom6(int, bpf_int32);
166 struct block *gen_portop(int, int, int);
167 static struct block *gen_port(int, int, int);
169 struct block *gen_portop6(int, int, int);
170 static struct block *gen_port6(int, int, int);
172 static int lookup_proto(const char *, 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)) {
298 snaplen = pcap_snapshot(p);
300 lex_init(buf ? buf : "");
301 init_linktype(pcap_datalink(p));
308 root = gen_retblk(snaplen);
310 if (optimize && !no_optimize) {
313 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
314 bpf_error("expression rejects all packets");
316 program->bf_insns = icode_to_fcode(root, &len);
317 program->bf_len = len;
324 * entry point for using the compiler with no pcap open
325 * pass in all the stuff that is needed explicitly instead.
328 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
329 struct bpf_program *program,
330 char *buf, int optimize, bpf_u_int32 mask)
338 if (setjmp(top_ctx)) {
345 /* XXX needed? I don't grok the use of globals here. */
346 snaplen = snaplen_arg;
348 lex_init(buf ? buf : "");
349 init_linktype(linktype_arg);
356 root = gen_retblk(snaplen_arg);
361 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
362 bpf_error("expression rejects all packets");
364 program->bf_insns = icode_to_fcode(root, &len);
365 program->bf_len = len;
372 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
373 * which of the jt and jf fields has been resolved and which is a pointer
374 * back to another unresolved block (or nil). At least one of the fields
375 * in each block is already resolved.
378 backpatch(list, target)
379 struct block *list, *target;
396 * Merge the lists in b0 and b1, using the 'sense' field to indicate
397 * which of jt and jf is the link.
401 struct block *b0, *b1;
403 register struct block **p = &b0;
405 /* Find end of list. */
407 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
409 /* Concatenate the lists. */
417 backpatch(p, gen_retblk(snaplen));
418 p->sense = !p->sense;
419 backpatch(p, gen_retblk(0));
425 struct block *b0, *b1;
427 backpatch(b0, b1->head);
428 b0->sense = !b0->sense;
429 b1->sense = !b1->sense;
431 b1->sense = !b1->sense;
437 struct block *b0, *b1;
439 b0->sense = !b0->sense;
440 backpatch(b0, b1->head);
441 b0->sense = !b0->sense;
450 b->sense = !b->sense;
453 static struct block *
454 gen_cmp(offset, size, v)
461 s = new_stmt(BPF_LD|BPF_ABS|size);
464 b = new_block(JMP(BPF_JEQ));
471 static struct block *
472 gen_mcmp(offset, size, v, mask)
477 struct block *b = gen_cmp(offset, size, v);
480 if (mask != 0xffffffff) {
481 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
488 static struct block *
489 gen_bcmp(offset, size, v)
490 register u_int offset, size;
491 register const u_char *v;
493 register struct block *b, *tmp;
497 register const u_char *p = &v[size - 4];
498 bpf_int32 w = ((bpf_int32)p[0] << 24) |
499 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
501 tmp = gen_cmp(offset + size - 4, BPF_W, w);
508 register const u_char *p = &v[size - 2];
509 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
511 tmp = gen_cmp(offset + size - 2, BPF_H, w);
518 tmp = gen_cmp(offset, BPF_B, (bpf_int32)v[0]);
527 * Various code constructs need to know the layout of the data link
528 * layer. These variables give the necessary offsets. off_linktype
529 * is set to -1 for no encapsulation, in which case, IP is assumed.
531 static u_int off_linktype;
550 * SLIP doesn't have a link level type. The 16 byte
551 * header is hacked into our SLIP driver.
558 /* XXX this may be the same as the DLT_PPP_BSDOS case */
584 * FDDI doesn't really have a link-level type field.
585 * We assume that SSAP = SNAP is being used and pick
586 * out the encapsulated Ethernet type.
590 off_linktype += pcap_fddipad;
594 off_nl += pcap_fddipad;
603 case DLT_ATM_RFC1483:
605 * assume routed, non-ISO PDUs
606 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
617 bpf_error("unknown data link type 0x%x", linktype);
621 static struct block *
628 s = new_stmt(BPF_LD|BPF_IMM);
630 b = new_block(JMP(BPF_JEQ));
636 static inline struct block *
639 return gen_uncond(1);
642 static inline struct block *
645 return gen_uncond(0);
648 static struct block *
652 struct block *b0, *b1;
654 /* If we're not using encapsulation and checking for IP, we're done */
655 if (off_linktype == -1 && proto == ETHERTYPE_IP)
664 if (proto == ETHERTYPE_IP)
665 proto = PPP_IP; /* XXX was 0x21 */
667 else if (proto == ETHERTYPE_IPV6)
676 b0 = gen_cmp(off_linktype, BPF_H, PPP_IP);
677 b1 = gen_cmp(off_linktype, BPF_H, PPP_VJC);
679 b0 = gen_cmp(off_linktype, BPF_H, PPP_VJNC);
694 case ETHERTYPE_ATALK:
706 if (proto == ETHERTYPE_IP)
707 return (gen_cmp(0, BPF_W, (bpf_int32)htonl(AF_INET)));
709 else if (proto == ETHERTYPE_IPV6)
710 return (gen_cmp(0, BPF_W, (bpf_int32)htonl(AF_INET6)));
716 * Having to look at SAP's here is quite disgusting,
717 * but given an internal architecture that _knows_ that
718 * it's looking at IP on Ethernet...
720 if (proto == LLC_ISO_LSAP) {
721 struct block *b0, *b1;
723 b0 = gen_cmp(off_linktype, BPF_H, (long)ETHERMTU);
724 b0->s.code = JMP(BPF_JGT);
726 b1 = gen_cmp(off_linktype + 2, BPF_H, (long)
727 ((LLC_ISO_LSAP << 8) | LLC_ISO_LSAP));
733 return gen_cmp(off_linktype, BPF_H, (bpf_int32)proto);
736 static struct block *
737 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
741 u_int src_off, dst_off;
743 struct block *b0, *b1;
757 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
758 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
764 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
765 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
772 b0 = gen_linktype(proto);
773 b1 = gen_mcmp(offset, BPF_W, (bpf_int32)addr, mask);
779 static struct block *
780 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
781 struct in6_addr *addr;
782 struct in6_addr *mask;
784 u_int src_off, dst_off;
786 struct block *b0, *b1;
801 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
802 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
808 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
809 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
816 /* this order is important */
817 a = (u_int32_t *)addr;
818 m = (u_int32_t *)mask;
819 b1 = gen_mcmp(offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
820 b0 = gen_mcmp(offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
822 b0 = gen_mcmp(offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
824 b0 = gen_mcmp(offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
826 b0 = gen_linktype(proto);
832 static struct block *
833 gen_ehostop(eaddr, dir)
834 register const u_char *eaddr;
837 register struct block *b0, *b1;
841 return gen_bcmp(6, 6, eaddr);
844 return gen_bcmp(0, 6, eaddr);
847 b0 = gen_ehostop(eaddr, Q_SRC);
848 b1 = gen_ehostop(eaddr, Q_DST);
854 b0 = gen_ehostop(eaddr, Q_SRC);
855 b1 = gen_ehostop(eaddr, Q_DST);
864 * Like gen_ehostop, but for DLT_FDDI
866 static struct block *
867 gen_fhostop(eaddr, dir)
868 register const u_char *eaddr;
871 struct block *b0, *b1;
876 return gen_bcmp(6 + 1 + pcap_fddipad, 6, eaddr);
878 return gen_bcmp(6 + 1, 6, eaddr);
883 return gen_bcmp(0 + 1 + pcap_fddipad, 6, eaddr);
885 return gen_bcmp(0 + 1, 6, eaddr);
889 b0 = gen_fhostop(eaddr, Q_SRC);
890 b1 = gen_fhostop(eaddr, Q_DST);
896 b0 = gen_fhostop(eaddr, Q_SRC);
897 b1 = gen_fhostop(eaddr, Q_DST);
906 * This is quite tricky because there may be pad bytes in front of the
907 * DECNET header, and then there are two possible data packet formats that
908 * carry both src and dst addresses, plus 5 packet types in a format that
909 * carries only the src node, plus 2 types that use a different format and
910 * also carry just the src node.
914 * Instead of doing those all right, we just look for data packets with
915 * 0 or 1 bytes of padding. If you want to look at other packets, that
916 * will require a lot more hacking.
918 * To add support for filtering on DECNET "areas" (network numbers)
919 * one would want to add a "mask" argument to this routine. That would
920 * make the filter even more inefficient, although one could be clever
921 * and not generate masking instructions if the mask is 0xFFFF.
923 static struct block *
924 gen_dnhostop(addr, dir, base_off)
929 struct block *b0, *b1, *b2, *tmp;
930 u_int offset_lh; /* offset if long header is received */
931 u_int offset_sh; /* offset if short header is received */
936 offset_sh = 1; /* follows flags */
937 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
941 offset_sh = 3; /* follows flags, dstnode */
942 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
946 /* Inefficient because we do our Calvinball dance twice */
947 b0 = gen_dnhostop(addr, Q_SRC, base_off);
948 b1 = gen_dnhostop(addr, Q_DST, base_off);
954 /* Inefficient because we do our Calvinball dance twice */
955 b0 = gen_dnhostop(addr, Q_SRC, base_off);
956 b1 = gen_dnhostop(addr, Q_DST, base_off);
963 b0 = gen_linktype(ETHERTYPE_DN);
964 /* Check for pad = 1, long header case */
965 tmp = gen_mcmp(base_off + 2, BPF_H,
966 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
967 b1 = gen_cmp(base_off + 2 + 1 + offset_lh,
968 BPF_H, (bpf_int32)ntohs(addr));
970 /* Check for pad = 0, long header case */
971 tmp = gen_mcmp(base_off + 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
972 b2 = gen_cmp(base_off + 2 + offset_lh, BPF_H, (bpf_int32)ntohs(addr));
975 /* Check for pad = 1, short header case */
976 tmp = gen_mcmp(base_off + 2, BPF_H,
977 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
978 b2 = gen_cmp(base_off + 2 + 1 + offset_sh,
979 BPF_H, (bpf_int32)ntohs(addr));
982 /* Check for pad = 0, short header case */
983 tmp = gen_mcmp(base_off + 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
984 b2 = gen_cmp(base_off + 2 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
988 /* Combine with test for linktype */
993 static struct block *
994 gen_host(addr, mask, proto, dir)
1000 struct block *b0, *b1;
1005 b0 = gen_host(addr, mask, Q_IP, dir);
1006 b1 = gen_host(addr, mask, Q_ARP, dir);
1008 b0 = gen_host(addr, mask, Q_RARP, dir);
1013 return gen_hostop(addr, mask, dir, ETHERTYPE_IP,
1014 off_nl + 12, off_nl + 16);
1017 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP,
1018 off_nl + 14, off_nl + 24);
1021 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP,
1022 off_nl + 14, off_nl + 24);
1025 bpf_error("'tcp' modifier applied to host");
1028 bpf_error("'udp' modifier applied to host");
1031 bpf_error("'icmp' modifier applied to host");
1034 bpf_error("'igmp' modifier applied to host");
1037 bpf_error("'igrp' modifier applied to host");
1040 bpf_error("'pim' modifier applied to host");
1043 bpf_error("ATALK host filtering not implemented");
1046 return gen_dnhostop(addr, dir, off_nl);
1049 bpf_error("SCA host filtering not implemented");
1052 bpf_error("LAT host filtering not implemented");
1055 bpf_error("MOPDL host filtering not implemented");
1058 bpf_error("MOPRC host filtering not implemented");
1061 bpf_error("ISO host filtering not implemented");
1065 bpf_error("'ip6' modifier applied to ip host");
1068 bpf_error("'icmp6' modifier applied to host");
1072 bpf_error("'ah' modifier applied to host");
1075 bpf_error("'esp' modifier applied to host");
1084 static struct block *
1085 gen_host6(addr, mask, proto, dir)
1086 struct in6_addr *addr;
1087 struct in6_addr *mask;
1091 struct block *b0, *b1;
1096 return gen_host6(addr, mask, Q_IPV6, dir);
1099 bpf_error("'ip' modifier applied to ip6 host");
1102 bpf_error("'rarp' modifier applied to ip6 host");
1105 bpf_error("'arp' modifier applied to ip6 host");
1108 bpf_error("'tcp' modifier applied to host");
1111 bpf_error("'udp' modifier applied to host");
1114 bpf_error("'icmp' modifier applied to host");
1117 bpf_error("'igmp' modifier applied to host");
1120 bpf_error("'igrp' modifier applied to host");
1123 bpf_error("'pim' modifier applied to host");
1126 bpf_error("ATALK host filtering not implemented");
1129 bpf_error("'decnet' modifier applied to ip6 host");
1132 bpf_error("SCA host filtering not implemented");
1135 bpf_error("LAT host filtering not implemented");
1138 bpf_error("MOPDL host filtering not implemented");
1141 bpf_error("MOPRC host filtering not implemented");
1144 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6,
1145 off_nl + 8, off_nl + 24);
1148 bpf_error("'icmp6' modifier applied to host");
1151 bpf_error("'ah' modifier applied to host");
1154 bpf_error("'esp' modifier applied to host");
1163 static struct block *
1164 gen_gateway(eaddr, alist, proto, dir)
1165 const u_char *eaddr;
1166 bpf_u_int32 **alist;
1170 struct block *b0, *b1, *tmp;
1173 bpf_error("direction applied to 'gateway'");
1180 if (linktype == DLT_EN10MB)
1181 b0 = gen_ehostop(eaddr, Q_OR);
1182 else if (linktype == DLT_FDDI)
1183 b0 = gen_fhostop(eaddr, Q_OR);
1186 "'gateway' supported only on ethernet or FDDI");
1188 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR);
1190 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR);
1198 bpf_error("illegal modifier of 'gateway'");
1203 gen_proto_abbrev(proto)
1206 struct block *b0, *b1;
1211 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
1213 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
1219 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
1221 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
1227 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
1231 b1 = gen_proto(2, Q_IP, Q_DEFAULT);
1234 #ifndef IPPROTO_IGRP
1235 #define IPPROTO_IGRP 9
1238 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
1242 #define IPPROTO_PIM 103
1246 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
1248 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
1254 b1 = gen_linktype(ETHERTYPE_IP);
1258 b1 = gen_linktype(ETHERTYPE_ARP);
1262 b1 = gen_linktype(ETHERTYPE_REVARP);
1266 bpf_error("link layer applied in wrong context");
1269 b1 = gen_linktype(ETHERTYPE_ATALK);
1273 b1 = gen_linktype(ETHERTYPE_DN);
1277 b1 = gen_linktype(ETHERTYPE_SCA);
1281 b1 = gen_linktype(ETHERTYPE_LAT);
1285 b1 = gen_linktype(ETHERTYPE_MOPDL);
1289 b1 = gen_linktype(ETHERTYPE_MOPRC);
1294 b1 = gen_linktype(ETHERTYPE_IPV6);
1297 #ifndef IPPROTO_ICMPV6
1298 #define IPPROTO_ICMPV6 58
1301 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
1306 #define IPPROTO_AH 51
1309 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
1311 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
1317 #define IPPROTO_ESP 50
1320 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
1322 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
1328 b1 = gen_linktype(LLC_ISO_LSAP);
1332 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
1336 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
1345 static struct block *
1352 s = new_stmt(BPF_LD|BPF_H|BPF_ABS);
1353 s->s.k = off_nl + 6;
1354 b = new_block(JMP(BPF_JSET));
1362 static struct block *
1363 gen_portatom(off, v)
1370 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1373 s->next = new_stmt(BPF_LD|BPF_IND|BPF_H);
1374 s->next->s.k = off_nl + off;
1376 b = new_block(JMP(BPF_JEQ));
1384 static struct block *
1385 gen_portatom6(off, v)
1389 return gen_cmp(off_nl + 40 + off, BPF_H, v);
1394 gen_portop(port, proto, dir)
1395 int port, proto, dir;
1397 struct block *b0, *b1, *tmp;
1399 /* ip proto 'proto' */
1400 tmp = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)proto);
1406 b1 = gen_portatom(0, (bpf_int32)port);
1410 b1 = gen_portatom(2, (bpf_int32)port);
1415 tmp = gen_portatom(0, (bpf_int32)port);
1416 b1 = gen_portatom(2, (bpf_int32)port);
1421 tmp = gen_portatom(0, (bpf_int32)port);
1422 b1 = gen_portatom(2, (bpf_int32)port);
1434 static struct block *
1435 gen_port(port, ip_proto, dir)
1440 struct block *b0, *b1, *tmp;
1442 /* ether proto ip */
1443 b0 = gen_linktype(ETHERTYPE_IP);
1448 b1 = gen_portop(port, ip_proto, dir);
1452 tmp = gen_portop(port, IPPROTO_TCP, dir);
1453 b1 = gen_portop(port, IPPROTO_UDP, dir);
1466 gen_portop6(port, proto, dir)
1467 int port, proto, dir;
1469 struct block *b0, *b1, *tmp;
1471 /* ip proto 'proto' */
1472 b0 = gen_cmp(off_nl + 6, BPF_B, (bpf_int32)proto);
1476 b1 = gen_portatom6(0, (bpf_int32)port);
1480 b1 = gen_portatom6(2, (bpf_int32)port);
1485 tmp = gen_portatom6(0, (bpf_int32)port);
1486 b1 = gen_portatom6(2, (bpf_int32)port);
1491 tmp = gen_portatom6(0, (bpf_int32)port);
1492 b1 = gen_portatom6(2, (bpf_int32)port);
1504 static struct block *
1505 gen_port6(port, ip_proto, dir)
1510 struct block *b0, *b1, *tmp;
1512 /* ether proto ip */
1513 b0 = gen_linktype(ETHERTYPE_IPV6);
1518 b1 = gen_portop6(port, ip_proto, dir);
1522 tmp = gen_portop6(port, IPPROTO_TCP, dir);
1523 b1 = gen_portop6(port, IPPROTO_UDP, dir);
1536 lookup_proto(name, proto)
1537 register const char *name;
1546 v = pcap_nametoproto(name);
1547 if (v == PROTO_UNDEF)
1548 bpf_error("unknown ip proto '%s'", name);
1552 /* XXX should look up h/w protocol type based on linktype */
1553 v = pcap_nametoeproto(name);
1554 if (v == PROTO_UNDEF)
1555 bpf_error("unknown ether proto '%s'", name);
1573 gen_protochain(v, proto, dir)
1578 #ifdef NO_PROTOCHAIN
1579 return gen_proto(v, proto, dir);
1581 struct block *b0, *b;
1582 struct slist *s[100], *sp;
1583 int fix2, fix3, fix4, fix5;
1584 int ahcheck, again, end;
1586 int reg1 = alloc_reg();
1587 int reg2 = alloc_reg();
1589 memset(s, 0, sizeof(s));
1590 fix2 = fix3 = fix4 = fix5 = 0;
1597 b0 = gen_protochain(v, Q_IP, dir);
1598 b = gen_protochain(v, Q_IPV6, dir);
1602 bpf_error("bad protocol applied for 'protochain'");
1606 no_optimize = 1; /*this code is not compatible with optimzer yet */
1609 * s[0] is a dummy entry to protect other BPF insn from damaged
1610 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1611 * hard to find interdependency made by jump table fixup.
1614 s[i] = new_stmt(0); /*dummy*/
1619 b0 = gen_linktype(ETHERTYPE_IP);
1622 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
1623 s[i]->s.k = off_nl + 9;
1625 /* X = ip->ip_hl << 2 */
1626 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1632 b0 = gen_linktype(ETHERTYPE_IPV6);
1634 /* A = ip6->ip_nxt */
1635 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
1636 s[i]->s.k = off_nl + 6;
1638 /* X = sizeof(struct ip6_hdr) */
1639 s[i] = new_stmt(BPF_LDX|BPF_IMM);
1645 bpf_error("unsupported proto to gen_protochain");
1649 /* again: if (A == v) goto end; else fall through; */
1651 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1653 s[i]->s.jt = NULL; /*later*/
1654 s[i]->s.jf = NULL; /*update in next stmt*/
1658 #ifndef IPPROTO_NONE
1659 #define IPPROTO_NONE 59
1661 /* if (A == IPPROTO_NONE) goto end */
1662 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1663 s[i]->s.jt = NULL; /*later*/
1664 s[i]->s.jf = NULL; /*update in next stmt*/
1665 s[i]->s.k = IPPROTO_NONE;
1666 s[fix5]->s.jf = s[i];
1671 if (proto == Q_IPV6) {
1672 int v6start, v6end, v6advance, j;
1675 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1676 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1677 s[i]->s.jt = NULL; /*later*/
1678 s[i]->s.jf = NULL; /*update in next stmt*/
1679 s[i]->s.k = IPPROTO_HOPOPTS;
1680 s[fix2]->s.jf = s[i];
1682 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1683 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1684 s[i]->s.jt = NULL; /*later*/
1685 s[i]->s.jf = NULL; /*update in next stmt*/
1686 s[i]->s.k = IPPROTO_DSTOPTS;
1688 /* if (A == IPPROTO_ROUTING) goto v6advance */
1689 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1690 s[i]->s.jt = NULL; /*later*/
1691 s[i]->s.jf = NULL; /*update in next stmt*/
1692 s[i]->s.k = IPPROTO_ROUTING;
1694 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1695 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1696 s[i]->s.jt = NULL; /*later*/
1697 s[i]->s.jf = NULL; /*later*/
1698 s[i]->s.k = IPPROTO_FRAGMENT;
1709 * X = X + (P[X] + 1) * 8;
1712 s[i] = new_stmt(BPF_MISC|BPF_TXA);
1715 s[i] = new_stmt(BPF_ST);
1719 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
1723 s[i] = new_stmt(BPF_MISC|BPF_TAX);
1725 /* A = P[X + packet head]; */
1726 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
1730 s[i] = new_stmt(BPF_ST);
1734 s[i] = new_stmt(BPF_LDX|BPF_MEM);
1737 /* A = P[X + packet head] */
1738 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
1742 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
1746 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
1750 s[i] = new_stmt(BPF_MISC|BPF_TAX);
1753 s[i] = new_stmt(BPF_LD|BPF_MEM);
1757 /* goto again; (must use BPF_JA for backward jump) */
1758 s[i] = new_stmt(BPF_JMP|BPF_JA);
1759 s[i]->s.k = again - i - 1;
1760 s[i - 1]->s.jf = s[i];
1764 for (j = v6start; j <= v6end; j++)
1765 s[j]->s.jt = s[v6advance];
1770 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
1772 s[fix2]->s.jf = s[i];
1778 /* if (A == IPPROTO_AH) then fall through; else goto end; */
1779 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1780 s[i]->s.jt = NULL; /*later*/
1781 s[i]->s.jf = NULL; /*later*/
1782 s[i]->s.k = IPPROTO_AH;
1784 s[fix3]->s.jf = s[ahcheck];
1791 * X = X + (P[X] + 2) * 4;
1794 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
1797 s[i] = new_stmt(BPF_ST);
1801 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
1805 s[i] = new_stmt(BPF_MISC|BPF_TAX);
1807 /* A = P[X + packet head]; */
1808 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
1812 s[i] = new_stmt(BPF_ST);
1816 s[i] = new_stmt(BPF_LDX|BPF_MEM);
1819 /* A = P[X + packet head] */
1820 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
1824 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
1828 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
1832 s[i] = new_stmt(BPF_MISC|BPF_TAX);
1835 s[i] = new_stmt(BPF_LD|BPF_MEM);
1839 /* goto again; (must use BPF_JA for backward jump) */
1840 s[i] = new_stmt(BPF_JMP|BPF_JA);
1841 s[i]->s.k = again - i - 1;
1846 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
1848 s[fix2]->s.jt = s[end];
1849 s[fix4]->s.jf = s[end];
1850 s[fix5]->s.jt = s[end];
1857 for (i = 0; i < max - 1; i++)
1858 s[i]->next = s[i + 1];
1859 s[max - 1]->next = NULL;
1864 b = new_block(JMP(BPF_JEQ));
1865 b->stmts = s[1]; /*remember, s[0] is dummy*/
1876 static struct block *
1877 gen_proto(v, proto, dir)
1882 struct block *b0, *b1;
1884 if (dir != Q_DEFAULT)
1885 bpf_error("direction applied to 'proto'");
1890 b0 = gen_proto(v, Q_IP, dir);
1891 b1 = gen_proto(v, Q_IPV6, dir);
1898 b0 = gen_linktype(ETHERTYPE_IP);
1900 b1 = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)v);
1902 b1 = gen_protochain(v, Q_IP);
1908 b0 = gen_linktype(LLC_ISO_LSAP);
1909 b1 = gen_cmp(off_nl + 3, BPF_B, (long)v);
1914 bpf_error("arp does not encapsulate another protocol");
1918 bpf_error("rarp does not encapsulate another protocol");
1922 bpf_error("atalk encapsulation is not specifiable");
1926 bpf_error("decnet encapsulation is not specifiable");
1930 bpf_error("sca does not encapsulate another protocol");
1934 bpf_error("lat does not encapsulate another protocol");
1938 bpf_error("moprc does not encapsulate another protocol");
1942 bpf_error("mopdl does not encapsulate another protocol");
1946 return gen_linktype(v);
1949 bpf_error("'udp proto' is bogus");
1953 bpf_error("'tcp proto' is bogus");
1957 bpf_error("'icmp proto' is bogus");
1961 bpf_error("'igmp proto' is bogus");
1965 bpf_error("'igrp proto' is bogus");
1969 bpf_error("'pim proto' is bogus");
1974 b0 = gen_linktype(ETHERTYPE_IPV6);
1976 b1 = gen_cmp(off_nl + 6, BPF_B, (bpf_int32)v);
1978 b1 = gen_protochain(v, Q_IPV6);
1984 bpf_error("'icmp6 proto' is bogus");
1988 bpf_error("'ah proto' is bogus");
1991 bpf_error("'ah proto' is bogus");
2002 register const char *name;
2005 int proto = q.proto;
2009 bpf_u_int32 mask, addr, **alist;
2012 struct sockaddr_in *sin;
2013 struct sockaddr_in6 *sin6;
2014 struct addrinfo *res, *res0;
2015 struct in6_addr mask128;
2017 struct block *b, *tmp;
2018 int port, real_proto;
2023 addr = pcap_nametonetaddr(name);
2025 bpf_error("unknown network '%s'", name);
2026 /* Left justify network addr and calculate its network mask */
2028 while (addr && (addr & 0xff000000) == 0) {
2032 return gen_host(addr, mask, proto, dir);
2036 if (proto == Q_LINK) {
2040 eaddr = pcap_ether_hostton(name);
2043 "unknown ether host '%s'", name);
2044 return gen_ehostop(eaddr, dir);
2047 eaddr = pcap_ether_hostton(name);
2050 "unknown FDDI host '%s'", name);
2051 return gen_fhostop(eaddr, dir);
2055 "only ethernet/FDDI supports link-level host name");
2058 } else if (proto == Q_DECNET) {
2059 unsigned short dn_addr = __pcap_nametodnaddr(name);
2061 * I don't think DECNET hosts can be multihomed, so
2062 * there is no need to build up a list of addresses
2064 return (gen_host(dn_addr, 0, proto, dir));
2067 alist = pcap_nametoaddr(name);
2068 if (alist == NULL || *alist == NULL)
2069 bpf_error("unknown host '%s'", name);
2071 if (off_linktype == -1 && tproto == Q_DEFAULT)
2073 b = gen_host(**alist++, 0xffffffff, tproto, dir);
2075 tmp = gen_host(**alist++, 0xffffffff,
2082 memset(&mask128, 0xff, sizeof(mask128));
2083 res0 = res = pcap_nametoaddr(name);
2085 bpf_error("unknown host '%s'", name);
2087 tproto = tproto6 = proto;
2088 if (off_linktype == -1 && tproto == Q_DEFAULT) {
2092 for (res = res0; res; res = res->ai_next) {
2093 switch (res->ai_family) {
2095 if (tproto == Q_IPV6)
2098 sin = (struct sockaddr_in *)
2100 tmp = gen_host(ntohl(sin->sin_addr.s_addr),
2101 0xffffffff, tproto, dir);
2104 if (tproto6 == Q_IP)
2107 sin6 = (struct sockaddr_in6 *)
2109 tmp = gen_host6(&sin6->sin6_addr,
2110 &mask128, tproto6, dir);
2119 bpf_error("unknown host '%s'%s", name,
2120 (proto == Q_DEFAULT)
2122 : " for specified address family");
2129 if (proto != Q_DEFAULT && proto != Q_UDP && proto != Q_TCP)
2130 bpf_error("illegal qualifier of 'port'");
2131 if (pcap_nametoport(name, &port, &real_proto) == 0)
2132 bpf_error("unknown port '%s'", name);
2133 if (proto == Q_UDP) {
2134 if (real_proto == IPPROTO_TCP)
2135 bpf_error("port '%s' is tcp", name);
2137 /* override PROTO_UNDEF */
2138 real_proto = IPPROTO_UDP;
2140 if (proto == Q_TCP) {
2141 if (real_proto == IPPROTO_UDP)
2142 bpf_error("port '%s' is udp", name);
2144 /* override PROTO_UNDEF */
2145 real_proto = IPPROTO_TCP;
2148 return gen_port(port, real_proto, dir);
2152 b = gen_port(port, real_proto, dir);
2153 gen_or(gen_port6(port, real_proto, dir), b);
2160 eaddr = pcap_ether_hostton(name);
2162 bpf_error("unknown ether host: %s", name);
2164 alist = pcap_nametoaddr(name);
2165 if (alist == NULL || *alist == NULL)
2166 bpf_error("unknown host '%s'", name);
2167 return gen_gateway(eaddr, alist, proto, dir);
2169 bpf_error("'gateway' not supported in this configuration");
2173 real_proto = lookup_proto(name, proto);
2174 if (real_proto >= 0)
2175 return gen_proto(real_proto, proto, dir);
2177 bpf_error("unknown protocol: %s", name);
2180 real_proto = lookup_proto(name, proto);
2181 if (real_proto >= 0)
2182 return gen_protochain(real_proto, proto, dir);
2184 bpf_error("unknown protocol: %s", name);
2196 gen_mcode(s1, s2, masklen, q)
2197 register const char *s1, *s2;
2198 register int masklen;
2201 register int nlen, mlen;
2204 nlen = __pcap_atoin(s1, &n);
2205 /* Promote short ipaddr */
2209 mlen = __pcap_atoin(s2, &m);
2210 /* Promote short ipaddr */
2213 bpf_error("non-network bits set in \"%s mask %s\"",
2216 /* Convert mask len to mask */
2218 bpf_error("mask length must be <= 32");
2219 m = 0xffffffff << (32 - masklen);
2221 bpf_error("non-network bits set in \"%s/%d\"",
2228 return gen_host(n, m, q.proto, q.dir);
2231 bpf_error("Mask syntax for networks only");
2238 register const char *s;
2243 int proto = q.proto;
2249 else if (q.proto == Q_DECNET)
2250 vlen = __pcap_atodn(s, &v);
2252 vlen = __pcap_atoin(s, &v);
2259 if (proto == Q_DECNET)
2260 return gen_host(v, 0, proto, dir);
2261 else if (proto == Q_LINK) {
2262 bpf_error("illegal link layer address");
2265 if (s == NULL && q.addr == Q_NET) {
2266 /* Promote short net number */
2267 while (v && (v & 0xff000000) == 0) {
2272 /* Promote short ipaddr */
2276 return gen_host(v, mask, proto, dir);
2281 proto = IPPROTO_UDP;
2282 else if (proto == Q_TCP)
2283 proto = IPPROTO_TCP;
2284 else if (proto == Q_DEFAULT)
2285 proto = PROTO_UNDEF;
2287 bpf_error("illegal qualifier of 'port'");
2290 return gen_port((int)v, proto, dir);
2294 b = gen_port((int)v, proto, dir);
2295 gen_or(gen_port6((int)v, proto, dir), b);
2301 bpf_error("'gateway' requires a name");
2305 return gen_proto((int)v, proto, dir);
2308 return gen_protochain((int)v, proto, dir);
2323 gen_mcode6(s1, s2, masklen, q)
2324 register const char *s1, *s2;
2325 register int masklen;
2328 struct addrinfo *res;
2329 struct in6_addr *addr;
2330 struct in6_addr mask;
2335 bpf_error("no mask %s supported", s2);
2337 res = pcap_nametoaddr(s1);
2339 bpf_error("invalid ip6 address %s", s1);
2341 bpf_error("%s resolved to multiple address", s1);
2342 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
2344 if (sizeof(mask) * 8 < masklen)
2345 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
2346 memset(&mask, 0xff, masklen / 8);
2348 mask.s6_addr[masklen / 8] =
2349 (0xff << (8 - masklen % 8)) & 0xff;
2352 a = (u_int32_t *)addr;
2353 m = (u_int32_t *)&mask;
2354 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
2355 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
2356 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
2364 bpf_error("Mask syntax for networks only");
2368 b = gen_host6(addr, &mask, q.proto, q.dir);
2373 bpf_error("invalid qualifier against IPv6 address");
2381 register const u_char *eaddr;
2384 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
2385 if (linktype == DLT_EN10MB)
2386 return gen_ehostop(eaddr, (int)q.dir);
2387 if (linktype == DLT_FDDI)
2388 return gen_fhostop(eaddr, (int)q.dir);
2390 bpf_error("ethernet address used in non-ether expression");
2396 struct slist *s0, *s1;
2399 * This is definitely not the best way to do this, but the
2400 * lists will rarely get long.
2407 static struct slist *
2413 s = new_stmt(BPF_LDX|BPF_MEM);
2418 static struct slist *
2424 s = new_stmt(BPF_LD|BPF_MEM);
2430 gen_load(proto, index, size)
2435 struct slist *s, *tmp;
2437 int regno = alloc_reg();
2439 free_reg(index->regno);
2443 bpf_error("data size must be 1, 2, or 4");
2459 bpf_error("unsupported index operation");
2462 s = xfer_to_x(index);
2463 tmp = new_stmt(BPF_LD|BPF_IND|size);
2465 sappend(index->s, s);
2480 /* XXX Note that we assume a fixed link header here. */
2481 s = xfer_to_x(index);
2482 tmp = new_stmt(BPF_LD|BPF_IND|size);
2485 sappend(index->s, s);
2487 b = gen_proto_abbrev(proto);
2489 gen_and(index->b, b);
2499 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
2501 sappend(s, xfer_to_a(index));
2502 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
2503 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
2504 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
2506 sappend(index->s, s);
2508 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
2510 gen_and(index->b, b);
2512 gen_and(gen_proto_abbrev(Q_IP), b);
2518 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2522 index->regno = regno;
2523 s = new_stmt(BPF_ST);
2525 sappend(index->s, s);
2531 gen_relation(code, a0, a1, reversed)
2533 struct arth *a0, *a1;
2536 struct slist *s0, *s1, *s2;
2537 struct block *b, *tmp;
2541 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
2542 b = new_block(JMP(code));
2543 if (code == BPF_JGT || code == BPF_JGE) {
2544 reversed = !reversed;
2545 b->s.k = 0x80000000;
2553 sappend(a0->s, a1->s);
2557 free_reg(a0->regno);
2558 free_reg(a1->regno);
2560 /* 'and' together protocol checks */
2563 gen_and(a0->b, tmp = a1->b);
2579 int regno = alloc_reg();
2580 struct arth *a = (struct arth *)newchunk(sizeof(*a));
2583 s = new_stmt(BPF_LD|BPF_LEN);
2584 s->next = new_stmt(BPF_ST);
2585 s->next->s.k = regno;
2600 a = (struct arth *)newchunk(sizeof(*a));
2604 s = new_stmt(BPF_LD|BPF_IMM);
2606 s->next = new_stmt(BPF_ST);
2622 s = new_stmt(BPF_ALU|BPF_NEG);
2625 s = new_stmt(BPF_ST);
2633 gen_arth(code, a0, a1)
2635 struct arth *a0, *a1;
2637 struct slist *s0, *s1, *s2;
2641 s2 = new_stmt(BPF_ALU|BPF_X|code);
2646 sappend(a0->s, a1->s);
2648 free_reg(a1->regno);
2650 s0 = new_stmt(BPF_ST);
2651 a0->regno = s0->s.k = alloc_reg();
2658 * Here we handle simple allocation of the scratch registers.
2659 * If too many registers are alloc'd, the allocator punts.
2661 static int regused[BPF_MEMWORDS];
2665 * Return the next free register.
2670 int n = BPF_MEMWORDS;
2673 if (regused[curreg])
2674 curreg = (curreg + 1) % BPF_MEMWORDS;
2676 regused[curreg] = 1;
2680 bpf_error("too many registers needed to evaluate expression");
2685 * Return a register to the table so it can
2695 static struct block *
2702 s = new_stmt(BPF_LD|BPF_LEN);
2703 b = new_block(JMP(jmp));
2714 return gen_len(BPF_JGE, n);
2718 * Actually, this is less than or equal.
2727 b = gen_len(BPF_JGT, n);
2734 gen_byteop(op, idx, val)
2745 return gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
2748 b = gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
2749 b->s.code = JMP(BPF_JGE);
2754 b = gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
2755 b->s.code = JMP(BPF_JGT);
2759 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
2763 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
2767 b = new_block(JMP(BPF_JEQ));
2775 gen_broadcast(proto)
2778 bpf_u_int32 hostmask;
2779 struct block *b0, *b1, *b2;
2780 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2786 if (linktype == DLT_EN10MB)
2787 return gen_ehostop(ebroadcast, Q_DST);
2788 if (linktype == DLT_FDDI)
2789 return gen_fhostop(ebroadcast, Q_DST);
2790 bpf_error("not a broadcast link");
2794 b0 = gen_linktype(ETHERTYPE_IP);
2795 hostmask = ~netmask;
2796 b1 = gen_mcmp(off_nl + 16, BPF_W, (bpf_int32)0, hostmask);
2797 b2 = gen_mcmp(off_nl + 16, BPF_W,
2798 (bpf_int32)(~0 & hostmask), hostmask);
2803 bpf_error("only ether/ip broadcast filters supported");
2807 gen_multicast(proto)
2810 register struct block *b0, *b1;
2811 register struct slist *s;
2817 if (linktype == DLT_EN10MB) {
2818 /* ether[0] & 1 != 0 */
2819 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2821 b0 = new_block(JMP(BPF_JSET));
2827 if (linktype == DLT_FDDI) {
2828 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
2829 /* fddi[1] & 1 != 0 */
2830 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2832 b0 = new_block(JMP(BPF_JSET));
2837 /* Link not known to support multicasts */
2841 b0 = gen_linktype(ETHERTYPE_IP);
2842 b1 = gen_cmp(off_nl + 16, BPF_B, (bpf_int32)224);
2843 b1->s.code = JMP(BPF_JGE);
2849 b0 = gen_linktype(ETHERTYPE_IPV6);
2850 b1 = gen_cmp(off_nl + 24, BPF_B, (bpf_int32)255);
2855 bpf_error("only IP multicast filters supported on ethernet/FDDI");
2859 * generate command for inbound/outbound. It's here so we can
2860 * make it link-type specific. 'dir' = 0 implies "inbound",
2861 * = 1 implies "outbound".
2867 register struct block *b0;
2869 b0 = gen_relation(BPF_JEQ,
2870 gen_load(Q_LINK, gen_loadi(0), 1),