]>
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.165 2002-06-01 23:22:57 guy Exp $ (LBL)";
31 #include <sys/types.h>
32 #include <sys/socket.h>
35 #include <sys/param.h>
38 struct mbuf
; /* Squelch compiler warnings on some platforms for */
39 struct rtentry
; /* declarations in <net/if.h> */
42 #include <netinet/in.h>
52 #include "ethertype.h"
60 #include <netdb.h> /* for "struct addrinfo" */
62 #include <pcap-namedb.h>
67 #define IPPROTO_SCTP 132
70 #ifdef HAVE_OS_PROTO_H
74 #define JMP(c) ((c)|BPF_JMP|BPF_K)
77 static jmp_buf top_ctx
;
78 static pcap_t
*bpf_pcap
;
80 /* Hack for updating VLAN offsets. */
81 static u_int orig_linktype
= -1, orig_nl
= -1, orig_nl_nosnap
= -1;
85 int pcap_fddipad
= PCAP_FDDIPAD
;
92 bpf_error(const char *fmt
, ...)
99 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
106 static void init_linktype(int);
108 static int alloc_reg(void);
109 static void free_reg(int);
111 static struct block
*root
;
114 * We divy out chunks of memory rather than call malloc each time so
115 * we don't have to worry about leaking memory. It's probably
116 * not a big deal if all this memory was wasted but it this ever
117 * goes into a library that would probably not be a good idea.
120 #define CHUNK0SIZE 1024
126 static struct chunk chunks
[NCHUNKS
];
127 static int cur_chunk
;
129 static void *newchunk(u_int
);
130 static void freechunks(void);
131 static inline struct block
*new_block(int);
132 static inline struct slist
*new_stmt(int);
133 static struct block
*gen_retblk(int);
134 static inline void syntax(void);
136 static void backpatch(struct block
*, struct block
*);
137 static void merge(struct block
*, struct block
*);
138 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
139 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
140 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
141 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
142 static struct block
*gen_uncond(int);
143 static inline struct block
*gen_true(void);
144 static inline struct block
*gen_false(void);
145 static struct block
*gen_linktype(int);
146 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
147 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
149 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
151 static struct block
*gen_ahostop(const u_char
*, int);
152 static struct block
*gen_ehostop(const u_char
*, int);
153 static struct block
*gen_fhostop(const u_char
*, int);
154 static struct block
*gen_thostop(const u_char
*, int);
155 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
156 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
158 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
161 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
163 static struct block
*gen_ipfrag(void);
164 static struct block
*gen_portatom(int, bpf_int32
);
166 static struct block
*gen_portatom6(int, bpf_int32
);
168 struct block
*gen_portop(int, int, int);
169 static struct block
*gen_port(int, int, int);
171 struct block
*gen_portop6(int, int, int);
172 static struct block
*gen_port6(int, int, int);
174 static int lookup_proto(const char *, int);
175 static struct block
*gen_protochain(int, int, int);
176 static struct block
*gen_proto(int, int, int);
177 static struct slist
*xfer_to_x(struct arth
*);
178 static struct slist
*xfer_to_a(struct arth
*);
179 static struct block
*gen_len(int, int);
189 /* XXX Round up to nearest long. */
190 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
192 /* XXX Round up to structure boundary. */
196 cp
= &chunks
[cur_chunk
];
197 if (n
> cp
->n_left
) {
198 ++cp
, k
= ++cur_chunk
;
200 bpf_error("out of memory");
201 size
= CHUNK0SIZE
<< k
;
202 cp
->m
= (void *)malloc(size
);
203 memset((char *)cp
->m
, 0, size
);
206 bpf_error("out of memory");
209 return (void *)((char *)cp
->m
+ cp
->n_left
);
218 for (i
= 0; i
< NCHUNKS
; ++i
)
219 if (chunks
[i
].m
!= NULL
) {
226 * A strdup whose allocations are freed after code generation is over.
230 register const char *s
;
232 int n
= strlen(s
) + 1;
233 char *cp
= newchunk(n
);
239 static inline struct block
*
245 p
= (struct block
*)newchunk(sizeof(*p
));
252 static inline struct slist
*
258 p
= (struct slist
*)newchunk(sizeof(*p
));
264 static struct block
*
268 struct block
*b
= new_block(BPF_RET
|BPF_K
);
277 bpf_error("syntax error in filter expression");
280 static bpf_u_int32 netmask
;
285 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
286 char *buf
, int optimize
, bpf_u_int32 mask
)
295 if (setjmp(top_ctx
)) {
303 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
)
346 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
349 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
355 * Clean up a "struct bpf_program" by freeing all the memory allocated
359 pcap_freecode(struct bpf_program
*program
)
362 if (program
->bf_insns
!= NULL
) {
363 free((char *)program
->bf_insns
);
364 program
->bf_insns
= NULL
;
369 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
370 * which of the jt and jf fields has been resolved and which is a pointer
371 * back to another unresolved block (or nil). At least one of the fields
372 * in each block is already resolved.
375 backpatch(list
, target
)
376 struct block
*list
, *target
;
393 * Merge the lists in b0 and b1, using the 'sense' field to indicate
394 * which of jt and jf is the link.
398 struct block
*b0
, *b1
;
400 register struct block
**p
= &b0
;
402 /* Find end of list. */
404 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
406 /* Concatenate the lists. */
414 backpatch(p
, gen_retblk(snaplen
));
415 p
->sense
= !p
->sense
;
416 backpatch(p
, gen_retblk(0));
422 struct block
*b0
, *b1
;
424 backpatch(b0
, b1
->head
);
425 b0
->sense
= !b0
->sense
;
426 b1
->sense
= !b1
->sense
;
428 b1
->sense
= !b1
->sense
;
434 struct block
*b0
, *b1
;
436 b0
->sense
= !b0
->sense
;
437 backpatch(b0
, b1
->head
);
438 b0
->sense
= !b0
->sense
;
447 b
->sense
= !b
->sense
;
450 static struct block
*
451 gen_cmp(offset
, size
, v
)
458 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
461 b
= new_block(JMP(BPF_JEQ
));
468 static struct block
*
469 gen_cmp_gt(offset
, size
, v
)
476 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
479 b
= new_block(JMP(BPF_JGT
));
486 static struct block
*
487 gen_mcmp(offset
, size
, v
, mask
)
492 struct block
*b
= gen_cmp(offset
, size
, v
);
495 if (mask
!= 0xffffffff) {
496 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
503 static struct block
*
504 gen_bcmp(offset
, size
, v
)
505 register u_int offset
, size
;
506 register const u_char
*v
;
508 register struct block
*b
, *tmp
;
512 register const u_char
*p
= &v
[size
- 4];
513 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
514 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
516 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
523 register const u_char
*p
= &v
[size
- 2];
524 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
526 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
533 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
542 * Various code constructs need to know the layout of the data link
543 * layer. These variables give the necessary offsets.
547 * "off_linktype" is the offset to information in the link-layer header
548 * giving the packet type.
550 * For Ethernet, it's the offset of the Ethernet type field.
552 * For link-layer types that always use 802.2 headers, it's the
553 * offset of the LLC header.
555 * For PPP, it's the offset of the PPP type field.
557 * For Cisco HDLC, it's the offset of the CHDLC type field.
559 * For BSD loopback, it's the offset of the AF_ value.
561 * For Linux cooked sockets, it's the offset of the type field.
563 * It's set to -1 for no encapsulation, in which case, IP is assumed.
565 static u_int off_linktype
;
568 * These are offsets to the beginning of the network-layer header.
570 * If the link layer never uses 802.2 LLC:
572 * "off_nl" and "off_nl_nosnap" are the same.
574 * If the link layer always uses 802.2 LLC:
576 * "off_nl" is the offset if there's a SNAP header following
579 * "off_nl_nosnap" is the offset if there's no SNAP header.
581 * If the link layer is Ethernet:
583 * "off_nl" is the offset if the packet is an Ethernet II packet
584 * (we assume no 802.3+802.2+SNAP);
586 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
587 * with an 802.2 header following it.
590 static u_int off_nl_nosnap
;
608 off_nl
= 6; /* XXX in reality, variable! */
609 off_nl_nosnap
= 6; /* no 802.2 LLC */
614 off_nl
= 14; /* Ethernet II */
615 off_nl_nosnap
= 17; /* 802.3+802.2 */
620 * SLIP doesn't have a link level type. The 16 byte
621 * header is hacked into our SLIP driver.
625 off_nl_nosnap
= 16; /* no 802.2 LLC */
629 /* XXX this may be the same as the DLT_PPP_BSDOS case */
633 off_nl_nosnap
= 24; /* no 802.2 LLC */
640 off_nl_nosnap
= 4; /* no 802.2 LLC */
644 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
645 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
648 off_nl_nosnap
= 4; /* no 802.2 LLC */
653 * This does no include the Ethernet header, and
654 * only covers session state.
658 off_nl_nosnap
= 8; /* no 802.2 LLC */
664 off_nl_nosnap
= 24; /* no 802.2 LLC */
669 * FDDI doesn't really have a link-level type field.
670 * We set "off_linktype" to the offset of the LLC header.
672 * To check for Ethernet types, we assume that SSAP = SNAP
673 * is being used and pick out the encapsulated Ethernet type.
674 * XXX - should we generate code to check for SNAP?
678 off_linktype
+= pcap_fddipad
;
680 off_nl
= 21; /* FDDI+802.2+SNAP */
681 off_nl_nosnap
= 16; /* FDDI+802.2 */
683 off_nl
+= pcap_fddipad
;
684 off_nl_nosnap
+= pcap_fddipad
;
690 * Token Ring doesn't really have a link-level type field.
691 * We set "off_linktype" to the offset of the LLC header.
693 * To check for Ethernet types, we assume that SSAP = SNAP
694 * is being used and pick out the encapsulated Ethernet type.
695 * XXX - should we generate code to check for SNAP?
697 * XXX - the header is actually variable-length.
698 * Some various Linux patched versions gave 38
699 * as "off_linktype" and 40 as "off_nl"; however,
700 * if a token ring packet has *no* routing
701 * information, i.e. is not source-routed, the correct
702 * values are 20 and 22, as they are in the vanilla code.
704 * A packet is source-routed iff the uppermost bit
705 * of the first byte of the source address, at an
706 * offset of 8, has the uppermost bit set. If the
707 * packet is source-routed, the total number of bytes
708 * of routing information is 2 plus bits 0x1F00 of
709 * the 16-bit value at an offset of 14 (shifted right
710 * 8 - figure out which byte that is).
713 off_nl
= 22; /* Token Ring+802.2+SNAP */
714 off_nl_nosnap
= 17; /* Token Ring+802.2 */
719 * 802.11 doesn't really have a link-level type field.
720 * We set "off_linktype" to the offset of the LLC header.
722 * To check for Ethernet types, we assume that SSAP = SNAP
723 * is being used and pick out the encapsulated Ethernet type.
724 * XXX - should we generate code to check for SNAP?
726 * XXX - the header is actually variable-length. We
727 * assume a 24-byte link-layer header, as appears in
728 * data frames in networks with no bridges.
731 off_nl
= 32; /* 802.11+802.2+SNAP */
732 off_nl_nosnap
= 27; /* 802.22+802.2 */
735 case DLT_PRISM_HEADER
:
737 * Same as 802.11, but with an additional header before
738 * the 802.11 header, containing a bunch of additional
739 * information including radio-level information.
741 * The header is 144 bytes long.
743 * XXX - same variable-length header problem; at least
744 * the Prism header is fixed-length.
746 off_linktype
= 144+24;
747 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
748 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
751 case DLT_ATM_RFC1483
:
752 case DLT_ATM_CLIP
: /* Linux ATM defines this */
754 * assume routed, non-ISO PDUs
755 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
758 off_nl
= 8; /* 802.2+SNAP */
759 off_nl_nosnap
= 3; /* 802.2 */
765 off_nl_nosnap
= 0; /* no 802.2 LLC */
768 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
771 off_nl_nosnap
= 16; /* no 802.2 LLC */
776 * LocalTalk does have a 1-byte type field in the LLAP header,
777 * but really it just indicates whether there is a "short" or
778 * "long" DDP packet following.
782 off_nl_nosnap
= 0; /* no 802.2 LLC */
785 bpf_error("unknown data link type %d", linktype
);
789 static struct block
*
796 s
= new_stmt(BPF_LD
|BPF_IMM
);
798 b
= new_block(JMP(BPF_JEQ
));
804 static inline struct block
*
807 return gen_uncond(1);
810 static inline struct block
*
813 return gen_uncond(0);
817 * Byte-swap a 32-bit number.
818 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
819 * big-endian platforms.)
821 #define SWAPLONG(y) \
822 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
824 static struct block
*
828 struct block
*b0
, *b1
;
837 * OSI protocols always use 802.2 encapsulation.
838 * XXX - should we check both the DSAP and the
839 * SSAP, like this, or should we check just the
842 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
844 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
845 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
851 * NetBEUI always uses 802.2 encapsulation.
852 * XXX - should we check both the DSAP and the
853 * SSAP, like this, or should we check just the
856 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
858 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
859 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
867 * Ethernet_II frames, which are Ethernet
868 * frames with a frame type of ETHERTYPE_IPX;
870 * Ethernet_802.3 frames, which are 802.3
871 * frames (i.e., the type/length field is
872 * a length field, <= ETHERMTU, rather than
873 * a type field) with the first two bytes
874 * after the Ethernet/802.3 header being
877 * Ethernet_802.2 frames, which are 802.3
878 * frames with an 802.2 LLC header and
879 * with the IPX LSAP as the DSAP in the LLC
882 * Ethernet_SNAP frames, which are 802.3
883 * frames with an LLC header and a SNAP
884 * header and with an OUI of 0x000000
885 * (encapsulated Ethernet) and a protocol
886 * ID of ETHERTYPE_IPX in the SNAP header.
888 * XXX - should we generate the same code both
889 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
893 * This generates code to check both for the
894 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
896 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
897 (bpf_int32
)LLCSAP_IPX
);
898 b1
= gen_cmp(off_linktype
+ 2, BPF_H
,
903 * Now we add code to check for SNAP frames with
904 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
906 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
910 * Now we generate code to check for 802.3
913 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
917 * Now add the check for 802.3 frames before the
918 * check for Ethernet_802.2 and Ethernet_802.3,
919 * as those checks should only be done on 802.3
920 * frames, not on Ethernet frames.
925 * Now add the check for Ethernet_II frames, and
926 * do that before checking for the other frame
929 b0
= gen_cmp(off_linktype
, BPF_H
,
930 (bpf_int32
)ETHERTYPE_IPX
);
934 case ETHERTYPE_ATALK
:
937 * EtherTalk (AppleTalk protocols on Ethernet link
938 * layer) may use 802.2 encapsulation.
942 * Check for 802.2 encapsulation (EtherTalk phase 2?);
943 * we check for an Ethernet type field less than
944 * 1500, which means it's an 802.3 length field.
946 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
950 * 802.2-encapsulated ETHERTYPE_ATALK packets are
951 * SNAP packets with an organization code of
952 * 0x080007 (Apple, for Appletalk) and a protocol
953 * type of ETHERTYPE_ATALK (Appletalk).
955 * 802.2-encapsulated ETHERTYPE_AARP packets are
956 * SNAP packets with an organization code of
957 * 0x000000 (encapsulated Ethernet) and a protocol
958 * type of ETHERTYPE_AARP (Appletalk ARP).
960 if (proto
== ETHERTYPE_ATALK
)
961 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
962 else /* proto == ETHERTYPE_AARP */
963 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
967 * Check for Ethernet encapsulation (Ethertalk
968 * phase 1?); we just check for the Ethernet
971 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
977 if (proto
<= ETHERMTU
) {
979 * This is an LLC SAP value, so the frames
980 * that match would be 802.2 frames.
981 * Check that the frame is an 802.2 frame
982 * (i.e., that the length/type field is
983 * a length field, <= ETHERMTU) and
984 * then check the DSAP.
986 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
988 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
994 * This is an Ethernet type, so compare
995 * the length/type field with it (if
996 * the frame is an 802.2 frame, the length
997 * field will be <= ETHERMTU, and, as
998 * "proto" is > ETHERMTU, this test
999 * will fail and the frame won't match,
1000 * which is what we want).
1002 return gen_cmp(off_linktype
, BPF_H
,
1008 case DLT_IEEE802_11
:
1009 case DLT_PRISM_HEADER
:
1012 case DLT_ATM_RFC1483
:
1015 * XXX - handle token-ring variable-length header.
1020 return gen_cmp(off_linktype
, BPF_H
, (long)
1021 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1023 case LLCSAP_NETBEUI
:
1024 return gen_cmp(off_linktype
, BPF_H
, (long)
1025 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1029 * XXX - are there ever SNAP frames for IPX on
1030 * non-Ethernet 802.x networks?
1032 return gen_cmp(off_linktype
, BPF_B
,
1033 (bpf_int32
)LLCSAP_IPX
);
1035 case ETHERTYPE_ATALK
:
1037 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1038 * SNAP packets with an organization code of
1039 * 0x080007 (Apple, for Appletalk) and a protocol
1040 * type of ETHERTYPE_ATALK (Appletalk).
1042 * XXX - check for an organization code of
1043 * encapsulated Ethernet as well?
1045 return gen_snap(0x080007, ETHERTYPE_ATALK
,
1051 * XXX - we don't have to check for IPX 802.3
1052 * here, but should we check for the IPX Ethertype?
1054 if (proto
<= ETHERMTU
) {
1056 * This is an LLC SAP value, so check
1059 return gen_cmp(off_linktype
, BPF_B
,
1063 * This is an Ethernet type; we assume
1064 * that it's unlikely that it'll
1065 * appear in the right place at random,
1066 * and therefore check only the
1067 * location that would hold the Ethernet
1068 * type in a SNAP frame with an organization
1069 * code of 0x000000 (encapsulated Ethernet).
1071 * XXX - if we were to check for the SNAP DSAP
1072 * and LSAP, as per XXX, and were also to check
1073 * for an organization code of 0x000000
1074 * (encapsulated Ethernet), we'd do
1076 * return gen_snap(0x000000, proto,
1079 * here; for now, we don't, as per the above.
1080 * I don't know whether it's worth the
1081 * extra CPU time to do the right check
1084 return gen_cmp(off_linktype
+6, BPF_H
,
1095 * OSI protocols always use 802.2 encapsulation.
1096 * XXX - should we check both the DSAP and the
1097 * LSAP, like this, or should we check just the
1100 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1101 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1102 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1106 case LLCSAP_NETBEUI
:
1108 * NetBEUI always uses 802.2 encapsulation.
1109 * XXX - should we check both the DSAP and the
1110 * LSAP, like this, or should we check just the
1113 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1114 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1115 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1121 * Ethernet_II frames, which are Ethernet
1122 * frames with a frame type of ETHERTYPE_IPX;
1124 * Ethernet_802.3 frames, which have a frame
1125 * type of LINUX_SLL_P_802_3;
1127 * Ethernet_802.2 frames, which are 802.3
1128 * frames with an 802.2 LLC header (i.e, have
1129 * a frame type of LINUX_SLL_P_802_2) and
1130 * with the IPX LSAP as the DSAP in the LLC
1133 * Ethernet_SNAP frames, which are 802.3
1134 * frames with an LLC header and a SNAP
1135 * header and with an OUI of 0x000000
1136 * (encapsulated Ethernet) and a protocol
1137 * ID of ETHERTYPE_IPX in the SNAP header.
1139 * First, do the checks on LINUX_SLL_P_802_2
1140 * frames; generate the check for either
1141 * Ethernet_802.2 or Ethernet_SNAP frames, and
1142 * then put a check for LINUX_SLL_P_802_2 frames
1145 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1146 (bpf_int32
)LLCSAP_IPX
);
1147 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1150 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1154 * Now check for 802.3 frames and OR that with
1155 * the previous test.
1157 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1161 * Now add the check for Ethernet_II frames, and
1162 * do that before checking for the other frame
1165 b0
= gen_cmp(off_linktype
, BPF_H
,
1166 (bpf_int32
)ETHERTYPE_IPX
);
1170 case ETHERTYPE_ATALK
:
1171 case ETHERTYPE_AARP
:
1173 * EtherTalk (AppleTalk protocols on Ethernet link
1174 * layer) may use 802.2 encapsulation.
1178 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1179 * we check for the 802.2 protocol type in the
1180 * "Ethernet type" field.
1182 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1185 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1186 * SNAP packets with an organization code of
1187 * 0x080007 (Apple, for Appletalk) and a protocol
1188 * type of ETHERTYPE_ATALK (Appletalk).
1190 * 802.2-encapsulated ETHERTYPE_AARP packets are
1191 * SNAP packets with an organization code of
1192 * 0x000000 (encapsulated Ethernet) and a protocol
1193 * type of ETHERTYPE_AARP (Appletalk ARP).
1195 if (proto
== ETHERTYPE_ATALK
)
1196 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1198 else /* proto == ETHERTYPE_AARP */
1199 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1204 * Check for Ethernet encapsulation (Ethertalk
1205 * phase 1?); we just check for the Ethernet
1208 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1214 if (proto
<= ETHERMTU
) {
1216 * This is an LLC SAP value, so the frames
1217 * that match would be 802.2 frames.
1218 * Check for the 802.2 protocol type
1219 * in the "Ethernet type" field, and
1220 * then check the DSAP.
1222 b0
= gen_cmp(off_linktype
, BPF_H
,
1224 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1230 * This is an Ethernet type, so compare
1231 * the length/type field with it (if
1232 * the frame is an 802.2 frame, the length
1233 * field will be <= ETHERMTU, and, as
1234 * "proto" is > ETHERMTU, this test
1235 * will fail and the frame won't match,
1236 * which is what we want).
1238 return gen_cmp(off_linktype
, BPF_H
,
1245 case DLT_SLIP_BSDOS
:
1248 * These types don't provide any type field; packets
1251 * XXX - for IPv4, check for a version number of 4, and,
1252 * for IPv6, check for a version number of 6?
1258 case ETHERTYPE_IPV6
:
1260 return gen_true(); /* always true */
1263 return gen_false(); /* always false */
1268 case DLT_PPP_SERIAL
:
1271 * We use Ethernet protocol types inside libpcap;
1272 * map them to the corresponding PPP protocol types.
1277 proto
= PPP_IP
; /* XXX was 0x21 */
1281 case ETHERTYPE_IPV6
:
1290 case ETHERTYPE_ATALK
:
1304 * I'm assuming the "Bridging PDU"s that go
1305 * over PPP are Spanning Tree Protocol
1319 * We use Ethernet protocol types inside libpcap;
1320 * map them to the corresponding PPP protocol types.
1325 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1326 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1328 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1333 case ETHERTYPE_IPV6
:
1343 case ETHERTYPE_ATALK
:
1357 * I'm assuming the "Bridging PDU"s that go
1358 * over PPP are Spanning Tree Protocol
1373 * For DLT_NULL, the link-layer header is a 32-bit
1374 * word containing an AF_ value in *host* byte order.
1376 * In addition, if we're reading a saved capture file,
1377 * the host byte order in the capture may not be the
1378 * same as the host byte order on this machine.
1380 * For DLT_LOOP, the link-layer header is a 32-bit
1381 * word containing an AF_ value in *network* byte order.
1383 * XXX - AF_ values may, unfortunately, be platform-
1384 * dependent; for example, FreeBSD's AF_INET6 is 24
1385 * whilst NetBSD's and OpenBSD's is 26.
1387 * This means that, when reading a capture file, just
1388 * checking for our AF_INET6 value won't work if the
1389 * capture file came from another OS.
1398 case ETHERTYPE_IPV6
:
1405 * Not a type on which we support filtering.
1406 * XXX - support those that have AF_ values
1407 * #defined on this platform, at least?
1412 if (linktype
== DLT_NULL
) {
1414 * The AF_ value is in host byte order, but
1415 * the BPF interpreter will convert it to
1416 * network byte order.
1418 * If this is a save file, and it's from a
1419 * machine with the opposite byte order to
1420 * ours, we byte-swap the AF_ value.
1422 * Then we run it through "htonl()", and
1423 * generate code to compare against the result.
1425 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1426 bpf_pcap
->sf
.swapped
)
1427 proto
= SWAPLONG(proto
);
1428 proto
= htonl(proto
);
1430 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1434 * XXX should we check for first fragment if the protocol
1441 case ETHERTYPE_IPV6
:
1442 return(gen_cmp(2, BPF_B
,
1443 (bpf_int32
)htonl(ARCTYPE_INET6
)));
1446 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_IP
));
1447 b1
= gen_cmp(2, BPF_B
,
1448 (bpf_int32
)htonl(ARCTYPE_IP_OLD
));
1452 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_ARP
));
1453 b1
= gen_cmp(2, BPF_B
,
1454 (bpf_int32
)htonl(ARCTYPE_ARP_OLD
));
1457 case ETHERTYPE_REVARP
:
1458 return(gen_cmp(2, BPF_B
,
1459 (bpf_int32
)htonl(ARCTYPE_REVARP
)));
1460 case ETHERTYPE_ATALK
:
1461 return(gen_cmp(2, BPF_B
,
1462 (bpf_int32
)htonl(ARCTYPE_ATALK
)));
1468 case ETHERTYPE_ATALK
:
1477 * All the types that have no encapsulation should either be
1478 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1479 * all packets are IP packets, or should be handled in some
1480 * special case, if none of them are (if some are and some
1481 * aren't, the lack of encapsulation is a problem, as we'd
1482 * have to find some other way of determining the packet type).
1484 * Therefore, if "off_linktype" is -1, there's an error.
1486 if (off_linktype
== -1)
1490 * Any type not handled above should always have an Ethernet
1491 * type at an offset of "off_linktype". (PPP is partially
1492 * handled above - the protocol type is mapped from the
1493 * Ethernet and LLC types we use internally to the corresponding
1494 * PPP type - but the PPP type is always specified by a value
1495 * at "off_linktype", so we don't have to do the code generation
1498 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1502 * Check for an LLC SNAP packet with a given organization code and
1503 * protocol type; we check the entire contents of the 802.2 LLC and
1504 * snap headers, checking for DSAP and SSAP of SNAP and a control
1505 * field of 0x03 in the LLC header, and for the specified organization
1506 * code and protocol type in the SNAP header.
1508 static struct block
*
1509 gen_snap(orgcode
, ptype
, offset
)
1510 bpf_u_int32 orgcode
;
1514 u_char snapblock
[8];
1516 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1517 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1518 snapblock
[2] = 0x03; /* control = UI */
1519 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1520 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1521 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1522 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1523 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1524 return gen_bcmp(offset
, 8, snapblock
);
1527 static struct block
*
1528 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1532 u_int src_off
, dst_off
;
1534 struct block
*b0
, *b1
;
1548 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1549 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1555 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1556 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1563 b0
= gen_linktype(proto
);
1564 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1570 static struct block
*
1571 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1572 struct in6_addr
*addr
;
1573 struct in6_addr
*mask
;
1575 u_int src_off
, dst_off
;
1577 struct block
*b0
, *b1
;
1592 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1593 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1599 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1600 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1607 /* this order is important */
1608 a
= (u_int32_t
*)addr
;
1609 m
= (u_int32_t
*)mask
;
1610 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1611 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1613 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1615 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1617 b0
= gen_linktype(proto
);
1623 static struct block
*
1624 gen_ehostop(eaddr
, dir
)
1625 register const u_char
*eaddr
;
1628 register struct block
*b0
, *b1
;
1632 return gen_bcmp(6, 6, eaddr
);
1635 return gen_bcmp(0, 6, eaddr
);
1638 b0
= gen_ehostop(eaddr
, Q_SRC
);
1639 b1
= gen_ehostop(eaddr
, Q_DST
);
1645 b0
= gen_ehostop(eaddr
, Q_SRC
);
1646 b1
= gen_ehostop(eaddr
, Q_DST
);
1655 * Like gen_ehostop, but for DLT_FDDI
1657 static struct block
*
1658 gen_fhostop(eaddr
, dir
)
1659 register const u_char
*eaddr
;
1662 struct block
*b0
, *b1
;
1667 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1669 return gen_bcmp(6 + 1, 6, eaddr
);
1674 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1676 return gen_bcmp(0 + 1, 6, eaddr
);
1680 b0
= gen_fhostop(eaddr
, Q_SRC
);
1681 b1
= gen_fhostop(eaddr
, Q_DST
);
1687 b0
= gen_fhostop(eaddr
, Q_SRC
);
1688 b1
= gen_fhostop(eaddr
, Q_DST
);
1697 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1699 static struct block
*
1700 gen_thostop(eaddr
, dir
)
1701 register const u_char
*eaddr
;
1704 register struct block
*b0
, *b1
;
1708 return gen_bcmp(8, 6, eaddr
);
1711 return gen_bcmp(2, 6, eaddr
);
1714 b0
= gen_thostop(eaddr
, Q_SRC
);
1715 b1
= gen_thostop(eaddr
, Q_DST
);
1721 b0
= gen_thostop(eaddr
, Q_SRC
);
1722 b1
= gen_thostop(eaddr
, Q_DST
);
1731 * This is quite tricky because there may be pad bytes in front of the
1732 * DECNET header, and then there are two possible data packet formats that
1733 * carry both src and dst addresses, plus 5 packet types in a format that
1734 * carries only the src node, plus 2 types that use a different format and
1735 * also carry just the src node.
1739 * Instead of doing those all right, we just look for data packets with
1740 * 0 or 1 bytes of padding. If you want to look at other packets, that
1741 * will require a lot more hacking.
1743 * To add support for filtering on DECNET "areas" (network numbers)
1744 * one would want to add a "mask" argument to this routine. That would
1745 * make the filter even more inefficient, although one could be clever
1746 * and not generate masking instructions if the mask is 0xFFFF.
1748 static struct block
*
1749 gen_dnhostop(addr
, dir
, base_off
)
1754 struct block
*b0
, *b1
, *b2
, *tmp
;
1755 u_int offset_lh
; /* offset if long header is received */
1756 u_int offset_sh
; /* offset if short header is received */
1761 offset_sh
= 1; /* follows flags */
1762 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1766 offset_sh
= 3; /* follows flags, dstnode */
1767 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1771 /* Inefficient because we do our Calvinball dance twice */
1772 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1773 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1779 /* Inefficient because we do our Calvinball dance twice */
1780 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1781 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1786 bpf_error("ISO host filtering not implemented");
1791 b0
= gen_linktype(ETHERTYPE_DN
);
1792 /* Check for pad = 1, long header case */
1793 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1794 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1795 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1796 BPF_H
, (bpf_int32
)ntohs(addr
));
1798 /* Check for pad = 0, long header case */
1799 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1800 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1803 /* Check for pad = 1, short header case */
1804 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1805 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1806 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1807 BPF_H
, (bpf_int32
)ntohs(addr
));
1810 /* Check for pad = 0, short header case */
1811 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1812 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1816 /* Combine with test for linktype */
1821 static struct block
*
1822 gen_host(addr
, mask
, proto
, dir
)
1828 struct block
*b0
, *b1
;
1833 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1834 if (off_linktype
!= -1) {
1835 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1837 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1843 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1844 off_nl
+ 12, off_nl
+ 16);
1847 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1848 off_nl
+ 14, off_nl
+ 24);
1851 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1852 off_nl
+ 14, off_nl
+ 24);
1855 bpf_error("'tcp' modifier applied to host");
1858 bpf_error("'sctp' modifier applied to host");
1861 bpf_error("'udp' modifier applied to host");
1864 bpf_error("'icmp' modifier applied to host");
1867 bpf_error("'igmp' modifier applied to host");
1870 bpf_error("'igrp' modifier applied to host");
1873 bpf_error("'pim' modifier applied to host");
1876 bpf_error("'vrrp' modifier applied to host");
1879 bpf_error("ATALK host filtering not implemented");
1882 bpf_error("AARP host filtering not implemented");
1885 return gen_dnhostop(addr
, dir
, off_nl
);
1888 bpf_error("SCA host filtering not implemented");
1891 bpf_error("LAT host filtering not implemented");
1894 bpf_error("MOPDL host filtering not implemented");
1897 bpf_error("MOPRC host filtering not implemented");
1901 bpf_error("'ip6' modifier applied to ip host");
1904 bpf_error("'icmp6' modifier applied to host");
1908 bpf_error("'ah' modifier applied to host");
1911 bpf_error("'esp' modifier applied to host");
1914 bpf_error("ISO host filtering not implemented");
1917 bpf_error("'esis' modifier applied to host");
1920 bpf_error("'isis' modifier applied to host");
1923 bpf_error("'clnp' modifier applied to host");
1926 bpf_error("'stp' modifier applied to host");
1929 bpf_error("IPX host filtering not implemented");
1932 bpf_error("'netbeui' modifier applied to host");
1941 static struct block
*
1942 gen_host6(addr
, mask
, proto
, dir
)
1943 struct in6_addr
*addr
;
1944 struct in6_addr
*mask
;
1951 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1954 bpf_error("'ip' modifier applied to ip6 host");
1957 bpf_error("'rarp' modifier applied to ip6 host");
1960 bpf_error("'arp' modifier applied to ip6 host");
1963 bpf_error("'sctp' modifier applied to host");
1966 bpf_error("'tcp' modifier applied to host");
1969 bpf_error("'udp' modifier applied to host");
1972 bpf_error("'icmp' modifier applied to host");
1975 bpf_error("'igmp' modifier applied to host");
1978 bpf_error("'igrp' modifier applied to host");
1981 bpf_error("'pim' modifier applied to host");
1984 bpf_error("'vrrp' modifier applied to host");
1987 bpf_error("ATALK host filtering not implemented");
1990 bpf_error("AARP host filtering not implemented");
1993 bpf_error("'decnet' modifier applied to ip6 host");
1996 bpf_error("SCA host filtering not implemented");
1999 bpf_error("LAT host filtering not implemented");
2002 bpf_error("MOPDL host filtering not implemented");
2005 bpf_error("MOPRC host filtering not implemented");
2008 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2009 off_nl
+ 8, off_nl
+ 24);
2012 bpf_error("'icmp6' modifier applied to host");
2015 bpf_error("'ah' modifier applied to host");
2018 bpf_error("'esp' modifier applied to host");
2021 bpf_error("ISO host filtering not implemented");
2024 bpf_error("'esis' modifier applied to host");
2027 bpf_error("'isis' modifier applied to host");
2030 bpf_error("'clnp' modifier applied to host");
2033 bpf_error("'stp' modifier applied to host");
2036 bpf_error("IPX host filtering not implemented");
2039 bpf_error("'netbeui' modifier applied to host");
2049 static struct block
*
2050 gen_gateway(eaddr
, alist
, proto
, dir
)
2051 const u_char
*eaddr
;
2052 bpf_u_int32
**alist
;
2056 struct block
*b0
, *b1
, *tmp
;
2059 bpf_error("direction applied to 'gateway'");
2066 if (linktype
== DLT_EN10MB
)
2067 b0
= gen_ehostop(eaddr
, Q_OR
);
2068 else if (linktype
== DLT_FDDI
)
2069 b0
= gen_fhostop(eaddr
, Q_OR
);
2070 else if (linktype
== DLT_IEEE802
)
2071 b0
= gen_thostop(eaddr
, Q_OR
);
2074 "'gateway' supported only on ethernet, FDDI or token ring");
2076 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2078 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2086 bpf_error("illegal modifier of 'gateway'");
2092 gen_proto_abbrev(proto
)
2103 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2105 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2111 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2113 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2119 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2121 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2127 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2130 #ifndef IPPROTO_IGMP
2131 #define IPPROTO_IGMP 2
2135 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2138 #ifndef IPPROTO_IGRP
2139 #define IPPROTO_IGRP 9
2142 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2146 #define IPPROTO_PIM 103
2150 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2152 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2157 #ifndef IPPROTO_VRRP
2158 #define IPPROTO_VRRP 112
2162 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2166 b1
= gen_linktype(ETHERTYPE_IP
);
2170 b1
= gen_linktype(ETHERTYPE_ARP
);
2174 b1
= gen_linktype(ETHERTYPE_REVARP
);
2178 bpf_error("link layer applied in wrong context");
2181 b1
= gen_linktype(ETHERTYPE_ATALK
);
2185 b1
= gen_linktype(ETHERTYPE_AARP
);
2189 b1
= gen_linktype(ETHERTYPE_DN
);
2193 b1
= gen_linktype(ETHERTYPE_SCA
);
2197 b1
= gen_linktype(ETHERTYPE_LAT
);
2201 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2205 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2210 b1
= gen_linktype(ETHERTYPE_IPV6
);
2213 #ifndef IPPROTO_ICMPV6
2214 #define IPPROTO_ICMPV6 58
2217 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2222 #define IPPROTO_AH 51
2225 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2227 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2233 #define IPPROTO_ESP 50
2236 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2238 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2244 b1
= gen_linktype(LLCSAP_ISONS
);
2248 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2252 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2256 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2260 b1
= gen_linktype(LLCSAP_8021D
);
2264 b1
= gen_linktype(LLCSAP_IPX
);
2268 b1
= gen_linktype(LLCSAP_NETBEUI
);
2277 static struct block
*
2284 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2285 s
->s
.k
= off_nl
+ 6;
2286 b
= new_block(JMP(BPF_JSET
));
2294 static struct block
*
2295 gen_portatom(off
, v
)
2302 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2305 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2306 s
->next
->s
.k
= off_nl
+ off
;
2308 b
= new_block(JMP(BPF_JEQ
));
2316 static struct block
*
2317 gen_portatom6(off
, v
)
2321 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2326 gen_portop(port
, proto
, dir
)
2327 int port
, proto
, dir
;
2329 struct block
*b0
, *b1
, *tmp
;
2331 /* ip proto 'proto' */
2332 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2338 b1
= gen_portatom(0, (bpf_int32
)port
);
2342 b1
= gen_portatom(2, (bpf_int32
)port
);
2347 tmp
= gen_portatom(0, (bpf_int32
)port
);
2348 b1
= gen_portatom(2, (bpf_int32
)port
);
2353 tmp
= gen_portatom(0, (bpf_int32
)port
);
2354 b1
= gen_portatom(2, (bpf_int32
)port
);
2366 static struct block
*
2367 gen_port(port
, ip_proto
, dir
)
2372 struct block
*b0
, *b1
, *tmp
;
2374 /* ether proto ip */
2375 b0
= gen_linktype(ETHERTYPE_IP
);
2381 b1
= gen_portop(port
, ip_proto
, dir
);
2385 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2386 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2388 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
2401 gen_portop6(port
, proto
, dir
)
2402 int port
, proto
, dir
;
2404 struct block
*b0
, *b1
, *tmp
;
2406 /* ip proto 'proto' */
2407 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2411 b1
= gen_portatom6(0, (bpf_int32
)port
);
2415 b1
= gen_portatom6(2, (bpf_int32
)port
);
2420 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2421 b1
= gen_portatom6(2, (bpf_int32
)port
);
2426 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2427 b1
= gen_portatom6(2, (bpf_int32
)port
);
2439 static struct block
*
2440 gen_port6(port
, ip_proto
, dir
)
2445 struct block
*b0
, *b1
, *tmp
;
2447 /* ether proto ip */
2448 b0
= gen_linktype(ETHERTYPE_IPV6
);
2454 b1
= gen_portop6(port
, ip_proto
, dir
);
2458 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2459 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2461 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
2474 lookup_proto(name
, proto
)
2475 register const char *name
;
2485 v
= pcap_nametoproto(name
);
2486 if (v
== PROTO_UNDEF
)
2487 bpf_error("unknown ip proto '%s'", name
);
2491 /* XXX should look up h/w protocol type based on linktype */
2492 v
= pcap_nametoeproto(name
);
2493 if (v
== PROTO_UNDEF
)
2494 bpf_error("unknown ether proto '%s'", name
);
2498 if (strcmp(name
, "esis") == 0)
2500 else if (strcmp(name
, "isis") == 0)
2502 else if (strcmp(name
, "clnp") == 0)
2505 bpf_error("unknown osi proto '%s'", name
);
2525 static struct block
*
2526 gen_protochain(v
, proto
, dir
)
2531 #ifdef NO_PROTOCHAIN
2532 return gen_proto(v
, proto
, dir
);
2534 struct block
*b0
, *b
;
2535 struct slist
*s
[100];
2536 int fix2
, fix3
, fix4
, fix5
;
2537 int ahcheck
, again
, end
;
2539 int reg2
= alloc_reg();
2541 memset(s
, 0, sizeof(s
));
2542 fix2
= fix3
= fix4
= fix5
= 0;
2549 b0
= gen_protochain(v
, Q_IP
, dir
);
2550 b
= gen_protochain(v
, Q_IPV6
, dir
);
2554 bpf_error("bad protocol applied for 'protochain'");
2558 no_optimize
= 1; /*this code is not compatible with optimzer yet */
2561 * s[0] is a dummy entry to protect other BPF insn from damaged
2562 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
2563 * hard to find interdependency made by jump table fixup.
2566 s
[i
] = new_stmt(0); /*dummy*/
2571 b0
= gen_linktype(ETHERTYPE_IP
);
2574 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2575 s
[i
]->s
.k
= off_nl
+ 9;
2577 /* X = ip->ip_hl << 2 */
2578 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2584 b0
= gen_linktype(ETHERTYPE_IPV6
);
2586 /* A = ip6->ip_nxt */
2587 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2588 s
[i
]->s
.k
= off_nl
+ 6;
2590 /* X = sizeof(struct ip6_hdr) */
2591 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
2597 bpf_error("unsupported proto to gen_protochain");
2601 /* again: if (A == v) goto end; else fall through; */
2603 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2605 s
[i
]->s
.jt
= NULL
; /*later*/
2606 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2610 #ifndef IPPROTO_NONE
2611 #define IPPROTO_NONE 59
2613 /* if (A == IPPROTO_NONE) goto end */
2614 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2615 s
[i
]->s
.jt
= NULL
; /*later*/
2616 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2617 s
[i
]->s
.k
= IPPROTO_NONE
;
2618 s
[fix5
]->s
.jf
= s
[i
];
2623 if (proto
== Q_IPV6
) {
2624 int v6start
, v6end
, v6advance
, j
;
2627 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
2628 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2629 s
[i
]->s
.jt
= NULL
; /*later*/
2630 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2631 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
2632 s
[fix2
]->s
.jf
= s
[i
];
2634 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
2635 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2636 s
[i
]->s
.jt
= NULL
; /*later*/
2637 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2638 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
2640 /* if (A == IPPROTO_ROUTING) goto v6advance */
2641 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2642 s
[i
]->s
.jt
= NULL
; /*later*/
2643 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2644 s
[i
]->s
.k
= IPPROTO_ROUTING
;
2646 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
2647 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2648 s
[i
]->s
.jt
= NULL
; /*later*/
2649 s
[i
]->s
.jf
= NULL
; /*later*/
2650 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
2661 * X = X + (P[X + 1] + 1) * 8;
2664 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2666 /* A = P[X + packet head] */
2667 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2671 s
[i
] = new_stmt(BPF_ST
);
2675 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2678 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2682 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2684 /* A = P[X + packet head]; */
2685 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2689 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2693 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2697 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2700 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2704 /* goto again; (must use BPF_JA for backward jump) */
2705 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2706 s
[i
]->s
.k
= again
- i
- 1;
2707 s
[i
- 1]->s
.jf
= s
[i
];
2711 for (j
= v6start
; j
<= v6end
; j
++)
2712 s
[j
]->s
.jt
= s
[v6advance
];
2717 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2719 s
[fix2
]->s
.jf
= s
[i
];
2725 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2726 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2727 s
[i
]->s
.jt
= NULL
; /*later*/
2728 s
[i
]->s
.jf
= NULL
; /*later*/
2729 s
[i
]->s
.k
= IPPROTO_AH
;
2731 s
[fix3
]->s
.jf
= s
[ahcheck
];
2738 * X = X + (P[X + 1] + 2) * 4;
2741 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2743 /* A = P[X + packet head]; */
2744 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2748 s
[i
] = new_stmt(BPF_ST
);
2752 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2755 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2759 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2761 /* A = P[X + packet head] */
2762 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2766 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2770 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2774 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2777 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2781 /* goto again; (must use BPF_JA for backward jump) */
2782 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2783 s
[i
]->s
.k
= again
- i
- 1;
2788 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2790 s
[fix2
]->s
.jt
= s
[end
];
2791 s
[fix4
]->s
.jf
= s
[end
];
2792 s
[fix5
]->s
.jt
= s
[end
];
2799 for (i
= 0; i
< max
- 1; i
++)
2800 s
[i
]->next
= s
[i
+ 1];
2801 s
[max
- 1]->next
= NULL
;
2806 b
= new_block(JMP(BPF_JEQ
));
2807 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2817 static struct block
*
2818 gen_proto(v
, proto
, dir
)
2823 struct block
*b0
, *b1
;
2825 if (dir
!= Q_DEFAULT
)
2826 bpf_error("direction applied to 'proto'");
2831 b0
= gen_proto(v
, Q_IP
, dir
);
2832 b1
= gen_proto(v
, Q_IPV6
, dir
);
2839 b0
= gen_linktype(ETHERTYPE_IP
);
2841 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2843 b1
= gen_protochain(v
, Q_IP
);
2849 b0
= gen_linktype(LLCSAP_ISONS
);
2850 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
2855 bpf_error("arp does not encapsulate another protocol");
2859 bpf_error("rarp does not encapsulate another protocol");
2863 bpf_error("atalk encapsulation is not specifiable");
2867 bpf_error("decnet encapsulation is not specifiable");
2871 bpf_error("sca does not encapsulate another protocol");
2875 bpf_error("lat does not encapsulate another protocol");
2879 bpf_error("moprc does not encapsulate another protocol");
2883 bpf_error("mopdl does not encapsulate another protocol");
2887 return gen_linktype(v
);
2890 bpf_error("'udp proto' is bogus");
2894 bpf_error("'tcp proto' is bogus");
2898 bpf_error("'sctp proto' is bogus");
2902 bpf_error("'icmp proto' is bogus");
2906 bpf_error("'igmp proto' is bogus");
2910 bpf_error("'igrp proto' is bogus");
2914 bpf_error("'pim proto' is bogus");
2918 bpf_error("'vrrp proto' is bogus");
2923 b0
= gen_linktype(ETHERTYPE_IPV6
);
2925 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2927 b1
= gen_protochain(v
, Q_IPV6
);
2933 bpf_error("'icmp6 proto' is bogus");
2937 bpf_error("'ah proto' is bogus");
2940 bpf_error("'ah proto' is bogus");
2943 bpf_error("'stp proto' is bogus");
2946 bpf_error("'ipx proto' is bogus");
2949 bpf_error("'netbeui proto' is bogus");
2960 register const char *name
;
2963 int proto
= q
.proto
;
2967 bpf_u_int32 mask
, addr
;
2969 bpf_u_int32
**alist
;
2972 struct sockaddr_in
*sin
;
2973 struct sockaddr_in6
*sin6
;
2974 struct addrinfo
*res
, *res0
;
2975 struct in6_addr mask128
;
2977 struct block
*b
, *tmp
;
2978 int port
, real_proto
;
2983 addr
= pcap_nametonetaddr(name
);
2985 bpf_error("unknown network '%s'", name
);
2986 /* Left justify network addr and calculate its network mask */
2988 while (addr
&& (addr
& 0xff000000) == 0) {
2992 return gen_host(addr
, mask
, proto
, dir
);
2996 if (proto
== Q_LINK
) {
3000 eaddr
= pcap_ether_hostton(name
);
3003 "unknown ether host '%s'", name
);
3004 b
= gen_ehostop(eaddr
, dir
);
3009 eaddr
= pcap_ether_hostton(name
);
3012 "unknown FDDI host '%s'", name
);
3013 b
= gen_fhostop(eaddr
, dir
);
3018 eaddr
= pcap_ether_hostton(name
);
3021 "unknown token ring host '%s'", name
);
3022 b
= gen_thostop(eaddr
, dir
);
3028 "only ethernet/FDDI/token ring supports link-level host name");
3031 } else if (proto
== Q_DECNET
) {
3032 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
3034 * I don't think DECNET hosts can be multihomed, so
3035 * there is no need to build up a list of addresses
3037 return (gen_host(dn_addr
, 0, proto
, dir
));
3040 alist
= pcap_nametoaddr(name
);
3041 if (alist
== NULL
|| *alist
== NULL
)
3042 bpf_error("unknown host '%s'", name
);
3044 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
3046 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
3048 tmp
= gen_host(**alist
++, 0xffffffff,
3055 memset(&mask128
, 0xff, sizeof(mask128
));
3056 res0
= res
= pcap_nametoaddrinfo(name
);
3058 bpf_error("unknown host '%s'", name
);
3060 tproto
= tproto6
= proto
;
3061 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
3065 for (res
= res0
; res
; res
= res
->ai_next
) {
3066 switch (res
->ai_family
) {
3068 if (tproto
== Q_IPV6
)
3071 sin
= (struct sockaddr_in
*)
3073 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
3074 0xffffffff, tproto
, dir
);
3077 if (tproto6
== Q_IP
)
3080 sin6
= (struct sockaddr_in6
*)
3082 tmp
= gen_host6(&sin6
->sin6_addr
,
3083 &mask128
, tproto6
, dir
);
3094 bpf_error("unknown host '%s'%s", name
,
3095 (proto
== Q_DEFAULT
)
3097 : " for specified address family");
3104 if (proto
!= Q_DEFAULT
&&
3105 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
3106 bpf_error("illegal qualifier of 'port'");
3107 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
3108 bpf_error("unknown port '%s'", name
);
3109 if (proto
== Q_UDP
) {
3110 if (real_proto
== IPPROTO_TCP
)
3111 bpf_error("port '%s' is tcp", name
);
3112 else if (real_proto
== IPPROTO_SCTP
)
3113 bpf_error("port '%s' is sctp", name
);
3115 /* override PROTO_UNDEF */
3116 real_proto
= IPPROTO_UDP
;
3118 if (proto
== Q_TCP
) {
3119 if (real_proto
== IPPROTO_UDP
)
3120 bpf_error("port '%s' is udp", name
);
3122 else if (real_proto
== IPPROTO_SCTP
)
3123 bpf_error("port '%s' is sctp", name
);
3125 /* override PROTO_UNDEF */
3126 real_proto
= IPPROTO_TCP
;
3128 if (proto
== Q_SCTP
) {
3129 if (real_proto
== IPPROTO_UDP
)
3130 bpf_error("port '%s' is udp", name
);
3132 else if (real_proto
== IPPROTO_TCP
)
3133 bpf_error("port '%s' is tcp", name
);
3135 /* override PROTO_UNDEF */
3136 real_proto
= IPPROTO_SCTP
;
3139 return gen_port(port
, real_proto
, dir
);
3143 b
= gen_port(port
, real_proto
, dir
);
3144 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3151 eaddr
= pcap_ether_hostton(name
);
3153 bpf_error("unknown ether host: %s", name
);
3155 alist
= pcap_nametoaddr(name
);
3156 if (alist
== NULL
|| *alist
== NULL
)
3157 bpf_error("unknown host '%s'", name
);
3158 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
3162 bpf_error("'gateway' not supported in this configuration");
3166 real_proto
= lookup_proto(name
, proto
);
3167 if (real_proto
>= 0)
3168 return gen_proto(real_proto
, proto
, dir
);
3170 bpf_error("unknown protocol: %s", name
);
3173 real_proto
= lookup_proto(name
, proto
);
3174 if (real_proto
>= 0)
3175 return gen_protochain(real_proto
, proto
, dir
);
3177 bpf_error("unknown protocol: %s", name
);
3189 gen_mcode(s1
, s2
, masklen
, q
)
3190 register const char *s1
, *s2
;
3191 register int masklen
;
3194 register int nlen
, mlen
;
3197 nlen
= __pcap_atoin(s1
, &n
);
3198 /* Promote short ipaddr */
3202 mlen
= __pcap_atoin(s2
, &m
);
3203 /* Promote short ipaddr */
3206 bpf_error("non-network bits set in \"%s mask %s\"",
3209 /* Convert mask len to mask */
3211 bpf_error("mask length must be <= 32");
3212 m
= 0xffffffff << (32 - masklen
);
3214 bpf_error("non-network bits set in \"%s/%d\"",
3221 return gen_host(n
, m
, q
.proto
, q
.dir
);
3224 bpf_error("Mask syntax for networks only");
3231 register const char *s
;
3236 int proto
= q
.proto
;
3242 else if (q
.proto
== Q_DECNET
)
3243 vlen
= __pcap_atodn(s
, &v
);
3245 vlen
= __pcap_atoin(s
, &v
);
3252 if (proto
== Q_DECNET
)
3253 return gen_host(v
, 0, proto
, dir
);
3254 else if (proto
== Q_LINK
) {
3255 bpf_error("illegal link layer address");
3258 if (s
== NULL
&& q
.addr
== Q_NET
) {
3259 /* Promote short net number */
3260 while (v
&& (v
& 0xff000000) == 0) {
3265 /* Promote short ipaddr */
3269 return gen_host(v
, mask
, proto
, dir
);
3274 proto
= IPPROTO_UDP
;
3275 else if (proto
== Q_TCP
)
3276 proto
= IPPROTO_TCP
;
3277 else if (proto
== Q_SCTP
)
3278 proto
= IPPROTO_SCTP
;
3279 else if (proto
== Q_DEFAULT
)
3280 proto
= PROTO_UNDEF
;
3282 bpf_error("illegal qualifier of 'port'");
3285 return gen_port((int)v
, proto
, dir
);
3289 b
= gen_port((int)v
, proto
, dir
);
3290 gen_or(gen_port6((int)v
, proto
, dir
), b
);
3296 bpf_error("'gateway' requires a name");
3300 return gen_proto((int)v
, proto
, dir
);
3303 return gen_protochain((int)v
, proto
, dir
);
3318 gen_mcode6(s1
, s2
, masklen
, q
)
3319 register const char *s1
, *s2
;
3320 register int masklen
;
3323 struct addrinfo
*res
;
3324 struct in6_addr
*addr
;
3325 struct in6_addr mask
;
3330 bpf_error("no mask %s supported", s2
);
3332 res
= pcap_nametoaddrinfo(s1
);
3334 bpf_error("invalid ip6 address %s", s1
);
3336 bpf_error("%s resolved to multiple address", s1
);
3337 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3339 if (sizeof(mask
) * 8 < masklen
)
3340 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3341 memset(&mask
, 0, sizeof(mask
));
3342 memset(&mask
, 0xff, masklen
/ 8);
3344 mask
.s6_addr
[masklen
/ 8] =
3345 (0xff << (8 - masklen
% 8)) & 0xff;
3348 a
= (u_int32_t
*)addr
;
3349 m
= (u_int32_t
*)&mask
;
3350 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3351 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3352 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
3360 bpf_error("Mask syntax for networks only");
3364 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
3369 bpf_error("invalid qualifier against IPv6 address");
3377 register const u_char
*eaddr
;
3380 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3381 if (linktype
== DLT_EN10MB
)
3382 return gen_ehostop(eaddr
, (int)q
.dir
);
3383 if (linktype
== DLT_FDDI
)
3384 return gen_fhostop(eaddr
, (int)q
.dir
);
3385 if (linktype
== DLT_IEEE802
)
3386 return gen_thostop(eaddr
, (int)q
.dir
);
3387 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
3389 bpf_error("ethernet address used in non-ether expression");
3395 struct slist
*s0
, *s1
;
3398 * This is definitely not the best way to do this, but the
3399 * lists will rarely get long.
3406 static struct slist
*
3412 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3417 static struct slist
*
3423 s
= new_stmt(BPF_LD
|BPF_MEM
);
3429 gen_load(proto
, index
, size
)
3434 struct slist
*s
, *tmp
;
3436 int regno
= alloc_reg();
3438 free_reg(index
->regno
);
3442 bpf_error("data size must be 1, 2, or 4");
3458 bpf_error("unsupported index operation");
3461 s
= xfer_to_x(index
);
3462 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3464 sappend(index
->s
, s
);
3479 /* XXX Note that we assume a fixed link header here. */
3480 s
= xfer_to_x(index
);
3481 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3484 sappend(index
->s
, s
);
3486 b
= gen_proto_abbrev(proto
);
3488 gen_and(index
->b
, b
);
3500 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3502 sappend(s
, xfer_to_a(index
));
3503 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3504 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3505 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3507 sappend(index
->s
, s
);
3509 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
3511 gen_and(index
->b
, b
);
3513 gen_and(gen_proto_abbrev(Q_IP
), b
);
3519 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
3523 index
->regno
= regno
;
3524 s
= new_stmt(BPF_ST
);
3526 sappend(index
->s
, s
);
3532 gen_relation(code
, a0
, a1
, reversed
)
3534 struct arth
*a0
, *a1
;
3537 struct slist
*s0
, *s1
, *s2
;
3538 struct block
*b
, *tmp
;
3542 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
3543 b
= new_block(JMP(code
));
3544 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
3545 reversed
= !reversed
;
3546 b
->s
.k
= 0x80000000;
3554 sappend(a0
->s
, a1
->s
);
3558 free_reg(a0
->regno
);
3559 free_reg(a1
->regno
);
3561 /* 'and' together protocol checks */
3564 gen_and(a0
->b
, tmp
= a1
->b
);
3580 int regno
= alloc_reg();
3581 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
3584 s
= new_stmt(BPF_LD
|BPF_LEN
);
3585 s
->next
= new_stmt(BPF_ST
);
3586 s
->next
->s
.k
= regno
;
3601 a
= (struct arth
*)newchunk(sizeof(*a
));
3605 s
= new_stmt(BPF_LD
|BPF_IMM
);
3607 s
->next
= new_stmt(BPF_ST
);
3623 s
= new_stmt(BPF_ALU
|BPF_NEG
);
3626 s
= new_stmt(BPF_ST
);
3634 gen_arth(code
, a0
, a1
)
3636 struct arth
*a0
, *a1
;
3638 struct slist
*s0
, *s1
, *s2
;
3642 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
3647 sappend(a0
->s
, a1
->s
);
3649 free_reg(a0
->regno
);
3650 free_reg(a1
->regno
);
3652 s0
= new_stmt(BPF_ST
);
3653 a0
->regno
= s0
->s
.k
= alloc_reg();
3660 * Here we handle simple allocation of the scratch registers.
3661 * If too many registers are alloc'd, the allocator punts.
3663 static int regused
[BPF_MEMWORDS
];
3667 * Return the next free register.
3672 int n
= BPF_MEMWORDS
;
3675 if (regused
[curreg
])
3676 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
3678 regused
[curreg
] = 1;
3682 bpf_error("too many registers needed to evaluate expression");
3687 * Return a register to the table so it can
3697 static struct block
*
3704 s
= new_stmt(BPF_LD
|BPF_LEN
);
3705 b
= new_block(JMP(jmp
));
3716 return gen_len(BPF_JGE
, n
);
3720 * Actually, this is less than or equal.
3728 b
= gen_len(BPF_JGT
, n
);
3735 gen_byteop(op
, idx
, val
)
3746 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3749 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3750 b
->s
.code
= JMP(BPF_JGE
);
3755 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3756 b
->s
.code
= JMP(BPF_JGT
);
3760 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3764 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3768 b
= new_block(JMP(BPF_JEQ
));
3775 static u_char abroadcast
[] = { 0x0 };
3778 gen_broadcast(proto
)
3781 bpf_u_int32 hostmask
;
3782 struct block
*b0
, *b1
, *b2
;
3783 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3789 if (linktype
== DLT_ARCNET
)
3790 return gen_ahostop(abroadcast
, Q_DST
);
3791 if (linktype
== DLT_EN10MB
)
3792 return gen_ehostop(ebroadcast
, Q_DST
);
3793 if (linktype
== DLT_FDDI
)
3794 return gen_fhostop(ebroadcast
, Q_DST
);
3795 if (linktype
== DLT_IEEE802
)
3796 return gen_thostop(ebroadcast
, Q_DST
);
3797 bpf_error("not a broadcast link");
3801 b0
= gen_linktype(ETHERTYPE_IP
);
3802 hostmask
= ~netmask
;
3803 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3804 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3805 (bpf_int32
)(~0 & hostmask
), hostmask
);
3810 bpf_error("only ether/ip broadcast filters supported");
3814 gen_multicast(proto
)
3817 register struct block
*b0
, *b1
;
3818 register struct slist
*s
;
3824 if (linktype
== DLT_ARCNET
)
3825 /* all ARCnet multicasts use the same address */
3826 return gen_ahostop(abroadcast
, Q_DST
);
3828 if (linktype
== DLT_EN10MB
) {
3829 /* ether[0] & 1 != 0 */
3830 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3832 b0
= new_block(JMP(BPF_JSET
));
3838 if (linktype
== DLT_FDDI
) {
3839 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3840 /* fddi[1] & 1 != 0 */
3841 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3843 b0
= new_block(JMP(BPF_JSET
));
3849 /* TODO - check how token ring handles multicast */
3850 /* if (linktype == DLT_IEEE802) ... */
3852 /* Link not known to support multicasts */
3856 b0
= gen_linktype(ETHERTYPE_IP
);
3857 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3858 b1
->s
.code
= JMP(BPF_JGE
);
3864 b0
= gen_linktype(ETHERTYPE_IPV6
);
3865 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3870 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3874 * generate command for inbound/outbound. It's here so we can
3875 * make it link-type specific. 'dir' = 0 implies "inbound",
3876 * = 1 implies "outbound".
3882 register struct block
*b0
;
3885 * Only some data link types support inbound/outbound qualifiers.
3890 b0
= gen_relation(BPF_JEQ
,
3891 gen_load(Q_LINK
, gen_loadi(0), 1),
3897 bpf_error("inbound/outbound not supported on linktype %d\n",
3907 register const u_char
*eaddr
;
3910 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3911 if (linktype
== DLT_ARCNET
)
3912 return gen_ahostop(eaddr
, (int)q
.dir
);
3914 bpf_error("ARCnet address used in non-arc expression");
3918 static struct block
*
3919 gen_ahostop(eaddr
, dir
)
3920 register const u_char
*eaddr
;
3923 register struct block
*b0
, *b1
;
3926 /* src comes first, different from Ethernet */
3928 return gen_bcmp(0, 1, eaddr
);
3931 return gen_bcmp(1, 1, eaddr
);
3934 b0
= gen_ahostop(eaddr
, Q_SRC
);
3935 b1
= gen_ahostop(eaddr
, Q_DST
);
3941 b0
= gen_ahostop(eaddr
, Q_SRC
);
3942 b1
= gen_ahostop(eaddr
, Q_DST
);
3951 * support IEEE 802.1Q VLAN trunk over ethernet
3960 * Change the offsets to point to the type and data fields within
3961 * the VLAN packet. This is somewhat of a kludge.
3963 if (orig_nl
== (u_int
)-1) {
3964 orig_linktype
= off_linktype
; /* save original values */
3966 orig_nl_nosnap
= off_nl_nosnap
;
3977 bpf_error("no VLAN support for data link type %d",
3983 /* check for VLAN */
3984 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3986 /* If a specific VLAN is requested, check VLAN id */
3987 if (vlan_num
>= 0) {
3990 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);