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