]>
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.109 2000-04-27 11:16:54 itojun Exp $ (LBL)";
27 #include <sys/types.h>
28 #include <sys/socket.h>
31 #include <sys/param.h>
41 #include <netinet/in.h>
42 #include <netinet/if_ether.h>
55 #include "ethertype.h"
58 #include <pcap-namedb.h>
61 #include <sys/socket.h>
65 #ifdef HAVE_OS_PROTO_H
69 #define JMP(c) ((c)|BPF_JMP|BPF_K)
72 static jmp_buf top_ctx
;
73 static pcap_t
*bpf_pcap
;
77 int pcap_fddipad
= PCAP_FDDIPAD
;
85 bpf_error(const char *fmt
, ...)
87 bpf_error(fmt
, va_alist
)
100 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
107 static void init_linktype(int);
109 static int alloc_reg(void);
110 static void free_reg(int);
112 static struct block
*root
;
115 * We divy out chunks of memory rather than call malloc each time so
116 * we don't have to worry about leaking memory. It's probably
117 * not a big deal if all this memory was wasted but it this ever
118 * goes into a library that would probably not be a good idea.
121 #define CHUNK0SIZE 1024
127 static struct chunk chunks
[NCHUNKS
];
128 static int cur_chunk
;
130 static void *newchunk(u_int
);
131 static void freechunks(void);
132 static inline struct block
*new_block(int);
133 static inline struct slist
*new_stmt(int);
134 static struct block
*gen_retblk(int);
135 static inline void syntax(void);
137 static void backpatch(struct block
*, struct block
*);
138 static void merge(struct block
*, struct block
*);
139 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
140 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
141 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
142 static struct block
*gen_uncond(int);
143 static inline struct block
*gen_true(void);
144 static inline struct block
*gen_false(void);
145 static struct block
*gen_linktype(int);
146 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
148 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
150 static struct block
*gen_ehostop(const u_char
*, int);
151 static struct block
*gen_fhostop(const u_char
*, int);
152 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
153 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
155 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
157 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
158 static struct block
*gen_ipfrag(void);
159 static struct block
*gen_portatom(int, bpf_int32
);
161 static struct block
*gen_portatom6(int, bpf_int32
);
163 struct block
*gen_portop(int, int, int);
164 static struct block
*gen_port(int, int, int);
166 struct block
*gen_portop6(int, int, int);
167 static struct block
*gen_port6(int, int, int);
169 static int lookup_proto(const char *, int);
170 static struct block
*gen_proto(int, int, int);
171 static struct slist
*xfer_to_x(struct arth
*);
172 static struct slist
*xfer_to_a(struct arth
*);
173 static struct block
*gen_len(int, int);
183 /* XXX Round up to nearest long. */
184 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
186 /* XXX Round up to structure boundary. */
190 cp
= &chunks
[cur_chunk
];
191 if (n
> cp
->n_left
) {
192 ++cp
, k
= ++cur_chunk
;
194 bpf_error("out of memory");
195 size
= CHUNK0SIZE
<< k
;
196 cp
->m
= (void *)malloc(size
);
197 memset((char *)cp
->m
, 0, size
);
200 bpf_error("out of memory");
203 return (void *)((char *)cp
->m
+ cp
->n_left
);
212 for (i
= 0; i
< NCHUNKS
; ++i
)
213 if (chunks
[i
].m
!= NULL
) {
220 * A strdup whose allocations are freed after code generation is over.
224 register const char *s
;
226 int n
= strlen(s
) + 1;
227 char *cp
= newchunk(n
);
233 static inline struct block
*
239 p
= (struct block
*)newchunk(sizeof(*p
));
246 static inline struct slist
*
252 p
= (struct slist
*)newchunk(sizeof(*p
));
258 static struct block
*
262 struct block
*b
= new_block(BPF_RET
|BPF_K
);
271 bpf_error("syntax error in filter expression");
274 static bpf_u_int32 netmask
;
279 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
280 char *buf
, int optimize
, bpf_u_int32 mask
)
289 if (setjmp(top_ctx
)) {
295 snaplen
= pcap_snapshot(p
);
297 lex_init(buf
? buf
: "");
298 init_linktype(pcap_datalink(p
));
305 root
= gen_retblk(snaplen
);
307 if (optimize
&& !no_optimize
) {
310 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
311 bpf_error("expression rejects all packets");
313 program
->bf_insns
= icode_to_fcode(root
, &len
);
314 program
->bf_len
= len
;
321 * entry point for using the compiler with no pcap open
322 * pass in all the stuff that is needed explicitly instead.
325 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
326 struct bpf_program
*program
,
327 char *buf
, int optimize
, bpf_u_int32 mask
)
335 if (setjmp(top_ctx
)) {
342 /* XXX needed? I don't grok the use of globals here. */
343 snaplen
= snaplen_arg
;
345 lex_init(buf
? buf
: "");
346 init_linktype(linktype_arg
);
353 root
= gen_retblk(snaplen_arg
);
358 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
359 bpf_error("expression rejects all packets");
361 program
->bf_insns
= icode_to_fcode(root
, &len
);
362 program
->bf_len
= len
;
369 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
370 * which of the jt and jf fields has been resolved and which is a pointer
371 * back to another unresolved block (or nil). At least one of the fields
372 * in each block is already resolved.
375 backpatch(list
, target
)
376 struct block
*list
, *target
;
393 * Merge the lists in b0 and b1, using the 'sense' field to indicate
394 * which of jt and jf is the link.
398 struct block
*b0
, *b1
;
400 register struct block
**p
= &b0
;
402 /* Find end of list. */
404 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
406 /* Concatenate the lists. */
414 backpatch(p
, gen_retblk(snaplen
));
415 p
->sense
= !p
->sense
;
416 backpatch(p
, gen_retblk(0));
422 struct block
*b0
, *b1
;
424 backpatch(b0
, b1
->head
);
425 b0
->sense
= !b0
->sense
;
426 b1
->sense
= !b1
->sense
;
428 b1
->sense
= !b1
->sense
;
434 struct block
*b0
, *b1
;
436 b0
->sense
= !b0
->sense
;
437 backpatch(b0
, b1
->head
);
438 b0
->sense
= !b0
->sense
;
447 b
->sense
= !b
->sense
;
450 static struct block
*
451 gen_cmp(offset
, size
, v
)
458 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
461 b
= new_block(JMP(BPF_JEQ
));
468 static struct block
*
469 gen_mcmp(offset
, size
, v
, mask
)
474 struct block
*b
= gen_cmp(offset
, size
, v
);
477 if (mask
!= 0xffffffff) {
478 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
485 static struct block
*
486 gen_bcmp(offset
, size
, v
)
487 register u_int offset
, size
;
488 register const u_char
*v
;
490 register struct block
*b
, *tmp
;
494 register const u_char
*p
= &v
[size
- 4];
495 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
496 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
498 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
505 register const u_char
*p
= &v
[size
- 2];
506 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
508 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
515 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
524 * Various code constructs need to know the layout of the data link
525 * layer. These variables give the necessary offsets. off_linktype
526 * is set to -1 for no encapsulation, in which case, IP is assumed.
528 static u_int off_linktype
;
547 * SLIP doesn't have a link level type. The 16 byte
548 * header is hacked into our SLIP driver.
555 /* XXX this may be the same as the DLT_PPP_BSDOS case */
579 * FDDI doesn't really have a link-level type field.
580 * We assume that SSAP = SNAP is being used and pick
581 * out the encapsulated Ethernet type.
585 off_linktype
+= pcap_fddipad
;
589 off_nl
+= pcap_fddipad
;
598 case DLT_ATM_RFC1483
:
600 * assume routed, non-ISO PDUs
601 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
612 bpf_error("unknown data link type 0x%x", linktype
);
616 static struct block
*
623 s
= new_stmt(BPF_LD
|BPF_IMM
);
625 b
= new_block(JMP(BPF_JEQ
));
631 static inline struct block
*
634 return gen_uncond(1);
637 static inline struct block
*
640 return gen_uncond(0);
643 static struct block
*
647 struct block
*b0
, *b1
;
649 /* If we're not using encapsulation and checking for IP, we're done */
650 if (off_linktype
== -1 && proto
== ETHERTYPE_IP
)
659 if (proto
== ETHERTYPE_IP
)
660 proto
= PPP_IP
; /* XXX was 0x21 */
662 else if (proto
== ETHERTYPE_IPV6
)
671 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
672 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
674 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
689 case ETHERTYPE_ATALK
:
701 if (proto
== ETHERTYPE_IP
)
702 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET
)));
704 else if (proto
== ETHERTYPE_IPV6
)
705 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET6
)));
710 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
713 static struct block
*
714 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
718 u_int src_off
, dst_off
;
720 struct block
*b0
, *b1
;
734 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
735 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
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
);
749 b0
= gen_linktype(proto
);
750 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
756 static struct block
*
757 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
758 struct in6_addr
*addr
;
759 struct in6_addr
*mask
;
761 u_int src_off
, dst_off
;
763 struct block
*b0
, *b1
;
778 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
779 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
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
);
793 /* this order is important */
794 a
= (u_int32_t
*)addr
;
795 m
= (u_int32_t
*)mask
;
796 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
797 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
799 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
801 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
803 b0
= gen_linktype(proto
);
809 static struct block
*
810 gen_ehostop(eaddr
, dir
)
811 register const u_char
*eaddr
;
814 register struct block
*b0
, *b1
;
818 return gen_bcmp(6, 6, eaddr
);
821 return gen_bcmp(0, 6, eaddr
);
824 b0
= gen_ehostop(eaddr
, Q_SRC
);
825 b1
= gen_ehostop(eaddr
, Q_DST
);
831 b0
= gen_ehostop(eaddr
, Q_SRC
);
832 b1
= gen_ehostop(eaddr
, Q_DST
);
841 * Like gen_ehostop, but for DLT_FDDI
843 static struct block
*
844 gen_fhostop(eaddr
, dir
)
845 register const u_char
*eaddr
;
848 struct block
*b0
, *b1
;
853 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
855 return gen_bcmp(6 + 1, 6, eaddr
);
860 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
862 return gen_bcmp(0 + 1, 6, eaddr
);
866 b0
= gen_fhostop(eaddr
, Q_SRC
);
867 b1
= gen_fhostop(eaddr
, Q_DST
);
873 b0
= gen_fhostop(eaddr
, Q_SRC
);
874 b1
= gen_fhostop(eaddr
, Q_DST
);
883 * This is quite tricky because there may be pad bytes in front of the
884 * DECNET header, and then there are two possible data packet formats that
885 * carry both src and dst addresses, plus 5 packet types in a format that
886 * carries only the src node, plus 2 types that use a different format and
887 * also carry just the src node.
891 * Instead of doing those all right, we just look for data packets with
892 * 0 or 1 bytes of padding. If you want to look at other packets, that
893 * will require a lot more hacking.
895 * To add support for filtering on DECNET "areas" (network numbers)
896 * one would want to add a "mask" argument to this routine. That would
897 * make the filter even more inefficient, although one could be clever
898 * and not generate masking instructions if the mask is 0xFFFF.
900 static struct block
*
901 gen_dnhostop(addr
, dir
, base_off
)
906 struct block
*b0
, *b1
, *b2
, *tmp
;
907 u_int offset_lh
; /* offset if long header is received */
908 u_int offset_sh
; /* offset if short header is received */
913 offset_sh
= 1; /* follows flags */
914 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
918 offset_sh
= 3; /* follows flags, dstnode */
919 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
923 /* Inefficient because we do our Calvinball dance twice */
924 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
925 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
931 /* Inefficient because we do our Calvinball dance twice */
932 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
933 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
940 b0
= gen_linktype(ETHERTYPE_DN
);
941 /* Check for pad = 1, long header case */
942 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
943 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
944 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
945 BPF_H
, (bpf_int32
)ntohs(addr
));
947 /* Check for pad = 0, long header case */
948 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
949 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
952 /* Check for pad = 1, short header case */
953 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
954 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
955 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
956 BPF_H
, (bpf_int32
)ntohs(addr
));
959 /* Check for pad = 0, short header case */
960 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
961 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
965 /* Combine with test for linktype */
970 static struct block
*
971 gen_host(addr
, mask
, proto
, dir
)
977 struct block
*b0
, *b1
;
982 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
983 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
985 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
990 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
991 off_nl
+ 12, off_nl
+ 16);
994 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
995 off_nl
+ 14, off_nl
+ 24);
998 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
999 off_nl
+ 14, off_nl
+ 24);
1002 bpf_error("'tcp' modifier applied to host");
1005 bpf_error("'udp' modifier applied to host");
1008 bpf_error("'icmp' modifier applied to host");
1011 bpf_error("'igmp' modifier applied to host");
1014 bpf_error("'igrp' modifier applied to host");
1017 bpf_error("'pim' modifier applied to host");
1020 bpf_error("ATALK host filtering not implemented");
1023 return gen_dnhostop(addr
, dir
, off_nl
);
1026 bpf_error("SCA host filtering not implemented");
1029 bpf_error("LAT host filtering not implemented");
1032 bpf_error("MOPDL host filtering not implemented");
1035 bpf_error("MOPRC host filtering not implemented");
1039 bpf_error("'ip6' modifier applied to ip host");
1042 bpf_error("'icmp6' modifier applied to host");
1046 bpf_error("'ah' modifier applied to host");
1049 bpf_error("'esp' modifier applied to host");
1058 static struct block
*
1059 gen_host6(addr
, mask
, proto
, dir
)
1060 struct in6_addr
*addr
;
1061 struct in6_addr
*mask
;
1065 struct block
*b0
, *b1
;
1070 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1073 bpf_error("'ip' modifier applied to ip6 host");
1076 bpf_error("'rarp' modifier applied to ip6 host");
1079 bpf_error("'arp' modifier applied to ip6 host");
1082 bpf_error("'tcp' modifier applied to host");
1085 bpf_error("'udp' modifier applied to host");
1088 bpf_error("'icmp' modifier applied to host");
1091 bpf_error("'igmp' modifier applied to host");
1094 bpf_error("'igrp' modifier applied to host");
1097 bpf_error("'pim' modifier applied to host");
1100 bpf_error("ATALK host filtering not implemented");
1103 bpf_error("'decnet' modifier applied to ip6 host");
1106 bpf_error("SCA host filtering not implemented");
1109 bpf_error("LAT host filtering not implemented");
1112 bpf_error("MOPDL host filtering not implemented");
1115 bpf_error("MOPRC host filtering not implemented");
1118 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1119 off_nl
+ 8, off_nl
+ 24);
1122 bpf_error("'icmp6' modifier applied to host");
1125 bpf_error("'ah' modifier applied to host");
1128 bpf_error("'esp' modifier applied to host");
1137 static struct block
*
1138 gen_gateway(eaddr
, alist
, proto
, dir
)
1139 const u_char
*eaddr
;
1140 bpf_u_int32
**alist
;
1144 struct block
*b0
, *b1
, *tmp
;
1147 bpf_error("direction applied to 'gateway'");
1154 if (linktype
== DLT_EN10MB
)
1155 b0
= gen_ehostop(eaddr
, Q_OR
);
1156 else if (linktype
== DLT_FDDI
)
1157 b0
= gen_fhostop(eaddr
, Q_OR
);
1160 "'gateway' supported only on ethernet or FDDI");
1162 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1164 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1172 bpf_error("illegal modifier of 'gateway'");
1177 gen_proto_abbrev(proto
)
1180 struct block
*b0
, *b1
;
1185 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1187 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1193 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1195 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1201 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1204 #ifndef IPPROTO_IGMP
1205 #define IPPROTO_IGMP 2
1209 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1212 #ifndef IPPROTO_IGRP
1213 #define IPPROTO_IGRP 9
1216 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1220 #define IPPROTO_PIM 103
1224 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1226 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1232 b1
= gen_linktype(ETHERTYPE_IP
);
1236 b1
= gen_linktype(ETHERTYPE_ARP
);
1240 b1
= gen_linktype(ETHERTYPE_REVARP
);
1244 bpf_error("link layer applied in wrong context");
1247 b1
= gen_linktype(ETHERTYPE_ATALK
);
1251 b1
= gen_linktype(ETHERTYPE_DN
);
1255 b1
= gen_linktype(ETHERTYPE_SCA
);
1259 b1
= gen_linktype(ETHERTYPE_LAT
);
1263 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1267 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1272 b1
= gen_linktype(ETHERTYPE_IPV6
);
1275 #ifndef IPPROTO_ICMPV6
1276 #define IPPROTO_ICMPV6 58
1279 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1284 #define IPPROTO_AH 51
1287 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1289 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1295 #define IPPROTO_ESP 50
1298 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1300 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1311 static struct block
*
1318 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1319 s
->s
.k
= off_nl
+ 6;
1320 b
= new_block(JMP(BPF_JSET
));
1328 static struct block
*
1329 gen_portatom(off
, v
)
1336 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1339 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1340 s
->next
->s
.k
= off_nl
+ off
;
1342 b
= new_block(JMP(BPF_JEQ
));
1350 static struct block
*
1351 gen_portatom6(off
, v
)
1355 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1360 gen_portop(port
, proto
, dir
)
1361 int port
, proto
, dir
;
1363 struct block
*b0
, *b1
, *tmp
;
1365 /* ip proto 'proto' */
1366 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1372 b1
= gen_portatom(0, (bpf_int32
)port
);
1376 b1
= gen_portatom(2, (bpf_int32
)port
);
1381 tmp
= gen_portatom(0, (bpf_int32
)port
);
1382 b1
= gen_portatom(2, (bpf_int32
)port
);
1387 tmp
= gen_portatom(0, (bpf_int32
)port
);
1388 b1
= gen_portatom(2, (bpf_int32
)port
);
1400 static struct block
*
1401 gen_port(port
, ip_proto
, dir
)
1406 struct block
*b0
, *b1
, *tmp
;
1408 /* ether proto ip */
1409 b0
= gen_linktype(ETHERTYPE_IP
);
1414 b1
= gen_portop(port
, ip_proto
, dir
);
1418 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1419 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1432 gen_portop6(port
, proto
, dir
)
1433 int port
, proto
, dir
;
1435 struct block
*b0
, *b1
, *tmp
;
1437 /* ip proto 'proto' */
1438 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1442 b1
= gen_portatom6(0, (bpf_int32
)port
);
1446 b1
= gen_portatom6(2, (bpf_int32
)port
);
1451 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1452 b1
= gen_portatom6(2, (bpf_int32
)port
);
1457 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1458 b1
= gen_portatom6(2, (bpf_int32
)port
);
1470 static struct block
*
1471 gen_port6(port
, ip_proto
, dir
)
1476 struct block
*b0
, *b1
, *tmp
;
1478 /* ether proto ip */
1479 b0
= gen_linktype(ETHERTYPE_IPV6
);
1484 b1
= gen_portop6(port
, ip_proto
, dir
);
1488 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1489 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1502 lookup_proto(name
, proto
)
1503 register const char *name
;
1512 v
= pcap_nametoproto(name
);
1513 if (v
== PROTO_UNDEF
)
1514 bpf_error("unknown ip proto '%s'", name
);
1518 /* XXX should look up h/w protocol type based on linktype */
1519 v
= pcap_nametoeproto(name
);
1520 if (v
== PROTO_UNDEF
)
1521 bpf_error("unknown ether proto '%s'", name
);
1539 gen_protochain(v
, proto
, dir
)
1544 #ifdef NO_PROTOCHAIN
1545 return gen_proto(v
, proto
, dir
);
1547 struct block
*b0
, *b
;
1548 struct slist
*s
[100], *sp
;
1549 int fix2
, fix3
, fix4
, fix5
;
1550 int ahcheck
, again
, end
;
1552 int reg1
= alloc_reg();
1553 int reg2
= alloc_reg();
1555 memset(s
, 0, sizeof(s
));
1556 fix2
= fix3
= fix4
= fix5
= 0;
1563 b0
= gen_protochain(v
, Q_IP
, dir
);
1564 b
= gen_protochain(v
, Q_IPV6
, dir
);
1568 bpf_error("bad protocol applied for 'protochain'");
1572 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1575 * s[0] is a dummy entry to protect other BPF insn from damaged
1576 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1577 * hard to find interdependency made by jump table fixup.
1580 s
[i
] = new_stmt(0); /*dummy*/
1585 b0
= gen_linktype(ETHERTYPE_IP
);
1588 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1589 s
[i
]->s
.k
= off_nl
+ 9;
1591 /* X = ip->ip_hl << 2 */
1592 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1598 b0
= gen_linktype(ETHERTYPE_IPV6
);
1600 /* A = ip6->ip_nxt */
1601 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1602 s
[i
]->s
.k
= off_nl
+ 6;
1604 /* X = sizeof(struct ip6_hdr) */
1605 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1611 bpf_error("unsupported proto to gen_protochain");
1615 /* again: if (A == v) goto end; else fall through; */
1617 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1619 s
[i
]->s
.jt
= NULL
; /*later*/
1620 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1624 #ifndef IPPROTO_NONE
1625 #define IPPROTO_NONE 59
1627 /* if (A == IPPROTO_NONE) goto end */
1628 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1629 s
[i
]->s
.jt
= NULL
; /*later*/
1630 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1631 s
[i
]->s
.k
= IPPROTO_NONE
;
1632 s
[fix5
]->s
.jf
= s
[i
];
1637 if (proto
== Q_IPV6
) {
1638 int v6start
, v6end
, v6advance
, j
;
1641 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1642 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1643 s
[i
]->s
.jt
= NULL
; /*later*/
1644 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1645 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
1646 s
[fix2
]->s
.jf
= s
[i
];
1648 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1649 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1650 s
[i
]->s
.jt
= NULL
; /*later*/
1651 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1652 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
1654 /* if (A == IPPROTO_ROUTING) goto v6advance */
1655 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1656 s
[i
]->s
.jt
= NULL
; /*later*/
1657 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1658 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1660 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
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
; /*later*/
1664 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
1675 * X = X + (P[X] + 1) * 8;
1678 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1681 s
[i
] = new_stmt(BPF_ST
);
1685 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1689 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1691 /* A = P[X + packet head]; */
1692 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1696 s
[i
] = new_stmt(BPF_ST
);
1700 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1703 /* A = P[X + packet head] */
1704 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1708 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1712 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1716 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1719 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1723 /* goto again; (must use BPF_JA for backward jump) */
1724 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1725 s
[i
]->s
.k
= again
- i
- 1;
1726 s
[i
- 1]->s
.jf
= s
[i
];
1730 for (j
= v6start
; j
<= v6end
; j
++)
1731 s
[j
]->s
.jt
= s
[v6advance
];
1736 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1738 s
[fix2
]->s
.jf
= s
[i
];
1744 /* if (A == IPPROTO_AH) then fall through; else goto end; */
1745 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1746 s
[i
]->s
.jt
= NULL
; /*later*/
1747 s
[i
]->s
.jf
= NULL
; /*later*/
1748 s
[i
]->s
.k
= IPPROTO_AH
;
1750 s
[fix3
]->s
.jf
= s
[ahcheck
];
1757 * X = X + (P[X] + 2) * 4;
1760 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1763 s
[i
] = new_stmt(BPF_ST
);
1767 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1771 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1773 /* A = P[X + packet head]; */
1774 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1778 s
[i
] = new_stmt(BPF_ST
);
1782 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1785 /* A = P[X + packet head] */
1786 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1790 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1794 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1798 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1801 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1805 /* goto again; (must use BPF_JA for backward jump) */
1806 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1807 s
[i
]->s
.k
= again
- i
- 1;
1812 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1814 s
[fix2
]->s
.jt
= s
[end
];
1815 s
[fix4
]->s
.jf
= s
[end
];
1816 s
[fix5
]->s
.jt
= s
[end
];
1823 for (i
= 0; i
< max
- 1; i
++)
1824 s
[i
]->next
= s
[i
+ 1];
1825 s
[max
- 1]->next
= NULL
;
1830 b
= new_block(JMP(BPF_JEQ
));
1831 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
1842 static struct block
*
1843 gen_proto(v
, proto
, dir
)
1848 struct block
*b0
, *b1
;
1850 if (dir
!= Q_DEFAULT
)
1851 bpf_error("direction applied to 'proto'");
1856 b0
= gen_proto(v
, Q_IP
, dir
);
1857 b1
= gen_proto(v
, Q_IPV6
, dir
);
1864 b0
= gen_linktype(ETHERTYPE_IP
);
1866 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
1868 b1
= gen_protochain(v
, Q_IP
);
1874 bpf_error("arp does not encapsulate another protocol");
1878 bpf_error("rarp does not encapsulate another protocol");
1882 bpf_error("atalk encapsulation is not specifiable");
1886 bpf_error("decnet encapsulation is not specifiable");
1890 bpf_error("sca does not encapsulate another protocol");
1894 bpf_error("lat does not encapsulate another protocol");
1898 bpf_error("moprc does not encapsulate another protocol");
1902 bpf_error("mopdl does not encapsulate another protocol");
1906 return gen_linktype(v
);
1909 bpf_error("'udp proto' is bogus");
1913 bpf_error("'tcp proto' is bogus");
1917 bpf_error("'icmp proto' is bogus");
1921 bpf_error("'igmp proto' is bogus");
1925 bpf_error("'igrp proto' is bogus");
1929 bpf_error("'pim proto' is bogus");
1934 b0
= gen_linktype(ETHERTYPE_IPV6
);
1936 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
1938 b1
= gen_protochain(v
, Q_IPV6
);
1944 bpf_error("'icmp6 proto' is bogus");
1948 bpf_error("'ah proto' is bogus");
1951 bpf_error("'ah proto' is bogus");
1962 register const char *name
;
1965 int proto
= q
.proto
;
1969 bpf_u_int32 mask
, addr
, **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),