]>
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.160.2.2 2002-04-07 00:05:12 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"
60 #include <netdb.h> /* for "struct addrinfo" */
62 #include <pcap-namedb.h>
67 #define IPPROTO_SCTP 132
70 #ifdef HAVE_OS_PROTO_H
74 #define JMP(c) ((c)|BPF_JMP|BPF_K)
77 static jmp_buf top_ctx
;
78 static pcap_t
*bpf_pcap
;
80 /* Hack for updating VLAN offsets. */
81 static u_int orig_linktype
= -1, orig_nl
= -1;
85 int pcap_fddipad
= PCAP_FDDIPAD
;
92 bpf_error(const char *fmt
, ...)
99 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
106 static void init_linktype(int);
108 static int alloc_reg(void);
109 static void free_reg(int);
111 static struct block
*root
;
114 * We divy out chunks of memory rather than call malloc each time so
115 * we don't have to worry about leaking memory. It's probably
116 * not a big deal if all this memory was wasted but it this ever
117 * goes into a library that would probably not be a good idea.
120 #define CHUNK0SIZE 1024
126 static struct chunk chunks
[NCHUNKS
];
127 static int cur_chunk
;
129 static void *newchunk(u_int
);
130 static void freechunks(void);
131 static inline struct block
*new_block(int);
132 static inline struct slist
*new_stmt(int);
133 static struct block
*gen_retblk(int);
134 static inline void syntax(void);
136 static void backpatch(struct block
*, struct block
*);
137 static void merge(struct block
*, struct block
*);
138 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
139 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
140 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
141 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
142 static struct block
*gen_uncond(int);
143 static inline struct block
*gen_true(void);
144 static inline struct block
*gen_false(void);
145 static struct block
*gen_linktype(int);
146 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
147 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
149 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
151 static struct block
*gen_ahostop(const u_char
*, int);
152 static struct block
*gen_ehostop(const u_char
*, int);
153 static struct block
*gen_fhostop(const u_char
*, int);
154 static struct block
*gen_thostop(const u_char
*, int);
155 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
156 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
158 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
161 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
163 static struct block
*gen_ipfrag(void);
164 static struct block
*gen_portatom(int, bpf_int32
);
166 static struct block
*gen_portatom6(int, bpf_int32
);
168 struct block
*gen_portop(int, int, int);
169 static struct block
*gen_port(int, int, int);
171 struct block
*gen_portop6(int, int, int);
172 static struct block
*gen_port6(int, int, int);
174 static int lookup_proto(const char *, int);
175 static struct block
*gen_protochain(int, int, int);
176 static struct block
*gen_proto(int, int, int);
177 static struct slist
*xfer_to_x(struct arth
*);
178 static struct slist
*xfer_to_a(struct arth
*);
179 static struct block
*gen_len(int, int);
189 /* XXX Round up to nearest long. */
190 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
192 /* XXX Round up to structure boundary. */
196 cp
= &chunks
[cur_chunk
];
197 if (n
> cp
->n_left
) {
198 ++cp
, k
= ++cur_chunk
;
200 bpf_error("out of memory");
201 size
= CHUNK0SIZE
<< k
;
202 cp
->m
= (void *)malloc(size
);
203 memset((char *)cp
->m
, 0, size
);
206 bpf_error("out of memory");
209 return (void *)((char *)cp
->m
+ cp
->n_left
);
218 for (i
= 0; i
< NCHUNKS
; ++i
)
219 if (chunks
[i
].m
!= NULL
) {
226 * A strdup whose allocations are freed after code generation is over.
230 register const char *s
;
232 int n
= strlen(s
) + 1;
233 char *cp
= newchunk(n
);
239 static inline struct block
*
245 p
= (struct block
*)newchunk(sizeof(*p
));
252 static inline struct slist
*
258 p
= (struct slist
*)newchunk(sizeof(*p
));
264 static struct block
*
268 struct block
*b
= new_block(BPF_RET
|BPF_K
);
277 bpf_error("syntax error in filter expression");
280 static bpf_u_int32 netmask
;
285 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
286 char *buf
, int optimize
, bpf_u_int32 mask
)
295 if (setjmp(top_ctx
)) {
303 snaplen
= pcap_snapshot(p
);
305 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
306 "snaplen of 0 rejects all packets");
310 lex_init(buf
? buf
: "");
311 init_linktype(pcap_datalink(p
));
318 root
= gen_retblk(snaplen
);
320 if (optimize
&& !no_optimize
) {
323 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
324 bpf_error("expression rejects all packets");
326 program
->bf_insns
= icode_to_fcode(root
, &len
);
327 program
->bf_len
= len
;
335 * entry point for using the compiler with no pcap open
336 * pass in all the stuff that is needed explicitly instead.
339 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
340 struct bpf_program
*program
,
341 char *buf
, int optimize
, bpf_u_int32 mask
)
346 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
349 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
355 * Clean up a "struct bpf_program" by freeing all the memory allocated
359 pcap_freecode(struct bpf_program
*program
)
362 if (program
->bf_insns
!= NULL
) {
363 free((char *)program
->bf_insns
);
364 program
->bf_insns
= NULL
;
369 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
370 * which of the jt and jf fields has been resolved and which is a pointer
371 * back to another unresolved block (or nil). At least one of the fields
372 * in each block is already resolved.
375 backpatch(list
, target
)
376 struct block
*list
, *target
;
393 * Merge the lists in b0 and b1, using the 'sense' field to indicate
394 * which of jt and jf is the link.
398 struct block
*b0
, *b1
;
400 register struct block
**p
= &b0
;
402 /* Find end of list. */
404 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
406 /* Concatenate the lists. */
414 backpatch(p
, gen_retblk(snaplen
));
415 p
->sense
= !p
->sense
;
416 backpatch(p
, gen_retblk(0));
422 struct block
*b0
, *b1
;
424 backpatch(b0
, b1
->head
);
425 b0
->sense
= !b0
->sense
;
426 b1
->sense
= !b1
->sense
;
428 b1
->sense
= !b1
->sense
;
434 struct block
*b0
, *b1
;
436 b0
->sense
= !b0
->sense
;
437 backpatch(b0
, b1
->head
);
438 b0
->sense
= !b0
->sense
;
447 b
->sense
= !b
->sense
;
450 static struct block
*
451 gen_cmp(offset
, size
, v
)
458 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
461 b
= new_block(JMP(BPF_JEQ
));
468 static struct block
*
469 gen_cmp_gt(offset
, size
, v
)
476 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
479 b
= new_block(JMP(BPF_JGT
));
486 static struct block
*
487 gen_mcmp(offset
, size
, v
, mask
)
492 struct block
*b
= gen_cmp(offset
, size
, v
);
495 if (mask
!= 0xffffffff) {
496 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
503 static struct block
*
504 gen_bcmp(offset
, size
, v
)
505 register u_int offset
, size
;
506 register const u_char
*v
;
508 register struct block
*b
, *tmp
;
512 register const u_char
*p
= &v
[size
- 4];
513 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
514 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
516 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
523 register const u_char
*p
= &v
[size
- 2];
524 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
526 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
533 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
542 * Various code constructs need to know the layout of the data link
543 * layer. These variables give the necessary offsets. off_linktype
544 * is set to -1 for no encapsulation, in which case, IP is assumed.
546 static u_int off_linktype
;
563 off_nl
= 6; /* XXX in reality, variable! */
573 * SLIP doesn't have a link level type. The 16 byte
574 * header is hacked into our SLIP driver.
581 /* XXX this may be the same as the DLT_PPP_BSDOS case */
594 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
595 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
602 * This does no include the Ethernet header, and
603 * only covers session state.
616 * FDDI doesn't really have a link-level type field.
617 * We set "off_linktype" to the offset of the LLC header.
619 * To check for Ethernet types, we assume that SSAP = SNAP
620 * is being used and pick out the encapsulated Ethernet type.
621 * XXX - should we generate code to check for SNAP?
625 off_linktype
+= pcap_fddipad
;
629 off_nl
+= pcap_fddipad
;
635 * Token Ring doesn't really have a link-level type field.
636 * We set "off_linktype" to the offset of the LLC header.
638 * To check for Ethernet types, we assume that SSAP = SNAP
639 * is being used and pick out the encapsulated Ethernet type.
640 * XXX - should we generate code to check for SNAP?
642 * XXX - the header is actually variable-length.
643 * Some various Linux patched versions gave 38
644 * as "off_linktype" and 40 as "off_nl"; however,
645 * if a token ring packet has *no* routing
646 * information, i.e. is not source-routed, the correct
647 * values are 20 and 22, as they are in the vanilla code.
649 * A packet is source-routed iff the uppermost bit
650 * of the first byte of the source address, at an
651 * offset of 8, has the uppermost bit set. If the
652 * packet is source-routed, the total number of bytes
653 * of routing information is 2 plus bits 0x1F00 of
654 * the 16-bit value at an offset of 14 (shifted right
655 * 8 - figure out which byte that is).
663 * 802.11 doesn't really have a link-level type field.
664 * We set "off_linktype" to the offset of the LLC header.
666 * To check for Ethernet types, we assume that SSAP = SNAP
667 * is being used and pick out the encapsulated Ethernet type.
668 * XXX - should we generate code to check for SNAP?
670 * XXX - the header is actually variable-length. We
671 * assume a 24-byte link-layer header, as appears in
672 * data frames in networks with no bridges.
678 case DLT_PRISM_HEADER
:
680 * Same as 802.11, but with an additional header before
681 * the 802.11 header, containing a bunch of additional
682 * information including radio-level information.
684 * The header is 144 bytes long.
686 * XXX - same variable-length header problem; at least
687 * the Prism header is fixed-length.
689 off_linktype
= 144+24;
693 case DLT_ATM_RFC1483
:
695 * assume routed, non-ISO PDUs
696 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
707 case DLT_ATM_CLIP
: /* Linux ATM defines this */
712 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
719 * LocalTalk does have a 1-byte type field in the LLAP header,
720 * but really it just indicates whether there is a "short" or
721 * "long" DDP packet following.
727 bpf_error("unknown data link type %d", linktype
);
731 static struct block
*
738 s
= new_stmt(BPF_LD
|BPF_IMM
);
740 b
= new_block(JMP(BPF_JEQ
));
746 static inline struct block
*
749 return gen_uncond(1);
752 static inline struct block
*
755 return gen_uncond(0);
759 * Byte-swap a 32-bit number.
760 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
761 * big-endian platforms.)
763 #define SWAPLONG(y) \
764 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
766 static struct block
*
770 struct block
*b0
, *b1
;
779 * OSI protocols always use 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_ISONS
<< 8) | LLCSAP_ISONS
));
793 * NetBEUI always uses 802.2 encapsulation.
794 * XXX - should we check both the DSAP and the
795 * SSAP, like this, or should we check just the
798 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
800 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
801 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
809 * Ethernet_II frames, which are Ethernet
810 * frames with a frame type of ETHERTYPE_IPX;
812 * Ethernet_802.3 frames, which are 802.3
813 * frames (i.e., the type/length field is
814 * a length field, <= ETHERMTU, rather than
815 * a type field) with the first two bytes
816 * after the Ethernet/802.3 header being
819 * Ethernet_802.2 frames, which are 802.3
820 * frames with an 802.2 LLC header and
821 * with the IPX LSAP as the DSAP in the LLC
824 * Ethernet_SNAP frames, which are 802.3
825 * frames with an LLC header and a SNAP
826 * header and with an OUI of 0x000000
827 * (encapsulated Ethernet) and a protocol
828 * ID of ETHERTYPE_IPX in the SNAP header.
830 * XXX - should we generate the same code both
831 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
835 * This generates code to check both for the
836 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
838 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
839 (bpf_int32
)LLCSAP_IPX
);
840 b1
= gen_cmp(off_linktype
+ 2, BPF_H
,
845 * Now we add code to check for SNAP frames with
846 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
848 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
852 * Now we generate code to check for 802.3
855 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
859 * Now add the check for 802.3 frames before the
860 * check for Ethernet_802.2 and Ethernet_802.3,
861 * as those checks should only be done on 802.3
862 * frames, not on Ethernet frames.
867 * Now add the check for Ethernet_II frames, and
868 * do that before checking for the other frame
871 b0
= gen_cmp(off_linktype
, BPF_H
,
872 (bpf_int32
)ETHERTYPE_IPX
);
876 case ETHERTYPE_ATALK
:
879 * EtherTalk (AppleTalk protocols on Ethernet link
880 * layer) may use 802.2 encapsulation.
884 * Check for 802.2 encapsulation (EtherTalk phase 2?);
885 * we check for an Ethernet type field less than
886 * 1500, which means it's an 802.3 length field.
888 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
892 * 802.2-encapsulated ETHERTYPE_ATALK packets are
893 * SNAP packets with an organization code of
894 * 0x080007 (Apple, for Appletalk) and a protocol
895 * type of ETHERTYPE_ATALK (Appletalk).
897 * 802.2-encapsulated ETHERTYPE_AARP packets are
898 * SNAP packets with an organization code of
899 * 0x000000 (encapsulated Ethernet) and a protocol
900 * type of ETHERTYPE_AARP (Appletalk ARP).
902 if (proto
== ETHERTYPE_ATALK
)
903 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
904 else /* proto == ETHERTYPE_AARP */
905 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
909 * Check for Ethernet encapsulation (Ethertalk
910 * phase 1?); we just check for the Ethernet
913 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
919 if (proto
<= ETHERMTU
) {
921 * This is an LLC SAP value, so the frames
922 * that match would be 802.2 frames.
923 * Check that the frame is an 802.2 frame
924 * (i.e., that the length/type field is
925 * a length field, <= ETHERMTU) and
926 * then check the DSAP.
928 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
930 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
936 * This is an Ethernet type, so compare
937 * the length/type field with it (if
938 * the frame is an 802.2 frame, the length
939 * field will be <= ETHERMTU, and, as
940 * "proto" is > ETHERMTU, this test
941 * will fail and the frame won't match,
942 * which is what we want).
944 return gen_cmp(off_linktype
, BPF_H
,
951 case DLT_PRISM_HEADER
:
954 case DLT_ATM_RFC1483
:
957 * XXX - handle token-ring variable-length header.
962 return gen_cmp(off_linktype
, BPF_H
, (long)
963 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
966 return gen_cmp(off_linktype
, BPF_H
, (long)
967 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
971 * XXX - are there ever SNAP frames for IPX on
972 * non-Ethernet 802.x networks?
974 return gen_cmp(off_linktype
, BPF_B
,
975 (bpf_int32
)LLCSAP_IPX
);
977 case ETHERTYPE_ATALK
:
979 * 802.2-encapsulated ETHERTYPE_ATALK packets are
980 * SNAP packets with an organization code of
981 * 0x080007 (Apple, for Appletalk) and a protocol
982 * type of ETHERTYPE_ATALK (Appletalk).
984 * XXX - check for an organization code of
985 * encapsulated Ethernet as well?
987 return gen_snap(0x080007, ETHERTYPE_ATALK
,
993 * XXX - we don't have to check for IPX 802.3
994 * here, but should we check for the IPX Ethertype?
996 if (proto
<= ETHERMTU
) {
998 * This is an LLC SAP value, so check
1001 return gen_cmp(off_linktype
, BPF_B
,
1005 * This is an Ethernet type; we assume
1006 * that it's unlikely that it'll
1007 * appear in the right place at random,
1008 * and therefore check only the
1009 * location that would hold the Ethernet
1010 * type in a SNAP frame with an organization
1011 * code of 0x000000 (encapsulated Ethernet).
1013 * XXX - if we were to check for the SNAP DSAP
1014 * and LSAP, as per XXX, and were also to check
1015 * for an organization code of 0x000000
1016 * (encapsulated Ethernet), we'd do
1018 * return gen_snap(0x000000, proto,
1021 * here; for now, we don't, as per the above.
1022 * I don't know whether it's worth the
1023 * extra CPU time to do the right check
1026 return gen_cmp(off_linktype
+6, BPF_H
,
1037 * OSI protocols always use 802.2 encapsulation.
1038 * XXX - should we check both the DSAP and the
1039 * LSAP, like this, or should we check just the
1042 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1043 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1044 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1048 case LLCSAP_NETBEUI
:
1050 * NetBEUI always uses 802.2 encapsulation.
1051 * XXX - should we check both the DSAP and the
1052 * LSAP, like this, or should we check just the
1055 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1056 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1057 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1063 * Ethernet_II frames, which are Ethernet
1064 * frames with a frame type of ETHERTYPE_IPX;
1066 * Ethernet_802.3 frames, which have a frame
1067 * type of LINUX_SLL_P_802_3;
1069 * Ethernet_802.2 frames, which are 802.3
1070 * frames with an 802.2 LLC header (i.e, have
1071 * a frame type of LINUX_SLL_P_802_2) and
1072 * with the IPX LSAP as the DSAP in the LLC
1075 * Ethernet_SNAP frames, which are 802.3
1076 * frames with an LLC header and a SNAP
1077 * header and with an OUI of 0x000000
1078 * (encapsulated Ethernet) and a protocol
1079 * ID of ETHERTYPE_IPX in the SNAP header.
1081 * First, do the checks on LINUX_SLL_P_802_2
1082 * frames; generate the check for either
1083 * Ethernet_802.2 or Ethernet_SNAP frames, and
1084 * then put a check for LINUX_SLL_P_802_2 frames
1087 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1088 (bpf_int32
)LLCSAP_IPX
);
1089 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1092 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1096 * Now check for 802.3 frames and OR that with
1097 * the previous test.
1099 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1103 * Now add the check for Ethernet_II frames, and
1104 * do that before checking for the other frame
1107 b0
= gen_cmp(off_linktype
, BPF_H
,
1108 (bpf_int32
)ETHERTYPE_IPX
);
1112 case ETHERTYPE_ATALK
:
1113 case ETHERTYPE_AARP
:
1115 * EtherTalk (AppleTalk protocols on Ethernet link
1116 * layer) may use 802.2 encapsulation.
1120 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1121 * we check for the 802.2 protocol type in the
1122 * "Ethernet type" field.
1124 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1127 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1128 * SNAP packets with an organization code of
1129 * 0x080007 (Apple, for Appletalk) and a protocol
1130 * type of ETHERTYPE_ATALK (Appletalk).
1132 * 802.2-encapsulated ETHERTYPE_AARP packets are
1133 * SNAP packets with an organization code of
1134 * 0x000000 (encapsulated Ethernet) and a protocol
1135 * type of ETHERTYPE_AARP (Appletalk ARP).
1137 if (proto
== ETHERTYPE_ATALK
)
1138 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1140 else /* proto == ETHERTYPE_AARP */
1141 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1146 * Check for Ethernet encapsulation (Ethertalk
1147 * phase 1?); we just check for the Ethernet
1150 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1156 if (proto
<= ETHERMTU
) {
1158 * This is an LLC SAP value, so the frames
1159 * that match would be 802.2 frames.
1160 * Check for the 802.2 protocol type
1161 * in the "Ethernet type" field, and
1162 * then check the DSAP.
1164 b0
= gen_cmp(off_linktype
, BPF_H
,
1166 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1172 * This is an Ethernet type, so compare
1173 * the length/type field with it (if
1174 * the frame is an 802.2 frame, the length
1175 * field will be <= ETHERMTU, and, as
1176 * "proto" is > ETHERMTU, this test
1177 * will fail and the frame won't match,
1178 * which is what we want).
1180 return gen_cmp(off_linktype
, BPF_H
,
1187 case DLT_SLIP_BSDOS
:
1190 * These types don't provide any type field; packets
1193 * XXX - for IPv4, check for a version number of 4, and,
1194 * for IPv6, check for a version number of 6?
1200 case ETHERTYPE_IPV6
:
1202 return gen_true(); /* always true */
1205 return gen_false(); /* always false */
1210 case DLT_PPP_SERIAL
:
1213 * We use Ethernet protocol types inside libpcap;
1214 * map them to the corresponding PPP protocol types.
1219 proto
= PPP_IP
; /* XXX was 0x21 */
1223 case ETHERTYPE_IPV6
:
1232 case ETHERTYPE_ATALK
:
1246 * I'm assuming the "Bridging PDU"s that go
1247 * over PPP are Spanning Tree Protocol
1261 * We use Ethernet protocol types inside libpcap;
1262 * map them to the corresponding PPP protocol types.
1267 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1268 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1270 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1275 case ETHERTYPE_IPV6
:
1285 case ETHERTYPE_ATALK
:
1299 * I'm assuming the "Bridging PDU"s that go
1300 * over PPP are Spanning Tree Protocol
1315 * For DLT_NULL, the link-layer header is a 32-bit
1316 * word containing an AF_ value in *host* byte order.
1318 * In addition, if we're reading a saved capture file,
1319 * the host byte order in the capture may not be the
1320 * same as the host byte order on this machine.
1322 * For DLT_LOOP, the link-layer header is a 32-bit
1323 * word containing an AF_ value in *network* byte order.
1325 * XXX - AF_ values may, unfortunately, be platform-
1326 * dependent; for example, FreeBSD's AF_INET6 is 24
1327 * whilst NetBSD's and OpenBSD's is 26.
1329 * This means that, when reading a capture file, just
1330 * checking for our AF_INET6 value won't work if the
1331 * capture file came from another OS.
1340 case ETHERTYPE_IPV6
:
1347 * Not a type on which we support filtering.
1348 * XXX - support those that have AF_ values
1349 * #defined on this platform, at least?
1354 if (linktype
== DLT_NULL
) {
1356 * The AF_ value is in host byte order, but
1357 * the BPF interpreter will convert it to
1358 * network byte order.
1360 * If this is a save file, and it's from a
1361 * machine with the opposite byte order to
1362 * ours, we byte-swap the AF_ value.
1364 * Then we run it through "htonl()", and
1365 * generate code to compare against the result.
1367 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1368 bpf_pcap
->sf
.swapped
)
1369 proto
= SWAPLONG(proto
);
1370 proto
= htonl(proto
);
1372 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1376 * XXX should we check for first fragment if the protocol
1383 case ETHERTYPE_IPV6
:
1384 return(gen_cmp(2, BPF_B
,
1385 (bpf_int32
)htonl(ARCTYPE_INET6
)));
1388 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_IP
));
1389 b1
= gen_cmp(2, BPF_B
,
1390 (bpf_int32
)htonl(ARCTYPE_IP_OLD
));
1394 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_ARP
));
1395 b1
= gen_cmp(2, BPF_B
,
1396 (bpf_int32
)htonl(ARCTYPE_ARP_OLD
));
1399 case ETHERTYPE_REVARP
:
1400 return(gen_cmp(2, BPF_B
,
1401 (bpf_int32
)htonl(ARCTYPE_REVARP
)));
1402 case ETHERTYPE_ATALK
:
1403 return(gen_cmp(2, BPF_B
,
1404 (bpf_int32
)htonl(ARCTYPE_ATALK
)));
1410 case ETHERTYPE_ATALK
:
1419 * All the types that have no encapsulation should either be
1420 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1421 * all packets are IP packets, or should be handled in some
1422 * special case, if none of them are (if some are and some
1423 * aren't, the lack of encapsulation is a problem, as we'd
1424 * have to find some other way of determining the packet type).
1426 * Therefore, if "off_linktype" is -1, there's an error.
1428 if (off_linktype
== -1)
1432 * Any type not handled above should always have an Ethernet
1433 * type at an offset of "off_linktype". (PPP is partially
1434 * handled above - the protocol type is mapped from the
1435 * Ethernet and LLC types we use internally to the corresponding
1436 * PPP type - but the PPP type is always specified by a value
1437 * at "off_linktype", so we don't have to do the code generation
1440 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1444 * Check for an LLC SNAP packet with a given organization code and
1445 * protocol type; we check the entire contents of the 802.2 LLC and
1446 * snap headers, checking for DSAP and SSAP of SNAP and a control
1447 * field of 0x03 in the LLC header, and for the specified organization
1448 * code and protocol type in the SNAP header.
1450 static struct block
*
1451 gen_snap(orgcode
, ptype
, offset
)
1452 bpf_u_int32 orgcode
;
1456 u_char snapblock
[8];
1458 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1459 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1460 snapblock
[2] = 0x03; /* control = UI */
1461 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1462 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1463 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1464 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1465 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1466 return gen_bcmp(offset
, 8, snapblock
);
1469 static struct block
*
1470 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1474 u_int src_off
, dst_off
;
1476 struct block
*b0
, *b1
;
1490 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1491 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1497 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1498 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1505 b0
= gen_linktype(proto
);
1506 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1512 static struct block
*
1513 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1514 struct in6_addr
*addr
;
1515 struct in6_addr
*mask
;
1517 u_int src_off
, dst_off
;
1519 struct block
*b0
, *b1
;
1534 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1535 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1541 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1542 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1549 /* this order is important */
1550 a
= (u_int32_t
*)addr
;
1551 m
= (u_int32_t
*)mask
;
1552 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1553 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1555 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1557 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1559 b0
= gen_linktype(proto
);
1565 static struct block
*
1566 gen_ehostop(eaddr
, dir
)
1567 register const u_char
*eaddr
;
1570 register struct block
*b0
, *b1
;
1574 return gen_bcmp(6, 6, eaddr
);
1577 return gen_bcmp(0, 6, eaddr
);
1580 b0
= gen_ehostop(eaddr
, Q_SRC
);
1581 b1
= gen_ehostop(eaddr
, Q_DST
);
1587 b0
= gen_ehostop(eaddr
, Q_SRC
);
1588 b1
= gen_ehostop(eaddr
, Q_DST
);
1597 * Like gen_ehostop, but for DLT_FDDI
1599 static struct block
*
1600 gen_fhostop(eaddr
, dir
)
1601 register const u_char
*eaddr
;
1604 struct block
*b0
, *b1
;
1609 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1611 return gen_bcmp(6 + 1, 6, eaddr
);
1616 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1618 return gen_bcmp(0 + 1, 6, eaddr
);
1622 b0
= gen_fhostop(eaddr
, Q_SRC
);
1623 b1
= gen_fhostop(eaddr
, Q_DST
);
1629 b0
= gen_fhostop(eaddr
, Q_SRC
);
1630 b1
= gen_fhostop(eaddr
, Q_DST
);
1639 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1641 static struct block
*
1642 gen_thostop(eaddr
, dir
)
1643 register const u_char
*eaddr
;
1646 register struct block
*b0
, *b1
;
1650 return gen_bcmp(8, 6, eaddr
);
1653 return gen_bcmp(2, 6, eaddr
);
1656 b0
= gen_thostop(eaddr
, Q_SRC
);
1657 b1
= gen_thostop(eaddr
, Q_DST
);
1663 b0
= gen_thostop(eaddr
, Q_SRC
);
1664 b1
= gen_thostop(eaddr
, Q_DST
);
1673 * This is quite tricky because there may be pad bytes in front of the
1674 * DECNET header, and then there are two possible data packet formats that
1675 * carry both src and dst addresses, plus 5 packet types in a format that
1676 * carries only the src node, plus 2 types that use a different format and
1677 * also carry just the src node.
1681 * Instead of doing those all right, we just look for data packets with
1682 * 0 or 1 bytes of padding. If you want to look at other packets, that
1683 * will require a lot more hacking.
1685 * To add support for filtering on DECNET "areas" (network numbers)
1686 * one would want to add a "mask" argument to this routine. That would
1687 * make the filter even more inefficient, although one could be clever
1688 * and not generate masking instructions if the mask is 0xFFFF.
1690 static struct block
*
1691 gen_dnhostop(addr
, dir
, base_off
)
1696 struct block
*b0
, *b1
, *b2
, *tmp
;
1697 u_int offset_lh
; /* offset if long header is received */
1698 u_int offset_sh
; /* offset if short header is received */
1703 offset_sh
= 1; /* follows flags */
1704 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1708 offset_sh
= 3; /* follows flags, dstnode */
1709 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1713 /* Inefficient because we do our Calvinball dance twice */
1714 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1715 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1721 /* Inefficient because we do our Calvinball dance twice */
1722 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1723 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1728 bpf_error("ISO host filtering not implemented");
1733 b0
= gen_linktype(ETHERTYPE_DN
);
1734 /* Check for pad = 1, long header case */
1735 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1736 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1737 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1738 BPF_H
, (bpf_int32
)ntohs(addr
));
1740 /* Check for pad = 0, long header case */
1741 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1742 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1745 /* Check for pad = 1, short header case */
1746 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1747 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1748 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1749 BPF_H
, (bpf_int32
)ntohs(addr
));
1752 /* Check for pad = 0, short header case */
1753 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1754 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1758 /* Combine with test for linktype */
1763 static struct block
*
1764 gen_host(addr
, mask
, proto
, dir
)
1770 struct block
*b0
, *b1
;
1775 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1776 if (off_linktype
!= -1) {
1777 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1779 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1785 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1786 off_nl
+ 12, off_nl
+ 16);
1789 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1790 off_nl
+ 14, off_nl
+ 24);
1793 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1794 off_nl
+ 14, off_nl
+ 24);
1797 bpf_error("'tcp' modifier applied to host");
1800 bpf_error("'sctp' modifier applied to host");
1803 bpf_error("'udp' modifier applied to host");
1806 bpf_error("'icmp' modifier applied to host");
1809 bpf_error("'igmp' modifier applied to host");
1812 bpf_error("'igrp' modifier applied to host");
1815 bpf_error("'pim' modifier applied to host");
1818 bpf_error("'vrrp' modifier applied to host");
1821 bpf_error("ATALK host filtering not implemented");
1824 bpf_error("AARP host filtering not implemented");
1827 return gen_dnhostop(addr
, dir
, off_nl
);
1830 bpf_error("SCA host filtering not implemented");
1833 bpf_error("LAT host filtering not implemented");
1836 bpf_error("MOPDL host filtering not implemented");
1839 bpf_error("MOPRC host filtering not implemented");
1843 bpf_error("'ip6' modifier applied to ip host");
1846 bpf_error("'icmp6' modifier applied to host");
1850 bpf_error("'ah' modifier applied to host");
1853 bpf_error("'esp' modifier applied to host");
1856 bpf_error("ISO host filtering not implemented");
1859 bpf_error("'esis' modifier applied to host");
1862 bpf_error("'isis' modifier applied to host");
1865 bpf_error("'clnp' modifier applied to host");
1868 bpf_error("'stp' modifier applied to host");
1871 bpf_error("IPX host filtering not implemented");
1874 bpf_error("'netbeui' modifier applied to host");
1883 static struct block
*
1884 gen_host6(addr
, mask
, proto
, dir
)
1885 struct in6_addr
*addr
;
1886 struct in6_addr
*mask
;
1893 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1896 bpf_error("'ip' modifier applied to ip6 host");
1899 bpf_error("'rarp' modifier applied to ip6 host");
1902 bpf_error("'arp' modifier applied to ip6 host");
1905 bpf_error("'sctp' modifier applied to host");
1908 bpf_error("'tcp' modifier applied to host");
1911 bpf_error("'udp' modifier applied to host");
1914 bpf_error("'icmp' modifier applied to host");
1917 bpf_error("'igmp' modifier applied to host");
1920 bpf_error("'igrp' modifier applied to host");
1923 bpf_error("'pim' modifier applied to host");
1926 bpf_error("'vrrp' modifier applied to host");
1929 bpf_error("ATALK host filtering not implemented");
1932 bpf_error("AARP host filtering not implemented");
1935 bpf_error("'decnet' modifier applied to ip6 host");
1938 bpf_error("SCA host filtering not implemented");
1941 bpf_error("LAT host filtering not implemented");
1944 bpf_error("MOPDL host filtering not implemented");
1947 bpf_error("MOPRC host filtering not implemented");
1950 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1951 off_nl
+ 8, off_nl
+ 24);
1954 bpf_error("'icmp6' modifier applied to host");
1957 bpf_error("'ah' modifier applied to host");
1960 bpf_error("'esp' modifier applied to host");
1963 bpf_error("ISO host filtering not implemented");
1966 bpf_error("'esis' modifier applied to host");
1969 bpf_error("'isis' modifier applied to host");
1972 bpf_error("'clnp' modifier applied to host");
1975 bpf_error("'stp' modifier applied to host");
1978 bpf_error("IPX host filtering not implemented");
1981 bpf_error("'netbeui' modifier applied to host");
1991 static struct block
*
1992 gen_gateway(eaddr
, alist
, proto
, dir
)
1993 const u_char
*eaddr
;
1994 bpf_u_int32
**alist
;
1998 struct block
*b0
, *b1
, *tmp
;
2001 bpf_error("direction applied to 'gateway'");
2008 if (linktype
== DLT_EN10MB
)
2009 b0
= gen_ehostop(eaddr
, Q_OR
);
2010 else if (linktype
== DLT_FDDI
)
2011 b0
= gen_fhostop(eaddr
, Q_OR
);
2012 else if (linktype
== DLT_IEEE802
)
2013 b0
= gen_thostop(eaddr
, Q_OR
);
2016 "'gateway' supported only on ethernet, FDDI or token ring");
2018 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2020 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2028 bpf_error("illegal modifier of 'gateway'");
2034 gen_proto_abbrev(proto
)
2045 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2047 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2053 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2055 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2061 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2063 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2069 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2072 #ifndef IPPROTO_IGMP
2073 #define IPPROTO_IGMP 2
2077 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2080 #ifndef IPPROTO_IGRP
2081 #define IPPROTO_IGRP 9
2084 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2088 #define IPPROTO_PIM 103
2092 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2094 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2099 #ifndef IPPROTO_VRRP
2100 #define IPPROTO_VRRP 112
2104 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2108 b1
= gen_linktype(ETHERTYPE_IP
);
2112 b1
= gen_linktype(ETHERTYPE_ARP
);
2116 b1
= gen_linktype(ETHERTYPE_REVARP
);
2120 bpf_error("link layer applied in wrong context");
2123 b1
= gen_linktype(ETHERTYPE_ATALK
);
2127 b1
= gen_linktype(ETHERTYPE_AARP
);
2131 b1
= gen_linktype(ETHERTYPE_DN
);
2135 b1
= gen_linktype(ETHERTYPE_SCA
);
2139 b1
= gen_linktype(ETHERTYPE_LAT
);
2143 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2147 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2152 b1
= gen_linktype(ETHERTYPE_IPV6
);
2155 #ifndef IPPROTO_ICMPV6
2156 #define IPPROTO_ICMPV6 58
2159 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2164 #define IPPROTO_AH 51
2167 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2169 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2175 #define IPPROTO_ESP 50
2178 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2180 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2186 b1
= gen_linktype(LLCSAP_ISONS
);
2190 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2194 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2198 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2202 b1
= gen_linktype(LLCSAP_8021D
);
2206 b1
= gen_linktype(LLCSAP_IPX
);
2210 b1
= gen_linktype(LLCSAP_NETBEUI
);
2219 static struct block
*
2226 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2227 s
->s
.k
= off_nl
+ 6;
2228 b
= new_block(JMP(BPF_JSET
));
2236 static struct block
*
2237 gen_portatom(off
, v
)
2244 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2247 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2248 s
->next
->s
.k
= off_nl
+ off
;
2250 b
= new_block(JMP(BPF_JEQ
));
2258 static struct block
*
2259 gen_portatom6(off
, v
)
2263 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2268 gen_portop(port
, proto
, dir
)
2269 int port
, proto
, dir
;
2271 struct block
*b0
, *b1
, *tmp
;
2273 /* ip proto 'proto' */
2274 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2280 b1
= gen_portatom(0, (bpf_int32
)port
);
2284 b1
= gen_portatom(2, (bpf_int32
)port
);
2289 tmp
= gen_portatom(0, (bpf_int32
)port
);
2290 b1
= gen_portatom(2, (bpf_int32
)port
);
2295 tmp
= gen_portatom(0, (bpf_int32
)port
);
2296 b1
= gen_portatom(2, (bpf_int32
)port
);
2308 static struct block
*
2309 gen_port(port
, ip_proto
, dir
)
2314 struct block
*b0
, *b1
, *tmp
;
2316 /* ether proto ip */
2317 b0
= gen_linktype(ETHERTYPE_IP
);
2323 b1
= gen_portop(port
, ip_proto
, dir
);
2327 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2328 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2330 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
2343 gen_portop6(port
, proto
, dir
)
2344 int port
, proto
, dir
;
2346 struct block
*b0
, *b1
, *tmp
;
2348 /* ip proto 'proto' */
2349 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2353 b1
= gen_portatom6(0, (bpf_int32
)port
);
2357 b1
= gen_portatom6(2, (bpf_int32
)port
);
2362 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2363 b1
= gen_portatom6(2, (bpf_int32
)port
);
2368 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2369 b1
= gen_portatom6(2, (bpf_int32
)port
);
2381 static struct block
*
2382 gen_port6(port
, ip_proto
, dir
)
2387 struct block
*b0
, *b1
, *tmp
;
2389 /* ether proto ip */
2390 b0
= gen_linktype(ETHERTYPE_IPV6
);
2396 b1
= gen_portop6(port
, ip_proto
, dir
);
2400 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2401 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2403 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
2416 lookup_proto(name
, proto
)
2417 register const char *name
;
2427 v
= pcap_nametoproto(name
);
2428 if (v
== PROTO_UNDEF
)
2429 bpf_error("unknown ip proto '%s'", name
);
2433 /* XXX should look up h/w protocol type based on linktype */
2434 v
= pcap_nametoeproto(name
);
2435 if (v
== PROTO_UNDEF
)
2436 bpf_error("unknown ether proto '%s'", name
);
2440 if (strcmp(name
, "esis") == 0)
2442 else if (strcmp(name
, "isis") == 0)
2444 else if (strcmp(name
, "clnp") == 0)
2447 bpf_error("unknown osi proto '%s'", name
);
2467 static struct block
*
2468 gen_protochain(v
, proto
, dir
)
2473 #ifdef NO_PROTOCHAIN
2474 return gen_proto(v
, proto
, dir
);
2476 struct block
*b0
, *b
;
2477 struct slist
*s
[100];
2478 int fix2
, fix3
, fix4
, fix5
;
2479 int ahcheck
, again
, end
;
2481 int reg2
= alloc_reg();
2483 memset(s
, 0, sizeof(s
));
2484 fix2
= fix3
= fix4
= fix5
= 0;
2491 b0
= gen_protochain(v
, Q_IP
, dir
);
2492 b
= gen_protochain(v
, Q_IPV6
, dir
);
2496 bpf_error("bad protocol applied for 'protochain'");
2500 no_optimize
= 1; /*this code is not compatible with optimzer yet */
2503 * s[0] is a dummy entry to protect other BPF insn from damaged
2504 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
2505 * hard to find interdependency made by jump table fixup.
2508 s
[i
] = new_stmt(0); /*dummy*/
2513 b0
= gen_linktype(ETHERTYPE_IP
);
2516 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2517 s
[i
]->s
.k
= off_nl
+ 9;
2519 /* X = ip->ip_hl << 2 */
2520 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2526 b0
= gen_linktype(ETHERTYPE_IPV6
);
2528 /* A = ip6->ip_nxt */
2529 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2530 s
[i
]->s
.k
= off_nl
+ 6;
2532 /* X = sizeof(struct ip6_hdr) */
2533 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
2539 bpf_error("unsupported proto to gen_protochain");
2543 /* again: if (A == v) goto end; else fall through; */
2545 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2547 s
[i
]->s
.jt
= NULL
; /*later*/
2548 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2552 #ifndef IPPROTO_NONE
2553 #define IPPROTO_NONE 59
2555 /* if (A == IPPROTO_NONE) goto end */
2556 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_NONE
;
2560 s
[fix5
]->s
.jf
= s
[i
];
2565 if (proto
== Q_IPV6
) {
2566 int v6start
, v6end
, v6advance
, j
;
2569 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
2570 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2571 s
[i
]->s
.jt
= NULL
; /*later*/
2572 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2573 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
2574 s
[fix2
]->s
.jf
= s
[i
];
2576 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
2577 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2578 s
[i
]->s
.jt
= NULL
; /*later*/
2579 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2580 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
2582 /* if (A == IPPROTO_ROUTING) goto v6advance */
2583 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2584 s
[i
]->s
.jt
= NULL
; /*later*/
2585 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2586 s
[i
]->s
.k
= IPPROTO_ROUTING
;
2588 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
2589 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2590 s
[i
]->s
.jt
= NULL
; /*later*/
2591 s
[i
]->s
.jf
= NULL
; /*later*/
2592 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
2603 * X = X + (P[X + 1] + 1) * 8;
2606 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2608 /* A = P[X + packet head] */
2609 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2613 s
[i
] = new_stmt(BPF_ST
);
2617 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2620 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2624 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2626 /* A = P[X + packet head]; */
2627 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2631 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2635 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2639 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2642 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2646 /* goto again; (must use BPF_JA for backward jump) */
2647 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2648 s
[i
]->s
.k
= again
- i
- 1;
2649 s
[i
- 1]->s
.jf
= s
[i
];
2653 for (j
= v6start
; j
<= v6end
; j
++)
2654 s
[j
]->s
.jt
= s
[v6advance
];
2659 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2661 s
[fix2
]->s
.jf
= s
[i
];
2667 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2668 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2669 s
[i
]->s
.jt
= NULL
; /*later*/
2670 s
[i
]->s
.jf
= NULL
; /*later*/
2671 s
[i
]->s
.k
= IPPROTO_AH
;
2673 s
[fix3
]->s
.jf
= s
[ahcheck
];
2680 * X = X + (P[X + 1] + 2) * 4;
2683 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2685 /* A = P[X + packet head]; */
2686 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2690 s
[i
] = new_stmt(BPF_ST
);
2694 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2697 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2701 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2703 /* A = P[X + packet head] */
2704 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2708 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2712 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2716 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2719 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2723 /* goto again; (must use BPF_JA for backward jump) */
2724 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2725 s
[i
]->s
.k
= again
- i
- 1;
2730 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2732 s
[fix2
]->s
.jt
= s
[end
];
2733 s
[fix4
]->s
.jf
= s
[end
];
2734 s
[fix5
]->s
.jt
= s
[end
];
2741 for (i
= 0; i
< max
- 1; i
++)
2742 s
[i
]->next
= s
[i
+ 1];
2743 s
[max
- 1]->next
= NULL
;
2748 b
= new_block(JMP(BPF_JEQ
));
2749 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2759 static struct block
*
2760 gen_proto(v
, proto
, dir
)
2765 struct block
*b0
, *b1
;
2767 if (dir
!= Q_DEFAULT
)
2768 bpf_error("direction applied to 'proto'");
2773 b0
= gen_proto(v
, Q_IP
, dir
);
2774 b1
= gen_proto(v
, Q_IPV6
, dir
);
2781 b0
= gen_linktype(ETHERTYPE_IP
);
2783 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2785 b1
= gen_protochain(v
, Q_IP
);
2791 b0
= gen_linktype(LLCSAP_ISONS
);
2792 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2797 bpf_error("arp does not encapsulate another protocol");
2801 bpf_error("rarp does not encapsulate another protocol");
2805 bpf_error("atalk encapsulation is not specifiable");
2809 bpf_error("decnet encapsulation is not specifiable");
2813 bpf_error("sca does not encapsulate another protocol");
2817 bpf_error("lat does not encapsulate another protocol");
2821 bpf_error("moprc does not encapsulate another protocol");
2825 bpf_error("mopdl does not encapsulate another protocol");
2829 return gen_linktype(v
);
2832 bpf_error("'udp proto' is bogus");
2836 bpf_error("'tcp proto' is bogus");
2840 bpf_error("'sctp proto' is bogus");
2844 bpf_error("'icmp proto' is bogus");
2848 bpf_error("'igmp proto' is bogus");
2852 bpf_error("'igrp proto' is bogus");
2856 bpf_error("'pim proto' is bogus");
2860 bpf_error("'vrrp proto' is bogus");
2865 b0
= gen_linktype(ETHERTYPE_IPV6
);
2867 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2869 b1
= gen_protochain(v
, Q_IPV6
);
2875 bpf_error("'icmp6 proto' is bogus");
2879 bpf_error("'ah proto' is bogus");
2882 bpf_error("'ah proto' is bogus");
2885 bpf_error("'stp proto' is bogus");
2888 bpf_error("'ipx proto' is bogus");
2891 bpf_error("'netbeui proto' is bogus");
2902 register const char *name
;
2905 int proto
= q
.proto
;
2909 bpf_u_int32 mask
, addr
;
2911 bpf_u_int32
**alist
;
2914 struct sockaddr_in
*sin
;
2915 struct sockaddr_in6
*sin6
;
2916 struct addrinfo
*res
, *res0
;
2917 struct in6_addr mask128
;
2919 struct block
*b
, *tmp
;
2920 int port
, real_proto
;
2925 addr
= pcap_nametonetaddr(name
);
2927 bpf_error("unknown network '%s'", name
);
2928 /* Left justify network addr and calculate its network mask */
2930 while (addr
&& (addr
& 0xff000000) == 0) {
2934 return gen_host(addr
, mask
, proto
, dir
);
2938 if (proto
== Q_LINK
) {
2942 eaddr
= pcap_ether_hostton(name
);
2945 "unknown ether host '%s'", name
);
2946 b
= gen_ehostop(eaddr
, dir
);
2951 eaddr
= pcap_ether_hostton(name
);
2954 "unknown FDDI host '%s'", name
);
2955 b
= gen_fhostop(eaddr
, dir
);
2960 eaddr
= pcap_ether_hostton(name
);
2963 "unknown token ring host '%s'", name
);
2964 b
= gen_thostop(eaddr
, dir
);
2970 "only ethernet/FDDI/token ring supports link-level host name");
2973 } else if (proto
== Q_DECNET
) {
2974 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2976 * I don't think DECNET hosts can be multihomed, so
2977 * there is no need to build up a list of addresses
2979 return (gen_host(dn_addr
, 0, proto
, dir
));
2982 alist
= pcap_nametoaddr(name
);
2983 if (alist
== NULL
|| *alist
== NULL
)
2984 bpf_error("unknown host '%s'", name
);
2986 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2988 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2990 tmp
= gen_host(**alist
++, 0xffffffff,
2997 memset(&mask128
, 0xff, sizeof(mask128
));
2998 res0
= res
= pcap_nametoaddrinfo(name
);
3000 bpf_error("unknown host '%s'", name
);
3002 tproto
= tproto6
= proto
;
3003 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
3007 for (res
= res0
; res
; res
= res
->ai_next
) {
3008 switch (res
->ai_family
) {
3010 if (tproto
== Q_IPV6
)
3013 sin
= (struct sockaddr_in
*)
3015 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
3016 0xffffffff, tproto
, dir
);
3019 if (tproto6
== Q_IP
)
3022 sin6
= (struct sockaddr_in6
*)
3024 tmp
= gen_host6(&sin6
->sin6_addr
,
3025 &mask128
, tproto6
, dir
);
3036 bpf_error("unknown host '%s'%s", name
,
3037 (proto
== Q_DEFAULT
)
3039 : " for specified address family");
3046 if (proto
!= Q_DEFAULT
&&
3047 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
3048 bpf_error("illegal qualifier of 'port'");
3049 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
3050 bpf_error("unknown port '%s'", name
);
3051 if (proto
== Q_UDP
) {
3052 if (real_proto
== IPPROTO_TCP
)
3053 bpf_error("port '%s' is tcp", name
);
3054 else if (real_proto
== IPPROTO_SCTP
)
3055 bpf_error("port '%s' is sctp", name
);
3057 /* override PROTO_UNDEF */
3058 real_proto
= IPPROTO_UDP
;
3060 if (proto
== Q_TCP
) {
3061 if (real_proto
== IPPROTO_UDP
)
3062 bpf_error("port '%s' is udp", name
);
3064 else if (real_proto
== IPPROTO_SCTP
)
3065 bpf_error("port '%s' is sctp", name
);
3067 /* override PROTO_UNDEF */
3068 real_proto
= IPPROTO_TCP
;
3070 if (proto
== Q_SCTP
) {
3071 if (real_proto
== IPPROTO_UDP
)
3072 bpf_error("port '%s' is udp", name
);
3074 else if (real_proto
== IPPROTO_TCP
)
3075 bpf_error("port '%s' is tcp", name
);
3077 /* override PROTO_UNDEF */
3078 real_proto
= IPPROTO_SCTP
;
3081 return gen_port(port
, real_proto
, dir
);
3085 b
= gen_port(port
, real_proto
, dir
);
3086 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3093 eaddr
= pcap_ether_hostton(name
);
3095 bpf_error("unknown ether host: %s", name
);
3097 alist
= pcap_nametoaddr(name
);
3098 if (alist
== NULL
|| *alist
== NULL
)
3099 bpf_error("unknown host '%s'", name
);
3100 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
3104 bpf_error("'gateway' not supported in this configuration");
3108 real_proto
= lookup_proto(name
, proto
);
3109 if (real_proto
>= 0)
3110 return gen_proto(real_proto
, proto
, dir
);
3112 bpf_error("unknown protocol: %s", name
);
3115 real_proto
= lookup_proto(name
, proto
);
3116 if (real_proto
>= 0)
3117 return gen_protochain(real_proto
, proto
, dir
);
3119 bpf_error("unknown protocol: %s", name
);
3131 gen_mcode(s1
, s2
, masklen
, q
)
3132 register const char *s1
, *s2
;
3133 register int masklen
;
3136 register int nlen
, mlen
;
3139 nlen
= __pcap_atoin(s1
, &n
);
3140 /* Promote short ipaddr */
3144 mlen
= __pcap_atoin(s2
, &m
);
3145 /* Promote short ipaddr */
3148 bpf_error("non-network bits set in \"%s mask %s\"",
3151 /* Convert mask len to mask */
3153 bpf_error("mask length must be <= 32");
3154 m
= 0xffffffff << (32 - masklen
);
3156 bpf_error("non-network bits set in \"%s/%d\"",
3163 return gen_host(n
, m
, q
.proto
, q
.dir
);
3166 bpf_error("Mask syntax for networks only");
3173 register const char *s
;
3178 int proto
= q
.proto
;
3184 else if (q
.proto
== Q_DECNET
)
3185 vlen
= __pcap_atodn(s
, &v
);
3187 vlen
= __pcap_atoin(s
, &v
);
3194 if (proto
== Q_DECNET
)
3195 return gen_host(v
, 0, proto
, dir
);
3196 else if (proto
== Q_LINK
) {
3197 bpf_error("illegal link layer address");
3200 if (s
== NULL
&& q
.addr
== Q_NET
) {
3201 /* Promote short net number */
3202 while (v
&& (v
& 0xff000000) == 0) {
3207 /* Promote short ipaddr */
3211 return gen_host(v
, mask
, proto
, dir
);
3216 proto
= IPPROTO_UDP
;
3217 else if (proto
== Q_TCP
)
3218 proto
= IPPROTO_TCP
;
3219 else if (proto
== Q_SCTP
)
3220 proto
= IPPROTO_SCTP
;
3221 else if (proto
== Q_DEFAULT
)
3222 proto
= PROTO_UNDEF
;
3224 bpf_error("illegal qualifier of 'port'");
3227 return gen_port((int)v
, proto
, dir
);
3231 b
= gen_port((int)v
, proto
, dir
);
3232 gen_or(gen_port6((int)v
, proto
, dir
), b
);
3238 bpf_error("'gateway' requires a name");
3242 return gen_proto((int)v
, proto
, dir
);
3245 return gen_protochain((int)v
, proto
, dir
);
3260 gen_mcode6(s1
, s2
, masklen
, q
)
3261 register const char *s1
, *s2
;
3262 register int masklen
;
3265 struct addrinfo
*res
;
3266 struct in6_addr
*addr
;
3267 struct in6_addr mask
;
3272 bpf_error("no mask %s supported", s2
);
3274 res
= pcap_nametoaddrinfo(s1
);
3276 bpf_error("invalid ip6 address %s", s1
);
3278 bpf_error("%s resolved to multiple address", s1
);
3279 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3281 if (sizeof(mask
) * 8 < masklen
)
3282 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3283 memset(&mask
, 0, sizeof(mask
));
3284 memset(&mask
, 0xff, masklen
/ 8);
3286 mask
.s6_addr
[masklen
/ 8] =
3287 (0xff << (8 - masklen
% 8)) & 0xff;
3290 a
= (u_int32_t
*)addr
;
3291 m
= (u_int32_t
*)&mask
;
3292 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3293 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3294 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
3302 bpf_error("Mask syntax for networks only");
3306 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
3311 bpf_error("invalid qualifier against IPv6 address");
3319 register const u_char
*eaddr
;
3322 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3323 if (linktype
== DLT_EN10MB
)
3324 return gen_ehostop(eaddr
, (int)q
.dir
);
3325 if (linktype
== DLT_FDDI
)
3326 return gen_fhostop(eaddr
, (int)q
.dir
);
3327 if (linktype
== DLT_IEEE802
)
3328 return gen_thostop(eaddr
, (int)q
.dir
);
3329 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
3331 bpf_error("ethernet address used in non-ether expression");
3337 struct slist
*s0
, *s1
;
3340 * This is definitely not the best way to do this, but the
3341 * lists will rarely get long.
3348 static struct slist
*
3354 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3359 static struct slist
*
3365 s
= new_stmt(BPF_LD
|BPF_MEM
);
3371 gen_load(proto
, index
, size
)
3376 struct slist
*s
, *tmp
;
3378 int regno
= alloc_reg();
3380 free_reg(index
->regno
);
3384 bpf_error("data size must be 1, 2, or 4");
3400 bpf_error("unsupported index operation");
3403 s
= xfer_to_x(index
);
3404 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3406 sappend(index
->s
, s
);
3421 /* XXX Note that we assume a fixed link header here. */
3422 s
= xfer_to_x(index
);
3423 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3426 sappend(index
->s
, s
);
3428 b
= gen_proto_abbrev(proto
);
3430 gen_and(index
->b
, b
);
3442 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3444 sappend(s
, xfer_to_a(index
));
3445 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3446 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3447 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3449 sappend(index
->s
, s
);
3451 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
3453 gen_and(index
->b
, b
);
3455 gen_and(gen_proto_abbrev(Q_IP
), b
);
3461 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
3465 index
->regno
= regno
;
3466 s
= new_stmt(BPF_ST
);
3468 sappend(index
->s
, s
);
3474 gen_relation(code
, a0
, a1
, reversed
)
3476 struct arth
*a0
, *a1
;
3479 struct slist
*s0
, *s1
, *s2
;
3480 struct block
*b
, *tmp
;
3484 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
3485 b
= new_block(JMP(code
));
3486 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
3487 reversed
= !reversed
;
3488 b
->s
.k
= 0x80000000;
3496 sappend(a0
->s
, a1
->s
);
3500 free_reg(a0
->regno
);
3501 free_reg(a1
->regno
);
3503 /* 'and' together protocol checks */
3506 gen_and(a0
->b
, tmp
= a1
->b
);
3522 int regno
= alloc_reg();
3523 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
3526 s
= new_stmt(BPF_LD
|BPF_LEN
);
3527 s
->next
= new_stmt(BPF_ST
);
3528 s
->next
->s
.k
= regno
;
3543 a
= (struct arth
*)newchunk(sizeof(*a
));
3547 s
= new_stmt(BPF_LD
|BPF_IMM
);
3549 s
->next
= new_stmt(BPF_ST
);
3565 s
= new_stmt(BPF_ALU
|BPF_NEG
);
3568 s
= new_stmt(BPF_ST
);
3576 gen_arth(code
, a0
, a1
)
3578 struct arth
*a0
, *a1
;
3580 struct slist
*s0
, *s1
, *s2
;
3584 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
3589 sappend(a0
->s
, a1
->s
);
3591 free_reg(a1
->regno
);
3593 s0
= new_stmt(BPF_ST
);
3594 a0
->regno
= s0
->s
.k
= alloc_reg();
3601 * Here we handle simple allocation of the scratch registers.
3602 * If too many registers are alloc'd, the allocator punts.
3604 static int regused
[BPF_MEMWORDS
];
3608 * Return the next free register.
3613 int n
= BPF_MEMWORDS
;
3616 if (regused
[curreg
])
3617 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
3619 regused
[curreg
] = 1;
3623 bpf_error("too many registers needed to evaluate expression");
3628 * Return a register to the table so it can
3638 static struct block
*
3645 s
= new_stmt(BPF_LD
|BPF_LEN
);
3646 b
= new_block(JMP(jmp
));
3657 return gen_len(BPF_JGE
, n
);
3661 * Actually, this is less than or equal.
3669 b
= gen_len(BPF_JGT
, n
);
3676 gen_byteop(op
, idx
, val
)
3687 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3690 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3691 b
->s
.code
= JMP(BPF_JGE
);
3696 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3697 b
->s
.code
= JMP(BPF_JGT
);
3701 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3705 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3709 b
= new_block(JMP(BPF_JEQ
));
3716 static u_char abroadcast
[] = { 0x0 };
3719 gen_broadcast(proto
)
3722 bpf_u_int32 hostmask
;
3723 struct block
*b0
, *b1
, *b2
;
3724 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3730 if (linktype
== DLT_ARCNET
)
3731 return gen_ahostop(abroadcast
, Q_DST
);
3732 if (linktype
== DLT_EN10MB
)
3733 return gen_ehostop(ebroadcast
, Q_DST
);
3734 if (linktype
== DLT_FDDI
)
3735 return gen_fhostop(ebroadcast
, Q_DST
);
3736 if (linktype
== DLT_IEEE802
)
3737 return gen_thostop(ebroadcast
, Q_DST
);
3738 bpf_error("not a broadcast link");
3742 b0
= gen_linktype(ETHERTYPE_IP
);
3743 hostmask
= ~netmask
;
3744 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3745 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3746 (bpf_int32
)(~0 & hostmask
), hostmask
);
3751 bpf_error("only ether/ip broadcast filters supported");
3755 gen_multicast(proto
)
3758 register struct block
*b0
, *b1
;
3759 register struct slist
*s
;
3765 if (linktype
== DLT_ARCNET
)
3766 /* all ARCnet multicasts use the same address */
3767 return gen_ahostop(abroadcast
, Q_DST
);
3769 if (linktype
== DLT_EN10MB
) {
3770 /* ether[0] & 1 != 0 */
3771 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3773 b0
= new_block(JMP(BPF_JSET
));
3779 if (linktype
== DLT_FDDI
) {
3780 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3781 /* fddi[1] & 1 != 0 */
3782 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3784 b0
= new_block(JMP(BPF_JSET
));
3790 /* TODO - check how token ring handles multicast */
3791 /* if (linktype == DLT_IEEE802) ... */
3793 /* Link not known to support multicasts */
3797 b0
= gen_linktype(ETHERTYPE_IP
);
3798 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3799 b1
->s
.code
= JMP(BPF_JGE
);
3805 b0
= gen_linktype(ETHERTYPE_IPV6
);
3806 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3811 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3815 * generate command for inbound/outbound. It's here so we can
3816 * make it link-type specific. 'dir' = 0 implies "inbound",
3817 * = 1 implies "outbound".
3823 register struct block
*b0
;
3826 * Only some data link types support inbound/outbound qualifiers.
3831 b0
= gen_relation(BPF_JEQ
,
3832 gen_load(Q_LINK
, gen_loadi(0), 1),
3838 bpf_error("inbound/outbound not supported on linktype %d\n",
3848 register const u_char
*eaddr
;
3851 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3852 if (linktype
== DLT_ARCNET
)
3853 return gen_ahostop(eaddr
, (int)q
.dir
);
3855 bpf_error("ARCnet address used in non-arc expression");
3859 static struct block
*
3860 gen_ahostop(eaddr
, dir
)
3861 register const u_char
*eaddr
;
3864 register struct block
*b0
, *b1
;
3867 /* src comes first, different from Ethernet */
3869 return gen_bcmp(0, 1, eaddr
);
3872 return gen_bcmp(1, 1, eaddr
);
3875 b0
= gen_ahostop(eaddr
, Q_SRC
);
3876 b1
= gen_ahostop(eaddr
, Q_DST
);
3882 b0
= gen_ahostop(eaddr
, Q_SRC
);
3883 b1
= gen_ahostop(eaddr
, Q_DST
);
3892 * support IEEE 802.1Q VLAN trunk over ethernet
3901 * Change the offsets to point to the type and data fields within
3902 * the VLAN packet. This is somewhat of a kludge.
3904 if (orig_nl
== (u_int
)-1) {
3905 orig_linktype
= off_linktype
; /* save original values */
3916 bpf_error("no VLAN support for data link type %d",
3922 /* check for VLAN */
3923 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3925 /* If a specific VLAN is requested, check VLAN id */
3926 if (vlan_num
>= 0) {
3929 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);