]>
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.156 2001-06-20 07:12:38 guy 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 */
597 * This does no include the Ethernet header, and
598 * only covers session state.
611 * FDDI doesn't really have a link-level type field.
612 * We set "off_linktype" to the offset of the LLC header.
614 * To check for Ethernet types, we assume that SSAP = SNAP
615 * is being used and pick out the encapsulated Ethernet type.
616 * XXX - should we generate code to check for SNAP?
620 off_linktype
+= pcap_fddipad
;
624 off_nl
+= pcap_fddipad
;
630 * Token Ring doesn't really have a link-level type field.
631 * We set "off_linktype" to the offset of the LLC header.
633 * To check for Ethernet types, we assume that SSAP = SNAP
634 * is being used and pick out the encapsulated Ethernet type.
635 * XXX - should we generate code to check for SNAP?
637 * XXX - the header is actually variable-length.
638 * Some various Linux patched versions gave 38
639 * as "off_linktype" and 40 as "off_nl"; however,
640 * if a token ring packet has *no* routing
641 * information, i.e. is not source-routed, the correct
642 * values are 20 and 22, as they are in the vanilla code.
644 * A packet is source-routed iff the uppermost bit
645 * of the first byte of the source address, at an
646 * offset of 8, has the uppermost bit set. If the
647 * packet is source-routed, the total number of bytes
648 * of routing information is 2 plus bits 0x1F00 of
649 * the 16-bit value at an offset of 14 (shifted right
650 * 8 - figure out which byte that is).
658 * 802.11 doesn't really have a link-level type field.
659 * We set "off_linktype" to the offset of the LLC header.
661 * To check for Ethernet types, we assume that SSAP = SNAP
662 * is being used and pick out the encapsulated Ethernet type.
663 * XXX - should we generate code to check for SNAP?
665 * XXX - the header is actually variable-length. We
666 * assume a 24-byte link-layer header, as appears in
667 * data frames in networks with no bridges.
673 case DLT_ATM_RFC1483
:
675 * assume routed, non-ISO PDUs
676 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
687 case DLT_ATM_CLIP
: /* Linux ATM defines this */
692 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
699 * LocalTalk does have a 1-byte type field in the LLAP header,
700 * but really it just indicates whether there is a "short" or
701 * "long" DDP packet following.
707 bpf_error("unknown data link type %d", linktype
);
711 static struct block
*
718 s
= new_stmt(BPF_LD
|BPF_IMM
);
720 b
= new_block(JMP(BPF_JEQ
));
726 static inline struct block
*
729 return gen_uncond(1);
732 static inline struct block
*
735 return gen_uncond(0);
739 * Byte-swap a 32-bit number.
740 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
741 * big-endian platforms.)
743 #define SWAPLONG(y) \
744 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
746 static struct block
*
750 struct block
*b0
, *b1
;
759 * OSI protocols always use 802.2 encapsulation.
760 * XXX - should we check both the DSAP and the
761 * SSAP, like this, or should we check just the
764 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
766 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
767 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
773 * NetBEUI always uses 802.2 encapsulation.
774 * XXX - should we check both the DSAP and the
775 * SSAP, like this, or should we check just the
778 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
780 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
781 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
789 * Ethernet_II frames, which are Ethernet
790 * frames with a frame type of ETHERTYPE_IPX;
792 * Ethernet_802.3 frames, which are 802.3
793 * frames (i.e., the type/length field is
794 * a length field, <= ETHERMTU, rather than
795 * a type field) with the first two bytes
796 * after the Ethernet/802.3 header being
799 * Ethernet_802.2 frames, which are 802.3
800 * frames with an 802.2 LLC header and
801 * with the IPX LSAP as the DSAP in the LLC
804 * Ethernet_SNAP frames, which are 802.3
805 * frames with an LLC header and a SNAP
806 * header and with an OUI of 0x000000
807 * (encapsulated Ethernet) and a protocol
808 * ID of ETHERTYPE_IPX in the SNAP header.
810 * XXX - should we generate the same code both
811 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
815 * This generates code to check both for the
816 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
818 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
819 (bpf_int32
)LLCSAP_IPX
);
820 b1
= gen_cmp(off_linktype
+ 2, BPF_H
,
825 * Now we add code to check for SNAP frames with
826 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
828 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
832 * Now we generate code to check for 802.3
835 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
839 * Now add the check for 802.3 frames before the
840 * check for Ethernet_802.2 and Ethernet_802.3,
841 * as those checks should only be done on 802.3
842 * frames, not on Ethernet frames.
847 * Now add the check for Ethernet_II frames, and
848 * do that before checking for the other frame
851 b0
= gen_cmp(off_linktype
, BPF_H
,
852 (bpf_int32
)ETHERTYPE_IPX
);
856 case ETHERTYPE_ATALK
:
859 * EtherTalk (AppleTalk protocols on Ethernet link
860 * layer) may use 802.2 encapsulation.
864 * Check for 802.2 encapsulation (EtherTalk phase 2?);
865 * we check for an Ethernet type field less than
866 * 1500, which means it's an 802.3 length field.
868 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
872 * 802.2-encapsulated ETHERTYPE_ATALK packets are
873 * SNAP packets with an organization code of
874 * 0x080007 (Apple, for Appletalk) and a protocol
875 * type of ETHERTYPE_ATALK (Appletalk).
877 * 802.2-encapsulated ETHERTYPE_AARP packets are
878 * SNAP packets with an organization code of
879 * 0x000000 (encapsulated Ethernet) and a protocol
880 * type of ETHERTYPE_AARP (Appletalk ARP).
882 if (proto
== ETHERTYPE_ATALK
)
883 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
884 else /* proto == ETHERTYPE_AARP */
885 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
889 * Check for Ethernet encapsulation (Ethertalk
890 * phase 1?); we just check for the Ethernet
893 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
899 if (proto
<= ETHERMTU
) {
901 * This is an LLC SAP value, so the frames
902 * that match would be 802.2 frames.
903 * Check that the frame is an 802.2 frame
904 * (i.e., that the length/type field is
905 * a length field, <= ETHERMTU) and
906 * then check the DSAP.
908 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
910 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
916 * This is an Ethernet type, so compare
917 * the length/type field with it (if
918 * the frame is an 802.2 frame, the length
919 * field will be <= ETHERMTU, and, as
920 * "proto" is > ETHERMTU, this test
921 * will fail and the frame won't match,
922 * which is what we want).
924 return gen_cmp(off_linktype
, BPF_H
,
933 case DLT_ATM_RFC1483
:
936 * XXX - handle token-ring variable-length header.
941 return gen_cmp(off_linktype
, BPF_H
, (long)
942 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
945 return gen_cmp(off_linktype
, BPF_H
, (long)
946 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
950 * XXX - are there ever SNAP frames for IPX on
951 * non-Ethernet 802.x networks?
953 return gen_cmp(off_linktype
, BPF_B
,
954 (bpf_int32
)LLCSAP_IPX
);
956 case ETHERTYPE_ATALK
:
958 * 802.2-encapsulated ETHERTYPE_ATALK packets are
959 * SNAP packets with an organization code of
960 * 0x080007 (Apple, for Appletalk) and a protocol
961 * type of ETHERTYPE_ATALK (Appletalk).
963 * XXX - check for an organization code of
964 * encapsulated Ethernet as well?
966 return gen_snap(0x080007, ETHERTYPE_ATALK
,
972 * XXX - we don't have to check for IPX 802.3
973 * here, but should we check for the IPX Ethertype?
975 if (proto
<= ETHERMTU
) {
977 * This is an LLC SAP value, so check
980 return gen_cmp(off_linktype
, BPF_B
,
984 * This is an Ethernet type; we assume
985 * that it's unlikely that it'll
986 * appear in the right place at random,
987 * and therefore check only the
988 * location that would hold the Ethernet
989 * type in a SNAP frame with an organization
990 * code of 0x000000 (encapsulated Ethernet).
992 * XXX - if we were to check for the SNAP DSAP
993 * and LSAP, as per XXX, and were also to check
994 * for an organization code of 0x000000
995 * (encapsulated Ethernet), we'd do
997 * return gen_snap(0x000000, proto,
1000 * here; for now, we don't, as per the above.
1001 * I don't know whether it's worth the
1002 * extra CPU time to do the right check
1005 return gen_cmp(off_linktype
+6, BPF_H
,
1016 * OSI protocols always use 802.2 encapsulation.
1017 * XXX - should we check both the DSAP and the
1018 * LSAP, like this, or should we check just the
1021 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1022 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1023 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1027 case LLCSAP_NETBEUI
:
1029 * NetBEUI always uses 802.2 encapsulation.
1030 * XXX - should we check both the DSAP and the
1031 * LSAP, like this, or should we check just the
1034 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1035 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1036 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1042 * Ethernet_II frames, which are Ethernet
1043 * frames with a frame type of ETHERTYPE_IPX;
1045 * Ethernet_802.3 frames, which have a frame
1046 * type of LINUX_SLL_P_802_3;
1048 * Ethernet_802.2 frames, which are 802.3
1049 * frames with an 802.2 LLC header (i.e, have
1050 * a frame type of LINUX_SLL_P_802_2) and
1051 * with the IPX LSAP as the DSAP in the LLC
1054 * Ethernet_SNAP frames, which are 802.3
1055 * frames with an LLC header and a SNAP
1056 * header and with an OUI of 0x000000
1057 * (encapsulated Ethernet) and a protocol
1058 * ID of ETHERTYPE_IPX in the SNAP header.
1060 * First, do the checks on LINUX_SLL_P_802_2
1061 * frames; generate the check for either
1062 * Ethernet_802.2 or Ethernet_SNAP frames, and
1063 * then put a check for LINUX_SLL_P_802_2 frames
1066 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1067 (bpf_int32
)LLCSAP_IPX
);
1068 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1071 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1075 * Now check for 802.3 frames and OR that with
1076 * the previous test.
1078 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1082 * Now add the check for Ethernet_II frames, and
1083 * do that before checking for the other frame
1086 b0
= gen_cmp(off_linktype
, BPF_H
,
1087 (bpf_int32
)ETHERTYPE_IPX
);
1091 case ETHERTYPE_ATALK
:
1092 case ETHERTYPE_AARP
:
1094 * EtherTalk (AppleTalk protocols on Ethernet link
1095 * layer) may use 802.2 encapsulation.
1099 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1100 * we check for the 802.2 protocol type in the
1101 * "Ethernet type" field.
1103 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1106 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1107 * SNAP packets with an organization code of
1108 * 0x080007 (Apple, for Appletalk) and a protocol
1109 * type of ETHERTYPE_ATALK (Appletalk).
1111 * 802.2-encapsulated ETHERTYPE_AARP packets are
1112 * SNAP packets with an organization code of
1113 * 0x000000 (encapsulated Ethernet) and a protocol
1114 * type of ETHERTYPE_AARP (Appletalk ARP).
1116 if (proto
== ETHERTYPE_ATALK
)
1117 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1119 else /* proto == ETHERTYPE_AARP */
1120 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1125 * Check for Ethernet encapsulation (Ethertalk
1126 * phase 1?); we just check for the Ethernet
1129 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1135 if (proto
<= ETHERMTU
) {
1137 * This is an LLC SAP value, so the frames
1138 * that match would be 802.2 frames.
1139 * Check for the 802.2 protocol type
1140 * in the "Ethernet type" field, and
1141 * then check the DSAP.
1143 b0
= gen_cmp(off_linktype
, BPF_H
,
1145 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1151 * This is an Ethernet type, so compare
1152 * the length/type field with it (if
1153 * the frame is an 802.2 frame, the length
1154 * field will be <= ETHERMTU, and, as
1155 * "proto" is > ETHERMTU, this test
1156 * will fail and the frame won't match,
1157 * which is what we want).
1159 return gen_cmp(off_linktype
, BPF_H
,
1166 case DLT_SLIP_BSDOS
:
1169 * These types don't provide any type field; packets
1172 * XXX - for IPv4, check for a version number of 4, and,
1173 * for IPv6, check for a version number of 6?
1179 case ETHERTYPE_IPV6
:
1181 return gen_true(); /* always true */
1184 return gen_false(); /* always false */
1189 case DLT_PPP_SERIAL
:
1192 * We use Ethernet protocol types inside libpcap;
1193 * map them to the corresponding PPP protocol types.
1198 proto
= PPP_IP
; /* XXX was 0x21 */
1202 case ETHERTYPE_IPV6
:
1211 case ETHERTYPE_ATALK
:
1225 * I'm assuming the "Bridging PDU"s that go
1226 * over PPP are Spanning Tree Protocol
1240 * We use Ethernet protocol types inside libpcap;
1241 * map them to the corresponding PPP protocol types.
1246 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1247 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1249 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1254 case ETHERTYPE_IPV6
:
1264 case ETHERTYPE_ATALK
:
1278 * I'm assuming the "Bridging PDU"s that go
1279 * over PPP are Spanning Tree Protocol
1294 * For DLT_NULL, the link-layer header is a 32-bit
1295 * word containing an AF_ value in *host* byte order.
1297 * In addition, if we're reading a saved capture file,
1298 * the host byte order in the capture may not be the
1299 * same as the host byte order on this machine.
1301 * For DLT_LOOP, the link-layer header is a 32-bit
1302 * word containing an AF_ value in *network* byte order.
1304 * XXX - AF_ values may, unfortunately, be platform-
1305 * dependent; for example, FreeBSD's AF_INET6 is 24
1306 * whilst NetBSD's and OpenBSD's is 26.
1308 * This means that, when reading a capture file, just
1309 * checking for our AF_INET6 value won't work if the
1310 * capture file came from another OS.
1319 case ETHERTYPE_IPV6
:
1326 * Not a type on which we support filtering.
1327 * XXX - support those that have AF_ values
1328 * #defined on this platform, at least?
1333 if (linktype
== DLT_NULL
) {
1335 * The AF_ value is in host byte order, but
1336 * the BPF interpreter will convert it to
1337 * network byte order.
1339 * If this is a save file, and it's from a
1340 * machine with the opposite byte order to
1341 * ours, we byte-swap the AF_ value.
1343 * Then we run it through "htonl()", and
1344 * generate code to compare against the result.
1346 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1347 bpf_pcap
->sf
.swapped
)
1348 proto
= SWAPLONG(proto
);
1349 proto
= htonl(proto
);
1351 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1355 * XXX should we check for first fragment if the protocol
1362 case ETHERTYPE_IPV6
:
1363 return(gen_cmp(2, BPF_B
,
1364 (bpf_int32
)htonl(ARCTYPE_INET6
)));
1367 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_IP
));
1368 b1
= gen_cmp(2, BPF_B
,
1369 (bpf_int32
)htonl(ARCTYPE_IP_OLD
));
1373 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_ARP
));
1374 b1
= gen_cmp(2, BPF_B
,
1375 (bpf_int32
)htonl(ARCTYPE_ARP_OLD
));
1378 case ETHERTYPE_REVARP
:
1379 return(gen_cmp(2, BPF_B
,
1380 (bpf_int32
)htonl(ARCTYPE_REVARP
)));
1381 case ETHERTYPE_ATALK
:
1382 return(gen_cmp(2, BPF_B
,
1383 (bpf_int32
)htonl(ARCTYPE_ATALK
)));
1389 case ETHERTYPE_ATALK
:
1398 * All the types that have no encapsulation should either be
1399 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1400 * all packets are IP packets, or should be handled in some
1401 * special case, if none of them are (if some are and some
1402 * aren't, the lack of encapsulation is a problem, as we'd
1403 * have to find some other way of determining the packet type).
1405 * Therefore, if "off_linktype" is -1, there's an error.
1407 if (off_linktype
== -1)
1411 * Any type not handled above should always have an Ethernet
1412 * type at an offset of "off_linktype". (PPP is partially
1413 * handled above - the protocol type is mapped from the
1414 * Ethernet and LLC types we use internally to the corresponding
1415 * PPP type - but the PPP type is always specified by a value
1416 * at "off_linktype", so we don't have to do the code generation
1419 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1423 * Check for an LLC SNAP packet with a given organization code and
1424 * protocol type; we check the entire contents of the 802.2 LLC and
1425 * snap headers, checking for DSAP and SSAP of SNAP and a control
1426 * field of 0x03 in the LLC header, and for the specified organization
1427 * code and protocol type in the SNAP header.
1429 static struct block
*
1430 gen_snap(orgcode
, ptype
, offset
)
1431 bpf_u_int32 orgcode
;
1435 u_char snapblock
[8];
1437 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1438 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1439 snapblock
[2] = 0x03; /* control = UI */
1440 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1441 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1442 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1443 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1444 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1445 return gen_bcmp(offset
, 8, snapblock
);
1448 static struct block
*
1449 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1453 u_int src_off
, dst_off
;
1455 struct block
*b0
, *b1
;
1469 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1470 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1476 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1477 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1484 b0
= gen_linktype(proto
);
1485 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1491 static struct block
*
1492 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1493 struct in6_addr
*addr
;
1494 struct in6_addr
*mask
;
1496 u_int src_off
, dst_off
;
1498 struct block
*b0
, *b1
;
1513 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1514 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1520 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1521 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1528 /* this order is important */
1529 a
= (u_int32_t
*)addr
;
1530 m
= (u_int32_t
*)mask
;
1531 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1532 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1534 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1536 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1538 b0
= gen_linktype(proto
);
1544 static struct block
*
1545 gen_ehostop(eaddr
, dir
)
1546 register const u_char
*eaddr
;
1549 register struct block
*b0
, *b1
;
1553 return gen_bcmp(6, 6, eaddr
);
1556 return gen_bcmp(0, 6, eaddr
);
1559 b0
= gen_ehostop(eaddr
, Q_SRC
);
1560 b1
= gen_ehostop(eaddr
, Q_DST
);
1566 b0
= gen_ehostop(eaddr
, Q_SRC
);
1567 b1
= gen_ehostop(eaddr
, Q_DST
);
1576 * Like gen_ehostop, but for DLT_FDDI
1578 static struct block
*
1579 gen_fhostop(eaddr
, dir
)
1580 register const u_char
*eaddr
;
1583 struct block
*b0
, *b1
;
1588 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1590 return gen_bcmp(6 + 1, 6, eaddr
);
1595 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1597 return gen_bcmp(0 + 1, 6, eaddr
);
1601 b0
= gen_fhostop(eaddr
, Q_SRC
);
1602 b1
= gen_fhostop(eaddr
, Q_DST
);
1608 b0
= gen_fhostop(eaddr
, Q_SRC
);
1609 b1
= gen_fhostop(eaddr
, Q_DST
);
1618 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1620 static struct block
*
1621 gen_thostop(eaddr
, dir
)
1622 register const u_char
*eaddr
;
1625 register struct block
*b0
, *b1
;
1629 return gen_bcmp(8, 6, eaddr
);
1632 return gen_bcmp(2, 6, eaddr
);
1635 b0
= gen_thostop(eaddr
, Q_SRC
);
1636 b1
= gen_thostop(eaddr
, Q_DST
);
1642 b0
= gen_thostop(eaddr
, Q_SRC
);
1643 b1
= gen_thostop(eaddr
, Q_DST
);
1652 * This is quite tricky because there may be pad bytes in front of the
1653 * DECNET header, and then there are two possible data packet formats that
1654 * carry both src and dst addresses, plus 5 packet types in a format that
1655 * carries only the src node, plus 2 types that use a different format and
1656 * also carry just the src node.
1660 * Instead of doing those all right, we just look for data packets with
1661 * 0 or 1 bytes of padding. If you want to look at other packets, that
1662 * will require a lot more hacking.
1664 * To add support for filtering on DECNET "areas" (network numbers)
1665 * one would want to add a "mask" argument to this routine. That would
1666 * make the filter even more inefficient, although one could be clever
1667 * and not generate masking instructions if the mask is 0xFFFF.
1669 static struct block
*
1670 gen_dnhostop(addr
, dir
, base_off
)
1675 struct block
*b0
, *b1
, *b2
, *tmp
;
1676 u_int offset_lh
; /* offset if long header is received */
1677 u_int offset_sh
; /* offset if short header is received */
1682 offset_sh
= 1; /* follows flags */
1683 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1687 offset_sh
= 3; /* follows flags, dstnode */
1688 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1692 /* Inefficient because we do our Calvinball dance twice */
1693 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1694 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1700 /* Inefficient because we do our Calvinball dance twice */
1701 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1702 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1707 bpf_error("ISO host filtering not implemented");
1712 b0
= gen_linktype(ETHERTYPE_DN
);
1713 /* Check for pad = 1, long header case */
1714 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1715 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1716 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1717 BPF_H
, (bpf_int32
)ntohs(addr
));
1719 /* Check for pad = 0, long header case */
1720 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1721 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1724 /* Check for pad = 1, short header case */
1725 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1726 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1727 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1728 BPF_H
, (bpf_int32
)ntohs(addr
));
1731 /* Check for pad = 0, short header case */
1732 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1733 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1737 /* Combine with test for linktype */
1742 static struct block
*
1743 gen_host(addr
, mask
, proto
, dir
)
1749 struct block
*b0
, *b1
;
1754 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1755 if (off_linktype
!= -1) {
1756 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1758 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1764 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1765 off_nl
+ 12, off_nl
+ 16);
1768 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1769 off_nl
+ 14, off_nl
+ 24);
1772 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1773 off_nl
+ 14, off_nl
+ 24);
1776 bpf_error("'tcp' modifier applied to host");
1779 bpf_error("'sctp' modifier applied to host");
1782 bpf_error("'udp' modifier applied to host");
1785 bpf_error("'icmp' modifier applied to host");
1788 bpf_error("'igmp' modifier applied to host");
1791 bpf_error("'igrp' modifier applied to host");
1794 bpf_error("'pim' modifier applied to host");
1797 bpf_error("'vrrp' modifier applied to host");
1800 bpf_error("ATALK host filtering not implemented");
1803 bpf_error("AARP host filtering not implemented");
1806 return gen_dnhostop(addr
, dir
, off_nl
);
1809 bpf_error("SCA host filtering not implemented");
1812 bpf_error("LAT host filtering not implemented");
1815 bpf_error("MOPDL host filtering not implemented");
1818 bpf_error("MOPRC host filtering not implemented");
1822 bpf_error("'ip6' modifier applied to ip host");
1825 bpf_error("'icmp6' modifier applied to host");
1829 bpf_error("'ah' modifier applied to host");
1832 bpf_error("'esp' modifier applied to host");
1835 bpf_error("ISO host filtering not implemented");
1838 bpf_error("'esis' modifier applied to host");
1841 bpf_error("'isis' modifier applied to host");
1844 bpf_error("'clnp' modifier applied to host");
1847 bpf_error("'stp' modifier applied to host");
1850 bpf_error("IPX host filtering not implemented");
1853 bpf_error("'netbeui' modifier applied to host");
1862 static struct block
*
1863 gen_host6(addr
, mask
, proto
, dir
)
1864 struct in6_addr
*addr
;
1865 struct in6_addr
*mask
;
1872 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1875 bpf_error("'ip' modifier applied to ip6 host");
1878 bpf_error("'rarp' modifier applied to ip6 host");
1881 bpf_error("'arp' modifier applied to ip6 host");
1884 bpf_error("'sctp' modifier applied to host");
1887 bpf_error("'tcp' modifier applied to host");
1890 bpf_error("'udp' modifier applied to host");
1893 bpf_error("'icmp' modifier applied to host");
1896 bpf_error("'igmp' modifier applied to host");
1899 bpf_error("'igrp' modifier applied to host");
1902 bpf_error("'pim' modifier applied to host");
1905 bpf_error("'vrrp' modifier applied to host");
1908 bpf_error("ATALK host filtering not implemented");
1911 bpf_error("AARP host filtering not implemented");
1914 bpf_error("'decnet' modifier applied to ip6 host");
1917 bpf_error("SCA host filtering not implemented");
1920 bpf_error("LAT host filtering not implemented");
1923 bpf_error("MOPDL host filtering not implemented");
1926 bpf_error("MOPRC host filtering not implemented");
1929 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1930 off_nl
+ 8, off_nl
+ 24);
1933 bpf_error("'icmp6' modifier applied to host");
1936 bpf_error("'ah' modifier applied to host");
1939 bpf_error("'esp' modifier applied to host");
1942 bpf_error("ISO host filtering not implemented");
1945 bpf_error("'esis' modifier applied to host");
1948 bpf_error("'isis' modifier applied to host");
1951 bpf_error("'clnp' modifier applied to host");
1954 bpf_error("'stp' modifier applied to host");
1957 bpf_error("IPX host filtering not implemented");
1960 bpf_error("'netbeui' modifier applied to host");
1970 static struct block
*
1971 gen_gateway(eaddr
, alist
, proto
, dir
)
1972 const u_char
*eaddr
;
1973 bpf_u_int32
**alist
;
1977 struct block
*b0
, *b1
, *tmp
;
1980 bpf_error("direction applied to 'gateway'");
1987 if (linktype
== DLT_EN10MB
)
1988 b0
= gen_ehostop(eaddr
, Q_OR
);
1989 else if (linktype
== DLT_FDDI
)
1990 b0
= gen_fhostop(eaddr
, Q_OR
);
1991 else if (linktype
== DLT_IEEE802
)
1992 b0
= gen_thostop(eaddr
, Q_OR
);
1995 "'gateway' supported only on ethernet, FDDI or token ring");
1997 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1999 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2007 bpf_error("illegal modifier of 'gateway'");
2013 gen_proto_abbrev(proto
)
2024 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2026 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2032 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2034 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2040 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2042 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2048 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2051 #ifndef IPPROTO_IGMP
2052 #define IPPROTO_IGMP 2
2056 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2059 #ifndef IPPROTO_IGRP
2060 #define IPPROTO_IGRP 9
2063 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2067 #define IPPROTO_PIM 103
2071 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2073 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2078 #ifndef IPPROTO_VRRP
2079 #define IPPROTO_VRRP 112
2083 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2087 b1
= gen_linktype(ETHERTYPE_IP
);
2091 b1
= gen_linktype(ETHERTYPE_ARP
);
2095 b1
= gen_linktype(ETHERTYPE_REVARP
);
2099 bpf_error("link layer applied in wrong context");
2102 b1
= gen_linktype(ETHERTYPE_ATALK
);
2106 b1
= gen_linktype(ETHERTYPE_AARP
);
2110 b1
= gen_linktype(ETHERTYPE_DN
);
2114 b1
= gen_linktype(ETHERTYPE_SCA
);
2118 b1
= gen_linktype(ETHERTYPE_LAT
);
2122 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2126 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2131 b1
= gen_linktype(ETHERTYPE_IPV6
);
2134 #ifndef IPPROTO_ICMPV6
2135 #define IPPROTO_ICMPV6 58
2138 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2143 #define IPPROTO_AH 51
2146 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2148 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2154 #define IPPROTO_ESP 50
2157 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2159 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2165 b1
= gen_linktype(LLCSAP_ISONS
);
2169 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2173 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2177 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2181 b1
= gen_linktype(LLCSAP_8021D
);
2185 b1
= gen_linktype(LLCSAP_IPX
);
2189 b1
= gen_linktype(LLCSAP_NETBEUI
);
2198 static struct block
*
2205 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2206 s
->s
.k
= off_nl
+ 6;
2207 b
= new_block(JMP(BPF_JSET
));
2215 static struct block
*
2216 gen_portatom(off
, v
)
2223 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2226 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2227 s
->next
->s
.k
= off_nl
+ off
;
2229 b
= new_block(JMP(BPF_JEQ
));
2237 static struct block
*
2238 gen_portatom6(off
, v
)
2242 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2247 gen_portop(port
, proto
, dir
)
2248 int port
, proto
, dir
;
2250 struct block
*b0
, *b1
, *tmp
;
2252 /* ip proto 'proto' */
2253 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2259 b1
= gen_portatom(0, (bpf_int32
)port
);
2263 b1
= gen_portatom(2, (bpf_int32
)port
);
2268 tmp
= 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
);
2287 static struct block
*
2288 gen_port(port
, ip_proto
, dir
)
2293 struct block
*b0
, *b1
, *tmp
;
2295 /* ether proto ip */
2296 b0
= gen_linktype(ETHERTYPE_IP
);
2302 b1
= gen_portop(port
, ip_proto
, dir
);
2306 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2307 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2309 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
2322 gen_portop6(port
, proto
, dir
)
2323 int port
, proto
, dir
;
2325 struct block
*b0
, *b1
, *tmp
;
2327 /* ip proto 'proto' */
2328 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2332 b1
= gen_portatom6(0, (bpf_int32
)port
);
2336 b1
= gen_portatom6(2, (bpf_int32
)port
);
2341 tmp
= 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
);
2360 static struct block
*
2361 gen_port6(port
, ip_proto
, dir
)
2366 struct block
*b0
, *b1
, *tmp
;
2368 /* ether proto ip */
2369 b0
= gen_linktype(ETHERTYPE_IPV6
);
2375 b1
= gen_portop6(port
, ip_proto
, dir
);
2379 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2380 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2382 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
2395 lookup_proto(name
, proto
)
2396 register const char *name
;
2406 v
= pcap_nametoproto(name
);
2407 if (v
== PROTO_UNDEF
)
2408 bpf_error("unknown ip proto '%s'", name
);
2412 /* XXX should look up h/w protocol type based on linktype */
2413 v
= pcap_nametoeproto(name
);
2414 if (v
== PROTO_UNDEF
)
2415 bpf_error("unknown ether proto '%s'", name
);
2419 if (strcmp(name
, "esis") == 0)
2421 else if (strcmp(name
, "isis") == 0)
2423 else if (strcmp(name
, "clnp") == 0)
2426 bpf_error("unknown osi proto '%s'", name
);
2446 static struct block
*
2447 gen_protochain(v
, proto
, dir
)
2452 #ifdef NO_PROTOCHAIN
2453 return gen_proto(v
, proto
, dir
);
2455 struct block
*b0
, *b
;
2456 struct slist
*s
[100];
2457 int fix2
, fix3
, fix4
, fix5
;
2458 int ahcheck
, again
, end
;
2460 int reg2
= alloc_reg();
2462 memset(s
, 0, sizeof(s
));
2463 fix2
= fix3
= fix4
= fix5
= 0;
2470 b0
= gen_protochain(v
, Q_IP
, dir
);
2471 b
= gen_protochain(v
, Q_IPV6
, dir
);
2475 bpf_error("bad protocol applied for 'protochain'");
2479 no_optimize
= 1; /*this code is not compatible with optimzer yet */
2482 * s[0] is a dummy entry to protect other BPF insn from damaged
2483 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
2484 * hard to find interdependency made by jump table fixup.
2487 s
[i
] = new_stmt(0); /*dummy*/
2492 b0
= gen_linktype(ETHERTYPE_IP
);
2495 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2496 s
[i
]->s
.k
= off_nl
+ 9;
2498 /* X = ip->ip_hl << 2 */
2499 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2505 b0
= gen_linktype(ETHERTYPE_IPV6
);
2507 /* A = ip6->ip_nxt */
2508 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2509 s
[i
]->s
.k
= off_nl
+ 6;
2511 /* X = sizeof(struct ip6_hdr) */
2512 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
2518 bpf_error("unsupported proto to gen_protochain");
2522 /* again: if (A == v) goto end; else fall through; */
2524 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2526 s
[i
]->s
.jt
= NULL
; /*later*/
2527 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2531 #ifndef IPPROTO_NONE
2532 #define IPPROTO_NONE 59
2534 /* if (A == IPPROTO_NONE) goto end */
2535 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2536 s
[i
]->s
.jt
= NULL
; /*later*/
2537 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2538 s
[i
]->s
.k
= IPPROTO_NONE
;
2539 s
[fix5
]->s
.jf
= s
[i
];
2544 if (proto
== Q_IPV6
) {
2545 int v6start
, v6end
, v6advance
, j
;
2548 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
2549 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2550 s
[i
]->s
.jt
= NULL
; /*later*/
2551 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2552 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
2553 s
[fix2
]->s
.jf
= s
[i
];
2555 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
2556 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2557 s
[i
]->s
.jt
= NULL
; /*later*/
2558 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2559 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
2561 /* if (A == IPPROTO_ROUTING) 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_ROUTING
;
2567 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
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
; /*later*/
2571 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
2582 * X = X + (P[X + 1] + 1) * 8;
2585 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2587 /* A = P[X + packet head] */
2588 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2592 s
[i
] = new_stmt(BPF_ST
);
2596 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2599 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2603 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2605 /* A = P[X + packet head]; */
2606 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2610 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2614 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2618 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2621 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2625 /* goto again; (must use BPF_JA for backward jump) */
2626 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2627 s
[i
]->s
.k
= again
- i
- 1;
2628 s
[i
- 1]->s
.jf
= s
[i
];
2632 for (j
= v6start
; j
<= v6end
; j
++)
2633 s
[j
]->s
.jt
= s
[v6advance
];
2638 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2640 s
[fix2
]->s
.jf
= s
[i
];
2646 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2647 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2648 s
[i
]->s
.jt
= NULL
; /*later*/
2649 s
[i
]->s
.jf
= NULL
; /*later*/
2650 s
[i
]->s
.k
= IPPROTO_AH
;
2652 s
[fix3
]->s
.jf
= s
[ahcheck
];
2659 * X = X + (P[X + 1] + 2) * 4;
2662 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2664 /* A = P[X + packet head]; */
2665 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2669 s
[i
] = new_stmt(BPF_ST
);
2673 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2676 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2680 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2682 /* A = P[X + packet head] */
2683 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2687 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2691 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2695 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2698 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2702 /* goto again; (must use BPF_JA for backward jump) */
2703 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2704 s
[i
]->s
.k
= again
- i
- 1;
2709 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2711 s
[fix2
]->s
.jt
= s
[end
];
2712 s
[fix4
]->s
.jf
= s
[end
];
2713 s
[fix5
]->s
.jt
= s
[end
];
2720 for (i
= 0; i
< max
- 1; i
++)
2721 s
[i
]->next
= s
[i
+ 1];
2722 s
[max
- 1]->next
= NULL
;
2727 b
= new_block(JMP(BPF_JEQ
));
2728 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2738 static struct block
*
2739 gen_proto(v
, proto
, dir
)
2744 struct block
*b0
, *b1
;
2746 if (dir
!= Q_DEFAULT
)
2747 bpf_error("direction applied to 'proto'");
2752 b0
= gen_proto(v
, Q_IP
, dir
);
2753 b1
= gen_proto(v
, Q_IPV6
, dir
);
2760 b0
= gen_linktype(ETHERTYPE_IP
);
2762 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2764 b1
= gen_protochain(v
, Q_IP
);
2770 b0
= gen_linktype(LLCSAP_ISONS
);
2771 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2776 bpf_error("arp does not encapsulate another protocol");
2780 bpf_error("rarp does not encapsulate another protocol");
2784 bpf_error("atalk encapsulation is not specifiable");
2788 bpf_error("decnet encapsulation is not specifiable");
2792 bpf_error("sca does not encapsulate another protocol");
2796 bpf_error("lat does not encapsulate another protocol");
2800 bpf_error("moprc does not encapsulate another protocol");
2804 bpf_error("mopdl does not encapsulate another protocol");
2808 return gen_linktype(v
);
2811 bpf_error("'udp proto' is bogus");
2815 bpf_error("'tcp proto' is bogus");
2819 bpf_error("'sctp proto' is bogus");
2823 bpf_error("'icmp proto' is bogus");
2827 bpf_error("'igmp proto' is bogus");
2831 bpf_error("'igrp proto' is bogus");
2835 bpf_error("'pim proto' is bogus");
2839 bpf_error("'vrrp proto' is bogus");
2844 b0
= gen_linktype(ETHERTYPE_IPV6
);
2846 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2848 b1
= gen_protochain(v
, Q_IPV6
);
2854 bpf_error("'icmp6 proto' is bogus");
2858 bpf_error("'ah proto' is bogus");
2861 bpf_error("'ah proto' is bogus");
2864 bpf_error("'stp proto' is bogus");
2867 bpf_error("'ipx proto' is bogus");
2870 bpf_error("'netbeui proto' is bogus");
2881 register const char *name
;
2884 int proto
= q
.proto
;
2888 bpf_u_int32 mask
, addr
;
2890 bpf_u_int32
**alist
;
2893 struct sockaddr_in
*sin
;
2894 struct sockaddr_in6
*sin6
;
2895 struct addrinfo
*res
, *res0
;
2896 struct in6_addr mask128
;
2898 struct block
*b
, *tmp
;
2899 int port
, real_proto
;
2904 addr
= pcap_nametonetaddr(name
);
2906 bpf_error("unknown network '%s'", name
);
2907 /* Left justify network addr and calculate its network mask */
2909 while (addr
&& (addr
& 0xff000000) == 0) {
2913 return gen_host(addr
, mask
, proto
, dir
);
2917 if (proto
== Q_LINK
) {
2921 eaddr
= pcap_ether_hostton(name
);
2924 "unknown ether host '%s'", name
);
2925 return gen_ehostop(eaddr
, dir
);
2928 eaddr
= pcap_ether_hostton(name
);
2931 "unknown FDDI host '%s'", name
);
2932 return gen_fhostop(eaddr
, dir
);
2935 eaddr
= pcap_ether_hostton(name
);
2938 "unknown token ring host '%s'", name
);
2939 return gen_thostop(eaddr
, dir
);
2943 "only ethernet/FDDI/token ring supports link-level host name");
2946 } else if (proto
== Q_DECNET
) {
2947 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2949 * I don't think DECNET hosts can be multihomed, so
2950 * there is no need to build up a list of addresses
2952 return (gen_host(dn_addr
, 0, proto
, dir
));
2955 alist
= pcap_nametoaddr(name
);
2956 if (alist
== NULL
|| *alist
== NULL
)
2957 bpf_error("unknown host '%s'", name
);
2959 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2961 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2963 tmp
= gen_host(**alist
++, 0xffffffff,
2970 memset(&mask128
, 0xff, sizeof(mask128
));
2971 res0
= res
= pcap_nametoaddrinfo(name
);
2973 bpf_error("unknown host '%s'", name
);
2975 tproto
= tproto6
= proto
;
2976 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2980 for (res
= res0
; res
; res
= res
->ai_next
) {
2981 switch (res
->ai_family
) {
2983 if (tproto
== Q_IPV6
)
2986 sin
= (struct sockaddr_in
*)
2988 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2989 0xffffffff, tproto
, dir
);
2992 if (tproto6
== Q_IP
)
2995 sin6
= (struct sockaddr_in6
*)
2997 tmp
= gen_host6(&sin6
->sin6_addr
,
2998 &mask128
, tproto6
, dir
);
3009 bpf_error("unknown host '%s'%s", name
,
3010 (proto
== Q_DEFAULT
)
3012 : " for specified address family");
3019 if (proto
!= Q_DEFAULT
&&
3020 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
3021 bpf_error("illegal qualifier of 'port'");
3022 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
3023 bpf_error("unknown port '%s'", name
);
3024 if (proto
== Q_UDP
) {
3025 if (real_proto
== IPPROTO_TCP
)
3026 bpf_error("port '%s' is tcp", name
);
3027 else if (real_proto
== IPPROTO_SCTP
)
3028 bpf_error("port '%s' is sctp", name
);
3030 /* override PROTO_UNDEF */
3031 real_proto
= IPPROTO_UDP
;
3033 if (proto
== Q_TCP
) {
3034 if (real_proto
== IPPROTO_UDP
)
3035 bpf_error("port '%s' is udp", name
);
3037 else if (real_proto
== IPPROTO_SCTP
)
3038 bpf_error("port '%s' is sctp", name
);
3040 /* override PROTO_UNDEF */
3041 real_proto
= IPPROTO_TCP
;
3043 if (proto
== Q_SCTP
) {
3044 if (real_proto
== IPPROTO_UDP
)
3045 bpf_error("port '%s' is udp", name
);
3047 else if (real_proto
== IPPROTO_TCP
)
3048 bpf_error("port '%s' is tcp", name
);
3050 /* override PROTO_UNDEF */
3051 real_proto
= IPPROTO_SCTP
;
3054 return gen_port(port
, real_proto
, dir
);
3058 b
= gen_port(port
, real_proto
, dir
);
3059 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3066 eaddr
= pcap_ether_hostton(name
);
3068 bpf_error("unknown ether host: %s", name
);
3070 alist
= pcap_nametoaddr(name
);
3071 if (alist
== NULL
|| *alist
== NULL
)
3072 bpf_error("unknown host '%s'", name
);
3073 return gen_gateway(eaddr
, alist
, proto
, dir
);
3075 bpf_error("'gateway' not supported in this configuration");
3079 real_proto
= lookup_proto(name
, proto
);
3080 if (real_proto
>= 0)
3081 return gen_proto(real_proto
, proto
, dir
);
3083 bpf_error("unknown protocol: %s", name
);
3086 real_proto
= lookup_proto(name
, proto
);
3087 if (real_proto
>= 0)
3088 return gen_protochain(real_proto
, proto
, dir
);
3090 bpf_error("unknown protocol: %s", name
);
3102 gen_mcode(s1
, s2
, masklen
, q
)
3103 register const char *s1
, *s2
;
3104 register int masklen
;
3107 register int nlen
, mlen
;
3110 nlen
= __pcap_atoin(s1
, &n
);
3111 /* Promote short ipaddr */
3115 mlen
= __pcap_atoin(s2
, &m
);
3116 /* Promote short ipaddr */
3119 bpf_error("non-network bits set in \"%s mask %s\"",
3122 /* Convert mask len to mask */
3124 bpf_error("mask length must be <= 32");
3125 m
= 0xffffffff << (32 - masklen
);
3127 bpf_error("non-network bits set in \"%s/%d\"",
3134 return gen_host(n
, m
, q
.proto
, q
.dir
);
3137 bpf_error("Mask syntax for networks only");
3144 register const char *s
;
3149 int proto
= q
.proto
;
3155 else if (q
.proto
== Q_DECNET
)
3156 vlen
= __pcap_atodn(s
, &v
);
3158 vlen
= __pcap_atoin(s
, &v
);
3165 if (proto
== Q_DECNET
)
3166 return gen_host(v
, 0, proto
, dir
);
3167 else if (proto
== Q_LINK
) {
3168 bpf_error("illegal link layer address");
3171 if (s
== NULL
&& q
.addr
== Q_NET
) {
3172 /* Promote short net number */
3173 while (v
&& (v
& 0xff000000) == 0) {
3178 /* Promote short ipaddr */
3182 return gen_host(v
, mask
, proto
, dir
);
3187 proto
= IPPROTO_UDP
;
3188 else if (proto
== Q_TCP
)
3189 proto
= IPPROTO_TCP
;
3190 else if (proto
== Q_SCTP
)
3191 proto
= IPPROTO_SCTP
;
3192 else if (proto
== Q_DEFAULT
)
3193 proto
= PROTO_UNDEF
;
3195 bpf_error("illegal qualifier of 'port'");
3198 return gen_port((int)v
, proto
, dir
);
3202 b
= gen_port((int)v
, proto
, dir
);
3203 gen_or(gen_port6((int)v
, proto
, dir
), b
);
3209 bpf_error("'gateway' requires a name");
3213 return gen_proto((int)v
, proto
, dir
);
3216 return gen_protochain((int)v
, proto
, dir
);
3231 gen_mcode6(s1
, s2
, masklen
, q
)
3232 register const char *s1
, *s2
;
3233 register int masklen
;
3236 struct addrinfo
*res
;
3237 struct in6_addr
*addr
;
3238 struct in6_addr mask
;
3243 bpf_error("no mask %s supported", s2
);
3245 res
= pcap_nametoaddrinfo(s1
);
3247 bpf_error("invalid ip6 address %s", s1
);
3249 bpf_error("%s resolved to multiple address", s1
);
3250 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3252 if (sizeof(mask
) * 8 < masklen
)
3253 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3254 memset(&mask
, 0xff, masklen
/ 8);
3256 mask
.s6_addr
[masklen
/ 8] =
3257 (0xff << (8 - masklen
% 8)) & 0xff;
3260 a
= (u_int32_t
*)addr
;
3261 m
= (u_int32_t
*)&mask
;
3262 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3263 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3264 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
3272 bpf_error("Mask syntax for networks only");
3276 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
3281 bpf_error("invalid qualifier against IPv6 address");
3289 register const u_char
*eaddr
;
3292 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3293 if (linktype
== DLT_EN10MB
)
3294 return gen_ehostop(eaddr
, (int)q
.dir
);
3295 if (linktype
== DLT_FDDI
)
3296 return gen_fhostop(eaddr
, (int)q
.dir
);
3297 if (linktype
== DLT_IEEE802
)
3298 return gen_thostop(eaddr
, (int)q
.dir
);
3299 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
3301 bpf_error("ethernet address used in non-ether expression");
3307 struct slist
*s0
, *s1
;
3310 * This is definitely not the best way to do this, but the
3311 * lists will rarely get long.
3318 static struct slist
*
3324 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3329 static struct slist
*
3335 s
= new_stmt(BPF_LD
|BPF_MEM
);
3341 gen_load(proto
, index
, size
)
3346 struct slist
*s
, *tmp
;
3348 int regno
= alloc_reg();
3350 free_reg(index
->regno
);
3354 bpf_error("data size must be 1, 2, or 4");
3370 bpf_error("unsupported index operation");
3373 s
= xfer_to_x(index
);
3374 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3376 sappend(index
->s
, s
);
3391 /* XXX Note that we assume a fixed link header here. */
3392 s
= xfer_to_x(index
);
3393 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3396 sappend(index
->s
, s
);
3398 b
= gen_proto_abbrev(proto
);
3400 gen_and(index
->b
, b
);
3412 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3414 sappend(s
, xfer_to_a(index
));
3415 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3416 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3417 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3419 sappend(index
->s
, s
);
3421 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
3423 gen_and(index
->b
, b
);
3425 gen_and(gen_proto_abbrev(Q_IP
), b
);
3431 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
3435 index
->regno
= regno
;
3436 s
= new_stmt(BPF_ST
);
3438 sappend(index
->s
, s
);
3444 gen_relation(code
, a0
, a1
, reversed
)
3446 struct arth
*a0
, *a1
;
3449 struct slist
*s0
, *s1
, *s2
;
3450 struct block
*b
, *tmp
;
3454 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
3455 b
= new_block(JMP(code
));
3456 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
3457 reversed
= !reversed
;
3458 b
->s
.k
= 0x80000000;
3466 sappend(a0
->s
, a1
->s
);
3470 free_reg(a0
->regno
);
3471 free_reg(a1
->regno
);
3473 /* 'and' together protocol checks */
3476 gen_and(a0
->b
, tmp
= a1
->b
);
3492 int regno
= alloc_reg();
3493 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
3496 s
= new_stmt(BPF_LD
|BPF_LEN
);
3497 s
->next
= new_stmt(BPF_ST
);
3498 s
->next
->s
.k
= regno
;
3513 a
= (struct arth
*)newchunk(sizeof(*a
));
3517 s
= new_stmt(BPF_LD
|BPF_IMM
);
3519 s
->next
= new_stmt(BPF_ST
);
3535 s
= new_stmt(BPF_ALU
|BPF_NEG
);
3538 s
= new_stmt(BPF_ST
);
3546 gen_arth(code
, a0
, a1
)
3548 struct arth
*a0
, *a1
;
3550 struct slist
*s0
, *s1
, *s2
;
3554 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
3559 sappend(a0
->s
, a1
->s
);
3561 free_reg(a1
->regno
);
3563 s0
= new_stmt(BPF_ST
);
3564 a0
->regno
= s0
->s
.k
= alloc_reg();
3571 * Here we handle simple allocation of the scratch registers.
3572 * If too many registers are alloc'd, the allocator punts.
3574 static int regused
[BPF_MEMWORDS
];
3578 * Return the next free register.
3583 int n
= BPF_MEMWORDS
;
3586 if (regused
[curreg
])
3587 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
3589 regused
[curreg
] = 1;
3593 bpf_error("too many registers needed to evaluate expression");
3598 * Return a register to the table so it can
3608 static struct block
*
3615 s
= new_stmt(BPF_LD
|BPF_LEN
);
3616 b
= new_block(JMP(jmp
));
3627 return gen_len(BPF_JGE
, n
);
3631 * Actually, this is less than or equal.
3639 b
= gen_len(BPF_JGT
, n
);
3646 gen_byteop(op
, idx
, val
)
3657 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3660 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3661 b
->s
.code
= JMP(BPF_JGE
);
3666 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3667 b
->s
.code
= JMP(BPF_JGT
);
3671 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3675 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3679 b
= new_block(JMP(BPF_JEQ
));
3686 static u_char abroadcast
[] = { 0x0 };
3689 gen_broadcast(proto
)
3692 bpf_u_int32 hostmask
;
3693 struct block
*b0
, *b1
, *b2
;
3694 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3700 if (linktype
== DLT_ARCNET
)
3701 return gen_ahostop(abroadcast
, Q_DST
);
3702 if (linktype
== DLT_EN10MB
)
3703 return gen_ehostop(ebroadcast
, Q_DST
);
3704 if (linktype
== DLT_FDDI
)
3705 return gen_fhostop(ebroadcast
, Q_DST
);
3706 if (linktype
== DLT_IEEE802
)
3707 return gen_thostop(ebroadcast
, Q_DST
);
3708 bpf_error("not a broadcast link");
3712 b0
= gen_linktype(ETHERTYPE_IP
);
3713 hostmask
= ~netmask
;
3714 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3715 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3716 (bpf_int32
)(~0 & hostmask
), hostmask
);
3721 bpf_error("only ether/ip broadcast filters supported");
3725 gen_multicast(proto
)
3728 register struct block
*b0
, *b1
;
3729 register struct slist
*s
;
3735 if (linktype
== DLT_ARCNET
)
3736 /* all ARCnet multicasts use the same address */
3737 return gen_ahostop(abroadcast
, Q_DST
);
3739 if (linktype
== DLT_EN10MB
) {
3740 /* ether[0] & 1 != 0 */
3741 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3743 b0
= new_block(JMP(BPF_JSET
));
3749 if (linktype
== DLT_FDDI
) {
3750 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3751 /* fddi[1] & 1 != 0 */
3752 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3754 b0
= new_block(JMP(BPF_JSET
));
3760 /* TODO - check how token ring handles multicast */
3761 /* if (linktype == DLT_IEEE802) ... */
3763 /* Link not known to support multicasts */
3767 b0
= gen_linktype(ETHERTYPE_IP
);
3768 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3769 b1
->s
.code
= JMP(BPF_JGE
);
3775 b0
= gen_linktype(ETHERTYPE_IPV6
);
3776 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3781 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3785 * generate command for inbound/outbound. It's here so we can
3786 * make it link-type specific. 'dir' = 0 implies "inbound",
3787 * = 1 implies "outbound".
3793 register struct block
*b0
;
3796 * Only some data link types support inbound/outbound qualifiers.
3801 b0
= gen_relation(BPF_JEQ
,
3802 gen_load(Q_LINK
, gen_loadi(0), 1),
3808 bpf_error("inbound/outbound not supported on linktype %d\n",
3818 register const u_char
*eaddr
;
3821 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3822 if (linktype
== DLT_ARCNET
)
3823 return gen_ahostop(eaddr
, (int)q
.dir
);
3825 bpf_error("ARCnet address used in non-arc expression");
3829 static struct block
*
3830 gen_ahostop(eaddr
, dir
)
3831 register const u_char
*eaddr
;
3834 register struct block
*b0
, *b1
;
3837 /* src comes first, different from Ethernet */
3839 return gen_bcmp(0, 1, eaddr
);
3842 return gen_bcmp(1, 1, eaddr
);
3845 b0
= gen_ahostop(eaddr
, Q_SRC
);
3846 b1
= gen_ahostop(eaddr
, Q_DST
);
3852 b0
= gen_ahostop(eaddr
, Q_SRC
);
3853 b1
= gen_ahostop(eaddr
, Q_DST
);
3862 * support IEEE 802.1Q VLAN trunk over ethernet
3868 static u_int orig_linktype
= -1, orig_nl
= -1;
3872 * Change the offsets to point to the type and data fields within
3873 * the VLAN packet. This is somewhat of a kludge.
3875 if (orig_nl
== (u_int
)-1) {
3876 orig_linktype
= off_linktype
; /* save original values */
3887 bpf_error("no VLAN support for data link type %d",
3893 /* check for VLAN */
3894 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3896 /* If a specific VLAN is requested, check VLAN id */
3897 if (vlan_num
>= 0) {
3900 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);