]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
2750df65f4f3ed7481658777e5e61b1ea233443a
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.120 2000-10-06 04:52:53 guy Exp $ (LBL)";
31 #include <sys/types.h>
32 #include <sys/socket.h>
35 #include <sys/param.h>
42 #include <netinet/in.h>
51 #include "ethertype.h"
54 #include <pcap-namedb.h>
57 #include <sys/socket.h>
60 #ifdef HAVE_OS_PROTO_H
64 #define JMP(c) ((c)|BPF_JMP|BPF_K)
67 static jmp_buf top_ctx
;
68 static pcap_t
*bpf_pcap
;
72 int pcap_fddipad
= PCAP_FDDIPAD
;
79 bpf_error(const char *fmt
, ...)
86 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
93 static void init_linktype(int);
95 static int alloc_reg(void);
96 static void free_reg(int);
98 static struct block
*root
;
101 * We divy out chunks of memory rather than call malloc each time so
102 * we don't have to worry about leaking memory. It's probably
103 * not a big deal if all this memory was wasted but it this ever
104 * goes into a library that would probably not be a good idea.
107 #define CHUNK0SIZE 1024
113 static struct chunk chunks
[NCHUNKS
];
114 static int cur_chunk
;
116 static void *newchunk(u_int
);
117 static void freechunks(void);
118 static inline struct block
*new_block(int);
119 static inline struct slist
*new_stmt(int);
120 static struct block
*gen_retblk(int);
121 static inline void syntax(void);
123 static void backpatch(struct block
*, struct block
*);
124 static void merge(struct block
*, struct block
*);
125 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
126 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
127 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
128 static struct block
*gen_uncond(int);
129 static inline struct block
*gen_true(void);
130 static inline struct block
*gen_false(void);
131 static struct block
*gen_linktype(int);
132 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
134 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
136 static struct block
*gen_ehostop(const u_char
*, int);
137 static struct block
*gen_fhostop(const u_char
*, int);
138 static struct block
*gen_thostop(const u_char
*, int);
139 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
140 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
142 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
145 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
147 static struct block
*gen_ipfrag(void);
148 static struct block
*gen_portatom(int, bpf_int32
);
150 static struct block
*gen_portatom6(int, bpf_int32
);
152 struct block
*gen_portop(int, int, int);
153 static struct block
*gen_port(int, int, int);
155 struct block
*gen_portop6(int, int, int);
156 static struct block
*gen_port6(int, int, int);
158 static int lookup_proto(const char *, int);
159 static struct block
*gen_protochain(int, int, int);
160 static struct block
*gen_proto(int, int, int);
161 static struct slist
*xfer_to_x(struct arth
*);
162 static struct slist
*xfer_to_a(struct arth
*);
163 static struct block
*gen_len(int, int);
173 /* XXX Round up to nearest long. */
174 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
176 /* XXX Round up to structure boundary. */
180 cp
= &chunks
[cur_chunk
];
181 if (n
> cp
->n_left
) {
182 ++cp
, k
= ++cur_chunk
;
184 bpf_error("out of memory");
185 size
= CHUNK0SIZE
<< k
;
186 cp
->m
= (void *)malloc(size
);
187 memset((char *)cp
->m
, 0, size
);
190 bpf_error("out of memory");
193 return (void *)((char *)cp
->m
+ cp
->n_left
);
202 for (i
= 0; i
< NCHUNKS
; ++i
)
203 if (chunks
[i
].m
!= NULL
) {
210 * A strdup whose allocations are freed after code generation is over.
214 register const char *s
;
216 int n
= strlen(s
) + 1;
217 char *cp
= newchunk(n
);
223 static inline struct block
*
229 p
= (struct block
*)newchunk(sizeof(*p
));
236 static inline struct slist
*
242 p
= (struct slist
*)newchunk(sizeof(*p
));
248 static struct block
*
252 struct block
*b
= new_block(BPF_RET
|BPF_K
);
261 bpf_error("syntax error in filter expression");
264 static bpf_u_int32 netmask
;
269 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
270 char *buf
, int optimize
, bpf_u_int32 mask
)
279 if (setjmp(top_ctx
)) {
287 /* On Linux we do not use the BPF filter to truncate the packet
288 * since the kernel provides other ways for that. In fact if we
289 * are using the packet filter for that duty we will be unable
290 * to acquire the original packet size. -- Torsten */
292 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
;
326 * entry point for using the compiler with no pcap open
327 * pass in all the stuff that is needed explicitly instead.
330 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
331 struct bpf_program
*program
,
332 char *buf
, int optimize
, bpf_u_int32 mask
)
340 if (setjmp(top_ctx
)) {
347 /* XXX needed? I don't grok the use of globals here. */
348 snaplen
= snaplen_arg
;
350 lex_init(buf
? buf
: "");
351 init_linktype(linktype_arg
);
358 root
= gen_retblk(snaplen_arg
);
363 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
364 bpf_error("expression rejects all packets");
366 program
->bf_insns
= icode_to_fcode(root
, &len
);
367 program
->bf_len
= len
;
374 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
375 * which of the jt and jf fields has been resolved and which is a pointer
376 * back to another unresolved block (or nil). At least one of the fields
377 * in each block is already resolved.
380 backpatch(list
, target
)
381 struct block
*list
, *target
;
398 * Merge the lists in b0 and b1, using the 'sense' field to indicate
399 * which of jt and jf is the link.
403 struct block
*b0
, *b1
;
405 register struct block
**p
= &b0
;
407 /* Find end of list. */
409 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
411 /* Concatenate the lists. */
419 backpatch(p
, gen_retblk(snaplen
));
420 p
->sense
= !p
->sense
;
421 backpatch(p
, gen_retblk(0));
427 struct block
*b0
, *b1
;
429 backpatch(b0
, b1
->head
);
430 b0
->sense
= !b0
->sense
;
431 b1
->sense
= !b1
->sense
;
433 b1
->sense
= !b1
->sense
;
439 struct block
*b0
, *b1
;
441 b0
->sense
= !b0
->sense
;
442 backpatch(b0
, b1
->head
);
443 b0
->sense
= !b0
->sense
;
452 b
->sense
= !b
->sense
;
455 static struct block
*
456 gen_cmp(offset
, size
, v
)
463 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
466 b
= new_block(JMP(BPF_JEQ
));
473 static struct block
*
474 gen_mcmp(offset
, size
, v
, mask
)
479 struct block
*b
= gen_cmp(offset
, size
, v
);
482 if (mask
!= 0xffffffff) {
483 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
490 static struct block
*
491 gen_bcmp(offset
, size
, v
)
492 register u_int offset
, size
;
493 register const u_char
*v
;
495 register struct block
*b
, *tmp
;
499 register const u_char
*p
= &v
[size
- 4];
500 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
501 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
503 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
510 register const u_char
*p
= &v
[size
- 2];
511 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
513 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
520 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
529 * Various code constructs need to know the layout of the data link
530 * layer. These variables give the necessary offsets. off_linktype
531 * is set to -1 for no encapsulation, in which case, IP is assumed.
533 static u_int off_linktype
;
542 * Map DLT_ codes that don't have the same value as the
543 * equivalent PCAP_ENCAP_ codes to the corresponding PCAP_ENCAP_
546 * Even though "pcap_open_live()" in "pcap-bpf.c" does a
547 * similar mapping, we do that mapping here as well, to
548 * handle filters constructed for savefiles.
550 * XXX - should we do this mapping in "savefile.c"? Doing so
551 * might cause programs that read one or more capture files
552 * and write another capture file with the same type as
553 * the input file(s) to use PCAP_ENCAP_ values that aren't
554 * supported by the libpcap on the system that wrote the original
555 * capture file, so we might not want to do that.
559 #ifdef DLT_ATM_RFC1483
560 case DLT_ATM_RFC1483
:
561 linktype
= PCAP_ENCAP_ATM_RFC1483
;
567 linktype
= PCAP_ENCAP_RAW
;
571 #ifdef DLT_SLIP_BSDOS
573 linktype
= PCAP_ENCAP_SLIP_BSDOS
;
579 linktype
= PCAP_ENCAP_PPP_BSDOS
;
585 linktype
= PCAP_ENCAP_ATM_CLIP
;
591 linktype
= PCAP_ENCAP_ATM_CLIP
;
595 #ifdef DLT_PPP_SERIAL
597 linktype
= PCAP_ENCAP_PPP_HDLC
;
608 case PCAP_ENCAP_ETHERNET
:
613 case PCAP_ENCAP_SLIP
:
615 * SLIP doesn't have a link level type. The 16 byte
616 * header is hacked into our SLIP driver.
622 case PCAP_ENCAP_SLIP_BSDOS
:
623 /* XXX this may be the same as the PCAP_ENCAP_PPP_BSDOS case */
629 case PCAP_ENCAP_NULL
:
635 case PCAP_ENCAP_C_HDLC
:
636 case PCAP_ENCAP_PPP_HDLC
:
641 case PCAP_ENCAP_PPP_BSDOS
:
646 case PCAP_ENCAP_FDDI
:
648 * FDDI doesn't really have a link-level type field.
649 * We assume that SSAP = SNAP is being used and pick
650 * out the encapsulated Ethernet type.
652 * XXX - should we generate code to check for SNAP?
656 off_linktype
+= pcap_fddipad
;
660 off_nl
+= pcap_fddipad
;
664 case PCAP_ENCAP_TOKEN_RING
:
666 * Token Ring doesn't really have a link-level type field.
667 * We assume that SSAP = SNAP is being used and pick
668 * out the encapsulated Ethernet type.
670 * XXX - should we generate code to check for SNAP?
672 * XXX - the header is actually variable-length.
673 * Some various Linux patched versions gave 38
674 * as "off_linktype" and 40 as "off_nl"; however,
675 * if a token ring packet has *no* routing
676 * information, i.e. is not source-routed, the correct
677 * values are 20 and 22, as they are in the vanilla code.
679 * A packet is source-routed iff the uppermost bit
680 * of the first byte of the source address, at an
681 * offset of 8, has the uppermost bit set. If the
682 * packet is source-routed, the total number of bytes
683 * of routing information is 2 plus bits 0x1F00 of
684 * the 16-bit value at an offset of 14 (shifted right
685 * 8 - figure out which byte that is).
691 case PCAP_ENCAP_ATM_RFC1483
:
693 * assume routed, non-ISO PDUs
694 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
705 case PCAP_ENCAP_ATM_CLIP
:
710 bpf_error("unknown data link type 0x%x", linktype
);
714 static struct block
*
721 s
= new_stmt(BPF_LD
|BPF_IMM
);
723 b
= new_block(JMP(BPF_JEQ
));
729 static inline struct block
*
732 return gen_uncond(1);
735 static inline struct block
*
738 return gen_uncond(0);
741 static struct block
*
745 struct block
*b0
, *b1
;
747 /* If we're not using encapsulation, we're done */
748 if (off_linktype
== -1)
753 case PCAP_ENCAP_SLIP
:
757 case PCAP_ENCAP_PPP_HDLC
:
758 if (proto
== ETHERTYPE_IP
)
759 proto
= PPP_IP
; /* XXX was 0x21 */
761 else if (proto
== ETHERTYPE_IPV6
)
766 case PCAP_ENCAP_PPP_BSDOS
:
770 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
771 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
773 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
788 case ETHERTYPE_ATALK
:
798 case PCAP_ENCAP_NULL
:
800 if (proto
== ETHERTYPE_IP
)
801 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET
)));
803 else if (proto
== ETHERTYPE_IPV6
)
804 return (gen_cmp(0, BPF_W
, (bpf_int32
)htonl(AF_INET6
)));
809 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
812 static struct block
*
813 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
817 u_int src_off
, dst_off
;
819 struct block
*b0
, *b1
;
833 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
834 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
840 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
841 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
848 b0
= gen_linktype(proto
);
849 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
855 static struct block
*
856 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
857 struct in6_addr
*addr
;
858 struct in6_addr
*mask
;
860 u_int src_off
, dst_off
;
862 struct block
*b0
, *b1
;
877 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
878 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
884 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
885 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
892 /* this order is important */
893 a
= (u_int32_t
*)addr
;
894 m
= (u_int32_t
*)mask
;
895 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
896 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
898 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
900 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
902 b0
= gen_linktype(proto
);
908 static struct block
*
909 gen_ehostop(eaddr
, dir
)
910 register const u_char
*eaddr
;
913 register struct block
*b0
, *b1
;
917 return gen_bcmp(6, 6, eaddr
);
920 return gen_bcmp(0, 6, eaddr
);
923 b0
= gen_ehostop(eaddr
, Q_SRC
);
924 b1
= gen_ehostop(eaddr
, Q_DST
);
930 b0
= gen_ehostop(eaddr
, Q_SRC
);
931 b1
= gen_ehostop(eaddr
, Q_DST
);
940 * Like gen_ehostop, but for PCAP_ENCAP_FDDI
942 static struct block
*
943 gen_fhostop(eaddr
, dir
)
944 register const u_char
*eaddr
;
947 struct block
*b0
, *b1
;
952 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
954 return gen_bcmp(6 + 1, 6, eaddr
);
959 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
961 return gen_bcmp(0 + 1, 6, eaddr
);
965 b0
= gen_fhostop(eaddr
, Q_SRC
);
966 b1
= gen_fhostop(eaddr
, Q_DST
);
972 b0
= gen_fhostop(eaddr
, Q_SRC
);
973 b1
= gen_fhostop(eaddr
, Q_DST
);
982 * Like gen_ehostop, but for PCAP_ENCAP_TOKEN_RING
984 static struct block
*
985 gen_thostop(eaddr
, dir
)
986 register const u_char
*eaddr
;
989 register struct block
*b0
, *b1
;
993 return gen_bcmp(8, 6, eaddr
);
996 return gen_bcmp(2, 6, eaddr
);
999 b0
= gen_thostop(eaddr
, Q_SRC
);
1000 b1
= gen_thostop(eaddr
, Q_DST
);
1006 b0
= gen_thostop(eaddr
, Q_SRC
);
1007 b1
= gen_thostop(eaddr
, Q_DST
);
1016 * This is quite tricky because there may be pad bytes in front of the
1017 * DECNET header, and then there are two possible data packet formats that
1018 * carry both src and dst addresses, plus 5 packet types in a format that
1019 * carries only the src node, plus 2 types that use a different format and
1020 * also carry just the src node.
1024 * Instead of doing those all right, we just look for data packets with
1025 * 0 or 1 bytes of padding. If you want to look at other packets, that
1026 * will require a lot more hacking.
1028 * To add support for filtering on DECNET "areas" (network numbers)
1029 * one would want to add a "mask" argument to this routine. That would
1030 * make the filter even more inefficient, although one could be clever
1031 * and not generate masking instructions if the mask is 0xFFFF.
1033 static struct block
*
1034 gen_dnhostop(addr
, dir
, base_off
)
1039 struct block
*b0
, *b1
, *b2
, *tmp
;
1040 u_int offset_lh
; /* offset if long header is received */
1041 u_int offset_sh
; /* offset if short header is received */
1046 offset_sh
= 1; /* follows flags */
1047 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
1051 offset_sh
= 3; /* follows flags, dstnode */
1052 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1056 /* Inefficient because we do our Calvinball dance twice */
1057 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1058 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1064 /* Inefficient because we do our Calvinball dance twice */
1065 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
1066 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
1073 b0
= gen_linktype(ETHERTYPE_DN
);
1074 /* Check for pad = 1, long header case */
1075 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1076 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
1077 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
1078 BPF_H
, (bpf_int32
)ntohs(addr
));
1080 /* Check for pad = 0, long header case */
1081 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
1082 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1085 /* Check for pad = 1, short header case */
1086 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
1087 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
1088 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
1089 BPF_H
, (bpf_int32
)ntohs(addr
));
1092 /* Check for pad = 0, short header case */
1093 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
1094 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
1098 /* Combine with test for linktype */
1103 static struct block
*
1104 gen_host(addr
, mask
, proto
, dir
)
1110 struct block
*b0
, *b1
;
1115 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
1116 if (off_linktype
!= -1) {
1117 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
1119 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
1125 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
1126 off_nl
+ 12, off_nl
+ 16);
1129 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
1130 off_nl
+ 14, off_nl
+ 24);
1133 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
1134 off_nl
+ 14, off_nl
+ 24);
1137 bpf_error("'tcp' modifier applied to host");
1140 bpf_error("'udp' modifier applied to host");
1143 bpf_error("'icmp' modifier applied to host");
1146 bpf_error("'igmp' modifier applied to host");
1149 bpf_error("'igrp' modifier applied to host");
1152 bpf_error("'pim' modifier applied to host");
1155 bpf_error("ATALK host filtering not implemented");
1158 return gen_dnhostop(addr
, dir
, off_nl
);
1161 bpf_error("SCA host filtering not implemented");
1164 bpf_error("LAT host filtering not implemented");
1167 bpf_error("MOPDL host filtering not implemented");
1170 bpf_error("MOPRC host filtering not implemented");
1174 bpf_error("'ip6' modifier applied to ip host");
1177 bpf_error("'icmp6' modifier applied to host");
1181 bpf_error("'ah' modifier applied to host");
1184 bpf_error("'esp' modifier applied to host");
1193 static struct block
*
1194 gen_host6(addr
, mask
, proto
, dir
)
1195 struct in6_addr
*addr
;
1196 struct in6_addr
*mask
;
1203 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
1206 bpf_error("'ip' modifier applied to ip6 host");
1209 bpf_error("'rarp' modifier applied to ip6 host");
1212 bpf_error("'arp' modifier applied to ip6 host");
1215 bpf_error("'tcp' modifier applied to host");
1218 bpf_error("'udp' modifier applied to host");
1221 bpf_error("'icmp' modifier applied to host");
1224 bpf_error("'igmp' modifier applied to host");
1227 bpf_error("'igrp' modifier applied to host");
1230 bpf_error("'pim' modifier applied to host");
1233 bpf_error("ATALK host filtering not implemented");
1236 bpf_error("'decnet' modifier applied to ip6 host");
1239 bpf_error("SCA host filtering not implemented");
1242 bpf_error("LAT host filtering not implemented");
1245 bpf_error("MOPDL host filtering not implemented");
1248 bpf_error("MOPRC host filtering not implemented");
1251 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
1252 off_nl
+ 8, off_nl
+ 24);
1255 bpf_error("'icmp6' modifier applied to host");
1258 bpf_error("'ah' modifier applied to host");
1261 bpf_error("'esp' modifier applied to host");
1271 static struct block
*
1272 gen_gateway(eaddr
, alist
, proto
, dir
)
1273 const u_char
*eaddr
;
1274 bpf_u_int32
**alist
;
1278 struct block
*b0
, *b1
, *tmp
;
1281 bpf_error("direction applied to 'gateway'");
1288 if (linktype
== PCAP_ENCAP_ETHERNET
)
1289 b0
= gen_ehostop(eaddr
, Q_OR
);
1290 else if (linktype
== PCAP_ENCAP_FDDI
)
1291 b0
= gen_fhostop(eaddr
, Q_OR
);
1292 else if (linktype
== PCAP_ENCAP_TOKEN_RING
)
1293 b0
= gen_thostop(eaddr
, Q_OR
);
1296 "'gateway' supported only on ethernet, FDDI or token ring");
1298 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1300 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
1308 bpf_error("illegal modifier of 'gateway'");
1314 gen_proto_abbrev(proto
)
1325 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
1327 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
1333 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
1335 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
1341 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
1344 #ifndef IPPROTO_IGMP
1345 #define IPPROTO_IGMP 2
1349 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
1352 #ifndef IPPROTO_IGRP
1353 #define IPPROTO_IGRP 9
1356 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
1360 #define IPPROTO_PIM 103
1364 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
1366 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
1372 b1
= gen_linktype(ETHERTYPE_IP
);
1376 b1
= gen_linktype(ETHERTYPE_ARP
);
1380 b1
= gen_linktype(ETHERTYPE_REVARP
);
1384 bpf_error("link layer applied in wrong context");
1387 b1
= gen_linktype(ETHERTYPE_ATALK
);
1391 b1
= gen_linktype(ETHERTYPE_DN
);
1395 b1
= gen_linktype(ETHERTYPE_SCA
);
1399 b1
= gen_linktype(ETHERTYPE_LAT
);
1403 b1
= gen_linktype(ETHERTYPE_MOPDL
);
1407 b1
= gen_linktype(ETHERTYPE_MOPRC
);
1412 b1
= gen_linktype(ETHERTYPE_IPV6
);
1415 #ifndef IPPROTO_ICMPV6
1416 #define IPPROTO_ICMPV6 58
1419 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
1424 #define IPPROTO_AH 51
1427 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
1429 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
1435 #define IPPROTO_ESP 50
1438 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
1440 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
1451 static struct block
*
1458 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
1459 s
->s
.k
= off_nl
+ 6;
1460 b
= new_block(JMP(BPF_JSET
));
1468 static struct block
*
1469 gen_portatom(off
, v
)
1476 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1479 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
1480 s
->next
->s
.k
= off_nl
+ off
;
1482 b
= new_block(JMP(BPF_JEQ
));
1490 static struct block
*
1491 gen_portatom6(off
, v
)
1495 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
1500 gen_portop(port
, proto
, dir
)
1501 int port
, proto
, dir
;
1503 struct block
*b0
, *b1
, *tmp
;
1505 /* ip proto 'proto' */
1506 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
1512 b1
= gen_portatom(0, (bpf_int32
)port
);
1516 b1
= gen_portatom(2, (bpf_int32
)port
);
1521 tmp
= gen_portatom(0, (bpf_int32
)port
);
1522 b1
= gen_portatom(2, (bpf_int32
)port
);
1527 tmp
= gen_portatom(0, (bpf_int32
)port
);
1528 b1
= gen_portatom(2, (bpf_int32
)port
);
1540 static struct block
*
1541 gen_port(port
, ip_proto
, dir
)
1546 struct block
*b0
, *b1
, *tmp
;
1548 /* ether proto ip */
1549 b0
= gen_linktype(ETHERTYPE_IP
);
1554 b1
= gen_portop(port
, ip_proto
, dir
);
1558 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
1559 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
1572 gen_portop6(port
, proto
, dir
)
1573 int port
, proto
, dir
;
1575 struct block
*b0
, *b1
, *tmp
;
1577 /* ip proto 'proto' */
1578 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
1582 b1
= gen_portatom6(0, (bpf_int32
)port
);
1586 b1
= gen_portatom6(2, (bpf_int32
)port
);
1591 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1592 b1
= gen_portatom6(2, (bpf_int32
)port
);
1597 tmp
= gen_portatom6(0, (bpf_int32
)port
);
1598 b1
= gen_portatom6(2, (bpf_int32
)port
);
1610 static struct block
*
1611 gen_port6(port
, ip_proto
, dir
)
1616 struct block
*b0
, *b1
, *tmp
;
1618 /* ether proto ip */
1619 b0
= gen_linktype(ETHERTYPE_IPV6
);
1624 b1
= gen_portop6(port
, ip_proto
, dir
);
1628 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
1629 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
1642 lookup_proto(name
, proto
)
1643 register const char *name
;
1652 v
= pcap_nametoproto(name
);
1653 if (v
== PROTO_UNDEF
)
1654 bpf_error("unknown ip proto '%s'", name
);
1658 /* XXX should look up h/w protocol type based on linktype */
1659 v
= pcap_nametoeproto(name
);
1660 if (v
== PROTO_UNDEF
)
1661 bpf_error("unknown ether proto '%s'", name
);
1681 static struct block
*
1682 gen_protochain(v
, proto
, dir
)
1687 #ifdef NO_PROTOCHAIN
1688 return gen_proto(v
, proto
, dir
);
1690 struct block
*b0
, *b
;
1691 struct slist
*s
[100];
1692 int fix2
, fix3
, fix4
, fix5
;
1693 int ahcheck
, again
, end
;
1695 int reg1
= alloc_reg();
1696 int reg2
= alloc_reg();
1698 memset(s
, 0, sizeof(s
));
1699 fix2
= fix3
= fix4
= fix5
= 0;
1706 b0
= gen_protochain(v
, Q_IP
, dir
);
1707 b
= gen_protochain(v
, Q_IPV6
, dir
);
1711 bpf_error("bad protocol applied for 'protochain'");
1715 no_optimize
= 1; /*this code is not compatible with optimzer yet */
1718 * s[0] is a dummy entry to protect other BPF insn from damaged
1719 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1720 * hard to find interdependency made by jump table fixup.
1723 s
[i
] = new_stmt(0); /*dummy*/
1728 b0
= gen_linktype(ETHERTYPE_IP
);
1731 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1732 s
[i
]->s
.k
= off_nl
+ 9;
1734 /* X = ip->ip_hl << 2 */
1735 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1741 b0
= gen_linktype(ETHERTYPE_IPV6
);
1743 /* A = ip6->ip_nxt */
1744 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
1745 s
[i
]->s
.k
= off_nl
+ 6;
1747 /* X = sizeof(struct ip6_hdr) */
1748 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
1754 bpf_error("unsupported proto to gen_protochain");
1758 /* again: if (A == v) goto end; else fall through; */
1760 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1762 s
[i
]->s
.jt
= NULL
; /*later*/
1763 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1767 #ifndef IPPROTO_NONE
1768 #define IPPROTO_NONE 59
1770 /* if (A == IPPROTO_NONE) goto end */
1771 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1772 s
[i
]->s
.jt
= NULL
; /*later*/
1773 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1774 s
[i
]->s
.k
= IPPROTO_NONE
;
1775 s
[fix5
]->s
.jf
= s
[i
];
1780 if (proto
== Q_IPV6
) {
1781 int v6start
, v6end
, v6advance
, j
;
1784 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1785 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1786 s
[i
]->s
.jt
= NULL
; /*later*/
1787 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1788 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
1789 s
[fix2
]->s
.jf
= s
[i
];
1791 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1792 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1793 s
[i
]->s
.jt
= NULL
; /*later*/
1794 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1795 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
1797 /* if (A == IPPROTO_ROUTING) goto v6advance */
1798 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1799 s
[i
]->s
.jt
= NULL
; /*later*/
1800 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
1801 s
[i
]->s
.k
= IPPROTO_ROUTING
;
1803 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1804 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1805 s
[i
]->s
.jt
= NULL
; /*later*/
1806 s
[i
]->s
.jf
= NULL
; /*later*/
1807 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
1818 * X = X + (P[X] + 1) * 8;
1821 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1824 s
[i
] = new_stmt(BPF_ST
);
1828 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1832 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1834 /* A = P[X + packet head]; */
1835 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1839 s
[i
] = new_stmt(BPF_ST
);
1843 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1846 /* A = P[X + packet head] */
1847 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1851 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1855 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1859 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1862 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1866 /* goto again; (must use BPF_JA for backward jump) */
1867 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1868 s
[i
]->s
.k
= again
- i
- 1;
1869 s
[i
- 1]->s
.jf
= s
[i
];
1873 for (j
= v6start
; j
<= v6end
; j
++)
1874 s
[j
]->s
.jt
= s
[v6advance
];
1879 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1881 s
[fix2
]->s
.jf
= s
[i
];
1887 /* if (A == IPPROTO_AH) then fall through; else goto end; */
1888 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
1889 s
[i
]->s
.jt
= NULL
; /*later*/
1890 s
[i
]->s
.jf
= NULL
; /*later*/
1891 s
[i
]->s
.k
= IPPROTO_AH
;
1893 s
[fix3
]->s
.jf
= s
[ahcheck
];
1900 * X = X + (P[X] + 2) * 4;
1903 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
1906 s
[i
] = new_stmt(BPF_ST
);
1910 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1914 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1916 /* A = P[X + packet head]; */
1917 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1921 s
[i
] = new_stmt(BPF_ST
);
1925 s
[i
] = new_stmt(BPF_LDX
|BPF_MEM
);
1928 /* A = P[X + packet head] */
1929 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1933 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1937 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
1941 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
1944 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
1948 /* goto again; (must use BPF_JA for backward jump) */
1949 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
1950 s
[i
]->s
.k
= again
- i
- 1;
1955 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
1957 s
[fix2
]->s
.jt
= s
[end
];
1958 s
[fix4
]->s
.jf
= s
[end
];
1959 s
[fix5
]->s
.jt
= s
[end
];
1966 for (i
= 0; i
< max
- 1; i
++)
1967 s
[i
]->next
= s
[i
+ 1];
1968 s
[max
- 1]->next
= NULL
;
1973 b
= new_block(JMP(BPF_JEQ
));
1974 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
1985 static struct block
*
1986 gen_proto(v
, proto
, dir
)
1991 struct block
*b0
, *b1
;
1993 if (dir
!= Q_DEFAULT
)
1994 bpf_error("direction applied to 'proto'");
1999 b0
= gen_proto(v
, Q_IP
, dir
);
2000 b1
= gen_proto(v
, Q_IPV6
, dir
);
2007 b0
= gen_linktype(ETHERTYPE_IP
);
2009 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
2011 b1
= gen_protochain(v
, Q_IP
);
2017 bpf_error("arp does not encapsulate another protocol");
2021 bpf_error("rarp does not encapsulate another protocol");
2025 bpf_error("atalk encapsulation is not specifiable");
2029 bpf_error("decnet encapsulation is not specifiable");
2033 bpf_error("sca does not encapsulate another protocol");
2037 bpf_error("lat does not encapsulate another protocol");
2041 bpf_error("moprc does not encapsulate another protocol");
2045 bpf_error("mopdl does not encapsulate another protocol");
2049 return gen_linktype(v
);
2052 bpf_error("'udp proto' is bogus");
2056 bpf_error("'tcp proto' is bogus");
2060 bpf_error("'icmp proto' is bogus");
2064 bpf_error("'igmp proto' is bogus");
2068 bpf_error("'igrp proto' is bogus");
2072 bpf_error("'pim proto' is bogus");
2077 b0
= gen_linktype(ETHERTYPE_IPV6
);
2079 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
2081 b1
= gen_protochain(v
, Q_IPV6
);
2087 bpf_error("'icmp6 proto' is bogus");
2091 bpf_error("'ah proto' is bogus");
2094 bpf_error("'ah proto' is bogus");
2105 register const char *name
;
2108 int proto
= q
.proto
;
2112 bpf_u_int32 mask
, addr
;
2114 bpf_u_int32
**alist
;
2117 struct sockaddr_in
*sin
;
2118 struct sockaddr_in6
*sin6
;
2119 struct addrinfo
*res
, *res0
;
2120 struct in6_addr mask128
;
2122 struct block
*b
, *tmp
;
2123 int port
, real_proto
;
2128 addr
= pcap_nametonetaddr(name
);
2130 bpf_error("unknown network '%s'", name
);
2131 /* Left justify network addr and calculate its network mask */
2133 while (addr
&& (addr
& 0xff000000) == 0) {
2137 return gen_host(addr
, mask
, proto
, dir
);
2141 if (proto
== Q_LINK
) {
2144 case PCAP_ENCAP_ETHERNET
:
2145 eaddr
= pcap_ether_hostton(name
);
2148 "unknown ether host '%s'", name
);
2149 return gen_ehostop(eaddr
, dir
);
2151 case PCAP_ENCAP_FDDI
:
2152 eaddr
= pcap_ether_hostton(name
);
2155 "unknown FDDI host '%s'", name
);
2156 return gen_fhostop(eaddr
, dir
);
2158 case PCAP_ENCAP_TOKEN_RING
:
2159 eaddr
= pcap_ether_hostton(name
);
2162 "unknown token ring host '%s'", name
);
2163 return gen_thostop(eaddr
, dir
);
2167 "only ethernet/FDDI/token ring supports link-level host name");
2170 } else if (proto
== Q_DECNET
) {
2171 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
2173 * I don't think DECNET hosts can be multihomed, so
2174 * there is no need to build up a list of addresses
2176 return (gen_host(dn_addr
, 0, proto
, dir
));
2179 alist
= pcap_nametoaddr(name
);
2180 if (alist
== NULL
|| *alist
== NULL
)
2181 bpf_error("unknown host '%s'", name
);
2183 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
2185 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
2187 tmp
= gen_host(**alist
++, 0xffffffff,
2194 memset(&mask128
, 0xff, sizeof(mask128
));
2195 res0
= res
= pcap_nametoaddrinfo(name
);
2197 bpf_error("unknown host '%s'", name
);
2199 tproto
= tproto6
= proto
;
2200 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
2204 for (res
= res0
; res
; res
= res
->ai_next
) {
2205 switch (res
->ai_family
) {
2207 if (tproto
== Q_IPV6
)
2210 sin
= (struct sockaddr_in
*)
2212 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
2213 0xffffffff, tproto
, dir
);
2216 if (tproto6
== Q_IP
)
2219 sin6
= (struct sockaddr_in6
*)
2221 tmp
= gen_host6(&sin6
->sin6_addr
,
2222 &mask128
, tproto6
, dir
);
2231 bpf_error("unknown host '%s'%s", name
,
2232 (proto
== Q_DEFAULT
)
2234 : " for specified address family");
2241 if (proto
!= Q_DEFAULT
&& proto
!= Q_UDP
&& proto
!= Q_TCP
)
2242 bpf_error("illegal qualifier of 'port'");
2243 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
2244 bpf_error("unknown port '%s'", name
);
2245 if (proto
== Q_UDP
) {
2246 if (real_proto
== IPPROTO_TCP
)
2247 bpf_error("port '%s' is tcp", name
);
2249 /* override PROTO_UNDEF */
2250 real_proto
= IPPROTO_UDP
;
2252 if (proto
== Q_TCP
) {
2253 if (real_proto
== IPPROTO_UDP
)
2254 bpf_error("port '%s' is udp", name
);
2256 /* override PROTO_UNDEF */
2257 real_proto
= IPPROTO_TCP
;
2260 return gen_port(port
, real_proto
, dir
);
2264 b
= gen_port(port
, real_proto
, dir
);
2265 gen_or(gen_port6(port
, real_proto
, dir
), b
);
2272 eaddr
= pcap_ether_hostton(name
);
2274 bpf_error("unknown ether host: %s", name
);
2276 alist
= pcap_nametoaddr(name
);
2277 if (alist
== NULL
|| *alist
== NULL
)
2278 bpf_error("unknown host '%s'", name
);
2279 return gen_gateway(eaddr
, alist
, proto
, dir
);
2281 bpf_error("'gateway' not supported in this configuration");
2285 real_proto
= lookup_proto(name
, proto
);
2286 if (real_proto
>= 0)
2287 return gen_proto(real_proto
, proto
, dir
);
2289 bpf_error("unknown protocol: %s", name
);
2292 real_proto
= lookup_proto(name
, proto
);
2293 if (real_proto
>= 0)
2294 return gen_protochain(real_proto
, proto
, dir
);
2296 bpf_error("unknown protocol: %s", name
);
2308 gen_mcode(s1
, s2
, masklen
, q
)
2309 register const char *s1
, *s2
;
2310 register int masklen
;
2313 register int nlen
, mlen
;
2316 nlen
= __pcap_atoin(s1
, &n
);
2317 /* Promote short ipaddr */
2321 mlen
= __pcap_atoin(s2
, &m
);
2322 /* Promote short ipaddr */
2325 bpf_error("non-network bits set in \"%s mask %s\"",
2328 /* Convert mask len to mask */
2330 bpf_error("mask length must be <= 32");
2331 m
= 0xffffffff << (32 - masklen
);
2333 bpf_error("non-network bits set in \"%s/%d\"",
2340 return gen_host(n
, m
, q
.proto
, q
.dir
);
2343 bpf_error("Mask syntax for networks only");
2350 register const char *s
;
2355 int proto
= q
.proto
;
2361 else if (q
.proto
== Q_DECNET
)
2362 vlen
= __pcap_atodn(s
, &v
);
2364 vlen
= __pcap_atoin(s
, &v
);
2371 if (proto
== Q_DECNET
)
2372 return gen_host(v
, 0, proto
, dir
);
2373 else if (proto
== Q_LINK
) {
2374 bpf_error("illegal link layer address");
2377 if (s
== NULL
&& q
.addr
== Q_NET
) {
2378 /* Promote short net number */
2379 while (v
&& (v
& 0xff000000) == 0) {
2384 /* Promote short ipaddr */
2388 return gen_host(v
, mask
, proto
, dir
);
2393 proto
= IPPROTO_UDP
;
2394 else if (proto
== Q_TCP
)
2395 proto
= IPPROTO_TCP
;
2396 else if (proto
== Q_DEFAULT
)
2397 proto
= PROTO_UNDEF
;
2399 bpf_error("illegal qualifier of 'port'");
2402 return gen_port((int)v
, proto
, dir
);
2406 b
= gen_port((int)v
, proto
, dir
);
2407 gen_or(gen_port6((int)v
, proto
, dir
), b
);
2413 bpf_error("'gateway' requires a name");
2417 return gen_proto((int)v
, proto
, dir
);
2420 return gen_protochain((int)v
, proto
, dir
);
2435 gen_mcode6(s1
, s2
, masklen
, q
)
2436 register const char *s1
, *s2
;
2437 register int masklen
;
2440 struct addrinfo
*res
;
2441 struct in6_addr
*addr
;
2442 struct in6_addr mask
;
2447 bpf_error("no mask %s supported", s2
);
2449 res
= pcap_nametoaddrinfo(s1
);
2451 bpf_error("invalid ip6 address %s", s1
);
2453 bpf_error("%s resolved to multiple address", s1
);
2454 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
2456 if (sizeof(mask
) * 8 < masklen
)
2457 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
2458 memset(&mask
, 0xff, masklen
/ 8);
2460 mask
.s6_addr
[masklen
/ 8] =
2461 (0xff << (8 - masklen
% 8)) & 0xff;
2464 a
= (u_int32_t
*)addr
;
2465 m
= (u_int32_t
*)&mask
;
2466 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
2467 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
2468 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
2476 bpf_error("Mask syntax for networks only");
2480 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
2485 bpf_error("invalid qualifier against IPv6 address");
2493 register const u_char
*eaddr
;
2496 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
2497 if (linktype
== PCAP_ENCAP_ETHERNET
)
2498 return gen_ehostop(eaddr
, (int)q
.dir
);
2499 if (linktype
== PCAP_ENCAP_FDDI
)
2500 return gen_fhostop(eaddr
, (int)q
.dir
);
2501 if (linktype
== PCAP_ENCAP_TOKEN_RING
)
2502 return gen_thostop(eaddr
, (int)q
.dir
);
2504 bpf_error("ethernet address used in non-ether expression");
2510 struct slist
*s0
, *s1
;
2513 * This is definitely not the best way to do this, but the
2514 * lists will rarely get long.
2521 static struct slist
*
2527 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2532 static struct slist
*
2538 s
= new_stmt(BPF_LD
|BPF_MEM
);
2544 gen_load(proto
, index
, size
)
2549 struct slist
*s
, *tmp
;
2551 int regno
= alloc_reg();
2553 free_reg(index
->regno
);
2557 bpf_error("data size must be 1, 2, or 4");
2573 bpf_error("unsupported index operation");
2576 s
= xfer_to_x(index
);
2577 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2579 sappend(index
->s
, s
);
2594 /* XXX Note that we assume a fixed link header here. */
2595 s
= xfer_to_x(index
);
2596 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
2599 sappend(index
->s
, s
);
2601 b
= gen_proto_abbrev(proto
);
2603 gen_and(index
->b
, b
);
2613 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2615 sappend(s
, xfer_to_a(index
));
2616 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
2617 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
2618 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
2620 sappend(index
->s
, s
);
2622 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
2624 gen_and(index
->b
, b
);
2626 gen_and(gen_proto_abbrev(Q_IP
), b
);
2632 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2636 index
->regno
= regno
;
2637 s
= new_stmt(BPF_ST
);
2639 sappend(index
->s
, s
);
2645 gen_relation(code
, a0
, a1
, reversed
)
2647 struct arth
*a0
, *a1
;
2650 struct slist
*s0
, *s1
, *s2
;
2651 struct block
*b
, *tmp
;
2655 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
2656 b
= new_block(JMP(code
));
2657 if (code
== BPF_JGT
|| code
== BPF_JGE
) {
2658 reversed
= !reversed
;
2659 b
->s
.k
= 0x80000000;
2667 sappend(a0
->s
, a1
->s
);
2671 free_reg(a0
->regno
);
2672 free_reg(a1
->regno
);
2674 /* 'and' together protocol checks */
2677 gen_and(a0
->b
, tmp
= a1
->b
);
2693 int regno
= alloc_reg();
2694 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
2697 s
= new_stmt(BPF_LD
|BPF_LEN
);
2698 s
->next
= new_stmt(BPF_ST
);
2699 s
->next
->s
.k
= regno
;
2714 a
= (struct arth
*)newchunk(sizeof(*a
));
2718 s
= new_stmt(BPF_LD
|BPF_IMM
);
2720 s
->next
= new_stmt(BPF_ST
);
2736 s
= new_stmt(BPF_ALU
|BPF_NEG
);
2739 s
= new_stmt(BPF_ST
);
2747 gen_arth(code
, a0
, a1
)
2749 struct arth
*a0
, *a1
;
2751 struct slist
*s0
, *s1
, *s2
;
2755 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
2760 sappend(a0
->s
, a1
->s
);
2762 free_reg(a1
->regno
);
2764 s0
= new_stmt(BPF_ST
);
2765 a0
->regno
= s0
->s
.k
= alloc_reg();
2772 * Here we handle simple allocation of the scratch registers.
2773 * If too many registers are alloc'd, the allocator punts.
2775 static int regused
[BPF_MEMWORDS
];
2779 * Return the next free register.
2784 int n
= BPF_MEMWORDS
;
2787 if (regused
[curreg
])
2788 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
2790 regused
[curreg
] = 1;
2794 bpf_error("too many registers needed to evaluate expression");
2799 * Return a register to the table so it can
2809 static struct block
*
2816 s
= new_stmt(BPF_LD
|BPF_LEN
);
2817 b
= new_block(JMP(jmp
));
2828 return gen_len(BPF_JGE
, n
);
2837 b
= gen_len(BPF_JGT
, n
);
2844 gen_byteop(op
, idx
, val
)
2855 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2858 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2859 b
->s
.code
= JMP(BPF_JGE
);
2864 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
2865 b
->s
.code
= JMP(BPF_JGT
);
2869 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
2873 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2877 b
= new_block(JMP(BPF_JEQ
));
2885 gen_broadcast(proto
)
2888 bpf_u_int32 hostmask
;
2889 struct block
*b0
, *b1
, *b2
;
2890 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2896 if (linktype
== PCAP_ENCAP_ETHERNET
)
2897 return gen_ehostop(ebroadcast
, Q_DST
);
2898 if (linktype
== PCAP_ENCAP_FDDI
)
2899 return gen_fhostop(ebroadcast
, Q_DST
);
2900 if (linktype
== PCAP_ENCAP_TOKEN_RING
)
2901 return gen_thostop(ebroadcast
, Q_DST
);
2902 bpf_error("not a broadcast link");
2906 b0
= gen_linktype(ETHERTYPE_IP
);
2907 hostmask
= ~netmask
;
2908 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
2909 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
2910 (bpf_int32
)(~0 & hostmask
), hostmask
);
2915 bpf_error("only ether/ip broadcast filters supported");
2919 gen_multicast(proto
)
2922 register struct block
*b0
, *b1
;
2923 register struct slist
*s
;
2929 if (linktype
== PCAP_ENCAP_ETHERNET
) {
2930 /* ether[0] & 1 != 0 */
2931 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2933 b0
= new_block(JMP(BPF_JSET
));
2939 if (linktype
== PCAP_ENCAP_FDDI
) {
2940 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
2941 /* fddi[1] & 1 != 0 */
2942 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2944 b0
= new_block(JMP(BPF_JSET
));
2950 /* TODO - check how token ring handles multicast */
2951 /* if (linktype == PCAP_ENCAP_TOKEN_RING) ... */
2953 /* Link not known to support multicasts */
2957 b0
= gen_linktype(ETHERTYPE_IP
);
2958 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
2959 b1
->s
.code
= JMP(BPF_JGE
);
2965 b0
= gen_linktype(ETHERTYPE_IPV6
);
2966 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
2971 bpf_error("only IP multicast filters supported on ethernet/FDDI");
2975 * generate command for inbound/outbound. It's here so we can
2976 * make it link-type specific. 'dir' = 0 implies "inbound",
2977 * = 1 implies "outbound".
2983 register struct block
*b0
;
2985 b0
= gen_relation(BPF_JEQ
,
2986 gen_load(Q_LINK
, gen_loadi(0), 1),