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