]>
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.174 2002-08-06 06:13:20 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
40 * XXX - why was this included even on UNIX?
49 #include <sys/param.h>
52 #include <netinet/in.h>
64 #include "ethertype.h"
69 #include "sunatmpos.h"
75 #include <netdb.h> /* for "struct addrinfo" */
78 #include <pcap-namedb.h>
83 #define IPPROTO_SCTP 132
86 #ifdef HAVE_OS_PROTO_H
90 #define JMP(c) ((c)|BPF_JMP|BPF_K)
93 static jmp_buf top_ctx
;
94 static pcap_t
*bpf_pcap
;
96 /* Hack for updating VLAN offsets. */
97 static u_int orig_linktype
= -1, orig_nl
= -1, orig_nl_nosnap
= -1;
101 int pcap_fddipad
= PCAP_FDDIPAD
;
108 bpf_error(const char *fmt
, ...)
114 if (bpf_pcap
!= NULL
)
115 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
122 static void init_linktype(int);
124 static int alloc_reg(void);
125 static void free_reg(int);
127 static struct block
*root
;
130 * We divy out chunks of memory rather than call malloc each time so
131 * we don't have to worry about leaking memory. It's probably
132 * not a big deal if all this memory was wasted but it this ever
133 * goes into a library that would probably not be a good idea.
136 #define CHUNK0SIZE 1024
142 static struct chunk chunks
[NCHUNKS
];
143 static int cur_chunk
;
145 static void *newchunk(u_int
);
146 static void freechunks(void);
147 static inline struct block
*new_block(int);
148 static inline struct slist
*new_stmt(int);
149 static struct block
*gen_retblk(int);
150 static inline void syntax(void);
152 static void backpatch(struct block
*, struct block
*);
153 static void merge(struct block
*, struct block
*);
154 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
155 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
156 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
157 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
158 static struct block
*gen_ncmp(bpf_u_int32
, bpf_u_int32
, bpf_u_int32
,
159 bpf_u_int32
, bpf_u_int32
, int);
160 static struct block
*gen_uncond(int);
161 static inline struct block
*gen_true(void);
162 static inline struct block
*gen_false(void);
163 static struct block
*gen_linktype(int);
164 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
165 static struct block
*gen_llc(int);
166 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
168 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
170 static struct block
*gen_ahostop(const u_char
*, int);
171 static struct block
*gen_ehostop(const u_char
*, int);
172 static struct block
*gen_fhostop(const u_char
*, int);
173 static struct block
*gen_thostop(const u_char
*, int);
174 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
175 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
177 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
180 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
182 static struct block
*gen_ipfrag(void);
183 static struct block
*gen_portatom(int, bpf_int32
);
185 static struct block
*gen_portatom6(int, bpf_int32
);
187 struct block
*gen_portop(int, int, int);
188 static struct block
*gen_port(int, int, int);
190 struct block
*gen_portop6(int, int, int);
191 static struct block
*gen_port6(int, int, int);
193 static int lookup_proto(const char *, int);
194 static struct block
*gen_protochain(int, int, int);
195 static struct block
*gen_proto(int, int, int);
196 static struct slist
*xfer_to_x(struct arth
*);
197 static struct slist
*xfer_to_a(struct arth
*);
198 static struct block
*gen_len(int, int);
200 static struct block
*gen_msg_abbrev(int type
);
210 /* XXX Round up to nearest long. */
211 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
213 /* XXX Round up to structure boundary. */
217 cp
= &chunks
[cur_chunk
];
218 if (n
> cp
->n_left
) {
219 ++cp
, k
= ++cur_chunk
;
221 bpf_error("out of memory");
222 size
= CHUNK0SIZE
<< k
;
223 cp
->m
= (void *)malloc(size
);
224 memset((char *)cp
->m
, 0, size
);
227 bpf_error("out of memory");
230 return (void *)((char *)cp
->m
+ cp
->n_left
);
239 for (i
= 0; i
< NCHUNKS
; ++i
)
240 if (chunks
[i
].m
!= NULL
) {
247 * A strdup whose allocations are freed after code generation is over.
251 register const char *s
;
253 int n
= strlen(s
) + 1;
254 char *cp
= newchunk(n
);
260 static inline struct block
*
266 p
= (struct block
*)newchunk(sizeof(*p
));
273 static inline struct slist
*
279 p
= (struct slist
*)newchunk(sizeof(*p
));
285 static struct block
*
289 struct block
*b
= new_block(BPF_RET
|BPF_K
);
298 bpf_error("syntax error in filter expression");
301 static bpf_u_int32 netmask
;
306 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
307 char *buf
, int optimize
, bpf_u_int32 mask
)
316 if (setjmp(top_ctx
)) {
324 snaplen
= pcap_snapshot(p
);
326 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
327 "snaplen of 0 rejects all packets");
331 lex_init(buf
? buf
: "");
332 init_linktype(pcap_datalink(p
));
339 root
= gen_retblk(snaplen
);
341 if (optimize
&& !no_optimize
) {
344 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
345 bpf_error("expression rejects all packets");
347 program
->bf_insns
= icode_to_fcode(root
, &len
);
348 program
->bf_len
= len
;
356 * entry point for using the compiler with no pcap open
357 * pass in all the stuff that is needed explicitly instead.
360 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
361 struct bpf_program
*program
,
362 char *buf
, int optimize
, bpf_u_int32 mask
)
367 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
370 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
376 * Clean up a "struct bpf_program" by freeing all the memory allocated
380 pcap_freecode(struct bpf_program
*program
)
383 if (program
->bf_insns
!= NULL
) {
384 free((char *)program
->bf_insns
);
385 program
->bf_insns
= NULL
;
390 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
391 * which of the jt and jf fields has been resolved and which is a pointer
392 * back to another unresolved block (or nil). At least one of the fields
393 * in each block is already resolved.
396 backpatch(list
, target
)
397 struct block
*list
, *target
;
414 * Merge the lists in b0 and b1, using the 'sense' field to indicate
415 * which of jt and jf is the link.
419 struct block
*b0
, *b1
;
421 register struct block
**p
= &b0
;
423 /* Find end of list. */
425 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
427 /* Concatenate the lists. */
435 backpatch(p
, gen_retblk(snaplen
));
436 p
->sense
= !p
->sense
;
437 backpatch(p
, gen_retblk(0));
443 struct block
*b0
, *b1
;
445 backpatch(b0
, b1
->head
);
446 b0
->sense
= !b0
->sense
;
447 b1
->sense
= !b1
->sense
;
449 b1
->sense
= !b1
->sense
;
455 struct block
*b0
, *b1
;
457 b0
->sense
= !b0
->sense
;
458 backpatch(b0
, b1
->head
);
459 b0
->sense
= !b0
->sense
;
468 b
->sense
= !b
->sense
;
471 static struct block
*
472 gen_cmp(offset
, size
, v
)
479 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
482 b
= new_block(JMP(BPF_JEQ
));
489 static struct block
*
490 gen_cmp_gt(offset
, size
, v
)
497 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
500 b
= new_block(JMP(BPF_JGT
));
507 static struct block
*
508 gen_mcmp(offset
, size
, v
, mask
)
513 struct block
*b
= gen_cmp(offset
, size
, v
);
516 if (mask
!= 0xffffffff) {
517 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
524 static struct block
*
525 gen_bcmp(offset
, size
, v
)
526 register u_int offset
, size
;
527 register const u_char
*v
;
529 register struct block
*b
, *tmp
;
533 register const u_char
*p
= &v
[size
- 4];
534 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
535 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
537 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
544 register const u_char
*p
= &v
[size
- 2];
545 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
547 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
554 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
562 static struct block
*
563 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
564 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
570 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
573 if (mask
!= 0xffffffff) {
574 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
578 b
= new_block(JMP(jtype
));
581 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
587 * Various code constructs need to know the layout of the data link
588 * layer. These variables give the necessary offsets.
592 * "off_linktype" is the offset to information in the link-layer header
593 * giving the packet type.
595 * For Ethernet, it's the offset of the Ethernet type field.
597 * For link-layer types that always use 802.2 headers, it's the
598 * offset of the LLC header.
600 * For PPP, it's the offset of the PPP type field.
602 * For Cisco HDLC, it's the offset of the CHDLC type field.
604 * For BSD loopback, it's the offset of the AF_ value.
606 * For Linux cooked sockets, it's the offset of the type field.
608 * It's set to -1 for no encapsulation, in which case, IP is assumed.
610 static u_int off_linktype
;
613 * TRUE if the link layer includes an ATM pseudo-header.
615 static int is_atm
= 0;
618 * These are offsets for the ATM pseudo-header.
620 static u_int off_vpi
;
621 static u_int off_vci
;
622 static u_int off_proto
;
625 * This is the offset to the message type for Q.2931 messages.
627 static u_int off_msg_type
;
630 * These are offsets to the beginning of the network-layer header.
632 * If the link layer never uses 802.2 LLC:
634 * "off_nl" and "off_nl_nosnap" are the same.
636 * If the link layer always uses 802.2 LLC:
638 * "off_nl" is the offset if there's a SNAP header following
641 * "off_nl_nosnap" is the offset if there's no SNAP header.
643 * If the link layer is Ethernet:
645 * "off_nl" is the offset if the packet is an Ethernet II packet
646 * (we assume no 802.3+802.2+SNAP);
648 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
649 * with an 802.2 header following it.
652 static u_int off_nl_nosnap
;
663 * Assume it's not raw ATM with a pseudo-header, for now.
679 off_nl
= 6; /* XXX in reality, variable! */
680 off_nl_nosnap
= 6; /* no 802.2 LLC */
685 off_nl
= 14; /* Ethernet II */
686 off_nl_nosnap
= 17; /* 802.3+802.2 */
691 * SLIP doesn't have a link level type. The 16 byte
692 * header is hacked into our SLIP driver.
696 off_nl_nosnap
= 16; /* no 802.2 LLC */
700 /* XXX this may be the same as the DLT_PPP_BSDOS case */
704 off_nl_nosnap
= 24; /* no 802.2 LLC */
711 off_nl_nosnap
= 4; /* no 802.2 LLC */
715 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
716 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
719 off_nl_nosnap
= 4; /* no 802.2 LLC */
724 * This does no include the Ethernet header, and
725 * only covers session state.
729 off_nl_nosnap
= 8; /* no 802.2 LLC */
735 off_nl_nosnap
= 24; /* no 802.2 LLC */
740 * FDDI doesn't really have a link-level type field.
741 * We set "off_linktype" to the offset of the LLC header.
743 * To check for Ethernet types, we assume that SSAP = SNAP
744 * is being used and pick out the encapsulated Ethernet type.
745 * XXX - should we generate code to check for SNAP?
749 off_linktype
+= pcap_fddipad
;
751 off_nl
= 21; /* FDDI+802.2+SNAP */
752 off_nl_nosnap
= 16; /* FDDI+802.2 */
754 off_nl
+= pcap_fddipad
;
755 off_nl_nosnap
+= pcap_fddipad
;
761 * Token Ring doesn't really have a link-level type field.
762 * We set "off_linktype" to the offset of the LLC header.
764 * To check for Ethernet types, we assume that SSAP = SNAP
765 * is being used and pick out the encapsulated Ethernet type.
766 * XXX - should we generate code to check for SNAP?
768 * XXX - the header is actually variable-length.
769 * Some various Linux patched versions gave 38
770 * as "off_linktype" and 40 as "off_nl"; however,
771 * if a token ring packet has *no* routing
772 * information, i.e. is not source-routed, the correct
773 * values are 20 and 22, as they are in the vanilla code.
775 * A packet is source-routed iff the uppermost bit
776 * of the first byte of the source address, at an
777 * offset of 8, has the uppermost bit set. If the
778 * packet is source-routed, the total number of bytes
779 * of routing information is 2 plus bits 0x1F00 of
780 * the 16-bit value at an offset of 14 (shifted right
781 * 8 - figure out which byte that is).
784 off_nl
= 22; /* Token Ring+802.2+SNAP */
785 off_nl_nosnap
= 17; /* Token Ring+802.2 */
790 * 802.11 doesn't really have a link-level type field.
791 * We set "off_linktype" to the offset of the LLC header.
793 * To check for Ethernet types, we assume that SSAP = SNAP
794 * is being used and pick out the encapsulated Ethernet type.
795 * XXX - should we generate code to check for SNAP?
797 * XXX - the header is actually variable-length. We
798 * assume a 24-byte link-layer header, as appears in
799 * data frames in networks with no bridges.
802 off_nl
= 32; /* 802.11+802.2+SNAP */
803 off_nl_nosnap
= 27; /* 802.11+802.2 */
806 case DLT_PRISM_HEADER
:
808 * Same as 802.11, but with an additional header before
809 * the 802.11 header, containing a bunch of additional
810 * information including radio-level information.
812 * The header is 144 bytes long.
814 * XXX - same variable-length header problem; at least
815 * the Prism header is fixed-length.
817 off_linktype
= 144+24;
818 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
819 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
822 case DLT_ATM_RFC1483
:
823 case DLT_ATM_CLIP
: /* Linux ATM defines this */
825 * assume routed, non-ISO PDUs
826 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
829 off_nl
= 8; /* 802.2+SNAP */
830 off_nl_nosnap
= 3; /* 802.2 */
835 * Full Frontal ATM; you get AALn PDUs with an ATM
839 off_linktype
= SUNATM_PKT_BEGIN_POS
;
840 off_vpi
= SUNATM_VPI_POS
;
841 off_vci
= SUNATM_VCI_POS
;
842 off_proto
= PROTO_POS
;
843 off_msg_type
= SUNATM_PKT_BEGIN_POS
+MSG_TYPE_POS
;
844 off_nl
= SUNATM_PKT_BEGIN_POS
+8; /* 802.2+SNAP */
845 off_nl_nosnap
= SUNATM_PKT_BEGIN_POS
+3; /* 802.2 */
851 off_nl_nosnap
= 0; /* no 802.2 LLC */
854 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
857 off_nl_nosnap
= 16; /* no 802.2 LLC */
862 * LocalTalk does have a 1-byte type field in the LLAP header,
863 * but really it just indicates whether there is a "short" or
864 * "long" DDP packet following.
868 off_nl_nosnap
= 0; /* no 802.2 LLC */
873 * XXX - we should set this to handle SNAP-encapsulated
874 * frames (NLPID of 0x80).
878 off_nl_nosnap
= 0; /* no 802.2 LLC */
881 bpf_error("unknown data link type %d", linktype
);
885 static struct block
*
892 s
= new_stmt(BPF_LD
|BPF_IMM
);
894 b
= new_block(JMP(BPF_JEQ
));
900 static inline struct block
*
903 return gen_uncond(1);
906 static inline struct block
*
909 return gen_uncond(0);
913 * Byte-swap a 32-bit number.
914 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
915 * big-endian platforms.)
917 #define SWAPLONG(y) \
918 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
920 static struct block
*
924 struct block
*b0
, *b1
, *b2
;
933 * OSI protocols always use 802.2 encapsulation.
934 * XXX - should we check both the DSAP and the
935 * SSAP, like this, or should we check just the
938 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
940 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
941 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
947 * NetBEUI always uses 802.2 encapsulation.
948 * XXX - should we check both the DSAP and the
949 * SSAP, like this, or should we check just the
952 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
954 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
955 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
963 * Ethernet_II frames, which are Ethernet
964 * frames with a frame type of ETHERTYPE_IPX;
966 * Ethernet_802.3 frames, which are 802.3
967 * frames (i.e., the type/length field is
968 * a length field, <= ETHERMTU, rather than
969 * a type field) with the first two bytes
970 * after the Ethernet/802.3 header being
973 * Ethernet_802.2 frames, which are 802.3
974 * frames with an 802.2 LLC header and
975 * with the IPX LSAP as the DSAP in the LLC
978 * Ethernet_SNAP frames, which are 802.3
979 * frames with an LLC header and a SNAP
980 * header and with an OUI of 0x000000
981 * (encapsulated Ethernet) and a protocol
982 * ID of ETHERTYPE_IPX in the SNAP header.
984 * XXX - should we generate the same code both
985 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
989 * This generates code to check both for the
990 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
992 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
993 (bpf_int32
)LLCSAP_IPX
);
994 b1
= gen_cmp(off_linktype
+ 2, BPF_H
,
999 * Now we add code to check for SNAP frames with
1000 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1002 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1006 * Now we generate code to check for 802.3
1007 * frames in general.
1009 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1013 * Now add the check for 802.3 frames before the
1014 * check for Ethernet_802.2 and Ethernet_802.3,
1015 * as those checks should only be done on 802.3
1016 * frames, not on Ethernet frames.
1021 * Now add the check for Ethernet_II frames, and
1022 * do that before checking for the other frame
1025 b0
= gen_cmp(off_linktype
, BPF_H
,
1026 (bpf_int32
)ETHERTYPE_IPX
);
1030 case ETHERTYPE_ATALK
:
1031 case ETHERTYPE_AARP
:
1033 * EtherTalk (AppleTalk protocols on Ethernet link
1034 * layer) may use 802.2 encapsulation.
1038 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1039 * we check for an Ethernet type field less than
1040 * 1500, which means it's an 802.3 length field.
1042 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1046 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1047 * SNAP packets with an organization code of
1048 * 0x080007 (Apple, for Appletalk) and a protocol
1049 * type of ETHERTYPE_ATALK (Appletalk).
1051 * 802.2-encapsulated ETHERTYPE_AARP packets are
1052 * SNAP packets with an organization code of
1053 * 0x000000 (encapsulated Ethernet) and a protocol
1054 * type of ETHERTYPE_AARP (Appletalk ARP).
1056 if (proto
== ETHERTYPE_ATALK
)
1057 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1058 else /* proto == ETHERTYPE_AARP */
1059 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1063 * Check for Ethernet encapsulation (Ethertalk
1064 * phase 1?); we just check for the Ethernet
1067 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1073 if (proto
<= ETHERMTU
) {
1075 * This is an LLC SAP value, so the frames
1076 * that match would be 802.2 frames.
1077 * Check that the frame is an 802.2 frame
1078 * (i.e., that the length/type field is
1079 * a length field, <= ETHERMTU) and
1080 * then check the DSAP.
1082 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1084 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1090 * This is an Ethernet type, so compare
1091 * the length/type field with it (if
1092 * the frame is an 802.2 frame, the length
1093 * field will be <= ETHERMTU, and, as
1094 * "proto" is > ETHERMTU, this test
1095 * will fail and the frame won't match,
1096 * which is what we want).
1098 return gen_cmp(off_linktype
, BPF_H
,
1104 case DLT_IEEE802_11
:
1105 case DLT_PRISM_HEADER
:
1108 case DLT_ATM_RFC1483
:
1110 return gen_llc(proto
);
1115 * Check for LLC encapsulation and then check the protocol.
1116 * XXX - also check for LANE and then check for an Ethernet
1119 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1120 b1
= gen_llc(proto
);
1129 * OSI protocols always use 802.2 encapsulation.
1130 * XXX - should we check both the DSAP and the
1131 * LSAP, like this, or should we check just the
1134 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1135 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1136 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1140 case LLCSAP_NETBEUI
:
1142 * NetBEUI always uses 802.2 encapsulation.
1143 * XXX - should we check both the DSAP and the
1144 * LSAP, like this, or should we check just the
1147 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1148 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1149 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1155 * Ethernet_II frames, which are Ethernet
1156 * frames with a frame type of ETHERTYPE_IPX;
1158 * Ethernet_802.3 frames, which have a frame
1159 * type of LINUX_SLL_P_802_3;
1161 * Ethernet_802.2 frames, which are 802.3
1162 * frames with an 802.2 LLC header (i.e, have
1163 * a frame type of LINUX_SLL_P_802_2) and
1164 * with the IPX LSAP as the DSAP in the LLC
1167 * Ethernet_SNAP frames, which are 802.3
1168 * frames with an LLC header and a SNAP
1169 * header and with an OUI of 0x000000
1170 * (encapsulated Ethernet) and a protocol
1171 * ID of ETHERTYPE_IPX in the SNAP header.
1173 * First, do the checks on LINUX_SLL_P_802_2
1174 * frames; generate the check for either
1175 * Ethernet_802.2 or Ethernet_SNAP frames, and
1176 * then put a check for LINUX_SLL_P_802_2 frames
1179 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1180 (bpf_int32
)LLCSAP_IPX
);
1181 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1184 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1188 * Now check for 802.3 frames and OR that with
1189 * the previous test.
1191 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1195 * Now add the check for Ethernet_II frames, and
1196 * do that before checking for the other frame
1199 b0
= gen_cmp(off_linktype
, BPF_H
,
1200 (bpf_int32
)ETHERTYPE_IPX
);
1204 case ETHERTYPE_ATALK
:
1205 case ETHERTYPE_AARP
:
1207 * EtherTalk (AppleTalk protocols on Ethernet link
1208 * layer) may use 802.2 encapsulation.
1212 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1213 * we check for the 802.2 protocol type in the
1214 * "Ethernet type" field.
1216 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1219 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1220 * SNAP packets with an organization code of
1221 * 0x080007 (Apple, for Appletalk) and a protocol
1222 * type of ETHERTYPE_ATALK (Appletalk).
1224 * 802.2-encapsulated ETHERTYPE_AARP packets are
1225 * SNAP packets with an organization code of
1226 * 0x000000 (encapsulated Ethernet) and a protocol
1227 * type of ETHERTYPE_AARP (Appletalk ARP).
1229 if (proto
== ETHERTYPE_ATALK
)
1230 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1232 else /* proto == ETHERTYPE_AARP */
1233 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1238 * Check for Ethernet encapsulation (Ethertalk
1239 * phase 1?); we just check for the Ethernet
1242 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1248 if (proto
<= ETHERMTU
) {
1250 * This is an LLC SAP value, so the frames
1251 * that match would be 802.2 frames.
1252 * Check for the 802.2 protocol type
1253 * in the "Ethernet type" field, and
1254 * then check the DSAP.
1256 b0
= gen_cmp(off_linktype
, BPF_H
,
1258 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1264 * This is an Ethernet type, so compare
1265 * the length/type field with it (if
1266 * the frame is an 802.2 frame, the length
1267 * field will be <= ETHERMTU, and, as
1268 * "proto" is > ETHERMTU, this test
1269 * will fail and the frame won't match,
1270 * which is what we want).
1272 return gen_cmp(off_linktype
, BPF_H
,
1279 case DLT_SLIP_BSDOS
:
1282 * These types don't provide any type field; packets
1285 * XXX - for IPv4, check for a version number of 4, and,
1286 * for IPv6, check for a version number of 6?
1292 case ETHERTYPE_IPV6
:
1294 return gen_true(); /* always true */
1297 return gen_false(); /* always false */
1302 case DLT_PPP_SERIAL
:
1305 * We use Ethernet protocol types inside libpcap;
1306 * map them to the corresponding PPP protocol types.
1311 proto
= PPP_IP
; /* XXX was 0x21 */
1315 case ETHERTYPE_IPV6
:
1324 case ETHERTYPE_ATALK
:
1338 * I'm assuming the "Bridging PDU"s that go
1339 * over PPP are Spanning Tree Protocol
1353 * We use Ethernet protocol types inside libpcap;
1354 * map them to the corresponding PPP protocol types.
1359 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1360 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1362 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1367 case ETHERTYPE_IPV6
:
1377 case ETHERTYPE_ATALK
:
1391 * I'm assuming the "Bridging PDU"s that go
1392 * over PPP are Spanning Tree Protocol
1407 * For DLT_NULL, the link-layer header is a 32-bit
1408 * word containing an AF_ value in *host* byte order.
1410 * In addition, if we're reading a saved capture file,
1411 * the host byte order in the capture may not be the
1412 * same as the host byte order on this machine.
1414 * For DLT_LOOP, the link-layer header is a 32-bit
1415 * word containing an AF_ value in *network* byte order.
1417 * XXX - AF_ values may, unfortunately, be platform-
1418 * dependent; for example, FreeBSD's AF_INET6 is 24
1419 * whilst NetBSD's and OpenBSD's is 26.
1421 * This means that, when reading a capture file, just
1422 * checking for our AF_INET6 value won't work if the
1423 * capture file came from another OS.
1432 case ETHERTYPE_IPV6
:
1439 * Not a type on which we support filtering.
1440 * XXX - support those that have AF_ values
1441 * #defined on this platform, at least?
1446 if (linktype
== DLT_NULL
) {
1448 * The AF_ value is in host byte order, but
1449 * the BPF interpreter will convert it to
1450 * network byte order.
1452 * If this is a save file, and it's from a
1453 * machine with the opposite byte order to
1454 * ours, we byte-swap the AF_ value.
1456 * Then we run it through "htonl()", and
1457 * generate code to compare against the result.
1459 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1460 bpf_pcap
->sf
.swapped
)
1461 proto
= SWAPLONG(proto
);
1462 proto
= htonl(proto
);
1464 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1468 * XXX should we check for first fragment if the protocol
1475 case ETHERTYPE_IPV6
:
1476 return(gen_cmp(2, BPF_B
,
1477 (bpf_int32
)htonl(ARCTYPE_INET6
)));
1480 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_IP
));
1481 b1
= gen_cmp(2, BPF_B
,
1482 (bpf_int32
)htonl(ARCTYPE_IP_OLD
));
1486 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_ARP
));
1487 b1
= gen_cmp(2, BPF_B
,
1488 (bpf_int32
)htonl(ARCTYPE_ARP_OLD
));
1491 case ETHERTYPE_REVARP
:
1492 return(gen_cmp(2, BPF_B
,
1493 (bpf_int32
)htonl(ARCTYPE_REVARP
)));
1494 case ETHERTYPE_ATALK
:
1495 return(gen_cmp(2, BPF_B
,
1496 (bpf_int32
)htonl(ARCTYPE_ATALK
)));
1502 case ETHERTYPE_ATALK
:
1511 * XXX - assumes a 2-byte Frame Relay header with
1512 * DLCI and flags. What if the address is longer?
1518 * Check for the special NLPID for IP.
1520 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1523 case ETHERTYPE_IPV6
:
1525 * Check for the special NLPID for IPv6.
1527 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1532 * Check for several OSI protocols.
1534 * Frame Relay packets typically have an OSI
1535 * NLPID at the beginning; we check for each
1538 * What we check for is the NLPID and a frame
1539 * control field of UI, i.e. 0x03 followed
1542 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1543 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1544 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1556 * All the types that have no encapsulation should either be
1557 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1558 * all packets are IP packets, or should be handled in some
1559 * special case, if none of them are (if some are and some
1560 * aren't, the lack of encapsulation is a problem, as we'd
1561 * have to find some other way of determining the packet type).
1563 * Therefore, if "off_linktype" is -1, there's an error.
1565 if (off_linktype
== -1)
1569 * Any type not handled above should always have an Ethernet
1570 * type at an offset of "off_linktype". (PPP is partially
1571 * handled above - the protocol type is mapped from the
1572 * Ethernet and LLC types we use internally to the corresponding
1573 * PPP type - but the PPP type is always specified by a value
1574 * at "off_linktype", so we don't have to do the code generation
1577 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1581 * Check for an LLC SNAP packet with a given organization code and
1582 * protocol type; we check the entire contents of the 802.2 LLC and
1583 * snap headers, checking for DSAP and SSAP of SNAP and a control
1584 * field of 0x03 in the LLC header, and for the specified organization
1585 * code and protocol type in the SNAP header.
1587 static struct block
*
1588 gen_snap(orgcode
, ptype
, offset
)
1589 bpf_u_int32 orgcode
;
1593 u_char snapblock
[8];
1595 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1596 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1597 snapblock
[2] = 0x03; /* control = UI */
1598 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1599 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1600 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1601 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1602 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1603 return gen_bcmp(offset
, 8, snapblock
);
1607 * Check for a given protocol value assuming an 802.2 LLC header.
1609 static struct block
*
1614 * XXX - handle token-ring variable-length header.
1619 return gen_cmp(off_linktype
, BPF_H
, (long)
1620 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1622 case LLCSAP_NETBEUI
:
1623 return gen_cmp(off_linktype
, BPF_H
, (long)
1624 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1628 * XXX - are there ever SNAP frames for IPX on
1629 * non-Ethernet 802.x networks?
1631 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1633 case ETHERTYPE_ATALK
:
1635 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1636 * SNAP packets with an organization code of
1637 * 0x080007 (Apple, for Appletalk) and a protocol
1638 * type of ETHERTYPE_ATALK (Appletalk).
1640 * XXX - check for an organization code of
1641 * encapsulated Ethernet as well?
1643 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1647 * XXX - we don't have to check for IPX 802.3
1648 * here, but should we check for the IPX Ethertype?
1650 if (proto
<= ETHERMTU
) {
1652 * This is an LLC SAP value, so check
1655 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1658 * This is an Ethernet type; we assume that it's
1659 * unlikely that it'll appear in the right place
1660 * at random, and therefore check only the
1661 * location that would hold the Ethernet type
1662 * in a SNAP frame with an organization code of
1663 * 0x000000 (encapsulated Ethernet).
1665 * XXX - if we were to check for the SNAP DSAP and
1666 * LSAP, as per XXX, and were also to check for an
1667 * organization code of 0x000000 (encapsulated
1668 * Ethernet), we'd do
1670 * return gen_snap(0x000000, proto,
1673 * here; for now, we don't, as per the above.
1674 * I don't know whether it's worth the extra CPU
1675 * time to do the right check or not.
1677 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
1682 static struct block
*
1683 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1687 u_int src_off
, dst_off
;
1689 struct block
*b0
, *b1
;
1703 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1704 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1710 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1711 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1718 b0
= gen_linktype(proto
);
1719 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1725 static struct block
*
1726 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1727 struct in6_addr
*addr
;
1728 struct in6_addr
*mask
;
1730 u_int src_off
, dst_off
;
1732 struct block
*b0
, *b1
;
1747 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1748 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1754 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1755 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1762 /* this order is important */
1763 a
= (u_int32_t
*)addr
;
1764 m
= (u_int32_t
*)mask
;
1765 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1766 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1768 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1770 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1772 b0
= gen_linktype(proto
);
1778 static struct block
*
1779 gen_ehostop(eaddr
, dir
)
1780 register const u_char
*eaddr
;
1783 register struct block
*b0
, *b1
;
1787 return gen_bcmp(6, 6, eaddr
);
1790 return gen_bcmp(0, 6, eaddr
);
1793 b0
= gen_ehostop(eaddr
, Q_SRC
);
1794 b1
= gen_ehostop(eaddr
, Q_DST
);
1800 b0
= gen_ehostop(eaddr
, Q_SRC
);
1801 b1
= gen_ehostop(eaddr
, Q_DST
);
1810 * Like gen_ehostop, but for DLT_FDDI
1812 static struct block
*
1813 gen_fhostop(eaddr
, dir
)
1814 register const u_char
*eaddr
;
1817 struct block
*b0
, *b1
;
1822 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1824 return gen_bcmp(6 + 1, 6, eaddr
);
1829 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1831 return gen_bcmp(0 + 1, 6, eaddr
);
1835 b0
= gen_fhostop(eaddr
, Q_SRC
);
1836 b1
= gen_fhostop(eaddr
, Q_DST
);
1842 b0
= gen_fhostop(eaddr
, Q_SRC
);
1843 b1
= gen_fhostop(eaddr
, Q_DST
);
1852 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1854 static struct block
*
1855 gen_thostop(eaddr
, dir
)
1856 register const u_char
*eaddr
;
1859 register struct block
*b0
, *b1
;
1863 return gen_bcmp(8, 6, eaddr
);
1866 return gen_bcmp(2, 6, eaddr
);
1869 b0
= gen_thostop(eaddr
, Q_SRC
);
1870 b1
= gen_thostop(eaddr
, Q_DST
);
1876 b0
= gen_thostop(eaddr
, Q_SRC
);
1877 b1
= gen_thostop(eaddr
, Q_DST
);
1886 * This is quite tricky because there may be pad bytes in front of the
1887 * DECNET header, and then there are two possible data packet formats that
1888 * carry both src and dst addresses, plus 5 packet types in a format that
1889 * carries only the src node, plus 2 types that use a different format and
1890 * also carry just the src node.
1894 * Instead of doing those all right, we just look for data packets with
1895 * 0 or 1 bytes of padding. If you want to look at other packets, that
1896 * will require a lot more hacking.
1898 * To add support for filtering on DECNET "areas" (network numbers)
1899 * one would want to add a "mask" argument to this routine. That would
1900 * make the filter even more inefficient, although one could be clever
1901 * and not generate masking instructions if the mask is 0xFFFF.
1903 static struct block
*
1904 gen_dnhostop(addr
, dir
, base_off
)
1909 struct block
*b0
, *b1
, *b2
, *tmp
;
1910 u_int offset_lh
; /* offset if long header is received */
1911 u_int offset_sh
; /* offset if short header is received */
1916 offset_sh
= 1; /* follows flags */
1917 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1921 offset_sh
= 3; /* follows flags, dstnode */
1922 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1926 /* Inefficient because we do our Calvinball dance twice */
1927 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1928 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1934 /* Inefficient because we do our Calvinball dance twice */
1935 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1936 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1941 bpf_error("ISO host filtering not implemented");
1946 b0
= gen_linktype(ETHERTYPE_DN
);
1947 /* Check for pad = 1, long header case */
1948 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1949 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1950 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1951 BPF_H
, (bpf_int32
)ntohs(addr
));
1953 /* Check for pad = 0, long header case */
1954 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1955 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1958 /* Check for pad = 1, short header case */
1959 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1960 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1961 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1962 BPF_H
, (bpf_int32
)ntohs(addr
));
1965 /* Check for pad = 0, short header case */
1966 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1967 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1971 /* Combine with test for linktype */
1976 static struct block
*
1977 gen_host(addr
, mask
, proto
, dir
)
1983 struct block
*b0
, *b1
;
1988 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1989 if (off_linktype
!= -1) {
1990 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1992 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1998 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1999 off_nl
+ 12, off_nl
+ 16);
2002 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2003 off_nl
+ 14, off_nl
+ 24);
2006 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2007 off_nl
+ 14, off_nl
+ 24);
2010 bpf_error("'tcp' modifier applied to host");
2013 bpf_error("'sctp' modifier applied to host");
2016 bpf_error("'udp' modifier applied to host");
2019 bpf_error("'icmp' modifier applied to host");
2022 bpf_error("'igmp' modifier applied to host");
2025 bpf_error("'igrp' modifier applied to host");
2028 bpf_error("'pim' modifier applied to host");
2031 bpf_error("'vrrp' modifier applied to host");
2034 bpf_error("ATALK host filtering not implemented");
2037 bpf_error("AARP host filtering not implemented");
2040 return gen_dnhostop(addr
, dir
, off_nl
);
2043 bpf_error("SCA host filtering not implemented");
2046 bpf_error("LAT host filtering not implemented");
2049 bpf_error("MOPDL host filtering not implemented");
2052 bpf_error("MOPRC host filtering not implemented");
2056 bpf_error("'ip6' modifier applied to ip host");
2059 bpf_error("'icmp6' modifier applied to host");
2063 bpf_error("'ah' modifier applied to host");
2066 bpf_error("'esp' modifier applied to host");
2069 bpf_error("ISO host filtering not implemented");
2072 bpf_error("'esis' modifier applied to host");
2075 bpf_error("'isis' modifier applied to host");
2078 bpf_error("'clnp' modifier applied to host");
2081 bpf_error("'stp' modifier applied to host");
2084 bpf_error("IPX host filtering not implemented");
2087 bpf_error("'netbeui' modifier applied to host");
2096 static struct block
*
2097 gen_host6(addr
, mask
, proto
, dir
)
2098 struct in6_addr
*addr
;
2099 struct in6_addr
*mask
;
2106 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2109 bpf_error("'ip' modifier applied to ip6 host");
2112 bpf_error("'rarp' modifier applied to ip6 host");
2115 bpf_error("'arp' modifier applied to ip6 host");
2118 bpf_error("'sctp' modifier applied to host");
2121 bpf_error("'tcp' modifier applied to host");
2124 bpf_error("'udp' modifier applied to host");
2127 bpf_error("'icmp' modifier applied to host");
2130 bpf_error("'igmp' modifier applied to host");
2133 bpf_error("'igrp' modifier applied to host");
2136 bpf_error("'pim' modifier applied to host");
2139 bpf_error("'vrrp' modifier applied to host");
2142 bpf_error("ATALK host filtering not implemented");
2145 bpf_error("AARP host filtering not implemented");
2148 bpf_error("'decnet' modifier applied to ip6 host");
2151 bpf_error("SCA host filtering not implemented");
2154 bpf_error("LAT host filtering not implemented");
2157 bpf_error("MOPDL host filtering not implemented");
2160 bpf_error("MOPRC host filtering not implemented");
2163 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2164 off_nl
+ 8, off_nl
+ 24);
2167 bpf_error("'icmp6' modifier applied to host");
2170 bpf_error("'ah' modifier applied to host");
2173 bpf_error("'esp' modifier applied to host");
2176 bpf_error("ISO host filtering not implemented");
2179 bpf_error("'esis' modifier applied to host");
2182 bpf_error("'isis' modifier applied to host");
2185 bpf_error("'clnp' modifier applied to host");
2188 bpf_error("'stp' modifier applied to host");
2191 bpf_error("IPX host filtering not implemented");
2194 bpf_error("'netbeui' modifier applied to host");
2204 static struct block
*
2205 gen_gateway(eaddr
, alist
, proto
, dir
)
2206 const u_char
*eaddr
;
2207 bpf_u_int32
**alist
;
2211 struct block
*b0
, *b1
, *tmp
;
2214 bpf_error("direction applied to 'gateway'");
2221 if (linktype
== DLT_EN10MB
)
2222 b0
= gen_ehostop(eaddr
, Q_OR
);
2223 else if (linktype
== DLT_FDDI
)
2224 b0
= gen_fhostop(eaddr
, Q_OR
);
2225 else if (linktype
== DLT_IEEE802
)
2226 b0
= gen_thostop(eaddr
, Q_OR
);
2229 "'gateway' supported only on ethernet, FDDI or token ring");
2231 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2233 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2241 bpf_error("illegal modifier of 'gateway'");
2247 gen_proto_abbrev(proto
)
2258 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2260 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2266 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2268 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2274 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2276 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2282 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2285 #ifndef IPPROTO_IGMP
2286 #define IPPROTO_IGMP 2
2290 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2293 #ifndef IPPROTO_IGRP
2294 #define IPPROTO_IGRP 9
2297 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2301 #define IPPROTO_PIM 103
2305 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2307 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2312 #ifndef IPPROTO_VRRP
2313 #define IPPROTO_VRRP 112
2317 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2321 b1
= gen_linktype(ETHERTYPE_IP
);
2325 b1
= gen_linktype(ETHERTYPE_ARP
);
2329 b1
= gen_linktype(ETHERTYPE_REVARP
);
2333 bpf_error("link layer applied in wrong context");
2336 b1
= gen_linktype(ETHERTYPE_ATALK
);
2340 b1
= gen_linktype(ETHERTYPE_AARP
);
2344 b1
= gen_linktype(ETHERTYPE_DN
);
2348 b1
= gen_linktype(ETHERTYPE_SCA
);
2352 b1
= gen_linktype(ETHERTYPE_LAT
);
2356 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2360 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2365 b1
= gen_linktype(ETHERTYPE_IPV6
);
2368 #ifndef IPPROTO_ICMPV6
2369 #define IPPROTO_ICMPV6 58
2372 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2377 #define IPPROTO_AH 51
2380 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2382 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2388 #define IPPROTO_ESP 50
2391 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2393 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2399 b1
= gen_linktype(LLCSAP_ISONS
);
2403 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2407 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2411 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2415 b1
= gen_linktype(LLCSAP_8021D
);
2419 b1
= gen_linktype(LLCSAP_IPX
);
2423 b1
= gen_linktype(LLCSAP_NETBEUI
);
2432 static struct block
*
2439 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2440 s
->s
.k
= off_nl
+ 6;
2441 b
= new_block(JMP(BPF_JSET
));
2449 static struct block
*
2450 gen_portatom(off
, v
)
2457 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2460 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2461 s
->next
->s
.k
= off_nl
+ off
;
2463 b
= new_block(JMP(BPF_JEQ
));
2471 static struct block
*
2472 gen_portatom6(off
, v
)
2476 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2481 gen_portop(port
, proto
, dir
)
2482 int port
, proto
, dir
;
2484 struct block
*b0
, *b1
, *tmp
;
2486 /* ip proto 'proto' */
2487 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2493 b1
= gen_portatom(0, (bpf_int32
)port
);
2497 b1
= gen_portatom(2, (bpf_int32
)port
);
2502 tmp
= gen_portatom(0, (bpf_int32
)port
);
2503 b1
= gen_portatom(2, (bpf_int32
)port
);
2508 tmp
= gen_portatom(0, (bpf_int32
)port
);
2509 b1
= gen_portatom(2, (bpf_int32
)port
);
2521 static struct block
*
2522 gen_port(port
, ip_proto
, dir
)
2527 struct block
*b0
, *b1
, *tmp
;
2529 /* ether proto ip */
2530 b0
= gen_linktype(ETHERTYPE_IP
);
2536 b1
= gen_portop(port
, ip_proto
, dir
);
2540 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2541 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2543 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
2556 gen_portop6(port
, proto
, dir
)
2557 int port
, proto
, dir
;
2559 struct block
*b0
, *b1
, *tmp
;
2561 /* ip proto 'proto' */
2562 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2566 b1
= gen_portatom6(0, (bpf_int32
)port
);
2570 b1
= gen_portatom6(2, (bpf_int32
)port
);
2575 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2576 b1
= gen_portatom6(2, (bpf_int32
)port
);
2581 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2582 b1
= gen_portatom6(2, (bpf_int32
)port
);
2594 static struct block
*
2595 gen_port6(port
, ip_proto
, dir
)
2600 struct block
*b0
, *b1
, *tmp
;
2602 /* ether proto ip */
2603 b0
= gen_linktype(ETHERTYPE_IPV6
);
2609 b1
= gen_portop6(port
, ip_proto
, dir
);
2613 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2614 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2616 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
2629 lookup_proto(name
, proto
)
2630 register const char *name
;
2640 v
= pcap_nametoproto(name
);
2641 if (v
== PROTO_UNDEF
)
2642 bpf_error("unknown ip proto '%s'", name
);
2646 /* XXX should look up h/w protocol type based on linktype */
2647 v
= pcap_nametoeproto(name
);
2648 if (v
== PROTO_UNDEF
)
2649 bpf_error("unknown ether proto '%s'", name
);
2653 if (strcmp(name
, "esis") == 0)
2655 else if (strcmp(name
, "isis") == 0)
2657 else if (strcmp(name
, "clnp") == 0)
2660 bpf_error("unknown osi proto '%s'", name
);
2680 static struct block
*
2681 gen_protochain(v
, proto
, dir
)
2686 #ifdef NO_PROTOCHAIN
2687 return gen_proto(v
, proto
, dir
);
2689 struct block
*b0
, *b
;
2690 struct slist
*s
[100];
2691 int fix2
, fix3
, fix4
, fix5
;
2692 int ahcheck
, again
, end
;
2694 int reg2
= alloc_reg();
2696 memset(s
, 0, sizeof(s
));
2697 fix2
= fix3
= fix4
= fix5
= 0;
2704 b0
= gen_protochain(v
, Q_IP
, dir
);
2705 b
= gen_protochain(v
, Q_IPV6
, dir
);
2709 bpf_error("bad protocol applied for 'protochain'");
2713 no_optimize
= 1; /*this code is not compatible with optimzer yet */
2716 * s[0] is a dummy entry to protect other BPF insn from damaged
2717 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
2718 * hard to find interdependency made by jump table fixup.
2721 s
[i
] = new_stmt(0); /*dummy*/
2726 b0
= gen_linktype(ETHERTYPE_IP
);
2729 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2730 s
[i
]->s
.k
= off_nl
+ 9;
2732 /* X = ip->ip_hl << 2 */
2733 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2739 b0
= gen_linktype(ETHERTYPE_IPV6
);
2741 /* A = ip6->ip_nxt */
2742 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2743 s
[i
]->s
.k
= off_nl
+ 6;
2745 /* X = sizeof(struct ip6_hdr) */
2746 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
2752 bpf_error("unsupported proto to gen_protochain");
2756 /* again: if (A == v) goto end; else fall through; */
2758 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2760 s
[i
]->s
.jt
= NULL
; /*later*/
2761 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2765 #ifndef IPPROTO_NONE
2766 #define IPPROTO_NONE 59
2768 /* if (A == IPPROTO_NONE) goto end */
2769 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2770 s
[i
]->s
.jt
= NULL
; /*later*/
2771 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2772 s
[i
]->s
.k
= IPPROTO_NONE
;
2773 s
[fix5
]->s
.jf
= s
[i
];
2778 if (proto
== Q_IPV6
) {
2779 int v6start
, v6end
, v6advance
, j
;
2782 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
2783 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2784 s
[i
]->s
.jt
= NULL
; /*later*/
2785 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2786 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
2787 s
[fix2
]->s
.jf
= s
[i
];
2789 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
2790 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2791 s
[i
]->s
.jt
= NULL
; /*later*/
2792 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2793 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
2795 /* if (A == IPPROTO_ROUTING) goto v6advance */
2796 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2797 s
[i
]->s
.jt
= NULL
; /*later*/
2798 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2799 s
[i
]->s
.k
= IPPROTO_ROUTING
;
2801 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
2802 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2803 s
[i
]->s
.jt
= NULL
; /*later*/
2804 s
[i
]->s
.jf
= NULL
; /*later*/
2805 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
2816 * X = X + (P[X + 1] + 1) * 8;
2819 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2821 /* A = P[X + packet head] */
2822 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2826 s
[i
] = new_stmt(BPF_ST
);
2830 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2833 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2837 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2839 /* A = P[X + packet head]; */
2840 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2844 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2848 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2852 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2855 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2859 /* goto again; (must use BPF_JA for backward jump) */
2860 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2861 s
[i
]->s
.k
= again
- i
- 1;
2862 s
[i
- 1]->s
.jf
= s
[i
];
2866 for (j
= v6start
; j
<= v6end
; j
++)
2867 s
[j
]->s
.jt
= s
[v6advance
];
2872 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2874 s
[fix2
]->s
.jf
= s
[i
];
2880 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2881 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2882 s
[i
]->s
.jt
= NULL
; /*later*/
2883 s
[i
]->s
.jf
= NULL
; /*later*/
2884 s
[i
]->s
.k
= IPPROTO_AH
;
2886 s
[fix3
]->s
.jf
= s
[ahcheck
];
2893 * X = X + (P[X + 1] + 2) * 4;
2896 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2898 /* A = P[X + packet head]; */
2899 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2903 s
[i
] = new_stmt(BPF_ST
);
2907 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2910 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2914 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2916 /* A = P[X + packet head] */
2917 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2921 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2925 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2929 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2932 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2936 /* goto again; (must use BPF_JA for backward jump) */
2937 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2938 s
[i
]->s
.k
= again
- i
- 1;
2943 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2945 s
[fix2
]->s
.jt
= s
[end
];
2946 s
[fix4
]->s
.jf
= s
[end
];
2947 s
[fix5
]->s
.jt
= s
[end
];
2954 for (i
= 0; i
< max
- 1; i
++)
2955 s
[i
]->next
= s
[i
+ 1];
2956 s
[max
- 1]->next
= NULL
;
2961 b
= new_block(JMP(BPF_JEQ
));
2962 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2972 static struct block
*
2973 gen_proto(v
, proto
, dir
)
2978 struct block
*b0
, *b1
;
2980 if (dir
!= Q_DEFAULT
)
2981 bpf_error("direction applied to 'proto'");
2986 b0
= gen_proto(v
, Q_IP
, dir
);
2987 b1
= gen_proto(v
, Q_IPV6
, dir
);
2994 b0
= gen_linktype(ETHERTYPE_IP
);
2996 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2998 b1
= gen_protochain(v
, Q_IP
);
3008 * Frame Relay packets typically have an OSI
3009 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3010 * generates code to check for all the OSI
3011 * NLPIDs, so calling it and then adding a check
3012 * for the particular NLPID for which we're
3013 * looking is bogus, as we can just check for
3016 * What we check for is the NLPID and a frame
3017 * control field value of UI, i.e. 0x03 followed
3020 * XXX - assumes a 2-byte Frame Relay header with
3021 * DLCI and flags. What if the address is longer?
3023 * XXX - what about SNAP-encapsulated frames?
3025 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3029 b0
= gen_linktype(LLCSAP_ISONS
);
3030 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3036 bpf_error("arp does not encapsulate another protocol");
3040 bpf_error("rarp does not encapsulate another protocol");
3044 bpf_error("atalk encapsulation is not specifiable");
3048 bpf_error("decnet encapsulation is not specifiable");
3052 bpf_error("sca does not encapsulate another protocol");
3056 bpf_error("lat does not encapsulate another protocol");
3060 bpf_error("moprc does not encapsulate another protocol");
3064 bpf_error("mopdl does not encapsulate another protocol");
3068 return gen_linktype(v
);
3071 bpf_error("'udp proto' is bogus");
3075 bpf_error("'tcp proto' is bogus");
3079 bpf_error("'sctp proto' is bogus");
3083 bpf_error("'icmp proto' is bogus");
3087 bpf_error("'igmp proto' is bogus");
3091 bpf_error("'igrp proto' is bogus");
3095 bpf_error("'pim proto' is bogus");
3099 bpf_error("'vrrp proto' is bogus");
3104 b0
= gen_linktype(ETHERTYPE_IPV6
);
3106 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3108 b1
= gen_protochain(v
, Q_IPV6
);
3114 bpf_error("'icmp6 proto' is bogus");
3118 bpf_error("'ah proto' is bogus");
3121 bpf_error("'ah proto' is bogus");
3124 bpf_error("'stp proto' is bogus");
3127 bpf_error("'ipx proto' is bogus");
3130 bpf_error("'netbeui proto' is bogus");
3141 register const char *name
;
3144 int proto
= q
.proto
;
3148 bpf_u_int32 mask
, addr
;
3150 bpf_u_int32
**alist
;
3153 struct sockaddr_in
*sin
;
3154 struct sockaddr_in6
*sin6
;
3155 struct addrinfo
*res
, *res0
;
3156 struct in6_addr mask128
;
3158 struct block
*b
, *tmp
;
3159 int port
, real_proto
;
3164 addr
= pcap_nametonetaddr(name
);
3166 bpf_error("unknown network '%s'", name
);
3167 /* Left justify network addr and calculate its network mask */
3169 while (addr
&& (addr
& 0xff000000) == 0) {
3173 return gen_host(addr
, mask
, proto
, dir
);
3177 if (proto
== Q_LINK
) {
3181 eaddr
= pcap_ether_hostton(name
);
3184 "unknown ether host '%s'", name
);
3185 b
= gen_ehostop(eaddr
, dir
);
3190 eaddr
= pcap_ether_hostton(name
);
3193 "unknown FDDI host '%s'", name
);
3194 b
= gen_fhostop(eaddr
, dir
);
3199 eaddr
= pcap_ether_hostton(name
);
3202 "unknown token ring host '%s'", name
);
3203 b
= gen_thostop(eaddr
, dir
);
3209 "only ethernet/FDDI/token ring supports link-level host name");
3212 } else if (proto
== Q_DECNET
) {
3213 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
3215 * I don't think DECNET hosts can be multihomed, so
3216 * there is no need to build up a list of addresses
3218 return (gen_host(dn_addr
, 0, proto
, dir
));
3221 alist
= pcap_nametoaddr(name
);
3222 if (alist
== NULL
|| *alist
== NULL
)
3223 bpf_error("unknown host '%s'", name
);
3225 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
3227 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
3229 tmp
= gen_host(**alist
++, 0xffffffff,
3236 memset(&mask128
, 0xff, sizeof(mask128
));
3237 res0
= res
= pcap_nametoaddrinfo(name
);
3239 bpf_error("unknown host '%s'", name
);
3241 tproto
= tproto6
= proto
;
3242 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
3246 for (res
= res0
; res
; res
= res
->ai_next
) {
3247 switch (res
->ai_family
) {
3249 if (tproto
== Q_IPV6
)
3252 sin
= (struct sockaddr_in
*)
3254 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
3255 0xffffffff, tproto
, dir
);
3258 if (tproto6
== Q_IP
)
3261 sin6
= (struct sockaddr_in6
*)
3263 tmp
= gen_host6(&sin6
->sin6_addr
,
3264 &mask128
, tproto6
, dir
);
3275 bpf_error("unknown host '%s'%s", name
,
3276 (proto
== Q_DEFAULT
)
3278 : " for specified address family");
3285 if (proto
!= Q_DEFAULT
&&
3286 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
3287 bpf_error("illegal qualifier of 'port'");
3288 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
3289 bpf_error("unknown port '%s'", name
);
3290 if (proto
== Q_UDP
) {
3291 if (real_proto
== IPPROTO_TCP
)
3292 bpf_error("port '%s' is tcp", name
);
3293 else if (real_proto
== IPPROTO_SCTP
)
3294 bpf_error("port '%s' is sctp", name
);
3296 /* override PROTO_UNDEF */
3297 real_proto
= IPPROTO_UDP
;
3299 if (proto
== Q_TCP
) {
3300 if (real_proto
== IPPROTO_UDP
)
3301 bpf_error("port '%s' is udp", name
);
3303 else if (real_proto
== IPPROTO_SCTP
)
3304 bpf_error("port '%s' is sctp", name
);
3306 /* override PROTO_UNDEF */
3307 real_proto
= IPPROTO_TCP
;
3309 if (proto
== Q_SCTP
) {
3310 if (real_proto
== IPPROTO_UDP
)
3311 bpf_error("port '%s' is udp", name
);
3313 else if (real_proto
== IPPROTO_TCP
)
3314 bpf_error("port '%s' is tcp", name
);
3316 /* override PROTO_UNDEF */
3317 real_proto
= IPPROTO_SCTP
;
3320 return gen_port(port
, real_proto
, dir
);
3324 b
= gen_port(port
, real_proto
, dir
);
3325 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3332 eaddr
= pcap_ether_hostton(name
);
3334 bpf_error("unknown ether host: %s", name
);
3336 alist
= pcap_nametoaddr(name
);
3337 if (alist
== NULL
|| *alist
== NULL
)
3338 bpf_error("unknown host '%s'", name
);
3339 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
3343 bpf_error("'gateway' not supported in this configuration");
3347 real_proto
= lookup_proto(name
, proto
);
3348 if (real_proto
>= 0)
3349 return gen_proto(real_proto
, proto
, dir
);
3351 bpf_error("unknown protocol: %s", name
);
3354 real_proto
= lookup_proto(name
, proto
);
3355 if (real_proto
>= 0)
3356 return gen_protochain(real_proto
, proto
, dir
);
3358 bpf_error("unknown protocol: %s", name
);
3370 gen_mcode(s1
, s2
, masklen
, q
)
3371 register const char *s1
, *s2
;
3372 register int masklen
;
3375 register int nlen
, mlen
;
3378 nlen
= __pcap_atoin(s1
, &n
);
3379 /* Promote short ipaddr */
3383 mlen
= __pcap_atoin(s2
, &m
);
3384 /* Promote short ipaddr */
3387 bpf_error("non-network bits set in \"%s mask %s\"",
3390 /* Convert mask len to mask */
3392 bpf_error("mask length must be <= 32");
3393 m
= 0xffffffff << (32 - masklen
);
3395 bpf_error("non-network bits set in \"%s/%d\"",
3402 return gen_host(n
, m
, q
.proto
, q
.dir
);
3405 bpf_error("Mask syntax for networks only");
3412 register const char *s
;
3417 int proto
= q
.proto
;
3423 else if (q
.proto
== Q_DECNET
)
3424 vlen
= __pcap_atodn(s
, &v
);
3426 vlen
= __pcap_atoin(s
, &v
);
3433 if (proto
== Q_DECNET
)
3434 return gen_host(v
, 0, proto
, dir
);
3435 else if (proto
== Q_LINK
) {
3436 bpf_error("illegal link layer address");
3439 if (s
== NULL
&& q
.addr
== Q_NET
) {
3440 /* Promote short net number */
3441 while (v
&& (v
& 0xff000000) == 0) {
3446 /* Promote short ipaddr */
3450 return gen_host(v
, mask
, proto
, dir
);
3455 proto
= IPPROTO_UDP
;
3456 else if (proto
== Q_TCP
)
3457 proto
= IPPROTO_TCP
;
3458 else if (proto
== Q_SCTP
)
3459 proto
= IPPROTO_SCTP
;
3460 else if (proto
== Q_DEFAULT
)
3461 proto
= PROTO_UNDEF
;
3463 bpf_error("illegal qualifier of 'port'");
3466 return gen_port((int)v
, proto
, dir
);
3470 b
= gen_port((int)v
, proto
, dir
);
3471 gen_or(gen_port6((int)v
, proto
, dir
), b
);
3477 bpf_error("'gateway' requires a name");
3481 return gen_proto((int)v
, proto
, dir
);
3484 return gen_protochain((int)v
, proto
, dir
);
3499 gen_mcode6(s1
, s2
, masklen
, q
)
3500 register const char *s1
, *s2
;
3501 register int masklen
;
3504 struct addrinfo
*res
;
3505 struct in6_addr
*addr
;
3506 struct in6_addr mask
;
3511 bpf_error("no mask %s supported", s2
);
3513 res
= pcap_nametoaddrinfo(s1
);
3515 bpf_error("invalid ip6 address %s", s1
);
3517 bpf_error("%s resolved to multiple address", s1
);
3518 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3520 if (sizeof(mask
) * 8 < masklen
)
3521 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3522 memset(&mask
, 0, sizeof(mask
));
3523 memset(&mask
, 0xff, masklen
/ 8);
3525 mask
.s6_addr
[masklen
/ 8] =
3526 (0xff << (8 - masklen
% 8)) & 0xff;
3529 a
= (u_int32_t
*)addr
;
3530 m
= (u_int32_t
*)&mask
;
3531 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3532 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3533 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
3541 bpf_error("Mask syntax for networks only");
3545 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
3550 bpf_error("invalid qualifier against IPv6 address");
3558 register const u_char
*eaddr
;
3561 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3562 if (linktype
== DLT_EN10MB
)
3563 return gen_ehostop(eaddr
, (int)q
.dir
);
3564 if (linktype
== DLT_FDDI
)
3565 return gen_fhostop(eaddr
, (int)q
.dir
);
3566 if (linktype
== DLT_IEEE802
)
3567 return gen_thostop(eaddr
, (int)q
.dir
);
3568 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
3570 bpf_error("ethernet address used in non-ether expression");
3576 struct slist
*s0
, *s1
;
3579 * This is definitely not the best way to do this, but the
3580 * lists will rarely get long.
3587 static struct slist
*
3593 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3598 static struct slist
*
3604 s
= new_stmt(BPF_LD
|BPF_MEM
);
3610 gen_load(proto
, index
, size
)
3615 struct slist
*s
, *tmp
;
3617 int regno
= alloc_reg();
3619 free_reg(index
->regno
);
3623 bpf_error("data size must be 1, 2, or 4");
3639 bpf_error("unsupported index operation");
3642 s
= xfer_to_x(index
);
3643 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3645 sappend(index
->s
, s
);
3660 /* XXX Note that we assume a fixed link header here. */
3661 s
= xfer_to_x(index
);
3662 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3665 sappend(index
->s
, s
);
3667 b
= gen_proto_abbrev(proto
);
3669 gen_and(index
->b
, b
);
3681 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3683 sappend(s
, xfer_to_a(index
));
3684 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3685 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3686 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3688 sappend(index
->s
, s
);
3690 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
3692 gen_and(index
->b
, b
);
3694 gen_and(gen_proto_abbrev(Q_IP
), b
);
3700 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
3704 index
->regno
= regno
;
3705 s
= new_stmt(BPF_ST
);
3707 sappend(index
->s
, s
);
3713 gen_relation(code
, a0
, a1
, reversed
)
3715 struct arth
*a0
, *a1
;
3718 struct slist
*s0
, *s1
, *s2
;
3719 struct block
*b
, *tmp
;
3723 if (code
== BPF_JEQ
) {
3724 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
3725 b
= new_block(JMP(code
));
3729 b
= new_block(BPF_JMP
|code
|BPF_X
);
3735 sappend(a0
->s
, a1
->s
);
3739 free_reg(a0
->regno
);
3740 free_reg(a1
->regno
);
3742 /* 'and' together protocol checks */
3745 gen_and(a0
->b
, tmp
= a1
->b
);
3761 int regno
= alloc_reg();
3762 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
3765 s
= new_stmt(BPF_LD
|BPF_LEN
);
3766 s
->next
= new_stmt(BPF_ST
);
3767 s
->next
->s
.k
= regno
;
3782 a
= (struct arth
*)newchunk(sizeof(*a
));
3786 s
= new_stmt(BPF_LD
|BPF_IMM
);
3788 s
->next
= new_stmt(BPF_ST
);
3804 s
= new_stmt(BPF_ALU
|BPF_NEG
);
3807 s
= new_stmt(BPF_ST
);
3815 gen_arth(code
, a0
, a1
)
3817 struct arth
*a0
, *a1
;
3819 struct slist
*s0
, *s1
, *s2
;
3823 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
3828 sappend(a0
->s
, a1
->s
);
3830 free_reg(a0
->regno
);
3831 free_reg(a1
->regno
);
3833 s0
= new_stmt(BPF_ST
);
3834 a0
->regno
= s0
->s
.k
= alloc_reg();
3841 * Here we handle simple allocation of the scratch registers.
3842 * If too many registers are alloc'd, the allocator punts.
3844 static int regused
[BPF_MEMWORDS
];
3848 * Return the next free register.
3853 int n
= BPF_MEMWORDS
;
3856 if (regused
[curreg
])
3857 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
3859 regused
[curreg
] = 1;
3863 bpf_error("too many registers needed to evaluate expression");
3868 * Return a register to the table so it can
3878 static struct block
*
3885 s
= new_stmt(BPF_LD
|BPF_LEN
);
3886 b
= new_block(JMP(jmp
));
3897 return gen_len(BPF_JGE
, n
);
3901 * Actually, this is less than or equal.
3909 b
= gen_len(BPF_JGT
, n
);
3916 gen_byteop(op
, idx
, val
)
3927 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3930 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3931 b
->s
.code
= JMP(BPF_JGE
);
3936 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3937 b
->s
.code
= JMP(BPF_JGT
);
3941 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3945 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3949 b
= new_block(JMP(BPF_JEQ
));
3956 static u_char abroadcast
[] = { 0x0 };
3959 gen_broadcast(proto
)
3962 bpf_u_int32 hostmask
;
3963 struct block
*b0
, *b1
, *b2
;
3964 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3970 if (linktype
== DLT_ARCNET
)
3971 return gen_ahostop(abroadcast
, Q_DST
);
3972 if (linktype
== DLT_EN10MB
)
3973 return gen_ehostop(ebroadcast
, Q_DST
);
3974 if (linktype
== DLT_FDDI
)
3975 return gen_fhostop(ebroadcast
, Q_DST
);
3976 if (linktype
== DLT_IEEE802
)
3977 return gen_thostop(ebroadcast
, Q_DST
);
3978 bpf_error("not a broadcast link");
3982 b0
= gen_linktype(ETHERTYPE_IP
);
3983 hostmask
= ~netmask
;
3984 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3985 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3986 (bpf_int32
)(~0 & hostmask
), hostmask
);
3991 bpf_error("only ether/ip broadcast filters supported");
3995 gen_multicast(proto
)
3998 register struct block
*b0
, *b1
;
3999 register struct slist
*s
;
4005 if (linktype
== DLT_ARCNET
)
4006 /* all ARCnet multicasts use the same address */
4007 return gen_ahostop(abroadcast
, Q_DST
);
4009 if (linktype
== DLT_EN10MB
) {
4010 /* ether[0] & 1 != 0 */
4011 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4013 b0
= new_block(JMP(BPF_JSET
));
4019 if (linktype
== DLT_FDDI
) {
4020 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
4021 /* fddi[1] & 1 != 0 */
4022 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4024 b0
= new_block(JMP(BPF_JSET
));
4030 /* TODO - check how token ring handles multicast */
4031 /* if (linktype == DLT_IEEE802) ... */
4033 /* Link not known to support multicasts */
4037 b0
= gen_linktype(ETHERTYPE_IP
);
4038 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
4039 b1
->s
.code
= JMP(BPF_JGE
);
4045 b0
= gen_linktype(ETHERTYPE_IPV6
);
4046 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
4051 bpf_error("only IP multicast filters supported on ethernet/FDDI");
4055 * generate command for inbound/outbound. It's here so we can
4056 * make it link-type specific. 'dir' = 0 implies "inbound",
4057 * = 1 implies "outbound".
4063 register struct block
*b0
;
4066 * Only some data link types support inbound/outbound qualifiers.
4071 b0
= gen_relation(BPF_JEQ
,
4072 gen_load(Q_LINK
, gen_loadi(0), 1),
4078 bpf_error("inbound/outbound not supported on linktype %d\n",
4088 register const u_char
*eaddr
;
4091 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4092 if (linktype
== DLT_ARCNET
)
4093 return gen_ahostop(eaddr
, (int)q
.dir
);
4095 bpf_error("ARCnet address used in non-arc expression");
4099 static struct block
*
4100 gen_ahostop(eaddr
, dir
)
4101 register const u_char
*eaddr
;
4104 register struct block
*b0
, *b1
;
4107 /* src comes first, different from Ethernet */
4109 return gen_bcmp(0, 1, eaddr
);
4112 return gen_bcmp(1, 1, eaddr
);
4115 b0
= gen_ahostop(eaddr
, Q_SRC
);
4116 b1
= gen_ahostop(eaddr
, Q_DST
);
4122 b0
= gen_ahostop(eaddr
, Q_SRC
);
4123 b1
= gen_ahostop(eaddr
, Q_DST
);
4132 * support IEEE 802.1Q VLAN trunk over ethernet
4141 * Change the offsets to point to the type and data fields within
4142 * the VLAN packet. This is somewhat of a kludge.
4144 if (orig_nl
== (u_int
)-1) {
4145 orig_linktype
= off_linktype
; /* save original values */
4147 orig_nl_nosnap
= off_nl_nosnap
;
4158 bpf_error("no VLAN support for data link type %d",
4164 /* check for VLAN */
4165 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
4167 /* If a specific VLAN is requested, check VLAN id */
4168 if (vlan_num
>= 0) {
4171 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);
4180 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
4192 bpf_error("'vpi' supported only on raw ATM");
4195 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
4196 (u_int
)jvalue
, reverse
);
4201 bpf_error("'vci' supported only on raw ATM");
4204 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
4205 (u_int
)jvalue
, reverse
);
4209 if (off_proto
== -1)
4210 abort(); /* XXX - this isn't on FreeBSD */
4211 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
4212 (u_int
)jvalue
, reverse
);
4216 if (off_msg_type
== -1)
4218 b0
= gen_ncmp(BPF_B
, off_msg_type
, 0xffffffff,
4219 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
4224 bpf_error("'callref' supported only on raw ATM");
4225 if (off_proto
== -1)
4227 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
4228 (u_int
)jvalue
, reverse
);
4238 gen_atmtype_abbrev(type
)
4241 struct block
*b0
, *b1
;
4246 /* Get all packets in Meta signalling Circuit */
4248 bpf_error("'metac' supported only on raw ATM");
4249 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4250 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
4255 /* Get all packets in Broadcast Circuit*/
4257 bpf_error("'bcc' supported only on raw ATM");
4258 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4259 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
4264 /* Get all cells in Segment OAM F4 circuit*/
4266 bpf_error("'oam4sc' supported only on raw ATM");
4267 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4268 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
4273 /* Get all cells in End-to-End OAM F4 Circuit*/
4275 bpf_error("'oam4ec' supported only on raw ATM");
4276 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4277 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
4282 /* Get all packets in connection Signalling Circuit */
4284 bpf_error("'sc' supported only on raw ATM");
4285 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4286 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
4291 /* Get all packets in ILMI Circuit */
4293 bpf_error("'ilmic' supported only on raw ATM");
4294 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4295 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
4300 /* Get all LANE packets */
4302 bpf_error("'lane' supported only on raw ATM");
4303 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
4307 /* Get all LLC-encapsulated packets */
4309 bpf_error("'llc' supported only on raw ATM");
4310 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
4320 static struct block
*
4321 gen_msg_abbrev(type
)
4327 * Q.2931 signalling protocol messages for handling virtual circuits
4328 * establishment and teardown
4333 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
4337 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
4341 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
4345 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
4349 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
4352 case A_RELEASE_DONE
:
4353 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
4363 gen_atmmulti_abbrev(type
)
4366 struct block
*b0
, *b1
;
4372 bpf_error("'oam' supported only on raw ATM");
4373 b1
= gen_atmmulti_abbrev(A_OAMF4
);
4378 bpf_error("'oamf4' supported only on raw ATM");
4380 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
4381 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
4383 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4389 * Get Q.2931 signalling messages for switched
4390 * virtual connection
4393 bpf_error("'connectmsg' supported only on raw ATM");
4394 b0
= gen_msg_abbrev(A_SETUP
);
4395 b1
= gen_msg_abbrev(A_CALLPROCEED
);
4397 b0
= gen_msg_abbrev(A_CONNECT
);
4399 b0
= gen_msg_abbrev(A_CONNECTACK
);
4401 b0
= gen_msg_abbrev(A_RELEASE
);
4403 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
4405 b0
= gen_atmtype_abbrev(A_SC
);
4411 bpf_error("'metaconnect' supported only on raw ATM");
4412 b0
= gen_msg_abbrev(A_SETUP
);
4413 b1
= gen_msg_abbrev(A_CALLPROCEED
);
4415 b0
= gen_msg_abbrev(A_CONNECT
);
4417 b0
= gen_msg_abbrev(A_RELEASE
);
4419 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
4421 b0
= gen_atmtype_abbrev(A_METAC
);