]>
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.127 2000-10-28 08:19:29 guy Exp $ (LBL)";
31 #include <sys/types.h>
32 #include <sys/socket.h>
35 #include <sys/param.h>
42 #include <netinet/in.h>
52 #include "ethertype.h"
55 #include <pcap-namedb.h>
58 #include <sys/socket.h>
61 #ifdef HAVE_OS_PROTO_H
65 #define JMP(c) ((c)|BPF_JMP|BPF_K)
68 static jmp_buf top_ctx
;
69 static pcap_t
*bpf_pcap
;
73 int pcap_fddipad
= PCAP_FDDIPAD
;
80 bpf_error(const char *fmt
, ...)
87 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
94 static void init_linktype(int);
96 static int alloc_reg(void);
97 static void free_reg(int);
99 static struct block
*root
;
102 * We divy out chunks of memory rather than call malloc each time so
103 * we don't have to worry about leaking memory. It's probably
104 * not a big deal if all this memory was wasted but it this ever
105 * goes into a library that would probably not be a good idea.
108 #define CHUNK0SIZE 1024
114 static struct chunk chunks
[NCHUNKS
];
115 static int cur_chunk
;
117 static void *newchunk(u_int
);
118 static void freechunks(void);
119 static inline struct block
*new_block(int);
120 static inline struct slist
*new_stmt(int);
121 static struct block
*gen_retblk(int);
122 static inline void syntax(void);
124 static void backpatch(struct block
*, struct block
*);
125 static void merge(struct block
*, struct block
*);
126 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
127 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
128 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
129 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
130 static struct block
*gen_uncond(int);
131 static inline struct block
*gen_true(void);
132 static inline struct block
*gen_false(void);
133 static struct block
*gen_linktype(int);
134 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
135 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
137 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
139 static struct block
*gen_ehostop(const u_char
*, int);
140 static struct block
*gen_fhostop(const u_char
*, int);
141 static struct block
*gen_thostop(const u_char
*, int);
142 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
143 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
145 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
148 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
150 static struct block
*gen_ipfrag(void);
151 static struct block
*gen_portatom(int, bpf_int32
);
153 static struct block
*gen_portatom6(int, bpf_int32
);
155 struct block
*gen_portop(int, int, int);
156 static struct block
*gen_port(int, int, int);
158 struct block
*gen_portop6(int, int, int);
159 static struct block
*gen_port6(int, int, int);
161 static int lookup_proto(const char *, int);
162 static struct block
*gen_protochain(int, int, int);
163 static struct block
*gen_proto(int, int, int);
164 static struct slist
*xfer_to_x(struct arth
*);
165 static struct slist
*xfer_to_a(struct arth
*);
166 static struct block
*gen_len(int, int);
176 /* XXX Round up to nearest long. */
177 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
179 /* XXX Round up to structure boundary. */
183 cp
= &chunks
[cur_chunk
];
184 if (n
> cp
->n_left
) {
185 ++cp
, k
= ++cur_chunk
;
187 bpf_error("out of memory");
188 size
= CHUNK0SIZE
<< k
;
189 cp
->m
= (void *)malloc(size
);
190 memset((char *)cp
->m
, 0, size
);
193 bpf_error("out of memory");
196 return (void *)((char *)cp
->m
+ cp
->n_left
);
205 for (i
= 0; i
< NCHUNKS
; ++i
)
206 if (chunks
[i
].m
!= NULL
) {
213 * A strdup whose allocations are freed after code generation is over.
217 register const char *s
;
219 int n
= strlen(s
) + 1;
220 char *cp
= newchunk(n
);
226 static inline struct block
*
232 p
= (struct block
*)newchunk(sizeof(*p
));
239 static inline struct slist
*
245 p
= (struct slist
*)newchunk(sizeof(*p
));
251 static struct block
*
255 struct block
*b
= new_block(BPF_RET
|BPF_K
);
264 bpf_error("syntax error in filter expression");
267 static bpf_u_int32 netmask
;
272 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
273 char *buf
, int optimize
, bpf_u_int32 mask
)
282 if (setjmp(top_ctx
)) {
290 /* On Linux we do not use the BPF filter to truncate the packet
291 * since the kernel provides other ways for that. In fact if we
292 * are using the packet filter for that duty we will be unable
293 * to acquire the original packet size. -- Torsten */
295 snaplen
= pcap_snapshot(p
);
300 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
301 "snaplen of 0 rejects all packets");
305 lex_init(buf
? buf
: "");
306 init_linktype(pcap_datalink(p
));
313 root
= gen_retblk(snaplen
);
315 if (optimize
&& !no_optimize
) {
318 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
319 bpf_error("expression rejects all packets");
321 program
->bf_insns
= icode_to_fcode(root
, &len
);
322 program
->bf_len
= len
;
330 * entry point for using the compiler with no pcap open
331 * pass in all the stuff that is needed explicitly instead.
334 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
335 struct bpf_program
*program
,
336 char *buf
, int optimize
, bpf_u_int32 mask
)
344 if (setjmp(top_ctx
)) {
351 /* XXX needed? I don't grok the use of globals here. */
352 snaplen
= snaplen_arg
;
354 lex_init(buf
? buf
: "");
355 init_linktype(linktype_arg
);
362 root
= gen_retblk(snaplen_arg
);
367 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
368 bpf_error("expression rejects all packets");
370 program
->bf_insns
= icode_to_fcode(root
, &len
);
371 program
->bf_len
= len
;
378 * Clean up a "struct bpf_program" by freeing all the memory allocated
382 pcap_freecode(struct bpf_program
*program
)
385 if (program
->bf_insns
!= NULL
) {
386 free((char *)program
->bf_insns
);
387 program
->bf_insns
= NULL
;
392 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
393 * which of the jt and jf fields has been resolved and which is a pointer
394 * back to another unresolved block (or nil). At least one of the fields
395 * in each block is already resolved.
398 backpatch(list
, target
)
399 struct block
*list
, *target
;
416 * Merge the lists in b0 and b1, using the 'sense' field to indicate
417 * which of jt and jf is the link.
421 struct block
*b0
, *b1
;
423 register struct block
**p
= &b0
;
425 /* Find end of list. */
427 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
429 /* Concatenate the lists. */
437 backpatch(p
, gen_retblk(snaplen
));
438 p
->sense
= !p
->sense
;
439 backpatch(p
, gen_retblk(0));
445 struct block
*b0
, *b1
;
447 backpatch(b0
, b1
->head
);
448 b0
->sense
= !b0
->sense
;
449 b1
->sense
= !b1
->sense
;
451 b1
->sense
= !b1
->sense
;
457 struct block
*b0
, *b1
;
459 b0
->sense
= !b0
->sense
;
460 backpatch(b0
, b1
->head
);
461 b0
->sense
= !b0
->sense
;
470 b
->sense
= !b
->sense
;
473 static struct block
*
474 gen_cmp(offset
, size
, v
)
481 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
484 b
= new_block(JMP(BPF_JEQ
));
491 static struct block
*
492 gen_cmp_gt(offset
, size
, v
)
499 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
502 b
= new_block(JMP(BPF_JGT
));
509 static struct block
*
510 gen_mcmp(offset
, size
, v
, mask
)
515 struct block
*b
= gen_cmp(offset
, size
, v
);
518 if (mask
!= 0xffffffff) {
519 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
526 static struct block
*
527 gen_bcmp(offset
, size
, v
)
528 register u_int offset
, size
;
529 register const u_char
*v
;
531 register struct block
*b
, *tmp
;
535 register const u_char
*p
= &v
[size
- 4];
536 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
537 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
539 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
546 register const u_char
*p
= &v
[size
- 2];
547 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
549 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
556 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
565 * Various code constructs need to know the layout of the data link
566 * layer. These variables give the necessary offsets. off_linktype
567 * is set to -1 for no encapsulation, in which case, IP is assumed.
569 static u_int off_linktype
;
588 * SLIP doesn't have a link level type. The 16 byte
589 * header is hacked into our SLIP driver.
596 /* XXX this may be the same as the DLT_PPP_BSDOS case */
621 * FDDI doesn't really have a link-level type field.
622 * We assume that SSAP = SNAP is being used and pick
623 * out the encapsulated Ethernet type.
625 * XXX - should we generate code to check for SNAP?
629 off_linktype
+= pcap_fddipad
;
633 off_nl
+= pcap_fddipad
;
639 * Token Ring doesn't really have a link-level type field.
640 * We assume that SSAP = SNAP is being used and pick
641 * out the encapsulated Ethernet type.
643 * XXX - should we generate code to check for SNAP?
645 * XXX - the header is actually variable-length.
646 * Some various Linux patched versions gave 38
647 * as "off_linktype" and 40 as "off_nl"; however,
648 * if a token ring packet has *no* routing
649 * information, i.e. is not source-routed, the correct
650 * values are 20 and 22, as they are in the vanilla code.
652 * A packet is source-routed iff the uppermost bit
653 * of the first byte of the source address, at an
654 * offset of 8, has the uppermost bit set. If the
655 * packet is source-routed, the total number of bytes
656 * of routing information is 2 plus bits 0x1F00 of
657 * the 16-bit value at an offset of 14 (shifted right
658 * 8 - figure out which byte that is).
664 case DLT_ATM_RFC1483
:
666 * assume routed, non-ISO PDUs
667 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
678 case DLT_ATM_CLIP
: /* Linux ATM defines this */
683 bpf_error("unknown data link type 0x%x", linktype
);
687 static struct block
*
694 s
= new_stmt(BPF_LD
|BPF_IMM
);
696 b
= new_block(JMP(BPF_JEQ
));
702 static inline struct block
*
705 return gen_uncond(1);
708 static inline struct block
*
711 return gen_uncond(0);
714 static struct block
*
718 struct block
*b0
, *b1
;
720 /* If we're not using encapsulation, we're done */
721 if (off_linktype
== -1)
728 * XXX - handle other LLC-encapsulated protocols here
733 case ETHERTYPE_ATALK
:
736 * EtherTalk (AppleTalk protocols on Ethernet link
737 * layer) may use 802.2 encapsulation.
741 * Check for 802.2 encapsulation (EtherTalk phase 2?);
742 * we check for an Ethernet type field less than
743 * 1500, which means it's an 802.3 length field.
745 b0
= gen_cmp_gt(off_linktype
, BPF_H
, 1500);
749 * 802.2-encapsulated ETHERTYPE_ATALK packets are
750 * SNAP packets with an organization code of
751 * 0x080007 (Apple, for Appletalk) and a protocol
752 * type of ETHERTYPE_ATALK (Appletalk).
754 * 802.2-encapsulated ETHERTYPE_AARP packets are
755 * SNAP packets with an organization code of
756 * 0x000000 (encapsulated Ethernet) and a protocol
757 * type of ETHERTYPE_AARP (Appletalk ARP).
759 if (proto
== ETHERTYPE_ATALK
)
760 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
761 else /* proto == ETHERTYPE_AARP */
762 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
766 * Check for Ethernet encapsulation (Ethertalk
767 * phase 1?); we just check for the Ethernet
770 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
782 if (proto
== ETHERTYPE_IP
)
783 proto
= PPP_IP
; /* XXX was 0x21 */
785 else if (proto
== ETHERTYPE_IPV6
)
794 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
795 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
797 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
812 case ETHERTYPE_ATALK
:
824 if (proto
== ETHERTYPE_IP
)
825 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET
)));
827 else if (proto
== ETHERTYPE_IPV6
)
828 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET6
)));
833 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
837 * Check for an LLC SNAP packet with a given organization code and
838 * protocol type; we check the entire contents of the 802.2 LLC and
839 * snap headers, checking for a DSAP of 0xAA, an SSAP of 0xAA, and
840 * a control field of 0x03 in the LLC header, and for the specified
841 * organization code and protocol type in the SNAP header.
843 static struct block
*
844 gen_snap(orgcode
, ptype
, offset
)
851 snapblock
[0] = 0xAA; /* DSAP = SNAP */
852 snapblock
[1] = 0xAA; /* SSAP = SNAP */
853 snapblock
[2] = 0x03; /* control = UI */
854 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
855 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
856 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
857 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
858 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
859 return gen_bcmp(offset
, 8, snapblock
);
862 static struct block
*
863 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
867 u_int src_off
, dst_off
;
869 struct block
*b0
, *b1
;
883 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
884 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
890 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
891 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
898 b0
= gen_linktype(proto
);
899 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
905 static struct block
*
906 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
907 struct in6_addr
*addr
;
908 struct in6_addr
*mask
;
910 u_int src_off
, dst_off
;
912 struct block
*b0
, *b1
;
927 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
928 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
934 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
935 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
942 /* this order is important */
943 a
= (u_int32_t
*)addr
;
944 m
= (u_int32_t
*)mask
;
945 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
946 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
948 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
950 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
952 b0
= gen_linktype(proto
);
958 static struct block
*
959 gen_ehostop(eaddr
, dir
)
960 register const u_char
*eaddr
;
963 register struct block
*b0
, *b1
;
967 return gen_bcmp(6, 6, eaddr
);
970 return gen_bcmp(0, 6, eaddr
);
973 b0
= gen_ehostop(eaddr
, Q_SRC
);
974 b1
= gen_ehostop(eaddr
, Q_DST
);
980 b0
= gen_ehostop(eaddr
, Q_SRC
);
981 b1
= gen_ehostop(eaddr
, Q_DST
);
990 * Like gen_ehostop, but for DLT_FDDI
992 static struct block
*
993 gen_fhostop(eaddr
, dir
)
994 register const u_char
*eaddr
;
997 struct block
*b0
, *b1
;
1002 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1004 return gen_bcmp(6 + 1, 6, eaddr
);
1009 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1011 return gen_bcmp(0 + 1, 6, eaddr
);
1015 b0
= gen_fhostop(eaddr
, Q_SRC
);
1016 b1
= gen_fhostop(eaddr
, Q_DST
);
1022 b0
= gen_fhostop(eaddr
, Q_SRC
);
1023 b1
= gen_fhostop(eaddr
, Q_DST
);
1032 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1034 static struct block
*
1035 gen_thostop(eaddr
, dir
)
1036 register const u_char
*eaddr
;
1039 register struct block
*b0
, *b1
;
1043 return gen_bcmp(8, 6, eaddr
);
1046 return gen_bcmp(2, 6, eaddr
);
1049 b0
= gen_thostop(eaddr
, Q_SRC
);
1050 b1
= gen_thostop(eaddr
, Q_DST
);
1056 b0
= gen_thostop(eaddr
, Q_SRC
);
1057 b1
= gen_thostop(eaddr
, Q_DST
);
1066 * This is quite tricky because there may be pad bytes in front of the
1067 * DECNET header, and then there are two possible data packet formats that
1068 * carry both src and dst addresses, plus 5 packet types in a format that
1069 * carries only the src node, plus 2 types that use a different format and
1070 * also carry just the src node.
1074 * Instead of doing those all right, we just look for data packets with
1075 * 0 or 1 bytes of padding. If you want to look at other packets, that
1076 * will require a lot more hacking.
1078 * To add support for filtering on DECNET "areas" (network numbers)
1079 * one would want to add a "mask" argument to this routine. That would
1080 * make the filter even more inefficient, although one could be clever
1081 * and not generate masking instructions if the mask is 0xFFFF.
1083 static struct block
*
1084 gen_dnhostop(addr
, dir
, base_off
)
1089 struct block
*b0
, *b1
, *b2
, *tmp
;
1090 u_int offset_lh
; /* offset if long header is received */
1091 u_int offset_sh
; /* offset if short header is received */
1096 offset_sh
= 1; /* follows flags */
1097 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1101 offset_sh
= 3; /* follows flags, dstnode */
1102 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1106 /* Inefficient because we do our Calvinball dance twice */
1107 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1108 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1114 /* Inefficient because we do our Calvinball dance twice */
1115 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1116 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1123 b0
= gen_linktype(ETHERTYPE_DN
);
1124 /* Check for pad = 1, long header case */
1125 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1126 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1127 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1128 BPF_H
, (bpf_int32
)ntohs(addr
));
1130 /* Check for pad = 0, long header case */
1131 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1132 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1135 /* Check for pad = 1, short header case */
1136 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1137 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1138 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1139 BPF_H
, (bpf_int32
)ntohs(addr
));
1142 /* Check for pad = 0, short header case */
1143 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1144 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1148 /* Combine with test for linktype */
1153 static struct block
*
1154 gen_host(addr
, mask
, proto
, dir
)
1160 struct block
*b0
, *b1
;
1165 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1166 if (off_linktype
!= -1) {
1167 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1169 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1175 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1176 off_nl
+ 12, off_nl
+ 16);
1179 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1180 off_nl
+ 14, off_nl
+ 24);
1183 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1184 off_nl
+ 14, off_nl
+ 24);
1187 bpf_error("'tcp' modifier applied to host");
1190 bpf_error("'udp' modifier applied to host");
1193 bpf_error("'icmp' modifier applied to host");
1196 bpf_error("'igmp' modifier applied to host");
1199 bpf_error("'igrp' modifier applied to host");
1202 bpf_error("'pim' modifier applied to host");
1205 bpf_error("ATALK host filtering not implemented");
1208 bpf_error("AARP host filtering not implemented");
1211 return gen_dnhostop(addr
, dir
, off_nl
);
1214 bpf_error("SCA host filtering not implemented");
1217 bpf_error("LAT host filtering not implemented");
1220 bpf_error("MOPDL host filtering not implemented");
1223 bpf_error("MOPRC host filtering not implemented");
1227 bpf_error("'ip6' modifier applied to ip host");
1230 bpf_error("'icmp6' modifier applied to host");
1234 bpf_error("'ah' modifier applied to host");
1237 bpf_error("'esp' modifier applied to host");
1246 static struct block
*
1247 gen_host6(addr
, mask
, proto
, dir
)
1248 struct in6_addr
*addr
;
1249 struct in6_addr
*mask
;
1256 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1259 bpf_error("'ip' modifier applied to ip6 host");
1262 bpf_error("'rarp' modifier applied to ip6 host");
1265 bpf_error("'arp' modifier applied to ip6 host");
1268 bpf_error("'tcp' modifier applied to host");
1271 bpf_error("'udp' modifier applied to host");
1274 bpf_error("'icmp' modifier applied to host");
1277 bpf_error("'igmp' modifier applied to host");
1280 bpf_error("'igrp' modifier applied to host");
1283 bpf_error("'pim' modifier applied to host");
1286 bpf_error("ATALK host filtering not implemented");
1289 bpf_error("AARP host filtering not implemented");
1292 bpf_error("'decnet' modifier applied to ip6 host");
1295 bpf_error("SCA host filtering not implemented");
1298 bpf_error("LAT host filtering not implemented");
1301 bpf_error("MOPDL host filtering not implemented");
1304 bpf_error("MOPRC host filtering not implemented");
1307 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1308 off_nl
+ 8, off_nl
+ 24);
1311 bpf_error("'icmp6' modifier applied to host");
1314 bpf_error("'ah' modifier applied to host");
1317 bpf_error("'esp' modifier applied to host");
1327 static struct block
*
1328 gen_gateway(eaddr
, alist
, proto
, dir
)
1329 const u_char
*eaddr
;
1330 bpf_u_int32
**alist
;
1334 struct block
*b0
, *b1
, *tmp
;
1337 bpf_error("direction applied to 'gateway'");
1344 if (linktype
== DLT_EN10MB
)
1345 b0
= gen_ehostop(eaddr
, Q_OR
);
1346 else if (linktype
== DLT_FDDI
)
1347 b0
= gen_fhostop(eaddr
, Q_OR
);
1348 else if (linktype
== DLT_IEEE802
)
1349 b0
= gen_thostop(eaddr
, Q_OR
);
1352 "'gateway' supported only on ethernet, FDDI or token ring");
1354 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1356 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1364 bpf_error("illegal modifier of 'gateway'");
1370 gen_proto_abbrev(proto
)
1381 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1383 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1389 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1391 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1397 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1400 #ifndef IPPROTO_IGMP
1401 #define IPPROTO_IGMP 2
1405 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1408 #ifndef IPPROTO_IGRP
1409 #define IPPROTO_IGRP 9
1412 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1416 #define IPPROTO_PIM 103
1420 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1422 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1428 b1
= gen_linktype(ETHERTYPE_IP
);
1432 b1
= gen_linktype(ETHERTYPE_ARP
);
1436 b1
= gen_linktype(ETHERTYPE_REVARP
);
1440 bpf_error("link layer applied in wrong context");
1443 b1
= gen_linktype(ETHERTYPE_ATALK
);
1447 b1
= gen_linktype(ETHERTYPE_AARP
);
1451 b1
= gen_linktype(ETHERTYPE_DN
);
1455 b1
= gen_linktype(ETHERTYPE_SCA
);
1459 b1
= gen_linktype(ETHERTYPE_LAT
);
1463 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1467 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1472 b1
= gen_linktype(ETHERTYPE_IPV6
);
1475 #ifndef IPPROTO_ICMPV6
1476 #define IPPROTO_ICMPV6 58
1479 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1484 #define IPPROTO_AH 51
1487 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1489 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1495 #define IPPROTO_ESP 50
1498 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1500 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1511 static struct block
*
1518 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1519 s
->s
.k
= off_nl
+ 6;
1520 b
= new_block(JMP(BPF_JSET
));
1528 static struct block
*
1529 gen_portatom(off
, v
)
1536 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1539 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1540 s
->next
->s
.k
= off_nl
+ off
;
1542 b
= new_block(JMP(BPF_JEQ
));
1550 static struct block
*
1551 gen_portatom6(off
, v
)
1555 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1560 gen_portop(port
, proto
, dir
)
1561 int port
, proto
, dir
;
1563 struct block
*b0
, *b1
, *tmp
;
1565 /* ip proto 'proto' */
1566 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1572 b1
= gen_portatom(0, (bpf_int32
)port
);
1576 b1
= gen_portatom(2, (bpf_int32
)port
);
1581 tmp
= gen_portatom(0, (bpf_int32
)port
);
1582 b1
= gen_portatom(2, (bpf_int32
)port
);
1587 tmp
= gen_portatom(0, (bpf_int32
)port
);
1588 b1
= gen_portatom(2, (bpf_int32
)port
);
1600 static struct block
*
1601 gen_port(port
, ip_proto
, dir
)
1606 struct block
*b0
, *b1
, *tmp
;
1608 /* ether proto ip */
1609 b0
= gen_linktype(ETHERTYPE_IP
);
1614 b1
= gen_portop(port
, ip_proto
, dir
);
1618 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1619 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1632 gen_portop6(port
, proto
, dir
)
1633 int port
, proto
, dir
;
1635 struct block
*b0
, *b1
, *tmp
;
1637 /* ip proto 'proto' */
1638 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1642 b1
= gen_portatom6(0, (bpf_int32
)port
);
1646 b1
= gen_portatom6(2, (bpf_int32
)port
);
1651 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1652 b1
= gen_portatom6(2, (bpf_int32
)port
);
1657 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1658 b1
= gen_portatom6(2, (bpf_int32
)port
);
1670 static struct block
*
1671 gen_port6(port
, ip_proto
, dir
)
1676 struct block
*b0
, *b1
, *tmp
;
1678 /* ether proto ip */
1679 b0
= gen_linktype(ETHERTYPE_IPV6
);
1684 b1
= gen_portop6(port
, ip_proto
, dir
);
1688 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1689 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1702 lookup_proto(name
, proto
)
1703 register const char *name
;
1712 v
= pcap_nametoproto(name
);
1713 if (v
== PROTO_UNDEF
)
1714 bpf_error("unknown ip proto '%s'", name
);
1718 /* XXX should look up h/w protocol type based on linktype */
1719 v
= pcap_nametoeproto(name
);
1720 if (v
== PROTO_UNDEF
)
1721 bpf_error("unknown ether proto '%s'", name
);
1741 static struct block
*
1742 gen_protochain(v
, proto
, dir
)
1747 #ifdef NO_PROTOCHAIN
1748 return gen_proto(v
, proto
, dir
);
1750 struct block
*b0
, *b
;
1751 struct slist
*s
[100];
1752 int fix2
, fix3
, fix4
, fix5
;
1753 int ahcheck
, again
, end
;
1755 int reg1
= alloc_reg();
1756 int reg2
= alloc_reg();
1758 memset(s
, 0, sizeof(s
));
1759 fix2
= fix3
= fix4
= fix5
= 0;
1766 b0
= gen_protochain(v
, Q_IP
, dir
);
1767 b
= gen_protochain(v
, Q_IPV6
, dir
);
1771 bpf_error("bad protocol applied for 'protochain'");
1775 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1778 * s[0] is a dummy entry to protect other BPF insn from damaged
1779 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1780 * hard to find interdependency made by jump table fixup.
1783 s
[i
] = new_stmt(0); /*dummy*/
1788 b0
= gen_linktype(ETHERTYPE_IP
);
1791 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1792 s
[i
]->s
.k
= off_nl
+ 9;
1794 /* X = ip->ip_hl << 2 */
1795 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1801 b0
= gen_linktype(ETHERTYPE_IPV6
);
1803 /* A = ip6->ip_nxt */
1804 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1805 s
[i
]->s
.k
= off_nl
+ 6;
1807 /* X = sizeof(struct ip6_hdr) */
1808 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1814 bpf_error("unsupported proto to gen_protochain");
1818 /* again: if (A == v) goto end; else fall through; */
1820 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1822 s
[i
]->s
.jt
= NULL
; /*later*/
1823 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1827 #ifndef IPPROTO_NONE
1828 #define IPPROTO_NONE 59
1830 /* if (A == IPPROTO_NONE) goto end */
1831 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1832 s
[i
]->s
.jt
= NULL
; /*later*/
1833 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1834 s
[i
]->s
.k
= IPPROTO_NONE
;
1835 s
[fix5
]->s
.jf
= s
[i
];
1840 if (proto
== Q_IPV6
) {
1841 int v6start
, v6end
, v6advance
, j
;
1844 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1845 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1846 s
[i
]->s
.jt
= NULL
; /*later*/
1847 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1848 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
1849 s
[fix2
]->s
.jf
= s
[i
];
1851 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1852 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1853 s
[i
]->s
.jt
= NULL
; /*later*/
1854 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1855 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
1857 /* if (A == IPPROTO_ROUTING) goto v6advance */
1858 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1859 s
[i
]->s
.jt
= NULL
; /*later*/
1860 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1861 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1863 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1864 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1865 s
[i
]->s
.jt
= NULL
; /*later*/
1866 s
[i
]->s
.jf
= NULL
; /*later*/
1867 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
1878 * X = X + (P[X] + 1) * 8;
1881 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1884 s
[i
] = new_stmt(BPF_ST
);
1888 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1892 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1894 /* A = P[X + packet head]; */
1895 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1899 s
[i
] = new_stmt(BPF_ST
);
1903 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1906 /* A = P[X + packet head] */
1907 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1911 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1915 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1919 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1922 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1926 /* goto again; (must use BPF_JA for backward jump) */
1927 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1928 s
[i
]->s
.k
= again
- i
- 1;
1929 s
[i
- 1]->s
.jf
= s
[i
];
1933 for (j
= v6start
; j
<= v6end
; j
++)
1934 s
[j
]->s
.jt
= s
[v6advance
];
1939 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1941 s
[fix2
]->s
.jf
= s
[i
];
1947 /* if (A == IPPROTO_AH) then fall through; else goto end; */
1948 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1949 s
[i
]->s
.jt
= NULL
; /*later*/
1950 s
[i
]->s
.jf
= NULL
; /*later*/
1951 s
[i
]->s
.k
= IPPROTO_AH
;
1953 s
[fix3
]->s
.jf
= s
[ahcheck
];
1960 * X = X + (P[X] + 2) * 4;
1963 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1966 s
[i
] = new_stmt(BPF_ST
);
1970 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1974 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1976 /* A = P[X + packet head]; */
1977 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1981 s
[i
] = new_stmt(BPF_ST
);
1985 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1988 /* A = P[X + packet head] */
1989 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1993 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1997 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2001 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2004 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2008 /* goto again; (must use BPF_JA for backward jump) */
2009 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2010 s
[i
]->s
.k
= again
- i
- 1;
2015 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2017 s
[fix2
]->s
.jt
= s
[end
];
2018 s
[fix4
]->s
.jf
= s
[end
];
2019 s
[fix5
]->s
.jt
= s
[end
];
2026 for (i
= 0; i
< max
- 1; i
++)
2027 s
[i
]->next
= s
[i
+ 1];
2028 s
[max
- 1]->next
= NULL
;
2033 b
= new_block(JMP(BPF_JEQ
));
2034 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2045 static struct block
*
2046 gen_proto(v
, proto
, dir
)
2051 struct block
*b0
, *b1
;
2053 if (dir
!= Q_DEFAULT
)
2054 bpf_error("direction applied to 'proto'");
2059 b0
= gen_proto(v
, Q_IP
, dir
);
2060 b1
= gen_proto(v
, Q_IPV6
, dir
);
2067 b0
= gen_linktype(ETHERTYPE_IP
);
2069 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2071 b1
= gen_protochain(v
, Q_IP
);
2077 bpf_error("arp does not encapsulate another protocol");
2081 bpf_error("rarp does not encapsulate another protocol");
2085 bpf_error("atalk encapsulation is not specifiable");
2089 bpf_error("decnet encapsulation is not specifiable");
2093 bpf_error("sca does not encapsulate another protocol");
2097 bpf_error("lat does not encapsulate another protocol");
2101 bpf_error("moprc does not encapsulate another protocol");
2105 bpf_error("mopdl does not encapsulate another protocol");
2109 return gen_linktype(v
);
2112 bpf_error("'udp proto' is bogus");
2116 bpf_error("'tcp proto' is bogus");
2120 bpf_error("'icmp proto' is bogus");
2124 bpf_error("'igmp proto' is bogus");
2128 bpf_error("'igrp proto' is bogus");
2132 bpf_error("'pim proto' is bogus");
2137 b0
= gen_linktype(ETHERTYPE_IPV6
);
2139 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2141 b1
= gen_protochain(v
, Q_IPV6
);
2147 bpf_error("'icmp6 proto' is bogus");
2151 bpf_error("'ah proto' is bogus");
2154 bpf_error("'ah proto' is bogus");
2165 register const char *name
;
2168 int proto
= q
.proto
;
2172 bpf_u_int32 mask
, addr
;
2174 bpf_u_int32
**alist
;
2177 struct sockaddr_in
*sin
;
2178 struct sockaddr_in6
*sin6
;
2179 struct addrinfo
*res
, *res0
;
2180 struct in6_addr mask128
;
2182 struct block
*b
, *tmp
;
2183 int port
, real_proto
;
2188 addr
= pcap_nametonetaddr(name
);
2190 bpf_error("unknown network '%s'", name
);
2191 /* Left justify network addr and calculate its network mask */
2193 while (addr
&& (addr
& 0xff000000) == 0) {
2197 return gen_host(addr
, mask
, proto
, dir
);
2201 if (proto
== Q_LINK
) {
2205 eaddr
= pcap_ether_hostton(name
);
2208 "unknown ether host '%s'", name
);
2209 return gen_ehostop(eaddr
, dir
);
2212 eaddr
= pcap_ether_hostton(name
);
2215 "unknown FDDI host '%s'", name
);
2216 return gen_fhostop(eaddr
, dir
);
2219 eaddr
= pcap_ether_hostton(name
);
2222 "unknown token ring host '%s'", name
);
2223 return gen_thostop(eaddr
, dir
);
2227 "only ethernet/FDDI/token ring supports link-level host name");
2230 } else if (proto
== Q_DECNET
) {
2231 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2233 * I don't think DECNET hosts can be multihomed, so
2234 * there is no need to build up a list of addresses
2236 return (gen_host(dn_addr
, 0, proto
, dir
));
2239 alist
= pcap_nametoaddr(name
);
2240 if (alist
== NULL
|| *alist
== NULL
)
2241 bpf_error("unknown host '%s'", name
);
2243 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2245 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2247 tmp
= gen_host(**alist
++, 0xffffffff,
2254 memset(&mask128
, 0xff, sizeof(mask128
));
2255 res0
= res
= pcap_nametoaddrinfo(name
);
2257 bpf_error("unknown host '%s'", name
);
2259 tproto
= tproto6
= proto
;
2260 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2264 for (res
= res0
; res
; res
= res
->ai_next
) {
2265 switch (res
->ai_family
) {
2267 if (tproto
== Q_IPV6
)
2270 sin
= (struct sockaddr_in
*)
2272 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2273 0xffffffff, tproto
, dir
);
2276 if (tproto6
== Q_IP
)
2279 sin6
= (struct sockaddr_in6
*)
2281 tmp
= gen_host6(&sin6
->sin6_addr
,
2282 &mask128
, tproto6
, dir
);
2291 bpf_error("unknown host '%s'%s", name
,
2292 (proto
== Q_DEFAULT
)
2294 : " for specified address family");
2301 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2302 bpf_error("illegal qualifier of 'port'");
2303 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2304 bpf_error("unknown port '%s'", name
);
2305 if (proto
== Q_UDP
) {
2306 if (real_proto
== IPPROTO_TCP
)
2307 bpf_error("port '%s' is tcp", name
);
2309 /* override PROTO_UNDEF */
2310 real_proto
= IPPROTO_UDP
;
2312 if (proto
== Q_TCP
) {
2313 if (real_proto
== IPPROTO_UDP
)
2314 bpf_error("port '%s' is udp", name
);
2316 /* override PROTO_UNDEF */
2317 real_proto
= IPPROTO_TCP
;
2320 return gen_port(port
, real_proto
, dir
);
2324 b
= gen_port(port
, real_proto
, dir
);
2325 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2332 eaddr
= pcap_ether_hostton(name
);
2334 bpf_error("unknown ether host: %s", name
);
2336 alist
= pcap_nametoaddr(name
);
2337 if (alist
== NULL
|| *alist
== NULL
)
2338 bpf_error("unknown host '%s'", name
);
2339 return gen_gateway(eaddr
, alist
, proto
, dir
);
2341 bpf_error("'gateway' not supported in this configuration");
2345 real_proto
= lookup_proto(name
, proto
);
2346 if (real_proto
>= 0)
2347 return gen_proto(real_proto
, proto
, dir
);
2349 bpf_error("unknown protocol: %s", name
);
2352 real_proto
= lookup_proto(name
, proto
);
2353 if (real_proto
>= 0)
2354 return gen_protochain(real_proto
, proto
, dir
);
2356 bpf_error("unknown protocol: %s", name
);
2368 gen_mcode(s1
, s2
, masklen
, q
)
2369 register const char *s1
, *s2
;
2370 register int masklen
;
2373 register int nlen
, mlen
;
2376 nlen
= __pcap_atoin(s1
, &n
);
2377 /* Promote short ipaddr */
2381 mlen
= __pcap_atoin(s2
, &m
);
2382 /* Promote short ipaddr */
2385 bpf_error("non-network bits set in \"%s mask %s\"",
2388 /* Convert mask len to mask */
2390 bpf_error("mask length must be <= 32");
2391 m
= 0xffffffff << (32 - masklen
);
2393 bpf_error("non-network bits set in \"%s/%d\"",
2400 return gen_host(n
, m
, q
.proto
, q
.dir
);
2403 bpf_error("Mask syntax for networks only");
2410 register const char *s
;
2415 int proto
= q
.proto
;
2421 else if (q
.proto
== Q_DECNET
)
2422 vlen
= __pcap_atodn(s
, &v
);
2424 vlen
= __pcap_atoin(s
, &v
);
2431 if (proto
== Q_DECNET
)
2432 return gen_host(v
, 0, proto
, dir
);
2433 else if (proto
== Q_LINK
) {
2434 bpf_error("illegal link layer address");
2437 if (s
== NULL
&& q
.addr
== Q_NET
) {
2438 /* Promote short net number */
2439 while (v
&& (v
& 0xff000000) == 0) {
2444 /* Promote short ipaddr */
2448 return gen_host(v
, mask
, proto
, dir
);
2453 proto
= IPPROTO_UDP
;
2454 else if (proto
== Q_TCP
)
2455 proto
= IPPROTO_TCP
;
2456 else if (proto
== Q_DEFAULT
)
2457 proto
= PROTO_UNDEF
;
2459 bpf_error("illegal qualifier of 'port'");
2462 return gen_port((int)v
, proto
, dir
);
2466 b
= gen_port((int)v
, proto
, dir
);
2467 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2473 bpf_error("'gateway' requires a name");
2477 return gen_proto((int)v
, proto
, dir
);
2480 return gen_protochain((int)v
, proto
, dir
);
2495 gen_mcode6(s1
, s2
, masklen
, q
)
2496 register const char *s1
, *s2
;
2497 register int masklen
;
2500 struct addrinfo
*res
;
2501 struct in6_addr
*addr
;
2502 struct in6_addr mask
;
2507 bpf_error("no mask %s supported", s2
);
2509 res
= pcap_nametoaddrinfo(s1
);
2511 bpf_error("invalid ip6 address %s", s1
);
2513 bpf_error("%s resolved to multiple address", s1
);
2514 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2516 if (sizeof(mask
) * 8 < masklen
)
2517 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2518 memset(&mask
, 0xff, masklen
/ 8);
2520 mask
.s6_addr
[masklen
/ 8] =
2521 (0xff << (8 - masklen
% 8)) & 0xff;
2524 a
= (u_int32_t
*)addr
;
2525 m
= (u_int32_t
*)&mask
;
2526 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2527 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2528 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2536 bpf_error("Mask syntax for networks only");
2540 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2545 bpf_error("invalid qualifier against IPv6 address");
2553 register const u_char
*eaddr
;
2556 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2557 if (linktype
== DLT_EN10MB
)
2558 return gen_ehostop(eaddr
, (int)q
.dir
);
2559 if (linktype
== DLT_FDDI
)
2560 return gen_fhostop(eaddr
, (int)q
.dir
);
2561 if (linktype
== DLT_IEEE802
)
2562 return gen_thostop(eaddr
, (int)q
.dir
);
2564 bpf_error("ethernet address used in non-ether expression");
2570 struct slist
*s0
, *s1
;
2573 * This is definitely not the best way to do this, but the
2574 * lists will rarely get long.
2581 static struct slist
*
2587 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2592 static struct slist
*
2598 s
= new_stmt(BPF_LD
|BPF_MEM
);
2604 gen_load(proto
, index
, size
)
2609 struct slist
*s
, *tmp
;
2611 int regno
= alloc_reg();
2613 free_reg(index
->regno
);
2617 bpf_error("data size must be 1, 2, or 4");
2633 bpf_error("unsupported index operation");
2636 s
= xfer_to_x(index
);
2637 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2639 sappend(index
->s
, s
);
2654 /* XXX Note that we assume a fixed link header here. */
2655 s
= xfer_to_x(index
);
2656 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2659 sappend(index
->s
, s
);
2661 b
= gen_proto_abbrev(proto
);
2663 gen_and(index
->b
, b
);
2673 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2675 sappend(s
, xfer_to_a(index
));
2676 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2677 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2678 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2680 sappend(index
->s
, s
);
2682 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2684 gen_and(index
->b
, b
);
2686 gen_and(gen_proto_abbrev(Q_IP
), b
);
2692 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2696 index
->regno
= regno
;
2697 s
= new_stmt(BPF_ST
);
2699 sappend(index
->s
, s
);
2705 gen_relation(code
, a0
, a1
, reversed
)
2707 struct arth
*a0
, *a1
;
2710 struct slist
*s0
, *s1
, *s2
;
2711 struct block
*b
, *tmp
;
2715 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2716 b
= new_block(JMP(code
));
2717 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2718 reversed
= !reversed
;
2719 b
->s
.k
= 0x80000000;
2727 sappend(a0
->s
, a1
->s
);
2731 free_reg(a0
->regno
);
2732 free_reg(a1
->regno
);
2734 /* 'and' together protocol checks */
2737 gen_and(a0
->b
, tmp
= a1
->b
);
2753 int regno
= alloc_reg();
2754 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2757 s
= new_stmt(BPF_LD
|BPF_LEN
);
2758 s
->next
= new_stmt(BPF_ST
);
2759 s
->next
->s
.k
= regno
;
2774 a
= (struct arth
*)newchunk(sizeof(*a
));
2778 s
= new_stmt(BPF_LD
|BPF_IMM
);
2780 s
->next
= new_stmt(BPF_ST
);
2796 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2799 s
= new_stmt(BPF_ST
);
2807 gen_arth(code
, a0
, a1
)
2809 struct arth
*a0
, *a1
;
2811 struct slist
*s0
, *s1
, *s2
;
2815 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2820 sappend(a0
->s
, a1
->s
);
2822 free_reg(a1
->regno
);
2824 s0
= new_stmt(BPF_ST
);
2825 a0
->regno
= s0
->s
.k
= alloc_reg();
2832 * Here we handle simple allocation of the scratch registers.
2833 * If too many registers are alloc'd, the allocator punts.
2835 static int regused
[BPF_MEMWORDS
];
2839 * Return the next free register.
2844 int n
= BPF_MEMWORDS
;
2847 if (regused
[curreg
])
2848 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2850 regused
[curreg
] = 1;
2854 bpf_error("too many registers needed to evaluate expression");
2859 * Return a register to the table so it can
2869 static struct block
*
2876 s
= new_stmt(BPF_LD
|BPF_LEN
);
2877 b
= new_block(JMP(jmp
));
2888 return gen_len(BPF_JGE
, n
);
2897 b
= gen_len(BPF_JGT
, n
);
2904 gen_byteop(op
, idx
, val
)
2915 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2918 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2919 b
->s
.code
= JMP(BPF_JGE
);
2924 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2925 b
->s
.code
= JMP(BPF_JGT
);
2929 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
2933 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2937 b
= new_block(JMP(BPF_JEQ
));
2945 gen_broadcast(proto
)
2948 bpf_u_int32 hostmask
;
2949 struct block
*b0
, *b1
, *b2
;
2950 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2956 if (linktype
== DLT_EN10MB
)
2957 return gen_ehostop(ebroadcast
, Q_DST
);
2958 if (linktype
== DLT_FDDI
)
2959 return gen_fhostop(ebroadcast
, Q_DST
);
2960 if (linktype
== DLT_IEEE802
)
2961 return gen_thostop(ebroadcast
, Q_DST
);
2962 bpf_error("not a broadcast link");
2966 b0
= gen_linktype(ETHERTYPE_IP
);
2967 hostmask
= ~netmask
;
2968 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
2969 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
2970 (bpf_int32
)(~0 & hostmask
), hostmask
);
2975 bpf_error("only ether/ip broadcast filters supported");
2979 gen_multicast(proto
)
2982 register struct block
*b0
, *b1
;
2983 register struct slist
*s
;
2989 if (linktype
== DLT_EN10MB
) {
2990 /* ether[0] & 1 != 0 */
2991 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2993 b0
= new_block(JMP(BPF_JSET
));
2999 if (linktype
== DLT_FDDI
) {
3000 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3001 /* fddi[1] & 1 != 0 */
3002 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3004 b0
= new_block(JMP(BPF_JSET
));
3010 /* TODO - check how token ring handles multicast */
3011 /* if (linktype == DLT_IEEE802) ... */
3013 /* Link not known to support multicasts */
3017 b0
= gen_linktype(ETHERTYPE_IP
);
3018 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3019 b1
->s
.code
= JMP(BPF_JGE
);
3025 b0
= gen_linktype(ETHERTYPE_IPV6
);
3026 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3031 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3035 * generate command for inbound/outbound. It's here so we can
3036 * make it link-type specific. 'dir' = 0 implies "inbound",
3037 * = 1 implies "outbound".
3043 register struct block
*b0
;
3045 b0
= gen_relation(BPF_JEQ
,
3046 gen_load(Q_LINK
, gen_loadi(0), 1),
3053 * support IEEE 802.1Q VLAN trunk over ethernet
3059 static u_int orig_linktype
= -1, orig_nl
= -1;
3063 * Change the offsets to point to the type and data fields within
3064 * the VLAN packet. This is somewhat of a kludge.
3066 if (orig_nl
== (u_int
)-1) {
3067 orig_linktype
= off_linktype
; /* save original values */
3078 bpf_error("no VLAN support for data link type %d",
3084 /* check for VLAN */
3085 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3087 /* If a specific VLAN is requested, check VLAN id */
3088 if (vlan_num
>= 0) {
3091 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);