]>
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.161 2001-12-18 09:10:49 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
;
81 /* Hack for updating VLAN offsets. */
82 static u_int orig_linktype
= -1, orig_nl
= -1;
86 int pcap_fddipad
= PCAP_FDDIPAD
;
93 bpf_error(const char *fmt
, ...)
100 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
107 static void init_linktype(int);
109 static int alloc_reg(void);
110 static void free_reg(int);
112 static struct block
*root
;
115 * We divy out chunks of memory rather than call malloc each time so
116 * we don't have to worry about leaking memory. It's probably
117 * not a big deal if all this memory was wasted but it this ever
118 * goes into a library that would probably not be a good idea.
121 #define CHUNK0SIZE 1024
127 static struct chunk chunks
[NCHUNKS
];
128 static int cur_chunk
;
130 static void *newchunk(u_int
);
131 static void freechunks(void);
132 static inline struct block
*new_block(int);
133 static inline struct slist
*new_stmt(int);
134 static struct block
*gen_retblk(int);
135 static inline void syntax(void);
137 static void backpatch(struct block
*, struct block
*);
138 static void merge(struct block
*, struct block
*);
139 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
140 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
141 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
142 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
143 static struct block
*gen_uncond(int);
144 static inline struct block
*gen_true(void);
145 static inline struct block
*gen_false(void);
146 static struct block
*gen_linktype(int);
147 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
148 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
150 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
152 static struct block
*gen_ahostop(const u_char
*, int);
153 static struct block
*gen_ehostop(const u_char
*, int);
154 static struct block
*gen_fhostop(const u_char
*, int);
155 static struct block
*gen_thostop(const u_char
*, int);
156 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
157 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
159 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
162 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
164 static struct block
*gen_ipfrag(void);
165 static struct block
*gen_portatom(int, bpf_int32
);
167 static struct block
*gen_portatom6(int, bpf_int32
);
169 struct block
*gen_portop(int, int, int);
170 static struct block
*gen_port(int, int, int);
172 struct block
*gen_portop6(int, int, int);
173 static struct block
*gen_port6(int, int, int);
175 static int lookup_proto(const char *, int);
176 static struct block
*gen_protochain(int, int, int);
177 static struct block
*gen_proto(int, int, int);
178 static struct slist
*xfer_to_x(struct arth
*);
179 static struct slist
*xfer_to_a(struct arth
*);
180 static struct block
*gen_len(int, int);
190 /* XXX Round up to nearest long. */
191 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
193 /* XXX Round up to structure boundary. */
197 cp
= &chunks
[cur_chunk
];
198 if (n
> cp
->n_left
) {
199 ++cp
, k
= ++cur_chunk
;
201 bpf_error("out of memory");
202 size
= CHUNK0SIZE
<< k
;
203 cp
->m
= (void *)malloc(size
);
204 memset((char *)cp
->m
, 0, size
);
207 bpf_error("out of memory");
210 return (void *)((char *)cp
->m
+ cp
->n_left
);
219 for (i
= 0; i
< NCHUNKS
; ++i
)
220 if (chunks
[i
].m
!= NULL
) {
227 * A strdup whose allocations are freed after code generation is over.
231 register const char *s
;
233 int n
= strlen(s
) + 1;
234 char *cp
= newchunk(n
);
240 static inline struct block
*
246 p
= (struct block
*)newchunk(sizeof(*p
));
253 static inline struct slist
*
259 p
= (struct slist
*)newchunk(sizeof(*p
));
265 static struct block
*
269 struct block
*b
= new_block(BPF_RET
|BPF_K
);
278 bpf_error("syntax error in filter expression");
281 static bpf_u_int32 netmask
;
286 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
287 char *buf
, int optimize
, bpf_u_int32 mask
)
296 if (setjmp(top_ctx
)) {
304 snaplen
= pcap_snapshot(p
);
306 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
307 "snaplen of 0 rejects all packets");
311 lex_init(buf
? buf
: "");
312 init_linktype(pcap_datalink(p
));
319 root
= gen_retblk(snaplen
);
321 if (optimize
&& !no_optimize
) {
324 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
325 bpf_error("expression rejects all packets");
327 program
->bf_insns
= icode_to_fcode(root
, &len
);
328 program
->bf_len
= len
;
336 * entry point for using the compiler with no pcap open
337 * pass in all the stuff that is needed explicitly instead.
340 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
341 struct bpf_program
*program
,
342 char *buf
, int optimize
, bpf_u_int32 mask
)
347 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
350 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
356 * Clean up a "struct bpf_program" by freeing all the memory allocated
360 pcap_freecode(struct bpf_program
*program
)
363 if (program
->bf_insns
!= NULL
) {
364 free((char *)program
->bf_insns
);
365 program
->bf_insns
= NULL
;
370 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
371 * which of the jt and jf fields has been resolved and which is a pointer
372 * back to another unresolved block (or nil). At least one of the fields
373 * in each block is already resolved.
376 backpatch(list
, target
)
377 struct block
*list
, *target
;
394 * Merge the lists in b0 and b1, using the 'sense' field to indicate
395 * which of jt and jf is the link.
399 struct block
*b0
, *b1
;
401 register struct block
**p
= &b0
;
403 /* Find end of list. */
405 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
407 /* Concatenate the lists. */
415 backpatch(p
, gen_retblk(snaplen
));
416 p
->sense
= !p
->sense
;
417 backpatch(p
, gen_retblk(0));
423 struct block
*b0
, *b1
;
425 backpatch(b0
, b1
->head
);
426 b0
->sense
= !b0
->sense
;
427 b1
->sense
= !b1
->sense
;
429 b1
->sense
= !b1
->sense
;
435 struct block
*b0
, *b1
;
437 b0
->sense
= !b0
->sense
;
438 backpatch(b0
, b1
->head
);
439 b0
->sense
= !b0
->sense
;
448 b
->sense
= !b
->sense
;
451 static struct block
*
452 gen_cmp(offset
, size
, v
)
459 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
462 b
= new_block(JMP(BPF_JEQ
));
469 static struct block
*
470 gen_cmp_gt(offset
, size
, v
)
477 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
480 b
= new_block(JMP(BPF_JGT
));
487 static struct block
*
488 gen_mcmp(offset
, size
, v
, mask
)
493 struct block
*b
= gen_cmp(offset
, size
, v
);
496 if (mask
!= 0xffffffff) {
497 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
504 static struct block
*
505 gen_bcmp(offset
, size
, v
)
506 register u_int offset
, size
;
507 register const u_char
*v
;
509 register struct block
*b
, *tmp
;
513 register const u_char
*p
= &v
[size
- 4];
514 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
515 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
517 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
524 register const u_char
*p
= &v
[size
- 2];
525 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
527 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
534 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
543 * Various code constructs need to know the layout of the data link
544 * layer. These variables give the necessary offsets. off_linktype
545 * is set to -1 for no encapsulation, in which case, IP is assumed.
547 static u_int off_linktype
;
564 off_nl
= 6; /* XXX in reality, variable! */
574 * SLIP doesn't have a link level type. The 16 byte
575 * header is hacked into our SLIP driver.
582 /* XXX this may be the same as the DLT_PPP_BSDOS case */
595 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
596 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
603 * This does no include the Ethernet header, and
604 * only covers session state.
617 * FDDI doesn't really have a link-level type field.
618 * We set "off_linktype" to the offset of the LLC header.
620 * To check for Ethernet types, we assume that SSAP = SNAP
621 * is being used and pick out the encapsulated Ethernet type.
622 * XXX - should we generate code to check for SNAP?
626 off_linktype
+= pcap_fddipad
;
630 off_nl
+= pcap_fddipad
;
636 * Token Ring doesn't really have a link-level type field.
637 * We set "off_linktype" to the offset of the LLC header.
639 * To check for Ethernet types, we assume that SSAP = SNAP
640 * is being used and pick out the encapsulated Ethernet type.
641 * XXX - should we generate code to check for SNAP?
643 * XXX - the header is actually variable-length.
644 * Some various Linux patched versions gave 38
645 * as "off_linktype" and 40 as "off_nl"; however,
646 * if a token ring packet has *no* routing
647 * information, i.e. is not source-routed, the correct
648 * values are 20 and 22, as they are in the vanilla code.
650 * A packet is source-routed iff the uppermost bit
651 * of the first byte of the source address, at an
652 * offset of 8, has the uppermost bit set. If the
653 * packet is source-routed, the total number of bytes
654 * of routing information is 2 plus bits 0x1F00 of
655 * the 16-bit value at an offset of 14 (shifted right
656 * 8 - figure out which byte that is).
664 * 802.11 doesn't really have a link-level type field.
665 * We set "off_linktype" to the offset of the LLC header.
667 * To check for Ethernet types, we assume that SSAP = SNAP
668 * is being used and pick out the encapsulated Ethernet type.
669 * XXX - should we generate code to check for SNAP?
671 * XXX - the header is actually variable-length. We
672 * assume a 24-byte link-layer header, as appears in
673 * data frames in networks with no bridges.
679 case DLT_PRISM_HEADER
:
681 * Same as 802.11, but with an additional header before
682 * the 802.11 header, containing a bunch of additional
683 * information including radio-level information.
685 * The header is 144 bytes long.
687 * XXX - same variable-length header problem; at least
688 * the Prism header is fixed-length.
690 off_linktype
= 144+24;
694 case DLT_ATM_RFC1483
:
696 * assume routed, non-ISO PDUs
697 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
708 case DLT_ATM_CLIP
: /* Linux ATM defines this */
713 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
720 * LocalTalk does have a 1-byte type field in the LLAP header,
721 * but really it just indicates whether there is a "short" or
722 * "long" DDP packet following.
728 bpf_error("unknown data link type %d", linktype
);
732 static struct block
*
739 s
= new_stmt(BPF_LD
|BPF_IMM
);
741 b
= new_block(JMP(BPF_JEQ
));
747 static inline struct block
*
750 return gen_uncond(1);
753 static inline struct block
*
756 return gen_uncond(0);
760 * Byte-swap a 32-bit number.
761 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
762 * big-endian platforms.)
764 #define SWAPLONG(y) \
765 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
767 static struct block
*
771 struct block
*b0
, *b1
;
780 * OSI protocols always use 802.2 encapsulation.
781 * XXX - should we check both the DSAP and the
782 * SSAP, like this, or should we check just the
785 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
787 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
788 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
794 * NetBEUI always uses 802.2 encapsulation.
795 * XXX - should we check both the DSAP and the
796 * SSAP, like this, or should we check just the
799 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
801 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
802 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
810 * Ethernet_II frames, which are Ethernet
811 * frames with a frame type of ETHERTYPE_IPX;
813 * Ethernet_802.3 frames, which are 802.3
814 * frames (i.e., the type/length field is
815 * a length field, <= ETHERMTU, rather than
816 * a type field) with the first two bytes
817 * after the Ethernet/802.3 header being
820 * Ethernet_802.2 frames, which are 802.3
821 * frames with an 802.2 LLC header and
822 * with the IPX LSAP as the DSAP in the LLC
825 * Ethernet_SNAP frames, which are 802.3
826 * frames with an LLC header and a SNAP
827 * header and with an OUI of 0x000000
828 * (encapsulated Ethernet) and a protocol
829 * ID of ETHERTYPE_IPX in the SNAP header.
831 * XXX - should we generate the same code both
832 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
836 * This generates code to check both for the
837 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
839 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
840 (bpf_int32
)LLCSAP_IPX
);
841 b1
= gen_cmp(off_linktype
+ 2, BPF_H
,
846 * Now we add code to check for SNAP frames with
847 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
849 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
853 * Now we generate code to check for 802.3
856 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
860 * Now add the check for 802.3 frames before the
861 * check for Ethernet_802.2 and Ethernet_802.3,
862 * as those checks should only be done on 802.3
863 * frames, not on Ethernet frames.
868 * Now add the check for Ethernet_II frames, and
869 * do that before checking for the other frame
872 b0
= gen_cmp(off_linktype
, BPF_H
,
873 (bpf_int32
)ETHERTYPE_IPX
);
877 case ETHERTYPE_ATALK
:
880 * EtherTalk (AppleTalk protocols on Ethernet link
881 * layer) may use 802.2 encapsulation.
885 * Check for 802.2 encapsulation (EtherTalk phase 2?);
886 * we check for an Ethernet type field less than
887 * 1500, which means it's an 802.3 length field.
889 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
893 * 802.2-encapsulated ETHERTYPE_ATALK packets are
894 * SNAP packets with an organization code of
895 * 0x080007 (Apple, for Appletalk) and a protocol
896 * type of ETHERTYPE_ATALK (Appletalk).
898 * 802.2-encapsulated ETHERTYPE_AARP packets are
899 * SNAP packets with an organization code of
900 * 0x000000 (encapsulated Ethernet) and a protocol
901 * type of ETHERTYPE_AARP (Appletalk ARP).
903 if (proto
== ETHERTYPE_ATALK
)
904 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
905 else /* proto == ETHERTYPE_AARP */
906 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
910 * Check for Ethernet encapsulation (Ethertalk
911 * phase 1?); we just check for the Ethernet
914 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
920 if (proto
<= ETHERMTU
) {
922 * This is an LLC SAP value, so the frames
923 * that match would be 802.2 frames.
924 * Check that the frame is an 802.2 frame
925 * (i.e., that the length/type field is
926 * a length field, <= ETHERMTU) and
927 * then check the DSAP.
929 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
931 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
937 * This is an Ethernet type, so compare
938 * the length/type field with it (if
939 * the frame is an 802.2 frame, the length
940 * field will be <= ETHERMTU, and, as
941 * "proto" is > ETHERMTU, this test
942 * will fail and the frame won't match,
943 * which is what we want).
945 return gen_cmp(off_linktype
, BPF_H
,
952 case DLT_PRISM_HEADER
:
955 case DLT_ATM_RFC1483
:
958 * XXX - handle token-ring variable-length header.
963 return gen_cmp(off_linktype
, BPF_H
, (long)
964 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
967 return gen_cmp(off_linktype
, BPF_H
, (long)
968 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
972 * XXX - are there ever SNAP frames for IPX on
973 * non-Ethernet 802.x networks?
975 return gen_cmp(off_linktype
, BPF_B
,
976 (bpf_int32
)LLCSAP_IPX
);
978 case ETHERTYPE_ATALK
:
980 * 802.2-encapsulated ETHERTYPE_ATALK packets are
981 * SNAP packets with an organization code of
982 * 0x080007 (Apple, for Appletalk) and a protocol
983 * type of ETHERTYPE_ATALK (Appletalk).
985 * XXX - check for an organization code of
986 * encapsulated Ethernet as well?
988 return gen_snap(0x080007, ETHERTYPE_ATALK
,
994 * XXX - we don't have to check for IPX 802.3
995 * here, but should we check for the IPX Ethertype?
997 if (proto
<= ETHERMTU
) {
999 * This is an LLC SAP value, so check
1002 return gen_cmp(off_linktype
, BPF_B
,
1006 * This is an Ethernet type; we assume
1007 * that it's unlikely that it'll
1008 * appear in the right place at random,
1009 * and therefore check only the
1010 * location that would hold the Ethernet
1011 * type in a SNAP frame with an organization
1012 * code of 0x000000 (encapsulated Ethernet).
1014 * XXX - if we were to check for the SNAP DSAP
1015 * and LSAP, as per XXX, and were also to check
1016 * for an organization code of 0x000000
1017 * (encapsulated Ethernet), we'd do
1019 * return gen_snap(0x000000, proto,
1022 * here; for now, we don't, as per the above.
1023 * I don't know whether it's worth the
1024 * extra CPU time to do the right check
1027 return gen_cmp(off_linktype
+6, BPF_H
,
1038 * OSI protocols always use 802.2 encapsulation.
1039 * XXX - should we check both the DSAP and the
1040 * LSAP, like this, or should we check just the
1043 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1044 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1045 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1049 case LLCSAP_NETBEUI
:
1051 * NetBEUI always uses 802.2 encapsulation.
1052 * XXX - should we check both the DSAP and the
1053 * LSAP, like this, or should we check just the
1056 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1057 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1058 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1064 * Ethernet_II frames, which are Ethernet
1065 * frames with a frame type of ETHERTYPE_IPX;
1067 * Ethernet_802.3 frames, which have a frame
1068 * type of LINUX_SLL_P_802_3;
1070 * Ethernet_802.2 frames, which are 802.3
1071 * frames with an 802.2 LLC header (i.e, have
1072 * a frame type of LINUX_SLL_P_802_2) and
1073 * with the IPX LSAP as the DSAP in the LLC
1076 * Ethernet_SNAP frames, which are 802.3
1077 * frames with an LLC header and a SNAP
1078 * header and with an OUI of 0x000000
1079 * (encapsulated Ethernet) and a protocol
1080 * ID of ETHERTYPE_IPX in the SNAP header.
1082 * First, do the checks on LINUX_SLL_P_802_2
1083 * frames; generate the check for either
1084 * Ethernet_802.2 or Ethernet_SNAP frames, and
1085 * then put a check for LINUX_SLL_P_802_2 frames
1088 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1089 (bpf_int32
)LLCSAP_IPX
);
1090 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1093 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1097 * Now check for 802.3 frames and OR that with
1098 * the previous test.
1100 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1104 * Now add the check for Ethernet_II frames, and
1105 * do that before checking for the other frame
1108 b0
= gen_cmp(off_linktype
, BPF_H
,
1109 (bpf_int32
)ETHERTYPE_IPX
);
1113 case ETHERTYPE_ATALK
:
1114 case ETHERTYPE_AARP
:
1116 * EtherTalk (AppleTalk protocols on Ethernet link
1117 * layer) may use 802.2 encapsulation.
1121 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1122 * we check for the 802.2 protocol type in the
1123 * "Ethernet type" field.
1125 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1128 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1129 * SNAP packets with an organization code of
1130 * 0x080007 (Apple, for Appletalk) and a protocol
1131 * type of ETHERTYPE_ATALK (Appletalk).
1133 * 802.2-encapsulated ETHERTYPE_AARP packets are
1134 * SNAP packets with an organization code of
1135 * 0x000000 (encapsulated Ethernet) and a protocol
1136 * type of ETHERTYPE_AARP (Appletalk ARP).
1138 if (proto
== ETHERTYPE_ATALK
)
1139 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1141 else /* proto == ETHERTYPE_AARP */
1142 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1147 * Check for Ethernet encapsulation (Ethertalk
1148 * phase 1?); we just check for the Ethernet
1151 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1157 if (proto
<= ETHERMTU
) {
1159 * This is an LLC SAP value, so the frames
1160 * that match would be 802.2 frames.
1161 * Check for the 802.2 protocol type
1162 * in the "Ethernet type" field, and
1163 * then check the DSAP.
1165 b0
= gen_cmp(off_linktype
, BPF_H
,
1167 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1173 * This is an Ethernet type, so compare
1174 * the length/type field with it (if
1175 * the frame is an 802.2 frame, the length
1176 * field will be <= ETHERMTU, and, as
1177 * "proto" is > ETHERMTU, this test
1178 * will fail and the frame won't match,
1179 * which is what we want).
1181 return gen_cmp(off_linktype
, BPF_H
,
1188 case DLT_SLIP_BSDOS
:
1191 * These types don't provide any type field; packets
1194 * XXX - for IPv4, check for a version number of 4, and,
1195 * for IPv6, check for a version number of 6?
1201 case ETHERTYPE_IPV6
:
1203 return gen_true(); /* always true */
1206 return gen_false(); /* always false */
1211 case DLT_PPP_SERIAL
:
1214 * We use Ethernet protocol types inside libpcap;
1215 * map them to the corresponding PPP protocol types.
1220 proto
= PPP_IP
; /* XXX was 0x21 */
1224 case ETHERTYPE_IPV6
:
1233 case ETHERTYPE_ATALK
:
1247 * I'm assuming the "Bridging PDU"s that go
1248 * over PPP are Spanning Tree Protocol
1262 * We use Ethernet protocol types inside libpcap;
1263 * map them to the corresponding PPP protocol types.
1268 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1269 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1271 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1276 case ETHERTYPE_IPV6
:
1286 case ETHERTYPE_ATALK
:
1300 * I'm assuming the "Bridging PDU"s that go
1301 * over PPP are Spanning Tree Protocol
1316 * For DLT_NULL, the link-layer header is a 32-bit
1317 * word containing an AF_ value in *host* byte order.
1319 * In addition, if we're reading a saved capture file,
1320 * the host byte order in the capture may not be the
1321 * same as the host byte order on this machine.
1323 * For DLT_LOOP, the link-layer header is a 32-bit
1324 * word containing an AF_ value in *network* byte order.
1326 * XXX - AF_ values may, unfortunately, be platform-
1327 * dependent; for example, FreeBSD's AF_INET6 is 24
1328 * whilst NetBSD's and OpenBSD's is 26.
1330 * This means that, when reading a capture file, just
1331 * checking for our AF_INET6 value won't work if the
1332 * capture file came from another OS.
1341 case ETHERTYPE_IPV6
:
1348 * Not a type on which we support filtering.
1349 * XXX - support those that have AF_ values
1350 * #defined on this platform, at least?
1355 if (linktype
== DLT_NULL
) {
1357 * The AF_ value is in host byte order, but
1358 * the BPF interpreter will convert it to
1359 * network byte order.
1361 * If this is a save file, and it's from a
1362 * machine with the opposite byte order to
1363 * ours, we byte-swap the AF_ value.
1365 * Then we run it through "htonl()", and
1366 * generate code to compare against the result.
1368 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1369 bpf_pcap
->sf
.swapped
)
1370 proto
= SWAPLONG(proto
);
1371 proto
= htonl(proto
);
1373 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1377 * XXX should we check for first fragment if the protocol
1384 case ETHERTYPE_IPV6
:
1385 return(gen_cmp(2, BPF_B
,
1386 (bpf_int32
)htonl(ARCTYPE_INET6
)));
1389 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_IP
));
1390 b1
= gen_cmp(2, BPF_B
,
1391 (bpf_int32
)htonl(ARCTYPE_IP_OLD
));
1395 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_ARP
));
1396 b1
= gen_cmp(2, BPF_B
,
1397 (bpf_int32
)htonl(ARCTYPE_ARP_OLD
));
1400 case ETHERTYPE_REVARP
:
1401 return(gen_cmp(2, BPF_B
,
1402 (bpf_int32
)htonl(ARCTYPE_REVARP
)));
1403 case ETHERTYPE_ATALK
:
1404 return(gen_cmp(2, BPF_B
,
1405 (bpf_int32
)htonl(ARCTYPE_ATALK
)));
1411 case ETHERTYPE_ATALK
:
1420 * All the types that have no encapsulation should either be
1421 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1422 * all packets are IP packets, or should be handled in some
1423 * special case, if none of them are (if some are and some
1424 * aren't, the lack of encapsulation is a problem, as we'd
1425 * have to find some other way of determining the packet type).
1427 * Therefore, if "off_linktype" is -1, there's an error.
1429 if (off_linktype
== -1)
1433 * Any type not handled above should always have an Ethernet
1434 * type at an offset of "off_linktype". (PPP is partially
1435 * handled above - the protocol type is mapped from the
1436 * Ethernet and LLC types we use internally to the corresponding
1437 * PPP type - but the PPP type is always specified by a value
1438 * at "off_linktype", so we don't have to do the code generation
1441 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1445 * Check for an LLC SNAP packet with a given organization code and
1446 * protocol type; we check the entire contents of the 802.2 LLC and
1447 * snap headers, checking for DSAP and SSAP of SNAP and a control
1448 * field of 0x03 in the LLC header, and for the specified organization
1449 * code and protocol type in the SNAP header.
1451 static struct block
*
1452 gen_snap(orgcode
, ptype
, offset
)
1453 bpf_u_int32 orgcode
;
1457 u_char snapblock
[8];
1459 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1460 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1461 snapblock
[2] = 0x03; /* control = UI */
1462 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1463 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1464 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1465 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1466 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1467 return gen_bcmp(offset
, 8, snapblock
);
1470 static struct block
*
1471 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1475 u_int src_off
, dst_off
;
1477 struct block
*b0
, *b1
;
1491 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1492 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1498 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1499 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1506 b0
= gen_linktype(proto
);
1507 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1513 static struct block
*
1514 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1515 struct in6_addr
*addr
;
1516 struct in6_addr
*mask
;
1518 u_int src_off
, dst_off
;
1520 struct block
*b0
, *b1
;
1535 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1536 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1542 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1543 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1550 /* this order is important */
1551 a
= (u_int32_t
*)addr
;
1552 m
= (u_int32_t
*)mask
;
1553 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1554 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1556 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1558 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1560 b0
= gen_linktype(proto
);
1566 static struct block
*
1567 gen_ehostop(eaddr
, dir
)
1568 register const u_char
*eaddr
;
1571 register struct block
*b0
, *b1
;
1575 return gen_bcmp(6, 6, eaddr
);
1578 return gen_bcmp(0, 6, eaddr
);
1581 b0
= gen_ehostop(eaddr
, Q_SRC
);
1582 b1
= gen_ehostop(eaddr
, Q_DST
);
1588 b0
= gen_ehostop(eaddr
, Q_SRC
);
1589 b1
= gen_ehostop(eaddr
, Q_DST
);
1598 * Like gen_ehostop, but for DLT_FDDI
1600 static struct block
*
1601 gen_fhostop(eaddr
, dir
)
1602 register const u_char
*eaddr
;
1605 struct block
*b0
, *b1
;
1610 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1612 return gen_bcmp(6 + 1, 6, eaddr
);
1617 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1619 return gen_bcmp(0 + 1, 6, eaddr
);
1623 b0
= gen_fhostop(eaddr
, Q_SRC
);
1624 b1
= gen_fhostop(eaddr
, Q_DST
);
1630 b0
= gen_fhostop(eaddr
, Q_SRC
);
1631 b1
= gen_fhostop(eaddr
, Q_DST
);
1640 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1642 static struct block
*
1643 gen_thostop(eaddr
, dir
)
1644 register const u_char
*eaddr
;
1647 register struct block
*b0
, *b1
;
1651 return gen_bcmp(8, 6, eaddr
);
1654 return gen_bcmp(2, 6, eaddr
);
1657 b0
= gen_thostop(eaddr
, Q_SRC
);
1658 b1
= gen_thostop(eaddr
, Q_DST
);
1664 b0
= gen_thostop(eaddr
, Q_SRC
);
1665 b1
= gen_thostop(eaddr
, Q_DST
);
1674 * This is quite tricky because there may be pad bytes in front of the
1675 * DECNET header, and then there are two possible data packet formats that
1676 * carry both src and dst addresses, plus 5 packet types in a format that
1677 * carries only the src node, plus 2 types that use a different format and
1678 * also carry just the src node.
1682 * Instead of doing those all right, we just look for data packets with
1683 * 0 or 1 bytes of padding. If you want to look at other packets, that
1684 * will require a lot more hacking.
1686 * To add support for filtering on DECNET "areas" (network numbers)
1687 * one would want to add a "mask" argument to this routine. That would
1688 * make the filter even more inefficient, although one could be clever
1689 * and not generate masking instructions if the mask is 0xFFFF.
1691 static struct block
*
1692 gen_dnhostop(addr
, dir
, base_off
)
1697 struct block
*b0
, *b1
, *b2
, *tmp
;
1698 u_int offset_lh
; /* offset if long header is received */
1699 u_int offset_sh
; /* offset if short header is received */
1704 offset_sh
= 1; /* follows flags */
1705 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1709 offset_sh
= 3; /* follows flags, dstnode */
1710 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1714 /* Inefficient because we do our Calvinball dance twice */
1715 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1716 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1722 /* Inefficient because we do our Calvinball dance twice */
1723 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1724 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1729 bpf_error("ISO host filtering not implemented");
1734 b0
= gen_linktype(ETHERTYPE_DN
);
1735 /* Check for pad = 1, long header case */
1736 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1737 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1738 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1739 BPF_H
, (bpf_int32
)ntohs(addr
));
1741 /* Check for pad = 0, long header case */
1742 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1743 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1746 /* Check for pad = 1, short header case */
1747 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1748 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1749 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1750 BPF_H
, (bpf_int32
)ntohs(addr
));
1753 /* Check for pad = 0, short header case */
1754 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1755 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1759 /* Combine with test for linktype */
1764 static struct block
*
1765 gen_host(addr
, mask
, proto
, dir
)
1771 struct block
*b0
, *b1
;
1776 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1777 if (off_linktype
!= -1) {
1778 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1780 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1786 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1787 off_nl
+ 12, off_nl
+ 16);
1790 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1791 off_nl
+ 14, off_nl
+ 24);
1794 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1795 off_nl
+ 14, off_nl
+ 24);
1798 bpf_error("'tcp' modifier applied to host");
1801 bpf_error("'sctp' modifier applied to host");
1804 bpf_error("'udp' modifier applied to host");
1807 bpf_error("'icmp' modifier applied to host");
1810 bpf_error("'igmp' modifier applied to host");
1813 bpf_error("'igrp' modifier applied to host");
1816 bpf_error("'pim' modifier applied to host");
1819 bpf_error("'vrrp' modifier applied to host");
1822 bpf_error("ATALK host filtering not implemented");
1825 bpf_error("AARP host filtering not implemented");
1828 return gen_dnhostop(addr
, dir
, off_nl
);
1831 bpf_error("SCA host filtering not implemented");
1834 bpf_error("LAT host filtering not implemented");
1837 bpf_error("MOPDL host filtering not implemented");
1840 bpf_error("MOPRC host filtering not implemented");
1844 bpf_error("'ip6' modifier applied to ip host");
1847 bpf_error("'icmp6' modifier applied to host");
1851 bpf_error("'ah' modifier applied to host");
1854 bpf_error("'esp' modifier applied to host");
1857 bpf_error("ISO host filtering not implemented");
1860 bpf_error("'esis' modifier applied to host");
1863 bpf_error("'isis' modifier applied to host");
1866 bpf_error("'clnp' modifier applied to host");
1869 bpf_error("'stp' modifier applied to host");
1872 bpf_error("IPX host filtering not implemented");
1875 bpf_error("'netbeui' modifier applied to host");
1884 static struct block
*
1885 gen_host6(addr
, mask
, proto
, dir
)
1886 struct in6_addr
*addr
;
1887 struct in6_addr
*mask
;
1894 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1897 bpf_error("'ip' modifier applied to ip6 host");
1900 bpf_error("'rarp' modifier applied to ip6 host");
1903 bpf_error("'arp' modifier applied to ip6 host");
1906 bpf_error("'sctp' modifier applied to host");
1909 bpf_error("'tcp' modifier applied to host");
1912 bpf_error("'udp' modifier applied to host");
1915 bpf_error("'icmp' modifier applied to host");
1918 bpf_error("'igmp' modifier applied to host");
1921 bpf_error("'igrp' modifier applied to host");
1924 bpf_error("'pim' modifier applied to host");
1927 bpf_error("'vrrp' modifier applied to host");
1930 bpf_error("ATALK host filtering not implemented");
1933 bpf_error("AARP host filtering not implemented");
1936 bpf_error("'decnet' modifier applied to ip6 host");
1939 bpf_error("SCA host filtering not implemented");
1942 bpf_error("LAT host filtering not implemented");
1945 bpf_error("MOPDL host filtering not implemented");
1948 bpf_error("MOPRC host filtering not implemented");
1951 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1952 off_nl
+ 8, off_nl
+ 24);
1955 bpf_error("'icmp6' modifier applied to host");
1958 bpf_error("'ah' modifier applied to host");
1961 bpf_error("'esp' modifier applied to host");
1964 bpf_error("ISO host filtering not implemented");
1967 bpf_error("'esis' modifier applied to host");
1970 bpf_error("'isis' modifier applied to host");
1973 bpf_error("'clnp' modifier applied to host");
1976 bpf_error("'stp' modifier applied to host");
1979 bpf_error("IPX host filtering not implemented");
1982 bpf_error("'netbeui' modifier applied to host");
1992 static struct block
*
1993 gen_gateway(eaddr
, alist
, proto
, dir
)
1994 const u_char
*eaddr
;
1995 bpf_u_int32
**alist
;
1999 struct block
*b0
, *b1
, *tmp
;
2002 bpf_error("direction applied to 'gateway'");
2009 if (linktype
== DLT_EN10MB
)
2010 b0
= gen_ehostop(eaddr
, Q_OR
);
2011 else if (linktype
== DLT_FDDI
)
2012 b0
= gen_fhostop(eaddr
, Q_OR
);
2013 else if (linktype
== DLT_IEEE802
)
2014 b0
= gen_thostop(eaddr
, Q_OR
);
2017 "'gateway' supported only on ethernet, FDDI or token ring");
2019 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2021 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2029 bpf_error("illegal modifier of 'gateway'");
2035 gen_proto_abbrev(proto
)
2046 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2048 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2054 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2056 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2062 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2064 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2070 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2073 #ifndef IPPROTO_IGMP
2074 #define IPPROTO_IGMP 2
2078 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2081 #ifndef IPPROTO_IGRP
2082 #define IPPROTO_IGRP 9
2085 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2089 #define IPPROTO_PIM 103
2093 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2095 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2100 #ifndef IPPROTO_VRRP
2101 #define IPPROTO_VRRP 112
2105 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2109 b1
= gen_linktype(ETHERTYPE_IP
);
2113 b1
= gen_linktype(ETHERTYPE_ARP
);
2117 b1
= gen_linktype(ETHERTYPE_REVARP
);
2121 bpf_error("link layer applied in wrong context");
2124 b1
= gen_linktype(ETHERTYPE_ATALK
);
2128 b1
= gen_linktype(ETHERTYPE_AARP
);
2132 b1
= gen_linktype(ETHERTYPE_DN
);
2136 b1
= gen_linktype(ETHERTYPE_SCA
);
2140 b1
= gen_linktype(ETHERTYPE_LAT
);
2144 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2148 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2153 b1
= gen_linktype(ETHERTYPE_IPV6
);
2156 #ifndef IPPROTO_ICMPV6
2157 #define IPPROTO_ICMPV6 58
2160 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2165 #define IPPROTO_AH 51
2168 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2170 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2176 #define IPPROTO_ESP 50
2179 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2181 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2187 b1
= gen_linktype(LLCSAP_ISONS
);
2191 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2195 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2199 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2203 b1
= gen_linktype(LLCSAP_8021D
);
2207 b1
= gen_linktype(LLCSAP_IPX
);
2211 b1
= gen_linktype(LLCSAP_NETBEUI
);
2220 static struct block
*
2227 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2228 s
->s
.k
= off_nl
+ 6;
2229 b
= new_block(JMP(BPF_JSET
));
2237 static struct block
*
2238 gen_portatom(off
, v
)
2245 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2248 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2249 s
->next
->s
.k
= off_nl
+ off
;
2251 b
= new_block(JMP(BPF_JEQ
));
2259 static struct block
*
2260 gen_portatom6(off
, v
)
2264 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2269 gen_portop(port
, proto
, dir
)
2270 int port
, proto
, dir
;
2272 struct block
*b0
, *b1
, *tmp
;
2274 /* ip proto 'proto' */
2275 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2281 b1
= gen_portatom(0, (bpf_int32
)port
);
2285 b1
= gen_portatom(2, (bpf_int32
)port
);
2290 tmp
= gen_portatom(0, (bpf_int32
)port
);
2291 b1
= gen_portatom(2, (bpf_int32
)port
);
2296 tmp
= gen_portatom(0, (bpf_int32
)port
);
2297 b1
= gen_portatom(2, (bpf_int32
)port
);
2309 static struct block
*
2310 gen_port(port
, ip_proto
, dir
)
2315 struct block
*b0
, *b1
, *tmp
;
2317 /* ether proto ip */
2318 b0
= gen_linktype(ETHERTYPE_IP
);
2324 b1
= gen_portop(port
, ip_proto
, dir
);
2328 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2329 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2331 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
2344 gen_portop6(port
, proto
, dir
)
2345 int port
, proto
, dir
;
2347 struct block
*b0
, *b1
, *tmp
;
2349 /* ip proto 'proto' */
2350 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2354 b1
= gen_portatom6(0, (bpf_int32
)port
);
2358 b1
= gen_portatom6(2, (bpf_int32
)port
);
2363 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2364 b1
= gen_portatom6(2, (bpf_int32
)port
);
2369 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2370 b1
= gen_portatom6(2, (bpf_int32
)port
);
2382 static struct block
*
2383 gen_port6(port
, ip_proto
, dir
)
2388 struct block
*b0
, *b1
, *tmp
;
2390 /* ether proto ip */
2391 b0
= gen_linktype(ETHERTYPE_IPV6
);
2397 b1
= gen_portop6(port
, ip_proto
, dir
);
2401 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2402 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2404 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
2417 lookup_proto(name
, proto
)
2418 register const char *name
;
2428 v
= pcap_nametoproto(name
);
2429 if (v
== PROTO_UNDEF
)
2430 bpf_error("unknown ip proto '%s'", name
);
2434 /* XXX should look up h/w protocol type based on linktype */
2435 v
= pcap_nametoeproto(name
);
2436 if (v
== PROTO_UNDEF
)
2437 bpf_error("unknown ether proto '%s'", name
);
2441 if (strcmp(name
, "esis") == 0)
2443 else if (strcmp(name
, "isis") == 0)
2445 else if (strcmp(name
, "clnp") == 0)
2448 bpf_error("unknown osi proto '%s'", name
);
2468 static struct block
*
2469 gen_protochain(v
, proto
, dir
)
2474 #ifdef NO_PROTOCHAIN
2475 return gen_proto(v
, proto
, dir
);
2477 struct block
*b0
, *b
;
2478 struct slist
*s
[100];
2479 int fix2
, fix3
, fix4
, fix5
;
2480 int ahcheck
, again
, end
;
2482 int reg2
= alloc_reg();
2484 memset(s
, 0, sizeof(s
));
2485 fix2
= fix3
= fix4
= fix5
= 0;
2492 b0
= gen_protochain(v
, Q_IP
, dir
);
2493 b
= gen_protochain(v
, Q_IPV6
, dir
);
2497 bpf_error("bad protocol applied for 'protochain'");
2501 no_optimize
= 1; /*this code is not compatible with optimzer yet */
2504 * s[0] is a dummy entry to protect other BPF insn from damaged
2505 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
2506 * hard to find interdependency made by jump table fixup.
2509 s
[i
] = new_stmt(0); /*dummy*/
2514 b0
= gen_linktype(ETHERTYPE_IP
);
2517 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2518 s
[i
]->s
.k
= off_nl
+ 9;
2520 /* X = ip->ip_hl << 2 */
2521 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2527 b0
= gen_linktype(ETHERTYPE_IPV6
);
2529 /* A = ip6->ip_nxt */
2530 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2531 s
[i
]->s
.k
= off_nl
+ 6;
2533 /* X = sizeof(struct ip6_hdr) */
2534 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
2540 bpf_error("unsupported proto to gen_protochain");
2544 /* again: if (A == v) goto end; else fall through; */
2546 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2548 s
[i
]->s
.jt
= NULL
; /*later*/
2549 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2553 #ifndef IPPROTO_NONE
2554 #define IPPROTO_NONE 59
2556 /* if (A == IPPROTO_NONE) goto end */
2557 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2558 s
[i
]->s
.jt
= NULL
; /*later*/
2559 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2560 s
[i
]->s
.k
= IPPROTO_NONE
;
2561 s
[fix5
]->s
.jf
= s
[i
];
2566 if (proto
== Q_IPV6
) {
2567 int v6start
, v6end
, v6advance
, j
;
2570 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
2571 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2572 s
[i
]->s
.jt
= NULL
; /*later*/
2573 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2574 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
2575 s
[fix2
]->s
.jf
= s
[i
];
2577 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
2578 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2579 s
[i
]->s
.jt
= NULL
; /*later*/
2580 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2581 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
2583 /* if (A == IPPROTO_ROUTING) goto v6advance */
2584 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2585 s
[i
]->s
.jt
= NULL
; /*later*/
2586 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2587 s
[i
]->s
.k
= IPPROTO_ROUTING
;
2589 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
2590 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2591 s
[i
]->s
.jt
= NULL
; /*later*/
2592 s
[i
]->s
.jf
= NULL
; /*later*/
2593 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
2604 * X = X + (P[X + 1] + 1) * 8;
2607 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2609 /* A = P[X + packet head] */
2610 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2614 s
[i
] = new_stmt(BPF_ST
);
2618 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2621 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2625 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2627 /* A = P[X + packet head]; */
2628 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2632 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2636 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2640 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2643 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2647 /* goto again; (must use BPF_JA for backward jump) */
2648 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2649 s
[i
]->s
.k
= again
- i
- 1;
2650 s
[i
- 1]->s
.jf
= s
[i
];
2654 for (j
= v6start
; j
<= v6end
; j
++)
2655 s
[j
]->s
.jt
= s
[v6advance
];
2660 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2662 s
[fix2
]->s
.jf
= s
[i
];
2668 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2669 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2670 s
[i
]->s
.jt
= NULL
; /*later*/
2671 s
[i
]->s
.jf
= NULL
; /*later*/
2672 s
[i
]->s
.k
= IPPROTO_AH
;
2674 s
[fix3
]->s
.jf
= s
[ahcheck
];
2681 * X = X + (P[X + 1] + 2) * 4;
2684 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2686 /* A = P[X + packet head]; */
2687 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2691 s
[i
] = new_stmt(BPF_ST
);
2695 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2698 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2702 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2704 /* A = P[X + packet head] */
2705 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2709 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2713 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2717 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2720 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2724 /* goto again; (must use BPF_JA for backward jump) */
2725 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2726 s
[i
]->s
.k
= again
- i
- 1;
2731 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2733 s
[fix2
]->s
.jt
= s
[end
];
2734 s
[fix4
]->s
.jf
= s
[end
];
2735 s
[fix5
]->s
.jt
= s
[end
];
2742 for (i
= 0; i
< max
- 1; i
++)
2743 s
[i
]->next
= s
[i
+ 1];
2744 s
[max
- 1]->next
= NULL
;
2749 b
= new_block(JMP(BPF_JEQ
));
2750 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2760 static struct block
*
2761 gen_proto(v
, proto
, dir
)
2766 struct block
*b0
, *b1
;
2768 if (dir
!= Q_DEFAULT
)
2769 bpf_error("direction applied to 'proto'");
2774 b0
= gen_proto(v
, Q_IP
, dir
);
2775 b1
= gen_proto(v
, Q_IPV6
, dir
);
2782 b0
= gen_linktype(ETHERTYPE_IP
);
2784 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2786 b1
= gen_protochain(v
, Q_IP
);
2792 b0
= gen_linktype(LLCSAP_ISONS
);
2793 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2798 bpf_error("arp does not encapsulate another protocol");
2802 bpf_error("rarp does not encapsulate another protocol");
2806 bpf_error("atalk encapsulation is not specifiable");
2810 bpf_error("decnet encapsulation is not specifiable");
2814 bpf_error("sca does not encapsulate another protocol");
2818 bpf_error("lat does not encapsulate another protocol");
2822 bpf_error("moprc does not encapsulate another protocol");
2826 bpf_error("mopdl does not encapsulate another protocol");
2830 return gen_linktype(v
);
2833 bpf_error("'udp proto' is bogus");
2837 bpf_error("'tcp proto' is bogus");
2841 bpf_error("'sctp proto' is bogus");
2845 bpf_error("'icmp proto' is bogus");
2849 bpf_error("'igmp proto' is bogus");
2853 bpf_error("'igrp proto' is bogus");
2857 bpf_error("'pim proto' is bogus");
2861 bpf_error("'vrrp proto' is bogus");
2866 b0
= gen_linktype(ETHERTYPE_IPV6
);
2868 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2870 b1
= gen_protochain(v
, Q_IPV6
);
2876 bpf_error("'icmp6 proto' is bogus");
2880 bpf_error("'ah proto' is bogus");
2883 bpf_error("'ah proto' is bogus");
2886 bpf_error("'stp proto' is bogus");
2889 bpf_error("'ipx proto' is bogus");
2892 bpf_error("'netbeui proto' is bogus");
2903 register const char *name
;
2906 int proto
= q
.proto
;
2910 bpf_u_int32 mask
, addr
;
2912 bpf_u_int32
**alist
;
2915 struct sockaddr_in
*sin
;
2916 struct sockaddr_in6
*sin6
;
2917 struct addrinfo
*res
, *res0
;
2918 struct in6_addr mask128
;
2920 struct block
*b
, *tmp
;
2921 int port
, real_proto
;
2926 addr
= pcap_nametonetaddr(name
);
2928 bpf_error("unknown network '%s'", name
);
2929 /* Left justify network addr and calculate its network mask */
2931 while (addr
&& (addr
& 0xff000000) == 0) {
2935 return gen_host(addr
, mask
, proto
, dir
);
2939 if (proto
== Q_LINK
) {
2943 eaddr
= pcap_ether_hostton(name
);
2946 "unknown ether host '%s'", name
);
2947 b
= gen_ehostop(eaddr
, dir
);
2952 eaddr
= pcap_ether_hostton(name
);
2955 "unknown FDDI host '%s'", name
);
2956 b
= gen_fhostop(eaddr
, dir
);
2961 eaddr
= pcap_ether_hostton(name
);
2964 "unknown token ring host '%s'", name
);
2965 b
= gen_thostop(eaddr
, dir
);
2971 "only ethernet/FDDI/token ring supports link-level host name");
2974 } else if (proto
== Q_DECNET
) {
2975 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2977 * I don't think DECNET hosts can be multihomed, so
2978 * there is no need to build up a list of addresses
2980 return (gen_host(dn_addr
, 0, proto
, dir
));
2983 alist
= pcap_nametoaddr(name
);
2984 if (alist
== NULL
|| *alist
== NULL
)
2985 bpf_error("unknown host '%s'", name
);
2987 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2989 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2991 tmp
= gen_host(**alist
++, 0xffffffff,
2998 memset(&mask128
, 0xff, sizeof(mask128
));
2999 res0
= res
= pcap_nametoaddrinfo(name
);
3001 bpf_error("unknown host '%s'", name
);
3003 tproto
= tproto6
= proto
;
3004 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
3008 for (res
= res0
; res
; res
= res
->ai_next
) {
3009 switch (res
->ai_family
) {
3011 if (tproto
== Q_IPV6
)
3014 sin
= (struct sockaddr_in
*)
3016 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
3017 0xffffffff, tproto
, dir
);
3020 if (tproto6
== Q_IP
)
3023 sin6
= (struct sockaddr_in6
*)
3025 tmp
= gen_host6(&sin6
->sin6_addr
,
3026 &mask128
, tproto6
, dir
);
3037 bpf_error("unknown host '%s'%s", name
,
3038 (proto
== Q_DEFAULT
)
3040 : " for specified address family");
3047 if (proto
!= Q_DEFAULT
&&
3048 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
3049 bpf_error("illegal qualifier of 'port'");
3050 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
3051 bpf_error("unknown port '%s'", name
);
3052 if (proto
== Q_UDP
) {
3053 if (real_proto
== IPPROTO_TCP
)
3054 bpf_error("port '%s' is tcp", name
);
3055 else if (real_proto
== IPPROTO_SCTP
)
3056 bpf_error("port '%s' is sctp", name
);
3058 /* override PROTO_UNDEF */
3059 real_proto
= IPPROTO_UDP
;
3061 if (proto
== Q_TCP
) {
3062 if (real_proto
== IPPROTO_UDP
)
3063 bpf_error("port '%s' is udp", name
);
3065 else if (real_proto
== IPPROTO_SCTP
)
3066 bpf_error("port '%s' is sctp", name
);
3068 /* override PROTO_UNDEF */
3069 real_proto
= IPPROTO_TCP
;
3071 if (proto
== Q_SCTP
) {
3072 if (real_proto
== IPPROTO_UDP
)
3073 bpf_error("port '%s' is udp", name
);
3075 else if (real_proto
== IPPROTO_TCP
)
3076 bpf_error("port '%s' is tcp", name
);
3078 /* override PROTO_UNDEF */
3079 real_proto
= IPPROTO_SCTP
;
3082 return gen_port(port
, real_proto
, dir
);
3086 b
= gen_port(port
, real_proto
, dir
);
3087 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3094 eaddr
= pcap_ether_hostton(name
);
3096 bpf_error("unknown ether host: %s", name
);
3098 alist
= pcap_nametoaddr(name
);
3099 if (alist
== NULL
|| *alist
== NULL
)
3100 bpf_error("unknown host '%s'", name
);
3101 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
3105 bpf_error("'gateway' not supported in this configuration");
3109 real_proto
= lookup_proto(name
, proto
);
3110 if (real_proto
>= 0)
3111 return gen_proto(real_proto
, proto
, dir
);
3113 bpf_error("unknown protocol: %s", name
);
3116 real_proto
= lookup_proto(name
, proto
);
3117 if (real_proto
>= 0)
3118 return gen_protochain(real_proto
, proto
, dir
);
3120 bpf_error("unknown protocol: %s", name
);
3132 gen_mcode(s1
, s2
, masklen
, q
)
3133 register const char *s1
, *s2
;
3134 register int masklen
;
3137 register int nlen
, mlen
;
3140 nlen
= __pcap_atoin(s1
, &n
);
3141 /* Promote short ipaddr */
3145 mlen
= __pcap_atoin(s2
, &m
);
3146 /* Promote short ipaddr */
3149 bpf_error("non-network bits set in \"%s mask %s\"",
3152 /* Convert mask len to mask */
3154 bpf_error("mask length must be <= 32");
3155 m
= 0xffffffff << (32 - masklen
);
3157 bpf_error("non-network bits set in \"%s/%d\"",
3164 return gen_host(n
, m
, q
.proto
, q
.dir
);
3167 bpf_error("Mask syntax for networks only");
3174 register const char *s
;
3179 int proto
= q
.proto
;
3185 else if (q
.proto
== Q_DECNET
)
3186 vlen
= __pcap_atodn(s
, &v
);
3188 vlen
= __pcap_atoin(s
, &v
);
3195 if (proto
== Q_DECNET
)
3196 return gen_host(v
, 0, proto
, dir
);
3197 else if (proto
== Q_LINK
) {
3198 bpf_error("illegal link layer address");
3201 if (s
== NULL
&& q
.addr
== Q_NET
) {
3202 /* Promote short net number */
3203 while (v
&& (v
& 0xff000000) == 0) {
3208 /* Promote short ipaddr */
3212 return gen_host(v
, mask
, proto
, dir
);
3217 proto
= IPPROTO_UDP
;
3218 else if (proto
== Q_TCP
)
3219 proto
= IPPROTO_TCP
;
3220 else if (proto
== Q_SCTP
)
3221 proto
= IPPROTO_SCTP
;
3222 else if (proto
== Q_DEFAULT
)
3223 proto
= PROTO_UNDEF
;
3225 bpf_error("illegal qualifier of 'port'");
3228 return gen_port((int)v
, proto
, dir
);
3232 b
= gen_port((int)v
, proto
, dir
);
3233 gen_or(gen_port6((int)v
, proto
, dir
), b
);
3239 bpf_error("'gateway' requires a name");
3243 return gen_proto((int)v
, proto
, dir
);
3246 return gen_protochain((int)v
, proto
, dir
);
3261 gen_mcode6(s1
, s2
, masklen
, q
)
3262 register const char *s1
, *s2
;
3263 register int masklen
;
3266 struct addrinfo
*res
;
3267 struct in6_addr
*addr
;
3268 struct in6_addr mask
;
3273 bpf_error("no mask %s supported", s2
);
3275 res
= pcap_nametoaddrinfo(s1
);
3277 bpf_error("invalid ip6 address %s", s1
);
3279 bpf_error("%s resolved to multiple address", s1
);
3280 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3282 if (sizeof(mask
) * 8 < masklen
)
3283 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3284 memset(&mask
, 0, sizeof(mask
));
3285 memset(&mask
, 0xff, masklen
/ 8);
3287 mask
.s6_addr
[masklen
/ 8] =
3288 (0xff << (8 - masklen
% 8)) & 0xff;
3291 a
= (u_int32_t
*)addr
;
3292 m
= (u_int32_t
*)&mask
;
3293 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3294 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3295 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
3303 bpf_error("Mask syntax for networks only");
3307 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
3312 bpf_error("invalid qualifier against IPv6 address");
3320 register const u_char
*eaddr
;
3323 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3324 if (linktype
== DLT_EN10MB
)
3325 return gen_ehostop(eaddr
, (int)q
.dir
);
3326 if (linktype
== DLT_FDDI
)
3327 return gen_fhostop(eaddr
, (int)q
.dir
);
3328 if (linktype
== DLT_IEEE802
)
3329 return gen_thostop(eaddr
, (int)q
.dir
);
3330 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
3332 bpf_error("ethernet address used in non-ether expression");
3338 struct slist
*s0
, *s1
;
3341 * This is definitely not the best way to do this, but the
3342 * lists will rarely get long.
3349 static struct slist
*
3355 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3360 static struct slist
*
3366 s
= new_stmt(BPF_LD
|BPF_MEM
);
3372 gen_load(proto
, index
, size
)
3377 struct slist
*s
, *tmp
;
3379 int regno
= alloc_reg();
3381 free_reg(index
->regno
);
3385 bpf_error("data size must be 1, 2, or 4");
3401 bpf_error("unsupported index operation");
3404 s
= xfer_to_x(index
);
3405 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3407 sappend(index
->s
, s
);
3422 /* XXX Note that we assume a fixed link header here. */
3423 s
= xfer_to_x(index
);
3424 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3427 sappend(index
->s
, s
);
3429 b
= gen_proto_abbrev(proto
);
3431 gen_and(index
->b
, b
);
3443 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3445 sappend(s
, xfer_to_a(index
));
3446 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3447 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3448 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3450 sappend(index
->s
, s
);
3452 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
3454 gen_and(index
->b
, b
);
3456 gen_and(gen_proto_abbrev(Q_IP
), b
);
3462 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
3466 index
->regno
= regno
;
3467 s
= new_stmt(BPF_ST
);
3469 sappend(index
->s
, s
);
3475 gen_relation(code
, a0
, a1
, reversed
)
3477 struct arth
*a0
, *a1
;
3480 struct slist
*s0
, *s1
, *s2
;
3481 struct block
*b
, *tmp
;
3485 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
3486 b
= new_block(JMP(code
));
3487 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
3488 reversed
= !reversed
;
3489 b
->s
.k
= 0x80000000;
3497 sappend(a0
->s
, a1
->s
);
3501 free_reg(a0
->regno
);
3502 free_reg(a1
->regno
);
3504 /* 'and' together protocol checks */
3507 gen_and(a0
->b
, tmp
= a1
->b
);
3523 int regno
= alloc_reg();
3524 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
3527 s
= new_stmt(BPF_LD
|BPF_LEN
);
3528 s
->next
= new_stmt(BPF_ST
);
3529 s
->next
->s
.k
= regno
;
3544 a
= (struct arth
*)newchunk(sizeof(*a
));
3548 s
= new_stmt(BPF_LD
|BPF_IMM
);
3550 s
->next
= new_stmt(BPF_ST
);
3566 s
= new_stmt(BPF_ALU
|BPF_NEG
);
3569 s
= new_stmt(BPF_ST
);
3577 gen_arth(code
, a0
, a1
)
3579 struct arth
*a0
, *a1
;
3581 struct slist
*s0
, *s1
, *s2
;
3585 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
3590 sappend(a0
->s
, a1
->s
);
3592 free_reg(a0
->regno
);
3593 free_reg(a1
->regno
);
3595 s0
= new_stmt(BPF_ST
);
3596 a0
->regno
= s0
->s
.k
= alloc_reg();
3603 * Here we handle simple allocation of the scratch registers.
3604 * If too many registers are alloc'd, the allocator punts.
3606 static int regused
[BPF_MEMWORDS
];
3610 * Return the next free register.
3615 int n
= BPF_MEMWORDS
;
3618 if (regused
[curreg
])
3619 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
3621 regused
[curreg
] = 1;
3625 bpf_error("too many registers needed to evaluate expression");
3630 * Return a register to the table so it can
3640 static struct block
*
3647 s
= new_stmt(BPF_LD
|BPF_LEN
);
3648 b
= new_block(JMP(jmp
));
3659 return gen_len(BPF_JGE
, n
);
3663 * Actually, this is less than or equal.
3671 b
= gen_len(BPF_JGT
, n
);
3678 gen_byteop(op
, idx
, val
)
3689 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3692 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3693 b
->s
.code
= JMP(BPF_JGE
);
3698 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3699 b
->s
.code
= JMP(BPF_JGT
);
3703 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3707 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3711 b
= new_block(JMP(BPF_JEQ
));
3718 static u_char abroadcast
[] = { 0x0 };
3721 gen_broadcast(proto
)
3724 bpf_u_int32 hostmask
;
3725 struct block
*b0
, *b1
, *b2
;
3726 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3732 if (linktype
== DLT_ARCNET
)
3733 return gen_ahostop(abroadcast
, Q_DST
);
3734 if (linktype
== DLT_EN10MB
)
3735 return gen_ehostop(ebroadcast
, Q_DST
);
3736 if (linktype
== DLT_FDDI
)
3737 return gen_fhostop(ebroadcast
, Q_DST
);
3738 if (linktype
== DLT_IEEE802
)
3739 return gen_thostop(ebroadcast
, Q_DST
);
3740 bpf_error("not a broadcast link");
3744 b0
= gen_linktype(ETHERTYPE_IP
);
3745 hostmask
= ~netmask
;
3746 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3747 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3748 (bpf_int32
)(~0 & hostmask
), hostmask
);
3753 bpf_error("only ether/ip broadcast filters supported");
3757 gen_multicast(proto
)
3760 register struct block
*b0
, *b1
;
3761 register struct slist
*s
;
3767 if (linktype
== DLT_ARCNET
)
3768 /* all ARCnet multicasts use the same address */
3769 return gen_ahostop(abroadcast
, Q_DST
);
3771 if (linktype
== DLT_EN10MB
) {
3772 /* ether[0] & 1 != 0 */
3773 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3775 b0
= new_block(JMP(BPF_JSET
));
3781 if (linktype
== DLT_FDDI
) {
3782 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3783 /* fddi[1] & 1 != 0 */
3784 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3786 b0
= new_block(JMP(BPF_JSET
));
3792 /* TODO - check how token ring handles multicast */
3793 /* if (linktype == DLT_IEEE802) ... */
3795 /* Link not known to support multicasts */
3799 b0
= gen_linktype(ETHERTYPE_IP
);
3800 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3801 b1
->s
.code
= JMP(BPF_JGE
);
3807 b0
= gen_linktype(ETHERTYPE_IPV6
);
3808 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3813 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3817 * generate command for inbound/outbound. It's here so we can
3818 * make it link-type specific. 'dir' = 0 implies "inbound",
3819 * = 1 implies "outbound".
3825 register struct block
*b0
;
3828 * Only some data link types support inbound/outbound qualifiers.
3833 b0
= gen_relation(BPF_JEQ
,
3834 gen_load(Q_LINK
, gen_loadi(0), 1),
3840 bpf_error("inbound/outbound not supported on linktype %d\n",
3850 register const u_char
*eaddr
;
3853 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3854 if (linktype
== DLT_ARCNET
)
3855 return gen_ahostop(eaddr
, (int)q
.dir
);
3857 bpf_error("ARCnet address used in non-arc expression");
3861 static struct block
*
3862 gen_ahostop(eaddr
, dir
)
3863 register const u_char
*eaddr
;
3866 register struct block
*b0
, *b1
;
3869 /* src comes first, different from Ethernet */
3871 return gen_bcmp(0, 1, eaddr
);
3874 return gen_bcmp(1, 1, eaddr
);
3877 b0
= gen_ahostop(eaddr
, Q_SRC
);
3878 b1
= gen_ahostop(eaddr
, Q_DST
);
3884 b0
= gen_ahostop(eaddr
, Q_SRC
);
3885 b1
= gen_ahostop(eaddr
, Q_DST
);
3894 * support IEEE 802.1Q VLAN trunk over ethernet
3903 * Change the offsets to point to the type and data fields within
3904 * the VLAN packet. This is somewhat of a kludge.
3906 if (orig_nl
== (u_int
)-1) {
3907 orig_linktype
= off_linktype
; /* save original values */
3918 bpf_error("no VLAN support for data link type %d",
3924 /* check for VLAN */
3925 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3927 /* If a specific VLAN is requested, check VLAN id */
3928 if (vlan_num
>= 0) {
3931 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);