]> The Tcpdump Group git mirrors - libpcap/blob - gencode.c
Move the code to generate code to check for a particular linktype in the
[libpcap] / gencode.c
1 /*#define CHASE_CHAIN*/
2 /*
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
5 *
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
17 * written permission.
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.
21 */
22 #ifndef lint
23 static const char rcsid[] _U_ =
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.211 2004-11-06 22:45:17 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #ifdef WIN32
32 #include <pcap-stdinc.h>
33 #else /* WIN32 */
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/time.h>
37 #endif /* WIN32 */
38
39 /*
40 * XXX - why was this included even on UNIX?
41 */
42 #ifdef __MINGW32__
43 #include "IP6_misc.h"
44 #endif
45
46 #ifndef WIN32
47
48 #ifdef __NetBSD__
49 #include <sys/param.h>
50 #endif
51
52 #include <netinet/in.h>
53
54 #endif /* WIN32 */
55
56 #include <stdlib.h>
57 #include <string.h>
58 #include <memory.h>
59 #include <setjmp.h>
60 #include <stdarg.h>
61
62 #include "pcap-int.h"
63
64 #include "ethertype.h"
65 #include "nlpid.h"
66 #include "llc.h"
67 #include "gencode.h"
68 #include "atmuni31.h"
69 #include "sunatmpos.h"
70 #include "ppp.h"
71 #include "sll.h"
72 #include "arcnet.h"
73 #include "pf.h"
74 #ifndef offsetof
75 #define offsetof(s, e) ((size_t)&((s *)0)->e)
76 #endif
77 #ifdef INET6
78 #ifndef WIN32
79 #include <netdb.h> /* for "struct addrinfo" */
80 #endif /* WIN32 */
81 #endif /*INET6*/
82 #include <pcap-namedb.h>
83
84 #define ETHERMTU 1500
85
86 #ifndef IPPROTO_SCTP
87 #define IPPROTO_SCTP 132
88 #endif
89
90 #ifdef HAVE_OS_PROTO_H
91 #include "os-proto.h"
92 #endif
93
94 #define JMP(c) ((c)|BPF_JMP|BPF_K)
95
96 /* Locals */
97 static jmp_buf top_ctx;
98 static pcap_t *bpf_pcap;
99
100 /* Hack for updating VLAN, MPLS offsets. */
101 static u_int orig_linktype = -1U, orig_nl = -1U, orig_nl_nosnap = -1U;
102
103 /* XXX */
104 #ifdef PCAP_FDDIPAD
105 int pcap_fddipad = PCAP_FDDIPAD;
106 #else
107 int pcap_fddipad;
108 #endif
109
110 /* VARARGS */
111 void
112 bpf_error(const char *fmt, ...)
113
114 {
115 va_list ap;
116
117 va_start(ap, fmt);
118 if (bpf_pcap != NULL)
119 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
120 fmt, ap);
121 va_end(ap);
122 longjmp(top_ctx, 1);
123 /* NOTREACHED */
124 }
125
126 static void init_linktype(int);
127
128 static int alloc_reg(void);
129 static void free_reg(int);
130
131 static struct block *root;
132
133 /*
134 * We divy out chunks of memory rather than call malloc each time so
135 * we don't have to worry about leaking memory. It's probably
136 * not a big deal if all this memory was wasted but it this ever
137 * goes into a library that would probably not be a good idea.
138 */
139 #define NCHUNKS 16
140 #define CHUNK0SIZE 1024
141 struct chunk {
142 u_int n_left;
143 void *m;
144 };
145
146 static struct chunk chunks[NCHUNKS];
147 static int cur_chunk;
148
149 static void *newchunk(u_int);
150 static void freechunks(void);
151 static inline struct block *new_block(int);
152 static inline struct slist *new_stmt(int);
153 static struct block *gen_retblk(int);
154 static inline void syntax(void);
155
156 static void backpatch(struct block *, struct block *);
157 static void merge(struct block *, struct block *);
158 static struct block *gen_cmp(u_int, u_int, bpf_int32);
159 static struct block *gen_cmp_gt(u_int, u_int, bpf_int32);
160 static struct block *gen_mcmp(u_int, u_int, bpf_int32, bpf_u_int32);
161 static struct block *gen_bcmp(u_int, u_int, const u_char *);
162 static struct block *gen_ncmp(bpf_u_int32, bpf_u_int32, bpf_u_int32,
163 bpf_u_int32, bpf_u_int32, int);
164 static struct block *gen_uncond(int);
165 static inline struct block *gen_true(void);
166 static inline struct block *gen_false(void);
167 static struct block *gen_ether_linktype(int);
168 static struct block *gen_linux_sll_linktype(int);
169 static struct block *gen_linktype(int);
170 static struct block *gen_snap(bpf_u_int32, bpf_u_int32, u_int);
171 static struct block *gen_llc(int);
172 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
173 #ifdef INET6
174 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
175 #endif
176 static struct block *gen_ahostop(const u_char *, int);
177 static struct block *gen_ehostop(const u_char *, int);
178 static struct block *gen_fhostop(const u_char *, int);
179 static struct block *gen_thostop(const u_char *, int);
180 static struct block *gen_wlanhostop(const u_char *, int);
181 static struct block *gen_ipfchostop(const u_char *, int);
182 static struct block *gen_dnhostop(bpf_u_int32, int, u_int);
183 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int);
184 #ifdef INET6
185 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int);
186 #endif
187 #ifndef INET6
188 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
189 #endif
190 static struct block *gen_ipfrag(void);
191 static struct block *gen_portatom(int, bpf_int32);
192 #ifdef INET6
193 static struct block *gen_portatom6(int, bpf_int32);
194 #endif
195 struct block *gen_portop(int, int, int);
196 static struct block *gen_port(int, int, int);
197 #ifdef INET6
198 struct block *gen_portop6(int, int, int);
199 static struct block *gen_port6(int, int, int);
200 #endif
201 static int lookup_proto(const char *, int);
202 static struct block *gen_protochain(int, int, int);
203 static struct block *gen_proto(int, int, int);
204 static struct slist *xfer_to_x(struct arth *);
205 static struct slist *xfer_to_a(struct arth *);
206 static struct block *gen_mac_multicast(int);
207 static struct block *gen_len(int, int);
208
209 static struct block *gen_msg_abbrev(int type);
210
211 static void *
212 newchunk(n)
213 u_int n;
214 {
215 struct chunk *cp;
216 int k;
217 size_t size;
218
219 #ifndef __NetBSD__
220 /* XXX Round up to nearest long. */
221 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
222 #else
223 /* XXX Round up to structure boundary. */
224 n = ALIGN(n);
225 #endif
226
227 cp = &chunks[cur_chunk];
228 if (n > cp->n_left) {
229 ++cp, k = ++cur_chunk;
230 if (k >= NCHUNKS)
231 bpf_error("out of memory");
232 size = CHUNK0SIZE << k;
233 cp->m = (void *)malloc(size);
234 if (cp->m == NULL)
235 bpf_error("out of memory");
236 memset((char *)cp->m, 0, size);
237 cp->n_left = size;
238 if (n > size)
239 bpf_error("out of memory");
240 }
241 cp->n_left -= n;
242 return (void *)((char *)cp->m + cp->n_left);
243 }
244
245 static void
246 freechunks()
247 {
248 int i;
249
250 cur_chunk = 0;
251 for (i = 0; i < NCHUNKS; ++i)
252 if (chunks[i].m != NULL) {
253 free(chunks[i].m);
254 chunks[i].m = NULL;
255 }
256 }
257
258 /*
259 * A strdup whose allocations are freed after code generation is over.
260 */
261 char *
262 sdup(s)
263 register const char *s;
264 {
265 int n = strlen(s) + 1;
266 char *cp = newchunk(n);
267
268 strlcpy(cp, s, n);
269 return (cp);
270 }
271
272 static inline struct block *
273 new_block(code)
274 int code;
275 {
276 struct block *p;
277
278 p = (struct block *)newchunk(sizeof(*p));
279 p->s.code = code;
280 p->head = p;
281
282 return p;
283 }
284
285 static inline struct slist *
286 new_stmt(code)
287 int code;
288 {
289 struct slist *p;
290
291 p = (struct slist *)newchunk(sizeof(*p));
292 p->s.code = code;
293
294 return p;
295 }
296
297 static struct block *
298 gen_retblk(v)
299 int v;
300 {
301 struct block *b = new_block(BPF_RET|BPF_K);
302
303 b->s.k = v;
304 return b;
305 }
306
307 static inline void
308 syntax()
309 {
310 bpf_error("syntax error in filter expression");
311 }
312
313 static bpf_u_int32 netmask;
314 static int snaplen;
315 int no_optimize;
316
317 int
318 pcap_compile(pcap_t *p, struct bpf_program *program,
319 char *buf, int optimize, bpf_u_int32 mask)
320 {
321 extern int n_errors;
322 int len;
323
324 no_optimize = 0;
325 n_errors = 0;
326 root = NULL;
327 bpf_pcap = p;
328 if (setjmp(top_ctx)) {
329 lex_cleanup();
330 freechunks();
331 return (-1);
332 }
333
334 netmask = mask;
335
336 snaplen = pcap_snapshot(p);
337 if (snaplen == 0) {
338 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
339 "snaplen of 0 rejects all packets");
340 return -1;
341 }
342
343 lex_init(buf ? buf : "");
344 init_linktype(pcap_datalink(p));
345 (void)pcap_parse();
346
347 if (n_errors)
348 syntax();
349
350 if (root == NULL)
351 root = gen_retblk(snaplen);
352
353 if (optimize && !no_optimize) {
354 bpf_optimize(&root);
355 if (root == NULL ||
356 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
357 bpf_error("expression rejects all packets");
358 }
359 program->bf_insns = icode_to_fcode(root, &len);
360 program->bf_len = len;
361
362 lex_cleanup();
363 freechunks();
364 return (0);
365 }
366
367 /*
368 * entry point for using the compiler with no pcap open
369 * pass in all the stuff that is needed explicitly instead.
370 */
371 int
372 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
373 struct bpf_program *program,
374 char *buf, int optimize, bpf_u_int32 mask)
375 {
376 pcap_t *p;
377 int ret;
378
379 p = pcap_open_dead(linktype_arg, snaplen_arg);
380 if (p == NULL)
381 return (-1);
382 ret = pcap_compile(p, program, buf, optimize, mask);
383 pcap_close(p);
384 return (ret);
385 }
386
387 /*
388 * Clean up a "struct bpf_program" by freeing all the memory allocated
389 * in it.
390 */
391 void
392 pcap_freecode(struct bpf_program *program)
393 {
394 program->bf_len = 0;
395 if (program->bf_insns != NULL) {
396 free((char *)program->bf_insns);
397 program->bf_insns = NULL;
398 }
399 }
400
401 /*
402 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
403 * which of the jt and jf fields has been resolved and which is a pointer
404 * back to another unresolved block (or nil). At least one of the fields
405 * in each block is already resolved.
406 */
407 static void
408 backpatch(list, target)
409 struct block *list, *target;
410 {
411 struct block *next;
412
413 while (list) {
414 if (!list->sense) {
415 next = JT(list);
416 JT(list) = target;
417 } else {
418 next = JF(list);
419 JF(list) = target;
420 }
421 list = next;
422 }
423 }
424
425 /*
426 * Merge the lists in b0 and b1, using the 'sense' field to indicate
427 * which of jt and jf is the link.
428 */
429 static void
430 merge(b0, b1)
431 struct block *b0, *b1;
432 {
433 register struct block **p = &b0;
434
435 /* Find end of list. */
436 while (*p)
437 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
438
439 /* Concatenate the lists. */
440 *p = b1;
441 }
442
443 void
444 finish_parse(p)
445 struct block *p;
446 {
447 backpatch(p, gen_retblk(snaplen));
448 p->sense = !p->sense;
449 backpatch(p, gen_retblk(0));
450 root = p->head;
451 }
452
453 void
454 gen_and(b0, b1)
455 struct block *b0, *b1;
456 {
457 backpatch(b0, b1->head);
458 b0->sense = !b0->sense;
459 b1->sense = !b1->sense;
460 merge(b1, b0);
461 b1->sense = !b1->sense;
462 b1->head = b0->head;
463 }
464
465 void
466 gen_or(b0, b1)
467 struct block *b0, *b1;
468 {
469 b0->sense = !b0->sense;
470 backpatch(b0, b1->head);
471 b0->sense = !b0->sense;
472 merge(b1, b0);
473 b1->head = b0->head;
474 }
475
476 void
477 gen_not(b)
478 struct block *b;
479 {
480 b->sense = !b->sense;
481 }
482
483 static struct block *
484 gen_cmp(offset, size, v)
485 u_int offset, size;
486 bpf_int32 v;
487 {
488 struct slist *s;
489 struct block *b;
490
491 s = new_stmt(BPF_LD|BPF_ABS|size);
492 s->s.k = offset;
493
494 b = new_block(JMP(BPF_JEQ));
495 b->stmts = s;
496 b->s.k = v;
497
498 return b;
499 }
500
501 static struct block *
502 gen_cmp_gt(offset, size, v)
503 u_int offset, size;
504 bpf_int32 v;
505 {
506 struct slist *s;
507 struct block *b;
508
509 s = new_stmt(BPF_LD|BPF_ABS|size);
510 s->s.k = offset;
511
512 b = new_block(JMP(BPF_JGT));
513 b->stmts = s;
514 b->s.k = v;
515
516 return b;
517 }
518
519 static struct block *
520 gen_mcmp(offset, size, v, mask)
521 u_int offset, size;
522 bpf_int32 v;
523 bpf_u_int32 mask;
524 {
525 struct block *b = gen_cmp(offset, size, v);
526 struct slist *s;
527
528 if (mask != 0xffffffff) {
529 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
530 s->s.k = mask;
531 b->stmts->next = s;
532 }
533 return b;
534 }
535
536 static struct block *
537 gen_bcmp(offset, size, v)
538 register u_int offset, size;
539 register const u_char *v;
540 {
541 register struct block *b, *tmp;
542
543 b = NULL;
544 while (size >= 4) {
545 register const u_char *p = &v[size - 4];
546 bpf_int32 w = ((bpf_int32)p[0] << 24) |
547 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
548
549 tmp = gen_cmp(offset + size - 4, BPF_W, w);
550 if (b != NULL)
551 gen_and(b, tmp);
552 b = tmp;
553 size -= 4;
554 }
555 while (size >= 2) {
556 register const u_char *p = &v[size - 2];
557 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
558
559 tmp = gen_cmp(offset + size - 2, BPF_H, w);
560 if (b != NULL)
561 gen_and(b, tmp);
562 b = tmp;
563 size -= 2;
564 }
565 if (size > 0) {
566 tmp = gen_cmp(offset, BPF_B, (bpf_int32)v[0]);
567 if (b != NULL)
568 gen_and(b, tmp);
569 b = tmp;
570 }
571 return b;
572 }
573
574 static struct block *
575 gen_ncmp(datasize, offset, mask, jtype, jvalue, reverse)
576 bpf_u_int32 datasize, offset, mask, jtype, jvalue;
577 int reverse;
578 {
579 struct slist *s;
580 struct block *b;
581
582 s = new_stmt(BPF_LD|datasize|BPF_ABS);
583 s->s.k = offset;
584
585 if (mask != 0xffffffff) {
586 s->next = new_stmt(BPF_ALU|BPF_AND|BPF_K);
587 s->next->s.k = mask;
588 }
589
590 b = new_block(JMP(jtype));
591 b->stmts = s;
592 b->s.k = jvalue;
593 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
594 gen_not(b);
595 return b;
596 }
597
598 /*
599 * Various code constructs need to know the layout of the data link
600 * layer. These variables give the necessary offsets.
601 */
602
603 /*
604 * This is the offset of the beginning of the MAC-layer header.
605 * It's usually 0, except for ATM LANE.
606 */
607 static u_int off_mac;
608
609 /*
610 * "off_linktype" is the offset to information in the link-layer header
611 * giving the packet type.
612 *
613 * For Ethernet, it's the offset of the Ethernet type field.
614 *
615 * For link-layer types that always use 802.2 headers, it's the
616 * offset of the LLC header.
617 *
618 * For PPP, it's the offset of the PPP type field.
619 *
620 * For Cisco HDLC, it's the offset of the CHDLC type field.
621 *
622 * For BSD loopback, it's the offset of the AF_ value.
623 *
624 * For Linux cooked sockets, it's the offset of the type field.
625 *
626 * It's set to -1 for no encapsulation, in which case, IP is assumed.
627 */
628 static u_int off_linktype;
629
630 /*
631 * TRUE if the link layer includes an ATM pseudo-header.
632 */
633 static int is_atm = 0;
634
635 /*
636 * TRUE if "lane" appeared in the filter; it causes us to generate
637 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
638 */
639 static int is_lane = 0;
640
641 /*
642 * These are offsets for the ATM pseudo-header.
643 */
644 static u_int off_vpi;
645 static u_int off_vci;
646 static u_int off_proto;
647
648 /*
649 * This is the offset of the first byte after the ATM pseudo_header,
650 * or -1 if there is no ATM pseudo-header.
651 */
652 static u_int off_payload;
653
654 /*
655 * These are offsets to the beginning of the network-layer header.
656 *
657 * If the link layer never uses 802.2 LLC:
658 *
659 * "off_nl" and "off_nl_nosnap" are the same.
660 *
661 * If the link layer always uses 802.2 LLC:
662 *
663 * "off_nl" is the offset if there's a SNAP header following
664 * the 802.2 header;
665 *
666 * "off_nl_nosnap" is the offset if there's no SNAP header.
667 *
668 * If the link layer is Ethernet:
669 *
670 * "off_nl" is the offset if the packet is an Ethernet II packet
671 * (we assume no 802.3+802.2+SNAP);
672 *
673 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
674 * with an 802.2 header following it.
675 */
676 static u_int off_nl;
677 static u_int off_nl_nosnap;
678
679 static int linktype;
680
681 static void
682 init_linktype(type)
683 int type;
684 {
685 linktype = type;
686
687 /*
688 * Assume it's not raw ATM with a pseudo-header, for now.
689 */
690 off_mac = 0;
691 is_atm = 0;
692 is_lane = 0;
693 off_vpi = -1;
694 off_vci = -1;
695 off_proto = -1;
696 off_payload = -1;
697
698 orig_linktype = -1;
699 orig_nl = -1;
700 orig_nl_nosnap = -1;
701
702 switch (type) {
703
704 case DLT_ARCNET:
705 off_linktype = 2;
706 off_nl = 6; /* XXX in reality, variable! */
707 off_nl_nosnap = 6; /* no 802.2 LLC */
708 return;
709
710 case DLT_ARCNET_LINUX:
711 off_linktype = 4;
712 off_nl = 8; /* XXX in reality, variable! */
713 off_nl_nosnap = 8; /* no 802.2 LLC */
714 return;
715
716 case DLT_EN10MB:
717 off_linktype = 12;
718 off_nl = 14; /* Ethernet II */
719 off_nl_nosnap = 17; /* 802.3+802.2 */
720 return;
721
722 case DLT_SLIP:
723 /*
724 * SLIP doesn't have a link level type. The 16 byte
725 * header is hacked into our SLIP driver.
726 */
727 off_linktype = -1;
728 off_nl = 16;
729 off_nl_nosnap = 16; /* no 802.2 LLC */
730 return;
731
732 case DLT_SLIP_BSDOS:
733 /* XXX this may be the same as the DLT_PPP_BSDOS case */
734 off_linktype = -1;
735 /* XXX end */
736 off_nl = 24;
737 off_nl_nosnap = 24; /* no 802.2 LLC */
738 return;
739
740 case DLT_NULL:
741 case DLT_LOOP:
742 off_linktype = 0;
743 off_nl = 4;
744 off_nl_nosnap = 4; /* no 802.2 LLC */
745 return;
746
747 case DLT_ENC:
748 off_linktype = 0;
749 off_nl = 12;
750 off_nl_nosnap = 12; /* no 802.2 LLC */
751 return;
752
753 case DLT_PPP:
754 case DLT_PPP_WITHDIRECTION:
755 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
756 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
757 off_linktype = 2;
758 off_nl = 4;
759 off_nl_nosnap = 4; /* no 802.2 LLC */
760 return;
761
762 case DLT_PPP_ETHER:
763 /*
764 * This does no include the Ethernet header, and
765 * only covers session state.
766 */
767 off_linktype = 6;
768 off_nl = 8;
769 off_nl_nosnap = 8; /* no 802.2 LLC */
770 return;
771
772 case DLT_PPP_BSDOS:
773 off_linktype = 5;
774 off_nl = 24;
775 off_nl_nosnap = 24; /* no 802.2 LLC */
776 return;
777
778 case DLT_FDDI:
779 /*
780 * FDDI doesn't really have a link-level type field.
781 * We set "off_linktype" to the offset of the LLC header.
782 *
783 * To check for Ethernet types, we assume that SSAP = SNAP
784 * is being used and pick out the encapsulated Ethernet type.
785 * XXX - should we generate code to check for SNAP?
786 */
787 off_linktype = 13;
788 #ifdef PCAP_FDDIPAD
789 off_linktype += pcap_fddipad;
790 #endif
791 off_nl = 21; /* FDDI+802.2+SNAP */
792 off_nl_nosnap = 16; /* FDDI+802.2 */
793 #ifdef PCAP_FDDIPAD
794 off_nl += pcap_fddipad;
795 off_nl_nosnap += pcap_fddipad;
796 #endif
797 return;
798
799 case DLT_IEEE802:
800 /*
801 * Token Ring doesn't really have a link-level type field.
802 * We set "off_linktype" to the offset of the LLC header.
803 *
804 * To check for Ethernet types, we assume that SSAP = SNAP
805 * is being used and pick out the encapsulated Ethernet type.
806 * XXX - should we generate code to check for SNAP?
807 *
808 * XXX - the header is actually variable-length.
809 * Some various Linux patched versions gave 38
810 * as "off_linktype" and 40 as "off_nl"; however,
811 * if a token ring packet has *no* routing
812 * information, i.e. is not source-routed, the correct
813 * values are 20 and 22, as they are in the vanilla code.
814 *
815 * A packet is source-routed iff the uppermost bit
816 * of the first byte of the source address, at an
817 * offset of 8, has the uppermost bit set. If the
818 * packet is source-routed, the total number of bytes
819 * of routing information is 2 plus bits 0x1F00 of
820 * the 16-bit value at an offset of 14 (shifted right
821 * 8 - figure out which byte that is).
822 */
823 off_linktype = 14;
824 off_nl = 22; /* Token Ring+802.2+SNAP */
825 off_nl_nosnap = 17; /* Token Ring+802.2 */
826 return;
827
828 case DLT_IEEE802_11:
829 /*
830 * 802.11 doesn't really have a link-level type field.
831 * We set "off_linktype" to the offset of the LLC header.
832 *
833 * To check for Ethernet types, we assume that SSAP = SNAP
834 * is being used and pick out the encapsulated Ethernet type.
835 * XXX - should we generate code to check for SNAP?
836 *
837 * XXX - the header is actually variable-length. We
838 * assume a 24-byte link-layer header, as appears in
839 * data frames in networks with no bridges. If the
840 * fromds and tods 802.11 header bits are both set,
841 * it's actually supposed to be 30 bytes.
842 */
843 off_linktype = 24;
844 off_nl = 32; /* 802.11+802.2+SNAP */
845 off_nl_nosnap = 27; /* 802.11+802.2 */
846 return;
847
848 case DLT_PRISM_HEADER:
849 /*
850 * Same as 802.11, but with an additional header before
851 * the 802.11 header, containing a bunch of additional
852 * information including radio-level information.
853 *
854 * The header is 144 bytes long.
855 *
856 * XXX - same variable-length header problem; at least
857 * the Prism header is fixed-length.
858 */
859 off_linktype = 144+24;
860 off_nl = 144+32; /* Prism+802.11+802.2+SNAP */
861 off_nl_nosnap = 144+27; /* Prism+802.11+802.2 */
862 return;
863
864 case DLT_IEEE802_11_RADIO_AVS:
865 /*
866 * Same as 802.11, but with an additional header before
867 * the 802.11 header, containing a bunch of additional
868 * information including radio-level information.
869 *
870 * The header is 64 bytes long, at least in its
871 * current incarnation.
872 *
873 * XXX - same variable-length header problem, only
874 * more so; this header is also variable-length,
875 * with the length being the 32-bit big-endian
876 * number at an offset of 4 from the beginning
877 * of the radio header.
878 */
879 off_linktype = 64+24;
880 off_nl = 64+32; /* Radio+802.11+802.2+SNAP */
881 off_nl_nosnap = 64+27; /* Radio+802.11+802.2 */
882 return;
883
884 case DLT_IEEE802_11_RADIO:
885 /*
886 * Same as 802.11, but with an additional header before
887 * the 802.11 header, containing a bunch of additional
888 * information including radio-level information.
889 *
890 * XXX - same variable-length header problem, only
891 * even *more* so; this header is also variable-length,
892 * with the length being the 16-bit number at an offset
893 * of 2 from the beginning of the radio header, and it's
894 * device-dependent (different devices might supply
895 * different amounts of information), so we can't even
896 * assume a fixed length for the current version of the
897 * header.
898 *
899 * Therefore, currently, only raw "link[N:M]" filtering is
900 * supported.
901 */
902 off_linktype = -1;
903 off_nl = -1;
904 off_nl_nosnap = -1;
905 return;
906
907 case DLT_ATM_RFC1483:
908 case DLT_ATM_CLIP: /* Linux ATM defines this */
909 /*
910 * assume routed, non-ISO PDUs
911 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
912 */
913 off_linktype = 0;
914 off_nl = 8; /* 802.2+SNAP */
915 off_nl_nosnap = 3; /* 802.2 */
916 return;
917
918 case DLT_SUNATM:
919 /*
920 * Full Frontal ATM; you get AALn PDUs with an ATM
921 * pseudo-header.
922 */
923 is_atm = 1;
924 off_vpi = SUNATM_VPI_POS;
925 off_vci = SUNATM_VCI_POS;
926 off_proto = PROTO_POS;
927 off_mac = -1; /* LLC-encapsulated, so no MAC-layer header */
928 off_payload = SUNATM_PKT_BEGIN_POS;
929 off_linktype = off_payload;
930 off_nl = off_payload+8; /* 802.2+SNAP */
931 off_nl_nosnap = off_payload+3; /* 802.2 */
932 return;
933
934 case DLT_RAW:
935 off_linktype = -1;
936 off_nl = 0;
937 off_nl_nosnap = 0; /* no 802.2 LLC */
938 return;
939
940 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
941 off_linktype = 14;
942 off_nl = 16;
943 off_nl_nosnap = 16; /* no 802.2 LLC */
944 return;
945
946 case DLT_LTALK:
947 /*
948 * LocalTalk does have a 1-byte type field in the LLAP header,
949 * but really it just indicates whether there is a "short" or
950 * "long" DDP packet following.
951 */
952 off_linktype = -1;
953 off_nl = 0;
954 off_nl_nosnap = 0; /* no 802.2 LLC */
955 return;
956
957 case DLT_IP_OVER_FC:
958 /*
959 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
960 * link-level type field. We set "off_linktype" to the
961 * offset of the LLC header.
962 *
963 * To check for Ethernet types, we assume that SSAP = SNAP
964 * is being used and pick out the encapsulated Ethernet type.
965 * XXX - should we generate code to check for SNAP? RFC
966 * 2625 says SNAP should be used.
967 */
968 off_linktype = 16;
969 off_nl = 24; /* IPFC+802.2+SNAP */
970 off_nl_nosnap = 19; /* IPFC+802.2 */
971 return;
972
973 case DLT_FRELAY:
974 /*
975 * XXX - we should set this to handle SNAP-encapsulated
976 * frames (NLPID of 0x80).
977 */
978 off_linktype = -1;
979 off_nl = 0;
980 off_nl_nosnap = 0; /* no 802.2 LLC */
981 return;
982
983 case DLT_APPLE_IP_OVER_IEEE1394:
984 off_linktype = 16;
985 off_nl = 18;
986 off_nl_nosnap = 0; /* no 802.2 LLC */
987 return;
988
989 case DLT_LINUX_IRDA:
990 /*
991 * Currently, only raw "link[N:M]" filtering is supported.
992 */
993 off_linktype = -1;
994 off_nl = -1;
995 off_nl_nosnap = -1;
996 return;
997
998 case DLT_DOCSIS:
999 /*
1000 * Currently, only raw "link[N:M]" filtering is supported.
1001 */
1002 off_linktype = -1;
1003 off_nl = -1;
1004 off_nl_nosnap = -1;
1005 return;
1006
1007 case DLT_SYMANTEC_FIREWALL:
1008 off_linktype = 6;
1009 off_nl = 44; /* Ethernet II */
1010 off_nl_nosnap = 44; /* XXX - what does it do with 802.3 packets? */
1011 return;
1012
1013 case DLT_PFLOG:
1014 off_linktype = 0;
1015 /* XXX read from header? */
1016 off_nl = PFLOG_HDRLEN;
1017 off_nl_nosnap = PFLOG_HDRLEN;
1018 return;
1019
1020 case DLT_JUNIPER_ATM1:
1021 off_linktype = 4; /* in reality variable between 4-8 */
1022 off_nl = 4;
1023 off_nl_nosnap = 14;
1024 return;
1025
1026 case DLT_JUNIPER_ATM2:
1027 off_linktype = 8; /* in reality variable between 8-12 */
1028 off_nl = 8;
1029 off_nl_nosnap = 18;
1030 return;
1031
1032 #ifdef DLT_PFSYNC
1033 case DLT_PFSYNC:
1034 off_linktype = -1;
1035 off_nl = 4;
1036 off_nl_nosnap = 4;
1037 return;
1038 #endif
1039 }
1040 bpf_error("unknown data link type %d", linktype);
1041 /* NOTREACHED */
1042 }
1043
1044 static struct block *
1045 gen_uncond(rsense)
1046 int rsense;
1047 {
1048 struct block *b;
1049 struct slist *s;
1050
1051 s = new_stmt(BPF_LD|BPF_IMM);
1052 s->s.k = !rsense;
1053 b = new_block(JMP(BPF_JEQ));
1054 b->stmts = s;
1055
1056 return b;
1057 }
1058
1059 static inline struct block *
1060 gen_true()
1061 {
1062 return gen_uncond(1);
1063 }
1064
1065 static inline struct block *
1066 gen_false()
1067 {
1068 return gen_uncond(0);
1069 }
1070
1071 /*
1072 * Byte-swap a 32-bit number.
1073 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1074 * big-endian platforms.)
1075 */
1076 #define SWAPLONG(y) \
1077 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1078
1079 static struct block *
1080 gen_ether_linktype(proto)
1081 register int proto;
1082 {
1083 struct block *b0, *b1;
1084
1085 switch (proto) {
1086
1087 case LLCSAP_ISONS:
1088 /*
1089 * OSI protocols always use 802.2 encapsulation.
1090 * XXX - should we check both the DSAP and the
1091 * SSAP, like this, or should we check just the
1092 * DSAP?
1093 */
1094 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU);
1095 gen_not(b0);
1096 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)
1097 ((LLCSAP_ISONS << 8) | LLCSAP_ISONS));
1098 gen_and(b0, b1);
1099 return b1;
1100
1101 case LLCSAP_IP:
1102 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU);
1103 gen_not(b0);
1104 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)
1105 ((LLCSAP_IP << 8) | LLCSAP_IP));
1106 gen_and(b0, b1);
1107 return b1;
1108
1109 case LLCSAP_NETBEUI:
1110 /*
1111 * NetBEUI always uses 802.2 encapsulation.
1112 * XXX - should we check both the DSAP and the
1113 * SSAP, like this, or should we check just the
1114 * DSAP?
1115 */
1116 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU);
1117 gen_not(b0);
1118 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)
1119 ((LLCSAP_NETBEUI << 8) | LLCSAP_NETBEUI));
1120 gen_and(b0, b1);
1121 return b1;
1122
1123 case LLCSAP_IPX:
1124 /*
1125 * Check for;
1126 *
1127 * Ethernet_II frames, which are Ethernet
1128 * frames with a frame type of ETHERTYPE_IPX;
1129 *
1130 * Ethernet_802.3 frames, which are 802.3
1131 * frames (i.e., the type/length field is
1132 * a length field, <= ETHERMTU, rather than
1133 * a type field) with the first two bytes
1134 * after the Ethernet/802.3 header being
1135 * 0xFFFF;
1136 *
1137 * Ethernet_802.2 frames, which are 802.3
1138 * frames with an 802.2 LLC header and
1139 * with the IPX LSAP as the DSAP in the LLC
1140 * header;
1141 *
1142 * Ethernet_SNAP frames, which are 802.3
1143 * frames with an LLC header and a SNAP
1144 * header and with an OUI of 0x000000
1145 * (encapsulated Ethernet) and a protocol
1146 * ID of ETHERTYPE_IPX in the SNAP header.
1147 *
1148 * XXX - should we generate the same code both
1149 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1150 */
1151
1152 /*
1153 * This generates code to check both for the
1154 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1155 */
1156 b0 = gen_cmp(off_linktype + 2, BPF_B, (bpf_int32)LLCSAP_IPX);
1157 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)0xFFFF);
1158 gen_or(b0, b1);
1159
1160 /*
1161 * Now we add code to check for SNAP frames with
1162 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1163 */
1164 b0 = gen_snap(0x000000, ETHERTYPE_IPX, 14);
1165 gen_or(b0, b1);
1166
1167 /*
1168 * Now we generate code to check for 802.3
1169 * frames in general.
1170 */
1171 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU);
1172 gen_not(b0);
1173
1174 /*
1175 * Now add the check for 802.3 frames before the
1176 * check for Ethernet_802.2 and Ethernet_802.3,
1177 * as those checks should only be done on 802.3
1178 * frames, not on Ethernet frames.
1179 */
1180 gen_and(b0, b1);
1181
1182 /*
1183 * Now add the check for Ethernet_II frames, and
1184 * do that before checking for the other frame
1185 * types.
1186 */
1187 b0 = gen_cmp(off_linktype, BPF_H, (bpf_int32)ETHERTYPE_IPX);
1188 gen_or(b0, b1);
1189 return b1;
1190
1191 case ETHERTYPE_ATALK:
1192 case ETHERTYPE_AARP:
1193 /*
1194 * EtherTalk (AppleTalk protocols on Ethernet link
1195 * layer) may use 802.2 encapsulation.
1196 */
1197
1198 /*
1199 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1200 * we check for an Ethernet type field less than
1201 * 1500, which means it's an 802.3 length field.
1202 */
1203 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU);
1204 gen_not(b0);
1205
1206 /*
1207 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1208 * SNAP packets with an organization code of
1209 * 0x080007 (Apple, for Appletalk) and a protocol
1210 * type of ETHERTYPE_ATALK (Appletalk).
1211 *
1212 * 802.2-encapsulated ETHERTYPE_AARP packets are
1213 * SNAP packets with an organization code of
1214 * 0x000000 (encapsulated Ethernet) and a protocol
1215 * type of ETHERTYPE_AARP (Appletalk ARP).
1216 */
1217 if (proto == ETHERTYPE_ATALK)
1218 b1 = gen_snap(0x080007, ETHERTYPE_ATALK, 14);
1219 else /* proto == ETHERTYPE_AARP */
1220 b1 = gen_snap(0x000000, ETHERTYPE_AARP, 14);
1221 gen_and(b0, b1);
1222
1223 /*
1224 * Check for Ethernet encapsulation (Ethertalk
1225 * phase 1?); we just check for the Ethernet
1226 * protocol type.
1227 */
1228 b0 = gen_cmp(off_linktype, BPF_H, (bpf_int32)proto);
1229
1230 gen_or(b0, b1);
1231 return b1;
1232
1233 default:
1234 if (proto <= ETHERMTU) {
1235 /*
1236 * This is an LLC SAP value, so the frames
1237 * that match would be 802.2 frames.
1238 * Check that the frame is an 802.2 frame
1239 * (i.e., that the length/type field is
1240 * a length field, <= ETHERMTU) and
1241 * then check the DSAP.
1242 */
1243 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU);
1244 gen_not(b0);
1245 b1 = gen_cmp(off_linktype + 2, BPF_B, (bpf_int32)proto);
1246 gen_and(b0, b1);
1247 return b1;
1248 } else {
1249 /*
1250 * This is an Ethernet type, so compare
1251 * the length/type field with it (if
1252 * the frame is an 802.2 frame, the length
1253 * field will be <= ETHERMTU, and, as
1254 * "proto" is > ETHERMTU, this test
1255 * will fail and the frame won't match,
1256 * which is what we want).
1257 */
1258 return gen_cmp(off_linktype, BPF_H, (bpf_int32)proto);
1259 }
1260 }
1261 }
1262
1263 static struct block *
1264 gen_linux_sll_linktype(proto)
1265 register int proto;
1266 {
1267 struct block *b0, *b1;
1268
1269 switch (proto) {
1270
1271 case LLCSAP_IP:
1272 b0 = gen_cmp(off_linktype, BPF_H, LINUX_SLL_P_802_2);
1273 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)
1274 ((LLCSAP_IP << 8) | LLCSAP_IP));
1275 gen_and(b0, b1);
1276 return b1;
1277
1278 case LLCSAP_ISONS:
1279 /*
1280 * OSI protocols always use 802.2 encapsulation.
1281 * XXX - should we check both the DSAP and the
1282 * LSAP, like this, or should we check just the
1283 * DSAP?
1284 */
1285 b0 = gen_cmp(off_linktype, BPF_H, LINUX_SLL_P_802_2);
1286 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)
1287 ((LLCSAP_ISONS << 8) | LLCSAP_ISONS));
1288 gen_and(b0, b1);
1289 return b1;
1290
1291 case LLCSAP_NETBEUI:
1292 /*
1293 * NetBEUI always uses 802.2 encapsulation.
1294 * XXX - should we check both the DSAP and the
1295 * LSAP, like this, or should we check just the
1296 * DSAP?
1297 */
1298 b0 = gen_cmp(off_linktype, BPF_H, LINUX_SLL_P_802_2);
1299 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)
1300 ((LLCSAP_NETBEUI << 8) | LLCSAP_NETBEUI));
1301 gen_and(b0, b1);
1302 return b1;
1303
1304 case LLCSAP_IPX:
1305 /*
1306 * Ethernet_II frames, which are Ethernet
1307 * frames with a frame type of ETHERTYPE_IPX;
1308 *
1309 * Ethernet_802.3 frames, which have a frame
1310 * type of LINUX_SLL_P_802_3;
1311 *
1312 * Ethernet_802.2 frames, which are 802.3
1313 * frames with an 802.2 LLC header (i.e, have
1314 * a frame type of LINUX_SLL_P_802_2) and
1315 * with the IPX LSAP as the DSAP in the LLC
1316 * header;
1317 *
1318 * Ethernet_SNAP frames, which are 802.3
1319 * frames with an LLC header and a SNAP
1320 * header and with an OUI of 0x000000
1321 * (encapsulated Ethernet) and a protocol
1322 * ID of ETHERTYPE_IPX in the SNAP header.
1323 *
1324 * First, do the checks on LINUX_SLL_P_802_2
1325 * frames; generate the check for either
1326 * Ethernet_802.2 or Ethernet_SNAP frames, and
1327 * then put a check for LINUX_SLL_P_802_2 frames
1328 * before it.
1329 */
1330 b0 = gen_cmp(off_linktype + 2, BPF_B,
1331 (bpf_int32)LLCSAP_IPX);
1332 b1 = gen_snap(0x000000, ETHERTYPE_IPX,
1333 off_linktype + 2);
1334 gen_or(b0, b1);
1335 b0 = gen_cmp(off_linktype, BPF_H, LINUX_SLL_P_802_2);
1336 gen_and(b0, b1);
1337
1338 /*
1339 * Now check for 802.3 frames and OR that with
1340 * the previous test.
1341 */
1342 b0 = gen_cmp(off_linktype, BPF_H, LINUX_SLL_P_802_3);
1343 gen_or(b0, b1);
1344
1345 /*
1346 * Now add the check for Ethernet_II frames, and
1347 * do that before checking for the other frame
1348 * types.
1349 */
1350 b0 = gen_cmp(off_linktype, BPF_H,
1351 (bpf_int32)ETHERTYPE_IPX);
1352 gen_or(b0, b1);
1353 return b1;
1354
1355 case ETHERTYPE_ATALK:
1356 case ETHERTYPE_AARP:
1357 /*
1358 * EtherTalk (AppleTalk protocols on Ethernet link
1359 * layer) may use 802.2 encapsulation.
1360 */
1361
1362 /*
1363 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1364 * we check for the 802.2 protocol type in the
1365 * "Ethernet type" field.
1366 */
1367 b0 = gen_cmp(off_linktype, BPF_H, LINUX_SLL_P_802_2);
1368
1369 /*
1370 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1371 * SNAP packets with an organization code of
1372 * 0x080007 (Apple, for Appletalk) and a protocol
1373 * type of ETHERTYPE_ATALK (Appletalk).
1374 *
1375 * 802.2-encapsulated ETHERTYPE_AARP packets are
1376 * SNAP packets with an organization code of
1377 * 0x000000 (encapsulated Ethernet) and a protocol
1378 * type of ETHERTYPE_AARP (Appletalk ARP).
1379 */
1380 if (proto == ETHERTYPE_ATALK)
1381 b1 = gen_snap(0x080007, ETHERTYPE_ATALK,
1382 off_linktype + 2);
1383 else /* proto == ETHERTYPE_AARP */
1384 b1 = gen_snap(0x000000, ETHERTYPE_AARP,
1385 off_linktype + 2);
1386 gen_and(b0, b1);
1387
1388 /*
1389 * Check for Ethernet encapsulation (Ethertalk
1390 * phase 1?); we just check for the Ethernet
1391 * protocol type.
1392 */
1393 b0 = gen_cmp(off_linktype, BPF_H, (bpf_int32)proto);
1394
1395 gen_or(b0, b1);
1396 return b1;
1397
1398 default:
1399 if (proto <= ETHERMTU) {
1400 /*
1401 * This is an LLC SAP value, so the frames
1402 * that match would be 802.2 frames.
1403 * Check for the 802.2 protocol type
1404 * in the "Ethernet type" field, and
1405 * then check the DSAP.
1406 */
1407 b0 = gen_cmp(off_linktype, BPF_H,
1408 LINUX_SLL_P_802_2);
1409 b1 = gen_cmp(off_linktype + 2, BPF_B,
1410 (bpf_int32)proto);
1411 gen_and(b0, b1);
1412 return b1;
1413 } else {
1414 /*
1415 * This is an Ethernet type, so compare
1416 * the length/type field with it (if
1417 * the frame is an 802.2 frame, the length
1418 * field will be <= ETHERMTU, and, as
1419 * "proto" is > ETHERMTU, this test
1420 * will fail and the frame won't match,
1421 * which is what we want).
1422 */
1423 return gen_cmp(off_linktype, BPF_H,
1424 (bpf_int32)proto);
1425 }
1426 }
1427 }
1428
1429 static struct block *
1430 gen_linktype(proto)
1431 register int proto;
1432 {
1433 struct block *b0, *b1, *b2;
1434
1435 switch (linktype) {
1436
1437 case DLT_EN10MB:
1438 return gen_ether_linktype(proto);
1439 /*NOTREACHED*/
1440 break;
1441
1442 case DLT_C_HDLC:
1443 switch (proto) {
1444
1445 case LLCSAP_ISONS:
1446 proto = (proto << 8 | LLCSAP_ISONS);
1447 /* fall through */
1448
1449 default:
1450 return gen_cmp(off_linktype, BPF_H, (bpf_int32)proto);
1451 /*NOTREACHED*/
1452 break;
1453 }
1454 break;
1455
1456 case DLT_IEEE802_11:
1457 case DLT_PRISM_HEADER:
1458 case DLT_IEEE802_11_RADIO:
1459 case DLT_FDDI:
1460 case DLT_IEEE802:
1461 case DLT_ATM_RFC1483:
1462 case DLT_ATM_CLIP:
1463 case DLT_IP_OVER_FC:
1464 return gen_llc(proto);
1465 /*NOTREACHED*/
1466 break;
1467
1468 case DLT_SUNATM:
1469 /*
1470 * If "is_lane" is set, check for a LANE-encapsulated
1471 * version of this protocol, otherwise check for an
1472 * LLC-encapsulated version of this protocol.
1473 *
1474 * We assume LANE means Ethernet, not Token Ring.
1475 */
1476 if (is_lane) {
1477 /*
1478 * Check that the packet doesn't begin with an
1479 * LE Control marker. (We've already generated
1480 * a test for LANE.)
1481 */
1482 b0 = gen_cmp(SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
1483 gen_not(b0);
1484
1485 /*
1486 * Now generate an Ethernet test.
1487 */
1488 b1 = gen_ether_linktype(proto);
1489 gen_and(b0, b1);
1490 return b1;
1491 } else {
1492 /*
1493 * Check for LLC encapsulation and then check the
1494 * protocol.
1495 */
1496 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
1497 b1 = gen_llc(proto);
1498 gen_and(b0, b1);
1499 return b1;
1500 }
1501
1502 case DLT_LINUX_SLL:
1503 return gen_linux_sll_linktype(proto);
1504 /*NOTREACHED*/
1505 break;
1506
1507 case DLT_SLIP:
1508 case DLT_SLIP_BSDOS:
1509 case DLT_RAW:
1510 /*
1511 * These types don't provide any type field; packets
1512 * are always IP.
1513 *
1514 * XXX - for IPv4, check for a version number of 4, and,
1515 * for IPv6, check for a version number of 6?
1516 */
1517 switch (proto) {
1518
1519 case ETHERTYPE_IP:
1520 #ifdef INET6
1521 case ETHERTYPE_IPV6:
1522 #endif
1523 return gen_true(); /* always true */
1524
1525 default:
1526 return gen_false(); /* always false */
1527 }
1528 /*NOTREACHED*/
1529 break;
1530
1531 case DLT_PPP:
1532 case DLT_PPP_WITHDIRECTION:
1533 case DLT_PPP_SERIAL:
1534 case DLT_PPP_ETHER:
1535 /*
1536 * We use Ethernet protocol types inside libpcap;
1537 * map them to the corresponding PPP protocol types.
1538 */
1539 switch (proto) {
1540
1541 case ETHERTYPE_IP:
1542 proto = PPP_IP;
1543 break;
1544
1545 #ifdef INET6
1546 case ETHERTYPE_IPV6:
1547 proto = PPP_IPV6;
1548 break;
1549 #endif
1550
1551 case ETHERTYPE_DN:
1552 proto = PPP_DECNET;
1553 break;
1554
1555 case ETHERTYPE_ATALK:
1556 proto = PPP_APPLE;
1557 break;
1558
1559 case ETHERTYPE_NS:
1560 proto = PPP_NS;
1561 break;
1562
1563 case LLCSAP_ISONS:
1564 proto = PPP_OSI;
1565 break;
1566
1567 case LLCSAP_8021D:
1568 /*
1569 * I'm assuming the "Bridging PDU"s that go
1570 * over PPP are Spanning Tree Protocol
1571 * Bridging PDUs.
1572 */
1573 proto = PPP_BRPDU;
1574 break;
1575
1576 case LLCSAP_IPX:
1577 proto = PPP_IPX;
1578 break;
1579 }
1580 break;
1581
1582 case DLT_PPP_BSDOS:
1583 /*
1584 * We use Ethernet protocol types inside libpcap;
1585 * map them to the corresponding PPP protocol types.
1586 */
1587 switch (proto) {
1588
1589 case ETHERTYPE_IP:
1590 b0 = gen_cmp(off_linktype, BPF_H, PPP_IP);
1591 b1 = gen_cmp(off_linktype, BPF_H, PPP_VJC);
1592 gen_or(b0, b1);
1593 b0 = gen_cmp(off_linktype, BPF_H, PPP_VJNC);
1594 gen_or(b1, b0);
1595 return b0;
1596
1597 #ifdef INET6
1598 case ETHERTYPE_IPV6:
1599 proto = PPP_IPV6;
1600 /* more to go? */
1601 break;
1602 #endif
1603
1604 case ETHERTYPE_DN:
1605 proto = PPP_DECNET;
1606 break;
1607
1608 case ETHERTYPE_ATALK:
1609 proto = PPP_APPLE;
1610 break;
1611
1612 case ETHERTYPE_NS:
1613 proto = PPP_NS;
1614 break;
1615
1616 case LLCSAP_ISONS:
1617 proto = PPP_OSI;
1618 break;
1619
1620 case LLCSAP_8021D:
1621 /*
1622 * I'm assuming the "Bridging PDU"s that go
1623 * over PPP are Spanning Tree Protocol
1624 * Bridging PDUs.
1625 */
1626 proto = PPP_BRPDU;
1627 break;
1628
1629 case LLCSAP_IPX:
1630 proto = PPP_IPX;
1631 break;
1632 }
1633 break;
1634
1635 case DLT_NULL:
1636 case DLT_LOOP:
1637 case DLT_ENC:
1638 /*
1639 * For DLT_NULL, the link-layer header is a 32-bit
1640 * word containing an AF_ value in *host* byte order,
1641 * and for DLT_ENC, the link-layer header begins
1642 * with a 32-bit work containing an AF_ value in
1643 * host byte order.
1644 *
1645 * In addition, if we're reading a saved capture file,
1646 * the host byte order in the capture may not be the
1647 * same as the host byte order on this machine.
1648 *
1649 * For DLT_LOOP, the link-layer header is a 32-bit
1650 * word containing an AF_ value in *network* byte order.
1651 *
1652 * XXX - AF_ values may, unfortunately, be platform-
1653 * dependent; for example, FreeBSD's AF_INET6 is 24
1654 * whilst NetBSD's and OpenBSD's is 26.
1655 *
1656 * This means that, when reading a capture file, just
1657 * checking for our AF_INET6 value won't work if the
1658 * capture file came from another OS.
1659 */
1660 switch (proto) {
1661
1662 case ETHERTYPE_IP:
1663 proto = AF_INET;
1664 break;
1665
1666 #ifdef INET6
1667 case ETHERTYPE_IPV6:
1668 proto = AF_INET6;
1669 break;
1670 #endif
1671
1672 default:
1673 /*
1674 * Not a type on which we support filtering.
1675 * XXX - support those that have AF_ values
1676 * #defined on this platform, at least?
1677 */
1678 return gen_false();
1679 }
1680
1681 if (linktype == DLT_NULL || linktype == DLT_ENC) {
1682 /*
1683 * The AF_ value is in host byte order, but
1684 * the BPF interpreter will convert it to
1685 * network byte order.
1686 *
1687 * If this is a save file, and it's from a
1688 * machine with the opposite byte order to
1689 * ours, we byte-swap the AF_ value.
1690 *
1691 * Then we run it through "htonl()", and
1692 * generate code to compare against the result.
1693 */
1694 if (bpf_pcap->sf.rfile != NULL &&
1695 bpf_pcap->sf.swapped)
1696 proto = SWAPLONG(proto);
1697 proto = htonl(proto);
1698 }
1699 return (gen_cmp(0, BPF_W, (bpf_int32)proto));
1700
1701 case DLT_PFLOG:
1702 /*
1703 * af field is host byte order in contrast to the rest of
1704 * the packet.
1705 */
1706 if (proto == ETHERTYPE_IP)
1707 return (gen_cmp(offsetof(struct pfloghdr, af), BPF_B,
1708 (bpf_int32)AF_INET));
1709 #ifdef INET6
1710 else if (proto == ETHERTYPE_IPV6)
1711 return (gen_cmp(offsetof(struct pfloghdr, af), BPF_B,
1712 (bpf_int32)AF_INET6));
1713 #endif /* INET6 */
1714 else
1715 return gen_false();
1716 /*NOTREACHED*/
1717 break;
1718
1719 case DLT_ARCNET:
1720 case DLT_ARCNET_LINUX:
1721 /*
1722 * XXX should we check for first fragment if the protocol
1723 * uses PHDS?
1724 */
1725 switch (proto) {
1726
1727 default:
1728 return gen_false();
1729
1730 #ifdef INET6
1731 case ETHERTYPE_IPV6:
1732 return (gen_cmp(off_linktype, BPF_B,
1733 (bpf_int32)ARCTYPE_INET6));
1734 #endif /* INET6 */
1735
1736 case ETHERTYPE_IP:
1737 b0 = gen_cmp(off_linktype, BPF_B,
1738 (bpf_int32)ARCTYPE_IP);
1739 b1 = gen_cmp(off_linktype, BPF_B,
1740 (bpf_int32)ARCTYPE_IP_OLD);
1741 gen_or(b0, b1);
1742 return (b1);
1743
1744 case ETHERTYPE_ARP:
1745 b0 = gen_cmp(off_linktype, BPF_B,
1746 (bpf_int32)ARCTYPE_ARP);
1747 b1 = gen_cmp(off_linktype, BPF_B,
1748 (bpf_int32)ARCTYPE_ARP_OLD);
1749 gen_or(b0, b1);
1750 return (b1);
1751
1752 case ETHERTYPE_REVARP:
1753 return (gen_cmp(off_linktype, BPF_B,
1754 (bpf_int32)ARCTYPE_REVARP));
1755
1756 case ETHERTYPE_ATALK:
1757 return (gen_cmp(off_linktype, BPF_B,
1758 (bpf_int32)ARCTYPE_ATALK));
1759 }
1760 /*NOTREACHED*/
1761 break;
1762
1763 case DLT_LTALK:
1764 switch (proto) {
1765 case ETHERTYPE_ATALK:
1766 return gen_true();
1767 default:
1768 return gen_false();
1769 }
1770 /*NOTREACHED*/
1771 break;
1772
1773 case DLT_FRELAY:
1774 /*
1775 * XXX - assumes a 2-byte Frame Relay header with
1776 * DLCI and flags. What if the address is longer?
1777 */
1778 switch (proto) {
1779
1780 case ETHERTYPE_IP:
1781 /*
1782 * Check for the special NLPID for IP.
1783 */
1784 return gen_cmp(2, BPF_H, (0x03<<8) | 0xcc);
1785
1786 #ifdef INET6
1787 case ETHERTYPE_IPV6:
1788 /*
1789 * Check for the special NLPID for IPv6.
1790 */
1791 return gen_cmp(2, BPF_H, (0x03<<8) | 0x8e);
1792 #endif
1793
1794 case LLCSAP_ISONS:
1795 /*
1796 * Check for several OSI protocols.
1797 *
1798 * Frame Relay packets typically have an OSI
1799 * NLPID at the beginning; we check for each
1800 * of them.
1801 *
1802 * What we check for is the NLPID and a frame
1803 * control field of UI, i.e. 0x03 followed
1804 * by the NLPID.
1805 */
1806 b0 = gen_cmp(2, BPF_H, (0x03<<8) | ISO8473_CLNP);
1807 b1 = gen_cmp(2, BPF_H, (0x03<<8) | ISO9542_ESIS);
1808 b2 = gen_cmp(2, BPF_H, (0x03<<8) | ISO10589_ISIS);
1809 gen_or(b1, b2);
1810 gen_or(b0, b2);
1811 return b2;
1812
1813 default:
1814 return gen_false();
1815 }
1816 /*NOTREACHED*/
1817 break;
1818
1819 case DLT_JUNIPER_ATM1:
1820 case DLT_JUNIPER_ATM2:
1821 /* just lets verify the magic number for now -
1822 * we may have upto 6 different encapsulations on the wire
1823 * and need a lot of heuristics to figure out that the payload
1824 * might be;
1825 *
1826 * FIXME encapsulation specific BPF_ filters
1827 */
1828 return gen_mcmp(0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
1829
1830 case DLT_LINUX_IRDA:
1831 bpf_error("IrDA link-layer type filtering not implemented");
1832
1833 case DLT_DOCSIS:
1834 bpf_error("DOCSIS link-layer type filtering not implemented");
1835 }
1836
1837 /*
1838 * All the types that have no encapsulation should either be
1839 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1840 * all packets are IP packets, or should be handled in some
1841 * special case, if none of them are (if some are and some
1842 * aren't, the lack of encapsulation is a problem, as we'd
1843 * have to find some other way of determining the packet type).
1844 *
1845 * Therefore, if "off_linktype" is -1, there's an error.
1846 */
1847 if (off_linktype == (u_int)-1)
1848 abort();
1849
1850 /*
1851 * Any type not handled above should always have an Ethernet
1852 * type at an offset of "off_linktype". (PPP is partially
1853 * handled above - the protocol type is mapped from the
1854 * Ethernet and LLC types we use internally to the corresponding
1855 * PPP type - but the PPP type is always specified by a value
1856 * at "off_linktype", so we don't have to do the code generation
1857 * above.)
1858 */
1859 return gen_cmp(off_linktype, BPF_H, (bpf_int32)proto);
1860 }
1861
1862 /*
1863 * Check for an LLC SNAP packet with a given organization code and
1864 * protocol type; we check the entire contents of the 802.2 LLC and
1865 * snap headers, checking for DSAP and SSAP of SNAP and a control
1866 * field of 0x03 in the LLC header, and for the specified organization
1867 * code and protocol type in the SNAP header.
1868 */
1869 static struct block *
1870 gen_snap(orgcode, ptype, offset)
1871 bpf_u_int32 orgcode;
1872 bpf_u_int32 ptype;
1873 u_int offset;
1874 {
1875 u_char snapblock[8];
1876
1877 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
1878 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
1879 snapblock[2] = 0x03; /* control = UI */
1880 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
1881 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
1882 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
1883 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
1884 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
1885 return gen_bcmp(offset, 8, snapblock);
1886 }
1887
1888 /*
1889 * Check for a given protocol value assuming an 802.2 LLC header.
1890 */
1891 static struct block *
1892 gen_llc(proto)
1893 int proto;
1894 {
1895 /*
1896 * XXX - handle token-ring variable-length header.
1897 */
1898 switch (proto) {
1899
1900 case LLCSAP_IP:
1901 return gen_cmp(off_linktype, BPF_H, (long)
1902 ((LLCSAP_IP << 8) | LLCSAP_IP));
1903
1904 case LLCSAP_ISONS:
1905 return gen_cmp(off_linktype, BPF_H, (long)
1906 ((LLCSAP_ISONS << 8) | LLCSAP_ISONS));
1907
1908 case LLCSAP_NETBEUI:
1909 return gen_cmp(off_linktype, BPF_H, (long)
1910 ((LLCSAP_NETBEUI << 8) | LLCSAP_NETBEUI));
1911
1912 case LLCSAP_IPX:
1913 /*
1914 * XXX - are there ever SNAP frames for IPX on
1915 * non-Ethernet 802.x networks?
1916 */
1917 return gen_cmp(off_linktype, BPF_B, (bpf_int32)LLCSAP_IPX);
1918
1919 case ETHERTYPE_ATALK:
1920 /*
1921 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1922 * SNAP packets with an organization code of
1923 * 0x080007 (Apple, for Appletalk) and a protocol
1924 * type of ETHERTYPE_ATALK (Appletalk).
1925 *
1926 * XXX - check for an organization code of
1927 * encapsulated Ethernet as well?
1928 */
1929 return gen_snap(0x080007, ETHERTYPE_ATALK, off_linktype);
1930
1931 default:
1932 /*
1933 * XXX - we don't have to check for IPX 802.3
1934 * here, but should we check for the IPX Ethertype?
1935 */
1936 if (proto <= ETHERMTU) {
1937 /*
1938 * This is an LLC SAP value, so check
1939 * the DSAP.
1940 */
1941 return gen_cmp(off_linktype, BPF_B, (bpf_int32)proto);
1942 } else {
1943 /*
1944 * This is an Ethernet type; we assume that it's
1945 * unlikely that it'll appear in the right place
1946 * at random, and therefore check only the
1947 * location that would hold the Ethernet type
1948 * in a SNAP frame with an organization code of
1949 * 0x000000 (encapsulated Ethernet).
1950 *
1951 * XXX - if we were to check for the SNAP DSAP and
1952 * LSAP, as per XXX, and were also to check for an
1953 * organization code of 0x000000 (encapsulated
1954 * Ethernet), we'd do
1955 *
1956 * return gen_snap(0x000000, proto,
1957 * off_linktype);
1958 *
1959 * here; for now, we don't, as per the above.
1960 * I don't know whether it's worth the extra CPU
1961 * time to do the right check or not.
1962 */
1963 return gen_cmp(off_linktype+6, BPF_H, (bpf_int32)proto);
1964 }
1965 }
1966 }
1967
1968 static struct block *
1969 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
1970 bpf_u_int32 addr;
1971 bpf_u_int32 mask;
1972 int dir, proto;
1973 u_int src_off, dst_off;
1974 {
1975 struct block *b0, *b1;
1976 u_int offset;
1977
1978 switch (dir) {
1979
1980 case Q_SRC:
1981 offset = src_off;
1982 break;
1983
1984 case Q_DST:
1985 offset = dst_off;
1986 break;
1987
1988 case Q_AND:
1989 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
1990 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
1991 gen_and(b0, b1);
1992 return b1;
1993
1994 case Q_OR:
1995 case Q_DEFAULT:
1996 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
1997 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
1998 gen_or(b0, b1);
1999 return b1;
2000
2001 default:
2002 abort();
2003 }
2004 b0 = gen_linktype(proto);
2005 b1 = gen_mcmp(offset, BPF_W, (bpf_int32)addr, mask);
2006 gen_and(b0, b1);
2007 return b1;
2008 }
2009
2010 #ifdef INET6
2011 static struct block *
2012 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
2013 struct in6_addr *addr;
2014 struct in6_addr *mask;
2015 int dir, proto;
2016 u_int src_off, dst_off;
2017 {
2018 struct block *b0, *b1;
2019 u_int offset;
2020 u_int32_t *a, *m;
2021
2022 switch (dir) {
2023
2024 case Q_SRC:
2025 offset = src_off;
2026 break;
2027
2028 case Q_DST:
2029 offset = dst_off;
2030 break;
2031
2032 case Q_AND:
2033 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2034 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2035 gen_and(b0, b1);
2036 return b1;
2037
2038 case Q_OR:
2039 case Q_DEFAULT:
2040 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
2041 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
2042 gen_or(b0, b1);
2043 return b1;
2044
2045 default:
2046 abort();
2047 }
2048 /* this order is important */
2049 a = (u_int32_t *)addr;
2050 m = (u_int32_t *)mask;
2051 b1 = gen_mcmp(offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
2052 b0 = gen_mcmp(offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
2053 gen_and(b0, b1);
2054 b0 = gen_mcmp(offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
2055 gen_and(b0, b1);
2056 b0 = gen_mcmp(offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
2057 gen_and(b0, b1);
2058 b0 = gen_linktype(proto);
2059 gen_and(b0, b1);
2060 return b1;
2061 }
2062 #endif /*INET6*/
2063
2064 static struct block *
2065 gen_ehostop(eaddr, dir)
2066 register const u_char *eaddr;
2067 register int dir;
2068 {
2069 register struct block *b0, *b1;
2070
2071 switch (dir) {
2072 case Q_SRC:
2073 return gen_bcmp(off_mac + 6, 6, eaddr);
2074
2075 case Q_DST:
2076 return gen_bcmp(off_mac + 0, 6, eaddr);
2077
2078 case Q_AND:
2079 b0 = gen_ehostop(eaddr, Q_SRC);
2080 b1 = gen_ehostop(eaddr, Q_DST);
2081 gen_and(b0, b1);
2082 return b1;
2083
2084 case Q_DEFAULT:
2085 case Q_OR:
2086 b0 = gen_ehostop(eaddr, Q_SRC);
2087 b1 = gen_ehostop(eaddr, Q_DST);
2088 gen_or(b0, b1);
2089 return b1;
2090 }
2091 abort();
2092 /* NOTREACHED */
2093 }
2094
2095 /*
2096 * Like gen_ehostop, but for DLT_FDDI
2097 */
2098 static struct block *
2099 gen_fhostop(eaddr, dir)
2100 register const u_char *eaddr;
2101 register int dir;
2102 {
2103 struct block *b0, *b1;
2104
2105 switch (dir) {
2106 case Q_SRC:
2107 #ifdef PCAP_FDDIPAD
2108 return gen_bcmp(6 + 1 + pcap_fddipad, 6, eaddr);
2109 #else
2110 return gen_bcmp(6 + 1, 6, eaddr);
2111 #endif
2112
2113 case Q_DST:
2114 #ifdef PCAP_FDDIPAD
2115 return gen_bcmp(0 + 1 + pcap_fddipad, 6, eaddr);
2116 #else
2117 return gen_bcmp(0 + 1, 6, eaddr);
2118 #endif
2119
2120 case Q_AND:
2121 b0 = gen_fhostop(eaddr, Q_SRC);
2122 b1 = gen_fhostop(eaddr, Q_DST);
2123 gen_and(b0, b1);
2124 return b1;
2125
2126 case Q_DEFAULT:
2127 case Q_OR:
2128 b0 = gen_fhostop(eaddr, Q_SRC);
2129 b1 = gen_fhostop(eaddr, Q_DST);
2130 gen_or(b0, b1);
2131 return b1;
2132 }
2133 abort();
2134 /* NOTREACHED */
2135 }
2136
2137 /*
2138 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2139 */
2140 static struct block *
2141 gen_thostop(eaddr, dir)
2142 register const u_char *eaddr;
2143 register int dir;
2144 {
2145 register struct block *b0, *b1;
2146
2147 switch (dir) {
2148 case Q_SRC:
2149 return gen_bcmp(8, 6, eaddr);
2150
2151 case Q_DST:
2152 return gen_bcmp(2, 6, eaddr);
2153
2154 case Q_AND:
2155 b0 = gen_thostop(eaddr, Q_SRC);
2156 b1 = gen_thostop(eaddr, Q_DST);
2157 gen_and(b0, b1);
2158 return b1;
2159
2160 case Q_DEFAULT:
2161 case Q_OR:
2162 b0 = gen_thostop(eaddr, Q_SRC);
2163 b1 = gen_thostop(eaddr, Q_DST);
2164 gen_or(b0, b1);
2165 return b1;
2166 }
2167 abort();
2168 /* NOTREACHED */
2169 }
2170
2171 /*
2172 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2173 */
2174 static struct block *
2175 gen_wlanhostop(eaddr, dir)
2176 register const u_char *eaddr;
2177 register int dir;
2178 {
2179 register struct block *b0, *b1, *b2;
2180 register struct slist *s;
2181
2182 switch (dir) {
2183 case Q_SRC:
2184 /*
2185 * Oh, yuk.
2186 *
2187 * For control frames, there is no SA.
2188 *
2189 * For management frames, SA is at an
2190 * offset of 10 from the beginning of
2191 * the packet.
2192 *
2193 * For data frames, SA is at an offset
2194 * of 10 from the beginning of the packet
2195 * if From DS is clear, at an offset of
2196 * 16 from the beginning of the packet
2197 * if From DS is set and To DS is clear,
2198 * and an offset of 24 from the beginning
2199 * of the packet if From DS is set and To DS
2200 * is set.
2201 */
2202
2203 /*
2204 * Generate the tests to be done for data frames
2205 * with From DS set.
2206 *
2207 * First, check for To DS set, i.e. check "link[1] & 0x01".
2208 */
2209 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2210 s->s.k = 1;
2211 b1 = new_block(JMP(BPF_JSET));
2212 b1->s.k = 0x01; /* To DS */
2213 b1->stmts = s;
2214
2215 /*
2216 * If To DS is set, the SA is at 24.
2217 */
2218 b0 = gen_bcmp(24, 6, eaddr);
2219 gen_and(b1, b0);
2220
2221 /*
2222 * Now, check for To DS not set, i.e. check
2223 * "!(link[1] & 0x01)".
2224 */
2225 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2226 s->s.k = 1;
2227 b2 = new_block(JMP(BPF_JSET));
2228 b2->s.k = 0x01; /* To DS */
2229 b2->stmts = s;
2230 gen_not(b2);
2231
2232 /*
2233 * If To DS is not set, the SA is at 16.
2234 */
2235 b1 = gen_bcmp(16, 6, eaddr);
2236 gen_and(b2, b1);
2237
2238 /*
2239 * Now OR together the last two checks. That gives
2240 * the complete set of checks for data frames with
2241 * From DS set.
2242 */
2243 gen_or(b1, b0);
2244
2245 /*
2246 * Now check for From DS being set, and AND that with
2247 * the ORed-together checks.
2248 */
2249 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2250 s->s.k = 1;
2251 b1 = new_block(JMP(BPF_JSET));
2252 b1->s.k = 0x02; /* From DS */
2253 b1->stmts = s;
2254 gen_and(b1, b0);
2255
2256 /*
2257 * Now check for data frames with From DS not set.
2258 */
2259 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2260 s->s.k = 1;
2261 b2 = new_block(JMP(BPF_JSET));
2262 b2->s.k = 0x02; /* From DS */
2263 b2->stmts = s;
2264 gen_not(b2);
2265
2266 /*
2267 * If From DS isn't set, the SA is at 10.
2268 */
2269 b1 = gen_bcmp(10, 6, eaddr);
2270 gen_and(b2, b1);
2271
2272 /*
2273 * Now OR together the checks for data frames with
2274 * From DS not set and for data frames with From DS
2275 * set; that gives the checks done for data frames.
2276 */
2277 gen_or(b1, b0);
2278
2279 /*
2280 * Now check for a data frame.
2281 * I.e, check "link[0] & 0x08".
2282 */
2283 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2284 s->s.k = 0;
2285 b1 = new_block(JMP(BPF_JSET));
2286 b1->s.k = 0x08;
2287 b1->stmts = s;
2288
2289 /*
2290 * AND that with the checks done for data frames.
2291 */
2292 gen_and(b1, b0);
2293
2294 /*
2295 * If the high-order bit of the type value is 0, this
2296 * is a management frame.
2297 * I.e, check "!(link[0] & 0x08)".
2298 */
2299 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2300 s->s.k = 0;
2301 b2 = new_block(JMP(BPF_JSET));
2302 b2->s.k = 0x08;
2303 b2->stmts = s;
2304 gen_not(b2);
2305
2306 /*
2307 * For management frames, the SA is at 10.
2308 */
2309 b1 = gen_bcmp(10, 6, eaddr);
2310 gen_and(b2, b1);
2311
2312 /*
2313 * OR that with the checks done for data frames.
2314 * That gives the checks done for management and
2315 * data frames.
2316 */
2317 gen_or(b1, b0);
2318
2319 /*
2320 * If the low-order bit of the type value is 1,
2321 * this is either a control frame or a frame
2322 * with a reserved type, and thus not a
2323 * frame with an SA.
2324 *
2325 * I.e., check "!(link[0] & 0x04)".
2326 */
2327 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2328 s->s.k = 0;
2329 b1 = new_block(JMP(BPF_JSET));
2330 b1->s.k = 0x04;
2331 b1->stmts = s;
2332 gen_not(b1);
2333
2334 /*
2335 * AND that with the checks for data and management
2336 * frames.
2337 */
2338 gen_and(b1, b0);
2339 return b0;
2340
2341 case Q_DST:
2342 /*
2343 * Oh, yuk.
2344 *
2345 * For control frames, there is no DA.
2346 *
2347 * For management frames, DA is at an
2348 * offset of 4 from the beginning of
2349 * the packet.
2350 *
2351 * For data frames, DA is at an offset
2352 * of 4 from the beginning of the packet
2353 * if To DS is clear and at an offset of
2354 * 16 from the beginning of the packet
2355 * if To DS is set.
2356 */
2357
2358 /*
2359 * Generate the tests to be done for data frames.
2360 *
2361 * First, check for To DS set, i.e. "link[1] & 0x01".
2362 */
2363 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2364 s->s.k = 1;
2365 b1 = new_block(JMP(BPF_JSET));
2366 b1->s.k = 0x01; /* To DS */
2367 b1->stmts = s;
2368
2369 /*
2370 * If To DS is set, the DA is at 16.
2371 */
2372 b0 = gen_bcmp(16, 6, eaddr);
2373 gen_and(b1, b0);
2374
2375 /*
2376 * Now, check for To DS not set, i.e. check
2377 * "!(link[1] & 0x01)".
2378 */
2379 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2380 s->s.k = 1;
2381 b2 = new_block(JMP(BPF_JSET));
2382 b2->s.k = 0x01; /* To DS */
2383 b2->stmts = s;
2384 gen_not(b2);
2385
2386 /*
2387 * If To DS is not set, the DA is at 4.
2388 */
2389 b1 = gen_bcmp(4, 6, eaddr);
2390 gen_and(b2, b1);
2391
2392 /*
2393 * Now OR together the last two checks. That gives
2394 * the complete set of checks for data frames.
2395 */
2396 gen_or(b1, b0);
2397
2398 /*
2399 * Now check for a data frame.
2400 * I.e, check "link[0] & 0x08".
2401 */
2402 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2403 s->s.k = 0;
2404 b1 = new_block(JMP(BPF_JSET));
2405 b1->s.k = 0x08;
2406 b1->stmts = s;
2407
2408 /*
2409 * AND that with the checks done for data frames.
2410 */
2411 gen_and(b1, b0);
2412
2413 /*
2414 * If the high-order bit of the type value is 0, this
2415 * is a management frame.
2416 * I.e, check "!(link[0] & 0x08)".
2417 */
2418 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2419 s->s.k = 0;
2420 b2 = new_block(JMP(BPF_JSET));
2421 b2->s.k = 0x08;
2422 b2->stmts = s;
2423 gen_not(b2);
2424
2425 /*
2426 * For management frames, the DA is at 4.
2427 */
2428 b1 = gen_bcmp(4, 6, eaddr);
2429 gen_and(b2, b1);
2430
2431 /*
2432 * OR that with the checks done for data frames.
2433 * That gives the checks done for management and
2434 * data frames.
2435 */
2436 gen_or(b1, b0);
2437
2438 /*
2439 * If the low-order bit of the type value is 1,
2440 * this is either a control frame or a frame
2441 * with a reserved type, and thus not a
2442 * frame with an SA.
2443 *
2444 * I.e., check "!(link[0] & 0x04)".
2445 */
2446 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2447 s->s.k = 0;
2448 b1 = new_block(JMP(BPF_JSET));
2449 b1->s.k = 0x04;
2450 b1->stmts = s;
2451 gen_not(b1);
2452
2453 /*
2454 * AND that with the checks for data and management
2455 * frames.
2456 */
2457 gen_and(b1, b0);
2458 return b0;
2459
2460 case Q_AND:
2461 b0 = gen_wlanhostop(eaddr, Q_SRC);
2462 b1 = gen_wlanhostop(eaddr, Q_DST);
2463 gen_and(b0, b1);
2464 return b1;
2465
2466 case Q_DEFAULT:
2467 case Q_OR:
2468 b0 = gen_wlanhostop(eaddr, Q_SRC);
2469 b1 = gen_wlanhostop(eaddr, Q_DST);
2470 gen_or(b0, b1);
2471 return b1;
2472 }
2473 abort();
2474 /* NOTREACHED */
2475 }
2476
2477 /*
2478 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2479 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2480 * as the RFC states.)
2481 */
2482 static struct block *
2483 gen_ipfchostop(eaddr, dir)
2484 register const u_char *eaddr;
2485 register int dir;
2486 {
2487 register struct block *b0, *b1;
2488
2489 switch (dir) {
2490 case Q_SRC:
2491 return gen_bcmp(10, 6, eaddr);
2492
2493 case Q_DST:
2494 return gen_bcmp(2, 6, eaddr);
2495
2496 case Q_AND:
2497 b0 = gen_ipfchostop(eaddr, Q_SRC);
2498 b1 = gen_ipfchostop(eaddr, Q_DST);
2499 gen_and(b0, b1);
2500 return b1;
2501
2502 case Q_DEFAULT:
2503 case Q_OR:
2504 b0 = gen_ipfchostop(eaddr, Q_SRC);
2505 b1 = gen_ipfchostop(eaddr, Q_DST);
2506 gen_or(b0, b1);
2507 return b1;
2508 }
2509 abort();
2510 /* NOTREACHED */
2511 }
2512
2513 /*
2514 * This is quite tricky because there may be pad bytes in front of the
2515 * DECNET header, and then there are two possible data packet formats that
2516 * carry both src and dst addresses, plus 5 packet types in a format that
2517 * carries only the src node, plus 2 types that use a different format and
2518 * also carry just the src node.
2519 *
2520 * Yuck.
2521 *
2522 * Instead of doing those all right, we just look for data packets with
2523 * 0 or 1 bytes of padding. If you want to look at other packets, that
2524 * will require a lot more hacking.
2525 *
2526 * To add support for filtering on DECNET "areas" (network numbers)
2527 * one would want to add a "mask" argument to this routine. That would
2528 * make the filter even more inefficient, although one could be clever
2529 * and not generate masking instructions if the mask is 0xFFFF.
2530 */
2531 static struct block *
2532 gen_dnhostop(addr, dir, base_off)
2533 bpf_u_int32 addr;
2534 int dir;
2535 u_int base_off;
2536 {
2537 struct block *b0, *b1, *b2, *tmp;
2538 u_int offset_lh; /* offset if long header is received */
2539 u_int offset_sh; /* offset if short header is received */
2540
2541 switch (dir) {
2542
2543 case Q_DST:
2544 offset_sh = 1; /* follows flags */
2545 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
2546 break;
2547
2548 case Q_SRC:
2549 offset_sh = 3; /* follows flags, dstnode */
2550 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2551 break;
2552
2553 case Q_AND:
2554 /* Inefficient because we do our Calvinball dance twice */
2555 b0 = gen_dnhostop(addr, Q_SRC, base_off);
2556 b1 = gen_dnhostop(addr, Q_DST, base_off);
2557 gen_and(b0, b1);
2558 return b1;
2559
2560 case Q_OR:
2561 case Q_DEFAULT:
2562 /* Inefficient because we do our Calvinball dance twice */
2563 b0 = gen_dnhostop(addr, Q_SRC, base_off);
2564 b1 = gen_dnhostop(addr, Q_DST, base_off);
2565 gen_or(b0, b1);
2566 return b1;
2567
2568 case Q_ISO:
2569 bpf_error("ISO host filtering not implemented");
2570
2571 default:
2572 abort();
2573 }
2574 b0 = gen_linktype(ETHERTYPE_DN);
2575 /* Check for pad = 1, long header case */
2576 tmp = gen_mcmp(base_off + 2, BPF_H,
2577 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
2578 b1 = gen_cmp(base_off + 2 + 1 + offset_lh,
2579 BPF_H, (bpf_int32)ntohs(addr));
2580 gen_and(tmp, b1);
2581 /* Check for pad = 0, long header case */
2582 tmp = gen_mcmp(base_off + 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
2583 b2 = gen_cmp(base_off + 2 + offset_lh, BPF_H, (bpf_int32)ntohs(addr));
2584 gen_and(tmp, b2);
2585 gen_or(b2, b1);
2586 /* Check for pad = 1, short header case */
2587 tmp = gen_mcmp(base_off + 2, BPF_H,
2588 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
2589 b2 = gen_cmp(base_off + 2 + 1 + offset_sh,
2590 BPF_H, (bpf_int32)ntohs(addr));
2591 gen_and(tmp, b2);
2592 gen_or(b2, b1);
2593 /* Check for pad = 0, short header case */
2594 tmp = gen_mcmp(base_off + 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
2595 b2 = gen_cmp(base_off + 2 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
2596 gen_and(tmp, b2);
2597 gen_or(b2, b1);
2598
2599 /* Combine with test for linktype */
2600 gen_and(b0, b1);
2601 return b1;
2602 }
2603
2604 static struct block *
2605 gen_host(addr, mask, proto, dir)
2606 bpf_u_int32 addr;
2607 bpf_u_int32 mask;
2608 int proto;
2609 int dir;
2610 {
2611 struct block *b0, *b1;
2612
2613 switch (proto) {
2614
2615 case Q_DEFAULT:
2616 b0 = gen_host(addr, mask, Q_IP, dir);
2617 if (off_linktype != (u_int)-1) {
2618 b1 = gen_host(addr, mask, Q_ARP, dir);
2619 gen_or(b0, b1);
2620 b0 = gen_host(addr, mask, Q_RARP, dir);
2621 gen_or(b1, b0);
2622 }
2623 return b0;
2624
2625 case Q_IP:
2626 return gen_hostop(addr, mask, dir, ETHERTYPE_IP,
2627 off_nl + 12, off_nl + 16);
2628
2629 case Q_RARP:
2630 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP,
2631 off_nl + 14, off_nl + 24);
2632
2633 case Q_ARP:
2634 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP,
2635 off_nl + 14, off_nl + 24);
2636
2637 case Q_TCP:
2638 bpf_error("'tcp' modifier applied to host");
2639
2640 case Q_SCTP:
2641 bpf_error("'sctp' modifier applied to host");
2642
2643 case Q_UDP:
2644 bpf_error("'udp' modifier applied to host");
2645
2646 case Q_ICMP:
2647 bpf_error("'icmp' modifier applied to host");
2648
2649 case Q_IGMP:
2650 bpf_error("'igmp' modifier applied to host");
2651
2652 case Q_IGRP:
2653 bpf_error("'igrp' modifier applied to host");
2654
2655 case Q_PIM:
2656 bpf_error("'pim' modifier applied to host");
2657
2658 case Q_VRRP:
2659 bpf_error("'vrrp' modifier applied to host");
2660
2661 case Q_ATALK:
2662 bpf_error("ATALK host filtering not implemented");
2663
2664 case Q_AARP:
2665 bpf_error("AARP host filtering not implemented");
2666
2667 case Q_DECNET:
2668 return gen_dnhostop(addr, dir, off_nl);
2669
2670 case Q_SCA:
2671 bpf_error("SCA host filtering not implemented");
2672
2673 case Q_LAT:
2674 bpf_error("LAT host filtering not implemented");
2675
2676 case Q_MOPDL:
2677 bpf_error("MOPDL host filtering not implemented");
2678
2679 case Q_MOPRC:
2680 bpf_error("MOPRC host filtering not implemented");
2681
2682 #ifdef INET6
2683 case Q_IPV6:
2684 bpf_error("'ip6' modifier applied to ip host");
2685
2686 case Q_ICMPV6:
2687 bpf_error("'icmp6' modifier applied to host");
2688 #endif /* INET6 */
2689
2690 case Q_AH:
2691 bpf_error("'ah' modifier applied to host");
2692
2693 case Q_ESP:
2694 bpf_error("'esp' modifier applied to host");
2695
2696 case Q_ISO:
2697 bpf_error("ISO host filtering not implemented");
2698
2699 case Q_ESIS:
2700 bpf_error("'esis' modifier applied to host");
2701
2702 case Q_ISIS:
2703 bpf_error("'isis' modifier applied to host");
2704
2705 case Q_CLNP:
2706 bpf_error("'clnp' modifier applied to host");
2707
2708 case Q_STP:
2709 bpf_error("'stp' modifier applied to host");
2710
2711 case Q_IPX:
2712 bpf_error("IPX host filtering not implemented");
2713
2714 case Q_NETBEUI:
2715 bpf_error("'netbeui' modifier applied to host");
2716
2717 default:
2718 abort();
2719 }
2720 /* NOTREACHED */
2721 }
2722
2723 #ifdef INET6
2724 static struct block *
2725 gen_host6(addr, mask, proto, dir)
2726 struct in6_addr *addr;
2727 struct in6_addr *mask;
2728 int proto;
2729 int dir;
2730 {
2731 switch (proto) {
2732
2733 case Q_DEFAULT:
2734 return gen_host6(addr, mask, Q_IPV6, dir);
2735
2736 case Q_IP:
2737 bpf_error("'ip' modifier applied to ip6 host");
2738
2739 case Q_RARP:
2740 bpf_error("'rarp' modifier applied to ip6 host");
2741
2742 case Q_ARP:
2743 bpf_error("'arp' modifier applied to ip6 host");
2744
2745 case Q_SCTP:
2746 bpf_error("'sctp' modifier applied to host");
2747
2748 case Q_TCP:
2749 bpf_error("'tcp' modifier applied to host");
2750
2751 case Q_UDP:
2752 bpf_error("'udp' modifier applied to host");
2753
2754 case Q_ICMP:
2755 bpf_error("'icmp' modifier applied to host");
2756
2757 case Q_IGMP:
2758 bpf_error("'igmp' modifier applied to host");
2759
2760 case Q_IGRP:
2761 bpf_error("'igrp' modifier applied to host");
2762
2763 case Q_PIM:
2764 bpf_error("'pim' modifier applied to host");
2765
2766 case Q_VRRP:
2767 bpf_error("'vrrp' modifier applied to host");
2768
2769 case Q_ATALK:
2770 bpf_error("ATALK host filtering not implemented");
2771
2772 case Q_AARP:
2773 bpf_error("AARP host filtering not implemented");
2774
2775 case Q_DECNET:
2776 bpf_error("'decnet' modifier applied to ip6 host");
2777
2778 case Q_SCA:
2779 bpf_error("SCA host filtering not implemented");
2780
2781 case Q_LAT:
2782 bpf_error("LAT host filtering not implemented");
2783
2784 case Q_MOPDL:
2785 bpf_error("MOPDL host filtering not implemented");
2786
2787 case Q_MOPRC:
2788 bpf_error("MOPRC host filtering not implemented");
2789
2790 case Q_IPV6:
2791 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6,
2792 off_nl + 8, off_nl + 24);
2793
2794 case Q_ICMPV6:
2795 bpf_error("'icmp6' modifier applied to host");
2796
2797 case Q_AH:
2798 bpf_error("'ah' modifier applied to host");
2799
2800 case Q_ESP:
2801 bpf_error("'esp' modifier applied to host");
2802
2803 case Q_ISO:
2804 bpf_error("ISO host filtering not implemented");
2805
2806 case Q_ESIS:
2807 bpf_error("'esis' modifier applied to host");
2808
2809 case Q_ISIS:
2810 bpf_error("'isis' modifier applied to host");
2811
2812 case Q_CLNP:
2813 bpf_error("'clnp' modifier applied to host");
2814
2815 case Q_STP:
2816 bpf_error("'stp' modifier applied to host");
2817
2818 case Q_IPX:
2819 bpf_error("IPX host filtering not implemented");
2820
2821 case Q_NETBEUI:
2822 bpf_error("'netbeui' modifier applied to host");
2823
2824 default:
2825 abort();
2826 }
2827 /* NOTREACHED */
2828 }
2829 #endif /*INET6*/
2830
2831 #ifndef INET6
2832 static struct block *
2833 gen_gateway(eaddr, alist, proto, dir)
2834 const u_char *eaddr;
2835 bpf_u_int32 **alist;
2836 int proto;
2837 int dir;
2838 {
2839 struct block *b0, *b1, *tmp;
2840
2841 if (dir != 0)
2842 bpf_error("direction applied to 'gateway'");
2843
2844 switch (proto) {
2845 case Q_DEFAULT:
2846 case Q_IP:
2847 case Q_ARP:
2848 case Q_RARP:
2849 if (linktype == DLT_EN10MB)
2850 b0 = gen_ehostop(eaddr, Q_OR);
2851 else if (linktype == DLT_FDDI)
2852 b0 = gen_fhostop(eaddr, Q_OR);
2853 else if (linktype == DLT_IEEE802)
2854 b0 = gen_thostop(eaddr, Q_OR);
2855 else if (linktype == DLT_IEEE802_11)
2856 b0 = gen_wlanhostop(eaddr, Q_OR);
2857 else if (linktype == DLT_SUNATM && is_lane) {
2858 /*
2859 * Check that the packet doesn't begin with an
2860 * LE Control marker. (We've already generated
2861 * a test for LANE.)
2862 */
2863 b1 = gen_cmp(SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
2864 gen_not(b1);
2865
2866 /*
2867 * Now check the MAC address.
2868 */
2869 b0 = gen_ehostop(eaddr, Q_OR);
2870 gen_and(b1, b0);
2871 } else if (linktype == DLT_IP_OVER_FC)
2872 b0 = gen_ipfchostop(eaddr, Q_OR);
2873 else
2874 bpf_error(
2875 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2876
2877 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR);
2878 while (*alist) {
2879 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR);
2880 gen_or(b1, tmp);
2881 b1 = tmp;
2882 }
2883 gen_not(b1);
2884 gen_and(b0, b1);
2885 return b1;
2886 }
2887 bpf_error("illegal modifier of 'gateway'");
2888 /* NOTREACHED */
2889 }
2890 #endif
2891
2892 struct block *
2893 gen_proto_abbrev(proto)
2894 int proto;
2895 {
2896 struct block *b0;
2897 struct block *b1;
2898
2899 switch (proto) {
2900
2901 case Q_SCTP:
2902 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
2903 #ifdef INET6
2904 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
2905 gen_or(b0, b1);
2906 #endif
2907 break;
2908
2909 case Q_TCP:
2910 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
2911 #ifdef INET6
2912 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
2913 gen_or(b0, b1);
2914 #endif
2915 break;
2916
2917 case Q_UDP:
2918 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
2919 #ifdef INET6
2920 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
2921 gen_or(b0, b1);
2922 #endif
2923 break;
2924
2925 case Q_ICMP:
2926 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
2927 break;
2928
2929 #ifndef IPPROTO_IGMP
2930 #define IPPROTO_IGMP 2
2931 #endif
2932
2933 case Q_IGMP:
2934 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
2935 break;
2936
2937 #ifndef IPPROTO_IGRP
2938 #define IPPROTO_IGRP 9
2939 #endif
2940 case Q_IGRP:
2941 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
2942 break;
2943
2944 #ifndef IPPROTO_PIM
2945 #define IPPROTO_PIM 103
2946 #endif
2947
2948 case Q_PIM:
2949 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
2950 #ifdef INET6
2951 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
2952 gen_or(b0, b1);
2953 #endif
2954 break;
2955
2956 #ifndef IPPROTO_VRRP
2957 #define IPPROTO_VRRP 112
2958 #endif
2959
2960 case Q_VRRP:
2961 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
2962 break;
2963
2964 case Q_IP:
2965 b1 = gen_linktype(ETHERTYPE_IP);
2966 break;
2967
2968 case Q_ARP:
2969 b1 = gen_linktype(ETHERTYPE_ARP);
2970 break;
2971
2972 case Q_RARP:
2973 b1 = gen_linktype(ETHERTYPE_REVARP);
2974 break;
2975
2976 case Q_LINK:
2977 bpf_error("link layer applied in wrong context");
2978
2979 case Q_ATALK:
2980 b1 = gen_linktype(ETHERTYPE_ATALK);
2981 break;
2982
2983 case Q_AARP:
2984 b1 = gen_linktype(ETHERTYPE_AARP);
2985 break;
2986
2987 case Q_DECNET:
2988 b1 = gen_linktype(ETHERTYPE_DN);
2989 break;
2990
2991 case Q_SCA:
2992 b1 = gen_linktype(ETHERTYPE_SCA);
2993 break;
2994
2995 case Q_LAT:
2996 b1 = gen_linktype(ETHERTYPE_LAT);
2997 break;
2998
2999 case Q_MOPDL:
3000 b1 = gen_linktype(ETHERTYPE_MOPDL);
3001 break;
3002
3003 case Q_MOPRC:
3004 b1 = gen_linktype(ETHERTYPE_MOPRC);
3005 break;
3006
3007 #ifdef INET6
3008 case Q_IPV6:
3009 b1 = gen_linktype(ETHERTYPE_IPV6);
3010 break;
3011
3012 #ifndef IPPROTO_ICMPV6
3013 #define IPPROTO_ICMPV6 58
3014 #endif
3015 case Q_ICMPV6:
3016 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
3017 break;
3018 #endif /* INET6 */
3019
3020 #ifndef IPPROTO_AH
3021 #define IPPROTO_AH 51
3022 #endif
3023 case Q_AH:
3024 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
3025 #ifdef INET6
3026 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
3027 gen_or(b0, b1);
3028 #endif
3029 break;
3030
3031 #ifndef IPPROTO_ESP
3032 #define IPPROTO_ESP 50
3033 #endif
3034 case Q_ESP:
3035 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
3036 #ifdef INET6
3037 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
3038 gen_or(b0, b1);
3039 #endif
3040 break;
3041
3042 case Q_ISO:
3043 b1 = gen_linktype(LLCSAP_ISONS);
3044 break;
3045
3046 case Q_ESIS:
3047 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
3048 break;
3049
3050 case Q_ISIS:
3051 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
3052 break;
3053
3054 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
3055 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3056 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3057 gen_or(b0, b1);
3058 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3059 gen_or(b0, b1);
3060 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3061 gen_or(b0, b1);
3062 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3063 gen_or(b0, b1);
3064 break;
3065
3066 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
3067 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3068 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
3069 gen_or(b0, b1);
3070 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3071 gen_or(b0, b1);
3072 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3073 gen_or(b0, b1);
3074 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3075 gen_or(b0, b1);
3076 break;
3077
3078 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
3079 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
3080 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
3081 gen_or(b0, b1);
3082 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
3083 gen_or(b0, b1);
3084 break;
3085
3086 case Q_ISIS_LSP:
3087 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
3088 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
3089 gen_or(b0, b1);
3090 break;
3091
3092 case Q_ISIS_SNP:
3093 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3094 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3095 gen_or(b0, b1);
3096 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3097 gen_or(b0, b1);
3098 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3099 gen_or(b0, b1);
3100 break;
3101
3102 case Q_ISIS_CSNP:
3103 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
3104 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
3105 gen_or(b0, b1);
3106 break;
3107
3108 case Q_ISIS_PSNP:
3109 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
3110 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
3111 gen_or(b0, b1);
3112 break;
3113
3114 case Q_CLNP:
3115 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
3116 break;
3117
3118 case Q_STP:
3119 b1 = gen_linktype(LLCSAP_8021D);
3120 break;
3121
3122 case Q_IPX:
3123 b1 = gen_linktype(LLCSAP_IPX);
3124 break;
3125
3126 case Q_NETBEUI:
3127 b1 = gen_linktype(LLCSAP_NETBEUI);
3128 break;
3129
3130 default:
3131 abort();
3132 }
3133 return b1;
3134 }
3135
3136 static struct block *
3137 gen_ipfrag()
3138 {
3139 struct slist *s;
3140 struct block *b;
3141
3142 /* not ip frag */
3143 s = new_stmt(BPF_LD|BPF_H|BPF_ABS);
3144 s->s.k = off_nl + 6;
3145 b = new_block(JMP(BPF_JSET));
3146 b->s.k = 0x1fff;
3147 b->stmts = s;
3148 gen_not(b);
3149
3150 return b;
3151 }
3152
3153 static struct block *
3154 gen_portatom(off, v)
3155 int off;
3156 bpf_int32 v;
3157 {
3158 struct slist *s;
3159 struct block *b;
3160
3161 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
3162 s->s.k = off_nl;
3163
3164 s->next = new_stmt(BPF_LD|BPF_IND|BPF_H);
3165 s->next->s.k = off_nl + off;
3166
3167 b = new_block(JMP(BPF_JEQ));
3168 b->stmts = s;
3169 b->s.k = v;
3170
3171 return b;
3172 }
3173
3174 #ifdef INET6
3175 static struct block *
3176 gen_portatom6(off, v)
3177 int off;
3178 bpf_int32 v;
3179 {
3180 return gen_cmp(off_nl + 40 + off, BPF_H, v);
3181 }
3182 #endif/*INET6*/
3183
3184 struct block *
3185 gen_portop(port, proto, dir)
3186 int port, proto, dir;
3187 {
3188 struct block *b0, *b1, *tmp;
3189
3190 /* ip proto 'proto' */
3191 tmp = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)proto);
3192 b0 = gen_ipfrag();
3193 gen_and(tmp, b0);
3194
3195 switch (dir) {
3196 case Q_SRC:
3197 b1 = gen_portatom(0, (bpf_int32)port);
3198 break;
3199
3200 case Q_DST:
3201 b1 = gen_portatom(2, (bpf_int32)port);
3202 break;
3203
3204 case Q_OR:
3205 case Q_DEFAULT:
3206 tmp = gen_portatom(0, (bpf_int32)port);
3207 b1 = gen_portatom(2, (bpf_int32)port);
3208 gen_or(tmp, b1);
3209 break;
3210
3211 case Q_AND:
3212 tmp = gen_portatom(0, (bpf_int32)port);
3213 b1 = gen_portatom(2, (bpf_int32)port);
3214 gen_and(tmp, b1);
3215 break;
3216
3217 default:
3218 abort();
3219 }
3220 gen_and(b0, b1);
3221
3222 return b1;
3223 }
3224
3225 static struct block *
3226 gen_port(port, ip_proto, dir)
3227 int port;
3228 int ip_proto;
3229 int dir;
3230 {
3231 struct block *b0, *b1, *tmp;
3232
3233 /*
3234 * ether proto ip
3235 *
3236 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3237 * not LLC encapsulation with LLCSAP_IP.
3238 *
3239 * For IEEE 802 networks - which includes 802.5 token ring
3240 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3241 * says that SNAP encapsulation is used, not LLC encapsulation
3242 * with LLCSAP_IP.
3243 *
3244 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3245 * RFC 2225 say that SNAP encapsulation is used, not LLC
3246 * encapsulation with LLCSAP_IP.
3247 *
3248 * So we always check for ETHERTYPE_IP.
3249 */
3250 b0 = gen_linktype(ETHERTYPE_IP);
3251
3252 switch (ip_proto) {
3253 case IPPROTO_UDP:
3254 case IPPROTO_TCP:
3255 case IPPROTO_SCTP:
3256 b1 = gen_portop(port, ip_proto, dir);
3257 break;
3258
3259 case PROTO_UNDEF:
3260 tmp = gen_portop(port, IPPROTO_TCP, dir);
3261 b1 = gen_portop(port, IPPROTO_UDP, dir);
3262 gen_or(tmp, b1);
3263 tmp = gen_portop(port, IPPROTO_SCTP, dir);
3264 gen_or(tmp, b1);
3265 break;
3266
3267 default:
3268 abort();
3269 }
3270 gen_and(b0, b1);
3271 return b1;
3272 }
3273
3274 #ifdef INET6
3275 struct block *
3276 gen_portop6(port, proto, dir)
3277 int port, proto, dir;
3278 {
3279 struct block *b0, *b1, *tmp;
3280
3281 /* ip proto 'proto' */
3282 b0 = gen_cmp(off_nl + 6, BPF_B, (bpf_int32)proto);
3283
3284 switch (dir) {
3285 case Q_SRC:
3286 b1 = gen_portatom6(0, (bpf_int32)port);
3287 break;
3288
3289 case Q_DST:
3290 b1 = gen_portatom6(2, (bpf_int32)port);
3291 break;
3292
3293 case Q_OR:
3294 case Q_DEFAULT:
3295 tmp = gen_portatom6(0, (bpf_int32)port);
3296 b1 = gen_portatom6(2, (bpf_int32)port);
3297 gen_or(tmp, b1);
3298 break;
3299
3300 case Q_AND:
3301 tmp = gen_portatom6(0, (bpf_int32)port);
3302 b1 = gen_portatom6(2, (bpf_int32)port);
3303 gen_and(tmp, b1);
3304 break;
3305
3306 default:
3307 abort();
3308 }
3309 gen_and(b0, b1);
3310
3311 return b1;
3312 }
3313
3314 static struct block *
3315 gen_port6(port, ip_proto, dir)
3316 int port;
3317 int ip_proto;
3318 int dir;
3319 {
3320 struct block *b0, *b1, *tmp;
3321
3322 /* ether proto ip */
3323 b0 = gen_linktype(ETHERTYPE_IPV6);
3324
3325 switch (ip_proto) {
3326 case IPPROTO_UDP:
3327 case IPPROTO_TCP:
3328 case IPPROTO_SCTP:
3329 b1 = gen_portop6(port, ip_proto, dir);
3330 break;
3331
3332 case PROTO_UNDEF:
3333 tmp = gen_portop6(port, IPPROTO_TCP, dir);
3334 b1 = gen_portop6(port, IPPROTO_UDP, dir);
3335 gen_or(tmp, b1);
3336 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
3337 gen_or(tmp, b1);
3338 break;
3339
3340 default:
3341 abort();
3342 }
3343 gen_and(b0, b1);
3344 return b1;
3345 }
3346 #endif /* INET6 */
3347
3348 static int
3349 lookup_proto(name, proto)
3350 register const char *name;
3351 register int proto;
3352 {
3353 register int v;
3354
3355 switch (proto) {
3356
3357 case Q_DEFAULT:
3358 case Q_IP:
3359 case Q_IPV6:
3360 v = pcap_nametoproto(name);
3361 if (v == PROTO_UNDEF)
3362 bpf_error("unknown ip proto '%s'", name);
3363 break;
3364
3365 case Q_LINK:
3366 /* XXX should look up h/w protocol type based on linktype */
3367 v = pcap_nametoeproto(name);
3368 if (v == PROTO_UNDEF)
3369 bpf_error("unknown ether proto '%s'", name);
3370 break;
3371
3372 case Q_ISO:
3373 if (strcmp(name, "esis") == 0)
3374 v = ISO9542_ESIS;
3375 else if (strcmp(name, "isis") == 0)
3376 v = ISO10589_ISIS;
3377 else if (strcmp(name, "clnp") == 0)
3378 v = ISO8473_CLNP;
3379 else
3380 bpf_error("unknown osi proto '%s'", name);
3381 break;
3382
3383 default:
3384 v = PROTO_UNDEF;
3385 break;
3386 }
3387 return v;
3388 }
3389
3390 #if 0
3391 struct stmt *
3392 gen_joinsp(s, n)
3393 struct stmt **s;
3394 int n;
3395 {
3396 return NULL;
3397 }
3398 #endif
3399
3400 static struct block *
3401 gen_protochain(v, proto, dir)
3402 int v;
3403 int proto;
3404 int dir;
3405 {
3406 #ifdef NO_PROTOCHAIN
3407 return gen_proto(v, proto, dir);
3408 #else
3409 struct block *b0, *b;
3410 struct slist *s[100];
3411 int fix2, fix3, fix4, fix5;
3412 int ahcheck, again, end;
3413 int i, max;
3414 int reg2 = alloc_reg();
3415
3416 memset(s, 0, sizeof(s));
3417 fix2 = fix3 = fix4 = fix5 = 0;
3418
3419 switch (proto) {
3420 case Q_IP:
3421 case Q_IPV6:
3422 break;
3423 case Q_DEFAULT:
3424 b0 = gen_protochain(v, Q_IP, dir);
3425 b = gen_protochain(v, Q_IPV6, dir);
3426 gen_or(b0, b);
3427 return b;
3428 default:
3429 bpf_error("bad protocol applied for 'protochain'");
3430 /*NOTREACHED*/
3431 }
3432
3433 no_optimize = 1; /*this code is not compatible with optimzer yet */
3434
3435 /*
3436 * s[0] is a dummy entry to protect other BPF insn from damaged
3437 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3438 * hard to find interdependency made by jump table fixup.
3439 */
3440 i = 0;
3441 s[i] = new_stmt(0); /*dummy*/
3442 i++;
3443
3444 switch (proto) {
3445 case Q_IP:
3446 b0 = gen_linktype(ETHERTYPE_IP);
3447
3448 /* A = ip->ip_p */
3449 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
3450 s[i]->s.k = off_nl + 9;
3451 i++;
3452 /* X = ip->ip_hl << 2 */
3453 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
3454 s[i]->s.k = off_nl;
3455 i++;
3456 break;
3457 #ifdef INET6
3458 case Q_IPV6:
3459 b0 = gen_linktype(ETHERTYPE_IPV6);
3460
3461 /* A = ip6->ip_nxt */
3462 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
3463 s[i]->s.k = off_nl + 6;
3464 i++;
3465 /* X = sizeof(struct ip6_hdr) */
3466 s[i] = new_stmt(BPF_LDX|BPF_IMM);
3467 s[i]->s.k = 40;
3468 i++;
3469 break;
3470 #endif
3471 default:
3472 bpf_error("unsupported proto to gen_protochain");
3473 /*NOTREACHED*/
3474 }
3475
3476 /* again: if (A == v) goto end; else fall through; */
3477 again = i;
3478 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
3479 s[i]->s.k = v;
3480 s[i]->s.jt = NULL; /*later*/
3481 s[i]->s.jf = NULL; /*update in next stmt*/
3482 fix5 = i;
3483 i++;
3484
3485 #ifndef IPPROTO_NONE
3486 #define IPPROTO_NONE 59
3487 #endif
3488 /* if (A == IPPROTO_NONE) goto end */
3489 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
3490 s[i]->s.jt = NULL; /*later*/
3491 s[i]->s.jf = NULL; /*update in next stmt*/
3492 s[i]->s.k = IPPROTO_NONE;
3493 s[fix5]->s.jf = s[i];
3494 fix2 = i;
3495 i++;
3496
3497 #ifdef INET6
3498 if (proto == Q_IPV6) {
3499 int v6start, v6end, v6advance, j;
3500
3501 v6start = i;
3502 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3503 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
3504 s[i]->s.jt = NULL; /*later*/
3505 s[i]->s.jf = NULL; /*update in next stmt*/
3506 s[i]->s.k = IPPROTO_HOPOPTS;
3507 s[fix2]->s.jf = s[i];
3508 i++;
3509 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3510 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
3511 s[i]->s.jt = NULL; /*later*/
3512 s[i]->s.jf = NULL; /*update in next stmt*/
3513 s[i]->s.k = IPPROTO_DSTOPTS;
3514 i++;
3515 /* if (A == IPPROTO_ROUTING) goto v6advance */
3516 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
3517 s[i]->s.jt = NULL; /*later*/
3518 s[i]->s.jf = NULL; /*update in next stmt*/
3519 s[i]->s.k = IPPROTO_ROUTING;
3520 i++;
3521 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3522 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
3523 s[i]->s.jt = NULL; /*later*/
3524 s[i]->s.jf = NULL; /*later*/
3525 s[i]->s.k = IPPROTO_FRAGMENT;
3526 fix3 = i;
3527 v6end = i;
3528 i++;
3529
3530 /* v6advance: */
3531 v6advance = i;
3532
3533 /*
3534 * in short,
3535 * A = P[X];
3536 * X = X + (P[X + 1] + 1) * 8;
3537 */
3538 /* A = X */
3539 s[i] = new_stmt(BPF_MISC|BPF_TXA);
3540 i++;
3541 /* A = P[X + packet head] */
3542 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
3543 s[i]->s.k = off_nl;
3544 i++;
3545 /* MEM[reg2] = A */
3546 s[i] = new_stmt(BPF_ST);
3547 s[i]->s.k = reg2;
3548 i++;
3549 /* A = X */
3550 s[i] = new_stmt(BPF_MISC|BPF_TXA);
3551 i++;
3552 /* A += 1 */
3553 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
3554 s[i]->s.k = 1;
3555 i++;
3556 /* X = A */
3557 s[i] = new_stmt(BPF_MISC|BPF_TAX);
3558 i++;
3559 /* A = P[X + packet head]; */
3560 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
3561 s[i]->s.k = off_nl;
3562 i++;
3563 /* A += 1 */
3564 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
3565 s[i]->s.k = 1;
3566 i++;
3567 /* A *= 8 */
3568 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
3569 s[i]->s.k = 8;
3570 i++;
3571 /* X = A; */
3572 s[i] = new_stmt(BPF_MISC|BPF_TAX);
3573 i++;
3574 /* A = MEM[reg2] */
3575 s[i] = new_stmt(BPF_LD|BPF_MEM);
3576 s[i]->s.k = reg2;
3577 i++;
3578
3579 /* goto again; (must use BPF_JA for backward jump) */
3580 s[i] = new_stmt(BPF_JMP|BPF_JA);
3581 s[i]->s.k = again - i - 1;
3582 s[i - 1]->s.jf = s[i];
3583 i++;
3584
3585 /* fixup */
3586 for (j = v6start; j <= v6end; j++)
3587 s[j]->s.jt = s[v6advance];
3588 } else
3589 #endif
3590 {
3591 /* nop */
3592 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
3593 s[i]->s.k = 0;
3594 s[fix2]->s.jf = s[i];
3595 i++;
3596 }
3597
3598 /* ahcheck: */
3599 ahcheck = i;
3600 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3601 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
3602 s[i]->s.jt = NULL; /*later*/
3603 s[i]->s.jf = NULL; /*later*/
3604 s[i]->s.k = IPPROTO_AH;
3605 if (fix3)
3606 s[fix3]->s.jf = s[ahcheck];
3607 fix4 = i;
3608 i++;
3609
3610 /*
3611 * in short,
3612 * A = P[X];
3613 * X = X + (P[X + 1] + 2) * 4;
3614 */
3615 /* A = X */
3616 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
3617 i++;
3618 /* A = P[X + packet head]; */
3619 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
3620 s[i]->s.k = off_nl;
3621 i++;
3622 /* MEM[reg2] = A */
3623 s[i] = new_stmt(BPF_ST);
3624 s[i]->s.k = reg2;
3625 i++;
3626 /* A = X */
3627 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
3628 i++;
3629 /* A += 1 */
3630 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
3631 s[i]->s.k = 1;
3632 i++;
3633 /* X = A */
3634 s[i] = new_stmt(BPF_MISC|BPF_TAX);
3635 i++;
3636 /* A = P[X + packet head] */
3637 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
3638 s[i]->s.k = off_nl;
3639 i++;
3640 /* A += 2 */
3641 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
3642 s[i]->s.k = 2;
3643 i++;
3644 /* A *= 4 */
3645 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
3646 s[i]->s.k = 4;
3647 i++;
3648 /* X = A; */
3649 s[i] = new_stmt(BPF_MISC|BPF_TAX);
3650 i++;
3651 /* A = MEM[reg2] */
3652 s[i] = new_stmt(BPF_LD|BPF_MEM);
3653 s[i]->s.k = reg2;
3654 i++;
3655
3656 /* goto again; (must use BPF_JA for backward jump) */
3657 s[i] = new_stmt(BPF_JMP|BPF_JA);
3658 s[i]->s.k = again - i - 1;
3659 i++;
3660
3661 /* end: nop */
3662 end = i;
3663 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
3664 s[i]->s.k = 0;
3665 s[fix2]->s.jt = s[end];
3666 s[fix4]->s.jf = s[end];
3667 s[fix5]->s.jt = s[end];
3668 i++;
3669
3670 /*
3671 * make slist chain
3672 */
3673 max = i;
3674 for (i = 0; i < max - 1; i++)
3675 s[i]->next = s[i + 1];
3676 s[max - 1]->next = NULL;
3677
3678 /*
3679 * emit final check
3680 */
3681 b = new_block(JMP(BPF_JEQ));
3682 b->stmts = s[1]; /*remember, s[0] is dummy*/
3683 b->s.k = v;
3684
3685 free_reg(reg2);
3686
3687 gen_and(b0, b);
3688 return b;
3689 #endif
3690 }
3691
3692 static struct block *
3693 gen_proto(v, proto, dir)
3694 int v;
3695 int proto;
3696 int dir;
3697 {
3698 struct block *b0, *b1;
3699
3700 if (dir != Q_DEFAULT)
3701 bpf_error("direction applied to 'proto'");
3702
3703 switch (proto) {
3704 case Q_DEFAULT:
3705 #ifdef INET6
3706 b0 = gen_proto(v, Q_IP, dir);
3707 b1 = gen_proto(v, Q_IPV6, dir);
3708 gen_or(b0, b1);
3709 return b1;
3710 #else
3711 /*FALLTHROUGH*/
3712 #endif
3713 case Q_IP:
3714 /*
3715 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3716 * not LLC encapsulation with LLCSAP_IP.
3717 *
3718 * For IEEE 802 networks - which includes 802.5 token ring
3719 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3720 * says that SNAP encapsulation is used, not LLC encapsulation
3721 * with LLCSAP_IP.
3722 *
3723 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3724 * RFC 2225 say that SNAP encapsulation is used, not LLC
3725 * encapsulation with LLCSAP_IP.
3726 *
3727 * So we always check for ETHERTYPE_IP.
3728 */
3729 b0 = gen_linktype(ETHERTYPE_IP);
3730 #ifndef CHASE_CHAIN
3731 b1 = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)v);
3732 #else
3733 b1 = gen_protochain(v, Q_IP);
3734 #endif
3735 gen_and(b0, b1);
3736 return b1;
3737
3738 case Q_ISO:
3739 switch (linktype) {
3740
3741 case DLT_FRELAY:
3742 /*
3743 * Frame Relay packets typically have an OSI
3744 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3745 * generates code to check for all the OSI
3746 * NLPIDs, so calling it and then adding a check
3747 * for the particular NLPID for which we're
3748 * looking is bogus, as we can just check for
3749 * the NLPID.
3750 *
3751 * What we check for is the NLPID and a frame
3752 * control field value of UI, i.e. 0x03 followed
3753 * by the NLPID.
3754 *
3755 * XXX - assumes a 2-byte Frame Relay header with
3756 * DLCI and flags. What if the address is longer?
3757 *
3758 * XXX - what about SNAP-encapsulated frames?
3759 */
3760 return gen_cmp(2, BPF_H, (0x03<<8) | v);
3761 /*NOTREACHED*/
3762 break;
3763
3764 case DLT_C_HDLC:
3765 /*
3766 * Cisco uses an Ethertype lookalike - for OSI,
3767 * it's 0xfefe.
3768 */
3769 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
3770 /* OSI in C-HDLC is stuffed with a fudge byte */
3771 b1 = gen_cmp(off_nl_nosnap+1, BPF_B, (long)v);
3772 gen_and(b0, b1);
3773 return b1;
3774
3775 default:
3776 b0 = gen_linktype(LLCSAP_ISONS);
3777 b1 = gen_cmp(off_nl_nosnap, BPF_B, (long)v);
3778 gen_and(b0, b1);
3779 return b1;
3780 }
3781
3782 case Q_ISIS:
3783 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
3784 /*
3785 * 4 is the offset of the PDU type relative to the IS-IS
3786 * header.
3787 */
3788 b1 = gen_cmp(off_nl_nosnap+4, BPF_B, (long)v);
3789 gen_and(b0, b1);
3790 return b1;
3791
3792 case Q_ARP:
3793 bpf_error("arp does not encapsulate another protocol");
3794 /* NOTREACHED */
3795
3796 case Q_RARP:
3797 bpf_error("rarp does not encapsulate another protocol");
3798 /* NOTREACHED */
3799
3800 case Q_ATALK:
3801 bpf_error("atalk encapsulation is not specifiable");
3802 /* NOTREACHED */
3803
3804 case Q_DECNET:
3805 bpf_error("decnet encapsulation is not specifiable");
3806 /* NOTREACHED */
3807
3808 case Q_SCA:
3809 bpf_error("sca does not encapsulate another protocol");
3810 /* NOTREACHED */
3811
3812 case Q_LAT:
3813 bpf_error("lat does not encapsulate another protocol");
3814 /* NOTREACHED */
3815
3816 case Q_MOPRC:
3817 bpf_error("moprc does not encapsulate another protocol");
3818 /* NOTREACHED */
3819
3820 case Q_MOPDL:
3821 bpf_error("mopdl does not encapsulate another protocol");
3822 /* NOTREACHED */
3823
3824 case Q_LINK:
3825 return gen_linktype(v);
3826
3827 case Q_UDP:
3828 bpf_error("'udp proto' is bogus");
3829 /* NOTREACHED */
3830
3831 case Q_TCP:
3832 bpf_error("'tcp proto' is bogus");
3833 /* NOTREACHED */
3834
3835 case Q_SCTP:
3836 bpf_error("'sctp proto' is bogus");
3837 /* NOTREACHED */
3838
3839 case Q_ICMP:
3840 bpf_error("'icmp proto' is bogus");
3841 /* NOTREACHED */
3842
3843 case Q_IGMP:
3844 bpf_error("'igmp proto' is bogus");
3845 /* NOTREACHED */
3846
3847 case Q_IGRP:
3848 bpf_error("'igrp proto' is bogus");
3849 /* NOTREACHED */
3850
3851 case Q_PIM:
3852 bpf_error("'pim proto' is bogus");
3853 /* NOTREACHED */
3854
3855 case Q_VRRP:
3856 bpf_error("'vrrp proto' is bogus");
3857 /* NOTREACHED */
3858
3859 #ifdef INET6
3860 case Q_IPV6:
3861 b0 = gen_linktype(ETHERTYPE_IPV6);
3862 #ifndef CHASE_CHAIN
3863 b1 = gen_cmp(off_nl + 6, BPF_B, (bpf_int32)v);
3864 #else
3865 b1 = gen_protochain(v, Q_IPV6);
3866 #endif
3867 gen_and(b0, b1);
3868 return b1;
3869
3870 case Q_ICMPV6:
3871 bpf_error("'icmp6 proto' is bogus");
3872 #endif /* INET6 */
3873
3874 case Q_AH:
3875 bpf_error("'ah proto' is bogus");
3876
3877 case Q_ESP:
3878 bpf_error("'ah proto' is bogus");
3879
3880 case Q_STP:
3881 bpf_error("'stp proto' is bogus");
3882
3883 case Q_IPX:
3884 bpf_error("'ipx proto' is bogus");
3885
3886 case Q_NETBEUI:
3887 bpf_error("'netbeui proto' is bogus");
3888
3889 default:
3890 abort();
3891 /* NOTREACHED */
3892 }
3893 /* NOTREACHED */
3894 }
3895
3896 struct block *
3897 gen_scode(name, q)
3898 register const char *name;
3899 struct qual q;
3900 {
3901 int proto = q.proto;
3902 int dir = q.dir;
3903 int tproto;
3904 u_char *eaddr;
3905 bpf_u_int32 mask, addr;
3906 #ifndef INET6
3907 bpf_u_int32 **alist;
3908 #else
3909 int tproto6;
3910 struct sockaddr_in *sin;
3911 struct sockaddr_in6 *sin6;
3912 struct addrinfo *res, *res0;
3913 struct in6_addr mask128;
3914 #endif /*INET6*/
3915 struct block *b, *tmp;
3916 int port, real_proto;
3917
3918 switch (q.addr) {
3919
3920 case Q_NET:
3921 addr = pcap_nametonetaddr(name);
3922 if (addr == 0)
3923 bpf_error("unknown network '%s'", name);
3924 /* Left justify network addr and calculate its network mask */
3925 mask = 0xffffffff;
3926 while (addr && (addr & 0xff000000) == 0) {
3927 addr <<= 8;
3928 mask <<= 8;
3929 }
3930 return gen_host(addr, mask, proto, dir);
3931
3932 case Q_DEFAULT:
3933 case Q_HOST:
3934 if (proto == Q_LINK) {
3935 switch (linktype) {
3936
3937 case DLT_EN10MB:
3938 eaddr = pcap_ether_hostton(name);
3939 if (eaddr == NULL)
3940 bpf_error(
3941 "unknown ether host '%s'", name);
3942 b = gen_ehostop(eaddr, dir);
3943 free(eaddr);
3944 return b;
3945
3946 case DLT_FDDI:
3947 eaddr = pcap_ether_hostton(name);
3948 if (eaddr == NULL)
3949 bpf_error(
3950 "unknown FDDI host '%s'", name);
3951 b = gen_fhostop(eaddr, dir);
3952 free(eaddr);
3953 return b;
3954
3955 case DLT_IEEE802:
3956 eaddr = pcap_ether_hostton(name);
3957 if (eaddr == NULL)
3958 bpf_error(
3959 "unknown token ring host '%s'", name);
3960 b = gen_thostop(eaddr, dir);
3961 free(eaddr);
3962 return b;
3963
3964 case DLT_IEEE802_11:
3965 eaddr = pcap_ether_hostton(name);
3966 if (eaddr == NULL)
3967 bpf_error(
3968 "unknown 802.11 host '%s'", name);
3969 b = gen_wlanhostop(eaddr, dir);
3970 free(eaddr);
3971 return b;
3972
3973 case DLT_IP_OVER_FC:
3974 eaddr = pcap_ether_hostton(name);
3975 if (eaddr == NULL)
3976 bpf_error(
3977 "unknown Fibre Channel host '%s'", name);
3978 b = gen_ipfchostop(eaddr, dir);
3979 free(eaddr);
3980 return b;
3981
3982 case DLT_SUNATM:
3983 if (!is_lane)
3984 break;
3985
3986 /*
3987 * Check that the packet doesn't begin
3988 * with an LE Control marker. (We've
3989 * already generated a test for LANE.)
3990 */
3991 tmp = gen_cmp(SUNATM_PKT_BEGIN_POS, BPF_H,
3992 0xFF00);
3993 gen_not(tmp);
3994
3995 eaddr = pcap_ether_hostton(name);
3996 if (eaddr == NULL)
3997 bpf_error(
3998 "unknown ether host '%s'", name);
3999 b = gen_ehostop(eaddr, dir);
4000 gen_and(tmp, b);
4001 free(eaddr);
4002 return b;
4003 }
4004
4005 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4006 } else if (proto == Q_DECNET) {
4007 unsigned short dn_addr = __pcap_nametodnaddr(name);
4008 /*
4009 * I don't think DECNET hosts can be multihomed, so
4010 * there is no need to build up a list of addresses
4011 */
4012 return (gen_host(dn_addr, 0, proto, dir));
4013 } else {
4014 #ifndef INET6
4015 alist = pcap_nametoaddr(name);
4016 if (alist == NULL || *alist == NULL)
4017 bpf_error("unknown host '%s'", name);
4018 tproto = proto;
4019 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
4020 tproto = Q_IP;
4021 b = gen_host(**alist++, 0xffffffff, tproto, dir);
4022 while (*alist) {
4023 tmp = gen_host(**alist++, 0xffffffff,
4024 tproto, dir);
4025 gen_or(b, tmp);
4026 b = tmp;
4027 }
4028 return b;
4029 #else
4030 memset(&mask128, 0xff, sizeof(mask128));
4031 res0 = res = pcap_nametoaddrinfo(name);
4032 if (res == NULL)
4033 bpf_error("unknown host '%s'", name);
4034 b = tmp = NULL;
4035 tproto = tproto6 = proto;
4036 if (off_linktype == -1 && tproto == Q_DEFAULT) {
4037 tproto = Q_IP;
4038 tproto6 = Q_IPV6;
4039 }
4040 for (res = res0; res; res = res->ai_next) {
4041 switch (res->ai_family) {
4042 case AF_INET:
4043 if (tproto == Q_IPV6)
4044 continue;
4045
4046 sin = (struct sockaddr_in *)
4047 res->ai_addr;
4048 tmp = gen_host(ntohl(sin->sin_addr.s_addr),
4049 0xffffffff, tproto, dir);
4050 break;
4051 case AF_INET6:
4052 if (tproto6 == Q_IP)
4053 continue;
4054
4055 sin6 = (struct sockaddr_in6 *)
4056 res->ai_addr;
4057 tmp = gen_host6(&sin6->sin6_addr,
4058 &mask128, tproto6, dir);
4059 break;
4060 default:
4061 continue;
4062 }
4063 if (b)
4064 gen_or(b, tmp);
4065 b = tmp;
4066 }
4067 freeaddrinfo(res0);
4068 if (b == NULL) {
4069 bpf_error("unknown host '%s'%s", name,
4070 (proto == Q_DEFAULT)
4071 ? ""
4072 : " for specified address family");
4073 }
4074 return b;
4075 #endif /*INET6*/
4076 }
4077
4078 case Q_PORT:
4079 if (proto != Q_DEFAULT &&
4080 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
4081 bpf_error("illegal qualifier of 'port'");
4082 if (pcap_nametoport(name, &port, &real_proto) == 0)
4083 bpf_error("unknown port '%s'", name);
4084 if (proto == Q_UDP) {
4085 if (real_proto == IPPROTO_TCP)
4086 bpf_error("port '%s' is tcp", name);
4087 else if (real_proto == IPPROTO_SCTP)
4088 bpf_error("port '%s' is sctp", name);
4089 else
4090 /* override PROTO_UNDEF */
4091 real_proto = IPPROTO_UDP;
4092 }
4093 if (proto == Q_TCP) {
4094 if (real_proto == IPPROTO_UDP)
4095 bpf_error("port '%s' is udp", name);
4096
4097 else if (real_proto == IPPROTO_SCTP)
4098 bpf_error("port '%s' is sctp", name);
4099 else
4100 /* override PROTO_UNDEF */
4101 real_proto = IPPROTO_TCP;
4102 }
4103 if (proto == Q_SCTP) {
4104 if (real_proto == IPPROTO_UDP)
4105 bpf_error("port '%s' is udp", name);
4106
4107 else if (real_proto == IPPROTO_TCP)
4108 bpf_error("port '%s' is tcp", name);
4109 else
4110 /* override PROTO_UNDEF */
4111 real_proto = IPPROTO_SCTP;
4112 }
4113 #ifndef INET6
4114 return gen_port(port, real_proto, dir);
4115 #else
4116 {
4117 struct block *b;
4118 b = gen_port(port, real_proto, dir);
4119 gen_or(gen_port6(port, real_proto, dir), b);
4120 return b;
4121 }
4122 #endif /* INET6 */
4123
4124 case Q_GATEWAY:
4125 #ifndef INET6
4126 eaddr = pcap_ether_hostton(name);
4127 if (eaddr == NULL)
4128 bpf_error("unknown ether host: %s", name);
4129
4130 alist = pcap_nametoaddr(name);
4131 if (alist == NULL || *alist == NULL)
4132 bpf_error("unknown host '%s'", name);
4133 b = gen_gateway(eaddr, alist, proto, dir);
4134 free(eaddr);
4135 return b;
4136 #else
4137 bpf_error("'gateway' not supported in this configuration");
4138 #endif /*INET6*/
4139
4140 case Q_PROTO:
4141 real_proto = lookup_proto(name, proto);
4142 if (real_proto >= 0)
4143 return gen_proto(real_proto, proto, dir);
4144 else
4145 bpf_error("unknown protocol: %s", name);
4146
4147 case Q_PROTOCHAIN:
4148 real_proto = lookup_proto(name, proto);
4149 if (real_proto >= 0)
4150 return gen_protochain(real_proto, proto, dir);
4151 else
4152 bpf_error("unknown protocol: %s", name);
4153
4154
4155 case Q_UNDEF:
4156 syntax();
4157 /* NOTREACHED */
4158 }
4159 abort();
4160 /* NOTREACHED */
4161 }
4162
4163 struct block *
4164 gen_mcode(s1, s2, masklen, q)
4165 register const char *s1, *s2;
4166 register int masklen;
4167 struct qual q;
4168 {
4169 register int nlen, mlen;
4170 bpf_u_int32 n, m;
4171
4172 nlen = __pcap_atoin(s1, &n);
4173 /* Promote short ipaddr */
4174 n <<= 32 - nlen;
4175
4176 if (s2 != NULL) {
4177 mlen = __pcap_atoin(s2, &m);
4178 /* Promote short ipaddr */
4179 m <<= 32 - mlen;
4180 if ((n & ~m) != 0)
4181 bpf_error("non-network bits set in \"%s mask %s\"",
4182 s1, s2);
4183 } else {
4184 /* Convert mask len to mask */
4185 if (masklen > 32)
4186 bpf_error("mask length must be <= 32");
4187 m = 0xffffffff << (32 - masklen);
4188 if ((n & ~m) != 0)
4189 bpf_error("non-network bits set in \"%s/%d\"",
4190 s1, masklen);
4191 }
4192
4193 switch (q.addr) {
4194
4195 case Q_NET:
4196 return gen_host(n, m, q.proto, q.dir);
4197
4198 default:
4199 bpf_error("Mask syntax for networks only");
4200 /* NOTREACHED */
4201 }
4202 /* NOTREACHED */
4203 }
4204
4205 struct block *
4206 gen_ncode(s, v, q)
4207 register const char *s;
4208 bpf_u_int32 v;
4209 struct qual q;
4210 {
4211 bpf_u_int32 mask;
4212 int proto = q.proto;
4213 int dir = q.dir;
4214 register int vlen;
4215
4216 if (s == NULL)
4217 vlen = 32;
4218 else if (q.proto == Q_DECNET)
4219 vlen = __pcap_atodn(s, &v);
4220 else
4221 vlen = __pcap_atoin(s, &v);
4222
4223 switch (q.addr) {
4224
4225 case Q_DEFAULT:
4226 case Q_HOST:
4227 case Q_NET:
4228 if (proto == Q_DECNET)
4229 return gen_host(v, 0, proto, dir);
4230 else if (proto == Q_LINK) {
4231 bpf_error("illegal link layer address");
4232 } else {
4233 mask = 0xffffffff;
4234 if (s == NULL && q.addr == Q_NET) {
4235 /* Promote short net number */
4236 while (v && (v & 0xff000000) == 0) {
4237 v <<= 8;
4238 mask <<= 8;
4239 }
4240 } else {
4241 /* Promote short ipaddr */
4242 v <<= 32 - vlen;
4243 mask <<= 32 - vlen;
4244 }
4245 return gen_host(v, mask, proto, dir);
4246 }
4247
4248 case Q_PORT:
4249 if (proto == Q_UDP)
4250 proto = IPPROTO_UDP;
4251 else if (proto == Q_TCP)
4252 proto = IPPROTO_TCP;
4253 else if (proto == Q_SCTP)
4254 proto = IPPROTO_SCTP;
4255 else if (proto == Q_DEFAULT)
4256 proto = PROTO_UNDEF;
4257 else
4258 bpf_error("illegal qualifier of 'port'");
4259
4260 #ifndef INET6
4261 return gen_port((int)v, proto, dir);
4262 #else
4263 {
4264 struct block *b;
4265 b = gen_port((int)v, proto, dir);
4266 gen_or(gen_port6((int)v, proto, dir), b);
4267 return b;
4268 }
4269 #endif /* INET6 */
4270
4271 case Q_GATEWAY:
4272 bpf_error("'gateway' requires a name");
4273 /* NOTREACHED */
4274
4275 case Q_PROTO:
4276 return gen_proto((int)v, proto, dir);
4277
4278 case Q_PROTOCHAIN:
4279 return gen_protochain((int)v, proto, dir);
4280
4281 case Q_UNDEF:
4282 syntax();
4283 /* NOTREACHED */
4284
4285 default:
4286 abort();
4287 /* NOTREACHED */
4288 }
4289 /* NOTREACHED */
4290 }
4291
4292 #ifdef INET6
4293 struct block *
4294 gen_mcode6(s1, s2, masklen, q)
4295 register const char *s1, *s2;
4296 register int masklen;
4297 struct qual q;
4298 {
4299 struct addrinfo *res;
4300 struct in6_addr *addr;
4301 struct in6_addr mask;
4302 struct block *b;
4303 u_int32_t *a, *m;
4304
4305 if (s2)
4306 bpf_error("no mask %s supported", s2);
4307
4308 res = pcap_nametoaddrinfo(s1);
4309 if (!res)
4310 bpf_error("invalid ip6 address %s", s1);
4311 if (res->ai_next)
4312 bpf_error("%s resolved to multiple address", s1);
4313 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
4314
4315 if (sizeof(mask) * 8 < masklen)
4316 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
4317 memset(&mask, 0, sizeof(mask));
4318 memset(&mask, 0xff, masklen / 8);
4319 if (masklen % 8) {
4320 mask.s6_addr[masklen / 8] =
4321 (0xff << (8 - masklen % 8)) & 0xff;
4322 }
4323
4324 a = (u_int32_t *)addr;
4325 m = (u_int32_t *)&mask;
4326 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
4327 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
4328 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
4329 }
4330
4331 switch (q.addr) {
4332
4333 case Q_DEFAULT:
4334 case Q_HOST:
4335 if (masklen != 128)
4336 bpf_error("Mask syntax for networks only");
4337 /* FALLTHROUGH */
4338
4339 case Q_NET:
4340 b = gen_host6(addr, &mask, q.proto, q.dir);
4341 freeaddrinfo(res);
4342 return b;
4343
4344 default:
4345 bpf_error("invalid qualifier against IPv6 address");
4346 /* NOTREACHED */
4347 }
4348 }
4349 #endif /*INET6*/
4350
4351 struct block *
4352 gen_ecode(eaddr, q)
4353 register const u_char *eaddr;
4354 struct qual q;
4355 {
4356 struct block *b, *tmp;
4357
4358 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
4359 if (linktype == DLT_EN10MB)
4360 return gen_ehostop(eaddr, (int)q.dir);
4361 if (linktype == DLT_FDDI)
4362 return gen_fhostop(eaddr, (int)q.dir);
4363 if (linktype == DLT_IEEE802)
4364 return gen_thostop(eaddr, (int)q.dir);
4365 if (linktype == DLT_IEEE802_11)
4366 return gen_wlanhostop(eaddr, (int)q.dir);
4367 if (linktype == DLT_SUNATM && is_lane) {
4368 /*
4369 * Check that the packet doesn't begin with an
4370 * LE Control marker. (We've already generated
4371 * a test for LANE.)
4372 */
4373 tmp = gen_cmp(SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
4374 gen_not(tmp);
4375
4376 /*
4377 * Now check the MAC address.
4378 */
4379 b = gen_ehostop(eaddr, (int)q.dir);
4380 gen_and(tmp, b);
4381 return b;
4382 }
4383 if (linktype == DLT_IP_OVER_FC)
4384 return gen_ipfchostop(eaddr, (int)q.dir);
4385 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4386 }
4387 bpf_error("ethernet address used in non-ether expression");
4388 /* NOTREACHED */
4389 }
4390
4391 void
4392 sappend(s0, s1)
4393 struct slist *s0, *s1;
4394 {
4395 /*
4396 * This is definitely not the best way to do this, but the
4397 * lists will rarely get long.
4398 */
4399 while (s0->next)
4400 s0 = s0->next;
4401 s0->next = s1;
4402 }
4403
4404 static struct slist *
4405 xfer_to_x(a)
4406 struct arth *a;
4407 {
4408 struct slist *s;
4409
4410 s = new_stmt(BPF_LDX|BPF_MEM);
4411 s->s.k = a->regno;
4412 return s;
4413 }
4414
4415 static struct slist *
4416 xfer_to_a(a)
4417 struct arth *a;
4418 {
4419 struct slist *s;
4420
4421 s = new_stmt(BPF_LD|BPF_MEM);
4422 s->s.k = a->regno;
4423 return s;
4424 }
4425
4426 struct arth *
4427 gen_load(proto, index, size)
4428 int proto;
4429 struct arth *index;
4430 int size;
4431 {
4432 struct slist *s, *tmp;
4433 struct block *b;
4434 int regno = alloc_reg();
4435
4436 free_reg(index->regno);
4437 switch (size) {
4438
4439 default:
4440 bpf_error("data size must be 1, 2, or 4");
4441
4442 case 1:
4443 size = BPF_B;
4444 break;
4445
4446 case 2:
4447 size = BPF_H;
4448 break;
4449
4450 case 4:
4451 size = BPF_W;
4452 break;
4453 }
4454 switch (proto) {
4455 default:
4456 bpf_error("unsupported index operation");
4457
4458 case Q_LINK:
4459 /*
4460 * XXX - what about ATM LANE? Should the index be
4461 * relative to the beginning of the AAL5 frame, so
4462 * that 0 refers to the beginning of the LE Control
4463 * field, or relative to the beginning of the LAN
4464 * frame, so that 0 refers, for Ethernet LANE, to
4465 * the beginning of the destination address?
4466 */
4467 s = xfer_to_x(index);
4468 tmp = new_stmt(BPF_LD|BPF_IND|size);
4469 sappend(s, tmp);
4470 sappend(index->s, s);
4471 break;
4472
4473 case Q_IP:
4474 case Q_ARP:
4475 case Q_RARP:
4476 case Q_ATALK:
4477 case Q_DECNET:
4478 case Q_SCA:
4479 case Q_LAT:
4480 case Q_MOPRC:
4481 case Q_MOPDL:
4482 #ifdef INET6
4483 case Q_IPV6:
4484 #endif
4485 /* XXX Note that we assume a fixed link header here. */
4486 s = xfer_to_x(index);
4487 tmp = new_stmt(BPF_LD|BPF_IND|size);
4488 tmp->s.k = off_nl;
4489 sappend(s, tmp);
4490 sappend(index->s, s);
4491
4492 b = gen_proto_abbrev(proto);
4493 if (index->b)
4494 gen_and(index->b, b);
4495 index->b = b;
4496 break;
4497
4498 case Q_SCTP:
4499 case Q_TCP:
4500 case Q_UDP:
4501 case Q_ICMP:
4502 case Q_IGMP:
4503 case Q_IGRP:
4504 case Q_PIM:
4505 case Q_VRRP:
4506 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
4507 s->s.k = off_nl;
4508 sappend(s, xfer_to_a(index));
4509 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
4510 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
4511 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
4512 tmp->s.k = off_nl;
4513 sappend(index->s, s);
4514
4515 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
4516 if (index->b)
4517 gen_and(index->b, b);
4518 #ifdef INET6
4519 gen_and(gen_proto_abbrev(Q_IP), b);
4520 #endif
4521 index->b = b;
4522 break;
4523 #ifdef INET6
4524 case Q_ICMPV6:
4525 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4526 /*NOTREACHED*/
4527 #endif
4528 }
4529 index->regno = regno;
4530 s = new_stmt(BPF_ST);
4531 s->s.k = regno;
4532 sappend(index->s, s);
4533
4534 return index;
4535 }
4536
4537 struct block *
4538 gen_relation(code, a0, a1, reversed)
4539 int code;
4540 struct arth *a0, *a1;
4541 int reversed;
4542 {
4543 struct slist *s0, *s1, *s2;
4544 struct block *b, *tmp;
4545
4546 s0 = xfer_to_x(a1);
4547 s1 = xfer_to_a(a0);
4548 if (code == BPF_JEQ) {
4549 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
4550 b = new_block(JMP(code));
4551 sappend(s1, s2);
4552 }
4553 else
4554 b = new_block(BPF_JMP|code|BPF_X);
4555 if (reversed)
4556 gen_not(b);
4557
4558 sappend(s0, s1);
4559 sappend(a1->s, s0);
4560 sappend(a0->s, a1->s);
4561
4562 b->stmts = a0->s;
4563
4564 free_reg(a0->regno);
4565 free_reg(a1->regno);
4566
4567 /* 'and' together protocol checks */
4568 if (a0->b) {
4569 if (a1->b) {
4570 gen_and(a0->b, tmp = a1->b);
4571 }
4572 else
4573 tmp = a0->b;
4574 } else
4575 tmp = a1->b;
4576
4577 if (tmp)
4578 gen_and(tmp, b);
4579
4580 return b;
4581 }
4582
4583 struct arth *
4584 gen_loadlen()
4585 {
4586 int regno = alloc_reg();
4587 struct arth *a = (struct arth *)newchunk(sizeof(*a));
4588 struct slist *s;
4589
4590 s = new_stmt(BPF_LD|BPF_LEN);
4591 s->next = new_stmt(BPF_ST);
4592 s->next->s.k = regno;
4593 a->s = s;
4594 a->regno = regno;
4595
4596 return a;
4597 }
4598
4599 struct arth *
4600 gen_loadi(val)
4601 int val;
4602 {
4603 struct arth *a;
4604 struct slist *s;
4605 int reg;
4606
4607 a = (struct arth *)newchunk(sizeof(*a));
4608
4609 reg = alloc_reg();
4610
4611 s = new_stmt(BPF_LD|BPF_IMM);
4612 s->s.k = val;
4613 s->next = new_stmt(BPF_ST);
4614 s->next->s.k = reg;
4615 a->s = s;
4616 a->regno = reg;
4617
4618 return a;
4619 }
4620
4621 struct arth *
4622 gen_neg(a)
4623 struct arth *a;
4624 {
4625 struct slist *s;
4626
4627 s = xfer_to_a(a);
4628 sappend(a->s, s);
4629 s = new_stmt(BPF_ALU|BPF_NEG);
4630 s->s.k = 0;
4631 sappend(a->s, s);
4632 s = new_stmt(BPF_ST);
4633 s->s.k = a->regno;
4634 sappend(a->s, s);
4635
4636 return a;
4637 }
4638
4639 struct arth *
4640 gen_arth(code, a0, a1)
4641 int code;
4642 struct arth *a0, *a1;
4643 {
4644 struct slist *s0, *s1, *s2;
4645
4646 s0 = xfer_to_x(a1);
4647 s1 = xfer_to_a(a0);
4648 s2 = new_stmt(BPF_ALU|BPF_X|code);
4649
4650 sappend(s1, s2);
4651 sappend(s0, s1);
4652 sappend(a1->s, s0);
4653 sappend(a0->s, a1->s);
4654
4655 free_reg(a0->regno);
4656 free_reg(a1->regno);
4657
4658 s0 = new_stmt(BPF_ST);
4659 a0->regno = s0->s.k = alloc_reg();
4660 sappend(a0->s, s0);
4661
4662 return a0;
4663 }
4664
4665 /*
4666 * Here we handle simple allocation of the scratch registers.
4667 * If too many registers are alloc'd, the allocator punts.
4668 */
4669 static int regused[BPF_MEMWORDS];
4670 static int curreg;
4671
4672 /*
4673 * Return the next free register.
4674 */
4675 static int
4676 alloc_reg()
4677 {
4678 int n = BPF_MEMWORDS;
4679
4680 while (--n >= 0) {
4681 if (regused[curreg])
4682 curreg = (curreg + 1) % BPF_MEMWORDS;
4683 else {
4684 regused[curreg] = 1;
4685 return curreg;
4686 }
4687 }
4688 bpf_error("too many registers needed to evaluate expression");
4689 /* NOTREACHED */
4690 }
4691
4692 /*
4693 * Return a register to the table so it can
4694 * be used later.
4695 */
4696 static void
4697 free_reg(n)
4698 int n;
4699 {
4700 regused[n] = 0;
4701 }
4702
4703 static struct block *
4704 gen_len(jmp, n)
4705 int jmp, n;
4706 {
4707 struct slist *s;
4708 struct block *b;
4709
4710 s = new_stmt(BPF_LD|BPF_LEN);
4711 b = new_block(JMP(jmp));
4712 b->stmts = s;
4713 b->s.k = n;
4714
4715 return b;
4716 }
4717
4718 struct block *
4719 gen_greater(n)
4720 int n;
4721 {
4722 return gen_len(BPF_JGE, n);
4723 }
4724
4725 /*
4726 * Actually, this is less than or equal.
4727 */
4728 struct block *
4729 gen_less(n)
4730 int n;
4731 {
4732 struct block *b;
4733
4734 b = gen_len(BPF_JGT, n);
4735 gen_not(b);
4736
4737 return b;
4738 }
4739
4740 struct block *
4741 gen_byteop(op, idx, val)
4742 int op, idx, val;
4743 {
4744 struct block *b;
4745 struct slist *s;
4746
4747 switch (op) {
4748 default:
4749 abort();
4750
4751 case '=':
4752 return gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
4753
4754 case '<':
4755 b = gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
4756 b->s.code = JMP(BPF_JGE);
4757 gen_not(b);
4758 return b;
4759
4760 case '>':
4761 b = gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
4762 b->s.code = JMP(BPF_JGT);
4763 return b;
4764
4765 case '|':
4766 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
4767 break;
4768
4769 case '&':
4770 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
4771 break;
4772 }
4773 s->s.k = val;
4774 b = new_block(JMP(BPF_JEQ));
4775 b->stmts = s;
4776 gen_not(b);
4777
4778 return b;
4779 }
4780
4781 static u_char abroadcast[] = { 0x0 };
4782
4783 struct block *
4784 gen_broadcast(proto)
4785 int proto;
4786 {
4787 bpf_u_int32 hostmask;
4788 struct block *b0, *b1, *b2;
4789 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4790
4791 switch (proto) {
4792
4793 case Q_DEFAULT:
4794 case Q_LINK:
4795 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
4796 return gen_ahostop(abroadcast, Q_DST);
4797 if (linktype == DLT_EN10MB)
4798 return gen_ehostop(ebroadcast, Q_DST);
4799 if (linktype == DLT_FDDI)
4800 return gen_fhostop(ebroadcast, Q_DST);
4801 if (linktype == DLT_IEEE802)
4802 return gen_thostop(ebroadcast, Q_DST);
4803 if (linktype == DLT_IEEE802_11)
4804 return gen_wlanhostop(ebroadcast, Q_DST);
4805 if (linktype == DLT_IP_OVER_FC)
4806 return gen_ipfchostop(ebroadcast, Q_DST);
4807 if (linktype == DLT_SUNATM && is_lane) {
4808 /*
4809 * Check that the packet doesn't begin with an
4810 * LE Control marker. (We've already generated
4811 * a test for LANE.)
4812 */
4813 b1 = gen_cmp(SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
4814 gen_not(b1);
4815
4816 /*
4817 * Now check the MAC address.
4818 */
4819 b0 = gen_ehostop(ebroadcast, Q_DST);
4820 gen_and(b1, b0);
4821 return b0;
4822 }
4823 bpf_error("not a broadcast link");
4824 break;
4825
4826 case Q_IP:
4827 b0 = gen_linktype(ETHERTYPE_IP);
4828 hostmask = ~netmask;
4829 b1 = gen_mcmp(off_nl + 16, BPF_W, (bpf_int32)0, hostmask);
4830 b2 = gen_mcmp(off_nl + 16, BPF_W,
4831 (bpf_int32)(~0 & hostmask), hostmask);
4832 gen_or(b1, b2);
4833 gen_and(b0, b2);
4834 return b2;
4835 }
4836 bpf_error("only link-layer/IP broadcast filters supported");
4837 /* NOTREACHED */
4838 }
4839
4840 /*
4841 * Generate code to test the low-order bit of a MAC address (that's
4842 * the bottom bit of the *first* byte).
4843 */
4844 static struct block *
4845 gen_mac_multicast(offset)
4846 int offset;
4847 {
4848 register struct block *b0;
4849 register struct slist *s;
4850
4851 /* link[offset] & 1 != 0 */
4852 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
4853 s->s.k = offset;
4854 b0 = new_block(JMP(BPF_JSET));
4855 b0->s.k = 1;
4856 b0->stmts = s;
4857 return b0;
4858 }
4859
4860 struct block *
4861 gen_multicast(proto)
4862 int proto;
4863 {
4864 register struct block *b0, *b1, *b2;
4865 register struct slist *s;
4866
4867 switch (proto) {
4868
4869 case Q_DEFAULT:
4870 case Q_LINK:
4871 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
4872 /* all ARCnet multicasts use the same address */
4873 return gen_ahostop(abroadcast, Q_DST);
4874
4875 if (linktype == DLT_EN10MB) {
4876 /* ether[0] & 1 != 0 */
4877 return gen_mac_multicast(0);
4878 }
4879
4880 if (linktype == DLT_FDDI) {
4881 /*
4882 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4883 *
4884 * XXX - was that referring to bit-order issues?
4885 */
4886 /* fddi[1] & 1 != 0 */
4887 return gen_mac_multicast(1);
4888 }
4889
4890 if (linktype == DLT_IEEE802) {
4891 /* tr[2] & 1 != 0 */
4892 return gen_mac_multicast(2);
4893 }
4894
4895 if (linktype == DLT_IEEE802_11) {
4896 /*
4897 * Oh, yuk.
4898 *
4899 * For control frames, there is no DA.
4900 *
4901 * For management frames, DA is at an
4902 * offset of 4 from the beginning of
4903 * the packet.
4904 *
4905 * For data frames, DA is at an offset
4906 * of 4 from the beginning of the packet
4907 * if To DS is clear and at an offset of
4908 * 16 from the beginning of the packet
4909 * if To DS is set.
4910 */
4911
4912 /*
4913 * Generate the tests to be done for data frames.
4914 *
4915 * First, check for To DS set, i.e. "link[1] & 0x01".
4916 */
4917 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
4918 s->s.k = 1;
4919 b1 = new_block(JMP(BPF_JSET));
4920 b1->s.k = 0x01; /* To DS */
4921 b1->stmts = s;
4922
4923 /*
4924 * If To DS is set, the DA is at 16.
4925 */
4926 b0 = gen_mac_multicast(16);
4927 gen_and(b1, b0);
4928
4929 /*
4930 * Now, check for To DS not set, i.e. check
4931 * "!(link[1] & 0x01)".
4932 */
4933 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
4934 s->s.k = 1;
4935 b2 = new_block(JMP(BPF_JSET));
4936 b2->s.k = 0x01; /* To DS */
4937 b2->stmts = s;
4938 gen_not(b2);
4939
4940 /*
4941 * If To DS is not set, the DA is at 4.
4942 */
4943 b1 = gen_mac_multicast(4);
4944 gen_and(b2, b1);
4945
4946 /*
4947 * Now OR together the last two checks. That gives
4948 * the complete set of checks for data frames.
4949 */
4950 gen_or(b1, b0);
4951
4952 /*
4953 * Now check for a data frame.
4954 * I.e, check "link[0] & 0x08".
4955 */
4956 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
4957 s->s.k = 0;
4958 b1 = new_block(JMP(BPF_JSET));
4959 b1->s.k = 0x08;
4960 b1->stmts = s;
4961
4962 /*
4963 * AND that with the checks done for data frames.
4964 */
4965 gen_and(b1, b0);
4966
4967 /*
4968 * If the high-order bit of the type value is 0, this
4969 * is a management frame.
4970 * I.e, check "!(link[0] & 0x08)".
4971 */
4972 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
4973 s->s.k = 0;
4974 b2 = new_block(JMP(BPF_JSET));
4975 b2->s.k = 0x08;
4976 b2->stmts = s;
4977 gen_not(b2);
4978
4979 /*
4980 * For management frames, the DA is at 4.
4981 */
4982 b1 = gen_mac_multicast(4);
4983 gen_and(b2, b1);
4984
4985 /*
4986 * OR that with the checks done for data frames.
4987 * That gives the checks done for management and
4988 * data frames.
4989 */
4990 gen_or(b1, b0);
4991
4992 /*
4993 * If the low-order bit of the type value is 1,
4994 * this is either a control frame or a frame
4995 * with a reserved type, and thus not a
4996 * frame with an SA.
4997 *
4998 * I.e., check "!(link[0] & 0x04)".
4999 */
5000 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
5001 s->s.k = 0;
5002 b1 = new_block(JMP(BPF_JSET));
5003 b1->s.k = 0x04;
5004 b1->stmts = s;
5005 gen_not(b1);
5006
5007 /*
5008 * AND that with the checks for data and management
5009 * frames.
5010 */
5011 gen_and(b1, b0);
5012 return b0;
5013 }
5014
5015 if (linktype == DLT_IP_OVER_FC) {
5016 b0 = gen_mac_multicast(2);
5017 return b0;
5018 }
5019
5020 if (linktype == DLT_SUNATM && is_lane) {
5021 /*
5022 * Check that the packet doesn't begin with an
5023 * LE Control marker. (We've already generated
5024 * a test for LANE.)
5025 */
5026 b1 = gen_cmp(SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
5027 gen_not(b1);
5028
5029 /* ether[off_mac] & 1 != 0 */
5030 b0 = gen_mac_multicast(off_mac);
5031 gen_and(b1, b0);
5032 return b0;
5033 }
5034
5035 /* Link not known to support multicasts */
5036 break;
5037
5038 case Q_IP:
5039 b0 = gen_linktype(ETHERTYPE_IP);
5040 b1 = gen_cmp(off_nl + 16, BPF_B, (bpf_int32)224);
5041 b1->s.code = JMP(BPF_JGE);
5042 gen_and(b0, b1);
5043 return b1;
5044
5045 #ifdef INET6
5046 case Q_IPV6:
5047 b0 = gen_linktype(ETHERTYPE_IPV6);
5048 b1 = gen_cmp(off_nl + 24, BPF_B, (bpf_int32)255);
5049 gen_and(b0, b1);
5050 return b1;
5051 #endif /* INET6 */
5052 }
5053 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5054 /* NOTREACHED */
5055 }
5056
5057 /*
5058 * generate command for inbound/outbound. It's here so we can
5059 * make it link-type specific. 'dir' = 0 implies "inbound",
5060 * = 1 implies "outbound".
5061 */
5062 struct block *
5063 gen_inbound(dir)
5064 int dir;
5065 {
5066 register struct block *b0;
5067
5068 /*
5069 * Only some data link types support inbound/outbound qualifiers.
5070 */
5071 switch (linktype) {
5072 case DLT_SLIP:
5073 b0 = gen_relation(BPF_JEQ,
5074 gen_load(Q_LINK, gen_loadi(0), 1),
5075 gen_loadi(0),
5076 dir);
5077 break;
5078
5079 case DLT_LINUX_SLL:
5080 if (dir) {
5081 /*
5082 * Match packets sent by this machine.
5083 */
5084 b0 = gen_cmp(0, BPF_H, LINUX_SLL_OUTGOING);
5085 } else {
5086 /*
5087 * Match packets sent to this machine.
5088 * (No broadcast or multicast packets, or
5089 * packets sent to some other machine and
5090 * received promiscuously.)
5091 *
5092 * XXX - packets sent to other machines probably
5093 * shouldn't be matched, but what about broadcast
5094 * or multicast packets we received?
5095 */
5096 b0 = gen_cmp(0, BPF_H, LINUX_SLL_HOST);
5097 }
5098 break;
5099
5100 case DLT_PFLOG:
5101 b0 = gen_cmp(offsetof(struct pfloghdr, dir), BPF_B,
5102 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
5103 break;
5104
5105 case DLT_PPP_WITHDIRECTION:
5106 if (dir) {
5107 /* match outgoing packets */
5108 b0 = gen_cmp(0, BPF_B, PPP_WITHDIRECTION_OUT);
5109 } else {
5110 /* match incoming packets */
5111 b0 = gen_cmp(0, BPF_B, PPP_WITHDIRECTION_IN);
5112 }
5113 break;
5114
5115 case DLT_JUNIPER_ATM1:
5116 case DLT_JUNIPER_ATM2:
5117 /* juniper flags (including direction) are stored
5118 * the byte after the 3-byte magic number */
5119 if (dir) {
5120 /* match outgoing packets */
5121 b0 = gen_mcmp(3, BPF_B, 0, 0x01);
5122 } else {
5123 /* match incoming packets */
5124 b0 = gen_mcmp(3, BPF_B, 1, 0x01);
5125 }
5126 break;
5127
5128 default:
5129 bpf_error("inbound/outbound not supported on linktype %d",
5130 linktype);
5131 b0 = NULL;
5132 /* NOTREACHED */
5133 }
5134 return (b0);
5135 }
5136
5137 /* PF firewall log matched interface */
5138 struct block *
5139 gen_pf_ifname(const char *ifname)
5140 {
5141 struct block *b0;
5142 u_int len, off;
5143
5144 if (linktype == DLT_PFLOG) {
5145 len = sizeof(((struct pfloghdr *)0)->ifname);
5146 off = offsetof(struct pfloghdr, ifname);
5147 } else {
5148 bpf_error("ifname not supported on linktype 0x%x", linktype);
5149 /* NOTREACHED */
5150 }
5151 if (strlen(ifname) >= len) {
5152 bpf_error("ifname interface names can only be %d characters",
5153 len-1);
5154 /* NOTREACHED */
5155 }
5156 b0 = gen_bcmp(off, strlen(ifname), (const u_char *)ifname);
5157 return (b0);
5158 }
5159
5160 /* PF firewall log matched interface */
5161 struct block *
5162 gen_pf_ruleset(char *ruleset)
5163 {
5164 struct block *b0;
5165
5166 if (linktype != DLT_PFLOG) {
5167 bpf_error("ruleset not supported on linktype 0x%x", linktype);
5168 /* NOTREACHED */
5169 }
5170 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
5171 bpf_error("ruleset names can only be %ld characters",
5172 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
5173 /* NOTREACHED */
5174 }
5175 b0 = gen_bcmp(offsetof(struct pfloghdr, ruleset),
5176 strlen(ruleset), (const u_char *)ruleset);
5177 return (b0);
5178 }
5179
5180 /* PF firewall log rule number */
5181 struct block *
5182 gen_pf_rnr(int rnr)
5183 {
5184 struct block *b0;
5185
5186 if (linktype == DLT_PFLOG) {
5187 b0 = gen_cmp(offsetof(struct pfloghdr, rulenr), BPF_W,
5188 (bpf_int32)rnr);
5189 } else {
5190 bpf_error("rnr not supported on linktype 0x%x", linktype);
5191 /* NOTREACHED */
5192 }
5193
5194 return (b0);
5195 }
5196
5197 /* PF firewall log sub-rule number */
5198 struct block *
5199 gen_pf_srnr(int srnr)
5200 {
5201 struct block *b0;
5202
5203 if (linktype != DLT_PFLOG) {
5204 bpf_error("srnr not supported on linktype 0x%x", linktype);
5205 /* NOTREACHED */
5206 }
5207
5208 b0 = gen_cmp(offsetof(struct pfloghdr, subrulenr), BPF_W,
5209 (bpf_int32)srnr);
5210 return (b0);
5211 }
5212
5213 /* PF firewall log reason code */
5214 struct block *
5215 gen_pf_reason(int reason)
5216 {
5217 struct block *b0;
5218
5219 if (linktype == DLT_PFLOG) {
5220 b0 = gen_cmp(offsetof(struct pfloghdr, reason), BPF_B,
5221 (bpf_int32)reason);
5222 } else {
5223 bpf_error("reason not supported on linktype 0x%x", linktype);
5224 /* NOTREACHED */
5225 }
5226
5227 return (b0);
5228 }
5229
5230 /* PF firewall log action */
5231 struct block *
5232 gen_pf_action(int action)
5233 {
5234 struct block *b0;
5235
5236 if (linktype == DLT_PFLOG) {
5237 b0 = gen_cmp(offsetof(struct pfloghdr, action), BPF_B,
5238 (bpf_int32)action);
5239 } else {
5240 bpf_error("action not supported on linktype 0x%x", linktype);
5241 /* NOTREACHED */
5242 }
5243
5244 return (b0);
5245 }
5246
5247 struct block *
5248 gen_acode(eaddr, q)
5249 register const u_char *eaddr;
5250 struct qual q;
5251 {
5252 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
5253 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
5254 return gen_ahostop(eaddr, (int)q.dir);
5255 }
5256 bpf_error("ARCnet address used in non-arc expression");
5257 /* NOTREACHED */
5258 }
5259
5260 static struct block *
5261 gen_ahostop(eaddr, dir)
5262 register const u_char *eaddr;
5263 register int dir;
5264 {
5265 register struct block *b0, *b1;
5266
5267 switch (dir) {
5268 /* src comes first, different from Ethernet */
5269 case Q_SRC:
5270 return gen_bcmp(0, 1, eaddr);
5271
5272 case Q_DST:
5273 return gen_bcmp(1, 1, eaddr);
5274
5275 case Q_AND:
5276 b0 = gen_ahostop(eaddr, Q_SRC);
5277 b1 = gen_ahostop(eaddr, Q_DST);
5278 gen_and(b0, b1);
5279 return b1;
5280
5281 case Q_DEFAULT:
5282 case Q_OR:
5283 b0 = gen_ahostop(eaddr, Q_SRC);
5284 b1 = gen_ahostop(eaddr, Q_DST);
5285 gen_or(b0, b1);
5286 return b1;
5287 }
5288 abort();
5289 /* NOTREACHED */
5290 }
5291
5292 /*
5293 * support IEEE 802.1Q VLAN trunk over ethernet
5294 */
5295 struct block *
5296 gen_vlan(vlan_num)
5297 int vlan_num;
5298 {
5299 struct block *b0;
5300
5301 /*
5302 * Change the offsets to point to the type and data fields within
5303 * the VLAN packet. This is somewhat of a kludge.
5304 */
5305 if (orig_nl == (u_int)-1) {
5306 orig_linktype = off_linktype; /* save original values */
5307 orig_nl = off_nl;
5308 orig_nl_nosnap = off_nl_nosnap;
5309
5310 switch (linktype) {
5311
5312 case DLT_EN10MB:
5313 off_linktype = 16;
5314 off_nl_nosnap = 18;
5315 off_nl = 18;
5316 break;
5317
5318 default:
5319 bpf_error("no VLAN support for data link type %d",
5320 linktype);
5321 /*NOTREACHED*/
5322 }
5323 }
5324
5325 /* check for VLAN */
5326 b0 = gen_cmp(orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q);
5327
5328 /* If a specific VLAN is requested, check VLAN id */
5329 if (vlan_num >= 0) {
5330 struct block *b1;
5331
5332 b1 = gen_mcmp(orig_nl, BPF_H, (bpf_int32)vlan_num, 0x0fff);
5333 gen_and(b0, b1);
5334 b0 = b1;
5335 }
5336
5337 return (b0);
5338 }
5339
5340 /*
5341 * support for MPLS
5342 */
5343 struct block *
5344 gen_mpls(label_num)
5345 int label_num;
5346 {
5347 struct block *b0;
5348
5349 /*
5350 * Change the offsets to point to the type and data fields within
5351 * the MPLS packet. This is somewhat of a kludge.
5352 */
5353 if (orig_nl == (u_int)-1) {
5354 orig_linktype = off_linktype; /* save original values */
5355 orig_nl = off_nl;
5356 orig_nl_nosnap = off_nl_nosnap;
5357
5358 switch (linktype) {
5359
5360 case DLT_EN10MB:
5361 off_linktype = 16;
5362 off_nl_nosnap = 18;
5363 off_nl = 18;
5364
5365 b0 = gen_cmp(orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_MPLS);
5366 break;
5367
5368 case DLT_PPP:
5369 off_linktype = 6;
5370 off_nl_nosnap = 8;
5371 off_nl = 8;
5372
5373 b0 = gen_cmp(orig_linktype, BPF_H, (bpf_int32)PPP_MPLS_UCAST);
5374 break;
5375
5376 case DLT_C_HDLC:
5377 off_linktype = 6;
5378 off_nl_nosnap = 8;
5379 off_nl = 8;
5380
5381 b0 = gen_cmp(orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_MPLS);
5382 break;
5383
5384 /* FIXME add other DLT_s ...
5385 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5386 * leave it for now */
5387
5388 default:
5389 bpf_error("no MPLS support for data link type %d",
5390 linktype);
5391 /*NOTREACHED*/
5392 }
5393 } else {
5394 bpf_error("'mpls' can't be combined with 'vlan' or another 'mpls'");
5395 /*NOTREACHED*/
5396 }
5397
5398 /* If a specific MPLS label is requested, check it */
5399 if (label_num >= 0) {
5400 struct block *b1;
5401
5402 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
5403 b1 = gen_mcmp(orig_nl, BPF_W, (bpf_int32)label_num, 0xfffff000); /* only compare the first 20 bits */
5404 gen_and(b0, b1);
5405 b0 = b1;
5406 }
5407
5408 return (b0);
5409 }
5410
5411 struct block *
5412 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
5413 int atmfield;
5414 bpf_u_int32 jvalue;
5415 bpf_u_int32 jtype;
5416 int reverse;
5417 {
5418 struct block *b0;
5419
5420 switch (atmfield) {
5421
5422 case A_VPI:
5423 if (!is_atm)
5424 bpf_error("'vpi' supported only on raw ATM");
5425 if (off_vpi == (u_int)-1)
5426 abort();
5427 b0 = gen_ncmp(BPF_B, off_vpi, 0xffffffff, (u_int)jtype,
5428 (u_int)jvalue, reverse);
5429 break;
5430
5431 case A_VCI:
5432 if (!is_atm)
5433 bpf_error("'vci' supported only on raw ATM");
5434 if (off_vci == (u_int)-1)
5435 abort();
5436 b0 = gen_ncmp(BPF_H, off_vci, 0xffffffff, (u_int)jtype,
5437 (u_int)jvalue, reverse);
5438 break;
5439
5440 case A_PROTOTYPE:
5441 if (off_proto == (u_int)-1)
5442 abort(); /* XXX - this isn't on FreeBSD */
5443 b0 = gen_ncmp(BPF_B, off_proto, 0x0f, (u_int)jtype,
5444 (u_int)jvalue, reverse);
5445 break;
5446
5447 case A_MSGTYPE:
5448 if (off_payload == (u_int)-1)
5449 abort();
5450 b0 = gen_ncmp(BPF_B, off_payload + MSG_TYPE_POS, 0xffffffff,
5451 (u_int)jtype, (u_int)jvalue, reverse);
5452 break;
5453
5454 case A_CALLREFTYPE:
5455 if (!is_atm)
5456 bpf_error("'callref' supported only on raw ATM");
5457 if (off_proto == (u_int)-1)
5458 abort();
5459 b0 = gen_ncmp(BPF_B, off_proto, 0xffffffff, (u_int)jtype,
5460 (u_int)jvalue, reverse);
5461 break;
5462
5463 default:
5464 abort();
5465 }
5466 return b0;
5467 }
5468
5469 struct block *
5470 gen_atmtype_abbrev(type)
5471 int type;
5472 {
5473 struct block *b0, *b1;
5474
5475 switch (type) {
5476
5477 case A_METAC:
5478 /* Get all packets in Meta signalling Circuit */
5479 if (!is_atm)
5480 bpf_error("'metac' supported only on raw ATM");
5481 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
5482 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
5483 gen_and(b0, b1);
5484 break;
5485
5486 case A_BCC:
5487 /* Get all packets in Broadcast Circuit*/
5488 if (!is_atm)
5489 bpf_error("'bcc' supported only on raw ATM");
5490 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
5491 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
5492 gen_and(b0, b1);
5493 break;
5494
5495 case A_OAMF4SC:
5496 /* Get all cells in Segment OAM F4 circuit*/
5497 if (!is_atm)
5498 bpf_error("'oam4sc' supported only on raw ATM");
5499 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
5500 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
5501 gen_and(b0, b1);
5502 break;
5503
5504 case A_OAMF4EC:
5505 /* Get all cells in End-to-End OAM F4 Circuit*/
5506 if (!is_atm)
5507 bpf_error("'oam4ec' supported only on raw ATM");
5508 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
5509 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
5510 gen_and(b0, b1);
5511 break;
5512
5513 case A_SC:
5514 /* Get all packets in connection Signalling Circuit */
5515 if (!is_atm)
5516 bpf_error("'sc' supported only on raw ATM");
5517 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
5518 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
5519 gen_and(b0, b1);
5520 break;
5521
5522 case A_ILMIC:
5523 /* Get all packets in ILMI Circuit */
5524 if (!is_atm)
5525 bpf_error("'ilmic' supported only on raw ATM");
5526 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
5527 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
5528 gen_and(b0, b1);
5529 break;
5530
5531 case A_LANE:
5532 /* Get all LANE packets */
5533 if (!is_atm)
5534 bpf_error("'lane' supported only on raw ATM");
5535 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
5536
5537 /*
5538 * Arrange that all subsequent tests assume LANE
5539 * rather than LLC-encapsulated packets, and set
5540 * the offsets appropriately for LANE-encapsulated
5541 * Ethernet.
5542 *
5543 * "off_mac" is the offset of the Ethernet header,
5544 * which is 2 bytes past the ATM pseudo-header
5545 * (skipping the pseudo-header and 2-byte LE Client
5546 * field). The other offsets are Ethernet offsets
5547 * relative to "off_mac".
5548 */
5549 is_lane = 1;
5550 off_mac = off_payload + 2; /* MAC header */
5551 off_linktype = off_mac + 12;
5552 off_nl = off_mac + 14; /* Ethernet II */
5553 off_nl_nosnap = off_mac + 17; /* 802.3+802.2 */
5554 break;
5555
5556 case A_LLC:
5557 /* Get all LLC-encapsulated packets */
5558 if (!is_atm)
5559 bpf_error("'llc' supported only on raw ATM");
5560 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
5561 is_lane = 0;
5562 break;
5563
5564 default:
5565 abort();
5566 }
5567 return b1;
5568 }
5569
5570
5571 static struct block *
5572 gen_msg_abbrev(type)
5573 int type;
5574 {
5575 struct block *b1;
5576
5577 /*
5578 * Q.2931 signalling protocol messages for handling virtual circuits
5579 * establishment and teardown
5580 */
5581 switch (type) {
5582
5583 case A_SETUP:
5584 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
5585 break;
5586
5587 case A_CALLPROCEED:
5588 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
5589 break;
5590
5591 case A_CONNECT:
5592 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
5593 break;
5594
5595 case A_CONNECTACK:
5596 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
5597 break;
5598
5599 case A_RELEASE:
5600 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
5601 break;
5602
5603 case A_RELEASE_DONE:
5604 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
5605 break;
5606
5607 default:
5608 abort();
5609 }
5610 return b1;
5611 }
5612
5613 struct block *
5614 gen_atmmulti_abbrev(type)
5615 int type;
5616 {
5617 struct block *b0, *b1;
5618
5619 switch (type) {
5620
5621 case A_OAM:
5622 if (!is_atm)
5623 bpf_error("'oam' supported only on raw ATM");
5624 b1 = gen_atmmulti_abbrev(A_OAMF4);
5625 break;
5626
5627 case A_OAMF4:
5628 if (!is_atm)
5629 bpf_error("'oamf4' supported only on raw ATM");
5630 /* OAM F4 type */
5631 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
5632 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
5633 gen_or(b0, b1);
5634 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
5635 gen_and(b0, b1);
5636 break;
5637
5638 case A_CONNECTMSG:
5639 /*
5640 * Get Q.2931 signalling messages for switched
5641 * virtual connection
5642 */
5643 if (!is_atm)
5644 bpf_error("'connectmsg' supported only on raw ATM");
5645 b0 = gen_msg_abbrev(A_SETUP);
5646 b1 = gen_msg_abbrev(A_CALLPROCEED);
5647 gen_or(b0, b1);
5648 b0 = gen_msg_abbrev(A_CONNECT);
5649 gen_or(b0, b1);
5650 b0 = gen_msg_abbrev(A_CONNECTACK);
5651 gen_or(b0, b1);
5652 b0 = gen_msg_abbrev(A_RELEASE);
5653 gen_or(b0, b1);
5654 b0 = gen_msg_abbrev(A_RELEASE_DONE);
5655 gen_or(b0, b1);
5656 b0 = gen_atmtype_abbrev(A_SC);
5657 gen_and(b0, b1);
5658 break;
5659
5660 case A_METACONNECT:
5661 if (!is_atm)
5662 bpf_error("'metaconnect' supported only on raw ATM");
5663 b0 = gen_msg_abbrev(A_SETUP);
5664 b1 = gen_msg_abbrev(A_CALLPROCEED);
5665 gen_or(b0, b1);
5666 b0 = gen_msg_abbrev(A_CONNECT);
5667 gen_or(b0, b1);
5668 b0 = gen_msg_abbrev(A_RELEASE);
5669 gen_or(b0, b1);
5670 b0 = gen_msg_abbrev(A_RELEASE_DONE);
5671 gen_or(b0, b1);
5672 b0 = gen_atmtype_abbrev(A_METAC);
5673 gen_and(b0, b1);
5674 break;
5675
5676 default:
5677 abort();
5678 }
5679 return b1;
5680 }