]>
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.110 2000-06-03 16:29:42 itojun 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)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
107 static void init_linktype(int);
109 static int alloc_reg(void);
110 static void free_reg(int);
112 static struct block
*root
;
115 * We divy out chunks of memory rather than call malloc each time so
116 * we don't have to worry about leaking memory. It's probably
117 * not a big deal if all this memory was wasted but it this ever
118 * goes into a library that would probably not be a good idea.
121 #define CHUNK0SIZE 1024
127 static struct chunk chunks
[NCHUNKS
];
128 static int cur_chunk
;
130 static void *newchunk(u_int
);
131 static void freechunks(void);
132 static inline struct block
*new_block(int);
133 static inline struct slist
*new_stmt(int);
134 static struct block
*gen_retblk(int);
135 static inline void syntax(void);
137 static void backpatch(struct block
*, struct block
*);
138 static void merge(struct block
*, struct block
*);
139 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
140 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
141 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
142 static struct block
*gen_uncond(int);
143 static inline struct block
*gen_true(void);
144 static inline struct block
*gen_false(void);
145 static struct block
*gen_linktype(int);
146 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
148 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
150 static struct block
*gen_ehostop(const u_char
*, int);
151 static struct block
*gen_fhostop(const u_char
*, int);
152 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
153 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
155 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
158 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
160 static struct block
*gen_ipfrag(void);
161 static struct block
*gen_portatom(int, bpf_int32
);
163 static struct block
*gen_portatom6(int, bpf_int32
);
165 struct block
*gen_portop(int, int, int);
166 static struct block
*gen_port(int, int, int);
168 struct block
*gen_portop6(int, int, int);
169 static struct block
*gen_port6(int, int, int);
171 static int lookup_proto(const char *, int);
172 static struct block
*gen_proto(int, int, int);
173 static struct slist
*xfer_to_x(struct arth
*);
174 static struct slist
*xfer_to_a(struct arth
*);
175 static struct block
*gen_len(int, int);
185 /* XXX Round up to nearest long. */
186 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
188 /* XXX Round up to structure boundary. */
192 cp
= &chunks
[cur_chunk
];
193 if (n
> cp
->n_left
) {
194 ++cp
, k
= ++cur_chunk
;
196 bpf_error("out of memory");
197 size
= CHUNK0SIZE
<< k
;
198 cp
->m
= (void *)malloc(size
);
199 memset((char *)cp
->m
, 0, size
);
202 bpf_error("out of memory");
205 return (void *)((char *)cp
->m
+ cp
->n_left
);
214 for (i
= 0; i
< NCHUNKS
; ++i
)
215 if (chunks
[i
].m
!= NULL
) {
222 * A strdup whose allocations are freed after code generation is over.
226 register const char *s
;
228 int n
= strlen(s
) + 1;
229 char *cp
= newchunk(n
);
235 static inline struct block
*
241 p
= (struct block
*)newchunk(sizeof(*p
));
248 static inline struct slist
*
254 p
= (struct slist
*)newchunk(sizeof(*p
));
260 static struct block
*
264 struct block
*b
= new_block(BPF_RET
|BPF_K
);
273 bpf_error("syntax error in filter expression");
276 static bpf_u_int32 netmask
;
281 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
282 char *buf
, int optimize
, bpf_u_int32 mask
)
291 if (setjmp(top_ctx
)) {
297 snaplen
= pcap_snapshot(p
);
299 lex_init(buf
? buf
: "");
300 init_linktype(pcap_datalink(p
));
307 root
= gen_retblk(snaplen
);
309 if (optimize
&& !no_optimize
) {
312 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
313 bpf_error("expression rejects all packets");
315 program
->bf_insns
= icode_to_fcode(root
, &len
);
316 program
->bf_len
= len
;
323 * entry point for using the compiler with no pcap open
324 * pass in all the stuff that is needed explicitly instead.
327 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
328 struct bpf_program
*program
,
329 char *buf
, int optimize
, bpf_u_int32 mask
)
337 if (setjmp(top_ctx
)) {
344 /* XXX needed? I don't grok the use of globals here. */
345 snaplen
= snaplen_arg
;
347 lex_init(buf
? buf
: "");
348 init_linktype(linktype_arg
);
355 root
= gen_retblk(snaplen_arg
);
360 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
361 bpf_error("expression rejects all packets");
363 program
->bf_insns
= icode_to_fcode(root
, &len
);
364 program
->bf_len
= len
;
371 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
372 * which of the jt and jf fields has been resolved and which is a pointer
373 * back to another unresolved block (or nil). At least one of the fields
374 * in each block is already resolved.
377 backpatch(list
, target
)
378 struct block
*list
, *target
;
395 * Merge the lists in b0 and b1, using the 'sense' field to indicate
396 * which of jt and jf is the link.
400 struct block
*b0
, *b1
;
402 register struct block
**p
= &b0
;
404 /* Find end of list. */
406 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
408 /* Concatenate the lists. */
416 backpatch(p
, gen_retblk(snaplen
));
417 p
->sense
= !p
->sense
;
418 backpatch(p
, gen_retblk(0));
424 struct block
*b0
, *b1
;
426 backpatch(b0
, b1
->head
);
427 b0
->sense
= !b0
->sense
;
428 b1
->sense
= !b1
->sense
;
430 b1
->sense
= !b1
->sense
;
436 struct block
*b0
, *b1
;
438 b0
->sense
= !b0
->sense
;
439 backpatch(b0
, b1
->head
);
440 b0
->sense
= !b0
->sense
;
449 b
->sense
= !b
->sense
;
452 static struct block
*
453 gen_cmp(offset
, size
, v
)
460 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
463 b
= new_block(JMP(BPF_JEQ
));
470 static struct block
*
471 gen_mcmp(offset
, size
, v
, mask
)
476 struct block
*b
= gen_cmp(offset
, size
, v
);
479 if (mask
!= 0xffffffff) {
480 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
487 static struct block
*
488 gen_bcmp(offset
, size
, v
)
489 register u_int offset
, size
;
490 register const u_char
*v
;
492 register struct block
*b
, *tmp
;
496 register const u_char
*p
= &v
[size
- 4];
497 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
498 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
500 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
507 register const u_char
*p
= &v
[size
- 2];
508 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
510 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
517 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
526 * Various code constructs need to know the layout of the data link
527 * layer. These variables give the necessary offsets. off_linktype
528 * is set to -1 for no encapsulation, in which case, IP is assumed.
530 static u_int off_linktype
;
549 * SLIP doesn't have a link level type. The 16 byte
550 * header is hacked into our SLIP driver.
557 /* XXX this may be the same as the DLT_PPP_BSDOS case */
581 * FDDI doesn't really have a link-level type field.
582 * We assume that SSAP = SNAP is being used and pick
583 * out the encapsulated Ethernet type.
587 off_linktype
+= pcap_fddipad
;
591 off_nl
+= pcap_fddipad
;
600 case DLT_ATM_RFC1483
:
602 * assume routed, non-ISO PDUs
603 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
614 bpf_error("unknown data link type 0x%x", linktype
);
618 static struct block
*
625 s
= new_stmt(BPF_LD
|BPF_IMM
);
627 b
= new_block(JMP(BPF_JEQ
));
633 static inline struct block
*
636 return gen_uncond(1);
639 static inline struct block
*
642 return gen_uncond(0);
645 static struct block
*
649 struct block
*b0
, *b1
;
651 /* If we're not using encapsulation and checking for IP, we're done */
652 if (off_linktype
== -1 && proto
== ETHERTYPE_IP
)
661 if (proto
== ETHERTYPE_IP
)
662 proto
= PPP_IP
; /* XXX was 0x21 */
664 else if (proto
== ETHERTYPE_IPV6
)
673 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
674 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
676 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
691 case ETHERTYPE_ATALK
:
703 if (proto
== ETHERTYPE_IP
)
704 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET
)));
706 else if (proto
== ETHERTYPE_IPV6
)
707 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET6
)));
712 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
715 static struct block
*
716 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
720 u_int src_off
, dst_off
;
722 struct block
*b0
, *b1
;
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
);
743 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
744 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
751 b0
= gen_linktype(proto
);
752 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
758 static struct block
*
759 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
760 struct in6_addr
*addr
;
761 struct in6_addr
*mask
;
763 u_int src_off
, dst_off
;
765 struct block
*b0
, *b1
;
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
);
787 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
788 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
795 /* this order is important */
796 a
= (u_int32_t
*)addr
;
797 m
= (u_int32_t
*)mask
;
798 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
799 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
801 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
803 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
805 b0
= gen_linktype(proto
);
811 static struct block
*
812 gen_ehostop(eaddr
, dir
)
813 register const u_char
*eaddr
;
816 register struct block
*b0
, *b1
;
820 return gen_bcmp(6, 6, eaddr
);
823 return gen_bcmp(0, 6, eaddr
);
826 b0
= gen_ehostop(eaddr
, Q_SRC
);
827 b1
= gen_ehostop(eaddr
, Q_DST
);
833 b0
= gen_ehostop(eaddr
, Q_SRC
);
834 b1
= gen_ehostop(eaddr
, Q_DST
);
843 * Like gen_ehostop, but for DLT_FDDI
845 static struct block
*
846 gen_fhostop(eaddr
, dir
)
847 register const u_char
*eaddr
;
850 struct block
*b0
, *b1
;
855 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
857 return gen_bcmp(6 + 1, 6, eaddr
);
862 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
864 return gen_bcmp(0 + 1, 6, eaddr
);
868 b0
= gen_fhostop(eaddr
, Q_SRC
);
869 b1
= gen_fhostop(eaddr
, Q_DST
);
875 b0
= gen_fhostop(eaddr
, Q_SRC
);
876 b1
= gen_fhostop(eaddr
, Q_DST
);
885 * This is quite tricky because there may be pad bytes in front of the
886 * DECNET header, and then there are two possible data packet formats that
887 * carry both src and dst addresses, plus 5 packet types in a format that
888 * carries only the src node, plus 2 types that use a different format and
889 * also carry just the src node.
893 * Instead of doing those all right, we just look for data packets with
894 * 0 or 1 bytes of padding. If you want to look at other packets, that
895 * will require a lot more hacking.
897 * To add support for filtering on DECNET "areas" (network numbers)
898 * one would want to add a "mask" argument to this routine. That would
899 * make the filter even more inefficient, although one could be clever
900 * and not generate masking instructions if the mask is 0xFFFF.
902 static struct block
*
903 gen_dnhostop(addr
, dir
, base_off
)
908 struct block
*b0
, *b1
, *b2
, *tmp
;
909 u_int offset_lh
; /* offset if long header is received */
910 u_int offset_sh
; /* offset if short header is received */
915 offset_sh
= 1; /* follows flags */
916 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
920 offset_sh
= 3; /* follows flags, dstnode */
921 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
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
);
933 /* Inefficient because we do our Calvinball dance twice */
934 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
935 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
942 b0
= gen_linktype(ETHERTYPE_DN
);
943 /* Check for pad = 1, long header case */
944 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
945 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
946 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
947 BPF_H
, (bpf_int32
)ntohs(addr
));
949 /* Check for pad = 0, long header case */
950 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
951 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
954 /* Check for pad = 1, short header case */
955 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
956 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
957 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
958 BPF_H
, (bpf_int32
)ntohs(addr
));
961 /* Check for pad = 0, short header case */
962 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
963 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
967 /* Combine with test for linktype */
972 static struct block
*
973 gen_host(addr
, mask
, proto
, dir
)
979 struct block
*b0
, *b1
;
984 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
985 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
987 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
992 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
993 off_nl
+ 12, off_nl
+ 16);
996 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
997 off_nl
+ 14, off_nl
+ 24);
1000 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1001 off_nl
+ 14, off_nl
+ 24);
1004 bpf_error("'tcp' modifier applied to host");
1007 bpf_error("'udp' modifier applied to host");
1010 bpf_error("'icmp' modifier applied to host");
1013 bpf_error("'igmp' modifier applied to host");
1016 bpf_error("'igrp' modifier applied to host");
1019 bpf_error("'pim' modifier applied to host");
1022 bpf_error("ATALK host filtering not implemented");
1025 return gen_dnhostop(addr
, dir
, off_nl
);
1028 bpf_error("SCA host filtering not implemented");
1031 bpf_error("LAT host filtering not implemented");
1034 bpf_error("MOPDL host filtering not implemented");
1037 bpf_error("MOPRC host filtering not implemented");
1041 bpf_error("'ip6' modifier applied to ip host");
1044 bpf_error("'icmp6' modifier applied to host");
1048 bpf_error("'ah' modifier applied to host");
1051 bpf_error("'esp' modifier applied to host");
1060 static struct block
*
1061 gen_host6(addr
, mask
, proto
, dir
)
1062 struct in6_addr
*addr
;
1063 struct in6_addr
*mask
;
1070 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1073 bpf_error("'ip' modifier applied to ip6 host");
1076 bpf_error("'rarp' modifier applied to ip6 host");
1079 bpf_error("'arp' modifier applied to ip6 host");
1082 bpf_error("'tcp' modifier applied to host");
1085 bpf_error("'udp' modifier applied to host");
1088 bpf_error("'icmp' modifier applied to host");
1091 bpf_error("'igmp' modifier applied to host");
1094 bpf_error("'igrp' modifier applied to host");
1097 bpf_error("'pim' modifier applied to host");
1100 bpf_error("ATALK host filtering not implemented");
1103 bpf_error("'decnet' modifier applied to ip6 host");
1106 bpf_error("SCA host filtering not implemented");
1109 bpf_error("LAT host filtering not implemented");
1112 bpf_error("MOPDL host filtering not implemented");
1115 bpf_error("MOPRC host filtering not implemented");
1118 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1119 off_nl
+ 8, off_nl
+ 24);
1122 bpf_error("'icmp6' modifier applied to host");
1125 bpf_error("'ah' modifier applied to host");
1128 bpf_error("'esp' modifier applied to host");
1138 static struct block
*
1139 gen_gateway(eaddr
, alist
, proto
, dir
)
1140 const u_char
*eaddr
;
1141 bpf_u_int32
**alist
;
1145 struct block
*b0
, *b1
, *tmp
;
1148 bpf_error("direction applied to 'gateway'");
1155 if (linktype
== DLT_EN10MB
)
1156 b0
= gen_ehostop(eaddr
, Q_OR
);
1157 else if (linktype
== DLT_FDDI
)
1158 b0
= gen_fhostop(eaddr
, Q_OR
);
1161 "'gateway' supported only on ethernet or FDDI");
1163 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1165 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1173 bpf_error("illegal modifier of 'gateway'");
1179 gen_proto_abbrev(proto
)
1182 struct block
*b0
, *b1
;
1187 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1189 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1195 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1197 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1203 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1206 #ifndef IPPROTO_IGMP
1207 #define IPPROTO_IGMP 2
1211 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1214 #ifndef IPPROTO_IGRP
1215 #define IPPROTO_IGRP 9
1218 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1222 #define IPPROTO_PIM 103
1226 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1228 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1234 b1
= gen_linktype(ETHERTYPE_IP
);
1238 b1
= gen_linktype(ETHERTYPE_ARP
);
1242 b1
= gen_linktype(ETHERTYPE_REVARP
);
1246 bpf_error("link layer applied in wrong context");
1249 b1
= gen_linktype(ETHERTYPE_ATALK
);
1253 b1
= gen_linktype(ETHERTYPE_DN
);
1257 b1
= gen_linktype(ETHERTYPE_SCA
);
1261 b1
= gen_linktype(ETHERTYPE_LAT
);
1265 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1269 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1274 b1
= gen_linktype(ETHERTYPE_IPV6
);
1277 #ifndef IPPROTO_ICMPV6
1278 #define IPPROTO_ICMPV6 58
1281 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1286 #define IPPROTO_AH 51
1289 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1291 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1297 #define IPPROTO_ESP 50
1300 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1302 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1313 static struct block
*
1320 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1321 s
->s
.k
= off_nl
+ 6;
1322 b
= new_block(JMP(BPF_JSET
));
1330 static struct block
*
1331 gen_portatom(off
, v
)
1338 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1341 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1342 s
->next
->s
.k
= off_nl
+ off
;
1344 b
= new_block(JMP(BPF_JEQ
));
1352 static struct block
*
1353 gen_portatom6(off
, v
)
1357 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1362 gen_portop(port
, proto
, dir
)
1363 int port
, proto
, dir
;
1365 struct block
*b0
, *b1
, *tmp
;
1367 /* ip proto 'proto' */
1368 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1374 b1
= 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
);
1389 tmp
= gen_portatom(0, (bpf_int32
)port
);
1390 b1
= gen_portatom(2, (bpf_int32
)port
);
1402 static struct block
*
1403 gen_port(port
, ip_proto
, dir
)
1408 struct block
*b0
, *b1
, *tmp
;
1410 /* ether proto ip */
1411 b0
= gen_linktype(ETHERTYPE_IP
);
1416 b1
= gen_portop(port
, ip_proto
, dir
);
1420 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1421 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1434 gen_portop6(port
, proto
, dir
)
1435 int port
, proto
, dir
;
1437 struct block
*b0
, *b1
, *tmp
;
1439 /* ip proto 'proto' */
1440 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1444 b1
= 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
);
1459 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1460 b1
= gen_portatom6(2, (bpf_int32
)port
);
1472 static struct block
*
1473 gen_port6(port
, ip_proto
, dir
)
1478 struct block
*b0
, *b1
, *tmp
;
1480 /* ether proto ip */
1481 b0
= gen_linktype(ETHERTYPE_IPV6
);
1486 b1
= gen_portop6(port
, ip_proto
, dir
);
1490 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1491 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1504 lookup_proto(name
, proto
)
1505 register const char *name
;
1514 v
= pcap_nametoproto(name
);
1515 if (v
== PROTO_UNDEF
)
1516 bpf_error("unknown ip proto '%s'", name
);
1520 /* XXX should look up h/w protocol type based on linktype */
1521 v
= pcap_nametoeproto(name
);
1522 if (v
== PROTO_UNDEF
)
1523 bpf_error("unknown ether proto '%s'", name
);
1544 gen_protochain(v
, proto
, dir
)
1549 #ifdef NO_PROTOCHAIN
1550 return gen_proto(v
, proto
, dir
);
1552 struct block
*b0
, *b
;
1553 struct slist
*s
[100];
1554 int fix2
, fix3
, fix4
, fix5
;
1555 int ahcheck
, again
, end
;
1557 int reg1
= alloc_reg();
1558 int reg2
= alloc_reg();
1560 memset(s
, 0, sizeof(s
));
1561 fix2
= fix3
= fix4
= fix5
= 0;
1568 b0
= gen_protochain(v
, Q_IP
, dir
);
1569 b
= gen_protochain(v
, Q_IPV6
, dir
);
1573 bpf_error("bad protocol applied for 'protochain'");
1577 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1580 * s[0] is a dummy entry to protect other BPF insn from damaged
1581 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1582 * hard to find interdependency made by jump table fixup.
1585 s
[i
] = new_stmt(0); /*dummy*/
1590 b0
= gen_linktype(ETHERTYPE_IP
);
1593 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1594 s
[i
]->s
.k
= off_nl
+ 9;
1596 /* X = ip->ip_hl << 2 */
1597 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1603 b0
= gen_linktype(ETHERTYPE_IPV6
);
1605 /* A = ip6->ip_nxt */
1606 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1607 s
[i
]->s
.k
= off_nl
+ 6;
1609 /* X = sizeof(struct ip6_hdr) */
1610 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1616 bpf_error("unsupported proto to gen_protochain");
1620 /* again: if (A == v) goto end; else fall through; */
1622 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1624 s
[i
]->s
.jt
= NULL
; /*later*/
1625 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1629 #ifndef IPPROTO_NONE
1630 #define IPPROTO_NONE 59
1632 /* if (A == IPPROTO_NONE) goto end */
1633 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1634 s
[i
]->s
.jt
= NULL
; /*later*/
1635 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1636 s
[i
]->s
.k
= IPPROTO_NONE
;
1637 s
[fix5
]->s
.jf
= s
[i
];
1642 if (proto
== Q_IPV6
) {
1643 int v6start
, v6end
, v6advance
, j
;
1646 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1647 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1648 s
[i
]->s
.jt
= NULL
; /*later*/
1649 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1650 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
1651 s
[fix2
]->s
.jf
= s
[i
];
1653 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1654 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1655 s
[i
]->s
.jt
= NULL
; /*later*/
1656 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1657 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
1659 /* if (A == IPPROTO_ROUTING) goto v6advance */
1660 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1661 s
[i
]->s
.jt
= NULL
; /*later*/
1662 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1663 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1665 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1666 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1667 s
[i
]->s
.jt
= NULL
; /*later*/
1668 s
[i
]->s
.jf
= NULL
; /*later*/
1669 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
1680 * X = X + (P[X] + 1) * 8;
1683 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1686 s
[i
] = new_stmt(BPF_ST
);
1690 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1694 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1696 /* A = P[X + packet head]; */
1697 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1701 s
[i
] = new_stmt(BPF_ST
);
1705 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1708 /* A = P[X + packet head] */
1709 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1713 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1717 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1721 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1724 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1728 /* goto again; (must use BPF_JA for backward jump) */
1729 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1730 s
[i
]->s
.k
= again
- i
- 1;
1731 s
[i
- 1]->s
.jf
= s
[i
];
1735 for (j
= v6start
; j
<= v6end
; j
++)
1736 s
[j
]->s
.jt
= s
[v6advance
];
1741 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1743 s
[fix2
]->s
.jf
= s
[i
];
1749 /* if (A == IPPROTO_AH) then fall through; else goto end; */
1750 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1751 s
[i
]->s
.jt
= NULL
; /*later*/
1752 s
[i
]->s
.jf
= NULL
; /*later*/
1753 s
[i
]->s
.k
= IPPROTO_AH
;
1755 s
[fix3
]->s
.jf
= s
[ahcheck
];
1762 * X = X + (P[X] + 2) * 4;
1765 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1768 s
[i
] = new_stmt(BPF_ST
);
1772 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1776 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1778 /* A = P[X + packet head]; */
1779 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1783 s
[i
] = new_stmt(BPF_ST
);
1787 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1790 /* A = P[X + packet head] */
1791 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1795 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1799 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1803 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1806 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1810 /* goto again; (must use BPF_JA for backward jump) */
1811 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1812 s
[i
]->s
.k
= again
- i
- 1;
1817 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1819 s
[fix2
]->s
.jt
= s
[end
];
1820 s
[fix4
]->s
.jf
= s
[end
];
1821 s
[fix5
]->s
.jt
= s
[end
];
1828 for (i
= 0; i
< max
- 1; i
++)
1829 s
[i
]->next
= s
[i
+ 1];
1830 s
[max
- 1]->next
= NULL
;
1835 b
= new_block(JMP(BPF_JEQ
));
1836 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
1847 static struct block
*
1848 gen_proto(v
, proto
, dir
)
1853 struct block
*b0
, *b1
;
1855 if (dir
!= Q_DEFAULT
)
1856 bpf_error("direction applied to 'proto'");
1861 b0
= gen_proto(v
, Q_IP
, dir
);
1862 b1
= gen_proto(v
, Q_IPV6
, dir
);
1869 b0
= gen_linktype(ETHERTYPE_IP
);
1871 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
1873 b1
= gen_protochain(v
, Q_IP
);
1879 bpf_error("arp does not encapsulate another protocol");
1883 bpf_error("rarp does not encapsulate another protocol");
1887 bpf_error("atalk encapsulation is not specifiable");
1891 bpf_error("decnet encapsulation is not specifiable");
1895 bpf_error("sca does not encapsulate another protocol");
1899 bpf_error("lat does not encapsulate another protocol");
1903 bpf_error("moprc does not encapsulate another protocol");
1907 bpf_error("mopdl does not encapsulate another protocol");
1911 return gen_linktype(v
);
1914 bpf_error("'udp proto' is bogus");
1918 bpf_error("'tcp proto' is bogus");
1922 bpf_error("'icmp proto' is bogus");
1926 bpf_error("'igmp proto' is bogus");
1930 bpf_error("'igrp proto' is bogus");
1934 bpf_error("'pim proto' is bogus");
1939 b0
= gen_linktype(ETHERTYPE_IPV6
);
1941 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
1943 b1
= gen_protochain(v
, Q_IPV6
);
1949 bpf_error("'icmp6 proto' is bogus");
1953 bpf_error("'ah proto' is bogus");
1956 bpf_error("'ah proto' is bogus");
1967 register const char *name
;
1970 int proto
= q
.proto
;
1974 bpf_u_int32 mask
, addr
;
1976 bpf_u_int32
**alist
;
1979 struct sockaddr_in
*sin
;
1980 struct sockaddr_in6
*sin6
;
1981 struct addrinfo
*res
, *res0
;
1982 struct in6_addr mask128
;
1984 struct block
*b
, *tmp
;
1985 int port
, real_proto
;
1990 addr
= pcap_nametonetaddr(name
);
1992 bpf_error("unknown network '%s'", name
);
1993 /* Left justify network addr and calculate its network mask */
1995 while (addr
&& (addr
& 0xff000000) == 0) {
1999 return gen_host(addr
, mask
, proto
, dir
);
2003 if (proto
== Q_LINK
) {
2007 eaddr
= pcap_ether_hostton(name
);
2010 "unknown ether host '%s'", name
);
2011 return gen_ehostop(eaddr
, dir
);
2014 eaddr
= pcap_ether_hostton(name
);
2017 "unknown FDDI host '%s'", name
);
2018 return gen_fhostop(eaddr
, dir
);
2022 "only ethernet/FDDI supports link-level host name");
2025 } else if (proto
== Q_DECNET
) {
2026 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2028 * I don't think DECNET hosts can be multihomed, so
2029 * there is no need to build up a list of addresses
2031 return (gen_host(dn_addr
, 0, proto
, dir
));
2034 alist
= pcap_nametoaddr(name
);
2035 if (alist
== NULL
|| *alist
== NULL
)
2036 bpf_error("unknown host '%s'", name
);
2038 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2040 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2042 tmp
= gen_host(**alist
++, 0xffffffff,
2049 memset(&mask128
, 0xff, sizeof(mask128
));
2050 res0
= res
= pcap_nametoaddrinfo(name
);
2052 bpf_error("unknown host '%s'", name
);
2054 tproto
= tproto6
= proto
;
2055 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2059 for (res
= res0
; res
; res
= res
->ai_next
) {
2060 switch (res
->ai_family
) {
2062 if (tproto
== Q_IPV6
)
2065 sin
= (struct sockaddr_in
*)
2067 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2068 0xffffffff, tproto
, dir
);
2071 if (tproto6
== Q_IP
)
2074 sin6
= (struct sockaddr_in6
*)
2076 tmp
= gen_host6(&sin6
->sin6_addr
,
2077 &mask128
, tproto6
, dir
);
2086 bpf_error("unknown host '%s'%s", name
,
2087 (proto
== Q_DEFAULT
)
2089 : " for specified address family");
2096 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2097 bpf_error("illegal qualifier of 'port'");
2098 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2099 bpf_error("unknown port '%s'", name
);
2100 if (proto
== Q_UDP
) {
2101 if (real_proto
== IPPROTO_TCP
)
2102 bpf_error("port '%s' is tcp", name
);
2104 /* override PROTO_UNDEF */
2105 real_proto
= IPPROTO_UDP
;
2107 if (proto
== Q_TCP
) {
2108 if (real_proto
== IPPROTO_UDP
)
2109 bpf_error("port '%s' is udp", name
);
2111 /* override PROTO_UNDEF */
2112 real_proto
= IPPROTO_TCP
;
2115 return gen_port(port
, real_proto
, dir
);
2119 b
= gen_port(port
, real_proto
, dir
);
2120 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2127 eaddr
= pcap_ether_hostton(name
);
2129 bpf_error("unknown ether host: %s", name
);
2131 alist
= pcap_nametoaddr(name
);
2132 if (alist
== NULL
|| *alist
== NULL
)
2133 bpf_error("unknown host '%s'", name
);
2134 return gen_gateway(eaddr
, alist
, proto
, dir
);
2136 bpf_error("'gateway' not supported in this configuration");
2140 real_proto
= lookup_proto(name
, proto
);
2141 if (real_proto
>= 0)
2142 return gen_proto(real_proto
, proto
, dir
);
2144 bpf_error("unknown protocol: %s", name
);
2147 real_proto
= lookup_proto(name
, proto
);
2148 if (real_proto
>= 0)
2149 return gen_protochain(real_proto
, proto
, dir
);
2151 bpf_error("unknown protocol: %s", name
);
2163 gen_mcode(s1
, s2
, masklen
, q
)
2164 register const char *s1
, *s2
;
2165 register int masklen
;
2168 register int nlen
, mlen
;
2171 nlen
= __pcap_atoin(s1
, &n
);
2172 /* Promote short ipaddr */
2176 mlen
= __pcap_atoin(s2
, &m
);
2177 /* Promote short ipaddr */
2180 bpf_error("non-network bits set in \"%s mask %s\"",
2183 /* Convert mask len to mask */
2185 bpf_error("mask length must be <= 32");
2186 m
= 0xffffffff << (32 - masklen
);
2188 bpf_error("non-network bits set in \"%s/%d\"",
2195 return gen_host(n
, m
, q
.proto
, q
.dir
);
2198 bpf_error("Mask syntax for networks only");
2205 register const char *s
;
2210 int proto
= q
.proto
;
2216 else if (q
.proto
== Q_DECNET
)
2217 vlen
= __pcap_atodn(s
, &v
);
2219 vlen
= __pcap_atoin(s
, &v
);
2226 if (proto
== Q_DECNET
)
2227 return gen_host(v
, 0, proto
, dir
);
2228 else if (proto
== Q_LINK
) {
2229 bpf_error("illegal link layer address");
2232 if (s
== NULL
&& q
.addr
== Q_NET
) {
2233 /* Promote short net number */
2234 while (v
&& (v
& 0xff000000) == 0) {
2239 /* Promote short ipaddr */
2243 return gen_host(v
, mask
, proto
, dir
);
2248 proto
= IPPROTO_UDP
;
2249 else if (proto
== Q_TCP
)
2250 proto
= IPPROTO_TCP
;
2251 else if (proto
== Q_DEFAULT
)
2252 proto
= PROTO_UNDEF
;
2254 bpf_error("illegal qualifier of 'port'");
2257 return gen_port((int)v
, proto
, dir
);
2261 b
= gen_port((int)v
, proto
, dir
);
2262 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2268 bpf_error("'gateway' requires a name");
2272 return gen_proto((int)v
, proto
, dir
);
2275 return gen_protochain((int)v
, proto
, dir
);
2290 gen_mcode6(s1
, s2
, masklen
, q
)
2291 register const char *s1
, *s2
;
2292 register int masklen
;
2295 struct addrinfo
*res
;
2296 struct in6_addr
*addr
;
2297 struct in6_addr mask
;
2302 bpf_error("no mask %s supported", s2
);
2304 res
= pcap_nametoaddrinfo(s1
);
2306 bpf_error("invalid ip6 address %s", s1
);
2308 bpf_error("%s resolved to multiple address", s1
);
2309 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2311 if (sizeof(mask
) * 8 < masklen
)
2312 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2313 memset(&mask
, 0xff, masklen
/ 8);
2315 mask
.s6_addr
[masklen
/ 8] =
2316 (0xff << (8 - masklen
% 8)) & 0xff;
2319 a
= (u_int32_t
*)addr
;
2320 m
= (u_int32_t
*)&mask
;
2321 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2322 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2323 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2331 bpf_error("Mask syntax for networks only");
2335 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2340 bpf_error("invalid qualifier against IPv6 address");
2348 register const u_char
*eaddr
;
2351 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2352 if (linktype
== DLT_EN10MB
)
2353 return gen_ehostop(eaddr
, (int)q
.dir
);
2354 if (linktype
== DLT_FDDI
)
2355 return gen_fhostop(eaddr
, (int)q
.dir
);
2357 bpf_error("ethernet address used in non-ether expression");
2363 struct slist
*s0
, *s1
;
2366 * This is definitely not the best way to do this, but the
2367 * lists will rarely get long.
2374 static struct slist
*
2380 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2385 static struct slist
*
2391 s
= new_stmt(BPF_LD
|BPF_MEM
);
2397 gen_load(proto
, index
, size
)
2402 struct slist
*s
, *tmp
;
2404 int regno
= alloc_reg();
2406 free_reg(index
->regno
);
2410 bpf_error("data size must be 1, 2, or 4");
2426 bpf_error("unsupported index operation");
2429 s
= xfer_to_x(index
);
2430 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2432 sappend(index
->s
, s
);
2447 /* XXX Note that we assume a fixed link header here. */
2448 s
= xfer_to_x(index
);
2449 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2452 sappend(index
->s
, s
);
2454 b
= gen_proto_abbrev(proto
);
2456 gen_and(index
->b
, b
);
2466 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2468 sappend(s
, xfer_to_a(index
));
2469 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2470 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2471 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2473 sappend(index
->s
, s
);
2475 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2477 gen_and(index
->b
, b
);
2479 gen_and(gen_proto_abbrev(Q_IP
), b
);
2485 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2489 index
->regno
= regno
;
2490 s
= new_stmt(BPF_ST
);
2492 sappend(index
->s
, s
);
2498 gen_relation(code
, a0
, a1
, reversed
)
2500 struct arth
*a0
, *a1
;
2503 struct slist
*s0
, *s1
, *s2
;
2504 struct block
*b
, *tmp
;
2508 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2509 b
= new_block(JMP(code
));
2510 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2511 reversed
= !reversed
;
2512 b
->s
.k
= 0x80000000;
2520 sappend(a0
->s
, a1
->s
);
2524 free_reg(a0
->regno
);
2525 free_reg(a1
->regno
);
2527 /* 'and' together protocol checks */
2530 gen_and(a0
->b
, tmp
= a1
->b
);
2546 int regno
= alloc_reg();
2547 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2550 s
= new_stmt(BPF_LD
|BPF_LEN
);
2551 s
->next
= new_stmt(BPF_ST
);
2552 s
->next
->s
.k
= regno
;
2567 a
= (struct arth
*)newchunk(sizeof(*a
));
2571 s
= new_stmt(BPF_LD
|BPF_IMM
);
2573 s
->next
= new_stmt(BPF_ST
);
2589 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2592 s
= new_stmt(BPF_ST
);
2600 gen_arth(code
, a0
, a1
)
2602 struct arth
*a0
, *a1
;
2604 struct slist
*s0
, *s1
, *s2
;
2608 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2613 sappend(a0
->s
, a1
->s
);
2615 free_reg(a1
->regno
);
2617 s0
= new_stmt(BPF_ST
);
2618 a0
->regno
= s0
->s
.k
= alloc_reg();
2625 * Here we handle simple allocation of the scratch registers.
2626 * If too many registers are alloc'd, the allocator punts.
2628 static int regused
[BPF_MEMWORDS
];
2632 * Return the next free register.
2637 int n
= BPF_MEMWORDS
;
2640 if (regused
[curreg
])
2641 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2643 regused
[curreg
] = 1;
2647 bpf_error("too many registers needed to evaluate expression");
2652 * Return a register to the table so it can
2662 static struct block
*
2669 s
= new_stmt(BPF_LD
|BPF_LEN
);
2670 b
= new_block(JMP(jmp
));
2681 return gen_len(BPF_JGE
, n
);
2690 b
= gen_len(BPF_JGT
, n
);
2697 gen_byteop(op
, idx
, val
)
2708 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2711 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2712 b
->s
.code
= JMP(BPF_JGE
);
2717 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2718 b
->s
.code
= JMP(BPF_JGT
);
2722 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
2726 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2730 b
= new_block(JMP(BPF_JEQ
));
2738 gen_broadcast(proto
)
2741 bpf_u_int32 hostmask
;
2742 struct block
*b0
, *b1
, *b2
;
2743 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2749 if (linktype
== DLT_EN10MB
)
2750 return gen_ehostop(ebroadcast
, Q_DST
);
2751 if (linktype
== DLT_FDDI
)
2752 return gen_fhostop(ebroadcast
, Q_DST
);
2753 bpf_error("not a broadcast link");
2757 b0
= gen_linktype(ETHERTYPE_IP
);
2758 hostmask
= ~netmask
;
2759 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
2760 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
2761 (bpf_int32
)(~0 & hostmask
), hostmask
);
2766 bpf_error("only ether/ip broadcast filters supported");
2770 gen_multicast(proto
)
2773 register struct block
*b0
, *b1
;
2774 register struct slist
*s
;
2780 if (linktype
== DLT_EN10MB
) {
2781 /* ether[0] & 1 != 0 */
2782 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2784 b0
= new_block(JMP(BPF_JSET
));
2790 if (linktype
== DLT_FDDI
) {
2791 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
2792 /* fddi[1] & 1 != 0 */
2793 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2795 b0
= new_block(JMP(BPF_JSET
));
2800 /* Link not known to support multicasts */
2804 b0
= gen_linktype(ETHERTYPE_IP
);
2805 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
2806 b1
->s
.code
= JMP(BPF_JGE
);
2812 b0
= gen_linktype(ETHERTYPE_IPV6
);
2813 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
2818 bpf_error("only IP multicast filters supported on ethernet/FDDI");
2822 * generate command for inbound/outbound. It's here so we can
2823 * make it link-type specific. 'dir' = 0 implies "inbound",
2824 * = 1 implies "outbound".
2830 register struct block
*b0
;
2832 b0
= gen_relation(BPF_JEQ
,
2833 gen_load(Q_LINK
, gen_loadi(0), 1),