]>
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.132 2000-10-28 10:18:39 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");
1294 bpf_error("'clnp' modifier applied to host");
1303 static struct block
*
1304 gen_host6(addr
, mask
, proto
, dir
)
1305 struct in6_addr
*addr
;
1306 struct in6_addr
*mask
;
1313 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1316 bpf_error("'ip' modifier applied to ip6 host");
1319 bpf_error("'rarp' modifier applied to ip6 host");
1322 bpf_error("'arp' modifier applied to ip6 host");
1325 bpf_error("'tcp' modifier applied to host");
1328 bpf_error("'udp' modifier applied to host");
1331 bpf_error("'icmp' modifier applied to host");
1334 bpf_error("'igmp' modifier applied to host");
1337 bpf_error("'igrp' modifier applied to host");
1340 bpf_error("'pim' modifier applied to host");
1343 bpf_error("ATALK host filtering not implemented");
1346 bpf_error("AARP host filtering not implemented");
1349 bpf_error("'decnet' modifier applied to ip6 host");
1352 bpf_error("SCA host filtering not implemented");
1355 bpf_error("LAT host filtering not implemented");
1358 bpf_error("MOPDL host filtering not implemented");
1361 bpf_error("MOPRC host filtering not implemented");
1364 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1365 off_nl
+ 8, off_nl
+ 24);
1368 bpf_error("'icmp6' modifier applied to host");
1371 bpf_error("'ah' modifier applied to host");
1374 bpf_error("'esp' modifier applied to host");
1377 bpf_error("ISO host filtering not implemented");
1380 bpf_error("'esis' modifier applied to host");
1383 bpf_error("'isis' modifier applied to host");
1386 bpf_error("'clnp' modifier applied to host");
1396 static struct block
*
1397 gen_gateway(eaddr
, alist
, proto
, dir
)
1398 const u_char
*eaddr
;
1399 bpf_u_int32
**alist
;
1403 struct block
*b0
, *b1
, *tmp
;
1406 bpf_error("direction applied to 'gateway'");
1413 if (linktype
== DLT_EN10MB
)
1414 b0
= gen_ehostop(eaddr
, Q_OR
);
1415 else if (linktype
== DLT_FDDI
)
1416 b0
= gen_fhostop(eaddr
, Q_OR
);
1417 else if (linktype
== DLT_IEEE802
)
1418 b0
= gen_thostop(eaddr
, Q_OR
);
1421 "'gateway' supported only on ethernet, FDDI or token ring");
1423 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1425 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1433 bpf_error("illegal modifier of 'gateway'");
1439 gen_proto_abbrev(proto
)
1450 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1452 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1458 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1460 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1466 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1469 #ifndef IPPROTO_IGMP
1470 #define IPPROTO_IGMP 2
1474 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1477 #ifndef IPPROTO_IGRP
1478 #define IPPROTO_IGRP 9
1481 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1485 #define IPPROTO_PIM 103
1489 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1491 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1497 b1
= gen_linktype(ETHERTYPE_IP
);
1501 b1
= gen_linktype(ETHERTYPE_ARP
);
1505 b1
= gen_linktype(ETHERTYPE_REVARP
);
1509 bpf_error("link layer applied in wrong context");
1512 b1
= gen_linktype(ETHERTYPE_ATALK
);
1516 b1
= gen_linktype(ETHERTYPE_AARP
);
1520 b1
= gen_linktype(ETHERTYPE_DN
);
1524 b1
= gen_linktype(ETHERTYPE_SCA
);
1528 b1
= gen_linktype(ETHERTYPE_LAT
);
1532 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1536 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1541 b1
= gen_linktype(ETHERTYPE_IPV6
);
1544 #ifndef IPPROTO_ICMPV6
1545 #define IPPROTO_ICMPV6 58
1548 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1553 #define IPPROTO_AH 51
1556 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1558 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1564 #define IPPROTO_ESP 50
1567 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1569 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1575 b1
= gen_linktype(LLC_ISO_LSAP
);
1579 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
1583 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
1587 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
1596 static struct block
*
1603 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1604 s
->s
.k
= off_nl
+ 6;
1605 b
= new_block(JMP(BPF_JSET
));
1613 static struct block
*
1614 gen_portatom(off
, v
)
1621 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1624 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1625 s
->next
->s
.k
= off_nl
+ off
;
1627 b
= new_block(JMP(BPF_JEQ
));
1635 static struct block
*
1636 gen_portatom6(off
, v
)
1640 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1645 gen_portop(port
, proto
, dir
)
1646 int port
, proto
, dir
;
1648 struct block
*b0
, *b1
, *tmp
;
1650 /* ip proto 'proto' */
1651 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1657 b1
= gen_portatom(0, (bpf_int32
)port
);
1661 b1
= gen_portatom(2, (bpf_int32
)port
);
1666 tmp
= gen_portatom(0, (bpf_int32
)port
);
1667 b1
= gen_portatom(2, (bpf_int32
)port
);
1672 tmp
= gen_portatom(0, (bpf_int32
)port
);
1673 b1
= gen_portatom(2, (bpf_int32
)port
);
1685 static struct block
*
1686 gen_port(port
, ip_proto
, dir
)
1691 struct block
*b0
, *b1
, *tmp
;
1693 /* ether proto ip */
1694 b0
= gen_linktype(ETHERTYPE_IP
);
1699 b1
= gen_portop(port
, ip_proto
, dir
);
1703 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1704 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1717 gen_portop6(port
, proto
, dir
)
1718 int port
, proto
, dir
;
1720 struct block
*b0
, *b1
, *tmp
;
1722 /* ip proto 'proto' */
1723 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1727 b1
= gen_portatom6(0, (bpf_int32
)port
);
1731 b1
= gen_portatom6(2, (bpf_int32
)port
);
1736 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1737 b1
= gen_portatom6(2, (bpf_int32
)port
);
1742 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1743 b1
= gen_portatom6(2, (bpf_int32
)port
);
1755 static struct block
*
1756 gen_port6(port
, ip_proto
, dir
)
1761 struct block
*b0
, *b1
, *tmp
;
1763 /* ether proto ip */
1764 b0
= gen_linktype(ETHERTYPE_IPV6
);
1769 b1
= gen_portop6(port
, ip_proto
, dir
);
1773 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1774 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1787 lookup_proto(name
, proto
)
1788 register const char *name
;
1797 v
= pcap_nametoproto(name
);
1798 if (v
== PROTO_UNDEF
)
1799 bpf_error("unknown ip proto '%s'", name
);
1803 /* XXX should look up h/w protocol type based on linktype */
1804 v
= pcap_nametoeproto(name
);
1805 if (v
== PROTO_UNDEF
)
1806 bpf_error("unknown ether proto '%s'", name
);
1810 if (strcmp(name
, "esis") == 0)
1812 else if (strcmp(name
, "isis") == 0)
1814 else if (strcmp(name
, "clnp") == 0)
1817 bpf_error("unknown osi proto '%s'", name
);
1837 static struct block
*
1838 gen_protochain(v
, proto
, dir
)
1843 #ifdef NO_PROTOCHAIN
1844 return gen_proto(v
, proto
, dir
);
1846 struct block
*b0
, *b
;
1847 struct slist
*s
[100];
1848 int fix2
, fix3
, fix4
, fix5
;
1849 int ahcheck
, again
, end
;
1851 int reg1
= alloc_reg();
1852 int reg2
= alloc_reg();
1854 memset(s
, 0, sizeof(s
));
1855 fix2
= fix3
= fix4
= fix5
= 0;
1862 b0
= gen_protochain(v
, Q_IP
, dir
);
1863 b
= gen_protochain(v
, Q_IPV6
, dir
);
1867 bpf_error("bad protocol applied for 'protochain'");
1871 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1874 * s[0] is a dummy entry to protect other BPF insn from damaged
1875 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1876 * hard to find interdependency made by jump table fixup.
1879 s
[i
] = new_stmt(0); /*dummy*/
1884 b0
= gen_linktype(ETHERTYPE_IP
);
1887 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1888 s
[i
]->s
.k
= off_nl
+ 9;
1890 /* X = ip->ip_hl << 2 */
1891 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1897 b0
= gen_linktype(ETHERTYPE_IPV6
);
1899 /* A = ip6->ip_nxt */
1900 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1901 s
[i
]->s
.k
= off_nl
+ 6;
1903 /* X = sizeof(struct ip6_hdr) */
1904 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1910 bpf_error("unsupported proto to gen_protochain");
1914 /* again: if (A == v) goto end; else fall through; */
1916 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1918 s
[i
]->s
.jt
= NULL
; /*later*/
1919 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1923 #ifndef IPPROTO_NONE
1924 #define IPPROTO_NONE 59
1926 /* if (A == IPPROTO_NONE) goto end */
1927 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1928 s
[i
]->s
.jt
= NULL
; /*later*/
1929 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1930 s
[i
]->s
.k
= IPPROTO_NONE
;
1931 s
[fix5
]->s
.jf
= s
[i
];
1936 if (proto
== Q_IPV6
) {
1937 int v6start
, v6end
, v6advance
, j
;
1940 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1941 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1942 s
[i
]->s
.jt
= NULL
; /*later*/
1943 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1944 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
1945 s
[fix2
]->s
.jf
= s
[i
];
1947 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
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
; /*update in next stmt*/
1951 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
1953 /* if (A == IPPROTO_ROUTING) goto v6advance */
1954 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1955 s
[i
]->s
.jt
= NULL
; /*later*/
1956 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1957 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1959 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1960 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1961 s
[i
]->s
.jt
= NULL
; /*later*/
1962 s
[i
]->s
.jf
= NULL
; /*later*/
1963 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
1974 * X = X + (P[X] + 1) * 8;
1977 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1980 s
[i
] = new_stmt(BPF_ST
);
1984 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1988 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1990 /* A = P[X + packet head]; */
1991 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1995 s
[i
] = new_stmt(BPF_ST
);
1999 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
2002 /* A = P[X + packet head] */
2003 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2007 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2011 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2015 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2018 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2022 /* goto again; (must use BPF_JA for backward jump) */
2023 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2024 s
[i
]->s
.k
= again
- i
- 1;
2025 s
[i
- 1]->s
.jf
= s
[i
];
2029 for (j
= v6start
; j
<= v6end
; j
++)
2030 s
[j
]->s
.jt
= s
[v6advance
];
2035 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2037 s
[fix2
]->s
.jf
= s
[i
];
2043 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2044 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2045 s
[i
]->s
.jt
= NULL
; /*later*/
2046 s
[i
]->s
.jf
= NULL
; /*later*/
2047 s
[i
]->s
.k
= IPPROTO_AH
;
2049 s
[fix3
]->s
.jf
= s
[ahcheck
];
2056 * X = X + (P[X] + 2) * 4;
2059 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2062 s
[i
] = new_stmt(BPF_ST
);
2066 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2070 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2072 /* A = P[X + packet head]; */
2073 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2077 s
[i
] = new_stmt(BPF_ST
);
2081 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
2084 /* A = P[X + packet head] */
2085 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2089 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2093 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2097 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2100 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2104 /* goto again; (must use BPF_JA for backward jump) */
2105 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2106 s
[i
]->s
.k
= again
- i
- 1;
2111 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2113 s
[fix2
]->s
.jt
= s
[end
];
2114 s
[fix4
]->s
.jf
= s
[end
];
2115 s
[fix5
]->s
.jt
= s
[end
];
2122 for (i
= 0; i
< max
- 1; i
++)
2123 s
[i
]->next
= s
[i
+ 1];
2124 s
[max
- 1]->next
= NULL
;
2129 b
= new_block(JMP(BPF_JEQ
));
2130 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2141 static struct block
*
2142 gen_proto(v
, proto
, dir
)
2147 struct block
*b0
, *b1
;
2149 if (dir
!= Q_DEFAULT
)
2150 bpf_error("direction applied to 'proto'");
2155 b0
= gen_proto(v
, Q_IP
, dir
);
2156 b1
= gen_proto(v
, Q_IPV6
, dir
);
2163 b0
= gen_linktype(ETHERTYPE_IP
);
2165 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2167 b1
= gen_protochain(v
, Q_IP
);
2173 b0
= gen_linktype(LLC_ISO_LSAP
);
2174 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2179 bpf_error("arp does not encapsulate another protocol");
2183 bpf_error("rarp does not encapsulate another protocol");
2187 bpf_error("atalk encapsulation is not specifiable");
2191 bpf_error("decnet encapsulation is not specifiable");
2195 bpf_error("sca does not encapsulate another protocol");
2199 bpf_error("lat does not encapsulate another protocol");
2203 bpf_error("moprc does not encapsulate another protocol");
2207 bpf_error("mopdl does not encapsulate another protocol");
2211 return gen_linktype(v
);
2214 bpf_error("'udp proto' is bogus");
2218 bpf_error("'tcp proto' is bogus");
2222 bpf_error("'icmp proto' is bogus");
2226 bpf_error("'igmp proto' is bogus");
2230 bpf_error("'igrp proto' is bogus");
2234 bpf_error("'pim proto' is bogus");
2239 b0
= gen_linktype(ETHERTYPE_IPV6
);
2241 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2243 b1
= gen_protochain(v
, Q_IPV6
);
2249 bpf_error("'icmp6 proto' is bogus");
2253 bpf_error("'ah proto' is bogus");
2256 bpf_error("'ah proto' is bogus");
2267 register const char *name
;
2270 int proto
= q
.proto
;
2274 bpf_u_int32 mask
, addr
;
2276 bpf_u_int32
**alist
;
2279 struct sockaddr_in
*sin
;
2280 struct sockaddr_in6
*sin6
;
2281 struct addrinfo
*res
, *res0
;
2282 struct in6_addr mask128
;
2284 struct block
*b
, *tmp
;
2285 int port
, real_proto
;
2290 addr
= pcap_nametonetaddr(name
);
2292 bpf_error("unknown network '%s'", name
);
2293 /* Left justify network addr and calculate its network mask */
2295 while (addr
&& (addr
& 0xff000000) == 0) {
2299 return gen_host(addr
, mask
, proto
, dir
);
2303 if (proto
== Q_LINK
) {
2307 eaddr
= pcap_ether_hostton(name
);
2310 "unknown ether host '%s'", name
);
2311 return gen_ehostop(eaddr
, dir
);
2314 eaddr
= pcap_ether_hostton(name
);
2317 "unknown FDDI host '%s'", name
);
2318 return gen_fhostop(eaddr
, dir
);
2321 eaddr
= pcap_ether_hostton(name
);
2324 "unknown token ring host '%s'", name
);
2325 return gen_thostop(eaddr
, dir
);
2329 "only ethernet/FDDI/token ring supports link-level host name");
2332 } else if (proto
== Q_DECNET
) {
2333 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2335 * I don't think DECNET hosts can be multihomed, so
2336 * there is no need to build up a list of addresses
2338 return (gen_host(dn_addr
, 0, proto
, dir
));
2341 alist
= pcap_nametoaddr(name
);
2342 if (alist
== NULL
|| *alist
== NULL
)
2343 bpf_error("unknown host '%s'", name
);
2345 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2347 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2349 tmp
= gen_host(**alist
++, 0xffffffff,
2356 memset(&mask128
, 0xff, sizeof(mask128
));
2357 res0
= res
= pcap_nametoaddrinfo(name
);
2359 bpf_error("unknown host '%s'", name
);
2361 tproto
= tproto6
= proto
;
2362 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2366 for (res
= res0
; res
; res
= res
->ai_next
) {
2367 switch (res
->ai_family
) {
2369 if (tproto
== Q_IPV6
)
2372 sin
= (struct sockaddr_in
*)
2374 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2375 0xffffffff, tproto
, dir
);
2378 if (tproto6
== Q_IP
)
2381 sin6
= (struct sockaddr_in6
*)
2383 tmp
= gen_host6(&sin6
->sin6_addr
,
2384 &mask128
, tproto6
, dir
);
2393 bpf_error("unknown host '%s'%s", name
,
2394 (proto
== Q_DEFAULT
)
2396 : " for specified address family");
2403 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2404 bpf_error("illegal qualifier of 'port'");
2405 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2406 bpf_error("unknown port '%s'", name
);
2407 if (proto
== Q_UDP
) {
2408 if (real_proto
== IPPROTO_TCP
)
2409 bpf_error("port '%s' is tcp", name
);
2411 /* override PROTO_UNDEF */
2412 real_proto
= IPPROTO_UDP
;
2414 if (proto
== Q_TCP
) {
2415 if (real_proto
== IPPROTO_UDP
)
2416 bpf_error("port '%s' is udp", name
);
2418 /* override PROTO_UNDEF */
2419 real_proto
= IPPROTO_TCP
;
2422 return gen_port(port
, real_proto
, dir
);
2426 b
= gen_port(port
, real_proto
, dir
);
2427 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2434 eaddr
= pcap_ether_hostton(name
);
2436 bpf_error("unknown ether host: %s", name
);
2438 alist
= pcap_nametoaddr(name
);
2439 if (alist
== NULL
|| *alist
== NULL
)
2440 bpf_error("unknown host '%s'", name
);
2441 return gen_gateway(eaddr
, alist
, proto
, dir
);
2443 bpf_error("'gateway' not supported in this configuration");
2447 real_proto
= lookup_proto(name
, proto
);
2448 if (real_proto
>= 0)
2449 return gen_proto(real_proto
, proto
, dir
);
2451 bpf_error("unknown protocol: %s", name
);
2454 real_proto
= lookup_proto(name
, proto
);
2455 if (real_proto
>= 0)
2456 return gen_protochain(real_proto
, proto
, dir
);
2458 bpf_error("unknown protocol: %s", name
);
2470 gen_mcode(s1
, s2
, masklen
, q
)
2471 register const char *s1
, *s2
;
2472 register int masklen
;
2475 register int nlen
, mlen
;
2478 nlen
= __pcap_atoin(s1
, &n
);
2479 /* Promote short ipaddr */
2483 mlen
= __pcap_atoin(s2
, &m
);
2484 /* Promote short ipaddr */
2487 bpf_error("non-network bits set in \"%s mask %s\"",
2490 /* Convert mask len to mask */
2492 bpf_error("mask length must be <= 32");
2493 m
= 0xffffffff << (32 - masklen
);
2495 bpf_error("non-network bits set in \"%s/%d\"",
2502 return gen_host(n
, m
, q
.proto
, q
.dir
);
2505 bpf_error("Mask syntax for networks only");
2512 register const char *s
;
2517 int proto
= q
.proto
;
2523 else if (q
.proto
== Q_DECNET
)
2524 vlen
= __pcap_atodn(s
, &v
);
2526 vlen
= __pcap_atoin(s
, &v
);
2533 if (proto
== Q_DECNET
)
2534 return gen_host(v
, 0, proto
, dir
);
2535 else if (proto
== Q_LINK
) {
2536 bpf_error("illegal link layer address");
2539 if (s
== NULL
&& q
.addr
== Q_NET
) {
2540 /* Promote short net number */
2541 while (v
&& (v
& 0xff000000) == 0) {
2546 /* Promote short ipaddr */
2550 return gen_host(v
, mask
, proto
, dir
);
2555 proto
= IPPROTO_UDP
;
2556 else if (proto
== Q_TCP
)
2557 proto
= IPPROTO_TCP
;
2558 else if (proto
== Q_DEFAULT
)
2559 proto
= PROTO_UNDEF
;
2561 bpf_error("illegal qualifier of 'port'");
2564 return gen_port((int)v
, proto
, dir
);
2568 b
= gen_port((int)v
, proto
, dir
);
2569 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2575 bpf_error("'gateway' requires a name");
2579 return gen_proto((int)v
, proto
, dir
);
2582 return gen_protochain((int)v
, proto
, dir
);
2597 gen_mcode6(s1
, s2
, masklen
, q
)
2598 register const char *s1
, *s2
;
2599 register int masklen
;
2602 struct addrinfo
*res
;
2603 struct in6_addr
*addr
;
2604 struct in6_addr mask
;
2609 bpf_error("no mask %s supported", s2
);
2611 res
= pcap_nametoaddrinfo(s1
);
2613 bpf_error("invalid ip6 address %s", s1
);
2615 bpf_error("%s resolved to multiple address", s1
);
2616 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2618 if (sizeof(mask
) * 8 < masklen
)
2619 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2620 memset(&mask
, 0xff, masklen
/ 8);
2622 mask
.s6_addr
[masklen
/ 8] =
2623 (0xff << (8 - masklen
% 8)) & 0xff;
2626 a
= (u_int32_t
*)addr
;
2627 m
= (u_int32_t
*)&mask
;
2628 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2629 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2630 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2638 bpf_error("Mask syntax for networks only");
2642 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2647 bpf_error("invalid qualifier against IPv6 address");
2655 register const u_char
*eaddr
;
2658 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2659 if (linktype
== DLT_EN10MB
)
2660 return gen_ehostop(eaddr
, (int)q
.dir
);
2661 if (linktype
== DLT_FDDI
)
2662 return gen_fhostop(eaddr
, (int)q
.dir
);
2663 if (linktype
== DLT_IEEE802
)
2664 return gen_thostop(eaddr
, (int)q
.dir
);
2666 bpf_error("ethernet address used in non-ether expression");
2672 struct slist
*s0
, *s1
;
2675 * This is definitely not the best way to do this, but the
2676 * lists will rarely get long.
2683 static struct slist
*
2689 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2694 static struct slist
*
2700 s
= new_stmt(BPF_LD
|BPF_MEM
);
2706 gen_load(proto
, index
, size
)
2711 struct slist
*s
, *tmp
;
2713 int regno
= alloc_reg();
2715 free_reg(index
->regno
);
2719 bpf_error("data size must be 1, 2, or 4");
2735 bpf_error("unsupported index operation");
2738 s
= xfer_to_x(index
);
2739 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2741 sappend(index
->s
, s
);
2756 /* XXX Note that we assume a fixed link header here. */
2757 s
= xfer_to_x(index
);
2758 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2761 sappend(index
->s
, s
);
2763 b
= gen_proto_abbrev(proto
);
2765 gen_and(index
->b
, b
);
2775 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2777 sappend(s
, xfer_to_a(index
));
2778 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2779 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2780 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2782 sappend(index
->s
, s
);
2784 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2786 gen_and(index
->b
, b
);
2788 gen_and(gen_proto_abbrev(Q_IP
), b
);
2794 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2798 index
->regno
= regno
;
2799 s
= new_stmt(BPF_ST
);
2801 sappend(index
->s
, s
);
2807 gen_relation(code
, a0
, a1
, reversed
)
2809 struct arth
*a0
, *a1
;
2812 struct slist
*s0
, *s1
, *s2
;
2813 struct block
*b
, *tmp
;
2817 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2818 b
= new_block(JMP(code
));
2819 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2820 reversed
= !reversed
;
2821 b
->s
.k
= 0x80000000;
2829 sappend(a0
->s
, a1
->s
);
2833 free_reg(a0
->regno
);
2834 free_reg(a1
->regno
);
2836 /* 'and' together protocol checks */
2839 gen_and(a0
->b
, tmp
= a1
->b
);
2855 int regno
= alloc_reg();
2856 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2859 s
= new_stmt(BPF_LD
|BPF_LEN
);
2860 s
->next
= new_stmt(BPF_ST
);
2861 s
->next
->s
.k
= regno
;
2876 a
= (struct arth
*)newchunk(sizeof(*a
));
2880 s
= new_stmt(BPF_LD
|BPF_IMM
);
2882 s
->next
= new_stmt(BPF_ST
);
2898 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2901 s
= new_stmt(BPF_ST
);
2909 gen_arth(code
, a0
, a1
)
2911 struct arth
*a0
, *a1
;
2913 struct slist
*s0
, *s1
, *s2
;
2917 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2922 sappend(a0
->s
, a1
->s
);
2924 free_reg(a1
->regno
);
2926 s0
= new_stmt(BPF_ST
);
2927 a0
->regno
= s0
->s
.k
= alloc_reg();
2934 * Here we handle simple allocation of the scratch registers.
2935 * If too many registers are alloc'd, the allocator punts.
2937 static int regused
[BPF_MEMWORDS
];
2941 * Return the next free register.
2946 int n
= BPF_MEMWORDS
;
2949 if (regused
[curreg
])
2950 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2952 regused
[curreg
] = 1;
2956 bpf_error("too many registers needed to evaluate expression");
2961 * Return a register to the table so it can
2971 static struct block
*
2978 s
= new_stmt(BPF_LD
|BPF_LEN
);
2979 b
= new_block(JMP(jmp
));
2990 return gen_len(BPF_JGE
, n
);
2994 * Actually, this is less than or equal.
3002 b
= gen_len(BPF_JGT
, n
);
3009 gen_byteop(op
, idx
, val
)
3020 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3023 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3024 b
->s
.code
= JMP(BPF_JGE
);
3029 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3030 b
->s
.code
= JMP(BPF_JGT
);
3034 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3038 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3042 b
= new_block(JMP(BPF_JEQ
));
3050 gen_broadcast(proto
)
3053 bpf_u_int32 hostmask
;
3054 struct block
*b0
, *b1
, *b2
;
3055 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3061 if (linktype
== DLT_EN10MB
)
3062 return gen_ehostop(ebroadcast
, Q_DST
);
3063 if (linktype
== DLT_FDDI
)
3064 return gen_fhostop(ebroadcast
, Q_DST
);
3065 if (linktype
== DLT_IEEE802
)
3066 return gen_thostop(ebroadcast
, Q_DST
);
3067 bpf_error("not a broadcast link");
3071 b0
= gen_linktype(ETHERTYPE_IP
);
3072 hostmask
= ~netmask
;
3073 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3074 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3075 (bpf_int32
)(~0 & hostmask
), hostmask
);
3080 bpf_error("only ether/ip broadcast filters supported");
3084 gen_multicast(proto
)
3087 register struct block
*b0
, *b1
;
3088 register struct slist
*s
;
3094 if (linktype
== DLT_EN10MB
) {
3095 /* ether[0] & 1 != 0 */
3096 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3098 b0
= new_block(JMP(BPF_JSET
));
3104 if (linktype
== DLT_FDDI
) {
3105 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3106 /* fddi[1] & 1 != 0 */
3107 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3109 b0
= new_block(JMP(BPF_JSET
));
3115 /* TODO - check how token ring handles multicast */
3116 /* if (linktype == DLT_IEEE802) ... */
3118 /* Link not known to support multicasts */
3122 b0
= gen_linktype(ETHERTYPE_IP
);
3123 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3124 b1
->s
.code
= JMP(BPF_JGE
);
3130 b0
= gen_linktype(ETHERTYPE_IPV6
);
3131 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3136 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3140 * generate command for inbound/outbound. It's here so we can
3141 * make it link-type specific. 'dir' = 0 implies "inbound",
3142 * = 1 implies "outbound".
3148 register struct block
*b0
;
3150 b0
= gen_relation(BPF_JEQ
,
3151 gen_load(Q_LINK
, gen_loadi(0), 1),
3158 * support IEEE 802.1Q VLAN trunk over ethernet
3164 static u_int orig_linktype
= -1, orig_nl
= -1;
3168 * Change the offsets to point to the type and data fields within
3169 * the VLAN packet. This is somewhat of a kludge.
3171 if (orig_nl
== (u_int
)-1) {
3172 orig_linktype
= off_linktype
; /* save original values */
3183 bpf_error("no VLAN support for data link type %d",
3189 /* check for VLAN */
3190 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3192 /* If a specific VLAN is requested, check VLAN id */
3193 if (vlan_num
>= 0) {
3196 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);