]>
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.111 2000-06-26 05:10:40 assar 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 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
300 "snaplen of 0 rejects all packets");
304 lex_init(buf
? buf
: "");
305 init_linktype(pcap_datalink(p
));
312 root
= gen_retblk(snaplen
);
314 if (optimize
&& !no_optimize
) {
317 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
318 bpf_error("expression rejects all packets");
320 program
->bf_insns
= icode_to_fcode(root
, &len
);
321 program
->bf_len
= len
;
328 * entry point for using the compiler with no pcap open
329 * pass in all the stuff that is needed explicitly instead.
332 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
333 struct bpf_program
*program
,
334 char *buf
, int optimize
, bpf_u_int32 mask
)
342 if (setjmp(top_ctx
)) {
349 /* XXX needed? I don't grok the use of globals here. */
350 snaplen
= snaplen_arg
;
352 lex_init(buf
? buf
: "");
353 init_linktype(linktype_arg
);
360 root
= gen_retblk(snaplen_arg
);
365 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
366 bpf_error("expression rejects all packets");
368 program
->bf_insns
= icode_to_fcode(root
, &len
);
369 program
->bf_len
= len
;
376 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
377 * which of the jt and jf fields has been resolved and which is a pointer
378 * back to another unresolved block (or nil). At least one of the fields
379 * in each block is already resolved.
382 backpatch(list
, target
)
383 struct block
*list
, *target
;
400 * Merge the lists in b0 and b1, using the 'sense' field to indicate
401 * which of jt and jf is the link.
405 struct block
*b0
, *b1
;
407 register struct block
**p
= &b0
;
409 /* Find end of list. */
411 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
413 /* Concatenate the lists. */
421 backpatch(p
, gen_retblk(snaplen
));
422 p
->sense
= !p
->sense
;
423 backpatch(p
, gen_retblk(0));
429 struct block
*b0
, *b1
;
431 backpatch(b0
, b1
->head
);
432 b0
->sense
= !b0
->sense
;
433 b1
->sense
= !b1
->sense
;
435 b1
->sense
= !b1
->sense
;
441 struct block
*b0
, *b1
;
443 b0
->sense
= !b0
->sense
;
444 backpatch(b0
, b1
->head
);
445 b0
->sense
= !b0
->sense
;
454 b
->sense
= !b
->sense
;
457 static struct block
*
458 gen_cmp(offset
, size
, v
)
465 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
468 b
= new_block(JMP(BPF_JEQ
));
475 static struct block
*
476 gen_mcmp(offset
, size
, v
, mask
)
481 struct block
*b
= gen_cmp(offset
, size
, v
);
484 if (mask
!= 0xffffffff) {
485 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
492 static struct block
*
493 gen_bcmp(offset
, size
, v
)
494 register u_int offset
, size
;
495 register const u_char
*v
;
497 register struct block
*b
, *tmp
;
501 register const u_char
*p
= &v
[size
- 4];
502 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
503 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
505 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
512 register const u_char
*p
= &v
[size
- 2];
513 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
515 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
522 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
531 * Various code constructs need to know the layout of the data link
532 * layer. These variables give the necessary offsets. off_linktype
533 * is set to -1 for no encapsulation, in which case, IP is assumed.
535 static u_int off_linktype
;
554 * SLIP doesn't have a link level type. The 16 byte
555 * header is hacked into our SLIP driver.
562 /* XXX this may be the same as the DLT_PPP_BSDOS case */
586 * FDDI doesn't really have a link-level type field.
587 * We assume that SSAP = SNAP is being used and pick
588 * out the encapsulated Ethernet type.
592 off_linktype
+= pcap_fddipad
;
596 off_nl
+= pcap_fddipad
;
605 case DLT_ATM_RFC1483
:
607 * assume routed, non-ISO PDUs
608 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
619 bpf_error("unknown data link type 0x%x", linktype
);
623 static struct block
*
630 s
= new_stmt(BPF_LD
|BPF_IMM
);
632 b
= new_block(JMP(BPF_JEQ
));
638 static inline struct block
*
641 return gen_uncond(1);
644 static inline struct block
*
647 return gen_uncond(0);
650 static struct block
*
654 struct block
*b0
, *b1
;
656 /* If we're not using encapsulation, we're done */
657 if (off_linktype
== -1)
666 if (proto
== ETHERTYPE_IP
)
667 proto
= PPP_IP
; /* XXX was 0x21 */
669 else if (proto
== ETHERTYPE_IPV6
)
678 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
679 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
681 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
696 case ETHERTYPE_ATALK
:
708 if (proto
== ETHERTYPE_IP
)
709 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET
)));
711 else if (proto
== ETHERTYPE_IPV6
)
712 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET6
)));
717 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
720 static struct block
*
721 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
725 u_int src_off
, dst_off
;
727 struct block
*b0
, *b1
;
741 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
742 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
748 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
749 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
756 b0
= gen_linktype(proto
);
757 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
763 static struct block
*
764 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
765 struct in6_addr
*addr
;
766 struct in6_addr
*mask
;
768 u_int src_off
, dst_off
;
770 struct block
*b0
, *b1
;
785 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
786 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
792 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
793 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
800 /* this order is important */
801 a
= (u_int32_t
*)addr
;
802 m
= (u_int32_t
*)mask
;
803 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
804 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
806 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
808 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
810 b0
= gen_linktype(proto
);
816 static struct block
*
817 gen_ehostop(eaddr
, dir
)
818 register const u_char
*eaddr
;
821 register struct block
*b0
, *b1
;
825 return gen_bcmp(6, 6, eaddr
);
828 return gen_bcmp(0, 6, eaddr
);
831 b0
= gen_ehostop(eaddr
, Q_SRC
);
832 b1
= gen_ehostop(eaddr
, Q_DST
);
838 b0
= gen_ehostop(eaddr
, Q_SRC
);
839 b1
= gen_ehostop(eaddr
, Q_DST
);
848 * Like gen_ehostop, but for DLT_FDDI
850 static struct block
*
851 gen_fhostop(eaddr
, dir
)
852 register const u_char
*eaddr
;
855 struct block
*b0
, *b1
;
860 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
862 return gen_bcmp(6 + 1, 6, eaddr
);
867 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
869 return gen_bcmp(0 + 1, 6, eaddr
);
873 b0
= gen_fhostop(eaddr
, Q_SRC
);
874 b1
= gen_fhostop(eaddr
, Q_DST
);
880 b0
= gen_fhostop(eaddr
, Q_SRC
);
881 b1
= gen_fhostop(eaddr
, Q_DST
);
890 * This is quite tricky because there may be pad bytes in front of the
891 * DECNET header, and then there are two possible data packet formats that
892 * carry both src and dst addresses, plus 5 packet types in a format that
893 * carries only the src node, plus 2 types that use a different format and
894 * also carry just the src node.
898 * Instead of doing those all right, we just look for data packets with
899 * 0 or 1 bytes of padding. If you want to look at other packets, that
900 * will require a lot more hacking.
902 * To add support for filtering on DECNET "areas" (network numbers)
903 * one would want to add a "mask" argument to this routine. That would
904 * make the filter even more inefficient, although one could be clever
905 * and not generate masking instructions if the mask is 0xFFFF.
907 static struct block
*
908 gen_dnhostop(addr
, dir
, base_off
)
913 struct block
*b0
, *b1
, *b2
, *tmp
;
914 u_int offset_lh
; /* offset if long header is received */
915 u_int offset_sh
; /* offset if short header is received */
920 offset_sh
= 1; /* follows flags */
921 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
925 offset_sh
= 3; /* follows flags, dstnode */
926 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
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
);
938 /* Inefficient because we do our Calvinball dance twice */
939 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
940 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
947 b0
= gen_linktype(ETHERTYPE_DN
);
948 /* Check for pad = 1, long header case */
949 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
950 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
951 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
952 BPF_H
, (bpf_int32
)ntohs(addr
));
954 /* Check for pad = 0, long header case */
955 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
956 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
959 /* Check for pad = 1, short header case */
960 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
961 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
962 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
963 BPF_H
, (bpf_int32
)ntohs(addr
));
966 /* Check for pad = 0, short header case */
967 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
968 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
972 /* Combine with test for linktype */
977 static struct block
*
978 gen_host(addr
, mask
, proto
, dir
)
984 struct block
*b0
, *b1
;
989 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
990 if (off_linktype
!= -1) {
991 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
993 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
999 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1000 off_nl
+ 12, off_nl
+ 16);
1003 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1004 off_nl
+ 14, off_nl
+ 24);
1007 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1008 off_nl
+ 14, off_nl
+ 24);
1011 bpf_error("'tcp' modifier applied to host");
1014 bpf_error("'udp' modifier applied to host");
1017 bpf_error("'icmp' modifier applied to host");
1020 bpf_error("'igmp' modifier applied to host");
1023 bpf_error("'igrp' modifier applied to host");
1026 bpf_error("'pim' modifier applied to host");
1029 bpf_error("ATALK host filtering not implemented");
1032 return gen_dnhostop(addr
, dir
, off_nl
);
1035 bpf_error("SCA host filtering not implemented");
1038 bpf_error("LAT host filtering not implemented");
1041 bpf_error("MOPDL host filtering not implemented");
1044 bpf_error("MOPRC host filtering not implemented");
1048 bpf_error("'ip6' modifier applied to ip host");
1051 bpf_error("'icmp6' modifier applied to host");
1055 bpf_error("'ah' modifier applied to host");
1058 bpf_error("'esp' modifier applied to host");
1067 static struct block
*
1068 gen_host6(addr
, mask
, proto
, dir
)
1069 struct in6_addr
*addr
;
1070 struct in6_addr
*mask
;
1077 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1080 bpf_error("'ip' modifier applied to ip6 host");
1083 bpf_error("'rarp' modifier applied to ip6 host");
1086 bpf_error("'arp' modifier applied to ip6 host");
1089 bpf_error("'tcp' modifier applied to host");
1092 bpf_error("'udp' modifier applied to host");
1095 bpf_error("'icmp' modifier applied to host");
1098 bpf_error("'igmp' modifier applied to host");
1101 bpf_error("'igrp' modifier applied to host");
1104 bpf_error("'pim' modifier applied to host");
1107 bpf_error("ATALK host filtering not implemented");
1110 bpf_error("'decnet' modifier applied to ip6 host");
1113 bpf_error("SCA host filtering not implemented");
1116 bpf_error("LAT host filtering not implemented");
1119 bpf_error("MOPDL host filtering not implemented");
1122 bpf_error("MOPRC host filtering not implemented");
1125 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1126 off_nl
+ 8, off_nl
+ 24);
1129 bpf_error("'icmp6' modifier applied to host");
1132 bpf_error("'ah' modifier applied to host");
1135 bpf_error("'esp' modifier applied to host");
1145 static struct block
*
1146 gen_gateway(eaddr
, alist
, proto
, dir
)
1147 const u_char
*eaddr
;
1148 bpf_u_int32
**alist
;
1152 struct block
*b0
, *b1
, *tmp
;
1155 bpf_error("direction applied to 'gateway'");
1162 if (linktype
== DLT_EN10MB
)
1163 b0
= gen_ehostop(eaddr
, Q_OR
);
1164 else if (linktype
== DLT_FDDI
)
1165 b0
= gen_fhostop(eaddr
, Q_OR
);
1168 "'gateway' supported only on ethernet or FDDI");
1170 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1172 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1180 bpf_error("illegal modifier of 'gateway'");
1186 gen_proto_abbrev(proto
)
1189 struct block
*b0
, *b1
;
1194 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1196 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1202 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1204 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1210 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1213 #ifndef IPPROTO_IGMP
1214 #define IPPROTO_IGMP 2
1218 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1221 #ifndef IPPROTO_IGRP
1222 #define IPPROTO_IGRP 9
1225 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1229 #define IPPROTO_PIM 103
1233 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1235 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1241 b1
= gen_linktype(ETHERTYPE_IP
);
1245 b1
= gen_linktype(ETHERTYPE_ARP
);
1249 b1
= gen_linktype(ETHERTYPE_REVARP
);
1253 bpf_error("link layer applied in wrong context");
1256 b1
= gen_linktype(ETHERTYPE_ATALK
);
1260 b1
= gen_linktype(ETHERTYPE_DN
);
1264 b1
= gen_linktype(ETHERTYPE_SCA
);
1268 b1
= gen_linktype(ETHERTYPE_LAT
);
1272 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1276 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1281 b1
= gen_linktype(ETHERTYPE_IPV6
);
1284 #ifndef IPPROTO_ICMPV6
1285 #define IPPROTO_ICMPV6 58
1288 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1293 #define IPPROTO_AH 51
1296 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1298 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1304 #define IPPROTO_ESP 50
1307 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1309 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1320 static struct block
*
1327 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1328 s
->s
.k
= off_nl
+ 6;
1329 b
= new_block(JMP(BPF_JSET
));
1337 static struct block
*
1338 gen_portatom(off
, v
)
1345 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1348 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1349 s
->next
->s
.k
= off_nl
+ off
;
1351 b
= new_block(JMP(BPF_JEQ
));
1359 static struct block
*
1360 gen_portatom6(off
, v
)
1364 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1369 gen_portop(port
, proto
, dir
)
1370 int port
, proto
, dir
;
1372 struct block
*b0
, *b1
, *tmp
;
1374 /* ip proto 'proto' */
1375 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1381 b1
= gen_portatom(0, (bpf_int32
)port
);
1385 b1
= gen_portatom(2, (bpf_int32
)port
);
1390 tmp
= gen_portatom(0, (bpf_int32
)port
);
1391 b1
= gen_portatom(2, (bpf_int32
)port
);
1396 tmp
= gen_portatom(0, (bpf_int32
)port
);
1397 b1
= gen_portatom(2, (bpf_int32
)port
);
1409 static struct block
*
1410 gen_port(port
, ip_proto
, dir
)
1415 struct block
*b0
, *b1
, *tmp
;
1417 /* ether proto ip */
1418 b0
= gen_linktype(ETHERTYPE_IP
);
1423 b1
= gen_portop(port
, ip_proto
, dir
);
1427 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1428 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1441 gen_portop6(port
, proto
, dir
)
1442 int port
, proto
, dir
;
1444 struct block
*b0
, *b1
, *tmp
;
1446 /* ip proto 'proto' */
1447 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1451 b1
= gen_portatom6(0, (bpf_int32
)port
);
1455 b1
= gen_portatom6(2, (bpf_int32
)port
);
1460 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1461 b1
= gen_portatom6(2, (bpf_int32
)port
);
1466 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1467 b1
= gen_portatom6(2, (bpf_int32
)port
);
1479 static struct block
*
1480 gen_port6(port
, ip_proto
, dir
)
1485 struct block
*b0
, *b1
, *tmp
;
1487 /* ether proto ip */
1488 b0
= gen_linktype(ETHERTYPE_IPV6
);
1493 b1
= gen_portop6(port
, ip_proto
, dir
);
1497 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1498 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1511 lookup_proto(name
, proto
)
1512 register const char *name
;
1521 v
= pcap_nametoproto(name
);
1522 if (v
== PROTO_UNDEF
)
1523 bpf_error("unknown ip proto '%s'", name
);
1527 /* XXX should look up h/w protocol type based on linktype */
1528 v
= pcap_nametoeproto(name
);
1529 if (v
== PROTO_UNDEF
)
1530 bpf_error("unknown ether proto '%s'", name
);
1551 gen_protochain(v
, proto
, dir
)
1556 #ifdef NO_PROTOCHAIN
1557 return gen_proto(v
, proto
, dir
);
1559 struct block
*b0
, *b
;
1560 struct slist
*s
[100];
1561 int fix2
, fix3
, fix4
, fix5
;
1562 int ahcheck
, again
, end
;
1564 int reg1
= alloc_reg();
1565 int reg2
= alloc_reg();
1567 memset(s
, 0, sizeof(s
));
1568 fix2
= fix3
= fix4
= fix5
= 0;
1575 b0
= gen_protochain(v
, Q_IP
, dir
);
1576 b
= gen_protochain(v
, Q_IPV6
, dir
);
1580 bpf_error("bad protocol applied for 'protochain'");
1584 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1587 * s[0] is a dummy entry to protect other BPF insn from damaged
1588 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1589 * hard to find interdependency made by jump table fixup.
1592 s
[i
] = new_stmt(0); /*dummy*/
1597 b0
= gen_linktype(ETHERTYPE_IP
);
1600 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1601 s
[i
]->s
.k
= off_nl
+ 9;
1603 /* X = ip->ip_hl << 2 */
1604 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1610 b0
= gen_linktype(ETHERTYPE_IPV6
);
1612 /* A = ip6->ip_nxt */
1613 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1614 s
[i
]->s
.k
= off_nl
+ 6;
1616 /* X = sizeof(struct ip6_hdr) */
1617 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1623 bpf_error("unsupported proto to gen_protochain");
1627 /* again: if (A == v) goto end; else fall through; */
1629 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1631 s
[i
]->s
.jt
= NULL
; /*later*/
1632 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1636 #ifndef IPPROTO_NONE
1637 #define IPPROTO_NONE 59
1639 /* if (A == IPPROTO_NONE) goto end */
1640 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1641 s
[i
]->s
.jt
= NULL
; /*later*/
1642 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1643 s
[i
]->s
.k
= IPPROTO_NONE
;
1644 s
[fix5
]->s
.jf
= s
[i
];
1649 if (proto
== Q_IPV6
) {
1650 int v6start
, v6end
, v6advance
, j
;
1653 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1654 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_HOPOPTS
;
1658 s
[fix2
]->s
.jf
= s
[i
];
1660 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1661 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1662 s
[i
]->s
.jt
= NULL
; /*later*/
1663 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1664 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
1666 /* if (A == IPPROTO_ROUTING) goto v6advance */
1667 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1668 s
[i
]->s
.jt
= NULL
; /*later*/
1669 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1670 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1672 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1673 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1674 s
[i
]->s
.jt
= NULL
; /*later*/
1675 s
[i
]->s
.jf
= NULL
; /*later*/
1676 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
1687 * X = X + (P[X] + 1) * 8;
1690 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1693 s
[i
] = new_stmt(BPF_ST
);
1697 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1701 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1703 /* A = P[X + packet head]; */
1704 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1708 s
[i
] = new_stmt(BPF_ST
);
1712 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1715 /* A = P[X + packet head] */
1716 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1720 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1724 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1728 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1731 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1735 /* goto again; (must use BPF_JA for backward jump) */
1736 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1737 s
[i
]->s
.k
= again
- i
- 1;
1738 s
[i
- 1]->s
.jf
= s
[i
];
1742 for (j
= v6start
; j
<= v6end
; j
++)
1743 s
[j
]->s
.jt
= s
[v6advance
];
1748 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1750 s
[fix2
]->s
.jf
= s
[i
];
1756 /* if (A == IPPROTO_AH) then fall through; else goto end; */
1757 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1758 s
[i
]->s
.jt
= NULL
; /*later*/
1759 s
[i
]->s
.jf
= NULL
; /*later*/
1760 s
[i
]->s
.k
= IPPROTO_AH
;
1762 s
[fix3
]->s
.jf
= s
[ahcheck
];
1769 * X = X + (P[X] + 2) * 4;
1772 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1775 s
[i
] = new_stmt(BPF_ST
);
1779 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1783 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1785 /* A = P[X + packet head]; */
1786 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1790 s
[i
] = new_stmt(BPF_ST
);
1794 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1797 /* A = P[X + packet head] */
1798 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1802 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1806 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1810 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1813 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1817 /* goto again; (must use BPF_JA for backward jump) */
1818 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1819 s
[i
]->s
.k
= again
- i
- 1;
1824 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1826 s
[fix2
]->s
.jt
= s
[end
];
1827 s
[fix4
]->s
.jf
= s
[end
];
1828 s
[fix5
]->s
.jt
= s
[end
];
1835 for (i
= 0; i
< max
- 1; i
++)
1836 s
[i
]->next
= s
[i
+ 1];
1837 s
[max
- 1]->next
= NULL
;
1842 b
= new_block(JMP(BPF_JEQ
));
1843 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
1854 static struct block
*
1855 gen_proto(v
, proto
, dir
)
1860 struct block
*b0
, *b1
;
1862 if (dir
!= Q_DEFAULT
)
1863 bpf_error("direction applied to 'proto'");
1868 b0
= gen_proto(v
, Q_IP
, dir
);
1869 b1
= gen_proto(v
, Q_IPV6
, dir
);
1876 b0
= gen_linktype(ETHERTYPE_IP
);
1878 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
1880 b1
= gen_protochain(v
, Q_IP
);
1886 bpf_error("arp does not encapsulate another protocol");
1890 bpf_error("rarp does not encapsulate another protocol");
1894 bpf_error("atalk encapsulation is not specifiable");
1898 bpf_error("decnet encapsulation is not specifiable");
1902 bpf_error("sca does not encapsulate another protocol");
1906 bpf_error("lat does not encapsulate another protocol");
1910 bpf_error("moprc does not encapsulate another protocol");
1914 bpf_error("mopdl does not encapsulate another protocol");
1918 return gen_linktype(v
);
1921 bpf_error("'udp proto' is bogus");
1925 bpf_error("'tcp proto' is bogus");
1929 bpf_error("'icmp proto' is bogus");
1933 bpf_error("'igmp proto' is bogus");
1937 bpf_error("'igrp proto' is bogus");
1941 bpf_error("'pim proto' is bogus");
1946 b0
= gen_linktype(ETHERTYPE_IPV6
);
1948 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
1950 b1
= gen_protochain(v
, Q_IPV6
);
1956 bpf_error("'icmp6 proto' is bogus");
1960 bpf_error("'ah proto' is bogus");
1963 bpf_error("'ah proto' is bogus");
1974 register const char *name
;
1977 int proto
= q
.proto
;
1981 bpf_u_int32 mask
, addr
;
1983 bpf_u_int32
**alist
;
1986 struct sockaddr_in
*sin
;
1987 struct sockaddr_in6
*sin6
;
1988 struct addrinfo
*res
, *res0
;
1989 struct in6_addr mask128
;
1991 struct block
*b
, *tmp
;
1992 int port
, real_proto
;
1997 addr
= pcap_nametonetaddr(name
);
1999 bpf_error("unknown network '%s'", name
);
2000 /* Left justify network addr and calculate its network mask */
2002 while (addr
&& (addr
& 0xff000000) == 0) {
2006 return gen_host(addr
, mask
, proto
, dir
);
2010 if (proto
== Q_LINK
) {
2014 eaddr
= pcap_ether_hostton(name
);
2017 "unknown ether host '%s'", name
);
2018 return gen_ehostop(eaddr
, dir
);
2021 eaddr
= pcap_ether_hostton(name
);
2024 "unknown FDDI host '%s'", name
);
2025 return gen_fhostop(eaddr
, dir
);
2029 "only ethernet/FDDI supports link-level host name");
2032 } else if (proto
== Q_DECNET
) {
2033 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2035 * I don't think DECNET hosts can be multihomed, so
2036 * there is no need to build up a list of addresses
2038 return (gen_host(dn_addr
, 0, proto
, dir
));
2041 alist
= pcap_nametoaddr(name
);
2042 if (alist
== NULL
|| *alist
== NULL
)
2043 bpf_error("unknown host '%s'", name
);
2045 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2047 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2049 tmp
= gen_host(**alist
++, 0xffffffff,
2056 memset(&mask128
, 0xff, sizeof(mask128
));
2057 res0
= res
= pcap_nametoaddrinfo(name
);
2059 bpf_error("unknown host '%s'", name
);
2061 tproto
= tproto6
= proto
;
2062 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2066 for (res
= res0
; res
; res
= res
->ai_next
) {
2067 switch (res
->ai_family
) {
2069 if (tproto
== Q_IPV6
)
2072 sin
= (struct sockaddr_in
*)
2074 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2075 0xffffffff, tproto
, dir
);
2078 if (tproto6
== Q_IP
)
2081 sin6
= (struct sockaddr_in6
*)
2083 tmp
= gen_host6(&sin6
->sin6_addr
,
2084 &mask128
, tproto6
, dir
);
2093 bpf_error("unknown host '%s'%s", name
,
2094 (proto
== Q_DEFAULT
)
2096 : " for specified address family");
2103 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2104 bpf_error("illegal qualifier of 'port'");
2105 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2106 bpf_error("unknown port '%s'", name
);
2107 if (proto
== Q_UDP
) {
2108 if (real_proto
== IPPROTO_TCP
)
2109 bpf_error("port '%s' is tcp", name
);
2111 /* override PROTO_UNDEF */
2112 real_proto
= IPPROTO_UDP
;
2114 if (proto
== Q_TCP
) {
2115 if (real_proto
== IPPROTO_UDP
)
2116 bpf_error("port '%s' is udp", name
);
2118 /* override PROTO_UNDEF */
2119 real_proto
= IPPROTO_TCP
;
2122 return gen_port(port
, real_proto
, dir
);
2126 b
= gen_port(port
, real_proto
, dir
);
2127 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2134 eaddr
= pcap_ether_hostton(name
);
2136 bpf_error("unknown ether host: %s", name
);
2138 alist
= pcap_nametoaddr(name
);
2139 if (alist
== NULL
|| *alist
== NULL
)
2140 bpf_error("unknown host '%s'", name
);
2141 return gen_gateway(eaddr
, alist
, proto
, dir
);
2143 bpf_error("'gateway' not supported in this configuration");
2147 real_proto
= lookup_proto(name
, proto
);
2148 if (real_proto
>= 0)
2149 return gen_proto(real_proto
, proto
, dir
);
2151 bpf_error("unknown protocol: %s", name
);
2154 real_proto
= lookup_proto(name
, proto
);
2155 if (real_proto
>= 0)
2156 return gen_protochain(real_proto
, proto
, dir
);
2158 bpf_error("unknown protocol: %s", name
);
2170 gen_mcode(s1
, s2
, masklen
, q
)
2171 register const char *s1
, *s2
;
2172 register int masklen
;
2175 register int nlen
, mlen
;
2178 nlen
= __pcap_atoin(s1
, &n
);
2179 /* Promote short ipaddr */
2183 mlen
= __pcap_atoin(s2
, &m
);
2184 /* Promote short ipaddr */
2187 bpf_error("non-network bits set in \"%s mask %s\"",
2190 /* Convert mask len to mask */
2192 bpf_error("mask length must be <= 32");
2193 m
= 0xffffffff << (32 - masklen
);
2195 bpf_error("non-network bits set in \"%s/%d\"",
2202 return gen_host(n
, m
, q
.proto
, q
.dir
);
2205 bpf_error("Mask syntax for networks only");
2212 register const char *s
;
2217 int proto
= q
.proto
;
2223 else if (q
.proto
== Q_DECNET
)
2224 vlen
= __pcap_atodn(s
, &v
);
2226 vlen
= __pcap_atoin(s
, &v
);
2233 if (proto
== Q_DECNET
)
2234 return gen_host(v
, 0, proto
, dir
);
2235 else if (proto
== Q_LINK
) {
2236 bpf_error("illegal link layer address");
2239 if (s
== NULL
&& q
.addr
== Q_NET
) {
2240 /* Promote short net number */
2241 while (v
&& (v
& 0xff000000) == 0) {
2246 /* Promote short ipaddr */
2250 return gen_host(v
, mask
, proto
, dir
);
2255 proto
= IPPROTO_UDP
;
2256 else if (proto
== Q_TCP
)
2257 proto
= IPPROTO_TCP
;
2258 else if (proto
== Q_DEFAULT
)
2259 proto
= PROTO_UNDEF
;
2261 bpf_error("illegal qualifier of 'port'");
2264 return gen_port((int)v
, proto
, dir
);
2268 b
= gen_port((int)v
, proto
, dir
);
2269 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2275 bpf_error("'gateway' requires a name");
2279 return gen_proto((int)v
, proto
, dir
);
2282 return gen_protochain((int)v
, proto
, dir
);
2297 gen_mcode6(s1
, s2
, masklen
, q
)
2298 register const char *s1
, *s2
;
2299 register int masklen
;
2302 struct addrinfo
*res
;
2303 struct in6_addr
*addr
;
2304 struct in6_addr mask
;
2309 bpf_error("no mask %s supported", s2
);
2311 res
= pcap_nametoaddrinfo(s1
);
2313 bpf_error("invalid ip6 address %s", s1
);
2315 bpf_error("%s resolved to multiple address", s1
);
2316 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2318 if (sizeof(mask
) * 8 < masklen
)
2319 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2320 memset(&mask
, 0xff, masklen
/ 8);
2322 mask
.s6_addr
[masklen
/ 8] =
2323 (0xff << (8 - masklen
% 8)) & 0xff;
2326 a
= (u_int32_t
*)addr
;
2327 m
= (u_int32_t
*)&mask
;
2328 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2329 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2330 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2338 bpf_error("Mask syntax for networks only");
2342 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2347 bpf_error("invalid qualifier against IPv6 address");
2355 register const u_char
*eaddr
;
2358 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2359 if (linktype
== DLT_EN10MB
)
2360 return gen_ehostop(eaddr
, (int)q
.dir
);
2361 if (linktype
== DLT_FDDI
)
2362 return gen_fhostop(eaddr
, (int)q
.dir
);
2364 bpf_error("ethernet address used in non-ether expression");
2370 struct slist
*s0
, *s1
;
2373 * This is definitely not the best way to do this, but the
2374 * lists will rarely get long.
2381 static struct slist
*
2387 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2392 static struct slist
*
2398 s
= new_stmt(BPF_LD
|BPF_MEM
);
2404 gen_load(proto
, index
, size
)
2409 struct slist
*s
, *tmp
;
2411 int regno
= alloc_reg();
2413 free_reg(index
->regno
);
2417 bpf_error("data size must be 1, 2, or 4");
2433 bpf_error("unsupported index operation");
2436 s
= xfer_to_x(index
);
2437 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2439 sappend(index
->s
, s
);
2454 /* XXX Note that we assume a fixed link header here. */
2455 s
= xfer_to_x(index
);
2456 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2459 sappend(index
->s
, s
);
2461 b
= gen_proto_abbrev(proto
);
2463 gen_and(index
->b
, b
);
2473 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2475 sappend(s
, xfer_to_a(index
));
2476 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2477 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2478 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2480 sappend(index
->s
, s
);
2482 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2484 gen_and(index
->b
, b
);
2486 gen_and(gen_proto_abbrev(Q_IP
), b
);
2492 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2496 index
->regno
= regno
;
2497 s
= new_stmt(BPF_ST
);
2499 sappend(index
->s
, s
);
2505 gen_relation(code
, a0
, a1
, reversed
)
2507 struct arth
*a0
, *a1
;
2510 struct slist
*s0
, *s1
, *s2
;
2511 struct block
*b
, *tmp
;
2515 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2516 b
= new_block(JMP(code
));
2517 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2518 reversed
= !reversed
;
2519 b
->s
.k
= 0x80000000;
2527 sappend(a0
->s
, a1
->s
);
2531 free_reg(a0
->regno
);
2532 free_reg(a1
->regno
);
2534 /* 'and' together protocol checks */
2537 gen_and(a0
->b
, tmp
= a1
->b
);
2553 int regno
= alloc_reg();
2554 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2557 s
= new_stmt(BPF_LD
|BPF_LEN
);
2558 s
->next
= new_stmt(BPF_ST
);
2559 s
->next
->s
.k
= regno
;
2574 a
= (struct arth
*)newchunk(sizeof(*a
));
2578 s
= new_stmt(BPF_LD
|BPF_IMM
);
2580 s
->next
= new_stmt(BPF_ST
);
2596 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2599 s
= new_stmt(BPF_ST
);
2607 gen_arth(code
, a0
, a1
)
2609 struct arth
*a0
, *a1
;
2611 struct slist
*s0
, *s1
, *s2
;
2615 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2620 sappend(a0
->s
, a1
->s
);
2622 free_reg(a1
->regno
);
2624 s0
= new_stmt(BPF_ST
);
2625 a0
->regno
= s0
->s
.k
= alloc_reg();
2632 * Here we handle simple allocation of the scratch registers.
2633 * If too many registers are alloc'd, the allocator punts.
2635 static int regused
[BPF_MEMWORDS
];
2639 * Return the next free register.
2644 int n
= BPF_MEMWORDS
;
2647 if (regused
[curreg
])
2648 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2650 regused
[curreg
] = 1;
2654 bpf_error("too many registers needed to evaluate expression");
2659 * Return a register to the table so it can
2669 static struct block
*
2676 s
= new_stmt(BPF_LD
|BPF_LEN
);
2677 b
= new_block(JMP(jmp
));
2688 return gen_len(BPF_JGE
, n
);
2697 b
= gen_len(BPF_JGT
, n
);
2704 gen_byteop(op
, idx
, val
)
2715 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2718 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2719 b
->s
.code
= JMP(BPF_JGE
);
2724 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2725 b
->s
.code
= JMP(BPF_JGT
);
2729 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
2733 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2737 b
= new_block(JMP(BPF_JEQ
));
2745 gen_broadcast(proto
)
2748 bpf_u_int32 hostmask
;
2749 struct block
*b0
, *b1
, *b2
;
2750 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2756 if (linktype
== DLT_EN10MB
)
2757 return gen_ehostop(ebroadcast
, Q_DST
);
2758 if (linktype
== DLT_FDDI
)
2759 return gen_fhostop(ebroadcast
, Q_DST
);
2760 bpf_error("not a broadcast link");
2764 b0
= gen_linktype(ETHERTYPE_IP
);
2765 hostmask
= ~netmask
;
2766 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
2767 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
2768 (bpf_int32
)(~0 & hostmask
), hostmask
);
2773 bpf_error("only ether/ip broadcast filters supported");
2777 gen_multicast(proto
)
2780 register struct block
*b0
, *b1
;
2781 register struct slist
*s
;
2787 if (linktype
== DLT_EN10MB
) {
2788 /* ether[0] & 1 != 0 */
2789 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2791 b0
= new_block(JMP(BPF_JSET
));
2797 if (linktype
== DLT_FDDI
) {
2798 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
2799 /* fddi[1] & 1 != 0 */
2800 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2802 b0
= new_block(JMP(BPF_JSET
));
2807 /* Link not known to support multicasts */
2811 b0
= gen_linktype(ETHERTYPE_IP
);
2812 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
2813 b1
->s
.code
= JMP(BPF_JGE
);
2819 b0
= gen_linktype(ETHERTYPE_IPV6
);
2820 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
2825 bpf_error("only IP multicast filters supported on ethernet/FDDI");
2829 * generate command for inbound/outbound. It's here so we can
2830 * make it link-type specific. 'dir' = 0 implies "inbound",
2831 * = 1 implies "outbound".
2837 register struct block
*b0
;
2839 b0
= gen_relation(BPF_JEQ
,
2840 gen_load(Q_LINK
, gen_loadi(0), 1),