]>
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.159 2001-11-12 21:59:44 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
;
81 /* Hack for updating VLAN offsets. */
82 static u_int orig_linktype
= -1, orig_nl
= -1;
86 int pcap_fddipad
= PCAP_FDDIPAD
;
93 bpf_error(const char *fmt
, ...)
100 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
107 static void init_linktype(int);
109 static int alloc_reg(void);
110 static void free_reg(int);
112 static struct block
*root
;
115 * We divy out chunks of memory rather than call malloc each time so
116 * we don't have to worry about leaking memory. It's probably
117 * not a big deal if all this memory was wasted but it this ever
118 * goes into a library that would probably not be a good idea.
121 #define CHUNK0SIZE 1024
127 static struct chunk chunks
[NCHUNKS
];
128 static int cur_chunk
;
130 static void *newchunk(u_int
);
131 static void freechunks(void);
132 static inline struct block
*new_block(int);
133 static inline struct slist
*new_stmt(int);
134 static struct block
*gen_retblk(int);
135 static inline void syntax(void);
137 static void backpatch(struct block
*, struct block
*);
138 static void merge(struct block
*, struct block
*);
139 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
140 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
141 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
142 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
143 static struct block
*gen_uncond(int);
144 static inline struct block
*gen_true(void);
145 static inline struct block
*gen_false(void);
146 static struct block
*gen_linktype(int);
147 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
148 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
150 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
152 static struct block
*gen_ahostop(const u_char
*, int);
153 static struct block
*gen_ehostop(const u_char
*, int);
154 static struct block
*gen_fhostop(const u_char
*, int);
155 static struct block
*gen_thostop(const u_char
*, int);
156 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
157 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
159 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
162 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
164 static struct block
*gen_ipfrag(void);
165 static struct block
*gen_portatom(int, bpf_int32
);
167 static struct block
*gen_portatom6(int, bpf_int32
);
169 struct block
*gen_portop(int, int, int);
170 static struct block
*gen_port(int, int, int);
172 struct block
*gen_portop6(int, int, int);
173 static struct block
*gen_port6(int, int, int);
175 static int lookup_proto(const char *, int);
176 static struct block
*gen_protochain(int, int, int);
177 static struct block
*gen_proto(int, int, int);
178 static struct slist
*xfer_to_x(struct arth
*);
179 static struct slist
*xfer_to_a(struct arth
*);
180 static struct block
*gen_len(int, int);
190 /* XXX Round up to nearest long. */
191 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
193 /* XXX Round up to structure boundary. */
197 cp
= &chunks
[cur_chunk
];
198 if (n
> cp
->n_left
) {
199 ++cp
, k
= ++cur_chunk
;
201 bpf_error("out of memory");
202 size
= CHUNK0SIZE
<< k
;
203 cp
->m
= (void *)malloc(size
);
204 memset((char *)cp
->m
, 0, size
);
207 bpf_error("out of memory");
210 return (void *)((char *)cp
->m
+ cp
->n_left
);
219 for (i
= 0; i
< NCHUNKS
; ++i
)
220 if (chunks
[i
].m
!= NULL
) {
227 * A strdup whose allocations are freed after code generation is over.
231 register const char *s
;
233 int n
= strlen(s
) + 1;
234 char *cp
= newchunk(n
);
240 static inline struct block
*
246 p
= (struct block
*)newchunk(sizeof(*p
));
253 static inline struct slist
*
259 p
= (struct slist
*)newchunk(sizeof(*p
));
265 static struct block
*
269 struct block
*b
= new_block(BPF_RET
|BPF_K
);
278 bpf_error("syntax error in filter expression");
281 static bpf_u_int32 netmask
;
286 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
287 char *buf
, int optimize
, bpf_u_int32 mask
)
296 if (setjmp(top_ctx
)) {
304 snaplen
= pcap_snapshot(p
);
306 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
307 "snaplen of 0 rejects all packets");
311 lex_init(buf
? buf
: "");
312 init_linktype(pcap_datalink(p
));
319 root
= gen_retblk(snaplen
);
321 if (optimize
&& !no_optimize
) {
324 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
325 bpf_error("expression rejects all packets");
327 program
->bf_insns
= icode_to_fcode(root
, &len
);
328 program
->bf_len
= len
;
336 * entry point for using the compiler with no pcap open
337 * pass in all the stuff that is needed explicitly instead.
340 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
341 struct bpf_program
*program
,
342 char *buf
, int optimize
, bpf_u_int32 mask
)
347 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
350 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
356 * Clean up a "struct bpf_program" by freeing all the memory allocated
360 pcap_freecode(struct bpf_program
*program
)
363 if (program
->bf_insns
!= NULL
) {
364 free((char *)program
->bf_insns
);
365 program
->bf_insns
= NULL
;
370 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
371 * which of the jt and jf fields has been resolved and which is a pointer
372 * back to another unresolved block (or nil). At least one of the fields
373 * in each block is already resolved.
376 backpatch(list
, target
)
377 struct block
*list
, *target
;
394 * Merge the lists in b0 and b1, using the 'sense' field to indicate
395 * which of jt and jf is the link.
399 struct block
*b0
, *b1
;
401 register struct block
**p
= &b0
;
403 /* Find end of list. */
405 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
407 /* Concatenate the lists. */
415 backpatch(p
, gen_retblk(snaplen
));
416 p
->sense
= !p
->sense
;
417 backpatch(p
, gen_retblk(0));
423 struct block
*b0
, *b1
;
425 backpatch(b0
, b1
->head
);
426 b0
->sense
= !b0
->sense
;
427 b1
->sense
= !b1
->sense
;
429 b1
->sense
= !b1
->sense
;
435 struct block
*b0
, *b1
;
437 b0
->sense
= !b0
->sense
;
438 backpatch(b0
, b1
->head
);
439 b0
->sense
= !b0
->sense
;
448 b
->sense
= !b
->sense
;
451 static struct block
*
452 gen_cmp(offset
, size
, v
)
459 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
462 b
= new_block(JMP(BPF_JEQ
));
469 static struct block
*
470 gen_cmp_gt(offset
, size
, v
)
477 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
480 b
= new_block(JMP(BPF_JGT
));
487 static struct block
*
488 gen_mcmp(offset
, size
, v
, mask
)
493 struct block
*b
= gen_cmp(offset
, size
, v
);
496 if (mask
!= 0xffffffff) {
497 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
504 static struct block
*
505 gen_bcmp(offset
, size
, v
)
506 register u_int offset
, size
;
507 register const u_char
*v
;
509 register struct block
*b
, *tmp
;
513 register const u_char
*p
= &v
[size
- 4];
514 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
515 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
517 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
524 register const u_char
*p
= &v
[size
- 2];
525 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
527 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
534 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
543 * Various code constructs need to know the layout of the data link
544 * layer. These variables give the necessary offsets. off_linktype
545 * is set to -1 for no encapsulation, in which case, IP is assumed.
547 static u_int off_linktype
;
564 off_nl
= 6; /* XXX in reality, variable! */
574 * SLIP doesn't have a link level type. The 16 byte
575 * header is hacked into our SLIP driver.
582 /* XXX this may be the same as the DLT_PPP_BSDOS case */
595 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
596 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
603 * This does no include the Ethernet header, and
604 * only covers session state.
617 * FDDI doesn't really have a link-level type field.
618 * We set "off_linktype" to the offset of the LLC header.
620 * To check for Ethernet types, we assume that SSAP = SNAP
621 * is being used and pick out the encapsulated Ethernet type.
622 * XXX - should we generate code to check for SNAP?
626 off_linktype
+= pcap_fddipad
;
630 off_nl
+= pcap_fddipad
;
636 * Token Ring doesn't really have a link-level type field.
637 * We set "off_linktype" to the offset of the LLC header.
639 * To check for Ethernet types, we assume that SSAP = SNAP
640 * is being used and pick out the encapsulated Ethernet type.
641 * XXX - should we generate code to check for SNAP?
643 * XXX - the header is actually variable-length.
644 * Some various Linux patched versions gave 38
645 * as "off_linktype" and 40 as "off_nl"; however,
646 * if a token ring packet has *no* routing
647 * information, i.e. is not source-routed, the correct
648 * values are 20 and 22, as they are in the vanilla code.
650 * A packet is source-routed iff the uppermost bit
651 * of the first byte of the source address, at an
652 * offset of 8, has the uppermost bit set. If the
653 * packet is source-routed, the total number of bytes
654 * of routing information is 2 plus bits 0x1F00 of
655 * the 16-bit value at an offset of 14 (shifted right
656 * 8 - figure out which byte that is).
664 * 802.11 doesn't really have a link-level type field.
665 * We set "off_linktype" to the offset of the LLC header.
667 * To check for Ethernet types, we assume that SSAP = SNAP
668 * is being used and pick out the encapsulated Ethernet type.
669 * XXX - should we generate code to check for SNAP?
671 * XXX - the header is actually variable-length. We
672 * assume a 24-byte link-layer header, as appears in
673 * data frames in networks with no bridges.
679 case DLT_ATM_RFC1483
:
681 * assume routed, non-ISO PDUs
682 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
693 case DLT_ATM_CLIP
: /* Linux ATM defines this */
698 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
705 * LocalTalk does have a 1-byte type field in the LLAP header,
706 * but really it just indicates whether there is a "short" or
707 * "long" DDP packet following.
713 bpf_error("unknown data link type %d", linktype
);
717 static struct block
*
724 s
= new_stmt(BPF_LD
|BPF_IMM
);
726 b
= new_block(JMP(BPF_JEQ
));
732 static inline struct block
*
735 return gen_uncond(1);
738 static inline struct block
*
741 return gen_uncond(0);
745 * Byte-swap a 32-bit number.
746 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
747 * big-endian platforms.)
749 #define SWAPLONG(y) \
750 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
752 static struct block
*
756 struct block
*b0
, *b1
;
765 * OSI protocols always use 802.2 encapsulation.
766 * XXX - should we check both the DSAP and the
767 * SSAP, like this, or should we check just the
770 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
772 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
773 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
779 * NetBEUI always uses 802.2 encapsulation.
780 * XXX - should we check both the DSAP and the
781 * SSAP, like this, or should we check just the
784 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
786 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
787 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
795 * Ethernet_II frames, which are Ethernet
796 * frames with a frame type of ETHERTYPE_IPX;
798 * Ethernet_802.3 frames, which are 802.3
799 * frames (i.e., the type/length field is
800 * a length field, <= ETHERMTU, rather than
801 * a type field) with the first two bytes
802 * after the Ethernet/802.3 header being
805 * Ethernet_802.2 frames, which are 802.3
806 * frames with an 802.2 LLC header and
807 * with the IPX LSAP as the DSAP in the LLC
810 * Ethernet_SNAP frames, which are 802.3
811 * frames with an LLC header and a SNAP
812 * header and with an OUI of 0x000000
813 * (encapsulated Ethernet) and a protocol
814 * ID of ETHERTYPE_IPX in the SNAP header.
816 * XXX - should we generate the same code both
817 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
821 * This generates code to check both for the
822 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
824 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
825 (bpf_int32
)LLCSAP_IPX
);
826 b1
= gen_cmp(off_linktype
+ 2, BPF_H
,
831 * Now we add code to check for SNAP frames with
832 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
834 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
838 * Now we generate code to check for 802.3
841 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
845 * Now add the check for 802.3 frames before the
846 * check for Ethernet_802.2 and Ethernet_802.3,
847 * as those checks should only be done on 802.3
848 * frames, not on Ethernet frames.
853 * Now add the check for Ethernet_II frames, and
854 * do that before checking for the other frame
857 b0
= gen_cmp(off_linktype
, BPF_H
,
858 (bpf_int32
)ETHERTYPE_IPX
);
862 case ETHERTYPE_ATALK
:
865 * EtherTalk (AppleTalk protocols on Ethernet link
866 * layer) may use 802.2 encapsulation.
870 * Check for 802.2 encapsulation (EtherTalk phase 2?);
871 * we check for an Ethernet type field less than
872 * 1500, which means it's an 802.3 length field.
874 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
878 * 802.2-encapsulated ETHERTYPE_ATALK packets are
879 * SNAP packets with an organization code of
880 * 0x080007 (Apple, for Appletalk) and a protocol
881 * type of ETHERTYPE_ATALK (Appletalk).
883 * 802.2-encapsulated ETHERTYPE_AARP packets are
884 * SNAP packets with an organization code of
885 * 0x000000 (encapsulated Ethernet) and a protocol
886 * type of ETHERTYPE_AARP (Appletalk ARP).
888 if (proto
== ETHERTYPE_ATALK
)
889 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
890 else /* proto == ETHERTYPE_AARP */
891 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
895 * Check for Ethernet encapsulation (Ethertalk
896 * phase 1?); we just check for the Ethernet
899 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
905 if (proto
<= ETHERMTU
) {
907 * This is an LLC SAP value, so the frames
908 * that match would be 802.2 frames.
909 * Check that the frame is an 802.2 frame
910 * (i.e., that the length/type field is
911 * a length field, <= ETHERMTU) and
912 * then check the DSAP.
914 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
916 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
922 * This is an Ethernet type, so compare
923 * the length/type field with it (if
924 * the frame is an 802.2 frame, the length
925 * field will be <= ETHERMTU, and, as
926 * "proto" is > ETHERMTU, this test
927 * will fail and the frame won't match,
928 * which is what we want).
930 return gen_cmp(off_linktype
, BPF_H
,
939 case DLT_ATM_RFC1483
:
942 * XXX - handle token-ring variable-length header.
947 return gen_cmp(off_linktype
, BPF_H
, (long)
948 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
951 return gen_cmp(off_linktype
, BPF_H
, (long)
952 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
956 * XXX - are there ever SNAP frames for IPX on
957 * non-Ethernet 802.x networks?
959 return gen_cmp(off_linktype
, BPF_B
,
960 (bpf_int32
)LLCSAP_IPX
);
962 case ETHERTYPE_ATALK
:
964 * 802.2-encapsulated ETHERTYPE_ATALK packets are
965 * SNAP packets with an organization code of
966 * 0x080007 (Apple, for Appletalk) and a protocol
967 * type of ETHERTYPE_ATALK (Appletalk).
969 * XXX - check for an organization code of
970 * encapsulated Ethernet as well?
972 return gen_snap(0x080007, ETHERTYPE_ATALK
,
978 * XXX - we don't have to check for IPX 802.3
979 * here, but should we check for the IPX Ethertype?
981 if (proto
<= ETHERMTU
) {
983 * This is an LLC SAP value, so check
986 return gen_cmp(off_linktype
, BPF_B
,
990 * This is an Ethernet type; we assume
991 * that it's unlikely that it'll
992 * appear in the right place at random,
993 * and therefore check only the
994 * location that would hold the Ethernet
995 * type in a SNAP frame with an organization
996 * code of 0x000000 (encapsulated Ethernet).
998 * XXX - if we were to check for the SNAP DSAP
999 * and LSAP, as per XXX, and were also to check
1000 * for an organization code of 0x000000
1001 * (encapsulated Ethernet), we'd do
1003 * return gen_snap(0x000000, proto,
1006 * here; for now, we don't, as per the above.
1007 * I don't know whether it's worth the
1008 * extra CPU time to do the right check
1011 return gen_cmp(off_linktype
+6, BPF_H
,
1022 * OSI protocols always use 802.2 encapsulation.
1023 * XXX - should we check both the DSAP and the
1024 * LSAP, like this, or should we check just the
1027 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1028 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1029 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1033 case LLCSAP_NETBEUI
:
1035 * NetBEUI always uses 802.2 encapsulation.
1036 * XXX - should we check both the DSAP and the
1037 * LSAP, like this, or should we check just the
1040 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1041 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1042 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1048 * Ethernet_II frames, which are Ethernet
1049 * frames with a frame type of ETHERTYPE_IPX;
1051 * Ethernet_802.3 frames, which have a frame
1052 * type of LINUX_SLL_P_802_3;
1054 * Ethernet_802.2 frames, which are 802.3
1055 * frames with an 802.2 LLC header (i.e, have
1056 * a frame type of LINUX_SLL_P_802_2) and
1057 * with the IPX LSAP as the DSAP in the LLC
1060 * Ethernet_SNAP frames, which are 802.3
1061 * frames with an LLC header and a SNAP
1062 * header and with an OUI of 0x000000
1063 * (encapsulated Ethernet) and a protocol
1064 * ID of ETHERTYPE_IPX in the SNAP header.
1066 * First, do the checks on LINUX_SLL_P_802_2
1067 * frames; generate the check for either
1068 * Ethernet_802.2 or Ethernet_SNAP frames, and
1069 * then put a check for LINUX_SLL_P_802_2 frames
1072 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1073 (bpf_int32
)LLCSAP_IPX
);
1074 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1077 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1081 * Now check for 802.3 frames and OR that with
1082 * the previous test.
1084 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1088 * Now add the check for Ethernet_II frames, and
1089 * do that before checking for the other frame
1092 b0
= gen_cmp(off_linktype
, BPF_H
,
1093 (bpf_int32
)ETHERTYPE_IPX
);
1097 case ETHERTYPE_ATALK
:
1098 case ETHERTYPE_AARP
:
1100 * EtherTalk (AppleTalk protocols on Ethernet link
1101 * layer) may use 802.2 encapsulation.
1105 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1106 * we check for the 802.2 protocol type in the
1107 * "Ethernet type" field.
1109 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1112 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1113 * SNAP packets with an organization code of
1114 * 0x080007 (Apple, for Appletalk) and a protocol
1115 * type of ETHERTYPE_ATALK (Appletalk).
1117 * 802.2-encapsulated ETHERTYPE_AARP packets are
1118 * SNAP packets with an organization code of
1119 * 0x000000 (encapsulated Ethernet) and a protocol
1120 * type of ETHERTYPE_AARP (Appletalk ARP).
1122 if (proto
== ETHERTYPE_ATALK
)
1123 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1125 else /* proto == ETHERTYPE_AARP */
1126 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1131 * Check for Ethernet encapsulation (Ethertalk
1132 * phase 1?); we just check for the Ethernet
1135 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1141 if (proto
<= ETHERMTU
) {
1143 * This is an LLC SAP value, so the frames
1144 * that match would be 802.2 frames.
1145 * Check for the 802.2 protocol type
1146 * in the "Ethernet type" field, and
1147 * then check the DSAP.
1149 b0
= gen_cmp(off_linktype
, BPF_H
,
1151 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1157 * This is an Ethernet type, so compare
1158 * the length/type field with it (if
1159 * the frame is an 802.2 frame, the length
1160 * field will be <= ETHERMTU, and, as
1161 * "proto" is > ETHERMTU, this test
1162 * will fail and the frame won't match,
1163 * which is what we want).
1165 return gen_cmp(off_linktype
, BPF_H
,
1172 case DLT_SLIP_BSDOS
:
1175 * These types don't provide any type field; packets
1178 * XXX - for IPv4, check for a version number of 4, and,
1179 * for IPv6, check for a version number of 6?
1185 case ETHERTYPE_IPV6
:
1187 return gen_true(); /* always true */
1190 return gen_false(); /* always false */
1195 case DLT_PPP_SERIAL
:
1198 * We use Ethernet protocol types inside libpcap;
1199 * map them to the corresponding PPP protocol types.
1204 proto
= PPP_IP
; /* XXX was 0x21 */
1208 case ETHERTYPE_IPV6
:
1217 case ETHERTYPE_ATALK
:
1231 * I'm assuming the "Bridging PDU"s that go
1232 * over PPP are Spanning Tree Protocol
1246 * We use Ethernet protocol types inside libpcap;
1247 * map them to the corresponding PPP protocol types.
1252 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1253 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1255 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1260 case ETHERTYPE_IPV6
:
1270 case ETHERTYPE_ATALK
:
1284 * I'm assuming the "Bridging PDU"s that go
1285 * over PPP are Spanning Tree Protocol
1300 * For DLT_NULL, the link-layer header is a 32-bit
1301 * word containing an AF_ value in *host* byte order.
1303 * In addition, if we're reading a saved capture file,
1304 * the host byte order in the capture may not be the
1305 * same as the host byte order on this machine.
1307 * For DLT_LOOP, the link-layer header is a 32-bit
1308 * word containing an AF_ value in *network* byte order.
1310 * XXX - AF_ values may, unfortunately, be platform-
1311 * dependent; for example, FreeBSD's AF_INET6 is 24
1312 * whilst NetBSD's and OpenBSD's is 26.
1314 * This means that, when reading a capture file, just
1315 * checking for our AF_INET6 value won't work if the
1316 * capture file came from another OS.
1325 case ETHERTYPE_IPV6
:
1332 * Not a type on which we support filtering.
1333 * XXX - support those that have AF_ values
1334 * #defined on this platform, at least?
1339 if (linktype
== DLT_NULL
) {
1341 * The AF_ value is in host byte order, but
1342 * the BPF interpreter will convert it to
1343 * network byte order.
1345 * If this is a save file, and it's from a
1346 * machine with the opposite byte order to
1347 * ours, we byte-swap the AF_ value.
1349 * Then we run it through "htonl()", and
1350 * generate code to compare against the result.
1352 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1353 bpf_pcap
->sf
.swapped
)
1354 proto
= SWAPLONG(proto
);
1355 proto
= htonl(proto
);
1357 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1361 * XXX should we check for first fragment if the protocol
1368 case ETHERTYPE_IPV6
:
1369 return(gen_cmp(2, BPF_B
,
1370 (bpf_int32
)htonl(ARCTYPE_INET6
)));
1373 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_IP
));
1374 b1
= gen_cmp(2, BPF_B
,
1375 (bpf_int32
)htonl(ARCTYPE_IP_OLD
));
1379 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_ARP
));
1380 b1
= gen_cmp(2, BPF_B
,
1381 (bpf_int32
)htonl(ARCTYPE_ARP_OLD
));
1384 case ETHERTYPE_REVARP
:
1385 return(gen_cmp(2, BPF_B
,
1386 (bpf_int32
)htonl(ARCTYPE_REVARP
)));
1387 case ETHERTYPE_ATALK
:
1388 return(gen_cmp(2, BPF_B
,
1389 (bpf_int32
)htonl(ARCTYPE_ATALK
)));
1395 case ETHERTYPE_ATALK
:
1404 * All the types that have no encapsulation should either be
1405 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1406 * all packets are IP packets, or should be handled in some
1407 * special case, if none of them are (if some are and some
1408 * aren't, the lack of encapsulation is a problem, as we'd
1409 * have to find some other way of determining the packet type).
1411 * Therefore, if "off_linktype" is -1, there's an error.
1413 if (off_linktype
== -1)
1417 * Any type not handled above should always have an Ethernet
1418 * type at an offset of "off_linktype". (PPP is partially
1419 * handled above - the protocol type is mapped from the
1420 * Ethernet and LLC types we use internally to the corresponding
1421 * PPP type - but the PPP type is always specified by a value
1422 * at "off_linktype", so we don't have to do the code generation
1425 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1429 * Check for an LLC SNAP packet with a given organization code and
1430 * protocol type; we check the entire contents of the 802.2 LLC and
1431 * snap headers, checking for DSAP and SSAP of SNAP and a control
1432 * field of 0x03 in the LLC header, and for the specified organization
1433 * code and protocol type in the SNAP header.
1435 static struct block
*
1436 gen_snap(orgcode
, ptype
, offset
)
1437 bpf_u_int32 orgcode
;
1441 u_char snapblock
[8];
1443 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1444 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1445 snapblock
[2] = 0x03; /* control = UI */
1446 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1447 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1448 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1449 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1450 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1451 return gen_bcmp(offset
, 8, snapblock
);
1454 static struct block
*
1455 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1459 u_int src_off
, dst_off
;
1461 struct block
*b0
, *b1
;
1475 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1476 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1482 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1483 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1490 b0
= gen_linktype(proto
);
1491 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1497 static struct block
*
1498 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1499 struct in6_addr
*addr
;
1500 struct in6_addr
*mask
;
1502 u_int src_off
, dst_off
;
1504 struct block
*b0
, *b1
;
1519 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1520 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1526 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1527 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1534 /* this order is important */
1535 a
= (u_int32_t
*)addr
;
1536 m
= (u_int32_t
*)mask
;
1537 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1538 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1540 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1542 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1544 b0
= gen_linktype(proto
);
1550 static struct block
*
1551 gen_ehostop(eaddr
, dir
)
1552 register const u_char
*eaddr
;
1555 register struct block
*b0
, *b1
;
1559 return gen_bcmp(6, 6, eaddr
);
1562 return gen_bcmp(0, 6, eaddr
);
1565 b0
= gen_ehostop(eaddr
, Q_SRC
);
1566 b1
= gen_ehostop(eaddr
, Q_DST
);
1572 b0
= gen_ehostop(eaddr
, Q_SRC
);
1573 b1
= gen_ehostop(eaddr
, Q_DST
);
1582 * Like gen_ehostop, but for DLT_FDDI
1584 static struct block
*
1585 gen_fhostop(eaddr
, dir
)
1586 register const u_char
*eaddr
;
1589 struct block
*b0
, *b1
;
1594 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1596 return gen_bcmp(6 + 1, 6, eaddr
);
1601 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1603 return gen_bcmp(0 + 1, 6, eaddr
);
1607 b0
= gen_fhostop(eaddr
, Q_SRC
);
1608 b1
= gen_fhostop(eaddr
, Q_DST
);
1614 b0
= gen_fhostop(eaddr
, Q_SRC
);
1615 b1
= gen_fhostop(eaddr
, Q_DST
);
1624 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1626 static struct block
*
1627 gen_thostop(eaddr
, dir
)
1628 register const u_char
*eaddr
;
1631 register struct block
*b0
, *b1
;
1635 return gen_bcmp(8, 6, eaddr
);
1638 return gen_bcmp(2, 6, eaddr
);
1641 b0
= gen_thostop(eaddr
, Q_SRC
);
1642 b1
= gen_thostop(eaddr
, Q_DST
);
1648 b0
= gen_thostop(eaddr
, Q_SRC
);
1649 b1
= gen_thostop(eaddr
, Q_DST
);
1658 * This is quite tricky because there may be pad bytes in front of the
1659 * DECNET header, and then there are two possible data packet formats that
1660 * carry both src and dst addresses, plus 5 packet types in a format that
1661 * carries only the src node, plus 2 types that use a different format and
1662 * also carry just the src node.
1666 * Instead of doing those all right, we just look for data packets with
1667 * 0 or 1 bytes of padding. If you want to look at other packets, that
1668 * will require a lot more hacking.
1670 * To add support for filtering on DECNET "areas" (network numbers)
1671 * one would want to add a "mask" argument to this routine. That would
1672 * make the filter even more inefficient, although one could be clever
1673 * and not generate masking instructions if the mask is 0xFFFF.
1675 static struct block
*
1676 gen_dnhostop(addr
, dir
, base_off
)
1681 struct block
*b0
, *b1
, *b2
, *tmp
;
1682 u_int offset_lh
; /* offset if long header is received */
1683 u_int offset_sh
; /* offset if short header is received */
1688 offset_sh
= 1; /* follows flags */
1689 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1693 offset_sh
= 3; /* follows flags, dstnode */
1694 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1698 /* Inefficient because we do our Calvinball dance twice */
1699 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1700 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1706 /* Inefficient because we do our Calvinball dance twice */
1707 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1708 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1713 bpf_error("ISO host filtering not implemented");
1718 b0
= gen_linktype(ETHERTYPE_DN
);
1719 /* Check for pad = 1, long header case */
1720 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1721 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1722 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1723 BPF_H
, (bpf_int32
)ntohs(addr
));
1725 /* Check for pad = 0, long header case */
1726 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1727 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1730 /* Check for pad = 1, short header case */
1731 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1732 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1733 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1734 BPF_H
, (bpf_int32
)ntohs(addr
));
1737 /* Check for pad = 0, short header case */
1738 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1739 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1743 /* Combine with test for linktype */
1748 static struct block
*
1749 gen_host(addr
, mask
, proto
, dir
)
1755 struct block
*b0
, *b1
;
1760 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1761 if (off_linktype
!= -1) {
1762 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1764 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1770 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1771 off_nl
+ 12, off_nl
+ 16);
1774 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1775 off_nl
+ 14, off_nl
+ 24);
1778 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1779 off_nl
+ 14, off_nl
+ 24);
1782 bpf_error("'tcp' modifier applied to host");
1785 bpf_error("'sctp' modifier applied to host");
1788 bpf_error("'udp' modifier applied to host");
1791 bpf_error("'icmp' modifier applied to host");
1794 bpf_error("'igmp' modifier applied to host");
1797 bpf_error("'igrp' modifier applied to host");
1800 bpf_error("'pim' modifier applied to host");
1803 bpf_error("'vrrp' modifier applied to host");
1806 bpf_error("ATALK host filtering not implemented");
1809 bpf_error("AARP host filtering not implemented");
1812 return gen_dnhostop(addr
, dir
, off_nl
);
1815 bpf_error("SCA host filtering not implemented");
1818 bpf_error("LAT host filtering not implemented");
1821 bpf_error("MOPDL host filtering not implemented");
1824 bpf_error("MOPRC host filtering not implemented");
1828 bpf_error("'ip6' modifier applied to ip host");
1831 bpf_error("'icmp6' modifier applied to host");
1835 bpf_error("'ah' modifier applied to host");
1838 bpf_error("'esp' modifier applied to host");
1841 bpf_error("ISO host filtering not implemented");
1844 bpf_error("'esis' modifier applied to host");
1847 bpf_error("'isis' modifier applied to host");
1850 bpf_error("'clnp' modifier applied to host");
1853 bpf_error("'stp' modifier applied to host");
1856 bpf_error("IPX host filtering not implemented");
1859 bpf_error("'netbeui' modifier applied to host");
1868 static struct block
*
1869 gen_host6(addr
, mask
, proto
, dir
)
1870 struct in6_addr
*addr
;
1871 struct in6_addr
*mask
;
1878 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1881 bpf_error("'ip' modifier applied to ip6 host");
1884 bpf_error("'rarp' modifier applied to ip6 host");
1887 bpf_error("'arp' modifier applied to ip6 host");
1890 bpf_error("'sctp' modifier applied to host");
1893 bpf_error("'tcp' modifier applied to host");
1896 bpf_error("'udp' modifier applied to host");
1899 bpf_error("'icmp' modifier applied to host");
1902 bpf_error("'igmp' modifier applied to host");
1905 bpf_error("'igrp' modifier applied to host");
1908 bpf_error("'pim' modifier applied to host");
1911 bpf_error("'vrrp' modifier applied to host");
1914 bpf_error("ATALK host filtering not implemented");
1917 bpf_error("AARP host filtering not implemented");
1920 bpf_error("'decnet' modifier applied to ip6 host");
1923 bpf_error("SCA host filtering not implemented");
1926 bpf_error("LAT host filtering not implemented");
1929 bpf_error("MOPDL host filtering not implemented");
1932 bpf_error("MOPRC host filtering not implemented");
1935 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1936 off_nl
+ 8, off_nl
+ 24);
1939 bpf_error("'icmp6' modifier applied to host");
1942 bpf_error("'ah' modifier applied to host");
1945 bpf_error("'esp' modifier applied to host");
1948 bpf_error("ISO host filtering not implemented");
1951 bpf_error("'esis' modifier applied to host");
1954 bpf_error("'isis' modifier applied to host");
1957 bpf_error("'clnp' modifier applied to host");
1960 bpf_error("'stp' modifier applied to host");
1963 bpf_error("IPX host filtering not implemented");
1966 bpf_error("'netbeui' modifier applied to host");
1976 static struct block
*
1977 gen_gateway(eaddr
, alist
, proto
, dir
)
1978 const u_char
*eaddr
;
1979 bpf_u_int32
**alist
;
1983 struct block
*b0
, *b1
, *tmp
;
1986 bpf_error("direction applied to 'gateway'");
1993 if (linktype
== DLT_EN10MB
)
1994 b0
= gen_ehostop(eaddr
, Q_OR
);
1995 else if (linktype
== DLT_FDDI
)
1996 b0
= gen_fhostop(eaddr
, Q_OR
);
1997 else if (linktype
== DLT_IEEE802
)
1998 b0
= gen_thostop(eaddr
, Q_OR
);
2001 "'gateway' supported only on ethernet, FDDI or token ring");
2003 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2005 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2013 bpf_error("illegal modifier of 'gateway'");
2019 gen_proto_abbrev(proto
)
2030 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2032 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2038 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2040 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2046 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2048 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2054 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2057 #ifndef IPPROTO_IGMP
2058 #define IPPROTO_IGMP 2
2062 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2065 #ifndef IPPROTO_IGRP
2066 #define IPPROTO_IGRP 9
2069 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2073 #define IPPROTO_PIM 103
2077 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2079 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2084 #ifndef IPPROTO_VRRP
2085 #define IPPROTO_VRRP 112
2089 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2093 b1
= gen_linktype(ETHERTYPE_IP
);
2097 b1
= gen_linktype(ETHERTYPE_ARP
);
2101 b1
= gen_linktype(ETHERTYPE_REVARP
);
2105 bpf_error("link layer applied in wrong context");
2108 b1
= gen_linktype(ETHERTYPE_ATALK
);
2112 b1
= gen_linktype(ETHERTYPE_AARP
);
2116 b1
= gen_linktype(ETHERTYPE_DN
);
2120 b1
= gen_linktype(ETHERTYPE_SCA
);
2124 b1
= gen_linktype(ETHERTYPE_LAT
);
2128 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2132 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2137 b1
= gen_linktype(ETHERTYPE_IPV6
);
2140 #ifndef IPPROTO_ICMPV6
2141 #define IPPROTO_ICMPV6 58
2144 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2149 #define IPPROTO_AH 51
2152 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2154 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2160 #define IPPROTO_ESP 50
2163 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2165 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2171 b1
= gen_linktype(LLCSAP_ISONS
);
2175 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2179 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2183 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2187 b1
= gen_linktype(LLCSAP_8021D
);
2191 b1
= gen_linktype(LLCSAP_IPX
);
2195 b1
= gen_linktype(LLCSAP_NETBEUI
);
2204 static struct block
*
2211 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2212 s
->s
.k
= off_nl
+ 6;
2213 b
= new_block(JMP(BPF_JSET
));
2221 static struct block
*
2222 gen_portatom(off
, v
)
2229 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2232 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2233 s
->next
->s
.k
= off_nl
+ off
;
2235 b
= new_block(JMP(BPF_JEQ
));
2243 static struct block
*
2244 gen_portatom6(off
, v
)
2248 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2253 gen_portop(port
, proto
, dir
)
2254 int port
, proto
, dir
;
2256 struct block
*b0
, *b1
, *tmp
;
2258 /* ip proto 'proto' */
2259 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2265 b1
= gen_portatom(0, (bpf_int32
)port
);
2269 b1
= gen_portatom(2, (bpf_int32
)port
);
2274 tmp
= gen_portatom(0, (bpf_int32
)port
);
2275 b1
= gen_portatom(2, (bpf_int32
)port
);
2280 tmp
= gen_portatom(0, (bpf_int32
)port
);
2281 b1
= gen_portatom(2, (bpf_int32
)port
);
2293 static struct block
*
2294 gen_port(port
, ip_proto
, dir
)
2299 struct block
*b0
, *b1
, *tmp
;
2301 /* ether proto ip */
2302 b0
= gen_linktype(ETHERTYPE_IP
);
2308 b1
= gen_portop(port
, ip_proto
, dir
);
2312 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2313 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2315 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
2328 gen_portop6(port
, proto
, dir
)
2329 int port
, proto
, dir
;
2331 struct block
*b0
, *b1
, *tmp
;
2333 /* ip proto 'proto' */
2334 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2338 b1
= gen_portatom6(0, (bpf_int32
)port
);
2342 b1
= gen_portatom6(2, (bpf_int32
)port
);
2347 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2348 b1
= gen_portatom6(2, (bpf_int32
)port
);
2353 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2354 b1
= gen_portatom6(2, (bpf_int32
)port
);
2366 static struct block
*
2367 gen_port6(port
, ip_proto
, dir
)
2372 struct block
*b0
, *b1
, *tmp
;
2374 /* ether proto ip */
2375 b0
= gen_linktype(ETHERTYPE_IPV6
);
2381 b1
= gen_portop6(port
, ip_proto
, dir
);
2385 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2386 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2388 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
2401 lookup_proto(name
, proto
)
2402 register const char *name
;
2412 v
= pcap_nametoproto(name
);
2413 if (v
== PROTO_UNDEF
)
2414 bpf_error("unknown ip proto '%s'", name
);
2418 /* XXX should look up h/w protocol type based on linktype */
2419 v
= pcap_nametoeproto(name
);
2420 if (v
== PROTO_UNDEF
)
2421 bpf_error("unknown ether proto '%s'", name
);
2425 if (strcmp(name
, "esis") == 0)
2427 else if (strcmp(name
, "isis") == 0)
2429 else if (strcmp(name
, "clnp") == 0)
2432 bpf_error("unknown osi proto '%s'", name
);
2452 static struct block
*
2453 gen_protochain(v
, proto
, dir
)
2458 #ifdef NO_PROTOCHAIN
2459 return gen_proto(v
, proto
, dir
);
2461 struct block
*b0
, *b
;
2462 struct slist
*s
[100];
2463 int fix2
, fix3
, fix4
, fix5
;
2464 int ahcheck
, again
, end
;
2466 int reg2
= alloc_reg();
2468 memset(s
, 0, sizeof(s
));
2469 fix2
= fix3
= fix4
= fix5
= 0;
2476 b0
= gen_protochain(v
, Q_IP
, dir
);
2477 b
= gen_protochain(v
, Q_IPV6
, dir
);
2481 bpf_error("bad protocol applied for 'protochain'");
2485 no_optimize
= 1; /*this code is not compatible with optimzer yet */
2488 * s[0] is a dummy entry to protect other BPF insn from damaged
2489 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
2490 * hard to find interdependency made by jump table fixup.
2493 s
[i
] = new_stmt(0); /*dummy*/
2498 b0
= gen_linktype(ETHERTYPE_IP
);
2501 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2502 s
[i
]->s
.k
= off_nl
+ 9;
2504 /* X = ip->ip_hl << 2 */
2505 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2511 b0
= gen_linktype(ETHERTYPE_IPV6
);
2513 /* A = ip6->ip_nxt */
2514 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2515 s
[i
]->s
.k
= off_nl
+ 6;
2517 /* X = sizeof(struct ip6_hdr) */
2518 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
2524 bpf_error("unsupported proto to gen_protochain");
2528 /* again: if (A == v) goto end; else fall through; */
2530 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2532 s
[i
]->s
.jt
= NULL
; /*later*/
2533 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2537 #ifndef IPPROTO_NONE
2538 #define IPPROTO_NONE 59
2540 /* if (A == IPPROTO_NONE) goto end */
2541 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2542 s
[i
]->s
.jt
= NULL
; /*later*/
2543 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2544 s
[i
]->s
.k
= IPPROTO_NONE
;
2545 s
[fix5
]->s
.jf
= s
[i
];
2550 if (proto
== Q_IPV6
) {
2551 int v6start
, v6end
, v6advance
, j
;
2554 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
2555 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2556 s
[i
]->s
.jt
= NULL
; /*later*/
2557 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2558 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
2559 s
[fix2
]->s
.jf
= s
[i
];
2561 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
2562 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2563 s
[i
]->s
.jt
= NULL
; /*later*/
2564 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2565 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
2567 /* if (A == IPPROTO_ROUTING) goto v6advance */
2568 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2569 s
[i
]->s
.jt
= NULL
; /*later*/
2570 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2571 s
[i
]->s
.k
= IPPROTO_ROUTING
;
2573 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
2574 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2575 s
[i
]->s
.jt
= NULL
; /*later*/
2576 s
[i
]->s
.jf
= NULL
; /*later*/
2577 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
2588 * X = X + (P[X + 1] + 1) * 8;
2591 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2593 /* A = P[X + packet head] */
2594 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2598 s
[i
] = new_stmt(BPF_ST
);
2602 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2605 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2609 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2611 /* A = P[X + packet head]; */
2612 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2616 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2620 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2624 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2627 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2631 /* goto again; (must use BPF_JA for backward jump) */
2632 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2633 s
[i
]->s
.k
= again
- i
- 1;
2634 s
[i
- 1]->s
.jf
= s
[i
];
2638 for (j
= v6start
; j
<= v6end
; j
++)
2639 s
[j
]->s
.jt
= s
[v6advance
];
2644 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2646 s
[fix2
]->s
.jf
= s
[i
];
2652 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2653 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2654 s
[i
]->s
.jt
= NULL
; /*later*/
2655 s
[i
]->s
.jf
= NULL
; /*later*/
2656 s
[i
]->s
.k
= IPPROTO_AH
;
2658 s
[fix3
]->s
.jf
= s
[ahcheck
];
2665 * X = X + (P[X + 1] + 2) * 4;
2668 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2670 /* A = P[X + packet head]; */
2671 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2675 s
[i
] = new_stmt(BPF_ST
);
2679 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2682 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2686 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2688 /* A = P[X + packet head] */
2689 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2693 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2697 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2701 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2704 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2708 /* goto again; (must use BPF_JA for backward jump) */
2709 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2710 s
[i
]->s
.k
= again
- i
- 1;
2715 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2717 s
[fix2
]->s
.jt
= s
[end
];
2718 s
[fix4
]->s
.jf
= s
[end
];
2719 s
[fix5
]->s
.jt
= s
[end
];
2726 for (i
= 0; i
< max
- 1; i
++)
2727 s
[i
]->next
= s
[i
+ 1];
2728 s
[max
- 1]->next
= NULL
;
2733 b
= new_block(JMP(BPF_JEQ
));
2734 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2744 static struct block
*
2745 gen_proto(v
, proto
, dir
)
2750 struct block
*b0
, *b1
;
2752 if (dir
!= Q_DEFAULT
)
2753 bpf_error("direction applied to 'proto'");
2758 b0
= gen_proto(v
, Q_IP
, dir
);
2759 b1
= gen_proto(v
, Q_IPV6
, dir
);
2766 b0
= gen_linktype(ETHERTYPE_IP
);
2768 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2770 b1
= gen_protochain(v
, Q_IP
);
2776 b0
= gen_linktype(LLCSAP_ISONS
);
2777 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2782 bpf_error("arp does not encapsulate another protocol");
2786 bpf_error("rarp does not encapsulate another protocol");
2790 bpf_error("atalk encapsulation is not specifiable");
2794 bpf_error("decnet encapsulation is not specifiable");
2798 bpf_error("sca does not encapsulate another protocol");
2802 bpf_error("lat does not encapsulate another protocol");
2806 bpf_error("moprc does not encapsulate another protocol");
2810 bpf_error("mopdl does not encapsulate another protocol");
2814 return gen_linktype(v
);
2817 bpf_error("'udp proto' is bogus");
2821 bpf_error("'tcp proto' is bogus");
2825 bpf_error("'sctp proto' is bogus");
2829 bpf_error("'icmp proto' is bogus");
2833 bpf_error("'igmp proto' is bogus");
2837 bpf_error("'igrp proto' is bogus");
2841 bpf_error("'pim proto' is bogus");
2845 bpf_error("'vrrp proto' is bogus");
2850 b0
= gen_linktype(ETHERTYPE_IPV6
);
2852 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2854 b1
= gen_protochain(v
, Q_IPV6
);
2860 bpf_error("'icmp6 proto' is bogus");
2864 bpf_error("'ah proto' is bogus");
2867 bpf_error("'ah proto' is bogus");
2870 bpf_error("'stp proto' is bogus");
2873 bpf_error("'ipx proto' is bogus");
2876 bpf_error("'netbeui proto' is bogus");
2887 register const char *name
;
2890 int proto
= q
.proto
;
2894 bpf_u_int32 mask
, addr
;
2896 bpf_u_int32
**alist
;
2899 struct sockaddr_in
*sin
;
2900 struct sockaddr_in6
*sin6
;
2901 struct addrinfo
*res
, *res0
;
2902 struct in6_addr mask128
;
2904 struct block
*b
, *tmp
;
2905 int port
, real_proto
;
2910 addr
= pcap_nametonetaddr(name
);
2912 bpf_error("unknown network '%s'", name
);
2913 /* Left justify network addr and calculate its network mask */
2915 while (addr
&& (addr
& 0xff000000) == 0) {
2919 return gen_host(addr
, mask
, proto
, dir
);
2923 if (proto
== Q_LINK
) {
2927 eaddr
= pcap_ether_hostton(name
);
2930 "unknown ether host '%s'", name
);
2931 b
= gen_ehostop(eaddr
, dir
);
2936 eaddr
= pcap_ether_hostton(name
);
2939 "unknown FDDI host '%s'", name
);
2940 b
= gen_fhostop(eaddr
, dir
);
2945 eaddr
= pcap_ether_hostton(name
);
2948 "unknown token ring host '%s'", name
);
2949 b
= gen_thostop(eaddr
, dir
);
2955 "only ethernet/FDDI/token ring supports link-level host name");
2958 } else if (proto
== Q_DECNET
) {
2959 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2961 * I don't think DECNET hosts can be multihomed, so
2962 * there is no need to build up a list of addresses
2964 return (gen_host(dn_addr
, 0, proto
, dir
));
2967 alist
= pcap_nametoaddr(name
);
2968 if (alist
== NULL
|| *alist
== NULL
)
2969 bpf_error("unknown host '%s'", name
);
2971 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2973 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2975 tmp
= gen_host(**alist
++, 0xffffffff,
2982 memset(&mask128
, 0xff, sizeof(mask128
));
2983 res0
= res
= pcap_nametoaddrinfo(name
);
2985 bpf_error("unknown host '%s'", name
);
2987 tproto
= tproto6
= proto
;
2988 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2992 for (res
= res0
; res
; res
= res
->ai_next
) {
2993 switch (res
->ai_family
) {
2995 if (tproto
== Q_IPV6
)
2998 sin
= (struct sockaddr_in
*)
3000 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
3001 0xffffffff, tproto
, dir
);
3004 if (tproto6
== Q_IP
)
3007 sin6
= (struct sockaddr_in6
*)
3009 tmp
= gen_host6(&sin6
->sin6_addr
,
3010 &mask128
, tproto6
, dir
);
3021 bpf_error("unknown host '%s'%s", name
,
3022 (proto
== Q_DEFAULT
)
3024 : " for specified address family");
3031 if (proto
!= Q_DEFAULT
&&
3032 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
3033 bpf_error("illegal qualifier of 'port'");
3034 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
3035 bpf_error("unknown port '%s'", name
);
3036 if (proto
== Q_UDP
) {
3037 if (real_proto
== IPPROTO_TCP
)
3038 bpf_error("port '%s' is tcp", name
);
3039 else if (real_proto
== IPPROTO_SCTP
)
3040 bpf_error("port '%s' is sctp", name
);
3042 /* override PROTO_UNDEF */
3043 real_proto
= IPPROTO_UDP
;
3045 if (proto
== Q_TCP
) {
3046 if (real_proto
== IPPROTO_UDP
)
3047 bpf_error("port '%s' is udp", name
);
3049 else if (real_proto
== IPPROTO_SCTP
)
3050 bpf_error("port '%s' is sctp", name
);
3052 /* override PROTO_UNDEF */
3053 real_proto
= IPPROTO_TCP
;
3055 if (proto
== Q_SCTP
) {
3056 if (real_proto
== IPPROTO_UDP
)
3057 bpf_error("port '%s' is udp", name
);
3059 else if (real_proto
== IPPROTO_TCP
)
3060 bpf_error("port '%s' is tcp", name
);
3062 /* override PROTO_UNDEF */
3063 real_proto
= IPPROTO_SCTP
;
3066 return gen_port(port
, real_proto
, dir
);
3070 b
= gen_port(port
, real_proto
, dir
);
3071 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3078 eaddr
= pcap_ether_hostton(name
);
3080 bpf_error("unknown ether host: %s", name
);
3082 alist
= pcap_nametoaddr(name
);
3083 if (alist
== NULL
|| *alist
== NULL
)
3084 bpf_error("unknown host '%s'", name
);
3085 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
3089 bpf_error("'gateway' not supported in this configuration");
3093 real_proto
= lookup_proto(name
, proto
);
3094 if (real_proto
>= 0)
3095 return gen_proto(real_proto
, proto
, dir
);
3097 bpf_error("unknown protocol: %s", name
);
3100 real_proto
= lookup_proto(name
, proto
);
3101 if (real_proto
>= 0)
3102 return gen_protochain(real_proto
, proto
, dir
);
3104 bpf_error("unknown protocol: %s", name
);
3116 gen_mcode(s1
, s2
, masklen
, q
)
3117 register const char *s1
, *s2
;
3118 register int masklen
;
3121 register int nlen
, mlen
;
3124 nlen
= __pcap_atoin(s1
, &n
);
3125 /* Promote short ipaddr */
3129 mlen
= __pcap_atoin(s2
, &m
);
3130 /* Promote short ipaddr */
3133 bpf_error("non-network bits set in \"%s mask %s\"",
3136 /* Convert mask len to mask */
3138 bpf_error("mask length must be <= 32");
3139 m
= 0xffffffff << (32 - masklen
);
3141 bpf_error("non-network bits set in \"%s/%d\"",
3148 return gen_host(n
, m
, q
.proto
, q
.dir
);
3151 bpf_error("Mask syntax for networks only");
3158 register const char *s
;
3163 int proto
= q
.proto
;
3169 else if (q
.proto
== Q_DECNET
)
3170 vlen
= __pcap_atodn(s
, &v
);
3172 vlen
= __pcap_atoin(s
, &v
);
3179 if (proto
== Q_DECNET
)
3180 return gen_host(v
, 0, proto
, dir
);
3181 else if (proto
== Q_LINK
) {
3182 bpf_error("illegal link layer address");
3185 if (s
== NULL
&& q
.addr
== Q_NET
) {
3186 /* Promote short net number */
3187 while (v
&& (v
& 0xff000000) == 0) {
3192 /* Promote short ipaddr */
3196 return gen_host(v
, mask
, proto
, dir
);
3201 proto
= IPPROTO_UDP
;
3202 else if (proto
== Q_TCP
)
3203 proto
= IPPROTO_TCP
;
3204 else if (proto
== Q_SCTP
)
3205 proto
= IPPROTO_SCTP
;
3206 else if (proto
== Q_DEFAULT
)
3207 proto
= PROTO_UNDEF
;
3209 bpf_error("illegal qualifier of 'port'");
3212 return gen_port((int)v
, proto
, dir
);
3216 b
= gen_port((int)v
, proto
, dir
);
3217 gen_or(gen_port6((int)v
, proto
, dir
), b
);
3223 bpf_error("'gateway' requires a name");
3227 return gen_proto((int)v
, proto
, dir
);
3230 return gen_protochain((int)v
, proto
, dir
);
3245 gen_mcode6(s1
, s2
, masklen
, q
)
3246 register const char *s1
, *s2
;
3247 register int masklen
;
3250 struct addrinfo
*res
;
3251 struct in6_addr
*addr
;
3252 struct in6_addr mask
;
3257 bpf_error("no mask %s supported", s2
);
3259 res
= pcap_nametoaddrinfo(s1
);
3261 bpf_error("invalid ip6 address %s", s1
);
3263 bpf_error("%s resolved to multiple address", s1
);
3264 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3266 if (sizeof(mask
) * 8 < masklen
)
3267 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3268 memset(&mask
, 0, sizeof(mask
));
3269 memset(&mask
, 0xff, masklen
/ 8);
3271 mask
.s6_addr
[masklen
/ 8] =
3272 (0xff << (8 - masklen
% 8)) & 0xff;
3275 a
= (u_int32_t
*)addr
;
3276 m
= (u_int32_t
*)&mask
;
3277 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3278 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3279 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
3287 bpf_error("Mask syntax for networks only");
3291 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
3296 bpf_error("invalid qualifier against IPv6 address");
3304 register const u_char
*eaddr
;
3307 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3308 if (linktype
== DLT_EN10MB
)
3309 return gen_ehostop(eaddr
, (int)q
.dir
);
3310 if (linktype
== DLT_FDDI
)
3311 return gen_fhostop(eaddr
, (int)q
.dir
);
3312 if (linktype
== DLT_IEEE802
)
3313 return gen_thostop(eaddr
, (int)q
.dir
);
3314 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
3316 bpf_error("ethernet address used in non-ether expression");
3322 struct slist
*s0
, *s1
;
3325 * This is definitely not the best way to do this, but the
3326 * lists will rarely get long.
3333 static struct slist
*
3339 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3344 static struct slist
*
3350 s
= new_stmt(BPF_LD
|BPF_MEM
);
3356 gen_load(proto
, index
, size
)
3361 struct slist
*s
, *tmp
;
3363 int regno
= alloc_reg();
3365 free_reg(index
->regno
);
3369 bpf_error("data size must be 1, 2, or 4");
3385 bpf_error("unsupported index operation");
3388 s
= xfer_to_x(index
);
3389 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3391 sappend(index
->s
, s
);
3406 /* XXX Note that we assume a fixed link header here. */
3407 s
= xfer_to_x(index
);
3408 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3411 sappend(index
->s
, s
);
3413 b
= gen_proto_abbrev(proto
);
3415 gen_and(index
->b
, b
);
3427 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3429 sappend(s
, xfer_to_a(index
));
3430 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3431 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3432 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3434 sappend(index
->s
, s
);
3436 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
3438 gen_and(index
->b
, b
);
3440 gen_and(gen_proto_abbrev(Q_IP
), b
);
3446 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
3450 index
->regno
= regno
;
3451 s
= new_stmt(BPF_ST
);
3453 sappend(index
->s
, s
);
3459 gen_relation(code
, a0
, a1
, reversed
)
3461 struct arth
*a0
, *a1
;
3464 struct slist
*s0
, *s1
, *s2
;
3465 struct block
*b
, *tmp
;
3469 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
3470 b
= new_block(JMP(code
));
3471 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
3472 reversed
= !reversed
;
3473 b
->s
.k
= 0x80000000;
3481 sappend(a0
->s
, a1
->s
);
3485 free_reg(a0
->regno
);
3486 free_reg(a1
->regno
);
3488 /* 'and' together protocol checks */
3491 gen_and(a0
->b
, tmp
= a1
->b
);
3507 int regno
= alloc_reg();
3508 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
3511 s
= new_stmt(BPF_LD
|BPF_LEN
);
3512 s
->next
= new_stmt(BPF_ST
);
3513 s
->next
->s
.k
= regno
;
3528 a
= (struct arth
*)newchunk(sizeof(*a
));
3532 s
= new_stmt(BPF_LD
|BPF_IMM
);
3534 s
->next
= new_stmt(BPF_ST
);
3550 s
= new_stmt(BPF_ALU
|BPF_NEG
);
3553 s
= new_stmt(BPF_ST
);
3561 gen_arth(code
, a0
, a1
)
3563 struct arth
*a0
, *a1
;
3565 struct slist
*s0
, *s1
, *s2
;
3569 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
3574 sappend(a0
->s
, a1
->s
);
3576 free_reg(a1
->regno
);
3578 s0
= new_stmt(BPF_ST
);
3579 a0
->regno
= s0
->s
.k
= alloc_reg();
3586 * Here we handle simple allocation of the scratch registers.
3587 * If too many registers are alloc'd, the allocator punts.
3589 static int regused
[BPF_MEMWORDS
];
3593 * Return the next free register.
3598 int n
= BPF_MEMWORDS
;
3601 if (regused
[curreg
])
3602 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
3604 regused
[curreg
] = 1;
3608 bpf_error("too many registers needed to evaluate expression");
3613 * Return a register to the table so it can
3623 static struct block
*
3630 s
= new_stmt(BPF_LD
|BPF_LEN
);
3631 b
= new_block(JMP(jmp
));
3642 return gen_len(BPF_JGE
, n
);
3646 * Actually, this is less than or equal.
3654 b
= gen_len(BPF_JGT
, n
);
3661 gen_byteop(op
, idx
, val
)
3672 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3675 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3676 b
->s
.code
= JMP(BPF_JGE
);
3681 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3682 b
->s
.code
= JMP(BPF_JGT
);
3686 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3690 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3694 b
= new_block(JMP(BPF_JEQ
));
3701 static u_char abroadcast
[] = { 0x0 };
3704 gen_broadcast(proto
)
3707 bpf_u_int32 hostmask
;
3708 struct block
*b0
, *b1
, *b2
;
3709 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3715 if (linktype
== DLT_ARCNET
)
3716 return gen_ahostop(abroadcast
, Q_DST
);
3717 if (linktype
== DLT_EN10MB
)
3718 return gen_ehostop(ebroadcast
, Q_DST
);
3719 if (linktype
== DLT_FDDI
)
3720 return gen_fhostop(ebroadcast
, Q_DST
);
3721 if (linktype
== DLT_IEEE802
)
3722 return gen_thostop(ebroadcast
, Q_DST
);
3723 bpf_error("not a broadcast link");
3727 b0
= gen_linktype(ETHERTYPE_IP
);
3728 hostmask
= ~netmask
;
3729 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3730 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3731 (bpf_int32
)(~0 & hostmask
), hostmask
);
3736 bpf_error("only ether/ip broadcast filters supported");
3740 gen_multicast(proto
)
3743 register struct block
*b0
, *b1
;
3744 register struct slist
*s
;
3750 if (linktype
== DLT_ARCNET
)
3751 /* all ARCnet multicasts use the same address */
3752 return gen_ahostop(abroadcast
, Q_DST
);
3754 if (linktype
== DLT_EN10MB
) {
3755 /* ether[0] & 1 != 0 */
3756 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3758 b0
= new_block(JMP(BPF_JSET
));
3764 if (linktype
== DLT_FDDI
) {
3765 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3766 /* fddi[1] & 1 != 0 */
3767 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3769 b0
= new_block(JMP(BPF_JSET
));
3775 /* TODO - check how token ring handles multicast */
3776 /* if (linktype == DLT_IEEE802) ... */
3778 /* Link not known to support multicasts */
3782 b0
= gen_linktype(ETHERTYPE_IP
);
3783 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3784 b1
->s
.code
= JMP(BPF_JGE
);
3790 b0
= gen_linktype(ETHERTYPE_IPV6
);
3791 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3796 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3800 * generate command for inbound/outbound. It's here so we can
3801 * make it link-type specific. 'dir' = 0 implies "inbound",
3802 * = 1 implies "outbound".
3808 register struct block
*b0
;
3811 * Only some data link types support inbound/outbound qualifiers.
3816 b0
= gen_relation(BPF_JEQ
,
3817 gen_load(Q_LINK
, gen_loadi(0), 1),
3823 bpf_error("inbound/outbound not supported on linktype %d\n",
3833 register const u_char
*eaddr
;
3836 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3837 if (linktype
== DLT_ARCNET
)
3838 return gen_ahostop(eaddr
, (int)q
.dir
);
3840 bpf_error("ARCnet address used in non-arc expression");
3844 static struct block
*
3845 gen_ahostop(eaddr
, dir
)
3846 register const u_char
*eaddr
;
3849 register struct block
*b0
, *b1
;
3852 /* src comes first, different from Ethernet */
3854 return gen_bcmp(0, 1, eaddr
);
3857 return gen_bcmp(1, 1, eaddr
);
3860 b0
= gen_ahostop(eaddr
, Q_SRC
);
3861 b1
= gen_ahostop(eaddr
, Q_DST
);
3867 b0
= gen_ahostop(eaddr
, Q_SRC
);
3868 b1
= gen_ahostop(eaddr
, Q_DST
);
3877 * support IEEE 802.1Q VLAN trunk over ethernet
3886 * Change the offsets to point to the type and data fields within
3887 * the VLAN packet. This is somewhat of a kludge.
3889 if (orig_nl
== (u_int
)-1) {
3890 orig_linktype
= off_linktype
; /* save original values */
3901 bpf_error("no VLAN support for data link type %d",
3907 /* check for VLAN */
3908 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3910 /* If a specific VLAN is requested, check VLAN id */
3911 if (vlan_num
>= 0) {
3914 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);