]>
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.170 2002-08-01 08:33:01 risso Exp $ (LBL)";
31 #include <pcap-stdinc.h>
38 #include <sys/param.h>
41 struct mbuf
; /* Squelch compiler warnings on some platforms for */
42 struct rtentry
; /* declarations in <net/if.h> */
53 #include "ethertype.h"
58 #include "sunatmpos.h"
64 #include <pcap-namedb.h>
69 #define IPPROTO_SCTP 132
72 #ifdef HAVE_OS_PROTO_H
76 #define JMP(c) ((c)|BPF_JMP|BPF_K)
79 static jmp_buf top_ctx
;
80 static pcap_t
*bpf_pcap
;
82 /* Hack for updating VLAN offsets. */
83 static u_int orig_linktype
= -1, orig_nl
= -1, orig_nl_nosnap
= -1;
87 int pcap_fddipad
= PCAP_FDDIPAD
;
94 bpf_error(const char *fmt
, ...)
100 if (bpf_pcap
!= NULL
)
101 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
108 static void init_linktype(int);
110 static int alloc_reg(void);
111 static void free_reg(int);
113 static struct block
*root
;
116 * We divy out chunks of memory rather than call malloc each time so
117 * we don't have to worry about leaking memory. It's probably
118 * not a big deal if all this memory was wasted but it this ever
119 * goes into a library that would probably not be a good idea.
122 #define CHUNK0SIZE 1024
128 static struct chunk chunks
[NCHUNKS
];
129 static int cur_chunk
;
131 static void *newchunk(u_int
);
132 static void freechunks(void);
133 static inline struct block
*new_block(int);
134 static inline struct slist
*new_stmt(int);
135 static struct block
*gen_retblk(int);
136 static inline void syntax(void);
138 static void backpatch(struct block
*, struct block
*);
139 static void merge(struct block
*, struct block
*);
140 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
141 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
142 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
143 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
144 static struct block
*gen_ncmp(bpf_u_int32
, bpf_u_int32
, bpf_u_int32
,
145 bpf_u_int32
, bpf_u_int32
, int);
146 static struct block
*gen_uncond(int);
147 static inline struct block
*gen_true(void);
148 static inline struct block
*gen_false(void);
149 static struct block
*gen_linktype(int);
150 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
151 static struct block
*gen_llc(int);
152 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
154 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
156 static struct block
*gen_ahostop(const u_char
*, int);
157 static struct block
*gen_ehostop(const u_char
*, int);
158 static struct block
*gen_fhostop(const u_char
*, int);
159 static struct block
*gen_thostop(const u_char
*, int);
160 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
161 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
163 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
166 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
168 static struct block
*gen_ipfrag(void);
169 static struct block
*gen_portatom(int, bpf_int32
);
171 static struct block
*gen_portatom6(int, bpf_int32
);
173 struct block
*gen_portop(int, int, int);
174 static struct block
*gen_port(int, int, int);
176 struct block
*gen_portop6(int, int, int);
177 static struct block
*gen_port6(int, int, int);
179 static int lookup_proto(const char *, int);
180 static struct block
*gen_protochain(int, int, int);
181 static struct block
*gen_proto(int, int, int);
182 static struct slist
*xfer_to_x(struct arth
*);
183 static struct slist
*xfer_to_a(struct arth
*);
184 static struct block
*gen_len(int, int);
186 static struct block
*gen_msg_abbrev(int type
);
196 /* XXX Round up to nearest long. */
197 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
199 /* XXX Round up to structure boundary. */
203 cp
= &chunks
[cur_chunk
];
204 if (n
> cp
->n_left
) {
205 ++cp
, k
= ++cur_chunk
;
207 bpf_error("out of memory");
208 size
= CHUNK0SIZE
<< k
;
209 cp
->m
= (void *)malloc(size
);
210 memset((char *)cp
->m
, 0, size
);
213 bpf_error("out of memory");
216 return (void *)((char *)cp
->m
+ cp
->n_left
);
225 for (i
= 0; i
< NCHUNKS
; ++i
)
226 if (chunks
[i
].m
!= NULL
) {
233 * A strdup whose allocations are freed after code generation is over.
237 register const char *s
;
239 int n
= strlen(s
) + 1;
240 char *cp
= newchunk(n
);
246 static inline struct block
*
252 p
= (struct block
*)newchunk(sizeof(*p
));
259 static inline struct slist
*
265 p
= (struct slist
*)newchunk(sizeof(*p
));
271 static struct block
*
275 struct block
*b
= new_block(BPF_RET
|BPF_K
);
284 bpf_error("syntax error in filter expression");
287 static bpf_u_int32 netmask
;
292 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
293 char *buf
, int optimize
, bpf_u_int32 mask
)
302 if (setjmp(top_ctx
)) {
310 snaplen
= pcap_snapshot(p
);
312 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
313 "snaplen of 0 rejects all packets");
317 lex_init(buf
? buf
: "");
318 init_linktype(pcap_datalink(p
));
325 root
= gen_retblk(snaplen
);
327 if (optimize
&& !no_optimize
) {
330 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
331 bpf_error("expression rejects all packets");
333 program
->bf_insns
= icode_to_fcode(root
, &len
);
334 program
->bf_len
= len
;
342 * entry point for using the compiler with no pcap open
343 * pass in all the stuff that is needed explicitly instead.
346 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
347 struct bpf_program
*program
,
348 char *buf
, int optimize
, bpf_u_int32 mask
)
353 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
356 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
362 * Clean up a "struct bpf_program" by freeing all the memory allocated
366 pcap_freecode(struct bpf_program
*program
)
369 if (program
->bf_insns
!= NULL
) {
370 free((char *)program
->bf_insns
);
371 program
->bf_insns
= NULL
;
376 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
377 * which of the jt and jf fields has been resolved and which is a pointer
378 * back to another unresolved block (or nil). At least one of the fields
379 * in each block is already resolved.
382 backpatch(list
, target
)
383 struct block
*list
, *target
;
400 * Merge the lists in b0 and b1, using the 'sense' field to indicate
401 * which of jt and jf is the link.
405 struct block
*b0
, *b1
;
407 register struct block
**p
= &b0
;
409 /* Find end of list. */
411 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
413 /* Concatenate the lists. */
421 backpatch(p
, gen_retblk(snaplen
));
422 p
->sense
= !p
->sense
;
423 backpatch(p
, gen_retblk(0));
429 struct block
*b0
, *b1
;
431 backpatch(b0
, b1
->head
);
432 b0
->sense
= !b0
->sense
;
433 b1
->sense
= !b1
->sense
;
435 b1
->sense
= !b1
->sense
;
441 struct block
*b0
, *b1
;
443 b0
->sense
= !b0
->sense
;
444 backpatch(b0
, b1
->head
);
445 b0
->sense
= !b0
->sense
;
454 b
->sense
= !b
->sense
;
457 static struct block
*
458 gen_cmp(offset
, size
, v
)
465 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
468 b
= new_block(JMP(BPF_JEQ
));
475 static struct block
*
476 gen_cmp_gt(offset
, size
, v
)
483 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
486 b
= new_block(JMP(BPF_JGT
));
493 static struct block
*
494 gen_mcmp(offset
, size
, v
, mask
)
499 struct block
*b
= gen_cmp(offset
, size
, v
);
502 if (mask
!= 0xffffffff) {
503 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
510 static struct block
*
511 gen_bcmp(offset
, size
, v
)
512 register u_int offset
, size
;
513 register const u_char
*v
;
515 register struct block
*b
, *tmp
;
519 register const u_char
*p
= &v
[size
- 4];
520 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
521 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
523 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
530 register const u_char
*p
= &v
[size
- 2];
531 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
533 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
540 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
548 static struct block
*
549 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
550 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
556 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
559 if (mask
!= 0xffffffff) {
560 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
564 b
= new_block(JMP(jtype
));
567 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
573 * Various code constructs need to know the layout of the data link
574 * layer. These variables give the necessary offsets.
578 * "off_linktype" is the offset to information in the link-layer header
579 * giving the packet type.
581 * For Ethernet, it's the offset of the Ethernet type field.
583 * For link-layer types that always use 802.2 headers, it's the
584 * offset of the LLC header.
586 * For PPP, it's the offset of the PPP type field.
588 * For Cisco HDLC, it's the offset of the CHDLC type field.
590 * For BSD loopback, it's the offset of the AF_ value.
592 * For Linux cooked sockets, it's the offset of the type field.
594 * It's set to -1 for no encapsulation, in which case, IP is assumed.
596 static u_int off_linktype
;
599 * TRUE if the link layer includes an ATM pseudo-header.
601 static int is_atm
= 0;
604 * These are offsets for the ATM pseudo-header.
606 static u_int off_vpi
;
607 static u_int off_vci
;
608 static u_int off_proto
;
611 * This is the offset to the message type for Q.2931 messages.
613 static u_int off_msg_type
;
616 * These are offsets to the beginning of the network-layer header.
618 * If the link layer never uses 802.2 LLC:
620 * "off_nl" and "off_nl_nosnap" are the same.
622 * If the link layer always uses 802.2 LLC:
624 * "off_nl" is the offset if there's a SNAP header following
627 * "off_nl_nosnap" is the offset if there's no SNAP header.
629 * If the link layer is Ethernet:
631 * "off_nl" is the offset if the packet is an Ethernet II packet
632 * (we assume no 802.3+802.2+SNAP);
634 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
635 * with an 802.2 header following it.
638 static u_int off_nl_nosnap
;
649 * Assume it's not raw ATM with a pseudo-header, for now.
665 off_nl
= 6; /* XXX in reality, variable! */
666 off_nl_nosnap
= 6; /* no 802.2 LLC */
671 off_nl
= 14; /* Ethernet II */
672 off_nl_nosnap
= 17; /* 802.3+802.2 */
677 * SLIP doesn't have a link level type. The 16 byte
678 * header is hacked into our SLIP driver.
682 off_nl_nosnap
= 16; /* no 802.2 LLC */
686 /* XXX this may be the same as the DLT_PPP_BSDOS case */
690 off_nl_nosnap
= 24; /* no 802.2 LLC */
697 off_nl_nosnap
= 4; /* no 802.2 LLC */
701 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
702 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
705 off_nl_nosnap
= 4; /* no 802.2 LLC */
710 * This does no include the Ethernet header, and
711 * only covers session state.
715 off_nl_nosnap
= 8; /* no 802.2 LLC */
721 off_nl_nosnap
= 24; /* no 802.2 LLC */
726 * FDDI doesn't really have a link-level type field.
727 * We set "off_linktype" to the offset of the LLC header.
729 * To check for Ethernet types, we assume that SSAP = SNAP
730 * is being used and pick out the encapsulated Ethernet type.
731 * XXX - should we generate code to check for SNAP?
735 off_linktype
+= pcap_fddipad
;
737 off_nl
= 21; /* FDDI+802.2+SNAP */
738 off_nl_nosnap
= 16; /* FDDI+802.2 */
740 off_nl
+= pcap_fddipad
;
741 off_nl_nosnap
+= pcap_fddipad
;
747 * Token Ring doesn't really have a link-level type field.
748 * We set "off_linktype" to the offset of the LLC header.
750 * To check for Ethernet types, we assume that SSAP = SNAP
751 * is being used and pick out the encapsulated Ethernet type.
752 * XXX - should we generate code to check for SNAP?
754 * XXX - the header is actually variable-length.
755 * Some various Linux patched versions gave 38
756 * as "off_linktype" and 40 as "off_nl"; however,
757 * if a token ring packet has *no* routing
758 * information, i.e. is not source-routed, the correct
759 * values are 20 and 22, as they are in the vanilla code.
761 * A packet is source-routed iff the uppermost bit
762 * of the first byte of the source address, at an
763 * offset of 8, has the uppermost bit set. If the
764 * packet is source-routed, the total number of bytes
765 * of routing information is 2 plus bits 0x1F00 of
766 * the 16-bit value at an offset of 14 (shifted right
767 * 8 - figure out which byte that is).
770 off_nl
= 22; /* Token Ring+802.2+SNAP */
771 off_nl_nosnap
= 17; /* Token Ring+802.2 */
776 * 802.11 doesn't really have a link-level type field.
777 * We set "off_linktype" to the offset of the LLC header.
779 * To check for Ethernet types, we assume that SSAP = SNAP
780 * is being used and pick out the encapsulated Ethernet type.
781 * XXX - should we generate code to check for SNAP?
783 * XXX - the header is actually variable-length. We
784 * assume a 24-byte link-layer header, as appears in
785 * data frames in networks with no bridges.
788 off_nl
= 32; /* 802.11+802.2+SNAP */
789 off_nl_nosnap
= 27; /* 802.22+802.2 */
792 case DLT_PRISM_HEADER
:
794 * Same as 802.11, but with an additional header before
795 * the 802.11 header, containing a bunch of additional
796 * information including radio-level information.
798 * The header is 144 bytes long.
800 * XXX - same variable-length header problem; at least
801 * the Prism header is fixed-length.
803 off_linktype
= 144+24;
804 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
805 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
808 case DLT_ATM_RFC1483
:
809 case DLT_ATM_CLIP
: /* Linux ATM defines this */
811 * assume routed, non-ISO PDUs
812 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
815 off_nl
= 8; /* 802.2+SNAP */
816 off_nl_nosnap
= 3; /* 802.2 */
821 * Full Frontal ATM; you get AALn PDUs with an ATM
825 off_linktype
= SUNATM_PKT_BEGIN_POS
;
826 off_vpi
= SUNATM_VPI_POS
;
827 off_vci
= SUNATM_VCI_POS
;
828 off_proto
= PROTO_POS
;
829 off_msg_type
= SUNATM_PKT_BEGIN_POS
+MSG_TYPE_POS
;
830 off_nl
= SUNATM_PKT_BEGIN_POS
+8; /* 802.2+SNAP */
831 off_nl_nosnap
= SUNATM_PKT_BEGIN_POS
+3; /* 802.2 */
837 off_nl_nosnap
= 0; /* no 802.2 LLC */
840 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
843 off_nl_nosnap
= 16; /* no 802.2 LLC */
848 * LocalTalk does have a 1-byte type field in the LLAP header,
849 * but really it just indicates whether there is a "short" or
850 * "long" DDP packet following.
854 off_nl_nosnap
= 0; /* no 802.2 LLC */
859 * XXX - we should set this to handle SNAP-encapsulated
860 * frames (NLPID of 0x80).
864 off_nl_nosnap
= 0; /* no 802.2 LLC */
867 bpf_error("unknown data link type %d", linktype
);
871 static struct block
*
878 s
= new_stmt(BPF_LD
|BPF_IMM
);
880 b
= new_block(JMP(BPF_JEQ
));
886 static inline struct block
*
889 return gen_uncond(1);
892 static inline struct block
*
895 return gen_uncond(0);
899 * Byte-swap a 32-bit number.
900 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
901 * big-endian platforms.)
903 #define SWAPLONG(y) \
904 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
906 static struct block
*
910 struct block
*b0
, *b1
, *b2
;
919 * OSI protocols always use 802.2 encapsulation.
920 * XXX - should we check both the DSAP and the
921 * SSAP, like this, or should we check just the
924 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
926 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
927 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
933 * NetBEUI always uses 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_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
949 * Ethernet_II frames, which are Ethernet
950 * frames with a frame type of ETHERTYPE_IPX;
952 * Ethernet_802.3 frames, which are 802.3
953 * frames (i.e., the type/length field is
954 * a length field, <= ETHERMTU, rather than
955 * a type field) with the first two bytes
956 * after the Ethernet/802.3 header being
959 * Ethernet_802.2 frames, which are 802.3
960 * frames with an 802.2 LLC header and
961 * with the IPX LSAP as the DSAP in the LLC
964 * Ethernet_SNAP frames, which are 802.3
965 * frames with an LLC header and a SNAP
966 * header and with an OUI of 0x000000
967 * (encapsulated Ethernet) and a protocol
968 * ID of ETHERTYPE_IPX in the SNAP header.
970 * XXX - should we generate the same code both
971 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
975 * This generates code to check both for the
976 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
978 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
979 (bpf_int32
)LLCSAP_IPX
);
980 b1
= gen_cmp(off_linktype
+ 2, BPF_H
,
985 * Now we add code to check for SNAP frames with
986 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
988 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
992 * Now we generate code to check for 802.3
995 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
999 * Now add the check for 802.3 frames before the
1000 * check for Ethernet_802.2 and Ethernet_802.3,
1001 * as those checks should only be done on 802.3
1002 * frames, not on Ethernet frames.
1007 * Now add the check for Ethernet_II frames, and
1008 * do that before checking for the other frame
1011 b0
= gen_cmp(off_linktype
, BPF_H
,
1012 (bpf_int32
)ETHERTYPE_IPX
);
1016 case ETHERTYPE_ATALK
:
1017 case ETHERTYPE_AARP
:
1019 * EtherTalk (AppleTalk protocols on Ethernet link
1020 * layer) may use 802.2 encapsulation.
1024 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1025 * we check for an Ethernet type field less than
1026 * 1500, which means it's an 802.3 length field.
1028 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1032 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1033 * SNAP packets with an organization code of
1034 * 0x080007 (Apple, for Appletalk) and a protocol
1035 * type of ETHERTYPE_ATALK (Appletalk).
1037 * 802.2-encapsulated ETHERTYPE_AARP packets are
1038 * SNAP packets with an organization code of
1039 * 0x000000 (encapsulated Ethernet) and a protocol
1040 * type of ETHERTYPE_AARP (Appletalk ARP).
1042 if (proto
== ETHERTYPE_ATALK
)
1043 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1044 else /* proto == ETHERTYPE_AARP */
1045 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1049 * Check for Ethernet encapsulation (Ethertalk
1050 * phase 1?); we just check for the Ethernet
1053 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1059 if (proto
<= ETHERMTU
) {
1061 * This is an LLC SAP value, so the frames
1062 * that match would be 802.2 frames.
1063 * Check that the frame is an 802.2 frame
1064 * (i.e., that the length/type field is
1065 * a length field, <= ETHERMTU) and
1066 * then check the DSAP.
1068 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1070 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1076 * This is an Ethernet type, so compare
1077 * the length/type field with it (if
1078 * the frame is an 802.2 frame, the length
1079 * field will be <= ETHERMTU, and, as
1080 * "proto" is > ETHERMTU, this test
1081 * will fail and the frame won't match,
1082 * which is what we want).
1084 return gen_cmp(off_linktype
, BPF_H
,
1090 case DLT_IEEE802_11
:
1091 case DLT_PRISM_HEADER
:
1094 case DLT_ATM_RFC1483
:
1096 return gen_llc(proto
);
1101 * Check for LLC encapsulation and then check the protocol.
1102 * XXX - also check for LANE and then check for an Ethernet
1105 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1106 b1
= gen_llc(proto
);
1115 * OSI protocols always use 802.2 encapsulation.
1116 * XXX - should we check both the DSAP and the
1117 * LSAP, like this, or should we check just the
1120 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1121 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1122 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1126 case LLCSAP_NETBEUI
:
1128 * NetBEUI always uses 802.2 encapsulation.
1129 * XXX - should we check both the DSAP and the
1130 * LSAP, like this, or should we check just the
1133 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1134 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1135 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1141 * Ethernet_II frames, which are Ethernet
1142 * frames with a frame type of ETHERTYPE_IPX;
1144 * Ethernet_802.3 frames, which have a frame
1145 * type of LINUX_SLL_P_802_3;
1147 * Ethernet_802.2 frames, which are 802.3
1148 * frames with an 802.2 LLC header (i.e, have
1149 * a frame type of LINUX_SLL_P_802_2) and
1150 * with the IPX LSAP as the DSAP in the LLC
1153 * Ethernet_SNAP frames, which are 802.3
1154 * frames with an LLC header and a SNAP
1155 * header and with an OUI of 0x000000
1156 * (encapsulated Ethernet) and a protocol
1157 * ID of ETHERTYPE_IPX in the SNAP header.
1159 * First, do the checks on LINUX_SLL_P_802_2
1160 * frames; generate the check for either
1161 * Ethernet_802.2 or Ethernet_SNAP frames, and
1162 * then put a check for LINUX_SLL_P_802_2 frames
1165 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1166 (bpf_int32
)LLCSAP_IPX
);
1167 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1170 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1174 * Now check for 802.3 frames and OR that with
1175 * the previous test.
1177 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1181 * Now add the check for Ethernet_II frames, and
1182 * do that before checking for the other frame
1185 b0
= gen_cmp(off_linktype
, BPF_H
,
1186 (bpf_int32
)ETHERTYPE_IPX
);
1190 case ETHERTYPE_ATALK
:
1191 case ETHERTYPE_AARP
:
1193 * EtherTalk (AppleTalk protocols on Ethernet link
1194 * layer) may use 802.2 encapsulation.
1198 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1199 * we check for the 802.2 protocol type in the
1200 * "Ethernet type" field.
1202 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1205 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1206 * SNAP packets with an organization code of
1207 * 0x080007 (Apple, for Appletalk) and a protocol
1208 * type of ETHERTYPE_ATALK (Appletalk).
1210 * 802.2-encapsulated ETHERTYPE_AARP packets are
1211 * SNAP packets with an organization code of
1212 * 0x000000 (encapsulated Ethernet) and a protocol
1213 * type of ETHERTYPE_AARP (Appletalk ARP).
1215 if (proto
== ETHERTYPE_ATALK
)
1216 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1218 else /* proto == ETHERTYPE_AARP */
1219 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1224 * Check for Ethernet encapsulation (Ethertalk
1225 * phase 1?); we just check for the Ethernet
1228 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1234 if (proto
<= ETHERMTU
) {
1236 * This is an LLC SAP value, so the frames
1237 * that match would be 802.2 frames.
1238 * Check for the 802.2 protocol type
1239 * in the "Ethernet type" field, and
1240 * then check the DSAP.
1242 b0
= gen_cmp(off_linktype
, BPF_H
,
1244 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1250 * This is an Ethernet type, so compare
1251 * the length/type field with it (if
1252 * the frame is an 802.2 frame, the length
1253 * field will be <= ETHERMTU, and, as
1254 * "proto" is > ETHERMTU, this test
1255 * will fail and the frame won't match,
1256 * which is what we want).
1258 return gen_cmp(off_linktype
, BPF_H
,
1265 case DLT_SLIP_BSDOS
:
1268 * These types don't provide any type field; packets
1271 * XXX - for IPv4, check for a version number of 4, and,
1272 * for IPv6, check for a version number of 6?
1278 case ETHERTYPE_IPV6
:
1280 return gen_true(); /* always true */
1283 return gen_false(); /* always false */
1288 case DLT_PPP_SERIAL
:
1291 * We use Ethernet protocol types inside libpcap;
1292 * map them to the corresponding PPP protocol types.
1297 proto
= PPP_IP
; /* XXX was 0x21 */
1301 case ETHERTYPE_IPV6
:
1310 case ETHERTYPE_ATALK
:
1324 * I'm assuming the "Bridging PDU"s that go
1325 * over PPP are Spanning Tree Protocol
1339 * We use Ethernet protocol types inside libpcap;
1340 * map them to the corresponding PPP protocol types.
1345 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1346 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1348 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1353 case ETHERTYPE_IPV6
:
1363 case ETHERTYPE_ATALK
:
1377 * I'm assuming the "Bridging PDU"s that go
1378 * over PPP are Spanning Tree Protocol
1393 * For DLT_NULL, the link-layer header is a 32-bit
1394 * word containing an AF_ value in *host* byte order.
1396 * In addition, if we're reading a saved capture file,
1397 * the host byte order in the capture may not be the
1398 * same as the host byte order on this machine.
1400 * For DLT_LOOP, the link-layer header is a 32-bit
1401 * word containing an AF_ value in *network* byte order.
1403 * XXX - AF_ values may, unfortunately, be platform-
1404 * dependent; for example, FreeBSD's AF_INET6 is 24
1405 * whilst NetBSD's and OpenBSD's is 26.
1407 * This means that, when reading a capture file, just
1408 * checking for our AF_INET6 value won't work if the
1409 * capture file came from another OS.
1418 case ETHERTYPE_IPV6
:
1425 * Not a type on which we support filtering.
1426 * XXX - support those that have AF_ values
1427 * #defined on this platform, at least?
1432 if (linktype
== DLT_NULL
) {
1434 * The AF_ value is in host byte order, but
1435 * the BPF interpreter will convert it to
1436 * network byte order.
1438 * If this is a save file, and it's from a
1439 * machine with the opposite byte order to
1440 * ours, we byte-swap the AF_ value.
1442 * Then we run it through "htonl()", and
1443 * generate code to compare against the result.
1445 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1446 bpf_pcap
->sf
.swapped
)
1447 proto
= SWAPLONG(proto
);
1448 proto
= htonl(proto
);
1450 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1454 * XXX should we check for first fragment if the protocol
1461 case ETHERTYPE_IPV6
:
1462 return(gen_cmp(2, BPF_B
,
1463 (bpf_int32
)htonl(ARCTYPE_INET6
)));
1466 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_IP
));
1467 b1
= gen_cmp(2, BPF_B
,
1468 (bpf_int32
)htonl(ARCTYPE_IP_OLD
));
1472 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_ARP
));
1473 b1
= gen_cmp(2, BPF_B
,
1474 (bpf_int32
)htonl(ARCTYPE_ARP_OLD
));
1477 case ETHERTYPE_REVARP
:
1478 return(gen_cmp(2, BPF_B
,
1479 (bpf_int32
)htonl(ARCTYPE_REVARP
)));
1480 case ETHERTYPE_ATALK
:
1481 return(gen_cmp(2, BPF_B
,
1482 (bpf_int32
)htonl(ARCTYPE_ATALK
)));
1488 case ETHERTYPE_ATALK
:
1497 * XXX - assumes a 2-byte Frame Relay header with
1498 * DLCI and flags. What if the address is longer?
1504 * Check for the special NLPID for IP.
1506 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1509 case ETHERTYPE_IPV6
:
1511 * Check for the special NLPID for IPv6.
1513 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1518 * Check for several OSI protocols.
1520 * Frame Relay packets typically have an OSI
1521 * NLPID at the beginning; we check for each
1524 * What we check for is the NLPID and a frame
1525 * control field of UI, i.e. 0x03 followed
1528 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1529 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1530 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1542 * All the types that have no encapsulation should either be
1543 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1544 * all packets are IP packets, or should be handled in some
1545 * special case, if none of them are (if some are and some
1546 * aren't, the lack of encapsulation is a problem, as we'd
1547 * have to find some other way of determining the packet type).
1549 * Therefore, if "off_linktype" is -1, there's an error.
1551 if (off_linktype
== -1)
1555 * Any type not handled above should always have an Ethernet
1556 * type at an offset of "off_linktype". (PPP is partially
1557 * handled above - the protocol type is mapped from the
1558 * Ethernet and LLC types we use internally to the corresponding
1559 * PPP type - but the PPP type is always specified by a value
1560 * at "off_linktype", so we don't have to do the code generation
1563 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1567 * Check for an LLC SNAP packet with a given organization code and
1568 * protocol type; we check the entire contents of the 802.2 LLC and
1569 * snap headers, checking for DSAP and SSAP of SNAP and a control
1570 * field of 0x03 in the LLC header, and for the specified organization
1571 * code and protocol type in the SNAP header.
1573 static struct block
*
1574 gen_snap(orgcode
, ptype
, offset
)
1575 bpf_u_int32 orgcode
;
1579 u_char snapblock
[8];
1581 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1582 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1583 snapblock
[2] = 0x03; /* control = UI */
1584 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1585 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1586 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1587 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1588 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1589 return gen_bcmp(offset
, 8, snapblock
);
1593 * Check for a given protocol value assuming an 802.2 LLC header.
1595 static struct block
*
1600 * XXX - handle token-ring variable-length header.
1605 return gen_cmp(off_linktype
, BPF_H
, (long)
1606 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1608 case LLCSAP_NETBEUI
:
1609 return gen_cmp(off_linktype
, BPF_H
, (long)
1610 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1614 * XXX - are there ever SNAP frames for IPX on
1615 * non-Ethernet 802.x networks?
1617 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1619 case ETHERTYPE_ATALK
:
1621 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1622 * SNAP packets with an organization code of
1623 * 0x080007 (Apple, for Appletalk) and a protocol
1624 * type of ETHERTYPE_ATALK (Appletalk).
1626 * XXX - check for an organization code of
1627 * encapsulated Ethernet as well?
1629 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1633 * XXX - we don't have to check for IPX 802.3
1634 * here, but should we check for the IPX Ethertype?
1636 if (proto
<= ETHERMTU
) {
1638 * This is an LLC SAP value, so check
1641 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1644 * This is an Ethernet type; we assume that it's
1645 * unlikely that it'll appear in the right place
1646 * at random, and therefore check only the
1647 * location that would hold the Ethernet type
1648 * in a SNAP frame with an organization code of
1649 * 0x000000 (encapsulated Ethernet).
1651 * XXX - if we were to check for the SNAP DSAP and
1652 * LSAP, as per XXX, and were also to check for an
1653 * organization code of 0x000000 (encapsulated
1654 * Ethernet), we'd do
1656 * return gen_snap(0x000000, proto,
1659 * here; for now, we don't, as per the above.
1660 * I don't know whether it's worth the extra CPU
1661 * time to do the right check or not.
1663 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
1668 static struct block
*
1669 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1673 u_int src_off
, dst_off
;
1675 struct block
*b0
, *b1
;
1689 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1690 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1696 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1697 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1704 b0
= gen_linktype(proto
);
1705 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1711 static struct block
*
1712 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1713 struct in6_addr
*addr
;
1714 struct in6_addr
*mask
;
1716 u_int src_off
, dst_off
;
1718 struct block
*b0
, *b1
;
1733 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1734 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1740 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1741 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1748 /* this order is important */
1749 a
= (u_int32_t
*)addr
;
1750 m
= (u_int32_t
*)mask
;
1751 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1752 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1754 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1756 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1758 b0
= gen_linktype(proto
);
1764 static struct block
*
1765 gen_ehostop(eaddr
, dir
)
1766 register const u_char
*eaddr
;
1769 register struct block
*b0
, *b1
;
1773 return gen_bcmp(6, 6, eaddr
);
1776 return gen_bcmp(0, 6, eaddr
);
1779 b0
= gen_ehostop(eaddr
, Q_SRC
);
1780 b1
= gen_ehostop(eaddr
, Q_DST
);
1786 b0
= gen_ehostop(eaddr
, Q_SRC
);
1787 b1
= gen_ehostop(eaddr
, Q_DST
);
1796 * Like gen_ehostop, but for DLT_FDDI
1798 static struct block
*
1799 gen_fhostop(eaddr
, dir
)
1800 register const u_char
*eaddr
;
1803 struct block
*b0
, *b1
;
1808 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1810 return gen_bcmp(6 + 1, 6, eaddr
);
1815 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1817 return gen_bcmp(0 + 1, 6, eaddr
);
1821 b0
= gen_fhostop(eaddr
, Q_SRC
);
1822 b1
= gen_fhostop(eaddr
, Q_DST
);
1828 b0
= gen_fhostop(eaddr
, Q_SRC
);
1829 b1
= gen_fhostop(eaddr
, Q_DST
);
1838 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1840 static struct block
*
1841 gen_thostop(eaddr
, dir
)
1842 register const u_char
*eaddr
;
1845 register struct block
*b0
, *b1
;
1849 return gen_bcmp(8, 6, eaddr
);
1852 return gen_bcmp(2, 6, eaddr
);
1855 b0
= gen_thostop(eaddr
, Q_SRC
);
1856 b1
= gen_thostop(eaddr
, Q_DST
);
1862 b0
= gen_thostop(eaddr
, Q_SRC
);
1863 b1
= gen_thostop(eaddr
, Q_DST
);
1872 * This is quite tricky because there may be pad bytes in front of the
1873 * DECNET header, and then there are two possible data packet formats that
1874 * carry both src and dst addresses, plus 5 packet types in a format that
1875 * carries only the src node, plus 2 types that use a different format and
1876 * also carry just the src node.
1880 * Instead of doing those all right, we just look for data packets with
1881 * 0 or 1 bytes of padding. If you want to look at other packets, that
1882 * will require a lot more hacking.
1884 * To add support for filtering on DECNET "areas" (network numbers)
1885 * one would want to add a "mask" argument to this routine. That would
1886 * make the filter even more inefficient, although one could be clever
1887 * and not generate masking instructions if the mask is 0xFFFF.
1889 static struct block
*
1890 gen_dnhostop(addr
, dir
, base_off
)
1895 struct block
*b0
, *b1
, *b2
, *tmp
;
1896 u_int offset_lh
; /* offset if long header is received */
1897 u_int offset_sh
; /* offset if short header is received */
1902 offset_sh
= 1; /* follows flags */
1903 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1907 offset_sh
= 3; /* follows flags, dstnode */
1908 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1912 /* Inefficient because we do our Calvinball dance twice */
1913 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1914 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1920 /* Inefficient because we do our Calvinball dance twice */
1921 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1922 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1927 bpf_error("ISO host filtering not implemented");
1932 b0
= gen_linktype(ETHERTYPE_DN
);
1933 /* Check for pad = 1, long header case */
1934 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1935 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1936 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1937 BPF_H
, (bpf_int32
)ntohs(addr
));
1939 /* Check for pad = 0, long header case */
1940 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1941 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1944 /* Check for pad = 1, short header case */
1945 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1946 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1947 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1948 BPF_H
, (bpf_int32
)ntohs(addr
));
1951 /* Check for pad = 0, short header case */
1952 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1953 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1957 /* Combine with test for linktype */
1962 static struct block
*
1963 gen_host(addr
, mask
, proto
, dir
)
1969 struct block
*b0
, *b1
;
1974 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1975 if (off_linktype
!= -1) {
1976 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1978 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1984 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1985 off_nl
+ 12, off_nl
+ 16);
1988 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1989 off_nl
+ 14, off_nl
+ 24);
1992 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1993 off_nl
+ 14, off_nl
+ 24);
1996 bpf_error("'tcp' modifier applied to host");
1999 bpf_error("'sctp' modifier applied to host");
2002 bpf_error("'udp' modifier applied to host");
2005 bpf_error("'icmp' modifier applied to host");
2008 bpf_error("'igmp' modifier applied to host");
2011 bpf_error("'igrp' modifier applied to host");
2014 bpf_error("'pim' modifier applied to host");
2017 bpf_error("'vrrp' modifier applied to host");
2020 bpf_error("ATALK host filtering not implemented");
2023 bpf_error("AARP host filtering not implemented");
2026 return gen_dnhostop(addr
, dir
, off_nl
);
2029 bpf_error("SCA host filtering not implemented");
2032 bpf_error("LAT host filtering not implemented");
2035 bpf_error("MOPDL host filtering not implemented");
2038 bpf_error("MOPRC host filtering not implemented");
2042 bpf_error("'ip6' modifier applied to ip host");
2045 bpf_error("'icmp6' modifier applied to host");
2049 bpf_error("'ah' modifier applied to host");
2052 bpf_error("'esp' modifier applied to host");
2055 bpf_error("ISO host filtering not implemented");
2058 bpf_error("'esis' modifier applied to host");
2061 bpf_error("'isis' modifier applied to host");
2064 bpf_error("'clnp' modifier applied to host");
2067 bpf_error("'stp' modifier applied to host");
2070 bpf_error("IPX host filtering not implemented");
2073 bpf_error("'netbeui' modifier applied to host");
2082 static struct block
*
2083 gen_host6(addr
, mask
, proto
, dir
)
2084 struct in6_addr
*addr
;
2085 struct in6_addr
*mask
;
2092 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2095 bpf_error("'ip' modifier applied to ip6 host");
2098 bpf_error("'rarp' modifier applied to ip6 host");
2101 bpf_error("'arp' modifier applied to ip6 host");
2104 bpf_error("'sctp' modifier applied to host");
2107 bpf_error("'tcp' modifier applied to host");
2110 bpf_error("'udp' modifier applied to host");
2113 bpf_error("'icmp' modifier applied to host");
2116 bpf_error("'igmp' modifier applied to host");
2119 bpf_error("'igrp' modifier applied to host");
2122 bpf_error("'pim' modifier applied to host");
2125 bpf_error("'vrrp' modifier applied to host");
2128 bpf_error("ATALK host filtering not implemented");
2131 bpf_error("AARP host filtering not implemented");
2134 bpf_error("'decnet' modifier applied to ip6 host");
2137 bpf_error("SCA host filtering not implemented");
2140 bpf_error("LAT host filtering not implemented");
2143 bpf_error("MOPDL host filtering not implemented");
2146 bpf_error("MOPRC host filtering not implemented");
2149 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2150 off_nl
+ 8, off_nl
+ 24);
2153 bpf_error("'icmp6' modifier applied to host");
2156 bpf_error("'ah' modifier applied to host");
2159 bpf_error("'esp' modifier applied to host");
2162 bpf_error("ISO host filtering not implemented");
2165 bpf_error("'esis' modifier applied to host");
2168 bpf_error("'isis' modifier applied to host");
2171 bpf_error("'clnp' modifier applied to host");
2174 bpf_error("'stp' modifier applied to host");
2177 bpf_error("IPX host filtering not implemented");
2180 bpf_error("'netbeui' modifier applied to host");
2190 static struct block
*
2191 gen_gateway(eaddr
, alist
, proto
, dir
)
2192 const u_char
*eaddr
;
2193 bpf_u_int32
**alist
;
2197 struct block
*b0
, *b1
, *tmp
;
2200 bpf_error("direction applied to 'gateway'");
2207 if (linktype
== DLT_EN10MB
)
2208 b0
= gen_ehostop(eaddr
, Q_OR
);
2209 else if (linktype
== DLT_FDDI
)
2210 b0
= gen_fhostop(eaddr
, Q_OR
);
2211 else if (linktype
== DLT_IEEE802
)
2212 b0
= gen_thostop(eaddr
, Q_OR
);
2215 "'gateway' supported only on ethernet, FDDI or token ring");
2217 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2219 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2227 bpf_error("illegal modifier of 'gateway'");
2233 gen_proto_abbrev(proto
)
2244 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2246 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2252 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2254 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2260 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2262 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2268 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2271 #ifndef IPPROTO_IGMP
2272 #define IPPROTO_IGMP 2
2276 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2279 #ifndef IPPROTO_IGRP
2280 #define IPPROTO_IGRP 9
2283 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2287 #define IPPROTO_PIM 103
2291 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2293 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2298 #ifndef IPPROTO_VRRP
2299 #define IPPROTO_VRRP 112
2303 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2307 b1
= gen_linktype(ETHERTYPE_IP
);
2311 b1
= gen_linktype(ETHERTYPE_ARP
);
2315 b1
= gen_linktype(ETHERTYPE_REVARP
);
2319 bpf_error("link layer applied in wrong context");
2322 b1
= gen_linktype(ETHERTYPE_ATALK
);
2326 b1
= gen_linktype(ETHERTYPE_AARP
);
2330 b1
= gen_linktype(ETHERTYPE_DN
);
2334 b1
= gen_linktype(ETHERTYPE_SCA
);
2338 b1
= gen_linktype(ETHERTYPE_LAT
);
2342 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2346 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2351 b1
= gen_linktype(ETHERTYPE_IPV6
);
2354 #ifndef IPPROTO_ICMPV6
2355 #define IPPROTO_ICMPV6 58
2358 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2363 #define IPPROTO_AH 51
2366 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2368 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2374 #define IPPROTO_ESP 50
2377 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2379 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2385 b1
= gen_linktype(LLCSAP_ISONS
);
2389 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2393 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2397 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2401 b1
= gen_linktype(LLCSAP_8021D
);
2405 b1
= gen_linktype(LLCSAP_IPX
);
2409 b1
= gen_linktype(LLCSAP_NETBEUI
);
2418 static struct block
*
2425 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2426 s
->s
.k
= off_nl
+ 6;
2427 b
= new_block(JMP(BPF_JSET
));
2435 static struct block
*
2436 gen_portatom(off
, v
)
2443 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2446 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2447 s
->next
->s
.k
= off_nl
+ off
;
2449 b
= new_block(JMP(BPF_JEQ
));
2457 static struct block
*
2458 gen_portatom6(off
, v
)
2462 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2467 gen_portop(port
, proto
, dir
)
2468 int port
, proto
, dir
;
2470 struct block
*b0
, *b1
, *tmp
;
2472 /* ip proto 'proto' */
2473 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2479 b1
= gen_portatom(0, (bpf_int32
)port
);
2483 b1
= gen_portatom(2, (bpf_int32
)port
);
2488 tmp
= gen_portatom(0, (bpf_int32
)port
);
2489 b1
= gen_portatom(2, (bpf_int32
)port
);
2494 tmp
= gen_portatom(0, (bpf_int32
)port
);
2495 b1
= gen_portatom(2, (bpf_int32
)port
);
2507 static struct block
*
2508 gen_port(port
, ip_proto
, dir
)
2513 struct block
*b0
, *b1
, *tmp
;
2515 /* ether proto ip */
2516 b0
= gen_linktype(ETHERTYPE_IP
);
2522 b1
= gen_portop(port
, ip_proto
, dir
);
2526 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2527 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2529 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
2542 gen_portop6(port
, proto
, dir
)
2543 int port
, proto
, dir
;
2545 struct block
*b0
, *b1
, *tmp
;
2547 /* ip proto 'proto' */
2548 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2552 b1
= gen_portatom6(0, (bpf_int32
)port
);
2556 b1
= gen_portatom6(2, (bpf_int32
)port
);
2561 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2562 b1
= gen_portatom6(2, (bpf_int32
)port
);
2567 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2568 b1
= gen_portatom6(2, (bpf_int32
)port
);
2580 static struct block
*
2581 gen_port6(port
, ip_proto
, dir
)
2586 struct block
*b0
, *b1
, *tmp
;
2588 /* ether proto ip */
2589 b0
= gen_linktype(ETHERTYPE_IPV6
);
2595 b1
= gen_portop6(port
, ip_proto
, dir
);
2599 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2600 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2602 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
2615 lookup_proto(name
, proto
)
2616 register const char *name
;
2626 v
= pcap_nametoproto(name
);
2627 if (v
== PROTO_UNDEF
)
2628 bpf_error("unknown ip proto '%s'", name
);
2632 /* XXX should look up h/w protocol type based on linktype */
2633 v
= pcap_nametoeproto(name
);
2634 if (v
== PROTO_UNDEF
)
2635 bpf_error("unknown ether proto '%s'", name
);
2639 if (strcmp(name
, "esis") == 0)
2641 else if (strcmp(name
, "isis") == 0)
2643 else if (strcmp(name
, "clnp") == 0)
2646 bpf_error("unknown osi proto '%s'", name
);
2666 static struct block
*
2667 gen_protochain(v
, proto
, dir
)
2672 #ifdef NO_PROTOCHAIN
2673 return gen_proto(v
, proto
, dir
);
2675 struct block
*b0
, *b
;
2676 struct slist
*s
[100];
2677 int fix2
, fix3
, fix4
, fix5
;
2678 int ahcheck
, again
, end
;
2680 int reg2
= alloc_reg();
2682 memset(s
, 0, sizeof(s
));
2683 fix2
= fix3
= fix4
= fix5
= 0;
2690 b0
= gen_protochain(v
, Q_IP
, dir
);
2691 b
= gen_protochain(v
, Q_IPV6
, dir
);
2695 bpf_error("bad protocol applied for 'protochain'");
2699 no_optimize
= 1; /*this code is not compatible with optimzer yet */
2702 * s[0] is a dummy entry to protect other BPF insn from damaged
2703 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
2704 * hard to find interdependency made by jump table fixup.
2707 s
[i
] = new_stmt(0); /*dummy*/
2712 b0
= gen_linktype(ETHERTYPE_IP
);
2715 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2716 s
[i
]->s
.k
= off_nl
+ 9;
2718 /* X = ip->ip_hl << 2 */
2719 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2725 b0
= gen_linktype(ETHERTYPE_IPV6
);
2727 /* A = ip6->ip_nxt */
2728 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2729 s
[i
]->s
.k
= off_nl
+ 6;
2731 /* X = sizeof(struct ip6_hdr) */
2732 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
2738 bpf_error("unsupported proto to gen_protochain");
2742 /* again: if (A == v) goto end; else fall through; */
2744 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2746 s
[i
]->s
.jt
= NULL
; /*later*/
2747 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2751 #ifndef IPPROTO_NONE
2752 #define IPPROTO_NONE 59
2754 /* if (A == IPPROTO_NONE) goto end */
2755 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2756 s
[i
]->s
.jt
= NULL
; /*later*/
2757 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2758 s
[i
]->s
.k
= IPPROTO_NONE
;
2759 s
[fix5
]->s
.jf
= s
[i
];
2764 if (proto
== Q_IPV6
) {
2765 int v6start
, v6end
, v6advance
, j
;
2768 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
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_HOPOPTS
;
2773 s
[fix2
]->s
.jf
= s
[i
];
2775 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
2776 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2777 s
[i
]->s
.jt
= NULL
; /*later*/
2778 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2779 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
2781 /* if (A == IPPROTO_ROUTING) goto v6advance */
2782 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2783 s
[i
]->s
.jt
= NULL
; /*later*/
2784 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2785 s
[i
]->s
.k
= IPPROTO_ROUTING
;
2787 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
2788 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2789 s
[i
]->s
.jt
= NULL
; /*later*/
2790 s
[i
]->s
.jf
= NULL
; /*later*/
2791 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
2802 * X = X + (P[X + 1] + 1) * 8;
2805 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2807 /* A = P[X + packet head] */
2808 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2812 s
[i
] = new_stmt(BPF_ST
);
2816 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2819 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2823 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2825 /* A = P[X + packet head]; */
2826 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2830 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2834 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2838 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2841 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2845 /* goto again; (must use BPF_JA for backward jump) */
2846 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2847 s
[i
]->s
.k
= again
- i
- 1;
2848 s
[i
- 1]->s
.jf
= s
[i
];
2852 for (j
= v6start
; j
<= v6end
; j
++)
2853 s
[j
]->s
.jt
= s
[v6advance
];
2858 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2860 s
[fix2
]->s
.jf
= s
[i
];
2866 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2867 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2868 s
[i
]->s
.jt
= NULL
; /*later*/
2869 s
[i
]->s
.jf
= NULL
; /*later*/
2870 s
[i
]->s
.k
= IPPROTO_AH
;
2872 s
[fix3
]->s
.jf
= s
[ahcheck
];
2879 * X = X + (P[X + 1] + 2) * 4;
2882 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2884 /* A = P[X + packet head]; */
2885 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2889 s
[i
] = new_stmt(BPF_ST
);
2893 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2896 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2900 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2902 /* A = P[X + packet head] */
2903 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2907 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2911 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2915 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2918 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2922 /* goto again; (must use BPF_JA for backward jump) */
2923 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2924 s
[i
]->s
.k
= again
- i
- 1;
2929 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2931 s
[fix2
]->s
.jt
= s
[end
];
2932 s
[fix4
]->s
.jf
= s
[end
];
2933 s
[fix5
]->s
.jt
= s
[end
];
2940 for (i
= 0; i
< max
- 1; i
++)
2941 s
[i
]->next
= s
[i
+ 1];
2942 s
[max
- 1]->next
= NULL
;
2947 b
= new_block(JMP(BPF_JEQ
));
2948 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2958 static struct block
*
2959 gen_proto(v
, proto
, dir
)
2964 struct block
*b0
, *b1
;
2966 if (dir
!= Q_DEFAULT
)
2967 bpf_error("direction applied to 'proto'");
2972 b0
= gen_proto(v
, Q_IP
, dir
);
2973 b1
= gen_proto(v
, Q_IPV6
, dir
);
2980 b0
= gen_linktype(ETHERTYPE_IP
);
2982 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2984 b1
= gen_protochain(v
, Q_IP
);
2994 * Frame Relay packets typically have an OSI
2995 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
2996 * generates code to check for all the OSI
2997 * NLPIDs, so calling it and then adding a check
2998 * for the particular NLPID for which we're
2999 * looking is bogus, as we can just check for
3002 * What we check for is the NLPID and a frame
3003 * control field value of UI, i.e. 0x03 followed
3006 * XXX - assumes a 2-byte Frame Relay header with
3007 * DLCI and flags. What if the address is longer?
3009 * XXX - what about SNAP-encapsulated frames?
3011 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3015 b0
= gen_linktype(LLCSAP_ISONS
);
3016 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3022 bpf_error("arp does not encapsulate another protocol");
3026 bpf_error("rarp does not encapsulate another protocol");
3030 bpf_error("atalk encapsulation is not specifiable");
3034 bpf_error("decnet encapsulation is not specifiable");
3038 bpf_error("sca does not encapsulate another protocol");
3042 bpf_error("lat does not encapsulate another protocol");
3046 bpf_error("moprc does not encapsulate another protocol");
3050 bpf_error("mopdl does not encapsulate another protocol");
3054 return gen_linktype(v
);
3057 bpf_error("'udp proto' is bogus");
3061 bpf_error("'tcp proto' is bogus");
3065 bpf_error("'sctp proto' is bogus");
3069 bpf_error("'icmp proto' is bogus");
3073 bpf_error("'igmp proto' is bogus");
3077 bpf_error("'igrp proto' is bogus");
3081 bpf_error("'pim proto' is bogus");
3085 bpf_error("'vrrp proto' is bogus");
3090 b0
= gen_linktype(ETHERTYPE_IPV6
);
3092 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3094 b1
= gen_protochain(v
, Q_IPV6
);
3100 bpf_error("'icmp6 proto' is bogus");
3104 bpf_error("'ah proto' is bogus");
3107 bpf_error("'ah proto' is bogus");
3110 bpf_error("'stp proto' is bogus");
3113 bpf_error("'ipx proto' is bogus");
3116 bpf_error("'netbeui proto' is bogus");
3127 register const char *name
;
3130 int proto
= q
.proto
;
3134 bpf_u_int32 mask
, addr
;
3136 bpf_u_int32
**alist
;
3139 struct sockaddr_in
*sin
;
3140 struct sockaddr_in6
*sin6
;
3141 struct addrinfo
*res
, *res0
;
3142 struct in6_addr mask128
;
3144 struct block
*b
, *tmp
;
3145 int port
, real_proto
;
3150 addr
= pcap_nametonetaddr(name
);
3152 bpf_error("unknown network '%s'", name
);
3153 /* Left justify network addr and calculate its network mask */
3155 while (addr
&& (addr
& 0xff000000) == 0) {
3159 return gen_host(addr
, mask
, proto
, dir
);
3163 if (proto
== Q_LINK
) {
3167 eaddr
= pcap_ether_hostton(name
);
3170 "unknown ether host '%s'", name
);
3171 b
= gen_ehostop(eaddr
, dir
);
3176 eaddr
= pcap_ether_hostton(name
);
3179 "unknown FDDI host '%s'", name
);
3180 b
= gen_fhostop(eaddr
, dir
);
3185 eaddr
= pcap_ether_hostton(name
);
3188 "unknown token ring host '%s'", name
);
3189 b
= gen_thostop(eaddr
, dir
);
3195 "only ethernet/FDDI/token ring supports link-level host name");
3198 } else if (proto
== Q_DECNET
) {
3199 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
3201 * I don't think DECNET hosts can be multihomed, so
3202 * there is no need to build up a list of addresses
3204 return (gen_host(dn_addr
, 0, proto
, dir
));
3207 alist
= pcap_nametoaddr(name
);
3208 if (alist
== NULL
|| *alist
== NULL
)
3209 bpf_error("unknown host '%s'", name
);
3211 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
3213 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
3215 tmp
= gen_host(**alist
++, 0xffffffff,
3222 memset(&mask128
, 0xff, sizeof(mask128
));
3223 res0
= res
= pcap_nametoaddrinfo(name
);
3225 bpf_error("unknown host '%s'", name
);
3227 tproto
= tproto6
= proto
;
3228 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
3232 for (res
= res0
; res
; res
= res
->ai_next
) {
3233 switch (res
->ai_family
) {
3235 if (tproto
== Q_IPV6
)
3238 sin
= (struct sockaddr_in
*)
3240 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
3241 0xffffffff, tproto
, dir
);
3244 if (tproto6
== Q_IP
)
3247 sin6
= (struct sockaddr_in6
*)
3249 tmp
= gen_host6(&sin6
->sin6_addr
,
3250 &mask128
, tproto6
, dir
);
3261 bpf_error("unknown host '%s'%s", name
,
3262 (proto
== Q_DEFAULT
)
3264 : " for specified address family");
3271 if (proto
!= Q_DEFAULT
&&
3272 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
3273 bpf_error("illegal qualifier of 'port'");
3274 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
3275 bpf_error("unknown port '%s'", name
);
3276 if (proto
== Q_UDP
) {
3277 if (real_proto
== IPPROTO_TCP
)
3278 bpf_error("port '%s' is tcp", name
);
3279 else if (real_proto
== IPPROTO_SCTP
)
3280 bpf_error("port '%s' is sctp", name
);
3282 /* override PROTO_UNDEF */
3283 real_proto
= IPPROTO_UDP
;
3285 if (proto
== Q_TCP
) {
3286 if (real_proto
== IPPROTO_UDP
)
3287 bpf_error("port '%s' is udp", name
);
3289 else if (real_proto
== IPPROTO_SCTP
)
3290 bpf_error("port '%s' is sctp", name
);
3292 /* override PROTO_UNDEF */
3293 real_proto
= IPPROTO_TCP
;
3295 if (proto
== Q_SCTP
) {
3296 if (real_proto
== IPPROTO_UDP
)
3297 bpf_error("port '%s' is udp", name
);
3299 else if (real_proto
== IPPROTO_TCP
)
3300 bpf_error("port '%s' is tcp", name
);
3302 /* override PROTO_UNDEF */
3303 real_proto
= IPPROTO_SCTP
;
3306 return gen_port(port
, real_proto
, dir
);
3310 b
= gen_port(port
, real_proto
, dir
);
3311 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3318 eaddr
= pcap_ether_hostton(name
);
3320 bpf_error("unknown ether host: %s", name
);
3322 alist
= pcap_nametoaddr(name
);
3323 if (alist
== NULL
|| *alist
== NULL
)
3324 bpf_error("unknown host '%s'", name
);
3325 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
3329 bpf_error("'gateway' not supported in this configuration");
3333 real_proto
= lookup_proto(name
, proto
);
3334 if (real_proto
>= 0)
3335 return gen_proto(real_proto
, proto
, dir
);
3337 bpf_error("unknown protocol: %s", name
);
3340 real_proto
= lookup_proto(name
, proto
);
3341 if (real_proto
>= 0)
3342 return gen_protochain(real_proto
, proto
, dir
);
3344 bpf_error("unknown protocol: %s", name
);
3356 gen_mcode(s1
, s2
, masklen
, q
)
3357 register const char *s1
, *s2
;
3358 register int masklen
;
3361 register int nlen
, mlen
;
3364 nlen
= __pcap_atoin(s1
, &n
);
3365 /* Promote short ipaddr */
3369 mlen
= __pcap_atoin(s2
, &m
);
3370 /* Promote short ipaddr */
3373 bpf_error("non-network bits set in \"%s mask %s\"",
3376 /* Convert mask len to mask */
3378 bpf_error("mask length must be <= 32");
3379 m
= 0xffffffff << (32 - masklen
);
3381 bpf_error("non-network bits set in \"%s/%d\"",
3388 return gen_host(n
, m
, q
.proto
, q
.dir
);
3391 bpf_error("Mask syntax for networks only");
3398 register const char *s
;
3403 int proto
= q
.proto
;
3409 else if (q
.proto
== Q_DECNET
)
3410 vlen
= __pcap_atodn(s
, &v
);
3412 vlen
= __pcap_atoin(s
, &v
);
3419 if (proto
== Q_DECNET
)
3420 return gen_host(v
, 0, proto
, dir
);
3421 else if (proto
== Q_LINK
) {
3422 bpf_error("illegal link layer address");
3425 if (s
== NULL
&& q
.addr
== Q_NET
) {
3426 /* Promote short net number */
3427 while (v
&& (v
& 0xff000000) == 0) {
3432 /* Promote short ipaddr */
3436 return gen_host(v
, mask
, proto
, dir
);
3441 proto
= IPPROTO_UDP
;
3442 else if (proto
== Q_TCP
)
3443 proto
= IPPROTO_TCP
;
3444 else if (proto
== Q_SCTP
)
3445 proto
= IPPROTO_SCTP
;
3446 else if (proto
== Q_DEFAULT
)
3447 proto
= PROTO_UNDEF
;
3449 bpf_error("illegal qualifier of 'port'");
3452 return gen_port((int)v
, proto
, dir
);
3456 b
= gen_port((int)v
, proto
, dir
);
3457 gen_or(gen_port6((int)v
, proto
, dir
), b
);
3463 bpf_error("'gateway' requires a name");
3467 return gen_proto((int)v
, proto
, dir
);
3470 return gen_protochain((int)v
, proto
, dir
);
3485 gen_mcode6(s1
, s2
, masklen
, q
)
3486 register const char *s1
, *s2
;
3487 register int masklen
;
3490 struct addrinfo
*res
;
3491 struct in6_addr
*addr
;
3492 struct in6_addr mask
;
3497 bpf_error("no mask %s supported", s2
);
3499 res
= pcap_nametoaddrinfo(s1
);
3501 bpf_error("invalid ip6 address %s", s1
);
3503 bpf_error("%s resolved to multiple address", s1
);
3504 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3506 if (sizeof(mask
) * 8 < masklen
)
3507 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3508 memset(&mask
, 0, sizeof(mask
));
3509 memset(&mask
, 0xff, masklen
/ 8);
3511 mask
.s6_addr
[masklen
/ 8] =
3512 (0xff << (8 - masklen
% 8)) & 0xff;
3515 a
= (u_int32_t
*)addr
;
3516 m
= (u_int32_t
*)&mask
;
3517 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3518 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3519 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
3527 bpf_error("Mask syntax for networks only");
3531 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
3536 bpf_error("invalid qualifier against IPv6 address");
3544 register const u_char
*eaddr
;
3547 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3548 if (linktype
== DLT_EN10MB
)
3549 return gen_ehostop(eaddr
, (int)q
.dir
);
3550 if (linktype
== DLT_FDDI
)
3551 return gen_fhostop(eaddr
, (int)q
.dir
);
3552 if (linktype
== DLT_IEEE802
)
3553 return gen_thostop(eaddr
, (int)q
.dir
);
3554 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
3556 bpf_error("ethernet address used in non-ether expression");
3562 struct slist
*s0
, *s1
;
3565 * This is definitely not the best way to do this, but the
3566 * lists will rarely get long.
3573 static struct slist
*
3579 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3584 static struct slist
*
3590 s
= new_stmt(BPF_LD
|BPF_MEM
);
3596 gen_load(proto
, index
, size
)
3601 struct slist
*s
, *tmp
;
3603 int regno
= alloc_reg();
3605 free_reg(index
->regno
);
3609 bpf_error("data size must be 1, 2, or 4");
3625 bpf_error("unsupported index operation");
3628 s
= xfer_to_x(index
);
3629 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3631 sappend(index
->s
, s
);
3646 /* XXX Note that we assume a fixed link header here. */
3647 s
= xfer_to_x(index
);
3648 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3651 sappend(index
->s
, s
);
3653 b
= gen_proto_abbrev(proto
);
3655 gen_and(index
->b
, b
);
3667 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3669 sappend(s
, xfer_to_a(index
));
3670 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3671 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3672 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3674 sappend(index
->s
, s
);
3676 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
3678 gen_and(index
->b
, b
);
3680 gen_and(gen_proto_abbrev(Q_IP
), b
);
3686 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
3690 index
->regno
= regno
;
3691 s
= new_stmt(BPF_ST
);
3693 sappend(index
->s
, s
);
3699 gen_relation(code
, a0
, a1
, reversed
)
3701 struct arth
*a0
, *a1
;
3704 struct slist
*s0
, *s1
, *s2
;
3705 struct block
*b
, *tmp
;
3709 if (code
== BPF_JEQ
) {
3710 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
3711 b
= new_block(JMP(code
));
3715 b
= new_block(BPF_JMP
|code
|BPF_X
);
3721 sappend(a0
->s
, a1
->s
);
3725 free_reg(a0
->regno
);
3726 free_reg(a1
->regno
);
3728 /* 'and' together protocol checks */
3731 gen_and(a0
->b
, tmp
= a1
->b
);
3747 int regno
= alloc_reg();
3748 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
3751 s
= new_stmt(BPF_LD
|BPF_LEN
);
3752 s
->next
= new_stmt(BPF_ST
);
3753 s
->next
->s
.k
= regno
;
3768 a
= (struct arth
*)newchunk(sizeof(*a
));
3772 s
= new_stmt(BPF_LD
|BPF_IMM
);
3774 s
->next
= new_stmt(BPF_ST
);
3790 s
= new_stmt(BPF_ALU
|BPF_NEG
);
3793 s
= new_stmt(BPF_ST
);
3801 gen_arth(code
, a0
, a1
)
3803 struct arth
*a0
, *a1
;
3805 struct slist
*s0
, *s1
, *s2
;
3809 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
3814 sappend(a0
->s
, a1
->s
);
3816 free_reg(a0
->regno
);
3817 free_reg(a1
->regno
);
3819 s0
= new_stmt(BPF_ST
);
3820 a0
->regno
= s0
->s
.k
= alloc_reg();
3827 * Here we handle simple allocation of the scratch registers.
3828 * If too many registers are alloc'd, the allocator punts.
3830 static int regused
[BPF_MEMWORDS
];
3834 * Return the next free register.
3839 int n
= BPF_MEMWORDS
;
3842 if (regused
[curreg
])
3843 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
3845 regused
[curreg
] = 1;
3849 bpf_error("too many registers needed to evaluate expression");
3854 * Return a register to the table so it can
3864 static struct block
*
3871 s
= new_stmt(BPF_LD
|BPF_LEN
);
3872 b
= new_block(JMP(jmp
));
3883 return gen_len(BPF_JGE
, n
);
3887 * Actually, this is less than or equal.
3895 b
= gen_len(BPF_JGT
, n
);
3902 gen_byteop(op
, idx
, val
)
3913 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3916 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3917 b
->s
.code
= JMP(BPF_JGE
);
3922 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3923 b
->s
.code
= JMP(BPF_JGT
);
3927 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3931 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3935 b
= new_block(JMP(BPF_JEQ
));
3942 static u_char abroadcast
[] = { 0x0 };
3945 gen_broadcast(proto
)
3948 bpf_u_int32 hostmask
;
3949 struct block
*b0
, *b1
, *b2
;
3950 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3956 if (linktype
== DLT_ARCNET
)
3957 return gen_ahostop(abroadcast
, Q_DST
);
3958 if (linktype
== DLT_EN10MB
)
3959 return gen_ehostop(ebroadcast
, Q_DST
);
3960 if (linktype
== DLT_FDDI
)
3961 return gen_fhostop(ebroadcast
, Q_DST
);
3962 if (linktype
== DLT_IEEE802
)
3963 return gen_thostop(ebroadcast
, Q_DST
);
3964 bpf_error("not a broadcast link");
3968 b0
= gen_linktype(ETHERTYPE_IP
);
3969 hostmask
= ~netmask
;
3970 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3971 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3972 (bpf_int32
)(~0 & hostmask
), hostmask
);
3977 bpf_error("only ether/ip broadcast filters supported");
3981 gen_multicast(proto
)
3984 register struct block
*b0
, *b1
;
3985 register struct slist
*s
;
3991 if (linktype
== DLT_ARCNET
)
3992 /* all ARCnet multicasts use the same address */
3993 return gen_ahostop(abroadcast
, Q_DST
);
3995 if (linktype
== DLT_EN10MB
) {
3996 /* ether[0] & 1 != 0 */
3997 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3999 b0
= new_block(JMP(BPF_JSET
));
4005 if (linktype
== DLT_FDDI
) {
4006 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
4007 /* fddi[1] & 1 != 0 */
4008 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4010 b0
= new_block(JMP(BPF_JSET
));
4016 /* TODO - check how token ring handles multicast */
4017 /* if (linktype == DLT_IEEE802) ... */
4019 /* Link not known to support multicasts */
4023 b0
= gen_linktype(ETHERTYPE_IP
);
4024 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
4025 b1
->s
.code
= JMP(BPF_JGE
);
4031 b0
= gen_linktype(ETHERTYPE_IPV6
);
4032 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
4037 bpf_error("only IP multicast filters supported on ethernet/FDDI");
4041 * generate command for inbound/outbound. It's here so we can
4042 * make it link-type specific. 'dir' = 0 implies "inbound",
4043 * = 1 implies "outbound".
4049 register struct block
*b0
;
4052 * Only some data link types support inbound/outbound qualifiers.
4057 b0
= gen_relation(BPF_JEQ
,
4058 gen_load(Q_LINK
, gen_loadi(0), 1),
4064 bpf_error("inbound/outbound not supported on linktype %d\n",
4074 register const u_char
*eaddr
;
4077 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4078 if (linktype
== DLT_ARCNET
)
4079 return gen_ahostop(eaddr
, (int)q
.dir
);
4081 bpf_error("ARCnet address used in non-arc expression");
4085 static struct block
*
4086 gen_ahostop(eaddr
, dir
)
4087 register const u_char
*eaddr
;
4090 register struct block
*b0
, *b1
;
4093 /* src comes first, different from Ethernet */
4095 return gen_bcmp(0, 1, eaddr
);
4098 return gen_bcmp(1, 1, eaddr
);
4101 b0
= gen_ahostop(eaddr
, Q_SRC
);
4102 b1
= gen_ahostop(eaddr
, Q_DST
);
4108 b0
= gen_ahostop(eaddr
, Q_SRC
);
4109 b1
= gen_ahostop(eaddr
, Q_DST
);
4118 * support IEEE 802.1Q VLAN trunk over ethernet
4127 * Change the offsets to point to the type and data fields within
4128 * the VLAN packet. This is somewhat of a kludge.
4130 if (orig_nl
== (u_int
)-1) {
4131 orig_linktype
= off_linktype
; /* save original values */
4133 orig_nl_nosnap
= off_nl_nosnap
;
4144 bpf_error("no VLAN support for data link type %d",
4150 /* check for VLAN */
4151 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
4153 /* If a specific VLAN is requested, check VLAN id */
4154 if (vlan_num
>= 0) {
4157 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);
4166 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
4178 bpf_error("'vpi' supported only on raw ATM");
4181 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
4182 (u_int
)jvalue
, reverse
);
4187 bpf_error("'vci' supported only on raw ATM");
4190 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
4191 (u_int
)jvalue
, reverse
);
4195 if (off_proto
== -1)
4196 abort(); /* XXX - this isn't on FreeBSD */
4197 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
4198 (u_int
)jvalue
, reverse
);
4202 if (off_msg_type
== -1)
4204 b0
= gen_ncmp(BPF_B
, off_msg_type
, 0xffffffff,
4205 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
4210 bpf_error("'callref' supported only on raw ATM");
4211 if (off_proto
== -1)
4213 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
4214 (u_int
)jvalue
, reverse
);
4224 gen_atmtype_abbrev(type
)
4227 struct block
*b0
, *b1
;
4232 /* Get all packets in Meta signalling Circuit */
4234 bpf_error("'metac' supported only on raw ATM");
4235 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4236 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
4241 /* Get all packets in Broadcast Circuit*/
4243 bpf_error("'bcc' supported only on raw ATM");
4244 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4245 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
4250 /* Get all cells in Segment OAM F4 circuit*/
4252 bpf_error("'oam4sc' supported only on raw ATM");
4253 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4254 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
4259 /* Get all cells in End-to-End OAM F4 Circuit*/
4261 bpf_error("'oam4ec' supported only on raw ATM");
4262 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4263 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
4268 /* Get all packets in connection Signalling Circuit */
4270 bpf_error("'sc' supported only on raw ATM");
4271 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4272 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
4277 /* Get all packets in ILMI Circuit */
4279 bpf_error("'ilmic' supported only on raw ATM");
4280 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4281 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
4286 /* Get all LANE packets */
4288 bpf_error("'lane' supported only on raw ATM");
4289 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
4293 /* Get all LLC-encapsulated packets */
4295 bpf_error("'llc' supported only on raw ATM");
4296 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
4306 static struct block
*
4307 gen_msg_abbrev(type
)
4313 * Q.2931 signalling protocol messages for handling virtual circuits
4314 * establishment and teardown
4319 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
4323 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
4327 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
4331 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
4335 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
4338 case A_RELEASE_DONE
:
4339 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
4349 gen_atmmulti_abbrev(type
)
4352 struct block
*b0
, *b1
;
4358 bpf_error("'oam' supported only on raw ATM");
4359 b1
= gen_atmmulti_abbrev(A_OAMF4
);
4364 bpf_error("'oamf4' supported only on raw ATM");
4366 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
4367 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
4369 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4375 * Get Q.2931 signalling messages for switched
4376 * virtual connection
4379 bpf_error("'connectmsg' supported only on raw ATM");
4380 b0
= gen_msg_abbrev(A_SETUP
);
4381 b1
= gen_msg_abbrev(A_CALLPROCEED
);
4383 b0
= gen_msg_abbrev(A_CONNECT
);
4385 b0
= gen_msg_abbrev(A_CONNECTACK
);
4387 b0
= gen_msg_abbrev(A_RELEASE
);
4389 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
4391 b0
= gen_atmtype_abbrev(A_SC
);
4397 bpf_error("'metaconnect' supported only on raw ATM");
4398 b0
= gen_msg_abbrev(A_SETUP
);
4399 b1
= gen_msg_abbrev(A_CALLPROCEED
);
4401 b0
= gen_msg_abbrev(A_CONNECT
);
4403 b0
= gen_msg_abbrev(A_RELEASE
);
4405 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
4407 b0
= gen_atmtype_abbrev(A_METAC
);