]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
92f83914ad48be737ad1a9adec4e1eb606e389d1
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.112 2000-07-01 03:32:50 assar Exp $ (LBL)";
27 #include <sys/types.h>
28 #include <sys/socket.h>
31 #include <sys/param.h>
38 #include <netinet/in.h>
39 #include <netinet/if_ether.h>
48 #include "ethertype.h"
51 #include <pcap-namedb.h>
54 #include <sys/socket.h>
58 #ifdef HAVE_OS_PROTO_H
62 #define JMP(c) ((c)|BPF_JMP|BPF_K)
65 static jmp_buf top_ctx
;
66 static pcap_t
*bpf_pcap
;
70 int pcap_fddipad
= PCAP_FDDIPAD
;
77 bpf_error(const char *fmt
, ...)
83 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
90 static void init_linktype(int);
92 static int alloc_reg(void);
93 static void free_reg(int);
95 static struct block
*root
;
98 * We divy out chunks of memory rather than call malloc each time so
99 * we don't have to worry about leaking memory. It's probably
100 * not a big deal if all this memory was wasted but it this ever
101 * goes into a library that would probably not be a good idea.
104 #define CHUNK0SIZE 1024
110 static struct chunk chunks
[NCHUNKS
];
111 static int cur_chunk
;
113 static void *newchunk(u_int
);
114 static void freechunks(void);
115 static inline struct block
*new_block(int);
116 static inline struct slist
*new_stmt(int);
117 static struct block
*gen_retblk(int);
118 static inline void syntax(void);
120 static void backpatch(struct block
*, struct block
*);
121 static void merge(struct block
*, struct block
*);
122 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
123 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
124 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
125 static struct block
*gen_uncond(int);
126 static inline struct block
*gen_true(void);
127 static inline struct block
*gen_false(void);
128 static struct block
*gen_linktype(int);
129 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
131 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
133 static struct block
*gen_ehostop(const u_char
*, int);
134 static struct block
*gen_fhostop(const u_char
*, int);
135 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
136 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
138 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
141 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
143 static struct block
*gen_ipfrag(void);
144 static struct block
*gen_portatom(int, bpf_int32
);
146 static struct block
*gen_portatom6(int, bpf_int32
);
148 struct block
*gen_portop(int, int, int);
149 static struct block
*gen_port(int, int, int);
151 struct block
*gen_portop6(int, int, int);
152 static struct block
*gen_port6(int, int, int);
154 static int lookup_proto(const char *, int);
155 static struct block
*gen_proto(int, int, int);
156 static struct slist
*xfer_to_x(struct arth
*);
157 static struct slist
*xfer_to_a(struct arth
*);
158 static struct block
*gen_len(int, int);
168 /* XXX Round up to nearest long. */
169 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
171 /* XXX Round up to structure boundary. */
175 cp
= &chunks
[cur_chunk
];
176 if (n
> cp
->n_left
) {
177 ++cp
, k
= ++cur_chunk
;
179 bpf_error("out of memory");
180 size
= CHUNK0SIZE
<< k
;
181 cp
->m
= (void *)malloc(size
);
182 memset((char *)cp
->m
, 0, size
);
185 bpf_error("out of memory");
188 return (void *)((char *)cp
->m
+ cp
->n_left
);
197 for (i
= 0; i
< NCHUNKS
; ++i
)
198 if (chunks
[i
].m
!= NULL
) {
205 * A strdup whose allocations are freed after code generation is over.
209 register const char *s
;
211 int n
= strlen(s
) + 1;
212 char *cp
= newchunk(n
);
218 static inline struct block
*
224 p
= (struct block
*)newchunk(sizeof(*p
));
231 static inline struct slist
*
237 p
= (struct slist
*)newchunk(sizeof(*p
));
243 static struct block
*
247 struct block
*b
= new_block(BPF_RET
|BPF_K
);
256 bpf_error("syntax error in filter expression");
259 static bpf_u_int32 netmask
;
264 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
265 char *buf
, int optimize
, bpf_u_int32 mask
)
274 if (setjmp(top_ctx
)) {
280 snaplen
= pcap_snapshot(p
);
282 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
283 "snaplen of 0 rejects all packets");
287 lex_init(buf
? buf
: "");
288 init_linktype(pcap_datalink(p
));
295 root
= gen_retblk(snaplen
);
297 if (optimize
&& !no_optimize
) {
300 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
301 bpf_error("expression rejects all packets");
303 program
->bf_insns
= icode_to_fcode(root
, &len
);
304 program
->bf_len
= len
;
311 * entry point for using the compiler with no pcap open
312 * pass in all the stuff that is needed explicitly instead.
315 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
316 struct bpf_program
*program
,
317 char *buf
, int optimize
, bpf_u_int32 mask
)
325 if (setjmp(top_ctx
)) {
332 /* XXX needed? I don't grok the use of globals here. */
333 snaplen
= snaplen_arg
;
335 lex_init(buf
? buf
: "");
336 init_linktype(linktype_arg
);
343 root
= gen_retblk(snaplen_arg
);
348 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
349 bpf_error("expression rejects all packets");
351 program
->bf_insns
= icode_to_fcode(root
, &len
);
352 program
->bf_len
= len
;
359 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
360 * which of the jt and jf fields has been resolved and which is a pointer
361 * back to another unresolved block (or nil). At least one of the fields
362 * in each block is already resolved.
365 backpatch(list
, target
)
366 struct block
*list
, *target
;
383 * Merge the lists in b0 and b1, using the 'sense' field to indicate
384 * which of jt and jf is the link.
388 struct block
*b0
, *b1
;
390 register struct block
**p
= &b0
;
392 /* Find end of list. */
394 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
396 /* Concatenate the lists. */
404 backpatch(p
, gen_retblk(snaplen
));
405 p
->sense
= !p
->sense
;
406 backpatch(p
, gen_retblk(0));
412 struct block
*b0
, *b1
;
414 backpatch(b0
, b1
->head
);
415 b0
->sense
= !b0
->sense
;
416 b1
->sense
= !b1
->sense
;
418 b1
->sense
= !b1
->sense
;
424 struct block
*b0
, *b1
;
426 b0
->sense
= !b0
->sense
;
427 backpatch(b0
, b1
->head
);
428 b0
->sense
= !b0
->sense
;
437 b
->sense
= !b
->sense
;
440 static struct block
*
441 gen_cmp(offset
, size
, v
)
448 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
451 b
= new_block(JMP(BPF_JEQ
));
458 static struct block
*
459 gen_mcmp(offset
, size
, v
, mask
)
464 struct block
*b
= gen_cmp(offset
, size
, v
);
467 if (mask
!= 0xffffffff) {
468 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
475 static struct block
*
476 gen_bcmp(offset
, size
, v
)
477 register u_int offset
, size
;
478 register const u_char
*v
;
480 register struct block
*b
, *tmp
;
484 register const u_char
*p
= &v
[size
- 4];
485 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
486 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
488 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
495 register const u_char
*p
= &v
[size
- 2];
496 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
498 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
505 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
514 * Various code constructs need to know the layout of the data link
515 * layer. These variables give the necessary offsets. off_linktype
516 * is set to -1 for no encapsulation, in which case, IP is assumed.
518 static u_int off_linktype
;
537 * SLIP doesn't have a link level type. The 16 byte
538 * header is hacked into our SLIP driver.
545 /* XXX this may be the same as the DLT_PPP_BSDOS case */
569 * FDDI doesn't really have a link-level type field.
570 * We assume that SSAP = SNAP is being used and pick
571 * out the encapsulated Ethernet type.
575 off_linktype
+= pcap_fddipad
;
579 off_nl
+= pcap_fddipad
;
588 case DLT_ATM_RFC1483
:
590 * assume routed, non-ISO PDUs
591 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
602 bpf_error("unknown data link type 0x%x", linktype
);
606 static struct block
*
613 s
= new_stmt(BPF_LD
|BPF_IMM
);
615 b
= new_block(JMP(BPF_JEQ
));
621 static inline struct block
*
624 return gen_uncond(1);
627 static inline struct block
*
630 return gen_uncond(0);
633 static struct block
*
637 struct block
*b0
, *b1
;
639 /* If we're not using encapsulation, we're done */
640 if (off_linktype
== -1)
649 if (proto
== ETHERTYPE_IP
)
650 proto
= PPP_IP
; /* XXX was 0x21 */
652 else if (proto
== ETHERTYPE_IPV6
)
661 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
662 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
664 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
679 case ETHERTYPE_ATALK
:
691 if (proto
== ETHERTYPE_IP
)
692 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET
)));
694 else if (proto
== ETHERTYPE_IPV6
)
695 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET6
)));
700 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
703 static struct block
*
704 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
708 u_int src_off
, dst_off
;
710 struct block
*b0
, *b1
;
724 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
725 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
731 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
732 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
739 b0
= gen_linktype(proto
);
740 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
746 static struct block
*
747 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
748 struct in6_addr
*addr
;
749 struct in6_addr
*mask
;
751 u_int src_off
, dst_off
;
753 struct block
*b0
, *b1
;
768 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
769 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
775 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
776 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
783 /* this order is important */
784 a
= (u_int32_t
*)addr
;
785 m
= (u_int32_t
*)mask
;
786 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
787 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
789 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
791 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
793 b0
= gen_linktype(proto
);
799 static struct block
*
800 gen_ehostop(eaddr
, dir
)
801 register const u_char
*eaddr
;
804 register struct block
*b0
, *b1
;
808 return gen_bcmp(6, 6, eaddr
);
811 return gen_bcmp(0, 6, eaddr
);
814 b0
= gen_ehostop(eaddr
, Q_SRC
);
815 b1
= gen_ehostop(eaddr
, Q_DST
);
821 b0
= gen_ehostop(eaddr
, Q_SRC
);
822 b1
= gen_ehostop(eaddr
, Q_DST
);
831 * Like gen_ehostop, but for DLT_FDDI
833 static struct block
*
834 gen_fhostop(eaddr
, dir
)
835 register const u_char
*eaddr
;
838 struct block
*b0
, *b1
;
843 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
845 return gen_bcmp(6 + 1, 6, eaddr
);
850 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
852 return gen_bcmp(0 + 1, 6, eaddr
);
856 b0
= gen_fhostop(eaddr
, Q_SRC
);
857 b1
= gen_fhostop(eaddr
, Q_DST
);
863 b0
= gen_fhostop(eaddr
, Q_SRC
);
864 b1
= gen_fhostop(eaddr
, Q_DST
);
873 * This is quite tricky because there may be pad bytes in front of the
874 * DECNET header, and then there are two possible data packet formats that
875 * carry both src and dst addresses, plus 5 packet types in a format that
876 * carries only the src node, plus 2 types that use a different format and
877 * also carry just the src node.
881 * Instead of doing those all right, we just look for data packets with
882 * 0 or 1 bytes of padding. If you want to look at other packets, that
883 * will require a lot more hacking.
885 * To add support for filtering on DECNET "areas" (network numbers)
886 * one would want to add a "mask" argument to this routine. That would
887 * make the filter even more inefficient, although one could be clever
888 * and not generate masking instructions if the mask is 0xFFFF.
890 static struct block
*
891 gen_dnhostop(addr
, dir
, base_off
)
896 struct block
*b0
, *b1
, *b2
, *tmp
;
897 u_int offset_lh
; /* offset if long header is received */
898 u_int offset_sh
; /* offset if short header is received */
903 offset_sh
= 1; /* follows flags */
904 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
908 offset_sh
= 3; /* follows flags, dstnode */
909 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
913 /* Inefficient because we do our Calvinball dance twice */
914 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
915 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
921 /* Inefficient because we do our Calvinball dance twice */
922 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
923 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
930 b0
= gen_linktype(ETHERTYPE_DN
);
931 /* Check for pad = 1, long header case */
932 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
933 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
934 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
935 BPF_H
, (bpf_int32
)ntohs(addr
));
937 /* Check for pad = 0, long header case */
938 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
939 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
942 /* Check for pad = 1, short header case */
943 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
944 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
945 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
946 BPF_H
, (bpf_int32
)ntohs(addr
));
949 /* Check for pad = 0, short header case */
950 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
951 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
955 /* Combine with test for linktype */
960 static struct block
*
961 gen_host(addr
, mask
, proto
, dir
)
967 struct block
*b0
, *b1
;
972 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
973 if (off_linktype
!= -1) {
974 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
976 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
982 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
983 off_nl
+ 12, off_nl
+ 16);
986 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
987 off_nl
+ 14, off_nl
+ 24);
990 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
991 off_nl
+ 14, off_nl
+ 24);
994 bpf_error("'tcp' modifier applied to host");
997 bpf_error("'udp' modifier applied to host");
1000 bpf_error("'icmp' modifier applied to host");
1003 bpf_error("'igmp' modifier applied to host");
1006 bpf_error("'igrp' modifier applied to host");
1009 bpf_error("'pim' modifier applied to host");
1012 bpf_error("ATALK host filtering not implemented");
1015 return gen_dnhostop(addr
, dir
, off_nl
);
1018 bpf_error("SCA host filtering not implemented");
1021 bpf_error("LAT host filtering not implemented");
1024 bpf_error("MOPDL host filtering not implemented");
1027 bpf_error("MOPRC host filtering not implemented");
1031 bpf_error("'ip6' modifier applied to ip host");
1034 bpf_error("'icmp6' modifier applied to host");
1038 bpf_error("'ah' modifier applied to host");
1041 bpf_error("'esp' modifier applied to host");
1050 static struct block
*
1051 gen_host6(addr
, mask
, proto
, dir
)
1052 struct in6_addr
*addr
;
1053 struct in6_addr
*mask
;
1060 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1063 bpf_error("'ip' modifier applied to ip6 host");
1066 bpf_error("'rarp' modifier applied to ip6 host");
1069 bpf_error("'arp' modifier applied to ip6 host");
1072 bpf_error("'tcp' modifier applied to host");
1075 bpf_error("'udp' modifier applied to host");
1078 bpf_error("'icmp' modifier applied to host");
1081 bpf_error("'igmp' modifier applied to host");
1084 bpf_error("'igrp' modifier applied to host");
1087 bpf_error("'pim' modifier applied to host");
1090 bpf_error("ATALK host filtering not implemented");
1093 bpf_error("'decnet' modifier applied to ip6 host");
1096 bpf_error("SCA host filtering not implemented");
1099 bpf_error("LAT host filtering not implemented");
1102 bpf_error("MOPDL host filtering not implemented");
1105 bpf_error("MOPRC host filtering not implemented");
1108 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1109 off_nl
+ 8, off_nl
+ 24);
1112 bpf_error("'icmp6' modifier applied to host");
1115 bpf_error("'ah' modifier applied to host");
1118 bpf_error("'esp' modifier applied to host");
1128 static struct block
*
1129 gen_gateway(eaddr
, alist
, proto
, dir
)
1130 const u_char
*eaddr
;
1131 bpf_u_int32
**alist
;
1135 struct block
*b0
, *b1
, *tmp
;
1138 bpf_error("direction applied to 'gateway'");
1145 if (linktype
== DLT_EN10MB
)
1146 b0
= gen_ehostop(eaddr
, Q_OR
);
1147 else if (linktype
== DLT_FDDI
)
1148 b0
= gen_fhostop(eaddr
, Q_OR
);
1151 "'gateway' supported only on ethernet or FDDI");
1153 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1155 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1163 bpf_error("illegal modifier of 'gateway'");
1169 gen_proto_abbrev(proto
)
1180 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1182 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1188 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1190 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1196 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1199 #ifndef IPPROTO_IGMP
1200 #define IPPROTO_IGMP 2
1204 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1207 #ifndef IPPROTO_IGRP
1208 #define IPPROTO_IGRP 9
1211 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1215 #define IPPROTO_PIM 103
1219 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1221 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1227 b1
= gen_linktype(ETHERTYPE_IP
);
1231 b1
= gen_linktype(ETHERTYPE_ARP
);
1235 b1
= gen_linktype(ETHERTYPE_REVARP
);
1239 bpf_error("link layer applied in wrong context");
1242 b1
= gen_linktype(ETHERTYPE_ATALK
);
1246 b1
= gen_linktype(ETHERTYPE_DN
);
1250 b1
= gen_linktype(ETHERTYPE_SCA
);
1254 b1
= gen_linktype(ETHERTYPE_LAT
);
1258 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1262 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1267 b1
= gen_linktype(ETHERTYPE_IPV6
);
1270 #ifndef IPPROTO_ICMPV6
1271 #define IPPROTO_ICMPV6 58
1274 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1279 #define IPPROTO_AH 51
1282 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1284 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1290 #define IPPROTO_ESP 50
1293 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1295 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1306 static struct block
*
1313 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1314 s
->s
.k
= off_nl
+ 6;
1315 b
= new_block(JMP(BPF_JSET
));
1323 static struct block
*
1324 gen_portatom(off
, v
)
1331 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1334 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1335 s
->next
->s
.k
= off_nl
+ off
;
1337 b
= new_block(JMP(BPF_JEQ
));
1345 static struct block
*
1346 gen_portatom6(off
, v
)
1350 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1355 gen_portop(port
, proto
, dir
)
1356 int port
, proto
, dir
;
1358 struct block
*b0
, *b1
, *tmp
;
1360 /* ip proto 'proto' */
1361 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1367 b1
= gen_portatom(0, (bpf_int32
)port
);
1371 b1
= gen_portatom(2, (bpf_int32
)port
);
1376 tmp
= gen_portatom(0, (bpf_int32
)port
);
1377 b1
= gen_portatom(2, (bpf_int32
)port
);
1382 tmp
= gen_portatom(0, (bpf_int32
)port
);
1383 b1
= gen_portatom(2, (bpf_int32
)port
);
1395 static struct block
*
1396 gen_port(port
, ip_proto
, dir
)
1401 struct block
*b0
, *b1
, *tmp
;
1403 /* ether proto ip */
1404 b0
= gen_linktype(ETHERTYPE_IP
);
1409 b1
= gen_portop(port
, ip_proto
, dir
);
1413 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1414 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1427 gen_portop6(port
, proto
, dir
)
1428 int port
, proto
, dir
;
1430 struct block
*b0
, *b1
, *tmp
;
1432 /* ip proto 'proto' */
1433 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1437 b1
= gen_portatom6(0, (bpf_int32
)port
);
1441 b1
= gen_portatom6(2, (bpf_int32
)port
);
1446 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1447 b1
= gen_portatom6(2, (bpf_int32
)port
);
1452 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1453 b1
= gen_portatom6(2, (bpf_int32
)port
);
1465 static struct block
*
1466 gen_port6(port
, ip_proto
, dir
)
1471 struct block
*b0
, *b1
, *tmp
;
1473 /* ether proto ip */
1474 b0
= gen_linktype(ETHERTYPE_IPV6
);
1479 b1
= gen_portop6(port
, ip_proto
, dir
);
1483 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1484 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1497 lookup_proto(name
, proto
)
1498 register const char *name
;
1507 v
= pcap_nametoproto(name
);
1508 if (v
== PROTO_UNDEF
)
1509 bpf_error("unknown ip proto '%s'", name
);
1513 /* XXX should look up h/w protocol type based on linktype */
1514 v
= pcap_nametoeproto(name
);
1515 if (v
== PROTO_UNDEF
)
1516 bpf_error("unknown ether proto '%s'", name
);
1537 gen_protochain(v
, proto
, dir
)
1542 #ifdef NO_PROTOCHAIN
1543 return gen_proto(v
, proto
, dir
);
1545 struct block
*b0
, *b
;
1546 struct slist
*s
[100];
1547 int fix2
, fix3
, fix4
, fix5
;
1548 int ahcheck
, again
, end
;
1550 int reg1
= alloc_reg();
1551 int reg2
= alloc_reg();
1553 memset(s
, 0, sizeof(s
));
1554 fix2
= fix3
= fix4
= fix5
= 0;
1561 b0
= gen_protochain(v
, Q_IP
, dir
);
1562 b
= gen_protochain(v
, Q_IPV6
, dir
);
1566 bpf_error("bad protocol applied for 'protochain'");
1570 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1573 * s[0] is a dummy entry to protect other BPF insn from damaged
1574 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1575 * hard to find interdependency made by jump table fixup.
1578 s
[i
] = new_stmt(0); /*dummy*/
1583 b0
= gen_linktype(ETHERTYPE_IP
);
1586 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1587 s
[i
]->s
.k
= off_nl
+ 9;
1589 /* X = ip->ip_hl << 2 */
1590 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1596 b0
= gen_linktype(ETHERTYPE_IPV6
);
1598 /* A = ip6->ip_nxt */
1599 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1600 s
[i
]->s
.k
= off_nl
+ 6;
1602 /* X = sizeof(struct ip6_hdr) */
1603 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1609 bpf_error("unsupported proto to gen_protochain");
1613 /* again: if (A == v) goto end; else fall through; */
1615 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1617 s
[i
]->s
.jt
= NULL
; /*later*/
1618 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1622 #ifndef IPPROTO_NONE
1623 #define IPPROTO_NONE 59
1625 /* if (A == IPPROTO_NONE) goto end */
1626 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1627 s
[i
]->s
.jt
= NULL
; /*later*/
1628 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1629 s
[i
]->s
.k
= IPPROTO_NONE
;
1630 s
[fix5
]->s
.jf
= s
[i
];
1635 if (proto
== Q_IPV6
) {
1636 int v6start
, v6end
, v6advance
, j
;
1639 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
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_HOPOPTS
;
1644 s
[fix2
]->s
.jf
= s
[i
];
1646 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1647 s
[i
- 1]->s
.jf
= 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_DSTOPTS
;
1652 /* if (A == IPPROTO_ROUTING) goto v6advance */
1653 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1654 s
[i
]->s
.jt
= NULL
; /*later*/
1655 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1656 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1658 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1659 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1660 s
[i
]->s
.jt
= NULL
; /*later*/
1661 s
[i
]->s
.jf
= NULL
; /*later*/
1662 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
1673 * X = X + (P[X] + 1) * 8;
1676 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1679 s
[i
] = new_stmt(BPF_ST
);
1683 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1687 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1689 /* A = P[X + packet head]; */
1690 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1694 s
[i
] = new_stmt(BPF_ST
);
1698 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1701 /* A = P[X + packet head] */
1702 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1706 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1710 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1714 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1717 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1721 /* goto again; (must use BPF_JA for backward jump) */
1722 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1723 s
[i
]->s
.k
= again
- i
- 1;
1724 s
[i
- 1]->s
.jf
= s
[i
];
1728 for (j
= v6start
; j
<= v6end
; j
++)
1729 s
[j
]->s
.jt
= s
[v6advance
];
1734 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1736 s
[fix2
]->s
.jf
= s
[i
];
1742 /* if (A == IPPROTO_AH) then fall through; else goto end; */
1743 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1744 s
[i
]->s
.jt
= NULL
; /*later*/
1745 s
[i
]->s
.jf
= NULL
; /*later*/
1746 s
[i
]->s
.k
= IPPROTO_AH
;
1748 s
[fix3
]->s
.jf
= s
[ahcheck
];
1755 * X = X + (P[X] + 2) * 4;
1758 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1761 s
[i
] = new_stmt(BPF_ST
);
1765 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1769 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1771 /* A = P[X + packet head]; */
1772 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1776 s
[i
] = new_stmt(BPF_ST
);
1780 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1783 /* A = P[X + packet head] */
1784 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1788 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1792 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1796 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1799 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1803 /* goto again; (must use BPF_JA for backward jump) */
1804 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1805 s
[i
]->s
.k
= again
- i
- 1;
1810 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1812 s
[fix2
]->s
.jt
= s
[end
];
1813 s
[fix4
]->s
.jf
= s
[end
];
1814 s
[fix5
]->s
.jt
= s
[end
];
1821 for (i
= 0; i
< max
- 1; i
++)
1822 s
[i
]->next
= s
[i
+ 1];
1823 s
[max
- 1]->next
= NULL
;
1828 b
= new_block(JMP(BPF_JEQ
));
1829 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
1840 static struct block
*
1841 gen_proto(v
, proto
, dir
)
1846 struct block
*b0
, *b1
;
1848 if (dir
!= Q_DEFAULT
)
1849 bpf_error("direction applied to 'proto'");
1854 b0
= gen_proto(v
, Q_IP
, dir
);
1855 b1
= gen_proto(v
, Q_IPV6
, dir
);
1862 b0
= gen_linktype(ETHERTYPE_IP
);
1864 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
1866 b1
= gen_protochain(v
, Q_IP
);
1872 bpf_error("arp does not encapsulate another protocol");
1876 bpf_error("rarp does not encapsulate another protocol");
1880 bpf_error("atalk encapsulation is not specifiable");
1884 bpf_error("decnet encapsulation is not specifiable");
1888 bpf_error("sca does not encapsulate another protocol");
1892 bpf_error("lat does not encapsulate another protocol");
1896 bpf_error("moprc does not encapsulate another protocol");
1900 bpf_error("mopdl does not encapsulate another protocol");
1904 return gen_linktype(v
);
1907 bpf_error("'udp proto' is bogus");
1911 bpf_error("'tcp proto' is bogus");
1915 bpf_error("'icmp proto' is bogus");
1919 bpf_error("'igmp proto' is bogus");
1923 bpf_error("'igrp proto' is bogus");
1927 bpf_error("'pim proto' is bogus");
1932 b0
= gen_linktype(ETHERTYPE_IPV6
);
1934 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
1936 b1
= gen_protochain(v
, Q_IPV6
);
1942 bpf_error("'icmp6 proto' is bogus");
1946 bpf_error("'ah proto' is bogus");
1949 bpf_error("'ah proto' is bogus");
1960 register const char *name
;
1963 int proto
= q
.proto
;
1967 bpf_u_int32 mask
, addr
;
1969 bpf_u_int32
**alist
;
1972 struct sockaddr_in
*sin
;
1973 struct sockaddr_in6
*sin6
;
1974 struct addrinfo
*res
, *res0
;
1975 struct in6_addr mask128
;
1977 struct block
*b
, *tmp
;
1978 int port
, real_proto
;
1983 addr
= pcap_nametonetaddr(name
);
1985 bpf_error("unknown network '%s'", name
);
1986 /* Left justify network addr and calculate its network mask */
1988 while (addr
&& (addr
& 0xff000000) == 0) {
1992 return gen_host(addr
, mask
, proto
, dir
);
1996 if (proto
== Q_LINK
) {
2000 eaddr
= pcap_ether_hostton(name
);
2003 "unknown ether host '%s'", name
);
2004 return gen_ehostop(eaddr
, dir
);
2007 eaddr
= pcap_ether_hostton(name
);
2010 "unknown FDDI host '%s'", name
);
2011 return gen_fhostop(eaddr
, dir
);
2015 "only ethernet/FDDI supports link-level host name");
2018 } else if (proto
== Q_DECNET
) {
2019 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2021 * I don't think DECNET hosts can be multihomed, so
2022 * there is no need to build up a list of addresses
2024 return (gen_host(dn_addr
, 0, proto
, dir
));
2027 alist
= pcap_nametoaddr(name
);
2028 if (alist
== NULL
|| *alist
== NULL
)
2029 bpf_error("unknown host '%s'", name
);
2031 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2033 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2035 tmp
= gen_host(**alist
++, 0xffffffff,
2042 memset(&mask128
, 0xff, sizeof(mask128
));
2043 res0
= res
= pcap_nametoaddrinfo(name
);
2045 bpf_error("unknown host '%s'", name
);
2047 tproto
= tproto6
= proto
;
2048 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2052 for (res
= res0
; res
; res
= res
->ai_next
) {
2053 switch (res
->ai_family
) {
2055 if (tproto
== Q_IPV6
)
2058 sin
= (struct sockaddr_in
*)
2060 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2061 0xffffffff, tproto
, dir
);
2064 if (tproto6
== Q_IP
)
2067 sin6
= (struct sockaddr_in6
*)
2069 tmp
= gen_host6(&sin6
->sin6_addr
,
2070 &mask128
, tproto6
, dir
);
2079 bpf_error("unknown host '%s'%s", name
,
2080 (proto
== Q_DEFAULT
)
2082 : " for specified address family");
2089 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2090 bpf_error("illegal qualifier of 'port'");
2091 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2092 bpf_error("unknown port '%s'", name
);
2093 if (proto
== Q_UDP
) {
2094 if (real_proto
== IPPROTO_TCP
)
2095 bpf_error("port '%s' is tcp", name
);
2097 /* override PROTO_UNDEF */
2098 real_proto
= IPPROTO_UDP
;
2100 if (proto
== Q_TCP
) {
2101 if (real_proto
== IPPROTO_UDP
)
2102 bpf_error("port '%s' is udp", name
);
2104 /* override PROTO_UNDEF */
2105 real_proto
= IPPROTO_TCP
;
2108 return gen_port(port
, real_proto
, dir
);
2112 b
= gen_port(port
, real_proto
, dir
);
2113 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2120 eaddr
= pcap_ether_hostton(name
);
2122 bpf_error("unknown ether host: %s", name
);
2124 alist
= pcap_nametoaddr(name
);
2125 if (alist
== NULL
|| *alist
== NULL
)
2126 bpf_error("unknown host '%s'", name
);
2127 return gen_gateway(eaddr
, alist
, proto
, dir
);
2129 bpf_error("'gateway' not supported in this configuration");
2133 real_proto
= lookup_proto(name
, proto
);
2134 if (real_proto
>= 0)
2135 return gen_proto(real_proto
, proto
, dir
);
2137 bpf_error("unknown protocol: %s", name
);
2140 real_proto
= lookup_proto(name
, proto
);
2141 if (real_proto
>= 0)
2142 return gen_protochain(real_proto
, proto
, dir
);
2144 bpf_error("unknown protocol: %s", name
);
2156 gen_mcode(s1
, s2
, masklen
, q
)
2157 register const char *s1
, *s2
;
2158 register int masklen
;
2161 register int nlen
, mlen
;
2164 nlen
= __pcap_atoin(s1
, &n
);
2165 /* Promote short ipaddr */
2169 mlen
= __pcap_atoin(s2
, &m
);
2170 /* Promote short ipaddr */
2173 bpf_error("non-network bits set in \"%s mask %s\"",
2176 /* Convert mask len to mask */
2178 bpf_error("mask length must be <= 32");
2179 m
= 0xffffffff << (32 - masklen
);
2181 bpf_error("non-network bits set in \"%s/%d\"",
2188 return gen_host(n
, m
, q
.proto
, q
.dir
);
2191 bpf_error("Mask syntax for networks only");
2198 register const char *s
;
2203 int proto
= q
.proto
;
2209 else if (q
.proto
== Q_DECNET
)
2210 vlen
= __pcap_atodn(s
, &v
);
2212 vlen
= __pcap_atoin(s
, &v
);
2219 if (proto
== Q_DECNET
)
2220 return gen_host(v
, 0, proto
, dir
);
2221 else if (proto
== Q_LINK
) {
2222 bpf_error("illegal link layer address");
2225 if (s
== NULL
&& q
.addr
== Q_NET
) {
2226 /* Promote short net number */
2227 while (v
&& (v
& 0xff000000) == 0) {
2232 /* Promote short ipaddr */
2236 return gen_host(v
, mask
, proto
, dir
);
2241 proto
= IPPROTO_UDP
;
2242 else if (proto
== Q_TCP
)
2243 proto
= IPPROTO_TCP
;
2244 else if (proto
== Q_DEFAULT
)
2245 proto
= PROTO_UNDEF
;
2247 bpf_error("illegal qualifier of 'port'");
2250 return gen_port((int)v
, proto
, dir
);
2254 b
= gen_port((int)v
, proto
, dir
);
2255 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2261 bpf_error("'gateway' requires a name");
2265 return gen_proto((int)v
, proto
, dir
);
2268 return gen_protochain((int)v
, proto
, dir
);
2283 gen_mcode6(s1
, s2
, masklen
, q
)
2284 register const char *s1
, *s2
;
2285 register int masklen
;
2288 struct addrinfo
*res
;
2289 struct in6_addr
*addr
;
2290 struct in6_addr mask
;
2295 bpf_error("no mask %s supported", s2
);
2297 res
= pcap_nametoaddrinfo(s1
);
2299 bpf_error("invalid ip6 address %s", s1
);
2301 bpf_error("%s resolved to multiple address", s1
);
2302 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2304 if (sizeof(mask
) * 8 < masklen
)
2305 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2306 memset(&mask
, 0xff, masklen
/ 8);
2308 mask
.s6_addr
[masklen
/ 8] =
2309 (0xff << (8 - masklen
% 8)) & 0xff;
2312 a
= (u_int32_t
*)addr
;
2313 m
= (u_int32_t
*)&mask
;
2314 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2315 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2316 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2324 bpf_error("Mask syntax for networks only");
2328 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2333 bpf_error("invalid qualifier against IPv6 address");
2341 register const u_char
*eaddr
;
2344 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2345 if (linktype
== DLT_EN10MB
)
2346 return gen_ehostop(eaddr
, (int)q
.dir
);
2347 if (linktype
== DLT_FDDI
)
2348 return gen_fhostop(eaddr
, (int)q
.dir
);
2350 bpf_error("ethernet address used in non-ether expression");
2356 struct slist
*s0
, *s1
;
2359 * This is definitely not the best way to do this, but the
2360 * lists will rarely get long.
2367 static struct slist
*
2373 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2378 static struct slist
*
2384 s
= new_stmt(BPF_LD
|BPF_MEM
);
2390 gen_load(proto
, index
, size
)
2395 struct slist
*s
, *tmp
;
2397 int regno
= alloc_reg();
2399 free_reg(index
->regno
);
2403 bpf_error("data size must be 1, 2, or 4");
2419 bpf_error("unsupported index operation");
2422 s
= xfer_to_x(index
);
2423 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2425 sappend(index
->s
, s
);
2440 /* XXX Note that we assume a fixed link header here. */
2441 s
= xfer_to_x(index
);
2442 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2445 sappend(index
->s
, s
);
2447 b
= gen_proto_abbrev(proto
);
2449 gen_and(index
->b
, b
);
2459 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2461 sappend(s
, xfer_to_a(index
));
2462 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2463 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2464 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2466 sappend(index
->s
, s
);
2468 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2470 gen_and(index
->b
, b
);
2472 gen_and(gen_proto_abbrev(Q_IP
), b
);
2478 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2482 index
->regno
= regno
;
2483 s
= new_stmt(BPF_ST
);
2485 sappend(index
->s
, s
);
2491 gen_relation(code
, a0
, a1
, reversed
)
2493 struct arth
*a0
, *a1
;
2496 struct slist
*s0
, *s1
, *s2
;
2497 struct block
*b
, *tmp
;
2501 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2502 b
= new_block(JMP(code
));
2503 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2504 reversed
= !reversed
;
2505 b
->s
.k
= 0x80000000;
2513 sappend(a0
->s
, a1
->s
);
2517 free_reg(a0
->regno
);
2518 free_reg(a1
->regno
);
2520 /* 'and' together protocol checks */
2523 gen_and(a0
->b
, tmp
= a1
->b
);
2539 int regno
= alloc_reg();
2540 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2543 s
= new_stmt(BPF_LD
|BPF_LEN
);
2544 s
->next
= new_stmt(BPF_ST
);
2545 s
->next
->s
.k
= regno
;
2560 a
= (struct arth
*)newchunk(sizeof(*a
));
2564 s
= new_stmt(BPF_LD
|BPF_IMM
);
2566 s
->next
= new_stmt(BPF_ST
);
2582 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2585 s
= new_stmt(BPF_ST
);
2593 gen_arth(code
, a0
, a1
)
2595 struct arth
*a0
, *a1
;
2597 struct slist
*s0
, *s1
, *s2
;
2601 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2606 sappend(a0
->s
, a1
->s
);
2608 free_reg(a1
->regno
);
2610 s0
= new_stmt(BPF_ST
);
2611 a0
->regno
= s0
->s
.k
= alloc_reg();
2618 * Here we handle simple allocation of the scratch registers.
2619 * If too many registers are alloc'd, the allocator punts.
2621 static int regused
[BPF_MEMWORDS
];
2625 * Return the next free register.
2630 int n
= BPF_MEMWORDS
;
2633 if (regused
[curreg
])
2634 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2636 regused
[curreg
] = 1;
2640 bpf_error("too many registers needed to evaluate expression");
2645 * Return a register to the table so it can
2655 static struct block
*
2662 s
= new_stmt(BPF_LD
|BPF_LEN
);
2663 b
= new_block(JMP(jmp
));
2674 return gen_len(BPF_JGE
, n
);
2683 b
= gen_len(BPF_JGT
, n
);
2690 gen_byteop(op
, idx
, val
)
2701 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2704 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2705 b
->s
.code
= JMP(BPF_JGE
);
2710 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2711 b
->s
.code
= JMP(BPF_JGT
);
2715 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
2719 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2723 b
= new_block(JMP(BPF_JEQ
));
2731 gen_broadcast(proto
)
2734 bpf_u_int32 hostmask
;
2735 struct block
*b0
, *b1
, *b2
;
2736 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2742 if (linktype
== DLT_EN10MB
)
2743 return gen_ehostop(ebroadcast
, Q_DST
);
2744 if (linktype
== DLT_FDDI
)
2745 return gen_fhostop(ebroadcast
, Q_DST
);
2746 bpf_error("not a broadcast link");
2750 b0
= gen_linktype(ETHERTYPE_IP
);
2751 hostmask
= ~netmask
;
2752 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
2753 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
2754 (bpf_int32
)(~0 & hostmask
), hostmask
);
2759 bpf_error("only ether/ip broadcast filters supported");
2763 gen_multicast(proto
)
2766 register struct block
*b0
, *b1
;
2767 register struct slist
*s
;
2773 if (linktype
== DLT_EN10MB
) {
2774 /* ether[0] & 1 != 0 */
2775 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2777 b0
= new_block(JMP(BPF_JSET
));
2783 if (linktype
== DLT_FDDI
) {
2784 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
2785 /* fddi[1] & 1 != 0 */
2786 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2788 b0
= new_block(JMP(BPF_JSET
));
2793 /* Link not known to support multicasts */
2797 b0
= gen_linktype(ETHERTYPE_IP
);
2798 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
2799 b1
->s
.code
= JMP(BPF_JGE
);
2805 b0
= gen_linktype(ETHERTYPE_IPV6
);
2806 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
2811 bpf_error("only IP multicast filters supported on ethernet/FDDI");
2815 * generate command for inbound/outbound. It's here so we can
2816 * make it link-type specific. 'dir' = 0 implies "inbound",
2817 * = 1 implies "outbound".
2823 register struct block
*b0
;
2825 b0
= gen_relation(BPF_JEQ
,
2826 gen_load(Q_LINK
, gen_loadi(0), 1),