]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
0ff11fc97acb5d6b9d6c06b0ff3fb21567c91d09
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.125 2000-10-25 07:28:22 guy Exp $ (LBL)";
31 #include <sys/types.h>
32 #include <sys/socket.h>
35 #include <sys/param.h>
42 #include <netinet/in.h>
52 #include "ethertype.h"
55 #include <pcap-namedb.h>
58 #include <sys/socket.h>
61 #ifdef HAVE_OS_PROTO_H
65 #define JMP(c) ((c)|BPF_JMP|BPF_K)
68 static jmp_buf top_ctx
;
69 static pcap_t
*bpf_pcap
;
73 int pcap_fddipad
= PCAP_FDDIPAD
;
80 bpf_error(const char *fmt
, ...)
87 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
94 static void init_linktype(int);
96 static int alloc_reg(void);
97 static void free_reg(int);
99 static struct block
*root
;
102 * We divy out chunks of memory rather than call malloc each time so
103 * we don't have to worry about leaking memory. It's probably
104 * not a big deal if all this memory was wasted but it this ever
105 * goes into a library that would probably not be a good idea.
108 #define CHUNK0SIZE 1024
114 static struct chunk chunks
[NCHUNKS
];
115 static int cur_chunk
;
117 static void *newchunk(u_int
);
118 static void freechunks(void);
119 static inline struct block
*new_block(int);
120 static inline struct slist
*new_stmt(int);
121 static struct block
*gen_retblk(int);
122 static inline void syntax(void);
124 static void backpatch(struct block
*, struct block
*);
125 static void merge(struct block
*, struct block
*);
126 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
127 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
128 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
129 static struct block
*gen_uncond(int);
130 static inline struct block
*gen_true(void);
131 static inline struct block
*gen_false(void);
132 static struct block
*gen_linktype(int);
133 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
135 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
137 static struct block
*gen_ehostop(const u_char
*, int);
138 static struct block
*gen_fhostop(const u_char
*, int);
139 static struct block
*gen_thostop(const u_char
*, int);
140 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
141 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
143 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
146 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
148 static struct block
*gen_ipfrag(void);
149 static struct block
*gen_portatom(int, bpf_int32
);
151 static struct block
*gen_portatom6(int, bpf_int32
);
153 struct block
*gen_portop(int, int, int);
154 static struct block
*gen_port(int, int, int);
156 struct block
*gen_portop6(int, int, int);
157 static struct block
*gen_port6(int, int, int);
159 static int lookup_proto(const char *, int);
160 static struct block
*gen_protochain(int, int, int);
161 static struct block
*gen_proto(int, int, int);
162 static struct slist
*xfer_to_x(struct arth
*);
163 static struct slist
*xfer_to_a(struct arth
*);
164 static struct block
*gen_len(int, int);
174 /* XXX Round up to nearest long. */
175 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
177 /* XXX Round up to structure boundary. */
181 cp
= &chunks
[cur_chunk
];
182 if (n
> cp
->n_left
) {
183 ++cp
, k
= ++cur_chunk
;
185 bpf_error("out of memory");
186 size
= CHUNK0SIZE
<< k
;
187 cp
->m
= (void *)malloc(size
);
188 memset((char *)cp
->m
, 0, size
);
191 bpf_error("out of memory");
194 return (void *)((char *)cp
->m
+ cp
->n_left
);
203 for (i
= 0; i
< NCHUNKS
; ++i
)
204 if (chunks
[i
].m
!= NULL
) {
211 * A strdup whose allocations are freed after code generation is over.
215 register const char *s
;
217 int n
= strlen(s
) + 1;
218 char *cp
= newchunk(n
);
224 static inline struct block
*
230 p
= (struct block
*)newchunk(sizeof(*p
));
237 static inline struct slist
*
243 p
= (struct slist
*)newchunk(sizeof(*p
));
249 static struct block
*
253 struct block
*b
= new_block(BPF_RET
|BPF_K
);
262 bpf_error("syntax error in filter expression");
265 static bpf_u_int32 netmask
;
270 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
271 char *buf
, int optimize
, bpf_u_int32 mask
)
280 if (setjmp(top_ctx
)) {
288 /* On Linux we do not use the BPF filter to truncate the packet
289 * since the kernel provides other ways for that. In fact if we
290 * are using the packet filter for that duty we will be unable
291 * to acquire the original packet size. -- Torsten */
293 snaplen
= pcap_snapshot(p
);
298 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
299 "snaplen of 0 rejects all packets");
303 lex_init(buf
? buf
: "");
304 init_linktype(pcap_datalink(p
));
311 root
= gen_retblk(snaplen
);
313 if (optimize
&& !no_optimize
) {
316 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
317 bpf_error("expression rejects all packets");
319 program
->bf_insns
= icode_to_fcode(root
, &len
);
320 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 * Clean up a "struct bpf_program" by freeing all the memory allocated
380 pcap_freecode(pcap_t
*p
, struct bpf_program
*program
)
383 if (program
->bf_insns
!= NULL
) {
384 free((char *)program
->bf_insns
);
385 program
->bf_insns
= NULL
;
390 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
391 * which of the jt and jf fields has been resolved and which is a pointer
392 * back to another unresolved block (or nil). At least one of the fields
393 * in each block is already resolved.
396 backpatch(list
, target
)
397 struct block
*list
, *target
;
414 * Merge the lists in b0 and b1, using the 'sense' field to indicate
415 * which of jt and jf is the link.
419 struct block
*b0
, *b1
;
421 register struct block
**p
= &b0
;
423 /* Find end of list. */
425 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
427 /* Concatenate the lists. */
435 backpatch(p
, gen_retblk(snaplen
));
436 p
->sense
= !p
->sense
;
437 backpatch(p
, gen_retblk(0));
443 struct block
*b0
, *b1
;
445 backpatch(b0
, b1
->head
);
446 b0
->sense
= !b0
->sense
;
447 b1
->sense
= !b1
->sense
;
449 b1
->sense
= !b1
->sense
;
455 struct block
*b0
, *b1
;
457 b0
->sense
= !b0
->sense
;
458 backpatch(b0
, b1
->head
);
459 b0
->sense
= !b0
->sense
;
468 b
->sense
= !b
->sense
;
471 static struct block
*
472 gen_cmp(offset
, size
, v
)
479 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
482 b
= new_block(JMP(BPF_JEQ
));
489 static struct block
*
490 gen_mcmp(offset
, size
, v
, mask
)
495 struct block
*b
= gen_cmp(offset
, size
, v
);
498 if (mask
!= 0xffffffff) {
499 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
506 static struct block
*
507 gen_bcmp(offset
, size
, v
)
508 register u_int offset
, size
;
509 register const u_char
*v
;
511 register struct block
*b
, *tmp
;
515 register const u_char
*p
= &v
[size
- 4];
516 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
517 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
519 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
526 register const u_char
*p
= &v
[size
- 2];
527 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
529 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
536 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
545 * Various code constructs need to know the layout of the data link
546 * layer. These variables give the necessary offsets. off_linktype
547 * is set to -1 for no encapsulation, in which case, IP is assumed.
549 static u_int off_linktype
;
568 * SLIP doesn't have a link level type. The 16 byte
569 * header is hacked into our SLIP driver.
576 /* XXX this may be the same as the DLT_PPP_BSDOS case */
601 * FDDI doesn't really have a link-level type field.
602 * We assume that SSAP = SNAP is being used and pick
603 * out the encapsulated Ethernet type.
605 * XXX - should we generate code to check for SNAP?
609 off_linktype
+= pcap_fddipad
;
613 off_nl
+= pcap_fddipad
;
619 * Token Ring doesn't really have a link-level type field.
620 * We assume that SSAP = SNAP is being used and pick
621 * out the encapsulated Ethernet type.
623 * XXX - should we generate code to check for SNAP?
625 * XXX - the header is actually variable-length.
626 * Some various Linux patched versions gave 38
627 * as "off_linktype" and 40 as "off_nl"; however,
628 * if a token ring packet has *no* routing
629 * information, i.e. is not source-routed, the correct
630 * values are 20 and 22, as they are in the vanilla code.
632 * A packet is source-routed iff the uppermost bit
633 * of the first byte of the source address, at an
634 * offset of 8, has the uppermost bit set. If the
635 * packet is source-routed, the total number of bytes
636 * of routing information is 2 plus bits 0x1F00 of
637 * the 16-bit value at an offset of 14 (shifted right
638 * 8 - figure out which byte that is).
644 case DLT_ATM_RFC1483
:
646 * assume routed, non-ISO PDUs
647 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
658 case DLT_ATM_CLIP
: /* Linux ATM defines this */
663 bpf_error("unknown data link type 0x%x", linktype
);
667 static struct block
*
674 s
= new_stmt(BPF_LD
|BPF_IMM
);
676 b
= new_block(JMP(BPF_JEQ
));
682 static inline struct block
*
685 return gen_uncond(1);
688 static inline struct block
*
691 return gen_uncond(0);
694 static struct block
*
698 struct block
*b0
, *b1
;
700 /* If we're not using encapsulation, we're done */
701 if (off_linktype
== -1)
711 if (proto
== ETHERTYPE_IP
)
712 proto
= PPP_IP
; /* XXX was 0x21 */
714 else if (proto
== ETHERTYPE_IPV6
)
723 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
724 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
726 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
741 case ETHERTYPE_ATALK
:
753 if (proto
== ETHERTYPE_IP
)
754 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET
)));
756 else if (proto
== ETHERTYPE_IPV6
)
757 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET6
)));
762 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
765 static struct block
*
766 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
770 u_int src_off
, dst_off
;
772 struct block
*b0
, *b1
;
786 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
787 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
793 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
794 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
801 b0
= gen_linktype(proto
);
802 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
808 static struct block
*
809 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
810 struct in6_addr
*addr
;
811 struct in6_addr
*mask
;
813 u_int src_off
, dst_off
;
815 struct block
*b0
, *b1
;
830 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
831 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
837 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
838 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
845 /* this order is important */
846 a
= (u_int32_t
*)addr
;
847 m
= (u_int32_t
*)mask
;
848 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
849 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
851 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
853 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
855 b0
= gen_linktype(proto
);
861 static struct block
*
862 gen_ehostop(eaddr
, dir
)
863 register const u_char
*eaddr
;
866 register struct block
*b0
, *b1
;
870 return gen_bcmp(6, 6, eaddr
);
873 return gen_bcmp(0, 6, eaddr
);
876 b0
= gen_ehostop(eaddr
, Q_SRC
);
877 b1
= gen_ehostop(eaddr
, Q_DST
);
883 b0
= gen_ehostop(eaddr
, Q_SRC
);
884 b1
= gen_ehostop(eaddr
, Q_DST
);
893 * Like gen_ehostop, but for DLT_FDDI
895 static struct block
*
896 gen_fhostop(eaddr
, dir
)
897 register const u_char
*eaddr
;
900 struct block
*b0
, *b1
;
905 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
907 return gen_bcmp(6 + 1, 6, eaddr
);
912 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
914 return gen_bcmp(0 + 1, 6, eaddr
);
918 b0
= gen_fhostop(eaddr
, Q_SRC
);
919 b1
= gen_fhostop(eaddr
, Q_DST
);
925 b0
= gen_fhostop(eaddr
, Q_SRC
);
926 b1
= gen_fhostop(eaddr
, Q_DST
);
935 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
937 static struct block
*
938 gen_thostop(eaddr
, dir
)
939 register const u_char
*eaddr
;
942 register struct block
*b0
, *b1
;
946 return gen_bcmp(8, 6, eaddr
);
949 return gen_bcmp(2, 6, eaddr
);
952 b0
= gen_thostop(eaddr
, Q_SRC
);
953 b1
= gen_thostop(eaddr
, Q_DST
);
959 b0
= gen_thostop(eaddr
, Q_SRC
);
960 b1
= gen_thostop(eaddr
, Q_DST
);
969 * This is quite tricky because there may be pad bytes in front of the
970 * DECNET header, and then there are two possible data packet formats that
971 * carry both src and dst addresses, plus 5 packet types in a format that
972 * carries only the src node, plus 2 types that use a different format and
973 * also carry just the src node.
977 * Instead of doing those all right, we just look for data packets with
978 * 0 or 1 bytes of padding. If you want to look at other packets, that
979 * will require a lot more hacking.
981 * To add support for filtering on DECNET "areas" (network numbers)
982 * one would want to add a "mask" argument to this routine. That would
983 * make the filter even more inefficient, although one could be clever
984 * and not generate masking instructions if the mask is 0xFFFF.
986 static struct block
*
987 gen_dnhostop(addr
, dir
, base_off
)
992 struct block
*b0
, *b1
, *b2
, *tmp
;
993 u_int offset_lh
; /* offset if long header is received */
994 u_int offset_sh
; /* offset if short header is received */
999 offset_sh
= 1; /* follows flags */
1000 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1004 offset_sh
= 3; /* follows flags, dstnode */
1005 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1009 /* Inefficient because we do our Calvinball dance twice */
1010 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1011 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1017 /* Inefficient because we do our Calvinball dance twice */
1018 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1019 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1026 b0
= gen_linktype(ETHERTYPE_DN
);
1027 /* Check for pad = 1, long header case */
1028 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1029 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1030 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1031 BPF_H
, (bpf_int32
)ntohs(addr
));
1033 /* Check for pad = 0, long header case */
1034 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1035 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1038 /* Check for pad = 1, short header case */
1039 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1040 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1041 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1042 BPF_H
, (bpf_int32
)ntohs(addr
));
1045 /* Check for pad = 0, short header case */
1046 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1047 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1051 /* Combine with test for linktype */
1056 static struct block
*
1057 gen_host(addr
, mask
, proto
, dir
)
1063 struct block
*b0
, *b1
;
1068 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1069 if (off_linktype
!= -1) {
1070 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1072 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1078 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1079 off_nl
+ 12, off_nl
+ 16);
1082 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1083 off_nl
+ 14, off_nl
+ 24);
1086 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1087 off_nl
+ 14, off_nl
+ 24);
1090 bpf_error("'tcp' modifier applied to host");
1093 bpf_error("'udp' modifier applied to host");
1096 bpf_error("'icmp' modifier applied to host");
1099 bpf_error("'igmp' modifier applied to host");
1102 bpf_error("'igrp' modifier applied to host");
1105 bpf_error("'pim' modifier applied to host");
1108 bpf_error("ATALK host filtering not implemented");
1111 return gen_dnhostop(addr
, dir
, off_nl
);
1114 bpf_error("SCA host filtering not implemented");
1117 bpf_error("LAT host filtering not implemented");
1120 bpf_error("MOPDL host filtering not implemented");
1123 bpf_error("MOPRC host filtering not implemented");
1127 bpf_error("'ip6' modifier applied to ip host");
1130 bpf_error("'icmp6' modifier applied to host");
1134 bpf_error("'ah' modifier applied to host");
1137 bpf_error("'esp' modifier applied to host");
1146 static struct block
*
1147 gen_host6(addr
, mask
, proto
, dir
)
1148 struct in6_addr
*addr
;
1149 struct in6_addr
*mask
;
1156 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1159 bpf_error("'ip' modifier applied to ip6 host");
1162 bpf_error("'rarp' modifier applied to ip6 host");
1165 bpf_error("'arp' modifier applied to ip6 host");
1168 bpf_error("'tcp' modifier applied to host");
1171 bpf_error("'udp' modifier applied to host");
1174 bpf_error("'icmp' modifier applied to host");
1177 bpf_error("'igmp' modifier applied to host");
1180 bpf_error("'igrp' modifier applied to host");
1183 bpf_error("'pim' modifier applied to host");
1186 bpf_error("ATALK host filtering not implemented");
1189 bpf_error("'decnet' modifier applied to ip6 host");
1192 bpf_error("SCA host filtering not implemented");
1195 bpf_error("LAT host filtering not implemented");
1198 bpf_error("MOPDL host filtering not implemented");
1201 bpf_error("MOPRC host filtering not implemented");
1204 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1205 off_nl
+ 8, off_nl
+ 24);
1208 bpf_error("'icmp6' modifier applied to host");
1211 bpf_error("'ah' modifier applied to host");
1214 bpf_error("'esp' modifier applied to host");
1224 static struct block
*
1225 gen_gateway(eaddr
, alist
, proto
, dir
)
1226 const u_char
*eaddr
;
1227 bpf_u_int32
**alist
;
1231 struct block
*b0
, *b1
, *tmp
;
1234 bpf_error("direction applied to 'gateway'");
1241 if (linktype
== DLT_EN10MB
)
1242 b0
= gen_ehostop(eaddr
, Q_OR
);
1243 else if (linktype
== DLT_FDDI
)
1244 b0
= gen_fhostop(eaddr
, Q_OR
);
1245 else if (linktype
== DLT_IEEE802
)
1246 b0
= gen_thostop(eaddr
, Q_OR
);
1249 "'gateway' supported only on ethernet, FDDI or token ring");
1251 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1253 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1261 bpf_error("illegal modifier of 'gateway'");
1267 gen_proto_abbrev(proto
)
1278 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1280 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1286 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1288 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1294 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1297 #ifndef IPPROTO_IGMP
1298 #define IPPROTO_IGMP 2
1302 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1305 #ifndef IPPROTO_IGRP
1306 #define IPPROTO_IGRP 9
1309 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1313 #define IPPROTO_PIM 103
1317 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1319 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1325 b1
= gen_linktype(ETHERTYPE_IP
);
1329 b1
= gen_linktype(ETHERTYPE_ARP
);
1333 b1
= gen_linktype(ETHERTYPE_REVARP
);
1337 bpf_error("link layer applied in wrong context");
1340 b1
= gen_linktype(ETHERTYPE_ATALK
);
1344 b1
= gen_linktype(ETHERTYPE_DN
);
1348 b1
= gen_linktype(ETHERTYPE_SCA
);
1352 b1
= gen_linktype(ETHERTYPE_LAT
);
1356 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1360 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1365 b1
= gen_linktype(ETHERTYPE_IPV6
);
1368 #ifndef IPPROTO_ICMPV6
1369 #define IPPROTO_ICMPV6 58
1372 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1377 #define IPPROTO_AH 51
1380 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1382 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1388 #define IPPROTO_ESP 50
1391 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1393 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1404 static struct block
*
1411 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1412 s
->s
.k
= off_nl
+ 6;
1413 b
= new_block(JMP(BPF_JSET
));
1421 static struct block
*
1422 gen_portatom(off
, v
)
1429 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1432 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1433 s
->next
->s
.k
= off_nl
+ off
;
1435 b
= new_block(JMP(BPF_JEQ
));
1443 static struct block
*
1444 gen_portatom6(off
, v
)
1448 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1453 gen_portop(port
, proto
, dir
)
1454 int port
, proto
, dir
;
1456 struct block
*b0
, *b1
, *tmp
;
1458 /* ip proto 'proto' */
1459 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1465 b1
= gen_portatom(0, (bpf_int32
)port
);
1469 b1
= gen_portatom(2, (bpf_int32
)port
);
1474 tmp
= gen_portatom(0, (bpf_int32
)port
);
1475 b1
= gen_portatom(2, (bpf_int32
)port
);
1480 tmp
= gen_portatom(0, (bpf_int32
)port
);
1481 b1
= gen_portatom(2, (bpf_int32
)port
);
1493 static struct block
*
1494 gen_port(port
, ip_proto
, dir
)
1499 struct block
*b0
, *b1
, *tmp
;
1501 /* ether proto ip */
1502 b0
= gen_linktype(ETHERTYPE_IP
);
1507 b1
= gen_portop(port
, ip_proto
, dir
);
1511 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1512 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1525 gen_portop6(port
, proto
, dir
)
1526 int port
, proto
, dir
;
1528 struct block
*b0
, *b1
, *tmp
;
1530 /* ip proto 'proto' */
1531 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1535 b1
= gen_portatom6(0, (bpf_int32
)port
);
1539 b1
= gen_portatom6(2, (bpf_int32
)port
);
1544 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1545 b1
= gen_portatom6(2, (bpf_int32
)port
);
1550 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1551 b1
= gen_portatom6(2, (bpf_int32
)port
);
1563 static struct block
*
1564 gen_port6(port
, ip_proto
, dir
)
1569 struct block
*b0
, *b1
, *tmp
;
1571 /* ether proto ip */
1572 b0
= gen_linktype(ETHERTYPE_IPV6
);
1577 b1
= gen_portop6(port
, ip_proto
, dir
);
1581 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1582 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1595 lookup_proto(name
, proto
)
1596 register const char *name
;
1605 v
= pcap_nametoproto(name
);
1606 if (v
== PROTO_UNDEF
)
1607 bpf_error("unknown ip proto '%s'", name
);
1611 /* XXX should look up h/w protocol type based on linktype */
1612 v
= pcap_nametoeproto(name
);
1613 if (v
== PROTO_UNDEF
)
1614 bpf_error("unknown ether proto '%s'", name
);
1634 static struct block
*
1635 gen_protochain(v
, proto
, dir
)
1640 #ifdef NO_PROTOCHAIN
1641 return gen_proto(v
, proto
, dir
);
1643 struct block
*b0
, *b
;
1644 struct slist
*s
[100];
1645 int fix2
, fix3
, fix4
, fix5
;
1646 int ahcheck
, again
, end
;
1648 int reg1
= alloc_reg();
1649 int reg2
= alloc_reg();
1651 memset(s
, 0, sizeof(s
));
1652 fix2
= fix3
= fix4
= fix5
= 0;
1659 b0
= gen_protochain(v
, Q_IP
, dir
);
1660 b
= gen_protochain(v
, Q_IPV6
, dir
);
1664 bpf_error("bad protocol applied for 'protochain'");
1668 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1671 * s[0] is a dummy entry to protect other BPF insn from damaged
1672 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1673 * hard to find interdependency made by jump table fixup.
1676 s
[i
] = new_stmt(0); /*dummy*/
1681 b0
= gen_linktype(ETHERTYPE_IP
);
1684 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1685 s
[i
]->s
.k
= off_nl
+ 9;
1687 /* X = ip->ip_hl << 2 */
1688 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1694 b0
= gen_linktype(ETHERTYPE_IPV6
);
1696 /* A = ip6->ip_nxt */
1697 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1698 s
[i
]->s
.k
= off_nl
+ 6;
1700 /* X = sizeof(struct ip6_hdr) */
1701 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1707 bpf_error("unsupported proto to gen_protochain");
1711 /* again: if (A == v) goto end; else fall through; */
1713 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1715 s
[i
]->s
.jt
= NULL
; /*later*/
1716 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1720 #ifndef IPPROTO_NONE
1721 #define IPPROTO_NONE 59
1723 /* if (A == IPPROTO_NONE) goto end */
1724 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1725 s
[i
]->s
.jt
= NULL
; /*later*/
1726 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1727 s
[i
]->s
.k
= IPPROTO_NONE
;
1728 s
[fix5
]->s
.jf
= s
[i
];
1733 if (proto
== Q_IPV6
) {
1734 int v6start
, v6end
, v6advance
, j
;
1737 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1738 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1739 s
[i
]->s
.jt
= NULL
; /*later*/
1740 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1741 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
1742 s
[fix2
]->s
.jf
= s
[i
];
1744 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1745 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1746 s
[i
]->s
.jt
= NULL
; /*later*/
1747 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1748 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
1750 /* if (A == IPPROTO_ROUTING) goto v6advance */
1751 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1752 s
[i
]->s
.jt
= NULL
; /*later*/
1753 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1754 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1756 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1757 s
[i
- 1]->s
.jf
= 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_FRAGMENT
;
1771 * X = X + (P[X] + 1) * 8;
1774 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1777 s
[i
] = new_stmt(BPF_ST
);
1781 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1785 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1787 /* A = P[X + packet head]; */
1788 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1792 s
[i
] = new_stmt(BPF_ST
);
1796 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1799 /* A = P[X + packet head] */
1800 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1804 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1808 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1812 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1815 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1819 /* goto again; (must use BPF_JA for backward jump) */
1820 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1821 s
[i
]->s
.k
= again
- i
- 1;
1822 s
[i
- 1]->s
.jf
= s
[i
];
1826 for (j
= v6start
; j
<= v6end
; j
++)
1827 s
[j
]->s
.jt
= s
[v6advance
];
1832 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1834 s
[fix2
]->s
.jf
= s
[i
];
1840 /* if (A == IPPROTO_AH) then fall through; else goto end; */
1841 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1842 s
[i
]->s
.jt
= NULL
; /*later*/
1843 s
[i
]->s
.jf
= NULL
; /*later*/
1844 s
[i
]->s
.k
= IPPROTO_AH
;
1846 s
[fix3
]->s
.jf
= s
[ahcheck
];
1853 * X = X + (P[X] + 2) * 4;
1856 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1859 s
[i
] = new_stmt(BPF_ST
);
1863 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1867 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1869 /* A = P[X + packet head]; */
1870 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1874 s
[i
] = new_stmt(BPF_ST
);
1878 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1881 /* A = P[X + packet head] */
1882 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1886 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1890 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1894 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1897 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1901 /* goto again; (must use BPF_JA for backward jump) */
1902 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1903 s
[i
]->s
.k
= again
- i
- 1;
1908 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1910 s
[fix2
]->s
.jt
= s
[end
];
1911 s
[fix4
]->s
.jf
= s
[end
];
1912 s
[fix5
]->s
.jt
= s
[end
];
1919 for (i
= 0; i
< max
- 1; i
++)
1920 s
[i
]->next
= s
[i
+ 1];
1921 s
[max
- 1]->next
= NULL
;
1926 b
= new_block(JMP(BPF_JEQ
));
1927 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
1938 static struct block
*
1939 gen_proto(v
, proto
, dir
)
1944 struct block
*b0
, *b1
;
1946 if (dir
!= Q_DEFAULT
)
1947 bpf_error("direction applied to 'proto'");
1952 b0
= gen_proto(v
, Q_IP
, dir
);
1953 b1
= gen_proto(v
, Q_IPV6
, dir
);
1960 b0
= gen_linktype(ETHERTYPE_IP
);
1962 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
1964 b1
= gen_protochain(v
, Q_IP
);
1970 bpf_error("arp does not encapsulate another protocol");
1974 bpf_error("rarp does not encapsulate another protocol");
1978 bpf_error("atalk encapsulation is not specifiable");
1982 bpf_error("decnet encapsulation is not specifiable");
1986 bpf_error("sca does not encapsulate another protocol");
1990 bpf_error("lat does not encapsulate another protocol");
1994 bpf_error("moprc does not encapsulate another protocol");
1998 bpf_error("mopdl does not encapsulate another protocol");
2002 return gen_linktype(v
);
2005 bpf_error("'udp proto' is bogus");
2009 bpf_error("'tcp proto' is bogus");
2013 bpf_error("'icmp proto' is bogus");
2017 bpf_error("'igmp proto' is bogus");
2021 bpf_error("'igrp proto' is bogus");
2025 bpf_error("'pim proto' is bogus");
2030 b0
= gen_linktype(ETHERTYPE_IPV6
);
2032 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2034 b1
= gen_protochain(v
, Q_IPV6
);
2040 bpf_error("'icmp6 proto' is bogus");
2044 bpf_error("'ah proto' is bogus");
2047 bpf_error("'ah proto' is bogus");
2058 register const char *name
;
2061 int proto
= q
.proto
;
2065 bpf_u_int32 mask
, addr
;
2067 bpf_u_int32
**alist
;
2070 struct sockaddr_in
*sin
;
2071 struct sockaddr_in6
*sin6
;
2072 struct addrinfo
*res
, *res0
;
2073 struct in6_addr mask128
;
2075 struct block
*b
, *tmp
;
2076 int port
, real_proto
;
2081 addr
= pcap_nametonetaddr(name
);
2083 bpf_error("unknown network '%s'", name
);
2084 /* Left justify network addr and calculate its network mask */
2086 while (addr
&& (addr
& 0xff000000) == 0) {
2090 return gen_host(addr
, mask
, proto
, dir
);
2094 if (proto
== Q_LINK
) {
2098 eaddr
= pcap_ether_hostton(name
);
2101 "unknown ether host '%s'", name
);
2102 return gen_ehostop(eaddr
, dir
);
2105 eaddr
= pcap_ether_hostton(name
);
2108 "unknown FDDI host '%s'", name
);
2109 return gen_fhostop(eaddr
, dir
);
2112 eaddr
= pcap_ether_hostton(name
);
2115 "unknown token ring host '%s'", name
);
2116 return gen_thostop(eaddr
, dir
);
2120 "only ethernet/FDDI/token ring supports link-level host name");
2123 } else if (proto
== Q_DECNET
) {
2124 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2126 * I don't think DECNET hosts can be multihomed, so
2127 * there is no need to build up a list of addresses
2129 return (gen_host(dn_addr
, 0, proto
, dir
));
2132 alist
= pcap_nametoaddr(name
);
2133 if (alist
== NULL
|| *alist
== NULL
)
2134 bpf_error("unknown host '%s'", name
);
2136 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2138 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2140 tmp
= gen_host(**alist
++, 0xffffffff,
2147 memset(&mask128
, 0xff, sizeof(mask128
));
2148 res0
= res
= pcap_nametoaddrinfo(name
);
2150 bpf_error("unknown host '%s'", name
);
2152 tproto
= tproto6
= proto
;
2153 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2157 for (res
= res0
; res
; res
= res
->ai_next
) {
2158 switch (res
->ai_family
) {
2160 if (tproto
== Q_IPV6
)
2163 sin
= (struct sockaddr_in
*)
2165 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2166 0xffffffff, tproto
, dir
);
2169 if (tproto6
== Q_IP
)
2172 sin6
= (struct sockaddr_in6
*)
2174 tmp
= gen_host6(&sin6
->sin6_addr
,
2175 &mask128
, tproto6
, dir
);
2184 bpf_error("unknown host '%s'%s", name
,
2185 (proto
== Q_DEFAULT
)
2187 : " for specified address family");
2194 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2195 bpf_error("illegal qualifier of 'port'");
2196 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2197 bpf_error("unknown port '%s'", name
);
2198 if (proto
== Q_UDP
) {
2199 if (real_proto
== IPPROTO_TCP
)
2200 bpf_error("port '%s' is tcp", name
);
2202 /* override PROTO_UNDEF */
2203 real_proto
= IPPROTO_UDP
;
2205 if (proto
== Q_TCP
) {
2206 if (real_proto
== IPPROTO_UDP
)
2207 bpf_error("port '%s' is udp", name
);
2209 /* override PROTO_UNDEF */
2210 real_proto
= IPPROTO_TCP
;
2213 return gen_port(port
, real_proto
, dir
);
2217 b
= gen_port(port
, real_proto
, dir
);
2218 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2225 eaddr
= pcap_ether_hostton(name
);
2227 bpf_error("unknown ether host: %s", name
);
2229 alist
= pcap_nametoaddr(name
);
2230 if (alist
== NULL
|| *alist
== NULL
)
2231 bpf_error("unknown host '%s'", name
);
2232 return gen_gateway(eaddr
, alist
, proto
, dir
);
2234 bpf_error("'gateway' not supported in this configuration");
2238 real_proto
= lookup_proto(name
, proto
);
2239 if (real_proto
>= 0)
2240 return gen_proto(real_proto
, proto
, dir
);
2242 bpf_error("unknown protocol: %s", name
);
2245 real_proto
= lookup_proto(name
, proto
);
2246 if (real_proto
>= 0)
2247 return gen_protochain(real_proto
, proto
, dir
);
2249 bpf_error("unknown protocol: %s", name
);
2261 gen_mcode(s1
, s2
, masklen
, q
)
2262 register const char *s1
, *s2
;
2263 register int masklen
;
2266 register int nlen
, mlen
;
2269 nlen
= __pcap_atoin(s1
, &n
);
2270 /* Promote short ipaddr */
2274 mlen
= __pcap_atoin(s2
, &m
);
2275 /* Promote short ipaddr */
2278 bpf_error("non-network bits set in \"%s mask %s\"",
2281 /* Convert mask len to mask */
2283 bpf_error("mask length must be <= 32");
2284 m
= 0xffffffff << (32 - masklen
);
2286 bpf_error("non-network bits set in \"%s/%d\"",
2293 return gen_host(n
, m
, q
.proto
, q
.dir
);
2296 bpf_error("Mask syntax for networks only");
2303 register const char *s
;
2308 int proto
= q
.proto
;
2314 else if (q
.proto
== Q_DECNET
)
2315 vlen
= __pcap_atodn(s
, &v
);
2317 vlen
= __pcap_atoin(s
, &v
);
2324 if (proto
== Q_DECNET
)
2325 return gen_host(v
, 0, proto
, dir
);
2326 else if (proto
== Q_LINK
) {
2327 bpf_error("illegal link layer address");
2330 if (s
== NULL
&& q
.addr
== Q_NET
) {
2331 /* Promote short net number */
2332 while (v
&& (v
& 0xff000000) == 0) {
2337 /* Promote short ipaddr */
2341 return gen_host(v
, mask
, proto
, dir
);
2346 proto
= IPPROTO_UDP
;
2347 else if (proto
== Q_TCP
)
2348 proto
= IPPROTO_TCP
;
2349 else if (proto
== Q_DEFAULT
)
2350 proto
= PROTO_UNDEF
;
2352 bpf_error("illegal qualifier of 'port'");
2355 return gen_port((int)v
, proto
, dir
);
2359 b
= gen_port((int)v
, proto
, dir
);
2360 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2366 bpf_error("'gateway' requires a name");
2370 return gen_proto((int)v
, proto
, dir
);
2373 return gen_protochain((int)v
, proto
, dir
);
2388 gen_mcode6(s1
, s2
, masklen
, q
)
2389 register const char *s1
, *s2
;
2390 register int masklen
;
2393 struct addrinfo
*res
;
2394 struct in6_addr
*addr
;
2395 struct in6_addr mask
;
2400 bpf_error("no mask %s supported", s2
);
2402 res
= pcap_nametoaddrinfo(s1
);
2404 bpf_error("invalid ip6 address %s", s1
);
2406 bpf_error("%s resolved to multiple address", s1
);
2407 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2409 if (sizeof(mask
) * 8 < masklen
)
2410 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2411 memset(&mask
, 0xff, masklen
/ 8);
2413 mask
.s6_addr
[masklen
/ 8] =
2414 (0xff << (8 - masklen
% 8)) & 0xff;
2417 a
= (u_int32_t
*)addr
;
2418 m
= (u_int32_t
*)&mask
;
2419 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2420 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2421 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2429 bpf_error("Mask syntax for networks only");
2433 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2438 bpf_error("invalid qualifier against IPv6 address");
2446 register const u_char
*eaddr
;
2449 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2450 if (linktype
== DLT_EN10MB
)
2451 return gen_ehostop(eaddr
, (int)q
.dir
);
2452 if (linktype
== DLT_FDDI
)
2453 return gen_fhostop(eaddr
, (int)q
.dir
);
2454 if (linktype
== DLT_IEEE802
)
2455 return gen_thostop(eaddr
, (int)q
.dir
);
2457 bpf_error("ethernet address used in non-ether expression");
2463 struct slist
*s0
, *s1
;
2466 * This is definitely not the best way to do this, but the
2467 * lists will rarely get long.
2474 static struct slist
*
2480 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2485 static struct slist
*
2491 s
= new_stmt(BPF_LD
|BPF_MEM
);
2497 gen_load(proto
, index
, size
)
2502 struct slist
*s
, *tmp
;
2504 int regno
= alloc_reg();
2506 free_reg(index
->regno
);
2510 bpf_error("data size must be 1, 2, or 4");
2526 bpf_error("unsupported index operation");
2529 s
= xfer_to_x(index
);
2530 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2532 sappend(index
->s
, s
);
2547 /* XXX Note that we assume a fixed link header here. */
2548 s
= xfer_to_x(index
);
2549 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2552 sappend(index
->s
, s
);
2554 b
= gen_proto_abbrev(proto
);
2556 gen_and(index
->b
, b
);
2566 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2568 sappend(s
, xfer_to_a(index
));
2569 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2570 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2571 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2573 sappend(index
->s
, s
);
2575 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2577 gen_and(index
->b
, b
);
2579 gen_and(gen_proto_abbrev(Q_IP
), b
);
2585 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2589 index
->regno
= regno
;
2590 s
= new_stmt(BPF_ST
);
2592 sappend(index
->s
, s
);
2598 gen_relation(code
, a0
, a1
, reversed
)
2600 struct arth
*a0
, *a1
;
2603 struct slist
*s0
, *s1
, *s2
;
2604 struct block
*b
, *tmp
;
2608 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2609 b
= new_block(JMP(code
));
2610 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2611 reversed
= !reversed
;
2612 b
->s
.k
= 0x80000000;
2620 sappend(a0
->s
, a1
->s
);
2624 free_reg(a0
->regno
);
2625 free_reg(a1
->regno
);
2627 /* 'and' together protocol checks */
2630 gen_and(a0
->b
, tmp
= a1
->b
);
2646 int regno
= alloc_reg();
2647 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2650 s
= new_stmt(BPF_LD
|BPF_LEN
);
2651 s
->next
= new_stmt(BPF_ST
);
2652 s
->next
->s
.k
= regno
;
2667 a
= (struct arth
*)newchunk(sizeof(*a
));
2671 s
= new_stmt(BPF_LD
|BPF_IMM
);
2673 s
->next
= new_stmt(BPF_ST
);
2689 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2692 s
= new_stmt(BPF_ST
);
2700 gen_arth(code
, a0
, a1
)
2702 struct arth
*a0
, *a1
;
2704 struct slist
*s0
, *s1
, *s2
;
2708 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2713 sappend(a0
->s
, a1
->s
);
2715 free_reg(a1
->regno
);
2717 s0
= new_stmt(BPF_ST
);
2718 a0
->regno
= s0
->s
.k
= alloc_reg();
2725 * Here we handle simple allocation of the scratch registers.
2726 * If too many registers are alloc'd, the allocator punts.
2728 static int regused
[BPF_MEMWORDS
];
2732 * Return the next free register.
2737 int n
= BPF_MEMWORDS
;
2740 if (regused
[curreg
])
2741 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2743 regused
[curreg
] = 1;
2747 bpf_error("too many registers needed to evaluate expression");
2752 * Return a register to the table so it can
2762 static struct block
*
2769 s
= new_stmt(BPF_LD
|BPF_LEN
);
2770 b
= new_block(JMP(jmp
));
2781 return gen_len(BPF_JGE
, n
);
2790 b
= gen_len(BPF_JGT
, n
);
2797 gen_byteop(op
, idx
, val
)
2808 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2811 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2812 b
->s
.code
= JMP(BPF_JGE
);
2817 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2818 b
->s
.code
= JMP(BPF_JGT
);
2822 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
2826 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2830 b
= new_block(JMP(BPF_JEQ
));
2838 gen_broadcast(proto
)
2841 bpf_u_int32 hostmask
;
2842 struct block
*b0
, *b1
, *b2
;
2843 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2849 if (linktype
== DLT_EN10MB
)
2850 return gen_ehostop(ebroadcast
, Q_DST
);
2851 if (linktype
== DLT_FDDI
)
2852 return gen_fhostop(ebroadcast
, Q_DST
);
2853 if (linktype
== DLT_IEEE802
)
2854 return gen_thostop(ebroadcast
, Q_DST
);
2855 bpf_error("not a broadcast link");
2859 b0
= gen_linktype(ETHERTYPE_IP
);
2860 hostmask
= ~netmask
;
2861 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
2862 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
2863 (bpf_int32
)(~0 & hostmask
), hostmask
);
2868 bpf_error("only ether/ip broadcast filters supported");
2872 gen_multicast(proto
)
2875 register struct block
*b0
, *b1
;
2876 register struct slist
*s
;
2882 if (linktype
== DLT_EN10MB
) {
2883 /* ether[0] & 1 != 0 */
2884 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2886 b0
= new_block(JMP(BPF_JSET
));
2892 if (linktype
== DLT_FDDI
) {
2893 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
2894 /* fddi[1] & 1 != 0 */
2895 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2897 b0
= new_block(JMP(BPF_JSET
));
2903 /* TODO - check how token ring handles multicast */
2904 /* if (linktype == DLT_IEEE802) ... */
2906 /* Link not known to support multicasts */
2910 b0
= gen_linktype(ETHERTYPE_IP
);
2911 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
2912 b1
->s
.code
= JMP(BPF_JGE
);
2918 b0
= gen_linktype(ETHERTYPE_IPV6
);
2919 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
2924 bpf_error("only IP multicast filters supported on ethernet/FDDI");
2928 * generate command for inbound/outbound. It's here so we can
2929 * make it link-type specific. 'dir' = 0 implies "inbound",
2930 * = 1 implies "outbound".
2936 register struct block
*b0
;
2938 b0
= gen_relation(BPF_JEQ
,
2939 gen_load(Q_LINK
, gen_loadi(0), 1),
2946 * support IEEE 802.1Q VLAN trunk over ethernet
2952 static u_int orig_linktype
= -1, orig_nl
= -1;
2956 * Change the offsets to point to the type and data fields within
2957 * the VLAN packet. This is somewhat of a kludge.
2959 if (orig_nl
== (u_int
)-1) {
2960 orig_linktype
= off_linktype
; /* save original values */
2971 bpf_error("no VLAN support for data link type %d",
2977 /* check for VLAN */
2978 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
2980 /* If a specific VLAN is requested, check VLAN id */
2981 if (vlan_num
>= 0) {
2984 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);