]>
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.101 2000-01-29 21:01:27 fenner Exp $ (LBL)";
27 #include <sys/types.h>
28 #include <sys/socket.h>
31 #include <sys/param.h>
41 #include <netinet/in.h>
42 #include <netinet/if_ether.h>
55 #include "ethertype.h"
58 #include <pcap-namedb.h>
61 #include <sys/socket.h>
65 #ifdef HAVE_OS_PROTO_H
69 #define JMP(c) ((c)|BPF_JMP|BPF_K)
72 static jmp_buf top_ctx
;
73 static pcap_t
*bpf_pcap
;
77 int pcap_fddipad
= PCAP_FDDIPAD
;
85 bpf_error(const char *fmt
, ...)
87 bpf_error(fmt
, va_alist
)
100 (void)vsprintf(pcap_geterr(bpf_pcap
), fmt
, ap
);
106 static void init_linktype(int);
108 static int alloc_reg(void);
109 static void free_reg(int);
111 static struct block
*root
;
114 * We divy out chunks of memory rather than call malloc each time so
115 * we don't have to worry about leaking memory. It's probably
116 * not a big deal if all this memory was wasted but it this ever
117 * goes into a library that would probably not be a good idea.
120 #define CHUNK0SIZE 1024
126 static struct chunk chunks
[NCHUNKS
];
127 static int cur_chunk
;
129 static void *newchunk(u_int
);
130 static void freechunks(void);
131 static inline struct block
*new_block(int);
132 static inline struct slist
*new_stmt(int);
133 static struct block
*gen_retblk(int);
134 static inline void syntax(void);
136 static void backpatch(struct block
*, struct block
*);
137 static void merge(struct block
*, struct block
*);
138 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
139 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
140 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
141 static struct block
*gen_uncond(int);
142 static inline struct block
*gen_true(void);
143 static inline struct block
*gen_false(void);
144 static struct block
*gen_linktype(int);
145 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
147 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
149 static struct block
*gen_ehostop(const u_char
*, int);
150 static struct block
*gen_fhostop(const u_char
*, int);
151 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
152 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
154 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
156 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
157 static struct block
*gen_ipfrag(void);
158 static struct block
*gen_portatom(int, bpf_int32
);
160 static struct block
*gen_portatom6(int, bpf_int32
);
162 struct block
*gen_portop(int, int, int);
163 static struct block
*gen_port(int, int, int);
165 struct block
*gen_portop6(int, int, int);
166 static struct block
*gen_port6(int, int, int);
168 static int lookup_proto(const char *, int);
169 static struct block
*gen_proto(int, int, int);
170 static struct slist
*xfer_to_x(struct arth
*);
171 static struct slist
*xfer_to_a(struct arth
*);
172 static struct block
*gen_len(int, int);
182 /* XXX Round up to nearest long. */
183 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
185 /* XXX Round up to structure boundary. */
189 cp
= &chunks
[cur_chunk
];
190 if (n
> cp
->n_left
) {
191 ++cp
, k
= ++cur_chunk
;
193 bpf_error("out of memory");
194 size
= CHUNK0SIZE
<< k
;
195 cp
->m
= (void *)malloc(size
);
196 memset((char *)cp
->m
, 0, size
);
199 bpf_error("out of memory");
202 return (void *)((char *)cp
->m
+ cp
->n_left
);
211 for (i
= 0; i
< NCHUNKS
; ++i
)
212 if (chunks
[i
].m
!= NULL
) {
219 * A strdup whose allocations are freed after code generation is over.
223 register const char *s
;
225 int n
= strlen(s
) + 1;
226 char *cp
= newchunk(n
);
232 static inline struct block
*
238 p
= (struct block
*)newchunk(sizeof(*p
));
245 static inline struct slist
*
251 p
= (struct slist
*)newchunk(sizeof(*p
));
257 static struct block
*
261 struct block
*b
= new_block(BPF_RET
|BPF_K
);
270 bpf_error("syntax error in filter expression");
273 static bpf_u_int32 netmask
;
278 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
279 char *buf
, int optimize
, bpf_u_int32 mask
)
288 if (setjmp(top_ctx
)) {
294 snaplen
= pcap_snapshot(p
);
296 lex_init(buf
? buf
: "");
297 init_linktype(pcap_datalink(p
));
304 root
= gen_retblk(snaplen
);
306 if (optimize
&& !no_optimize
) {
309 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
310 bpf_error("expression rejects all packets");
312 program
->bf_insns
= icode_to_fcode(root
, &len
);
313 program
->bf_len
= len
;
320 * entry point for using the compiler with no pcap open
321 * pass in all the stuff that is needed explicitly instead.
324 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
325 struct bpf_program
*program
,
326 char *buf
, int optimize
, bpf_u_int32 mask
)
334 if (setjmp(top_ctx
)) {
341 /* XXX needed? I don't grok the use of globals here. */
342 snaplen
= snaplen_arg
;
344 lex_init(buf
? buf
: "");
345 init_linktype(linktype_arg
);
352 root
= gen_retblk(snaplen_arg
);
357 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
358 bpf_error("expression rejects all packets");
360 program
->bf_insns
= icode_to_fcode(root
, &len
);
361 program
->bf_len
= len
;
368 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
369 * which of the jt and jf fields has been resolved and which is a pointer
370 * back to another unresolved block (or nil). At least one of the fields
371 * in each block is already resolved.
374 backpatch(list
, target
)
375 struct block
*list
, *target
;
392 * Merge the lists in b0 and b1, using the 'sense' field to indicate
393 * which of jt and jf is the link.
397 struct block
*b0
, *b1
;
399 register struct block
**p
= &b0
;
401 /* Find end of list. */
403 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
405 /* Concatenate the lists. */
413 backpatch(p
, gen_retblk(snaplen
));
414 p
->sense
= !p
->sense
;
415 backpatch(p
, gen_retblk(0));
421 struct block
*b0
, *b1
;
423 backpatch(b0
, b1
->head
);
424 b0
->sense
= !b0
->sense
;
425 b1
->sense
= !b1
->sense
;
427 b1
->sense
= !b1
->sense
;
433 struct block
*b0
, *b1
;
435 b0
->sense
= !b0
->sense
;
436 backpatch(b0
, b1
->head
);
437 b0
->sense
= !b0
->sense
;
446 b
->sense
= !b
->sense
;
449 static struct block
*
450 gen_cmp(offset
, size
, v
)
457 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
460 b
= new_block(JMP(BPF_JEQ
));
467 static struct block
*
468 gen_mcmp(offset
, size
, v
, mask
)
473 struct block
*b
= gen_cmp(offset
, size
, v
);
476 if (mask
!= 0xffffffff) {
477 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
484 static struct block
*
485 gen_bcmp(offset
, size
, v
)
486 register u_int offset
, size
;
487 register const u_char
*v
;
489 register struct block
*b
, *tmp
;
493 register const u_char
*p
= &v
[size
- 4];
494 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
495 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
497 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
504 register const u_char
*p
= &v
[size
- 2];
505 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
507 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
514 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
523 * Various code constructs need to know the layout of the data link
524 * layer. These variables give the necessary offsets. off_linktype
525 * is set to -1 for no encapsulation, in which case, IP is assumed.
527 static u_int off_linktype
;
546 * SLIP doesn't have a link level type. The 16 byte
547 * header is hacked into our SLIP driver.
554 /* XXX this may be the same as the DLT_PPP_BSDOS case */
578 * FDDI doesn't really have a link-level type field.
579 * We assume that SSAP = SNAP is being used and pick
580 * out the encapsulated Ethernet type.
584 off_linktype
+= pcap_fddipad
;
588 off_nl
+= pcap_fddipad
;
597 case DLT_ATM_RFC1483
:
599 * assume routed, non-ISO PDUs
600 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
611 bpf_error("unknown data link type 0x%x", linktype
);
615 static struct block
*
622 s
= new_stmt(BPF_LD
|BPF_IMM
);
624 b
= new_block(JMP(BPF_JEQ
));
630 static inline struct block
*
633 return gen_uncond(1);
636 static inline struct block
*
639 return gen_uncond(0);
642 static struct block
*
646 struct block
*b0
, *b1
;
648 /* If we're not using encapsulation and checking for IP, we're done */
649 if (off_linktype
== -1 && proto
== ETHERTYPE_IP
)
658 if (proto
== ETHERTYPE_IP
)
659 proto
= PPP_IP
; /* XXX was 0x21 */
661 else if (proto
== ETHERTYPE_IPV6
)
670 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
671 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
673 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
688 case ETHERTYPE_ATALK
:
700 if (proto
== ETHERTYPE_IP
)
701 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET
)));
703 else if (proto
== ETHERTYPE_IPV6
)
704 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET6
)));
709 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
712 static struct block
*
713 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
717 u_int src_off
, dst_off
;
719 struct block
*b0
, *b1
;
733 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
734 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
740 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
741 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
748 b0
= gen_linktype(proto
);
749 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
755 static struct block
*
756 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
757 struct in6_addr
*addr
;
758 struct in6_addr
*mask
;
760 u_int src_off
, dst_off
;
762 struct block
*b0
, *b1
;
777 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
778 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
784 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
785 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
792 /* this order is important */
793 a
= (u_int32_t
*)addr
;
794 m
= (u_int32_t
*)mask
;
795 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
796 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
798 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
800 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
802 b0
= gen_linktype(proto
);
808 static struct block
*
809 gen_ehostop(eaddr
, dir
)
810 register const u_char
*eaddr
;
813 register struct block
*b0
, *b1
;
817 return gen_bcmp(6, 6, eaddr
);
820 return gen_bcmp(0, 6, eaddr
);
823 b0
= gen_ehostop(eaddr
, Q_SRC
);
824 b1
= gen_ehostop(eaddr
, Q_DST
);
830 b0
= gen_ehostop(eaddr
, Q_SRC
);
831 b1
= gen_ehostop(eaddr
, Q_DST
);
840 * Like gen_ehostop, but for DLT_FDDI
842 static struct block
*
843 gen_fhostop(eaddr
, dir
)
844 register const u_char
*eaddr
;
847 struct block
*b0
, *b1
;
852 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
854 return gen_bcmp(6 + 1, 6, eaddr
);
859 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
861 return gen_bcmp(0 + 1, 6, eaddr
);
865 b0
= gen_fhostop(eaddr
, Q_SRC
);
866 b1
= gen_fhostop(eaddr
, Q_DST
);
872 b0
= gen_fhostop(eaddr
, Q_SRC
);
873 b1
= gen_fhostop(eaddr
, Q_DST
);
882 * This is quite tricky because there may be pad bytes in front of the
883 * DECNET header, and then there are two possible data packet formats that
884 * carry both src and dst addresses, plus 5 packet types in a format that
885 * carries only the src node, plus 2 types that use a different format and
886 * also carry just the src node.
890 * Instead of doing those all right, we just look for data packets with
891 * 0 or 1 bytes of padding. If you want to look at other packets, that
892 * will require a lot more hacking.
894 * To add support for filtering on DECNET "areas" (network numbers)
895 * one would want to add a "mask" argument to this routine. That would
896 * make the filter even more inefficient, although one could be clever
897 * and not generate masking instructions if the mask is 0xFFFF.
899 static struct block
*
900 gen_dnhostop(addr
, dir
, base_off
)
905 struct block
*b0
, *b1
, *b2
, *tmp
;
906 u_int offset_lh
; /* offset if long header is received */
907 u_int offset_sh
; /* offset if short header is received */
912 offset_sh
= 1; /* follows flags */
913 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
917 offset_sh
= 3; /* follows flags, dstnode */
918 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
922 /* Inefficient because we do our Calvinball dance twice */
923 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
924 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
930 /* Inefficient because we do our Calvinball dance twice */
931 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
932 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
939 b0
= gen_linktype(ETHERTYPE_DN
);
940 /* Check for pad = 1, long header case */
941 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
942 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
943 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
944 BPF_H
, (bpf_int32
)ntohs(addr
));
946 /* Check for pad = 0, long header case */
947 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
948 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
951 /* Check for pad = 1, short header case */
952 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
953 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
954 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
955 BPF_H
, (bpf_int32
)ntohs(addr
));
958 /* Check for pad = 0, short header case */
959 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
960 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
964 /* Combine with test for linktype */
969 static struct block
*
970 gen_host(addr
, mask
, proto
, dir
)
976 struct block
*b0
, *b1
;
981 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
982 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
984 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
989 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
990 off_nl
+ 12, off_nl
+ 16);
993 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
994 off_nl
+ 14, off_nl
+ 24);
997 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
998 off_nl
+ 14, off_nl
+ 24);
1001 bpf_error("'tcp' modifier applied to host");
1004 bpf_error("'udp' modifier applied to host");
1007 bpf_error("'icmp' modifier applied to host");
1010 bpf_error("'igmp' modifier applied to host");
1013 bpf_error("'igrp' modifier applied to host");
1016 bpf_error("'pim' modifier applied to host");
1019 bpf_error("ATALK host filtering not implemented");
1022 return gen_dnhostop(addr
, dir
, off_nl
);
1025 bpf_error("SCA host filtering not implemented");
1028 bpf_error("LAT host filtering not implemented");
1031 bpf_error("MOPDL host filtering not implemented");
1034 bpf_error("MOPRC host filtering not implemented");
1038 bpf_error("'ip6' modifier applied to ip host");
1041 bpf_error("'icmp6' modifier applied to host");
1045 bpf_error("'ah' modifier applied to host");
1048 bpf_error("'esp' modifier applied to host");
1057 static struct block
*
1058 gen_host6(addr
, mask
, proto
, dir
)
1059 struct in6_addr
*addr
;
1060 struct in6_addr
*mask
;
1064 struct block
*b0
, *b1
;
1069 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1072 bpf_error("'ip' modifier applied to ip6 host");
1075 bpf_error("'rarp' modifier applied to ip6 host");
1078 bpf_error("'arp' modifier applied to ip6 host");
1081 bpf_error("'tcp' modifier applied to host");
1084 bpf_error("'udp' modifier applied to host");
1087 bpf_error("'icmp' modifier applied to host");
1090 bpf_error("'igmp' modifier applied to host");
1093 bpf_error("'igrp' modifier applied to host");
1096 bpf_error("'pim' modifier applied to host");
1099 bpf_error("ATALK host filtering not implemented");
1102 bpf_error("'decnet' modifier applied to ip6 host");
1105 bpf_error("SCA host filtering not implemented");
1108 bpf_error("LAT host filtering not implemented");
1111 bpf_error("MOPDL host filtering not implemented");
1114 bpf_error("MOPRC host filtering not implemented");
1117 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1118 off_nl
+ 8, off_nl
+ 24);
1121 bpf_error("'icmp6' modifier applied to host");
1124 bpf_error("'ah' modifier applied to host");
1127 bpf_error("'esp' modifier applied to host");
1136 static struct block
*
1137 gen_gateway(eaddr
, alist
, proto
, dir
)
1138 const u_char
*eaddr
;
1139 bpf_u_int32
**alist
;
1143 struct block
*b0
, *b1
, *tmp
;
1146 bpf_error("direction applied to 'gateway'");
1153 if (linktype
== DLT_EN10MB
)
1154 b0
= gen_ehostop(eaddr
, Q_OR
);
1155 else if (linktype
== DLT_FDDI
)
1156 b0
= gen_fhostop(eaddr
, Q_OR
);
1159 "'gateway' supported only on ethernet or FDDI");
1161 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1163 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1171 bpf_error("illegal modifier of 'gateway'");
1176 gen_proto_abbrev(proto
)
1179 struct block
*b0
, *b1
;
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
);
1204 b1
= gen_proto(2, Q_IP
, Q_DEFAULT
);
1207 #ifndef IPPROTO_IGRP
1208 #define IPPROTO_IGRP 9
1211 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1216 #define IPPROTO_PIM 103
1220 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1222 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1228 b1
= gen_linktype(ETHERTYPE_IP
);
1232 b1
= gen_linktype(ETHERTYPE_ARP
);
1236 b1
= gen_linktype(ETHERTYPE_REVARP
);
1240 bpf_error("link layer applied in wrong context");
1243 b1
= gen_linktype(ETHERTYPE_ATALK
);
1247 b1
= gen_linktype(ETHERTYPE_DN
);
1251 b1
= gen_linktype(ETHERTYPE_SCA
);
1255 b1
= gen_linktype(ETHERTYPE_LAT
);
1259 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1263 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1268 b1
= gen_linktype(ETHERTYPE_IPV6
);
1271 #ifndef IPPROTO_ICMPV6
1272 #define IPPROTO_ICMPV6 58
1275 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1280 #define IPPROTO_AH 51
1283 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1285 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1291 #define IPPROTO_ESP 50
1294 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1296 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1307 static struct block
*
1314 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1315 s
->s
.k
= off_nl
+ 6;
1316 b
= new_block(JMP(BPF_JSET
));
1324 static struct block
*
1325 gen_portatom(off
, v
)
1332 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1335 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1336 s
->next
->s
.k
= off_nl
+ off
;
1338 b
= new_block(JMP(BPF_JEQ
));
1346 static struct block
*
1347 gen_portatom6(off
, v
)
1351 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1356 gen_portop(port
, proto
, dir
)
1357 int port
, proto
, dir
;
1359 struct block
*b0
, *b1
, *tmp
;
1361 /* ip proto 'proto' */
1362 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1368 b1
= gen_portatom(0, (bpf_int32
)port
);
1372 b1
= gen_portatom(2, (bpf_int32
)port
);
1377 tmp
= gen_portatom(0, (bpf_int32
)port
);
1378 b1
= gen_portatom(2, (bpf_int32
)port
);
1383 tmp
= gen_portatom(0, (bpf_int32
)port
);
1384 b1
= gen_portatom(2, (bpf_int32
)port
);
1396 static struct block
*
1397 gen_port(port
, ip_proto
, dir
)
1402 struct block
*b0
, *b1
, *tmp
;
1404 /* ether proto ip */
1405 b0
= gen_linktype(ETHERTYPE_IP
);
1410 b1
= gen_portop(port
, ip_proto
, dir
);
1414 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1415 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1428 gen_portop6(port
, proto
, dir
)
1429 int port
, proto
, dir
;
1431 struct block
*b0
, *b1
, *tmp
;
1433 /* ip proto 'proto' */
1434 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1438 b1
= gen_portatom6(0, (bpf_int32
)port
);
1442 b1
= gen_portatom6(2, (bpf_int32
)port
);
1447 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1448 b1
= gen_portatom6(2, (bpf_int32
)port
);
1453 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1454 b1
= gen_portatom6(2, (bpf_int32
)port
);
1466 static struct block
*
1467 gen_port6(port
, ip_proto
, dir
)
1472 struct block
*b0
, *b1
, *tmp
;
1474 /* ether proto ip */
1475 b0
= gen_linktype(ETHERTYPE_IPV6
);
1480 b1
= gen_portop6(port
, ip_proto
, dir
);
1484 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1485 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1498 lookup_proto(name
, proto
)
1499 register const char *name
;
1508 v
= pcap_nametoproto(name
);
1509 if (v
== PROTO_UNDEF
)
1510 bpf_error("unknown ip proto '%s'", name
);
1514 /* XXX should look up h/w protocol type based on linktype */
1515 v
= pcap_nametoeproto(name
);
1516 if (v
== PROTO_UNDEF
)
1517 bpf_error("unknown ether proto '%s'", name
);
1535 gen_protochain(v
, proto
, dir
)
1540 #ifdef NO_PROTOCHAIN
1541 return gen_proto(v
, proto
, dir
);
1543 struct block
*b0
, *b
;
1544 struct slist
*s
[100], *sp
;
1545 int fix2
, fix3
, fix4
, fix5
;
1546 int ahcheck
, again
, end
;
1548 int reg1
= alloc_reg();
1549 int reg2
= alloc_reg();
1551 memset(s
, 0, sizeof(s
));
1552 fix2
= fix3
= fix4
= fix5
= 0;
1559 b0
= gen_protochain(v
, Q_IP
, dir
);
1560 b
= gen_protochain(v
, Q_IPV6
, dir
);
1564 bpf_error("bad protocol applied for 'protochain'");
1568 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1571 * s[0] is a dummy entry to protect other BPF insn from damaged
1572 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1573 * hard to find interdependency made by jump table fixup.
1576 s
[i
] = new_stmt(0); /*dummy*/
1581 b0
= gen_linktype(ETHERTYPE_IP
);
1584 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1585 s
[i
]->s
.k
= off_nl
+ 9;
1587 /* X = ip->ip_hl << 2 */
1588 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1594 b0
= gen_linktype(ETHERTYPE_IPV6
);
1596 /* A = ip6->ip_nxt */
1597 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1598 s
[i
]->s
.k
= off_nl
+ 6;
1600 /* X = sizeof(struct ip6_hdr) */
1601 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1607 bpf_error("unsupported proto to gen_protochain");
1611 /* again: if (A == v) goto end; else fall through; */
1613 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1615 s
[i
]->s
.jt
= NULL
; /*later*/
1616 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1620 #ifndef IPPROTO_NONE
1621 #define IPPROTO_NONE 59
1623 /* if (A == IPPROTO_NONE) goto end */
1624 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1625 s
[i
]->s
.jt
= NULL
; /*later*/
1626 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1627 s
[i
]->s
.k
= IPPROTO_NONE
;
1628 s
[fix5
]->s
.jf
= s
[i
];
1633 if (proto
== Q_IPV6
) {
1634 int v6start
, v6end
, v6advance
, j
;
1637 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1638 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1639 s
[i
]->s
.jt
= NULL
; /*later*/
1640 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1641 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
1642 s
[fix2
]->s
.jf
= s
[i
];
1644 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1645 s
[i
- 1]->s
.jf
= 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_DSTOPTS
;
1650 /* if (A == IPPROTO_ROUTING) 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_ROUTING
;
1656 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
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
; /*later*/
1660 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
1671 * X = X + (P[X] + 1) * 8;
1674 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1677 s
[i
] = new_stmt(BPF_ST
);
1681 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1685 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1687 /* A = P[X + packet head]; */
1688 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1692 s
[i
] = new_stmt(BPF_ST
);
1696 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1699 /* A = P[X + packet head] */
1700 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1704 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1708 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1712 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1715 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1719 /* goto again; (must use BPF_JA for backward jump) */
1720 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1721 s
[i
]->s
.k
= again
- i
- 1;
1722 s
[i
- 1]->s
.jf
= s
[i
];
1726 for (j
= v6start
; j
<= v6end
; j
++)
1727 s
[j
]->s
.jt
= s
[v6advance
];
1732 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1734 s
[fix2
]->s
.jf
= s
[i
];
1740 /* if (A == IPPROTO_AH) then fall through; else goto end; */
1741 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1742 s
[i
]->s
.jt
= NULL
; /*later*/
1743 s
[i
]->s
.jf
= NULL
; /*later*/
1744 s
[i
]->s
.k
= IPPROTO_AH
;
1746 s
[fix3
]->s
.jf
= s
[ahcheck
];
1753 * X = X + (P[X] + 2) * 4;
1756 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1759 s
[i
] = new_stmt(BPF_ST
);
1763 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1767 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1769 /* A = P[X + packet head]; */
1770 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1774 s
[i
] = new_stmt(BPF_ST
);
1778 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1781 /* A = P[X + packet head] */
1782 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1786 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1790 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1794 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1797 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1801 /* goto again; (must use BPF_JA for backward jump) */
1802 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1803 s
[i
]->s
.k
= again
- i
- 1;
1808 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1810 s
[fix2
]->s
.jt
= s
[end
];
1811 s
[fix4
]->s
.jf
= s
[end
];
1812 s
[fix5
]->s
.jt
= s
[end
];
1819 for (i
= 0; i
< max
- 1; i
++)
1820 s
[i
]->next
= s
[i
+ 1];
1821 s
[max
- 1]->next
= NULL
;
1826 b
= new_block(JMP(BPF_JEQ
));
1827 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
1838 static struct block
*
1839 gen_proto(v
, proto
, dir
)
1844 struct block
*b0
, *b1
;
1846 if (dir
!= Q_DEFAULT
)
1847 bpf_error("direction applied to 'proto'");
1852 b0
= gen_proto(v
, Q_IP
, dir
);
1853 b1
= gen_proto(v
, Q_IPV6
, dir
);
1860 b0
= gen_linktype(ETHERTYPE_IP
);
1862 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
1864 b1
= gen_protochain(v
, Q_IP
);
1870 bpf_error("arp does not encapsulate another protocol");
1874 bpf_error("rarp does not encapsulate another protocol");
1878 bpf_error("atalk encapsulation is not specifiable");
1882 bpf_error("decnet encapsulation is not specifiable");
1886 bpf_error("sca does not encapsulate another protocol");
1890 bpf_error("lat does not encapsulate another protocol");
1894 bpf_error("moprc does not encapsulate another protocol");
1898 bpf_error("mopdl does not encapsulate another protocol");
1902 return gen_linktype(v
);
1905 bpf_error("'udp proto' is bogus");
1909 bpf_error("'tcp proto' is bogus");
1913 bpf_error("'icmp proto' is bogus");
1917 bpf_error("'igmp proto' is bogus");
1921 bpf_error("'igrp proto' is bogus");
1925 bpf_error("'pim proto' is bogus");
1930 b0
= gen_linktype(ETHERTYPE_IPV6
);
1932 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
1934 b1
= gen_protochain(v
, Q_IPV6
);
1940 bpf_error("'icmp6 proto' is bogus");
1944 bpf_error("'ah proto' is bogus");
1947 bpf_error("'ah proto' is bogus");
1958 register const char *name
;
1961 int proto
= q
.proto
;
1965 bpf_u_int32 mask
, addr
, **alist
;
1968 struct sockaddr_in
*sin
;
1969 struct sockaddr_in6
*sin6
;
1970 struct addrinfo
*res
, *res0
;
1971 struct in6_addr mask128
;
1973 struct block
*b
, *tmp
;
1974 int port
, real_proto
;
1979 addr
= pcap_nametonetaddr(name
);
1981 bpf_error("unknown network '%s'", name
);
1982 /* Left justify network addr and calculate its network mask */
1984 while (addr
&& (addr
& 0xff000000) == 0) {
1988 return gen_host(addr
, mask
, proto
, dir
);
1992 if (proto
== Q_LINK
) {
1996 eaddr
= pcap_ether_hostton(name
);
1999 "unknown ether host '%s'", name
);
2000 return gen_ehostop(eaddr
, dir
);
2003 eaddr
= pcap_ether_hostton(name
);
2006 "unknown FDDI host '%s'", name
);
2007 return gen_fhostop(eaddr
, dir
);
2011 "only ethernet/FDDI supports link-level host name");
2014 } else if (proto
== Q_DECNET
) {
2015 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2017 * I don't think DECNET hosts can be multihomed, so
2018 * there is no need to build up a list of addresses
2020 return (gen_host(dn_addr
, 0, proto
, dir
));
2023 alist
= pcap_nametoaddr(name
);
2024 if (alist
== NULL
|| *alist
== NULL
)
2025 bpf_error("unknown host '%s'", name
);
2027 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2029 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2031 tmp
= gen_host(**alist
++, 0xffffffff,
2038 memset(&mask128
, 0xff, sizeof(mask128
));
2039 res0
= res
= pcap_nametoaddr(name
);
2041 bpf_error("unknown host '%s'", name
);
2043 tproto
= tproto6
= proto
;
2044 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2049 switch (res
->ai_family
) {
2051 sin
= (struct sockaddr_in
*)
2053 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2054 0xffffffff, tproto
, dir
);
2057 sin6
= (struct sockaddr_in6
*)
2059 tmp
= gen_host6(&sin6
->sin6_addr
,
2060 &mask128
, tproto6
, dir
);
2075 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2076 bpf_error("illegal qualifier of 'port'");
2077 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2078 bpf_error("unknown port '%s'", name
);
2079 if (proto
== Q_UDP
) {
2080 if (real_proto
== IPPROTO_TCP
)
2081 bpf_error("port '%s' is tcp", name
);
2083 /* override PROTO_UNDEF */
2084 real_proto
= IPPROTO_UDP
;
2086 if (proto
== Q_TCP
) {
2087 if (real_proto
== IPPROTO_UDP
)
2088 bpf_error("port '%s' is udp", name
);
2090 /* override PROTO_UNDEF */
2091 real_proto
= IPPROTO_TCP
;
2094 return gen_port(port
, real_proto
, dir
);
2098 b
= gen_port(port
, real_proto
, dir
);
2099 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2106 eaddr
= pcap_ether_hostton(name
);
2108 bpf_error("unknown ether host: %s", name
);
2110 alist
= pcap_nametoaddr(name
);
2111 if (alist
== NULL
|| *alist
== NULL
)
2112 bpf_error("unknown host '%s'", name
);
2113 return gen_gateway(eaddr
, alist
, proto
, dir
);
2115 bpf_error("'gateway' not supported in this configuration");
2119 real_proto
= lookup_proto(name
, proto
);
2120 if (real_proto
>= 0)
2121 return gen_proto(real_proto
, proto
, dir
);
2123 bpf_error("unknown protocol: %s", name
);
2126 real_proto
= lookup_proto(name
, proto
);
2127 if (real_proto
>= 0)
2128 return gen_protochain(real_proto
, proto
, dir
);
2130 bpf_error("unknown protocol: %s", name
);
2142 gen_mcode(s1
, s2
, masklen
, q
)
2143 register const char *s1
, *s2
;
2144 register int masklen
;
2147 register int nlen
, mlen
;
2150 nlen
= __pcap_atoin(s1
, &n
);
2151 /* Promote short ipaddr */
2155 mlen
= __pcap_atoin(s2
, &m
);
2156 /* Promote short ipaddr */
2159 bpf_error("non-network bits set in \"%s mask %s\"",
2162 /* Convert mask len to mask */
2164 bpf_error("mask length must be <= 32");
2165 m
= 0xffffffff << (32 - masklen
);
2167 bpf_error("non-network bits set in \"%s/%d\"",
2174 return gen_host(n
, m
, q
.proto
, q
.dir
);
2177 bpf_error("Mask syntax for networks only");
2184 register const char *s
;
2189 int proto
= q
.proto
;
2195 else if (q
.proto
== Q_DECNET
)
2196 vlen
= __pcap_atodn(s
, &v
);
2198 vlen
= __pcap_atoin(s
, &v
);
2205 if (proto
== Q_DECNET
)
2206 return gen_host(v
, 0, proto
, dir
);
2207 else if (proto
== Q_LINK
) {
2208 bpf_error("illegal link layer address");
2211 if (s
== NULL
&& q
.addr
== Q_NET
) {
2212 /* Promote short net number */
2213 while (v
&& (v
& 0xff000000) == 0) {
2218 /* Promote short ipaddr */
2222 return gen_host(v
, mask
, proto
, dir
);
2227 proto
= IPPROTO_UDP
;
2228 else if (proto
== Q_TCP
)
2229 proto
= IPPROTO_TCP
;
2230 else if (proto
== Q_DEFAULT
)
2231 proto
= PROTO_UNDEF
;
2233 bpf_error("illegal qualifier of 'port'");
2236 return gen_port((int)v
, proto
, dir
);
2240 b
= gen_port((int)v
, proto
, dir
);
2241 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2247 bpf_error("'gateway' requires a name");
2251 return gen_proto((int)v
, proto
, dir
);
2254 return gen_protochain((int)v
, proto
, dir
);
2269 gen_mcode6(s1
, s2
, masklen
, q
)
2270 register const char *s1
, *s2
;
2271 register int masklen
;
2274 struct addrinfo
*res
;
2275 struct in6_addr
*addr
;
2276 struct in6_addr mask
;
2281 bpf_error("no mask %s supported", s2
);
2283 res
= pcap_nametoaddr(s1
);
2285 bpf_error("invalid ip6 address %s", s1
);
2287 bpf_error("%s resolved to multiple address", s1
);
2288 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2290 if (sizeof(mask
) * 8 < masklen
)
2291 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2292 memset(&mask
, 0xff, masklen
/ 8);
2294 mask
.s6_addr
[masklen
/ 8] =
2295 (0xff << (8 - masklen
% 8)) & 0xff;
2298 a
= (u_int32_t
*)addr
;
2299 m
= (u_int32_t
*)&mask
;
2300 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2301 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2302 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2310 bpf_error("Mask syntax for networks only");
2314 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2319 bpf_error("invalid qualifier against IPv6 address");
2327 register const u_char
*eaddr
;
2330 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2331 if (linktype
== DLT_EN10MB
)
2332 return gen_ehostop(eaddr
, (int)q
.dir
);
2333 if (linktype
== DLT_FDDI
)
2334 return gen_fhostop(eaddr
, (int)q
.dir
);
2336 bpf_error("ethernet address used in non-ether expression");
2342 struct slist
*s0
, *s1
;
2345 * This is definitely not the best way to do this, but the
2346 * lists will rarely get long.
2353 static struct slist
*
2359 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2364 static struct slist
*
2370 s
= new_stmt(BPF_LD
|BPF_MEM
);
2376 gen_load(proto
, index
, size
)
2381 struct slist
*s
, *tmp
;
2383 int regno
= alloc_reg();
2385 free_reg(index
->regno
);
2389 bpf_error("data size must be 1, 2, or 4");
2405 bpf_error("unsupported index operation");
2408 s
= xfer_to_x(index
);
2409 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2411 sappend(index
->s
, s
);
2426 /* XXX Note that we assume a fixed link header here. */
2427 s
= xfer_to_x(index
);
2428 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2431 sappend(index
->s
, s
);
2433 b
= gen_proto_abbrev(proto
);
2435 gen_and(index
->b
, b
);
2445 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2447 sappend(s
, xfer_to_a(index
));
2448 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2449 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2450 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2452 sappend(index
->s
, s
);
2454 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2456 gen_and(index
->b
, b
);
2458 gen_and(gen_proto_abbrev(Q_IP
), b
);
2464 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2468 index
->regno
= regno
;
2469 s
= new_stmt(BPF_ST
);
2471 sappend(index
->s
, s
);
2477 gen_relation(code
, a0
, a1
, reversed
)
2479 struct arth
*a0
, *a1
;
2482 struct slist
*s0
, *s1
, *s2
;
2483 struct block
*b
, *tmp
;
2487 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2488 b
= new_block(JMP(code
));
2489 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2490 reversed
= !reversed
;
2491 b
->s
.k
= 0x80000000;
2499 sappend(a0
->s
, a1
->s
);
2503 free_reg(a0
->regno
);
2504 free_reg(a1
->regno
);
2506 /* 'and' together protocol checks */
2509 gen_and(a0
->b
, tmp
= a1
->b
);
2525 int regno
= alloc_reg();
2526 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2529 s
= new_stmt(BPF_LD
|BPF_LEN
);
2530 s
->next
= new_stmt(BPF_ST
);
2531 s
->next
->s
.k
= regno
;
2546 a
= (struct arth
*)newchunk(sizeof(*a
));
2550 s
= new_stmt(BPF_LD
|BPF_IMM
);
2552 s
->next
= new_stmt(BPF_ST
);
2568 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2571 s
= new_stmt(BPF_ST
);
2579 gen_arth(code
, a0
, a1
)
2581 struct arth
*a0
, *a1
;
2583 struct slist
*s0
, *s1
, *s2
;
2587 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2592 sappend(a0
->s
, a1
->s
);
2594 free_reg(a1
->regno
);
2596 s0
= new_stmt(BPF_ST
);
2597 a0
->regno
= s0
->s
.k
= alloc_reg();
2604 * Here we handle simple allocation of the scratch registers.
2605 * If too many registers are alloc'd, the allocator punts.
2607 static int regused
[BPF_MEMWORDS
];
2611 * Return the next free register.
2616 int n
= BPF_MEMWORDS
;
2619 if (regused
[curreg
])
2620 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2622 regused
[curreg
] = 1;
2626 bpf_error("too many registers needed to evaluate expression");
2631 * Return a register to the table so it can
2641 static struct block
*
2648 s
= new_stmt(BPF_LD
|BPF_LEN
);
2649 b
= new_block(JMP(jmp
));
2660 return gen_len(BPF_JGE
, n
);
2669 b
= gen_len(BPF_JGT
, n
);
2676 gen_byteop(op
, idx
, val
)
2687 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2690 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2691 b
->s
.code
= JMP(BPF_JGE
);
2696 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2697 b
->s
.code
= JMP(BPF_JGT
);
2701 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
2705 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2709 b
= new_block(JMP(BPF_JEQ
));
2717 gen_broadcast(proto
)
2720 bpf_u_int32 hostmask
;
2721 struct block
*b0
, *b1
, *b2
;
2722 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2728 if (linktype
== DLT_EN10MB
)
2729 return gen_ehostop(ebroadcast
, Q_DST
);
2730 if (linktype
== DLT_FDDI
)
2731 return gen_fhostop(ebroadcast
, Q_DST
);
2732 bpf_error("not a broadcast link");
2736 b0
= gen_linktype(ETHERTYPE_IP
);
2737 hostmask
= ~netmask
;
2738 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
2739 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
2740 (bpf_int32
)(~0 & hostmask
), hostmask
);
2745 bpf_error("only ether/ip broadcast filters supported");
2749 gen_multicast(proto
)
2752 register struct block
*b0
, *b1
;
2753 register struct slist
*s
;
2759 if (linktype
== DLT_EN10MB
) {
2760 /* ether[0] & 1 != 0 */
2761 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2763 b0
= new_block(JMP(BPF_JSET
));
2769 if (linktype
== DLT_FDDI
) {
2770 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
2771 /* fddi[1] & 1 != 0 */
2772 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2774 b0
= new_block(JMP(BPF_JSET
));
2779 /* Link not known to support multicasts */
2783 b0
= gen_linktype(ETHERTYPE_IP
);
2784 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
2785 b1
->s
.code
= JMP(BPF_JGE
);
2791 b0
= gen_linktype(ETHERTYPE_IPV6
);
2792 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
2797 bpf_error("only IP multicast filters supported on ethernet/FDDI");
2801 * generate command for inbound/outbound. It's here so we can
2802 * make it link-type specific. 'dir' = 0 implies "inbound",
2803 * = 1 implies "outbound".
2809 register struct block
*b0
;
2811 b0
= gen_relation(BPF_JEQ
,
2812 gen_load(Q_LINK
, gen_loadi(0), 1),