]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
2a49825d235bbff07af2cf779b79b48008ea0413
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.168 2002-07-11 07:56:44 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 */
787 * XXX - we should set this to handle SNAP-encapsulated
788 * frames (NLPID of 0x80).
792 off_nl_nosnap
= 0; /* no 802.2 LLC */
795 bpf_error("unknown data link type %d", linktype
);
799 static struct block
*
806 s
= new_stmt(BPF_LD
|BPF_IMM
);
808 b
= new_block(JMP(BPF_JEQ
));
814 static inline struct block
*
817 return gen_uncond(1);
820 static inline struct block
*
823 return gen_uncond(0);
827 * Byte-swap a 32-bit number.
828 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
829 * big-endian platforms.)
831 #define SWAPLONG(y) \
832 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
834 static struct block
*
838 struct block
*b0
, *b1
, *b2
;
847 * OSI protocols always use 802.2 encapsulation.
848 * XXX - should we check both the DSAP and the
849 * SSAP, like this, or should we check just the
852 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
854 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
855 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
861 * NetBEUI always uses 802.2 encapsulation.
862 * XXX - should we check both the DSAP and the
863 * SSAP, like this, or should we check just the
866 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
868 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
869 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
877 * Ethernet_II frames, which are Ethernet
878 * frames with a frame type of ETHERTYPE_IPX;
880 * Ethernet_802.3 frames, which are 802.3
881 * frames (i.e., the type/length field is
882 * a length field, <= ETHERMTU, rather than
883 * a type field) with the first two bytes
884 * after the Ethernet/802.3 header being
887 * Ethernet_802.2 frames, which are 802.3
888 * frames with an 802.2 LLC header and
889 * with the IPX LSAP as the DSAP in the LLC
892 * Ethernet_SNAP frames, which are 802.3
893 * frames with an LLC header and a SNAP
894 * header and with an OUI of 0x000000
895 * (encapsulated Ethernet) and a protocol
896 * ID of ETHERTYPE_IPX in the SNAP header.
898 * XXX - should we generate the same code both
899 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
903 * This generates code to check both for the
904 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
906 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
907 (bpf_int32
)LLCSAP_IPX
);
908 b1
= gen_cmp(off_linktype
+ 2, BPF_H
,
913 * Now we add code to check for SNAP frames with
914 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
916 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
920 * Now we generate code to check for 802.3
923 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
927 * Now add the check for 802.3 frames before the
928 * check for Ethernet_802.2 and Ethernet_802.3,
929 * as those checks should only be done on 802.3
930 * frames, not on Ethernet frames.
935 * Now add the check for Ethernet_II frames, and
936 * do that before checking for the other frame
939 b0
= gen_cmp(off_linktype
, BPF_H
,
940 (bpf_int32
)ETHERTYPE_IPX
);
944 case ETHERTYPE_ATALK
:
947 * EtherTalk (AppleTalk protocols on Ethernet link
948 * layer) may use 802.2 encapsulation.
952 * Check for 802.2 encapsulation (EtherTalk phase 2?);
953 * we check for an Ethernet type field less than
954 * 1500, which means it's an 802.3 length field.
956 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
960 * 802.2-encapsulated ETHERTYPE_ATALK packets are
961 * SNAP packets with an organization code of
962 * 0x080007 (Apple, for Appletalk) and a protocol
963 * type of ETHERTYPE_ATALK (Appletalk).
965 * 802.2-encapsulated ETHERTYPE_AARP packets are
966 * SNAP packets with an organization code of
967 * 0x000000 (encapsulated Ethernet) and a protocol
968 * type of ETHERTYPE_AARP (Appletalk ARP).
970 if (proto
== ETHERTYPE_ATALK
)
971 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
972 else /* proto == ETHERTYPE_AARP */
973 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
977 * Check for Ethernet encapsulation (Ethertalk
978 * phase 1?); we just check for the Ethernet
981 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
987 if (proto
<= ETHERMTU
) {
989 * This is an LLC SAP value, so the frames
990 * that match would be 802.2 frames.
991 * Check that the frame is an 802.2 frame
992 * (i.e., that the length/type field is
993 * a length field, <= ETHERMTU) and
994 * then check the DSAP.
996 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
998 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1004 * This is an Ethernet type, so compare
1005 * the length/type field with it (if
1006 * the frame is an 802.2 frame, the length
1007 * field will be <= ETHERMTU, and, as
1008 * "proto" is > ETHERMTU, this test
1009 * will fail and the frame won't match,
1010 * which is what we want).
1012 return gen_cmp(off_linktype
, BPF_H
,
1018 case DLT_IEEE802_11
:
1019 case DLT_PRISM_HEADER
:
1022 case DLT_ATM_RFC1483
:
1025 * XXX - handle token-ring variable-length header.
1030 return gen_cmp(off_linktype
, BPF_H
, (long)
1031 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1033 case LLCSAP_NETBEUI
:
1034 return gen_cmp(off_linktype
, BPF_H
, (long)
1035 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1039 * XXX - are there ever SNAP frames for IPX on
1040 * non-Ethernet 802.x networks?
1042 return gen_cmp(off_linktype
, BPF_B
,
1043 (bpf_int32
)LLCSAP_IPX
);
1045 case ETHERTYPE_ATALK
:
1047 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1048 * SNAP packets with an organization code of
1049 * 0x080007 (Apple, for Appletalk) and a protocol
1050 * type of ETHERTYPE_ATALK (Appletalk).
1052 * XXX - check for an organization code of
1053 * encapsulated Ethernet as well?
1055 return gen_snap(0x080007, ETHERTYPE_ATALK
,
1061 * XXX - we don't have to check for IPX 802.3
1062 * here, but should we check for the IPX Ethertype?
1064 if (proto
<= ETHERMTU
) {
1066 * This is an LLC SAP value, so check
1069 return gen_cmp(off_linktype
, BPF_B
,
1073 * This is an Ethernet type; we assume
1074 * that it's unlikely that it'll
1075 * appear in the right place at random,
1076 * and therefore check only the
1077 * location that would hold the Ethernet
1078 * type in a SNAP frame with an organization
1079 * code of 0x000000 (encapsulated Ethernet).
1081 * XXX - if we were to check for the SNAP DSAP
1082 * and LSAP, as per XXX, and were also to check
1083 * for an organization code of 0x000000
1084 * (encapsulated Ethernet), we'd do
1086 * return gen_snap(0x000000, proto,
1089 * here; for now, we don't, as per the above.
1090 * I don't know whether it's worth the
1091 * extra CPU time to do the right check
1094 return gen_cmp(off_linktype
+6, BPF_H
,
1105 * OSI protocols always use 802.2 encapsulation.
1106 * XXX - should we check both the DSAP and the
1107 * LSAP, like this, or should we check just the
1110 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1111 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1112 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1116 case LLCSAP_NETBEUI
:
1118 * NetBEUI always uses 802.2 encapsulation.
1119 * XXX - should we check both the DSAP and the
1120 * LSAP, like this, or should we check just the
1123 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1124 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1125 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1131 * Ethernet_II frames, which are Ethernet
1132 * frames with a frame type of ETHERTYPE_IPX;
1134 * Ethernet_802.3 frames, which have a frame
1135 * type of LINUX_SLL_P_802_3;
1137 * Ethernet_802.2 frames, which are 802.3
1138 * frames with an 802.2 LLC header (i.e, have
1139 * a frame type of LINUX_SLL_P_802_2) and
1140 * with the IPX LSAP as the DSAP in the LLC
1143 * Ethernet_SNAP frames, which are 802.3
1144 * frames with an LLC header and a SNAP
1145 * header and with an OUI of 0x000000
1146 * (encapsulated Ethernet) and a protocol
1147 * ID of ETHERTYPE_IPX in the SNAP header.
1149 * First, do the checks on LINUX_SLL_P_802_2
1150 * frames; generate the check for either
1151 * Ethernet_802.2 or Ethernet_SNAP frames, and
1152 * then put a check for LINUX_SLL_P_802_2 frames
1155 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1156 (bpf_int32
)LLCSAP_IPX
);
1157 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1160 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1164 * Now check for 802.3 frames and OR that with
1165 * the previous test.
1167 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1171 * Now add the check for Ethernet_II frames, and
1172 * do that before checking for the other frame
1175 b0
= gen_cmp(off_linktype
, BPF_H
,
1176 (bpf_int32
)ETHERTYPE_IPX
);
1180 case ETHERTYPE_ATALK
:
1181 case ETHERTYPE_AARP
:
1183 * EtherTalk (AppleTalk protocols on Ethernet link
1184 * layer) may use 802.2 encapsulation.
1188 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1189 * we check for the 802.2 protocol type in the
1190 * "Ethernet type" field.
1192 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1195 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1196 * SNAP packets with an organization code of
1197 * 0x080007 (Apple, for Appletalk) and a protocol
1198 * type of ETHERTYPE_ATALK (Appletalk).
1200 * 802.2-encapsulated ETHERTYPE_AARP packets are
1201 * SNAP packets with an organization code of
1202 * 0x000000 (encapsulated Ethernet) and a protocol
1203 * type of ETHERTYPE_AARP (Appletalk ARP).
1205 if (proto
== ETHERTYPE_ATALK
)
1206 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1208 else /* proto == ETHERTYPE_AARP */
1209 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1214 * Check for Ethernet encapsulation (Ethertalk
1215 * phase 1?); we just check for the Ethernet
1218 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1224 if (proto
<= ETHERMTU
) {
1226 * This is an LLC SAP value, so the frames
1227 * that match would be 802.2 frames.
1228 * Check for the 802.2 protocol type
1229 * in the "Ethernet type" field, and
1230 * then check the DSAP.
1232 b0
= gen_cmp(off_linktype
, BPF_H
,
1234 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1240 * This is an Ethernet type, so compare
1241 * the length/type field with it (if
1242 * the frame is an 802.2 frame, the length
1243 * field will be <= ETHERMTU, and, as
1244 * "proto" is > ETHERMTU, this test
1245 * will fail and the frame won't match,
1246 * which is what we want).
1248 return gen_cmp(off_linktype
, BPF_H
,
1255 case DLT_SLIP_BSDOS
:
1258 * These types don't provide any type field; packets
1261 * XXX - for IPv4, check for a version number of 4, and,
1262 * for IPv6, check for a version number of 6?
1268 case ETHERTYPE_IPV6
:
1270 return gen_true(); /* always true */
1273 return gen_false(); /* always false */
1278 case DLT_PPP_SERIAL
:
1281 * We use Ethernet protocol types inside libpcap;
1282 * map them to the corresponding PPP protocol types.
1287 proto
= PPP_IP
; /* XXX was 0x21 */
1291 case ETHERTYPE_IPV6
:
1300 case ETHERTYPE_ATALK
:
1314 * I'm assuming the "Bridging PDU"s that go
1315 * over PPP are Spanning Tree Protocol
1329 * We use Ethernet protocol types inside libpcap;
1330 * map them to the corresponding PPP protocol types.
1335 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1336 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1338 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1343 case ETHERTYPE_IPV6
:
1353 case ETHERTYPE_ATALK
:
1367 * I'm assuming the "Bridging PDU"s that go
1368 * over PPP are Spanning Tree Protocol
1383 * For DLT_NULL, the link-layer header is a 32-bit
1384 * word containing an AF_ value in *host* byte order.
1386 * In addition, if we're reading a saved capture file,
1387 * the host byte order in the capture may not be the
1388 * same as the host byte order on this machine.
1390 * For DLT_LOOP, the link-layer header is a 32-bit
1391 * word containing an AF_ value in *network* byte order.
1393 * XXX - AF_ values may, unfortunately, be platform-
1394 * dependent; for example, FreeBSD's AF_INET6 is 24
1395 * whilst NetBSD's and OpenBSD's is 26.
1397 * This means that, when reading a capture file, just
1398 * checking for our AF_INET6 value won't work if the
1399 * capture file came from another OS.
1408 case ETHERTYPE_IPV6
:
1415 * Not a type on which we support filtering.
1416 * XXX - support those that have AF_ values
1417 * #defined on this platform, at least?
1422 if (linktype
== DLT_NULL
) {
1424 * The AF_ value is in host byte order, but
1425 * the BPF interpreter will convert it to
1426 * network byte order.
1428 * If this is a save file, and it's from a
1429 * machine with the opposite byte order to
1430 * ours, we byte-swap the AF_ value.
1432 * Then we run it through "htonl()", and
1433 * generate code to compare against the result.
1435 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1436 bpf_pcap
->sf
.swapped
)
1437 proto
= SWAPLONG(proto
);
1438 proto
= htonl(proto
);
1440 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1444 * XXX should we check for first fragment if the protocol
1451 case ETHERTYPE_IPV6
:
1452 return(gen_cmp(2, BPF_B
,
1453 (bpf_int32
)htonl(ARCTYPE_INET6
)));
1456 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_IP
));
1457 b1
= gen_cmp(2, BPF_B
,
1458 (bpf_int32
)htonl(ARCTYPE_IP_OLD
));
1462 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_ARP
));
1463 b1
= gen_cmp(2, BPF_B
,
1464 (bpf_int32
)htonl(ARCTYPE_ARP_OLD
));
1467 case ETHERTYPE_REVARP
:
1468 return(gen_cmp(2, BPF_B
,
1469 (bpf_int32
)htonl(ARCTYPE_REVARP
)));
1470 case ETHERTYPE_ATALK
:
1471 return(gen_cmp(2, BPF_B
,
1472 (bpf_int32
)htonl(ARCTYPE_ATALK
)));
1478 case ETHERTYPE_ATALK
:
1487 * XXX - assumes a 2-byte Frame Relay header with
1488 * DLCI and flags. What if the address is longer?
1494 * Check for the special NLPID for IP.
1496 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1499 case ETHERTYPE_IPV6
:
1501 * Check for the special NLPID for IPv6.
1503 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1508 * Check for several OSI protocols.
1510 * Frame Relay packets typically have an OSI
1511 * NLPID at the beginning; we check for each
1514 * What we check for is the NLPID and a frame
1515 * control field of UI, i.e. 0x03 followed
1518 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1519 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1520 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1532 * All the types that have no encapsulation should either be
1533 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1534 * all packets are IP packets, or should be handled in some
1535 * special case, if none of them are (if some are and some
1536 * aren't, the lack of encapsulation is a problem, as we'd
1537 * have to find some other way of determining the packet type).
1539 * Therefore, if "off_linktype" is -1, there's an error.
1541 if (off_linktype
== -1)
1545 * Any type not handled above should always have an Ethernet
1546 * type at an offset of "off_linktype". (PPP is partially
1547 * handled above - the protocol type is mapped from the
1548 * Ethernet and LLC types we use internally to the corresponding
1549 * PPP type - but the PPP type is always specified by a value
1550 * at "off_linktype", so we don't have to do the code generation
1553 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1557 * Check for an LLC SNAP packet with a given organization code and
1558 * protocol type; we check the entire contents of the 802.2 LLC and
1559 * snap headers, checking for DSAP and SSAP of SNAP and a control
1560 * field of 0x03 in the LLC header, and for the specified organization
1561 * code and protocol type in the SNAP header.
1563 static struct block
*
1564 gen_snap(orgcode
, ptype
, offset
)
1565 bpf_u_int32 orgcode
;
1569 u_char snapblock
[8];
1571 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1572 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1573 snapblock
[2] = 0x03; /* control = UI */
1574 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1575 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1576 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1577 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1578 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1579 return gen_bcmp(offset
, 8, snapblock
);
1582 static struct block
*
1583 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1587 u_int src_off
, dst_off
;
1589 struct block
*b0
, *b1
;
1603 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1604 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1610 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1611 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1618 b0
= gen_linktype(proto
);
1619 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1625 static struct block
*
1626 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1627 struct in6_addr
*addr
;
1628 struct in6_addr
*mask
;
1630 u_int src_off
, dst_off
;
1632 struct block
*b0
, *b1
;
1647 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1648 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1654 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1655 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1662 /* this order is important */
1663 a
= (u_int32_t
*)addr
;
1664 m
= (u_int32_t
*)mask
;
1665 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1666 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1668 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1670 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1672 b0
= gen_linktype(proto
);
1678 static struct block
*
1679 gen_ehostop(eaddr
, dir
)
1680 register const u_char
*eaddr
;
1683 register struct block
*b0
, *b1
;
1687 return gen_bcmp(6, 6, eaddr
);
1690 return gen_bcmp(0, 6, eaddr
);
1693 b0
= gen_ehostop(eaddr
, Q_SRC
);
1694 b1
= gen_ehostop(eaddr
, Q_DST
);
1700 b0
= gen_ehostop(eaddr
, Q_SRC
);
1701 b1
= gen_ehostop(eaddr
, Q_DST
);
1710 * Like gen_ehostop, but for DLT_FDDI
1712 static struct block
*
1713 gen_fhostop(eaddr
, dir
)
1714 register const u_char
*eaddr
;
1717 struct block
*b0
, *b1
;
1722 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1724 return gen_bcmp(6 + 1, 6, eaddr
);
1729 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1731 return gen_bcmp(0 + 1, 6, eaddr
);
1735 b0
= gen_fhostop(eaddr
, Q_SRC
);
1736 b1
= gen_fhostop(eaddr
, Q_DST
);
1742 b0
= gen_fhostop(eaddr
, Q_SRC
);
1743 b1
= gen_fhostop(eaddr
, Q_DST
);
1752 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1754 static struct block
*
1755 gen_thostop(eaddr
, dir
)
1756 register const u_char
*eaddr
;
1759 register struct block
*b0
, *b1
;
1763 return gen_bcmp(8, 6, eaddr
);
1766 return gen_bcmp(2, 6, eaddr
);
1769 b0
= gen_thostop(eaddr
, Q_SRC
);
1770 b1
= gen_thostop(eaddr
, Q_DST
);
1776 b0
= gen_thostop(eaddr
, Q_SRC
);
1777 b1
= gen_thostop(eaddr
, Q_DST
);
1786 * This is quite tricky because there may be pad bytes in front of the
1787 * DECNET header, and then there are two possible data packet formats that
1788 * carry both src and dst addresses, plus 5 packet types in a format that
1789 * carries only the src node, plus 2 types that use a different format and
1790 * also carry just the src node.
1794 * Instead of doing those all right, we just look for data packets with
1795 * 0 or 1 bytes of padding. If you want to look at other packets, that
1796 * will require a lot more hacking.
1798 * To add support for filtering on DECNET "areas" (network numbers)
1799 * one would want to add a "mask" argument to this routine. That would
1800 * make the filter even more inefficient, although one could be clever
1801 * and not generate masking instructions if the mask is 0xFFFF.
1803 static struct block
*
1804 gen_dnhostop(addr
, dir
, base_off
)
1809 struct block
*b0
, *b1
, *b2
, *tmp
;
1810 u_int offset_lh
; /* offset if long header is received */
1811 u_int offset_sh
; /* offset if short header is received */
1816 offset_sh
= 1; /* follows flags */
1817 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1821 offset_sh
= 3; /* follows flags, dstnode */
1822 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1826 /* Inefficient because we do our Calvinball dance twice */
1827 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1828 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1834 /* Inefficient because we do our Calvinball dance twice */
1835 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1836 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1841 bpf_error("ISO host filtering not implemented");
1846 b0
= gen_linktype(ETHERTYPE_DN
);
1847 /* Check for pad = 1, long header case */
1848 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1849 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1850 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1851 BPF_H
, (bpf_int32
)ntohs(addr
));
1853 /* Check for pad = 0, long header case */
1854 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1855 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1858 /* Check for pad = 1, short header case */
1859 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1860 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1861 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1862 BPF_H
, (bpf_int32
)ntohs(addr
));
1865 /* Check for pad = 0, short header case */
1866 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1867 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1871 /* Combine with test for linktype */
1876 static struct block
*
1877 gen_host(addr
, mask
, proto
, dir
)
1883 struct block
*b0
, *b1
;
1888 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1889 if (off_linktype
!= -1) {
1890 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1892 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1898 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1899 off_nl
+ 12, off_nl
+ 16);
1902 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1903 off_nl
+ 14, off_nl
+ 24);
1906 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1907 off_nl
+ 14, off_nl
+ 24);
1910 bpf_error("'tcp' modifier applied to host");
1913 bpf_error("'sctp' modifier applied to host");
1916 bpf_error("'udp' modifier applied to host");
1919 bpf_error("'icmp' modifier applied to host");
1922 bpf_error("'igmp' modifier applied to host");
1925 bpf_error("'igrp' modifier applied to host");
1928 bpf_error("'pim' modifier applied to host");
1931 bpf_error("'vrrp' modifier applied to host");
1934 bpf_error("ATALK host filtering not implemented");
1937 bpf_error("AARP host filtering not implemented");
1940 return gen_dnhostop(addr
, dir
, off_nl
);
1943 bpf_error("SCA host filtering not implemented");
1946 bpf_error("LAT host filtering not implemented");
1949 bpf_error("MOPDL host filtering not implemented");
1952 bpf_error("MOPRC host filtering not implemented");
1956 bpf_error("'ip6' modifier applied to ip host");
1959 bpf_error("'icmp6' modifier applied to host");
1963 bpf_error("'ah' modifier applied to host");
1966 bpf_error("'esp' modifier applied to host");
1969 bpf_error("ISO host filtering not implemented");
1972 bpf_error("'esis' modifier applied to host");
1975 bpf_error("'isis' modifier applied to host");
1978 bpf_error("'clnp' modifier applied to host");
1981 bpf_error("'stp' modifier applied to host");
1984 bpf_error("IPX host filtering not implemented");
1987 bpf_error("'netbeui' modifier applied to host");
1996 static struct block
*
1997 gen_host6(addr
, mask
, proto
, dir
)
1998 struct in6_addr
*addr
;
1999 struct in6_addr
*mask
;
2006 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2009 bpf_error("'ip' modifier applied to ip6 host");
2012 bpf_error("'rarp' modifier applied to ip6 host");
2015 bpf_error("'arp' modifier applied to ip6 host");
2018 bpf_error("'sctp' modifier applied to host");
2021 bpf_error("'tcp' modifier applied to host");
2024 bpf_error("'udp' modifier applied to host");
2027 bpf_error("'icmp' modifier applied to host");
2030 bpf_error("'igmp' modifier applied to host");
2033 bpf_error("'igrp' modifier applied to host");
2036 bpf_error("'pim' modifier applied to host");
2039 bpf_error("'vrrp' modifier applied to host");
2042 bpf_error("ATALK host filtering not implemented");
2045 bpf_error("AARP host filtering not implemented");
2048 bpf_error("'decnet' modifier applied to ip6 host");
2051 bpf_error("SCA host filtering not implemented");
2054 bpf_error("LAT host filtering not implemented");
2057 bpf_error("MOPDL host filtering not implemented");
2060 bpf_error("MOPRC host filtering not implemented");
2063 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2064 off_nl
+ 8, off_nl
+ 24);
2067 bpf_error("'icmp6' modifier applied to host");
2070 bpf_error("'ah' modifier applied to host");
2073 bpf_error("'esp' modifier applied to host");
2076 bpf_error("ISO host filtering not implemented");
2079 bpf_error("'esis' modifier applied to host");
2082 bpf_error("'isis' modifier applied to host");
2085 bpf_error("'clnp' modifier applied to host");
2088 bpf_error("'stp' modifier applied to host");
2091 bpf_error("IPX host filtering not implemented");
2094 bpf_error("'netbeui' modifier applied to host");
2104 static struct block
*
2105 gen_gateway(eaddr
, alist
, proto
, dir
)
2106 const u_char
*eaddr
;
2107 bpf_u_int32
**alist
;
2111 struct block
*b0
, *b1
, *tmp
;
2114 bpf_error("direction applied to 'gateway'");
2121 if (linktype
== DLT_EN10MB
)
2122 b0
= gen_ehostop(eaddr
, Q_OR
);
2123 else if (linktype
== DLT_FDDI
)
2124 b0
= gen_fhostop(eaddr
, Q_OR
);
2125 else if (linktype
== DLT_IEEE802
)
2126 b0
= gen_thostop(eaddr
, Q_OR
);
2129 "'gateway' supported only on ethernet, FDDI or token ring");
2131 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2133 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2141 bpf_error("illegal modifier of 'gateway'");
2147 gen_proto_abbrev(proto
)
2158 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2160 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2166 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2168 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2174 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2176 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2182 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2185 #ifndef IPPROTO_IGMP
2186 #define IPPROTO_IGMP 2
2190 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2193 #ifndef IPPROTO_IGRP
2194 #define IPPROTO_IGRP 9
2197 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2201 #define IPPROTO_PIM 103
2205 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2207 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2212 #ifndef IPPROTO_VRRP
2213 #define IPPROTO_VRRP 112
2217 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2221 b1
= gen_linktype(ETHERTYPE_IP
);
2225 b1
= gen_linktype(ETHERTYPE_ARP
);
2229 b1
= gen_linktype(ETHERTYPE_REVARP
);
2233 bpf_error("link layer applied in wrong context");
2236 b1
= gen_linktype(ETHERTYPE_ATALK
);
2240 b1
= gen_linktype(ETHERTYPE_AARP
);
2244 b1
= gen_linktype(ETHERTYPE_DN
);
2248 b1
= gen_linktype(ETHERTYPE_SCA
);
2252 b1
= gen_linktype(ETHERTYPE_LAT
);
2256 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2260 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2265 b1
= gen_linktype(ETHERTYPE_IPV6
);
2268 #ifndef IPPROTO_ICMPV6
2269 #define IPPROTO_ICMPV6 58
2272 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2277 #define IPPROTO_AH 51
2280 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2282 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2288 #define IPPROTO_ESP 50
2291 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2293 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2299 b1
= gen_linktype(LLCSAP_ISONS
);
2303 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2307 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2311 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2315 b1
= gen_linktype(LLCSAP_8021D
);
2319 b1
= gen_linktype(LLCSAP_IPX
);
2323 b1
= gen_linktype(LLCSAP_NETBEUI
);
2332 static struct block
*
2339 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2340 s
->s
.k
= off_nl
+ 6;
2341 b
= new_block(JMP(BPF_JSET
));
2349 static struct block
*
2350 gen_portatom(off
, v
)
2357 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2360 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2361 s
->next
->s
.k
= off_nl
+ off
;
2363 b
= new_block(JMP(BPF_JEQ
));
2371 static struct block
*
2372 gen_portatom6(off
, v
)
2376 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2381 gen_portop(port
, proto
, dir
)
2382 int port
, proto
, dir
;
2384 struct block
*b0
, *b1
, *tmp
;
2386 /* ip proto 'proto' */
2387 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2393 b1
= gen_portatom(0, (bpf_int32
)port
);
2397 b1
= gen_portatom(2, (bpf_int32
)port
);
2402 tmp
= gen_portatom(0, (bpf_int32
)port
);
2403 b1
= gen_portatom(2, (bpf_int32
)port
);
2408 tmp
= gen_portatom(0, (bpf_int32
)port
);
2409 b1
= gen_portatom(2, (bpf_int32
)port
);
2421 static struct block
*
2422 gen_port(port
, ip_proto
, dir
)
2427 struct block
*b0
, *b1
, *tmp
;
2429 /* ether proto ip */
2430 b0
= gen_linktype(ETHERTYPE_IP
);
2436 b1
= gen_portop(port
, ip_proto
, dir
);
2440 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2441 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2443 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
2456 gen_portop6(port
, proto
, dir
)
2457 int port
, proto
, dir
;
2459 struct block
*b0
, *b1
, *tmp
;
2461 /* ip proto 'proto' */
2462 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2466 b1
= gen_portatom6(0, (bpf_int32
)port
);
2470 b1
= gen_portatom6(2, (bpf_int32
)port
);
2475 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2476 b1
= gen_portatom6(2, (bpf_int32
)port
);
2481 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2482 b1
= gen_portatom6(2, (bpf_int32
)port
);
2494 static struct block
*
2495 gen_port6(port
, ip_proto
, dir
)
2500 struct block
*b0
, *b1
, *tmp
;
2502 /* ether proto ip */
2503 b0
= gen_linktype(ETHERTYPE_IPV6
);
2509 b1
= gen_portop6(port
, ip_proto
, dir
);
2513 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2514 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2516 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
2529 lookup_proto(name
, proto
)
2530 register const char *name
;
2540 v
= pcap_nametoproto(name
);
2541 if (v
== PROTO_UNDEF
)
2542 bpf_error("unknown ip proto '%s'", name
);
2546 /* XXX should look up h/w protocol type based on linktype */
2547 v
= pcap_nametoeproto(name
);
2548 if (v
== PROTO_UNDEF
)
2549 bpf_error("unknown ether proto '%s'", name
);
2553 if (strcmp(name
, "esis") == 0)
2555 else if (strcmp(name
, "isis") == 0)
2557 else if (strcmp(name
, "clnp") == 0)
2560 bpf_error("unknown osi proto '%s'", name
);
2580 static struct block
*
2581 gen_protochain(v
, proto
, dir
)
2586 #ifdef NO_PROTOCHAIN
2587 return gen_proto(v
, proto
, dir
);
2589 struct block
*b0
, *b
;
2590 struct slist
*s
[100];
2591 int fix2
, fix3
, fix4
, fix5
;
2592 int ahcheck
, again
, end
;
2594 int reg2
= alloc_reg();
2596 memset(s
, 0, sizeof(s
));
2597 fix2
= fix3
= fix4
= fix5
= 0;
2604 b0
= gen_protochain(v
, Q_IP
, dir
);
2605 b
= gen_protochain(v
, Q_IPV6
, dir
);
2609 bpf_error("bad protocol applied for 'protochain'");
2613 no_optimize
= 1; /*this code is not compatible with optimzer yet */
2616 * s[0] is a dummy entry to protect other BPF insn from damaged
2617 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
2618 * hard to find interdependency made by jump table fixup.
2621 s
[i
] = new_stmt(0); /*dummy*/
2626 b0
= gen_linktype(ETHERTYPE_IP
);
2629 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2630 s
[i
]->s
.k
= off_nl
+ 9;
2632 /* X = ip->ip_hl << 2 */
2633 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2639 b0
= gen_linktype(ETHERTYPE_IPV6
);
2641 /* A = ip6->ip_nxt */
2642 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2643 s
[i
]->s
.k
= off_nl
+ 6;
2645 /* X = sizeof(struct ip6_hdr) */
2646 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
2652 bpf_error("unsupported proto to gen_protochain");
2656 /* again: if (A == v) goto end; else fall through; */
2658 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2660 s
[i
]->s
.jt
= NULL
; /*later*/
2661 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2665 #ifndef IPPROTO_NONE
2666 #define IPPROTO_NONE 59
2668 /* if (A == IPPROTO_NONE) goto end */
2669 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2670 s
[i
]->s
.jt
= NULL
; /*later*/
2671 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2672 s
[i
]->s
.k
= IPPROTO_NONE
;
2673 s
[fix5
]->s
.jf
= s
[i
];
2678 if (proto
== Q_IPV6
) {
2679 int v6start
, v6end
, v6advance
, j
;
2682 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
2683 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2684 s
[i
]->s
.jt
= NULL
; /*later*/
2685 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2686 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
2687 s
[fix2
]->s
.jf
= s
[i
];
2689 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
2690 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2691 s
[i
]->s
.jt
= NULL
; /*later*/
2692 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2693 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
2695 /* if (A == IPPROTO_ROUTING) goto v6advance */
2696 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2697 s
[i
]->s
.jt
= NULL
; /*later*/
2698 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2699 s
[i
]->s
.k
= IPPROTO_ROUTING
;
2701 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
2702 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2703 s
[i
]->s
.jt
= NULL
; /*later*/
2704 s
[i
]->s
.jf
= NULL
; /*later*/
2705 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
2716 * X = X + (P[X + 1] + 1) * 8;
2719 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2721 /* A = P[X + packet head] */
2722 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2726 s
[i
] = new_stmt(BPF_ST
);
2730 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2733 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2737 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2739 /* A = P[X + packet head]; */
2740 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2744 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2748 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2752 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2755 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2759 /* goto again; (must use BPF_JA for backward jump) */
2760 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2761 s
[i
]->s
.k
= again
- i
- 1;
2762 s
[i
- 1]->s
.jf
= s
[i
];
2766 for (j
= v6start
; j
<= v6end
; j
++)
2767 s
[j
]->s
.jt
= s
[v6advance
];
2772 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2774 s
[fix2
]->s
.jf
= s
[i
];
2780 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2781 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2782 s
[i
]->s
.jt
= NULL
; /*later*/
2783 s
[i
]->s
.jf
= NULL
; /*later*/
2784 s
[i
]->s
.k
= IPPROTO_AH
;
2786 s
[fix3
]->s
.jf
= s
[ahcheck
];
2793 * X = X + (P[X + 1] + 2) * 4;
2796 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2798 /* A = P[X + packet head]; */
2799 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2803 s
[i
] = new_stmt(BPF_ST
);
2807 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2810 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2814 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2816 /* A = P[X + packet head] */
2817 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2821 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2825 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2829 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2832 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2836 /* goto again; (must use BPF_JA for backward jump) */
2837 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2838 s
[i
]->s
.k
= again
- i
- 1;
2843 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2845 s
[fix2
]->s
.jt
= s
[end
];
2846 s
[fix4
]->s
.jf
= s
[end
];
2847 s
[fix5
]->s
.jt
= s
[end
];
2854 for (i
= 0; i
< max
- 1; i
++)
2855 s
[i
]->next
= s
[i
+ 1];
2856 s
[max
- 1]->next
= NULL
;
2861 b
= new_block(JMP(BPF_JEQ
));
2862 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2872 static struct block
*
2873 gen_proto(v
, proto
, dir
)
2878 struct block
*b0
, *b1
;
2880 if (dir
!= Q_DEFAULT
)
2881 bpf_error("direction applied to 'proto'");
2886 b0
= gen_proto(v
, Q_IP
, dir
);
2887 b1
= gen_proto(v
, Q_IPV6
, dir
);
2894 b0
= gen_linktype(ETHERTYPE_IP
);
2896 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2898 b1
= gen_protochain(v
, Q_IP
);
2908 * Frame Relay packets typically have an OSI
2909 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
2910 * generates code to check for all the OSI
2911 * NLPIDs, so calling it and then adding a check
2912 * for the particular NLPID for which we're
2913 * looking is bogus, as we can just check for
2916 * What we check for is the NLPID and a frame
2917 * control field value of UI, i.e. 0x03 followed
2920 * XXX - assumes a 2-byte Frame Relay header with
2921 * DLCI and flags. What if the address is longer?
2923 * XXX - what about SNAP-encapsulated frames?
2925 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
2929 b0
= gen_linktype(LLCSAP_ISONS
);
2930 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
2936 bpf_error("arp does not encapsulate another protocol");
2940 bpf_error("rarp does not encapsulate another protocol");
2944 bpf_error("atalk encapsulation is not specifiable");
2948 bpf_error("decnet encapsulation is not specifiable");
2952 bpf_error("sca does not encapsulate another protocol");
2956 bpf_error("lat does not encapsulate another protocol");
2960 bpf_error("moprc does not encapsulate another protocol");
2964 bpf_error("mopdl does not encapsulate another protocol");
2968 return gen_linktype(v
);
2971 bpf_error("'udp proto' is bogus");
2975 bpf_error("'tcp proto' is bogus");
2979 bpf_error("'sctp proto' is bogus");
2983 bpf_error("'icmp proto' is bogus");
2987 bpf_error("'igmp proto' is bogus");
2991 bpf_error("'igrp proto' is bogus");
2995 bpf_error("'pim proto' is bogus");
2999 bpf_error("'vrrp proto' is bogus");
3004 b0
= gen_linktype(ETHERTYPE_IPV6
);
3006 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3008 b1
= gen_protochain(v
, Q_IPV6
);
3014 bpf_error("'icmp6 proto' is bogus");
3018 bpf_error("'ah proto' is bogus");
3021 bpf_error("'ah proto' is bogus");
3024 bpf_error("'stp proto' is bogus");
3027 bpf_error("'ipx proto' is bogus");
3030 bpf_error("'netbeui proto' is bogus");
3041 register const char *name
;
3044 int proto
= q
.proto
;
3048 bpf_u_int32 mask
, addr
;
3050 bpf_u_int32
**alist
;
3053 struct sockaddr_in
*sin
;
3054 struct sockaddr_in6
*sin6
;
3055 struct addrinfo
*res
, *res0
;
3056 struct in6_addr mask128
;
3058 struct block
*b
, *tmp
;
3059 int port
, real_proto
;
3064 addr
= pcap_nametonetaddr(name
);
3066 bpf_error("unknown network '%s'", name
);
3067 /* Left justify network addr and calculate its network mask */
3069 while (addr
&& (addr
& 0xff000000) == 0) {
3073 return gen_host(addr
, mask
, proto
, dir
);
3077 if (proto
== Q_LINK
) {
3081 eaddr
= pcap_ether_hostton(name
);
3084 "unknown ether host '%s'", name
);
3085 b
= gen_ehostop(eaddr
, dir
);
3090 eaddr
= pcap_ether_hostton(name
);
3093 "unknown FDDI host '%s'", name
);
3094 b
= gen_fhostop(eaddr
, dir
);
3099 eaddr
= pcap_ether_hostton(name
);
3102 "unknown token ring host '%s'", name
);
3103 b
= gen_thostop(eaddr
, dir
);
3109 "only ethernet/FDDI/token ring supports link-level host name");
3112 } else if (proto
== Q_DECNET
) {
3113 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
3115 * I don't think DECNET hosts can be multihomed, so
3116 * there is no need to build up a list of addresses
3118 return (gen_host(dn_addr
, 0, proto
, dir
));
3121 alist
= pcap_nametoaddr(name
);
3122 if (alist
== NULL
|| *alist
== NULL
)
3123 bpf_error("unknown host '%s'", name
);
3125 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
3127 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
3129 tmp
= gen_host(**alist
++, 0xffffffff,
3136 memset(&mask128
, 0xff, sizeof(mask128
));
3137 res0
= res
= pcap_nametoaddrinfo(name
);
3139 bpf_error("unknown host '%s'", name
);
3141 tproto
= tproto6
= proto
;
3142 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
3146 for (res
= res0
; res
; res
= res
->ai_next
) {
3147 switch (res
->ai_family
) {
3149 if (tproto
== Q_IPV6
)
3152 sin
= (struct sockaddr_in
*)
3154 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
3155 0xffffffff, tproto
, dir
);
3158 if (tproto6
== Q_IP
)
3161 sin6
= (struct sockaddr_in6
*)
3163 tmp
= gen_host6(&sin6
->sin6_addr
,
3164 &mask128
, tproto6
, dir
);
3175 bpf_error("unknown host '%s'%s", name
,
3176 (proto
== Q_DEFAULT
)
3178 : " for specified address family");
3185 if (proto
!= Q_DEFAULT
&&
3186 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
3187 bpf_error("illegal qualifier of 'port'");
3188 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
3189 bpf_error("unknown port '%s'", name
);
3190 if (proto
== Q_UDP
) {
3191 if (real_proto
== IPPROTO_TCP
)
3192 bpf_error("port '%s' is tcp", name
);
3193 else if (real_proto
== IPPROTO_SCTP
)
3194 bpf_error("port '%s' is sctp", name
);
3196 /* override PROTO_UNDEF */
3197 real_proto
= IPPROTO_UDP
;
3199 if (proto
== Q_TCP
) {
3200 if (real_proto
== IPPROTO_UDP
)
3201 bpf_error("port '%s' is udp", name
);
3203 else if (real_proto
== IPPROTO_SCTP
)
3204 bpf_error("port '%s' is sctp", name
);
3206 /* override PROTO_UNDEF */
3207 real_proto
= IPPROTO_TCP
;
3209 if (proto
== Q_SCTP
) {
3210 if (real_proto
== IPPROTO_UDP
)
3211 bpf_error("port '%s' is udp", name
);
3213 else if (real_proto
== IPPROTO_TCP
)
3214 bpf_error("port '%s' is tcp", name
);
3216 /* override PROTO_UNDEF */
3217 real_proto
= IPPROTO_SCTP
;
3220 return gen_port(port
, real_proto
, dir
);
3224 b
= gen_port(port
, real_proto
, dir
);
3225 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3232 eaddr
= pcap_ether_hostton(name
);
3234 bpf_error("unknown ether host: %s", name
);
3236 alist
= pcap_nametoaddr(name
);
3237 if (alist
== NULL
|| *alist
== NULL
)
3238 bpf_error("unknown host '%s'", name
);
3239 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
3243 bpf_error("'gateway' not supported in this configuration");
3247 real_proto
= lookup_proto(name
, proto
);
3248 if (real_proto
>= 0)
3249 return gen_proto(real_proto
, proto
, dir
);
3251 bpf_error("unknown protocol: %s", name
);
3254 real_proto
= lookup_proto(name
, proto
);
3255 if (real_proto
>= 0)
3256 return gen_protochain(real_proto
, proto
, dir
);
3258 bpf_error("unknown protocol: %s", name
);
3270 gen_mcode(s1
, s2
, masklen
, q
)
3271 register const char *s1
, *s2
;
3272 register int masklen
;
3275 register int nlen
, mlen
;
3278 nlen
= __pcap_atoin(s1
, &n
);
3279 /* Promote short ipaddr */
3283 mlen
= __pcap_atoin(s2
, &m
);
3284 /* Promote short ipaddr */
3287 bpf_error("non-network bits set in \"%s mask %s\"",
3290 /* Convert mask len to mask */
3292 bpf_error("mask length must be <= 32");
3293 m
= 0xffffffff << (32 - masklen
);
3295 bpf_error("non-network bits set in \"%s/%d\"",
3302 return gen_host(n
, m
, q
.proto
, q
.dir
);
3305 bpf_error("Mask syntax for networks only");
3312 register const char *s
;
3317 int proto
= q
.proto
;
3323 else if (q
.proto
== Q_DECNET
)
3324 vlen
= __pcap_atodn(s
, &v
);
3326 vlen
= __pcap_atoin(s
, &v
);
3333 if (proto
== Q_DECNET
)
3334 return gen_host(v
, 0, proto
, dir
);
3335 else if (proto
== Q_LINK
) {
3336 bpf_error("illegal link layer address");
3339 if (s
== NULL
&& q
.addr
== Q_NET
) {
3340 /* Promote short net number */
3341 while (v
&& (v
& 0xff000000) == 0) {
3346 /* Promote short ipaddr */
3350 return gen_host(v
, mask
, proto
, dir
);
3355 proto
= IPPROTO_UDP
;
3356 else if (proto
== Q_TCP
)
3357 proto
= IPPROTO_TCP
;
3358 else if (proto
== Q_SCTP
)
3359 proto
= IPPROTO_SCTP
;
3360 else if (proto
== Q_DEFAULT
)
3361 proto
= PROTO_UNDEF
;
3363 bpf_error("illegal qualifier of 'port'");
3366 return gen_port((int)v
, proto
, dir
);
3370 b
= gen_port((int)v
, proto
, dir
);
3371 gen_or(gen_port6((int)v
, proto
, dir
), b
);
3377 bpf_error("'gateway' requires a name");
3381 return gen_proto((int)v
, proto
, dir
);
3384 return gen_protochain((int)v
, proto
, dir
);
3399 gen_mcode6(s1
, s2
, masklen
, q
)
3400 register const char *s1
, *s2
;
3401 register int masklen
;
3404 struct addrinfo
*res
;
3405 struct in6_addr
*addr
;
3406 struct in6_addr mask
;
3411 bpf_error("no mask %s supported", s2
);
3413 res
= pcap_nametoaddrinfo(s1
);
3415 bpf_error("invalid ip6 address %s", s1
);
3417 bpf_error("%s resolved to multiple address", s1
);
3418 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3420 if (sizeof(mask
) * 8 < masklen
)
3421 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3422 memset(&mask
, 0, sizeof(mask
));
3423 memset(&mask
, 0xff, masklen
/ 8);
3425 mask
.s6_addr
[masklen
/ 8] =
3426 (0xff << (8 - masklen
% 8)) & 0xff;
3429 a
= (u_int32_t
*)addr
;
3430 m
= (u_int32_t
*)&mask
;
3431 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3432 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3433 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
3441 bpf_error("Mask syntax for networks only");
3445 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
3450 bpf_error("invalid qualifier against IPv6 address");
3458 register const u_char
*eaddr
;
3461 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3462 if (linktype
== DLT_EN10MB
)
3463 return gen_ehostop(eaddr
, (int)q
.dir
);
3464 if (linktype
== DLT_FDDI
)
3465 return gen_fhostop(eaddr
, (int)q
.dir
);
3466 if (linktype
== DLT_IEEE802
)
3467 return gen_thostop(eaddr
, (int)q
.dir
);
3468 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
3470 bpf_error("ethernet address used in non-ether expression");
3476 struct slist
*s0
, *s1
;
3479 * This is definitely not the best way to do this, but the
3480 * lists will rarely get long.
3487 static struct slist
*
3493 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3498 static struct slist
*
3504 s
= new_stmt(BPF_LD
|BPF_MEM
);
3510 gen_load(proto
, index
, size
)
3515 struct slist
*s
, *tmp
;
3517 int regno
= alloc_reg();
3519 free_reg(index
->regno
);
3523 bpf_error("data size must be 1, 2, or 4");
3539 bpf_error("unsupported index operation");
3542 s
= xfer_to_x(index
);
3543 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3545 sappend(index
->s
, s
);
3560 /* XXX Note that we assume a fixed link header here. */
3561 s
= xfer_to_x(index
);
3562 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3565 sappend(index
->s
, s
);
3567 b
= gen_proto_abbrev(proto
);
3569 gen_and(index
->b
, b
);
3581 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3583 sappend(s
, xfer_to_a(index
));
3584 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3585 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3586 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3588 sappend(index
->s
, s
);
3590 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
3592 gen_and(index
->b
, b
);
3594 gen_and(gen_proto_abbrev(Q_IP
), b
);
3600 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
3604 index
->regno
= regno
;
3605 s
= new_stmt(BPF_ST
);
3607 sappend(index
->s
, s
);
3613 gen_relation(code
, a0
, a1
, reversed
)
3615 struct arth
*a0
, *a1
;
3618 struct slist
*s0
, *s1
, *s2
;
3619 struct block
*b
, *tmp
;
3623 if (code
== BPF_JEQ
) {
3624 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
3625 b
= new_block(JMP(code
));
3629 b
= new_block(BPF_JMP
|code
|BPF_X
);
3635 sappend(a0
->s
, a1
->s
);
3639 free_reg(a0
->regno
);
3640 free_reg(a1
->regno
);
3642 /* 'and' together protocol checks */
3645 gen_and(a0
->b
, tmp
= a1
->b
);
3661 int regno
= alloc_reg();
3662 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
3665 s
= new_stmt(BPF_LD
|BPF_LEN
);
3666 s
->next
= new_stmt(BPF_ST
);
3667 s
->next
->s
.k
= regno
;
3682 a
= (struct arth
*)newchunk(sizeof(*a
));
3686 s
= new_stmt(BPF_LD
|BPF_IMM
);
3688 s
->next
= new_stmt(BPF_ST
);
3704 s
= new_stmt(BPF_ALU
|BPF_NEG
);
3707 s
= new_stmt(BPF_ST
);
3715 gen_arth(code
, a0
, a1
)
3717 struct arth
*a0
, *a1
;
3719 struct slist
*s0
, *s1
, *s2
;
3723 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
3728 sappend(a0
->s
, a1
->s
);
3730 free_reg(a0
->regno
);
3731 free_reg(a1
->regno
);
3733 s0
= new_stmt(BPF_ST
);
3734 a0
->regno
= s0
->s
.k
= alloc_reg();
3741 * Here we handle simple allocation of the scratch registers.
3742 * If too many registers are alloc'd, the allocator punts.
3744 static int regused
[BPF_MEMWORDS
];
3748 * Return the next free register.
3753 int n
= BPF_MEMWORDS
;
3756 if (regused
[curreg
])
3757 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
3759 regused
[curreg
] = 1;
3763 bpf_error("too many registers needed to evaluate expression");
3768 * Return a register to the table so it can
3778 static struct block
*
3785 s
= new_stmt(BPF_LD
|BPF_LEN
);
3786 b
= new_block(JMP(jmp
));
3797 return gen_len(BPF_JGE
, n
);
3801 * Actually, this is less than or equal.
3809 b
= gen_len(BPF_JGT
, n
);
3816 gen_byteop(op
, idx
, val
)
3827 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3830 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3831 b
->s
.code
= JMP(BPF_JGE
);
3836 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3837 b
->s
.code
= JMP(BPF_JGT
);
3841 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3845 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3849 b
= new_block(JMP(BPF_JEQ
));
3856 static u_char abroadcast
[] = { 0x0 };
3859 gen_broadcast(proto
)
3862 bpf_u_int32 hostmask
;
3863 struct block
*b0
, *b1
, *b2
;
3864 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3870 if (linktype
== DLT_ARCNET
)
3871 return gen_ahostop(abroadcast
, Q_DST
);
3872 if (linktype
== DLT_EN10MB
)
3873 return gen_ehostop(ebroadcast
, Q_DST
);
3874 if (linktype
== DLT_FDDI
)
3875 return gen_fhostop(ebroadcast
, Q_DST
);
3876 if (linktype
== DLT_IEEE802
)
3877 return gen_thostop(ebroadcast
, Q_DST
);
3878 bpf_error("not a broadcast link");
3882 b0
= gen_linktype(ETHERTYPE_IP
);
3883 hostmask
= ~netmask
;
3884 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3885 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3886 (bpf_int32
)(~0 & hostmask
), hostmask
);
3891 bpf_error("only ether/ip broadcast filters supported");
3895 gen_multicast(proto
)
3898 register struct block
*b0
, *b1
;
3899 register struct slist
*s
;
3905 if (linktype
== DLT_ARCNET
)
3906 /* all ARCnet multicasts use the same address */
3907 return gen_ahostop(abroadcast
, Q_DST
);
3909 if (linktype
== DLT_EN10MB
) {
3910 /* ether[0] & 1 != 0 */
3911 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3913 b0
= new_block(JMP(BPF_JSET
));
3919 if (linktype
== DLT_FDDI
) {
3920 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3921 /* fddi[1] & 1 != 0 */
3922 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3924 b0
= new_block(JMP(BPF_JSET
));
3930 /* TODO - check how token ring handles multicast */
3931 /* if (linktype == DLT_IEEE802) ... */
3933 /* Link not known to support multicasts */
3937 b0
= gen_linktype(ETHERTYPE_IP
);
3938 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3939 b1
->s
.code
= JMP(BPF_JGE
);
3945 b0
= gen_linktype(ETHERTYPE_IPV6
);
3946 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3951 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3955 * generate command for inbound/outbound. It's here so we can
3956 * make it link-type specific. 'dir' = 0 implies "inbound",
3957 * = 1 implies "outbound".
3963 register struct block
*b0
;
3966 * Only some data link types support inbound/outbound qualifiers.
3971 b0
= gen_relation(BPF_JEQ
,
3972 gen_load(Q_LINK
, gen_loadi(0), 1),
3978 bpf_error("inbound/outbound not supported on linktype %d\n",
3988 register const u_char
*eaddr
;
3991 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3992 if (linktype
== DLT_ARCNET
)
3993 return gen_ahostop(eaddr
, (int)q
.dir
);
3995 bpf_error("ARCnet address used in non-arc expression");
3999 static struct block
*
4000 gen_ahostop(eaddr
, dir
)
4001 register const u_char
*eaddr
;
4004 register struct block
*b0
, *b1
;
4007 /* src comes first, different from Ethernet */
4009 return gen_bcmp(0, 1, eaddr
);
4012 return gen_bcmp(1, 1, eaddr
);
4015 b0
= gen_ahostop(eaddr
, Q_SRC
);
4016 b1
= gen_ahostop(eaddr
, Q_DST
);
4022 b0
= gen_ahostop(eaddr
, Q_SRC
);
4023 b1
= gen_ahostop(eaddr
, Q_DST
);
4032 * support IEEE 802.1Q VLAN trunk over ethernet
4041 * Change the offsets to point to the type and data fields within
4042 * the VLAN packet. This is somewhat of a kludge.
4044 if (orig_nl
== (u_int
)-1) {
4045 orig_linktype
= off_linktype
; /* save original values */
4047 orig_nl_nosnap
= off_nl_nosnap
;
4058 bpf_error("no VLAN support for data link type %d",
4064 /* check for VLAN */
4065 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
4067 /* If a specific VLAN is requested, check VLAN id */
4068 if (vlan_num
>= 0) {
4071 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);