]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
465cba1fbcaed4f47debf589a1f02265a4ea8061
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.142 2001-01-14 05:30:07 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"
57 #include <pcap-namedb.h>
60 #include <sys/socket.h>
65 * Note that these fit in one byte, and are thus less than 1500, and
66 * are thus distinguishable from ETHERTYPE_ values, so we can use them
67 * as protocol types values.
69 #define LLC_SNAP_LSAP 0xaa
70 #define LLC_ISO_LSAP 0xfe
71 #define LLC_STP_LSAP 0x42
75 #ifdef HAVE_OS_PROTO_H
79 #define JMP(c) ((c)|BPF_JMP|BPF_K)
82 static jmp_buf top_ctx
;
83 static pcap_t
*bpf_pcap
;
87 int pcap_fddipad
= PCAP_FDDIPAD
;
94 bpf_error(const char *fmt
, ...)
100 if (bpf_pcap
!= NULL
)
101 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
108 static void init_linktype(int);
110 static int alloc_reg(void);
111 static void free_reg(int);
113 static struct block
*root
;
116 * We divy out chunks of memory rather than call malloc each time so
117 * we don't have to worry about leaking memory. It's probably
118 * not a big deal if all this memory was wasted but it this ever
119 * goes into a library that would probably not be a good idea.
122 #define CHUNK0SIZE 1024
128 static struct chunk chunks
[NCHUNKS
];
129 static int cur_chunk
;
131 static void *newchunk(u_int
);
132 static void freechunks(void);
133 static inline struct block
*new_block(int);
134 static inline struct slist
*new_stmt(int);
135 static struct block
*gen_retblk(int);
136 static inline void syntax(void);
138 static void backpatch(struct block
*, struct block
*);
139 static void merge(struct block
*, struct block
*);
140 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
141 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
142 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
143 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
144 static struct block
*gen_uncond(int);
145 static inline struct block
*gen_true(void);
146 static inline struct block
*gen_false(void);
147 static struct block
*gen_linktype(int);
148 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
149 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
151 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_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
;
566 * SLIP doesn't have a link level type. The 16 byte
567 * header is hacked into our SLIP driver.
574 /* XXX this may be the same as the DLT_PPP_BSDOS case */
600 * FDDI doesn't really have a link-level type field.
601 * We set "off_linktype" to the offset of the LLC header.
603 * To check for Ethernet types, we assume that SSAP = SNAP
604 * is being used and pick out the encapsulated Ethernet type.
605 * XXX - should we generate code to check for SNAP?
609 off_linktype
+= pcap_fddipad
;
613 off_nl
+= pcap_fddipad
;
619 * Token Ring doesn't really have a link-level type field.
620 * We set "off_linktype" to the offset of the LLC header.
622 * To check for Ethernet types, we assume that SSAP = SNAP
623 * is being used and pick out the encapsulated Ethernet type.
624 * XXX - should we generate code to check for SNAP?
626 * XXX - the header is actually variable-length.
627 * Some various Linux patched versions gave 38
628 * as "off_linktype" and 40 as "off_nl"; however,
629 * if a token ring packet has *no* routing
630 * information, i.e. is not source-routed, the correct
631 * values are 20 and 22, as they are in the vanilla code.
633 * A packet is source-routed iff the uppermost bit
634 * of the first byte of the source address, at an
635 * offset of 8, has the uppermost bit set. If the
636 * packet is source-routed, the total number of bytes
637 * of routing information is 2 plus bits 0x1F00 of
638 * the 16-bit value at an offset of 14 (shifted right
639 * 8 - figure out which byte that is).
645 case DLT_ATM_RFC1483
:
647 * assume routed, non-ISO PDUs
648 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
659 case DLT_ATM_CLIP
: /* Linux ATM defines this */
664 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
669 bpf_error("unknown data link type %d", linktype
);
673 static struct block
*
680 s
= new_stmt(BPF_LD
|BPF_IMM
);
682 b
= new_block(JMP(BPF_JEQ
));
688 static inline struct block
*
691 return gen_uncond(1);
694 static inline struct block
*
697 return gen_uncond(0);
701 * Byte-swap a 32-bit number.
702 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
703 * big-endian platforms.)
705 #define SWAPLONG(y) \
706 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
708 static struct block
*
712 struct block
*b0
, *b1
;
721 * OSI protocols always use 802.2 encapsulation.
722 * XXX - should we check both the DSAP and the
723 * LSAP, like this, or should we check just the
726 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
728 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
729 ((LLC_ISO_LSAP
<< 8) | LLC_ISO_LSAP
));
733 case ETHERTYPE_ATALK
:
736 * EtherTalk (AppleTalk protocols on Ethernet link
737 * layer) may use 802.2 encapsulation.
741 * Check for 802.2 encapsulation (EtherTalk phase 2?);
742 * we check for an Ethernet type field less than
743 * 1500, which means it's an 802.3 length field.
745 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
749 * 802.2-encapsulated ETHERTYPE_ATALK packets are
750 * SNAP packets with an organization code of
751 * 0x080007 (Apple, for Appletalk) and a protocol
752 * type of ETHERTYPE_ATALK (Appletalk).
754 * 802.2-encapsulated ETHERTYPE_AARP packets are
755 * SNAP packets with an organization code of
756 * 0x000000 (encapsulated Ethernet) and a protocol
757 * type of ETHERTYPE_AARP (Appletalk ARP).
759 if (proto
== ETHERTYPE_ATALK
)
760 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
761 else /* proto == ETHERTYPE_AARP */
762 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
766 * Check for Ethernet encapsulation (Ethertalk
767 * phase 1?); we just check for the Ethernet
770 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
776 if (proto
<= ETHERMTU
) {
778 * This is an LLC SAP value, so the frames
779 * that match would be 802.2 frames.
780 * Check that the frame is an 802.2 frame
781 * (i.e., that the length/type field is
782 * a length field, <= ETHERMTU) and
783 * then check the DSAP.
785 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
787 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
793 * This is an Ethernet type, so compare
794 * the length/type field with it (if
795 * the frame is an 802.2 frame, the length
796 * field will be <= ETHERMTU, and, as
797 * "proto" is > ETHERMTU, this test
798 * will fail and the frame won't match,
799 * which is what we want).
801 return gen_cmp(off_linktype
, BPF_H
,
809 case DLT_ATM_RFC1483
:
812 * XXX - handle token-ring variable-length header.
817 return gen_cmp(off_linktype
, BPF_H
, (long)
818 ((LLC_ISO_LSAP
<< 8) | LLC_ISO_LSAP
));
820 case ETHERTYPE_ATALK
:
822 * 802.2-encapsulated ETHERTYPE_ATALK packets are
823 * SNAP packets with an organization code of
824 * 0x080007 (Apple, for Appletalk) and a protocol
825 * type of ETHERTYPE_ATALK (Appletalk).
827 * XXX - check for an organization code of
828 * encapsulated Ethernet as well?
830 return gen_snap(0x080007, ETHERTYPE_ATALK
,
835 if (proto
<= ETHERMTU
) {
837 * This is an LLC SAP value, so check
840 return gen_cmp(off_linktype
, BPF_B
,
844 * This is an Ethernet type; we assume
845 * that it's unlikely that it'll
846 * appear in the right place at random,
847 * and therefore check only the
848 * location that would hold the Ethernet
849 * type in a SNAP frame with an organization
850 * code of 0x000000 (encapsulated Ethernet).
852 * XXX - if we were to check for the SNAP DSAP
853 * and LSAP, as per XXX, and were also to check
854 * for an organization code of 0x000000
855 * (encapsulated Ethernet), we'd do
857 * return gen_snap(0x000000, proto,
860 * here; for now, we don't, as per the above.
861 * I don't know whether it's worth the
862 * extra CPU time to do the right check
865 return gen_cmp(off_linktype
+6, BPF_H
,
876 * OSI protocols always use 802.2 encapsulation.
877 * XXX - should we check both the DSAP and the
878 * LSAP, like this, or should we check just the
881 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
883 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
884 ((LLC_ISO_LSAP
<< 8) | LLC_ISO_LSAP
));
888 case ETHERTYPE_ATALK
:
891 * EtherTalk (AppleTalk protocols on Ethernet link
892 * layer) may use 802.2 encapsulation.
896 * Check for 802.2 encapsulation (EtherTalk phase 2?);
897 * we check for the 802.2 protocol type in the
898 * "Ethernet type" field.
900 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
903 * 802.2-encapsulated ETHERTYPE_ATALK packets are
904 * SNAP packets with an organization code of
905 * 0x080007 (Apple, for Appletalk) and a protocol
906 * type of ETHERTYPE_ATALK (Appletalk).
908 * 802.2-encapsulated ETHERTYPE_AARP packets are
909 * SNAP packets with an organization code of
910 * 0x000000 (encapsulated Ethernet) and a protocol
911 * type of ETHERTYPE_AARP (Appletalk ARP).
913 if (proto
== ETHERTYPE_ATALK
)
914 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
916 else /* proto == ETHERTYPE_AARP */
917 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
922 * Check for Ethernet encapsulation (Ethertalk
923 * phase 1?); we just check for the Ethernet
926 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
932 if (proto
<= ETHERMTU
) {
934 * This is an LLC SAP value, so the frames
935 * that match would be 802.2 frames.
936 * Check for the 802.2 protocol type
937 * in the "Ethernet type" field, and
938 * then check the DSAP.
940 b0
= gen_cmp(off_linktype
, BPF_H
,
942 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
948 * This is an Ethernet type, so compare
949 * the length/type field with it (if
950 * the frame is an 802.2 frame, the length
951 * field will be <= ETHERMTU, and, as
952 * "proto" is > ETHERMTU, this test
953 * will fail and the frame won't match,
954 * which is what we want).
956 return gen_cmp(off_linktype
, BPF_H
,
966 * These types don't provide any type field; packets
969 * XXX - for IPv4, check for a version number of 4, and,
970 * for IPv6, check for a version number of 6?
978 return gen_true(); /* always true */
981 return gen_false(); /* always false */
988 * We use Ethernet protocol types inside libpcap;
989 * map them to the corresponding PPP protocol types.
994 proto
= PPP_IP
; /* XXX was 0x21 */
1007 case ETHERTYPE_ATALK
:
1023 * We use Ethernet protocol types inside libpcap;
1024 * map them to the corresponding PPP protocol types.
1029 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1030 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1032 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1037 case ETHERTYPE_IPV6
:
1047 case ETHERTYPE_ATALK
:
1064 * For DLT_NULL, the link-layer header is a 32-bit
1065 * word containing an AF_ value in *host* byte order.
1067 * In addition, if we're reading a saved capture file,
1068 * the host byte order in the capture may not be the
1069 * same as the host byte order on this machine.
1071 * For DLT_LOOP, the link-layer header is a 32-bit
1072 * word containing an AF_ value in *network* byte order.
1074 * XXX - AF_ values may, unfortunately, be platform-
1075 * dependent; for example, FreeBSD's AF_INET6 is 24
1076 * whilst NetBSD's and OpenBSD's is 26.
1078 * This means that, when reading a capture file, just
1079 * checking for our AF_INET6 value won't work if the
1080 * capture file came from another OS.
1089 case ETHERTYPE_IPV6
:
1096 * Not a type on which we support filtering.
1097 * XXX - support those that have AF_ values
1098 * #defined on this platform, at least?
1103 if (linktype
== DLT_NULL
) {
1105 * The AF_ value is in host byte order, but
1106 * the BPF interpreter will convert it to
1107 * network byte order.
1109 * If this is a save file, and it's from a
1110 * machine with the opposite byte order to
1111 * ours, we byte-swap the AF_ value.
1113 * Then we run it through "htonl()", and
1114 * generate code to compare against the result.
1116 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1117 bpf_pcap
->sf
.swapped
)
1118 proto
= SWAPLONG(proto
);
1119 proto
= htonl(proto
);
1121 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1125 * All the types that have no encapsulation should either be
1126 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1127 * all packets are IP packets, or should be handled in some
1128 * special case, if none of them are (if some are and some
1129 * aren't, the lack of encapsulation is a problem, as we'd
1130 * have to find some other way of determining the packet type).
1132 * Therefore, if "off_linktype" is -1, there's an error.
1134 if (off_linktype
== -1)
1138 * Any type not handled above should always have an Ethernet
1139 * type at an offset of "off_linktype".
1141 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1145 * Check for an LLC SNAP packet with a given organization code and
1146 * protocol type; we check the entire contents of the 802.2 LLC and
1147 * snap headers, checking for DSAP and SSAP of SNAP and a control
1148 * field of 0x03 in the LLC header, and for the specified organization
1149 * code and protocol type in the SNAP header.
1151 static struct block
*
1152 gen_snap(orgcode
, ptype
, offset
)
1153 bpf_u_int32 orgcode
;
1157 u_char snapblock
[8];
1159 snapblock
[0] = LLC_SNAP_LSAP
; /* DSAP = SNAP */
1160 snapblock
[1] = LLC_SNAP_LSAP
; /* SSAP = SNAP */
1161 snapblock
[2] = 0x03; /* control = UI */
1162 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1163 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1164 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1165 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1166 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1167 return gen_bcmp(offset
, 8, snapblock
);
1170 static struct block
*
1171 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1175 u_int src_off
, dst_off
;
1177 struct block
*b0
, *b1
;
1191 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1192 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1198 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1199 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1206 b0
= gen_linktype(proto
);
1207 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1213 static struct block
*
1214 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1215 struct in6_addr
*addr
;
1216 struct in6_addr
*mask
;
1218 u_int src_off
, dst_off
;
1220 struct block
*b0
, *b1
;
1235 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1236 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1242 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1243 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1250 /* this order is important */
1251 a
= (u_int32_t
*)addr
;
1252 m
= (u_int32_t
*)mask
;
1253 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1254 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1256 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1258 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1260 b0
= gen_linktype(proto
);
1266 static struct block
*
1267 gen_ehostop(eaddr
, dir
)
1268 register const u_char
*eaddr
;
1271 register struct block
*b0
, *b1
;
1275 return gen_bcmp(6, 6, eaddr
);
1278 return gen_bcmp(0, 6, eaddr
);
1281 b0
= gen_ehostop(eaddr
, Q_SRC
);
1282 b1
= gen_ehostop(eaddr
, Q_DST
);
1288 b0
= gen_ehostop(eaddr
, Q_SRC
);
1289 b1
= gen_ehostop(eaddr
, Q_DST
);
1298 * Like gen_ehostop, but for DLT_FDDI
1300 static struct block
*
1301 gen_fhostop(eaddr
, dir
)
1302 register const u_char
*eaddr
;
1305 struct block
*b0
, *b1
;
1310 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1312 return gen_bcmp(6 + 1, 6, eaddr
);
1317 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1319 return gen_bcmp(0 + 1, 6, eaddr
);
1323 b0
= gen_fhostop(eaddr
, Q_SRC
);
1324 b1
= gen_fhostop(eaddr
, Q_DST
);
1330 b0
= gen_fhostop(eaddr
, Q_SRC
);
1331 b1
= gen_fhostop(eaddr
, Q_DST
);
1340 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1342 static struct block
*
1343 gen_thostop(eaddr
, dir
)
1344 register const u_char
*eaddr
;
1347 register struct block
*b0
, *b1
;
1351 return gen_bcmp(8, 6, eaddr
);
1354 return gen_bcmp(2, 6, eaddr
);
1357 b0
= gen_thostop(eaddr
, Q_SRC
);
1358 b1
= gen_thostop(eaddr
, Q_DST
);
1364 b0
= gen_thostop(eaddr
, Q_SRC
);
1365 b1
= gen_thostop(eaddr
, Q_DST
);
1374 * This is quite tricky because there may be pad bytes in front of the
1375 * DECNET header, and then there are two possible data packet formats that
1376 * carry both src and dst addresses, plus 5 packet types in a format that
1377 * carries only the src node, plus 2 types that use a different format and
1378 * also carry just the src node.
1382 * Instead of doing those all right, we just look for data packets with
1383 * 0 or 1 bytes of padding. If you want to look at other packets, that
1384 * will require a lot more hacking.
1386 * To add support for filtering on DECNET "areas" (network numbers)
1387 * one would want to add a "mask" argument to this routine. That would
1388 * make the filter even more inefficient, although one could be clever
1389 * and not generate masking instructions if the mask is 0xFFFF.
1391 static struct block
*
1392 gen_dnhostop(addr
, dir
, base_off
)
1397 struct block
*b0
, *b1
, *b2
, *tmp
;
1398 u_int offset_lh
; /* offset if long header is received */
1399 u_int offset_sh
; /* offset if short header is received */
1404 offset_sh
= 1; /* follows flags */
1405 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1409 offset_sh
= 3; /* follows flags, dstnode */
1410 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1414 /* Inefficient because we do our Calvinball dance twice */
1415 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1416 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1422 /* Inefficient because we do our Calvinball dance twice */
1423 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1424 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1429 bpf_error("ISO host filtering not implemented");
1434 b0
= gen_linktype(ETHERTYPE_DN
);
1435 /* Check for pad = 1, long header case */
1436 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1437 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1438 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1439 BPF_H
, (bpf_int32
)ntohs(addr
));
1441 /* Check for pad = 0, long header case */
1442 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1443 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1446 /* Check for pad = 1, short header case */
1447 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1448 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1449 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1450 BPF_H
, (bpf_int32
)ntohs(addr
));
1453 /* Check for pad = 0, short header case */
1454 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1455 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1459 /* Combine with test for linktype */
1464 static struct block
*
1465 gen_host(addr
, mask
, proto
, dir
)
1471 struct block
*b0
, *b1
;
1476 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1477 if (off_linktype
!= -1) {
1478 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1480 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1486 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1487 off_nl
+ 12, off_nl
+ 16);
1490 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1491 off_nl
+ 14, off_nl
+ 24);
1494 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1495 off_nl
+ 14, off_nl
+ 24);
1498 bpf_error("'tcp' modifier applied to host");
1501 bpf_error("'udp' modifier applied to host");
1504 bpf_error("'icmp' modifier applied to host");
1507 bpf_error("'igmp' modifier applied to host");
1510 bpf_error("'igrp' modifier applied to host");
1513 bpf_error("'pim' modifier applied to host");
1516 bpf_error("ATALK host filtering not implemented");
1519 bpf_error("AARP host filtering not implemented");
1522 return gen_dnhostop(addr
, dir
, off_nl
);
1525 bpf_error("SCA host filtering not implemented");
1528 bpf_error("LAT host filtering not implemented");
1531 bpf_error("MOPDL host filtering not implemented");
1534 bpf_error("MOPRC host filtering not implemented");
1538 bpf_error("'ip6' modifier applied to ip host");
1541 bpf_error("'icmp6' modifier applied to host");
1545 bpf_error("'ah' modifier applied to host");
1548 bpf_error("'esp' modifier applied to host");
1551 bpf_error("ISO host filtering not implemented");
1554 bpf_error("'esis' modifier applied to host");
1557 bpf_error("'isis' modifier applied to host");
1560 bpf_error("'clnp' modifier applied to host");
1563 bpf_error("'stp' modifier applied to host");
1572 static struct block
*
1573 gen_host6(addr
, mask
, proto
, dir
)
1574 struct in6_addr
*addr
;
1575 struct in6_addr
*mask
;
1582 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1585 bpf_error("'ip' modifier applied to ip6 host");
1588 bpf_error("'rarp' modifier applied to ip6 host");
1591 bpf_error("'arp' modifier applied to ip6 host");
1594 bpf_error("'tcp' modifier applied to host");
1597 bpf_error("'udp' modifier applied to host");
1600 bpf_error("'icmp' modifier applied to host");
1603 bpf_error("'igmp' modifier applied to host");
1606 bpf_error("'igrp' modifier applied to host");
1609 bpf_error("'pim' modifier applied to host");
1612 bpf_error("ATALK host filtering not implemented");
1615 bpf_error("AARP host filtering not implemented");
1618 bpf_error("'decnet' modifier applied to ip6 host");
1621 bpf_error("SCA host filtering not implemented");
1624 bpf_error("LAT host filtering not implemented");
1627 bpf_error("MOPDL host filtering not implemented");
1630 bpf_error("MOPRC host filtering not implemented");
1633 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1634 off_nl
+ 8, off_nl
+ 24);
1637 bpf_error("'icmp6' modifier applied to host");
1640 bpf_error("'ah' modifier applied to host");
1643 bpf_error("'esp' modifier applied to host");
1646 bpf_error("ISO host filtering not implemented");
1649 bpf_error("'esis' modifier applied to host");
1652 bpf_error("'isis' modifier applied to host");
1655 bpf_error("'clnp' modifier applied to host");
1658 bpf_error("'stp' modifier applied to host");
1668 static struct block
*
1669 gen_gateway(eaddr
, alist
, proto
, dir
)
1670 const u_char
*eaddr
;
1671 bpf_u_int32
**alist
;
1675 struct block
*b0
, *b1
, *tmp
;
1678 bpf_error("direction applied to 'gateway'");
1685 if (linktype
== DLT_EN10MB
)
1686 b0
= gen_ehostop(eaddr
, Q_OR
);
1687 else if (linktype
== DLT_FDDI
)
1688 b0
= gen_fhostop(eaddr
, Q_OR
);
1689 else if (linktype
== DLT_IEEE802
)
1690 b0
= gen_thostop(eaddr
, Q_OR
);
1693 "'gateway' supported only on ethernet, FDDI or token ring");
1695 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1697 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1705 bpf_error("illegal modifier of 'gateway'");
1711 gen_proto_abbrev(proto
)
1722 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1724 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1730 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1732 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1738 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1741 #ifndef IPPROTO_IGMP
1742 #define IPPROTO_IGMP 2
1746 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1749 #ifndef IPPROTO_IGRP
1750 #define IPPROTO_IGRP 9
1753 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1757 #define IPPROTO_PIM 103
1761 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1763 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1769 b1
= gen_linktype(ETHERTYPE_IP
);
1773 b1
= gen_linktype(ETHERTYPE_ARP
);
1777 b1
= gen_linktype(ETHERTYPE_REVARP
);
1781 bpf_error("link layer applied in wrong context");
1784 b1
= gen_linktype(ETHERTYPE_ATALK
);
1788 b1
= gen_linktype(ETHERTYPE_AARP
);
1792 b1
= gen_linktype(ETHERTYPE_DN
);
1796 b1
= gen_linktype(ETHERTYPE_SCA
);
1800 b1
= gen_linktype(ETHERTYPE_LAT
);
1804 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1808 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1813 b1
= gen_linktype(ETHERTYPE_IPV6
);
1816 #ifndef IPPROTO_ICMPV6
1817 #define IPPROTO_ICMPV6 58
1820 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1825 #define IPPROTO_AH 51
1828 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1830 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1836 #define IPPROTO_ESP 50
1839 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1841 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1847 b1
= gen_linktype(LLC_ISO_LSAP
);
1851 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
1855 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
1859 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
1863 b1
= gen_linktype(LLC_STP_LSAP
);
1872 static struct block
*
1879 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1880 s
->s
.k
= off_nl
+ 6;
1881 b
= new_block(JMP(BPF_JSET
));
1889 static struct block
*
1890 gen_portatom(off
, v
)
1897 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1900 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1901 s
->next
->s
.k
= off_nl
+ off
;
1903 b
= new_block(JMP(BPF_JEQ
));
1911 static struct block
*
1912 gen_portatom6(off
, v
)
1916 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1921 gen_portop(port
, proto
, dir
)
1922 int port
, proto
, dir
;
1924 struct block
*b0
, *b1
, *tmp
;
1926 /* ip proto 'proto' */
1927 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1933 b1
= gen_portatom(0, (bpf_int32
)port
);
1937 b1
= gen_portatom(2, (bpf_int32
)port
);
1942 tmp
= gen_portatom(0, (bpf_int32
)port
);
1943 b1
= gen_portatom(2, (bpf_int32
)port
);
1948 tmp
= gen_portatom(0, (bpf_int32
)port
);
1949 b1
= gen_portatom(2, (bpf_int32
)port
);
1961 static struct block
*
1962 gen_port(port
, ip_proto
, dir
)
1967 struct block
*b0
, *b1
, *tmp
;
1969 /* ether proto ip */
1970 b0
= gen_linktype(ETHERTYPE_IP
);
1975 b1
= gen_portop(port
, ip_proto
, dir
);
1979 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1980 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1993 gen_portop6(port
, proto
, dir
)
1994 int port
, proto
, dir
;
1996 struct block
*b0
, *b1
, *tmp
;
1998 /* ip proto 'proto' */
1999 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2003 b1
= gen_portatom6(0, (bpf_int32
)port
);
2007 b1
= gen_portatom6(2, (bpf_int32
)port
);
2012 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2013 b1
= gen_portatom6(2, (bpf_int32
)port
);
2018 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2019 b1
= gen_portatom6(2, (bpf_int32
)port
);
2031 static struct block
*
2032 gen_port6(port
, ip_proto
, dir
)
2037 struct block
*b0
, *b1
, *tmp
;
2039 /* ether proto ip */
2040 b0
= gen_linktype(ETHERTYPE_IPV6
);
2045 b1
= gen_portop6(port
, ip_proto
, dir
);
2049 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2050 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2063 lookup_proto(name
, proto
)
2064 register const char *name
;
2073 v
= pcap_nametoproto(name
);
2074 if (v
== PROTO_UNDEF
)
2075 bpf_error("unknown ip proto '%s'", name
);
2079 /* XXX should look up h/w protocol type based on linktype */
2080 v
= pcap_nametoeproto(name
);
2081 if (v
== PROTO_UNDEF
)
2082 bpf_error("unknown ether proto '%s'", name
);
2086 if (strcmp(name
, "esis") == 0)
2088 else if (strcmp(name
, "isis") == 0)
2090 else if (strcmp(name
, "clnp") == 0)
2093 bpf_error("unknown osi proto '%s'", name
);
2113 static struct block
*
2114 gen_protochain(v
, proto
, dir
)
2119 #ifdef NO_PROTOCHAIN
2120 return gen_proto(v
, proto
, dir
);
2122 struct block
*b0
, *b
;
2123 struct slist
*s
[100];
2124 int fix2
, fix3
, fix4
, fix5
;
2125 int ahcheck
, again
, end
;
2127 int reg2
= alloc_reg();
2129 memset(s
, 0, sizeof(s
));
2130 fix2
= fix3
= fix4
= fix5
= 0;
2137 b0
= gen_protochain(v
, Q_IP
, dir
);
2138 b
= gen_protochain(v
, Q_IPV6
, dir
);
2142 bpf_error("bad protocol applied for 'protochain'");
2146 no_optimize
= 1; /*this code is not compatible with optimzer yet */
2149 * s[0] is a dummy entry to protect other BPF insn from damaged
2150 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
2151 * hard to find interdependency made by jump table fixup.
2154 s
[i
] = new_stmt(0); /*dummy*/
2159 b0
= gen_linktype(ETHERTYPE_IP
);
2162 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2163 s
[i
]->s
.k
= off_nl
+ 9;
2165 /* X = ip->ip_hl << 2 */
2166 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2172 b0
= gen_linktype(ETHERTYPE_IPV6
);
2174 /* A = ip6->ip_nxt */
2175 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2176 s
[i
]->s
.k
= off_nl
+ 6;
2178 /* X = sizeof(struct ip6_hdr) */
2179 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
2185 bpf_error("unsupported proto to gen_protochain");
2189 /* again: if (A == v) goto end; else fall through; */
2191 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2193 s
[i
]->s
.jt
= NULL
; /*later*/
2194 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2198 #ifndef IPPROTO_NONE
2199 #define IPPROTO_NONE 59
2201 /* if (A == IPPROTO_NONE) goto end */
2202 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2203 s
[i
]->s
.jt
= NULL
; /*later*/
2204 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2205 s
[i
]->s
.k
= IPPROTO_NONE
;
2206 s
[fix5
]->s
.jf
= s
[i
];
2211 if (proto
== Q_IPV6
) {
2212 int v6start
, v6end
, v6advance
, j
;
2215 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
2216 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2217 s
[i
]->s
.jt
= NULL
; /*later*/
2218 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2219 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
2220 s
[fix2
]->s
.jf
= s
[i
];
2222 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
2223 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2224 s
[i
]->s
.jt
= NULL
; /*later*/
2225 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2226 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
2228 /* if (A == IPPROTO_ROUTING) goto v6advance */
2229 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2230 s
[i
]->s
.jt
= NULL
; /*later*/
2231 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
2232 s
[i
]->s
.k
= IPPROTO_ROUTING
;
2234 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
2235 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2236 s
[i
]->s
.jt
= NULL
; /*later*/
2237 s
[i
]->s
.jf
= NULL
; /*later*/
2238 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
2249 * X = X + (P[X + 1] + 1) * 8;
2252 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2254 /* A = P[X + packet head] */
2255 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2259 s
[i
] = new_stmt(BPF_ST
);
2263 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2266 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2270 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2272 /* A = P[X + packet head]; */
2273 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2277 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2281 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2285 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2288 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2292 /* goto again; (must use BPF_JA for backward jump) */
2293 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2294 s
[i
]->s
.k
= again
- i
- 1;
2295 s
[i
- 1]->s
.jf
= s
[i
];
2299 for (j
= v6start
; j
<= v6end
; j
++)
2300 s
[j
]->s
.jt
= s
[v6advance
];
2305 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2307 s
[fix2
]->s
.jf
= s
[i
];
2313 /* if (A == IPPROTO_AH) then fall through; else goto end; */
2314 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
2315 s
[i
]->s
.jt
= NULL
; /*later*/
2316 s
[i
]->s
.jf
= NULL
; /*later*/
2317 s
[i
]->s
.k
= IPPROTO_AH
;
2319 s
[fix3
]->s
.jf
= s
[ahcheck
];
2326 * X = X + (P[X + 1] + 2) * 4;
2329 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2331 /* A = P[X + packet head]; */
2332 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2336 s
[i
] = new_stmt(BPF_ST
);
2340 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
2343 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2347 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2349 /* A = P[X + packet head] */
2350 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2354 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2358 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
2362 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
2365 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
2369 /* goto again; (must use BPF_JA for backward jump) */
2370 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
2371 s
[i
]->s
.k
= again
- i
- 1;
2376 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2378 s
[fix2
]->s
.jt
= s
[end
];
2379 s
[fix4
]->s
.jf
= s
[end
];
2380 s
[fix5
]->s
.jt
= s
[end
];
2387 for (i
= 0; i
< max
- 1; i
++)
2388 s
[i
]->next
= s
[i
+ 1];
2389 s
[max
- 1]->next
= NULL
;
2394 b
= new_block(JMP(BPF_JEQ
));
2395 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
2405 static struct block
*
2406 gen_proto(v
, proto
, dir
)
2411 struct block
*b0
, *b1
;
2413 if (dir
!= Q_DEFAULT
)
2414 bpf_error("direction applied to 'proto'");
2419 b0
= gen_proto(v
, Q_IP
, dir
);
2420 b1
= gen_proto(v
, Q_IPV6
, dir
);
2427 b0
= gen_linktype(ETHERTYPE_IP
);
2429 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2431 b1
= gen_protochain(v
, Q_IP
);
2437 b0
= gen_linktype(LLC_ISO_LSAP
);
2438 b1
= gen_cmp(off_nl
+ 3, BPF_B
, (long)v
);
2443 bpf_error("arp does not encapsulate another protocol");
2447 bpf_error("rarp does not encapsulate another protocol");
2451 bpf_error("atalk encapsulation is not specifiable");
2455 bpf_error("decnet encapsulation is not specifiable");
2459 bpf_error("sca does not encapsulate another protocol");
2463 bpf_error("lat does not encapsulate another protocol");
2467 bpf_error("moprc does not encapsulate another protocol");
2471 bpf_error("mopdl does not encapsulate another protocol");
2475 return gen_linktype(v
);
2478 bpf_error("'udp proto' is bogus");
2482 bpf_error("'tcp proto' is bogus");
2486 bpf_error("'icmp proto' is bogus");
2490 bpf_error("'igmp proto' is bogus");
2494 bpf_error("'igrp proto' is bogus");
2498 bpf_error("'pim proto' is bogus");
2503 b0
= gen_linktype(ETHERTYPE_IPV6
);
2505 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2507 b1
= gen_protochain(v
, Q_IPV6
);
2513 bpf_error("'icmp6 proto' is bogus");
2517 bpf_error("'ah proto' is bogus");
2520 bpf_error("'ah proto' is bogus");
2523 bpf_error("'stp proto' is bogus");
2534 register const char *name
;
2537 int proto
= q
.proto
;
2541 bpf_u_int32 mask
, addr
;
2543 bpf_u_int32
**alist
;
2546 struct sockaddr_in
*sin
;
2547 struct sockaddr_in6
*sin6
;
2548 struct addrinfo
*res
, *res0
;
2549 struct in6_addr mask128
;
2551 struct block
*b
, *tmp
;
2552 int port
, real_proto
;
2557 addr
= pcap_nametonetaddr(name
);
2559 bpf_error("unknown network '%s'", name
);
2560 /* Left justify network addr and calculate its network mask */
2562 while (addr
&& (addr
& 0xff000000) == 0) {
2566 return gen_host(addr
, mask
, proto
, dir
);
2570 if (proto
== Q_LINK
) {
2574 eaddr
= pcap_ether_hostton(name
);
2577 "unknown ether host '%s'", name
);
2578 return gen_ehostop(eaddr
, dir
);
2581 eaddr
= pcap_ether_hostton(name
);
2584 "unknown FDDI host '%s'", name
);
2585 return gen_fhostop(eaddr
, dir
);
2588 eaddr
= pcap_ether_hostton(name
);
2591 "unknown token ring host '%s'", name
);
2592 return gen_thostop(eaddr
, dir
);
2596 "only ethernet/FDDI/token ring supports link-level host name");
2599 } else if (proto
== Q_DECNET
) {
2600 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2602 * I don't think DECNET hosts can be multihomed, so
2603 * there is no need to build up a list of addresses
2605 return (gen_host(dn_addr
, 0, proto
, dir
));
2608 alist
= pcap_nametoaddr(name
);
2609 if (alist
== NULL
|| *alist
== NULL
)
2610 bpf_error("unknown host '%s'", name
);
2612 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2614 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2616 tmp
= gen_host(**alist
++, 0xffffffff,
2623 memset(&mask128
, 0xff, sizeof(mask128
));
2624 res0
= res
= pcap_nametoaddrinfo(name
);
2626 bpf_error("unknown host '%s'", name
);
2628 tproto
= tproto6
= proto
;
2629 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2633 for (res
= res0
; res
; res
= res
->ai_next
) {
2634 switch (res
->ai_family
) {
2636 if (tproto
== Q_IPV6
)
2639 sin
= (struct sockaddr_in
*)
2641 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2642 0xffffffff, tproto
, dir
);
2645 if (tproto6
== Q_IP
)
2648 sin6
= (struct sockaddr_in6
*)
2650 tmp
= gen_host6(&sin6
->sin6_addr
,
2651 &mask128
, tproto6
, dir
);
2660 bpf_error("unknown host '%s'%s", name
,
2661 (proto
== Q_DEFAULT
)
2663 : " for specified address family");
2670 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2671 bpf_error("illegal qualifier of 'port'");
2672 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2673 bpf_error("unknown port '%s'", name
);
2674 if (proto
== Q_UDP
) {
2675 if (real_proto
== IPPROTO_TCP
)
2676 bpf_error("port '%s' is tcp", name
);
2678 /* override PROTO_UNDEF */
2679 real_proto
= IPPROTO_UDP
;
2681 if (proto
== Q_TCP
) {
2682 if (real_proto
== IPPROTO_UDP
)
2683 bpf_error("port '%s' is udp", name
);
2685 /* override PROTO_UNDEF */
2686 real_proto
= IPPROTO_TCP
;
2689 return gen_port(port
, real_proto
, dir
);
2693 b
= gen_port(port
, real_proto
, dir
);
2694 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2701 eaddr
= pcap_ether_hostton(name
);
2703 bpf_error("unknown ether host: %s", name
);
2705 alist
= pcap_nametoaddr(name
);
2706 if (alist
== NULL
|| *alist
== NULL
)
2707 bpf_error("unknown host '%s'", name
);
2708 return gen_gateway(eaddr
, alist
, proto
, dir
);
2710 bpf_error("'gateway' not supported in this configuration");
2714 real_proto
= lookup_proto(name
, proto
);
2715 if (real_proto
>= 0)
2716 return gen_proto(real_proto
, proto
, dir
);
2718 bpf_error("unknown protocol: %s", name
);
2721 real_proto
= lookup_proto(name
, proto
);
2722 if (real_proto
>= 0)
2723 return gen_protochain(real_proto
, proto
, dir
);
2725 bpf_error("unknown protocol: %s", name
);
2737 gen_mcode(s1
, s2
, masklen
, q
)
2738 register const char *s1
, *s2
;
2739 register int masklen
;
2742 register int nlen
, mlen
;
2745 nlen
= __pcap_atoin(s1
, &n
);
2746 /* Promote short ipaddr */
2750 mlen
= __pcap_atoin(s2
, &m
);
2751 /* Promote short ipaddr */
2754 bpf_error("non-network bits set in \"%s mask %s\"",
2757 /* Convert mask len to mask */
2759 bpf_error("mask length must be <= 32");
2760 m
= 0xffffffff << (32 - masklen
);
2762 bpf_error("non-network bits set in \"%s/%d\"",
2769 return gen_host(n
, m
, q
.proto
, q
.dir
);
2772 bpf_error("Mask syntax for networks only");
2779 register const char *s
;
2784 int proto
= q
.proto
;
2790 else if (q
.proto
== Q_DECNET
)
2791 vlen
= __pcap_atodn(s
, &v
);
2793 vlen
= __pcap_atoin(s
, &v
);
2800 if (proto
== Q_DECNET
)
2801 return gen_host(v
, 0, proto
, dir
);
2802 else if (proto
== Q_LINK
) {
2803 bpf_error("illegal link layer address");
2806 if (s
== NULL
&& q
.addr
== Q_NET
) {
2807 /* Promote short net number */
2808 while (v
&& (v
& 0xff000000) == 0) {
2813 /* Promote short ipaddr */
2817 return gen_host(v
, mask
, proto
, dir
);
2822 proto
= IPPROTO_UDP
;
2823 else if (proto
== Q_TCP
)
2824 proto
= IPPROTO_TCP
;
2825 else if (proto
== Q_DEFAULT
)
2826 proto
= PROTO_UNDEF
;
2828 bpf_error("illegal qualifier of 'port'");
2831 return gen_port((int)v
, proto
, dir
);
2835 b
= gen_port((int)v
, proto
, dir
);
2836 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2842 bpf_error("'gateway' requires a name");
2846 return gen_proto((int)v
, proto
, dir
);
2849 return gen_protochain((int)v
, proto
, dir
);
2864 gen_mcode6(s1
, s2
, masklen
, q
)
2865 register const char *s1
, *s2
;
2866 register int masklen
;
2869 struct addrinfo
*res
;
2870 struct in6_addr
*addr
;
2871 struct in6_addr mask
;
2876 bpf_error("no mask %s supported", s2
);
2878 res
= pcap_nametoaddrinfo(s1
);
2880 bpf_error("invalid ip6 address %s", s1
);
2882 bpf_error("%s resolved to multiple address", s1
);
2883 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2885 if (sizeof(mask
) * 8 < masklen
)
2886 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2887 memset(&mask
, 0xff, masklen
/ 8);
2889 mask
.s6_addr
[masklen
/ 8] =
2890 (0xff << (8 - masklen
% 8)) & 0xff;
2893 a
= (u_int32_t
*)addr
;
2894 m
= (u_int32_t
*)&mask
;
2895 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2896 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2897 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2905 bpf_error("Mask syntax for networks only");
2909 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2914 bpf_error("invalid qualifier against IPv6 address");
2922 register const u_char
*eaddr
;
2925 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2926 if (linktype
== DLT_EN10MB
)
2927 return gen_ehostop(eaddr
, (int)q
.dir
);
2928 if (linktype
== DLT_FDDI
)
2929 return gen_fhostop(eaddr
, (int)q
.dir
);
2930 if (linktype
== DLT_IEEE802
)
2931 return gen_thostop(eaddr
, (int)q
.dir
);
2932 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring");
2934 bpf_error("ethernet address used in non-ether expression");
2940 struct slist
*s0
, *s1
;
2943 * This is definitely not the best way to do this, but the
2944 * lists will rarely get long.
2951 static struct slist
*
2957 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2962 static struct slist
*
2968 s
= new_stmt(BPF_LD
|BPF_MEM
);
2974 gen_load(proto
, index
, size
)
2979 struct slist
*s
, *tmp
;
2981 int regno
= alloc_reg();
2983 free_reg(index
->regno
);
2987 bpf_error("data size must be 1, 2, or 4");
3003 bpf_error("unsupported index operation");
3006 s
= xfer_to_x(index
);
3007 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3009 sappend(index
->s
, s
);
3024 /* XXX Note that we assume a fixed link header here. */
3025 s
= xfer_to_x(index
);
3026 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3029 sappend(index
->s
, s
);
3031 b
= gen_proto_abbrev(proto
);
3033 gen_and(index
->b
, b
);
3043 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3045 sappend(s
, xfer_to_a(index
));
3046 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3047 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3048 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3050 sappend(index
->s
, s
);
3052 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
3054 gen_and(index
->b
, b
);
3056 gen_and(gen_proto_abbrev(Q_IP
), b
);
3062 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
3066 index
->regno
= regno
;
3067 s
= new_stmt(BPF_ST
);
3069 sappend(index
->s
, s
);
3075 gen_relation(code
, a0
, a1
, reversed
)
3077 struct arth
*a0
, *a1
;
3080 struct slist
*s0
, *s1
, *s2
;
3081 struct block
*b
, *tmp
;
3085 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
3086 b
= new_block(JMP(code
));
3087 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
3088 reversed
= !reversed
;
3089 b
->s
.k
= 0x80000000;
3097 sappend(a0
->s
, a1
->s
);
3101 free_reg(a0
->regno
);
3102 free_reg(a1
->regno
);
3104 /* 'and' together protocol checks */
3107 gen_and(a0
->b
, tmp
= a1
->b
);
3123 int regno
= alloc_reg();
3124 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
3127 s
= new_stmt(BPF_LD
|BPF_LEN
);
3128 s
->next
= new_stmt(BPF_ST
);
3129 s
->next
->s
.k
= regno
;
3144 a
= (struct arth
*)newchunk(sizeof(*a
));
3148 s
= new_stmt(BPF_LD
|BPF_IMM
);
3150 s
->next
= new_stmt(BPF_ST
);
3166 s
= new_stmt(BPF_ALU
|BPF_NEG
);
3169 s
= new_stmt(BPF_ST
);
3177 gen_arth(code
, a0
, a1
)
3179 struct arth
*a0
, *a1
;
3181 struct slist
*s0
, *s1
, *s2
;
3185 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
3190 sappend(a0
->s
, a1
->s
);
3192 free_reg(a1
->regno
);
3194 s0
= new_stmt(BPF_ST
);
3195 a0
->regno
= s0
->s
.k
= alloc_reg();
3202 * Here we handle simple allocation of the scratch registers.
3203 * If too many registers are alloc'd, the allocator punts.
3205 static int regused
[BPF_MEMWORDS
];
3209 * Return the next free register.
3214 int n
= BPF_MEMWORDS
;
3217 if (regused
[curreg
])
3218 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
3220 regused
[curreg
] = 1;
3224 bpf_error("too many registers needed to evaluate expression");
3229 * Return a register to the table so it can
3239 static struct block
*
3246 s
= new_stmt(BPF_LD
|BPF_LEN
);
3247 b
= new_block(JMP(jmp
));
3258 return gen_len(BPF_JGE
, n
);
3262 * Actually, this is less than or equal.
3270 b
= gen_len(BPF_JGT
, n
);
3277 gen_byteop(op
, idx
, val
)
3288 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3291 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3292 b
->s
.code
= JMP(BPF_JGE
);
3297 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
3298 b
->s
.code
= JMP(BPF_JGT
);
3302 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
3306 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
3310 b
= new_block(JMP(BPF_JEQ
));
3318 gen_broadcast(proto
)
3321 bpf_u_int32 hostmask
;
3322 struct block
*b0
, *b1
, *b2
;
3323 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3329 if (linktype
== DLT_EN10MB
)
3330 return gen_ehostop(ebroadcast
, Q_DST
);
3331 if (linktype
== DLT_FDDI
)
3332 return gen_fhostop(ebroadcast
, Q_DST
);
3333 if (linktype
== DLT_IEEE802
)
3334 return gen_thostop(ebroadcast
, Q_DST
);
3335 bpf_error("not a broadcast link");
3339 b0
= gen_linktype(ETHERTYPE_IP
);
3340 hostmask
= ~netmask
;
3341 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
3342 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
3343 (bpf_int32
)(~0 & hostmask
), hostmask
);
3348 bpf_error("only ether/ip broadcast filters supported");
3352 gen_multicast(proto
)
3355 register struct block
*b0
, *b1
;
3356 register struct slist
*s
;
3362 if (linktype
== DLT_EN10MB
) {
3363 /* ether[0] & 1 != 0 */
3364 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3366 b0
= new_block(JMP(BPF_JSET
));
3372 if (linktype
== DLT_FDDI
) {
3373 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
3374 /* fddi[1] & 1 != 0 */
3375 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
3377 b0
= new_block(JMP(BPF_JSET
));
3383 /* TODO - check how token ring handles multicast */
3384 /* if (linktype == DLT_IEEE802) ... */
3386 /* Link not known to support multicasts */
3390 b0
= gen_linktype(ETHERTYPE_IP
);
3391 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
3392 b1
->s
.code
= JMP(BPF_JGE
);
3398 b0
= gen_linktype(ETHERTYPE_IPV6
);
3399 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
3404 bpf_error("only IP multicast filters supported on ethernet/FDDI");
3408 * generate command for inbound/outbound. It's here so we can
3409 * make it link-type specific. 'dir' = 0 implies "inbound",
3410 * = 1 implies "outbound".
3416 register struct block
*b0
;
3419 * Only some data link types support inbound/outbound qualifiers.
3424 b0
= gen_relation(BPF_JEQ
,
3425 gen_load(Q_LINK
, gen_loadi(0), 1),
3431 bpf_error("inbound/outbound not supported on linktype %d\n",
3440 * support IEEE 802.1Q VLAN trunk over ethernet
3446 static u_int orig_linktype
= -1, orig_nl
= -1;
3450 * Change the offsets to point to the type and data fields within
3451 * the VLAN packet. This is somewhat of a kludge.
3453 if (orig_nl
== (u_int
)-1) {
3454 orig_linktype
= off_linktype
; /* save original values */
3465 bpf_error("no VLAN support for data link type %d",
3471 /* check for VLAN */
3472 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
3474 /* If a specific VLAN is requested, check VLAN id */
3475 if (vlan_num
>= 0) {
3478 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);