]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid
[] =
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.114 2000-07-13 06:51:56 guy Exp $ (LBL)";
31 #include <sys/types.h>
32 #include <sys/socket.h>
35 #include <sys/param.h>
42 #include <netinet/in.h>
43 #include <netinet/if_ether.h>
52 #include "ethertype.h"
55 #include <pcap-namedb.h>
58 #include <sys/socket.h>
61 #ifdef HAVE_OS_PROTO_H
65 #define JMP(c) ((c)|BPF_JMP|BPF_K)
68 static jmp_buf top_ctx
;
69 static pcap_t
*bpf_pcap
;
73 int pcap_fddipad
= PCAP_FDDIPAD
;
80 bpf_error(const char *fmt
, ...)
87 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
94 static void init_linktype(int);
96 static int alloc_reg(void);
97 static void free_reg(int);
99 static struct block
*root
;
102 * We divy out chunks of memory rather than call malloc each time so
103 * we don't have to worry about leaking memory. It's probably
104 * not a big deal if all this memory was wasted but it this ever
105 * goes into a library that would probably not be a good idea.
108 #define CHUNK0SIZE 1024
114 static struct chunk chunks
[NCHUNKS
];
115 static int cur_chunk
;
117 static void *newchunk(u_int
);
118 static void freechunks(void);
119 static inline struct block
*new_block(int);
120 static inline struct slist
*new_stmt(int);
121 static struct block
*gen_retblk(int);
122 static inline void syntax(void);
124 static void backpatch(struct block
*, struct block
*);
125 static void merge(struct block
*, struct block
*);
126 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
127 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
128 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
129 static struct block
*gen_uncond(int);
130 static inline struct block
*gen_true(void);
131 static inline struct block
*gen_false(void);
132 static struct block
*gen_linktype(int);
133 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
135 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
137 static struct block
*gen_ehostop(const u_char
*, int);
138 static struct block
*gen_fhostop(const u_char
*, int);
139 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
140 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
142 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
145 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
147 static struct block
*gen_ipfrag(void);
148 static struct block
*gen_portatom(int, bpf_int32
);
150 static struct block
*gen_portatom6(int, bpf_int32
);
152 struct block
*gen_portop(int, int, int);
153 static struct block
*gen_port(int, int, int);
155 struct block
*gen_portop6(int, int, int);
156 static struct block
*gen_port6(int, int, int);
158 static int lookup_proto(const char *, int);
159 static struct block
*gen_proto(int, int, int);
160 static struct slist
*xfer_to_x(struct arth
*);
161 static struct slist
*xfer_to_a(struct arth
*);
162 static struct block
*gen_len(int, int);
172 /* XXX Round up to nearest long. */
173 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
175 /* XXX Round up to structure boundary. */
179 cp
= &chunks
[cur_chunk
];
180 if (n
> cp
->n_left
) {
181 ++cp
, k
= ++cur_chunk
;
183 bpf_error("out of memory");
184 size
= CHUNK0SIZE
<< k
;
185 cp
->m
= (void *)malloc(size
);
186 memset((char *)cp
->m
, 0, size
);
189 bpf_error("out of memory");
192 return (void *)((char *)cp
->m
+ cp
->n_left
);
201 for (i
= 0; i
< NCHUNKS
; ++i
)
202 if (chunks
[i
].m
!= NULL
) {
209 * A strdup whose allocations are freed after code generation is over.
213 register const char *s
;
215 int n
= strlen(s
) + 1;
216 char *cp
= newchunk(n
);
222 static inline struct block
*
228 p
= (struct block
*)newchunk(sizeof(*p
));
235 static inline struct slist
*
241 p
= (struct slist
*)newchunk(sizeof(*p
));
247 static struct block
*
251 struct block
*b
= new_block(BPF_RET
|BPF_K
);
260 bpf_error("syntax error in filter expression");
263 static bpf_u_int32 netmask
;
268 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
269 char *buf
, int optimize
, bpf_u_int32 mask
)
278 if (setjmp(top_ctx
)) {
285 snaplen
= pcap_snapshot(p
);
287 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
288 "snaplen of 0 rejects all packets");
292 lex_init(buf
? buf
: "");
293 init_linktype(pcap_datalink(p
));
300 root
= gen_retblk(snaplen
);
302 if (optimize
&& !no_optimize
) {
305 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
306 bpf_error("expression rejects all packets");
308 program
->bf_insns
= icode_to_fcode(root
, &len
);
309 program
->bf_len
= len
;
316 * entry point for using the compiler with no pcap open
317 * pass in all the stuff that is needed explicitly instead.
320 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
321 struct bpf_program
*program
,
322 char *buf
, int optimize
, bpf_u_int32 mask
)
330 if (setjmp(top_ctx
)) {
337 /* XXX needed? I don't grok the use of globals here. */
338 snaplen
= snaplen_arg
;
340 lex_init(buf
? buf
: "");
341 init_linktype(linktype_arg
);
348 root
= gen_retblk(snaplen_arg
);
353 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
354 bpf_error("expression rejects all packets");
356 program
->bf_insns
= icode_to_fcode(root
, &len
);
357 program
->bf_len
= len
;
364 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
365 * which of the jt and jf fields has been resolved and which is a pointer
366 * back to another unresolved block (or nil). At least one of the fields
367 * in each block is already resolved.
370 backpatch(list
, target
)
371 struct block
*list
, *target
;
388 * Merge the lists in b0 and b1, using the 'sense' field to indicate
389 * which of jt and jf is the link.
393 struct block
*b0
, *b1
;
395 register struct block
**p
= &b0
;
397 /* Find end of list. */
399 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
401 /* Concatenate the lists. */
409 backpatch(p
, gen_retblk(snaplen
));
410 p
->sense
= !p
->sense
;
411 backpatch(p
, gen_retblk(0));
417 struct block
*b0
, *b1
;
419 backpatch(b0
, b1
->head
);
420 b0
->sense
= !b0
->sense
;
421 b1
->sense
= !b1
->sense
;
423 b1
->sense
= !b1
->sense
;
429 struct block
*b0
, *b1
;
431 b0
->sense
= !b0
->sense
;
432 backpatch(b0
, b1
->head
);
433 b0
->sense
= !b0
->sense
;
442 b
->sense
= !b
->sense
;
445 static struct block
*
446 gen_cmp(offset
, size
, v
)
453 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
456 b
= new_block(JMP(BPF_JEQ
));
463 static struct block
*
464 gen_mcmp(offset
, size
, v
, mask
)
469 struct block
*b
= gen_cmp(offset
, size
, v
);
472 if (mask
!= 0xffffffff) {
473 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
480 static struct block
*
481 gen_bcmp(offset
, size
, v
)
482 register u_int offset
, size
;
483 register const u_char
*v
;
485 register struct block
*b
, *tmp
;
489 register const u_char
*p
= &v
[size
- 4];
490 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
491 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
493 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
500 register const u_char
*p
= &v
[size
- 2];
501 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
503 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
510 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
519 * Various code constructs need to know the layout of the data link
520 * layer. These variables give the necessary offsets. off_linktype
521 * is set to -1 for no encapsulation, in which case, IP is assumed.
523 static u_int off_linktype
;
542 * SLIP doesn't have a link level type. The 16 byte
543 * header is hacked into our SLIP driver.
550 /* XXX this may be the same as the DLT_PPP_BSDOS case */
574 * FDDI doesn't really have a link-level type field.
575 * We assume that SSAP = SNAP is being used and pick
576 * out the encapsulated Ethernet type.
580 off_linktype
+= pcap_fddipad
;
584 off_nl
+= pcap_fddipad
;
593 case DLT_ATM_RFC1483
:
595 * assume routed, non-ISO PDUs
596 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
607 bpf_error("unknown data link type 0x%x", linktype
);
611 static struct block
*
618 s
= new_stmt(BPF_LD
|BPF_IMM
);
620 b
= new_block(JMP(BPF_JEQ
));
626 static inline struct block
*
629 return gen_uncond(1);
632 static inline struct block
*
635 return gen_uncond(0);
638 static struct block
*
642 struct block
*b0
, *b1
;
644 /* If we're not using encapsulation, we're done */
645 if (off_linktype
== -1)
654 if (proto
== ETHERTYPE_IP
)
655 proto
= PPP_IP
; /* XXX was 0x21 */
657 else if (proto
== ETHERTYPE_IPV6
)
666 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
667 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
669 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
684 case ETHERTYPE_ATALK
:
696 if (proto
== ETHERTYPE_IP
)
697 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET
)));
699 else if (proto
== ETHERTYPE_IPV6
)
700 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET6
)));
705 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
708 static struct block
*
709 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
713 u_int src_off
, dst_off
;
715 struct block
*b0
, *b1
;
729 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
730 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
736 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
737 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
744 b0
= gen_linktype(proto
);
745 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
751 static struct block
*
752 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
753 struct in6_addr
*addr
;
754 struct in6_addr
*mask
;
756 u_int src_off
, dst_off
;
758 struct block
*b0
, *b1
;
773 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
774 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
780 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
781 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
788 /* this order is important */
789 a
= (u_int32_t
*)addr
;
790 m
= (u_int32_t
*)mask
;
791 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
792 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
794 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
796 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
798 b0
= gen_linktype(proto
);
804 static struct block
*
805 gen_ehostop(eaddr
, dir
)
806 register const u_char
*eaddr
;
809 register struct block
*b0
, *b1
;
813 return gen_bcmp(6, 6, eaddr
);
816 return gen_bcmp(0, 6, eaddr
);
819 b0
= gen_ehostop(eaddr
, Q_SRC
);
820 b1
= gen_ehostop(eaddr
, Q_DST
);
826 b0
= gen_ehostop(eaddr
, Q_SRC
);
827 b1
= gen_ehostop(eaddr
, Q_DST
);
836 * Like gen_ehostop, but for DLT_FDDI
838 static struct block
*
839 gen_fhostop(eaddr
, dir
)
840 register const u_char
*eaddr
;
843 struct block
*b0
, *b1
;
848 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
850 return gen_bcmp(6 + 1, 6, eaddr
);
855 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
857 return gen_bcmp(0 + 1, 6, eaddr
);
861 b0
= gen_fhostop(eaddr
, Q_SRC
);
862 b1
= gen_fhostop(eaddr
, Q_DST
);
868 b0
= gen_fhostop(eaddr
, Q_SRC
);
869 b1
= gen_fhostop(eaddr
, Q_DST
);
878 * This is quite tricky because there may be pad bytes in front of the
879 * DECNET header, and then there are two possible data packet formats that
880 * carry both src and dst addresses, plus 5 packet types in a format that
881 * carries only the src node, plus 2 types that use a different format and
882 * also carry just the src node.
886 * Instead of doing those all right, we just look for data packets with
887 * 0 or 1 bytes of padding. If you want to look at other packets, that
888 * will require a lot more hacking.
890 * To add support for filtering on DECNET "areas" (network numbers)
891 * one would want to add a "mask" argument to this routine. That would
892 * make the filter even more inefficient, although one could be clever
893 * and not generate masking instructions if the mask is 0xFFFF.
895 static struct block
*
896 gen_dnhostop(addr
, dir
, base_off
)
901 struct block
*b0
, *b1
, *b2
, *tmp
;
902 u_int offset_lh
; /* offset if long header is received */
903 u_int offset_sh
; /* offset if short header is received */
908 offset_sh
= 1; /* follows flags */
909 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
913 offset_sh
= 3; /* follows flags, dstnode */
914 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
918 /* Inefficient because we do our Calvinball dance twice */
919 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
920 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
926 /* Inefficient because we do our Calvinball dance twice */
927 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
928 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
935 b0
= gen_linktype(ETHERTYPE_DN
);
936 /* Check for pad = 1, long header case */
937 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
938 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
939 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
940 BPF_H
, (bpf_int32
)ntohs(addr
));
942 /* Check for pad = 0, long header case */
943 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
944 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
947 /* Check for pad = 1, short header case */
948 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
949 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
950 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
951 BPF_H
, (bpf_int32
)ntohs(addr
));
954 /* Check for pad = 0, short header case */
955 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
956 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
960 /* Combine with test for linktype */
965 static struct block
*
966 gen_host(addr
, mask
, proto
, dir
)
972 struct block
*b0
, *b1
;
977 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
978 if (off_linktype
!= -1) {
979 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
981 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
987 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
988 off_nl
+ 12, off_nl
+ 16);
991 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
992 off_nl
+ 14, off_nl
+ 24);
995 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
996 off_nl
+ 14, off_nl
+ 24);
999 bpf_error("'tcp' modifier applied to host");
1002 bpf_error("'udp' modifier applied to host");
1005 bpf_error("'icmp' modifier applied to host");
1008 bpf_error("'igmp' modifier applied to host");
1011 bpf_error("'igrp' modifier applied to host");
1014 bpf_error("'pim' modifier applied to host");
1017 bpf_error("ATALK host filtering not implemented");
1020 return gen_dnhostop(addr
, dir
, off_nl
);
1023 bpf_error("SCA host filtering not implemented");
1026 bpf_error("LAT host filtering not implemented");
1029 bpf_error("MOPDL host filtering not implemented");
1032 bpf_error("MOPRC host filtering not implemented");
1036 bpf_error("'ip6' modifier applied to ip host");
1039 bpf_error("'icmp6' modifier applied to host");
1043 bpf_error("'ah' modifier applied to host");
1046 bpf_error("'esp' modifier applied to host");
1055 static struct block
*
1056 gen_host6(addr
, mask
, proto
, dir
)
1057 struct in6_addr
*addr
;
1058 struct in6_addr
*mask
;
1065 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1068 bpf_error("'ip' modifier applied to ip6 host");
1071 bpf_error("'rarp' modifier applied to ip6 host");
1074 bpf_error("'arp' modifier applied to ip6 host");
1077 bpf_error("'tcp' modifier applied to host");
1080 bpf_error("'udp' modifier applied to host");
1083 bpf_error("'icmp' modifier applied to host");
1086 bpf_error("'igmp' modifier applied to host");
1089 bpf_error("'igrp' modifier applied to host");
1092 bpf_error("'pim' modifier applied to host");
1095 bpf_error("ATALK host filtering not implemented");
1098 bpf_error("'decnet' modifier applied to ip6 host");
1101 bpf_error("SCA host filtering not implemented");
1104 bpf_error("LAT host filtering not implemented");
1107 bpf_error("MOPDL host filtering not implemented");
1110 bpf_error("MOPRC host filtering not implemented");
1113 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1114 off_nl
+ 8, off_nl
+ 24);
1117 bpf_error("'icmp6' modifier applied to host");
1120 bpf_error("'ah' modifier applied to host");
1123 bpf_error("'esp' modifier applied to host");
1133 static struct block
*
1134 gen_gateway(eaddr
, alist
, proto
, dir
)
1135 const u_char
*eaddr
;
1136 bpf_u_int32
**alist
;
1140 struct block
*b0
, *b1
, *tmp
;
1143 bpf_error("direction applied to 'gateway'");
1150 if (linktype
== DLT_EN10MB
)
1151 b0
= gen_ehostop(eaddr
, Q_OR
);
1152 else if (linktype
== DLT_FDDI
)
1153 b0
= gen_fhostop(eaddr
, Q_OR
);
1156 "'gateway' supported only on ethernet or FDDI");
1158 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1160 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1168 bpf_error("illegal modifier of 'gateway'");
1174 gen_proto_abbrev(proto
)
1185 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1187 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1193 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1195 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1201 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1204 #ifndef IPPROTO_IGMP
1205 #define IPPROTO_IGMP 2
1209 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1212 #ifndef IPPROTO_IGRP
1213 #define IPPROTO_IGRP 9
1216 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1220 #define IPPROTO_PIM 103
1224 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1226 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1232 b1
= gen_linktype(ETHERTYPE_IP
);
1236 b1
= gen_linktype(ETHERTYPE_ARP
);
1240 b1
= gen_linktype(ETHERTYPE_REVARP
);
1244 bpf_error("link layer applied in wrong context");
1247 b1
= gen_linktype(ETHERTYPE_ATALK
);
1251 b1
= gen_linktype(ETHERTYPE_DN
);
1255 b1
= gen_linktype(ETHERTYPE_SCA
);
1259 b1
= gen_linktype(ETHERTYPE_LAT
);
1263 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1267 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1272 b1
= gen_linktype(ETHERTYPE_IPV6
);
1275 #ifndef IPPROTO_ICMPV6
1276 #define IPPROTO_ICMPV6 58
1279 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1284 #define IPPROTO_AH 51
1287 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1289 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1295 #define IPPROTO_ESP 50
1298 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1300 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1311 static struct block
*
1318 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1319 s
->s
.k
= off_nl
+ 6;
1320 b
= new_block(JMP(BPF_JSET
));
1328 static struct block
*
1329 gen_portatom(off
, v
)
1336 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1339 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1340 s
->next
->s
.k
= off_nl
+ off
;
1342 b
= new_block(JMP(BPF_JEQ
));
1350 static struct block
*
1351 gen_portatom6(off
, v
)
1355 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1360 gen_portop(port
, proto
, dir
)
1361 int port
, proto
, dir
;
1363 struct block
*b0
, *b1
, *tmp
;
1365 /* ip proto 'proto' */
1366 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1372 b1
= gen_portatom(0, (bpf_int32
)port
);
1376 b1
= gen_portatom(2, (bpf_int32
)port
);
1381 tmp
= gen_portatom(0, (bpf_int32
)port
);
1382 b1
= gen_portatom(2, (bpf_int32
)port
);
1387 tmp
= gen_portatom(0, (bpf_int32
)port
);
1388 b1
= gen_portatom(2, (bpf_int32
)port
);
1400 static struct block
*
1401 gen_port(port
, ip_proto
, dir
)
1406 struct block
*b0
, *b1
, *tmp
;
1408 /* ether proto ip */
1409 b0
= gen_linktype(ETHERTYPE_IP
);
1414 b1
= gen_portop(port
, ip_proto
, dir
);
1418 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1419 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1432 gen_portop6(port
, proto
, dir
)
1433 int port
, proto
, dir
;
1435 struct block
*b0
, *b1
, *tmp
;
1437 /* ip proto 'proto' */
1438 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1442 b1
= gen_portatom6(0, (bpf_int32
)port
);
1446 b1
= gen_portatom6(2, (bpf_int32
)port
);
1451 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1452 b1
= gen_portatom6(2, (bpf_int32
)port
);
1457 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1458 b1
= gen_portatom6(2, (bpf_int32
)port
);
1470 static struct block
*
1471 gen_port6(port
, ip_proto
, dir
)
1476 struct block
*b0
, *b1
, *tmp
;
1478 /* ether proto ip */
1479 b0
= gen_linktype(ETHERTYPE_IPV6
);
1484 b1
= gen_portop6(port
, ip_proto
, dir
);
1488 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1489 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1502 lookup_proto(name
, proto
)
1503 register const char *name
;
1512 v
= pcap_nametoproto(name
);
1513 if (v
== PROTO_UNDEF
)
1514 bpf_error("unknown ip proto '%s'", name
);
1518 /* XXX should look up h/w protocol type based on linktype */
1519 v
= pcap_nametoeproto(name
);
1520 if (v
== PROTO_UNDEF
)
1521 bpf_error("unknown ether proto '%s'", name
);
1542 gen_protochain(v
, proto
, dir
)
1547 #ifdef NO_PROTOCHAIN
1548 return gen_proto(v
, proto
, dir
);
1550 struct block
*b0
, *b
;
1551 struct slist
*s
[100];
1552 int fix2
, fix3
, fix4
, fix5
;
1553 int ahcheck
, again
, end
;
1555 int reg1
= alloc_reg();
1556 int reg2
= alloc_reg();
1558 memset(s
, 0, sizeof(s
));
1559 fix2
= fix3
= fix4
= fix5
= 0;
1566 b0
= gen_protochain(v
, Q_IP
, dir
);
1567 b
= gen_protochain(v
, Q_IPV6
, dir
);
1571 bpf_error("bad protocol applied for 'protochain'");
1575 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1578 * s[0] is a dummy entry to protect other BPF insn from damaged
1579 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1580 * hard to find interdependency made by jump table fixup.
1583 s
[i
] = new_stmt(0); /*dummy*/
1588 b0
= gen_linktype(ETHERTYPE_IP
);
1591 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1592 s
[i
]->s
.k
= off_nl
+ 9;
1594 /* X = ip->ip_hl << 2 */
1595 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1601 b0
= gen_linktype(ETHERTYPE_IPV6
);
1603 /* A = ip6->ip_nxt */
1604 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1605 s
[i
]->s
.k
= off_nl
+ 6;
1607 /* X = sizeof(struct ip6_hdr) */
1608 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1614 bpf_error("unsupported proto to gen_protochain");
1618 /* again: if (A == v) goto end; else fall through; */
1620 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1622 s
[i
]->s
.jt
= NULL
; /*later*/
1623 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1627 #ifndef IPPROTO_NONE
1628 #define IPPROTO_NONE 59
1630 /* if (A == IPPROTO_NONE) goto end */
1631 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1632 s
[i
]->s
.jt
= NULL
; /*later*/
1633 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1634 s
[i
]->s
.k
= IPPROTO_NONE
;
1635 s
[fix5
]->s
.jf
= s
[i
];
1640 if (proto
== Q_IPV6
) {
1641 int v6start
, v6end
, v6advance
, j
;
1644 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1645 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1646 s
[i
]->s
.jt
= NULL
; /*later*/
1647 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1648 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
1649 s
[fix2
]->s
.jf
= s
[i
];
1651 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1652 s
[i
- 1]->s
.jf
= 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*/
1655 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
1657 /* if (A == IPPROTO_ROUTING) goto v6advance */
1658 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1659 s
[i
]->s
.jt
= NULL
; /*later*/
1660 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1661 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1663 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1664 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1665 s
[i
]->s
.jt
= NULL
; /*later*/
1666 s
[i
]->s
.jf
= NULL
; /*later*/
1667 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
1678 * X = X + (P[X] + 1) * 8;
1681 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1684 s
[i
] = new_stmt(BPF_ST
);
1688 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1692 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1694 /* A = P[X + packet head]; */
1695 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1699 s
[i
] = new_stmt(BPF_ST
);
1703 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1706 /* A = P[X + packet head] */
1707 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1711 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1715 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1719 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1722 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1726 /* goto again; (must use BPF_JA for backward jump) */
1727 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1728 s
[i
]->s
.k
= again
- i
- 1;
1729 s
[i
- 1]->s
.jf
= s
[i
];
1733 for (j
= v6start
; j
<= v6end
; j
++)
1734 s
[j
]->s
.jt
= s
[v6advance
];
1739 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1741 s
[fix2
]->s
.jf
= s
[i
];
1747 /* if (A == IPPROTO_AH) then fall through; else goto end; */
1748 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1749 s
[i
]->s
.jt
= NULL
; /*later*/
1750 s
[i
]->s
.jf
= NULL
; /*later*/
1751 s
[i
]->s
.k
= IPPROTO_AH
;
1753 s
[fix3
]->s
.jf
= s
[ahcheck
];
1760 * X = X + (P[X] + 2) * 4;
1763 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1766 s
[i
] = new_stmt(BPF_ST
);
1770 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1774 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1776 /* A = P[X + packet head]; */
1777 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1781 s
[i
] = new_stmt(BPF_ST
);
1785 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1788 /* A = P[X + packet head] */
1789 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1793 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1797 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1801 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1804 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1808 /* goto again; (must use BPF_JA for backward jump) */
1809 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1810 s
[i
]->s
.k
= again
- i
- 1;
1815 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1817 s
[fix2
]->s
.jt
= s
[end
];
1818 s
[fix4
]->s
.jf
= s
[end
];
1819 s
[fix5
]->s
.jt
= s
[end
];
1826 for (i
= 0; i
< max
- 1; i
++)
1827 s
[i
]->next
= s
[i
+ 1];
1828 s
[max
- 1]->next
= NULL
;
1833 b
= new_block(JMP(BPF_JEQ
));
1834 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
1845 static struct block
*
1846 gen_proto(v
, proto
, dir
)
1851 struct block
*b0
, *b1
;
1853 if (dir
!= Q_DEFAULT
)
1854 bpf_error("direction applied to 'proto'");
1859 b0
= gen_proto(v
, Q_IP
, dir
);
1860 b1
= gen_proto(v
, Q_IPV6
, dir
);
1867 b0
= gen_linktype(ETHERTYPE_IP
);
1869 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
1871 b1
= gen_protochain(v
, Q_IP
);
1877 bpf_error("arp does not encapsulate another protocol");
1881 bpf_error("rarp does not encapsulate another protocol");
1885 bpf_error("atalk encapsulation is not specifiable");
1889 bpf_error("decnet encapsulation is not specifiable");
1893 bpf_error("sca does not encapsulate another protocol");
1897 bpf_error("lat does not encapsulate another protocol");
1901 bpf_error("moprc does not encapsulate another protocol");
1905 bpf_error("mopdl does not encapsulate another protocol");
1909 return gen_linktype(v
);
1912 bpf_error("'udp proto' is bogus");
1916 bpf_error("'tcp proto' is bogus");
1920 bpf_error("'icmp proto' is bogus");
1924 bpf_error("'igmp proto' is bogus");
1928 bpf_error("'igrp proto' is bogus");
1932 bpf_error("'pim proto' is bogus");
1937 b0
= gen_linktype(ETHERTYPE_IPV6
);
1939 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
1941 b1
= gen_protochain(v
, Q_IPV6
);
1947 bpf_error("'icmp6 proto' is bogus");
1951 bpf_error("'ah proto' is bogus");
1954 bpf_error("'ah proto' is bogus");
1965 register const char *name
;
1968 int proto
= q
.proto
;
1972 bpf_u_int32 mask
, addr
;
1974 bpf_u_int32
**alist
;
1977 struct sockaddr_in
*sin
;
1978 struct sockaddr_in6
*sin6
;
1979 struct addrinfo
*res
, *res0
;
1980 struct in6_addr mask128
;
1982 struct block
*b
, *tmp
;
1983 int port
, real_proto
;
1988 addr
= pcap_nametonetaddr(name
);
1990 bpf_error("unknown network '%s'", name
);
1991 /* Left justify network addr and calculate its network mask */
1993 while (addr
&& (addr
& 0xff000000) == 0) {
1997 return gen_host(addr
, mask
, proto
, dir
);
2001 if (proto
== Q_LINK
) {
2005 eaddr
= pcap_ether_hostton(name
);
2008 "unknown ether host '%s'", name
);
2009 return gen_ehostop(eaddr
, dir
);
2012 eaddr
= pcap_ether_hostton(name
);
2015 "unknown FDDI host '%s'", name
);
2016 return gen_fhostop(eaddr
, dir
);
2020 "only ethernet/FDDI supports link-level host name");
2023 } else if (proto
== Q_DECNET
) {
2024 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2026 * I don't think DECNET hosts can be multihomed, so
2027 * there is no need to build up a list of addresses
2029 return (gen_host(dn_addr
, 0, proto
, dir
));
2032 alist
= pcap_nametoaddr(name
);
2033 if (alist
== NULL
|| *alist
== NULL
)
2034 bpf_error("unknown host '%s'", name
);
2036 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2038 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2040 tmp
= gen_host(**alist
++, 0xffffffff,
2047 memset(&mask128
, 0xff, sizeof(mask128
));
2048 res0
= res
= pcap_nametoaddrinfo(name
);
2050 bpf_error("unknown host '%s'", name
);
2052 tproto
= tproto6
= proto
;
2053 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2057 for (res
= res0
; res
; res
= res
->ai_next
) {
2058 switch (res
->ai_family
) {
2060 if (tproto
== Q_IPV6
)
2063 sin
= (struct sockaddr_in
*)
2065 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2066 0xffffffff, tproto
, dir
);
2069 if (tproto6
== Q_IP
)
2072 sin6
= (struct sockaddr_in6
*)
2074 tmp
= gen_host6(&sin6
->sin6_addr
,
2075 &mask128
, tproto6
, dir
);
2084 bpf_error("unknown host '%s'%s", name
,
2085 (proto
== Q_DEFAULT
)
2087 : " for specified address family");
2094 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2095 bpf_error("illegal qualifier of 'port'");
2096 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2097 bpf_error("unknown port '%s'", name
);
2098 if (proto
== Q_UDP
) {
2099 if (real_proto
== IPPROTO_TCP
)
2100 bpf_error("port '%s' is tcp", name
);
2102 /* override PROTO_UNDEF */
2103 real_proto
= IPPROTO_UDP
;
2105 if (proto
== Q_TCP
) {
2106 if (real_proto
== IPPROTO_UDP
)
2107 bpf_error("port '%s' is udp", name
);
2109 /* override PROTO_UNDEF */
2110 real_proto
= IPPROTO_TCP
;
2113 return gen_port(port
, real_proto
, dir
);
2117 b
= gen_port(port
, real_proto
, dir
);
2118 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2125 eaddr
= pcap_ether_hostton(name
);
2127 bpf_error("unknown ether host: %s", name
);
2129 alist
= pcap_nametoaddr(name
);
2130 if (alist
== NULL
|| *alist
== NULL
)
2131 bpf_error("unknown host '%s'", name
);
2132 return gen_gateway(eaddr
, alist
, proto
, dir
);
2134 bpf_error("'gateway' not supported in this configuration");
2138 real_proto
= lookup_proto(name
, proto
);
2139 if (real_proto
>= 0)
2140 return gen_proto(real_proto
, proto
, dir
);
2142 bpf_error("unknown protocol: %s", name
);
2145 real_proto
= lookup_proto(name
, proto
);
2146 if (real_proto
>= 0)
2147 return gen_protochain(real_proto
, proto
, dir
);
2149 bpf_error("unknown protocol: %s", name
);
2161 gen_mcode(s1
, s2
, masklen
, q
)
2162 register const char *s1
, *s2
;
2163 register int masklen
;
2166 register int nlen
, mlen
;
2169 nlen
= __pcap_atoin(s1
, &n
);
2170 /* Promote short ipaddr */
2174 mlen
= __pcap_atoin(s2
, &m
);
2175 /* Promote short ipaddr */
2178 bpf_error("non-network bits set in \"%s mask %s\"",
2181 /* Convert mask len to mask */
2183 bpf_error("mask length must be <= 32");
2184 m
= 0xffffffff << (32 - masklen
);
2186 bpf_error("non-network bits set in \"%s/%d\"",
2193 return gen_host(n
, m
, q
.proto
, q
.dir
);
2196 bpf_error("Mask syntax for networks only");
2203 register const char *s
;
2208 int proto
= q
.proto
;
2214 else if (q
.proto
== Q_DECNET
)
2215 vlen
= __pcap_atodn(s
, &v
);
2217 vlen
= __pcap_atoin(s
, &v
);
2224 if (proto
== Q_DECNET
)
2225 return gen_host(v
, 0, proto
, dir
);
2226 else if (proto
== Q_LINK
) {
2227 bpf_error("illegal link layer address");
2230 if (s
== NULL
&& q
.addr
== Q_NET
) {
2231 /* Promote short net number */
2232 while (v
&& (v
& 0xff000000) == 0) {
2237 /* Promote short ipaddr */
2241 return gen_host(v
, mask
, proto
, dir
);
2246 proto
= IPPROTO_UDP
;
2247 else if (proto
== Q_TCP
)
2248 proto
= IPPROTO_TCP
;
2249 else if (proto
== Q_DEFAULT
)
2250 proto
= PROTO_UNDEF
;
2252 bpf_error("illegal qualifier of 'port'");
2255 return gen_port((int)v
, proto
, dir
);
2259 b
= gen_port((int)v
, proto
, dir
);
2260 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2266 bpf_error("'gateway' requires a name");
2270 return gen_proto((int)v
, proto
, dir
);
2273 return gen_protochain((int)v
, proto
, dir
);
2288 gen_mcode6(s1
, s2
, masklen
, q
)
2289 register const char *s1
, *s2
;
2290 register int masklen
;
2293 struct addrinfo
*res
;
2294 struct in6_addr
*addr
;
2295 struct in6_addr mask
;
2300 bpf_error("no mask %s supported", s2
);
2302 res
= pcap_nametoaddrinfo(s1
);
2304 bpf_error("invalid ip6 address %s", s1
);
2306 bpf_error("%s resolved to multiple address", s1
);
2307 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2309 if (sizeof(mask
) * 8 < masklen
)
2310 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2311 memset(&mask
, 0xff, masklen
/ 8);
2313 mask
.s6_addr
[masklen
/ 8] =
2314 (0xff << (8 - masklen
% 8)) & 0xff;
2317 a
= (u_int32_t
*)addr
;
2318 m
= (u_int32_t
*)&mask
;
2319 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2320 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2321 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2329 bpf_error("Mask syntax for networks only");
2333 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2338 bpf_error("invalid qualifier against IPv6 address");
2346 register const u_char
*eaddr
;
2349 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2350 if (linktype
== DLT_EN10MB
)
2351 return gen_ehostop(eaddr
, (int)q
.dir
);
2352 if (linktype
== DLT_FDDI
)
2353 return gen_fhostop(eaddr
, (int)q
.dir
);
2355 bpf_error("ethernet address used in non-ether expression");
2361 struct slist
*s0
, *s1
;
2364 * This is definitely not the best way to do this, but the
2365 * lists will rarely get long.
2372 static struct slist
*
2378 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2383 static struct slist
*
2389 s
= new_stmt(BPF_LD
|BPF_MEM
);
2395 gen_load(proto
, index
, size
)
2400 struct slist
*s
, *tmp
;
2402 int regno
= alloc_reg();
2404 free_reg(index
->regno
);
2408 bpf_error("data size must be 1, 2, or 4");
2424 bpf_error("unsupported index operation");
2427 s
= xfer_to_x(index
);
2428 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2430 sappend(index
->s
, s
);
2445 /* XXX Note that we assume a fixed link header here. */
2446 s
= xfer_to_x(index
);
2447 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2450 sappend(index
->s
, s
);
2452 b
= gen_proto_abbrev(proto
);
2454 gen_and(index
->b
, b
);
2464 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2466 sappend(s
, xfer_to_a(index
));
2467 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2468 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2469 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2471 sappend(index
->s
, s
);
2473 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2475 gen_and(index
->b
, b
);
2477 gen_and(gen_proto_abbrev(Q_IP
), b
);
2483 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2487 index
->regno
= regno
;
2488 s
= new_stmt(BPF_ST
);
2490 sappend(index
->s
, s
);
2496 gen_relation(code
, a0
, a1
, reversed
)
2498 struct arth
*a0
, *a1
;
2501 struct slist
*s0
, *s1
, *s2
;
2502 struct block
*b
, *tmp
;
2506 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2507 b
= new_block(JMP(code
));
2508 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2509 reversed
= !reversed
;
2510 b
->s
.k
= 0x80000000;
2518 sappend(a0
->s
, a1
->s
);
2522 free_reg(a0
->regno
);
2523 free_reg(a1
->regno
);
2525 /* 'and' together protocol checks */
2528 gen_and(a0
->b
, tmp
= a1
->b
);
2544 int regno
= alloc_reg();
2545 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2548 s
= new_stmt(BPF_LD
|BPF_LEN
);
2549 s
->next
= new_stmt(BPF_ST
);
2550 s
->next
->s
.k
= regno
;
2565 a
= (struct arth
*)newchunk(sizeof(*a
));
2569 s
= new_stmt(BPF_LD
|BPF_IMM
);
2571 s
->next
= new_stmt(BPF_ST
);
2587 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2590 s
= new_stmt(BPF_ST
);
2598 gen_arth(code
, a0
, a1
)
2600 struct arth
*a0
, *a1
;
2602 struct slist
*s0
, *s1
, *s2
;
2606 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2611 sappend(a0
->s
, a1
->s
);
2613 free_reg(a1
->regno
);
2615 s0
= new_stmt(BPF_ST
);
2616 a0
->regno
= s0
->s
.k
= alloc_reg();
2623 * Here we handle simple allocation of the scratch registers.
2624 * If too many registers are alloc'd, the allocator punts.
2626 static int regused
[BPF_MEMWORDS
];
2630 * Return the next free register.
2635 int n
= BPF_MEMWORDS
;
2638 if (regused
[curreg
])
2639 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2641 regused
[curreg
] = 1;
2645 bpf_error("too many registers needed to evaluate expression");
2650 * Return a register to the table so it can
2660 static struct block
*
2667 s
= new_stmt(BPF_LD
|BPF_LEN
);
2668 b
= new_block(JMP(jmp
));
2679 return gen_len(BPF_JGE
, n
);
2688 b
= gen_len(BPF_JGT
, n
);
2695 gen_byteop(op
, idx
, val
)
2706 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2709 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2710 b
->s
.code
= JMP(BPF_JGE
);
2715 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2716 b
->s
.code
= JMP(BPF_JGT
);
2720 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
2724 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2728 b
= new_block(JMP(BPF_JEQ
));
2736 gen_broadcast(proto
)
2739 bpf_u_int32 hostmask
;
2740 struct block
*b0
, *b1
, *b2
;
2741 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2747 if (linktype
== DLT_EN10MB
)
2748 return gen_ehostop(ebroadcast
, Q_DST
);
2749 if (linktype
== DLT_FDDI
)
2750 return gen_fhostop(ebroadcast
, Q_DST
);
2751 bpf_error("not a broadcast link");
2755 b0
= gen_linktype(ETHERTYPE_IP
);
2756 hostmask
= ~netmask
;
2757 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
2758 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
2759 (bpf_int32
)(~0 & hostmask
), hostmask
);
2764 bpf_error("only ether/ip broadcast filters supported");
2768 gen_multicast(proto
)
2771 register struct block
*b0
, *b1
;
2772 register struct slist
*s
;
2778 if (linktype
== DLT_EN10MB
) {
2779 /* ether[0] & 1 != 0 */
2780 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2782 b0
= new_block(JMP(BPF_JSET
));
2788 if (linktype
== DLT_FDDI
) {
2789 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
2790 /* fddi[1] & 1 != 0 */
2791 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2793 b0
= new_block(JMP(BPF_JSET
));
2798 /* Link not known to support multicasts */
2802 b0
= gen_linktype(ETHERTYPE_IP
);
2803 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
2804 b1
->s
.code
= JMP(BPF_JGE
);
2810 b0
= gen_linktype(ETHERTYPE_IPV6
);
2811 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
2816 bpf_error("only IP multicast filters supported on ethernet/FDDI");
2820 * generate command for inbound/outbound. It's here so we can
2821 * make it link-type specific. 'dir' = 0 implies "inbound",
2822 * = 1 implies "outbound".
2828 register struct block
*b0
;
2830 b0
= gen_relation(BPF_JEQ
,
2831 gen_load(Q_LINK
, gen_loadi(0), 1),