]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
a033a783a90a861604b834bd1ed2002559953172
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.149 2001-02-21 09:33:04 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"
58 #include <pcap-namedb.h>
61 #include <sys/socket.h>
66 #ifdef HAVE_OS_PROTO_H
70 #define JMP(c) ((c)|BPF_JMP|BPF_K)
73 static jmp_buf top_ctx
;
74 static pcap_t
*bpf_pcap
;
78 int pcap_fddipad
= PCAP_FDDIPAD
;
85 bpf_error(const char *fmt
, ...)
92 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
99 static void init_linktype(int);
101 static int alloc_reg(void);
102 static void free_reg(int);
104 static struct block
*root
;
107 * We divy out chunks of memory rather than call malloc each time so
108 * we don't have to worry about leaking memory. It's probably
109 * not a big deal if all this memory was wasted but it this ever
110 * goes into a library that would probably not be a good idea.
113 #define CHUNK0SIZE 1024
119 static struct chunk chunks
[NCHUNKS
];
120 static int cur_chunk
;
122 static void *newchunk(u_int
);
123 static void freechunks(void);
124 static inline struct block
*new_block(int);
125 static inline struct slist
*new_stmt(int);
126 static struct block
*gen_retblk(int);
127 static inline void syntax(void);
129 static void backpatch(struct block
*, struct block
*);
130 static void merge(struct block
*, struct block
*);
131 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
132 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
133 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
134 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
135 static struct block
*gen_uncond(int);
136 static inline struct block
*gen_true(void);
137 static inline struct block
*gen_false(void);
138 static struct block
*gen_linktype(int);
139 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
140 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
142 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
144 static struct block
*gen_ehostop(const u_char
*, int);
145 static struct block
*gen_fhostop(const u_char
*, int);
146 static struct block
*gen_thostop(const u_char
*, int);
147 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
148 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
150 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
153 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
155 static struct block
*gen_ipfrag(void);
156 static struct block
*gen_portatom(int, bpf_int32
);
158 static struct block
*gen_portatom6(int, bpf_int32
);
160 struct block
*gen_portop(int, int, int);
161 static struct block
*gen_port(int, int, int);
163 struct block
*gen_portop6(int, int, int);
164 static struct block
*gen_port6(int, int, int);
166 static int lookup_proto(const char *, int);
167 static struct block
*gen_protochain(int, int, int);
168 static struct block
*gen_proto(int, int, int);
169 static struct slist
*xfer_to_x(struct arth
*);
170 static struct slist
*xfer_to_a(struct arth
*);
171 static struct block
*gen_len(int, int);
181 /* XXX Round up to nearest long. */
182 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
184 /* XXX Round up to structure boundary. */
188 cp
= &chunks
[cur_chunk
];
189 if (n
> cp
->n_left
) {
190 ++cp
, k
= ++cur_chunk
;
192 bpf_error("out of memory");
193 size
= CHUNK0SIZE
<< k
;
194 cp
->m
= (void *)malloc(size
);
195 memset((char *)cp
->m
, 0, size
);
198 bpf_error("out of memory");
201 return (void *)((char *)cp
->m
+ cp
->n_left
);
210 for (i
= 0; i
< NCHUNKS
; ++i
)
211 if (chunks
[i
].m
!= NULL
) {
218 * A strdup whose allocations are freed after code generation is over.
222 register const char *s
;
224 int n
= strlen(s
) + 1;
225 char *cp
= newchunk(n
);
231 static inline struct block
*
237 p
= (struct block
*)newchunk(sizeof(*p
));
244 static inline struct slist
*
250 p
= (struct slist
*)newchunk(sizeof(*p
));
256 static struct block
*
260 struct block
*b
= new_block(BPF_RET
|BPF_K
);
269 bpf_error("syntax error in filter expression");
272 static bpf_u_int32 netmask
;
277 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
278 char *buf
, int optimize
, bpf_u_int32 mask
)
287 if (setjmp(top_ctx
)) {
295 snaplen
= pcap_snapshot(p
);
297 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
298 "snaplen of 0 rejects all packets");
302 lex_init(buf
? buf
: "");
303 init_linktype(pcap_datalink(p
));
310 root
= gen_retblk(snaplen
);
312 if (optimize
&& !no_optimize
) {
315 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
316 bpf_error("expression rejects all packets");
318 program
->bf_insns
= icode_to_fcode(root
, &len
);
319 program
->bf_len
= len
;
327 * entry point for using the compiler with no pcap open
328 * pass in all the stuff that is needed explicitly instead.
331 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
332 struct bpf_program
*program
,
333 char *buf
, int optimize
, bpf_u_int32 mask
)
338 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
341 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
347 * Clean up a "struct bpf_program" by freeing all the memory allocated
351 pcap_freecode(struct bpf_program
*program
)
354 if (program
->bf_insns
!= NULL
) {
355 free((char *)program
->bf_insns
);
356 program
->bf_insns
= NULL
;
361 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
362 * which of the jt and jf fields has been resolved and which is a pointer
363 * back to another unresolved block (or nil). At least one of the fields
364 * in each block is already resolved.
367 backpatch(list
, target
)
368 struct block
*list
, *target
;
385 * Merge the lists in b0 and b1, using the 'sense' field to indicate
386 * which of jt and jf is the link.
390 struct block
*b0
, *b1
;
392 register struct block
**p
= &b0
;
394 /* Find end of list. */
396 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
398 /* Concatenate the lists. */
406 backpatch(p
, gen_retblk(snaplen
));
407 p
->sense
= !p
->sense
;
408 backpatch(p
, gen_retblk(0));
414 struct block
*b0
, *b1
;
416 backpatch(b0
, b1
->head
);
417 b0
->sense
= !b0
->sense
;
418 b1
->sense
= !b1
->sense
;
420 b1
->sense
= !b1
->sense
;
426 struct block
*b0
, *b1
;
428 b0
->sense
= !b0
->sense
;
429 backpatch(b0
, b1
->head
);
430 b0
->sense
= !b0
->sense
;
439 b
->sense
= !b
->sense
;
442 static struct block
*
443 gen_cmp(offset
, size
, v
)
450 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
453 b
= new_block(JMP(BPF_JEQ
));
460 static struct block
*
461 gen_cmp_gt(offset
, size
, v
)
468 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
471 b
= new_block(JMP(BPF_JGT
));
478 static struct block
*
479 gen_mcmp(offset
, size
, v
, mask
)
484 struct block
*b
= gen_cmp(offset
, size
, v
);
487 if (mask
!= 0xffffffff) {
488 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
495 static struct block
*
496 gen_bcmp(offset
, size
, v
)
497 register u_int offset
, size
;
498 register const u_char
*v
;
500 register struct block
*b
, *tmp
;
504 register const u_char
*p
= &v
[size
- 4];
505 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
506 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
508 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
515 register const u_char
*p
= &v
[size
- 2];
516 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
518 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
525 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
534 * Various code constructs need to know the layout of the data link
535 * layer. These variables give the necessary offsets. off_linktype
536 * is set to -1 for no encapsulation, in which case, IP is assumed.
538 static u_int off_linktype
;
557 * SLIP doesn't have a link level type. The 16 byte
558 * header is hacked into our SLIP driver.
565 /* XXX this may be the same as the DLT_PPP_BSDOS case */
591 * FDDI doesn't really have a link-level type field.
592 * We set "off_linktype" to the offset of the LLC header.
594 * To check for Ethernet types, we assume that SSAP = SNAP
595 * is being used and pick out the encapsulated Ethernet type.
596 * XXX - should we generate code to check for SNAP?
600 off_linktype
+= pcap_fddipad
;
604 off_nl
+= pcap_fddipad
;
610 * Token Ring doesn't really have a link-level type field.
611 * We set "off_linktype" to the offset of the LLC header.
613 * To check for Ethernet types, we assume that SSAP = SNAP
614 * is being used and pick out the encapsulated Ethernet type.
615 * XXX - should we generate code to check for SNAP?
617 * XXX - the header is actually variable-length.
618 * Some various Linux patched versions gave 38
619 * as "off_linktype" and 40 as "off_nl"; however,
620 * if a token ring packet has *no* routing
621 * information, i.e. is not source-routed, the correct
622 * values are 20 and 22, as they are in the vanilla code.
624 * A packet is source-routed iff the uppermost bit
625 * of the first byte of the source address, at an
626 * offset of 8, has the uppermost bit set. If the
627 * packet is source-routed, the total number of bytes
628 * of routing information is 2 plus bits 0x1F00 of
629 * the 16-bit value at an offset of 14 (shifted right
630 * 8 - figure out which byte that is).
636 case DLT_ATM_RFC1483
:
638 * assume routed, non-ISO PDUs
639 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
650 case DLT_ATM_CLIP
: /* Linux ATM defines this */
655 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
660 bpf_error("unknown data link type %d", linktype
);
664 static struct block
*
671 s
= new_stmt(BPF_LD
|BPF_IMM
);
673 b
= new_block(JMP(BPF_JEQ
));
679 static inline struct block
*
682 return gen_uncond(1);
685 static inline struct block
*
688 return gen_uncond(0);
692 * Byte-swap a 32-bit number.
693 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
694 * big-endian platforms.)
696 #define SWAPLONG(y) \
697 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
699 static struct block
*
703 struct block
*b0
, *b1
;
712 * OSI protocols always use 802.2 encapsulation.
713 * XXX - should we check both the DSAP and the
714 * SSAP, like this, or should we check just the
717 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
719 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
720 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
726 * NetBEUI always uses 802.2 encapsulation.
727 * XXX - should we check both the DSAP and the
728 * SSAP, like this, or should we check just the
731 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
733 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
734 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
742 * Ethernet_II frames, which are Ethernet
743 * frames with a frame type of ETHERTYPE_IPX;
745 * Ethernet_802.3 frames, which are 802.3
746 * frames (i.e., the type/length field is
747 * a length field, <= ETHERMTU, rather than
748 * a type field) with the first two bytes
749 * after the Ethernet/802.3 header being
752 * Ethernet_802.2 frames, which are 802.3
753 * frames with an 802.2 LLC header and
754 * with the IPX LSAP as the DSAP in the LLC
757 * Ethernet_SNAP frames, which are 802.3
758 * frames with an LLC header and a SNAP
759 * header and with an OUI of 0x000000
760 * (encapsulated Ethernet) and a protocol
761 * ID of ETHERTYPE_IPX in the SNAP header.
763 * XXX - should we generate the same code both
764 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
768 * This generates code to check both for the
769 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
771 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
772 (bpf_int32
)LLCSAP_IPX
);
773 b1
= gen_cmp(off_linktype
+ 2, BPF_H
,
778 * Now we add code to check for SNAP frames with
779 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
781 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
785 * Now we generate code to check for 802.3
788 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
792 * Now add the check for 802.3 frames before the
793 * check for Ethernet_802.2 and Ethernet_802.3,
794 * as those checks should only be done on 802.3
795 * frames, not on Ethernet frames.
800 * Now add the check for Ethernet_II frames, and
801 * do that before checking for the other frame
804 b0
= gen_cmp(off_linktype
, BPF_H
,
805 (bpf_int32
)ETHERTYPE_IPX
);
809 case ETHERTYPE_ATALK
:
812 * EtherTalk (AppleTalk protocols on Ethernet link
813 * layer) may use 802.2 encapsulation.
817 * Check for 802.2 encapsulation (EtherTalk phase 2?);
818 * we check for an Ethernet type field less than
819 * 1500, which means it's an 802.3 length field.
821 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
825 * 802.2-encapsulated ETHERTYPE_ATALK packets are
826 * SNAP packets with an organization code of
827 * 0x080007 (Apple, for Appletalk) and a protocol
828 * type of ETHERTYPE_ATALK (Appletalk).
830 * 802.2-encapsulated ETHERTYPE_AARP packets are
831 * SNAP packets with an organization code of
832 * 0x000000 (encapsulated Ethernet) and a protocol
833 * type of ETHERTYPE_AARP (Appletalk ARP).
835 if (proto
== ETHERTYPE_ATALK
)
836 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
837 else /* proto == ETHERTYPE_AARP */
838 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
842 * Check for Ethernet encapsulation (Ethertalk
843 * phase 1?); we just check for the Ethernet
846 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
852 if (proto
<= ETHERMTU
) {
854 * This is an LLC SAP value, so the frames
855 * that match would be 802.2 frames.
856 * Check that the frame is an 802.2 frame
857 * (i.e., that the length/type field is
858 * a length field, <= ETHERMTU) and
859 * then check the DSAP.
861 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
863 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
869 * This is an Ethernet type, so compare
870 * the length/type field with it (if
871 * the frame is an 802.2 frame, the length
872 * field will be <= ETHERMTU, and, as
873 * "proto" is > ETHERMTU, this test
874 * will fail and the frame won't match,
875 * which is what we want).
877 return gen_cmp(off_linktype
, BPF_H
,
885 case DLT_ATM_RFC1483
:
888 * XXX - handle token-ring variable-length header.
893 return gen_cmp(off_linktype
, BPF_H
, (long)
894 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
897 return gen_cmp(off_linktype
, BPF_H
, (long)
898 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
902 * XXX - are there ever SNAP frames for IPX on
903 * non-Ethernet 802.x networks?
905 return gen_cmp(off_linktype
, BPF_B
,
906 (bpf_int32
)LLCSAP_IPX
);
908 case ETHERTYPE_ATALK
:
910 * 802.2-encapsulated ETHERTYPE_ATALK packets are
911 * SNAP packets with an organization code of
912 * 0x080007 (Apple, for Appletalk) and a protocol
913 * type of ETHERTYPE_ATALK (Appletalk).
915 * XXX - check for an organization code of
916 * encapsulated Ethernet as well?
918 return gen_snap(0x080007, ETHERTYPE_ATALK
,
924 * XXX - we don't have to check for IPX 802.3
925 * here, but should we check for the IPX Ethertype?
927 if (proto
<= ETHERMTU
) {
929 * This is an LLC SAP value, so check
932 return gen_cmp(off_linktype
, BPF_B
,
936 * This is an Ethernet type; we assume
937 * that it's unlikely that it'll
938 * appear in the right place at random,
939 * and therefore check only the
940 * location that would hold the Ethernet
941 * type in a SNAP frame with an organization
942 * code of 0x000000 (encapsulated Ethernet).
944 * XXX - if we were to check for the SNAP DSAP
945 * and LSAP, as per XXX, and were also to check
946 * for an organization code of 0x000000
947 * (encapsulated Ethernet), we'd do
949 * return gen_snap(0x000000, proto,
952 * here; for now, we don't, as per the above.
953 * I don't know whether it's worth the
954 * extra CPU time to do the right check
957 return gen_cmp(off_linktype
+6, BPF_H
,
968 * OSI protocols always use 802.2 encapsulation.
969 * XXX - should we check both the DSAP and the
970 * LSAP, like this, or should we check just the
973 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
974 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
975 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
981 * NetBEUI always uses 802.2 encapsulation.
982 * XXX - should we check both the DSAP and the
983 * LSAP, like this, or should we check just the
986 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
987 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
988 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
994 * Ethernet_II frames, which are Ethernet
995 * frames with a frame type of ETHERTYPE_IPX;
997 * Ethernet_802.3 frames, which have a frame
998 * type of LINUX_SLL_P_802_3;
1000 * Ethernet_802.2 frames, which are 802.3
1001 * frames with an 802.2 LLC header (i.e, have
1002 * a frame type of LINUX_SLL_P_802_2) and
1003 * with the IPX LSAP as the DSAP in the LLC
1006 * Ethernet_SNAP frames, which are 802.3
1007 * frames with an LLC header and a SNAP
1008 * header and with an OUI of 0x000000
1009 * (encapsulated Ethernet) and a protocol
1010 * ID of ETHERTYPE_IPX in the SNAP header.
1012 * First, do the checks on LINUX_SLL_P_802_2
1013 * frames; generate the check for either
1014 * Ethernet_802.2 or Ethernet_SNAP frames, and
1015 * then put a check for LINUX_SLL_P_802_2 frames
1018 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1019 (bpf_int32
)LLCSAP_IPX
);
1020 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1023 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1027 * Now check for 802.3 frames and OR that with
1028 * the previous test.
1030 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1034 * Now add the check for Ethernet_II frames, and
1035 * do that before checking for the other frame
1038 b0
= gen_cmp(off_linktype
, BPF_H
,
1039 (bpf_int32
)ETHERTYPE_IPX
);
1043 case ETHERTYPE_ATALK
:
1044 case ETHERTYPE_AARP
:
1046 * EtherTalk (AppleTalk protocols on Ethernet link
1047 * layer) may use 802.2 encapsulation.
1051 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1052 * we check for the 802.2 protocol type in the
1053 * "Ethernet type" field.
1055 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1058 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1059 * SNAP packets with an organization code of
1060 * 0x080007 (Apple, for Appletalk) and a protocol
1061 * type of ETHERTYPE_ATALK (Appletalk).
1063 * 802.2-encapsulated ETHERTYPE_AARP packets are
1064 * SNAP packets with an organization code of
1065 * 0x000000 (encapsulated Ethernet) and a protocol
1066 * type of ETHERTYPE_AARP (Appletalk ARP).
1068 if (proto
== ETHERTYPE_ATALK
)
1069 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1071 else /* proto == ETHERTYPE_AARP */
1072 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1077 * Check for Ethernet encapsulation (Ethertalk
1078 * phase 1?); we just check for the Ethernet
1081 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1087 if (proto
<= ETHERMTU
) {
1089 * This is an LLC SAP value, so the frames
1090 * that match would be 802.2 frames.
1091 * Check for the 802.2 protocol type
1092 * in the "Ethernet type" field, and
1093 * then check the DSAP.
1095 b0
= gen_cmp(off_linktype
, BPF_H
,
1097 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1103 * This is an Ethernet type, so compare
1104 * the length/type field with it (if
1105 * the frame is an 802.2 frame, the length
1106 * field will be <= ETHERMTU, and, as
1107 * "proto" is > ETHERMTU, this test
1108 * will fail and the frame won't match,
1109 * which is what we want).
1111 return gen_cmp(off_linktype
, BPF_H
,
1118 case DLT_SLIP_BSDOS
:
1121 * These types don't provide any type field; packets
1124 * XXX - for IPv4, check for a version number of 4, and,
1125 * for IPv6, check for a version number of 6?
1131 case ETHERTYPE_IPV6
:
1133 return gen_true(); /* always true */
1136 return gen_false(); /* always false */
1141 case DLT_PPP_SERIAL
:
1143 * We use Ethernet protocol types inside libpcap;
1144 * map them to the corresponding PPP protocol types.
1149 proto
= PPP_IP
; /* XXX was 0x21 */
1153 case ETHERTYPE_IPV6
:
1162 case ETHERTYPE_ATALK
:
1176 * I'm assuming the "Bridging PDU"s that go
1177 * over PPP are Spanning Tree Protocol
1191 * We use Ethernet protocol types inside libpcap;
1192 * map them to the corresponding PPP protocol types.
1197 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1198 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1200 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1205 case ETHERTYPE_IPV6
:
1215 case ETHERTYPE_ATALK
:
1229 * I'm assuming the "Bridging PDU"s that go
1230 * over PPP are Spanning Tree Protocol
1245 * For DLT_NULL, the link-layer header is a 32-bit
1246 * word containing an AF_ value in *host* byte order.
1248 * In addition, if we're reading a saved capture file,
1249 * the host byte order in the capture may not be the
1250 * same as the host byte order on this machine.
1252 * For DLT_LOOP, the link-layer header is a 32-bit
1253 * word containing an AF_ value in *network* byte order.
1255 * XXX - AF_ values may, unfortunately, be platform-
1256 * dependent; for example, FreeBSD's AF_INET6 is 24
1257 * whilst NetBSD's and OpenBSD's is 26.
1259 * This means that, when reading a capture file, just
1260 * checking for our AF_INET6 value won't work if the
1261 * capture file came from another OS.
1270 case ETHERTYPE_IPV6
:
1277 * Not a type on which we support filtering.
1278 * XXX - support those that have AF_ values
1279 * #defined on this platform, at least?
1284 if (linktype
== DLT_NULL
) {
1286 * The AF_ value is in host byte order, but
1287 * the BPF interpreter will convert it to
1288 * network byte order.
1290 * If this is a save file, and it's from a
1291 * machine with the opposite byte order to
1292 * ours, we byte-swap the AF_ value.
1294 * Then we run it through "htonl()", and
1295 * generate code to compare against the result.
1297 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1298 bpf_pcap
->sf
.swapped
)
1299 proto
= SWAPLONG(proto
);
1300 proto
= htonl(proto
);
1302 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1306 * All the types that have no encapsulation should either be
1307 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1308 * all packets are IP packets, or should be handled in some
1309 * special case, if none of them are (if some are and some
1310 * aren't, the lack of encapsulation is a problem, as we'd
1311 * have to find some other way of determining the packet type).
1313 * Therefore, if "off_linktype" is -1, there's an error.
1315 if (off_linktype
== -1)
1319 * Any type not handled above should always have an Ethernet
1320 * type at an offset of "off_linktype". (PPP is partially
1321 * handled above - the protocol type is mapped from the
1322 * Ethernet and LLC types we use internally to the corresponding
1323 * PPP type - but the PPP type is always specified by a value
1324 * at "off_linktype", so we don't have to do the code generation
1327 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1331 * Check for an LLC SNAP packet with a given organization code and
1332 * protocol type; we check the entire contents of the 802.2 LLC and
1333 * snap headers, checking for DSAP and SSAP of SNAP and a control
1334 * field of 0x03 in the LLC header, and for the specified organization
1335 * code and protocol type in the SNAP header.
1337 static struct block
*
1338 gen_snap(orgcode
, ptype
, offset
)
1339 bpf_u_int32 orgcode
;
1343 u_char snapblock
[8];
1345 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1346 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1347 snapblock
[2] = 0x03; /* control = UI */
1348 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1349 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1350 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1351 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1352 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1353 return gen_bcmp(offset
, 8, snapblock
);
1356 static struct block
*
1357 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1361 u_int src_off
, dst_off
;
1363 struct block
*b0
, *b1
;
1377 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1378 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1384 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1385 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1392 b0
= gen_linktype(proto
);
1393 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1399 static struct block
*
1400 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1401 struct in6_addr
*addr
;
1402 struct in6_addr
*mask
;
1404 u_int src_off
, dst_off
;
1406 struct block
*b0
, *b1
;
1421 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1422 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1428 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1429 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1436 /* this order is important */
1437 a
= (u_int32_t
*)addr
;
1438 m
= (u_int32_t
*)mask
;
1439 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1440 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1442 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1444 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1446 b0
= gen_linktype(proto
);
1452 static struct block
*
1453 gen_ehostop(eaddr
, dir
)
1454 register const u_char
*eaddr
;
1457 register struct block
*b0
, *b1
;
1461 return gen_bcmp(6, 6, eaddr
);
1464 return gen_bcmp(0, 6, eaddr
);
1467 b0
= gen_ehostop(eaddr
, Q_SRC
);
1468 b1
= gen_ehostop(eaddr
, Q_DST
);
1474 b0
= gen_ehostop(eaddr
, Q_SRC
);
1475 b1
= gen_ehostop(eaddr
, Q_DST
);
1484 * Like gen_ehostop, but for DLT_FDDI
1486 static struct block
*
1487 gen_fhostop(eaddr
, dir
)
1488 register const u_char
*eaddr
;
1491 struct block
*b0
, *b1
;
1496 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1498 return gen_bcmp(6 + 1, 6, eaddr
);
1503 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1505 return gen_bcmp(0 + 1, 6, eaddr
);
1509 b0
= gen_fhostop(eaddr
, Q_SRC
);
1510 b1
= gen_fhostop(eaddr
, Q_DST
);
1516 b0
= gen_fhostop(eaddr
, Q_SRC
);
1517 b1
= gen_fhostop(eaddr
, Q_DST
);
1526 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1528 static struct block
*
1529 gen_thostop(eaddr
, dir
)
1530 register const u_char
*eaddr
;
1533 register struct block
*b0
, *b1
;
1537 return gen_bcmp(8, 6, eaddr
);
1540 return gen_bcmp(2, 6, eaddr
);
1543 b0
= gen_thostop(eaddr
, Q_SRC
);
1544 b1
= gen_thostop(eaddr
, Q_DST
);
1550 b0
= gen_thostop(eaddr
, Q_SRC
);
1551 b1
= gen_thostop(eaddr
, Q_DST
);
1560 * This is quite tricky because there may be pad bytes in front of the
1561 * DECNET header, and then there are two possible data packet formats that
1562 * carry both src and dst addresses, plus 5 packet types in a format that
1563 * carries only the src node, plus 2 types that use a different format and
1564 * also carry just the src node.
1568 * Instead of doing those all right, we just look for data packets with
1569 * 0 or 1 bytes of padding. If you want to look at other packets, that
1570 * will require a lot more hacking.
1572 * To add support for filtering on DECNET "areas" (network numbers)
1573 * one would want to add a "mask" argument to this routine. That would
1574 * make the filter even more inefficient, although one could be clever
1575 * and not generate masking instructions if the mask is 0xFFFF.
1577 static struct block
*
1578 gen_dnhostop(addr
, dir
, base_off
)
1583 struct block
*b0
, *b1
, *b2
, *tmp
;
1584 u_int offset_lh
; /* offset if long header is received */
1585 u_int offset_sh
; /* offset if short header is received */
1590 offset_sh
= 1; /* follows flags */
1591 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1595 offset_sh
= 3; /* follows flags, dstnode */
1596 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1600 /* Inefficient because we do our Calvinball dance twice */
1601 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1602 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1608 /* Inefficient because we do our Calvinball dance twice */
1609 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1610 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1615 bpf_error("ISO host filtering not implemented");
1620 b0
= gen_linktype(ETHERTYPE_DN
);
1621 /* Check for pad = 1, long header case */
1622 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1623 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1624 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1625 BPF_H
, (bpf_int32
)ntohs(addr
));
1627 /* Check for pad = 0, long header case */
1628 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1629 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1632 /* Check for pad = 1, short header case */
1633 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1634 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1635 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1636 BPF_H
, (bpf_int32
)ntohs(addr
));
1639 /* Check for pad = 0, short header case */
1640 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1641 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1645 /* Combine with test for linktype */
1650 static struct block
*
1651 gen_host(addr
, mask
, proto
, dir
)
1657 struct block
*b0
, *b1
;
1662 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1663 if (off_linktype
!= -1) {
1664 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1666 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1672 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1673 off_nl
+ 12, off_nl
+ 16);
1676 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1677 off_nl
+ 14, off_nl
+ 24);
1680 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1681 off_nl
+ 14, off_nl
+ 24);
1684 bpf_error("'tcp' modifier applied to host");
1687 bpf_error("'udp' modifier applied to host");
1690 bpf_error("'icmp' modifier applied to host");
1693 bpf_error("'igmp' modifier applied to host");
1696 bpf_error("'igrp' modifier applied to host");
1699 bpf_error("'pim' modifier applied to host");
1702 bpf_error("'vrrp' modifier applied to host");
1705 bpf_error("ATALK host filtering not implemented");
1708 bpf_error("AARP host filtering not implemented");
1711 return gen_dnhostop(addr
, dir
, off_nl
);
1714 bpf_error("SCA host filtering not implemented");
1717 bpf_error("LAT host filtering not implemented");
1720 bpf_error("MOPDL host filtering not implemented");
1723 bpf_error("MOPRC host filtering not implemented");
1727 bpf_error("'ip6' modifier applied to ip host");
1730 bpf_error("'icmp6' modifier applied to host");
1734 bpf_error("'ah' modifier applied to host");
1737 bpf_error("'esp' modifier applied to host");
1740 bpf_error("ISO host filtering not implemented");
1743 bpf_error("'esis' modifier applied to host");
1746 bpf_error("'isis' modifier applied to host");
1749 bpf_error("'clnp' modifier applied to host");
1752 bpf_error("'stp' modifier applied to host");
1755 bpf_error("IPX host filtering not implemented");
1758 bpf_error("'netbeui' modifier applied to host");
1767 static struct block
*
1768 gen_host6(addr
, mask
, proto
, dir
)
1769 struct in6_addr
*addr
;
1770 struct in6_addr
*mask
;
1777 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1780 bpf_error("'ip' modifier applied to ip6 host");
1783 bpf_error("'rarp' modifier applied to ip6 host");
1786 bpf_error("'arp' modifier applied to ip6 host");
1789 bpf_error("'tcp' modifier applied to host");
1792 bpf_error("'udp' modifier applied to host");
1795 bpf_error("'icmp' modifier applied to host");
1798 bpf_error("'igmp' modifier applied to host");
1801 bpf_error("'igrp' modifier applied to host");
1804 bpf_error("'pim' modifier applied to host");
1807 bpf_error("'vrrp' modifier applied to host");
1810 bpf_error("ATALK host filtering not implemented");
1813 bpf_error("AARP host filtering not implemented");
1816 bpf_error("'decnet' modifier applied to ip6 host");
1819 bpf_error("SCA host filtering not implemented");
1822 bpf_error("LAT host filtering not implemented");
1825 bpf_error("MOPDL host filtering not implemented");
1828 bpf_error("MOPRC host filtering not implemented");
1831 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1832 off_nl
+ 8, off_nl
+ 24);
1835 bpf_error("'icmp6' modifier applied to host");
1838 bpf_error("'ah' modifier applied to host");
1841 bpf_error("'esp' modifier applied to host");
1844 bpf_error("ISO host filtering not implemented");
1847 bpf_error("'esis' modifier applied to host");
1850 bpf_error("'isis' modifier applied to host");
1853 bpf_error("'clnp' modifier applied to host");
1856 bpf_error("'stp' modifier applied to host");
1859 bpf_error("IPX host filtering not implemented");
1862 bpf_error("'netbeui' modifier applied to host");
1872 static struct block
*
1873 gen_gateway(eaddr
, alist
, proto
, dir
)
1874 const u_char
*eaddr
;
1875 bpf_u_int32
**alist
;
1879 struct block
*b0
, *b1
, *tmp
;
1882 bpf_error("direction applied to 'gateway'");
1889 if (linktype
== DLT_EN10MB
)
1890 b0
= gen_ehostop(eaddr
, Q_OR
);
1891 else if (linktype
== DLT_FDDI
)
1892 b0
= gen_fhostop(eaddr
, Q_OR
);
1893 else if (linktype
== DLT_IEEE802
)
1894 b0
= gen_thostop(eaddr
, Q_OR
);
1897 "'gateway' supported only on ethernet, FDDI or token ring");
1899 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1901 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1909 bpf_error("illegal modifier of 'gateway'");
1915 gen_proto_abbrev(proto
)
1926 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1928 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1934 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1936 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1942 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1945 #ifndef IPPROTO_IGMP
1946 #define IPPROTO_IGMP 2
1950 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1953 #ifndef IPPROTO_IGRP
1954 #define IPPROTO_IGRP 9
1957 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1961 #define IPPROTO_PIM 103
1965 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1967 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1972 #ifndef IPPROTO_VRRP
1973 #define IPPROTO_VRRP 112
1977 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
1981 b1
= gen_linktype(ETHERTYPE_IP
);
1985 b1
= gen_linktype(ETHERTYPE_ARP
);
1989 b1
= gen_linktype(ETHERTYPE_REVARP
);
1993 bpf_error("link layer applied in wrong context");
1996 b1
= gen_linktype(ETHERTYPE_ATALK
);
2000 b1
= gen_linktype(ETHERTYPE_AARP
);
2004 b1
= gen_linktype(ETHERTYPE_DN
);
2008 b1
= gen_linktype(ETHERTYPE_SCA
);
2012 b1
= gen_linktype(ETHERTYPE_LAT
);
2016 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2020 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2025 b1
= gen_linktype(ETHERTYPE_IPV6
);
2028 #ifndef IPPROTO_ICMPV6
2029 #define IPPROTO_ICMPV6 58
2032 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2037 #define IPPROTO_AH 51
2040 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2042 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2048 #define IPPROTO_ESP 50
2051 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2053 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2059 b1
= gen_linktype(LLCSAP_ISONS
);
2063 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2067 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2071 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2075 b1
= gen_linktype(LLCSAP_8021D
);
2079 b1
= gen_linktype(LLCSAP_IPX
);
2083 b1
= gen_linktype(LLCSAP_NETBEUI
);
2092 static struct block
*
2099 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2100 s
->s
.k
= off_nl
+ 6;
2101 b
= new_block(JMP(BPF_JSET
));
2109 static struct block
*
2110 gen_portatom(off
, v
)
2117 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2120 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2121 s
->next
->s
.k
= off_nl
+ off
;
2123 b
= new_block(JMP(BPF_JEQ
));
2131 static struct block
*
2132 gen_portatom6(off
, v
)
2136 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2141 gen_portop(port
, proto
, dir
)
2142 int port
, proto
, dir
;
2144 struct block
*b0
, *b1
, *tmp
;
2146 /* ip proto 'proto' */
2147 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2153 b1
= gen_portatom(0, (bpf_int32
)port
);
2157 b1
= gen_portatom(2, (bpf_int32
)port
);
2162 tmp
= gen_portatom(0, (bpf_int32
)port
);
2163 b1
= gen_portatom(2, (bpf_int32
)port
);
2168 tmp
= gen_portatom(0, (bpf_int32
)port
);
2169 b1
= gen_portatom(2, (bpf_int32
)port
);
2181 static struct block
*
2182 gen_port(port
, ip_proto
, dir
)
2187 struct block
*b0
, *b1
, *tmp
;
2189 /* ether proto ip */
2190 b0
= gen_linktype(ETHERTYPE_IP
);
2195 b1
= gen_portop(port
, ip_proto
, dir
);
2199 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2200 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2213 gen_portop6(port
, proto
, dir
)
2214 int port
, proto
, dir
;
2216 struct block
*b0
, *b1
, *tmp
;
2218 /* ip proto 'proto' */
2219 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2223 b1
= gen_portatom6(0, (bpf_int32
)port
);
2227 b1
= gen_portatom6(2, (bpf_int32
)port
);
2232 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2233 b1
= gen_portatom6(2, (bpf_int32
)port
);
2238 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2239 b1
= gen_portatom6(2, (bpf_int32
)port
);
2251 static struct block
*
2252 gen_port6(port
, ip_proto
, dir
)
2257 struct block
*b0
, *b1
, *tmp
;
2259 /* ether proto ip */
2260 b0
= gen_linktype(ETHERTYPE_IPV6
);
2265 b1
= gen_portop6(port
, ip_proto
, dir
);
2269 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2270 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2283 lookup_proto(name
, proto
)
2284 register const char *name
;
2293 v
= pcap_nametoproto(name
);
2294 if (v
== PROTO_UNDEF
)
2295 bpf_error("unknown ip proto '%s'", name
);
2299 /* XXX should look up h/w protocol type based on linktype */
2300 v
= pcap_nametoeproto(name
);
2301 if (v
== PROTO_UNDEF
)
2302 bpf_error("unknown ether proto '%s'", name
);
2306 if (strcmp(name
, "esis") == 0)
2308 else if (strcmp(name
, "isis") == 0)
2310 else if (strcmp(name
, "clnp") == 0)
2313 bpf_error("unknown osi proto '%s'", name
);
2333 static struct block
*
2334 gen_protochain(v
, proto
, dir
)
2339 #ifdef NO_PROTOCHAIN
2340 return gen_proto(v
, proto
, dir
);
2342 struct block
*b0
, *b
;
2343 struct slist
*s
[100];
2344 int fix2
, fix3
, fix4
, fix5
;
2345 int ahcheck
, again
, end
;
2347 int reg2
= alloc_reg();
2349 memset(s
, 0, sizeof(s
));
2350 fix2
= fix3
= fix4
= fix5
= 0;
2357 b0
= gen_protochain(v
, Q_IP
, dir
);
2358 b
= gen_protochain(v
, Q_IPV6
, dir
);
2362 bpf_error("bad protocol applied for 'protochain'");
2366 no_optimize
= 1; /*this code is not compatible with optimzer yet */
2369 * s[0] is a dummy entry to protect other BPF insn from damaged
2370 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
2371 * hard to find interdependency made by jump table fixup.
2374 s
[i
] = new_stmt(0); /*dummy*/
2379 b0
= gen_linktype(ETHERTYPE_IP
);
2382 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2383 s
[i
]->s
.k
= off_nl
+ 9;
2385 /* X = ip->ip_hl << 2 */
2386 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2392 b0
= gen_linktype(ETHERTYPE_IPV6
);
2394 /* A = ip6->ip_nxt */
2395 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2396 s
[i
]->s
.k
= off_nl
+ 6;
2398 /* X = sizeof(struct ip6_hdr) */
2399 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
2405 bpf_error("unsupported proto to gen_protochain");
2409 /* again: if (A == v) goto end; else fall through; */
2411 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2413 s
[i
]->s
.jt
= NULL
; /*later*/
2414 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2418 #ifndef IPPROTO_NONE
2419 #define IPPROTO_NONE 59
2421 /* if (A == IPPROTO_NONE) goto end */
2422 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2423 s
[i
]->s
.jt
= NULL
; /*later*/
2424 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2425 s
[i
]->s
.k
= IPPROTO_NONE
;
2426 s
[fix5
]->s
.jf
= s
[i
];
2431 if (proto
== Q_IPV6
) {
2432 int v6start
, v6end
, v6advance
, j
;
2435 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
2436 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2437 s
[i
]->s
.jt
= NULL
; /*later*/
2438 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2439 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
2440 s
[fix2
]->s
.jf
= s
[i
];
2442 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
2443 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2444 s
[i
]->s
.jt
= NULL
; /*later*/
2445 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2446 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
2448 /* if (A == IPPROTO_ROUTING) goto v6advance */
2449 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2450 s
[i
]->s
.jt
= NULL
; /*later*/
2451 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2452 s
[i
]->s
.k
= IPPROTO_ROUTING
;
2454 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
2455 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2456 s
[i
]->s
.jt
= NULL
; /*later*/
2457 s
[i
]->s
.jf
= NULL
; /*later*/
2458 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
2469 * X = X + (P[X + 1] + 1) * 8;
2472 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2474 /* A = P[X + packet head] */
2475 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2479 s
[i
] = new_stmt(BPF_ST
);
2483 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2486 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2490 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2492 /* A = P[X + packet head]; */
2493 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2497 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2501 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2505 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2508 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2512 /* goto again; (must use BPF_JA for backward jump) */
2513 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2514 s
[i
]->s
.k
= again
- i
- 1;
2515 s
[i
- 1]->s
.jf
= s
[i
];
2519 for (j
= v6start
; j
<= v6end
; j
++)
2520 s
[j
]->s
.jt
= s
[v6advance
];
2525 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2527 s
[fix2
]->s
.jf
= s
[i
];
2533 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2534 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2535 s
[i
]->s
.jt
= NULL
; /*later*/
2536 s
[i
]->s
.jf
= NULL
; /*later*/
2537 s
[i
]->s
.k
= IPPROTO_AH
;
2539 s
[fix3
]->s
.jf
= s
[ahcheck
];
2546 * X = X + (P[X + 1] + 2) * 4;
2549 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2551 /* A = P[X + packet head]; */
2552 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2556 s
[i
] = new_stmt(BPF_ST
);
2560 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2563 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2567 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2569 /* A = P[X + packet head] */
2570 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2574 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2578 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2582 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2585 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2589 /* goto again; (must use BPF_JA for backward jump) */
2590 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2591 s
[i
]->s
.k
= again
- i
- 1;
2596 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2598 s
[fix2
]->s
.jt
= s
[end
];
2599 s
[fix4
]->s
.jf
= s
[end
];
2600 s
[fix5
]->s
.jt
= s
[end
];
2607 for (i
= 0; i
< max
- 1; i
++)
2608 s
[i
]->next
= s
[i
+ 1];
2609 s
[max
- 1]->next
= NULL
;
2614 b
= new_block(JMP(BPF_JEQ
));
2615 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2625 static struct block
*
2626 gen_proto(v
, proto
, dir
)
2631 struct block
*b0
, *b1
;
2633 if (dir
!= Q_DEFAULT
)
2634 bpf_error("direction applied to 'proto'");
2639 b0
= gen_proto(v
, Q_IP
, dir
);
2640 b1
= gen_proto(v
, Q_IPV6
, dir
);
2647 b0
= gen_linktype(ETHERTYPE_IP
);
2649 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2651 b1
= gen_protochain(v
, Q_IP
);
2657 b0
= gen_linktype(LLCSAP_ISONS
);
2658 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2663 bpf_error("arp does not encapsulate another protocol");
2667 bpf_error("rarp does not encapsulate another protocol");
2671 bpf_error("atalk encapsulation is not specifiable");
2675 bpf_error("decnet encapsulation is not specifiable");
2679 bpf_error("sca does not encapsulate another protocol");
2683 bpf_error("lat does not encapsulate another protocol");
2687 bpf_error("moprc does not encapsulate another protocol");
2691 bpf_error("mopdl does not encapsulate another protocol");
2695 return gen_linktype(v
);
2698 bpf_error("'udp proto' is bogus");
2702 bpf_error("'tcp proto' is bogus");
2706 bpf_error("'icmp proto' is bogus");
2710 bpf_error("'igmp proto' is bogus");
2714 bpf_error("'igrp proto' is bogus");
2718 bpf_error("'pim proto' is bogus");
2722 bpf_error("'vrrp proto' is bogus");
2727 b0
= gen_linktype(ETHERTYPE_IPV6
);
2729 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2731 b1
= gen_protochain(v
, Q_IPV6
);
2737 bpf_error("'icmp6 proto' is bogus");
2741 bpf_error("'ah proto' is bogus");
2744 bpf_error("'ah proto' is bogus");
2747 bpf_error("'stp proto' is bogus");
2750 bpf_error("'ipx proto' is bogus");
2753 bpf_error("'netbeui proto' is bogus");
2764 register const char *name
;
2767 int proto
= q
.proto
;
2771 bpf_u_int32 mask
, addr
;
2773 bpf_u_int32
**alist
;
2776 struct sockaddr_in
*sin
;
2777 struct sockaddr_in6
*sin6
;
2778 struct addrinfo
*res
, *res0
;
2779 struct in6_addr mask128
;
2781 struct block
*b
, *tmp
;
2782 int port
, real_proto
;
2787 addr
= pcap_nametonetaddr(name
);
2789 bpf_error("unknown network '%s'", name
);
2790 /* Left justify network addr and calculate its network mask */
2792 while (addr
&& (addr
& 0xff000000) == 0) {
2796 return gen_host(addr
, mask
, proto
, dir
);
2800 if (proto
== Q_LINK
) {
2804 eaddr
= pcap_ether_hostton(name
);
2807 "unknown ether host '%s'", name
);
2808 return gen_ehostop(eaddr
, dir
);
2811 eaddr
= pcap_ether_hostton(name
);
2814 "unknown FDDI host '%s'", name
);
2815 return gen_fhostop(eaddr
, dir
);
2818 eaddr
= pcap_ether_hostton(name
);
2821 "unknown token ring host '%s'", name
);
2822 return gen_thostop(eaddr
, dir
);
2826 "only ethernet/FDDI/token ring supports link-level host name");
2829 } else if (proto
== Q_DECNET
) {
2830 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2832 * I don't think DECNET hosts can be multihomed, so
2833 * there is no need to build up a list of addresses
2835 return (gen_host(dn_addr
, 0, proto
, dir
));
2838 alist
= pcap_nametoaddr(name
);
2839 if (alist
== NULL
|| *alist
== NULL
)
2840 bpf_error("unknown host '%s'", name
);
2842 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2844 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2846 tmp
= gen_host(**alist
++, 0xffffffff,
2853 memset(&mask128
, 0xff, sizeof(mask128
));
2854 res0
= res
= pcap_nametoaddrinfo(name
);
2856 bpf_error("unknown host '%s'", name
);
2858 tproto
= tproto6
= proto
;
2859 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2863 for (res
= res0
; res
; res
= res
->ai_next
) {
2864 switch (res
->ai_family
) {
2866 if (tproto
== Q_IPV6
)
2869 sin
= (struct sockaddr_in
*)
2871 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2872 0xffffffff, tproto
, dir
);
2875 if (tproto6
== Q_IP
)
2878 sin6
= (struct sockaddr_in6
*)
2880 tmp
= gen_host6(&sin6
->sin6_addr
,
2881 &mask128
, tproto6
, dir
);
2892 bpf_error("unknown host '%s'%s", name
,
2893 (proto
== Q_DEFAULT
)
2895 : " for specified address family");
2902 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2903 bpf_error("illegal qualifier of 'port'");
2904 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2905 bpf_error("unknown port '%s'", name
);
2906 if (proto
== Q_UDP
) {
2907 if (real_proto
== IPPROTO_TCP
)
2908 bpf_error("port '%s' is tcp", name
);
2910 /* override PROTO_UNDEF */
2911 real_proto
= IPPROTO_UDP
;
2913 if (proto
== Q_TCP
) {
2914 if (real_proto
== IPPROTO_UDP
)
2915 bpf_error("port '%s' is udp", name
);
2917 /* override PROTO_UNDEF */
2918 real_proto
= IPPROTO_TCP
;
2921 return gen_port(port
, real_proto
, dir
);
2925 b
= gen_port(port
, real_proto
, dir
);
2926 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2933 eaddr
= pcap_ether_hostton(name
);
2935 bpf_error("unknown ether host: %s", name
);
2937 alist
= pcap_nametoaddr(name
);
2938 if (alist
== NULL
|| *alist
== NULL
)
2939 bpf_error("unknown host '%s'", name
);
2940 return gen_gateway(eaddr
, alist
, proto
, dir
);
2942 bpf_error("'gateway' not supported in this configuration");
2946 real_proto
= lookup_proto(name
, proto
);
2947 if (real_proto
>= 0)
2948 return gen_proto(real_proto
, proto
, dir
);
2950 bpf_error("unknown protocol: %s", name
);
2953 real_proto
= lookup_proto(name
, proto
);
2954 if (real_proto
>= 0)
2955 return gen_protochain(real_proto
, proto
, dir
);
2957 bpf_error("unknown protocol: %s", name
);
2969 gen_mcode(s1
, s2
, masklen
, q
)
2970 register const char *s1
, *s2
;
2971 register int masklen
;
2974 register int nlen
, mlen
;
2977 nlen
= __pcap_atoin(s1
, &n
);
2978 /* Promote short ipaddr */
2982 mlen
= __pcap_atoin(s2
, &m
);
2983 /* Promote short ipaddr */
2986 bpf_error("non-network bits set in \"%s mask %s\"",
2989 /* Convert mask len to mask */
2991 bpf_error("mask length must be <= 32");
2992 m
= 0xffffffff << (32 - masklen
);
2994 bpf_error("non-network bits set in \"%s/%d\"",
3001 return gen_host(n
, m
, q
.proto
, q
.dir
);
3004 bpf_error("Mask syntax for networks only");
3011 register const char *s
;
3016 int proto
= q
.proto
;
3022 else if (q
.proto
== Q_DECNET
)
3023 vlen
= __pcap_atodn(s
, &v
);
3025 vlen
= __pcap_atoin(s
, &v
);
3032 if (proto
== Q_DECNET
)
3033 return gen_host(v
, 0, proto
, dir
);
3034 else if (proto
== Q_LINK
) {
3035 bpf_error("illegal link layer address");
3038 if (s
== NULL
&& q
.addr
== Q_NET
) {
3039 /* Promote short net number */
3040 while (v
&& (v
& 0xff000000) == 0) {
3045 /* Promote short ipaddr */
3049 return gen_host(v
, mask
, proto
, dir
);
3054 proto
= IPPROTO_UDP
;
3055 else if (proto
== Q_TCP
)
3056 proto
= IPPROTO_TCP
;
3057 else if (proto
== Q_DEFAULT
)
3058 proto
= PROTO_UNDEF
;
3060 bpf_error("illegal qualifier of 'port'");
3063 return gen_port((int)v
, proto
, dir
);
3067 b
= gen_port((int)v
, proto
, dir
);
3068 gen_or(gen_port6((int)v
, proto
, dir
), b
);
3074 bpf_error("'gateway' requires a name");
3078 return gen_proto((int)v
, proto
, dir
);
3081 return gen_protochain((int)v
, proto
, dir
);
3096 gen_mcode6(s1
, s2
, masklen
, q
)
3097 register const char *s1
, *s2
;
3098 register int masklen
;
3101 struct addrinfo
*res
;
3102 struct in6_addr
*addr
;
3103 struct in6_addr mask
;
3108 bpf_error("no mask %s supported", s2
);
3110 res
= pcap_nametoaddrinfo(s1
);
3112 bpf_error("invalid ip6 address %s", s1
);
3114 bpf_error("%s resolved to multiple address", s1
);
3115 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3117 if (sizeof(mask
) * 8 < masklen
)
3118 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3119 memset(&mask
, 0xff, masklen
/ 8);
3121 mask
.s6_addr
[masklen
/ 8] =
3122 (0xff << (8 - masklen
% 8)) & 0xff;
3125 a
= (u_int32_t
*)addr
;
3126 m
= (u_int32_t
*)&mask
;
3127 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3128 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3129 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
3137 bpf_error("Mask syntax for networks only");
3141 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
3146 bpf_error("invalid qualifier against IPv6 address");
3154 register const u_char
*eaddr
;
3157 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3158 if (linktype
== DLT_EN10MB
)
3159 return gen_ehostop(eaddr
, (int)q
.dir
);
3160 if (linktype
== DLT_FDDI
)
3161 return gen_fhostop(eaddr
, (int)q
.dir
);
3162 if (linktype
== DLT_IEEE802
)
3163 return gen_thostop(eaddr
, (int)q
.dir
);
3164 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
3166 bpf_error("ethernet address used in non-ether expression");
3172 struct slist
*s0
, *s1
;
3175 * This is definitely not the best way to do this, but the
3176 * lists will rarely get long.
3183 static struct slist
*
3189 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3194 static struct slist
*
3200 s
= new_stmt(BPF_LD
|BPF_MEM
);
3206 gen_load(proto
, index
, size
)
3211 struct slist
*s
, *tmp
;
3213 int regno
= alloc_reg();
3215 free_reg(index
->regno
);
3219 bpf_error("data size must be 1, 2, or 4");
3235 bpf_error("unsupported index operation");
3238 s
= xfer_to_x(index
);
3239 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3241 sappend(index
->s
, s
);
3256 /* XXX Note that we assume a fixed link header here. */
3257 s
= xfer_to_x(index
);
3258 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3261 sappend(index
->s
, s
);
3263 b
= gen_proto_abbrev(proto
);
3265 gen_and(index
->b
, b
);
3276 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3278 sappend(s
, xfer_to_a(index
));
3279 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3280 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3281 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3283 sappend(index
->s
, s
);
3285 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
3287 gen_and(index
->b
, b
);
3289 gen_and(gen_proto_abbrev(Q_IP
), b
);
3295 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
3299 index
->regno
= regno
;
3300 s
= new_stmt(BPF_ST
);
3302 sappend(index
->s
, s
);
3308 gen_relation(code
, a0
, a1
, reversed
)
3310 struct arth
*a0
, *a1
;
3313 struct slist
*s0
, *s1
, *s2
;
3314 struct block
*b
, *tmp
;
3318 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
3319 b
= new_block(JMP(code
));
3320 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
3321 reversed
= !reversed
;
3322 b
->s
.k
= 0x80000000;
3330 sappend(a0
->s
, a1
->s
);
3334 free_reg(a0
->regno
);
3335 free_reg(a1
->regno
);
3337 /* 'and' together protocol checks */
3340 gen_and(a0
->b
, tmp
= a1
->b
);
3356 int regno
= alloc_reg();
3357 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
3360 s
= new_stmt(BPF_LD
|BPF_LEN
);
3361 s
->next
= new_stmt(BPF_ST
);
3362 s
->next
->s
.k
= regno
;
3377 a
= (struct arth
*)newchunk(sizeof(*a
));
3381 s
= new_stmt(BPF_LD
|BPF_IMM
);
3383 s
->next
= new_stmt(BPF_ST
);
3399 s
= new_stmt(BPF_ALU
|BPF_NEG
);
3402 s
= new_stmt(BPF_ST
);
3410 gen_arth(code
, a0
, a1
)
3412 struct arth
*a0
, *a1
;
3414 struct slist
*s0
, *s1
, *s2
;
3418 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
3423 sappend(a0
->s
, a1
->s
);
3425 free_reg(a1
->regno
);
3427 s0
= new_stmt(BPF_ST
);
3428 a0
->regno
= s0
->s
.k
= alloc_reg();
3435 * Here we handle simple allocation of the scratch registers.
3436 * If too many registers are alloc'd, the allocator punts.
3438 static int regused
[BPF_MEMWORDS
];
3442 * Return the next free register.
3447 int n
= BPF_MEMWORDS
;
3450 if (regused
[curreg
])
3451 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
3453 regused
[curreg
] = 1;
3457 bpf_error("too many registers needed to evaluate expression");
3462 * Return a register to the table so it can
3472 static struct block
*
3479 s
= new_stmt(BPF_LD
|BPF_LEN
);
3480 b
= new_block(JMP(jmp
));
3491 return gen_len(BPF_JGE
, n
);
3495 * Actually, this is less than or equal.
3503 b
= gen_len(BPF_JGT
, n
);
3510 gen_byteop(op
, idx
, val
)
3521 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3524 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3525 b
->s
.code
= JMP(BPF_JGE
);
3530 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3531 b
->s
.code
= JMP(BPF_JGT
);
3535 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3539 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3543 b
= new_block(JMP(BPF_JEQ
));
3551 gen_broadcast(proto
)
3554 bpf_u_int32 hostmask
;
3555 struct block
*b0
, *b1
, *b2
;
3556 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3562 if (linktype
== DLT_EN10MB
)
3563 return gen_ehostop(ebroadcast
, Q_DST
);
3564 if (linktype
== DLT_FDDI
)
3565 return gen_fhostop(ebroadcast
, Q_DST
);
3566 if (linktype
== DLT_IEEE802
)
3567 return gen_thostop(ebroadcast
, Q_DST
);
3568 bpf_error("not a broadcast link");
3572 b0
= gen_linktype(ETHERTYPE_IP
);
3573 hostmask
= ~netmask
;
3574 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3575 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3576 (bpf_int32
)(~0 & hostmask
), hostmask
);
3581 bpf_error("only ether/ip broadcast filters supported");
3585 gen_multicast(proto
)
3588 register struct block
*b0
, *b1
;
3589 register struct slist
*s
;
3595 if (linktype
== DLT_EN10MB
) {
3596 /* ether[0] & 1 != 0 */
3597 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3599 b0
= new_block(JMP(BPF_JSET
));
3605 if (linktype
== DLT_FDDI
) {
3606 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3607 /* fddi[1] & 1 != 0 */
3608 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3610 b0
= new_block(JMP(BPF_JSET
));
3616 /* TODO - check how token ring handles multicast */
3617 /* if (linktype == DLT_IEEE802) ... */
3619 /* Link not known to support multicasts */
3623 b0
= gen_linktype(ETHERTYPE_IP
);
3624 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3625 b1
->s
.code
= JMP(BPF_JGE
);
3631 b0
= gen_linktype(ETHERTYPE_IPV6
);
3632 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3637 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3641 * generate command for inbound/outbound. It's here so we can
3642 * make it link-type specific. 'dir' = 0 implies "inbound",
3643 * = 1 implies "outbound".
3649 register struct block
*b0
;
3652 * Only some data link types support inbound/outbound qualifiers.
3657 b0
= gen_relation(BPF_JEQ
,
3658 gen_load(Q_LINK
, gen_loadi(0), 1),
3664 bpf_error("inbound/outbound not supported on linktype %d\n",
3673 * support IEEE 802.1Q VLAN trunk over ethernet
3679 static u_int orig_linktype
= -1, orig_nl
= -1;
3683 * Change the offsets to point to the type and data fields within
3684 * the VLAN packet. This is somewhat of a kludge.
3686 if (orig_nl
== (u_int
)-1) {
3687 orig_linktype
= off_linktype
; /* save original values */
3698 bpf_error("no VLAN support for data link type %d",
3704 /* check for VLAN */
3705 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3707 /* If a specific VLAN is requested, check VLAN id */
3708 if (vlan_num
>= 0) {
3711 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);