]>
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.135 2000-10-30 06:06:53 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"
56 #include <pcap-namedb.h>
59 #include <sys/socket.h>
62 #define LLC_SNAP_LSAP 0xaa
63 #define LLC_ISO_LSAP 0xfe
67 #ifdef HAVE_OS_PROTO_H
71 #define JMP(c) ((c)|BPF_JMP|BPF_K)
74 static jmp_buf top_ctx
;
75 static pcap_t
*bpf_pcap
;
79 int pcap_fddipad
= PCAP_FDDIPAD
;
86 bpf_error(const char *fmt
, ...)
93 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
100 static void init_linktype(int);
102 static int alloc_reg(void);
103 static void free_reg(int);
105 static struct block
*root
;
108 * We divy out chunks of memory rather than call malloc each time so
109 * we don't have to worry about leaking memory. It's probably
110 * not a big deal if all this memory was wasted but it this ever
111 * goes into a library that would probably not be a good idea.
114 #define CHUNK0SIZE 1024
120 static struct chunk chunks
[NCHUNKS
];
121 static int cur_chunk
;
123 static void *newchunk(u_int
);
124 static void freechunks(void);
125 static inline struct block
*new_block(int);
126 static inline struct slist
*new_stmt(int);
127 static struct block
*gen_retblk(int);
128 static inline void syntax(void);
130 static void backpatch(struct block
*, struct block
*);
131 static void merge(struct block
*, struct block
*);
132 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
133 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
134 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
135 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
136 static struct block
*gen_uncond(int);
137 static inline struct block
*gen_true(void);
138 static inline struct block
*gen_false(void);
139 static struct block
*gen_linktype(int);
140 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
141 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
143 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
145 static struct block
*gen_ehostop(const u_char
*, int);
146 static struct block
*gen_fhostop(const u_char
*, int);
147 static struct block
*gen_thostop(const u_char
*, int);
148 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
149 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
151 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
154 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
156 static struct block
*gen_ipfrag(void);
157 static struct block
*gen_portatom(int, bpf_int32
);
159 static struct block
*gen_portatom6(int, bpf_int32
);
161 struct block
*gen_portop(int, int, int);
162 static struct block
*gen_port(int, int, int);
164 struct block
*gen_portop6(int, int, int);
165 static struct block
*gen_port6(int, int, int);
167 static int lookup_proto(const char *, int);
168 static struct block
*gen_protochain(int, int, int);
169 static struct block
*gen_proto(int, int, int);
170 static struct slist
*xfer_to_x(struct arth
*);
171 static struct slist
*xfer_to_a(struct arth
*);
172 static struct block
*gen_len(int, int);
182 /* XXX Round up to nearest long. */
183 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
185 /* XXX Round up to structure boundary. */
189 cp
= &chunks
[cur_chunk
];
190 if (n
> cp
->n_left
) {
191 ++cp
, k
= ++cur_chunk
;
193 bpf_error("out of memory");
194 size
= CHUNK0SIZE
<< k
;
195 cp
->m
= (void *)malloc(size
);
196 memset((char *)cp
->m
, 0, size
);
199 bpf_error("out of memory");
202 return (void *)((char *)cp
->m
+ cp
->n_left
);
211 for (i
= 0; i
< NCHUNKS
; ++i
)
212 if (chunks
[i
].m
!= NULL
) {
219 * A strdup whose allocations are freed after code generation is over.
223 register const char *s
;
225 int n
= strlen(s
) + 1;
226 char *cp
= newchunk(n
);
232 static inline struct block
*
238 p
= (struct block
*)newchunk(sizeof(*p
));
245 static inline struct slist
*
251 p
= (struct slist
*)newchunk(sizeof(*p
));
257 static struct block
*
261 struct block
*b
= new_block(BPF_RET
|BPF_K
);
270 bpf_error("syntax error in filter expression");
273 static bpf_u_int32 netmask
;
278 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
279 char *buf
, int optimize
, bpf_u_int32 mask
)
288 if (setjmp(top_ctx
)) {
296 /* On Linux we do not use the BPF filter to truncate the packet
297 * since the kernel provides other ways for that. In fact if we
298 * are using the packet filter for that duty we will be unable
299 * to acquire the original packet size. -- Torsten */
301 snaplen
= pcap_snapshot(p
);
306 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
307 "snaplen of 0 rejects all packets");
311 lex_init(buf
? buf
: "");
312 init_linktype(pcap_datalink(p
));
319 root
= gen_retblk(snaplen
);
321 if (optimize
&& !no_optimize
) {
324 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
325 bpf_error("expression rejects all packets");
327 program
->bf_insns
= icode_to_fcode(root
, &len
);
328 program
->bf_len
= len
;
336 * entry point for using the compiler with no pcap open
337 * pass in all the stuff that is needed explicitly instead.
340 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
341 struct bpf_program
*program
,
342 char *buf
, int optimize
, bpf_u_int32 mask
)
350 if (setjmp(top_ctx
)) {
357 /* XXX needed? I don't grok the use of globals here. */
358 snaplen
= snaplen_arg
;
360 lex_init(buf
? buf
: "");
361 init_linktype(linktype_arg
);
368 root
= gen_retblk(snaplen_arg
);
373 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
374 bpf_error("expression rejects all packets");
376 program
->bf_insns
= icode_to_fcode(root
, &len
);
377 program
->bf_len
= len
;
384 * Clean up a "struct bpf_program" by freeing all the memory allocated
388 pcap_freecode(struct bpf_program
*program
)
391 if (program
->bf_insns
!= NULL
) {
392 free((char *)program
->bf_insns
);
393 program
->bf_insns
= NULL
;
398 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
399 * which of the jt and jf fields has been resolved and which is a pointer
400 * back to another unresolved block (or nil). At least one of the fields
401 * in each block is already resolved.
404 backpatch(list
, target
)
405 struct block
*list
, *target
;
422 * Merge the lists in b0 and b1, using the 'sense' field to indicate
423 * which of jt and jf is the link.
427 struct block
*b0
, *b1
;
429 register struct block
**p
= &b0
;
431 /* Find end of list. */
433 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
435 /* Concatenate the lists. */
443 backpatch(p
, gen_retblk(snaplen
));
444 p
->sense
= !p
->sense
;
445 backpatch(p
, gen_retblk(0));
451 struct block
*b0
, *b1
;
453 backpatch(b0
, b1
->head
);
454 b0
->sense
= !b0
->sense
;
455 b1
->sense
= !b1
->sense
;
457 b1
->sense
= !b1
->sense
;
463 struct block
*b0
, *b1
;
465 b0
->sense
= !b0
->sense
;
466 backpatch(b0
, b1
->head
);
467 b0
->sense
= !b0
->sense
;
476 b
->sense
= !b
->sense
;
479 static struct block
*
480 gen_cmp(offset
, size
, v
)
487 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
490 b
= new_block(JMP(BPF_JEQ
));
497 static struct block
*
498 gen_cmp_gt(offset
, size
, v
)
505 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
508 b
= new_block(JMP(BPF_JGT
));
515 static struct block
*
516 gen_mcmp(offset
, size
, v
, mask
)
521 struct block
*b
= gen_cmp(offset
, size
, v
);
524 if (mask
!= 0xffffffff) {
525 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
532 static struct block
*
533 gen_bcmp(offset
, size
, v
)
534 register u_int offset
, size
;
535 register const u_char
*v
;
537 register struct block
*b
, *tmp
;
541 register const u_char
*p
= &v
[size
- 4];
542 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
543 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
545 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
552 register const u_char
*p
= &v
[size
- 2];
553 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
555 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
562 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
571 * Various code constructs need to know the layout of the data link
572 * layer. These variables give the necessary offsets. off_linktype
573 * is set to -1 for no encapsulation, in which case, IP is assumed.
575 static u_int off_linktype
;
594 * SLIP doesn't have a link level type. The 16 byte
595 * header is hacked into our SLIP driver.
602 /* XXX this may be the same as the DLT_PPP_BSDOS case */
627 * FDDI doesn't really have a link-level type field.
628 * We assume that SSAP = SNAP is being used and pick
629 * out the encapsulated Ethernet type.
631 * XXX - should we generate code to check for SNAP?
635 off_linktype
+= pcap_fddipad
;
639 off_nl
+= pcap_fddipad
;
645 * Token Ring doesn't really have a link-level type field.
646 * We assume that SSAP = SNAP is being used and pick
647 * out the encapsulated Ethernet type.
649 * XXX - should we generate code to check for SNAP?
651 * XXX - the header is actually variable-length.
652 * Some various Linux patched versions gave 38
653 * as "off_linktype" and 40 as "off_nl"; however,
654 * if a token ring packet has *no* routing
655 * information, i.e. is not source-routed, the correct
656 * values are 20 and 22, as they are in the vanilla code.
658 * A packet is source-routed iff the uppermost bit
659 * of the first byte of the source address, at an
660 * offset of 8, has the uppermost bit set. If the
661 * packet is source-routed, the total number of bytes
662 * of routing information is 2 plus bits 0x1F00 of
663 * the 16-bit value at an offset of 14 (shifted right
664 * 8 - figure out which byte that is).
670 case DLT_ATM_RFC1483
:
672 * assume routed, non-ISO PDUs
673 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
684 case DLT_ATM_CLIP
: /* Linux ATM defines this */
689 bpf_error("unknown data link type %d", linktype
);
693 static struct block
*
700 s
= new_stmt(BPF_LD
|BPF_IMM
);
702 b
= new_block(JMP(BPF_JEQ
));
708 static inline struct block
*
711 return gen_uncond(1);
714 static inline struct block
*
717 return gen_uncond(0);
720 static struct block
*
724 struct block
*b0
, *b1
;
726 /* If we're not using encapsulation, we're done */
727 if (off_linktype
== -1)
734 * XXX - handle other LLC-encapsulated protocols here
741 * OSI protocols always use 802.2 encapsulation.
743 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
745 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
746 ((LLC_ISO_LSAP
<< 8) | LLC_ISO_LSAP
));
750 case ETHERTYPE_ATALK
:
753 * EtherTalk (AppleTalk protocols on Ethernet link
754 * layer) may use 802.2 encapsulation.
758 * Check for 802.2 encapsulation (EtherTalk phase 2?);
759 * we check for an Ethernet type field less than
760 * 1500, which means it's an 802.3 length field.
762 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
766 * 802.2-encapsulated ETHERTYPE_ATALK packets are
767 * SNAP packets with an organization code of
768 * 0x080007 (Apple, for Appletalk) and a protocol
769 * type of ETHERTYPE_ATALK (Appletalk).
771 * 802.2-encapsulated ETHERTYPE_AARP packets are
772 * SNAP packets with an organization code of
773 * 0x000000 (encapsulated Ethernet) and a protocol
774 * type of ETHERTYPE_AARP (Appletalk ARP).
776 if (proto
== ETHERTYPE_ATALK
)
777 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
778 else /* proto == ETHERTYPE_AARP */
779 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
783 * Check for Ethernet encapsulation (Ethertalk
784 * phase 1?); we just check for the Ethernet
787 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
800 * We use Ethernet protocol types inside libpcap;
801 * map them to the corresponding PPP protocol types.
806 proto
= PPP_IP
; /* XXX was 0x21 */
819 case ETHERTYPE_ATALK
:
835 * We use Ethernet protocol types inside libpcap;
836 * map them to the corresponding PPP protocol types.
841 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
842 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
844 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
859 case ETHERTYPE_ATALK
:
875 if (proto
== ETHERTYPE_IP
)
876 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET
)));
878 else if (proto
== ETHERTYPE_IPV6
)
879 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET6
)));
884 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
888 * Check for an LLC SNAP packet with a given organization code and
889 * protocol type; we check the entire contents of the 802.2 LLC and
890 * snap headers, checking for DSAP and SSAP of SNAP and a control
891 * field of 0x03 in the LLC header, and for the specified organization
892 * code and protocol type in the SNAP header.
894 static struct block
*
895 gen_snap(orgcode
, ptype
, offset
)
902 snapblock
[0] = LLC_SNAP_LSAP
; /* DSAP = SNAP */
903 snapblock
[1] = LLC_SNAP_LSAP
; /* SSAP = SNAP */
904 snapblock
[2] = 0x03; /* control = UI */
905 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
906 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
907 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
908 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
909 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
910 return gen_bcmp(offset
, 8, snapblock
);
913 static struct block
*
914 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
918 u_int src_off
, dst_off
;
920 struct block
*b0
, *b1
;
934 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
935 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
941 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
942 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
949 b0
= gen_linktype(proto
);
950 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
956 static struct block
*
957 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
958 struct in6_addr
*addr
;
959 struct in6_addr
*mask
;
961 u_int src_off
, dst_off
;
963 struct block
*b0
, *b1
;
978 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
979 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
985 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
986 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
993 /* this order is important */
994 a
= (u_int32_t
*)addr
;
995 m
= (u_int32_t
*)mask
;
996 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
997 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
999 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1001 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1003 b0
= gen_linktype(proto
);
1009 static struct block
*
1010 gen_ehostop(eaddr
, dir
)
1011 register const u_char
*eaddr
;
1014 register struct block
*b0
, *b1
;
1018 return gen_bcmp(6, 6, eaddr
);
1021 return gen_bcmp(0, 6, eaddr
);
1024 b0
= gen_ehostop(eaddr
, Q_SRC
);
1025 b1
= gen_ehostop(eaddr
, Q_DST
);
1031 b0
= gen_ehostop(eaddr
, Q_SRC
);
1032 b1
= gen_ehostop(eaddr
, Q_DST
);
1041 * Like gen_ehostop, but for DLT_FDDI
1043 static struct block
*
1044 gen_fhostop(eaddr
, dir
)
1045 register const u_char
*eaddr
;
1048 struct block
*b0
, *b1
;
1053 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1055 return gen_bcmp(6 + 1, 6, eaddr
);
1060 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1062 return gen_bcmp(0 + 1, 6, eaddr
);
1066 b0
= gen_fhostop(eaddr
, Q_SRC
);
1067 b1
= gen_fhostop(eaddr
, Q_DST
);
1073 b0
= gen_fhostop(eaddr
, Q_SRC
);
1074 b1
= gen_fhostop(eaddr
, Q_DST
);
1083 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1085 static struct block
*
1086 gen_thostop(eaddr
, dir
)
1087 register const u_char
*eaddr
;
1090 register struct block
*b0
, *b1
;
1094 return gen_bcmp(8, 6, eaddr
);
1097 return gen_bcmp(2, 6, eaddr
);
1100 b0
= gen_thostop(eaddr
, Q_SRC
);
1101 b1
= gen_thostop(eaddr
, Q_DST
);
1107 b0
= gen_thostop(eaddr
, Q_SRC
);
1108 b1
= gen_thostop(eaddr
, Q_DST
);
1117 * This is quite tricky because there may be pad bytes in front of the
1118 * DECNET header, and then there are two possible data packet formats that
1119 * carry both src and dst addresses, plus 5 packet types in a format that
1120 * carries only the src node, plus 2 types that use a different format and
1121 * also carry just the src node.
1125 * Instead of doing those all right, we just look for data packets with
1126 * 0 or 1 bytes of padding. If you want to look at other packets, that
1127 * will require a lot more hacking.
1129 * To add support for filtering on DECNET "areas" (network numbers)
1130 * one would want to add a "mask" argument to this routine. That would
1131 * make the filter even more inefficient, although one could be clever
1132 * and not generate masking instructions if the mask is 0xFFFF.
1134 static struct block
*
1135 gen_dnhostop(addr
, dir
, base_off
)
1140 struct block
*b0
, *b1
, *b2
, *tmp
;
1141 u_int offset_lh
; /* offset if long header is received */
1142 u_int offset_sh
; /* offset if short header is received */
1147 offset_sh
= 1; /* follows flags */
1148 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1152 offset_sh
= 3; /* follows flags, dstnode */
1153 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1157 /* Inefficient because we do our Calvinball dance twice */
1158 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1159 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1165 /* Inefficient because we do our Calvinball dance twice */
1166 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1167 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1172 bpf_error("ISO host filtering not implemented");
1177 b0
= gen_linktype(ETHERTYPE_DN
);
1178 /* Check for pad = 1, long header case */
1179 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1180 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1181 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1182 BPF_H
, (bpf_int32
)ntohs(addr
));
1184 /* Check for pad = 0, long header case */
1185 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1186 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1189 /* Check for pad = 1, short header case */
1190 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1191 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1192 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1193 BPF_H
, (bpf_int32
)ntohs(addr
));
1196 /* Check for pad = 0, short header case */
1197 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1198 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1202 /* Combine with test for linktype */
1207 static struct block
*
1208 gen_host(addr
, mask
, proto
, dir
)
1214 struct block
*b0
, *b1
;
1219 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1220 if (off_linktype
!= -1) {
1221 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1223 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1229 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1230 off_nl
+ 12, off_nl
+ 16);
1233 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1234 off_nl
+ 14, off_nl
+ 24);
1237 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1238 off_nl
+ 14, off_nl
+ 24);
1241 bpf_error("'tcp' modifier applied to host");
1244 bpf_error("'udp' modifier applied to host");
1247 bpf_error("'icmp' modifier applied to host");
1250 bpf_error("'igmp' modifier applied to host");
1253 bpf_error("'igrp' modifier applied to host");
1256 bpf_error("'pim' modifier applied to host");
1259 bpf_error("ATALK host filtering not implemented");
1262 bpf_error("AARP host filtering not implemented");
1265 return gen_dnhostop(addr
, dir
, off_nl
);
1268 bpf_error("SCA host filtering not implemented");
1271 bpf_error("LAT host filtering not implemented");
1274 bpf_error("MOPDL host filtering not implemented");
1277 bpf_error("MOPRC host filtering not implemented");
1281 bpf_error("'ip6' modifier applied to ip host");
1284 bpf_error("'icmp6' modifier applied to host");
1288 bpf_error("'ah' modifier applied to host");
1291 bpf_error("'esp' modifier applied to host");
1294 bpf_error("ISO host filtering not implemented");
1297 bpf_error("'esis' modifier applied to host");
1300 bpf_error("'isis' modifier applied to host");
1303 bpf_error("'clnp' modifier applied to host");
1312 static struct block
*
1313 gen_host6(addr
, mask
, proto
, dir
)
1314 struct in6_addr
*addr
;
1315 struct in6_addr
*mask
;
1322 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1325 bpf_error("'ip' modifier applied to ip6 host");
1328 bpf_error("'rarp' modifier applied to ip6 host");
1331 bpf_error("'arp' modifier applied to ip6 host");
1334 bpf_error("'tcp' modifier applied to host");
1337 bpf_error("'udp' modifier applied to host");
1340 bpf_error("'icmp' modifier applied to host");
1343 bpf_error("'igmp' modifier applied to host");
1346 bpf_error("'igrp' modifier applied to host");
1349 bpf_error("'pim' modifier applied to host");
1352 bpf_error("ATALK host filtering not implemented");
1355 bpf_error("AARP host filtering not implemented");
1358 bpf_error("'decnet' modifier applied to ip6 host");
1361 bpf_error("SCA host filtering not implemented");
1364 bpf_error("LAT host filtering not implemented");
1367 bpf_error("MOPDL host filtering not implemented");
1370 bpf_error("MOPRC host filtering not implemented");
1373 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1374 off_nl
+ 8, off_nl
+ 24);
1377 bpf_error("'icmp6' modifier applied to host");
1380 bpf_error("'ah' modifier applied to host");
1383 bpf_error("'esp' modifier applied to host");
1386 bpf_error("ISO host filtering not implemented");
1389 bpf_error("'esis' modifier applied to host");
1392 bpf_error("'isis' modifier applied to host");
1395 bpf_error("'clnp' modifier applied to host");
1405 static struct block
*
1406 gen_gateway(eaddr
, alist
, proto
, dir
)
1407 const u_char
*eaddr
;
1408 bpf_u_int32
**alist
;
1412 struct block
*b0
, *b1
, *tmp
;
1415 bpf_error("direction applied to 'gateway'");
1422 if (linktype
== DLT_EN10MB
)
1423 b0
= gen_ehostop(eaddr
, Q_OR
);
1424 else if (linktype
== DLT_FDDI
)
1425 b0
= gen_fhostop(eaddr
, Q_OR
);
1426 else if (linktype
== DLT_IEEE802
)
1427 b0
= gen_thostop(eaddr
, Q_OR
);
1430 "'gateway' supported only on ethernet, FDDI or token ring");
1432 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1434 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1442 bpf_error("illegal modifier of 'gateway'");
1448 gen_proto_abbrev(proto
)
1459 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1461 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1467 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1469 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1475 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1478 #ifndef IPPROTO_IGMP
1479 #define IPPROTO_IGMP 2
1483 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1486 #ifndef IPPROTO_IGRP
1487 #define IPPROTO_IGRP 9
1490 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1494 #define IPPROTO_PIM 103
1498 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1500 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1506 b1
= gen_linktype(ETHERTYPE_IP
);
1510 b1
= gen_linktype(ETHERTYPE_ARP
);
1514 b1
= gen_linktype(ETHERTYPE_REVARP
);
1518 bpf_error("link layer applied in wrong context");
1521 b1
= gen_linktype(ETHERTYPE_ATALK
);
1525 b1
= gen_linktype(ETHERTYPE_AARP
);
1529 b1
= gen_linktype(ETHERTYPE_DN
);
1533 b1
= gen_linktype(ETHERTYPE_SCA
);
1537 b1
= gen_linktype(ETHERTYPE_LAT
);
1541 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1545 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1550 b1
= gen_linktype(ETHERTYPE_IPV6
);
1553 #ifndef IPPROTO_ICMPV6
1554 #define IPPROTO_ICMPV6 58
1557 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1562 #define IPPROTO_AH 51
1565 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1567 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1573 #define IPPROTO_ESP 50
1576 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1578 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1584 b1
= gen_linktype(LLC_ISO_LSAP
);
1588 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
1592 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
1596 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
1605 static struct block
*
1612 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1613 s
->s
.k
= off_nl
+ 6;
1614 b
= new_block(JMP(BPF_JSET
));
1622 static struct block
*
1623 gen_portatom(off
, v
)
1630 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1633 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1634 s
->next
->s
.k
= off_nl
+ off
;
1636 b
= new_block(JMP(BPF_JEQ
));
1644 static struct block
*
1645 gen_portatom6(off
, v
)
1649 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1654 gen_portop(port
, proto
, dir
)
1655 int port
, proto
, dir
;
1657 struct block
*b0
, *b1
, *tmp
;
1659 /* ip proto 'proto' */
1660 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1666 b1
= gen_portatom(0, (bpf_int32
)port
);
1670 b1
= gen_portatom(2, (bpf_int32
)port
);
1675 tmp
= gen_portatom(0, (bpf_int32
)port
);
1676 b1
= gen_portatom(2, (bpf_int32
)port
);
1681 tmp
= gen_portatom(0, (bpf_int32
)port
);
1682 b1
= gen_portatom(2, (bpf_int32
)port
);
1694 static struct block
*
1695 gen_port(port
, ip_proto
, dir
)
1700 struct block
*b0
, *b1
, *tmp
;
1702 /* ether proto ip */
1703 b0
= gen_linktype(ETHERTYPE_IP
);
1708 b1
= gen_portop(port
, ip_proto
, dir
);
1712 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1713 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1726 gen_portop6(port
, proto
, dir
)
1727 int port
, proto
, dir
;
1729 struct block
*b0
, *b1
, *tmp
;
1731 /* ip proto 'proto' */
1732 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1736 b1
= gen_portatom6(0, (bpf_int32
)port
);
1740 b1
= gen_portatom6(2, (bpf_int32
)port
);
1745 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1746 b1
= gen_portatom6(2, (bpf_int32
)port
);
1751 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1752 b1
= gen_portatom6(2, (bpf_int32
)port
);
1764 static struct block
*
1765 gen_port6(port
, ip_proto
, dir
)
1770 struct block
*b0
, *b1
, *tmp
;
1772 /* ether proto ip */
1773 b0
= gen_linktype(ETHERTYPE_IPV6
);
1778 b1
= gen_portop6(port
, ip_proto
, dir
);
1782 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1783 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1796 lookup_proto(name
, proto
)
1797 register const char *name
;
1806 v
= pcap_nametoproto(name
);
1807 if (v
== PROTO_UNDEF
)
1808 bpf_error("unknown ip proto '%s'", name
);
1812 /* XXX should look up h/w protocol type based on linktype */
1813 v
= pcap_nametoeproto(name
);
1814 if (v
== PROTO_UNDEF
)
1815 bpf_error("unknown ether proto '%s'", name
);
1819 if (strcmp(name
, "esis") == 0)
1821 else if (strcmp(name
, "isis") == 0)
1823 else if (strcmp(name
, "clnp") == 0)
1826 bpf_error("unknown osi proto '%s'", name
);
1846 static struct block
*
1847 gen_protochain(v
, proto
, dir
)
1852 #ifdef NO_PROTOCHAIN
1853 return gen_proto(v
, proto
, dir
);
1855 struct block
*b0
, *b
;
1856 struct slist
*s
[100];
1857 int fix2
, fix3
, fix4
, fix5
;
1858 int ahcheck
, again
, end
;
1860 int reg1
= alloc_reg();
1861 int reg2
= alloc_reg();
1863 memset(s
, 0, sizeof(s
));
1864 fix2
= fix3
= fix4
= fix5
= 0;
1871 b0
= gen_protochain(v
, Q_IP
, dir
);
1872 b
= gen_protochain(v
, Q_IPV6
, dir
);
1876 bpf_error("bad protocol applied for 'protochain'");
1880 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1883 * s[0] is a dummy entry to protect other BPF insn from damaged
1884 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1885 * hard to find interdependency made by jump table fixup.
1888 s
[i
] = new_stmt(0); /*dummy*/
1893 b0
= gen_linktype(ETHERTYPE_IP
);
1896 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1897 s
[i
]->s
.k
= off_nl
+ 9;
1899 /* X = ip->ip_hl << 2 */
1900 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1906 b0
= gen_linktype(ETHERTYPE_IPV6
);
1908 /* A = ip6->ip_nxt */
1909 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1910 s
[i
]->s
.k
= off_nl
+ 6;
1912 /* X = sizeof(struct ip6_hdr) */
1913 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1919 bpf_error("unsupported proto to gen_protochain");
1923 /* again: if (A == v) goto end; else fall through; */
1925 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1927 s
[i
]->s
.jt
= NULL
; /*later*/
1928 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1932 #ifndef IPPROTO_NONE
1933 #define IPPROTO_NONE 59
1935 /* if (A == IPPROTO_NONE) goto end */
1936 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1937 s
[i
]->s
.jt
= NULL
; /*later*/
1938 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1939 s
[i
]->s
.k
= IPPROTO_NONE
;
1940 s
[fix5
]->s
.jf
= s
[i
];
1945 if (proto
== Q_IPV6
) {
1946 int v6start
, v6end
, v6advance
, j
;
1949 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1950 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1951 s
[i
]->s
.jt
= NULL
; /*later*/
1952 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1953 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
1954 s
[fix2
]->s
.jf
= s
[i
];
1956 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1957 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1958 s
[i
]->s
.jt
= NULL
; /*later*/
1959 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1960 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
1962 /* if (A == IPPROTO_ROUTING) goto v6advance */
1963 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1964 s
[i
]->s
.jt
= NULL
; /*later*/
1965 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1966 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1968 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1969 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1970 s
[i
]->s
.jt
= NULL
; /*later*/
1971 s
[i
]->s
.jf
= NULL
; /*later*/
1972 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
1983 * X = X + (P[X] + 1) * 8;
1986 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1989 s
[i
] = new_stmt(BPF_ST
);
1993 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1997 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1999 /* A = P[X + packet head]; */
2000 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2004 s
[i
] = new_stmt(BPF_ST
);
2008 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
2011 /* A = P[X + packet head] */
2012 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2016 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2020 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2024 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2027 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2031 /* goto again; (must use BPF_JA for backward jump) */
2032 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2033 s
[i
]->s
.k
= again
- i
- 1;
2034 s
[i
- 1]->s
.jf
= s
[i
];
2038 for (j
= v6start
; j
<= v6end
; j
++)
2039 s
[j
]->s
.jt
= s
[v6advance
];
2044 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2046 s
[fix2
]->s
.jf
= s
[i
];
2052 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2053 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2054 s
[i
]->s
.jt
= NULL
; /*later*/
2055 s
[i
]->s
.jf
= NULL
; /*later*/
2056 s
[i
]->s
.k
= IPPROTO_AH
;
2058 s
[fix3
]->s
.jf
= s
[ahcheck
];
2065 * X = X + (P[X] + 2) * 4;
2068 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2071 s
[i
] = new_stmt(BPF_ST
);
2075 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2079 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2081 /* A = P[X + packet head]; */
2082 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2086 s
[i
] = new_stmt(BPF_ST
);
2090 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
2093 /* A = P[X + packet head] */
2094 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2098 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2102 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2106 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2109 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2113 /* goto again; (must use BPF_JA for backward jump) */
2114 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2115 s
[i
]->s
.k
= again
- i
- 1;
2120 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2122 s
[fix2
]->s
.jt
= s
[end
];
2123 s
[fix4
]->s
.jf
= s
[end
];
2124 s
[fix5
]->s
.jt
= s
[end
];
2131 for (i
= 0; i
< max
- 1; i
++)
2132 s
[i
]->next
= s
[i
+ 1];
2133 s
[max
- 1]->next
= NULL
;
2138 b
= new_block(JMP(BPF_JEQ
));
2139 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2150 static struct block
*
2151 gen_proto(v
, proto
, dir
)
2156 struct block
*b0
, *b1
;
2158 if (dir
!= Q_DEFAULT
)
2159 bpf_error("direction applied to 'proto'");
2164 b0
= gen_proto(v
, Q_IP
, dir
);
2165 b1
= gen_proto(v
, Q_IPV6
, dir
);
2172 b0
= gen_linktype(ETHERTYPE_IP
);
2174 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2176 b1
= gen_protochain(v
, Q_IP
);
2182 b0
= gen_linktype(LLC_ISO_LSAP
);
2183 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2188 bpf_error("arp does not encapsulate another protocol");
2192 bpf_error("rarp does not encapsulate another protocol");
2196 bpf_error("atalk encapsulation is not specifiable");
2200 bpf_error("decnet encapsulation is not specifiable");
2204 bpf_error("sca does not encapsulate another protocol");
2208 bpf_error("lat does not encapsulate another protocol");
2212 bpf_error("moprc does not encapsulate another protocol");
2216 bpf_error("mopdl does not encapsulate another protocol");
2220 return gen_linktype(v
);
2223 bpf_error("'udp proto' is bogus");
2227 bpf_error("'tcp proto' is bogus");
2231 bpf_error("'icmp proto' is bogus");
2235 bpf_error("'igmp proto' is bogus");
2239 bpf_error("'igrp proto' is bogus");
2243 bpf_error("'pim proto' is bogus");
2248 b0
= gen_linktype(ETHERTYPE_IPV6
);
2250 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2252 b1
= gen_protochain(v
, Q_IPV6
);
2258 bpf_error("'icmp6 proto' is bogus");
2262 bpf_error("'ah proto' is bogus");
2265 bpf_error("'ah proto' is bogus");
2276 register const char *name
;
2279 int proto
= q
.proto
;
2283 bpf_u_int32 mask
, addr
;
2285 bpf_u_int32
**alist
;
2288 struct sockaddr_in
*sin
;
2289 struct sockaddr_in6
*sin6
;
2290 struct addrinfo
*res
, *res0
;
2291 struct in6_addr mask128
;
2293 struct block
*b
, *tmp
;
2294 int port
, real_proto
;
2299 addr
= pcap_nametonetaddr(name
);
2301 bpf_error("unknown network '%s'", name
);
2302 /* Left justify network addr and calculate its network mask */
2304 while (addr
&& (addr
& 0xff000000) == 0) {
2308 return gen_host(addr
, mask
, proto
, dir
);
2312 if (proto
== Q_LINK
) {
2316 eaddr
= pcap_ether_hostton(name
);
2319 "unknown ether host '%s'", name
);
2320 return gen_ehostop(eaddr
, dir
);
2323 eaddr
= pcap_ether_hostton(name
);
2326 "unknown FDDI host '%s'", name
);
2327 return gen_fhostop(eaddr
, dir
);
2330 eaddr
= pcap_ether_hostton(name
);
2333 "unknown token ring host '%s'", name
);
2334 return gen_thostop(eaddr
, dir
);
2338 "only ethernet/FDDI/token ring supports link-level host name");
2341 } else if (proto
== Q_DECNET
) {
2342 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2344 * I don't think DECNET hosts can be multihomed, so
2345 * there is no need to build up a list of addresses
2347 return (gen_host(dn_addr
, 0, proto
, dir
));
2350 alist
= pcap_nametoaddr(name
);
2351 if (alist
== NULL
|| *alist
== NULL
)
2352 bpf_error("unknown host '%s'", name
);
2354 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2356 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2358 tmp
= gen_host(**alist
++, 0xffffffff,
2365 memset(&mask128
, 0xff, sizeof(mask128
));
2366 res0
= res
= pcap_nametoaddrinfo(name
);
2368 bpf_error("unknown host '%s'", name
);
2370 tproto
= tproto6
= proto
;
2371 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2375 for (res
= res0
; res
; res
= res
->ai_next
) {
2376 switch (res
->ai_family
) {
2378 if (tproto
== Q_IPV6
)
2381 sin
= (struct sockaddr_in
*)
2383 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2384 0xffffffff, tproto
, dir
);
2387 if (tproto6
== Q_IP
)
2390 sin6
= (struct sockaddr_in6
*)
2392 tmp
= gen_host6(&sin6
->sin6_addr
,
2393 &mask128
, tproto6
, dir
);
2402 bpf_error("unknown host '%s'%s", name
,
2403 (proto
== Q_DEFAULT
)
2405 : " for specified address family");
2412 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2413 bpf_error("illegal qualifier of 'port'");
2414 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2415 bpf_error("unknown port '%s'", name
);
2416 if (proto
== Q_UDP
) {
2417 if (real_proto
== IPPROTO_TCP
)
2418 bpf_error("port '%s' is tcp", name
);
2420 /* override PROTO_UNDEF */
2421 real_proto
= IPPROTO_UDP
;
2423 if (proto
== Q_TCP
) {
2424 if (real_proto
== IPPROTO_UDP
)
2425 bpf_error("port '%s' is udp", name
);
2427 /* override PROTO_UNDEF */
2428 real_proto
= IPPROTO_TCP
;
2431 return gen_port(port
, real_proto
, dir
);
2435 b
= gen_port(port
, real_proto
, dir
);
2436 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2443 eaddr
= pcap_ether_hostton(name
);
2445 bpf_error("unknown ether host: %s", name
);
2447 alist
= pcap_nametoaddr(name
);
2448 if (alist
== NULL
|| *alist
== NULL
)
2449 bpf_error("unknown host '%s'", name
);
2450 return gen_gateway(eaddr
, alist
, proto
, dir
);
2452 bpf_error("'gateway' not supported in this configuration");
2456 real_proto
= lookup_proto(name
, proto
);
2457 if (real_proto
>= 0)
2458 return gen_proto(real_proto
, proto
, dir
);
2460 bpf_error("unknown protocol: %s", name
);
2463 real_proto
= lookup_proto(name
, proto
);
2464 if (real_proto
>= 0)
2465 return gen_protochain(real_proto
, proto
, dir
);
2467 bpf_error("unknown protocol: %s", name
);
2479 gen_mcode(s1
, s2
, masklen
, q
)
2480 register const char *s1
, *s2
;
2481 register int masklen
;
2484 register int nlen
, mlen
;
2487 nlen
= __pcap_atoin(s1
, &n
);
2488 /* Promote short ipaddr */
2492 mlen
= __pcap_atoin(s2
, &m
);
2493 /* Promote short ipaddr */
2496 bpf_error("non-network bits set in \"%s mask %s\"",
2499 /* Convert mask len to mask */
2501 bpf_error("mask length must be <= 32");
2502 m
= 0xffffffff << (32 - masklen
);
2504 bpf_error("non-network bits set in \"%s/%d\"",
2511 return gen_host(n
, m
, q
.proto
, q
.dir
);
2514 bpf_error("Mask syntax for networks only");
2521 register const char *s
;
2526 int proto
= q
.proto
;
2532 else if (q
.proto
== Q_DECNET
)
2533 vlen
= __pcap_atodn(s
, &v
);
2535 vlen
= __pcap_atoin(s
, &v
);
2542 if (proto
== Q_DECNET
)
2543 return gen_host(v
, 0, proto
, dir
);
2544 else if (proto
== Q_LINK
) {
2545 bpf_error("illegal link layer address");
2548 if (s
== NULL
&& q
.addr
== Q_NET
) {
2549 /* Promote short net number */
2550 while (v
&& (v
& 0xff000000) == 0) {
2555 /* Promote short ipaddr */
2559 return gen_host(v
, mask
, proto
, dir
);
2564 proto
= IPPROTO_UDP
;
2565 else if (proto
== Q_TCP
)
2566 proto
= IPPROTO_TCP
;
2567 else if (proto
== Q_DEFAULT
)
2568 proto
= PROTO_UNDEF
;
2570 bpf_error("illegal qualifier of 'port'");
2573 return gen_port((int)v
, proto
, dir
);
2577 b
= gen_port((int)v
, proto
, dir
);
2578 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2584 bpf_error("'gateway' requires a name");
2588 return gen_proto((int)v
, proto
, dir
);
2591 return gen_protochain((int)v
, proto
, dir
);
2606 gen_mcode6(s1
, s2
, masklen
, q
)
2607 register const char *s1
, *s2
;
2608 register int masklen
;
2611 struct addrinfo
*res
;
2612 struct in6_addr
*addr
;
2613 struct in6_addr mask
;
2618 bpf_error("no mask %s supported", s2
);
2620 res
= pcap_nametoaddrinfo(s1
);
2622 bpf_error("invalid ip6 address %s", s1
);
2624 bpf_error("%s resolved to multiple address", s1
);
2625 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2627 if (sizeof(mask
) * 8 < masklen
)
2628 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2629 memset(&mask
, 0xff, masklen
/ 8);
2631 mask
.s6_addr
[masklen
/ 8] =
2632 (0xff << (8 - masklen
% 8)) & 0xff;
2635 a
= (u_int32_t
*)addr
;
2636 m
= (u_int32_t
*)&mask
;
2637 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2638 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2639 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2647 bpf_error("Mask syntax for networks only");
2651 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2656 bpf_error("invalid qualifier against IPv6 address");
2664 register const u_char
*eaddr
;
2667 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2668 if (linktype
== DLT_EN10MB
)
2669 return gen_ehostop(eaddr
, (int)q
.dir
);
2670 if (linktype
== DLT_FDDI
)
2671 return gen_fhostop(eaddr
, (int)q
.dir
);
2672 if (linktype
== DLT_IEEE802
)
2673 return gen_thostop(eaddr
, (int)q
.dir
);
2675 bpf_error("ethernet address used in non-ether expression");
2681 struct slist
*s0
, *s1
;
2684 * This is definitely not the best way to do this, but the
2685 * lists will rarely get long.
2692 static struct slist
*
2698 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2703 static struct slist
*
2709 s
= new_stmt(BPF_LD
|BPF_MEM
);
2715 gen_load(proto
, index
, size
)
2720 struct slist
*s
, *tmp
;
2722 int regno
= alloc_reg();
2724 free_reg(index
->regno
);
2728 bpf_error("data size must be 1, 2, or 4");
2744 bpf_error("unsupported index operation");
2747 s
= xfer_to_x(index
);
2748 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2750 sappend(index
->s
, s
);
2765 /* XXX Note that we assume a fixed link header here. */
2766 s
= xfer_to_x(index
);
2767 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2770 sappend(index
->s
, s
);
2772 b
= gen_proto_abbrev(proto
);
2774 gen_and(index
->b
, b
);
2784 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2786 sappend(s
, xfer_to_a(index
));
2787 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2788 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2789 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2791 sappend(index
->s
, s
);
2793 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2795 gen_and(index
->b
, b
);
2797 gen_and(gen_proto_abbrev(Q_IP
), b
);
2803 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2807 index
->regno
= regno
;
2808 s
= new_stmt(BPF_ST
);
2810 sappend(index
->s
, s
);
2816 gen_relation(code
, a0
, a1
, reversed
)
2818 struct arth
*a0
, *a1
;
2821 struct slist
*s0
, *s1
, *s2
;
2822 struct block
*b
, *tmp
;
2826 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2827 b
= new_block(JMP(code
));
2828 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2829 reversed
= !reversed
;
2830 b
->s
.k
= 0x80000000;
2838 sappend(a0
->s
, a1
->s
);
2842 free_reg(a0
->regno
);
2843 free_reg(a1
->regno
);
2845 /* 'and' together protocol checks */
2848 gen_and(a0
->b
, tmp
= a1
->b
);
2864 int regno
= alloc_reg();
2865 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2868 s
= new_stmt(BPF_LD
|BPF_LEN
);
2869 s
->next
= new_stmt(BPF_ST
);
2870 s
->next
->s
.k
= regno
;
2885 a
= (struct arth
*)newchunk(sizeof(*a
));
2889 s
= new_stmt(BPF_LD
|BPF_IMM
);
2891 s
->next
= new_stmt(BPF_ST
);
2907 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2910 s
= new_stmt(BPF_ST
);
2918 gen_arth(code
, a0
, a1
)
2920 struct arth
*a0
, *a1
;
2922 struct slist
*s0
, *s1
, *s2
;
2926 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2931 sappend(a0
->s
, a1
->s
);
2933 free_reg(a1
->regno
);
2935 s0
= new_stmt(BPF_ST
);
2936 a0
->regno
= s0
->s
.k
= alloc_reg();
2943 * Here we handle simple allocation of the scratch registers.
2944 * If too many registers are alloc'd, the allocator punts.
2946 static int regused
[BPF_MEMWORDS
];
2950 * Return the next free register.
2955 int n
= BPF_MEMWORDS
;
2958 if (regused
[curreg
])
2959 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2961 regused
[curreg
] = 1;
2965 bpf_error("too many registers needed to evaluate expression");
2970 * Return a register to the table so it can
2980 static struct block
*
2987 s
= new_stmt(BPF_LD
|BPF_LEN
);
2988 b
= new_block(JMP(jmp
));
2999 return gen_len(BPF_JGE
, n
);
3003 * Actually, this is less than or equal.
3011 b
= gen_len(BPF_JGT
, n
);
3018 gen_byteop(op
, idx
, val
)
3029 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3032 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3033 b
->s
.code
= JMP(BPF_JGE
);
3038 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3039 b
->s
.code
= JMP(BPF_JGT
);
3043 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3047 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3051 b
= new_block(JMP(BPF_JEQ
));
3059 gen_broadcast(proto
)
3062 bpf_u_int32 hostmask
;
3063 struct block
*b0
, *b1
, *b2
;
3064 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3070 if (linktype
== DLT_EN10MB
)
3071 return gen_ehostop(ebroadcast
, Q_DST
);
3072 if (linktype
== DLT_FDDI
)
3073 return gen_fhostop(ebroadcast
, Q_DST
);
3074 if (linktype
== DLT_IEEE802
)
3075 return gen_thostop(ebroadcast
, Q_DST
);
3076 bpf_error("not a broadcast link");
3080 b0
= gen_linktype(ETHERTYPE_IP
);
3081 hostmask
= ~netmask
;
3082 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3083 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3084 (bpf_int32
)(~0 & hostmask
), hostmask
);
3089 bpf_error("only ether/ip broadcast filters supported");
3093 gen_multicast(proto
)
3096 register struct block
*b0
, *b1
;
3097 register struct slist
*s
;
3103 if (linktype
== DLT_EN10MB
) {
3104 /* ether[0] & 1 != 0 */
3105 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3107 b0
= new_block(JMP(BPF_JSET
));
3113 if (linktype
== DLT_FDDI
) {
3114 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3115 /* fddi[1] & 1 != 0 */
3116 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3118 b0
= new_block(JMP(BPF_JSET
));
3124 /* TODO - check how token ring handles multicast */
3125 /* if (linktype == DLT_IEEE802) ... */
3127 /* Link not known to support multicasts */
3131 b0
= gen_linktype(ETHERTYPE_IP
);
3132 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3133 b1
->s
.code
= JMP(BPF_JGE
);
3139 b0
= gen_linktype(ETHERTYPE_IPV6
);
3140 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3145 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3149 * generate command for inbound/outbound. It's here so we can
3150 * make it link-type specific. 'dir' = 0 implies "inbound",
3151 * = 1 implies "outbound".
3157 register struct block
*b0
;
3159 b0
= gen_relation(BPF_JEQ
,
3160 gen_load(Q_LINK
, gen_loadi(0), 1),
3167 * support IEEE 802.1Q VLAN trunk over ethernet
3173 static u_int orig_linktype
= -1, orig_nl
= -1;
3177 * Change the offsets to point to the type and data fields within
3178 * the VLAN packet. This is somewhat of a kludge.
3180 if (orig_nl
== (u_int
)-1) {
3181 orig_linktype
= off_linktype
; /* save original values */
3192 bpf_error("no VLAN support for data link type %d",
3198 /* check for VLAN */
3199 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3201 /* If a specific VLAN is requested, check VLAN id */
3202 if (vlan_num
>= 0) {
3205 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);