]>
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.97 1999-10-30 05:16:35 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)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 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
321 * which of the jt and jf fields has been resolved and which is a pointer
322 * back to another unresolved block (or nil). At least one of the fields
323 * in each block is already resolved.
326 backpatch(list
, target
)
327 struct block
*list
, *target
;
344 * Merge the lists in b0 and b1, using the 'sense' field to indicate
345 * which of jt and jf is the link.
349 struct block
*b0
, *b1
;
351 register struct block
**p
= &b0
;
353 /* Find end of list. */
355 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
357 /* Concatenate the lists. */
365 backpatch(p
, gen_retblk(snaplen
));
366 p
->sense
= !p
->sense
;
367 backpatch(p
, gen_retblk(0));
373 struct block
*b0
, *b1
;
375 backpatch(b0
, b1
->head
);
376 b0
->sense
= !b0
->sense
;
377 b1
->sense
= !b1
->sense
;
379 b1
->sense
= !b1
->sense
;
385 struct block
*b0
, *b1
;
387 b0
->sense
= !b0
->sense
;
388 backpatch(b0
, b1
->head
);
389 b0
->sense
= !b0
->sense
;
398 b
->sense
= !b
->sense
;
401 static struct block
*
402 gen_cmp(offset
, size
, v
)
409 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
412 b
= new_block(JMP(BPF_JEQ
));
419 static struct block
*
420 gen_mcmp(offset
, size
, v
, mask
)
425 struct block
*b
= gen_cmp(offset
, size
, v
);
428 if (mask
!= 0xffffffff) {
429 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
436 static struct block
*
437 gen_bcmp(offset
, size
, v
)
438 register u_int offset
, size
;
439 register const u_char
*v
;
441 register struct block
*b
, *tmp
;
445 register const u_char
*p
= &v
[size
- 4];
446 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
447 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
449 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
456 register const u_char
*p
= &v
[size
- 2];
457 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
459 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
466 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
475 * Various code constructs need to know the layout of the data link
476 * layer. These variables give the necessary offsets. off_linktype
477 * is set to -1 for no encapsulation, in which case, IP is assumed.
479 static u_int off_linktype
;
498 * SLIP doesn't have a link level type. The 16 byte
499 * header is hacked into our SLIP driver.
506 /* XXX this may be the same as the DLT_PPP_BSDOS case */
530 * FDDI doesn't really have a link-level type field.
531 * We assume that SSAP = SNAP is being used and pick
532 * out the encapsulated Ethernet type.
536 off_linktype
+= pcap_fddipad
;
540 off_nl
+= pcap_fddipad
;
549 case DLT_ATM_RFC1483
:
551 * assume routed, non-ISO PDUs
552 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
563 bpf_error("unknown data link type 0x%x", linktype
);
567 static struct block
*
574 s
= new_stmt(BPF_LD
|BPF_IMM
);
576 b
= new_block(JMP(BPF_JEQ
));
582 static inline struct block
*
585 return gen_uncond(1);
588 static inline struct block
*
591 return gen_uncond(0);
594 static struct block
*
598 struct block
*b0
, *b1
;
600 /* If we're not using encapsulation and checking for IP, we're done */
601 if (off_linktype
== -1 && proto
== ETHERTYPE_IP
)
610 if (proto
== ETHERTYPE_IP
)
611 proto
= PPP_IP
; /* XXX was 0x21 */
613 else if (proto
== ETHERTYPE_IPV6
)
622 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
623 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
625 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
640 case ETHERTYPE_ATALK
:
652 if (proto
== ETHERTYPE_IP
)
653 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET
)));
655 else if (proto
== ETHERTYPE_IPV6
)
656 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET6
)));
661 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
664 static struct block
*
665 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
669 u_int src_off
, dst_off
;
671 struct block
*b0
, *b1
;
685 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
686 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
692 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
693 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
700 b0
= gen_linktype(proto
);
701 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
707 static struct block
*
708 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
709 struct in6_addr
*addr
;
710 struct in6_addr
*mask
;
712 u_int src_off
, dst_off
;
714 struct block
*b0
, *b1
;
729 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
730 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
736 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
737 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
744 /* this order is important */
745 a
= (u_int32_t
*)addr
;
746 m
= (u_int32_t
*)mask
;
747 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
748 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
750 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
752 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
754 b0
= gen_linktype(proto
);
760 static struct block
*
761 gen_ehostop(eaddr
, dir
)
762 register const u_char
*eaddr
;
765 register struct block
*b0
, *b1
;
769 return gen_bcmp(6, 6, eaddr
);
772 return gen_bcmp(0, 6, eaddr
);
775 b0
= gen_ehostop(eaddr
, Q_SRC
);
776 b1
= gen_ehostop(eaddr
, Q_DST
);
782 b0
= gen_ehostop(eaddr
, Q_SRC
);
783 b1
= gen_ehostop(eaddr
, Q_DST
);
792 * Like gen_ehostop, but for DLT_FDDI
794 static struct block
*
795 gen_fhostop(eaddr
, dir
)
796 register const u_char
*eaddr
;
799 struct block
*b0
, *b1
;
804 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
806 return gen_bcmp(6 + 1, 6, eaddr
);
811 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
813 return gen_bcmp(0 + 1, 6, eaddr
);
817 b0
= gen_fhostop(eaddr
, Q_SRC
);
818 b1
= gen_fhostop(eaddr
, Q_DST
);
824 b0
= gen_fhostop(eaddr
, Q_SRC
);
825 b1
= gen_fhostop(eaddr
, Q_DST
);
834 * This is quite tricky because there may be pad bytes in front of the
835 * DECNET header, and then there are two possible data packet formats that
836 * carry both src and dst addresses, plus 5 packet types in a format that
837 * carries only the src node, plus 2 types that use a different format and
838 * also carry just the src node.
842 * Instead of doing those all right, we just look for data packets with
843 * 0 or 1 bytes of padding. If you want to look at other packets, that
844 * will require a lot more hacking.
846 * To add support for filtering on DECNET "areas" (network numbers)
847 * one would want to add a "mask" argument to this routine. That would
848 * make the filter even more inefficient, although one could be clever
849 * and not generate masking instructions if the mask is 0xFFFF.
851 static struct block
*
852 gen_dnhostop(addr
, dir
, base_off
)
857 struct block
*b0
, *b1
, *b2
, *tmp
;
858 u_int offset_lh
; /* offset if long header is received */
859 u_int offset_sh
; /* offset if short header is received */
864 offset_sh
= 1; /* follows flags */
865 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
869 offset_sh
= 3; /* follows flags, dstnode */
870 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
874 /* Inefficient because we do our Calvinball dance twice */
875 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
876 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
882 /* Inefficient because we do our Calvinball dance twice */
883 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
884 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
891 b0
= gen_linktype(ETHERTYPE_DN
);
892 /* Check for pad = 1, long header case */
893 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
894 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
895 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
896 BPF_H
, (bpf_int32
)ntohs(addr
));
898 /* Check for pad = 0, long header case */
899 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
900 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
903 /* Check for pad = 1, short header case */
904 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
905 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
906 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
907 BPF_H
, (bpf_int32
)ntohs(addr
));
910 /* Check for pad = 0, short header case */
911 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
912 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
916 /* Combine with test for linktype */
921 static struct block
*
922 gen_host(addr
, mask
, proto
, dir
)
928 struct block
*b0
, *b1
;
933 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
934 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
936 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
941 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
942 off_nl
+ 12, off_nl
+ 16);
945 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
946 off_nl
+ 14, off_nl
+ 24);
949 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
950 off_nl
+ 14, off_nl
+ 24);
953 bpf_error("'tcp' modifier applied to host");
956 bpf_error("'udp' modifier applied to host");
959 bpf_error("'icmp' modifier applied to host");
962 bpf_error("'igmp' modifier applied to host");
965 bpf_error("'igrp' modifier applied to host");
968 bpf_error("'pim' modifier applied to host");
971 bpf_error("ATALK host filtering not implemented");
974 return gen_dnhostop(addr
, dir
, off_nl
);
977 bpf_error("SCA host filtering not implemented");
980 bpf_error("LAT host filtering not implemented");
983 bpf_error("MOPDL host filtering not implemented");
986 bpf_error("MOPRC host filtering not implemented");
990 bpf_error("'ip6' modifier applied to ip host");
993 bpf_error("'icmp6' modifier applied to host");
997 bpf_error("'ah' modifier applied to host");
1000 bpf_error("'esp' modifier applied to host");
1009 static struct block
*
1010 gen_host6(addr
, mask
, proto
, dir
)
1011 struct in6_addr
*addr
;
1012 struct in6_addr
*mask
;
1016 struct block
*b0
, *b1
;
1021 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1024 bpf_error("'ip' modifier applied to ip6 host");
1027 bpf_error("'rarp' modifier applied to ip6 host");
1030 bpf_error("'arp' modifier applied to ip6 host");
1033 bpf_error("'tcp' modifier applied to host");
1036 bpf_error("'udp' modifier applied to host");
1039 bpf_error("'icmp' modifier applied to host");
1042 bpf_error("'igmp' modifier applied to host");
1045 bpf_error("'igrp' modifier applied to host");
1048 bpf_error("'pim' modifier applied to host");
1051 bpf_error("ATALK host filtering not implemented");
1054 bpf_error("'decnet' modifier applied to ip6 host");
1057 bpf_error("SCA host filtering not implemented");
1060 bpf_error("LAT host filtering not implemented");
1063 bpf_error("MOPDL host filtering not implemented");
1066 bpf_error("MOPRC host filtering not implemented");
1069 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1070 off_nl
+ 8, off_nl
+ 24);
1073 bpf_error("'icmp6' modifier applied to host");
1076 bpf_error("'ah' modifier applied to host");
1079 bpf_error("'esp' modifier applied to host");
1088 static struct block
*
1089 gen_gateway(eaddr
, alist
, proto
, dir
)
1090 const u_char
*eaddr
;
1091 bpf_u_int32
**alist
;
1095 struct block
*b0
, *b1
, *tmp
;
1098 bpf_error("direction applied to 'gateway'");
1105 if (linktype
== DLT_EN10MB
)
1106 b0
= gen_ehostop(eaddr
, Q_OR
);
1107 else if (linktype
== DLT_FDDI
)
1108 b0
= gen_fhostop(eaddr
, Q_OR
);
1111 "'gateway' supported only on ethernet or FDDI");
1113 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1115 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1123 bpf_error("illegal modifier of 'gateway'");
1128 gen_proto_abbrev(proto
)
1131 struct block
*b0
, *b1
;
1136 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1138 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1144 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1146 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1152 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1156 b1
= gen_proto(2, Q_IP
, Q_DEFAULT
);
1159 #ifndef IPPROTO_IGRP
1160 #define IPPROTO_IGRP 9
1163 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1168 #define IPPROTO_PIM 103
1172 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1174 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1180 b1
= gen_linktype(ETHERTYPE_IP
);
1184 b1
= gen_linktype(ETHERTYPE_ARP
);
1188 b1
= gen_linktype(ETHERTYPE_REVARP
);
1192 bpf_error("link layer applied in wrong context");
1195 b1
= gen_linktype(ETHERTYPE_ATALK
);
1199 b1
= gen_linktype(ETHERTYPE_DN
);
1203 b1
= gen_linktype(ETHERTYPE_SCA
);
1207 b1
= gen_linktype(ETHERTYPE_LAT
);
1211 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1215 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1220 b1
= gen_linktype(ETHERTYPE_IPV6
);
1223 #ifndef IPPROTO_ICMPV6
1224 #define IPPROTO_ICMPV6 58
1227 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1232 #define IPPROTO_AH 51
1235 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1237 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1243 #define IPPROTO_ESP 50
1246 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1248 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1259 static struct block
*
1266 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1267 s
->s
.k
= off_nl
+ 6;
1268 b
= new_block(JMP(BPF_JSET
));
1276 static struct block
*
1277 gen_portatom(off
, v
)
1284 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1287 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1288 s
->next
->s
.k
= off_nl
+ off
;
1290 b
= new_block(JMP(BPF_JEQ
));
1298 static struct block
*
1299 gen_portatom6(off
, v
)
1303 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1308 gen_portop(port
, proto
, dir
)
1309 int port
, proto
, dir
;
1311 struct block
*b0
, *b1
, *tmp
;
1313 /* ip proto 'proto' */
1314 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1320 b1
= gen_portatom(0, (bpf_int32
)port
);
1324 b1
= gen_portatom(2, (bpf_int32
)port
);
1329 tmp
= gen_portatom(0, (bpf_int32
)port
);
1330 b1
= gen_portatom(2, (bpf_int32
)port
);
1335 tmp
= gen_portatom(0, (bpf_int32
)port
);
1336 b1
= gen_portatom(2, (bpf_int32
)port
);
1348 static struct block
*
1349 gen_port(port
, ip_proto
, dir
)
1354 struct block
*b0
, *b1
, *tmp
;
1356 /* ether proto ip */
1357 b0
= gen_linktype(ETHERTYPE_IP
);
1362 b1
= gen_portop(port
, ip_proto
, dir
);
1366 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1367 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1380 gen_portop6(port
, proto
, dir
)
1381 int port
, proto
, dir
;
1383 struct block
*b0
, *b1
, *tmp
;
1385 /* ip proto 'proto' */
1386 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1390 b1
= gen_portatom6(0, (bpf_int32
)port
);
1394 b1
= gen_portatom6(2, (bpf_int32
)port
);
1399 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1400 b1
= gen_portatom6(2, (bpf_int32
)port
);
1405 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1406 b1
= gen_portatom6(2, (bpf_int32
)port
);
1418 static struct block
*
1419 gen_port6(port
, ip_proto
, dir
)
1424 struct block
*b0
, *b1
, *tmp
;
1426 /* ether proto ip */
1427 b0
= gen_linktype(ETHERTYPE_IPV6
);
1432 b1
= gen_portop6(port
, ip_proto
, dir
);
1436 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1437 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1450 lookup_proto(name
, proto
)
1451 register const char *name
;
1460 v
= pcap_nametoproto(name
);
1461 if (v
== PROTO_UNDEF
)
1462 bpf_error("unknown ip proto '%s'", name
);
1466 /* XXX should look up h/w protocol type based on linktype */
1467 v
= pcap_nametoeproto(name
);
1468 if (v
== PROTO_UNDEF
)
1469 bpf_error("unknown ether proto '%s'", name
);
1487 gen_protochain(v
, proto
, dir
)
1492 struct block
*b0
, *b
;
1493 struct slist
*s
[100], *sp
;
1494 int fix2
, fix3
, fix4
, fix5
;
1495 int ahcheck
, again
, end
;
1497 int reg1
= alloc_reg();
1498 int reg2
= alloc_reg();
1500 memset(s
, 0, sizeof(s
));
1501 fix2
= fix3
= fix4
= fix5
= 0;
1508 b0
= gen_protochain(v
, Q_IP
, dir
);
1509 b
= gen_protochain(v
, Q_IP
, dir
);
1513 bpf_error("bad protocol applied for 'protochain'");
1517 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1520 * s[0] is a dummy entry to protect other BPF insn from damaged
1521 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1522 * hard to find interdependency made by jump table fixup.
1525 s
[i
] = new_stmt(0); /*dummy*/
1530 b0
= gen_linktype(ETHERTYPE_IP
);
1533 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1534 s
[i
]->s
.k
= off_nl
+ 9;
1536 /* X = ip->ip_hl << 2 */
1537 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1543 b0
= gen_linktype(ETHERTYPE_IPV6
);
1545 /* A = ip6->ip_nxt */
1546 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1547 s
[i
]->s
.k
= off_nl
+ 6;
1549 /* X = sizeof(struct ip6_hdr) */
1550 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1556 bpf_error("unsupported proto to gen_protochain");
1560 /* again: if (A == v) goto end; else fall through; */
1562 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1564 s
[i
]->s
.jt
= NULL
; /*later*/
1565 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1569 #ifndef IPPROTO_NONE
1570 #define IPPROTO_NONE 59
1572 /* if (A == IPPROTO_NONE) goto end */
1573 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1574 s
[i
]->s
.jt
= NULL
; /*later*/
1575 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1576 s
[i
]->s
.k
= IPPROTO_NONE
;
1577 s
[fix5
]->s
.jf
= s
[i
];
1582 if (proto
== Q_IPV6
) {
1583 int v6start
, v6end
, v6advance
, j
;
1586 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1587 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1588 s
[i
]->s
.jt
= NULL
; /*later*/
1589 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1590 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
1591 s
[fix2
]->s
.jf
= s
[i
];
1593 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1594 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1595 s
[i
]->s
.jt
= NULL
; /*later*/
1596 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1597 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
1599 /* if (A == IPPROTO_ROUTING) goto v6advance */
1600 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1601 s
[i
]->s
.jt
= NULL
; /*later*/
1602 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1603 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1605 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1606 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1607 s
[i
]->s
.jt
= NULL
; /*later*/
1608 s
[i
]->s
.jf
= NULL
; /*later*/
1609 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
1620 * X = X + (P[X] + 1) * 8;
1623 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1626 s
[i
] = new_stmt(BPF_ST
);
1630 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1634 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1636 /* A = P[X + packet head]; */
1637 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1641 s
[i
] = new_stmt(BPF_ST
);
1645 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1648 /* A = P[X + packet head] */
1649 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1653 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1657 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1661 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1664 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1668 /* goto again; (must use BPF_JA for backward jump) */
1669 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1670 s
[i
]->s
.k
= again
- i
- 1;
1671 s
[i
- 1]->s
.jf
= s
[i
];
1675 for (j
= v6start
; j
<= v6end
; j
++)
1676 s
[j
]->s
.jt
= s
[v6advance
];
1681 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1683 s
[fix2
]->s
.jf
= s
[i
];
1689 /* if (A == IPPROTO_AH) then fall through; else goto end; */
1690 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1691 s
[i
]->s
.jt
= NULL
; /*later*/
1692 s
[i
]->s
.jf
= NULL
; /*later*/
1693 s
[i
]->s
.k
= IPPROTO_AH
;
1695 s
[fix3
]->s
.jf
= s
[ahcheck
];
1702 * X = X + (P[X] + 2) * 4;
1705 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1708 s
[i
] = new_stmt(BPF_ST
);
1712 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1716 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1718 /* A = P[X + packet head]; */
1719 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1723 s
[i
] = new_stmt(BPF_ST
);
1727 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1730 /* A = P[X + packet head] */
1731 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1735 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1739 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1743 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1746 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1750 /* goto again; (must use BPF_JA for backward jump) */
1751 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1752 s
[i
]->s
.k
= again
- i
- 1;
1757 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1759 s
[fix2
]->s
.jt
= s
[end
];
1760 s
[fix4
]->s
.jf
= s
[end
];
1761 s
[fix5
]->s
.jt
= s
[end
];
1768 for (i
= 0; i
< max
- 1; i
++)
1769 s
[i
]->next
= s
[i
+ 1];
1770 s
[max
- 1]->next
= NULL
;
1775 b
= new_block(JMP(BPF_JEQ
));
1776 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
1786 static struct block
*
1787 gen_proto(v
, proto
, dir
)
1792 struct block
*b0
, *b1
;
1794 if (dir
!= Q_DEFAULT
)
1795 bpf_error("direction applied to 'proto'");
1800 b0
= gen_proto(v
, Q_IP
, dir
);
1801 b1
= gen_proto(v
, Q_IPV6
, dir
);
1808 b0
= gen_linktype(ETHERTYPE_IP
);
1810 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
1812 b1
= gen_protochain(v
, Q_IP
);
1818 bpf_error("arp does not encapsulate another protocol");
1822 bpf_error("rarp does not encapsulate another protocol");
1826 bpf_error("atalk encapsulation is not specifiable");
1830 bpf_error("decnet encapsulation is not specifiable");
1834 bpf_error("sca does not encapsulate another protocol");
1838 bpf_error("lat does not encapsulate another protocol");
1842 bpf_error("moprc does not encapsulate another protocol");
1846 bpf_error("mopdl does not encapsulate another protocol");
1850 return gen_linktype(v
);
1853 bpf_error("'udp proto' is bogus");
1857 bpf_error("'tcp proto' is bogus");
1861 bpf_error("'icmp proto' is bogus");
1865 bpf_error("'igmp proto' is bogus");
1869 bpf_error("'igrp proto' is bogus");
1873 bpf_error("'pim proto' is bogus");
1878 b0
= gen_linktype(ETHERTYPE_IPV6
);
1880 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
1882 b1
= gen_protochain(v
, Q_IPV6
);
1888 bpf_error("'icmp6 proto' is bogus");
1892 bpf_error("'ah proto' is bogus");
1895 bpf_error("'ah proto' is bogus");
1906 register const char *name
;
1909 int proto
= q
.proto
;
1913 bpf_u_int32 mask
, addr
, **alist
;
1916 struct sockaddr_in
*sin
;
1917 struct sockaddr_in6
*sin6
;
1918 struct addrinfo
*res
, *res0
;
1919 struct in6_addr mask128
;
1921 struct block
*b
, *tmp
;
1922 int port
, real_proto
;
1927 addr
= pcap_nametonetaddr(name
);
1929 bpf_error("unknown network '%s'", name
);
1930 /* Left justify network addr and calculate its network mask */
1932 while (addr
&& (addr
& 0xff000000) == 0) {
1936 return gen_host(addr
, mask
, proto
, dir
);
1940 if (proto
== Q_LINK
) {
1944 eaddr
= pcap_ether_hostton(name
);
1947 "unknown ether host '%s'", name
);
1948 return gen_ehostop(eaddr
, dir
);
1951 eaddr
= pcap_ether_hostton(name
);
1954 "unknown FDDI host '%s'", name
);
1955 return gen_fhostop(eaddr
, dir
);
1959 "only ethernet/FDDI supports link-level host name");
1962 } else if (proto
== Q_DECNET
) {
1963 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
1965 * I don't think DECNET hosts can be multihomed, so
1966 * there is no need to build up a list of addresses
1968 return (gen_host(dn_addr
, 0, proto
, dir
));
1971 alist
= pcap_nametoaddr(name
);
1972 if (alist
== NULL
|| *alist
== NULL
)
1973 bpf_error("unknown host '%s'", name
);
1975 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
1977 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
1979 tmp
= gen_host(**alist
++, 0xffffffff,
1986 memset(&mask128
, 0xff, sizeof(mask128
));
1987 res0
= res
= pcap_nametoaddr(name
);
1989 bpf_error("unknown host '%s'", name
);
1991 tproto
= tproto6
= proto
;
1992 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
1997 switch (res
->ai_family
) {
1999 sin
= (struct sockaddr_in
*)
2001 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2002 0xffffffff, tproto
, dir
);
2005 sin6
= (struct sockaddr_in6
*)
2007 tmp
= gen_host6(&sin6
->sin6_addr
,
2008 &mask128
, tproto6
, dir
);
2023 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2024 bpf_error("illegal qualifier of 'port'");
2025 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2026 bpf_error("unknown port '%s'", name
);
2027 if (proto
== Q_UDP
) {
2028 if (real_proto
== IPPROTO_TCP
)
2029 bpf_error("port '%s' is tcp", name
);
2031 /* override PROTO_UNDEF */
2032 real_proto
= IPPROTO_UDP
;
2034 if (proto
== Q_TCP
) {
2035 if (real_proto
== IPPROTO_UDP
)
2036 bpf_error("port '%s' is udp", name
);
2038 /* override PROTO_UNDEF */
2039 real_proto
= IPPROTO_TCP
;
2042 return gen_port(port
, real_proto
, dir
);
2046 b
= gen_port(port
, real_proto
, dir
);
2047 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2054 eaddr
= pcap_ether_hostton(name
);
2056 bpf_error("unknown ether host: %s", name
);
2058 alist
= pcap_nametoaddr(name
);
2059 if (alist
== NULL
|| *alist
== NULL
)
2060 bpf_error("unknown host '%s'", name
);
2061 return gen_gateway(eaddr
, alist
, proto
, dir
);
2063 bpf_error("'gateway' not supported in this configuration");
2067 real_proto
= lookup_proto(name
, proto
);
2068 if (real_proto
>= 0)
2069 return gen_proto(real_proto
, proto
, dir
);
2071 bpf_error("unknown protocol: %s", name
);
2074 real_proto
= lookup_proto(name
, proto
);
2075 if (real_proto
>= 0)
2076 return gen_protochain(real_proto
, proto
, dir
);
2078 bpf_error("unknown protocol: %s", name
);
2090 gen_mcode(s1
, s2
, masklen
, q
)
2091 register const char *s1
, *s2
;
2092 register int masklen
;
2095 register int nlen
, mlen
;
2098 nlen
= __pcap_atoin(s1
, &n
);
2099 /* Promote short ipaddr */
2103 mlen
= __pcap_atoin(s2
, &m
);
2104 /* Promote short ipaddr */
2107 bpf_error("non-network bits set in \"%s mask %s\"",
2110 /* Convert mask len to mask */
2112 bpf_error("mask length must be <= 32");
2113 m
= 0xffffffff << (32 - masklen
);
2115 bpf_error("non-network bits set in \"%s/%d\"",
2122 return gen_host(n
, m
, q
.proto
, q
.dir
);
2125 bpf_error("Mask syntax for networks only");
2132 register const char *s
;
2137 int proto
= q
.proto
;
2143 else if (q
.proto
== Q_DECNET
)
2144 vlen
= __pcap_atodn(s
, &v
);
2146 vlen
= __pcap_atoin(s
, &v
);
2153 if (proto
== Q_DECNET
)
2154 return gen_host(v
, 0, proto
, dir
);
2155 else if (proto
== Q_LINK
) {
2156 bpf_error("illegal link layer address");
2159 if (s
== NULL
&& q
.addr
== Q_NET
) {
2160 /* Promote short net number */
2161 while (v
&& (v
& 0xff000000) == 0) {
2166 /* Promote short ipaddr */
2170 return gen_host(v
, mask
, proto
, dir
);
2175 proto
= IPPROTO_UDP
;
2176 else if (proto
== Q_TCP
)
2177 proto
= IPPROTO_TCP
;
2178 else if (proto
== Q_DEFAULT
)
2179 proto
= PROTO_UNDEF
;
2181 bpf_error("illegal qualifier of 'port'");
2184 return gen_port((int)v
, proto
, dir
);
2188 b
= gen_port((int)v
, proto
, dir
);
2189 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2195 bpf_error("'gateway' requires a name");
2199 return gen_proto((int)v
, proto
, dir
);
2202 return gen_protochain((int)v
, proto
, dir
);
2217 gen_mcode6(s1
, s2
, masklen
, q
)
2218 register const char *s1
, *s2
;
2219 register int masklen
;
2222 struct addrinfo
*res
;
2223 struct in6_addr
*addr
;
2224 struct in6_addr mask
;
2229 bpf_error("no mask %s supported", s2
);
2231 res
= pcap_nametoaddr(s1
);
2233 bpf_error("invalid ip6 address %s", s1
);
2235 bpf_error("%s resolved to multiple address", s1
);
2236 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2238 if (sizeof(mask
) * 8 < masklen
)
2239 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2240 memset(&mask
, 0xff, masklen
/ 8);
2242 mask
.s6_addr
[masklen
/ 8] =
2243 (0xff << (8 - masklen
% 8)) & 0xff;
2246 a
= (u_int32_t
*)addr
;
2247 m
= (u_int32_t
*)&mask
;
2248 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2249 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2250 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2258 bpf_error("Mask syntax for networks only");
2262 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2267 bpf_error("invalid qualifier against IPv6 address");
2275 register const u_char
*eaddr
;
2278 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2279 if (linktype
== DLT_EN10MB
)
2280 return gen_ehostop(eaddr
, (int)q
.dir
);
2281 if (linktype
== DLT_FDDI
)
2282 return gen_fhostop(eaddr
, (int)q
.dir
);
2284 bpf_error("ethernet address used in non-ether expression");
2290 struct slist
*s0
, *s1
;
2293 * This is definitely not the best way to do this, but the
2294 * lists will rarely get long.
2301 static struct slist
*
2307 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2312 static struct slist
*
2318 s
= new_stmt(BPF_LD
|BPF_MEM
);
2324 gen_load(proto
, index
, size
)
2329 struct slist
*s
, *tmp
;
2331 int regno
= alloc_reg();
2333 free_reg(index
->regno
);
2337 bpf_error("data size must be 1, 2, or 4");
2353 bpf_error("unsupported index operation");
2356 s
= xfer_to_x(index
);
2357 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2359 sappend(index
->s
, s
);
2374 /* XXX Note that we assume a fixed link link header here. */
2375 s
= xfer_to_x(index
);
2376 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2379 sappend(index
->s
, s
);
2381 b
= gen_proto_abbrev(proto
);
2383 gen_and(index
->b
, b
);
2393 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2395 sappend(s
, xfer_to_a(index
));
2396 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2397 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2398 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2400 sappend(index
->s
, s
);
2402 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2404 gen_and(index
->b
, b
);
2406 gen_and(gen_proto_abbrev(Q_IP
), b
);
2412 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2416 index
->regno
= regno
;
2417 s
= new_stmt(BPF_ST
);
2419 sappend(index
->s
, s
);
2425 gen_relation(code
, a0
, a1
, reversed
)
2427 struct arth
*a0
, *a1
;
2430 struct slist
*s0
, *s1
, *s2
;
2431 struct block
*b
, *tmp
;
2435 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2436 b
= new_block(JMP(code
));
2437 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2438 reversed
= !reversed
;
2439 b
->s
.k
= 0x80000000;
2447 sappend(a0
->s
, a1
->s
);
2451 free_reg(a0
->regno
);
2452 free_reg(a1
->regno
);
2454 /* 'and' together protocol checks */
2457 gen_and(a0
->b
, tmp
= a1
->b
);
2473 int regno
= alloc_reg();
2474 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2477 s
= new_stmt(BPF_LD
|BPF_LEN
);
2478 s
->next
= new_stmt(BPF_ST
);
2479 s
->next
->s
.k
= regno
;
2494 a
= (struct arth
*)newchunk(sizeof(*a
));
2498 s
= new_stmt(BPF_LD
|BPF_IMM
);
2500 s
->next
= new_stmt(BPF_ST
);
2516 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2519 s
= new_stmt(BPF_ST
);
2527 gen_arth(code
, a0
, a1
)
2529 struct arth
*a0
, *a1
;
2531 struct slist
*s0
, *s1
, *s2
;
2535 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2540 sappend(a0
->s
, a1
->s
);
2542 free_reg(a1
->regno
);
2544 s0
= new_stmt(BPF_ST
);
2545 a0
->regno
= s0
->s
.k
= alloc_reg();
2552 * Here we handle simple allocation of the scratch registers.
2553 * If too many registers are alloc'd, the allocator punts.
2555 static int regused
[BPF_MEMWORDS
];
2559 * Return the next free register.
2564 int n
= BPF_MEMWORDS
;
2567 if (regused
[curreg
])
2568 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2570 regused
[curreg
] = 1;
2574 bpf_error("too many registers needed to evaluate expression");
2579 * Return a register to the table so it can
2589 static struct block
*
2596 s
= new_stmt(BPF_LD
|BPF_LEN
);
2597 b
= new_block(JMP(jmp
));
2608 return gen_len(BPF_JGE
, n
);
2617 b
= gen_len(BPF_JGT
, n
);
2624 gen_byteop(op
, idx
, val
)
2635 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2638 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2639 b
->s
.code
= JMP(BPF_JGE
);
2644 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2645 b
->s
.code
= JMP(BPF_JGT
);
2649 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
2653 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2657 b
= new_block(JMP(BPF_JEQ
));
2665 gen_broadcast(proto
)
2668 bpf_u_int32 hostmask
;
2669 struct block
*b0
, *b1
, *b2
;
2670 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2676 if (linktype
== DLT_EN10MB
)
2677 return gen_ehostop(ebroadcast
, Q_DST
);
2678 if (linktype
== DLT_FDDI
)
2679 return gen_fhostop(ebroadcast
, Q_DST
);
2680 bpf_error("not a broadcast link");
2684 b0
= gen_linktype(ETHERTYPE_IP
);
2685 hostmask
= ~netmask
;
2686 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
2687 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
2688 (bpf_int32
)(~0 & hostmask
), hostmask
);
2693 bpf_error("only ether/ip broadcast filters supported");
2697 gen_multicast(proto
)
2700 register struct block
*b0
, *b1
;
2701 register struct slist
*s
;
2707 if (linktype
== DLT_EN10MB
) {
2708 /* ether[0] & 1 != 0 */
2709 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2711 b0
= new_block(JMP(BPF_JSET
));
2717 if (linktype
== DLT_FDDI
) {
2718 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
2719 /* fddi[1] & 1 != 0 */
2720 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2722 b0
= new_block(JMP(BPF_JSET
));
2727 /* Link not known to support multicasts */
2731 b0
= gen_linktype(ETHERTYPE_IP
);
2732 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
2733 b1
->s
.code
= JMP(BPF_JGE
);
2739 b0
= gen_linktype(ETHERTYPE_IPV6
);
2740 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
2745 bpf_error("only IP multicast filters supported on ethernet/FDDI");
2749 * generate command for inbound/outbound. It's here so we can
2750 * make it link-type specific. 'dir' = 0 implies "inbound",
2751 * = 1 implies "outbound".
2757 register struct block
*b0
;
2759 b0
= gen_relation(BPF_JEQ
,
2760 gen_load(Q_LINK
, gen_loadi(0), 1),