]>
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.129 2000-10-28 09:30:21 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_ISO_LSAP 0xfe
66 #ifdef HAVE_OS_PROTO_H
70 #define JMP(c) ((c)|BPF_JMP|BPF_K)
73 static jmp_buf top_ctx
;
74 static pcap_t
*bpf_pcap
;
78 int pcap_fddipad
= PCAP_FDDIPAD
;
85 bpf_error(const char *fmt
, ...)
92 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
99 static void init_linktype(int);
101 static int alloc_reg(void);
102 static void free_reg(int);
104 static struct block
*root
;
107 * We divy out chunks of memory rather than call malloc each time so
108 * we don't have to worry about leaking memory. It's probably
109 * not a big deal if all this memory was wasted but it this ever
110 * goes into a library that would probably not be a good idea.
113 #define CHUNK0SIZE 1024
119 static struct chunk chunks
[NCHUNKS
];
120 static int cur_chunk
;
122 static void *newchunk(u_int
);
123 static void freechunks(void);
124 static inline struct block
*new_block(int);
125 static inline struct slist
*new_stmt(int);
126 static struct block
*gen_retblk(int);
127 static inline void syntax(void);
129 static void backpatch(struct block
*, struct block
*);
130 static void merge(struct block
*, struct block
*);
131 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
132 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
133 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
134 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
135 static struct block
*gen_uncond(int);
136 static inline struct block
*gen_true(void);
137 static inline struct block
*gen_false(void);
138 static struct block
*gen_linktype(int);
139 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
140 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
142 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
144 static struct block
*gen_ehostop(const u_char
*, int);
145 static struct block
*gen_fhostop(const u_char
*, int);
146 static struct block
*gen_thostop(const u_char
*, int);
147 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
148 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
150 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
153 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
155 static struct block
*gen_ipfrag(void);
156 static struct block
*gen_portatom(int, bpf_int32
);
158 static struct block
*gen_portatom6(int, bpf_int32
);
160 struct block
*gen_portop(int, int, int);
161 static struct block
*gen_port(int, int, int);
163 struct block
*gen_portop6(int, int, int);
164 static struct block
*gen_port6(int, int, int);
166 static int lookup_proto(const char *, int);
167 static struct block
*gen_protochain(int, int, int);
168 static struct block
*gen_proto(int, int, int);
169 static struct slist
*xfer_to_x(struct arth
*);
170 static struct slist
*xfer_to_a(struct arth
*);
171 static struct block
*gen_len(int, int);
181 /* XXX Round up to nearest long. */
182 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
184 /* XXX Round up to structure boundary. */
188 cp
= &chunks
[cur_chunk
];
189 if (n
> cp
->n_left
) {
190 ++cp
, k
= ++cur_chunk
;
192 bpf_error("out of memory");
193 size
= CHUNK0SIZE
<< k
;
194 cp
->m
= (void *)malloc(size
);
195 memset((char *)cp
->m
, 0, size
);
198 bpf_error("out of memory");
201 return (void *)((char *)cp
->m
+ cp
->n_left
);
210 for (i
= 0; i
< NCHUNKS
; ++i
)
211 if (chunks
[i
].m
!= NULL
) {
218 * A strdup whose allocations are freed after code generation is over.
222 register const char *s
;
224 int n
= strlen(s
) + 1;
225 char *cp
= newchunk(n
);
231 static inline struct block
*
237 p
= (struct block
*)newchunk(sizeof(*p
));
244 static inline struct slist
*
250 p
= (struct slist
*)newchunk(sizeof(*p
));
256 static struct block
*
260 struct block
*b
= new_block(BPF_RET
|BPF_K
);
269 bpf_error("syntax error in filter expression");
272 static bpf_u_int32 netmask
;
277 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
278 char *buf
, int optimize
, bpf_u_int32 mask
)
287 if (setjmp(top_ctx
)) {
295 /* On Linux we do not use the BPF filter to truncate the packet
296 * since the kernel provides other ways for that. In fact if we
297 * are using the packet filter for that duty we will be unable
298 * to acquire the original packet size. -- Torsten */
300 snaplen
= pcap_snapshot(p
);
305 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
306 "snaplen of 0 rejects all packets");
310 lex_init(buf
? buf
: "");
311 init_linktype(pcap_datalink(p
));
318 root
= gen_retblk(snaplen
);
320 if (optimize
&& !no_optimize
) {
323 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
324 bpf_error("expression rejects all packets");
326 program
->bf_insns
= icode_to_fcode(root
, &len
);
327 program
->bf_len
= len
;
335 * entry point for using the compiler with no pcap open
336 * pass in all the stuff that is needed explicitly instead.
339 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
340 struct bpf_program
*program
,
341 char *buf
, int optimize
, bpf_u_int32 mask
)
349 if (setjmp(top_ctx
)) {
356 /* XXX needed? I don't grok the use of globals here. */
357 snaplen
= snaplen_arg
;
359 lex_init(buf
? buf
: "");
360 init_linktype(linktype_arg
);
367 root
= gen_retblk(snaplen_arg
);
372 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
373 bpf_error("expression rejects all packets");
375 program
->bf_insns
= icode_to_fcode(root
, &len
);
376 program
->bf_len
= len
;
383 * Clean up a "struct bpf_program" by freeing all the memory allocated
387 pcap_freecode(struct bpf_program
*program
)
390 if (program
->bf_insns
!= NULL
) {
391 free((char *)program
->bf_insns
);
392 program
->bf_insns
= NULL
;
397 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
398 * which of the jt and jf fields has been resolved and which is a pointer
399 * back to another unresolved block (or nil). At least one of the fields
400 * in each block is already resolved.
403 backpatch(list
, target
)
404 struct block
*list
, *target
;
421 * Merge the lists in b0 and b1, using the 'sense' field to indicate
422 * which of jt and jf is the link.
426 struct block
*b0
, *b1
;
428 register struct block
**p
= &b0
;
430 /* Find end of list. */
432 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
434 /* Concatenate the lists. */
442 backpatch(p
, gen_retblk(snaplen
));
443 p
->sense
= !p
->sense
;
444 backpatch(p
, gen_retblk(0));
450 struct block
*b0
, *b1
;
452 backpatch(b0
, b1
->head
);
453 b0
->sense
= !b0
->sense
;
454 b1
->sense
= !b1
->sense
;
456 b1
->sense
= !b1
->sense
;
462 struct block
*b0
, *b1
;
464 b0
->sense
= !b0
->sense
;
465 backpatch(b0
, b1
->head
);
466 b0
->sense
= !b0
->sense
;
475 b
->sense
= !b
->sense
;
478 static struct block
*
479 gen_cmp(offset
, size
, v
)
486 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
489 b
= new_block(JMP(BPF_JEQ
));
496 static struct block
*
497 gen_cmp_gt(offset
, size
, v
)
504 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
507 b
= new_block(JMP(BPF_JGT
));
514 static struct block
*
515 gen_mcmp(offset
, size
, v
, mask
)
520 struct block
*b
= gen_cmp(offset
, size
, v
);
523 if (mask
!= 0xffffffff) {
524 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
531 static struct block
*
532 gen_bcmp(offset
, size
, v
)
533 register u_int offset
, size
;
534 register const u_char
*v
;
536 register struct block
*b
, *tmp
;
540 register const u_char
*p
= &v
[size
- 4];
541 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
542 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
544 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
551 register const u_char
*p
= &v
[size
- 2];
552 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
554 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
561 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
570 * Various code constructs need to know the layout of the data link
571 * layer. These variables give the necessary offsets. off_linktype
572 * is set to -1 for no encapsulation, in which case, IP is assumed.
574 static u_int off_linktype
;
593 * SLIP doesn't have a link level type. The 16 byte
594 * header is hacked into our SLIP driver.
601 /* XXX this may be the same as the DLT_PPP_BSDOS case */
626 * FDDI doesn't really have a link-level type field.
627 * We assume that SSAP = SNAP is being used and pick
628 * out the encapsulated Ethernet type.
630 * XXX - should we generate code to check for SNAP?
634 off_linktype
+= pcap_fddipad
;
638 off_nl
+= pcap_fddipad
;
644 * Token Ring doesn't really have a link-level type field.
645 * We assume that SSAP = SNAP is being used and pick
646 * out the encapsulated Ethernet type.
648 * XXX - should we generate code to check for SNAP?
650 * XXX - the header is actually variable-length.
651 * Some various Linux patched versions gave 38
652 * as "off_linktype" and 40 as "off_nl"; however,
653 * if a token ring packet has *no* routing
654 * information, i.e. is not source-routed, the correct
655 * values are 20 and 22, as they are in the vanilla code.
657 * A packet is source-routed iff the uppermost bit
658 * of the first byte of the source address, at an
659 * offset of 8, has the uppermost bit set. If the
660 * packet is source-routed, the total number of bytes
661 * of routing information is 2 plus bits 0x1F00 of
662 * the 16-bit value at an offset of 14 (shifted right
663 * 8 - figure out which byte that is).
669 case DLT_ATM_RFC1483
:
671 * assume routed, non-ISO PDUs
672 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
683 case DLT_ATM_CLIP
: /* Linux ATM defines this */
688 bpf_error("unknown data link type 0x%x", linktype
);
692 static struct block
*
699 s
= new_stmt(BPF_LD
|BPF_IMM
);
701 b
= new_block(JMP(BPF_JEQ
));
707 static inline struct block
*
710 return gen_uncond(1);
713 static inline struct block
*
716 return gen_uncond(0);
719 static struct block
*
723 struct block
*b0
, *b1
;
725 /* If we're not using encapsulation, we're done */
726 if (off_linktype
== -1)
733 * XXX - handle other LLC-encapsulated protocols here
740 * OSI protocols always use 802.2 encapsulation.
742 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
744 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (long)
745 ((LLC_ISO_LSAP
<< 8) | LLC_ISO_LSAP
));
749 case ETHERTYPE_ATALK
:
752 * EtherTalk (AppleTalk protocols on Ethernet link
753 * layer) may use 802.2 encapsulation.
757 * Check for 802.2 encapsulation (EtherTalk phase 2?);
758 * we check for an Ethernet type field less than
759 * 1500, which means it's an 802.3 length field.
761 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
765 * 802.2-encapsulated ETHERTYPE_ATALK packets are
766 * SNAP packets with an organization code of
767 * 0x080007 (Apple, for Appletalk) and a protocol
768 * type of ETHERTYPE_ATALK (Appletalk).
770 * 802.2-encapsulated ETHERTYPE_AARP packets are
771 * SNAP packets with an organization code of
772 * 0x000000 (encapsulated Ethernet) and a protocol
773 * type of ETHERTYPE_AARP (Appletalk ARP).
775 if (proto
== ETHERTYPE_ATALK
)
776 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
777 else /* proto == ETHERTYPE_AARP */
778 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
782 * Check for Ethernet encapsulation (Ethertalk
783 * phase 1?); we just check for the Ethernet
786 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
799 * We use Ethernet protocol types inside libpcap;
800 * map them to the corresponding PPP protocol types.
805 proto
= PPP_IP
; /* XXX was 0x21 */
818 case ETHERTYPE_ATALK
:
830 * We use Ethernet protocol types inside libpcap;
831 * map them to the corresponding PPP protocol types.
836 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
837 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
839 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
854 case ETHERTYPE_ATALK
:
866 if (proto
== ETHERTYPE_IP
)
867 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET
)));
869 else if (proto
== ETHERTYPE_IPV6
)
870 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET6
)));
875 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
879 * Check for an LLC SNAP packet with a given organization code and
880 * protocol type; we check the entire contents of the 802.2 LLC and
881 * snap headers, checking for a DSAP of 0xAA, an SSAP of 0xAA, and
882 * a control field of 0x03 in the LLC header, and for the specified
883 * organization code and protocol type in the SNAP header.
885 static struct block
*
886 gen_snap(orgcode
, ptype
, offset
)
893 snapblock
[0] = 0xAA; /* DSAP = SNAP */
894 snapblock
[1] = 0xAA; /* SSAP = SNAP */
895 snapblock
[2] = 0x03; /* control = UI */
896 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
897 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
898 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
899 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
900 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
901 return gen_bcmp(offset
, 8, snapblock
);
904 static struct block
*
905 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
909 u_int src_off
, dst_off
;
911 struct block
*b0
, *b1
;
925 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
926 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
932 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
933 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
940 b0
= gen_linktype(proto
);
941 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
947 static struct block
*
948 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
949 struct in6_addr
*addr
;
950 struct in6_addr
*mask
;
952 u_int src_off
, dst_off
;
954 struct block
*b0
, *b1
;
969 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
970 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
976 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
977 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
984 /* this order is important */
985 a
= (u_int32_t
*)addr
;
986 m
= (u_int32_t
*)mask
;
987 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
988 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
990 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
992 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
994 b0
= gen_linktype(proto
);
1000 static struct block
*
1001 gen_ehostop(eaddr
, dir
)
1002 register const u_char
*eaddr
;
1005 register struct block
*b0
, *b1
;
1009 return gen_bcmp(6, 6, eaddr
);
1012 return gen_bcmp(0, 6, eaddr
);
1015 b0
= gen_ehostop(eaddr
, Q_SRC
);
1016 b1
= gen_ehostop(eaddr
, Q_DST
);
1022 b0
= gen_ehostop(eaddr
, Q_SRC
);
1023 b1
= gen_ehostop(eaddr
, Q_DST
);
1032 * Like gen_ehostop, but for DLT_FDDI
1034 static struct block
*
1035 gen_fhostop(eaddr
, dir
)
1036 register const u_char
*eaddr
;
1039 struct block
*b0
, *b1
;
1044 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1046 return gen_bcmp(6 + 1, 6, eaddr
);
1051 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1053 return gen_bcmp(0 + 1, 6, eaddr
);
1057 b0
= gen_fhostop(eaddr
, Q_SRC
);
1058 b1
= gen_fhostop(eaddr
, Q_DST
);
1064 b0
= gen_fhostop(eaddr
, Q_SRC
);
1065 b1
= gen_fhostop(eaddr
, Q_DST
);
1074 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1076 static struct block
*
1077 gen_thostop(eaddr
, dir
)
1078 register const u_char
*eaddr
;
1081 register struct block
*b0
, *b1
;
1085 return gen_bcmp(8, 6, eaddr
);
1088 return gen_bcmp(2, 6, eaddr
);
1091 b0
= gen_thostop(eaddr
, Q_SRC
);
1092 b1
= gen_thostop(eaddr
, Q_DST
);
1098 b0
= gen_thostop(eaddr
, Q_SRC
);
1099 b1
= gen_thostop(eaddr
, Q_DST
);
1108 * This is quite tricky because there may be pad bytes in front of the
1109 * DECNET header, and then there are two possible data packet formats that
1110 * carry both src and dst addresses, plus 5 packet types in a format that
1111 * carries only the src node, plus 2 types that use a different format and
1112 * also carry just the src node.
1116 * Instead of doing those all right, we just look for data packets with
1117 * 0 or 1 bytes of padding. If you want to look at other packets, that
1118 * will require a lot more hacking.
1120 * To add support for filtering on DECNET "areas" (network numbers)
1121 * one would want to add a "mask" argument to this routine. That would
1122 * make the filter even more inefficient, although one could be clever
1123 * and not generate masking instructions if the mask is 0xFFFF.
1125 static struct block
*
1126 gen_dnhostop(addr
, dir
, base_off
)
1131 struct block
*b0
, *b1
, *b2
, *tmp
;
1132 u_int offset_lh
; /* offset if long header is received */
1133 u_int offset_sh
; /* offset if short header is received */
1138 offset_sh
= 1; /* follows flags */
1139 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1143 offset_sh
= 3; /* follows flags, dstnode */
1144 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1148 /* Inefficient because we do our Calvinball dance twice */
1149 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1150 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1156 /* Inefficient because we do our Calvinball dance twice */
1157 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1158 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1163 bpf_error("ISO host filtering not implemented");
1168 b0
= gen_linktype(ETHERTYPE_DN
);
1169 /* Check for pad = 1, long header case */
1170 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1171 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1172 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1173 BPF_H
, (bpf_int32
)ntohs(addr
));
1175 /* Check for pad = 0, long header case */
1176 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1177 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1180 /* Check for pad = 1, short header case */
1181 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1182 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1183 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1184 BPF_H
, (bpf_int32
)ntohs(addr
));
1187 /* Check for pad = 0, short header case */
1188 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1189 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1193 /* Combine with test for linktype */
1198 static struct block
*
1199 gen_host(addr
, mask
, proto
, dir
)
1205 struct block
*b0
, *b1
;
1210 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1211 if (off_linktype
!= -1) {
1212 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1214 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1220 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1221 off_nl
+ 12, off_nl
+ 16);
1224 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1225 off_nl
+ 14, off_nl
+ 24);
1228 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1229 off_nl
+ 14, off_nl
+ 24);
1232 bpf_error("'tcp' modifier applied to host");
1235 bpf_error("'udp' modifier applied to host");
1238 bpf_error("'icmp' modifier applied to host");
1241 bpf_error("'igmp' modifier applied to host");
1244 bpf_error("'igrp' modifier applied to host");
1247 bpf_error("'pim' modifier applied to host");
1250 bpf_error("ATALK host filtering not implemented");
1253 bpf_error("AARP host filtering not implemented");
1256 return gen_dnhostop(addr
, dir
, off_nl
);
1259 bpf_error("SCA host filtering not implemented");
1262 bpf_error("LAT host filtering not implemented");
1265 bpf_error("MOPDL host filtering not implemented");
1268 bpf_error("MOPRC host filtering not implemented");
1272 bpf_error("'ip6' modifier applied to ip host");
1275 bpf_error("'icmp6' modifier applied to host");
1279 bpf_error("'ah' modifier applied to host");
1282 bpf_error("'esp' modifier applied to host");
1291 static struct block
*
1292 gen_host6(addr
, mask
, proto
, dir
)
1293 struct in6_addr
*addr
;
1294 struct in6_addr
*mask
;
1301 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1304 bpf_error("'ip' modifier applied to ip6 host");
1307 bpf_error("'rarp' modifier applied to ip6 host");
1310 bpf_error("'arp' modifier applied to ip6 host");
1313 bpf_error("'tcp' modifier applied to host");
1316 bpf_error("'udp' modifier applied to host");
1319 bpf_error("'icmp' modifier applied to host");
1322 bpf_error("'igmp' modifier applied to host");
1325 bpf_error("'igrp' modifier applied to host");
1328 bpf_error("'pim' modifier applied to host");
1331 bpf_error("ATALK host filtering not implemented");
1334 bpf_error("AARP host filtering not implemented");
1337 bpf_error("'decnet' modifier applied to ip6 host");
1340 bpf_error("SCA host filtering not implemented");
1343 bpf_error("LAT host filtering not implemented");
1346 bpf_error("MOPDL host filtering not implemented");
1349 bpf_error("MOPRC host filtering not implemented");
1352 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1353 off_nl
+ 8, off_nl
+ 24);
1356 bpf_error("'icmp6' modifier applied to host");
1359 bpf_error("'ah' modifier applied to host");
1362 bpf_error("'esp' modifier applied to host");
1372 static struct block
*
1373 gen_gateway(eaddr
, alist
, proto
, dir
)
1374 const u_char
*eaddr
;
1375 bpf_u_int32
**alist
;
1379 struct block
*b0
, *b1
, *tmp
;
1382 bpf_error("direction applied to 'gateway'");
1389 if (linktype
== DLT_EN10MB
)
1390 b0
= gen_ehostop(eaddr
, Q_OR
);
1391 else if (linktype
== DLT_FDDI
)
1392 b0
= gen_fhostop(eaddr
, Q_OR
);
1393 else if (linktype
== DLT_IEEE802
)
1394 b0
= gen_thostop(eaddr
, Q_OR
);
1397 "'gateway' supported only on ethernet, FDDI or token ring");
1399 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1401 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1409 bpf_error("illegal modifier of 'gateway'");
1415 gen_proto_abbrev(proto
)
1426 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1428 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1434 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1436 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1442 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1445 #ifndef IPPROTO_IGMP
1446 #define IPPROTO_IGMP 2
1450 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1453 #ifndef IPPROTO_IGRP
1454 #define IPPROTO_IGRP 9
1457 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1461 #define IPPROTO_PIM 103
1465 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1467 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1473 b1
= gen_linktype(ETHERTYPE_IP
);
1477 b1
= gen_linktype(ETHERTYPE_ARP
);
1481 b1
= gen_linktype(ETHERTYPE_REVARP
);
1485 bpf_error("link layer applied in wrong context");
1488 b1
= gen_linktype(ETHERTYPE_ATALK
);
1492 b1
= gen_linktype(ETHERTYPE_AARP
);
1496 b1
= gen_linktype(ETHERTYPE_DN
);
1500 b1
= gen_linktype(ETHERTYPE_SCA
);
1504 b1
= gen_linktype(ETHERTYPE_LAT
);
1508 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1512 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1517 b1
= gen_linktype(ETHERTYPE_IPV6
);
1520 #ifndef IPPROTO_ICMPV6
1521 #define IPPROTO_ICMPV6 58
1524 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1529 #define IPPROTO_AH 51
1532 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1534 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1540 #define IPPROTO_ESP 50
1543 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1545 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1551 b1
= gen_linktype(LLC_ISO_LSAP
);
1555 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
1559 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
1568 static struct block
*
1575 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1576 s
->s
.k
= off_nl
+ 6;
1577 b
= new_block(JMP(BPF_JSET
));
1585 static struct block
*
1586 gen_portatom(off
, v
)
1593 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1596 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1597 s
->next
->s
.k
= off_nl
+ off
;
1599 b
= new_block(JMP(BPF_JEQ
));
1607 static struct block
*
1608 gen_portatom6(off
, v
)
1612 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1617 gen_portop(port
, proto
, dir
)
1618 int port
, proto
, dir
;
1620 struct block
*b0
, *b1
, *tmp
;
1622 /* ip proto 'proto' */
1623 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1629 b1
= gen_portatom(0, (bpf_int32
)port
);
1633 b1
= gen_portatom(2, (bpf_int32
)port
);
1638 tmp
= gen_portatom(0, (bpf_int32
)port
);
1639 b1
= gen_portatom(2, (bpf_int32
)port
);
1644 tmp
= gen_portatom(0, (bpf_int32
)port
);
1645 b1
= gen_portatom(2, (bpf_int32
)port
);
1657 static struct block
*
1658 gen_port(port
, ip_proto
, dir
)
1663 struct block
*b0
, *b1
, *tmp
;
1665 /* ether proto ip */
1666 b0
= gen_linktype(ETHERTYPE_IP
);
1671 b1
= gen_portop(port
, ip_proto
, dir
);
1675 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1676 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1689 gen_portop6(port
, proto
, dir
)
1690 int port
, proto
, dir
;
1692 struct block
*b0
, *b1
, *tmp
;
1694 /* ip proto 'proto' */
1695 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1699 b1
= gen_portatom6(0, (bpf_int32
)port
);
1703 b1
= gen_portatom6(2, (bpf_int32
)port
);
1708 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1709 b1
= gen_portatom6(2, (bpf_int32
)port
);
1714 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1715 b1
= gen_portatom6(2, (bpf_int32
)port
);
1727 static struct block
*
1728 gen_port6(port
, ip_proto
, dir
)
1733 struct block
*b0
, *b1
, *tmp
;
1735 /* ether proto ip */
1736 b0
= gen_linktype(ETHERTYPE_IPV6
);
1741 b1
= gen_portop6(port
, ip_proto
, dir
);
1745 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1746 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1759 lookup_proto(name
, proto
)
1760 register const char *name
;
1769 v
= pcap_nametoproto(name
);
1770 if (v
== PROTO_UNDEF
)
1771 bpf_error("unknown ip proto '%s'", name
);
1775 /* XXX should look up h/w protocol type based on linktype */
1776 v
= pcap_nametoeproto(name
);
1777 if (v
== PROTO_UNDEF
)
1778 bpf_error("unknown ether proto '%s'", name
);
1798 static struct block
*
1799 gen_protochain(v
, proto
, dir
)
1804 #ifdef NO_PROTOCHAIN
1805 return gen_proto(v
, proto
, dir
);
1807 struct block
*b0
, *b
;
1808 struct slist
*s
[100];
1809 int fix2
, fix3
, fix4
, fix5
;
1810 int ahcheck
, again
, end
;
1812 int reg1
= alloc_reg();
1813 int reg2
= alloc_reg();
1815 memset(s
, 0, sizeof(s
));
1816 fix2
= fix3
= fix4
= fix5
= 0;
1823 b0
= gen_protochain(v
, Q_IP
, dir
);
1824 b
= gen_protochain(v
, Q_IPV6
, dir
);
1828 bpf_error("bad protocol applied for 'protochain'");
1832 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1835 * s[0] is a dummy entry to protect other BPF insn from damaged
1836 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1837 * hard to find interdependency made by jump table fixup.
1840 s
[i
] = new_stmt(0); /*dummy*/
1845 b0
= gen_linktype(ETHERTYPE_IP
);
1848 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1849 s
[i
]->s
.k
= off_nl
+ 9;
1851 /* X = ip->ip_hl << 2 */
1852 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1858 b0
= gen_linktype(ETHERTYPE_IPV6
);
1860 /* A = ip6->ip_nxt */
1861 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1862 s
[i
]->s
.k
= off_nl
+ 6;
1864 /* X = sizeof(struct ip6_hdr) */
1865 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1871 bpf_error("unsupported proto to gen_protochain");
1875 /* again: if (A == v) goto end; else fall through; */
1877 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1879 s
[i
]->s
.jt
= NULL
; /*later*/
1880 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1884 #ifndef IPPROTO_NONE
1885 #define IPPROTO_NONE 59
1887 /* if (A == IPPROTO_NONE) goto end */
1888 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1889 s
[i
]->s
.jt
= NULL
; /*later*/
1890 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1891 s
[i
]->s
.k
= IPPROTO_NONE
;
1892 s
[fix5
]->s
.jf
= s
[i
];
1897 if (proto
== Q_IPV6
) {
1898 int v6start
, v6end
, v6advance
, j
;
1901 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1902 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1903 s
[i
]->s
.jt
= NULL
; /*later*/
1904 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1905 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
1906 s
[fix2
]->s
.jf
= s
[i
];
1908 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1909 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1910 s
[i
]->s
.jt
= NULL
; /*later*/
1911 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1912 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
1914 /* if (A == IPPROTO_ROUTING) goto v6advance */
1915 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1916 s
[i
]->s
.jt
= NULL
; /*later*/
1917 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1918 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1920 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1921 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1922 s
[i
]->s
.jt
= NULL
; /*later*/
1923 s
[i
]->s
.jf
= NULL
; /*later*/
1924 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
1935 * X = X + (P[X] + 1) * 8;
1938 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1941 s
[i
] = new_stmt(BPF_ST
);
1945 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1949 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1951 /* A = P[X + packet head]; */
1952 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1956 s
[i
] = new_stmt(BPF_ST
);
1960 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1963 /* A = P[X + packet head] */
1964 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1968 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1972 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1976 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1979 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1983 /* goto again; (must use BPF_JA for backward jump) */
1984 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1985 s
[i
]->s
.k
= again
- i
- 1;
1986 s
[i
- 1]->s
.jf
= s
[i
];
1990 for (j
= v6start
; j
<= v6end
; j
++)
1991 s
[j
]->s
.jt
= s
[v6advance
];
1996 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1998 s
[fix2
]->s
.jf
= s
[i
];
2004 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2005 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2006 s
[i
]->s
.jt
= NULL
; /*later*/
2007 s
[i
]->s
.jf
= NULL
; /*later*/
2008 s
[i
]->s
.k
= IPPROTO_AH
;
2010 s
[fix3
]->s
.jf
= s
[ahcheck
];
2017 * X = X + (P[X] + 2) * 4;
2020 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2023 s
[i
] = new_stmt(BPF_ST
);
2027 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2031 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2033 /* A = P[X + packet head]; */
2034 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2038 s
[i
] = new_stmt(BPF_ST
);
2042 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
2045 /* A = P[X + packet head] */
2046 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2050 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2054 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2058 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2061 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2065 /* goto again; (must use BPF_JA for backward jump) */
2066 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2067 s
[i
]->s
.k
= again
- i
- 1;
2072 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2074 s
[fix2
]->s
.jt
= s
[end
];
2075 s
[fix4
]->s
.jf
= s
[end
];
2076 s
[fix5
]->s
.jt
= s
[end
];
2083 for (i
= 0; i
< max
- 1; i
++)
2084 s
[i
]->next
= s
[i
+ 1];
2085 s
[max
- 1]->next
= NULL
;
2090 b
= new_block(JMP(BPF_JEQ
));
2091 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2102 static struct block
*
2103 gen_proto(v
, proto
, dir
)
2108 struct block
*b0
, *b1
;
2110 if (dir
!= Q_DEFAULT
)
2111 bpf_error("direction applied to 'proto'");
2116 b0
= gen_proto(v
, Q_IP
, dir
);
2117 b1
= gen_proto(v
, Q_IPV6
, dir
);
2124 b0
= gen_linktype(ETHERTYPE_IP
);
2126 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2128 b1
= gen_protochain(v
, Q_IP
);
2134 b0
= gen_linktype(LLC_ISO_LSAP
);
2135 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2140 bpf_error("arp does not encapsulate another protocol");
2144 bpf_error("rarp does not encapsulate another protocol");
2148 bpf_error("atalk encapsulation is not specifiable");
2152 bpf_error("decnet encapsulation is not specifiable");
2156 bpf_error("sca does not encapsulate another protocol");
2160 bpf_error("lat does not encapsulate another protocol");
2164 bpf_error("moprc does not encapsulate another protocol");
2168 bpf_error("mopdl does not encapsulate another protocol");
2172 return gen_linktype(v
);
2175 bpf_error("'udp proto' is bogus");
2179 bpf_error("'tcp proto' is bogus");
2183 bpf_error("'icmp proto' is bogus");
2187 bpf_error("'igmp proto' is bogus");
2191 bpf_error("'igrp proto' is bogus");
2195 bpf_error("'pim proto' is bogus");
2200 b0
= gen_linktype(ETHERTYPE_IPV6
);
2202 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2204 b1
= gen_protochain(v
, Q_IPV6
);
2210 bpf_error("'icmp6 proto' is bogus");
2214 bpf_error("'ah proto' is bogus");
2217 bpf_error("'ah proto' is bogus");
2228 register const char *name
;
2231 int proto
= q
.proto
;
2235 bpf_u_int32 mask
, addr
;
2237 bpf_u_int32
**alist
;
2240 struct sockaddr_in
*sin
;
2241 struct sockaddr_in6
*sin6
;
2242 struct addrinfo
*res
, *res0
;
2243 struct in6_addr mask128
;
2245 struct block
*b
, *tmp
;
2246 int port
, real_proto
;
2251 addr
= pcap_nametonetaddr(name
);
2253 bpf_error("unknown network '%s'", name
);
2254 /* Left justify network addr and calculate its network mask */
2256 while (addr
&& (addr
& 0xff000000) == 0) {
2260 return gen_host(addr
, mask
, proto
, dir
);
2264 if (proto
== Q_LINK
) {
2268 eaddr
= pcap_ether_hostton(name
);
2271 "unknown ether host '%s'", name
);
2272 return gen_ehostop(eaddr
, dir
);
2275 eaddr
= pcap_ether_hostton(name
);
2278 "unknown FDDI host '%s'", name
);
2279 return gen_fhostop(eaddr
, dir
);
2282 eaddr
= pcap_ether_hostton(name
);
2285 "unknown token ring host '%s'", name
);
2286 return gen_thostop(eaddr
, dir
);
2290 "only ethernet/FDDI/token ring supports link-level host name");
2293 } else if (proto
== Q_DECNET
) {
2294 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2296 * I don't think DECNET hosts can be multihomed, so
2297 * there is no need to build up a list of addresses
2299 return (gen_host(dn_addr
, 0, proto
, dir
));
2302 alist
= pcap_nametoaddr(name
);
2303 if (alist
== NULL
|| *alist
== NULL
)
2304 bpf_error("unknown host '%s'", name
);
2306 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2308 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2310 tmp
= gen_host(**alist
++, 0xffffffff,
2317 memset(&mask128
, 0xff, sizeof(mask128
));
2318 res0
= res
= pcap_nametoaddrinfo(name
);
2320 bpf_error("unknown host '%s'", name
);
2322 tproto
= tproto6
= proto
;
2323 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2327 for (res
= res0
; res
; res
= res
->ai_next
) {
2328 switch (res
->ai_family
) {
2330 if (tproto
== Q_IPV6
)
2333 sin
= (struct sockaddr_in
*)
2335 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2336 0xffffffff, tproto
, dir
);
2339 if (tproto6
== Q_IP
)
2342 sin6
= (struct sockaddr_in6
*)
2344 tmp
= gen_host6(&sin6
->sin6_addr
,
2345 &mask128
, tproto6
, dir
);
2354 bpf_error("unknown host '%s'%s", name
,
2355 (proto
== Q_DEFAULT
)
2357 : " for specified address family");
2364 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2365 bpf_error("illegal qualifier of 'port'");
2366 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2367 bpf_error("unknown port '%s'", name
);
2368 if (proto
== Q_UDP
) {
2369 if (real_proto
== IPPROTO_TCP
)
2370 bpf_error("port '%s' is tcp", name
);
2372 /* override PROTO_UNDEF */
2373 real_proto
= IPPROTO_UDP
;
2375 if (proto
== Q_TCP
) {
2376 if (real_proto
== IPPROTO_UDP
)
2377 bpf_error("port '%s' is udp", name
);
2379 /* override PROTO_UNDEF */
2380 real_proto
= IPPROTO_TCP
;
2383 return gen_port(port
, real_proto
, dir
);
2387 b
= gen_port(port
, real_proto
, dir
);
2388 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2395 eaddr
= pcap_ether_hostton(name
);
2397 bpf_error("unknown ether host: %s", name
);
2399 alist
= pcap_nametoaddr(name
);
2400 if (alist
== NULL
|| *alist
== NULL
)
2401 bpf_error("unknown host '%s'", name
);
2402 return gen_gateway(eaddr
, alist
, proto
, dir
);
2404 bpf_error("'gateway' not supported in this configuration");
2408 real_proto
= lookup_proto(name
, proto
);
2409 if (real_proto
>= 0)
2410 return gen_proto(real_proto
, proto
, dir
);
2412 bpf_error("unknown protocol: %s", name
);
2415 real_proto
= lookup_proto(name
, proto
);
2416 if (real_proto
>= 0)
2417 return gen_protochain(real_proto
, proto
, dir
);
2419 bpf_error("unknown protocol: %s", name
);
2431 gen_mcode(s1
, s2
, masklen
, q
)
2432 register const char *s1
, *s2
;
2433 register int masklen
;
2436 register int nlen
, mlen
;
2439 nlen
= __pcap_atoin(s1
, &n
);
2440 /* Promote short ipaddr */
2444 mlen
= __pcap_atoin(s2
, &m
);
2445 /* Promote short ipaddr */
2448 bpf_error("non-network bits set in \"%s mask %s\"",
2451 /* Convert mask len to mask */
2453 bpf_error("mask length must be <= 32");
2454 m
= 0xffffffff << (32 - masklen
);
2456 bpf_error("non-network bits set in \"%s/%d\"",
2463 return gen_host(n
, m
, q
.proto
, q
.dir
);
2466 bpf_error("Mask syntax for networks only");
2473 register const char *s
;
2478 int proto
= q
.proto
;
2484 else if (q
.proto
== Q_DECNET
)
2485 vlen
= __pcap_atodn(s
, &v
);
2487 vlen
= __pcap_atoin(s
, &v
);
2494 if (proto
== Q_DECNET
)
2495 return gen_host(v
, 0, proto
, dir
);
2496 else if (proto
== Q_LINK
) {
2497 bpf_error("illegal link layer address");
2500 if (s
== NULL
&& q
.addr
== Q_NET
) {
2501 /* Promote short net number */
2502 while (v
&& (v
& 0xff000000) == 0) {
2507 /* Promote short ipaddr */
2511 return gen_host(v
, mask
, proto
, dir
);
2516 proto
= IPPROTO_UDP
;
2517 else if (proto
== Q_TCP
)
2518 proto
= IPPROTO_TCP
;
2519 else if (proto
== Q_DEFAULT
)
2520 proto
= PROTO_UNDEF
;
2522 bpf_error("illegal qualifier of 'port'");
2525 return gen_port((int)v
, proto
, dir
);
2529 b
= gen_port((int)v
, proto
, dir
);
2530 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2536 bpf_error("'gateway' requires a name");
2540 return gen_proto((int)v
, proto
, dir
);
2543 return gen_protochain((int)v
, proto
, dir
);
2558 gen_mcode6(s1
, s2
, masklen
, q
)
2559 register const char *s1
, *s2
;
2560 register int masklen
;
2563 struct addrinfo
*res
;
2564 struct in6_addr
*addr
;
2565 struct in6_addr mask
;
2570 bpf_error("no mask %s supported", s2
);
2572 res
= pcap_nametoaddrinfo(s1
);
2574 bpf_error("invalid ip6 address %s", s1
);
2576 bpf_error("%s resolved to multiple address", s1
);
2577 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2579 if (sizeof(mask
) * 8 < masklen
)
2580 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2581 memset(&mask
, 0xff, masklen
/ 8);
2583 mask
.s6_addr
[masklen
/ 8] =
2584 (0xff << (8 - masklen
% 8)) & 0xff;
2587 a
= (u_int32_t
*)addr
;
2588 m
= (u_int32_t
*)&mask
;
2589 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2590 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2591 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2599 bpf_error("Mask syntax for networks only");
2603 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2608 bpf_error("invalid qualifier against IPv6 address");
2616 register const u_char
*eaddr
;
2619 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2620 if (linktype
== DLT_EN10MB
)
2621 return gen_ehostop(eaddr
, (int)q
.dir
);
2622 if (linktype
== DLT_FDDI
)
2623 return gen_fhostop(eaddr
, (int)q
.dir
);
2624 if (linktype
== DLT_IEEE802
)
2625 return gen_thostop(eaddr
, (int)q
.dir
);
2627 bpf_error("ethernet address used in non-ether expression");
2633 struct slist
*s0
, *s1
;
2636 * This is definitely not the best way to do this, but the
2637 * lists will rarely get long.
2644 static struct slist
*
2650 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2655 static struct slist
*
2661 s
= new_stmt(BPF_LD
|BPF_MEM
);
2667 gen_load(proto
, index
, size
)
2672 struct slist
*s
, *tmp
;
2674 int regno
= alloc_reg();
2676 free_reg(index
->regno
);
2680 bpf_error("data size must be 1, 2, or 4");
2696 bpf_error("unsupported index operation");
2699 s
= xfer_to_x(index
);
2700 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2702 sappend(index
->s
, s
);
2717 /* XXX Note that we assume a fixed link header here. */
2718 s
= xfer_to_x(index
);
2719 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2722 sappend(index
->s
, s
);
2724 b
= gen_proto_abbrev(proto
);
2726 gen_and(index
->b
, b
);
2736 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2738 sappend(s
, xfer_to_a(index
));
2739 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2740 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2741 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2743 sappend(index
->s
, s
);
2745 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2747 gen_and(index
->b
, b
);
2749 gen_and(gen_proto_abbrev(Q_IP
), b
);
2755 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2759 index
->regno
= regno
;
2760 s
= new_stmt(BPF_ST
);
2762 sappend(index
->s
, s
);
2768 gen_relation(code
, a0
, a1
, reversed
)
2770 struct arth
*a0
, *a1
;
2773 struct slist
*s0
, *s1
, *s2
;
2774 struct block
*b
, *tmp
;
2778 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2779 b
= new_block(JMP(code
));
2780 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2781 reversed
= !reversed
;
2782 b
->s
.k
= 0x80000000;
2790 sappend(a0
->s
, a1
->s
);
2794 free_reg(a0
->regno
);
2795 free_reg(a1
->regno
);
2797 /* 'and' together protocol checks */
2800 gen_and(a0
->b
, tmp
= a1
->b
);
2816 int regno
= alloc_reg();
2817 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2820 s
= new_stmt(BPF_LD
|BPF_LEN
);
2821 s
->next
= new_stmt(BPF_ST
);
2822 s
->next
->s
.k
= regno
;
2837 a
= (struct arth
*)newchunk(sizeof(*a
));
2841 s
= new_stmt(BPF_LD
|BPF_IMM
);
2843 s
->next
= new_stmt(BPF_ST
);
2859 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2862 s
= new_stmt(BPF_ST
);
2870 gen_arth(code
, a0
, a1
)
2872 struct arth
*a0
, *a1
;
2874 struct slist
*s0
, *s1
, *s2
;
2878 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2883 sappend(a0
->s
, a1
->s
);
2885 free_reg(a1
->regno
);
2887 s0
= new_stmt(BPF_ST
);
2888 a0
->regno
= s0
->s
.k
= alloc_reg();
2895 * Here we handle simple allocation of the scratch registers.
2896 * If too many registers are alloc'd, the allocator punts.
2898 static int regused
[BPF_MEMWORDS
];
2902 * Return the next free register.
2907 int n
= BPF_MEMWORDS
;
2910 if (regused
[curreg
])
2911 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2913 regused
[curreg
] = 1;
2917 bpf_error("too many registers needed to evaluate expression");
2922 * Return a register to the table so it can
2932 static struct block
*
2939 s
= new_stmt(BPF_LD
|BPF_LEN
);
2940 b
= new_block(JMP(jmp
));
2951 return gen_len(BPF_JGE
, n
);
2955 * Actually, this is less than or equal.
2963 b
= gen_len(BPF_JGT
, n
);
2970 gen_byteop(op
, idx
, val
)
2981 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2984 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2985 b
->s
.code
= JMP(BPF_JGE
);
2990 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2991 b
->s
.code
= JMP(BPF_JGT
);
2995 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
2999 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3003 b
= new_block(JMP(BPF_JEQ
));
3011 gen_broadcast(proto
)
3014 bpf_u_int32 hostmask
;
3015 struct block
*b0
, *b1
, *b2
;
3016 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3022 if (linktype
== DLT_EN10MB
)
3023 return gen_ehostop(ebroadcast
, Q_DST
);
3024 if (linktype
== DLT_FDDI
)
3025 return gen_fhostop(ebroadcast
, Q_DST
);
3026 if (linktype
== DLT_IEEE802
)
3027 return gen_thostop(ebroadcast
, Q_DST
);
3028 bpf_error("not a broadcast link");
3032 b0
= gen_linktype(ETHERTYPE_IP
);
3033 hostmask
= ~netmask
;
3034 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3035 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3036 (bpf_int32
)(~0 & hostmask
), hostmask
);
3041 bpf_error("only ether/ip broadcast filters supported");
3045 gen_multicast(proto
)
3048 register struct block
*b0
, *b1
;
3049 register struct slist
*s
;
3055 if (linktype
== DLT_EN10MB
) {
3056 /* ether[0] & 1 != 0 */
3057 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3059 b0
= new_block(JMP(BPF_JSET
));
3065 if (linktype
== DLT_FDDI
) {
3066 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3067 /* fddi[1] & 1 != 0 */
3068 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3070 b0
= new_block(JMP(BPF_JSET
));
3076 /* TODO - check how token ring handles multicast */
3077 /* if (linktype == DLT_IEEE802) ... */
3079 /* Link not known to support multicasts */
3083 b0
= gen_linktype(ETHERTYPE_IP
);
3084 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3085 b1
->s
.code
= JMP(BPF_JGE
);
3091 b0
= gen_linktype(ETHERTYPE_IPV6
);
3092 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3097 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3101 * generate command for inbound/outbound. It's here so we can
3102 * make it link-type specific. 'dir' = 0 implies "inbound",
3103 * = 1 implies "outbound".
3109 register struct block
*b0
;
3111 b0
= gen_relation(BPF_JEQ
,
3112 gen_load(Q_LINK
, gen_loadi(0), 1),
3119 * support IEEE 802.1Q VLAN trunk over ethernet
3125 static u_int orig_linktype
= -1, orig_nl
= -1;
3129 * Change the offsets to point to the type and data fields within
3130 * the VLAN packet. This is somewhat of a kludge.
3132 if (orig_nl
== (u_int
)-1) {
3133 orig_linktype
= off_linktype
; /* save original values */
3144 bpf_error("no VLAN support for data link type %d",
3150 /* check for VLAN */
3151 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3153 /* If a specific VLAN is requested, check VLAN id */
3154 if (vlan_num
>= 0) {
3157 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);