]>
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.153 2001-05-30 01:27:21 fenner Exp $ (LBL)";
31 #include <sys/types.h>
32 #include <sys/socket.h>
35 #include <sys/param.h>
42 #include <netinet/in.h>
52 #include "ethertype.h"
59 #include <pcap-namedb.h>
62 #include <sys/socket.h>
68 #define IPPROTO_SCTP 132
71 #ifdef HAVE_OS_PROTO_H
75 #define JMP(c) ((c)|BPF_JMP|BPF_K)
78 static jmp_buf top_ctx
;
79 static pcap_t
*bpf_pcap
;
83 int pcap_fddipad
= PCAP_FDDIPAD
;
90 bpf_error(const char *fmt
, ...)
97 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
104 static void init_linktype(int);
106 static int alloc_reg(void);
107 static void free_reg(int);
109 static struct block
*root
;
112 * We divy out chunks of memory rather than call malloc each time so
113 * we don't have to worry about leaking memory. It's probably
114 * not a big deal if all this memory was wasted but it this ever
115 * goes into a library that would probably not be a good idea.
118 #define CHUNK0SIZE 1024
124 static struct chunk chunks
[NCHUNKS
];
125 static int cur_chunk
;
127 static void *newchunk(u_int
);
128 static void freechunks(void);
129 static inline struct block
*new_block(int);
130 static inline struct slist
*new_stmt(int);
131 static struct block
*gen_retblk(int);
132 static inline void syntax(void);
134 static void backpatch(struct block
*, struct block
*);
135 static void merge(struct block
*, struct block
*);
136 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
137 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
138 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
139 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
140 static struct block
*gen_uncond(int);
141 static inline struct block
*gen_true(void);
142 static inline struct block
*gen_false(void);
143 static struct block
*gen_linktype(int);
144 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
145 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
147 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
149 static struct block
*gen_ahostop(const u_char
*, int);
150 static struct block
*gen_ehostop(const u_char
*, int);
151 static struct block
*gen_fhostop(const u_char
*, int);
152 static struct block
*gen_thostop(const u_char
*, int);
153 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
154 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
156 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
159 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
161 static struct block
*gen_ipfrag(void);
162 static struct block
*gen_portatom(int, bpf_int32
);
164 static struct block
*gen_portatom6(int, bpf_int32
);
166 struct block
*gen_portop(int, int, int);
167 static struct block
*gen_port(int, int, int);
169 struct block
*gen_portop6(int, int, int);
170 static struct block
*gen_port6(int, int, int);
172 static int lookup_proto(const char *, int);
173 static struct block
*gen_protochain(int, int, int);
174 static struct block
*gen_proto(int, int, int);
175 static struct slist
*xfer_to_x(struct arth
*);
176 static struct slist
*xfer_to_a(struct arth
*);
177 static struct block
*gen_len(int, int);
187 /* XXX Round up to nearest long. */
188 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
190 /* XXX Round up to structure boundary. */
194 cp
= &chunks
[cur_chunk
];
195 if (n
> cp
->n_left
) {
196 ++cp
, k
= ++cur_chunk
;
198 bpf_error("out of memory");
199 size
= CHUNK0SIZE
<< k
;
200 cp
->m
= (void *)malloc(size
);
201 memset((char *)cp
->m
, 0, size
);
204 bpf_error("out of memory");
207 return (void *)((char *)cp
->m
+ cp
->n_left
);
216 for (i
= 0; i
< NCHUNKS
; ++i
)
217 if (chunks
[i
].m
!= NULL
) {
224 * A strdup whose allocations are freed after code generation is over.
228 register const char *s
;
230 int n
= strlen(s
) + 1;
231 char *cp
= newchunk(n
);
237 static inline struct block
*
243 p
= (struct block
*)newchunk(sizeof(*p
));
250 static inline struct slist
*
256 p
= (struct slist
*)newchunk(sizeof(*p
));
262 static struct block
*
266 struct block
*b
= new_block(BPF_RET
|BPF_K
);
275 bpf_error("syntax error in filter expression");
278 static bpf_u_int32 netmask
;
283 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
284 char *buf
, int optimize
, bpf_u_int32 mask
)
293 if (setjmp(top_ctx
)) {
301 snaplen
= pcap_snapshot(p
);
303 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
304 "snaplen of 0 rejects all packets");
308 lex_init(buf
? buf
: "");
309 init_linktype(pcap_datalink(p
));
316 root
= gen_retblk(snaplen
);
318 if (optimize
&& !no_optimize
) {
321 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
322 bpf_error("expression rejects all packets");
324 program
->bf_insns
= icode_to_fcode(root
, &len
);
325 program
->bf_len
= len
;
333 * entry point for using the compiler with no pcap open
334 * pass in all the stuff that is needed explicitly instead.
337 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
338 struct bpf_program
*program
,
339 char *buf
, int optimize
, bpf_u_int32 mask
)
344 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
347 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
353 * Clean up a "struct bpf_program" by freeing all the memory allocated
357 pcap_freecode(struct bpf_program
*program
)
360 if (program
->bf_insns
!= NULL
) {
361 free((char *)program
->bf_insns
);
362 program
->bf_insns
= NULL
;
367 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
368 * which of the jt and jf fields has been resolved and which is a pointer
369 * back to another unresolved block (or nil). At least one of the fields
370 * in each block is already resolved.
373 backpatch(list
, target
)
374 struct block
*list
, *target
;
391 * Merge the lists in b0 and b1, using the 'sense' field to indicate
392 * which of jt and jf is the link.
396 struct block
*b0
, *b1
;
398 register struct block
**p
= &b0
;
400 /* Find end of list. */
402 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
404 /* Concatenate the lists. */
412 backpatch(p
, gen_retblk(snaplen
));
413 p
->sense
= !p
->sense
;
414 backpatch(p
, gen_retblk(0));
420 struct block
*b0
, *b1
;
422 backpatch(b0
, b1
->head
);
423 b0
->sense
= !b0
->sense
;
424 b1
->sense
= !b1
->sense
;
426 b1
->sense
= !b1
->sense
;
432 struct block
*b0
, *b1
;
434 b0
->sense
= !b0
->sense
;
435 backpatch(b0
, b1
->head
);
436 b0
->sense
= !b0
->sense
;
445 b
->sense
= !b
->sense
;
448 static struct block
*
449 gen_cmp(offset
, size
, v
)
456 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
459 b
= new_block(JMP(BPF_JEQ
));
466 static struct block
*
467 gen_cmp_gt(offset
, size
, v
)
474 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
477 b
= new_block(JMP(BPF_JGT
));
484 static struct block
*
485 gen_mcmp(offset
, size
, v
, mask
)
490 struct block
*b
= gen_cmp(offset
, size
, v
);
493 if (mask
!= 0xffffffff) {
494 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
501 static struct block
*
502 gen_bcmp(offset
, size
, v
)
503 register u_int offset
, size
;
504 register const u_char
*v
;
506 register struct block
*b
, *tmp
;
510 register const u_char
*p
= &v
[size
- 4];
511 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
512 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
514 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
521 register const u_char
*p
= &v
[size
- 2];
522 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
524 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
531 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
540 * Various code constructs need to know the layout of the data link
541 * layer. These variables give the necessary offsets. off_linktype
542 * is set to -1 for no encapsulation, in which case, IP is assumed.
544 static u_int off_linktype
;
558 off_nl
= 6; /* XXX in reality, variable! */
568 * SLIP doesn't have a link level type. The 16 byte
569 * header is hacked into our SLIP driver.
576 /* XXX this may be the same as the DLT_PPP_BSDOS case */
589 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
590 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
595 case DLT_PPP_ETHER
: /* NetBSD PPP over Ethernet */
597 * This includes the Ethernet header (since we need
598 * the ethertype to dispatch Session vs. Discovery)
599 * and the PPPoE (RFC 2516) header.
612 * FDDI doesn't really have a link-level type field.
613 * We set "off_linktype" to the offset of the LLC header.
615 * To check for Ethernet types, we assume that SSAP = SNAP
616 * is being used and pick out the encapsulated Ethernet type.
617 * XXX - should we generate code to check for SNAP?
621 off_linktype
+= pcap_fddipad
;
625 off_nl
+= pcap_fddipad
;
631 * Token Ring doesn't really have a link-level type field.
632 * We set "off_linktype" to the offset of the LLC header.
634 * To check for Ethernet types, we assume that SSAP = SNAP
635 * is being used and pick out the encapsulated Ethernet type.
636 * XXX - should we generate code to check for SNAP?
638 * XXX - the header is actually variable-length.
639 * Some various Linux patched versions gave 38
640 * as "off_linktype" and 40 as "off_nl"; however,
641 * if a token ring packet has *no* routing
642 * information, i.e. is not source-routed, the correct
643 * values are 20 and 22, as they are in the vanilla code.
645 * A packet is source-routed iff the uppermost bit
646 * of the first byte of the source address, at an
647 * offset of 8, has the uppermost bit set. If the
648 * packet is source-routed, the total number of bytes
649 * of routing information is 2 plus bits 0x1F00 of
650 * the 16-bit value at an offset of 14 (shifted right
651 * 8 - figure out which byte that is).
657 case DLT_ATM_RFC1483
:
659 * assume routed, non-ISO PDUs
660 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
671 case DLT_ATM_CLIP
: /* Linux ATM defines this */
676 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
681 bpf_error("unknown data link type %d", linktype
);
685 static struct block
*
692 s
= new_stmt(BPF_LD
|BPF_IMM
);
694 b
= new_block(JMP(BPF_JEQ
));
700 static inline struct block
*
703 return gen_uncond(1);
706 static inline struct block
*
709 return gen_uncond(0);
713 * Byte-swap a 32-bit number.
714 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
715 * big-endian platforms.)
717 #define SWAPLONG(y) \
718 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
720 static struct block
*
724 struct block
*b0
, *b1
;
733 * OSI protocols always use 802.2 encapsulation.
734 * XXX - should we check both the DSAP and the
735 * SSAP, like this, or should we check just the
738 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
740 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
741 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
747 * NetBEUI always uses 802.2 encapsulation.
748 * XXX - should we check both the DSAP and the
749 * SSAP, like this, or should we check just the
752 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
754 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
755 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
763 * Ethernet_II frames, which are Ethernet
764 * frames with a frame type of ETHERTYPE_IPX;
766 * Ethernet_802.3 frames, which are 802.3
767 * frames (i.e., the type/length field is
768 * a length field, <= ETHERMTU, rather than
769 * a type field) with the first two bytes
770 * after the Ethernet/802.3 header being
773 * Ethernet_802.2 frames, which are 802.3
774 * frames with an 802.2 LLC header and
775 * with the IPX LSAP as the DSAP in the LLC
778 * Ethernet_SNAP frames, which are 802.3
779 * frames with an LLC header and a SNAP
780 * header and with an OUI of 0x000000
781 * (encapsulated Ethernet) and a protocol
782 * ID of ETHERTYPE_IPX in the SNAP header.
784 * XXX - should we generate the same code both
785 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
789 * This generates code to check both for the
790 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
792 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
793 (bpf_int32
)LLCSAP_IPX
);
794 b1
= gen_cmp(off_linktype
+ 2, BPF_H
,
799 * Now we add code to check for SNAP frames with
800 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
802 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
806 * Now we generate code to check for 802.3
809 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
813 * Now add the check for 802.3 frames before the
814 * check for Ethernet_802.2 and Ethernet_802.3,
815 * as those checks should only be done on 802.3
816 * frames, not on Ethernet frames.
821 * Now add the check for Ethernet_II frames, and
822 * do that before checking for the other frame
825 b0
= gen_cmp(off_linktype
, BPF_H
,
826 (bpf_int32
)ETHERTYPE_IPX
);
830 case ETHERTYPE_ATALK
:
833 * EtherTalk (AppleTalk protocols on Ethernet link
834 * layer) may use 802.2 encapsulation.
838 * Check for 802.2 encapsulation (EtherTalk phase 2?);
839 * we check for an Ethernet type field less than
840 * 1500, which means it's an 802.3 length field.
842 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
846 * 802.2-encapsulated ETHERTYPE_ATALK packets are
847 * SNAP packets with an organization code of
848 * 0x080007 (Apple, for Appletalk) and a protocol
849 * type of ETHERTYPE_ATALK (Appletalk).
851 * 802.2-encapsulated ETHERTYPE_AARP packets are
852 * SNAP packets with an organization code of
853 * 0x000000 (encapsulated Ethernet) and a protocol
854 * type of ETHERTYPE_AARP (Appletalk ARP).
856 if (proto
== ETHERTYPE_ATALK
)
857 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
858 else /* proto == ETHERTYPE_AARP */
859 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
863 * Check for Ethernet encapsulation (Ethertalk
864 * phase 1?); we just check for the Ethernet
867 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
873 if (proto
<= ETHERMTU
) {
875 * This is an LLC SAP value, so the frames
876 * that match would be 802.2 frames.
877 * Check that the frame is an 802.2 frame
878 * (i.e., that the length/type field is
879 * a length field, <= ETHERMTU) and
880 * then check the DSAP.
882 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
884 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
890 * This is an Ethernet type, so compare
891 * the length/type field with it (if
892 * the frame is an 802.2 frame, the length
893 * field will be <= ETHERMTU, and, as
894 * "proto" is > ETHERMTU, this test
895 * will fail and the frame won't match,
896 * which is what we want).
898 return gen_cmp(off_linktype
, BPF_H
,
906 case DLT_ATM_RFC1483
:
909 * XXX - handle token-ring variable-length header.
914 return gen_cmp(off_linktype
, BPF_H
, (long)
915 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
918 return gen_cmp(off_linktype
, BPF_H
, (long)
919 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
923 * XXX - are there ever SNAP frames for IPX on
924 * non-Ethernet 802.x networks?
926 return gen_cmp(off_linktype
, BPF_B
,
927 (bpf_int32
)LLCSAP_IPX
);
929 case ETHERTYPE_ATALK
:
931 * 802.2-encapsulated ETHERTYPE_ATALK packets are
932 * SNAP packets with an organization code of
933 * 0x080007 (Apple, for Appletalk) and a protocol
934 * type of ETHERTYPE_ATALK (Appletalk).
936 * XXX - check for an organization code of
937 * encapsulated Ethernet as well?
939 return gen_snap(0x080007, ETHERTYPE_ATALK
,
945 * XXX - we don't have to check for IPX 802.3
946 * here, but should we check for the IPX Ethertype?
948 if (proto
<= ETHERMTU
) {
950 * This is an LLC SAP value, so check
953 return gen_cmp(off_linktype
, BPF_B
,
957 * This is an Ethernet type; we assume
958 * that it's unlikely that it'll
959 * appear in the right place at random,
960 * and therefore check only the
961 * location that would hold the Ethernet
962 * type in a SNAP frame with an organization
963 * code of 0x000000 (encapsulated Ethernet).
965 * XXX - if we were to check for the SNAP DSAP
966 * and LSAP, as per XXX, and were also to check
967 * for an organization code of 0x000000
968 * (encapsulated Ethernet), we'd do
970 * return gen_snap(0x000000, proto,
973 * here; for now, we don't, as per the above.
974 * I don't know whether it's worth the
975 * extra CPU time to do the right check
978 return gen_cmp(off_linktype
+6, BPF_H
,
989 * OSI protocols always use 802.2 encapsulation.
990 * XXX - should we check both the DSAP and the
991 * LSAP, like this, or should we check just the
994 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
995 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
996 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1000 case LLCSAP_NETBEUI
:
1002 * NetBEUI always uses 802.2 encapsulation.
1003 * XXX - should we check both the DSAP and the
1004 * LSAP, like this, or should we check just the
1007 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1008 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1009 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1015 * Ethernet_II frames, which are Ethernet
1016 * frames with a frame type of ETHERTYPE_IPX;
1018 * Ethernet_802.3 frames, which have a frame
1019 * type of LINUX_SLL_P_802_3;
1021 * Ethernet_802.2 frames, which are 802.3
1022 * frames with an 802.2 LLC header (i.e, have
1023 * a frame type of LINUX_SLL_P_802_2) and
1024 * with the IPX LSAP as the DSAP in the LLC
1027 * Ethernet_SNAP frames, which are 802.3
1028 * frames with an LLC header and a SNAP
1029 * header and with an OUI of 0x000000
1030 * (encapsulated Ethernet) and a protocol
1031 * ID of ETHERTYPE_IPX in the SNAP header.
1033 * First, do the checks on LINUX_SLL_P_802_2
1034 * frames; generate the check for either
1035 * Ethernet_802.2 or Ethernet_SNAP frames, and
1036 * then put a check for LINUX_SLL_P_802_2 frames
1039 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1040 (bpf_int32
)LLCSAP_IPX
);
1041 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1044 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1048 * Now check for 802.3 frames and OR that with
1049 * the previous test.
1051 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1055 * Now add the check for Ethernet_II frames, and
1056 * do that before checking for the other frame
1059 b0
= gen_cmp(off_linktype
, BPF_H
,
1060 (bpf_int32
)ETHERTYPE_IPX
);
1064 case ETHERTYPE_ATALK
:
1065 case ETHERTYPE_AARP
:
1067 * EtherTalk (AppleTalk protocols on Ethernet link
1068 * layer) may use 802.2 encapsulation.
1072 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1073 * we check for the 802.2 protocol type in the
1074 * "Ethernet type" field.
1076 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1079 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1080 * SNAP packets with an organization code of
1081 * 0x080007 (Apple, for Appletalk) and a protocol
1082 * type of ETHERTYPE_ATALK (Appletalk).
1084 * 802.2-encapsulated ETHERTYPE_AARP packets are
1085 * SNAP packets with an organization code of
1086 * 0x000000 (encapsulated Ethernet) and a protocol
1087 * type of ETHERTYPE_AARP (Appletalk ARP).
1089 if (proto
== ETHERTYPE_ATALK
)
1090 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1092 else /* proto == ETHERTYPE_AARP */
1093 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1098 * Check for Ethernet encapsulation (Ethertalk
1099 * phase 1?); we just check for the Ethernet
1102 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1108 if (proto
<= ETHERMTU
) {
1110 * This is an LLC SAP value, so the frames
1111 * that match would be 802.2 frames.
1112 * Check for the 802.2 protocol type
1113 * in the "Ethernet type" field, and
1114 * then check the DSAP.
1116 b0
= gen_cmp(off_linktype
, BPF_H
,
1118 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1124 * This is an Ethernet type, so compare
1125 * the length/type field with it (if
1126 * the frame is an 802.2 frame, the length
1127 * field will be <= ETHERMTU, and, as
1128 * "proto" is > ETHERMTU, this test
1129 * will fail and the frame won't match,
1130 * which is what we want).
1132 return gen_cmp(off_linktype
, BPF_H
,
1139 case DLT_SLIP_BSDOS
:
1142 * These types don't provide any type field; packets
1145 * XXX - for IPv4, check for a version number of 4, and,
1146 * for IPv6, check for a version number of 6?
1152 case ETHERTYPE_IPV6
:
1154 return gen_true(); /* always true */
1157 return gen_false(); /* always false */
1162 case DLT_PPP_SERIAL
:
1164 * We use Ethernet protocol types inside libpcap;
1165 * map them to the corresponding PPP protocol types.
1170 proto
= PPP_IP
; /* XXX was 0x21 */
1174 case ETHERTYPE_IPV6
:
1183 case ETHERTYPE_ATALK
:
1197 * I'm assuming the "Bridging PDU"s that go
1198 * over PPP are Spanning Tree Protocol
1212 * We use Ethernet protocol types inside libpcap;
1213 * map them to the corresponding PPP protocol types.
1218 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1219 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1221 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1226 case ETHERTYPE_IPV6
:
1236 case ETHERTYPE_ATALK
:
1250 * I'm assuming the "Bridging PDU"s that go
1251 * over PPP are Spanning Tree Protocol
1266 * For DLT_NULL, the link-layer header is a 32-bit
1267 * word containing an AF_ value in *host* byte order.
1269 * In addition, if we're reading a saved capture file,
1270 * the host byte order in the capture may not be the
1271 * same as the host byte order on this machine.
1273 * For DLT_LOOP, the link-layer header is a 32-bit
1274 * word containing an AF_ value in *network* byte order.
1276 * XXX - AF_ values may, unfortunately, be platform-
1277 * dependent; for example, FreeBSD's AF_INET6 is 24
1278 * whilst NetBSD's and OpenBSD's is 26.
1280 * This means that, when reading a capture file, just
1281 * checking for our AF_INET6 value won't work if the
1282 * capture file came from another OS.
1291 case ETHERTYPE_IPV6
:
1298 * Not a type on which we support filtering.
1299 * XXX - support those that have AF_ values
1300 * #defined on this platform, at least?
1305 if (linktype
== DLT_NULL
) {
1307 * The AF_ value is in host byte order, but
1308 * the BPF interpreter will convert it to
1309 * network byte order.
1311 * If this is a save file, and it's from a
1312 * machine with the opposite byte order to
1313 * ours, we byte-swap the AF_ value.
1315 * Then we run it through "htonl()", and
1316 * generate code to compare against the result.
1318 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1319 bpf_pcap
->sf
.swapped
)
1320 proto
= SWAPLONG(proto
);
1321 proto
= htonl(proto
);
1323 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1327 * XXX should we check for first fragment if the protocol
1334 case ETHERTYPE_IPV6
:
1335 return(gen_cmp(2, BPF_B
,
1336 (bpf_int32
)htonl(ARCTYPE_INET6
)));
1339 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_IP
));
1340 b1
= gen_cmp(2, BPF_B
,
1341 (bpf_int32
)htonl(ARCTYPE_IP_OLD
));
1345 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_ARP
));
1346 b1
= gen_cmp(2, BPF_B
,
1347 (bpf_int32
)htonl(ARCTYPE_ARP_OLD
));
1350 case ETHERTYPE_REVARP
:
1351 return(gen_cmp(2, BPF_B
,
1352 (bpf_int32
)htonl(ARCTYPE_REVARP
)));
1353 case ETHERTYPE_ATALK
:
1354 return(gen_cmp(2, BPF_B
,
1355 (bpf_int32
)htonl(ARCTYPE_ATALK
)));
1361 * All the types that have no encapsulation should either be
1362 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1363 * all packets are IP packets, or should be handled in some
1364 * special case, if none of them are (if some are and some
1365 * aren't, the lack of encapsulation is a problem, as we'd
1366 * have to find some other way of determining the packet type).
1368 * Therefore, if "off_linktype" is -1, there's an error.
1370 if (off_linktype
== -1)
1374 * Any type not handled above should always have an Ethernet
1375 * type at an offset of "off_linktype". (PPP is partially
1376 * handled above - the protocol type is mapped from the
1377 * Ethernet and LLC types we use internally to the corresponding
1378 * PPP type - but the PPP type is always specified by a value
1379 * at "off_linktype", so we don't have to do the code generation
1382 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1386 * Check for an LLC SNAP packet with a given organization code and
1387 * protocol type; we check the entire contents of the 802.2 LLC and
1388 * snap headers, checking for DSAP and SSAP of SNAP and a control
1389 * field of 0x03 in the LLC header, and for the specified organization
1390 * code and protocol type in the SNAP header.
1392 static struct block
*
1393 gen_snap(orgcode
, ptype
, offset
)
1394 bpf_u_int32 orgcode
;
1398 u_char snapblock
[8];
1400 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1401 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1402 snapblock
[2] = 0x03; /* control = UI */
1403 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1404 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1405 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1406 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1407 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1408 return gen_bcmp(offset
, 8, snapblock
);
1411 static struct block
*
1412 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1416 u_int src_off
, dst_off
;
1418 struct block
*b0
, *b1
;
1432 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1433 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1439 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1440 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1447 b0
= gen_linktype(proto
);
1448 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1454 static struct block
*
1455 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1456 struct in6_addr
*addr
;
1457 struct in6_addr
*mask
;
1459 u_int src_off
, dst_off
;
1461 struct block
*b0
, *b1
;
1476 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1477 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1483 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1484 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1491 /* this order is important */
1492 a
= (u_int32_t
*)addr
;
1493 m
= (u_int32_t
*)mask
;
1494 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1495 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1497 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1499 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1501 b0
= gen_linktype(proto
);
1507 static struct block
*
1508 gen_ehostop(eaddr
, dir
)
1509 register const u_char
*eaddr
;
1512 register struct block
*b0
, *b1
;
1516 return gen_bcmp(6, 6, eaddr
);
1519 return gen_bcmp(0, 6, eaddr
);
1522 b0
= gen_ehostop(eaddr
, Q_SRC
);
1523 b1
= gen_ehostop(eaddr
, Q_DST
);
1529 b0
= gen_ehostop(eaddr
, Q_SRC
);
1530 b1
= gen_ehostop(eaddr
, Q_DST
);
1539 * Like gen_ehostop, but for DLT_FDDI
1541 static struct block
*
1542 gen_fhostop(eaddr
, dir
)
1543 register const u_char
*eaddr
;
1546 struct block
*b0
, *b1
;
1551 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1553 return gen_bcmp(6 + 1, 6, eaddr
);
1558 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1560 return gen_bcmp(0 + 1, 6, eaddr
);
1564 b0
= gen_fhostop(eaddr
, Q_SRC
);
1565 b1
= gen_fhostop(eaddr
, Q_DST
);
1571 b0
= gen_fhostop(eaddr
, Q_SRC
);
1572 b1
= gen_fhostop(eaddr
, Q_DST
);
1581 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1583 static struct block
*
1584 gen_thostop(eaddr
, dir
)
1585 register const u_char
*eaddr
;
1588 register struct block
*b0
, *b1
;
1592 return gen_bcmp(8, 6, eaddr
);
1595 return gen_bcmp(2, 6, eaddr
);
1598 b0
= gen_thostop(eaddr
, Q_SRC
);
1599 b1
= gen_thostop(eaddr
, Q_DST
);
1605 b0
= gen_thostop(eaddr
, Q_SRC
);
1606 b1
= gen_thostop(eaddr
, Q_DST
);
1615 * This is quite tricky because there may be pad bytes in front of the
1616 * DECNET header, and then there are two possible data packet formats that
1617 * carry both src and dst addresses, plus 5 packet types in a format that
1618 * carries only the src node, plus 2 types that use a different format and
1619 * also carry just the src node.
1623 * Instead of doing those all right, we just look for data packets with
1624 * 0 or 1 bytes of padding. If you want to look at other packets, that
1625 * will require a lot more hacking.
1627 * To add support for filtering on DECNET "areas" (network numbers)
1628 * one would want to add a "mask" argument to this routine. That would
1629 * make the filter even more inefficient, although one could be clever
1630 * and not generate masking instructions if the mask is 0xFFFF.
1632 static struct block
*
1633 gen_dnhostop(addr
, dir
, base_off
)
1638 struct block
*b0
, *b1
, *b2
, *tmp
;
1639 u_int offset_lh
; /* offset if long header is received */
1640 u_int offset_sh
; /* offset if short header is received */
1645 offset_sh
= 1; /* follows flags */
1646 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1650 offset_sh
= 3; /* follows flags, dstnode */
1651 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1655 /* Inefficient because we do our Calvinball dance twice */
1656 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1657 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1663 /* Inefficient because we do our Calvinball dance twice */
1664 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1665 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1670 bpf_error("ISO host filtering not implemented");
1675 b0
= gen_linktype(ETHERTYPE_DN
);
1676 /* Check for pad = 1, long header case */
1677 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1678 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1679 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1680 BPF_H
, (bpf_int32
)ntohs(addr
));
1682 /* Check for pad = 0, long header case */
1683 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1684 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1687 /* Check for pad = 1, short header case */
1688 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1689 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1690 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1691 BPF_H
, (bpf_int32
)ntohs(addr
));
1694 /* Check for pad = 0, short header case */
1695 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1696 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1700 /* Combine with test for linktype */
1705 static struct block
*
1706 gen_host(addr
, mask
, proto
, dir
)
1712 struct block
*b0
, *b1
;
1717 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1718 if (off_linktype
!= -1) {
1719 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1721 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1727 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1728 off_nl
+ 12, off_nl
+ 16);
1731 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1732 off_nl
+ 14, off_nl
+ 24);
1735 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1736 off_nl
+ 14, off_nl
+ 24);
1739 bpf_error("'tcp' modifier applied to host");
1742 bpf_error("'sctp' modifier applied to host");
1745 bpf_error("'udp' modifier applied to host");
1748 bpf_error("'icmp' modifier applied to host");
1751 bpf_error("'igmp' modifier applied to host");
1754 bpf_error("'igrp' modifier applied to host");
1757 bpf_error("'pim' modifier applied to host");
1760 bpf_error("'vrrp' modifier applied to host");
1763 bpf_error("ATALK host filtering not implemented");
1766 bpf_error("AARP host filtering not implemented");
1769 return gen_dnhostop(addr
, dir
, off_nl
);
1772 bpf_error("SCA host filtering not implemented");
1775 bpf_error("LAT host filtering not implemented");
1778 bpf_error("MOPDL host filtering not implemented");
1781 bpf_error("MOPRC host filtering not implemented");
1785 bpf_error("'ip6' modifier applied to ip host");
1788 bpf_error("'icmp6' modifier applied to host");
1792 bpf_error("'ah' modifier applied to host");
1795 bpf_error("'esp' modifier applied to host");
1798 bpf_error("ISO host filtering not implemented");
1801 bpf_error("'esis' modifier applied to host");
1804 bpf_error("'isis' modifier applied to host");
1807 bpf_error("'clnp' modifier applied to host");
1810 bpf_error("'stp' modifier applied to host");
1813 bpf_error("IPX host filtering not implemented");
1816 bpf_error("'netbeui' modifier applied to host");
1825 static struct block
*
1826 gen_host6(addr
, mask
, proto
, dir
)
1827 struct in6_addr
*addr
;
1828 struct in6_addr
*mask
;
1835 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1838 bpf_error("'ip' modifier applied to ip6 host");
1841 bpf_error("'rarp' modifier applied to ip6 host");
1844 bpf_error("'arp' modifier applied to ip6 host");
1847 bpf_error("'sctp' modifier applied to host");
1850 bpf_error("'tcp' modifier applied to host");
1853 bpf_error("'udp' modifier applied to host");
1856 bpf_error("'icmp' modifier applied to host");
1859 bpf_error("'igmp' modifier applied to host");
1862 bpf_error("'igrp' modifier applied to host");
1865 bpf_error("'pim' modifier applied to host");
1868 bpf_error("'vrrp' modifier applied to host");
1871 bpf_error("ATALK host filtering not implemented");
1874 bpf_error("AARP host filtering not implemented");
1877 bpf_error("'decnet' modifier applied to ip6 host");
1880 bpf_error("SCA host filtering not implemented");
1883 bpf_error("LAT host filtering not implemented");
1886 bpf_error("MOPDL host filtering not implemented");
1889 bpf_error("MOPRC host filtering not implemented");
1892 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1893 off_nl
+ 8, off_nl
+ 24);
1896 bpf_error("'icmp6' modifier applied to host");
1899 bpf_error("'ah' modifier applied to host");
1902 bpf_error("'esp' modifier applied to host");
1905 bpf_error("ISO host filtering not implemented");
1908 bpf_error("'esis' modifier applied to host");
1911 bpf_error("'isis' modifier applied to host");
1914 bpf_error("'clnp' modifier applied to host");
1917 bpf_error("'stp' modifier applied to host");
1920 bpf_error("IPX host filtering not implemented");
1923 bpf_error("'netbeui' modifier applied to host");
1933 static struct block
*
1934 gen_gateway(eaddr
, alist
, proto
, dir
)
1935 const u_char
*eaddr
;
1936 bpf_u_int32
**alist
;
1940 struct block
*b0
, *b1
, *tmp
;
1943 bpf_error("direction applied to 'gateway'");
1950 if (linktype
== DLT_EN10MB
)
1951 b0
= gen_ehostop(eaddr
, Q_OR
);
1952 else if (linktype
== DLT_FDDI
)
1953 b0
= gen_fhostop(eaddr
, Q_OR
);
1954 else if (linktype
== DLT_IEEE802
)
1955 b0
= gen_thostop(eaddr
, Q_OR
);
1958 "'gateway' supported only on ethernet, FDDI or token ring");
1960 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1962 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1970 bpf_error("illegal modifier of 'gateway'");
1976 gen_proto_abbrev(proto
)
1987 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
1989 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
1995 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1997 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2003 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2005 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2011 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2014 #ifndef IPPROTO_IGMP
2015 #define IPPROTO_IGMP 2
2019 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2022 #ifndef IPPROTO_IGRP
2023 #define IPPROTO_IGRP 9
2026 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2030 #define IPPROTO_PIM 103
2034 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2036 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2041 #ifndef IPPROTO_VRRP
2042 #define IPPROTO_VRRP 112
2046 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2050 b1
= gen_linktype(ETHERTYPE_IP
);
2054 b1
= gen_linktype(ETHERTYPE_ARP
);
2058 b1
= gen_linktype(ETHERTYPE_REVARP
);
2062 bpf_error("link layer applied in wrong context");
2065 b1
= gen_linktype(ETHERTYPE_ATALK
);
2069 b1
= gen_linktype(ETHERTYPE_AARP
);
2073 b1
= gen_linktype(ETHERTYPE_DN
);
2077 b1
= gen_linktype(ETHERTYPE_SCA
);
2081 b1
= gen_linktype(ETHERTYPE_LAT
);
2085 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2089 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2094 b1
= gen_linktype(ETHERTYPE_IPV6
);
2097 #ifndef IPPROTO_ICMPV6
2098 #define IPPROTO_ICMPV6 58
2101 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2106 #define IPPROTO_AH 51
2109 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2111 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2117 #define IPPROTO_ESP 50
2120 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2122 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2128 b1
= gen_linktype(LLCSAP_ISONS
);
2132 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2136 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2140 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2144 b1
= gen_linktype(LLCSAP_8021D
);
2148 b1
= gen_linktype(LLCSAP_IPX
);
2152 b1
= gen_linktype(LLCSAP_NETBEUI
);
2161 static struct block
*
2168 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2169 s
->s
.k
= off_nl
+ 6;
2170 b
= new_block(JMP(BPF_JSET
));
2178 static struct block
*
2179 gen_portatom(off
, v
)
2186 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2189 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2190 s
->next
->s
.k
= off_nl
+ off
;
2192 b
= new_block(JMP(BPF_JEQ
));
2200 static struct block
*
2201 gen_portatom6(off
, v
)
2205 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2210 gen_portop(port
, proto
, dir
)
2211 int port
, proto
, dir
;
2213 struct block
*b0
, *b1
, *tmp
;
2215 /* ip proto 'proto' */
2216 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2222 b1
= gen_portatom(0, (bpf_int32
)port
);
2226 b1
= gen_portatom(2, (bpf_int32
)port
);
2231 tmp
= gen_portatom(0, (bpf_int32
)port
);
2232 b1
= gen_portatom(2, (bpf_int32
)port
);
2237 tmp
= gen_portatom(0, (bpf_int32
)port
);
2238 b1
= gen_portatom(2, (bpf_int32
)port
);
2250 static struct block
*
2251 gen_port(port
, ip_proto
, dir
)
2256 struct block
*b0
, *b1
, *tmp
;
2258 /* ether proto ip */
2259 b0
= gen_linktype(ETHERTYPE_IP
);
2265 b1
= gen_portop(port
, ip_proto
, dir
);
2269 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2270 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2272 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
2285 gen_portop6(port
, proto
, dir
)
2286 int port
, proto
, dir
;
2288 struct block
*b0
, *b1
, *tmp
;
2290 /* ip proto 'proto' */
2291 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2295 b1
= gen_portatom6(0, (bpf_int32
)port
);
2299 b1
= gen_portatom6(2, (bpf_int32
)port
);
2304 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2305 b1
= gen_portatom6(2, (bpf_int32
)port
);
2310 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2311 b1
= gen_portatom6(2, (bpf_int32
)port
);
2323 static struct block
*
2324 gen_port6(port
, ip_proto
, dir
)
2329 struct block
*b0
, *b1
, *tmp
;
2331 /* ether proto ip */
2332 b0
= gen_linktype(ETHERTYPE_IPV6
);
2338 b1
= gen_portop6(port
, ip_proto
, dir
);
2342 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2343 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2345 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
2358 lookup_proto(name
, proto
)
2359 register const char *name
;
2369 v
= pcap_nametoproto(name
);
2370 if (v
== PROTO_UNDEF
)
2371 bpf_error("unknown ip proto '%s'", name
);
2375 /* XXX should look up h/w protocol type based on linktype */
2376 v
= pcap_nametoeproto(name
);
2377 if (v
== PROTO_UNDEF
)
2378 bpf_error("unknown ether proto '%s'", name
);
2382 if (strcmp(name
, "esis") == 0)
2384 else if (strcmp(name
, "isis") == 0)
2386 else if (strcmp(name
, "clnp") == 0)
2389 bpf_error("unknown osi proto '%s'", name
);
2409 static struct block
*
2410 gen_protochain(v
, proto
, dir
)
2415 #ifdef NO_PROTOCHAIN
2416 return gen_proto(v
, proto
, dir
);
2418 struct block
*b0
, *b
;
2419 struct slist
*s
[100];
2420 int fix2
, fix3
, fix4
, fix5
;
2421 int ahcheck
, again
, end
;
2423 int reg2
= alloc_reg();
2425 memset(s
, 0, sizeof(s
));
2426 fix2
= fix3
= fix4
= fix5
= 0;
2433 b0
= gen_protochain(v
, Q_IP
, dir
);
2434 b
= gen_protochain(v
, Q_IPV6
, dir
);
2438 bpf_error("bad protocol applied for 'protochain'");
2442 no_optimize
= 1; /*this code is not compatible with optimzer yet */
2445 * s[0] is a dummy entry to protect other BPF insn from damaged
2446 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
2447 * hard to find interdependency made by jump table fixup.
2450 s
[i
] = new_stmt(0); /*dummy*/
2455 b0
= gen_linktype(ETHERTYPE_IP
);
2458 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2459 s
[i
]->s
.k
= off_nl
+ 9;
2461 /* X = ip->ip_hl << 2 */
2462 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2468 b0
= gen_linktype(ETHERTYPE_IPV6
);
2470 /* A = ip6->ip_nxt */
2471 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2472 s
[i
]->s
.k
= off_nl
+ 6;
2474 /* X = sizeof(struct ip6_hdr) */
2475 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
2481 bpf_error("unsupported proto to gen_protochain");
2485 /* again: if (A == v) goto end; else fall through; */
2487 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2489 s
[i
]->s
.jt
= NULL
; /*later*/
2490 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2494 #ifndef IPPROTO_NONE
2495 #define IPPROTO_NONE 59
2497 /* if (A == IPPROTO_NONE) goto end */
2498 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2499 s
[i
]->s
.jt
= NULL
; /*later*/
2500 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2501 s
[i
]->s
.k
= IPPROTO_NONE
;
2502 s
[fix5
]->s
.jf
= s
[i
];
2507 if (proto
== Q_IPV6
) {
2508 int v6start
, v6end
, v6advance
, j
;
2511 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
2512 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2513 s
[i
]->s
.jt
= NULL
; /*later*/
2514 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2515 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
2516 s
[fix2
]->s
.jf
= s
[i
];
2518 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
2519 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2520 s
[i
]->s
.jt
= NULL
; /*later*/
2521 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2522 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
2524 /* if (A == IPPROTO_ROUTING) goto v6advance */
2525 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2526 s
[i
]->s
.jt
= NULL
; /*later*/
2527 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2528 s
[i
]->s
.k
= IPPROTO_ROUTING
;
2530 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
2531 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2532 s
[i
]->s
.jt
= NULL
; /*later*/
2533 s
[i
]->s
.jf
= NULL
; /*later*/
2534 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
2545 * X = X + (P[X + 1] + 1) * 8;
2548 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2550 /* A = P[X + packet head] */
2551 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2555 s
[i
] = new_stmt(BPF_ST
);
2559 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2562 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2566 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2568 /* A = P[X + packet head]; */
2569 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2573 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2577 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2581 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2584 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2588 /* goto again; (must use BPF_JA for backward jump) */
2589 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2590 s
[i
]->s
.k
= again
- i
- 1;
2591 s
[i
- 1]->s
.jf
= s
[i
];
2595 for (j
= v6start
; j
<= v6end
; j
++)
2596 s
[j
]->s
.jt
= s
[v6advance
];
2601 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2603 s
[fix2
]->s
.jf
= s
[i
];
2609 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2610 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2611 s
[i
]->s
.jt
= NULL
; /*later*/
2612 s
[i
]->s
.jf
= NULL
; /*later*/
2613 s
[i
]->s
.k
= IPPROTO_AH
;
2615 s
[fix3
]->s
.jf
= s
[ahcheck
];
2622 * X = X + (P[X + 1] + 2) * 4;
2625 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2627 /* A = P[X + packet head]; */
2628 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2632 s
[i
] = new_stmt(BPF_ST
);
2636 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2639 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2643 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2645 /* A = P[X + packet head] */
2646 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2650 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2654 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2658 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2661 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2665 /* goto again; (must use BPF_JA for backward jump) */
2666 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2667 s
[i
]->s
.k
= again
- i
- 1;
2672 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2674 s
[fix2
]->s
.jt
= s
[end
];
2675 s
[fix4
]->s
.jf
= s
[end
];
2676 s
[fix5
]->s
.jt
= s
[end
];
2683 for (i
= 0; i
< max
- 1; i
++)
2684 s
[i
]->next
= s
[i
+ 1];
2685 s
[max
- 1]->next
= NULL
;
2690 b
= new_block(JMP(BPF_JEQ
));
2691 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2701 static struct block
*
2702 gen_proto(v
, proto
, dir
)
2707 struct block
*b0
, *b1
;
2709 if (dir
!= Q_DEFAULT
)
2710 bpf_error("direction applied to 'proto'");
2715 b0
= gen_proto(v
, Q_IP
, dir
);
2716 b1
= gen_proto(v
, Q_IPV6
, dir
);
2723 b0
= gen_linktype(ETHERTYPE_IP
);
2725 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2727 b1
= gen_protochain(v
, Q_IP
);
2733 b0
= gen_linktype(LLCSAP_ISONS
);
2734 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2739 bpf_error("arp does not encapsulate another protocol");
2743 bpf_error("rarp does not encapsulate another protocol");
2747 bpf_error("atalk encapsulation is not specifiable");
2751 bpf_error("decnet encapsulation is not specifiable");
2755 bpf_error("sca does not encapsulate another protocol");
2759 bpf_error("lat does not encapsulate another protocol");
2763 bpf_error("moprc does not encapsulate another protocol");
2767 bpf_error("mopdl does not encapsulate another protocol");
2771 return gen_linktype(v
);
2774 bpf_error("'udp proto' is bogus");
2778 bpf_error("'tcp proto' is bogus");
2782 bpf_error("'sctp proto' is bogus");
2786 bpf_error("'icmp proto' is bogus");
2790 bpf_error("'igmp proto' is bogus");
2794 bpf_error("'igrp proto' is bogus");
2798 bpf_error("'pim proto' is bogus");
2802 bpf_error("'vrrp proto' is bogus");
2807 b0
= gen_linktype(ETHERTYPE_IPV6
);
2809 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2811 b1
= gen_protochain(v
, Q_IPV6
);
2817 bpf_error("'icmp6 proto' is bogus");
2821 bpf_error("'ah proto' is bogus");
2824 bpf_error("'ah proto' is bogus");
2827 bpf_error("'stp proto' is bogus");
2830 bpf_error("'ipx proto' is bogus");
2833 bpf_error("'netbeui proto' is bogus");
2844 register const char *name
;
2847 int proto
= q
.proto
;
2851 bpf_u_int32 mask
, addr
;
2853 bpf_u_int32
**alist
;
2856 struct sockaddr_in
*sin
;
2857 struct sockaddr_in6
*sin6
;
2858 struct addrinfo
*res
, *res0
;
2859 struct in6_addr mask128
;
2861 struct block
*b
, *tmp
;
2862 int port
, real_proto
;
2867 addr
= pcap_nametonetaddr(name
);
2869 bpf_error("unknown network '%s'", name
);
2870 /* Left justify network addr and calculate its network mask */
2872 while (addr
&& (addr
& 0xff000000) == 0) {
2876 return gen_host(addr
, mask
, proto
, dir
);
2880 if (proto
== Q_LINK
) {
2884 eaddr
= pcap_ether_hostton(name
);
2887 "unknown ether host '%s'", name
);
2888 return gen_ehostop(eaddr
, dir
);
2891 eaddr
= pcap_ether_hostton(name
);
2894 "unknown FDDI host '%s'", name
);
2895 return gen_fhostop(eaddr
, dir
);
2898 eaddr
= pcap_ether_hostton(name
);
2901 "unknown token ring host '%s'", name
);
2902 return gen_thostop(eaddr
, dir
);
2906 "only ethernet/FDDI/token ring supports link-level host name");
2909 } else if (proto
== Q_DECNET
) {
2910 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2912 * I don't think DECNET hosts can be multihomed, so
2913 * there is no need to build up a list of addresses
2915 return (gen_host(dn_addr
, 0, proto
, dir
));
2918 alist
= pcap_nametoaddr(name
);
2919 if (alist
== NULL
|| *alist
== NULL
)
2920 bpf_error("unknown host '%s'", name
);
2922 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2924 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2926 tmp
= gen_host(**alist
++, 0xffffffff,
2933 memset(&mask128
, 0xff, sizeof(mask128
));
2934 res0
= res
= pcap_nametoaddrinfo(name
);
2936 bpf_error("unknown host '%s'", name
);
2938 tproto
= tproto6
= proto
;
2939 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2943 for (res
= res0
; res
; res
= res
->ai_next
) {
2944 switch (res
->ai_family
) {
2946 if (tproto
== Q_IPV6
)
2949 sin
= (struct sockaddr_in
*)
2951 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2952 0xffffffff, tproto
, dir
);
2955 if (tproto6
== Q_IP
)
2958 sin6
= (struct sockaddr_in6
*)
2960 tmp
= gen_host6(&sin6
->sin6_addr
,
2961 &mask128
, tproto6
, dir
);
2972 bpf_error("unknown host '%s'%s", name
,
2973 (proto
== Q_DEFAULT
)
2975 : " for specified address family");
2982 if (proto
!= Q_DEFAULT
&&
2983 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
2984 bpf_error("illegal qualifier of 'port'");
2985 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2986 bpf_error("unknown port '%s'", name
);
2987 if (proto
== Q_UDP
) {
2988 if (real_proto
== IPPROTO_TCP
)
2989 bpf_error("port '%s' is tcp", name
);
2990 else if (real_proto
== IPPROTO_SCTP
)
2991 bpf_error("port '%s' is sctp", name
);
2993 /* override PROTO_UNDEF */
2994 real_proto
= IPPROTO_UDP
;
2996 if (proto
== Q_TCP
) {
2997 if (real_proto
== IPPROTO_UDP
)
2998 bpf_error("port '%s' is udp", name
);
3000 else if (real_proto
== IPPROTO_SCTP
)
3001 bpf_error("port '%s' is sctp", name
);
3003 /* override PROTO_UNDEF */
3004 real_proto
= IPPROTO_TCP
;
3006 if (proto
== Q_SCTP
) {
3007 if (real_proto
== IPPROTO_UDP
)
3008 bpf_error("port '%s' is udp", name
);
3010 else if (real_proto
== IPPROTO_TCP
)
3011 bpf_error("port '%s' is tcp", name
);
3013 /* override PROTO_UNDEF */
3014 real_proto
= IPPROTO_SCTP
;
3017 return gen_port(port
, real_proto
, dir
);
3021 b
= gen_port(port
, real_proto
, dir
);
3022 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3029 eaddr
= pcap_ether_hostton(name
);
3031 bpf_error("unknown ether host: %s", name
);
3033 alist
= pcap_nametoaddr(name
);
3034 if (alist
== NULL
|| *alist
== NULL
)
3035 bpf_error("unknown host '%s'", name
);
3036 return gen_gateway(eaddr
, alist
, proto
, dir
);
3038 bpf_error("'gateway' not supported in this configuration");
3042 real_proto
= lookup_proto(name
, proto
);
3043 if (real_proto
>= 0)
3044 return gen_proto(real_proto
, proto
, dir
);
3046 bpf_error("unknown protocol: %s", name
);
3049 real_proto
= lookup_proto(name
, proto
);
3050 if (real_proto
>= 0)
3051 return gen_protochain(real_proto
, proto
, dir
);
3053 bpf_error("unknown protocol: %s", name
);
3065 gen_mcode(s1
, s2
, masklen
, q
)
3066 register const char *s1
, *s2
;
3067 register int masklen
;
3070 register int nlen
, mlen
;
3073 nlen
= __pcap_atoin(s1
, &n
);
3074 /* Promote short ipaddr */
3078 mlen
= __pcap_atoin(s2
, &m
);
3079 /* Promote short ipaddr */
3082 bpf_error("non-network bits set in \"%s mask %s\"",
3085 /* Convert mask len to mask */
3087 bpf_error("mask length must be <= 32");
3088 m
= 0xffffffff << (32 - masklen
);
3090 bpf_error("non-network bits set in \"%s/%d\"",
3097 return gen_host(n
, m
, q
.proto
, q
.dir
);
3100 bpf_error("Mask syntax for networks only");
3107 register const char *s
;
3112 int proto
= q
.proto
;
3118 else if (q
.proto
== Q_DECNET
)
3119 vlen
= __pcap_atodn(s
, &v
);
3121 vlen
= __pcap_atoin(s
, &v
);
3128 if (proto
== Q_DECNET
)
3129 return gen_host(v
, 0, proto
, dir
);
3130 else if (proto
== Q_LINK
) {
3131 bpf_error("illegal link layer address");
3134 if (s
== NULL
&& q
.addr
== Q_NET
) {
3135 /* Promote short net number */
3136 while (v
&& (v
& 0xff000000) == 0) {
3141 /* Promote short ipaddr */
3145 return gen_host(v
, mask
, proto
, dir
);
3150 proto
= IPPROTO_UDP
;
3151 else if (proto
== Q_TCP
)
3152 proto
= IPPROTO_TCP
;
3153 else if (proto
== Q_SCTP
)
3154 proto
= IPPROTO_SCTP
;
3155 else if (proto
== Q_DEFAULT
)
3156 proto
= PROTO_UNDEF
;
3158 bpf_error("illegal qualifier of 'port'");
3161 return gen_port((int)v
, proto
, dir
);
3165 b
= gen_port((int)v
, proto
, dir
);
3166 gen_or(gen_port6((int)v
, proto
, dir
), b
);
3172 bpf_error("'gateway' requires a name");
3176 return gen_proto((int)v
, proto
, dir
);
3179 return gen_protochain((int)v
, proto
, dir
);
3194 gen_mcode6(s1
, s2
, masklen
, q
)
3195 register const char *s1
, *s2
;
3196 register int masklen
;
3199 struct addrinfo
*res
;
3200 struct in6_addr
*addr
;
3201 struct in6_addr mask
;
3206 bpf_error("no mask %s supported", s2
);
3208 res
= pcap_nametoaddrinfo(s1
);
3210 bpf_error("invalid ip6 address %s", s1
);
3212 bpf_error("%s resolved to multiple address", s1
);
3213 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3215 if (sizeof(mask
) * 8 < masklen
)
3216 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3217 memset(&mask
, 0xff, masklen
/ 8);
3219 mask
.s6_addr
[masklen
/ 8] =
3220 (0xff << (8 - masklen
% 8)) & 0xff;
3223 a
= (u_int32_t
*)addr
;
3224 m
= (u_int32_t
*)&mask
;
3225 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3226 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3227 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
3235 bpf_error("Mask syntax for networks only");
3239 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
3244 bpf_error("invalid qualifier against IPv6 address");
3252 register const u_char
*eaddr
;
3255 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3256 if (linktype
== DLT_EN10MB
)
3257 return gen_ehostop(eaddr
, (int)q
.dir
);
3258 if (linktype
== DLT_FDDI
)
3259 return gen_fhostop(eaddr
, (int)q
.dir
);
3260 if (linktype
== DLT_IEEE802
)
3261 return gen_thostop(eaddr
, (int)q
.dir
);
3262 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
3264 bpf_error("ethernet address used in non-ether expression");
3270 struct slist
*s0
, *s1
;
3273 * This is definitely not the best way to do this, but the
3274 * lists will rarely get long.
3281 static struct slist
*
3287 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3292 static struct slist
*
3298 s
= new_stmt(BPF_LD
|BPF_MEM
);
3304 gen_load(proto
, index
, size
)
3309 struct slist
*s
, *tmp
;
3311 int regno
= alloc_reg();
3313 free_reg(index
->regno
);
3317 bpf_error("data size must be 1, 2, or 4");
3333 bpf_error("unsupported index operation");
3336 s
= xfer_to_x(index
);
3337 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3339 sappend(index
->s
, s
);
3354 /* XXX Note that we assume a fixed link header here. */
3355 s
= xfer_to_x(index
);
3356 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3359 sappend(index
->s
, s
);
3361 b
= gen_proto_abbrev(proto
);
3363 gen_and(index
->b
, b
);
3375 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3377 sappend(s
, xfer_to_a(index
));
3378 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3379 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3380 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3382 sappend(index
->s
, s
);
3384 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
3386 gen_and(index
->b
, b
);
3388 gen_and(gen_proto_abbrev(Q_IP
), b
);
3394 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
3398 index
->regno
= regno
;
3399 s
= new_stmt(BPF_ST
);
3401 sappend(index
->s
, s
);
3407 gen_relation(code
, a0
, a1
, reversed
)
3409 struct arth
*a0
, *a1
;
3412 struct slist
*s0
, *s1
, *s2
;
3413 struct block
*b
, *tmp
;
3417 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
3418 b
= new_block(JMP(code
));
3419 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
3420 reversed
= !reversed
;
3421 b
->s
.k
= 0x80000000;
3429 sappend(a0
->s
, a1
->s
);
3433 free_reg(a0
->regno
);
3434 free_reg(a1
->regno
);
3436 /* 'and' together protocol checks */
3439 gen_and(a0
->b
, tmp
= a1
->b
);
3455 int regno
= alloc_reg();
3456 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
3459 s
= new_stmt(BPF_LD
|BPF_LEN
);
3460 s
->next
= new_stmt(BPF_ST
);
3461 s
->next
->s
.k
= regno
;
3476 a
= (struct arth
*)newchunk(sizeof(*a
));
3480 s
= new_stmt(BPF_LD
|BPF_IMM
);
3482 s
->next
= new_stmt(BPF_ST
);
3498 s
= new_stmt(BPF_ALU
|BPF_NEG
);
3501 s
= new_stmt(BPF_ST
);
3509 gen_arth(code
, a0
, a1
)
3511 struct arth
*a0
, *a1
;
3513 struct slist
*s0
, *s1
, *s2
;
3517 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
3522 sappend(a0
->s
, a1
->s
);
3524 free_reg(a1
->regno
);
3526 s0
= new_stmt(BPF_ST
);
3527 a0
->regno
= s0
->s
.k
= alloc_reg();
3534 * Here we handle simple allocation of the scratch registers.
3535 * If too many registers are alloc'd, the allocator punts.
3537 static int regused
[BPF_MEMWORDS
];
3541 * Return the next free register.
3546 int n
= BPF_MEMWORDS
;
3549 if (regused
[curreg
])
3550 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
3552 regused
[curreg
] = 1;
3556 bpf_error("too many registers needed to evaluate expression");
3561 * Return a register to the table so it can
3571 static struct block
*
3578 s
= new_stmt(BPF_LD
|BPF_LEN
);
3579 b
= new_block(JMP(jmp
));
3590 return gen_len(BPF_JGE
, n
);
3594 * Actually, this is less than or equal.
3602 b
= gen_len(BPF_JGT
, n
);
3609 gen_byteop(op
, idx
, val
)
3620 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3623 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3624 b
->s
.code
= JMP(BPF_JGE
);
3629 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3630 b
->s
.code
= JMP(BPF_JGT
);
3634 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3638 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3642 b
= new_block(JMP(BPF_JEQ
));
3649 static u_char abroadcast
[] = { 0x0 };
3652 gen_broadcast(proto
)
3655 bpf_u_int32 hostmask
;
3656 struct block
*b0
, *b1
, *b2
;
3657 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3663 if (linktype
== DLT_ARCNET
)
3664 return gen_ahostop(abroadcast
, Q_DST
);
3665 if (linktype
== DLT_EN10MB
)
3666 return gen_ehostop(ebroadcast
, Q_DST
);
3667 if (linktype
== DLT_FDDI
)
3668 return gen_fhostop(ebroadcast
, Q_DST
);
3669 if (linktype
== DLT_IEEE802
)
3670 return gen_thostop(ebroadcast
, Q_DST
);
3671 bpf_error("not a broadcast link");
3675 b0
= gen_linktype(ETHERTYPE_IP
);
3676 hostmask
= ~netmask
;
3677 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3678 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3679 (bpf_int32
)(~0 & hostmask
), hostmask
);
3684 bpf_error("only ether/ip broadcast filters supported");
3688 gen_multicast(proto
)
3691 register struct block
*b0
, *b1
;
3692 register struct slist
*s
;
3698 if (linktype
== DLT_ARCNET
)
3699 /* all ARCnet multicasts use the same address */
3700 return gen_ahostop(abroadcast
, Q_DST
);
3702 if (linktype
== DLT_EN10MB
) {
3703 /* ether[0] & 1 != 0 */
3704 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3706 b0
= new_block(JMP(BPF_JSET
));
3712 if (linktype
== DLT_FDDI
) {
3713 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3714 /* fddi[1] & 1 != 0 */
3715 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3717 b0
= new_block(JMP(BPF_JSET
));
3723 /* TODO - check how token ring handles multicast */
3724 /* if (linktype == DLT_IEEE802) ... */
3726 /* Link not known to support multicasts */
3730 b0
= gen_linktype(ETHERTYPE_IP
);
3731 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3732 b1
->s
.code
= JMP(BPF_JGE
);
3738 b0
= gen_linktype(ETHERTYPE_IPV6
);
3739 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3744 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3748 * generate command for inbound/outbound. It's here so we can
3749 * make it link-type specific. 'dir' = 0 implies "inbound",
3750 * = 1 implies "outbound".
3756 register struct block
*b0
;
3759 * Only some data link types support inbound/outbound qualifiers.
3764 b0
= gen_relation(BPF_JEQ
,
3765 gen_load(Q_LINK
, gen_loadi(0), 1),
3771 bpf_error("inbound/outbound not supported on linktype %d\n",
3781 register const u_char
*eaddr
;
3784 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3785 if (linktype
== DLT_ARCNET
)
3786 return gen_ahostop(eaddr
, (int)q
.dir
);
3788 bpf_error("ARCnet address used in non-arc expression");
3792 static struct block
*
3793 gen_ahostop(eaddr
, dir
)
3794 register const u_char
*eaddr
;
3797 register struct block
*b0
, *b1
;
3800 /* src comes first, different from Ethernet */
3802 return gen_bcmp(0, 1, eaddr
);
3805 return gen_bcmp(1, 1, eaddr
);
3808 b0
= gen_ahostop(eaddr
, Q_SRC
);
3809 b1
= gen_ahostop(eaddr
, Q_DST
);
3815 b0
= gen_ahostop(eaddr
, Q_SRC
);
3816 b1
= gen_ahostop(eaddr
, Q_DST
);
3825 * support IEEE 802.1Q VLAN trunk over ethernet
3831 static u_int orig_linktype
= -1, orig_nl
= -1;
3835 * Change the offsets to point to the type and data fields within
3836 * the VLAN packet. This is somewhat of a kludge.
3838 if (orig_nl
== (u_int
)-1) {
3839 orig_linktype
= off_linktype
; /* save original values */
3850 bpf_error("no VLAN support for data link type %d",
3856 /* check for VLAN */
3857 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3859 /* If a specific VLAN is requested, check VLAN id */
3860 if (vlan_num
>= 0) {
3863 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);