]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
e85f6cfd07e056796ce9816c632055fc9ccbb9e9
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.152 2001-05-10 14:48:01 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
;
2368 v
= pcap_nametoproto(name
);
2369 if (v
== PROTO_UNDEF
)
2370 bpf_error("unknown ip proto '%s'", name
);
2374 /* XXX should look up h/w protocol type based on linktype */
2375 v
= pcap_nametoeproto(name
);
2376 if (v
== PROTO_UNDEF
)
2377 bpf_error("unknown ether proto '%s'", name
);
2381 if (strcmp(name
, "esis") == 0)
2383 else if (strcmp(name
, "isis") == 0)
2385 else if (strcmp(name
, "clnp") == 0)
2388 bpf_error("unknown osi proto '%s'", name
);
2408 static struct block
*
2409 gen_protochain(v
, proto
, dir
)
2414 #ifdef NO_PROTOCHAIN
2415 return gen_proto(v
, proto
, dir
);
2417 struct block
*b0
, *b
;
2418 struct slist
*s
[100];
2419 int fix2
, fix3
, fix4
, fix5
;
2420 int ahcheck
, again
, end
;
2422 int reg2
= alloc_reg();
2424 memset(s
, 0, sizeof(s
));
2425 fix2
= fix3
= fix4
= fix5
= 0;
2432 b0
= gen_protochain(v
, Q_IP
, dir
);
2433 b
= gen_protochain(v
, Q_IPV6
, dir
);
2437 bpf_error("bad protocol applied for 'protochain'");
2441 no_optimize
= 1; /*this code is not compatible with optimzer yet */
2444 * s[0] is a dummy entry to protect other BPF insn from damaged
2445 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
2446 * hard to find interdependency made by jump table fixup.
2449 s
[i
] = new_stmt(0); /*dummy*/
2454 b0
= gen_linktype(ETHERTYPE_IP
);
2457 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2458 s
[i
]->s
.k
= off_nl
+ 9;
2460 /* X = ip->ip_hl << 2 */
2461 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2467 b0
= gen_linktype(ETHERTYPE_IPV6
);
2469 /* A = ip6->ip_nxt */
2470 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2471 s
[i
]->s
.k
= off_nl
+ 6;
2473 /* X = sizeof(struct ip6_hdr) */
2474 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
2480 bpf_error("unsupported proto to gen_protochain");
2484 /* again: if (A == v) goto end; else fall through; */
2486 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2488 s
[i
]->s
.jt
= NULL
; /*later*/
2489 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2493 #ifndef IPPROTO_NONE
2494 #define IPPROTO_NONE 59
2496 /* if (A == IPPROTO_NONE) goto end */
2497 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2498 s
[i
]->s
.jt
= NULL
; /*later*/
2499 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2500 s
[i
]->s
.k
= IPPROTO_NONE
;
2501 s
[fix5
]->s
.jf
= s
[i
];
2506 if (proto
== Q_IPV6
) {
2507 int v6start
, v6end
, v6advance
, j
;
2510 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
2511 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2512 s
[i
]->s
.jt
= NULL
; /*later*/
2513 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2514 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
2515 s
[fix2
]->s
.jf
= s
[i
];
2517 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
2518 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2519 s
[i
]->s
.jt
= NULL
; /*later*/
2520 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2521 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
2523 /* if (A == IPPROTO_ROUTING) goto v6advance */
2524 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2525 s
[i
]->s
.jt
= NULL
; /*later*/
2526 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2527 s
[i
]->s
.k
= IPPROTO_ROUTING
;
2529 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
2530 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2531 s
[i
]->s
.jt
= NULL
; /*later*/
2532 s
[i
]->s
.jf
= NULL
; /*later*/
2533 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
2544 * X = X + (P[X + 1] + 1) * 8;
2547 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2549 /* A = P[X + packet head] */
2550 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2554 s
[i
] = new_stmt(BPF_ST
);
2558 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2561 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2565 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2567 /* A = P[X + packet head]; */
2568 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2572 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2576 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2580 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2583 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2587 /* goto again; (must use BPF_JA for backward jump) */
2588 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2589 s
[i
]->s
.k
= again
- i
- 1;
2590 s
[i
- 1]->s
.jf
= s
[i
];
2594 for (j
= v6start
; j
<= v6end
; j
++)
2595 s
[j
]->s
.jt
= s
[v6advance
];
2600 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2602 s
[fix2
]->s
.jf
= s
[i
];
2608 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2609 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2610 s
[i
]->s
.jt
= NULL
; /*later*/
2611 s
[i
]->s
.jf
= NULL
; /*later*/
2612 s
[i
]->s
.k
= IPPROTO_AH
;
2614 s
[fix3
]->s
.jf
= s
[ahcheck
];
2621 * X = X + (P[X + 1] + 2) * 4;
2624 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2626 /* A = P[X + packet head]; */
2627 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2631 s
[i
] = new_stmt(BPF_ST
);
2635 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2638 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2642 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2644 /* A = P[X + packet head] */
2645 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2649 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2653 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2657 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2660 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2664 /* goto again; (must use BPF_JA for backward jump) */
2665 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2666 s
[i
]->s
.k
= again
- i
- 1;
2671 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2673 s
[fix2
]->s
.jt
= s
[end
];
2674 s
[fix4
]->s
.jf
= s
[end
];
2675 s
[fix5
]->s
.jt
= s
[end
];
2682 for (i
= 0; i
< max
- 1; i
++)
2683 s
[i
]->next
= s
[i
+ 1];
2684 s
[max
- 1]->next
= NULL
;
2689 b
= new_block(JMP(BPF_JEQ
));
2690 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2700 static struct block
*
2701 gen_proto(v
, proto
, dir
)
2706 struct block
*b0
, *b1
;
2708 if (dir
!= Q_DEFAULT
)
2709 bpf_error("direction applied to 'proto'");
2714 b0
= gen_proto(v
, Q_IP
, dir
);
2715 b1
= gen_proto(v
, Q_IPV6
, dir
);
2722 b0
= gen_linktype(ETHERTYPE_IP
);
2724 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2726 b1
= gen_protochain(v
, Q_IP
);
2732 b0
= gen_linktype(LLCSAP_ISONS
);
2733 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2738 bpf_error("arp does not encapsulate another protocol");
2742 bpf_error("rarp does not encapsulate another protocol");
2746 bpf_error("atalk encapsulation is not specifiable");
2750 bpf_error("decnet encapsulation is not specifiable");
2754 bpf_error("sca does not encapsulate another protocol");
2758 bpf_error("lat does not encapsulate another protocol");
2762 bpf_error("moprc does not encapsulate another protocol");
2766 bpf_error("mopdl does not encapsulate another protocol");
2770 return gen_linktype(v
);
2773 bpf_error("'udp proto' is bogus");
2777 bpf_error("'tcp proto' is bogus");
2781 bpf_error("'sctp proto' is bogus");
2785 bpf_error("'icmp proto' is bogus");
2789 bpf_error("'igmp proto' is bogus");
2793 bpf_error("'igrp proto' is bogus");
2797 bpf_error("'pim proto' is bogus");
2801 bpf_error("'vrrp proto' is bogus");
2806 b0
= gen_linktype(ETHERTYPE_IPV6
);
2808 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2810 b1
= gen_protochain(v
, Q_IPV6
);
2816 bpf_error("'icmp6 proto' is bogus");
2820 bpf_error("'ah proto' is bogus");
2823 bpf_error("'ah proto' is bogus");
2826 bpf_error("'stp proto' is bogus");
2829 bpf_error("'ipx proto' is bogus");
2832 bpf_error("'netbeui proto' is bogus");
2843 register const char *name
;
2846 int proto
= q
.proto
;
2850 bpf_u_int32 mask
, addr
;
2852 bpf_u_int32
**alist
;
2855 struct sockaddr_in
*sin
;
2856 struct sockaddr_in6
*sin6
;
2857 struct addrinfo
*res
, *res0
;
2858 struct in6_addr mask128
;
2860 struct block
*b
, *tmp
;
2861 int port
, real_proto
;
2866 addr
= pcap_nametonetaddr(name
);
2868 bpf_error("unknown network '%s'", name
);
2869 /* Left justify network addr and calculate its network mask */
2871 while (addr
&& (addr
& 0xff000000) == 0) {
2875 return gen_host(addr
, mask
, proto
, dir
);
2879 if (proto
== Q_LINK
) {
2883 eaddr
= pcap_ether_hostton(name
);
2886 "unknown ether host '%s'", name
);
2887 return gen_ehostop(eaddr
, dir
);
2890 eaddr
= pcap_ether_hostton(name
);
2893 "unknown FDDI host '%s'", name
);
2894 return gen_fhostop(eaddr
, dir
);
2897 eaddr
= pcap_ether_hostton(name
);
2900 "unknown token ring host '%s'", name
);
2901 return gen_thostop(eaddr
, dir
);
2905 "only ethernet/FDDI/token ring supports link-level host name");
2908 } else if (proto
== Q_DECNET
) {
2909 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2911 * I don't think DECNET hosts can be multihomed, so
2912 * there is no need to build up a list of addresses
2914 return (gen_host(dn_addr
, 0, proto
, dir
));
2917 alist
= pcap_nametoaddr(name
);
2918 if (alist
== NULL
|| *alist
== NULL
)
2919 bpf_error("unknown host '%s'", name
);
2921 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2923 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2925 tmp
= gen_host(**alist
++, 0xffffffff,
2932 memset(&mask128
, 0xff, sizeof(mask128
));
2933 res0
= res
= pcap_nametoaddrinfo(name
);
2935 bpf_error("unknown host '%s'", name
);
2937 tproto
= tproto6
= proto
;
2938 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2942 for (res
= res0
; res
; res
= res
->ai_next
) {
2943 switch (res
->ai_family
) {
2945 if (tproto
== Q_IPV6
)
2948 sin
= (struct sockaddr_in
*)
2950 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2951 0xffffffff, tproto
, dir
);
2954 if (tproto6
== Q_IP
)
2957 sin6
= (struct sockaddr_in6
*)
2959 tmp
= gen_host6(&sin6
->sin6_addr
,
2960 &mask128
, tproto6
, dir
);
2971 bpf_error("unknown host '%s'%s", name
,
2972 (proto
== Q_DEFAULT
)
2974 : " for specified address family");
2981 if (proto
!= Q_DEFAULT
&&
2982 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
2983 bpf_error("illegal qualifier of 'port'");
2984 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2985 bpf_error("unknown port '%s'", name
);
2986 if (proto
== Q_UDP
) {
2987 if (real_proto
== IPPROTO_TCP
)
2988 bpf_error("port '%s' is tcp", name
);
2989 else if (real_proto
== IPPROTO_SCTP
)
2990 bpf_error("port '%s' is sctp", name
);
2992 /* override PROTO_UNDEF */
2993 real_proto
= IPPROTO_UDP
;
2995 if (proto
== Q_TCP
) {
2996 if (real_proto
== IPPROTO_UDP
)
2997 bpf_error("port '%s' is udp", name
);
2999 else if (real_proto
== IPPROTO_SCTP
)
3000 bpf_error("port '%s' is sctp", name
);
3002 /* override PROTO_UNDEF */
3003 real_proto
= IPPROTO_TCP
;
3005 if (proto
== Q_SCTP
) {
3006 if (real_proto
== IPPROTO_UDP
)
3007 bpf_error("port '%s' is udp", name
);
3009 else if (real_proto
== IPPROTO_TCP
)
3010 bpf_error("port '%s' is tcp", name
);
3012 /* override PROTO_UNDEF */
3013 real_proto
= IPPROTO_SCTP
;
3016 return gen_port(port
, real_proto
, dir
);
3020 b
= gen_port(port
, real_proto
, dir
);
3021 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3028 eaddr
= pcap_ether_hostton(name
);
3030 bpf_error("unknown ether host: %s", name
);
3032 alist
= pcap_nametoaddr(name
);
3033 if (alist
== NULL
|| *alist
== NULL
)
3034 bpf_error("unknown host '%s'", name
);
3035 return gen_gateway(eaddr
, alist
, proto
, dir
);
3037 bpf_error("'gateway' not supported in this configuration");
3041 real_proto
= lookup_proto(name
, proto
);
3042 if (real_proto
>= 0)
3043 return gen_proto(real_proto
, proto
, dir
);
3045 bpf_error("unknown protocol: %s", name
);
3048 real_proto
= lookup_proto(name
, proto
);
3049 if (real_proto
>= 0)
3050 return gen_protochain(real_proto
, proto
, dir
);
3052 bpf_error("unknown protocol: %s", name
);
3064 gen_mcode(s1
, s2
, masklen
, q
)
3065 register const char *s1
, *s2
;
3066 register int masklen
;
3069 register int nlen
, mlen
;
3072 nlen
= __pcap_atoin(s1
, &n
);
3073 /* Promote short ipaddr */
3077 mlen
= __pcap_atoin(s2
, &m
);
3078 /* Promote short ipaddr */
3081 bpf_error("non-network bits set in \"%s mask %s\"",
3084 /* Convert mask len to mask */
3086 bpf_error("mask length must be <= 32");
3087 m
= 0xffffffff << (32 - masklen
);
3089 bpf_error("non-network bits set in \"%s/%d\"",
3096 return gen_host(n
, m
, q
.proto
, q
.dir
);
3099 bpf_error("Mask syntax for networks only");
3106 register const char *s
;
3111 int proto
= q
.proto
;
3117 else if (q
.proto
== Q_DECNET
)
3118 vlen
= __pcap_atodn(s
, &v
);
3120 vlen
= __pcap_atoin(s
, &v
);
3127 if (proto
== Q_DECNET
)
3128 return gen_host(v
, 0, proto
, dir
);
3129 else if (proto
== Q_LINK
) {
3130 bpf_error("illegal link layer address");
3133 if (s
== NULL
&& q
.addr
== Q_NET
) {
3134 /* Promote short net number */
3135 while (v
&& (v
& 0xff000000) == 0) {
3140 /* Promote short ipaddr */
3144 return gen_host(v
, mask
, proto
, dir
);
3149 proto
= IPPROTO_UDP
;
3150 else if (proto
== Q_TCP
)
3151 proto
= IPPROTO_TCP
;
3152 else if (proto
== Q_SCTP
)
3153 proto
= IPPROTO_SCTP
;
3154 else if (proto
== Q_DEFAULT
)
3155 proto
= PROTO_UNDEF
;
3157 bpf_error("illegal qualifier of 'port'");
3160 return gen_port((int)v
, proto
, dir
);
3164 b
= gen_port((int)v
, proto
, dir
);
3165 gen_or(gen_port6((int)v
, proto
, dir
), b
);
3171 bpf_error("'gateway' requires a name");
3175 return gen_proto((int)v
, proto
, dir
);
3178 return gen_protochain((int)v
, proto
, dir
);
3193 gen_mcode6(s1
, s2
, masklen
, q
)
3194 register const char *s1
, *s2
;
3195 register int masklen
;
3198 struct addrinfo
*res
;
3199 struct in6_addr
*addr
;
3200 struct in6_addr mask
;
3205 bpf_error("no mask %s supported", s2
);
3207 res
= pcap_nametoaddrinfo(s1
);
3209 bpf_error("invalid ip6 address %s", s1
);
3211 bpf_error("%s resolved to multiple address", s1
);
3212 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3214 if (sizeof(mask
) * 8 < masklen
)
3215 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3216 memset(&mask
, 0xff, masklen
/ 8);
3218 mask
.s6_addr
[masklen
/ 8] =
3219 (0xff << (8 - masklen
% 8)) & 0xff;
3222 a
= (u_int32_t
*)addr
;
3223 m
= (u_int32_t
*)&mask
;
3224 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3225 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3226 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
3234 bpf_error("Mask syntax for networks only");
3238 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
3243 bpf_error("invalid qualifier against IPv6 address");
3251 register const u_char
*eaddr
;
3254 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3255 if (linktype
== DLT_EN10MB
)
3256 return gen_ehostop(eaddr
, (int)q
.dir
);
3257 if (linktype
== DLT_FDDI
)
3258 return gen_fhostop(eaddr
, (int)q
.dir
);
3259 if (linktype
== DLT_IEEE802
)
3260 return gen_thostop(eaddr
, (int)q
.dir
);
3261 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
3263 bpf_error("ethernet address used in non-ether expression");
3269 struct slist
*s0
, *s1
;
3272 * This is definitely not the best way to do this, but the
3273 * lists will rarely get long.
3280 static struct slist
*
3286 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3291 static struct slist
*
3297 s
= new_stmt(BPF_LD
|BPF_MEM
);
3303 gen_load(proto
, index
, size
)
3308 struct slist
*s
, *tmp
;
3310 int regno
= alloc_reg();
3312 free_reg(index
->regno
);
3316 bpf_error("data size must be 1, 2, or 4");
3332 bpf_error("unsupported index operation");
3335 s
= xfer_to_x(index
);
3336 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3338 sappend(index
->s
, s
);
3353 /* XXX Note that we assume a fixed link header here. */
3354 s
= xfer_to_x(index
);
3355 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3358 sappend(index
->s
, s
);
3360 b
= gen_proto_abbrev(proto
);
3362 gen_and(index
->b
, b
);
3374 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3376 sappend(s
, xfer_to_a(index
));
3377 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3378 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3379 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3381 sappend(index
->s
, s
);
3383 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
3385 gen_and(index
->b
, b
);
3387 gen_and(gen_proto_abbrev(Q_IP
), b
);
3393 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
3397 index
->regno
= regno
;
3398 s
= new_stmt(BPF_ST
);
3400 sappend(index
->s
, s
);
3406 gen_relation(code
, a0
, a1
, reversed
)
3408 struct arth
*a0
, *a1
;
3411 struct slist
*s0
, *s1
, *s2
;
3412 struct block
*b
, *tmp
;
3416 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
3417 b
= new_block(JMP(code
));
3418 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
3419 reversed
= !reversed
;
3420 b
->s
.k
= 0x80000000;
3428 sappend(a0
->s
, a1
->s
);
3432 free_reg(a0
->regno
);
3433 free_reg(a1
->regno
);
3435 /* 'and' together protocol checks */
3438 gen_and(a0
->b
, tmp
= a1
->b
);
3454 int regno
= alloc_reg();
3455 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
3458 s
= new_stmt(BPF_LD
|BPF_LEN
);
3459 s
->next
= new_stmt(BPF_ST
);
3460 s
->next
->s
.k
= regno
;
3475 a
= (struct arth
*)newchunk(sizeof(*a
));
3479 s
= new_stmt(BPF_LD
|BPF_IMM
);
3481 s
->next
= new_stmt(BPF_ST
);
3497 s
= new_stmt(BPF_ALU
|BPF_NEG
);
3500 s
= new_stmt(BPF_ST
);
3508 gen_arth(code
, a0
, a1
)
3510 struct arth
*a0
, *a1
;
3512 struct slist
*s0
, *s1
, *s2
;
3516 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
3521 sappend(a0
->s
, a1
->s
);
3523 free_reg(a1
->regno
);
3525 s0
= new_stmt(BPF_ST
);
3526 a0
->regno
= s0
->s
.k
= alloc_reg();
3533 * Here we handle simple allocation of the scratch registers.
3534 * If too many registers are alloc'd, the allocator punts.
3536 static int regused
[BPF_MEMWORDS
];
3540 * Return the next free register.
3545 int n
= BPF_MEMWORDS
;
3548 if (regused
[curreg
])
3549 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
3551 regused
[curreg
] = 1;
3555 bpf_error("too many registers needed to evaluate expression");
3560 * Return a register to the table so it can
3570 static struct block
*
3577 s
= new_stmt(BPF_LD
|BPF_LEN
);
3578 b
= new_block(JMP(jmp
));
3589 return gen_len(BPF_JGE
, n
);
3593 * Actually, this is less than or equal.
3601 b
= gen_len(BPF_JGT
, n
);
3608 gen_byteop(op
, idx
, val
)
3619 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3622 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3623 b
->s
.code
= JMP(BPF_JGE
);
3628 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3629 b
->s
.code
= JMP(BPF_JGT
);
3633 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3637 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3641 b
= new_block(JMP(BPF_JEQ
));
3648 static u_char abroadcast
[] = { 0x0 };
3651 gen_broadcast(proto
)
3654 bpf_u_int32 hostmask
;
3655 struct block
*b0
, *b1
, *b2
;
3656 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3662 if (linktype
== DLT_ARCNET
)
3663 return gen_ahostop(abroadcast
, Q_DST
);
3664 if (linktype
== DLT_EN10MB
)
3665 return gen_ehostop(ebroadcast
, Q_DST
);
3666 if (linktype
== DLT_FDDI
)
3667 return gen_fhostop(ebroadcast
, Q_DST
);
3668 if (linktype
== DLT_IEEE802
)
3669 return gen_thostop(ebroadcast
, Q_DST
);
3670 bpf_error("not a broadcast link");
3674 b0
= gen_linktype(ETHERTYPE_IP
);
3675 hostmask
= ~netmask
;
3676 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3677 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3678 (bpf_int32
)(~0 & hostmask
), hostmask
);
3683 bpf_error("only ether/ip broadcast filters supported");
3687 gen_multicast(proto
)
3690 register struct block
*b0
, *b1
;
3691 register struct slist
*s
;
3697 if (linktype
== DLT_ARCNET
)
3698 /* all ARCnet multicasts use the same address */
3699 return gen_ahostop(abroadcast
, Q_DST
);
3701 if (linktype
== DLT_EN10MB
) {
3702 /* ether[0] & 1 != 0 */
3703 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3705 b0
= new_block(JMP(BPF_JSET
));
3711 if (linktype
== DLT_FDDI
) {
3712 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3713 /* fddi[1] & 1 != 0 */
3714 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3716 b0
= new_block(JMP(BPF_JSET
));
3722 /* TODO - check how token ring handles multicast */
3723 /* if (linktype == DLT_IEEE802) ... */
3725 /* Link not known to support multicasts */
3729 b0
= gen_linktype(ETHERTYPE_IP
);
3730 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3731 b1
->s
.code
= JMP(BPF_JGE
);
3737 b0
= gen_linktype(ETHERTYPE_IPV6
);
3738 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3743 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3747 * generate command for inbound/outbound. It's here so we can
3748 * make it link-type specific. 'dir' = 0 implies "inbound",
3749 * = 1 implies "outbound".
3755 register struct block
*b0
;
3758 * Only some data link types support inbound/outbound qualifiers.
3763 b0
= gen_relation(BPF_JEQ
,
3764 gen_load(Q_LINK
, gen_loadi(0), 1),
3770 bpf_error("inbound/outbound not supported on linktype %d\n",
3780 register const u_char
*eaddr
;
3783 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3784 if (linktype
== DLT_ARCNET
)
3785 return gen_ahostop(eaddr
, (int)q
.dir
);
3787 bpf_error("ARCnet address used in non-arc expression");
3791 static struct block
*
3792 gen_ahostop(eaddr
, dir
)
3793 register const u_char
*eaddr
;
3796 register struct block
*b0
, *b1
;
3799 /* src comes first, different from Ethernet */
3801 return gen_bcmp(0, 1, eaddr
);
3804 return gen_bcmp(1, 1, eaddr
);
3807 b0
= gen_ahostop(eaddr
, Q_SRC
);
3808 b1
= gen_ahostop(eaddr
, Q_DST
);
3814 b0
= gen_ahostop(eaddr
, Q_SRC
);
3815 b1
= gen_ahostop(eaddr
, Q_DST
);
3824 * support IEEE 802.1Q VLAN trunk over ethernet
3830 static u_int orig_linktype
= -1, orig_nl
= -1;
3834 * Change the offsets to point to the type and data fields within
3835 * the VLAN packet. This is somewhat of a kludge.
3837 if (orig_nl
== (u_int
)-1) {
3838 orig_linktype
= off_linktype
; /* save original values */
3849 bpf_error("no VLAN support for data link type %d",
3855 /* check for VLAN */
3856 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3858 /* If a specific VLAN is requested, check VLAN id */
3859 if (vlan_num
>= 0) {
3862 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);