]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
15099e5c18e4099b70f53bd5bbd93b2392cd34ec
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.131 2000-10-28 10:05:46 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");
1285 bpf_error("ISO host filtering not implemented");
1288 bpf_error("'esis' modifier applied to host");
1291 bpf_error("'isis' modifier applied to host");
1300 static struct block
*
1301 gen_host6(addr
, mask
, proto
, dir
)
1302 struct in6_addr
*addr
;
1303 struct in6_addr
*mask
;
1310 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1313 bpf_error("'ip' modifier applied to ip6 host");
1316 bpf_error("'rarp' modifier applied to ip6 host");
1319 bpf_error("'arp' modifier applied to ip6 host");
1322 bpf_error("'tcp' modifier applied to host");
1325 bpf_error("'udp' modifier applied to host");
1328 bpf_error("'icmp' modifier applied to host");
1331 bpf_error("'igmp' modifier applied to host");
1334 bpf_error("'igrp' modifier applied to host");
1337 bpf_error("'pim' modifier applied to host");
1340 bpf_error("ATALK host filtering not implemented");
1343 bpf_error("AARP host filtering not implemented");
1346 bpf_error("'decnet' modifier applied to ip6 host");
1349 bpf_error("SCA host filtering not implemented");
1352 bpf_error("LAT host filtering not implemented");
1355 bpf_error("MOPDL host filtering not implemented");
1358 bpf_error("MOPRC host filtering not implemented");
1361 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1362 off_nl
+ 8, off_nl
+ 24);
1365 bpf_error("'icmp6' modifier applied to host");
1368 bpf_error("'ah' modifier applied to host");
1371 bpf_error("'esp' modifier applied to host");
1374 bpf_error("ISO host filtering not implemented");
1377 bpf_error("'esis' modifier applied to host");
1380 bpf_error("'isis' modifier applied to host");
1390 static struct block
*
1391 gen_gateway(eaddr
, alist
, proto
, dir
)
1392 const u_char
*eaddr
;
1393 bpf_u_int32
**alist
;
1397 struct block
*b0
, *b1
, *tmp
;
1400 bpf_error("direction applied to 'gateway'");
1407 if (linktype
== DLT_EN10MB
)
1408 b0
= gen_ehostop(eaddr
, Q_OR
);
1409 else if (linktype
== DLT_FDDI
)
1410 b0
= gen_fhostop(eaddr
, Q_OR
);
1411 else if (linktype
== DLT_IEEE802
)
1412 b0
= gen_thostop(eaddr
, Q_OR
);
1415 "'gateway' supported only on ethernet, FDDI or token ring");
1417 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1419 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1427 bpf_error("illegal modifier of 'gateway'");
1433 gen_proto_abbrev(proto
)
1444 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1446 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1452 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1454 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1460 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1463 #ifndef IPPROTO_IGMP
1464 #define IPPROTO_IGMP 2
1468 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1471 #ifndef IPPROTO_IGRP
1472 #define IPPROTO_IGRP 9
1475 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1479 #define IPPROTO_PIM 103
1483 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1485 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1491 b1
= gen_linktype(ETHERTYPE_IP
);
1495 b1
= gen_linktype(ETHERTYPE_ARP
);
1499 b1
= gen_linktype(ETHERTYPE_REVARP
);
1503 bpf_error("link layer applied in wrong context");
1506 b1
= gen_linktype(ETHERTYPE_ATALK
);
1510 b1
= gen_linktype(ETHERTYPE_AARP
);
1514 b1
= gen_linktype(ETHERTYPE_DN
);
1518 b1
= gen_linktype(ETHERTYPE_SCA
);
1522 b1
= gen_linktype(ETHERTYPE_LAT
);
1526 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1530 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1535 b1
= gen_linktype(ETHERTYPE_IPV6
);
1538 #ifndef IPPROTO_ICMPV6
1539 #define IPPROTO_ICMPV6 58
1542 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1547 #define IPPROTO_AH 51
1550 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1552 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1558 #define IPPROTO_ESP 50
1561 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1563 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1569 b1
= gen_linktype(LLC_ISO_LSAP
);
1573 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
1577 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
1586 static struct block
*
1593 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1594 s
->s
.k
= off_nl
+ 6;
1595 b
= new_block(JMP(BPF_JSET
));
1603 static struct block
*
1604 gen_portatom(off
, v
)
1611 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1614 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1615 s
->next
->s
.k
= off_nl
+ off
;
1617 b
= new_block(JMP(BPF_JEQ
));
1625 static struct block
*
1626 gen_portatom6(off
, v
)
1630 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1635 gen_portop(port
, proto
, dir
)
1636 int port
, proto
, dir
;
1638 struct block
*b0
, *b1
, *tmp
;
1640 /* ip proto 'proto' */
1641 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1647 b1
= gen_portatom(0, (bpf_int32
)port
);
1651 b1
= gen_portatom(2, (bpf_int32
)port
);
1656 tmp
= gen_portatom(0, (bpf_int32
)port
);
1657 b1
= gen_portatom(2, (bpf_int32
)port
);
1662 tmp
= gen_portatom(0, (bpf_int32
)port
);
1663 b1
= gen_portatom(2, (bpf_int32
)port
);
1675 static struct block
*
1676 gen_port(port
, ip_proto
, dir
)
1681 struct block
*b0
, *b1
, *tmp
;
1683 /* ether proto ip */
1684 b0
= gen_linktype(ETHERTYPE_IP
);
1689 b1
= gen_portop(port
, ip_proto
, dir
);
1693 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1694 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1707 gen_portop6(port
, proto
, dir
)
1708 int port
, proto
, dir
;
1710 struct block
*b0
, *b1
, *tmp
;
1712 /* ip proto 'proto' */
1713 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1717 b1
= gen_portatom6(0, (bpf_int32
)port
);
1721 b1
= gen_portatom6(2, (bpf_int32
)port
);
1726 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1727 b1
= gen_portatom6(2, (bpf_int32
)port
);
1732 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1733 b1
= gen_portatom6(2, (bpf_int32
)port
);
1745 static struct block
*
1746 gen_port6(port
, ip_proto
, dir
)
1751 struct block
*b0
, *b1
, *tmp
;
1753 /* ether proto ip */
1754 b0
= gen_linktype(ETHERTYPE_IPV6
);
1759 b1
= gen_portop6(port
, ip_proto
, dir
);
1763 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1764 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1777 lookup_proto(name
, proto
)
1778 register const char *name
;
1787 v
= pcap_nametoproto(name
);
1788 if (v
== PROTO_UNDEF
)
1789 bpf_error("unknown ip proto '%s'", name
);
1793 /* XXX should look up h/w protocol type based on linktype */
1794 v
= pcap_nametoeproto(name
);
1795 if (v
== PROTO_UNDEF
)
1796 bpf_error("unknown ether proto '%s'", name
);
1800 if (strcmp(name
, "esis") == 0)
1802 else if (strcmp(name
, "isis") == 0)
1805 bpf_error("unknown osi proto '%s'", name
);
1825 static struct block
*
1826 gen_protochain(v
, proto
, dir
)
1831 #ifdef NO_PROTOCHAIN
1832 return gen_proto(v
, proto
, dir
);
1834 struct block
*b0
, *b
;
1835 struct slist
*s
[100];
1836 int fix2
, fix3
, fix4
, fix5
;
1837 int ahcheck
, again
, end
;
1839 int reg1
= alloc_reg();
1840 int reg2
= alloc_reg();
1842 memset(s
, 0, sizeof(s
));
1843 fix2
= fix3
= fix4
= fix5
= 0;
1850 b0
= gen_protochain(v
, Q_IP
, dir
);
1851 b
= gen_protochain(v
, Q_IPV6
, dir
);
1855 bpf_error("bad protocol applied for 'protochain'");
1859 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1862 * s[0] is a dummy entry to protect other BPF insn from damaged
1863 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1864 * hard to find interdependency made by jump table fixup.
1867 s
[i
] = new_stmt(0); /*dummy*/
1872 b0
= gen_linktype(ETHERTYPE_IP
);
1875 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1876 s
[i
]->s
.k
= off_nl
+ 9;
1878 /* X = ip->ip_hl << 2 */
1879 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1885 b0
= gen_linktype(ETHERTYPE_IPV6
);
1887 /* A = ip6->ip_nxt */
1888 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1889 s
[i
]->s
.k
= off_nl
+ 6;
1891 /* X = sizeof(struct ip6_hdr) */
1892 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1898 bpf_error("unsupported proto to gen_protochain");
1902 /* again: if (A == v) goto end; else fall through; */
1904 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1906 s
[i
]->s
.jt
= NULL
; /*later*/
1907 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1911 #ifndef IPPROTO_NONE
1912 #define IPPROTO_NONE 59
1914 /* if (A == IPPROTO_NONE) goto end */
1915 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_NONE
;
1919 s
[fix5
]->s
.jf
= s
[i
];
1924 if (proto
== Q_IPV6
) {
1925 int v6start
, v6end
, v6advance
, j
;
1928 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1929 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1930 s
[i
]->s
.jt
= NULL
; /*later*/
1931 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1932 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
1933 s
[fix2
]->s
.jf
= s
[i
];
1935 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1936 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1937 s
[i
]->s
.jt
= NULL
; /*later*/
1938 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1939 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
1941 /* if (A == IPPROTO_ROUTING) goto v6advance */
1942 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1943 s
[i
]->s
.jt
= NULL
; /*later*/
1944 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1945 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1947 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1948 s
[i
- 1]->s
.jf
= 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_FRAGMENT
;
1962 * X = X + (P[X] + 1) * 8;
1965 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1968 s
[i
] = new_stmt(BPF_ST
);
1972 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1976 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1978 /* A = P[X + packet head]; */
1979 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1983 s
[i
] = new_stmt(BPF_ST
);
1987 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1990 /* A = P[X + packet head] */
1991 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1995 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1999 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2003 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2006 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2010 /* goto again; (must use BPF_JA for backward jump) */
2011 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2012 s
[i
]->s
.k
= again
- i
- 1;
2013 s
[i
- 1]->s
.jf
= s
[i
];
2017 for (j
= v6start
; j
<= v6end
; j
++)
2018 s
[j
]->s
.jt
= s
[v6advance
];
2023 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2025 s
[fix2
]->s
.jf
= s
[i
];
2031 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2032 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2033 s
[i
]->s
.jt
= NULL
; /*later*/
2034 s
[i
]->s
.jf
= NULL
; /*later*/
2035 s
[i
]->s
.k
= IPPROTO_AH
;
2037 s
[fix3
]->s
.jf
= s
[ahcheck
];
2044 * X = X + (P[X] + 2) * 4;
2047 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2050 s
[i
] = new_stmt(BPF_ST
);
2054 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2058 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2060 /* A = P[X + packet head]; */
2061 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2065 s
[i
] = new_stmt(BPF_ST
);
2069 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
2072 /* A = P[X + packet head] */
2073 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2077 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2081 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2085 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2088 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2092 /* goto again; (must use BPF_JA for backward jump) */
2093 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2094 s
[i
]->s
.k
= again
- i
- 1;
2099 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2101 s
[fix2
]->s
.jt
= s
[end
];
2102 s
[fix4
]->s
.jf
= s
[end
];
2103 s
[fix5
]->s
.jt
= s
[end
];
2110 for (i
= 0; i
< max
- 1; i
++)
2111 s
[i
]->next
= s
[i
+ 1];
2112 s
[max
- 1]->next
= NULL
;
2117 b
= new_block(JMP(BPF_JEQ
));
2118 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2129 static struct block
*
2130 gen_proto(v
, proto
, dir
)
2135 struct block
*b0
, *b1
;
2137 if (dir
!= Q_DEFAULT
)
2138 bpf_error("direction applied to 'proto'");
2143 b0
= gen_proto(v
, Q_IP
, dir
);
2144 b1
= gen_proto(v
, Q_IPV6
, dir
);
2151 b0
= gen_linktype(ETHERTYPE_IP
);
2153 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2155 b1
= gen_protochain(v
, Q_IP
);
2161 b0
= gen_linktype(LLC_ISO_LSAP
);
2162 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2167 bpf_error("arp does not encapsulate another protocol");
2171 bpf_error("rarp does not encapsulate another protocol");
2175 bpf_error("atalk encapsulation is not specifiable");
2179 bpf_error("decnet encapsulation is not specifiable");
2183 bpf_error("sca does not encapsulate another protocol");
2187 bpf_error("lat does not encapsulate another protocol");
2191 bpf_error("moprc does not encapsulate another protocol");
2195 bpf_error("mopdl does not encapsulate another protocol");
2199 return gen_linktype(v
);
2202 bpf_error("'udp proto' is bogus");
2206 bpf_error("'tcp proto' is bogus");
2210 bpf_error("'icmp proto' is bogus");
2214 bpf_error("'igmp proto' is bogus");
2218 bpf_error("'igrp proto' is bogus");
2222 bpf_error("'pim proto' is bogus");
2227 b0
= gen_linktype(ETHERTYPE_IPV6
);
2229 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2231 b1
= gen_protochain(v
, Q_IPV6
);
2237 bpf_error("'icmp6 proto' is bogus");
2241 bpf_error("'ah proto' is bogus");
2244 bpf_error("'ah proto' is bogus");
2255 register const char *name
;
2258 int proto
= q
.proto
;
2262 bpf_u_int32 mask
, addr
;
2264 bpf_u_int32
**alist
;
2267 struct sockaddr_in
*sin
;
2268 struct sockaddr_in6
*sin6
;
2269 struct addrinfo
*res
, *res0
;
2270 struct in6_addr mask128
;
2272 struct block
*b
, *tmp
;
2273 int port
, real_proto
;
2278 addr
= pcap_nametonetaddr(name
);
2280 bpf_error("unknown network '%s'", name
);
2281 /* Left justify network addr and calculate its network mask */
2283 while (addr
&& (addr
& 0xff000000) == 0) {
2287 return gen_host(addr
, mask
, proto
, dir
);
2291 if (proto
== Q_LINK
) {
2295 eaddr
= pcap_ether_hostton(name
);
2298 "unknown ether host '%s'", name
);
2299 return gen_ehostop(eaddr
, dir
);
2302 eaddr
= pcap_ether_hostton(name
);
2305 "unknown FDDI host '%s'", name
);
2306 return gen_fhostop(eaddr
, dir
);
2309 eaddr
= pcap_ether_hostton(name
);
2312 "unknown token ring host '%s'", name
);
2313 return gen_thostop(eaddr
, dir
);
2317 "only ethernet/FDDI/token ring supports link-level host name");
2320 } else if (proto
== Q_DECNET
) {
2321 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2323 * I don't think DECNET hosts can be multihomed, so
2324 * there is no need to build up a list of addresses
2326 return (gen_host(dn_addr
, 0, proto
, dir
));
2329 alist
= pcap_nametoaddr(name
);
2330 if (alist
== NULL
|| *alist
== NULL
)
2331 bpf_error("unknown host '%s'", name
);
2333 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2335 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2337 tmp
= gen_host(**alist
++, 0xffffffff,
2344 memset(&mask128
, 0xff, sizeof(mask128
));
2345 res0
= res
= pcap_nametoaddrinfo(name
);
2347 bpf_error("unknown host '%s'", name
);
2349 tproto
= tproto6
= proto
;
2350 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2354 for (res
= res0
; res
; res
= res
->ai_next
) {
2355 switch (res
->ai_family
) {
2357 if (tproto
== Q_IPV6
)
2360 sin
= (struct sockaddr_in
*)
2362 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2363 0xffffffff, tproto
, dir
);
2366 if (tproto6
== Q_IP
)
2369 sin6
= (struct sockaddr_in6
*)
2371 tmp
= gen_host6(&sin6
->sin6_addr
,
2372 &mask128
, tproto6
, dir
);
2381 bpf_error("unknown host '%s'%s", name
,
2382 (proto
== Q_DEFAULT
)
2384 : " for specified address family");
2391 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2392 bpf_error("illegal qualifier of 'port'");
2393 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2394 bpf_error("unknown port '%s'", name
);
2395 if (proto
== Q_UDP
) {
2396 if (real_proto
== IPPROTO_TCP
)
2397 bpf_error("port '%s' is tcp", name
);
2399 /* override PROTO_UNDEF */
2400 real_proto
= IPPROTO_UDP
;
2402 if (proto
== Q_TCP
) {
2403 if (real_proto
== IPPROTO_UDP
)
2404 bpf_error("port '%s' is udp", name
);
2406 /* override PROTO_UNDEF */
2407 real_proto
= IPPROTO_TCP
;
2410 return gen_port(port
, real_proto
, dir
);
2414 b
= gen_port(port
, real_proto
, dir
);
2415 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2422 eaddr
= pcap_ether_hostton(name
);
2424 bpf_error("unknown ether host: %s", name
);
2426 alist
= pcap_nametoaddr(name
);
2427 if (alist
== NULL
|| *alist
== NULL
)
2428 bpf_error("unknown host '%s'", name
);
2429 return gen_gateway(eaddr
, alist
, proto
, dir
);
2431 bpf_error("'gateway' not supported in this configuration");
2435 real_proto
= lookup_proto(name
, proto
);
2436 if (real_proto
>= 0)
2437 return gen_proto(real_proto
, proto
, dir
);
2439 bpf_error("unknown protocol: %s", name
);
2442 real_proto
= lookup_proto(name
, proto
);
2443 if (real_proto
>= 0)
2444 return gen_protochain(real_proto
, proto
, dir
);
2446 bpf_error("unknown protocol: %s", name
);
2458 gen_mcode(s1
, s2
, masklen
, q
)
2459 register const char *s1
, *s2
;
2460 register int masklen
;
2463 register int nlen
, mlen
;
2466 nlen
= __pcap_atoin(s1
, &n
);
2467 /* Promote short ipaddr */
2471 mlen
= __pcap_atoin(s2
, &m
);
2472 /* Promote short ipaddr */
2475 bpf_error("non-network bits set in \"%s mask %s\"",
2478 /* Convert mask len to mask */
2480 bpf_error("mask length must be <= 32");
2481 m
= 0xffffffff << (32 - masklen
);
2483 bpf_error("non-network bits set in \"%s/%d\"",
2490 return gen_host(n
, m
, q
.proto
, q
.dir
);
2493 bpf_error("Mask syntax for networks only");
2500 register const char *s
;
2505 int proto
= q
.proto
;
2511 else if (q
.proto
== Q_DECNET
)
2512 vlen
= __pcap_atodn(s
, &v
);
2514 vlen
= __pcap_atoin(s
, &v
);
2521 if (proto
== Q_DECNET
)
2522 return gen_host(v
, 0, proto
, dir
);
2523 else if (proto
== Q_LINK
) {
2524 bpf_error("illegal link layer address");
2527 if (s
== NULL
&& q
.addr
== Q_NET
) {
2528 /* Promote short net number */
2529 while (v
&& (v
& 0xff000000) == 0) {
2534 /* Promote short ipaddr */
2538 return gen_host(v
, mask
, proto
, dir
);
2543 proto
= IPPROTO_UDP
;
2544 else if (proto
== Q_TCP
)
2545 proto
= IPPROTO_TCP
;
2546 else if (proto
== Q_DEFAULT
)
2547 proto
= PROTO_UNDEF
;
2549 bpf_error("illegal qualifier of 'port'");
2552 return gen_port((int)v
, proto
, dir
);
2556 b
= gen_port((int)v
, proto
, dir
);
2557 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2563 bpf_error("'gateway' requires a name");
2567 return gen_proto((int)v
, proto
, dir
);
2570 return gen_protochain((int)v
, proto
, dir
);
2585 gen_mcode6(s1
, s2
, masklen
, q
)
2586 register const char *s1
, *s2
;
2587 register int masklen
;
2590 struct addrinfo
*res
;
2591 struct in6_addr
*addr
;
2592 struct in6_addr mask
;
2597 bpf_error("no mask %s supported", s2
);
2599 res
= pcap_nametoaddrinfo(s1
);
2601 bpf_error("invalid ip6 address %s", s1
);
2603 bpf_error("%s resolved to multiple address", s1
);
2604 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2606 if (sizeof(mask
) * 8 < masklen
)
2607 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2608 memset(&mask
, 0xff, masklen
/ 8);
2610 mask
.s6_addr
[masklen
/ 8] =
2611 (0xff << (8 - masklen
% 8)) & 0xff;
2614 a
= (u_int32_t
*)addr
;
2615 m
= (u_int32_t
*)&mask
;
2616 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2617 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2618 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2626 bpf_error("Mask syntax for networks only");
2630 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2635 bpf_error("invalid qualifier against IPv6 address");
2643 register const u_char
*eaddr
;
2646 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2647 if (linktype
== DLT_EN10MB
)
2648 return gen_ehostop(eaddr
, (int)q
.dir
);
2649 if (linktype
== DLT_FDDI
)
2650 return gen_fhostop(eaddr
, (int)q
.dir
);
2651 if (linktype
== DLT_IEEE802
)
2652 return gen_thostop(eaddr
, (int)q
.dir
);
2654 bpf_error("ethernet address used in non-ether expression");
2660 struct slist
*s0
, *s1
;
2663 * This is definitely not the best way to do this, but the
2664 * lists will rarely get long.
2671 static struct slist
*
2677 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2682 static struct slist
*
2688 s
= new_stmt(BPF_LD
|BPF_MEM
);
2694 gen_load(proto
, index
, size
)
2699 struct slist
*s
, *tmp
;
2701 int regno
= alloc_reg();
2703 free_reg(index
->regno
);
2707 bpf_error("data size must be 1, 2, or 4");
2723 bpf_error("unsupported index operation");
2726 s
= xfer_to_x(index
);
2727 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2729 sappend(index
->s
, s
);
2744 /* XXX Note that we assume a fixed link header here. */
2745 s
= xfer_to_x(index
);
2746 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2749 sappend(index
->s
, s
);
2751 b
= gen_proto_abbrev(proto
);
2753 gen_and(index
->b
, b
);
2763 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2765 sappend(s
, xfer_to_a(index
));
2766 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2767 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2768 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2770 sappend(index
->s
, s
);
2772 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2774 gen_and(index
->b
, b
);
2776 gen_and(gen_proto_abbrev(Q_IP
), b
);
2782 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2786 index
->regno
= regno
;
2787 s
= new_stmt(BPF_ST
);
2789 sappend(index
->s
, s
);
2795 gen_relation(code
, a0
, a1
, reversed
)
2797 struct arth
*a0
, *a1
;
2800 struct slist
*s0
, *s1
, *s2
;
2801 struct block
*b
, *tmp
;
2805 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2806 b
= new_block(JMP(code
));
2807 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2808 reversed
= !reversed
;
2809 b
->s
.k
= 0x80000000;
2817 sappend(a0
->s
, a1
->s
);
2821 free_reg(a0
->regno
);
2822 free_reg(a1
->regno
);
2824 /* 'and' together protocol checks */
2827 gen_and(a0
->b
, tmp
= a1
->b
);
2843 int regno
= alloc_reg();
2844 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2847 s
= new_stmt(BPF_LD
|BPF_LEN
);
2848 s
->next
= new_stmt(BPF_ST
);
2849 s
->next
->s
.k
= regno
;
2864 a
= (struct arth
*)newchunk(sizeof(*a
));
2868 s
= new_stmt(BPF_LD
|BPF_IMM
);
2870 s
->next
= new_stmt(BPF_ST
);
2886 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2889 s
= new_stmt(BPF_ST
);
2897 gen_arth(code
, a0
, a1
)
2899 struct arth
*a0
, *a1
;
2901 struct slist
*s0
, *s1
, *s2
;
2905 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2910 sappend(a0
->s
, a1
->s
);
2912 free_reg(a1
->regno
);
2914 s0
= new_stmt(BPF_ST
);
2915 a0
->regno
= s0
->s
.k
= alloc_reg();
2922 * Here we handle simple allocation of the scratch registers.
2923 * If too many registers are alloc'd, the allocator punts.
2925 static int regused
[BPF_MEMWORDS
];
2929 * Return the next free register.
2934 int n
= BPF_MEMWORDS
;
2937 if (regused
[curreg
])
2938 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2940 regused
[curreg
] = 1;
2944 bpf_error("too many registers needed to evaluate expression");
2949 * Return a register to the table so it can
2959 static struct block
*
2966 s
= new_stmt(BPF_LD
|BPF_LEN
);
2967 b
= new_block(JMP(jmp
));
2978 return gen_len(BPF_JGE
, n
);
2982 * Actually, this is less than or equal.
2990 b
= gen_len(BPF_JGT
, n
);
2997 gen_byteop(op
, idx
, val
)
3008 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3011 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3012 b
->s
.code
= JMP(BPF_JGE
);
3017 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3018 b
->s
.code
= JMP(BPF_JGT
);
3022 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3026 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3030 b
= new_block(JMP(BPF_JEQ
));
3038 gen_broadcast(proto
)
3041 bpf_u_int32 hostmask
;
3042 struct block
*b0
, *b1
, *b2
;
3043 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3049 if (linktype
== DLT_EN10MB
)
3050 return gen_ehostop(ebroadcast
, Q_DST
);
3051 if (linktype
== DLT_FDDI
)
3052 return gen_fhostop(ebroadcast
, Q_DST
);
3053 if (linktype
== DLT_IEEE802
)
3054 return gen_thostop(ebroadcast
, Q_DST
);
3055 bpf_error("not a broadcast link");
3059 b0
= gen_linktype(ETHERTYPE_IP
);
3060 hostmask
= ~netmask
;
3061 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3062 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3063 (bpf_int32
)(~0 & hostmask
), hostmask
);
3068 bpf_error("only ether/ip broadcast filters supported");
3072 gen_multicast(proto
)
3075 register struct block
*b0
, *b1
;
3076 register struct slist
*s
;
3082 if (linktype
== DLT_EN10MB
) {
3083 /* ether[0] & 1 != 0 */
3084 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3086 b0
= new_block(JMP(BPF_JSET
));
3092 if (linktype
== DLT_FDDI
) {
3093 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3094 /* fddi[1] & 1 != 0 */
3095 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3097 b0
= new_block(JMP(BPF_JSET
));
3103 /* TODO - check how token ring handles multicast */
3104 /* if (linktype == DLT_IEEE802) ... */
3106 /* Link not known to support multicasts */
3110 b0
= gen_linktype(ETHERTYPE_IP
);
3111 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3112 b1
->s
.code
= JMP(BPF_JGE
);
3118 b0
= gen_linktype(ETHERTYPE_IPV6
);
3119 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3124 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3128 * generate command for inbound/outbound. It's here so we can
3129 * make it link-type specific. 'dir' = 0 implies "inbound",
3130 * = 1 implies "outbound".
3136 register struct block
*b0
;
3138 b0
= gen_relation(BPF_JEQ
,
3139 gen_load(Q_LINK
, gen_loadi(0), 1),
3146 * support IEEE 802.1Q VLAN trunk over ethernet
3152 static u_int orig_linktype
= -1, orig_nl
= -1;
3156 * Change the offsets to point to the type and data fields within
3157 * the VLAN packet. This is somewhat of a kludge.
3159 if (orig_nl
== (u_int
)-1) {
3160 orig_linktype
= off_linktype
; /* save original values */
3171 bpf_error("no VLAN support for data link type %d",
3177 /* check for VLAN */
3178 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3180 /* If a specific VLAN is requested, check VLAN id */
3181 if (vlan_num
>= 0) {
3184 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);