]>
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.137 2000-12-12 03:26:16 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
)
347 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
350 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
356 * Clean up a "struct bpf_program" by freeing all the memory allocated
360 pcap_freecode(struct bpf_program
*program
)
363 if (program
->bf_insns
!= NULL
) {
364 free((char *)program
->bf_insns
);
365 program
->bf_insns
= NULL
;
370 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
371 * which of the jt and jf fields has been resolved and which is a pointer
372 * back to another unresolved block (or nil). At least one of the fields
373 * in each block is already resolved.
376 backpatch(list
, target
)
377 struct block
*list
, *target
;
394 * Merge the lists in b0 and b1, using the 'sense' field to indicate
395 * which of jt and jf is the link.
399 struct block
*b0
, *b1
;
401 register struct block
**p
= &b0
;
403 /* Find end of list. */
405 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
407 /* Concatenate the lists. */
415 backpatch(p
, gen_retblk(snaplen
));
416 p
->sense
= !p
->sense
;
417 backpatch(p
, gen_retblk(0));
423 struct block
*b0
, *b1
;
425 backpatch(b0
, b1
->head
);
426 b0
->sense
= !b0
->sense
;
427 b1
->sense
= !b1
->sense
;
429 b1
->sense
= !b1
->sense
;
435 struct block
*b0
, *b1
;
437 b0
->sense
= !b0
->sense
;
438 backpatch(b0
, b1
->head
);
439 b0
->sense
= !b0
->sense
;
448 b
->sense
= !b
->sense
;
451 static struct block
*
452 gen_cmp(offset
, size
, v
)
459 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
462 b
= new_block(JMP(BPF_JEQ
));
469 static struct block
*
470 gen_cmp_gt(offset
, size
, v
)
477 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
480 b
= new_block(JMP(BPF_JGT
));
487 static struct block
*
488 gen_mcmp(offset
, size
, v
, mask
)
493 struct block
*b
= gen_cmp(offset
, size
, v
);
496 if (mask
!= 0xffffffff) {
497 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
504 static struct block
*
505 gen_bcmp(offset
, size
, v
)
506 register u_int offset
, size
;
507 register const u_char
*v
;
509 register struct block
*b
, *tmp
;
513 register const u_char
*p
= &v
[size
- 4];
514 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
515 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
517 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
524 register const u_char
*p
= &v
[size
- 2];
525 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
527 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
534 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
543 * Various code constructs need to know the layout of the data link
544 * layer. These variables give the necessary offsets. off_linktype
545 * is set to -1 for no encapsulation, in which case, IP is assumed.
547 static u_int off_linktype
;
566 * SLIP doesn't have a link level type. The 16 byte
567 * header is hacked into our SLIP driver.
574 /* XXX this may be the same as the DLT_PPP_BSDOS case */
599 * FDDI doesn't really have a link-level type field.
600 * We assume that SSAP = SNAP is being used and pick
601 * out the encapsulated Ethernet type.
603 * XXX - should we generate code to check for SNAP?
607 off_linktype
+= pcap_fddipad
;
611 off_nl
+= pcap_fddipad
;
617 * Token Ring doesn't really have a link-level type field.
618 * We assume that SSAP = SNAP is being used and pick
619 * out the encapsulated Ethernet type.
621 * XXX - should we generate code to check for SNAP?
623 * XXX - the header is actually variable-length.
624 * Some various Linux patched versions gave 38
625 * as "off_linktype" and 40 as "off_nl"; however,
626 * if a token ring packet has *no* routing
627 * information, i.e. is not source-routed, the correct
628 * values are 20 and 22, as they are in the vanilla code.
630 * A packet is source-routed iff the uppermost bit
631 * of the first byte of the source address, at an
632 * offset of 8, has the uppermost bit set. If the
633 * packet is source-routed, the total number of bytes
634 * of routing information is 2 plus bits 0x1F00 of
635 * the 16-bit value at an offset of 14 (shifted right
636 * 8 - figure out which byte that is).
642 case DLT_ATM_RFC1483
:
644 * assume routed, non-ISO PDUs
645 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
656 case DLT_ATM_CLIP
: /* Linux ATM defines this */
661 bpf_error("unknown data link type %d", linktype
);
665 static struct block
*
672 s
= new_stmt(BPF_LD
|BPF_IMM
);
674 b
= new_block(JMP(BPF_JEQ
));
680 static inline struct block
*
683 return gen_uncond(1);
686 static inline struct block
*
689 return gen_uncond(0);
692 static struct block
*
696 struct block
*b0
, *b1
;
698 /* If we're not using encapsulation, we're done */
699 if (off_linktype
== -1)
706 * XXX - handle other LLC-encapsulated protocols here
713 * OSI protocols always use 802.2 encapsulation.
715 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
717 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
718 ((LLC_ISO_LSAP
<< 8) | LLC_ISO_LSAP
));
722 case ETHERTYPE_ATALK
:
725 * EtherTalk (AppleTalk protocols on Ethernet link
726 * layer) may use 802.2 encapsulation.
730 * Check for 802.2 encapsulation (EtherTalk phase 2?);
731 * we check for an Ethernet type field less than
732 * 1500, which means it's an 802.3 length field.
734 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
738 * 802.2-encapsulated ETHERTYPE_ATALK packets are
739 * SNAP packets with an organization code of
740 * 0x080007 (Apple, for Appletalk) and a protocol
741 * type of ETHERTYPE_ATALK (Appletalk).
743 * 802.2-encapsulated ETHERTYPE_AARP packets are
744 * SNAP packets with an organization code of
745 * 0x000000 (encapsulated Ethernet) and a protocol
746 * type of ETHERTYPE_AARP (Appletalk ARP).
748 if (proto
== ETHERTYPE_ATALK
)
749 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
750 else /* proto == ETHERTYPE_AARP */
751 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
755 * Check for Ethernet encapsulation (Ethertalk
756 * phase 1?); we just check for the Ethernet
759 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
772 * We use Ethernet protocol types inside libpcap;
773 * map them to the corresponding PPP protocol types.
778 proto
= PPP_IP
; /* XXX was 0x21 */
791 case ETHERTYPE_ATALK
:
807 * We use Ethernet protocol types inside libpcap;
808 * map them to the corresponding PPP protocol types.
813 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
814 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
816 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
831 case ETHERTYPE_ATALK
:
847 if (proto
== ETHERTYPE_IP
)
848 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET
)));
850 else if (proto
== ETHERTYPE_IPV6
)
851 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET6
)));
856 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
860 * Check for an LLC SNAP packet with a given organization code and
861 * protocol type; we check the entire contents of the 802.2 LLC and
862 * snap headers, checking for DSAP and SSAP of SNAP and a control
863 * field of 0x03 in the LLC header, and for the specified organization
864 * code and protocol type in the SNAP header.
866 static struct block
*
867 gen_snap(orgcode
, ptype
, offset
)
874 snapblock
[0] = LLC_SNAP_LSAP
; /* DSAP = SNAP */
875 snapblock
[1] = LLC_SNAP_LSAP
; /* SSAP = SNAP */
876 snapblock
[2] = 0x03; /* control = UI */
877 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
878 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
879 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
880 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
881 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
882 return gen_bcmp(offset
, 8, snapblock
);
885 static struct block
*
886 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
890 u_int src_off
, dst_off
;
892 struct block
*b0
, *b1
;
906 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
907 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
913 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
914 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
921 b0
= gen_linktype(proto
);
922 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
928 static struct block
*
929 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
930 struct in6_addr
*addr
;
931 struct in6_addr
*mask
;
933 u_int src_off
, dst_off
;
935 struct block
*b0
, *b1
;
950 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
951 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
957 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
958 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
965 /* this order is important */
966 a
= (u_int32_t
*)addr
;
967 m
= (u_int32_t
*)mask
;
968 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
969 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
971 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
973 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
975 b0
= gen_linktype(proto
);
981 static struct block
*
982 gen_ehostop(eaddr
, dir
)
983 register const u_char
*eaddr
;
986 register struct block
*b0
, *b1
;
990 return gen_bcmp(6, 6, eaddr
);
993 return gen_bcmp(0, 6, eaddr
);
996 b0
= gen_ehostop(eaddr
, Q_SRC
);
997 b1
= gen_ehostop(eaddr
, Q_DST
);
1003 b0
= gen_ehostop(eaddr
, Q_SRC
);
1004 b1
= gen_ehostop(eaddr
, Q_DST
);
1013 * Like gen_ehostop, but for DLT_FDDI
1015 static struct block
*
1016 gen_fhostop(eaddr
, dir
)
1017 register const u_char
*eaddr
;
1020 struct block
*b0
, *b1
;
1025 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1027 return gen_bcmp(6 + 1, 6, eaddr
);
1032 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1034 return gen_bcmp(0 + 1, 6, eaddr
);
1038 b0
= gen_fhostop(eaddr
, Q_SRC
);
1039 b1
= gen_fhostop(eaddr
, Q_DST
);
1045 b0
= gen_fhostop(eaddr
, Q_SRC
);
1046 b1
= gen_fhostop(eaddr
, Q_DST
);
1055 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1057 static struct block
*
1058 gen_thostop(eaddr
, dir
)
1059 register const u_char
*eaddr
;
1062 register struct block
*b0
, *b1
;
1066 return gen_bcmp(8, 6, eaddr
);
1069 return gen_bcmp(2, 6, eaddr
);
1072 b0
= gen_thostop(eaddr
, Q_SRC
);
1073 b1
= gen_thostop(eaddr
, Q_DST
);
1079 b0
= gen_thostop(eaddr
, Q_SRC
);
1080 b1
= gen_thostop(eaddr
, Q_DST
);
1089 * This is quite tricky because there may be pad bytes in front of the
1090 * DECNET header, and then there are two possible data packet formats that
1091 * carry both src and dst addresses, plus 5 packet types in a format that
1092 * carries only the src node, plus 2 types that use a different format and
1093 * also carry just the src node.
1097 * Instead of doing those all right, we just look for data packets with
1098 * 0 or 1 bytes of padding. If you want to look at other packets, that
1099 * will require a lot more hacking.
1101 * To add support for filtering on DECNET "areas" (network numbers)
1102 * one would want to add a "mask" argument to this routine. That would
1103 * make the filter even more inefficient, although one could be clever
1104 * and not generate masking instructions if the mask is 0xFFFF.
1106 static struct block
*
1107 gen_dnhostop(addr
, dir
, base_off
)
1112 struct block
*b0
, *b1
, *b2
, *tmp
;
1113 u_int offset_lh
; /* offset if long header is received */
1114 u_int offset_sh
; /* offset if short header is received */
1119 offset_sh
= 1; /* follows flags */
1120 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1124 offset_sh
= 3; /* follows flags, dstnode */
1125 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1129 /* Inefficient because we do our Calvinball dance twice */
1130 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1131 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1137 /* Inefficient because we do our Calvinball dance twice */
1138 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1139 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1144 bpf_error("ISO host filtering not implemented");
1149 b0
= gen_linktype(ETHERTYPE_DN
);
1150 /* Check for pad = 1, long header case */
1151 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1152 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1153 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1154 BPF_H
, (bpf_int32
)ntohs(addr
));
1156 /* Check for pad = 0, long header case */
1157 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1158 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1161 /* Check for pad = 1, short header case */
1162 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1163 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1164 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1165 BPF_H
, (bpf_int32
)ntohs(addr
));
1168 /* Check for pad = 0, short header case */
1169 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1170 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1174 /* Combine with test for linktype */
1179 static struct block
*
1180 gen_host(addr
, mask
, proto
, dir
)
1186 struct block
*b0
, *b1
;
1191 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1192 if (off_linktype
!= -1) {
1193 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1195 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1201 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1202 off_nl
+ 12, off_nl
+ 16);
1205 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1206 off_nl
+ 14, off_nl
+ 24);
1209 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1210 off_nl
+ 14, off_nl
+ 24);
1213 bpf_error("'tcp' modifier applied to host");
1216 bpf_error("'udp' modifier applied to host");
1219 bpf_error("'icmp' modifier applied to host");
1222 bpf_error("'igmp' modifier applied to host");
1225 bpf_error("'igrp' modifier applied to host");
1228 bpf_error("'pim' modifier applied to host");
1231 bpf_error("ATALK host filtering not implemented");
1234 bpf_error("AARP host filtering not implemented");
1237 return gen_dnhostop(addr
, dir
, off_nl
);
1240 bpf_error("SCA host filtering not implemented");
1243 bpf_error("LAT host filtering not implemented");
1246 bpf_error("MOPDL host filtering not implemented");
1249 bpf_error("MOPRC host filtering not implemented");
1253 bpf_error("'ip6' modifier applied to ip host");
1256 bpf_error("'icmp6' modifier applied to host");
1260 bpf_error("'ah' modifier applied to host");
1263 bpf_error("'esp' modifier applied to host");
1266 bpf_error("ISO host filtering not implemented");
1269 bpf_error("'esis' modifier applied to host");
1272 bpf_error("'isis' modifier applied to host");
1275 bpf_error("'clnp' modifier applied to host");
1284 static struct block
*
1285 gen_host6(addr
, mask
, proto
, dir
)
1286 struct in6_addr
*addr
;
1287 struct in6_addr
*mask
;
1294 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1297 bpf_error("'ip' modifier applied to ip6 host");
1300 bpf_error("'rarp' modifier applied to ip6 host");
1303 bpf_error("'arp' modifier applied to ip6 host");
1306 bpf_error("'tcp' modifier applied to host");
1309 bpf_error("'udp' modifier applied to host");
1312 bpf_error("'icmp' modifier applied to host");
1315 bpf_error("'igmp' modifier applied to host");
1318 bpf_error("'igrp' modifier applied to host");
1321 bpf_error("'pim' modifier applied to host");
1324 bpf_error("ATALK host filtering not implemented");
1327 bpf_error("AARP host filtering not implemented");
1330 bpf_error("'decnet' modifier applied to ip6 host");
1333 bpf_error("SCA host filtering not implemented");
1336 bpf_error("LAT host filtering not implemented");
1339 bpf_error("MOPDL host filtering not implemented");
1342 bpf_error("MOPRC host filtering not implemented");
1345 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1346 off_nl
+ 8, off_nl
+ 24);
1349 bpf_error("'icmp6' modifier applied to host");
1352 bpf_error("'ah' modifier applied to host");
1355 bpf_error("'esp' modifier applied to host");
1358 bpf_error("ISO host filtering not implemented");
1361 bpf_error("'esis' modifier applied to host");
1364 bpf_error("'isis' modifier applied to host");
1367 bpf_error("'clnp' modifier applied to host");
1377 static struct block
*
1378 gen_gateway(eaddr
, alist
, proto
, dir
)
1379 const u_char
*eaddr
;
1380 bpf_u_int32
**alist
;
1384 struct block
*b0
, *b1
, *tmp
;
1387 bpf_error("direction applied to 'gateway'");
1394 if (linktype
== DLT_EN10MB
)
1395 b0
= gen_ehostop(eaddr
, Q_OR
);
1396 else if (linktype
== DLT_FDDI
)
1397 b0
= gen_fhostop(eaddr
, Q_OR
);
1398 else if (linktype
== DLT_IEEE802
)
1399 b0
= gen_thostop(eaddr
, Q_OR
);
1402 "'gateway' supported only on ethernet, FDDI or token ring");
1404 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1406 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1414 bpf_error("illegal modifier of 'gateway'");
1420 gen_proto_abbrev(proto
)
1431 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1433 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1439 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1441 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1447 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1450 #ifndef IPPROTO_IGMP
1451 #define IPPROTO_IGMP 2
1455 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1458 #ifndef IPPROTO_IGRP
1459 #define IPPROTO_IGRP 9
1462 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1466 #define IPPROTO_PIM 103
1470 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1472 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1478 b1
= gen_linktype(ETHERTYPE_IP
);
1482 b1
= gen_linktype(ETHERTYPE_ARP
);
1486 b1
= gen_linktype(ETHERTYPE_REVARP
);
1490 bpf_error("link layer applied in wrong context");
1493 b1
= gen_linktype(ETHERTYPE_ATALK
);
1497 b1
= gen_linktype(ETHERTYPE_AARP
);
1501 b1
= gen_linktype(ETHERTYPE_DN
);
1505 b1
= gen_linktype(ETHERTYPE_SCA
);
1509 b1
= gen_linktype(ETHERTYPE_LAT
);
1513 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1517 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1522 b1
= gen_linktype(ETHERTYPE_IPV6
);
1525 #ifndef IPPROTO_ICMPV6
1526 #define IPPROTO_ICMPV6 58
1529 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1534 #define IPPROTO_AH 51
1537 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1539 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1545 #define IPPROTO_ESP 50
1548 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1550 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1556 b1
= gen_linktype(LLC_ISO_LSAP
);
1560 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
1564 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
1568 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
1577 static struct block
*
1584 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1585 s
->s
.k
= off_nl
+ 6;
1586 b
= new_block(JMP(BPF_JSET
));
1594 static struct block
*
1595 gen_portatom(off
, v
)
1602 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1605 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1606 s
->next
->s
.k
= off_nl
+ off
;
1608 b
= new_block(JMP(BPF_JEQ
));
1616 static struct block
*
1617 gen_portatom6(off
, v
)
1621 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1626 gen_portop(port
, proto
, dir
)
1627 int port
, proto
, dir
;
1629 struct block
*b0
, *b1
, *tmp
;
1631 /* ip proto 'proto' */
1632 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1638 b1
= gen_portatom(0, (bpf_int32
)port
);
1642 b1
= gen_portatom(2, (bpf_int32
)port
);
1647 tmp
= gen_portatom(0, (bpf_int32
)port
);
1648 b1
= gen_portatom(2, (bpf_int32
)port
);
1653 tmp
= gen_portatom(0, (bpf_int32
)port
);
1654 b1
= gen_portatom(2, (bpf_int32
)port
);
1666 static struct block
*
1667 gen_port(port
, ip_proto
, dir
)
1672 struct block
*b0
, *b1
, *tmp
;
1674 /* ether proto ip */
1675 b0
= gen_linktype(ETHERTYPE_IP
);
1680 b1
= gen_portop(port
, ip_proto
, dir
);
1684 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1685 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1698 gen_portop6(port
, proto
, dir
)
1699 int port
, proto
, dir
;
1701 struct block
*b0
, *b1
, *tmp
;
1703 /* ip proto 'proto' */
1704 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1708 b1
= gen_portatom6(0, (bpf_int32
)port
);
1712 b1
= gen_portatom6(2, (bpf_int32
)port
);
1717 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1718 b1
= gen_portatom6(2, (bpf_int32
)port
);
1723 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1724 b1
= gen_portatom6(2, (bpf_int32
)port
);
1736 static struct block
*
1737 gen_port6(port
, ip_proto
, dir
)
1742 struct block
*b0
, *b1
, *tmp
;
1744 /* ether proto ip */
1745 b0
= gen_linktype(ETHERTYPE_IPV6
);
1750 b1
= gen_portop6(port
, ip_proto
, dir
);
1754 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1755 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1768 lookup_proto(name
, proto
)
1769 register const char *name
;
1778 v
= pcap_nametoproto(name
);
1779 if (v
== PROTO_UNDEF
)
1780 bpf_error("unknown ip proto '%s'", name
);
1784 /* XXX should look up h/w protocol type based on linktype */
1785 v
= pcap_nametoeproto(name
);
1786 if (v
== PROTO_UNDEF
)
1787 bpf_error("unknown ether proto '%s'", name
);
1791 if (strcmp(name
, "esis") == 0)
1793 else if (strcmp(name
, "isis") == 0)
1795 else if (strcmp(name
, "clnp") == 0)
1798 bpf_error("unknown osi proto '%s'", name
);
1818 static struct block
*
1819 gen_protochain(v
, proto
, dir
)
1824 #ifdef NO_PROTOCHAIN
1825 return gen_proto(v
, proto
, dir
);
1827 struct block
*b0
, *b
;
1828 struct slist
*s
[100];
1829 int fix2
, fix3
, fix4
, fix5
;
1830 int ahcheck
, again
, end
;
1832 int reg2
= alloc_reg();
1834 memset(s
, 0, sizeof(s
));
1835 fix2
= fix3
= fix4
= fix5
= 0;
1842 b0
= gen_protochain(v
, Q_IP
, dir
);
1843 b
= gen_protochain(v
, Q_IPV6
, dir
);
1847 bpf_error("bad protocol applied for 'protochain'");
1851 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1854 * s[0] is a dummy entry to protect other BPF insn from damaged
1855 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1856 * hard to find interdependency made by jump table fixup.
1859 s
[i
] = new_stmt(0); /*dummy*/
1864 b0
= gen_linktype(ETHERTYPE_IP
);
1867 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1868 s
[i
]->s
.k
= off_nl
+ 9;
1870 /* X = ip->ip_hl << 2 */
1871 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1877 b0
= gen_linktype(ETHERTYPE_IPV6
);
1879 /* A = ip6->ip_nxt */
1880 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1881 s
[i
]->s
.k
= off_nl
+ 6;
1883 /* X = sizeof(struct ip6_hdr) */
1884 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1890 bpf_error("unsupported proto to gen_protochain");
1894 /* again: if (A == v) goto end; else fall through; */
1896 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1898 s
[i
]->s
.jt
= NULL
; /*later*/
1899 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1903 #ifndef IPPROTO_NONE
1904 #define IPPROTO_NONE 59
1906 /* if (A == IPPROTO_NONE) goto end */
1907 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1908 s
[i
]->s
.jt
= NULL
; /*later*/
1909 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1910 s
[i
]->s
.k
= IPPROTO_NONE
;
1911 s
[fix5
]->s
.jf
= s
[i
];
1916 if (proto
== Q_IPV6
) {
1917 int v6start
, v6end
, v6advance
, j
;
1920 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1921 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1922 s
[i
]->s
.jt
= NULL
; /*later*/
1923 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1924 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
1925 s
[fix2
]->s
.jf
= s
[i
];
1927 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1928 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1929 s
[i
]->s
.jt
= NULL
; /*later*/
1930 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1931 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
1933 /* if (A == IPPROTO_ROUTING) goto v6advance */
1934 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1935 s
[i
]->s
.jt
= NULL
; /*later*/
1936 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1937 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1939 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1940 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1941 s
[i
]->s
.jt
= NULL
; /*later*/
1942 s
[i
]->s
.jf
= NULL
; /*later*/
1943 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
1954 * X = X + (P[X + 1] + 1) * 8;
1957 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1959 /* A = P[X + packet head] */
1960 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1964 s
[i
] = new_stmt(BPF_ST
);
1968 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1971 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1975 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1977 /* A = P[X + packet head]; */
1978 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1982 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1986 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1990 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1993 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1997 /* goto again; (must use BPF_JA for backward jump) */
1998 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1999 s
[i
]->s
.k
= again
- i
- 1;
2000 s
[i
- 1]->s
.jf
= s
[i
];
2004 for (j
= v6start
; j
<= v6end
; j
++)
2005 s
[j
]->s
.jt
= s
[v6advance
];
2010 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2012 s
[fix2
]->s
.jf
= s
[i
];
2018 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2019 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2020 s
[i
]->s
.jt
= NULL
; /*later*/
2021 s
[i
]->s
.jf
= NULL
; /*later*/
2022 s
[i
]->s
.k
= IPPROTO_AH
;
2024 s
[fix3
]->s
.jf
= s
[ahcheck
];
2031 * X = X + (P[X + 1] + 2) * 4;
2034 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2036 /* A = P[X + packet head]; */
2037 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2041 s
[i
] = new_stmt(BPF_ST
);
2045 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2048 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2052 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2054 /* A = P[X + packet head] */
2055 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2059 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2063 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2067 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2070 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2074 /* goto again; (must use BPF_JA for backward jump) */
2075 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2076 s
[i
]->s
.k
= again
- i
- 1;
2081 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2083 s
[fix2
]->s
.jt
= s
[end
];
2084 s
[fix4
]->s
.jf
= s
[end
];
2085 s
[fix5
]->s
.jt
= s
[end
];
2092 for (i
= 0; i
< max
- 1; i
++)
2093 s
[i
]->next
= s
[i
+ 1];
2094 s
[max
- 1]->next
= NULL
;
2099 b
= new_block(JMP(BPF_JEQ
));
2100 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2110 static struct block
*
2111 gen_proto(v
, proto
, dir
)
2116 struct block
*b0
, *b1
;
2118 if (dir
!= Q_DEFAULT
)
2119 bpf_error("direction applied to 'proto'");
2124 b0
= gen_proto(v
, Q_IP
, dir
);
2125 b1
= gen_proto(v
, Q_IPV6
, dir
);
2132 b0
= gen_linktype(ETHERTYPE_IP
);
2134 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2136 b1
= gen_protochain(v
, Q_IP
);
2142 b0
= gen_linktype(LLC_ISO_LSAP
);
2143 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2148 bpf_error("arp does not encapsulate another protocol");
2152 bpf_error("rarp does not encapsulate another protocol");
2156 bpf_error("atalk encapsulation is not specifiable");
2160 bpf_error("decnet encapsulation is not specifiable");
2164 bpf_error("sca does not encapsulate another protocol");
2168 bpf_error("lat does not encapsulate another protocol");
2172 bpf_error("moprc does not encapsulate another protocol");
2176 bpf_error("mopdl does not encapsulate another protocol");
2180 return gen_linktype(v
);
2183 bpf_error("'udp proto' is bogus");
2187 bpf_error("'tcp proto' is bogus");
2191 bpf_error("'icmp proto' is bogus");
2195 bpf_error("'igmp proto' is bogus");
2199 bpf_error("'igrp proto' is bogus");
2203 bpf_error("'pim proto' is bogus");
2208 b0
= gen_linktype(ETHERTYPE_IPV6
);
2210 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2212 b1
= gen_protochain(v
, Q_IPV6
);
2218 bpf_error("'icmp6 proto' is bogus");
2222 bpf_error("'ah proto' is bogus");
2225 bpf_error("'ah proto' is bogus");
2236 register const char *name
;
2239 int proto
= q
.proto
;
2243 bpf_u_int32 mask
, addr
;
2245 bpf_u_int32
**alist
;
2248 struct sockaddr_in
*sin
;
2249 struct sockaddr_in6
*sin6
;
2250 struct addrinfo
*res
, *res0
;
2251 struct in6_addr mask128
;
2253 struct block
*b
, *tmp
;
2254 int port
, real_proto
;
2259 addr
= pcap_nametonetaddr(name
);
2261 bpf_error("unknown network '%s'", name
);
2262 /* Left justify network addr and calculate its network mask */
2264 while (addr
&& (addr
& 0xff000000) == 0) {
2268 return gen_host(addr
, mask
, proto
, dir
);
2272 if (proto
== Q_LINK
) {
2276 eaddr
= pcap_ether_hostton(name
);
2279 "unknown ether host '%s'", name
);
2280 return gen_ehostop(eaddr
, dir
);
2283 eaddr
= pcap_ether_hostton(name
);
2286 "unknown FDDI host '%s'", name
);
2287 return gen_fhostop(eaddr
, dir
);
2290 eaddr
= pcap_ether_hostton(name
);
2293 "unknown token ring host '%s'", name
);
2294 return gen_thostop(eaddr
, dir
);
2298 "only ethernet/FDDI/token ring supports link-level host name");
2301 } else if (proto
== Q_DECNET
) {
2302 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2304 * I don't think DECNET hosts can be multihomed, so
2305 * there is no need to build up a list of addresses
2307 return (gen_host(dn_addr
, 0, proto
, dir
));
2310 alist
= pcap_nametoaddr(name
);
2311 if (alist
== NULL
|| *alist
== NULL
)
2312 bpf_error("unknown host '%s'", name
);
2314 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2316 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2318 tmp
= gen_host(**alist
++, 0xffffffff,
2325 memset(&mask128
, 0xff, sizeof(mask128
));
2326 res0
= res
= pcap_nametoaddrinfo(name
);
2328 bpf_error("unknown host '%s'", name
);
2330 tproto
= tproto6
= proto
;
2331 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2335 for (res
= res0
; res
; res
= res
->ai_next
) {
2336 switch (res
->ai_family
) {
2338 if (tproto
== Q_IPV6
)
2341 sin
= (struct sockaddr_in
*)
2343 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2344 0xffffffff, tproto
, dir
);
2347 if (tproto6
== Q_IP
)
2350 sin6
= (struct sockaddr_in6
*)
2352 tmp
= gen_host6(&sin6
->sin6_addr
,
2353 &mask128
, tproto6
, dir
);
2362 bpf_error("unknown host '%s'%s", name
,
2363 (proto
== Q_DEFAULT
)
2365 : " for specified address family");
2372 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2373 bpf_error("illegal qualifier of 'port'");
2374 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2375 bpf_error("unknown port '%s'", name
);
2376 if (proto
== Q_UDP
) {
2377 if (real_proto
== IPPROTO_TCP
)
2378 bpf_error("port '%s' is tcp", name
);
2380 /* override PROTO_UNDEF */
2381 real_proto
= IPPROTO_UDP
;
2383 if (proto
== Q_TCP
) {
2384 if (real_proto
== IPPROTO_UDP
)
2385 bpf_error("port '%s' is udp", name
);
2387 /* override PROTO_UNDEF */
2388 real_proto
= IPPROTO_TCP
;
2391 return gen_port(port
, real_proto
, dir
);
2395 b
= gen_port(port
, real_proto
, dir
);
2396 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2403 eaddr
= pcap_ether_hostton(name
);
2405 bpf_error("unknown ether host: %s", name
);
2407 alist
= pcap_nametoaddr(name
);
2408 if (alist
== NULL
|| *alist
== NULL
)
2409 bpf_error("unknown host '%s'", name
);
2410 return gen_gateway(eaddr
, alist
, proto
, dir
);
2412 bpf_error("'gateway' not supported in this configuration");
2416 real_proto
= lookup_proto(name
, proto
);
2417 if (real_proto
>= 0)
2418 return gen_proto(real_proto
, proto
, dir
);
2420 bpf_error("unknown protocol: %s", name
);
2423 real_proto
= lookup_proto(name
, proto
);
2424 if (real_proto
>= 0)
2425 return gen_protochain(real_proto
, proto
, dir
);
2427 bpf_error("unknown protocol: %s", name
);
2439 gen_mcode(s1
, s2
, masklen
, q
)
2440 register const char *s1
, *s2
;
2441 register int masklen
;
2444 register int nlen
, mlen
;
2447 nlen
= __pcap_atoin(s1
, &n
);
2448 /* Promote short ipaddr */
2452 mlen
= __pcap_atoin(s2
, &m
);
2453 /* Promote short ipaddr */
2456 bpf_error("non-network bits set in \"%s mask %s\"",
2459 /* Convert mask len to mask */
2461 bpf_error("mask length must be <= 32");
2462 m
= 0xffffffff << (32 - masklen
);
2464 bpf_error("non-network bits set in \"%s/%d\"",
2471 return gen_host(n
, m
, q
.proto
, q
.dir
);
2474 bpf_error("Mask syntax for networks only");
2481 register const char *s
;
2486 int proto
= q
.proto
;
2492 else if (q
.proto
== Q_DECNET
)
2493 vlen
= __pcap_atodn(s
, &v
);
2495 vlen
= __pcap_atoin(s
, &v
);
2502 if (proto
== Q_DECNET
)
2503 return gen_host(v
, 0, proto
, dir
);
2504 else if (proto
== Q_LINK
) {
2505 bpf_error("illegal link layer address");
2508 if (s
== NULL
&& q
.addr
== Q_NET
) {
2509 /* Promote short net number */
2510 while (v
&& (v
& 0xff000000) == 0) {
2515 /* Promote short ipaddr */
2519 return gen_host(v
, mask
, proto
, dir
);
2524 proto
= IPPROTO_UDP
;
2525 else if (proto
== Q_TCP
)
2526 proto
= IPPROTO_TCP
;
2527 else if (proto
== Q_DEFAULT
)
2528 proto
= PROTO_UNDEF
;
2530 bpf_error("illegal qualifier of 'port'");
2533 return gen_port((int)v
, proto
, dir
);
2537 b
= gen_port((int)v
, proto
, dir
);
2538 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2544 bpf_error("'gateway' requires a name");
2548 return gen_proto((int)v
, proto
, dir
);
2551 return gen_protochain((int)v
, proto
, dir
);
2566 gen_mcode6(s1
, s2
, masklen
, q
)
2567 register const char *s1
, *s2
;
2568 register int masklen
;
2571 struct addrinfo
*res
;
2572 struct in6_addr
*addr
;
2573 struct in6_addr mask
;
2578 bpf_error("no mask %s supported", s2
);
2580 res
= pcap_nametoaddrinfo(s1
);
2582 bpf_error("invalid ip6 address %s", s1
);
2584 bpf_error("%s resolved to multiple address", s1
);
2585 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2587 if (sizeof(mask
) * 8 < masklen
)
2588 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2589 memset(&mask
, 0xff, masklen
/ 8);
2591 mask
.s6_addr
[masklen
/ 8] =
2592 (0xff << (8 - masklen
% 8)) & 0xff;
2595 a
= (u_int32_t
*)addr
;
2596 m
= (u_int32_t
*)&mask
;
2597 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2598 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2599 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2607 bpf_error("Mask syntax for networks only");
2611 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2616 bpf_error("invalid qualifier against IPv6 address");
2624 register const u_char
*eaddr
;
2627 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2628 if (linktype
== DLT_EN10MB
)
2629 return gen_ehostop(eaddr
, (int)q
.dir
);
2630 if (linktype
== DLT_FDDI
)
2631 return gen_fhostop(eaddr
, (int)q
.dir
);
2632 if (linktype
== DLT_IEEE802
)
2633 return gen_thostop(eaddr
, (int)q
.dir
);
2635 bpf_error("ethernet address used in non-ether expression");
2641 struct slist
*s0
, *s1
;
2644 * This is definitely not the best way to do this, but the
2645 * lists will rarely get long.
2652 static struct slist
*
2658 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2663 static struct slist
*
2669 s
= new_stmt(BPF_LD
|BPF_MEM
);
2675 gen_load(proto
, index
, size
)
2680 struct slist
*s
, *tmp
;
2682 int regno
= alloc_reg();
2684 free_reg(index
->regno
);
2688 bpf_error("data size must be 1, 2, or 4");
2704 bpf_error("unsupported index operation");
2707 s
= xfer_to_x(index
);
2708 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2710 sappend(index
->s
, s
);
2725 /* XXX Note that we assume a fixed link header here. */
2726 s
= xfer_to_x(index
);
2727 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2730 sappend(index
->s
, s
);
2732 b
= gen_proto_abbrev(proto
);
2734 gen_and(index
->b
, b
);
2744 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2746 sappend(s
, xfer_to_a(index
));
2747 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2748 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2749 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2751 sappend(index
->s
, s
);
2753 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2755 gen_and(index
->b
, b
);
2757 gen_and(gen_proto_abbrev(Q_IP
), b
);
2763 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2767 index
->regno
= regno
;
2768 s
= new_stmt(BPF_ST
);
2770 sappend(index
->s
, s
);
2776 gen_relation(code
, a0
, a1
, reversed
)
2778 struct arth
*a0
, *a1
;
2781 struct slist
*s0
, *s1
, *s2
;
2782 struct block
*b
, *tmp
;
2786 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2787 b
= new_block(JMP(code
));
2788 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2789 reversed
= !reversed
;
2790 b
->s
.k
= 0x80000000;
2798 sappend(a0
->s
, a1
->s
);
2802 free_reg(a0
->regno
);
2803 free_reg(a1
->regno
);
2805 /* 'and' together protocol checks */
2808 gen_and(a0
->b
, tmp
= a1
->b
);
2824 int regno
= alloc_reg();
2825 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2828 s
= new_stmt(BPF_LD
|BPF_LEN
);
2829 s
->next
= new_stmt(BPF_ST
);
2830 s
->next
->s
.k
= regno
;
2845 a
= (struct arth
*)newchunk(sizeof(*a
));
2849 s
= new_stmt(BPF_LD
|BPF_IMM
);
2851 s
->next
= new_stmt(BPF_ST
);
2867 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2870 s
= new_stmt(BPF_ST
);
2878 gen_arth(code
, a0
, a1
)
2880 struct arth
*a0
, *a1
;
2882 struct slist
*s0
, *s1
, *s2
;
2886 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2891 sappend(a0
->s
, a1
->s
);
2893 free_reg(a1
->regno
);
2895 s0
= new_stmt(BPF_ST
);
2896 a0
->regno
= s0
->s
.k
= alloc_reg();
2903 * Here we handle simple allocation of the scratch registers.
2904 * If too many registers are alloc'd, the allocator punts.
2906 static int regused
[BPF_MEMWORDS
];
2910 * Return the next free register.
2915 int n
= BPF_MEMWORDS
;
2918 if (regused
[curreg
])
2919 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2921 regused
[curreg
] = 1;
2925 bpf_error("too many registers needed to evaluate expression");
2930 * Return a register to the table so it can
2940 static struct block
*
2947 s
= new_stmt(BPF_LD
|BPF_LEN
);
2948 b
= new_block(JMP(jmp
));
2959 return gen_len(BPF_JGE
, n
);
2963 * Actually, this is less than or equal.
2971 b
= gen_len(BPF_JGT
, n
);
2978 gen_byteop(op
, idx
, val
)
2989 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2992 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2993 b
->s
.code
= JMP(BPF_JGE
);
2998 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2999 b
->s
.code
= JMP(BPF_JGT
);
3003 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3007 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3011 b
= new_block(JMP(BPF_JEQ
));
3019 gen_broadcast(proto
)
3022 bpf_u_int32 hostmask
;
3023 struct block
*b0
, *b1
, *b2
;
3024 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3030 if (linktype
== DLT_EN10MB
)
3031 return gen_ehostop(ebroadcast
, Q_DST
);
3032 if (linktype
== DLT_FDDI
)
3033 return gen_fhostop(ebroadcast
, Q_DST
);
3034 if (linktype
== DLT_IEEE802
)
3035 return gen_thostop(ebroadcast
, Q_DST
);
3036 bpf_error("not a broadcast link");
3040 b0
= gen_linktype(ETHERTYPE_IP
);
3041 hostmask
= ~netmask
;
3042 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3043 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3044 (bpf_int32
)(~0 & hostmask
), hostmask
);
3049 bpf_error("only ether/ip broadcast filters supported");
3053 gen_multicast(proto
)
3056 register struct block
*b0
, *b1
;
3057 register struct slist
*s
;
3063 if (linktype
== DLT_EN10MB
) {
3064 /* ether[0] & 1 != 0 */
3065 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3067 b0
= new_block(JMP(BPF_JSET
));
3073 if (linktype
== DLT_FDDI
) {
3074 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3075 /* fddi[1] & 1 != 0 */
3076 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3078 b0
= new_block(JMP(BPF_JSET
));
3084 /* TODO - check how token ring handles multicast */
3085 /* if (linktype == DLT_IEEE802) ... */
3087 /* Link not known to support multicasts */
3091 b0
= gen_linktype(ETHERTYPE_IP
);
3092 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3093 b1
->s
.code
= JMP(BPF_JGE
);
3099 b0
= gen_linktype(ETHERTYPE_IPV6
);
3100 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3105 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3109 * generate command for inbound/outbound. It's here so we can
3110 * make it link-type specific. 'dir' = 0 implies "inbound",
3111 * = 1 implies "outbound".
3117 register struct block
*b0
;
3119 b0
= gen_relation(BPF_JEQ
,
3120 gen_load(Q_LINK
, gen_loadi(0), 1),
3127 * support IEEE 802.1Q VLAN trunk over ethernet
3133 static u_int orig_linktype
= -1, orig_nl
= -1;
3137 * Change the offsets to point to the type and data fields within
3138 * the VLAN packet. This is somewhat of a kludge.
3140 if (orig_nl
== (u_int
)-1) {
3141 orig_linktype
= off_linktype
; /* save original values */
3152 bpf_error("no VLAN support for data link type %d",
3158 /* check for VLAN */
3159 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3161 /* If a specific VLAN is requested, check VLAN id */
3162 if (vlan_num
>= 0) {
3165 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);