]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
6eab6ced4f0847b3d9d9e14b816df09ddddf4574
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.113 2000-07-11 00:37:04 assar 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
)) {
284 snaplen
= pcap_snapshot(p
);
286 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
287 "snaplen of 0 rejects all packets");
291 lex_init(buf
? buf
: "");
292 init_linktype(pcap_datalink(p
));
299 root
= gen_retblk(snaplen
);
301 if (optimize
&& !no_optimize
) {
304 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
305 bpf_error("expression rejects all packets");
307 program
->bf_insns
= icode_to_fcode(root
, &len
);
308 program
->bf_len
= len
;
315 * entry point for using the compiler with no pcap open
316 * pass in all the stuff that is needed explicitly instead.
319 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
320 struct bpf_program
*program
,
321 char *buf
, int optimize
, bpf_u_int32 mask
)
329 if (setjmp(top_ctx
)) {
336 /* XXX needed? I don't grok the use of globals here. */
337 snaplen
= snaplen_arg
;
339 lex_init(buf
? buf
: "");
340 init_linktype(linktype_arg
);
347 root
= gen_retblk(snaplen_arg
);
352 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
353 bpf_error("expression rejects all packets");
355 program
->bf_insns
= icode_to_fcode(root
, &len
);
356 program
->bf_len
= len
;
363 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
364 * which of the jt and jf fields has been resolved and which is a pointer
365 * back to another unresolved block (or nil). At least one of the fields
366 * in each block is already resolved.
369 backpatch(list
, target
)
370 struct block
*list
, *target
;
387 * Merge the lists in b0 and b1, using the 'sense' field to indicate
388 * which of jt and jf is the link.
392 struct block
*b0
, *b1
;
394 register struct block
**p
= &b0
;
396 /* Find end of list. */
398 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
400 /* Concatenate the lists. */
408 backpatch(p
, gen_retblk(snaplen
));
409 p
->sense
= !p
->sense
;
410 backpatch(p
, gen_retblk(0));
416 struct block
*b0
, *b1
;
418 backpatch(b0
, b1
->head
);
419 b0
->sense
= !b0
->sense
;
420 b1
->sense
= !b1
->sense
;
422 b1
->sense
= !b1
->sense
;
428 struct block
*b0
, *b1
;
430 b0
->sense
= !b0
->sense
;
431 backpatch(b0
, b1
->head
);
432 b0
->sense
= !b0
->sense
;
441 b
->sense
= !b
->sense
;
444 static struct block
*
445 gen_cmp(offset
, size
, v
)
452 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
455 b
= new_block(JMP(BPF_JEQ
));
462 static struct block
*
463 gen_mcmp(offset
, size
, v
, mask
)
468 struct block
*b
= gen_cmp(offset
, size
, v
);
471 if (mask
!= 0xffffffff) {
472 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
479 static struct block
*
480 gen_bcmp(offset
, size
, v
)
481 register u_int offset
, size
;
482 register const u_char
*v
;
484 register struct block
*b
, *tmp
;
488 register const u_char
*p
= &v
[size
- 4];
489 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
490 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
492 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
499 register const u_char
*p
= &v
[size
- 2];
500 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
502 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
509 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
518 * Various code constructs need to know the layout of the data link
519 * layer. These variables give the necessary offsets. off_linktype
520 * is set to -1 for no encapsulation, in which case, IP is assumed.
522 static u_int off_linktype
;
541 * SLIP doesn't have a link level type. The 16 byte
542 * header is hacked into our SLIP driver.
549 /* XXX this may be the same as the DLT_PPP_BSDOS case */
573 * FDDI doesn't really have a link-level type field.
574 * We assume that SSAP = SNAP is being used and pick
575 * out the encapsulated Ethernet type.
579 off_linktype
+= pcap_fddipad
;
583 off_nl
+= pcap_fddipad
;
592 case DLT_ATM_RFC1483
:
594 * assume routed, non-ISO PDUs
595 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
606 bpf_error("unknown data link type 0x%x", linktype
);
610 static struct block
*
617 s
= new_stmt(BPF_LD
|BPF_IMM
);
619 b
= new_block(JMP(BPF_JEQ
));
625 static inline struct block
*
628 return gen_uncond(1);
631 static inline struct block
*
634 return gen_uncond(0);
637 static struct block
*
641 struct block
*b0
, *b1
;
643 /* If we're not using encapsulation, we're done */
644 if (off_linktype
== -1)
653 if (proto
== ETHERTYPE_IP
)
654 proto
= PPP_IP
; /* XXX was 0x21 */
656 else if (proto
== ETHERTYPE_IPV6
)
665 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
666 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
668 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
683 case ETHERTYPE_ATALK
:
695 if (proto
== ETHERTYPE_IP
)
696 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET
)));
698 else if (proto
== ETHERTYPE_IPV6
)
699 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET6
)));
704 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
707 static struct block
*
708 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
712 u_int src_off
, dst_off
;
714 struct block
*b0
, *b1
;
728 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
729 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
735 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
736 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
743 b0
= gen_linktype(proto
);
744 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
750 static struct block
*
751 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
752 struct in6_addr
*addr
;
753 struct in6_addr
*mask
;
755 u_int src_off
, dst_off
;
757 struct block
*b0
, *b1
;
772 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
773 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
779 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
780 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
787 /* this order is important */
788 a
= (u_int32_t
*)addr
;
789 m
= (u_int32_t
*)mask
;
790 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
791 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
793 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
795 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
797 b0
= gen_linktype(proto
);
803 static struct block
*
804 gen_ehostop(eaddr
, dir
)
805 register const u_char
*eaddr
;
808 register struct block
*b0
, *b1
;
812 return gen_bcmp(6, 6, eaddr
);
815 return gen_bcmp(0, 6, eaddr
);
818 b0
= gen_ehostop(eaddr
, Q_SRC
);
819 b1
= gen_ehostop(eaddr
, Q_DST
);
825 b0
= gen_ehostop(eaddr
, Q_SRC
);
826 b1
= gen_ehostop(eaddr
, Q_DST
);
835 * Like gen_ehostop, but for DLT_FDDI
837 static struct block
*
838 gen_fhostop(eaddr
, dir
)
839 register const u_char
*eaddr
;
842 struct block
*b0
, *b1
;
847 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
849 return gen_bcmp(6 + 1, 6, eaddr
);
854 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
856 return gen_bcmp(0 + 1, 6, eaddr
);
860 b0
= gen_fhostop(eaddr
, Q_SRC
);
861 b1
= gen_fhostop(eaddr
, Q_DST
);
867 b0
= gen_fhostop(eaddr
, Q_SRC
);
868 b1
= gen_fhostop(eaddr
, Q_DST
);
877 * This is quite tricky because there may be pad bytes in front of the
878 * DECNET header, and then there are two possible data packet formats that
879 * carry both src and dst addresses, plus 5 packet types in a format that
880 * carries only the src node, plus 2 types that use a different format and
881 * also carry just the src node.
885 * Instead of doing those all right, we just look for data packets with
886 * 0 or 1 bytes of padding. If you want to look at other packets, that
887 * will require a lot more hacking.
889 * To add support for filtering on DECNET "areas" (network numbers)
890 * one would want to add a "mask" argument to this routine. That would
891 * make the filter even more inefficient, although one could be clever
892 * and not generate masking instructions if the mask is 0xFFFF.
894 static struct block
*
895 gen_dnhostop(addr
, dir
, base_off
)
900 struct block
*b0
, *b1
, *b2
, *tmp
;
901 u_int offset_lh
; /* offset if long header is received */
902 u_int offset_sh
; /* offset if short header is received */
907 offset_sh
= 1; /* follows flags */
908 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
912 offset_sh
= 3; /* follows flags, dstnode */
913 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
917 /* Inefficient because we do our Calvinball dance twice */
918 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
919 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
925 /* Inefficient because we do our Calvinball dance twice */
926 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
927 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
934 b0
= gen_linktype(ETHERTYPE_DN
);
935 /* Check for pad = 1, long header case */
936 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
937 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
938 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
939 BPF_H
, (bpf_int32
)ntohs(addr
));
941 /* Check for pad = 0, long header case */
942 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
943 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
946 /* Check for pad = 1, short header case */
947 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
948 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
949 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
950 BPF_H
, (bpf_int32
)ntohs(addr
));
953 /* Check for pad = 0, short header case */
954 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
955 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
959 /* Combine with test for linktype */
964 static struct block
*
965 gen_host(addr
, mask
, proto
, dir
)
971 struct block
*b0
, *b1
;
976 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
977 if (off_linktype
!= -1) {
978 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
980 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
986 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
987 off_nl
+ 12, off_nl
+ 16);
990 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
991 off_nl
+ 14, off_nl
+ 24);
994 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
995 off_nl
+ 14, off_nl
+ 24);
998 bpf_error("'tcp' modifier applied to host");
1001 bpf_error("'udp' modifier applied to host");
1004 bpf_error("'icmp' modifier applied to host");
1007 bpf_error("'igmp' modifier applied to host");
1010 bpf_error("'igrp' modifier applied to host");
1013 bpf_error("'pim' modifier applied to host");
1016 bpf_error("ATALK host filtering not implemented");
1019 return gen_dnhostop(addr
, dir
, off_nl
);
1022 bpf_error("SCA host filtering not implemented");
1025 bpf_error("LAT host filtering not implemented");
1028 bpf_error("MOPDL host filtering not implemented");
1031 bpf_error("MOPRC host filtering not implemented");
1035 bpf_error("'ip6' modifier applied to ip host");
1038 bpf_error("'icmp6' modifier applied to host");
1042 bpf_error("'ah' modifier applied to host");
1045 bpf_error("'esp' modifier applied to host");
1054 static struct block
*
1055 gen_host6(addr
, mask
, proto
, dir
)
1056 struct in6_addr
*addr
;
1057 struct in6_addr
*mask
;
1064 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1067 bpf_error("'ip' modifier applied to ip6 host");
1070 bpf_error("'rarp' modifier applied to ip6 host");
1073 bpf_error("'arp' modifier applied to ip6 host");
1076 bpf_error("'tcp' modifier applied to host");
1079 bpf_error("'udp' modifier applied to host");
1082 bpf_error("'icmp' modifier applied to host");
1085 bpf_error("'igmp' modifier applied to host");
1088 bpf_error("'igrp' modifier applied to host");
1091 bpf_error("'pim' modifier applied to host");
1094 bpf_error("ATALK host filtering not implemented");
1097 bpf_error("'decnet' modifier applied to ip6 host");
1100 bpf_error("SCA host filtering not implemented");
1103 bpf_error("LAT host filtering not implemented");
1106 bpf_error("MOPDL host filtering not implemented");
1109 bpf_error("MOPRC host filtering not implemented");
1112 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1113 off_nl
+ 8, off_nl
+ 24);
1116 bpf_error("'icmp6' modifier applied to host");
1119 bpf_error("'ah' modifier applied to host");
1122 bpf_error("'esp' modifier applied to host");
1132 static struct block
*
1133 gen_gateway(eaddr
, alist
, proto
, dir
)
1134 const u_char
*eaddr
;
1135 bpf_u_int32
**alist
;
1139 struct block
*b0
, *b1
, *tmp
;
1142 bpf_error("direction applied to 'gateway'");
1149 if (linktype
== DLT_EN10MB
)
1150 b0
= gen_ehostop(eaddr
, Q_OR
);
1151 else if (linktype
== DLT_FDDI
)
1152 b0
= gen_fhostop(eaddr
, Q_OR
);
1155 "'gateway' supported only on ethernet or FDDI");
1157 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1159 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1167 bpf_error("illegal modifier of 'gateway'");
1173 gen_proto_abbrev(proto
)
1184 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1186 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1192 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1194 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1200 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1203 #ifndef IPPROTO_IGMP
1204 #define IPPROTO_IGMP 2
1208 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1211 #ifndef IPPROTO_IGRP
1212 #define IPPROTO_IGRP 9
1215 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1219 #define IPPROTO_PIM 103
1223 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1225 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1231 b1
= gen_linktype(ETHERTYPE_IP
);
1235 b1
= gen_linktype(ETHERTYPE_ARP
);
1239 b1
= gen_linktype(ETHERTYPE_REVARP
);
1243 bpf_error("link layer applied in wrong context");
1246 b1
= gen_linktype(ETHERTYPE_ATALK
);
1250 b1
= gen_linktype(ETHERTYPE_DN
);
1254 b1
= gen_linktype(ETHERTYPE_SCA
);
1258 b1
= gen_linktype(ETHERTYPE_LAT
);
1262 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1266 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1271 b1
= gen_linktype(ETHERTYPE_IPV6
);
1274 #ifndef IPPROTO_ICMPV6
1275 #define IPPROTO_ICMPV6 58
1278 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1283 #define IPPROTO_AH 51
1286 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1288 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1294 #define IPPROTO_ESP 50
1297 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1299 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1310 static struct block
*
1317 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1318 s
->s
.k
= off_nl
+ 6;
1319 b
= new_block(JMP(BPF_JSET
));
1327 static struct block
*
1328 gen_portatom(off
, v
)
1335 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1338 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1339 s
->next
->s
.k
= off_nl
+ off
;
1341 b
= new_block(JMP(BPF_JEQ
));
1349 static struct block
*
1350 gen_portatom6(off
, v
)
1354 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1359 gen_portop(port
, proto
, dir
)
1360 int port
, proto
, dir
;
1362 struct block
*b0
, *b1
, *tmp
;
1364 /* ip proto 'proto' */
1365 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1371 b1
= gen_portatom(0, (bpf_int32
)port
);
1375 b1
= gen_portatom(2, (bpf_int32
)port
);
1380 tmp
= gen_portatom(0, (bpf_int32
)port
);
1381 b1
= gen_portatom(2, (bpf_int32
)port
);
1386 tmp
= gen_portatom(0, (bpf_int32
)port
);
1387 b1
= gen_portatom(2, (bpf_int32
)port
);
1399 static struct block
*
1400 gen_port(port
, ip_proto
, dir
)
1405 struct block
*b0
, *b1
, *tmp
;
1407 /* ether proto ip */
1408 b0
= gen_linktype(ETHERTYPE_IP
);
1413 b1
= gen_portop(port
, ip_proto
, dir
);
1417 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1418 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1431 gen_portop6(port
, proto
, dir
)
1432 int port
, proto
, dir
;
1434 struct block
*b0
, *b1
, *tmp
;
1436 /* ip proto 'proto' */
1437 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1441 b1
= gen_portatom6(0, (bpf_int32
)port
);
1445 b1
= gen_portatom6(2, (bpf_int32
)port
);
1450 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1451 b1
= gen_portatom6(2, (bpf_int32
)port
);
1456 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1457 b1
= gen_portatom6(2, (bpf_int32
)port
);
1469 static struct block
*
1470 gen_port6(port
, ip_proto
, dir
)
1475 struct block
*b0
, *b1
, *tmp
;
1477 /* ether proto ip */
1478 b0
= gen_linktype(ETHERTYPE_IPV6
);
1483 b1
= gen_portop6(port
, ip_proto
, dir
);
1487 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1488 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1501 lookup_proto(name
, proto
)
1502 register const char *name
;
1511 v
= pcap_nametoproto(name
);
1512 if (v
== PROTO_UNDEF
)
1513 bpf_error("unknown ip proto '%s'", name
);
1517 /* XXX should look up h/w protocol type based on linktype */
1518 v
= pcap_nametoeproto(name
);
1519 if (v
== PROTO_UNDEF
)
1520 bpf_error("unknown ether proto '%s'", name
);
1541 gen_protochain(v
, proto
, dir
)
1546 #ifdef NO_PROTOCHAIN
1547 return gen_proto(v
, proto
, dir
);
1549 struct block
*b0
, *b
;
1550 struct slist
*s
[100];
1551 int fix2
, fix3
, fix4
, fix5
;
1552 int ahcheck
, again
, end
;
1554 int reg1
= alloc_reg();
1555 int reg2
= alloc_reg();
1557 memset(s
, 0, sizeof(s
));
1558 fix2
= fix3
= fix4
= fix5
= 0;
1565 b0
= gen_protochain(v
, Q_IP
, dir
);
1566 b
= gen_protochain(v
, Q_IPV6
, dir
);
1570 bpf_error("bad protocol applied for 'protochain'");
1574 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1577 * s[0] is a dummy entry to protect other BPF insn from damaged
1578 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1579 * hard to find interdependency made by jump table fixup.
1582 s
[i
] = new_stmt(0); /*dummy*/
1587 b0
= gen_linktype(ETHERTYPE_IP
);
1590 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1591 s
[i
]->s
.k
= off_nl
+ 9;
1593 /* X = ip->ip_hl << 2 */
1594 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1600 b0
= gen_linktype(ETHERTYPE_IPV6
);
1602 /* A = ip6->ip_nxt */
1603 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1604 s
[i
]->s
.k
= off_nl
+ 6;
1606 /* X = sizeof(struct ip6_hdr) */
1607 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1613 bpf_error("unsupported proto to gen_protochain");
1617 /* again: if (A == v) goto end; else fall through; */
1619 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1621 s
[i
]->s
.jt
= NULL
; /*later*/
1622 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1626 #ifndef IPPROTO_NONE
1627 #define IPPROTO_NONE 59
1629 /* if (A == IPPROTO_NONE) goto end */
1630 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1631 s
[i
]->s
.jt
= NULL
; /*later*/
1632 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1633 s
[i
]->s
.k
= IPPROTO_NONE
;
1634 s
[fix5
]->s
.jf
= s
[i
];
1639 if (proto
== Q_IPV6
) {
1640 int v6start
, v6end
, v6advance
, j
;
1643 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1644 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1645 s
[i
]->s
.jt
= NULL
; /*later*/
1646 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1647 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
1648 s
[fix2
]->s
.jf
= s
[i
];
1650 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1651 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1652 s
[i
]->s
.jt
= NULL
; /*later*/
1653 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1654 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
1656 /* if (A == IPPROTO_ROUTING) goto v6advance */
1657 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1658 s
[i
]->s
.jt
= NULL
; /*later*/
1659 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1660 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1662 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1663 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1664 s
[i
]->s
.jt
= NULL
; /*later*/
1665 s
[i
]->s
.jf
= NULL
; /*later*/
1666 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
1677 * X = X + (P[X] + 1) * 8;
1680 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1683 s
[i
] = new_stmt(BPF_ST
);
1687 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1691 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1693 /* A = P[X + packet head]; */
1694 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1698 s
[i
] = new_stmt(BPF_ST
);
1702 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1705 /* A = P[X + packet head] */
1706 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1710 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1714 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1718 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1721 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1725 /* goto again; (must use BPF_JA for backward jump) */
1726 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1727 s
[i
]->s
.k
= again
- i
- 1;
1728 s
[i
- 1]->s
.jf
= s
[i
];
1732 for (j
= v6start
; j
<= v6end
; j
++)
1733 s
[j
]->s
.jt
= s
[v6advance
];
1738 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1740 s
[fix2
]->s
.jf
= s
[i
];
1746 /* if (A == IPPROTO_AH) then fall through; else goto end; */
1747 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1748 s
[i
]->s
.jt
= NULL
; /*later*/
1749 s
[i
]->s
.jf
= NULL
; /*later*/
1750 s
[i
]->s
.k
= IPPROTO_AH
;
1752 s
[fix3
]->s
.jf
= s
[ahcheck
];
1759 * X = X + (P[X] + 2) * 4;
1762 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1765 s
[i
] = new_stmt(BPF_ST
);
1769 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1773 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1775 /* A = P[X + packet head]; */
1776 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1780 s
[i
] = new_stmt(BPF_ST
);
1784 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1787 /* A = P[X + packet head] */
1788 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1792 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1796 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1800 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1803 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1807 /* goto again; (must use BPF_JA for backward jump) */
1808 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1809 s
[i
]->s
.k
= again
- i
- 1;
1814 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1816 s
[fix2
]->s
.jt
= s
[end
];
1817 s
[fix4
]->s
.jf
= s
[end
];
1818 s
[fix5
]->s
.jt
= s
[end
];
1825 for (i
= 0; i
< max
- 1; i
++)
1826 s
[i
]->next
= s
[i
+ 1];
1827 s
[max
- 1]->next
= NULL
;
1832 b
= new_block(JMP(BPF_JEQ
));
1833 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
1844 static struct block
*
1845 gen_proto(v
, proto
, dir
)
1850 struct block
*b0
, *b1
;
1852 if (dir
!= Q_DEFAULT
)
1853 bpf_error("direction applied to 'proto'");
1858 b0
= gen_proto(v
, Q_IP
, dir
);
1859 b1
= gen_proto(v
, Q_IPV6
, dir
);
1866 b0
= gen_linktype(ETHERTYPE_IP
);
1868 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
1870 b1
= gen_protochain(v
, Q_IP
);
1876 bpf_error("arp does not encapsulate another protocol");
1880 bpf_error("rarp does not encapsulate another protocol");
1884 bpf_error("atalk encapsulation is not specifiable");
1888 bpf_error("decnet encapsulation is not specifiable");
1892 bpf_error("sca does not encapsulate another protocol");
1896 bpf_error("lat does not encapsulate another protocol");
1900 bpf_error("moprc does not encapsulate another protocol");
1904 bpf_error("mopdl does not encapsulate another protocol");
1908 return gen_linktype(v
);
1911 bpf_error("'udp proto' is bogus");
1915 bpf_error("'tcp proto' is bogus");
1919 bpf_error("'icmp proto' is bogus");
1923 bpf_error("'igmp proto' is bogus");
1927 bpf_error("'igrp proto' is bogus");
1931 bpf_error("'pim proto' is bogus");
1936 b0
= gen_linktype(ETHERTYPE_IPV6
);
1938 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
1940 b1
= gen_protochain(v
, Q_IPV6
);
1946 bpf_error("'icmp6 proto' is bogus");
1950 bpf_error("'ah proto' is bogus");
1953 bpf_error("'ah proto' is bogus");
1964 register const char *name
;
1967 int proto
= q
.proto
;
1971 bpf_u_int32 mask
, addr
;
1973 bpf_u_int32
**alist
;
1976 struct sockaddr_in
*sin
;
1977 struct sockaddr_in6
*sin6
;
1978 struct addrinfo
*res
, *res0
;
1979 struct in6_addr mask128
;
1981 struct block
*b
, *tmp
;
1982 int port
, real_proto
;
1987 addr
= pcap_nametonetaddr(name
);
1989 bpf_error("unknown network '%s'", name
);
1990 /* Left justify network addr and calculate its network mask */
1992 while (addr
&& (addr
& 0xff000000) == 0) {
1996 return gen_host(addr
, mask
, proto
, dir
);
2000 if (proto
== Q_LINK
) {
2004 eaddr
= pcap_ether_hostton(name
);
2007 "unknown ether host '%s'", name
);
2008 return gen_ehostop(eaddr
, dir
);
2011 eaddr
= pcap_ether_hostton(name
);
2014 "unknown FDDI host '%s'", name
);
2015 return gen_fhostop(eaddr
, dir
);
2019 "only ethernet/FDDI supports link-level host name");
2022 } else if (proto
== Q_DECNET
) {
2023 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2025 * I don't think DECNET hosts can be multihomed, so
2026 * there is no need to build up a list of addresses
2028 return (gen_host(dn_addr
, 0, proto
, dir
));
2031 alist
= pcap_nametoaddr(name
);
2032 if (alist
== NULL
|| *alist
== NULL
)
2033 bpf_error("unknown host '%s'", name
);
2035 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2037 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2039 tmp
= gen_host(**alist
++, 0xffffffff,
2046 memset(&mask128
, 0xff, sizeof(mask128
));
2047 res0
= res
= pcap_nametoaddrinfo(name
);
2049 bpf_error("unknown host '%s'", name
);
2051 tproto
= tproto6
= proto
;
2052 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2056 for (res
= res0
; res
; res
= res
->ai_next
) {
2057 switch (res
->ai_family
) {
2059 if (tproto
== Q_IPV6
)
2062 sin
= (struct sockaddr_in
*)
2064 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2065 0xffffffff, tproto
, dir
);
2068 if (tproto6
== Q_IP
)
2071 sin6
= (struct sockaddr_in6
*)
2073 tmp
= gen_host6(&sin6
->sin6_addr
,
2074 &mask128
, tproto6
, dir
);
2083 bpf_error("unknown host '%s'%s", name
,
2084 (proto
== Q_DEFAULT
)
2086 : " for specified address family");
2093 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2094 bpf_error("illegal qualifier of 'port'");
2095 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2096 bpf_error("unknown port '%s'", name
);
2097 if (proto
== Q_UDP
) {
2098 if (real_proto
== IPPROTO_TCP
)
2099 bpf_error("port '%s' is tcp", name
);
2101 /* override PROTO_UNDEF */
2102 real_proto
= IPPROTO_UDP
;
2104 if (proto
== Q_TCP
) {
2105 if (real_proto
== IPPROTO_UDP
)
2106 bpf_error("port '%s' is udp", name
);
2108 /* override PROTO_UNDEF */
2109 real_proto
= IPPROTO_TCP
;
2112 return gen_port(port
, real_proto
, dir
);
2116 b
= gen_port(port
, real_proto
, dir
);
2117 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2124 eaddr
= pcap_ether_hostton(name
);
2126 bpf_error("unknown ether host: %s", name
);
2128 alist
= pcap_nametoaddr(name
);
2129 if (alist
== NULL
|| *alist
== NULL
)
2130 bpf_error("unknown host '%s'", name
);
2131 return gen_gateway(eaddr
, alist
, proto
, dir
);
2133 bpf_error("'gateway' not supported in this configuration");
2137 real_proto
= lookup_proto(name
, proto
);
2138 if (real_proto
>= 0)
2139 return gen_proto(real_proto
, proto
, dir
);
2141 bpf_error("unknown protocol: %s", name
);
2144 real_proto
= lookup_proto(name
, proto
);
2145 if (real_proto
>= 0)
2146 return gen_protochain(real_proto
, proto
, dir
);
2148 bpf_error("unknown protocol: %s", name
);
2160 gen_mcode(s1
, s2
, masklen
, q
)
2161 register const char *s1
, *s2
;
2162 register int masklen
;
2165 register int nlen
, mlen
;
2168 nlen
= __pcap_atoin(s1
, &n
);
2169 /* Promote short ipaddr */
2173 mlen
= __pcap_atoin(s2
, &m
);
2174 /* Promote short ipaddr */
2177 bpf_error("non-network bits set in \"%s mask %s\"",
2180 /* Convert mask len to mask */
2182 bpf_error("mask length must be <= 32");
2183 m
= 0xffffffff << (32 - masklen
);
2185 bpf_error("non-network bits set in \"%s/%d\"",
2192 return gen_host(n
, m
, q
.proto
, q
.dir
);
2195 bpf_error("Mask syntax for networks only");
2202 register const char *s
;
2207 int proto
= q
.proto
;
2213 else if (q
.proto
== Q_DECNET
)
2214 vlen
= __pcap_atodn(s
, &v
);
2216 vlen
= __pcap_atoin(s
, &v
);
2223 if (proto
== Q_DECNET
)
2224 return gen_host(v
, 0, proto
, dir
);
2225 else if (proto
== Q_LINK
) {
2226 bpf_error("illegal link layer address");
2229 if (s
== NULL
&& q
.addr
== Q_NET
) {
2230 /* Promote short net number */
2231 while (v
&& (v
& 0xff000000) == 0) {
2236 /* Promote short ipaddr */
2240 return gen_host(v
, mask
, proto
, dir
);
2245 proto
= IPPROTO_UDP
;
2246 else if (proto
== Q_TCP
)
2247 proto
= IPPROTO_TCP
;
2248 else if (proto
== Q_DEFAULT
)
2249 proto
= PROTO_UNDEF
;
2251 bpf_error("illegal qualifier of 'port'");
2254 return gen_port((int)v
, proto
, dir
);
2258 b
= gen_port((int)v
, proto
, dir
);
2259 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2265 bpf_error("'gateway' requires a name");
2269 return gen_proto((int)v
, proto
, dir
);
2272 return gen_protochain((int)v
, proto
, dir
);
2287 gen_mcode6(s1
, s2
, masklen
, q
)
2288 register const char *s1
, *s2
;
2289 register int masklen
;
2292 struct addrinfo
*res
;
2293 struct in6_addr
*addr
;
2294 struct in6_addr mask
;
2299 bpf_error("no mask %s supported", s2
);
2301 res
= pcap_nametoaddrinfo(s1
);
2303 bpf_error("invalid ip6 address %s", s1
);
2305 bpf_error("%s resolved to multiple address", s1
);
2306 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2308 if (sizeof(mask
) * 8 < masklen
)
2309 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2310 memset(&mask
, 0xff, masklen
/ 8);
2312 mask
.s6_addr
[masklen
/ 8] =
2313 (0xff << (8 - masklen
% 8)) & 0xff;
2316 a
= (u_int32_t
*)addr
;
2317 m
= (u_int32_t
*)&mask
;
2318 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2319 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2320 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2328 bpf_error("Mask syntax for networks only");
2332 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2337 bpf_error("invalid qualifier against IPv6 address");
2345 register const u_char
*eaddr
;
2348 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2349 if (linktype
== DLT_EN10MB
)
2350 return gen_ehostop(eaddr
, (int)q
.dir
);
2351 if (linktype
== DLT_FDDI
)
2352 return gen_fhostop(eaddr
, (int)q
.dir
);
2354 bpf_error("ethernet address used in non-ether expression");
2360 struct slist
*s0
, *s1
;
2363 * This is definitely not the best way to do this, but the
2364 * lists will rarely get long.
2371 static struct slist
*
2377 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2382 static struct slist
*
2388 s
= new_stmt(BPF_LD
|BPF_MEM
);
2394 gen_load(proto
, index
, size
)
2399 struct slist
*s
, *tmp
;
2401 int regno
= alloc_reg();
2403 free_reg(index
->regno
);
2407 bpf_error("data size must be 1, 2, or 4");
2423 bpf_error("unsupported index operation");
2426 s
= xfer_to_x(index
);
2427 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2429 sappend(index
->s
, s
);
2444 /* XXX Note that we assume a fixed link header here. */
2445 s
= xfer_to_x(index
);
2446 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2449 sappend(index
->s
, s
);
2451 b
= gen_proto_abbrev(proto
);
2453 gen_and(index
->b
, b
);
2463 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2465 sappend(s
, xfer_to_a(index
));
2466 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2467 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2468 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2470 sappend(index
->s
, s
);
2472 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2474 gen_and(index
->b
, b
);
2476 gen_and(gen_proto_abbrev(Q_IP
), b
);
2482 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2486 index
->regno
= regno
;
2487 s
= new_stmt(BPF_ST
);
2489 sappend(index
->s
, s
);
2495 gen_relation(code
, a0
, a1
, reversed
)
2497 struct arth
*a0
, *a1
;
2500 struct slist
*s0
, *s1
, *s2
;
2501 struct block
*b
, *tmp
;
2505 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2506 b
= new_block(JMP(code
));
2507 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2508 reversed
= !reversed
;
2509 b
->s
.k
= 0x80000000;
2517 sappend(a0
->s
, a1
->s
);
2521 free_reg(a0
->regno
);
2522 free_reg(a1
->regno
);
2524 /* 'and' together protocol checks */
2527 gen_and(a0
->b
, tmp
= a1
->b
);
2543 int regno
= alloc_reg();
2544 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2547 s
= new_stmt(BPF_LD
|BPF_LEN
);
2548 s
->next
= new_stmt(BPF_ST
);
2549 s
->next
->s
.k
= regno
;
2564 a
= (struct arth
*)newchunk(sizeof(*a
));
2568 s
= new_stmt(BPF_LD
|BPF_IMM
);
2570 s
->next
= new_stmt(BPF_ST
);
2586 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2589 s
= new_stmt(BPF_ST
);
2597 gen_arth(code
, a0
, a1
)
2599 struct arth
*a0
, *a1
;
2601 struct slist
*s0
, *s1
, *s2
;
2605 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2610 sappend(a0
->s
, a1
->s
);
2612 free_reg(a1
->regno
);
2614 s0
= new_stmt(BPF_ST
);
2615 a0
->regno
= s0
->s
.k
= alloc_reg();
2622 * Here we handle simple allocation of the scratch registers.
2623 * If too many registers are alloc'd, the allocator punts.
2625 static int regused
[BPF_MEMWORDS
];
2629 * Return the next free register.
2634 int n
= BPF_MEMWORDS
;
2637 if (regused
[curreg
])
2638 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2640 regused
[curreg
] = 1;
2644 bpf_error("too many registers needed to evaluate expression");
2649 * Return a register to the table so it can
2659 static struct block
*
2666 s
= new_stmt(BPF_LD
|BPF_LEN
);
2667 b
= new_block(JMP(jmp
));
2678 return gen_len(BPF_JGE
, n
);
2687 b
= gen_len(BPF_JGT
, n
);
2694 gen_byteop(op
, idx
, val
)
2705 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2708 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2709 b
->s
.code
= JMP(BPF_JGE
);
2714 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2715 b
->s
.code
= JMP(BPF_JGT
);
2719 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
2723 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2727 b
= new_block(JMP(BPF_JEQ
));
2735 gen_broadcast(proto
)
2738 bpf_u_int32 hostmask
;
2739 struct block
*b0
, *b1
, *b2
;
2740 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2746 if (linktype
== DLT_EN10MB
)
2747 return gen_ehostop(ebroadcast
, Q_DST
);
2748 if (linktype
== DLT_FDDI
)
2749 return gen_fhostop(ebroadcast
, Q_DST
);
2750 bpf_error("not a broadcast link");
2754 b0
= gen_linktype(ETHERTYPE_IP
);
2755 hostmask
= ~netmask
;
2756 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
2757 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
2758 (bpf_int32
)(~0 & hostmask
), hostmask
);
2763 bpf_error("only ether/ip broadcast filters supported");
2767 gen_multicast(proto
)
2770 register struct block
*b0
, *b1
;
2771 register struct slist
*s
;
2777 if (linktype
== DLT_EN10MB
) {
2778 /* ether[0] & 1 != 0 */
2779 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2781 b0
= new_block(JMP(BPF_JSET
));
2787 if (linktype
== DLT_FDDI
) {
2788 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
2789 /* fddi[1] & 1 != 0 */
2790 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2792 b0
= new_block(JMP(BPF_JSET
));
2797 /* Link not known to support multicasts */
2801 b0
= gen_linktype(ETHERTYPE_IP
);
2802 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
2803 b1
->s
.code
= JMP(BPF_JGE
);
2809 b0
= gen_linktype(ETHERTYPE_IPV6
);
2810 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
2815 bpf_error("only IP multicast filters supported on ethernet/FDDI");
2819 * generate command for inbound/outbound. It's here so we can
2820 * make it link-type specific. 'dir' = 0 implies "inbound",
2821 * = 1 implies "outbound".
2827 register struct block
*b0
;
2829 b0
= gen_relation(BPF_JEQ
,
2830 gen_load(Q_LINK
, gen_loadi(0), 1),