PageRenderTime 86ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 1ms

/external/bsd/libpcap/dist/gencode.c

https://github.com/rumpkernel/netbsd-userspace-src
C | 8542 lines | 5204 code | 1076 blank | 2262 comment | 551 complexity | a9abca5d0b506d3e7ee682545d00488d MD5 | raw file
  1. /* $NetBSD: gencode.c,v 1.6 2013/12/31 17:08:23 christos Exp $ */
  2. /*#define CHASE_CHAIN*/
  3. /*
  4. * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that: (1) source code distributions
  9. * retain the above copyright notice and this paragraph in its entirety, (2)
  10. * distributions including binary code include the above copyright notice and
  11. * this paragraph in its entirety in the documentation or other materials
  12. * provided with the distribution, and (3) all advertising materials mentioning
  13. * features or use of this software display the following acknowledgement:
  14. * ``This product includes software developed by the University of California,
  15. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  16. * the University nor the names of its contributors may be used to endorse
  17. * or promote products derived from this software without specific prior
  18. * written permission.
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  22. */
  23. #ifndef lint
  24. static const char rcsid[] _U_ =
  25. "@(#) Header: /tcpdump/master/libpcap/gencode.c,v 1.309 2008-12-23 20:13:29 guy Exp (LBL)";
  26. #endif
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #ifdef WIN32
  31. #include <pcap-stdinc.h>
  32. #else /* WIN32 */
  33. #if HAVE_INTTYPES_H
  34. #include <inttypes.h>
  35. #elif HAVE_STDINT_H
  36. #include <stdint.h>
  37. #endif
  38. #ifdef HAVE_SYS_BITYPES_H
  39. #include <sys/bitypes.h>
  40. #endif
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #endif /* WIN32 */
  44. /*
  45. * XXX - why was this included even on UNIX?
  46. */
  47. #ifdef __MINGW32__
  48. #include "ip6_misc.h"
  49. #endif
  50. #ifndef WIN32
  51. #ifdef __NetBSD__
  52. #include <sys/param.h>
  53. #include <net/dlt.h>
  54. #endif
  55. #include <netinet/in.h>
  56. #include <arpa/inet.h>
  57. #endif /* WIN32 */
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <memory.h>
  61. #include <setjmp.h>
  62. #include <stdarg.h>
  63. #ifdef MSDOS
  64. #include "pcap-dos.h"
  65. #endif
  66. #include "pcap-int.h"
  67. #include "ethertype.h"
  68. #include "nlpid.h"
  69. #include "llc.h"
  70. #include "gencode.h"
  71. #include "ieee80211.h"
  72. #include "atmuni31.h"
  73. #include "sunatmpos.h"
  74. #include "ppp.h"
  75. #include "pcap/sll.h"
  76. #include "pcap/ipnet.h"
  77. #include "arcnet.h"
  78. #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
  79. #include <linux/types.h>
  80. #include <linux/if_packet.h>
  81. #include <linux/filter.h>
  82. #endif
  83. #ifdef HAVE_NET_PFVAR_H
  84. #include <sys/socket.h>
  85. #include <net/if.h>
  86. #include <net/pfvar.h>
  87. #include <net/if_pflog.h>
  88. #endif
  89. #ifndef offsetof
  90. #define offsetof(s, e) ((size_t)&((s *)0)->e)
  91. #endif
  92. #ifdef INET6
  93. #ifndef WIN32
  94. #include <netdb.h> /* for "struct addrinfo" */
  95. #endif /* WIN32 */
  96. #endif /*INET6*/
  97. #include <pcap/namedb.h>
  98. #define ETHERMTU 1500
  99. #ifndef IPPROTO_HOPOPTS
  100. #define IPPROTO_HOPOPTS 0
  101. #endif
  102. #ifndef IPPROTO_ROUTING
  103. #define IPPROTO_ROUTING 43
  104. #endif
  105. #ifndef IPPROTO_FRAGMENT
  106. #define IPPROTO_FRAGMENT 44
  107. #endif
  108. #ifndef IPPROTO_DSTOPTS
  109. #define IPPROTO_DSTOPTS 60
  110. #endif
  111. #ifndef IPPROTO_SCTP
  112. #define IPPROTO_SCTP 132
  113. #endif
  114. #ifdef HAVE_OS_PROTO_H
  115. #include "os-proto.h"
  116. #endif
  117. #define JMP(c) ((c)|BPF_JMP|BPF_K)
  118. /* Locals */
  119. static jmp_buf top_ctx;
  120. static pcap_t *bpf_pcap;
  121. /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
  122. #ifdef WIN32
  123. static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
  124. #else
  125. static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
  126. #endif
  127. /* XXX */
  128. static int pcap_fddipad;
  129. /* VARARGS */
  130. void
  131. bpf_error(const char *fmt, ...)
  132. {
  133. va_list ap;
  134. va_start(ap, fmt);
  135. if (bpf_pcap != NULL)
  136. (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
  137. fmt, ap);
  138. va_end(ap);
  139. longjmp(top_ctx, 1);
  140. /* NOTREACHED */
  141. }
  142. static void init_linktype(pcap_t *);
  143. static void init_regs(void);
  144. static int alloc_reg(void);
  145. static void free_reg(int);
  146. static struct block *root;
  147. /*
  148. * Value passed to gen_load_a() to indicate what the offset argument
  149. * is relative to.
  150. */
  151. enum e_offrel {
  152. OR_PACKET, /* relative to the beginning of the packet */
  153. OR_LINK, /* relative to the beginning of the link-layer header */
  154. OR_MACPL, /* relative to the end of the MAC-layer header */
  155. OR_NET, /* relative to the network-layer header */
  156. OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
  157. OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
  158. OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
  159. };
  160. #ifdef INET6
  161. /*
  162. * As errors are handled by a longjmp, anything allocated must be freed
  163. * in the longjmp handler, so it must be reachable from that handler.
  164. * One thing that's allocated is the result of pcap_nametoaddrinfo();
  165. * it must be freed with freeaddrinfo(). This variable points to any
  166. * addrinfo structure that would need to be freed.
  167. */
  168. static struct addrinfo *ai;
  169. #endif
  170. /*
  171. * We divy out chunks of memory rather than call malloc each time so
  172. * we don't have to worry about leaking memory. It's probably
  173. * not a big deal if all this memory was wasted but if this ever
  174. * goes into a library that would probably not be a good idea.
  175. *
  176. * XXX - this *is* in a library....
  177. */
  178. #define NCHUNKS 16
  179. #define CHUNK0SIZE 1024
  180. struct chunk {
  181. u_int n_left;
  182. void *m;
  183. };
  184. static struct chunk chunks[NCHUNKS];
  185. static int cur_chunk;
  186. static void *newchunk(u_int);
  187. static void freechunks(void);
  188. static inline struct block *new_block(int);
  189. static inline struct slist *new_stmt(int);
  190. static struct block *gen_retblk(int);
  191. static inline void syntax(void);
  192. static void backpatch(struct block *, struct block *);
  193. static void merge(struct block *, struct block *);
  194. static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
  195. static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
  196. static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
  197. static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
  198. static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
  199. static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
  200. bpf_u_int32);
  201. static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
  202. static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
  203. bpf_u_int32, bpf_u_int32, int, bpf_int32);
  204. static struct slist *gen_load_llrel(u_int, u_int);
  205. static struct slist *gen_load_macplrel(u_int, u_int);
  206. static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
  207. static struct slist *gen_loadx_iphdrlen(void);
  208. static struct block *gen_uncond(int);
  209. static inline struct block *gen_true(void);
  210. static inline struct block *gen_false(void);
  211. static struct block *gen_ether_linktype(int);
  212. static struct block *gen_ipnet_linktype(int);
  213. static struct block *gen_linux_sll_linktype(int);
  214. static struct slist *gen_load_prism_llprefixlen(void);
  215. static struct slist *gen_load_avs_llprefixlen(void);
  216. static struct slist *gen_load_radiotap_llprefixlen(void);
  217. static struct slist *gen_load_ppi_llprefixlen(void);
  218. static void insert_compute_vloffsets(struct block *);
  219. static struct slist *gen_llprefixlen(void);
  220. static struct slist *gen_off_macpl(void);
  221. static int ethertype_to_ppptype(int);
  222. static struct block *gen_linktype(int);
  223. static struct block *gen_snap(bpf_u_int32, bpf_u_int32);
  224. static struct block *gen_llc_linktype(int);
  225. static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
  226. #ifdef INET6
  227. static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
  228. #endif
  229. static struct block *gen_ahostop(const u_char *, int);
  230. static struct block *gen_ehostop(const u_char *, int);
  231. static struct block *gen_fhostop(const u_char *, int);
  232. static struct block *gen_thostop(const u_char *, int);
  233. static struct block *gen_wlanhostop(const u_char *, int);
  234. static struct block *gen_ipfchostop(const u_char *, int);
  235. static struct block *gen_dnhostop(bpf_u_int32, int);
  236. static struct block *gen_mpls_linktype(int);
  237. static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
  238. #ifdef INET6
  239. static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
  240. #endif
  241. #ifndef INET6
  242. static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
  243. #endif
  244. static struct block *gen_ipfrag(void);
  245. static struct block *gen_portatom(int, bpf_int32);
  246. static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
  247. static struct block *gen_portatom6(int, bpf_int32);
  248. static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
  249. struct block *gen_portop(int, int, int);
  250. static struct block *gen_port(int, int, int);
  251. struct block *gen_portrangeop(int, int, int, int);
  252. static struct block *gen_portrange(int, int, int, int);
  253. struct block *gen_portop6(int, int, int);
  254. static struct block *gen_port6(int, int, int);
  255. struct block *gen_portrangeop6(int, int, int, int);
  256. static struct block *gen_portrange6(int, int, int, int);
  257. static int lookup_proto(const char *, int);
  258. static struct block *gen_protochain(int, int, int);
  259. static struct block *gen_proto(int, int, int);
  260. static struct slist *xfer_to_x(struct arth *);
  261. static struct slist *xfer_to_a(struct arth *);
  262. static struct block *gen_mac_multicast(int);
  263. static struct block *gen_len(int, int);
  264. static struct block *gen_check_802_11_data_frame(void);
  265. static struct block *gen_ppi_dlt_check(void);
  266. static struct block *gen_msg_abbrev(int type);
  267. static void *
  268. newchunk(n)
  269. u_int n;
  270. {
  271. struct chunk *cp;
  272. int k;
  273. size_t size;
  274. #ifndef __NetBSD__
  275. /* XXX Round up to nearest long. */
  276. n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
  277. #else
  278. /* XXX Round up to structure boundary. */
  279. n = ALIGN(n);
  280. #endif
  281. cp = &chunks[cur_chunk];
  282. if (n > cp->n_left) {
  283. ++cp, k = ++cur_chunk;
  284. if (k >= NCHUNKS)
  285. bpf_error("out of memory");
  286. size = CHUNK0SIZE << k;
  287. cp->m = (void *)malloc(size);
  288. if (cp->m == NULL)
  289. bpf_error("out of memory");
  290. memset((char *)cp->m, 0, size);
  291. cp->n_left = size;
  292. if (n > size)
  293. bpf_error("out of memory");
  294. }
  295. cp->n_left -= n;
  296. return (void *)((char *)cp->m + cp->n_left);
  297. }
  298. static void
  299. freechunks()
  300. {
  301. int i;
  302. cur_chunk = 0;
  303. for (i = 0; i < NCHUNKS; ++i)
  304. if (chunks[i].m != NULL) {
  305. free(chunks[i].m);
  306. chunks[i].m = NULL;
  307. }
  308. }
  309. /*
  310. * A strdup whose allocations are freed after code generation is over.
  311. */
  312. char *
  313. sdup(s)
  314. register const char *s;
  315. {
  316. int n = strlen(s) + 1;
  317. char *cp = newchunk(n);
  318. strlcpy(cp, s, n);
  319. return (cp);
  320. }
  321. static inline struct block *
  322. new_block(code)
  323. int code;
  324. {
  325. struct block *p;
  326. p = (struct block *)newchunk(sizeof(*p));
  327. p->s.code = code;
  328. p->head = p;
  329. return p;
  330. }
  331. static inline struct slist *
  332. new_stmt(code)
  333. int code;
  334. {
  335. struct slist *p;
  336. p = (struct slist *)newchunk(sizeof(*p));
  337. p->s.code = code;
  338. return p;
  339. }
  340. static struct block *
  341. gen_retblk(v)
  342. int v;
  343. {
  344. struct block *b = new_block(BPF_RET|BPF_K);
  345. b->s.k = v;
  346. return b;
  347. }
  348. __dead static inline void
  349. syntax(void)
  350. {
  351. bpf_error("syntax error in filter expression");
  352. }
  353. static bpf_u_int32 netmask;
  354. static int snaplen;
  355. int no_optimize;
  356. #ifdef WIN32
  357. static int
  358. pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
  359. const char *buf, int optimize, bpf_u_int32 mask);
  360. int
  361. pcap_compile(pcap_t *p, struct bpf_program *program,
  362. const char *buf, int optimize, bpf_u_int32 mask)
  363. {
  364. int result;
  365. EnterCriticalSection(&g_PcapCompileCriticalSection);
  366. result = pcap_compile_unsafe(p, program, buf, optimize, mask);
  367. LeaveCriticalSection(&g_PcapCompileCriticalSection);
  368. return result;
  369. }
  370. static int
  371. pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
  372. const char *buf, int optimize, bpf_u_int32 mask)
  373. #else /* WIN32 */
  374. int
  375. pcap_compile(pcap_t *p, struct bpf_program *program,
  376. const char *buf, int optimize, bpf_u_int32 mask)
  377. #endif /* WIN32 */
  378. {
  379. extern int n_errors;
  380. const char * volatile xbuf = buf;
  381. u_int len;
  382. /*
  383. * If this pcap_t hasn't been activated, it doesn't have a
  384. * link-layer type, so we can't use it.
  385. */
  386. if (!p->activated) {
  387. snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  388. "not-yet-activated pcap_t passed to pcap_compile");
  389. return (-1);
  390. }
  391. no_optimize = 0;
  392. n_errors = 0;
  393. root = NULL;
  394. bpf_pcap = p;
  395. init_regs();
  396. if (setjmp(top_ctx)) {
  397. #ifdef INET6
  398. if (ai != NULL) {
  399. freeaddrinfo(ai);
  400. ai = NULL;
  401. }
  402. #endif
  403. lex_cleanup();
  404. freechunks();
  405. return (-1);
  406. }
  407. netmask = mask;
  408. snaplen = pcap_snapshot(p);
  409. if (snaplen == 0) {
  410. snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  411. "snaplen of 0 rejects all packets");
  412. return -1;
  413. }
  414. lex_init(xbuf ? xbuf : "");
  415. init_linktype(p);
  416. (void)pcap_parse();
  417. if (n_errors)
  418. syntax();
  419. if (root == NULL)
  420. root = gen_retblk(snaplen);
  421. if (optimize && !no_optimize) {
  422. bpf_optimize(&root);
  423. if (root == NULL ||
  424. (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
  425. bpf_error("expression rejects all packets");
  426. }
  427. program->bf_insns = icode_to_fcode(root, &len);
  428. program->bf_len = len;
  429. lex_cleanup();
  430. freechunks();
  431. return (0);
  432. }
  433. /*
  434. * entry point for using the compiler with no pcap open
  435. * pass in all the stuff that is needed explicitly instead.
  436. */
  437. int
  438. pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
  439. struct bpf_program *program,
  440. const char *buf, int optimize, bpf_u_int32 mask)
  441. {
  442. pcap_t *p;
  443. int ret;
  444. p = pcap_open_dead(linktype_arg, snaplen_arg);
  445. if (p == NULL)
  446. return (-1);
  447. ret = pcap_compile(p, program, buf, optimize, mask);
  448. pcap_close(p);
  449. return (ret);
  450. }
  451. /*
  452. * Clean up a "struct bpf_program" by freeing all the memory allocated
  453. * in it.
  454. */
  455. void
  456. pcap_freecode(struct bpf_program *program)
  457. {
  458. program->bf_len = 0;
  459. if (program->bf_insns != NULL) {
  460. free((char *)program->bf_insns);
  461. program->bf_insns = NULL;
  462. }
  463. }
  464. /*
  465. * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
  466. * which of the jt and jf fields has been resolved and which is a pointer
  467. * back to another unresolved block (or nil). At least one of the fields
  468. * in each block is already resolved.
  469. */
  470. static void
  471. backpatch(list, target)
  472. struct block *list, *target;
  473. {
  474. struct block *next;
  475. while (list) {
  476. if (!list->sense) {
  477. next = JT(list);
  478. JT(list) = target;
  479. } else {
  480. next = JF(list);
  481. JF(list) = target;
  482. }
  483. list = next;
  484. }
  485. }
  486. /*
  487. * Merge the lists in b0 and b1, using the 'sense' field to indicate
  488. * which of jt and jf is the link.
  489. */
  490. static void
  491. merge(b0, b1)
  492. struct block *b0, *b1;
  493. {
  494. register struct block **p = &b0;
  495. /* Find end of list. */
  496. while (*p)
  497. p = !((*p)->sense) ? &JT(*p) : &JF(*p);
  498. /* Concatenate the lists. */
  499. *p = b1;
  500. }
  501. void
  502. finish_parse(p)
  503. struct block *p;
  504. {
  505. struct block *ppi_dlt_check;
  506. /*
  507. * Insert before the statements of the first (root) block any
  508. * statements needed to load the lengths of any variable-length
  509. * headers into registers.
  510. *
  511. * XXX - a fancier strategy would be to insert those before the
  512. * statements of all blocks that use those lengths and that
  513. * have no predecessors that use them, so that we only compute
  514. * the lengths if we need them. There might be even better
  515. * approaches than that.
  516. *
  517. * However, those strategies would be more complicated, and
  518. * as we don't generate code to compute a length if the
  519. * program has no tests that use the length, and as most
  520. * tests will probably use those lengths, we would just
  521. * postpone computing the lengths so that it's not done
  522. * for tests that fail early, and it's not clear that's
  523. * worth the effort.
  524. */
  525. insert_compute_vloffsets(p->head);
  526. /*
  527. * For DLT_PPI captures, generate a check of the per-packet
  528. * DLT value to make sure it's DLT_IEEE802_11.
  529. */
  530. ppi_dlt_check = gen_ppi_dlt_check();
  531. if (ppi_dlt_check != NULL)
  532. gen_and(ppi_dlt_check, p);
  533. backpatch(p, gen_retblk(snaplen));
  534. p->sense = !p->sense;
  535. backpatch(p, gen_retblk(0));
  536. root = p->head;
  537. }
  538. void
  539. gen_and(b0, b1)
  540. struct block *b0, *b1;
  541. {
  542. backpatch(b0, b1->head);
  543. b0->sense = !b0->sense;
  544. b1->sense = !b1->sense;
  545. merge(b1, b0);
  546. b1->sense = !b1->sense;
  547. b1->head = b0->head;
  548. }
  549. void
  550. gen_or(b0, b1)
  551. struct block *b0, *b1;
  552. {
  553. b0->sense = !b0->sense;
  554. backpatch(b0, b1->head);
  555. b0->sense = !b0->sense;
  556. merge(b1, b0);
  557. b1->head = b0->head;
  558. }
  559. void
  560. gen_not(b)
  561. struct block *b;
  562. {
  563. b->sense = !b->sense;
  564. }
  565. static struct block *
  566. gen_cmp(offrel, offset, size, v)
  567. enum e_offrel offrel;
  568. u_int offset, size;
  569. bpf_int32 v;
  570. {
  571. return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
  572. }
  573. static struct block *
  574. gen_cmp_gt(offrel, offset, size, v)
  575. enum e_offrel offrel;
  576. u_int offset, size;
  577. bpf_int32 v;
  578. {
  579. return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
  580. }
  581. static struct block *
  582. gen_cmp_ge(offrel, offset, size, v)
  583. enum e_offrel offrel;
  584. u_int offset, size;
  585. bpf_int32 v;
  586. {
  587. return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
  588. }
  589. static struct block *
  590. gen_cmp_lt(offrel, offset, size, v)
  591. enum e_offrel offrel;
  592. u_int offset, size;
  593. bpf_int32 v;
  594. {
  595. return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
  596. }
  597. static struct block *
  598. gen_cmp_le(offrel, offset, size, v)
  599. enum e_offrel offrel;
  600. u_int offset, size;
  601. bpf_int32 v;
  602. {
  603. return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
  604. }
  605. static struct block *
  606. gen_mcmp(offrel, offset, size, v, mask)
  607. enum e_offrel offrel;
  608. u_int offset, size;
  609. bpf_int32 v;
  610. bpf_u_int32 mask;
  611. {
  612. return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
  613. }
  614. static struct block *
  615. gen_bcmp(offrel, offset, size, v)
  616. enum e_offrel offrel;
  617. register u_int offset, size;
  618. register const u_char *v;
  619. {
  620. register struct block *b, *tmp;
  621. b = NULL;
  622. while (size >= 4) {
  623. register const u_char *p = &v[size - 4];
  624. bpf_int32 w = ((bpf_int32)p[0] << 24) |
  625. ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
  626. tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
  627. if (b != NULL)
  628. gen_and(b, tmp);
  629. b = tmp;
  630. size -= 4;
  631. }
  632. while (size >= 2) {
  633. register const u_char *p = &v[size - 2];
  634. bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
  635. tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
  636. if (b != NULL)
  637. gen_and(b, tmp);
  638. b = tmp;
  639. size -= 2;
  640. }
  641. if (size > 0) {
  642. tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
  643. if (b != NULL)
  644. gen_and(b, tmp);
  645. b = tmp;
  646. }
  647. return b;
  648. }
  649. /*
  650. * AND the field of size "size" at offset "offset" relative to the header
  651. * specified by "offrel" with "mask", and compare it with the value "v"
  652. * with the test specified by "jtype"; if "reverse" is true, the test
  653. * should test the opposite of "jtype".
  654. */
  655. static struct block *
  656. gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
  657. enum e_offrel offrel;
  658. bpf_int32 v;
  659. bpf_u_int32 offset, size, mask, jtype;
  660. int reverse;
  661. {
  662. struct slist *s, *s2;
  663. struct block *b;
  664. s = gen_load_a(offrel, offset, size);
  665. if (mask != 0xffffffff) {
  666. s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
  667. s2->s.k = mask;
  668. sappend(s, s2);
  669. }
  670. b = new_block(JMP(jtype));
  671. b->stmts = s;
  672. b->s.k = v;
  673. if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
  674. gen_not(b);
  675. return b;
  676. }
  677. /*
  678. * Various code constructs need to know the layout of the data link
  679. * layer. These variables give the necessary offsets from the beginning
  680. * of the packet data.
  681. */
  682. /*
  683. * This is the offset of the beginning of the link-layer header from
  684. * the beginning of the raw packet data.
  685. *
  686. * It's usually 0, except for 802.11 with a fixed-length radio header.
  687. * (For 802.11 with a variable-length radio header, we have to generate
  688. * code to compute that offset; off_ll is 0 in that case.)
  689. */
  690. static u_int off_ll;
  691. /*
  692. * If there's a variable-length header preceding the link-layer header,
  693. * "reg_off_ll" is the register number for a register containing the
  694. * length of that header, and therefore the offset of the link-layer
  695. * header from the beginning of the raw packet data. Otherwise,
  696. * "reg_off_ll" is -1.
  697. */
  698. static int reg_off_ll;
  699. /*
  700. * This is the offset of the beginning of the MAC-layer header from
  701. * the beginning of the link-layer header.
  702. * It's usually 0, except for ATM LANE, where it's the offset, relative
  703. * to the beginning of the raw packet data, of the Ethernet header, and
  704. * for Ethernet with various additional information.
  705. */
  706. static u_int off_mac;
  707. /*
  708. * This is the offset of the beginning of the MAC-layer payload,
  709. * from the beginning of the raw packet data.
  710. *
  711. * I.e., it's the sum of the length of the link-layer header (without,
  712. * for example, any 802.2 LLC header, so it's the MAC-layer
  713. * portion of that header), plus any prefix preceding the
  714. * link-layer header.
  715. */
  716. static u_int off_macpl;
  717. /*
  718. * This is 1 if the offset of the beginning of the MAC-layer payload
  719. * from the beginning of the link-layer header is variable-length.
  720. */
  721. static int off_macpl_is_variable;
  722. /*
  723. * If the link layer has variable_length headers, "reg_off_macpl"
  724. * is the register number for a register containing the length of the
  725. * link-layer header plus the length of any variable-length header
  726. * preceding the link-layer header. Otherwise, "reg_off_macpl"
  727. * is -1.
  728. */
  729. static int reg_off_macpl;
  730. /*
  731. * "off_linktype" is the offset to information in the link-layer header
  732. * giving the packet type. This offset is relative to the beginning
  733. * of the link-layer header (i.e., it doesn't include off_ll).
  734. *
  735. * For Ethernet, it's the offset of the Ethernet type field.
  736. *
  737. * For link-layer types that always use 802.2 headers, it's the
  738. * offset of the LLC header.
  739. *
  740. * For PPP, it's the offset of the PPP type field.
  741. *
  742. * For Cisco HDLC, it's the offset of the CHDLC type field.
  743. *
  744. * For BSD loopback, it's the offset of the AF_ value.
  745. *
  746. * For Linux cooked sockets, it's the offset of the type field.
  747. *
  748. * It's set to -1 for no encapsulation, in which case, IP is assumed.
  749. */
  750. static u_int off_linktype;
  751. /*
  752. * TRUE if "pppoes" appeared in the filter; it causes link-layer type
  753. * checks to check the PPP header, assumed to follow a LAN-style link-
  754. * layer header and a PPPoE session header.
  755. */
  756. static int is_pppoes = 0;
  757. /*
  758. * TRUE if the link layer includes an ATM pseudo-header.
  759. */
  760. static int is_atm = 0;
  761. /*
  762. * TRUE if "lane" appeared in the filter; it causes us to generate
  763. * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
  764. */
  765. static int is_lane = 0;
  766. /*
  767. * These are offsets for the ATM pseudo-header.
  768. */
  769. static u_int off_vpi;
  770. static u_int off_vci;
  771. static u_int off_proto;
  772. /*
  773. * These are offsets for the MTP2 fields.
  774. */
  775. static u_int off_li;
  776. static u_int off_li_hsl;
  777. /*
  778. * These are offsets for the MTP3 fields.
  779. */
  780. static u_int off_sio;
  781. static u_int off_opc;
  782. static u_int off_dpc;
  783. static u_int off_sls;
  784. /*
  785. * This is the offset of the first byte after the ATM pseudo_header,
  786. * or -1 if there is no ATM pseudo-header.
  787. */
  788. static u_int off_payload;
  789. /*
  790. * These are offsets to the beginning of the network-layer header.
  791. * They are relative to the beginning of the MAC-layer payload (i.e.,
  792. * they don't include off_ll or off_macpl).
  793. *
  794. * If the link layer never uses 802.2 LLC:
  795. *
  796. * "off_nl" and "off_nl_nosnap" are the same.
  797. *
  798. * If the link layer always uses 802.2 LLC:
  799. *
  800. * "off_nl" is the offset if there's a SNAP header following
  801. * the 802.2 header;
  802. *
  803. * "off_nl_nosnap" is the offset if there's no SNAP header.
  804. *
  805. * If the link layer is Ethernet:
  806. *
  807. * "off_nl" is the offset if the packet is an Ethernet II packet
  808. * (we assume no 802.3+802.2+SNAP);
  809. *
  810. * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
  811. * with an 802.2 header following it.
  812. */
  813. static u_int off_nl;
  814. static u_int off_nl_nosnap;
  815. static int linktype;
  816. static void
  817. init_linktype(p)
  818. pcap_t *p;
  819. {
  820. linktype = pcap_datalink(p);
  821. pcap_fddipad = p->fddipad;
  822. /*
  823. * Assume it's not raw ATM with a pseudo-header, for now.
  824. */
  825. off_mac = 0;
  826. is_atm = 0;
  827. is_lane = 0;
  828. off_vpi = -1;
  829. off_vci = -1;
  830. off_proto = -1;
  831. off_payload = -1;
  832. /*
  833. * And that we're not doing PPPoE.
  834. */
  835. is_pppoes = 0;
  836. /*
  837. * And assume we're not doing SS7.
  838. */
  839. off_li = -1;
  840. off_li_hsl = -1;
  841. off_sio = -1;
  842. off_opc = -1;
  843. off_dpc = -1;
  844. off_sls = -1;
  845. /*
  846. * Also assume it's not 802.11.
  847. */
  848. off_ll = 0;
  849. off_macpl = 0;
  850. off_macpl_is_variable = 0;
  851. orig_linktype = -1;
  852. orig_nl = -1;
  853. label_stack_depth = 0;
  854. reg_off_ll = -1;
  855. reg_off_macpl = -1;
  856. switch (linktype) {
  857. case DLT_ARCNET:
  858. off_linktype = 2;
  859. off_macpl = 6;
  860. off_nl = 0; /* XXX in reality, variable! */
  861. off_nl_nosnap = 0; /* no 802.2 LLC */
  862. return;
  863. case DLT_ARCNET_LINUX:
  864. off_linktype = 4;
  865. off_macpl = 8;
  866. off_nl = 0; /* XXX in reality, variable! */
  867. off_nl_nosnap = 0; /* no 802.2 LLC */
  868. return;
  869. case DLT_EN10MB:
  870. off_linktype = 12;
  871. off_macpl = 14; /* Ethernet header length */
  872. off_nl = 0; /* Ethernet II */
  873. off_nl_nosnap = 3; /* 802.3+802.2 */
  874. return;
  875. case DLT_SLIP:
  876. /*
  877. * SLIP doesn't have a link level type. The 16 byte
  878. * header is hacked into our SLIP driver.
  879. */
  880. off_linktype = -1;
  881. off_macpl = 16;
  882. off_nl = 0;
  883. off_nl_nosnap = 0; /* no 802.2 LLC */
  884. return;
  885. case DLT_SLIP_BSDOS:
  886. /* XXX this may be the same as the DLT_PPP_BSDOS case */
  887. off_linktype = -1;
  888. /* XXX end */
  889. off_macpl = 24;
  890. off_nl = 0;
  891. off_nl_nosnap = 0; /* no 802.2 LLC */
  892. return;
  893. case DLT_NULL:
  894. case DLT_LOOP:
  895. off_linktype = 0;
  896. off_macpl = 4;
  897. off_nl = 0;
  898. off_nl_nosnap = 0; /* no 802.2 LLC */
  899. return;
  900. case DLT_ENC:
  901. off_linktype = 0;
  902. off_macpl = 12;
  903. off_nl = 0;
  904. off_nl_nosnap = 0; /* no 802.2 LLC */
  905. return;
  906. case DLT_PPP:
  907. case DLT_PPP_PPPD:
  908. case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
  909. case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
  910. off_linktype = 2;
  911. off_macpl = 4;
  912. off_nl = 0;
  913. off_nl_nosnap = 0; /* no 802.2 LLC */
  914. return;
  915. case DLT_PPP_ETHER:
  916. /*
  917. * This does no include the Ethernet header, and
  918. * only covers session state.
  919. */
  920. off_linktype = 6;
  921. off_macpl = 8;
  922. off_nl = 0;
  923. off_nl_nosnap = 0; /* no 802.2 LLC */
  924. return;
  925. case DLT_PPP_BSDOS:
  926. off_linktype = 5;
  927. off_macpl = 24;
  928. off_nl = 0;
  929. off_nl_nosnap = 0; /* no 802.2 LLC */
  930. return;
  931. case DLT_FDDI:
  932. /*
  933. * FDDI doesn't really have a link-level type field.
  934. * We set "off_linktype" to the offset of the LLC header.
  935. *
  936. * To check for Ethernet types, we assume that SSAP = SNAP
  937. * is being used and pick out the encapsulated Ethernet type.
  938. * XXX - should we generate code to check for SNAP?
  939. */
  940. off_linktype = 13;
  941. off_linktype += pcap_fddipad;
  942. off_macpl = 13; /* FDDI MAC header length */
  943. off_macpl += pcap_fddipad;
  944. off_nl = 8; /* 802.2+SNAP */
  945. off_nl_nosnap = 3; /* 802.2 */
  946. return;
  947. case DLT_IEEE802:
  948. /*
  949. * Token Ring doesn't really have a link-level type field.
  950. * We set "off_linktype" to the offset of the LLC header.
  951. *
  952. * To check for Ethernet types, we assume that SSAP = SNAP
  953. * is being used and pick out the encapsulated Ethernet type.
  954. * XXX - should we generate code to check for SNAP?
  955. *
  956. * XXX - the header is actually variable-length.
  957. * Some various Linux patched versions gave 38
  958. * as "off_linktype" and 40 as "off_nl"; however,
  959. * if a token ring packet has *no* routing
  960. * information, i.e. is not source-routed, the correct
  961. * values are 20 and 22, as they are in the vanilla code.
  962. *
  963. * A packet is source-routed iff the uppermost bit
  964. * of the first byte of the source address, at an
  965. * offset of 8, has the uppermost bit set. If the
  966. * packet is source-routed, the total number of bytes
  967. * of routing information is 2 plus bits 0x1F00 of
  968. * the 16-bit value at an offset of 14 (shifted right
  969. * 8 - figure out which byte that is).
  970. */
  971. off_linktype = 14;
  972. off_macpl = 14; /* Token Ring MAC header length */
  973. off_nl = 8; /* 802.2+SNAP */
  974. off_nl_nosnap = 3; /* 802.2 */
  975. return;
  976. case DLT_IEEE802_11:
  977. case DLT_PRISM_HEADER:
  978. case DLT_IEEE802_11_RADIO_AVS:
  979. case DLT_IEEE802_11_RADIO:
  980. /*
  981. * 802.11 doesn't really have a link-level type field.
  982. * We set "off_linktype" to the offset of the LLC header.
  983. *
  984. * To check for Ethernet types, we assume that SSAP = SNAP
  985. * is being used and pick out the encapsulated Ethernet type.
  986. * XXX - should we generate code to check for SNAP?
  987. *
  988. * We also handle variable-length radio headers here.
  989. * The Prism header is in theory variable-length, but in
  990. * practice it's always 144 bytes long. However, some
  991. * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
  992. * sometimes or always supply an AVS header, so we
  993. * have to check whether the radio header is a Prism
  994. * header or an AVS header, so, in practice, it's
  995. * variable-length.
  996. */
  997. off_linktype = 24;
  998. off_macpl = 0; /* link-layer header is variable-length */
  999. off_macpl_is_variable = 1;
  1000. off_nl = 8; /* 802.2+SNAP */
  1001. off_nl_nosnap = 3; /* 802.2 */
  1002. return;
  1003. case DLT_PPI:
  1004. /*
  1005. * At the moment we treat PPI the same way that we treat
  1006. * normal Radiotap encoded packets. The difference is in
  1007. * the function that generates the code at the beginning
  1008. * to compute the header length. Since this code generator
  1009. * of PPI supports bare 802.11 encapsulation only (i.e.
  1010. * the encapsulated DLT should be DLT_IEEE802_11) we
  1011. * generate code to check for this too.
  1012. */
  1013. off_linktype = 24;
  1014. off_macpl = 0; /* link-layer header is variable-length */
  1015. off_macpl_is_variable = 1;
  1016. off_nl = 8; /* 802.2+SNAP */
  1017. off_nl_nosnap = 3; /* 802.2 */
  1018. return;
  1019. case DLT_ATM_RFC1483:
  1020. case DLT_ATM_CLIP: /* Linux ATM defines this */
  1021. /*
  1022. * assume routed, non-ISO PDUs
  1023. * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
  1024. *
  1025. * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
  1026. * or PPP with the PPP NLPID (e.g., PPPoA)? The
  1027. * latter would presumably be treated the way PPPoE
  1028. * should be, so you can do "pppoe and udp port 2049"
  1029. * or "pppoa and tcp port 80" and have it check for
  1030. * PPPo{A,E} and a PPP protocol of IP and....
  1031. */
  1032. off_linktype = 0;
  1033. off_macpl = 0; /* packet begins with LLC header */
  1034. off_nl = 8; /* 802.2+SNAP */
  1035. off_nl_nosnap = 3; /* 802.2 */
  1036. return;
  1037. case DLT_SUNATM:
  1038. /*
  1039. * Full Frontal ATM; you get AALn PDUs with an ATM
  1040. * pseudo-header.
  1041. */
  1042. is_atm = 1;
  1043. off_vpi = SUNATM_VPI_POS;
  1044. off_vci = SUNATM_VCI_POS;
  1045. off_proto = PROTO_POS;
  1046. off_mac = -1; /* assume LLC-encapsulated, so no MAC-layer header */
  1047. off_payload = SUNATM_PKT_BEGIN_POS;
  1048. off_linktype = off_payload;
  1049. off_macpl = off_payload; /* if LLC-encapsulated */
  1050. off_nl = 8; /* 802.2+SNAP */
  1051. off_nl_nosnap = 3; /* 802.2 */
  1052. return;
  1053. case DLT_RAW:
  1054. case DLT_IPV4:
  1055. case DLT_IPV6:
  1056. off_linktype = -1;
  1057. off_macpl = 0;
  1058. off_nl = 0;
  1059. off_nl_nosnap = 0; /* no 802.2 LLC */
  1060. return;
  1061. case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
  1062. off_linktype = 14;
  1063. off_macpl = 16;
  1064. off_nl = 0;
  1065. off_nl_nosnap = 0; /* no 802.2 LLC */
  1066. return;
  1067. case DLT_LTALK:
  1068. /*
  1069. * LocalTalk does have a 1-byte type field in the LLAP header,
  1070. * but really it just indicates whether there is a "short" or
  1071. * "long" DDP packet following.
  1072. */
  1073. off_linktype = -1;
  1074. off_macpl = 0;
  1075. off_nl = 0;
  1076. off_nl_nosnap = 0; /* no 802.2 LLC */
  1077. return;
  1078. case DLT_IP_OVER_FC:
  1079. /*
  1080. * RFC 2625 IP-over-Fibre-Channel doesn't really have a
  1081. * link-level type field. We set "off_linktype" to the
  1082. * offset of the LLC header.
  1083. *
  1084. * To check for Ethernet types, we assume that SSAP = SNAP
  1085. * is being used and pick out the encapsulated Ethernet type.
  1086. * XXX - should we generate code to check for SNAP? RFC
  1087. * 2625 says SNAP should be used.
  1088. */
  1089. off_linktype = 16;
  1090. off_macpl = 16;
  1091. off_nl = 8; /* 802.2+SNAP */
  1092. off_nl_nosnap = 3; /* 802.2 */
  1093. return;
  1094. case DLT_FRELAY:
  1095. /*
  1096. * XXX - we should set this to handle SNAP-encapsulated
  1097. * frames (NLPID of 0x80).
  1098. */
  1099. off_linktype = -1;
  1100. off_macpl = 0;
  1101. off_nl = 0;
  1102. off_nl_nosnap = 0; /* no 802.2 LLC */
  1103. return;
  1104. /*
  1105. * the only BPF-interesting FRF.16 frames are non-control frames;
  1106. * Frame Relay has a variable length link-layer
  1107. * so lets start with offset 4 for now and increments later on (FIXME);
  1108. */
  1109. case DLT_MFR:
  1110. off_linktype = -1;
  1111. off_macpl = 0;
  1112. off_nl = 4;
  1113. off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
  1114. return;
  1115. case DLT_APPLE_IP_OVER_IEEE1394:
  1116. off_linktype = 16;
  1117. off_macpl = 18;
  1118. off_nl = 0;
  1119. off_nl_nosnap = 0; /* no 802.2 LLC */
  1120. return;
  1121. case DLT_SYMANTEC_FIREWALL:
  1122. off_linktype = 6;
  1123. off_macpl = 44;
  1124. off_nl = 0; /* Ethernet II */
  1125. off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
  1126. return;
  1127. #ifdef HAVE_NET_PFVAR_H
  1128. case DLT_PFLOG:
  1129. off_linktype = 0;
  1130. off_macpl = PFLOG_HDRLEN;
  1131. off_nl = 0;
  1132. off_nl_nosnap = 0; /* no 802.2 LLC */
  1133. return;
  1134. #endif
  1135. case DLT_JUNIPER_MFR:
  1136. case DLT_JUNIPER_MLFR:
  1137. case DLT_JUNIPER_MLPPP:
  1138. case DLT_JUNIPER_PPP:
  1139. case DLT_JUNIPER_CHDLC:
  1140. case DLT_JUNIPER_FRELAY:
  1141. off_linktype = 4;
  1142. off_macpl = 4;
  1143. off_nl = 0;
  1144. off_nl_nosnap = -1; /* no 802.2 LLC */
  1145. return;
  1146. case DLT_JUNIPER_ATM1:
  1147. off_linktype = 4; /* in reality variable between 4-8 */
  1148. off_macpl = 4; /* in reality variable between 4-8 */
  1149. off_nl = 0;
  1150. off_nl_nosnap = 10;
  1151. return;
  1152. case DLT_JUNIPER_ATM2:
  1153. off_linktype = 8; /* in reality variable between 8-12 */
  1154. off_macpl = 8; /* in reality variable between 8-12 */
  1155. off_nl = 0;
  1156. off_nl_nosnap = 10;
  1157. return;
  1158. /* frames captured on a Juniper PPPoE service PIC
  1159. * contain raw ethernet frames */
  1160. case DLT_JUNIPER_PPPOE:
  1161. case DLT_JUNIPER_ETHER:
  1162. off_macpl = 14;
  1163. off_linktype = 16;
  1164. off_nl = 18; /* Ethernet II */
  1165. off_nl_nosnap = 21; /* 802.3+802.2 */
  1166. return;
  1167. case DLT_JUNIPER_PPPOE_ATM:
  1168. off_linktype = 4;
  1169. off_macpl = 6;
  1170. off_nl = 0;
  1171. off_nl_nosnap = -1; /* no 802.2 LLC */
  1172. return;
  1173. case DLT_JUNIPER_GGSN:
  1174. off_linktype = 6;
  1175. off_macpl = 12;
  1176. off_nl = 0;
  1177. off_nl_nosnap = -1; /* no 802.2 LLC */
  1178. return;
  1179. case DLT_JUNIPER_ES:
  1180. off_linktype = 6;
  1181. off_macpl = -1; /* not really a network layer but raw IP addresses */
  1182. off_nl = -1; /* not really a network layer but raw IP addresses */
  1183. off_nl_nosnap = -1; /* no 802.2 LLC */
  1184. return;
  1185. case DLT_JUNIPER_MONITOR:
  1186. off_linktype = 12;
  1187. off_macpl = 12;
  1188. off_nl = 0; /* raw IP/IP6 header */
  1189. off_nl_nosnap = -1; /* no 802.2 LLC */
  1190. return;
  1191. case DLT_BACNET_MS_TP:
  1192. off_linktype = -1;
  1193. off_macpl = -1;
  1194. off_nl = -1;
  1195. off_nl_nosnap = -1;
  1196. return;
  1197. case DLT_JUNIPER_SERVICES:
  1198. off_linktype = 12;
  1199. off_macpl = -1; /* L3 proto location dep. on cookie type */
  1200. off_nl = -1; /* L3 proto location dep. on cookie type */
  1201. off_nl_nosnap = -1; /* no 802.2 LLC */
  1202. return;
  1203. case DLT_JUNIPER_VP:
  1204. off_linktype = 18;
  1205. off_macpl = -1;
  1206. off_nl = -1;
  1207. off_nl_nosnap = -1;
  1208. return;
  1209. case DLT_JUNIPER_ST:
  1210. off_linktype = 18;
  1211. off_macpl = -1;
  1212. off_nl = -1;
  1213. off_nl_nosnap = -1;
  1214. return;
  1215. case DLT_JUNIPER_ISM:
  1216. off_linktype = 8;
  1217. off_macpl = -1;
  1218. off_nl = -1;
  1219. off_nl_nosnap = -1;
  1220. return;
  1221. case DLT_JUNIPER_VS:
  1222. case DLT_JUNIPER_SRX_E2E:
  1223. case DLT_JUNIPER_FIBRECHANNEL:
  1224. case DLT_JUNIPER_ATM_CEMIC:
  1225. off_linktype = 8;
  1226. off_macpl = -1;
  1227. off_nl = -1;
  1228. off_nl_nosnap = -1;
  1229. return;
  1230. case DLT_MTP2:
  1231. off_li = 2;
  1232. off_li_hsl = 4;
  1233. off_sio = 3;
  1234. off_opc = 4;
  1235. off_dpc = 4;
  1236. off_sls = 7;
  1237. off_linktype = -1;
  1238. off_macpl = -1;
  1239. off_nl = -1;
  1240. off_nl_nosnap = -1;
  1241. return;
  1242. case DLT_MTP2_WITH_PHDR:
  1243. off_li = 6;
  1244. off_li_hsl = 8;
  1245. off_sio = 7;
  1246. off_opc = 8;
  1247. off_dpc = 8;
  1248. off_sls = 11;
  1249. off_linktype = -1;
  1250. off_macpl = -1;
  1251. off_nl = -1;
  1252. off_nl_nosnap = -1;
  1253. return;
  1254. case DLT_ERF:
  1255. off_li = 22;
  1256. off_li_hsl = 24;
  1257. off_sio = 23;
  1258. off_opc = 24;
  1259. off_dpc = 24;
  1260. off_sls = 27;
  1261. off_linktype = -1;
  1262. off_macpl = -1;
  1263. off_nl = -1;
  1264. off_nl_nosnap = -1;
  1265. return;
  1266. case DLT_PFSYNC:
  1267. off_linktype = -1;
  1268. off_macpl = 4;
  1269. off_nl = 0;
  1270. off_nl_nosnap = 0;
  1271. return;
  1272. case DLT_AX25_KISS:
  1273. /*
  1274. * Currently, only raw "link[N:M]" filtering is supported.
  1275. */
  1276. off_linktype = -1; /* variable, min 15, max 71 steps of 7 */
  1277. off_macpl = -1;
  1278. off_nl = -1; /* variable, min 16, max 71 steps of 7 */
  1279. off_nl_nosnap = -1; /* no 802.2 LLC */
  1280. off_mac = 1; /* step over the kiss length byte */
  1281. return;
  1282. case DLT_IPNET:
  1283. off_linktype = 1;
  1284. off_macpl = 24; /* ipnet header length */
  1285. off_nl = 0;
  1286. off_nl_nosnap = -1;
  1287. return;
  1288. case DLT_NETANALYZER:
  1289. off_mac = 4; /* MAC header is past 4-byte pseudo-header */
  1290. off_linktype = 16; /* includes 4-byte pseudo-header */
  1291. off_macpl = 18; /* pseudo-header+Ethernet header length */
  1292. off_nl = 0; /* Ethernet II */
  1293. off_nl_nosnap = 3; /* 802.3+802.2 */
  1294. return;
  1295. case DLT_NETANALYZER_TRANSPARENT:
  1296. off_mac = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
  1297. off_linktype = 24; /* includes 4-byte pseudo-header+preamble+SFD */
  1298. off_macpl = 26; /* pseudo-header+preamble+SFD+Ethernet header length */
  1299. off_nl = 0; /* Ethernet II */
  1300. off_nl_nosnap = 3; /* 802.3+802.2 */
  1301. return;
  1302. default:
  1303. /*
  1304. * For values in the range in which we've assigned new
  1305. * DLT_ values, only raw "link[N:M]" filtering is supported.
  1306. */
  1307. if (linktype >= DLT_MATCHING_MIN &&
  1308. linktype <= DLT_MATCHING_MAX) {
  1309. off_linktype = -1;
  1310. off_macpl = -1;
  1311. off_nl = -1;
  1312. off_nl_nosnap = -1;
  1313. return;
  1314. }
  1315. }
  1316. bpf_error("unknown data link type %d", linktype);
  1317. /* NOTREACHED */
  1318. }
  1319. /*
  1320. * Load a value relative to the beginning of the link-layer header.
  1321. * The link-layer header doesn't necessarily begin at the beginning
  1322. * of the packet data; there might be a variable-length prefix containing
  1323. * radio information.
  1324. */
  1325. static struct slist *
  1326. gen_load_llrel(offset, size)
  1327. u_int offset, size;
  1328. {
  1329. struct slist *s, *s2;
  1330. s = gen_llprefixlen();
  1331. /*
  1332. * If "s" is non-null, it has code to arrange that the X register
  1333. * contains the length of the prefix preceding the link-layer
  1334. * header.
  1335. *
  1336. * Otherwise, the length of the prefix preceding the link-layer
  1337. * header is "off_ll".
  1338. */
  1339. if (s != NULL) {
  1340. /*
  1341. * There's a variable-length prefix preceding the
  1342. * link-layer header. "s" points to a list of statements
  1343. * that put the length of that prefix into the X register.
  1344. * do an indirect load, to use the X register as an offset.
  1345. */
  1346. s2 = new_stmt(BPF_LD|BPF_IND|size);
  1347. s2->s.k = offset;
  1348. sappend(s, s2);
  1349. } else {
  1350. /*
  1351. * There is no variable-length header preceding the
  1352. * link-layer header; add in off_ll, which, if there's
  1353. * a fixed-length header preceding the link-layer header,
  1354. * is the length of that header.
  1355. */
  1356. s = new_stmt(BPF_LD|BPF_ABS|size);
  1357. s->s.k = offset + off_ll;
  1358. }
  1359. return s;
  1360. }
  1361. /*
  1362. * Load a value relative to the beginning of the MAC-layer payload.
  1363. */
  1364. static struct slist *
  1365. gen_load_macplrel(offset, size)
  1366. u_int offset, size;
  1367. {
  1368. struct slist *s, *s2;
  1369. s = gen_off_macpl();
  1370. /*
  1371. * If s is non-null, the offset of the MAC-layer payload is
  1372. * variable, and s points to a list of instructions that
  1373. * arrange that the X register contains that offset.
  1374. *
  1375. * Otherwise, the offset of the MAC-layer payload is constant,
  1376. * and is in off_macpl.
  1377. */
  1378. if (s != NULL) {
  1379. /*
  1380. * The offset of the MAC-layer payload is in the X
  1381. * register. Do an indirect load, to use the X register
  1382. * as an offset.
  1383. */
  1384. s2 = new_stmt(BPF_LD|BPF_IND|size);
  1385. s2->s.k = offset;
  1386. sappend(s, s2);
  1387. } else {
  1388. /*
  1389. * The offset of the MAC-layer payload is constant,
  1390. * and is in off_macpl; load the value at that offset
  1391. * plus the specified offset.
  1392. */
  1393. s = new_stmt(BPF_LD|BPF_ABS|size);
  1394. s->s.k = off_macpl + offset;
  1395. }
  1396. return s;
  1397. }
  1398. /*
  1399. * Load a value relative to the beginning of the specified header.
  1400. */
  1401. static struct slist *
  1402. gen_load_a(offrel, offset, size)
  1403. enum e_offrel offrel;
  1404. u_int offset, size;
  1405. {
  1406. struct slist *s, *s2;
  1407. switch (offrel) {
  1408. case OR_PACKET:
  1409. s = new_stmt(BPF_LD|BPF_ABS|size);
  1410. s->s.k = offset;
  1411. break;
  1412. case OR_LINK:
  1413. s = gen_load_llrel(offset, size);
  1414. break;
  1415. case OR_MACPL:
  1416. s = gen_load_macplrel(offset, size);
  1417. break;
  1418. case OR_NET:
  1419. s = gen_load_macplrel(off_nl + offset, size);
  1420. break;
  1421. case OR_NET_NOSNAP:
  1422. s = gen_load_macplrel(off_nl_nosnap + offset, size);
  1423. break;
  1424. case OR_TRAN_IPV4:
  1425. /*
  1426. * Load the X register with the length of the IPv4 header
  1427. * (plus the offset of the link-layer header, if it's
  1428. * preceded by a variable-length header such as a radio
  1429. * header), in bytes.
  1430. */
  1431. s = gen_loadx_iphdrlen();
  1432. /*
  1433. * Load the item at {offset of the MAC-layer payload} +
  1434. * {offset, relative to the start of the MAC-layer
  1435. * paylod, of the IPv4 header} + {length of the IPv4 header} +
  1436. * {specified offset}.
  1437. *
  1438. * (If the offset of the MAC-layer payload is variable,
  1439. * it's included in the value in the X register, and
  1440. * off_macpl is 0.)
  1441. */
  1442. s2 = new_stmt(BPF_LD|BPF_IND|size);
  1443. s2->s.k = off_macpl + off_nl + offset;
  1444. sappend(s, s2);
  1445. break;
  1446. case OR_TRAN_IPV6:
  1447. s = gen_load_macplrel(off_nl + 40 + offset, size);
  1448. break;
  1449. default:
  1450. abort();
  1451. return NULL;
  1452. }
  1453. return s;
  1454. }
  1455. /*
  1456. * Generate code to load into the X register the sum of the length of
  1457. * the IPv4 header and any variable-length header preceding the link-layer
  1458. * header.
  1459. */
  1460. static struct slist *
  1461. gen_loadx_iphdrlen()
  1462. {
  1463. struct slist *s, *s2;
  1464. s = gen_off_macpl();
  1465. if (s != NULL) {
  1466. /*
  1467. * There's a variable-length prefix preceding the
  1468. * link-layer header, or the link-layer header is itself
  1469. * variable-length. "s" points to a list of statements
  1470. * that put the offset of the MAC-layer payload into
  1471. * the X register.
  1472. *
  1473. * The 4*([k]&0xf) addressing mode can't be used, as we
  1474. * don't have a constant offset, so we have to load the
  1475. * value in question into the A register and add to it
  1476. * the value from the X register.
  1477. */
  1478. s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
  1479. s2->s.k = off_nl;
  1480. sappend(s, s2);
  1481. s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
  1482. s2->s.k = 0xf;
  1483. sappend(s, s2);
  1484. s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
  1485. s2->s.k = 2;
  1486. sappend(s, s2);
  1487. /*
  1488. * The A register now contains the length of the
  1489. * IP header. We need to add to it the offset of
  1490. * the MAC-layer payload, which is still in the X
  1491. * register, and move the result into the X register.
  1492. */
  1493. sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
  1494. sappend(s, new_stmt(BPF_MISC|BPF_TAX));
  1495. } else {
  1496. /*
  1497. * There is no variable-length header preceding the
  1498. * link-layer header, and the link-layer header is
  1499. * fixed-length; load the length of the IPv4 header,
  1500. * which is at an offset of off_nl from the beginning
  1501. * of the MAC-layer payload, and thus at an offset
  1502. * of off_mac_pl + off_nl from the beginning of the
  1503. * raw packet data.
  1504. */
  1505. s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
  1506. s->s.k = off_macpl + off_nl;
  1507. }
  1508. return s;
  1509. }
  1510. static struct block *
  1511. gen_uncond(rsense)
  1512. int rsense;
  1513. {
  1514. struct block *b;
  1515. struct slist *s;
  1516. s = new_stmt(BPF_LD|BPF_IMM);
  1517. s->s.k = !rsense;
  1518. b = new_block(JMP(BPF_JEQ));
  1519. b->stmts = s;
  1520. return b;
  1521. }
  1522. static inline struct block *
  1523. gen_true()
  1524. {
  1525. return gen_uncond(1);
  1526. }
  1527. static inline struct block *
  1528. gen_false()
  1529. {
  1530. return gen_uncond(0);
  1531. }
  1532. /*
  1533. * Byte-swap a 32-bit number.
  1534. * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
  1535. * big-endian platforms.)
  1536. */
  1537. #define SWAPLONG(y) \
  1538. ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
  1539. /*
  1540. * Generate code to match a particular packet type.
  1541. *
  1542. * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
  1543. * value, if <= ETHERMTU. We use that to determine whether to
  1544. * match the type/length field or to check the type/length field for
  1545. * a value <= ETHERMTU to see whether it's a type field and then do
  1546. * the appropriate test.
  1547. */
  1548. static struct block *
  1549. gen_ether_linktype(proto)
  1550. register int proto;
  1551. {
  1552. struct block *b0, *b1;
  1553. switch (proto) {
  1554. case LLCSAP_ISONS:
  1555. case LLCSAP_IP:
  1556. case LLCSAP_NETBEUI:
  1557. /*
  1558. * OSI protocols and NetBEUI always use 802.2 encapsulation,
  1559. * so we check the DSAP and SSAP.
  1560. *
  1561. * LLCSAP_IP checks for IP-over-802.2, rather
  1562. * than IP-over-Ethernet or IP-over-SNAP.
  1563. *
  1564. * XXX - should we check both the DSAP and the
  1565. * SSAP, like this, or should we check just the
  1566. * DSAP, as we do for other types <= ETHERMTU
  1567. * (i.e., other SAP values)?
  1568. */
  1569. b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
  1570. gen_not(b0);
  1571. b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
  1572. ((proto << 8) | proto));
  1573. gen_and(b0, b1);
  1574. return b1;
  1575. case LLCSAP_IPX:
  1576. /*
  1577. * Check for;
  1578. *
  1579. * Ethernet_II frames, which are Ethernet
  1580. * frames with a frame type of ETHERTYPE_IPX;
  1581. *
  1582. * Ethernet_802.3 frames, which are 802.3
  1583. * frames (i.e., the type/length field is
  1584. * a length field, <= ETHERMTU, rather than
  1585. * a type field) with the first two bytes
  1586. * after the Ethernet/802.3 header being
  1587. * 0xFFFF;
  1588. *
  1589. * Ethernet_802.2 frames, which are 802.3
  1590. * frames with an 802.2 LLC header and
  1591. * with the IPX LSAP as the DSAP in the LLC
  1592. * header;
  1593. *
  1594. * Ethernet_SNAP frames, which are 802.3
  1595. * frames with an LLC header and a SNAP
  1596. * header and with an OUI of 0x000000
  1597. * (encapsulated Ethernet) and a protocol
  1598. * ID of ETHERTYPE_IPX in the SNAP header.
  1599. *
  1600. * XXX - should we generate the same code both
  1601. * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
  1602. */
  1603. /*
  1604. * This generates code to check both for the
  1605. * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
  1606. */
  1607. b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
  1608. b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)0xFFFF);
  1609. gen_or(b0, b1);
  1610. /*
  1611. * Now we add code to check for SNAP frames with
  1612. * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
  1613. */
  1614. b0 = gen_snap(0x000000, ETHERTYPE_IPX);
  1615. gen_or(b0, b1);
  1616. /*
  1617. * Now we generate code to check for 802.3
  1618. * frames in general.
  1619. */
  1620. b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
  1621. gen_not(b0);
  1622. /*
  1623. * Now add the check for 802.3 frames before the
  1624. * check for Ethernet_802.2 and Ethernet_802.3,
  1625. * as those checks should only be done on 802.3
  1626. * frames, not on Ethernet frames.
  1627. */
  1628. gen_and(b0, b1);
  1629. /*
  1630. * Now add the check for Ethernet_II frames, and
  1631. * do that before checking for the other frame
  1632. * types.
  1633. */
  1634. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
  1635. (bpf_int32)ETHERTYPE_IPX);
  1636. gen_or(b0, b1);
  1637. return b1;
  1638. case ETHERTYPE_ATALK:
  1639. case ETHERTYPE_AARP:
  1640. /*
  1641. * EtherTalk (AppleTalk protocols on Ethernet link
  1642. * layer) may use 802.2 encapsulation.
  1643. */
  1644. /*
  1645. * Check for 802.2 encapsulation (EtherTalk phase 2?);
  1646. * we check for an Ethernet type field less than
  1647. * 1500, which means it's an 802.3 length field.
  1648. */
  1649. b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
  1650. gen_not(b0);
  1651. /*
  1652. * 802.2-encapsulated ETHERTYPE_ATALK packets are
  1653. * SNAP packets with an organization code of
  1654. * 0x080007 (Apple, for Appletalk) and a protocol
  1655. * type of ETHERTYPE_ATALK (Appletalk).
  1656. *
  1657. * 802.2-encapsulated ETHERTYPE_AARP packets are
  1658. * SNAP packets with an organization code of
  1659. * 0x000000 (encapsulated Ethernet) and a protocol
  1660. * type of ETHERTYPE_AARP (Appletalk ARP).
  1661. */
  1662. if (proto == ETHERTYPE_ATALK)
  1663. b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
  1664. else /* proto == ETHERTYPE_AARP */
  1665. b1 = gen_snap(0x000000, ETHERTYPE_AARP);
  1666. gen_and(b0, b1);
  1667. /*
  1668. * Check for Ethernet encapsulation (Ethertalk
  1669. * phase 1?); we just check for the Ethernet
  1670. * protocol type.
  1671. */
  1672. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
  1673. gen_or(b0, b1);
  1674. return b1;
  1675. default:
  1676. if (proto <= ETHERMTU) {
  1677. /*
  1678. * This is an LLC SAP value, so the frames
  1679. * that match would be 802.2 frames.
  1680. * Check that the frame is an 802.2 frame
  1681. * (i.e., that the length/type field is
  1682. * a length field, <= ETHERMTU) and
  1683. * then check the DSAP.
  1684. */
  1685. b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
  1686. gen_not(b0);
  1687. b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
  1688. (bpf_int32)proto);
  1689. gen_and(b0, b1);
  1690. return b1;
  1691. } else {
  1692. /*
  1693. * This is an Ethernet type, so compare
  1694. * the length/type field with it (if
  1695. * the frame is an 802.2 frame, the length
  1696. * field will be <= ETHERMTU, and, as
  1697. * "proto" is > ETHERMTU, this test
  1698. * will fail and the frame won't match,
  1699. * which is what we want).
  1700. */
  1701. return gen_cmp(OR_LINK, off_linktype, BPF_H,
  1702. (bpf_int32)proto);
  1703. }
  1704. }
  1705. }
  1706. /*
  1707. * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
  1708. * or IPv6 then we have an error.
  1709. */
  1710. static struct block *
  1711. gen_ipnet_linktype(proto)
  1712. register int proto;
  1713. {
  1714. switch (proto) {
  1715. case ETHERTYPE_IP:
  1716. return gen_cmp(OR_LINK, off_linktype, BPF_B,
  1717. (bpf_int32)IPH_AF_INET);
  1718. /* NOTREACHED */
  1719. case ETHERTYPE_IPV6:
  1720. return gen_cmp(OR_LINK, off_linktype, BPF_B,
  1721. (bpf_int32)IPH_AF_INET6);
  1722. /* NOTREACHED */
  1723. default:
  1724. break;
  1725. }
  1726. return gen_false();
  1727. }
  1728. /*
  1729. * Generate code to match a particular packet type.
  1730. *
  1731. * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
  1732. * value, if <= ETHERMTU. We use that to determine whether to
  1733. * match the type field or to check the type field for the special
  1734. * LINUX_SLL_P_802_2 value and then do the appropriate test.
  1735. */
  1736. static struct block *
  1737. gen_linux_sll_linktype(proto)
  1738. register int proto;
  1739. {
  1740. struct block *b0, *b1;
  1741. switch (proto) {
  1742. case LLCSAP_ISONS:
  1743. case LLCSAP_IP:
  1744. case LLCSAP_NETBEUI:
  1745. /*
  1746. * OSI protocols and NetBEUI always use 802.2 encapsulation,
  1747. * so we check the DSAP and SSAP.
  1748. *
  1749. * LLCSAP_IP checks for IP-over-802.2, rather
  1750. * than IP-over-Ethernet or IP-over-SNAP.
  1751. *
  1752. * XXX - should we check both the DSAP and the
  1753. * SSAP, like this, or should we check just the
  1754. * DSAP, as we do for other types <= ETHERMTU
  1755. * (i.e., other SAP values)?
  1756. */
  1757. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
  1758. b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
  1759. ((proto << 8) | proto));
  1760. gen_and(b0, b1);
  1761. return b1;
  1762. case LLCSAP_IPX:
  1763. /*
  1764. * Ethernet_II frames, which are Ethernet
  1765. * frames with a frame type of ETHERTYPE_IPX;
  1766. *
  1767. * Ethernet_802.3 frames, which have a frame
  1768. * type of LINUX_SLL_P_802_3;
  1769. *
  1770. * Ethernet_802.2 frames, which are 802.3
  1771. * frames with an 802.2 LLC header (i.e, have
  1772. * a frame type of LINUX_SLL_P_802_2) and
  1773. * with the IPX LSAP as the DSAP in the LLC
  1774. * header;
  1775. *
  1776. * Ethernet_SNAP frames, which are 802.3
  1777. * frames with an LLC header and a SNAP
  1778. * header and with an OUI of 0x000000
  1779. * (encapsulated Ethernet) and a protocol
  1780. * ID of ETHERTYPE_IPX in the SNAP header.
  1781. *
  1782. * First, do the checks on LINUX_SLL_P_802_2
  1783. * frames; generate the check for either
  1784. * Ethernet_802.2 or Ethernet_SNAP frames, and
  1785. * then put a check for LINUX_SLL_P_802_2 frames
  1786. * before it.
  1787. */
  1788. b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
  1789. b1 = gen_snap(0x000000, ETHERTYPE_IPX);
  1790. gen_or(b0, b1);
  1791. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
  1792. gen_and(b0, b1);
  1793. /*
  1794. * Now check for 802.3 frames and OR that with
  1795. * the previous test.
  1796. */
  1797. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
  1798. gen_or(b0, b1);
  1799. /*
  1800. * Now add the check for Ethernet_II frames, and
  1801. * do that before checking for the other frame
  1802. * types.
  1803. */
  1804. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
  1805. (bpf_int32)ETHERTYPE_IPX);
  1806. gen_or(b0, b1);
  1807. return b1;
  1808. case ETHERTYPE_ATALK:
  1809. case ETHERTYPE_AARP:
  1810. /*
  1811. * EtherTalk (AppleTalk protocols on Ethernet link
  1812. * layer) may use 802.2 encapsulation.
  1813. */
  1814. /*
  1815. * Check for 802.2 encapsulation (EtherTalk phase 2?);
  1816. * we check for the 802.2 protocol type in the
  1817. * "Ethernet type" field.
  1818. */
  1819. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
  1820. /*
  1821. * 802.2-encapsulated ETHERTYPE_ATALK packets are
  1822. * SNAP packets with an organization code of
  1823. * 0x080007 (Apple, for Appletalk) and a protocol
  1824. * type of ETHERTYPE_ATALK (Appletalk).
  1825. *
  1826. * 802.2-encapsulated ETHERTYPE_AARP packets are
  1827. * SNAP packets with an organization code of
  1828. * 0x000000 (encapsulated Ethernet) and a protocol
  1829. * type of ETHERTYPE_AARP (Appletalk ARP).
  1830. */
  1831. if (proto == ETHERTYPE_ATALK)
  1832. b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
  1833. else /* proto == ETHERTYPE_AARP */
  1834. b1 = gen_snap(0x000000, ETHERTYPE_AARP);
  1835. gen_and(b0, b1);
  1836. /*
  1837. * Check for Ethernet encapsulation (Ethertalk
  1838. * phase 1?); we just check for the Ethernet
  1839. * protocol type.
  1840. */
  1841. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
  1842. gen_or(b0, b1);
  1843. return b1;
  1844. default:
  1845. if (proto <= ETHERMTU) {
  1846. /*
  1847. * This is an LLC SAP value, so the frames
  1848. * that match would be 802.2 frames.
  1849. * Check for the 802.2 protocol type
  1850. * in the "Ethernet type" field, and
  1851. * then check the DSAP.
  1852. */
  1853. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
  1854. LINUX_SLL_P_802_2);
  1855. b1 = gen_cmp(OR_LINK, off_macpl, BPF_B,
  1856. (bpf_int32)proto);
  1857. gen_and(b0, b1);
  1858. return b1;
  1859. } else {
  1860. /*
  1861. * This is an Ethernet type, so compare
  1862. * the length/type field with it (if
  1863. * the frame is an 802.2 frame, the length
  1864. * field will be <= ETHERMTU, and, as
  1865. * "proto" is > ETHERMTU, this test
  1866. * will fail and the frame won't match,
  1867. * which is what we want).
  1868. */
  1869. return gen_cmp(OR_LINK, off_linktype, BPF_H,
  1870. (bpf_int32)proto);
  1871. }
  1872. }
  1873. }
  1874. static struct slist *
  1875. gen_load_prism_llprefixlen()
  1876. {
  1877. struct slist *s1, *s2;
  1878. struct slist *sjeq_avs_cookie;
  1879. struct slist *sjcommon;
  1880. /*
  1881. * This code is not compatible with the optimizer, as
  1882. * we are generating jmp instructions within a normal
  1883. * slist of instructions
  1884. */
  1885. no_optimize = 1;
  1886. /*
  1887. * Generate code to load the length of the radio header into
  1888. * the register assigned to hold that length, if one has been
  1889. * assigned. (If one hasn't been assigned, no code we've
  1890. * generated uses that prefix, so we don't need to generate any
  1891. * code to load it.)
  1892. *
  1893. * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
  1894. * or always use the AVS header rather than the Prism header.
  1895. * We load a 4-byte big-endian value at the beginning of the
  1896. * raw packet data, and see whether, when masked with 0xFFFFF000,
  1897. * it's equal to 0x80211000. If so, that indicates that it's
  1898. * an AVS header (the masked-out bits are the version number).
  1899. * Otherwise, it's a Prism header.
  1900. *
  1901. * XXX - the Prism header is also, in theory, variable-length,
  1902. * but no known software generates headers that aren't 144
  1903. * bytes long.
  1904. */
  1905. if (reg_off_ll != -1) {
  1906. /*
  1907. * Load the cookie.
  1908. */
  1909. s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
  1910. s1->s.k = 0;
  1911. /*
  1912. * AND it with 0xFFFFF000.
  1913. */
  1914. s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
  1915. s2->s.k = 0xFFFFF000;
  1916. sappend(s1, s2);
  1917. /*
  1918. * Compare with 0x80211000.
  1919. */
  1920. sjeq_avs_cookie = new_stmt(JMP(BPF_JEQ));
  1921. sjeq_avs_cookie->s.k = 0x80211000;
  1922. sappend(s1, sjeq_avs_cookie);
  1923. /*
  1924. * If it's AVS:
  1925. *
  1926. * The 4 bytes at an offset of 4 from the beginning of
  1927. * the AVS header are the length of the AVS header.
  1928. * That field is big-endian.
  1929. */
  1930. s2 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
  1931. s2->s.k = 4;
  1932. sappend(s1, s2);
  1933. sjeq_avs_cookie->s.jt = s2;
  1934. /*
  1935. * Now jump to the code to allocate a register
  1936. * into which to save the header length and
  1937. * store the length there. (The "jump always"
  1938. * instruction needs to have the k field set;
  1939. * it's added to the PC, so, as we're jumping
  1940. * over a single instruction, it should be 1.)
  1941. */
  1942. sjcommon = new_stmt(JMP(BPF_JA));
  1943. sjcommon->s.k = 1;
  1944. sappend(s1, sjcommon);
  1945. /*
  1946. * Now for the code that handles the Prism header.
  1947. * Just load the length of the Prism header (144)
  1948. * into the A register. Have the test for an AVS
  1949. * header branch here if we don't have an AVS header.
  1950. */
  1951. s2 = new_stmt(BPF_LD|BPF_W|BPF_IMM);
  1952. s2->s.k = 144;
  1953. sappend(s1, s2);
  1954. sjeq_avs_cookie->s.jf = s2;
  1955. /*
  1956. * Now allocate a register to hold that value and store
  1957. * it. The code for the AVS header will jump here after
  1958. * loading the length of the AVS header.
  1959. */
  1960. s2 = new_stmt(BPF_ST);
  1961. s2->s.k = reg_off_ll;
  1962. sappend(s1, s2);
  1963. sjcommon->s.jf = s2;
  1964. /*
  1965. * Now move it into the X register.
  1966. */
  1967. s2 = new_stmt(BPF_MISC|BPF_TAX);
  1968. sappend(s1, s2);
  1969. return (s1);
  1970. } else
  1971. return (NULL);
  1972. }
  1973. static struct slist *
  1974. gen_load_avs_llprefixlen()
  1975. {
  1976. struct slist *s1, *s2;
  1977. /*
  1978. * Generate code to load the length of the AVS header into
  1979. * the register assigned to hold that length, if one has been
  1980. * assigned. (If one hasn't been assigned, no code we've
  1981. * generated uses that prefix, so we don't need to generate any
  1982. * code to load it.)
  1983. */
  1984. if (reg_off_ll != -1) {
  1985. /*
  1986. * The 4 bytes at an offset of 4 from the beginning of
  1987. * the AVS header are the length of the AVS header.
  1988. * That field is big-endian.
  1989. */
  1990. s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
  1991. s1->s.k = 4;
  1992. /*
  1993. * Now allocate a register to hold that value and store
  1994. * it.
  1995. */
  1996. s2 = new_stmt(BPF_ST);
  1997. s2->s.k = reg_off_ll;
  1998. sappend(s1, s2);
  1999. /*
  2000. * Now move it into the X register.
  2001. */
  2002. s2 = new_stmt(BPF_MISC|BPF_TAX);
  2003. sappend(s1, s2);
  2004. return (s1);
  2005. } else
  2006. return (NULL);
  2007. }
  2008. static struct slist *
  2009. gen_load_radiotap_llprefixlen()
  2010. {
  2011. struct slist *s1, *s2;
  2012. /*
  2013. * Generate code to load the length of the radiotap header into
  2014. * the register assigned to hold that length, if one has been
  2015. * assigned. (If one hasn't been assigned, no code we've
  2016. * generated uses that prefix, so we don't need to generate any
  2017. * code to load it.)
  2018. */
  2019. if (reg_off_ll != -1) {
  2020. /*
  2021. * The 2 bytes at offsets of 2 and 3 from the beginning
  2022. * of the radiotap header are the length of the radiotap
  2023. * header; unfortunately, it's little-endian, so we have
  2024. * to load it a byte at a time and construct the value.
  2025. */
  2026. /*
  2027. * Load the high-order byte, at an offset of 3, shift it
  2028. * left a byte, and put the result in the X register.
  2029. */
  2030. s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
  2031. s1->s.k = 3;
  2032. s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
  2033. sappend(s1, s2);
  2034. s2->s.k = 8;
  2035. s2 = new_stmt(BPF_MISC|BPF_TAX);
  2036. sappend(s1, s2);
  2037. /*
  2038. * Load the next byte, at an offset of 2, and OR the
  2039. * value from the X register into it.
  2040. */
  2041. s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
  2042. sappend(s1, s2);
  2043. s2->s.k = 2;
  2044. s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
  2045. sappend(s1, s2);
  2046. /*
  2047. * Now allocate a register to hold that value and store
  2048. * it.
  2049. */
  2050. s2 = new_stmt(BPF_ST);
  2051. s2->s.k = reg_off_ll;
  2052. sappend(s1, s2);
  2053. /*
  2054. * Now move it into the X register.
  2055. */
  2056. s2 = new_stmt(BPF_MISC|BPF_TAX);
  2057. sappend(s1, s2);
  2058. return (s1);
  2059. } else
  2060. return (NULL);
  2061. }
  2062. /*
  2063. * At the moment we treat PPI as normal Radiotap encoded
  2064. * packets. The difference is in the function that generates
  2065. * the code at the beginning to compute the header length.
  2066. * Since this code generator of PPI supports bare 802.11
  2067. * encapsulation only (i.e. the encapsulated DLT should be
  2068. * DLT_IEEE802_11) we generate code to check for this too;
  2069. * that's done in finish_parse().
  2070. */
  2071. static struct slist *
  2072. gen_load_ppi_llprefixlen()
  2073. {
  2074. struct slist *s1, *s2;
  2075. /*
  2076. * Generate code to load the length of the radiotap header
  2077. * into the register assigned to hold that length, if one has
  2078. * been assigned.
  2079. */
  2080. if (reg_off_ll != -1) {
  2081. /*
  2082. * The 2 bytes at offsets of 2 and 3 from the beginning
  2083. * of the radiotap header are the length of the radiotap
  2084. * header; unfortunately, it's little-endian, so we have
  2085. * to load it a byte at a time and construct the value.
  2086. */
  2087. /*
  2088. * Load the high-order byte, at an offset of 3, shift it
  2089. * left a byte, and put the result in the X register.
  2090. */
  2091. s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
  2092. s1->s.k = 3;
  2093. s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
  2094. sappend(s1, s2);
  2095. s2->s.k = 8;
  2096. s2 = new_stmt(BPF_MISC|BPF_TAX);
  2097. sappend(s1, s2);
  2098. /*
  2099. * Load the next byte, at an offset of 2, and OR the
  2100. * value from the X register into it.
  2101. */
  2102. s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
  2103. sappend(s1, s2);
  2104. s2->s.k = 2;
  2105. s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
  2106. sappend(s1, s2);
  2107. /*
  2108. * Now allocate a register to hold that value and store
  2109. * it.
  2110. */
  2111. s2 = new_stmt(BPF_ST);
  2112. s2->s.k = reg_off_ll;
  2113. sappend(s1, s2);
  2114. /*
  2115. * Now move it into the X register.
  2116. */
  2117. s2 = new_stmt(BPF_MISC|BPF_TAX);
  2118. sappend(s1, s2);
  2119. return (s1);
  2120. } else
  2121. return (NULL);
  2122. }
  2123. /*
  2124. * Load a value relative to the beginning of the link-layer header after the 802.11
  2125. * header, i.e. LLC_SNAP.
  2126. * The link-layer header doesn't necessarily begin at the beginning
  2127. * of the packet data; there might be a variable-length prefix containing
  2128. * radio information.
  2129. */
  2130. static struct slist *
  2131. gen_load_802_11_header_len(struct slist *s, struct slist *snext)
  2132. {
  2133. struct slist *s2;
  2134. struct slist *sjset_data_frame_1;
  2135. struct slist *sjset_data_frame_2;
  2136. struct slist *sjset_qos;
  2137. struct slist *sjset_radiotap_flags;
  2138. struct slist *sjset_radiotap_tsft;
  2139. struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
  2140. struct slist *s_roundup;
  2141. if (reg_off_macpl == -1) {
  2142. /*
  2143. * No register has been assigned to the offset of
  2144. * the MAC-layer payload, which means nobody needs
  2145. * it; don't bother computing it - just return
  2146. * what we already have.
  2147. */
  2148. return (s);
  2149. }
  2150. /*
  2151. * This code is not compatible with the optimizer, as
  2152. * we are generating jmp instructions within a normal
  2153. * slist of instructions
  2154. */
  2155. no_optimize = 1;
  2156. /*
  2157. * If "s" is non-null, it has code to arrange that the X register
  2158. * contains the length of the prefix preceding the link-layer
  2159. * header.
  2160. *
  2161. * Otherwise, the length of the prefix preceding the link-layer
  2162. * header is "off_ll".
  2163. */
  2164. if (s == NULL) {
  2165. /*
  2166. * There is no variable-length header preceding the
  2167. * link-layer header.
  2168. *
  2169. * Load the length of the fixed-length prefix preceding
  2170. * the link-layer header (if any) into the X register,
  2171. * and store it in the reg_off_macpl register.
  2172. * That length is off_ll.
  2173. */
  2174. s = new_stmt(BPF_LDX|BPF_IMM);
  2175. s->s.k = off_ll;
  2176. }
  2177. /*
  2178. * The X register contains the offset of the beginning of the
  2179. * link-layer header; add 24, which is the minimum length
  2180. * of the MAC header for a data frame, to that, and store it
  2181. * in reg_off_macpl, and then load the Frame Control field,
  2182. * which is at the offset in the X register, with an indexed load.
  2183. */
  2184. s2 = new_stmt(BPF_MISC|BPF_TXA);
  2185. sappend(s, s2);
  2186. s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
  2187. s2->s.k = 24;
  2188. sappend(s, s2);
  2189. s2 = new_stmt(BPF_ST);
  2190. s2->s.k = reg_off_macpl;
  2191. sappend(s, s2);
  2192. s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
  2193. s2->s.k = 0;
  2194. sappend(s, s2);
  2195. /*
  2196. * Check the Frame Control field to see if this is a data frame;
  2197. * a data frame has the 0x08 bit (b3) in that field set and the
  2198. * 0x04 bit (b2) clear.
  2199. */
  2200. sjset_data_frame_1 = new_stmt(JMP(BPF_JSET));
  2201. sjset_data_frame_1->s.k = 0x08;
  2202. sappend(s, sjset_data_frame_1);
  2203. /*
  2204. * If b3 is set, test b2, otherwise go to the first statement of
  2205. * the rest of the program.
  2206. */
  2207. sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(JMP(BPF_JSET));
  2208. sjset_data_frame_2->s.k = 0x04;
  2209. sappend(s, sjset_data_frame_2);
  2210. sjset_data_frame_1->s.jf = snext;
  2211. /*
  2212. * If b2 is not set, this is a data frame; test the QoS bit.
  2213. * Otherwise, go to the first statement of the rest of the
  2214. * program.
  2215. */
  2216. sjset_data_frame_2->s.jt = snext;
  2217. sjset_data_frame_2->s.jf = sjset_qos = new_stmt(JMP(BPF_JSET));
  2218. sjset_qos->s.k = 0x80; /* QoS bit */
  2219. sappend(s, sjset_qos);
  2220. /*
  2221. * If it's set, add 2 to reg_off_macpl, to skip the QoS
  2222. * field.
  2223. * Otherwise, go to the first statement of the rest of the
  2224. * program.
  2225. */
  2226. sjset_qos->s.jt = s2 = new_stmt(BPF_LD|BPF_MEM);
  2227. s2->s.k = reg_off_macpl;
  2228. sappend(s, s2);
  2229. s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
  2230. s2->s.k = 2;
  2231. sappend(s, s2);
  2232. s2 = new_stmt(BPF_ST);
  2233. s2->s.k = reg_off_macpl;
  2234. sappend(s, s2);
  2235. /*
  2236. * If we have a radiotap header, look at it to see whether
  2237. * there's Atheros padding between the MAC-layer header
  2238. * and the payload.
  2239. *
  2240. * Note: all of the fields in the radiotap header are
  2241. * little-endian, so we byte-swap all of the values
  2242. * we test against, as they will be loaded as big-endian
  2243. * values.
  2244. */
  2245. if (linktype == DLT_IEEE802_11_RADIO) {
  2246. /*
  2247. * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
  2248. * in the presence flag?
  2249. */
  2250. sjset_qos->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_W);
  2251. s2->s.k = 4;
  2252. sappend(s, s2);
  2253. sjset_radiotap_flags = new_stmt(JMP(BPF_JSET));
  2254. sjset_radiotap_flags->s.k = SWAPLONG(0x00000002);
  2255. sappend(s, sjset_radiotap_flags);
  2256. /*
  2257. * If not, skip all of this.
  2258. */
  2259. sjset_radiotap_flags->s.jf = snext;
  2260. /*
  2261. * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
  2262. */
  2263. sjset_radiotap_tsft = sjset_radiotap_flags->s.jt =
  2264. new_stmt(JMP(BPF_JSET));
  2265. sjset_radiotap_tsft->s.k = SWAPLONG(0x00000001);
  2266. sappend(s, sjset_radiotap_tsft);
  2267. /*
  2268. * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
  2269. * at an offset of 16 from the beginning of the raw packet
  2270. * data (8 bytes for the radiotap header and 8 bytes for
  2271. * the TSFT field).
  2272. *
  2273. * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
  2274. * is set.
  2275. */
  2276. sjset_radiotap_tsft->s.jt = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
  2277. s2->s.k = 16;
  2278. sappend(s, s2);
  2279. sjset_tsft_datapad = new_stmt(JMP(BPF_JSET));
  2280. sjset_tsft_datapad->s.k = 0x20;
  2281. sappend(s, sjset_tsft_datapad);
  2282. /*
  2283. * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
  2284. * at an offset of 8 from the beginning of the raw packet
  2285. * data (8 bytes for the radiotap header).
  2286. *
  2287. * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
  2288. * is set.
  2289. */
  2290. sjset_radiotap_tsft->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
  2291. s2->s.k = 8;
  2292. sappend(s, s2);
  2293. sjset_notsft_datapad = new_stmt(JMP(BPF_JSET));
  2294. sjset_notsft_datapad->s.k = 0x20;
  2295. sappend(s, sjset_notsft_datapad);
  2296. /*
  2297. * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
  2298. * set, round the length of the 802.11 header to
  2299. * a multiple of 4. Do that by adding 3 and then
  2300. * dividing by and multiplying by 4, which we do by
  2301. * ANDing with ~3.
  2302. */
  2303. s_roundup = new_stmt(BPF_LD|BPF_MEM);
  2304. s_roundup->s.k = reg_off_macpl;
  2305. sappend(s, s_roundup);
  2306. s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
  2307. s2->s.k = 3;
  2308. sappend(s, s2);
  2309. s2 = new_stmt(BPF_ALU|BPF_AND|BPF_IMM);
  2310. s2->s.k = ~3;
  2311. sappend(s, s2);
  2312. s2 = new_stmt(BPF_ST);
  2313. s2->s.k = reg_off_macpl;
  2314. sappend(s, s2);
  2315. sjset_tsft_datapad->s.jt = s_roundup;
  2316. sjset_tsft_datapad->s.jf = snext;
  2317. sjset_notsft_datapad->s.jt = s_roundup;
  2318. sjset_notsft_datapad->s.jf = snext;
  2319. } else
  2320. sjset_qos->s.jf = snext;
  2321. return s;
  2322. }
  2323. static void
  2324. insert_compute_vloffsets(b)
  2325. struct block *b;
  2326. {
  2327. struct slist *s;
  2328. /*
  2329. * For link-layer types that have a variable-length header
  2330. * preceding the link-layer header, generate code to load
  2331. * the offset of the link-layer header into the register
  2332. * assigned to that offset, if any.
  2333. */
  2334. switch (linktype) {
  2335. case DLT_PRISM_HEADER:
  2336. s = gen_load_prism_llprefixlen();
  2337. break;
  2338. case DLT_IEEE802_11_RADIO_AVS:
  2339. s = gen_load_avs_llprefixlen();
  2340. break;
  2341. case DLT_IEEE802_11_RADIO:
  2342. s = gen_load_radiotap_llprefixlen();
  2343. break;
  2344. case DLT_PPI:
  2345. s = gen_load_ppi_llprefixlen();
  2346. break;
  2347. default:
  2348. s = NULL;
  2349. break;
  2350. }
  2351. /*
  2352. * For link-layer types that have a variable-length link-layer
  2353. * header, generate code to load the offset of the MAC-layer
  2354. * payload into the register assigned to that offset, if any.
  2355. */
  2356. switch (linktype) {
  2357. case DLT_IEEE802_11:
  2358. case DLT_PRISM_HEADER:
  2359. case DLT_IEEE802_11_RADIO_AVS:
  2360. case DLT_IEEE802_11_RADIO:
  2361. case DLT_PPI:
  2362. s = gen_load_802_11_header_len(s, b->stmts);
  2363. break;
  2364. }
  2365. /*
  2366. * If we have any offset-loading code, append all the
  2367. * existing statements in the block to those statements,
  2368. * and make the resulting list the list of statements
  2369. * for the block.
  2370. */
  2371. if (s != NULL) {
  2372. sappend(s, b->stmts);
  2373. b->stmts = s;
  2374. }
  2375. }
  2376. static struct block *
  2377. gen_ppi_dlt_check(void)
  2378. {
  2379. struct slist *s_load_dlt;
  2380. struct block *b;
  2381. if (linktype == DLT_PPI)
  2382. {
  2383. /* Create the statements that check for the DLT
  2384. */
  2385. s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
  2386. s_load_dlt->s.k = 4;
  2387. b = new_block(JMP(BPF_JEQ));
  2388. b->stmts = s_load_dlt;
  2389. b->s.k = SWAPLONG(DLT_IEEE802_11);
  2390. }
  2391. else
  2392. {
  2393. b = NULL;
  2394. }
  2395. return b;
  2396. }
  2397. static struct slist *
  2398. gen_prism_llprefixlen(void)
  2399. {
  2400. struct slist *s;
  2401. if (reg_off_ll == -1) {
  2402. /*
  2403. * We haven't yet assigned a register for the length
  2404. * of the radio header; allocate one.
  2405. */
  2406. reg_off_ll = alloc_reg();
  2407. }
  2408. /*
  2409. * Load the register containing the radio length
  2410. * into the X register.
  2411. */
  2412. s = new_stmt(BPF_LDX|BPF_MEM);
  2413. s->s.k = reg_off_ll;
  2414. return s;
  2415. }
  2416. static struct slist *
  2417. gen_avs_llprefixlen(void)
  2418. {
  2419. struct slist *s;
  2420. if (reg_off_ll == -1) {
  2421. /*
  2422. * We haven't yet assigned a register for the length
  2423. * of the AVS header; allocate one.
  2424. */
  2425. reg_off_ll = alloc_reg();
  2426. }
  2427. /*
  2428. * Load the register containing the AVS length
  2429. * into the X register.
  2430. */
  2431. s = new_stmt(BPF_LDX|BPF_MEM);
  2432. s->s.k = reg_off_ll;
  2433. return s;
  2434. }
  2435. static struct slist *
  2436. gen_radiotap_llprefixlen(void)
  2437. {
  2438. struct slist *s;
  2439. if (reg_off_ll == -1) {
  2440. /*
  2441. * We haven't yet assigned a register for the length
  2442. * of the radiotap header; allocate one.
  2443. */
  2444. reg_off_ll = alloc_reg();
  2445. }
  2446. /*
  2447. * Load the register containing the radiotap length
  2448. * into the X register.
  2449. */
  2450. s = new_stmt(BPF_LDX|BPF_MEM);
  2451. s->s.k = reg_off_ll;
  2452. return s;
  2453. }
  2454. /*
  2455. * At the moment we treat PPI as normal Radiotap encoded
  2456. * packets. The difference is in the function that generates
  2457. * the code at the beginning to compute the header length.
  2458. * Since this code generator of PPI supports bare 802.11
  2459. * encapsulation only (i.e. the encapsulated DLT should be
  2460. * DLT_IEEE802_11) we generate code to check for this too.
  2461. */
  2462. static struct slist *
  2463. gen_ppi_llprefixlen(void)
  2464. {
  2465. struct slist *s;
  2466. if (reg_off_ll == -1) {
  2467. /*
  2468. * We haven't yet assigned a register for the length
  2469. * of the radiotap header; allocate one.
  2470. */
  2471. reg_off_ll = alloc_reg();
  2472. }
  2473. /*
  2474. * Load the register containing the PPI length
  2475. * into the X register.
  2476. */
  2477. s = new_stmt(BPF_LDX|BPF_MEM);
  2478. s->s.k = reg_off_ll;
  2479. return s;
  2480. }
  2481. /*
  2482. * Generate code to compute the link-layer header length, if necessary,
  2483. * putting it into the X register, and to return either a pointer to a
  2484. * "struct slist" for the list of statements in that code, or NULL if
  2485. * no code is necessary.
  2486. */
  2487. static struct slist *
  2488. gen_llprefixlen(void)
  2489. {
  2490. switch (linktype) {
  2491. case DLT_PRISM_HEADER:
  2492. return gen_prism_llprefixlen();
  2493. case DLT_IEEE802_11_RADIO_AVS:
  2494. return gen_avs_llprefixlen();
  2495. case DLT_IEEE802_11_RADIO:
  2496. return gen_radiotap_llprefixlen();
  2497. case DLT_PPI:
  2498. return gen_ppi_llprefixlen();
  2499. default:
  2500. return NULL;
  2501. }
  2502. }
  2503. /*
  2504. * Generate code to load the register containing the offset of the
  2505. * MAC-layer payload into the X register; if no register for that offset
  2506. * has been allocated, allocate it first.
  2507. */
  2508. static struct slist *
  2509. gen_off_macpl(void)
  2510. {
  2511. struct slist *s;
  2512. if (off_macpl_is_variable) {
  2513. if (reg_off_macpl == -1) {
  2514. /*
  2515. * We haven't yet assigned a register for the offset
  2516. * of the MAC-layer payload; allocate one.
  2517. */
  2518. reg_off_macpl = alloc_reg();
  2519. }
  2520. /*
  2521. * Load the register containing the offset of the MAC-layer
  2522. * payload into the X register.
  2523. */
  2524. s = new_stmt(BPF_LDX|BPF_MEM);
  2525. s->s.k = reg_off_macpl;
  2526. return s;
  2527. } else {
  2528. /*
  2529. * That offset isn't variable, so we don't need to
  2530. * generate any code.
  2531. */
  2532. return NULL;
  2533. }
  2534. }
  2535. /*
  2536. * Map an Ethernet type to the equivalent PPP type.
  2537. */
  2538. static int
  2539. ethertype_to_ppptype(proto)
  2540. int proto;
  2541. {
  2542. switch (proto) {
  2543. case ETHERTYPE_IP:
  2544. proto = PPP_IP;
  2545. break;
  2546. case ETHERTYPE_IPV6:
  2547. proto = PPP_IPV6;
  2548. break;
  2549. case ETHERTYPE_DN:
  2550. proto = PPP_DECNET;
  2551. break;
  2552. case ETHERTYPE_ATALK:
  2553. proto = PPP_APPLE;
  2554. break;
  2555. case ETHERTYPE_NS:
  2556. proto = PPP_NS;
  2557. break;
  2558. case LLCSAP_ISONS:
  2559. proto = PPP_OSI;
  2560. break;
  2561. case LLCSAP_8021D:
  2562. /*
  2563. * I'm assuming the "Bridging PDU"s that go
  2564. * over PPP are Spanning Tree Protocol
  2565. * Bridging PDUs.
  2566. */
  2567. proto = PPP_BRPDU;
  2568. break;
  2569. case LLCSAP_IPX:
  2570. proto = PPP_IPX;
  2571. break;
  2572. }
  2573. return (proto);
  2574. }
  2575. /*
  2576. * Generate code to match a particular packet type by matching the
  2577. * link-layer type field or fields in the 802.2 LLC header.
  2578. *
  2579. * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
  2580. * value, if <= ETHERMTU.
  2581. */
  2582. static struct block *
  2583. gen_linktype(proto)
  2584. register int proto;
  2585. {
  2586. struct block *b0, *b1, *b2;
  2587. /* are we checking MPLS-encapsulated packets? */
  2588. if (label_stack_depth > 0) {
  2589. switch (proto) {
  2590. case ETHERTYPE_IP:
  2591. case PPP_IP:
  2592. /* FIXME add other L3 proto IDs */
  2593. return gen_mpls_linktype(Q_IP);
  2594. case ETHERTYPE_IPV6:
  2595. case PPP_IPV6:
  2596. /* FIXME add other L3 proto IDs */
  2597. return gen_mpls_linktype(Q_IPV6);
  2598. default:
  2599. bpf_error("unsupported protocol over mpls");
  2600. /* NOTREACHED */
  2601. }
  2602. }
  2603. /*
  2604. * Are we testing PPPoE packets?
  2605. */
  2606. if (is_pppoes) {
  2607. /*
  2608. * The PPPoE session header is part of the
  2609. * MAC-layer payload, so all references
  2610. * should be relative to the beginning of
  2611. * that payload.
  2612. */
  2613. /*
  2614. * We use Ethernet protocol types inside libpcap;
  2615. * map them to the corresponding PPP protocol types.
  2616. */
  2617. proto = ethertype_to_ppptype(proto);
  2618. return gen_cmp(OR_MACPL, off_linktype, BPF_H, (bpf_int32)proto);
  2619. }
  2620. switch (linktype) {
  2621. case DLT_EN10MB:
  2622. case DLT_NETANALYZER:
  2623. case DLT_NETANALYZER_TRANSPARENT:
  2624. return gen_ether_linktype(proto);
  2625. /*NOTREACHED*/
  2626. break;
  2627. case DLT_C_HDLC:
  2628. switch (proto) {
  2629. case LLCSAP_ISONS:
  2630. proto = (proto << 8 | LLCSAP_ISONS);
  2631. /* fall through */
  2632. default:
  2633. return gen_cmp(OR_LINK, off_linktype, BPF_H,
  2634. (bpf_int32)proto);
  2635. /*NOTREACHED*/
  2636. break;
  2637. }
  2638. break;
  2639. case DLT_IEEE802_11:
  2640. case DLT_PRISM_HEADER:
  2641. case DLT_IEEE802_11_RADIO_AVS:
  2642. case DLT_IEEE802_11_RADIO:
  2643. case DLT_PPI:
  2644. /*
  2645. * Check that we have a data frame.
  2646. */
  2647. b0 = gen_check_802_11_data_frame();
  2648. /*
  2649. * Now check for the specified link-layer type.
  2650. */
  2651. b1 = gen_llc_linktype(proto);
  2652. gen_and(b0, b1);
  2653. return b1;
  2654. /*NOTREACHED*/
  2655. break;
  2656. case DLT_FDDI:
  2657. /*
  2658. * XXX - check for asynchronous frames, as per RFC 1103.
  2659. */
  2660. return gen_llc_linktype(proto);
  2661. /*NOTREACHED*/
  2662. break;
  2663. case DLT_IEEE802:
  2664. /*
  2665. * XXX - check for LLC PDUs, as per IEEE 802.5.
  2666. */
  2667. return gen_llc_linktype(proto);
  2668. /*NOTREACHED*/
  2669. break;
  2670. case DLT_ATM_RFC1483:
  2671. case DLT_ATM_CLIP:
  2672. case DLT_IP_OVER_FC:
  2673. return gen_llc_linktype(proto);
  2674. /*NOTREACHED*/
  2675. break;
  2676. case DLT_SUNATM:
  2677. /*
  2678. * If "is_lane" is set, check for a LANE-encapsulated
  2679. * version of this protocol, otherwise check for an
  2680. * LLC-encapsulated version of this protocol.
  2681. *
  2682. * We assume LANE means Ethernet, not Token Ring.
  2683. */
  2684. if (is_lane) {
  2685. /*
  2686. * Check that the packet doesn't begin with an
  2687. * LE Control marker. (We've already generated
  2688. * a test for LANE.)
  2689. */
  2690. b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
  2691. 0xFF00);
  2692. gen_not(b0);
  2693. /*
  2694. * Now generate an Ethernet test.
  2695. */
  2696. b1 = gen_ether_linktype(proto);
  2697. gen_and(b0, b1);
  2698. return b1;
  2699. } else {
  2700. /*
  2701. * Check for LLC encapsulation and then check the
  2702. * protocol.
  2703. */
  2704. b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
  2705. b1 = gen_llc_linktype(proto);
  2706. gen_and(b0, b1);
  2707. return b1;
  2708. }
  2709. /*NOTREACHED*/
  2710. break;
  2711. case DLT_LINUX_SLL:
  2712. return gen_linux_sll_linktype(proto);
  2713. /*NOTREACHED*/
  2714. break;
  2715. case DLT_SLIP:
  2716. case DLT_SLIP_BSDOS:
  2717. case DLT_RAW:
  2718. /*
  2719. * These types don't provide any type field; packets
  2720. * are always IPv4 or IPv6.
  2721. *
  2722. * XXX - for IPv4, check for a version number of 4, and,
  2723. * for IPv6, check for a version number of 6?
  2724. */
  2725. switch (proto) {
  2726. case ETHERTYPE_IP:
  2727. /* Check for a version number of 4. */
  2728. return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
  2729. case ETHERTYPE_IPV6:
  2730. /* Check for a version number of 6. */
  2731. return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
  2732. default:
  2733. return gen_false(); /* always false */
  2734. }
  2735. /*NOTREACHED*/
  2736. break;
  2737. case DLT_IPV4:
  2738. /*
  2739. * Raw IPv4, so no type field.
  2740. */
  2741. if (proto == ETHERTYPE_IP)
  2742. return gen_true(); /* always true */
  2743. /* Checking for something other than IPv4; always false */
  2744. return gen_false();
  2745. /*NOTREACHED*/
  2746. break;
  2747. case DLT_IPV6:
  2748. /*
  2749. * Raw IPv6, so no type field.
  2750. */
  2751. if (proto == ETHERTYPE_IPV6)
  2752. return gen_true(); /* always true */
  2753. /* Checking for something other than IPv6; always false */
  2754. return gen_false();
  2755. /*NOTREACHED*/
  2756. break;
  2757. case DLT_PPP:
  2758. case DLT_PPP_PPPD:
  2759. case DLT_PPP_SERIAL:
  2760. case DLT_PPP_ETHER:
  2761. /*
  2762. * We use Ethernet protocol types inside libpcap;
  2763. * map them to the corresponding PPP protocol types.
  2764. */
  2765. proto = ethertype_to_ppptype(proto);
  2766. return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
  2767. /*NOTREACHED*/
  2768. break;
  2769. case DLT_PPP_BSDOS:
  2770. /*
  2771. * We use Ethernet protocol types inside libpcap;
  2772. * map them to the corresponding PPP protocol types.
  2773. */
  2774. switch (proto) {
  2775. case ETHERTYPE_IP:
  2776. /*
  2777. * Also check for Van Jacobson-compressed IP.
  2778. * XXX - do this for other forms of PPP?
  2779. */
  2780. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
  2781. b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
  2782. gen_or(b0, b1);
  2783. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
  2784. gen_or(b1, b0);
  2785. return b0;
  2786. default:
  2787. proto = ethertype_to_ppptype(proto);
  2788. return gen_cmp(OR_LINK, off_linktype, BPF_H,
  2789. (bpf_int32)proto);
  2790. }
  2791. /*NOTREACHED*/
  2792. break;
  2793. case DLT_NULL:
  2794. case DLT_LOOP:
  2795. case DLT_ENC:
  2796. /*
  2797. * For DLT_NULL, the link-layer header is a 32-bit
  2798. * word containing an AF_ value in *host* byte order,
  2799. * and for DLT_ENC, the link-layer header begins
  2800. * with a 32-bit work containing an AF_ value in
  2801. * host byte order.
  2802. *
  2803. * In addition, if we're reading a saved capture file,
  2804. * the host byte order in the capture may not be the
  2805. * same as the host byte order on this machine.
  2806. *
  2807. * For DLT_LOOP, the link-layer header is a 32-bit
  2808. * word containing an AF_ value in *network* byte order.
  2809. *
  2810. * XXX - AF_ values may, unfortunately, be platform-
  2811. * dependent; for example, FreeBSD's AF_INET6 is 24
  2812. * whilst NetBSD's and OpenBSD's is 26.
  2813. *
  2814. * This means that, when reading a capture file, just
  2815. * checking for our AF_INET6 value won't work if the
  2816. * capture file came from another OS.
  2817. */
  2818. switch (proto) {
  2819. case ETHERTYPE_IP:
  2820. proto = AF_INET;
  2821. break;
  2822. #ifdef INET6
  2823. case ETHERTYPE_IPV6:
  2824. proto = AF_INET6;
  2825. break;
  2826. #endif
  2827. default:
  2828. /*
  2829. * Not a type on which we support filtering.
  2830. * XXX - support those that have AF_ values
  2831. * #defined on this platform, at least?
  2832. */
  2833. return gen_false();
  2834. }
  2835. if (linktype == DLT_NULL || linktype == DLT_ENC) {
  2836. /*
  2837. * The AF_ value is in host byte order, but
  2838. * the BPF interpreter will convert it to
  2839. * network byte order.
  2840. *
  2841. * If this is a save file, and it's from a
  2842. * machine with the opposite byte order to
  2843. * ours, we byte-swap the AF_ value.
  2844. *
  2845. * Then we run it through "htonl()", and
  2846. * generate code to compare against the result.
  2847. */
  2848. if (bpf_pcap->rfile != NULL && bpf_pcap->swapped)
  2849. proto = SWAPLONG(proto);
  2850. proto = htonl(proto);
  2851. }
  2852. return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
  2853. #ifdef HAVE_NET_PFVAR_H
  2854. case DLT_PFLOG:
  2855. /*
  2856. * af field is host byte order in contrast to the rest of
  2857. * the packet.
  2858. */
  2859. if (proto == ETHERTYPE_IP)
  2860. return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
  2861. BPF_B, (bpf_int32)AF_INET));
  2862. else if (proto == ETHERTYPE_IPV6)
  2863. return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
  2864. BPF_B, (bpf_int32)AF_INET6));
  2865. else
  2866. return gen_false();
  2867. /*NOTREACHED*/
  2868. break;
  2869. #endif /* HAVE_NET_PFVAR_H */
  2870. case DLT_ARCNET:
  2871. case DLT_ARCNET_LINUX:
  2872. /*
  2873. * XXX should we check for first fragment if the protocol
  2874. * uses PHDS?
  2875. */
  2876. switch (proto) {
  2877. default:
  2878. return gen_false();
  2879. case ETHERTYPE_IPV6:
  2880. return (gen_cmp(OR_LINK, off_linktype, BPF_B,
  2881. (bpf_int32)ARCTYPE_INET6));
  2882. case ETHERTYPE_IP:
  2883. b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
  2884. (bpf_int32)ARCTYPE_IP);
  2885. b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
  2886. (bpf_int32)ARCTYPE_IP_OLD);
  2887. gen_or(b0, b1);
  2888. return (b1);
  2889. case ETHERTYPE_ARP:
  2890. b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
  2891. (bpf_int32)ARCTYPE_ARP);
  2892. b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
  2893. (bpf_int32)ARCTYPE_ARP_OLD);
  2894. gen_or(b0, b1);
  2895. return (b1);
  2896. case ETHERTYPE_REVARP:
  2897. return (gen_cmp(OR_LINK, off_linktype, BPF_B,
  2898. (bpf_int32)ARCTYPE_REVARP));
  2899. case ETHERTYPE_ATALK:
  2900. return (gen_cmp(OR_LINK, off_linktype, BPF_B,
  2901. (bpf_int32)ARCTYPE_ATALK));
  2902. }
  2903. /*NOTREACHED*/
  2904. break;
  2905. case DLT_LTALK:
  2906. switch (proto) {
  2907. case ETHERTYPE_ATALK:
  2908. return gen_true();
  2909. default:
  2910. return gen_false();
  2911. }
  2912. /*NOTREACHED*/
  2913. break;
  2914. case DLT_FRELAY:
  2915. /*
  2916. * XXX - assumes a 2-byte Frame Relay header with
  2917. * DLCI and flags. What if the address is longer?
  2918. */
  2919. switch (proto) {
  2920. case ETHERTYPE_IP:
  2921. /*
  2922. * Check for the special NLPID for IP.
  2923. */
  2924. return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
  2925. case ETHERTYPE_IPV6:
  2926. /*
  2927. * Check for the special NLPID for IPv6.
  2928. */
  2929. return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
  2930. case LLCSAP_ISONS:
  2931. /*
  2932. * Check for several OSI protocols.
  2933. *
  2934. * Frame Relay packets typically have an OSI
  2935. * NLPID at the beginning; we check for each
  2936. * of them.
  2937. *
  2938. * What we check for is the NLPID and a frame
  2939. * control field of UI, i.e. 0x03 followed
  2940. * by the NLPID.
  2941. */
  2942. b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
  2943. b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
  2944. b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
  2945. gen_or(b1, b2);
  2946. gen_or(b0, b2);
  2947. return b2;
  2948. default:
  2949. return gen_false();
  2950. }
  2951. /*NOTREACHED*/
  2952. break;
  2953. case DLT_MFR:
  2954. bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
  2955. case DLT_JUNIPER_MFR:
  2956. case DLT_JUNIPER_MLFR:
  2957. case DLT_JUNIPER_MLPPP:
  2958. case DLT_JUNIPER_ATM1:
  2959. case DLT_JUNIPER_ATM2:
  2960. case DLT_JUNIPER_PPPOE:
  2961. case DLT_JUNIPER_PPPOE_ATM:
  2962. case DLT_JUNIPER_GGSN:
  2963. case DLT_JUNIPER_ES:
  2964. case DLT_JUNIPER_MONITOR:
  2965. case DLT_JUNIPER_SERVICES:
  2966. case DLT_JUNIPER_ETHER:
  2967. case DLT_JUNIPER_PPP:
  2968. case DLT_JUNIPER_FRELAY:
  2969. case DLT_JUNIPER_CHDLC:
  2970. case DLT_JUNIPER_VP:
  2971. case DLT_JUNIPER_ST:
  2972. case DLT_JUNIPER_ISM:
  2973. case DLT_JUNIPER_VS:
  2974. case DLT_JUNIPER_SRX_E2E:
  2975. case DLT_JUNIPER_FIBRECHANNEL:
  2976. case DLT_JUNIPER_ATM_CEMIC:
  2977. /* just lets verify the magic number for now -
  2978. * on ATM we may have up to 6 different encapsulations on the wire
  2979. * and need a lot of heuristics to figure out that the payload
  2980. * might be;
  2981. *
  2982. * FIXME encapsulation specific BPF_ filters
  2983. */
  2984. return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
  2985. case DLT_BACNET_MS_TP:
  2986. return gen_mcmp(OR_LINK, 0, BPF_W, 0x55FF0000, 0xffff0000);
  2987. case DLT_IPNET:
  2988. return gen_ipnet_linktype(proto);
  2989. case DLT_LINUX_IRDA:
  2990. bpf_error("IrDA link-layer type filtering not implemented");
  2991. case DLT_DOCSIS:
  2992. bpf_error("DOCSIS link-layer type filtering not implemented");
  2993. case DLT_MTP2:
  2994. case DLT_MTP2_WITH_PHDR:
  2995. bpf_error("MTP2 link-layer type filtering not implemented");
  2996. case DLT_ERF:
  2997. bpf_error("ERF link-layer type filtering not implemented");
  2998. case DLT_PFSYNC:
  2999. bpf_error("PFSYNC link-layer type filtering not implemented");
  3000. case DLT_LINUX_LAPD:
  3001. bpf_error("LAPD link-layer type filtering not implemented");
  3002. case DLT_USB:
  3003. case DLT_USB_LINUX:
  3004. case DLT_USB_LINUX_MMAPPED:
  3005. bpf_error("USB link-layer type filtering not implemented");
  3006. case DLT_BLUETOOTH_HCI_H4:
  3007. case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
  3008. bpf_error("Bluetooth link-layer type filtering not implemented");
  3009. case DLT_CAN20B:
  3010. case DLT_CAN_SOCKETCAN:
  3011. bpf_error("CAN link-layer type filtering not implemented");
  3012. case DLT_IEEE802_15_4:
  3013. case DLT_IEEE802_15_4_LINUX:
  3014. case DLT_IEEE802_15_4_NONASK_PHY:
  3015. case DLT_IEEE802_15_4_NOFCS:
  3016. bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
  3017. case DLT_IEEE802_16_MAC_CPS_RADIO:
  3018. bpf_error("IEEE 802.16 link-layer type filtering not implemented");
  3019. case DLT_SITA:
  3020. bpf_error("SITA link-layer type filtering not implemented");
  3021. case DLT_RAIF1:
  3022. bpf_error("RAIF1 link-layer type filtering not implemented");
  3023. case DLT_IPMB:
  3024. bpf_error("IPMB link-layer type filtering not implemented");
  3025. case DLT_AX25_KISS:
  3026. bpf_error("AX.25 link-layer type filtering not implemented");
  3027. }
  3028. /*
  3029. * All the types that have no encapsulation should either be
  3030. * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
  3031. * all packets are IP packets, or should be handled in some
  3032. * special case, if none of them are (if some are and some
  3033. * aren't, the lack of encapsulation is a problem, as we'd
  3034. * have to find some other way of determining the packet type).
  3035. *
  3036. * Therefore, if "off_linktype" is -1, there's an error.
  3037. */
  3038. if (off_linktype == (u_int)-1)
  3039. abort();
  3040. /*
  3041. * Any type not handled above should always have an Ethernet
  3042. * type at an offset of "off_linktype".
  3043. */
  3044. return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
  3045. }
  3046. /*
  3047. * Check for an LLC SNAP packet with a given organization code and
  3048. * protocol type; we check the entire contents of the 802.2 LLC and
  3049. * snap headers, checking for DSAP and SSAP of SNAP and a control
  3050. * field of 0x03 in the LLC header, and for the specified organization
  3051. * code and protocol type in the SNAP header.
  3052. */
  3053. static struct block *
  3054. gen_snap(orgcode, ptype)
  3055. bpf_u_int32 orgcode;
  3056. bpf_u_int32 ptype;
  3057. {
  3058. u_char snapblock[8];
  3059. snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
  3060. snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
  3061. snapblock[2] = 0x03; /* control = UI */
  3062. snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
  3063. snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
  3064. snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
  3065. snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
  3066. snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
  3067. return gen_bcmp(OR_MACPL, 0, 8, snapblock);
  3068. }
  3069. /*
  3070. * Generate code to match a particular packet type, for link-layer types
  3071. * using 802.2 LLC headers.
  3072. *
  3073. * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
  3074. * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
  3075. *
  3076. * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
  3077. * value, if <= ETHERMTU. We use that to determine whether to
  3078. * match the DSAP or both DSAP and LSAP or to check the OUI and
  3079. * protocol ID in a SNAP header.
  3080. */
  3081. static struct block *
  3082. gen_llc_linktype(proto)
  3083. int proto;
  3084. {
  3085. /*
  3086. * XXX - handle token-ring variable-length header.
  3087. */
  3088. switch (proto) {
  3089. case LLCSAP_IP:
  3090. case LLCSAP_ISONS:
  3091. case LLCSAP_NETBEUI:
  3092. /*
  3093. * XXX - should we check both the DSAP and the
  3094. * SSAP, like this, or should we check just the
  3095. * DSAP, as we do for other types <= ETHERMTU
  3096. * (i.e., other SAP values)?
  3097. */
  3098. return gen_cmp(OR_MACPL, 0, BPF_H, (bpf_u_int32)
  3099. ((proto << 8) | proto));
  3100. case LLCSAP_IPX:
  3101. /*
  3102. * XXX - are there ever SNAP frames for IPX on
  3103. * non-Ethernet 802.x networks?
  3104. */
  3105. return gen_cmp(OR_MACPL, 0, BPF_B,
  3106. (bpf_int32)LLCSAP_IPX);
  3107. case ETHERTYPE_ATALK:
  3108. /*
  3109. * 802.2-encapsulated ETHERTYPE_ATALK packets are
  3110. * SNAP packets with an organization code of
  3111. * 0x080007 (Apple, for Appletalk) and a protocol
  3112. * type of ETHERTYPE_ATALK (Appletalk).
  3113. *
  3114. * XXX - check for an organization code of
  3115. * encapsulated Ethernet as well?
  3116. */
  3117. return gen_snap(0x080007, ETHERTYPE_ATALK);
  3118. default:
  3119. /*
  3120. * XXX - we don't have to check for IPX 802.3
  3121. * here, but should we check for the IPX Ethertype?
  3122. */
  3123. if (proto <= ETHERMTU) {
  3124. /*
  3125. * This is an LLC SAP value, so check
  3126. * the DSAP.
  3127. */
  3128. return gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)proto);
  3129. } else {
  3130. /*
  3131. * This is an Ethernet type; we assume that it's
  3132. * unlikely that it'll appear in the right place
  3133. * at random, and therefore check only the
  3134. * location that would hold the Ethernet type
  3135. * in a SNAP frame with an organization code of
  3136. * 0x000000 (encapsulated Ethernet).
  3137. *
  3138. * XXX - if we were to check for the SNAP DSAP and
  3139. * LSAP, as per XXX, and were also to check for an
  3140. * organization code of 0x000000 (encapsulated
  3141. * Ethernet), we'd do
  3142. *
  3143. * return gen_snap(0x000000, proto);
  3144. *
  3145. * here; for now, we don't, as per the above.
  3146. * I don't know whether it's worth the extra CPU
  3147. * time to do the right check or not.
  3148. */
  3149. return gen_cmp(OR_MACPL, 6, BPF_H, (bpf_int32)proto);
  3150. }
  3151. }
  3152. }
  3153. static struct block *
  3154. gen_hostop(addr, mask, dir, proto, src_off, dst_off)
  3155. bpf_u_int32 addr;
  3156. bpf_u_int32 mask;
  3157. int dir, proto;
  3158. u_int src_off, dst_off;
  3159. {
  3160. struct block *b0, *b1;
  3161. u_int offset;
  3162. switch (dir) {
  3163. case Q_SRC:
  3164. offset = src_off;
  3165. break;
  3166. case Q_DST:
  3167. offset = dst_off;
  3168. break;
  3169. case Q_AND:
  3170. b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
  3171. b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
  3172. gen_and(b0, b1);
  3173. return b1;
  3174. case Q_OR:
  3175. case Q_DEFAULT:
  3176. b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
  3177. b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
  3178. gen_or(b0, b1);
  3179. return b1;
  3180. default:
  3181. abort();
  3182. }
  3183. b0 = gen_linktype(proto);
  3184. b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
  3185. gen_and(b0, b1);
  3186. return b1;
  3187. }
  3188. #ifdef INET6
  3189. static struct block *
  3190. gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
  3191. struct in6_addr *addr;
  3192. struct in6_addr *mask;
  3193. int dir, proto;
  3194. u_int src_off, dst_off;
  3195. {
  3196. struct block *b0, *b1;
  3197. u_int offset;
  3198. u_int32_t *a, *m;
  3199. switch (dir) {
  3200. case Q_SRC:
  3201. offset = src_off;
  3202. break;
  3203. case Q_DST:
  3204. offset = dst_off;
  3205. break;
  3206. case Q_AND:
  3207. b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
  3208. b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
  3209. gen_and(b0, b1);
  3210. return b1;
  3211. case Q_OR:
  3212. case Q_DEFAULT:
  3213. b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
  3214. b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
  3215. gen_or(b0, b1);
  3216. return b1;
  3217. default:
  3218. abort();
  3219. }
  3220. /* this order is important */
  3221. a = (u_int32_t *)addr;
  3222. m = (u_int32_t *)mask;
  3223. b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
  3224. b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
  3225. gen_and(b0, b1);
  3226. b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
  3227. gen_and(b0, b1);
  3228. b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
  3229. gen_and(b0, b1);
  3230. b0 = gen_linktype(proto);
  3231. gen_and(b0, b1);
  3232. return b1;
  3233. }
  3234. #endif
  3235. static struct block *
  3236. gen_ehostop(eaddr, dir)
  3237. register const u_char *eaddr;
  3238. register int dir;
  3239. {
  3240. register struct block *b0, *b1;
  3241. switch (dir) {
  3242. case Q_SRC:
  3243. return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
  3244. case Q_DST:
  3245. return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
  3246. case Q_AND:
  3247. b0 = gen_ehostop(eaddr, Q_SRC);
  3248. b1 = gen_ehostop(eaddr, Q_DST);
  3249. gen_and(b0, b1);
  3250. return b1;
  3251. case Q_DEFAULT:
  3252. case Q_OR:
  3253. b0 = gen_ehostop(eaddr, Q_SRC);
  3254. b1 = gen_ehostop(eaddr, Q_DST);
  3255. gen_or(b0, b1);
  3256. return b1;
  3257. case Q_ADDR1:
  3258. bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
  3259. break;
  3260. case Q_ADDR2:
  3261. bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
  3262. break;
  3263. case Q_ADDR3:
  3264. bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
  3265. break;
  3266. case Q_ADDR4:
  3267. bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
  3268. break;
  3269. case Q_RA:
  3270. bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
  3271. break;
  3272. case Q_TA:
  3273. bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
  3274. break;
  3275. }
  3276. abort();
  3277. /* NOTREACHED */
  3278. }
  3279. /*
  3280. * Like gen_ehostop, but for DLT_FDDI
  3281. */
  3282. static struct block *
  3283. gen_fhostop(eaddr, dir)
  3284. register const u_char *eaddr;
  3285. register int dir;
  3286. {
  3287. struct block *b0, *b1;
  3288. switch (dir) {
  3289. case Q_SRC:
  3290. return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
  3291. case Q_DST:
  3292. return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
  3293. case Q_AND:
  3294. b0 = gen_fhostop(eaddr, Q_SRC);
  3295. b1 = gen_fhostop(eaddr, Q_DST);
  3296. gen_and(b0, b1);
  3297. return b1;
  3298. case Q_DEFAULT:
  3299. case Q_OR:
  3300. b0 = gen_fhostop(eaddr, Q_SRC);
  3301. b1 = gen_fhostop(eaddr, Q_DST);
  3302. gen_or(b0, b1);
  3303. return b1;
  3304. case Q_ADDR1:
  3305. bpf_error("'addr1' is only supported on 802.11");
  3306. break;
  3307. case Q_ADDR2:
  3308. bpf_error("'addr2' is only supported on 802.11");
  3309. break;
  3310. case Q_ADDR3:
  3311. bpf_error("'addr3' is only supported on 802.11");
  3312. break;
  3313. case Q_ADDR4:
  3314. bpf_error("'addr4' is only supported on 802.11");
  3315. break;
  3316. case Q_RA:
  3317. bpf_error("'ra' is only supported on 802.11");
  3318. break;
  3319. case Q_TA:
  3320. bpf_error("'ta' is only supported on 802.11");
  3321. break;
  3322. }
  3323. abort();
  3324. /* NOTREACHED */
  3325. }
  3326. /*
  3327. * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
  3328. */
  3329. static struct block *
  3330. gen_thostop(eaddr, dir)
  3331. register const u_char *eaddr;
  3332. register int dir;
  3333. {
  3334. register struct block *b0, *b1;
  3335. switch (dir) {
  3336. case Q_SRC:
  3337. return gen_bcmp(OR_LINK, 8, 6, eaddr);
  3338. case Q_DST:
  3339. return gen_bcmp(OR_LINK, 2, 6, eaddr);
  3340. case Q_AND:
  3341. b0 = gen_thostop(eaddr, Q_SRC);
  3342. b1 = gen_thostop(eaddr, Q_DST);
  3343. gen_and(b0, b1);
  3344. return b1;
  3345. case Q_DEFAULT:
  3346. case Q_OR:
  3347. b0 = gen_thostop(eaddr, Q_SRC);
  3348. b1 = gen_thostop(eaddr, Q_DST);
  3349. gen_or(b0, b1);
  3350. return b1;
  3351. case Q_ADDR1:
  3352. bpf_error("'addr1' is only supported on 802.11");
  3353. break;
  3354. case Q_ADDR2:
  3355. bpf_error("'addr2' is only supported on 802.11");
  3356. break;
  3357. case Q_ADDR3:
  3358. bpf_error("'addr3' is only supported on 802.11");
  3359. break;
  3360. case Q_ADDR4:
  3361. bpf_error("'addr4' is only supported on 802.11");
  3362. break;
  3363. case Q_RA:
  3364. bpf_error("'ra' is only supported on 802.11");
  3365. break;
  3366. case Q_TA:
  3367. bpf_error("'ta' is only supported on 802.11");
  3368. break;
  3369. }
  3370. abort();
  3371. /* NOTREACHED */
  3372. }
  3373. /*
  3374. * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
  3375. * various 802.11 + radio headers.
  3376. */
  3377. static struct block *
  3378. gen_wlanhostop(eaddr, dir)
  3379. register const u_char *eaddr;
  3380. register int dir;
  3381. {
  3382. register struct block *b0, *b1, *b2;
  3383. register struct slist *s;
  3384. #ifdef ENABLE_WLAN_FILTERING_PATCH
  3385. /*
  3386. * TODO GV 20070613
  3387. * We need to disable the optimizer because the optimizer is buggy
  3388. * and wipes out some LD instructions generated by the below
  3389. * code to validate the Frame Control bits
  3390. */
  3391. no_optimize = 1;
  3392. #endif /* ENABLE_WLAN_FILTERING_PATCH */
  3393. switch (dir) {
  3394. case Q_SRC:
  3395. /*
  3396. * Oh, yuk.
  3397. *
  3398. * For control frames, there is no SA.
  3399. *
  3400. * For management frames, SA is at an
  3401. * offset of 10 from the beginning of
  3402. * the packet.
  3403. *
  3404. * For data frames, SA is at an offset
  3405. * of 10 from the beginning of the packet
  3406. * if From DS is clear, at an offset of
  3407. * 16 from the beginning of the packet
  3408. * if From DS is set and To DS is clear,
  3409. * and an offset of 24 from the beginning
  3410. * of the packet if From DS is set and To DS
  3411. * is set.
  3412. */
  3413. /*
  3414. * Generate the tests to be done for data frames
  3415. * with From DS set.
  3416. *
  3417. * First, check for To DS set, i.e. check "link[1] & 0x01".
  3418. */
  3419. s = gen_load_a(OR_LINK, 1, BPF_B);
  3420. b1 = new_block(JMP(BPF_JSET));
  3421. b1->s.k = 0x01; /* To DS */
  3422. b1->stmts = s;
  3423. /*
  3424. * If To DS is set, the SA is at 24.
  3425. */
  3426. b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
  3427. gen_and(b1, b0);
  3428. /*
  3429. * Now, check for To DS not set, i.e. check
  3430. * "!(link[1] & 0x01)".
  3431. */
  3432. s = gen_load_a(OR_LINK, 1, BPF_B);
  3433. b2 = new_block(JMP(BPF_JSET));
  3434. b2->s.k = 0x01; /* To DS */
  3435. b2->stmts = s;
  3436. gen_not(b2);
  3437. /*
  3438. * If To DS is not set, the SA is at 16.
  3439. */
  3440. b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
  3441. gen_and(b2, b1);
  3442. /*
  3443. * Now OR together the last two checks. That gives
  3444. * the complete set of checks for data frames with
  3445. * From DS set.
  3446. */
  3447. gen_or(b1, b0);
  3448. /*
  3449. * Now check for From DS being set, and AND that with
  3450. * the ORed-together checks.
  3451. */
  3452. s = gen_load_a(OR_LINK, 1, BPF_B);
  3453. b1 = new_block(JMP(BPF_JSET));
  3454. b1->s.k = 0x02; /* From DS */
  3455. b1->stmts = s;
  3456. gen_and(b1, b0);
  3457. /*
  3458. * Now check for data frames with From DS not set.
  3459. */
  3460. s = gen_load_a(OR_LINK, 1, BPF_B);
  3461. b2 = new_block(JMP(BPF_JSET));
  3462. b2->s.k = 0x02; /* From DS */
  3463. b2->stmts = s;
  3464. gen_not(b2);
  3465. /*
  3466. * If From DS isn't set, the SA is at 10.
  3467. */
  3468. b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
  3469. gen_and(b2, b1);
  3470. /*
  3471. * Now OR together the checks for data frames with
  3472. * From DS not set and for data frames with From DS
  3473. * set; that gives the checks done for data frames.
  3474. */
  3475. gen_or(b1, b0);
  3476. /*
  3477. * Now check for a data frame.
  3478. * I.e, check "link[0] & 0x08".
  3479. */
  3480. s = gen_load_a(OR_LINK, 0, BPF_B);
  3481. b1 = new_block(JMP(BPF_JSET));
  3482. b1->s.k = 0x08;
  3483. b1->stmts = s;
  3484. /*
  3485. * AND that with the checks done for data frames.
  3486. */
  3487. gen_and(b1, b0);
  3488. /*
  3489. * If the high-order bit of the type value is 0, this
  3490. * is a management frame.
  3491. * I.e, check "!(link[0] & 0x08)".
  3492. */
  3493. s = gen_load_a(OR_LINK, 0, BPF_B);
  3494. b2 = new_block(JMP(BPF_JSET));
  3495. b2->s.k = 0x08;
  3496. b2->stmts = s;
  3497. gen_not(b2);
  3498. /*
  3499. * For management frames, the SA is at 10.
  3500. */
  3501. b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
  3502. gen_and(b2, b1);
  3503. /*
  3504. * OR that with the checks done for data frames.
  3505. * That gives the checks done for management and
  3506. * data frames.
  3507. */
  3508. gen_or(b1, b0);
  3509. /*
  3510. * If the low-order bit of the type value is 1,
  3511. * this is either a control frame or a frame
  3512. * with a reserved type, and thus not a
  3513. * frame with an SA.
  3514. *
  3515. * I.e., check "!(link[0] & 0x04)".
  3516. */
  3517. s = gen_load_a(OR_LINK, 0, BPF_B);
  3518. b1 = new_block(JMP(BPF_JSET));
  3519. b1->s.k = 0x04;
  3520. b1->stmts = s;
  3521. gen_not(b1);
  3522. /*
  3523. * AND that with the checks for data and management
  3524. * frames.
  3525. */
  3526. gen_and(b1, b0);
  3527. return b0;
  3528. case Q_DST:
  3529. /*
  3530. * Oh, yuk.
  3531. *
  3532. * For control frames, there is no DA.
  3533. *
  3534. * For management frames, DA is at an
  3535. * offset of 4 from the beginning of
  3536. * the packet.
  3537. *
  3538. * For data frames, DA is at an offset
  3539. * of 4 from the beginning of the packet
  3540. * if To DS is clear and at an offset of
  3541. * 16 from the beginning of the packet
  3542. * if To DS is set.
  3543. */
  3544. /*
  3545. * Generate the tests to be done for data frames.
  3546. *
  3547. * First, check for To DS set, i.e. "link[1] & 0x01".
  3548. */
  3549. s = gen_load_a(OR_LINK, 1, BPF_B);
  3550. b1 = new_block(JMP(BPF_JSET));
  3551. b1->s.k = 0x01; /* To DS */
  3552. b1->stmts = s;
  3553. /*
  3554. * If To DS is set, the DA is at 16.
  3555. */
  3556. b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
  3557. gen_and(b1, b0);
  3558. /*
  3559. * Now, check for To DS not set, i.e. check
  3560. * "!(link[1] & 0x01)".
  3561. */
  3562. s = gen_load_a(OR_LINK, 1, BPF_B);
  3563. b2 = new_block(JMP(BPF_JSET));
  3564. b2->s.k = 0x01; /* To DS */
  3565. b2->stmts = s;
  3566. gen_not(b2);
  3567. /*
  3568. * If To DS is not set, the DA is at 4.
  3569. */
  3570. b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
  3571. gen_and(b2, b1);
  3572. /*
  3573. * Now OR together the last two checks. That gives
  3574. * the complete set of checks for data frames.
  3575. */
  3576. gen_or(b1, b0);
  3577. /*
  3578. * Now check for a data frame.
  3579. * I.e, check "link[0] & 0x08".
  3580. */
  3581. s = gen_load_a(OR_LINK, 0, BPF_B);
  3582. b1 = new_block(JMP(BPF_JSET));
  3583. b1->s.k = 0x08;
  3584. b1->stmts = s;
  3585. /*
  3586. * AND that with the checks done for data frames.
  3587. */
  3588. gen_and(b1, b0);
  3589. /*
  3590. * If the high-order bit of the type value is 0, this
  3591. * is a management frame.
  3592. * I.e, check "!(link[0] & 0x08)".
  3593. */
  3594. s = gen_load_a(OR_LINK, 0, BPF_B);
  3595. b2 = new_block(JMP(BPF_JSET));
  3596. b2->s.k = 0x08;
  3597. b2->stmts = s;
  3598. gen_not(b2);
  3599. /*
  3600. * For management frames, the DA is at 4.
  3601. */
  3602. b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
  3603. gen_and(b2, b1);
  3604. /*
  3605. * OR that with the checks done for data frames.
  3606. * That gives the checks done for management and
  3607. * data frames.
  3608. */
  3609. gen_or(b1, b0);
  3610. /*
  3611. * If the low-order bit of the type value is 1,
  3612. * this is either a control frame or a frame
  3613. * with a reserved type, and thus not a
  3614. * frame with an SA.
  3615. *
  3616. * I.e., check "!(link[0] & 0x04)".
  3617. */
  3618. s = gen_load_a(OR_LINK, 0, BPF_B);
  3619. b1 = new_block(JMP(BPF_JSET));
  3620. b1->s.k = 0x04;
  3621. b1->stmts = s;
  3622. gen_not(b1);
  3623. /*
  3624. * AND that with the checks for data and management
  3625. * frames.
  3626. */
  3627. gen_and(b1, b0);
  3628. return b0;
  3629. case Q_RA:
  3630. /*
  3631. * Not present in management frames; addr1 in other
  3632. * frames.
  3633. */
  3634. /*
  3635. * If the high-order bit of the type value is 0, this
  3636. * is a management frame.
  3637. * I.e, check "(link[0] & 0x08)".
  3638. */
  3639. s = gen_load_a(OR_LINK, 0, BPF_B);
  3640. b1 = new_block(JMP(BPF_JSET));
  3641. b1->s.k = 0x08;
  3642. b1->stmts = s;
  3643. /*
  3644. * Check addr1.
  3645. */
  3646. b0 = gen_bcmp(OR_LINK, 4, 6, eaddr);
  3647. /*
  3648. * AND that with the check of addr1.
  3649. */
  3650. gen_and(b1, b0);
  3651. return (b0);
  3652. case Q_TA:
  3653. /*
  3654. * Not present in management frames; addr2, if present,
  3655. * in other frames.
  3656. */
  3657. /*
  3658. * Not present in CTS or ACK control frames.
  3659. */
  3660. b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
  3661. IEEE80211_FC0_TYPE_MASK);
  3662. gen_not(b0);
  3663. b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
  3664. IEEE80211_FC0_SUBTYPE_MASK);
  3665. gen_not(b1);
  3666. b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
  3667. IEEE80211_FC0_SUBTYPE_MASK);
  3668. gen_not(b2);
  3669. gen_and(b1, b2);
  3670. gen_or(b0, b2);
  3671. /*
  3672. * If the high-order bit of the type value is 0, this
  3673. * is a management frame.
  3674. * I.e, check "(link[0] & 0x08)".
  3675. */
  3676. s = gen_load_a(OR_LINK, 0, BPF_B);
  3677. b1 = new_block(JMP(BPF_JSET));
  3678. b1->s.k = 0x08;
  3679. b1->stmts = s;
  3680. /*
  3681. * AND that with the check for frames other than
  3682. * CTS and ACK frames.
  3683. */
  3684. gen_and(b1, b2);
  3685. /*
  3686. * Check addr2.
  3687. */
  3688. b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
  3689. gen_and(b2, b1);
  3690. return b1;
  3691. /*
  3692. * XXX - add BSSID keyword?
  3693. */
  3694. case Q_ADDR1:
  3695. return (gen_bcmp(OR_LINK, 4, 6, eaddr));
  3696. case Q_ADDR2:
  3697. /*
  3698. * Not present in CTS or ACK control frames.
  3699. */
  3700. b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
  3701. IEEE80211_FC0_TYPE_MASK);
  3702. gen_not(b0);
  3703. b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
  3704. IEEE80211_FC0_SUBTYPE_MASK);
  3705. gen_not(b1);
  3706. b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
  3707. IEEE80211_FC0_SUBTYPE_MASK);
  3708. gen_not(b2);
  3709. gen_and(b1, b2);
  3710. gen_or(b0, b2);
  3711. b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
  3712. gen_and(b2, b1);
  3713. return b1;
  3714. case Q_ADDR3:
  3715. /*
  3716. * Not present in control frames.
  3717. */
  3718. b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
  3719. IEEE80211_FC0_TYPE_MASK);
  3720. gen_not(b0);
  3721. b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
  3722. gen_and(b0, b1);
  3723. return b1;
  3724. case Q_ADDR4:
  3725. /*
  3726. * Present only if the direction mask has both "From DS"
  3727. * and "To DS" set. Neither control frames nor management
  3728. * frames should have both of those set, so we don't
  3729. * check the frame type.
  3730. */
  3731. b0 = gen_mcmp(OR_LINK, 1, BPF_B,
  3732. IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
  3733. b1 = gen_bcmp(OR_LINK, 24, 6, eaddr);
  3734. gen_and(b0, b1);
  3735. return b1;
  3736. case Q_AND:
  3737. b0 = gen_wlanhostop(eaddr, Q_SRC);
  3738. b1 = gen_wlanhostop(eaddr, Q_DST);
  3739. gen_and(b0, b1);
  3740. return b1;
  3741. case Q_DEFAULT:
  3742. case Q_OR:
  3743. b0 = gen_wlanhostop(eaddr, Q_SRC);
  3744. b1 = gen_wlanhostop(eaddr, Q_DST);
  3745. gen_or(b0, b1);
  3746. return b1;
  3747. }
  3748. abort();
  3749. /* NOTREACHED */
  3750. }
  3751. /*
  3752. * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
  3753. * (We assume that the addresses are IEEE 48-bit MAC addresses,
  3754. * as the RFC states.)
  3755. */
  3756. static struct block *
  3757. gen_ipfchostop(eaddr, dir)
  3758. register const u_char *eaddr;
  3759. register int dir;
  3760. {
  3761. register struct block *b0, *b1;
  3762. switch (dir) {
  3763. case Q_SRC:
  3764. return gen_bcmp(OR_LINK, 10, 6, eaddr);
  3765. case Q_DST:
  3766. return gen_bcmp(OR_LINK, 2, 6, eaddr);
  3767. case Q_AND:
  3768. b0 = gen_ipfchostop(eaddr, Q_SRC);
  3769. b1 = gen_ipfchostop(eaddr, Q_DST);
  3770. gen_and(b0, b1);
  3771. return b1;
  3772. case Q_DEFAULT:
  3773. case Q_OR:
  3774. b0 = gen_ipfchostop(eaddr, Q_SRC);
  3775. b1 = gen_ipfchostop(eaddr, Q_DST);
  3776. gen_or(b0, b1);
  3777. return b1;
  3778. case Q_ADDR1:
  3779. bpf_error("'addr1' is only supported on 802.11");
  3780. break;
  3781. case Q_ADDR2:
  3782. bpf_error("'addr2' is only supported on 802.11");
  3783. break;
  3784. case Q_ADDR3:
  3785. bpf_error("'addr3' is only supported on 802.11");
  3786. break;
  3787. case Q_ADDR4:
  3788. bpf_error("'addr4' is only supported on 802.11");
  3789. break;
  3790. case Q_RA:
  3791. bpf_error("'ra' is only supported on 802.11");
  3792. break;
  3793. case Q_TA:
  3794. bpf_error("'ta' is only supported on 802.11");
  3795. break;
  3796. }
  3797. abort();
  3798. /* NOTREACHED */
  3799. }
  3800. /*
  3801. * This is quite tricky because there may be pad bytes in front of the
  3802. * DECNET header, and then there are two possible data packet formats that
  3803. * carry both src and dst addresses, plus 5 packet types in a format that
  3804. * carries only the src node, plus 2 types that use a different format and
  3805. * also carry just the src node.
  3806. *
  3807. * Yuck.
  3808. *
  3809. * Instead of doing those all right, we just look for data packets with
  3810. * 0 or 1 bytes of padding. If you want to look at other packets, that
  3811. * will require a lot more hacking.
  3812. *
  3813. * To add support for filtering on DECNET "areas" (network numbers)
  3814. * one would want to add a "mask" argument to this routine. That would
  3815. * make the filter even more inefficient, although one could be clever
  3816. * and not generate masking instructions if the mask is 0xFFFF.
  3817. */
  3818. static struct block *
  3819. gen_dnhostop(addr, dir)
  3820. bpf_u_int32 addr;
  3821. int dir;
  3822. {
  3823. struct block *b0, *b1, *b2, *tmp;
  3824. u_int offset_lh; /* offset if long header is received */
  3825. u_int offset_sh; /* offset if short header is received */
  3826. switch (dir) {
  3827. case Q_DST:
  3828. offset_sh = 1; /* follows flags */
  3829. offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
  3830. break;
  3831. case Q_SRC:
  3832. offset_sh = 3; /* follows flags, dstnode */
  3833. offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
  3834. break;
  3835. case Q_AND:
  3836. /* Inefficient because we do our Calvinball dance twice */
  3837. b0 = gen_dnhostop(addr, Q_SRC);
  3838. b1 = gen_dnhostop(addr, Q_DST);
  3839. gen_and(b0, b1);
  3840. return b1;
  3841. case Q_OR:
  3842. case Q_DEFAULT:
  3843. /* Inefficient because we do our Calvinball dance twice */
  3844. b0 = gen_dnhostop(addr, Q_SRC);
  3845. b1 = gen_dnhostop(addr, Q_DST);
  3846. gen_or(b0, b1);
  3847. return b1;
  3848. case Q_ISO:
  3849. bpf_error("ISO host filtering not implemented");
  3850. default:
  3851. abort();
  3852. }
  3853. b0 = gen_linktype(ETHERTYPE_DN);
  3854. /* Check for pad = 1, long header case */
  3855. tmp = gen_mcmp(OR_NET, 2, BPF_H,
  3856. (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
  3857. b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
  3858. BPF_H, (bpf_int32)ntohs((u_short)addr));
  3859. gen_and(tmp, b1);
  3860. /* Check for pad = 0, long header case */
  3861. tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
  3862. b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
  3863. gen_and(tmp, b2);
  3864. gen_or(b2, b1);
  3865. /* Check for pad = 1, short header case */
  3866. tmp = gen_mcmp(OR_NET, 2, BPF_H,
  3867. (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
  3868. b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
  3869. gen_and(tmp, b2);
  3870. gen_or(b2, b1);
  3871. /* Check for pad = 0, short header case */
  3872. tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
  3873. b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
  3874. gen_and(tmp, b2);
  3875. gen_or(b2, b1);
  3876. /* Combine with test for linktype */
  3877. gen_and(b0, b1);
  3878. return b1;
  3879. }
  3880. /*
  3881. * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
  3882. * test the bottom-of-stack bit, and then check the version number
  3883. * field in the IP header.
  3884. */
  3885. static struct block *
  3886. gen_mpls_linktype(proto)
  3887. int proto;
  3888. {
  3889. struct block *b0, *b1;
  3890. switch (proto) {
  3891. case Q_IP:
  3892. /* match the bottom-of-stack bit */
  3893. b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
  3894. /* match the IPv4 version number */
  3895. b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
  3896. gen_and(b0, b1);
  3897. return b1;
  3898. case Q_IPV6:
  3899. /* match the bottom-of-stack bit */
  3900. b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
  3901. /* match the IPv4 version number */
  3902. b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
  3903. gen_and(b0, b1);
  3904. return b1;
  3905. default:
  3906. abort();
  3907. }
  3908. }
  3909. static struct block *
  3910. gen_host(addr, mask, proto, dir, type)
  3911. bpf_u_int32 addr;
  3912. bpf_u_int32 mask;
  3913. int proto;
  3914. int dir;
  3915. int type;
  3916. {
  3917. struct block *b0, *b1;
  3918. const char *typestr;
  3919. if (type == Q_NET)
  3920. typestr = "net";
  3921. else
  3922. typestr = "host";
  3923. switch (proto) {
  3924. case Q_DEFAULT:
  3925. b0 = gen_host(addr, mask, Q_IP, dir, type);
  3926. /*
  3927. * Only check for non-IPv4 addresses if we're not
  3928. * checking MPLS-encapsulated packets.
  3929. */
  3930. if (label_stack_depth == 0) {
  3931. b1 = gen_host(addr, mask, Q_ARP, dir, type);
  3932. gen_or(b0, b1);
  3933. b0 = gen_host(addr, mask, Q_RARP, dir, type);
  3934. gen_or(b1, b0);
  3935. }
  3936. return b0;
  3937. case Q_IP:
  3938. return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
  3939. case Q_RARP:
  3940. return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
  3941. case Q_ARP:
  3942. return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
  3943. case Q_TCP:
  3944. bpf_error("'tcp' modifier applied to %s", typestr);
  3945. case Q_SCTP:
  3946. bpf_error("'sctp' modifier applied to %s", typestr);
  3947. case Q_UDP:
  3948. bpf_error("'udp' modifier applied to %s", typestr);
  3949. case Q_ICMP:
  3950. bpf_error("'icmp' modifier applied to %s", typestr);
  3951. case Q_IGMP:
  3952. bpf_error("'igmp' modifier applied to %s", typestr);
  3953. case Q_IGRP:
  3954. bpf_error("'igrp' modifier applied to %s", typestr);
  3955. case Q_PIM:
  3956. bpf_error("'pim' modifier applied to %s", typestr);
  3957. case Q_VRRP:
  3958. bpf_error("'vrrp' modifier applied to %s", typestr);
  3959. case Q_CARP:
  3960. bpf_error("'carp' modifier applied to %s", typestr);
  3961. case Q_ATALK:
  3962. bpf_error("ATALK host filtering not implemented");
  3963. case Q_AARP:
  3964. bpf_error("AARP host filtering not implemented");
  3965. case Q_DECNET:
  3966. return gen_dnhostop(addr, dir);
  3967. case Q_SCA:
  3968. bpf_error("SCA host filtering not implemented");
  3969. case Q_LAT:
  3970. bpf_error("LAT host filtering not implemented");
  3971. case Q_MOPDL:
  3972. bpf_error("MOPDL host filtering not implemented");
  3973. case Q_MOPRC:
  3974. bpf_error("MOPRC host filtering not implemented");
  3975. case Q_IPV6:
  3976. bpf_error("'ip6' modifier applied to ip host");
  3977. case Q_ICMPV6:
  3978. bpf_error("'icmp6' modifier applied to %s", typestr);
  3979. case Q_AH:
  3980. bpf_error("'ah' modifier applied to %s", typestr);
  3981. case Q_ESP:
  3982. bpf_error("'esp' modifier applied to %s", typestr);
  3983. case Q_ISO:
  3984. bpf_error("ISO host filtering not implemented");
  3985. case Q_ESIS:
  3986. bpf_error("'esis' modifier applied to %s", typestr);
  3987. case Q_ISIS:
  3988. bpf_error("'isis' modifier applied to %s", typestr);
  3989. case Q_CLNP:
  3990. bpf_error("'clnp' modifier applied to %s", typestr);
  3991. case Q_STP:
  3992. bpf_error("'stp' modifier applied to %s", typestr);
  3993. case Q_IPX:
  3994. bpf_error("IPX host filtering not implemented");
  3995. case Q_NETBEUI:
  3996. bpf_error("'netbeui' modifier applied to %s", typestr);
  3997. case Q_RADIO:
  3998. bpf_error("'radio' modifier applied to %s", typestr);
  3999. default:
  4000. abort();
  4001. }
  4002. /* NOTREACHED */
  4003. }
  4004. #ifdef INET6
  4005. static struct block *
  4006. gen_host6(addr, mask, proto, dir, type)
  4007. struct in6_addr *addr;
  4008. struct in6_addr *mask;
  4009. int proto;
  4010. int dir;
  4011. int type;
  4012. {
  4013. const char *typestr;
  4014. if (type == Q_NET)
  4015. typestr = "net";
  4016. else
  4017. typestr = "host";
  4018. switch (proto) {
  4019. case Q_DEFAULT:
  4020. return gen_host6(addr, mask, Q_IPV6, dir, type);
  4021. case Q_LINK:
  4022. bpf_error("link-layer modifier applied to ip6 %s", typestr);
  4023. case Q_IP:
  4024. bpf_error("'ip' modifier applied to ip6 %s", typestr);
  4025. case Q_RARP:
  4026. bpf_error("'rarp' modifier applied to ip6 %s", typestr);
  4027. case Q_ARP:
  4028. bpf_error("'arp' modifier applied to ip6 %s", typestr);
  4029. case Q_SCTP:
  4030. bpf_error("'sctp' modifier applied to %s", typestr);
  4031. case Q_TCP:
  4032. bpf_error("'tcp' modifier applied to %s", typestr);
  4033. case Q_UDP:
  4034. bpf_error("'udp' modifier applied to %s", typestr);
  4035. case Q_ICMP:
  4036. bpf_error("'icmp' modifier applied to %s", typestr);
  4037. case Q_IGMP:
  4038. bpf_error("'igmp' modifier applied to %s", typestr);
  4039. case Q_IGRP:
  4040. bpf_error("'igrp' modifier applied to %s", typestr);
  4041. case Q_PIM:
  4042. bpf_error("'pim' modifier applied to %s", typestr);
  4043. case Q_VRRP:
  4044. bpf_error("'vrrp' modifier applied to %s", typestr);
  4045. case Q_CARP:
  4046. bpf_error("'carp' modifier applied to %s", typestr);
  4047. case Q_ATALK:
  4048. bpf_error("ATALK host filtering not implemented");
  4049. case Q_AARP:
  4050. bpf_error("AARP host filtering not implemented");
  4051. case Q_DECNET:
  4052. bpf_error("'decnet' modifier applied to ip6 %s", typestr);
  4053. case Q_SCA:
  4054. bpf_error("SCA host filtering not implemented");
  4055. case Q_LAT:
  4056. bpf_error("LAT host filtering not implemented");
  4057. case Q_MOPDL:
  4058. bpf_error("MOPDL host filtering not implemented");
  4059. case Q_MOPRC:
  4060. bpf_error("MOPRC host filtering not implemented");
  4061. case Q_IPV6:
  4062. return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
  4063. case Q_ICMPV6:
  4064. bpf_error("'icmp6' modifier applied to %s", typestr);
  4065. case Q_AH:
  4066. bpf_error("'ah' modifier applied to %s", typestr);
  4067. case Q_ESP:
  4068. bpf_error("'esp' modifier applied to %s", typestr);
  4069. case Q_ISO:
  4070. bpf_error("ISO host filtering not implemented");
  4071. case Q_ESIS:
  4072. bpf_error("'esis' modifier applied to %s", typestr);
  4073. case Q_ISIS:
  4074. bpf_error("'isis' modifier applied to %s", typestr);
  4075. case Q_CLNP:
  4076. bpf_error("'clnp' modifier applied to %s", typestr);
  4077. case Q_STP:
  4078. bpf_error("'stp' modifier applied to %s", typestr);
  4079. case Q_IPX:
  4080. bpf_error("IPX host filtering not implemented");
  4081. case Q_NETBEUI:
  4082. bpf_error("'netbeui' modifier applied to %s", typestr);
  4083. case Q_RADIO:
  4084. bpf_error("'radio' modifier applied to %s", typestr);
  4085. default:
  4086. abort();
  4087. }
  4088. /* NOTREACHED */
  4089. }
  4090. #endif
  4091. #ifndef INET6
  4092. static struct block *
  4093. gen_gateway(eaddr, alist, proto, dir)
  4094. const u_char *eaddr;
  4095. bpf_u_int32 **alist;
  4096. int proto;
  4097. int dir;
  4098. {
  4099. struct block *b0, *b1, *tmp;
  4100. if (dir != 0)
  4101. bpf_error("direction applied to 'gateway'");
  4102. switch (proto) {
  4103. case Q_DEFAULT:
  4104. case Q_IP:
  4105. case Q_ARP:
  4106. case Q_RARP:
  4107. switch (linktype) {
  4108. case DLT_EN10MB:
  4109. case DLT_NETANALYZER:
  4110. case DLT_NETANALYZER_TRANSPARENT:
  4111. b0 = gen_ehostop(eaddr, Q_OR);
  4112. break;
  4113. case DLT_FDDI:
  4114. b0 = gen_fhostop(eaddr, Q_OR);
  4115. break;
  4116. case DLT_IEEE802:
  4117. b0 = gen_thostop(eaddr, Q_OR);
  4118. break;
  4119. case DLT_IEEE802_11:
  4120. case DLT_PRISM_HEADER:
  4121. case DLT_IEEE802_11_RADIO_AVS:
  4122. case DLT_IEEE802_11_RADIO:
  4123. case DLT_PPI:
  4124. b0 = gen_wlanhostop(eaddr, Q_OR);
  4125. break;
  4126. case DLT_SUNATM:
  4127. if (!is_lane)
  4128. bpf_error(
  4129. "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
  4130. /*
  4131. * Check that the packet doesn't begin with an
  4132. * LE Control marker. (We've already generated
  4133. * a test for LANE.)
  4134. */
  4135. b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
  4136. BPF_H, 0xFF00);
  4137. gen_not(b1);
  4138. /*
  4139. * Now check the MAC address.
  4140. */
  4141. b0 = gen_ehostop(eaddr, Q_OR);
  4142. gen_and(b1, b0);
  4143. break;
  4144. case DLT_IP_OVER_FC:
  4145. b0 = gen_ipfchostop(eaddr, Q_OR);
  4146. break;
  4147. default:
  4148. bpf_error(
  4149. "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
  4150. }
  4151. b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST);
  4152. while (*alist) {
  4153. tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR,
  4154. Q_HOST);
  4155. gen_or(b1, tmp);
  4156. b1 = tmp;
  4157. }
  4158. gen_not(b1);
  4159. gen_and(b0, b1);
  4160. return b1;
  4161. }
  4162. bpf_error("illegal modifier of 'gateway'");
  4163. /* NOTREACHED */
  4164. }
  4165. #endif
  4166. struct block *
  4167. gen_proto_abbrev(proto)
  4168. int proto;
  4169. {
  4170. struct block *b0;
  4171. struct block *b1;
  4172. switch (proto) {
  4173. case Q_SCTP:
  4174. b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
  4175. b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
  4176. gen_or(b0, b1);
  4177. break;
  4178. case Q_TCP:
  4179. b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
  4180. b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
  4181. gen_or(b0, b1);
  4182. break;
  4183. case Q_UDP:
  4184. b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
  4185. b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
  4186. gen_or(b0, b1);
  4187. break;
  4188. case Q_ICMP:
  4189. b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
  4190. break;
  4191. #ifndef IPPROTO_IGMP
  4192. #define IPPROTO_IGMP 2
  4193. #endif
  4194. case Q_IGMP:
  4195. b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
  4196. break;
  4197. #ifndef IPPROTO_IGRP
  4198. #define IPPROTO_IGRP 9
  4199. #endif
  4200. case Q_IGRP:
  4201. b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
  4202. break;
  4203. #ifndef IPPROTO_PIM
  4204. #define IPPROTO_PIM 103
  4205. #endif
  4206. case Q_PIM:
  4207. b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
  4208. b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
  4209. gen_or(b0, b1);
  4210. break;
  4211. #ifndef IPPROTO_VRRP
  4212. #define IPPROTO_VRRP 112
  4213. #endif
  4214. case Q_VRRP:
  4215. b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
  4216. break;
  4217. #ifndef IPPROTO_CARP
  4218. #define IPPROTO_CARP 112
  4219. #endif
  4220. case Q_CARP:
  4221. b1 = gen_proto(IPPROTO_CARP, Q_IP, Q_DEFAULT);
  4222. break;
  4223. case Q_IP:
  4224. b1 = gen_linktype(ETHERTYPE_IP);
  4225. break;
  4226. case Q_ARP:
  4227. b1 = gen_linktype(ETHERTYPE_ARP);
  4228. break;
  4229. case Q_RARP:
  4230. b1 = gen_linktype(ETHERTYPE_REVARP);
  4231. break;
  4232. case Q_LINK:
  4233. bpf_error("link layer applied in wrong context");
  4234. case Q_ATALK:
  4235. b1 = gen_linktype(ETHERTYPE_ATALK);
  4236. break;
  4237. case Q_AARP:
  4238. b1 = gen_linktype(ETHERTYPE_AARP);
  4239. break;
  4240. case Q_DECNET:
  4241. b1 = gen_linktype(ETHERTYPE_DN);
  4242. break;
  4243. case Q_SCA:
  4244. b1 = gen_linktype(ETHERTYPE_SCA);
  4245. break;
  4246. case Q_LAT:
  4247. b1 = gen_linktype(ETHERTYPE_LAT);
  4248. break;
  4249. case Q_MOPDL:
  4250. b1 = gen_linktype(ETHERTYPE_MOPDL);
  4251. break;
  4252. case Q_MOPRC:
  4253. b1 = gen_linktype(ETHERTYPE_MOPRC);
  4254. break;
  4255. case Q_IPV6:
  4256. b1 = gen_linktype(ETHERTYPE_IPV6);
  4257. break;
  4258. #ifndef IPPROTO_ICMPV6
  4259. #define IPPROTO_ICMPV6 58
  4260. #endif
  4261. case Q_ICMPV6:
  4262. b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
  4263. break;
  4264. #ifndef IPPROTO_AH
  4265. #define IPPROTO_AH 51
  4266. #endif
  4267. case Q_AH:
  4268. b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
  4269. b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
  4270. gen_or(b0, b1);
  4271. break;
  4272. #ifndef IPPROTO_ESP
  4273. #define IPPROTO_ESP 50
  4274. #endif
  4275. case Q_ESP:
  4276. b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
  4277. b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
  4278. gen_or(b0, b1);
  4279. break;
  4280. case Q_ISO:
  4281. b1 = gen_linktype(LLCSAP_ISONS);
  4282. break;
  4283. case Q_ESIS:
  4284. b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
  4285. break;
  4286. case Q_ISIS:
  4287. b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
  4288. break;
  4289. case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
  4290. b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
  4291. b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
  4292. gen_or(b0, b1);
  4293. b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
  4294. gen_or(b0, b1);
  4295. b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
  4296. gen_or(b0, b1);
  4297. b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
  4298. gen_or(b0, b1);
  4299. break;
  4300. case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
  4301. b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
  4302. b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
  4303. gen_or(b0, b1);
  4304. b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
  4305. gen_or(b0, b1);
  4306. b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
  4307. gen_or(b0, b1);
  4308. b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
  4309. gen_or(b0, b1);
  4310. break;
  4311. case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
  4312. b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
  4313. b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
  4314. gen_or(b0, b1);
  4315. b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
  4316. gen_or(b0, b1);
  4317. break;
  4318. case Q_ISIS_LSP:
  4319. b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
  4320. b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
  4321. gen_or(b0, b1);
  4322. break;
  4323. case Q_ISIS_SNP:
  4324. b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
  4325. b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
  4326. gen_or(b0, b1);
  4327. b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
  4328. gen_or(b0, b1);
  4329. b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
  4330. gen_or(b0, b1);
  4331. break;
  4332. case Q_ISIS_CSNP:
  4333. b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
  4334. b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
  4335. gen_or(b0, b1);
  4336. break;
  4337. case Q_ISIS_PSNP:
  4338. b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
  4339. b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
  4340. gen_or(b0, b1);
  4341. break;
  4342. case Q_CLNP:
  4343. b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
  4344. break;
  4345. case Q_STP:
  4346. b1 = gen_linktype(LLCSAP_8021D);
  4347. break;
  4348. case Q_IPX:
  4349. b1 = gen_linktype(LLCSAP_IPX);
  4350. break;
  4351. case Q_NETBEUI:
  4352. b1 = gen_linktype(LLCSAP_NETBEUI);
  4353. break;
  4354. case Q_RADIO:
  4355. bpf_error("'radio' is not a valid protocol type");
  4356. default:
  4357. abort();
  4358. }
  4359. return b1;
  4360. }
  4361. static struct block *
  4362. gen_ipfrag()
  4363. {
  4364. struct slist *s;
  4365. struct block *b;
  4366. /* not IPv4 frag other than the first frag */
  4367. s = gen_load_a(OR_NET, 6, BPF_H);
  4368. b = new_block(JMP(BPF_JSET));
  4369. b->s.k = 0x1fff;
  4370. b->stmts = s;
  4371. gen_not(b);
  4372. return b;
  4373. }
  4374. /*
  4375. * Generate a comparison to a port value in the transport-layer header
  4376. * at the specified offset from the beginning of that header.
  4377. *
  4378. * XXX - this handles a variable-length prefix preceding the link-layer
  4379. * header, such as the radiotap or AVS radio prefix, but doesn't handle
  4380. * variable-length link-layer headers (such as Token Ring or 802.11
  4381. * headers).
  4382. */
  4383. static struct block *
  4384. gen_portatom(off, v)
  4385. int off;
  4386. bpf_int32 v;
  4387. {
  4388. return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
  4389. }
  4390. static struct block *
  4391. gen_portatom6(off, v)
  4392. int off;
  4393. bpf_int32 v;
  4394. {
  4395. return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
  4396. }
  4397. struct block *
  4398. gen_portop(port, proto, dir)
  4399. int port, proto, dir;
  4400. {
  4401. struct block *b0, *b1, *tmp;
  4402. /* ip proto 'proto' and not a fragment other than the first fragment */
  4403. tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
  4404. b0 = gen_ipfrag();
  4405. gen_and(tmp, b0);
  4406. switch (dir) {
  4407. case Q_SRC:
  4408. b1 = gen_portatom(0, (bpf_int32)port);
  4409. break;
  4410. case Q_DST:
  4411. b1 = gen_portatom(2, (bpf_int32)port);
  4412. break;
  4413. case Q_OR:
  4414. case Q_DEFAULT:
  4415. tmp = gen_portatom(0, (bpf_int32)port);
  4416. b1 = gen_portatom(2, (bpf_int32)port);
  4417. gen_or(tmp, b1);
  4418. break;
  4419. case Q_AND:
  4420. tmp = gen_portatom(0, (bpf_int32)port);
  4421. b1 = gen_portatom(2, (bpf_int32)port);
  4422. gen_and(tmp, b1);
  4423. break;
  4424. default:
  4425. abort();
  4426. }
  4427. gen_and(b0, b1);
  4428. return b1;
  4429. }
  4430. static struct block *
  4431. gen_port(port, ip_proto, dir)
  4432. int port;
  4433. int ip_proto;
  4434. int dir;
  4435. {
  4436. struct block *b0, *b1, *tmp;
  4437. /*
  4438. * ether proto ip
  4439. *
  4440. * For FDDI, RFC 1188 says that SNAP encapsulation is used,
  4441. * not LLC encapsulation with LLCSAP_IP.
  4442. *
  4443. * For IEEE 802 networks - which includes 802.5 token ring
  4444. * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
  4445. * says that SNAP encapsulation is used, not LLC encapsulation
  4446. * with LLCSAP_IP.
  4447. *
  4448. * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
  4449. * RFC 2225 say that SNAP encapsulation is used, not LLC
  4450. * encapsulation with LLCSAP_IP.
  4451. *
  4452. * So we always check for ETHERTYPE_IP.
  4453. */
  4454. b0 = gen_linktype(ETHERTYPE_IP);
  4455. switch (ip_proto) {
  4456. case IPPROTO_UDP:
  4457. case IPPROTO_TCP:
  4458. case IPPROTO_SCTP:
  4459. b1 = gen_portop(port, ip_proto, dir);
  4460. break;
  4461. case PROTO_UNDEF:
  4462. tmp = gen_portop(port, IPPROTO_TCP, dir);
  4463. b1 = gen_portop(port, IPPROTO_UDP, dir);
  4464. gen_or(tmp, b1);
  4465. tmp = gen_portop(port, IPPROTO_SCTP, dir);
  4466. gen_or(tmp, b1);
  4467. break;
  4468. default:
  4469. abort();
  4470. }
  4471. gen_and(b0, b1);
  4472. return b1;
  4473. }
  4474. struct block *
  4475. gen_portop6(port, proto, dir)
  4476. int port, proto, dir;
  4477. {
  4478. struct block *b0, *b1, *tmp;
  4479. /* ip6 proto 'proto' */
  4480. /* XXX - catch the first fragment of a fragmented packet? */
  4481. b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
  4482. switch (dir) {
  4483. case Q_SRC:
  4484. b1 = gen_portatom6(0, (bpf_int32)port);
  4485. break;
  4486. case Q_DST:
  4487. b1 = gen_portatom6(2, (bpf_int32)port);
  4488. break;
  4489. case Q_OR:
  4490. case Q_DEFAULT:
  4491. tmp = gen_portatom6(0, (bpf_int32)port);
  4492. b1 = gen_portatom6(2, (bpf_int32)port);
  4493. gen_or(tmp, b1);
  4494. break;
  4495. case Q_AND:
  4496. tmp = gen_portatom6(0, (bpf_int32)port);
  4497. b1 = gen_portatom6(2, (bpf_int32)port);
  4498. gen_and(tmp, b1);
  4499. break;
  4500. default:
  4501. abort();
  4502. }
  4503. gen_and(b0, b1);
  4504. return b1;
  4505. }
  4506. static struct block *
  4507. gen_port6(port, ip_proto, dir)
  4508. int port;
  4509. int ip_proto;
  4510. int dir;
  4511. {
  4512. struct block *b0, *b1, *tmp;
  4513. /* link proto ip6 */
  4514. b0 = gen_linktype(ETHERTYPE_IPV6);
  4515. switch (ip_proto) {
  4516. case IPPROTO_UDP:
  4517. case IPPROTO_TCP:
  4518. case IPPROTO_SCTP:
  4519. b1 = gen_portop6(port, ip_proto, dir);
  4520. break;
  4521. case PROTO_UNDEF:
  4522. tmp = gen_portop6(port, IPPROTO_TCP, dir);
  4523. b1 = gen_portop6(port, IPPROTO_UDP, dir);
  4524. gen_or(tmp, b1);
  4525. tmp = gen_portop6(port, IPPROTO_SCTP, dir);
  4526. gen_or(tmp, b1);
  4527. break;
  4528. default:
  4529. abort();
  4530. }
  4531. gen_and(b0, b1);
  4532. return b1;
  4533. }
  4534. /* gen_portrange code */
  4535. static struct block *
  4536. gen_portrangeatom(off, v1, v2)
  4537. int off;
  4538. bpf_int32 v1, v2;
  4539. {
  4540. struct block *b1, *b2;
  4541. if (v1 > v2) {
  4542. /*
  4543. * Reverse the order of the ports, so v1 is the lower one.
  4544. */
  4545. bpf_int32 vtemp;
  4546. vtemp = v1;
  4547. v1 = v2;
  4548. v2 = vtemp;
  4549. }
  4550. b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
  4551. b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
  4552. gen_and(b1, b2);
  4553. return b2;
  4554. }
  4555. struct block *
  4556. gen_portrangeop(port1, port2, proto, dir)
  4557. int port1, port2;
  4558. int proto;
  4559. int dir;
  4560. {
  4561. struct block *b0, *b1, *tmp;
  4562. /* ip proto 'proto' and not a fragment other than the first fragment */
  4563. tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
  4564. b0 = gen_ipfrag();
  4565. gen_and(tmp, b0);
  4566. switch (dir) {
  4567. case Q_SRC:
  4568. b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
  4569. break;
  4570. case Q_DST:
  4571. b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
  4572. break;
  4573. case Q_OR:
  4574. case Q_DEFAULT:
  4575. tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
  4576. b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
  4577. gen_or(tmp, b1);
  4578. break;
  4579. case Q_AND:
  4580. tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
  4581. b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
  4582. gen_and(tmp, b1);
  4583. break;
  4584. default:
  4585. abort();
  4586. }
  4587. gen_and(b0, b1);
  4588. return b1;
  4589. }
  4590. static struct block *
  4591. gen_portrange(port1, port2, ip_proto, dir)
  4592. int port1, port2;
  4593. int ip_proto;
  4594. int dir;
  4595. {
  4596. struct block *b0, *b1, *tmp;
  4597. /* link proto ip */
  4598. b0 = gen_linktype(ETHERTYPE_IP);
  4599. switch (ip_proto) {
  4600. case IPPROTO_UDP:
  4601. case IPPROTO_TCP:
  4602. case IPPROTO_SCTP:
  4603. b1 = gen_portrangeop(port1, port2, ip_proto, dir);
  4604. break;
  4605. case PROTO_UNDEF:
  4606. tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
  4607. b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
  4608. gen_or(tmp, b1);
  4609. tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
  4610. gen_or(tmp, b1);
  4611. break;
  4612. default:
  4613. abort();
  4614. }
  4615. gen_and(b0, b1);
  4616. return b1;
  4617. }
  4618. static struct block *
  4619. gen_portrangeatom6(off, v1, v2)
  4620. int off;
  4621. bpf_int32 v1, v2;
  4622. {
  4623. struct block *b1, *b2;
  4624. if (v1 > v2) {
  4625. /*
  4626. * Reverse the order of the ports, so v1 is the lower one.
  4627. */
  4628. bpf_int32 vtemp;
  4629. vtemp = v1;
  4630. v1 = v2;
  4631. v2 = vtemp;
  4632. }
  4633. b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
  4634. b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
  4635. gen_and(b1, b2);
  4636. return b2;
  4637. }
  4638. struct block *
  4639. gen_portrangeop6(port1, port2, proto, dir)
  4640. int port1, port2;
  4641. int proto;
  4642. int dir;
  4643. {
  4644. struct block *b0, *b1, *tmp;
  4645. /* ip6 proto 'proto' */
  4646. /* XXX - catch the first fragment of a fragmented packet? */
  4647. b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
  4648. switch (dir) {
  4649. case Q_SRC:
  4650. b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
  4651. break;
  4652. case Q_DST:
  4653. b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
  4654. break;
  4655. case Q_OR:
  4656. case Q_DEFAULT:
  4657. tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
  4658. b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
  4659. gen_or(tmp, b1);
  4660. break;
  4661. case Q_AND:
  4662. tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
  4663. b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
  4664. gen_and(tmp, b1);
  4665. break;
  4666. default:
  4667. abort();
  4668. }
  4669. gen_and(b0, b1);
  4670. return b1;
  4671. }
  4672. static struct block *
  4673. gen_portrange6(port1, port2, ip_proto, dir)
  4674. int port1, port2;
  4675. int ip_proto;
  4676. int dir;
  4677. {
  4678. struct block *b0, *b1, *tmp;
  4679. /* link proto ip6 */
  4680. b0 = gen_linktype(ETHERTYPE_IPV6);
  4681. switch (ip_proto) {
  4682. case IPPROTO_UDP:
  4683. case IPPROTO_TCP:
  4684. case IPPROTO_SCTP:
  4685. b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
  4686. break;
  4687. case PROTO_UNDEF:
  4688. tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
  4689. b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
  4690. gen_or(tmp, b1);
  4691. tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
  4692. gen_or(tmp, b1);
  4693. break;
  4694. default:
  4695. abort();
  4696. }
  4697. gen_and(b0, b1);
  4698. return b1;
  4699. }
  4700. static int
  4701. lookup_proto(name, proto)
  4702. register const char *name;
  4703. register int proto;
  4704. {
  4705. register int v;
  4706. switch (proto) {
  4707. case Q_DEFAULT:
  4708. case Q_IP:
  4709. case Q_IPV6:
  4710. v = pcap_nametoproto(name);
  4711. if (v == PROTO_UNDEF)
  4712. bpf_error("unknown ip proto '%s'", name);
  4713. break;
  4714. case Q_LINK:
  4715. /* XXX should look up h/w protocol type based on linktype */
  4716. v = pcap_nametoeproto(name);
  4717. if (v == PROTO_UNDEF) {
  4718. v = pcap_nametollc(name);
  4719. if (v == PROTO_UNDEF)
  4720. bpf_error("unknown ether proto '%s'", name);
  4721. }
  4722. break;
  4723. case Q_ISO:
  4724. if (strcmp(name, "esis") == 0)
  4725. v = ISO9542_ESIS;
  4726. else if (strcmp(name, "isis") == 0)
  4727. v = ISO10589_ISIS;
  4728. else if (strcmp(name, "clnp") == 0)
  4729. v = ISO8473_CLNP;
  4730. else
  4731. bpf_error("unknown osi proto '%s'", name);
  4732. break;
  4733. default:
  4734. v = PROTO_UNDEF;
  4735. break;
  4736. }
  4737. return v;
  4738. }
  4739. #if 0
  4740. struct stmt *
  4741. gen_joinsp(s, n)
  4742. struct stmt **s;
  4743. int n;
  4744. {
  4745. return NULL;
  4746. }
  4747. #endif
  4748. static struct block *
  4749. gen_protochain(v, proto, dir)
  4750. int v;
  4751. int proto;
  4752. int dir;
  4753. {
  4754. #ifdef NO_PROTOCHAIN
  4755. return gen_proto(v, proto, dir);
  4756. #else
  4757. struct block *b0, *b;
  4758. struct slist *s[100];
  4759. int fix2, fix3, fix4, fix5;
  4760. int ahcheck, again, end;
  4761. int i, max;
  4762. int reg2 = alloc_reg();
  4763. memset(s, 0, sizeof(s));
  4764. fix2 = fix3 = fix4 = fix5 = 0;
  4765. switch (proto) {
  4766. case Q_IP:
  4767. case Q_IPV6:
  4768. break;
  4769. case Q_DEFAULT:
  4770. b0 = gen_protochain(v, Q_IP, dir);
  4771. b = gen_protochain(v, Q_IPV6, dir);
  4772. gen_or(b0, b);
  4773. return b;
  4774. default:
  4775. bpf_error("bad protocol applied for 'protochain'");
  4776. /*NOTREACHED*/
  4777. }
  4778. /*
  4779. * We don't handle variable-length prefixes before the link-layer
  4780. * header, or variable-length link-layer headers, here yet.
  4781. * We might want to add BPF instructions to do the protochain
  4782. * work, to simplify that and, on platforms that have a BPF
  4783. * interpreter with the new instructions, let the filtering
  4784. * be done in the kernel. (We already require a modified BPF
  4785. * engine to do the protochain stuff, to support backward
  4786. * branches, and backward branch support is unlikely to appear
  4787. * in kernel BPF engines.)
  4788. */
  4789. switch (linktype) {
  4790. case DLT_IEEE802_11:
  4791. case DLT_PRISM_HEADER:
  4792. case DLT_IEEE802_11_RADIO_AVS:
  4793. case DLT_IEEE802_11_RADIO:
  4794. case DLT_PPI:
  4795. bpf_error("'protochain' not supported with 802.11");
  4796. }
  4797. no_optimize = 1; /*this code is not compatible with optimzer yet */
  4798. /*
  4799. * s[0] is a dummy entry to protect other BPF insn from damage
  4800. * by s[fix] = foo with uninitialized variable "fix". It is somewhat
  4801. * hard to find interdependency made by jump table fixup.
  4802. */
  4803. i = 0;
  4804. s[i] = new_stmt(0); /*dummy*/
  4805. i++;
  4806. switch (proto) {
  4807. case Q_IP:
  4808. b0 = gen_linktype(ETHERTYPE_IP);
  4809. /* A = ip->ip_p */
  4810. s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
  4811. s[i]->s.k = off_macpl + off_nl + 9;
  4812. i++;
  4813. /* X = ip->ip_hl << 2 */
  4814. s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
  4815. s[i]->s.k = off_macpl + off_nl;
  4816. i++;
  4817. break;
  4818. case Q_IPV6:
  4819. b0 = gen_linktype(ETHERTYPE_IPV6);
  4820. /* A = ip6->ip_nxt */
  4821. s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
  4822. s[i]->s.k = off_macpl + off_nl + 6;
  4823. i++;
  4824. /* X = sizeof(struct ip6_hdr) */
  4825. s[i] = new_stmt(BPF_LDX|BPF_IMM);
  4826. s[i]->s.k = 40;
  4827. i++;
  4828. break;
  4829. default:
  4830. bpf_error("unsupported proto to gen_protochain");
  4831. /*NOTREACHED*/
  4832. }
  4833. /* again: if (A == v) goto end; else fall through; */
  4834. again = i;
  4835. s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
  4836. s[i]->s.k = v;
  4837. s[i]->s.jt = NULL; /*later*/
  4838. s[i]->s.jf = NULL; /*update in next stmt*/
  4839. fix5 = i;
  4840. i++;
  4841. #ifndef IPPROTO_NONE
  4842. #define IPPROTO_NONE 59
  4843. #endif
  4844. /* if (A == IPPROTO_NONE) goto end */
  4845. s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
  4846. s[i]->s.jt = NULL; /*later*/
  4847. s[i]->s.jf = NULL; /*update in next stmt*/
  4848. s[i]->s.k = IPPROTO_NONE;
  4849. s[fix5]->s.jf = s[i];
  4850. fix2 = i;
  4851. i++;
  4852. if (proto == Q_IPV6) {
  4853. int v6start, v6end, v6advance, j;
  4854. v6start = i;
  4855. /* if (A == IPPROTO_HOPOPTS) goto v6advance */
  4856. s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
  4857. s[i]->s.jt = NULL; /*later*/
  4858. s[i]->s.jf = NULL; /*update in next stmt*/
  4859. s[i]->s.k = IPPROTO_HOPOPTS;
  4860. s[fix2]->s.jf = s[i];
  4861. i++;
  4862. /* if (A == IPPROTO_DSTOPTS) goto v6advance */
  4863. s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
  4864. s[i]->s.jt = NULL; /*later*/
  4865. s[i]->s.jf = NULL; /*update in next stmt*/
  4866. s[i]->s.k = IPPROTO_DSTOPTS;
  4867. i++;
  4868. /* if (A == IPPROTO_ROUTING) goto v6advance */
  4869. s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
  4870. s[i]->s.jt = NULL; /*later*/
  4871. s[i]->s.jf = NULL; /*update in next stmt*/
  4872. s[i]->s.k = IPPROTO_ROUTING;
  4873. i++;
  4874. /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
  4875. s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
  4876. s[i]->s.jt = NULL; /*later*/
  4877. s[i]->s.jf = NULL; /*later*/
  4878. s[i]->s.k = IPPROTO_FRAGMENT;
  4879. fix3 = i;
  4880. v6end = i;
  4881. i++;
  4882. /* v6advance: */
  4883. v6advance = i;
  4884. /*
  4885. * in short,
  4886. * A = P[X + packet head];
  4887. * X = X + (P[X + packet head + 1] + 1) * 8;
  4888. */
  4889. /* A = P[X + packet head] */
  4890. s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
  4891. s[i]->s.k = off_macpl + off_nl;
  4892. i++;
  4893. /* MEM[reg2] = A */
  4894. s[i] = new_stmt(BPF_ST);
  4895. s[i]->s.k = reg2;
  4896. i++;
  4897. /* A = P[X + packet head + 1]; */
  4898. s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
  4899. s[i]->s.k = off_macpl + off_nl + 1;
  4900. i++;
  4901. /* A += 1 */
  4902. s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
  4903. s[i]->s.k = 1;
  4904. i++;
  4905. /* A *= 8 */
  4906. s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
  4907. s[i]->s.k = 8;
  4908. i++;
  4909. /* A += X */
  4910. s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_X);
  4911. s[i]->s.k = 0;
  4912. i++;
  4913. /* X = A; */
  4914. s[i] = new_stmt(BPF_MISC|BPF_TAX);
  4915. i++;
  4916. /* A = MEM[reg2] */
  4917. s[i] = new_stmt(BPF_LD|BPF_MEM);
  4918. s[i]->s.k = reg2;
  4919. i++;
  4920. /* goto again; (must use BPF_JA for backward jump) */
  4921. s[i] = new_stmt(BPF_JMP|BPF_JA);
  4922. s[i]->s.k = again - i - 1;
  4923. s[i - 1]->s.jf = s[i];
  4924. i++;
  4925. /* fixup */
  4926. for (j = v6start; j <= v6end; j++)
  4927. s[j]->s.jt = s[v6advance];
  4928. } else {
  4929. /* nop */
  4930. s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
  4931. s[i]->s.k = 0;
  4932. s[fix2]->s.jf = s[i];
  4933. i++;
  4934. }
  4935. /* ahcheck: */
  4936. ahcheck = i;
  4937. /* if (A == IPPROTO_AH) then fall through; else goto end; */
  4938. s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
  4939. s[i]->s.jt = NULL; /*later*/
  4940. s[i]->s.jf = NULL; /*later*/
  4941. s[i]->s.k = IPPROTO_AH;
  4942. if (fix3)
  4943. s[fix3]->s.jf = s[ahcheck];
  4944. fix4 = i;
  4945. i++;
  4946. /*
  4947. * in short,
  4948. * A = P[X];
  4949. * X = X + (P[X + 1] + 2) * 4;
  4950. */
  4951. /* A = X */
  4952. s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
  4953. i++;
  4954. /* A = P[X + packet head]; */
  4955. s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
  4956. s[i]->s.k = off_macpl + off_nl;
  4957. i++;
  4958. /* MEM[reg2] = A */
  4959. s[i] = new_stmt(BPF_ST);
  4960. s[i]->s.k = reg2;
  4961. i++;
  4962. /* A = X */
  4963. s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
  4964. i++;
  4965. /* A += 1 */
  4966. s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
  4967. s[i]->s.k = 1;
  4968. i++;
  4969. /* X = A */
  4970. s[i] = new_stmt(BPF_MISC|BPF_TAX);
  4971. i++;
  4972. /* A = P[X + packet head] */
  4973. s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
  4974. s[i]->s.k = off_macpl + off_nl;
  4975. i++;
  4976. /* A += 2 */
  4977. s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
  4978. s[i]->s.k = 2;
  4979. i++;
  4980. /* A *= 4 */
  4981. s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
  4982. s[i]->s.k = 4;
  4983. i++;
  4984. /* X = A; */
  4985. s[i] = new_stmt(BPF_MISC|BPF_TAX);
  4986. i++;
  4987. /* A = MEM[reg2] */
  4988. s[i] = new_stmt(BPF_LD|BPF_MEM);
  4989. s[i]->s.k = reg2;
  4990. i++;
  4991. /* goto again; (must use BPF_JA for backward jump) */
  4992. s[i] = new_stmt(BPF_JMP|BPF_JA);
  4993. s[i]->s.k = again - i - 1;
  4994. i++;
  4995. /* end: nop */
  4996. end = i;
  4997. s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
  4998. s[i]->s.k = 0;
  4999. s[fix2]->s.jt = s[end];
  5000. s[fix4]->s.jf = s[end];
  5001. s[fix5]->s.jt = s[end];
  5002. i++;
  5003. /*
  5004. * make slist chain
  5005. */
  5006. max = i;
  5007. for (i = 0; i < max - 1; i++)
  5008. s[i]->next = s[i + 1];
  5009. s[max - 1]->next = NULL;
  5010. /*
  5011. * emit final check
  5012. */
  5013. b = new_block(JMP(BPF_JEQ));
  5014. b->stmts = s[1]; /*remember, s[0] is dummy*/
  5015. b->s.k = v;
  5016. free_reg(reg2);
  5017. gen_and(b0, b);
  5018. return b;
  5019. #endif
  5020. }
  5021. static struct block *
  5022. gen_check_802_11_data_frame()
  5023. {
  5024. struct slist *s;
  5025. struct block *b0, *b1;
  5026. /*
  5027. * A data frame has the 0x08 bit (b3) in the frame control field set
  5028. * and the 0x04 bit (b2) clear.
  5029. */
  5030. s = gen_load_a(OR_LINK, 0, BPF_B);
  5031. b0 = new_block(JMP(BPF_JSET));
  5032. b0->s.k = 0x08;
  5033. b0->stmts = s;
  5034. s = gen_load_a(OR_LINK, 0, BPF_B);
  5035. b1 = new_block(JMP(BPF_JSET));
  5036. b1->s.k = 0x04;
  5037. b1->stmts = s;
  5038. gen_not(b1);
  5039. gen_and(b1, b0);
  5040. return b0;
  5041. }
  5042. /*
  5043. * Generate code that checks whether the packet is a packet for protocol
  5044. * <proto> and whether the type field in that protocol's header has
  5045. * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
  5046. * IP packet and checks the protocol number in the IP header against <v>.
  5047. *
  5048. * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
  5049. * against Q_IP and Q_IPV6.
  5050. */
  5051. static struct block *
  5052. gen_proto(v, proto, dir)
  5053. int v;
  5054. int proto;
  5055. int dir;
  5056. {
  5057. struct block *b0, *b1;
  5058. #ifndef CHASE_CHAIN
  5059. struct block *b2;
  5060. #endif
  5061. if (dir != Q_DEFAULT)
  5062. bpf_error("direction applied to 'proto'");
  5063. switch (proto) {
  5064. case Q_DEFAULT:
  5065. b0 = gen_proto(v, Q_IP, dir);
  5066. b1 = gen_proto(v, Q_IPV6, dir);
  5067. gen_or(b0, b1);
  5068. return b1;
  5069. case Q_IP:
  5070. /*
  5071. * For FDDI, RFC 1188 says that SNAP encapsulation is used,
  5072. * not LLC encapsulation with LLCSAP_IP.
  5073. *
  5074. * For IEEE 802 networks - which includes 802.5 token ring
  5075. * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
  5076. * says that SNAP encapsulation is used, not LLC encapsulation
  5077. * with LLCSAP_IP.
  5078. *
  5079. * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
  5080. * RFC 2225 say that SNAP encapsulation is used, not LLC
  5081. * encapsulation with LLCSAP_IP.
  5082. *
  5083. * So we always check for ETHERTYPE_IP.
  5084. */
  5085. b0 = gen_linktype(ETHERTYPE_IP);
  5086. #ifndef CHASE_CHAIN
  5087. b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
  5088. #else
  5089. b1 = gen_protochain(v, Q_IP);
  5090. #endif
  5091. gen_and(b0, b1);
  5092. return b1;
  5093. case Q_ISO:
  5094. switch (linktype) {
  5095. case DLT_FRELAY:
  5096. /*
  5097. * Frame Relay packets typically have an OSI
  5098. * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
  5099. * generates code to check for all the OSI
  5100. * NLPIDs, so calling it and then adding a check
  5101. * for the particular NLPID for which we're
  5102. * looking is bogus, as we can just check for
  5103. * the NLPID.
  5104. *
  5105. * What we check for is the NLPID and a frame
  5106. * control field value of UI, i.e. 0x03 followed
  5107. * by the NLPID.
  5108. *
  5109. * XXX - assumes a 2-byte Frame Relay header with
  5110. * DLCI and flags. What if the address is longer?
  5111. *
  5112. * XXX - what about SNAP-encapsulated frames?
  5113. */
  5114. return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
  5115. /*NOTREACHED*/
  5116. break;
  5117. case DLT_C_HDLC:
  5118. /*
  5119. * Cisco uses an Ethertype lookalike - for OSI,
  5120. * it's 0xfefe.
  5121. */
  5122. b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
  5123. /* OSI in C-HDLC is stuffed with a fudge byte */
  5124. b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
  5125. gen_and(b0, b1);
  5126. return b1;
  5127. default:
  5128. b0 = gen_linktype(LLCSAP_ISONS);
  5129. b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
  5130. gen_and(b0, b1);
  5131. return b1;
  5132. }
  5133. case Q_ISIS:
  5134. b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
  5135. /*
  5136. * 4 is the offset of the PDU type relative to the IS-IS
  5137. * header.
  5138. */
  5139. b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
  5140. gen_and(b0, b1);
  5141. return b1;
  5142. case Q_ARP:
  5143. bpf_error("arp does not encapsulate another protocol");
  5144. /* NOTREACHED */
  5145. case Q_RARP:
  5146. bpf_error("rarp does not encapsulate another protocol");
  5147. /* NOTREACHED */
  5148. case Q_ATALK:
  5149. bpf_error("atalk encapsulation is not specifiable");
  5150. /* NOTREACHED */
  5151. case Q_DECNET:
  5152. bpf_error("decnet encapsulation is not specifiable");
  5153. /* NOTREACHED */
  5154. case Q_SCA:
  5155. bpf_error("sca does not encapsulate another protocol");
  5156. /* NOTREACHED */
  5157. case Q_LAT:
  5158. bpf_error("lat does not encapsulate another protocol");
  5159. /* NOTREACHED */
  5160. case Q_MOPRC:
  5161. bpf_error("moprc does not encapsulate another protocol");
  5162. /* NOTREACHED */
  5163. case Q_MOPDL:
  5164. bpf_error("mopdl does not encapsulate another protocol");
  5165. /* NOTREACHED */
  5166. case Q_LINK:
  5167. return gen_linktype(v);
  5168. case Q_UDP:
  5169. bpf_error("'udp proto' is bogus");
  5170. /* NOTREACHED */
  5171. case Q_TCP:
  5172. bpf_error("'tcp proto' is bogus");
  5173. /* NOTREACHED */
  5174. case Q_SCTP:
  5175. bpf_error("'sctp proto' is bogus");
  5176. /* NOTREACHED */
  5177. case Q_ICMP:
  5178. bpf_error("'icmp proto' is bogus");
  5179. /* NOTREACHED */
  5180. case Q_IGMP:
  5181. bpf_error("'igmp proto' is bogus");
  5182. /* NOTREACHED */
  5183. case Q_IGRP:
  5184. bpf_error("'igrp proto' is bogus");
  5185. /* NOTREACHED */
  5186. case Q_PIM:
  5187. bpf_error("'pim proto' is bogus");
  5188. /* NOTREACHED */
  5189. case Q_VRRP:
  5190. bpf_error("'vrrp proto' is bogus");
  5191. /* NOTREACHED */
  5192. case Q_CARP:
  5193. bpf_error("'carp proto' is bogus");
  5194. /* NOTREACHED */
  5195. case Q_IPV6:
  5196. b0 = gen_linktype(ETHERTYPE_IPV6);
  5197. #ifndef CHASE_CHAIN
  5198. /*
  5199. * Also check for a fragment header before the final
  5200. * header.
  5201. */
  5202. b2 = gen_cmp(OR_NET, 6, BPF_B, IPPROTO_FRAGMENT);
  5203. b1 = gen_cmp(OR_NET, 40, BPF_B, (bpf_int32)v);
  5204. gen_and(b2, b1);
  5205. b2 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
  5206. gen_or(b2, b1);
  5207. #else
  5208. b1 = gen_protochain(v, Q_IPV6);
  5209. #endif
  5210. gen_and(b0, b1);
  5211. return b1;
  5212. case Q_ICMPV6:
  5213. bpf_error("'icmp6 proto' is bogus");
  5214. case Q_AH:
  5215. bpf_error("'ah proto' is bogus");
  5216. case Q_ESP:
  5217. bpf_error("'ah proto' is bogus");
  5218. case Q_STP:
  5219. bpf_error("'stp proto' is bogus");
  5220. case Q_IPX:
  5221. bpf_error("'ipx proto' is bogus");
  5222. case Q_NETBEUI:
  5223. bpf_error("'netbeui proto' is bogus");
  5224. case Q_RADIO:
  5225. bpf_error("'radio proto' is bogus");
  5226. default:
  5227. abort();
  5228. /* NOTREACHED */
  5229. }
  5230. /* NOTREACHED */
  5231. }
  5232. struct block *
  5233. gen_scode(name, q)
  5234. register const char *name;
  5235. struct qual q;
  5236. {
  5237. int proto = q.proto;
  5238. int dir = q.dir;
  5239. int tproto;
  5240. u_char *eaddr;
  5241. bpf_u_int32 mask, addr;
  5242. #ifndef INET6
  5243. bpf_u_int32 **alist;
  5244. #else
  5245. int tproto6;
  5246. struct sockaddr_in *sin4;
  5247. struct sockaddr_in6 *sin6;
  5248. struct addrinfo *res, *res0;
  5249. struct in6_addr mask128;
  5250. #endif /*INET6*/
  5251. struct block *b, *tmp;
  5252. int port, real_proto;
  5253. int port1, port2;
  5254. switch (q.addr) {
  5255. case Q_NET:
  5256. addr = pcap_nametonetaddr(name);
  5257. if (addr == 0)
  5258. bpf_error("unknown network '%s'", name);
  5259. /* Left justify network addr and calculate its network mask */
  5260. mask = 0xffffffff;
  5261. while (addr && (addr & 0xff000000) == 0) {
  5262. addr <<= 8;
  5263. mask <<= 8;
  5264. }
  5265. return gen_host(addr, mask, proto, dir, q.addr);
  5266. case Q_DEFAULT:
  5267. case Q_HOST:
  5268. if (proto == Q_LINK) {
  5269. switch (linktype) {
  5270. case DLT_EN10MB:
  5271. case DLT_NETANALYZER:
  5272. case DLT_NETANALYZER_TRANSPARENT:
  5273. eaddr = pcap_ether_hostton(name);
  5274. if (eaddr == NULL)
  5275. bpf_error(
  5276. "unknown ether host '%s'", name);
  5277. b = gen_ehostop(eaddr, dir);
  5278. free(eaddr);
  5279. return b;
  5280. case DLT_FDDI:
  5281. eaddr = pcap_ether_hostton(name);
  5282. if (eaddr == NULL)
  5283. bpf_error(
  5284. "unknown FDDI host '%s'", name);
  5285. b = gen_fhostop(eaddr, dir);
  5286. free(eaddr);
  5287. return b;
  5288. case DLT_IEEE802:
  5289. eaddr = pcap_ether_hostton(name);
  5290. if (eaddr == NULL)
  5291. bpf_error(
  5292. "unknown token ring host '%s'", name);
  5293. b = gen_thostop(eaddr, dir);
  5294. free(eaddr);
  5295. return b;
  5296. case DLT_IEEE802_11:
  5297. case DLT_PRISM_HEADER:
  5298. case DLT_IEEE802_11_RADIO_AVS:
  5299. case DLT_IEEE802_11_RADIO:
  5300. case DLT_PPI:
  5301. eaddr = pcap_ether_hostton(name);
  5302. if (eaddr == NULL)
  5303. bpf_error(
  5304. "unknown 802.11 host '%s'", name);
  5305. b = gen_wlanhostop(eaddr, dir);
  5306. free(eaddr);
  5307. return b;
  5308. case DLT_IP_OVER_FC:
  5309. eaddr = pcap_ether_hostton(name);
  5310. if (eaddr == NULL)
  5311. bpf_error(
  5312. "unknown Fibre Channel host '%s'", name);
  5313. b = gen_ipfchostop(eaddr, dir);
  5314. free(eaddr);
  5315. return b;
  5316. case DLT_SUNATM:
  5317. if (!is_lane)
  5318. break;
  5319. /*
  5320. * Check that the packet doesn't begin
  5321. * with an LE Control marker. (We've
  5322. * already generated a test for LANE.)
  5323. */
  5324. tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
  5325. BPF_H, 0xFF00);
  5326. gen_not(tmp);
  5327. eaddr = pcap_ether_hostton(name);
  5328. if (eaddr == NULL)
  5329. bpf_error(
  5330. "unknown ether host '%s'", name);
  5331. b = gen_ehostop(eaddr, dir);
  5332. gen_and(tmp, b);
  5333. free(eaddr);
  5334. return b;
  5335. }
  5336. bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
  5337. } else if (proto == Q_DECNET) {
  5338. unsigned short dn_addr = __pcap_nametodnaddr(name);
  5339. /*
  5340. * I don't think DECNET hosts can be multihomed, so
  5341. * there is no need to build up a list of addresses
  5342. */
  5343. return (gen_host(dn_addr, 0, proto, dir, q.addr));
  5344. } else {
  5345. #ifndef INET6
  5346. alist = pcap_nametoaddr(name);
  5347. if (alist == NULL || *alist == NULL)
  5348. bpf_error("unknown host '%s'", name);
  5349. tproto = proto;
  5350. if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
  5351. tproto = Q_IP;
  5352. b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
  5353. while (*alist) {
  5354. tmp = gen_host(**alist++, 0xffffffff,
  5355. tproto, dir, q.addr);
  5356. gen_or(b, tmp);
  5357. b = tmp;
  5358. }
  5359. return b;
  5360. #else
  5361. memset(&mask128, 0xff, sizeof(mask128));
  5362. res0 = res = pcap_nametoaddrinfo(name);
  5363. if (res == NULL)
  5364. bpf_error("unknown host '%s'", name);
  5365. ai = res;
  5366. b = tmp = NULL;
  5367. tproto = tproto6 = proto;
  5368. if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT) {
  5369. tproto = Q_IP;
  5370. tproto6 = Q_IPV6;
  5371. }
  5372. for (res = res0; res; res = res->ai_next) {
  5373. switch (res->ai_family) {
  5374. case AF_INET:
  5375. if (tproto == Q_IPV6)
  5376. continue;
  5377. sin4 = (struct sockaddr_in *)
  5378. res->ai_addr;
  5379. tmp = gen_host(ntohl(sin4->sin_addr.s_addr),
  5380. 0xffffffff, tproto, dir, q.addr);
  5381. break;
  5382. case AF_INET6:
  5383. if (tproto6 == Q_IP)
  5384. continue;
  5385. sin6 = (struct sockaddr_in6 *)
  5386. res->ai_addr;
  5387. tmp = gen_host6(&sin6->sin6_addr,
  5388. &mask128, tproto6, dir, q.addr);
  5389. break;
  5390. default:
  5391. continue;
  5392. }
  5393. if (b)
  5394. gen_or(b, tmp);
  5395. b = tmp;
  5396. }
  5397. ai = NULL;
  5398. freeaddrinfo(res0);
  5399. if (b == NULL) {
  5400. bpf_error("unknown host '%s'%s", name,
  5401. (proto == Q_DEFAULT)
  5402. ? ""
  5403. : " for specified address family");
  5404. }
  5405. return b;
  5406. #endif /*INET6*/
  5407. }
  5408. case Q_PORT:
  5409. if (proto != Q_DEFAULT &&
  5410. proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
  5411. bpf_error("illegal qualifier of 'port'");
  5412. if (pcap_nametoport(name, &port, &real_proto) == 0)
  5413. bpf_error("unknown port '%s'", name);
  5414. if (proto == Q_UDP) {
  5415. if (real_proto == IPPROTO_TCP)
  5416. bpf_error("port '%s' is tcp", name);
  5417. else if (real_proto == IPPROTO_SCTP)
  5418. bpf_error("port '%s' is sctp", name);
  5419. else
  5420. /* override PROTO_UNDEF */
  5421. real_proto = IPPROTO_UDP;
  5422. }
  5423. if (proto == Q_TCP) {
  5424. if (real_proto == IPPROTO_UDP)
  5425. bpf_error("port '%s' is udp", name);
  5426. else if (real_proto == IPPROTO_SCTP)
  5427. bpf_error("port '%s' is sctp", name);
  5428. else
  5429. /* override PROTO_UNDEF */
  5430. real_proto = IPPROTO_TCP;
  5431. }
  5432. if (proto == Q_SCTP) {
  5433. if (real_proto == IPPROTO_UDP)
  5434. bpf_error("port '%s' is udp", name);
  5435. else if (real_proto == IPPROTO_TCP)
  5436. bpf_error("port '%s' is tcp", name);
  5437. else
  5438. /* override PROTO_UNDEF */
  5439. real_proto = IPPROTO_SCTP;
  5440. }
  5441. if (port < 0)
  5442. bpf_error("illegal port number %d < 0", port);
  5443. if (port > 65535)
  5444. bpf_error("illegal port number %d > 65535", port);
  5445. b = gen_port(port, real_proto, dir);
  5446. gen_or(gen_port6(port, real_proto, dir), b);
  5447. return b;
  5448. case Q_PORTRANGE:
  5449. if (proto != Q_DEFAULT &&
  5450. proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
  5451. bpf_error("illegal qualifier of 'portrange'");
  5452. if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
  5453. bpf_error("unknown port in range '%s'", name);
  5454. if (proto == Q_UDP) {
  5455. if (real_proto == IPPROTO_TCP)
  5456. bpf_error("port in range '%s' is tcp", name);
  5457. else if (real_proto == IPPROTO_SCTP)
  5458. bpf_error("port in range '%s' is sctp", name);
  5459. else
  5460. /* override PROTO_UNDEF */
  5461. real_proto = IPPROTO_UDP;
  5462. }
  5463. if (proto == Q_TCP) {
  5464. if (real_proto == IPPROTO_UDP)
  5465. bpf_error("port in range '%s' is udp", name);
  5466. else if (real_proto == IPPROTO_SCTP)
  5467. bpf_error("port in range '%s' is sctp", name);
  5468. else
  5469. /* override PROTO_UNDEF */
  5470. real_proto = IPPROTO_TCP;
  5471. }
  5472. if (proto == Q_SCTP) {
  5473. if (real_proto == IPPROTO_UDP)
  5474. bpf_error("port in range '%s' is udp", name);
  5475. else if (real_proto == IPPROTO_TCP)
  5476. bpf_error("port in range '%s' is tcp", name);
  5477. else
  5478. /* override PROTO_UNDEF */
  5479. real_proto = IPPROTO_SCTP;
  5480. }
  5481. if (port1 < 0)
  5482. bpf_error("illegal port number %d < 0", port1);
  5483. if (port1 > 65535)
  5484. bpf_error("illegal port number %d > 65535", port1);
  5485. if (port2 < 0)
  5486. bpf_error("illegal port number %d < 0", port2);
  5487. if (port2 > 65535)
  5488. bpf_error("illegal port number %d > 65535", port2);
  5489. b = gen_portrange(port1, port2, real_proto, dir);
  5490. gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
  5491. return b;
  5492. case Q_GATEWAY:
  5493. #ifndef INET6
  5494. eaddr = pcap_ether_hostton(name);
  5495. if (eaddr == NULL)
  5496. bpf_error("unknown ether host: %s", name);
  5497. alist = pcap_nametoaddr(name);
  5498. if (alist == NULL || *alist == NULL)
  5499. bpf_error("unknown host '%s'", name);
  5500. b = gen_gateway(eaddr, alist, proto, dir);
  5501. free(eaddr);
  5502. return b;
  5503. #else
  5504. bpf_error("'gateway' not supported in this configuration");
  5505. #endif /*INET6*/
  5506. case Q_PROTO:
  5507. real_proto = lookup_proto(name, proto);
  5508. if (real_proto >= 0)
  5509. return gen_proto(real_proto, proto, dir);
  5510. else
  5511. bpf_error("unknown protocol: %s", name);
  5512. case Q_PROTOCHAIN:
  5513. real_proto = lookup_proto(name, proto);
  5514. if (real_proto >= 0)
  5515. return gen_protochain(real_proto, proto, dir);
  5516. else
  5517. bpf_error("unknown protocol: %s", name);
  5518. case Q_UNDEF:
  5519. syntax();
  5520. /* NOTREACHED */
  5521. }
  5522. abort();
  5523. /* NOTREACHED */
  5524. }
  5525. struct block *
  5526. gen_mcode(s1, s2, masklen, q)
  5527. register const char *s1, *s2;
  5528. register int masklen;
  5529. struct qual q;
  5530. {
  5531. register int nlen, mlen;
  5532. bpf_u_int32 n, m;
  5533. nlen = __pcap_atoin(s1, &n);
  5534. /* Promote short ipaddr */
  5535. n <<= 32 - nlen;
  5536. if (s2 != NULL) {
  5537. mlen = __pcap_atoin(s2, &m);
  5538. /* Promote short ipaddr */
  5539. m <<= 32 - mlen;
  5540. if ((n & ~m) != 0)
  5541. bpf_error("non-network bits set in \"%s mask %s\"",
  5542. s1, s2);
  5543. } else {
  5544. /* Convert mask len to mask */
  5545. if (masklen > 32)
  5546. bpf_error("mask length must be <= 32");
  5547. if (masklen == 0) {
  5548. /*
  5549. * X << 32 is not guaranteed by C to be 0; it's
  5550. * undefined.
  5551. */
  5552. m = 0;
  5553. } else
  5554. m = 0xffffffff << (32 - masklen);
  5555. if ((n & ~m) != 0)
  5556. bpf_error("non-network bits set in \"%s/%d\"",
  5557. s1, masklen);
  5558. }
  5559. switch (q.addr) {
  5560. case Q_NET:
  5561. return gen_host(n, m, q.proto, q.dir, q.addr);
  5562. default:
  5563. bpf_error("Mask syntax for networks only");
  5564. /* NOTREACHED */
  5565. }
  5566. /* NOTREACHED */
  5567. return NULL;
  5568. }
  5569. struct block *
  5570. gen_ncode(s, v, q)
  5571. register const char *s;
  5572. bpf_u_int32 v;
  5573. struct qual q;
  5574. {
  5575. bpf_u_int32 mask;
  5576. int proto = q.proto;
  5577. int dir = q.dir;
  5578. register int vlen;
  5579. if (s == NULL)
  5580. vlen = 32;
  5581. else if (q.proto == Q_DECNET)
  5582. vlen = __pcap_atodn(s, &v);
  5583. else
  5584. vlen = __pcap_atoin(s, &v);
  5585. switch (q.addr) {
  5586. case Q_DEFAULT:
  5587. case Q_HOST:
  5588. case Q_NET:
  5589. if (proto == Q_DECNET)
  5590. return gen_host(v, 0, proto, dir, q.addr);
  5591. else if (proto == Q_LINK) {
  5592. bpf_error("illegal link layer address");
  5593. } else {
  5594. mask = 0xffffffff;
  5595. if (s == NULL && q.addr == Q_NET) {
  5596. /* Promote short net number */
  5597. while (v && (v & 0xff000000) == 0) {
  5598. v <<= 8;
  5599. mask <<= 8;
  5600. }
  5601. } else {
  5602. /* Promote short ipaddr */
  5603. v <<= 32 - vlen;
  5604. mask <<= 32 - vlen;
  5605. }
  5606. return gen_host(v, mask, proto, dir, q.addr);
  5607. }
  5608. case Q_PORT:
  5609. if (proto == Q_UDP)
  5610. proto = IPPROTO_UDP;
  5611. else if (proto == Q_TCP)
  5612. proto = IPPROTO_TCP;
  5613. else if (proto == Q_SCTP)
  5614. proto = IPPROTO_SCTP;
  5615. else if (proto == Q_DEFAULT)
  5616. proto = PROTO_UNDEF;
  5617. else
  5618. bpf_error("illegal qualifier of 'port'");
  5619. if (v > 65535)
  5620. bpf_error("illegal port number %u > 65535", v);
  5621. {
  5622. struct block *b;
  5623. b = gen_port((int)v, proto, dir);
  5624. gen_or(gen_port6((int)v, proto, dir), b);
  5625. return b;
  5626. }
  5627. case Q_PORTRANGE:
  5628. if (proto == Q_UDP)
  5629. proto = IPPROTO_UDP;
  5630. else if (proto == Q_TCP)
  5631. proto = IPPROTO_TCP;
  5632. else if (proto == Q_SCTP)
  5633. proto = IPPROTO_SCTP;
  5634. else if (proto == Q_DEFAULT)
  5635. proto = PROTO_UNDEF;
  5636. else
  5637. bpf_error("illegal qualifier of 'portrange'");
  5638. if (v > 65535)
  5639. bpf_error("illegal port number %u > 65535", v);
  5640. {
  5641. struct block *b;
  5642. b = gen_portrange((int)v, (int)v, proto, dir);
  5643. gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
  5644. return b;
  5645. }
  5646. case Q_GATEWAY:
  5647. bpf_error("'gateway' requires a name");
  5648. /* NOTREACHED */
  5649. case Q_PROTO:
  5650. return gen_proto((int)v, proto, dir);
  5651. case Q_PROTOCHAIN:
  5652. return gen_protochain((int)v, proto, dir);
  5653. case Q_UNDEF:
  5654. syntax();
  5655. /* NOTREACHED */
  5656. default:
  5657. abort();
  5658. /* NOTREACHED */
  5659. }
  5660. /* NOTREACHED */
  5661. }
  5662. #ifdef INET6
  5663. struct block *
  5664. gen_mcode6(s1, s2, masklen, q)
  5665. register const char *s1, *s2;
  5666. register int masklen;
  5667. struct qual q;
  5668. {
  5669. struct addrinfo *res;
  5670. struct in6_addr *addr;
  5671. struct in6_addr mask;
  5672. struct block *b;
  5673. u_int32_t *a, *m;
  5674. if (s2)
  5675. bpf_error("no mask %s supported", s2);
  5676. res = pcap_nametoaddrinfo(s1);
  5677. if (!res)
  5678. bpf_error("invalid ip6 address %s", s1);
  5679. ai = res;
  5680. if (res->ai_next)
  5681. bpf_error("%s resolved to multiple address", s1);
  5682. addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
  5683. if ((int)sizeof(mask) * 8 < masklen)
  5684. bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
  5685. memset(&mask, 0, sizeof(mask));
  5686. memset(&mask, 0xff, masklen / 8);
  5687. if (masklen % 8) {
  5688. mask.s6_addr[masklen / 8] =
  5689. (0xff << (8 - masklen % 8)) & 0xff;
  5690. }
  5691. a = (u_int32_t *)addr;
  5692. m = (u_int32_t *)&mask;
  5693. if ((a[0] & ~m[0]) || (a[1] & ~m[1])
  5694. || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
  5695. bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
  5696. }
  5697. switch (q.addr) {
  5698. case Q_DEFAULT:
  5699. case Q_HOST:
  5700. if (masklen != 128)
  5701. bpf_error("Mask syntax for networks only");
  5702. /* FALLTHROUGH */
  5703. case Q_NET:
  5704. b = gen_host6(addr, &mask, q.proto, q.dir, q.addr);
  5705. ai = NULL;
  5706. freeaddrinfo(res);
  5707. return b;
  5708. default:
  5709. bpf_error("invalid qualifier against IPv6 address");
  5710. /* NOTREACHED */
  5711. }
  5712. return NULL;
  5713. }
  5714. #endif /*INET6*/
  5715. struct block *
  5716. gen_ecode(eaddr, q)
  5717. register const u_char *eaddr;
  5718. struct qual q;
  5719. {
  5720. struct block *b, *tmp;
  5721. if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
  5722. switch (linktype) {
  5723. case DLT_EN10MB:
  5724. case DLT_NETANALYZER:
  5725. case DLT_NETANALYZER_TRANSPARENT:
  5726. return gen_ehostop(eaddr, (int)q.dir);
  5727. case DLT_FDDI:
  5728. return gen_fhostop(eaddr, (int)q.dir);
  5729. case DLT_IEEE802:
  5730. return gen_thostop(eaddr, (int)q.dir);
  5731. case DLT_IEEE802_11:
  5732. case DLT_PRISM_HEADER:
  5733. case DLT_IEEE802_11_RADIO_AVS:
  5734. case DLT_IEEE802_11_RADIO:
  5735. case DLT_PPI:
  5736. return gen_wlanhostop(eaddr, (int)q.dir);
  5737. case DLT_SUNATM:
  5738. if (is_lane) {
  5739. /*
  5740. * Check that the packet doesn't begin with an
  5741. * LE Control marker. (We've already generated
  5742. * a test for LANE.)
  5743. */
  5744. tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
  5745. 0xFF00);
  5746. gen_not(tmp);
  5747. /*
  5748. * Now check the MAC address.
  5749. */
  5750. b = gen_ehostop(eaddr, (int)q.dir);
  5751. gen_and(tmp, b);
  5752. return b;
  5753. }
  5754. break;
  5755. case DLT_IP_OVER_FC:
  5756. return gen_ipfchostop(eaddr, (int)q.dir);
  5757. default:
  5758. bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
  5759. break;
  5760. }
  5761. }
  5762. bpf_error("ethernet address used in non-ether expression");
  5763. /* NOTREACHED */
  5764. return NULL;
  5765. }
  5766. void
  5767. sappend(s0, s1)
  5768. struct slist *s0, *s1;
  5769. {
  5770. /*
  5771. * This is definitely not the best way to do this, but the
  5772. * lists will rarely get long.
  5773. */
  5774. while (s0->next)
  5775. s0 = s0->next;
  5776. s0->next = s1;
  5777. }
  5778. static struct slist *
  5779. xfer_to_x(a)
  5780. struct arth *a;
  5781. {
  5782. struct slist *s;
  5783. s = new_stmt(BPF_LDX|BPF_MEM);
  5784. s->s.k = a->regno;
  5785. return s;
  5786. }
  5787. static struct slist *
  5788. xfer_to_a(a)
  5789. struct arth *a;
  5790. {
  5791. struct slist *s;
  5792. s = new_stmt(BPF_LD|BPF_MEM);
  5793. s->s.k = a->regno;
  5794. return s;
  5795. }
  5796. /*
  5797. * Modify "index" to use the value stored into its register as an
  5798. * offset relative to the beginning of the header for the protocol
  5799. * "proto", and allocate a register and put an item "size" bytes long
  5800. * (1, 2, or 4) at that offset into that register, making it the register
  5801. * for "index".
  5802. */
  5803. struct arth *
  5804. gen_load(proto, inst, size)
  5805. int proto;
  5806. struct arth *inst;
  5807. int size;
  5808. {
  5809. struct slist *s, *tmp;
  5810. struct block *b;
  5811. int regno = alloc_reg();
  5812. free_reg(inst->regno);
  5813. switch (size) {
  5814. default:
  5815. bpf_error("data size must be 1, 2, or 4");
  5816. case 1:
  5817. size = BPF_B;
  5818. break;
  5819. case 2:
  5820. size = BPF_H;
  5821. break;
  5822. case 4:
  5823. size = BPF_W;
  5824. break;
  5825. }
  5826. switch (proto) {
  5827. default:
  5828. bpf_error("unsupported index operation");
  5829. case Q_RADIO:
  5830. /*
  5831. * The offset is relative to the beginning of the packet
  5832. * data, if we have a radio header. (If we don't, this
  5833. * is an error.)
  5834. */
  5835. if (linktype != DLT_IEEE802_11_RADIO_AVS &&
  5836. linktype != DLT_IEEE802_11_RADIO &&
  5837. linktype != DLT_PRISM_HEADER)
  5838. bpf_error("radio information not present in capture");
  5839. /*
  5840. * Load into the X register the offset computed into the
  5841. * register specified by "index".
  5842. */
  5843. s = xfer_to_x(inst);
  5844. /*
  5845. * Load the item at that offset.
  5846. */
  5847. tmp = new_stmt(BPF_LD|BPF_IND|size);
  5848. sappend(s, tmp);
  5849. sappend(inst->s, s);
  5850. break;
  5851. case Q_LINK:
  5852. /*
  5853. * The offset is relative to the beginning of
  5854. * the link-layer header.
  5855. *
  5856. * XXX - what about ATM LANE? Should the index be
  5857. * relative to the beginning of the AAL5 frame, so
  5858. * that 0 refers to the beginning of the LE Control
  5859. * field, or relative to the beginning of the LAN
  5860. * frame, so that 0 refers, for Ethernet LANE, to
  5861. * the beginning of the destination address?
  5862. */
  5863. s = gen_llprefixlen();
  5864. /*
  5865. * If "s" is non-null, it has code to arrange that the
  5866. * X register contains the length of the prefix preceding
  5867. * the link-layer header. Add to it the offset computed
  5868. * into the register specified by "index", and move that
  5869. * into the X register. Otherwise, just load into the X
  5870. * register the offset computed into the register specified
  5871. * by "index".
  5872. */
  5873. if (s != NULL) {
  5874. sappend(s, xfer_to_a(inst));
  5875. sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
  5876. sappend(s, new_stmt(BPF_MISC|BPF_TAX));
  5877. } else
  5878. s = xfer_to_x(inst);
  5879. /*
  5880. * Load the item at the sum of the offset we've put in the
  5881. * X register and the offset of the start of the link
  5882. * layer header (which is 0 if the radio header is
  5883. * variable-length; that header length is what we put
  5884. * into the X register and then added to the index).
  5885. */
  5886. tmp = new_stmt(BPF_LD|BPF_IND|size);
  5887. tmp->s.k = off_ll;
  5888. sappend(s, tmp);
  5889. sappend(inst->s, s);
  5890. break;
  5891. case Q_IP:
  5892. case Q_ARP:
  5893. case Q_RARP:
  5894. case Q_ATALK:
  5895. case Q_DECNET:
  5896. case Q_SCA:
  5897. case Q_LAT:
  5898. case Q_MOPRC:
  5899. case Q_MOPDL:
  5900. case Q_IPV6:
  5901. /*
  5902. * The offset is relative to the beginning of
  5903. * the network-layer header.
  5904. * XXX - are there any cases where we want
  5905. * off_nl_nosnap?
  5906. */
  5907. s = gen_off_macpl();
  5908. /*
  5909. * If "s" is non-null, it has code to arrange that the
  5910. * X register contains the offset of the MAC-layer
  5911. * payload. Add to it the offset computed into the
  5912. * register specified by "index", and move that into
  5913. * the X register. Otherwise, just load into the X
  5914. * register the offset computed into the register specified
  5915. * by "index".
  5916. */
  5917. if (s != NULL) {
  5918. sappend(s, xfer_to_a(inst));
  5919. sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
  5920. sappend(s, new_stmt(BPF_MISC|BPF_TAX));
  5921. } else
  5922. s = xfer_to_x(inst);
  5923. /*
  5924. * Load the item at the sum of the offset we've put in the
  5925. * X register, the offset of the start of the network
  5926. * layer header from the beginning of the MAC-layer
  5927. * payload, and the purported offset of the start of the
  5928. * MAC-layer payload (which might be 0 if there's a
  5929. * variable-length prefix before the link-layer header
  5930. * or the link-layer header itself is variable-length;
  5931. * the variable-length offset of the start of the
  5932. * MAC-layer payload is what we put into the X register
  5933. * and then added to the index).
  5934. */
  5935. tmp = new_stmt(BPF_LD|BPF_IND|size);
  5936. tmp->s.k = off_macpl + off_nl;
  5937. sappend(s, tmp);
  5938. sappend(inst->s, s);
  5939. /*
  5940. * Do the computation only if the packet contains
  5941. * the protocol in question.
  5942. */
  5943. b = gen_proto_abbrev(proto);
  5944. if (inst->b)
  5945. gen_and(inst->b, b);
  5946. inst->b = b;
  5947. break;
  5948. case Q_SCTP:
  5949. case Q_TCP:
  5950. case Q_UDP:
  5951. case Q_ICMP:
  5952. case Q_IGMP:
  5953. case Q_IGRP:
  5954. case Q_PIM:
  5955. case Q_VRRP:
  5956. case Q_CARP:
  5957. /*
  5958. * The offset is relative to the beginning of
  5959. * the transport-layer header.
  5960. *
  5961. * Load the X register with the length of the IPv4 header
  5962. * (plus the offset of the link-layer header, if it's
  5963. * a variable-length header), in bytes.
  5964. *
  5965. * XXX - are there any cases where we want
  5966. * off_nl_nosnap?
  5967. * XXX - we should, if we're built with
  5968. * IPv6 support, generate code to load either
  5969. * IPv4, IPv6, or both, as appropriate.
  5970. */
  5971. s = gen_loadx_iphdrlen();
  5972. /*
  5973. * The X register now contains the sum of the length
  5974. * of any variable-length header preceding the link-layer
  5975. * header, any variable-length link-layer header, and the
  5976. * length of the network-layer header.
  5977. *
  5978. * Load into the A register the offset relative to
  5979. * the beginning of the transport layer header,
  5980. * add the X register to that, move that to the
  5981. * X register, and load with an offset from the
  5982. * X register equal to the offset of the network
  5983. * layer header relative to the beginning of
  5984. * the MAC-layer payload plus the fixed-length
  5985. * portion of the offset of the MAC-layer payload
  5986. * from the beginning of the raw packet data.
  5987. */
  5988. sappend(s, xfer_to_a(inst));
  5989. sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
  5990. sappend(s, new_stmt(BPF_MISC|BPF_TAX));
  5991. sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
  5992. tmp->s.k = off_macpl + off_nl;
  5993. sappend(inst->s, s);
  5994. /*
  5995. * Do the computation only if the packet contains
  5996. * the protocol in question - which is true only
  5997. * if this is an IP datagram and is the first or
  5998. * only fragment of that datagram.
  5999. */
  6000. gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
  6001. if (inst->b)
  6002. gen_and(inst->b, b);
  6003. gen_and(gen_proto_abbrev(Q_IP), b);
  6004. inst->b = b;
  6005. break;
  6006. case Q_ICMPV6:
  6007. bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
  6008. /*NOTREACHED*/
  6009. }
  6010. inst->regno = regno;
  6011. s = new_stmt(BPF_ST);
  6012. s->s.k = regno;
  6013. sappend(inst->s, s);
  6014. return inst;
  6015. }
  6016. struct block *
  6017. gen_relation(code, a0, a1, reversed)
  6018. int code;
  6019. struct arth *a0, *a1;
  6020. int reversed;
  6021. {
  6022. struct slist *s0, *s1, *s2;
  6023. struct block *b, *tmp;
  6024. s0 = xfer_to_x(a1);
  6025. s1 = xfer_to_a(a0);
  6026. if (code == BPF_JEQ) {
  6027. s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
  6028. b = new_block(JMP(code));
  6029. sappend(s1, s2);
  6030. }
  6031. else
  6032. b = new_block(BPF_JMP|code|BPF_X);
  6033. if (reversed)
  6034. gen_not(b);
  6035. sappend(s0, s1);
  6036. sappend(a1->s, s0);
  6037. sappend(a0->s, a1->s);
  6038. b->stmts = a0->s;
  6039. free_reg(a0->regno);
  6040. free_reg(a1->regno);
  6041. /* 'and' together protocol checks */
  6042. if (a0->b) {
  6043. if (a1->b) {
  6044. gen_and(a0->b, tmp = a1->b);
  6045. }
  6046. else
  6047. tmp = a0->b;
  6048. } else
  6049. tmp = a1->b;
  6050. if (tmp)
  6051. gen_and(tmp, b);
  6052. return b;
  6053. }
  6054. struct arth *
  6055. gen_loadlen()
  6056. {
  6057. int regno = alloc_reg();
  6058. struct arth *a = (struct arth *)newchunk(sizeof(*a));
  6059. struct slist *s;
  6060. s = new_stmt(BPF_LD|BPF_LEN);
  6061. s->next = new_stmt(BPF_ST);
  6062. s->next->s.k = regno;
  6063. a->s = s;
  6064. a->regno = regno;
  6065. return a;
  6066. }
  6067. struct arth *
  6068. gen_loadi(val)
  6069. int val;
  6070. {
  6071. struct arth *a;
  6072. struct slist *s;
  6073. int reg;
  6074. a = (struct arth *)newchunk(sizeof(*a));
  6075. reg = alloc_reg();
  6076. s = new_stmt(BPF_LD|BPF_IMM);
  6077. s->s.k = val;
  6078. s->next = new_stmt(BPF_ST);
  6079. s->next->s.k = reg;
  6080. a->s = s;
  6081. a->regno = reg;
  6082. return a;
  6083. }
  6084. struct arth *
  6085. gen_neg(a)
  6086. struct arth *a;
  6087. {
  6088. struct slist *s;
  6089. s = xfer_to_a(a);
  6090. sappend(a->s, s);
  6091. s = new_stmt(BPF_ALU|BPF_NEG);
  6092. s->s.k = 0;
  6093. sappend(a->s, s);
  6094. s = new_stmt(BPF_ST);
  6095. s->s.k = a->regno;
  6096. sappend(a->s, s);
  6097. return a;
  6098. }
  6099. struct arth *
  6100. gen_arth(code, a0, a1)
  6101. int code;
  6102. struct arth *a0, *a1;
  6103. {
  6104. struct slist *s0, *s1, *s2;
  6105. s0 = xfer_to_x(a1);
  6106. s1 = xfer_to_a(a0);
  6107. s2 = new_stmt(BPF_ALU|BPF_X|code);
  6108. sappend(s1, s2);
  6109. sappend(s0, s1);
  6110. sappend(a1->s, s0);
  6111. sappend(a0->s, a1->s);
  6112. free_reg(a0->regno);
  6113. free_reg(a1->regno);
  6114. s0 = new_stmt(BPF_ST);
  6115. a0->regno = s0->s.k = alloc_reg();
  6116. sappend(a0->s, s0);
  6117. return a0;
  6118. }
  6119. /*
  6120. * Here we handle simple allocation of the scratch registers.
  6121. * If too many registers are alloc'd, the allocator punts.
  6122. */
  6123. static int regused[BPF_MEMWORDS];
  6124. static int curreg;
  6125. /*
  6126. * Initialize the table of used registers and the current register.
  6127. */
  6128. static void
  6129. init_regs()
  6130. {
  6131. curreg = 0;
  6132. memset(regused, 0, sizeof regused);
  6133. }
  6134. /*
  6135. * Return the next free register.
  6136. */
  6137. static int
  6138. alloc_reg()
  6139. {
  6140. int n = BPF_MEMWORDS;
  6141. while (--n >= 0) {
  6142. if (regused[curreg])
  6143. curreg = (curreg + 1) % BPF_MEMWORDS;
  6144. else {
  6145. regused[curreg] = 1;
  6146. return curreg;
  6147. }
  6148. }
  6149. bpf_error("too many registers needed to evaluate expression");
  6150. /* NOTREACHED */
  6151. return 0;
  6152. }
  6153. /*
  6154. * Return a register to the table so it can
  6155. * be used later.
  6156. */
  6157. static void
  6158. free_reg(n)
  6159. int n;
  6160. {
  6161. regused[n] = 0;
  6162. }
  6163. static struct block *
  6164. gen_len(jmp, n)
  6165. int jmp, n;
  6166. {
  6167. struct slist *s;
  6168. struct block *b;
  6169. s = new_stmt(BPF_LD|BPF_LEN);
  6170. b = new_block(JMP(jmp));
  6171. b->stmts = s;
  6172. b->s.k = n;
  6173. return b;
  6174. }
  6175. struct block *
  6176. gen_greater(n)
  6177. int n;
  6178. {
  6179. return gen_len(BPF_JGE, n);
  6180. }
  6181. /*
  6182. * Actually, this is less than or equal.
  6183. */
  6184. struct block *
  6185. gen_less(n)
  6186. int n;
  6187. {
  6188. struct block *b;
  6189. b = gen_len(BPF_JGT, n);
  6190. gen_not(b);
  6191. return b;
  6192. }
  6193. /*
  6194. * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
  6195. * the beginning of the link-layer header.
  6196. * XXX - that means you can't test values in the radiotap header, but
  6197. * as that header is difficult if not impossible to parse generally
  6198. * without a loop, that might not be a severe problem. A new keyword
  6199. * "radio" could be added for that, although what you'd really want
  6200. * would be a way of testing particular radio header values, which
  6201. * would generate code appropriate to the radio header in question.
  6202. */
  6203. struct block *
  6204. gen_byteop(op, idx, val)
  6205. int op, idx, val;
  6206. {
  6207. struct block *b;
  6208. struct slist *s;
  6209. switch (op) {
  6210. default:
  6211. abort();
  6212. case '=':
  6213. return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
  6214. case '<':
  6215. b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
  6216. return b;
  6217. case '>':
  6218. b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
  6219. return b;
  6220. case '|':
  6221. s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
  6222. break;
  6223. case '&':
  6224. s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
  6225. break;
  6226. }
  6227. s->s.k = val;
  6228. b = new_block(JMP(BPF_JEQ));
  6229. b->stmts = s;
  6230. gen_not(b);
  6231. return b;
  6232. }
  6233. static u_char abroadcast[] = { 0x0 };
  6234. struct block *
  6235. gen_broadcast(proto)
  6236. int proto;
  6237. {
  6238. bpf_u_int32 hostmask;
  6239. struct block *b0, *b1, *b2;
  6240. static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  6241. switch (proto) {
  6242. case Q_DEFAULT:
  6243. case Q_LINK:
  6244. switch (linktype) {
  6245. case DLT_ARCNET:
  6246. case DLT_ARCNET_LINUX:
  6247. return gen_ahostop(abroadcast, Q_DST);
  6248. case DLT_EN10MB:
  6249. case DLT_NETANALYZER:
  6250. case DLT_NETANALYZER_TRANSPARENT:
  6251. return gen_ehostop(ebroadcast, Q_DST);
  6252. case DLT_FDDI:
  6253. return gen_fhostop(ebroadcast, Q_DST);
  6254. case DLT_IEEE802:
  6255. return gen_thostop(ebroadcast, Q_DST);
  6256. case DLT_IEEE802_11:
  6257. case DLT_PRISM_HEADER:
  6258. case DLT_IEEE802_11_RADIO_AVS:
  6259. case DLT_IEEE802_11_RADIO:
  6260. case DLT_PPI:
  6261. return gen_wlanhostop(ebroadcast, Q_DST);
  6262. case DLT_IP_OVER_FC:
  6263. return gen_ipfchostop(ebroadcast, Q_DST);
  6264. case DLT_SUNATM:
  6265. if (is_lane) {
  6266. /*
  6267. * Check that the packet doesn't begin with an
  6268. * LE Control marker. (We've already generated
  6269. * a test for LANE.)
  6270. */
  6271. b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
  6272. BPF_H, 0xFF00);
  6273. gen_not(b1);
  6274. /*
  6275. * Now check the MAC address.
  6276. */
  6277. b0 = gen_ehostop(ebroadcast, Q_DST);
  6278. gen_and(b1, b0);
  6279. return b0;
  6280. }
  6281. break;
  6282. default:
  6283. bpf_error("not a broadcast link");
  6284. }
  6285. break;
  6286. case Q_IP:
  6287. /*
  6288. * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
  6289. * as an indication that we don't know the netmask, and fail
  6290. * in that case.
  6291. */
  6292. if (netmask == PCAP_NETMASK_UNKNOWN)
  6293. bpf_error("netmask not known, so 'ip broadcast' not supported");
  6294. b0 = gen_linktype(ETHERTYPE_IP);
  6295. hostmask = ~netmask;
  6296. b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
  6297. b2 = gen_mcmp(OR_NET, 16, BPF_W,
  6298. (bpf_int32)(~0 & hostmask), hostmask);
  6299. gen_or(b1, b2);
  6300. gen_and(b0, b2);
  6301. return b2;
  6302. }
  6303. bpf_error("only link-layer/IP broadcast filters supported");
  6304. /* NOTREACHED */
  6305. return NULL;
  6306. }
  6307. /*
  6308. * Generate code to test the low-order bit of a MAC address (that's
  6309. * the bottom bit of the *first* byte).
  6310. */
  6311. static struct block *
  6312. gen_mac_multicast(offset)
  6313. int offset;
  6314. {
  6315. register struct block *b0;
  6316. register struct slist *s;
  6317. /* link[offset] & 1 != 0 */
  6318. s = gen_load_a(OR_LINK, offset, BPF_B);
  6319. b0 = new_block(JMP(BPF_JSET));
  6320. b0->s.k = 1;
  6321. b0->stmts = s;
  6322. return b0;
  6323. }
  6324. struct block *
  6325. gen_multicast(proto)
  6326. int proto;
  6327. {
  6328. register struct block *b0, *b1, *b2;
  6329. register struct slist *s;
  6330. switch (proto) {
  6331. case Q_DEFAULT:
  6332. case Q_LINK:
  6333. switch (linktype) {
  6334. case DLT_ARCNET:
  6335. case DLT_ARCNET_LINUX:
  6336. /* all ARCnet multicasts use the same address */
  6337. return gen_ahostop(abroadcast, Q_DST);
  6338. case DLT_EN10MB:
  6339. case DLT_NETANALYZER:
  6340. case DLT_NETANALYZER_TRANSPARENT:
  6341. /* ether[0] & 1 != 0 */
  6342. return gen_mac_multicast(0);
  6343. case DLT_FDDI:
  6344. /*
  6345. * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
  6346. *
  6347. * XXX - was that referring to bit-order issues?
  6348. */
  6349. /* fddi[1] & 1 != 0 */
  6350. return gen_mac_multicast(1);
  6351. case DLT_IEEE802:
  6352. /* tr[2] & 1 != 0 */
  6353. return gen_mac_multicast(2);
  6354. case DLT_IEEE802_11:
  6355. case DLT_PRISM_HEADER:
  6356. case DLT_IEEE802_11_RADIO_AVS:
  6357. case DLT_IEEE802_11_RADIO:
  6358. case DLT_PPI:
  6359. /*
  6360. * Oh, yuk.
  6361. *
  6362. * For control frames, there is no DA.
  6363. *
  6364. * For management frames, DA is at an
  6365. * offset of 4 from the beginning of
  6366. * the packet.
  6367. *
  6368. * For data frames, DA is at an offset
  6369. * of 4 from the beginning of the packet
  6370. * if To DS is clear and at an offset of
  6371. * 16 from the beginning of the packet
  6372. * if To DS is set.
  6373. */
  6374. /*
  6375. * Generate the tests to be done for data frames.
  6376. *
  6377. * First, check for To DS set, i.e. "link[1] & 0x01".
  6378. */
  6379. s = gen_load_a(OR_LINK, 1, BPF_B);
  6380. b1 = new_block(JMP(BPF_JSET));
  6381. b1->s.k = 0x01; /* To DS */
  6382. b1->stmts = s;
  6383. /*
  6384. * If To DS is set, the DA is at 16.
  6385. */
  6386. b0 = gen_mac_multicast(16);
  6387. gen_and(b1, b0);
  6388. /*
  6389. * Now, check for To DS not set, i.e. check
  6390. * "!(link[1] & 0x01)".
  6391. */
  6392. s = gen_load_a(OR_LINK, 1, BPF_B);
  6393. b2 = new_block(JMP(BPF_JSET));
  6394. b2->s.k = 0x01; /* To DS */
  6395. b2->stmts = s;
  6396. gen_not(b2);
  6397. /*
  6398. * If To DS is not set, the DA is at 4.
  6399. */
  6400. b1 = gen_mac_multicast(4);
  6401. gen_and(b2, b1);
  6402. /*
  6403. * Now OR together the last two checks. That gives
  6404. * the complete set of checks for data frames.
  6405. */
  6406. gen_or(b1, b0);
  6407. /*
  6408. * Now check for a data frame.
  6409. * I.e, check "link[0] & 0x08".
  6410. */
  6411. s = gen_load_a(OR_LINK, 0, BPF_B);
  6412. b1 = new_block(JMP(BPF_JSET));
  6413. b1->s.k = 0x08;
  6414. b1->stmts = s;
  6415. /*
  6416. * AND that with the checks done for data frames.
  6417. */
  6418. gen_and(b1, b0);
  6419. /*
  6420. * If the high-order bit of the type value is 0, this
  6421. * is a management frame.
  6422. * I.e, check "!(link[0] & 0x08)".
  6423. */
  6424. s = gen_load_a(OR_LINK, 0, BPF_B);
  6425. b2 = new_block(JMP(BPF_JSET));
  6426. b2->s.k = 0x08;
  6427. b2->stmts = s;
  6428. gen_not(b2);
  6429. /*
  6430. * For management frames, the DA is at 4.
  6431. */
  6432. b1 = gen_mac_multicast(4);
  6433. gen_and(b2, b1);
  6434. /*
  6435. * OR that with the checks done for data frames.
  6436. * That gives the checks done for management and
  6437. * data frames.
  6438. */
  6439. gen_or(b1, b0);
  6440. /*
  6441. * If the low-order bit of the type value is 1,
  6442. * this is either a control frame or a frame
  6443. * with a reserved type, and thus not a
  6444. * frame with an SA.
  6445. *
  6446. * I.e., check "!(link[0] & 0x04)".
  6447. */
  6448. s = gen_load_a(OR_LINK, 0, BPF_B);
  6449. b1 = new_block(JMP(BPF_JSET));
  6450. b1->s.k = 0x04;
  6451. b1->stmts = s;
  6452. gen_not(b1);
  6453. /*
  6454. * AND that with the checks for data and management
  6455. * frames.
  6456. */
  6457. gen_and(b1, b0);
  6458. return b0;
  6459. case DLT_IP_OVER_FC:
  6460. b0 = gen_mac_multicast(2);
  6461. return b0;
  6462. case DLT_SUNATM:
  6463. if (is_lane) {
  6464. /*
  6465. * Check that the packet doesn't begin with an
  6466. * LE Control marker. (We've already generated
  6467. * a test for LANE.)
  6468. */
  6469. b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
  6470. BPF_H, 0xFF00);
  6471. gen_not(b1);
  6472. /* ether[off_mac] & 1 != 0 */
  6473. b0 = gen_mac_multicast(off_mac);
  6474. gen_and(b1, b0);
  6475. return b0;
  6476. }
  6477. break;
  6478. default:
  6479. break;
  6480. }
  6481. /* Link not known to support multicasts */
  6482. break;
  6483. case Q_IP:
  6484. b0 = gen_linktype(ETHERTYPE_IP);
  6485. b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
  6486. gen_and(b0, b1);
  6487. return b1;
  6488. case Q_IPV6:
  6489. b0 = gen_linktype(ETHERTYPE_IPV6);
  6490. b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
  6491. gen_and(b0, b1);
  6492. return b1;
  6493. }
  6494. bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
  6495. /* NOTREACHED */
  6496. return NULL;
  6497. }
  6498. /*
  6499. * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
  6500. * Outbound traffic is sent by this machine, while inbound traffic is
  6501. * sent by a remote machine (and may include packets destined for a
  6502. * unicast or multicast link-layer address we are not subscribing to).
  6503. * These are the same definitions implemented by pcap_setdirection().
  6504. * Capturing only unicast traffic destined for this host is probably
  6505. * better accomplished using a higher-layer filter.
  6506. */
  6507. struct block *
  6508. gen_inbound(dir)
  6509. int dir;
  6510. {
  6511. register struct block *b0;
  6512. /*
  6513. * Only some data link types support inbound/outbound qualifiers.
  6514. */
  6515. switch (linktype) {
  6516. case DLT_SLIP:
  6517. b0 = gen_relation(BPF_JEQ,
  6518. gen_load(Q_LINK, gen_loadi(0), 1),
  6519. gen_loadi(0),
  6520. dir);
  6521. break;
  6522. case DLT_IPNET:
  6523. if (dir) {
  6524. /* match outgoing packets */
  6525. b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_OUTBOUND);
  6526. } else {
  6527. /* match incoming packets */
  6528. b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_INBOUND);
  6529. }
  6530. break;
  6531. case DLT_LINUX_SLL:
  6532. /* match outgoing packets */
  6533. b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
  6534. if (!dir) {
  6535. /* to filter on inbound traffic, invert the match */
  6536. gen_not(b0);
  6537. }
  6538. break;
  6539. #ifdef HAVE_NET_PFVAR_H
  6540. case DLT_PFLOG:
  6541. b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
  6542. (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
  6543. break;
  6544. #endif
  6545. case DLT_PPP_PPPD:
  6546. if (dir) {
  6547. /* match outgoing packets */
  6548. b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
  6549. } else {
  6550. /* match incoming packets */
  6551. b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
  6552. }
  6553. break;
  6554. case DLT_JUNIPER_MFR:
  6555. case DLT_JUNIPER_MLFR:
  6556. case DLT_JUNIPER_MLPPP:
  6557. case DLT_JUNIPER_ATM1:
  6558. case DLT_JUNIPER_ATM2:
  6559. case DLT_JUNIPER_PPPOE:
  6560. case DLT_JUNIPER_PPPOE_ATM:
  6561. case DLT_JUNIPER_GGSN:
  6562. case DLT_JUNIPER_ES:
  6563. case DLT_JUNIPER_MONITOR:
  6564. case DLT_JUNIPER_SERVICES:
  6565. case DLT_JUNIPER_ETHER:
  6566. case DLT_JUNIPER_PPP:
  6567. case DLT_JUNIPER_FRELAY:
  6568. case DLT_JUNIPER_CHDLC:
  6569. case DLT_JUNIPER_VP:
  6570. case DLT_JUNIPER_ST:
  6571. case DLT_JUNIPER_ISM:
  6572. case DLT_JUNIPER_VS:
  6573. case DLT_JUNIPER_SRX_E2E:
  6574. case DLT_JUNIPER_FIBRECHANNEL:
  6575. case DLT_JUNIPER_ATM_CEMIC:
  6576. /* juniper flags (including direction) are stored
  6577. * the byte after the 3-byte magic number */
  6578. if (dir) {
  6579. /* match outgoing packets */
  6580. b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
  6581. } else {
  6582. /* match incoming packets */
  6583. b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
  6584. }
  6585. break;
  6586. default:
  6587. /*
  6588. * If we have packet meta-data indicating a direction,
  6589. * check it, otherwise give up as this link-layer type
  6590. * has nothing in the packet data.
  6591. */
  6592. #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
  6593. /*
  6594. * This is Linux with PF_PACKET support.
  6595. * If this is a *live* capture, we can look at
  6596. * special meta-data in the filter expression;
  6597. * if it's a savefile, we can't.
  6598. */
  6599. if (bpf_pcap->rfile != NULL) {
  6600. /* We have a FILE *, so this is a savefile */
  6601. bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
  6602. linktype);
  6603. b0 = NULL;
  6604. /* NOTREACHED */
  6605. }
  6606. /* match outgoing packets */
  6607. b0 = gen_cmp(OR_LINK, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
  6608. PACKET_OUTGOING);
  6609. if (!dir) {
  6610. /* to filter on inbound traffic, invert the match */
  6611. gen_not(b0);
  6612. }
  6613. #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
  6614. bpf_error("inbound/outbound not supported on linktype %d",
  6615. linktype);
  6616. b0 = NULL;
  6617. /* NOTREACHED */
  6618. #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
  6619. }
  6620. return (b0);
  6621. }
  6622. #ifdef HAVE_NET_PFVAR_H
  6623. /* PF firewall log matched interface */
  6624. struct block *
  6625. gen_pf_ifname(const char *ifname)
  6626. {
  6627. struct block *b0;
  6628. u_int len, off;
  6629. if (linktype != DLT_PFLOG) {
  6630. bpf_error("ifname supported only on PF linktype");
  6631. /* NOTREACHED */
  6632. }
  6633. len = sizeof(((struct pfloghdr *)0)->ifname);
  6634. off = offsetof(struct pfloghdr, ifname);
  6635. if (strlen(ifname) >= len) {
  6636. bpf_error("ifname interface names can only be %d characters",
  6637. len-1);
  6638. /* NOTREACHED */
  6639. }
  6640. b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
  6641. return (b0);
  6642. }
  6643. /* PF firewall log ruleset name */
  6644. struct block *
  6645. gen_pf_ruleset(char *ruleset)
  6646. {
  6647. struct block *b0;
  6648. if (linktype != DLT_PFLOG) {
  6649. bpf_error("ruleset supported only on PF linktype");
  6650. /* NOTREACHED */
  6651. }
  6652. if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
  6653. bpf_error("ruleset names can only be %ld characters",
  6654. (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
  6655. /* NOTREACHED */
  6656. }
  6657. b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
  6658. strlen(ruleset), (const u_char *)ruleset);
  6659. return (b0);
  6660. }
  6661. /* PF firewall log rule number */
  6662. struct block *
  6663. gen_pf_rnr(int rnr)
  6664. {
  6665. struct block *b0;
  6666. if (linktype != DLT_PFLOG) {
  6667. bpf_error("rnr supported only on PF linktype");
  6668. /* NOTREACHED */
  6669. }
  6670. b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
  6671. (bpf_int32)rnr);
  6672. return (b0);
  6673. }
  6674. /* PF firewall log sub-rule number */
  6675. struct block *
  6676. gen_pf_srnr(int srnr)
  6677. {
  6678. struct block *b0;
  6679. if (linktype != DLT_PFLOG) {
  6680. bpf_error("srnr supported only on PF linktype");
  6681. /* NOTREACHED */
  6682. }
  6683. b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
  6684. (bpf_int32)srnr);
  6685. return (b0);
  6686. }
  6687. /* PF firewall log reason code */
  6688. struct block *
  6689. gen_pf_reason(int reason)
  6690. {
  6691. struct block *b0;
  6692. if (linktype != DLT_PFLOG) {
  6693. bpf_error("reason supported only on PF linktype");
  6694. /* NOTREACHED */
  6695. }
  6696. b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
  6697. (bpf_int32)reason);
  6698. return (b0);
  6699. }
  6700. /* PF firewall log action */
  6701. struct block *
  6702. gen_pf_action(int action)
  6703. {
  6704. struct block *b0;
  6705. if (linktype != DLT_PFLOG) {
  6706. bpf_error("action supported only on PF linktype");
  6707. /* NOTREACHED */
  6708. }
  6709. b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
  6710. (bpf_int32)action);
  6711. return (b0);
  6712. }
  6713. #else /* !HAVE_NET_PFVAR_H */
  6714. struct block *
  6715. gen_pf_ifname(const char *ifname)
  6716. {
  6717. bpf_error("libpcap was compiled without pf support");
  6718. /* NOTREACHED */
  6719. return (NULL);
  6720. }
  6721. struct block *
  6722. gen_pf_ruleset(char *ruleset)
  6723. {
  6724. bpf_error("libpcap was compiled on a machine without pf support");
  6725. /* NOTREACHED */
  6726. return (NULL);
  6727. }
  6728. struct block *
  6729. gen_pf_rnr(int rnr)
  6730. {
  6731. bpf_error("libpcap was compiled on a machine without pf support");
  6732. /* NOTREACHED */
  6733. return (NULL);
  6734. }
  6735. struct block *
  6736. gen_pf_srnr(int srnr)
  6737. {
  6738. bpf_error("libpcap was compiled on a machine without pf support");
  6739. /* NOTREACHED */
  6740. return (NULL);
  6741. }
  6742. struct block *
  6743. gen_pf_reason(int reason)
  6744. {
  6745. bpf_error("libpcap was compiled on a machine without pf support");
  6746. /* NOTREACHED */
  6747. return (NULL);
  6748. }
  6749. struct block *
  6750. gen_pf_action(int action)
  6751. {
  6752. bpf_error("libpcap was compiled on a machine without pf support");
  6753. /* NOTREACHED */
  6754. return (NULL);
  6755. }
  6756. #endif /* HAVE_NET_PFVAR_H */
  6757. /* IEEE 802.11 wireless header */
  6758. struct block *
  6759. gen_p80211_type(int type, int mask)
  6760. {
  6761. struct block *b0;
  6762. switch (linktype) {
  6763. case DLT_IEEE802_11:
  6764. case DLT_PRISM_HEADER:
  6765. case DLT_IEEE802_11_RADIO_AVS:
  6766. case DLT_IEEE802_11_RADIO:
  6767. b0 = gen_mcmp(OR_LINK, 0, BPF_B, (bpf_int32)type,
  6768. (bpf_int32)mask);
  6769. break;
  6770. default:
  6771. bpf_error("802.11 link-layer types supported only on 802.11");
  6772. /* NOTREACHED */
  6773. }
  6774. return (b0);
  6775. }
  6776. struct block *
  6777. gen_p80211_fcdir(int fcdir)
  6778. {
  6779. struct block *b0;
  6780. switch (linktype) {
  6781. case DLT_IEEE802_11:
  6782. case DLT_PRISM_HEADER:
  6783. case DLT_IEEE802_11_RADIO_AVS:
  6784. case DLT_IEEE802_11_RADIO:
  6785. break;
  6786. default:
  6787. bpf_error("frame direction supported only with 802.11 headers");
  6788. /* NOTREACHED */
  6789. }
  6790. b0 = gen_mcmp(OR_LINK, 1, BPF_B, (bpf_int32)fcdir,
  6791. (bpf_u_int32)IEEE80211_FC1_DIR_MASK);
  6792. return (b0);
  6793. }
  6794. struct block *
  6795. gen_acode(eaddr, q)
  6796. register const u_char *eaddr;
  6797. struct qual q;
  6798. {
  6799. switch (linktype) {
  6800. case DLT_ARCNET:
  6801. case DLT_ARCNET_LINUX:
  6802. if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
  6803. q.proto == Q_LINK)
  6804. return (gen_ahostop(eaddr, (int)q.dir));
  6805. else {
  6806. bpf_error("ARCnet address used in non-arc expression");
  6807. /* NOTREACHED */
  6808. }
  6809. break;
  6810. default:
  6811. bpf_error("aid supported only on ARCnet");
  6812. /* NOTREACHED */
  6813. }
  6814. bpf_error("ARCnet address used in non-arc expression");
  6815. /* NOTREACHED */
  6816. return NULL;
  6817. }
  6818. static struct block *
  6819. gen_ahostop(eaddr, dir)
  6820. register const u_char *eaddr;
  6821. register int dir;
  6822. {
  6823. register struct block *b0, *b1;
  6824. switch (dir) {
  6825. /* src comes first, different from Ethernet */
  6826. case Q_SRC:
  6827. return gen_bcmp(OR_LINK, 0, 1, eaddr);
  6828. case Q_DST:
  6829. return gen_bcmp(OR_LINK, 1, 1, eaddr);
  6830. case Q_AND:
  6831. b0 = gen_ahostop(eaddr, Q_SRC);
  6832. b1 = gen_ahostop(eaddr, Q_DST);
  6833. gen_and(b0, b1);
  6834. return b1;
  6835. case Q_DEFAULT:
  6836. case Q_OR:
  6837. b0 = gen_ahostop(eaddr, Q_SRC);
  6838. b1 = gen_ahostop(eaddr, Q_DST);
  6839. gen_or(b0, b1);
  6840. return b1;
  6841. case Q_ADDR1:
  6842. bpf_error("'addr1' is only supported on 802.11");
  6843. break;
  6844. case Q_ADDR2:
  6845. bpf_error("'addr2' is only supported on 802.11");
  6846. break;
  6847. case Q_ADDR3:
  6848. bpf_error("'addr3' is only supported on 802.11");
  6849. break;
  6850. case Q_ADDR4:
  6851. bpf_error("'addr4' is only supported on 802.11");
  6852. break;
  6853. case Q_RA:
  6854. bpf_error("'ra' is only supported on 802.11");
  6855. break;
  6856. case Q_TA:
  6857. bpf_error("'ta' is only supported on 802.11");
  6858. break;
  6859. }
  6860. abort();
  6861. /* NOTREACHED */
  6862. }
  6863. /*
  6864. * support IEEE 802.1Q VLAN trunk over ethernet
  6865. */
  6866. struct block *
  6867. gen_vlan(vlan_num)
  6868. int vlan_num;
  6869. {
  6870. struct block *b0, *b1;
  6871. /* can't check for VLAN-encapsulated packets inside MPLS */
  6872. if (label_stack_depth > 0)
  6873. bpf_error("no VLAN match after MPLS");
  6874. /*
  6875. * Check for a VLAN packet, and then change the offsets to point
  6876. * to the type and data fields within the VLAN packet. Just
  6877. * increment the offsets, so that we can support a hierarchy, e.g.
  6878. * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
  6879. * VLAN 100.
  6880. *
  6881. * XXX - this is a bit of a kludge. If we were to split the
  6882. * compiler into a parser that parses an expression and
  6883. * generates an expression tree, and a code generator that
  6884. * takes an expression tree (which could come from our
  6885. * parser or from some other parser) and generates BPF code,
  6886. * we could perhaps make the offsets parameters of routines
  6887. * and, in the handler for an "AND" node, pass to subnodes
  6888. * other than the VLAN node the adjusted offsets.
  6889. *
  6890. * This would mean that "vlan" would, instead of changing the
  6891. * behavior of *all* tests after it, change only the behavior
  6892. * of tests ANDed with it. That would change the documented
  6893. * semantics of "vlan", which might break some expressions.
  6894. * However, it would mean that "(vlan and ip) or ip" would check
  6895. * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
  6896. * checking only for VLAN-encapsulated IP, so that could still
  6897. * be considered worth doing; it wouldn't break expressions
  6898. * that are of the form "vlan and ..." or "vlan N and ...",
  6899. * which I suspect are the most common expressions involving
  6900. * "vlan". "vlan or ..." doesn't necessarily do what the user
  6901. * would really want, now, as all the "or ..." tests would
  6902. * be done assuming a VLAN, even though the "or" could be viewed
  6903. * as meaning "or, if this isn't a VLAN packet...".
  6904. */
  6905. orig_nl = off_nl;
  6906. switch (linktype) {
  6907. case DLT_EN10MB:
  6908. case DLT_NETANALYZER:
  6909. case DLT_NETANALYZER_TRANSPARENT:
  6910. /* check for VLAN, including QinQ */
  6911. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
  6912. (bpf_int32)ETHERTYPE_8021Q);
  6913. b1 = gen_cmp(OR_LINK, off_linktype, BPF_H,
  6914. (bpf_int32)ETHERTYPE_8021QINQ);
  6915. gen_or(b0,b1);
  6916. b0 = b1;
  6917. /* If a specific VLAN is requested, check VLAN id */
  6918. if (vlan_num >= 0) {
  6919. b1 = gen_mcmp(OR_MACPL, 0, BPF_H,
  6920. (bpf_int32)vlan_num, 0x0fff);
  6921. gen_and(b0, b1);
  6922. b0 = b1;
  6923. }
  6924. off_macpl += 4;
  6925. off_linktype += 4;
  6926. #if 0
  6927. off_nl_nosnap += 4;
  6928. off_nl += 4;
  6929. #endif
  6930. break;
  6931. default:
  6932. bpf_error("no VLAN support for data link type %d",
  6933. linktype);
  6934. /*NOTREACHED*/
  6935. }
  6936. return (b0);
  6937. }
  6938. /*
  6939. * support for MPLS
  6940. */
  6941. struct block *
  6942. gen_mpls(label_num)
  6943. int label_num;
  6944. {
  6945. struct block *b0,*b1;
  6946. /*
  6947. * Change the offsets to point to the type and data fields within
  6948. * the MPLS packet. Just increment the offsets, so that we
  6949. * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
  6950. * capture packets with an outer label of 100000 and an inner
  6951. * label of 1024.
  6952. *
  6953. * XXX - this is a bit of a kludge. See comments in gen_vlan().
  6954. */
  6955. orig_nl = off_nl;
  6956. if (label_stack_depth > 0) {
  6957. /* just match the bottom-of-stack bit clear */
  6958. b0 = gen_mcmp(OR_MACPL, orig_nl-2, BPF_B, 0, 0x01);
  6959. } else {
  6960. /*
  6961. * Indicate that we're checking MPLS-encapsulated headers,
  6962. * to make sure higher level code generators don't try to
  6963. * match against IP-related protocols such as Q_ARP, Q_RARP
  6964. * etc.
  6965. */
  6966. switch (linktype) {
  6967. case DLT_C_HDLC: /* fall through */
  6968. case DLT_EN10MB:
  6969. case DLT_NETANALYZER:
  6970. case DLT_NETANALYZER_TRANSPARENT:
  6971. b0 = gen_linktype(ETHERTYPE_MPLS);
  6972. break;
  6973. case DLT_PPP:
  6974. b0 = gen_linktype(PPP_MPLS_UCAST);
  6975. break;
  6976. /* FIXME add other DLT_s ...
  6977. * for Frame-Relay/and ATM this may get messy due to SNAP headers
  6978. * leave it for now */
  6979. default:
  6980. bpf_error("no MPLS support for data link type %d",
  6981. linktype);
  6982. b0 = NULL;
  6983. /*NOTREACHED*/
  6984. break;
  6985. }
  6986. }
  6987. /* If a specific MPLS label is requested, check it */
  6988. if (label_num >= 0) {
  6989. label_num = label_num << 12; /* label is shifted 12 bits on the wire */
  6990. b1 = gen_mcmp(OR_MACPL, orig_nl, BPF_W, (bpf_int32)label_num,
  6991. 0xfffff000); /* only compare the first 20 bits */
  6992. gen_and(b0, b1);
  6993. b0 = b1;
  6994. }
  6995. off_nl_nosnap += 4;
  6996. off_nl += 4;
  6997. label_stack_depth++;
  6998. return (b0);
  6999. }
  7000. /*
  7001. * Support PPPOE discovery and session.
  7002. */
  7003. struct block *
  7004. gen_pppoed()
  7005. {
  7006. /* check for PPPoE discovery */
  7007. return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
  7008. }
  7009. struct block *
  7010. gen_pppoes(sess_num)
  7011. int sess_num;
  7012. {
  7013. struct block *b0, *b1;
  7014. /*
  7015. * Test against the PPPoE session link-layer type.
  7016. */
  7017. b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
  7018. /*
  7019. * Change the offsets to point to the type and data fields within
  7020. * the PPP packet, and note that this is PPPoE rather than
  7021. * raw PPP.
  7022. *
  7023. * XXX - this is a bit of a kludge. If we were to split the
  7024. * compiler into a parser that parses an expression and
  7025. * generates an expression tree, and a code generator that
  7026. * takes an expression tree (which could come from our
  7027. * parser or from some other parser) and generates BPF code,
  7028. * we could perhaps make the offsets parameters of routines
  7029. * and, in the handler for an "AND" node, pass to subnodes
  7030. * other than the PPPoE node the adjusted offsets.
  7031. *
  7032. * This would mean that "pppoes" would, instead of changing the
  7033. * behavior of *all* tests after it, change only the behavior
  7034. * of tests ANDed with it. That would change the documented
  7035. * semantics of "pppoes", which might break some expressions.
  7036. * However, it would mean that "(pppoes and ip) or ip" would check
  7037. * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
  7038. * checking only for VLAN-encapsulated IP, so that could still
  7039. * be considered worth doing; it wouldn't break expressions
  7040. * that are of the form "pppoes and ..." which I suspect are the
  7041. * most common expressions involving "pppoes". "pppoes or ..."
  7042. * doesn't necessarily do what the user would really want, now,
  7043. * as all the "or ..." tests would be done assuming PPPoE, even
  7044. * though the "or" could be viewed as meaning "or, if this isn't
  7045. * a PPPoE packet...".
  7046. */
  7047. orig_linktype = off_linktype; /* save original values */
  7048. orig_nl = off_nl;
  7049. is_pppoes = 1;
  7050. /* If a specific session is requested, check PPPoE session id */
  7051. if (sess_num >= 0) {
  7052. b1 = gen_mcmp(OR_MACPL, orig_nl, BPF_W,
  7053. (bpf_int32)sess_num, 0x0000ffff);
  7054. gen_and(b0, b1);
  7055. b0 = b1;
  7056. }
  7057. /*
  7058. * The "network-layer" protocol is PPPoE, which has a 6-byte
  7059. * PPPoE header, followed by a PPP packet.
  7060. *
  7061. * There is no HDLC encapsulation for the PPP packet (it's
  7062. * encapsulated in PPPoES instead), so the link-layer type
  7063. * starts at the first byte of the PPP packet. For PPPoE,
  7064. * that offset is relative to the beginning of the total
  7065. * link-layer payload, including any 802.2 LLC header, so
  7066. * it's 6 bytes past off_nl.
  7067. */
  7068. off_linktype = off_nl + 6;
  7069. /*
  7070. * The network-layer offsets are relative to the beginning
  7071. * of the MAC-layer payload; that's past the 6-byte
  7072. * PPPoE header and the 2-byte PPP header.
  7073. */
  7074. off_nl = 6+2;
  7075. off_nl_nosnap = 6+2;
  7076. return b0;
  7077. }
  7078. struct block *
  7079. gen_atmfield_code(atmfield, jvalue, jtype, reverse)
  7080. int atmfield;
  7081. bpf_int32 jvalue;
  7082. bpf_u_int32 jtype;
  7083. int reverse;
  7084. {
  7085. struct block *b0;
  7086. switch (atmfield) {
  7087. case A_VPI:
  7088. if (!is_atm)
  7089. bpf_error("'vpi' supported only on raw ATM");
  7090. if (off_vpi == (u_int)-1)
  7091. abort();
  7092. b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
  7093. reverse, jvalue);
  7094. break;
  7095. case A_VCI:
  7096. if (!is_atm)
  7097. bpf_error("'vci' supported only on raw ATM");
  7098. if (off_vci == (u_int)-1)
  7099. abort();
  7100. b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
  7101. reverse, jvalue);
  7102. break;
  7103. case A_PROTOTYPE:
  7104. if (off_proto == (u_int)-1)
  7105. abort(); /* XXX - this isn't on FreeBSD */
  7106. b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
  7107. reverse, jvalue);
  7108. break;
  7109. case A_MSGTYPE:
  7110. if (off_payload == (u_int)-1)
  7111. abort();
  7112. b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
  7113. 0xffffffff, jtype, reverse, jvalue);
  7114. break;
  7115. case A_CALLREFTYPE:
  7116. if (!is_atm)
  7117. bpf_error("'callref' supported only on raw ATM");
  7118. if (off_proto == (u_int)-1)
  7119. abort();
  7120. b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
  7121. jtype, reverse, jvalue);
  7122. break;
  7123. default:
  7124. abort();
  7125. }
  7126. return b0;
  7127. }
  7128. struct block *
  7129. gen_atmtype_abbrev(type)
  7130. int type;
  7131. {
  7132. struct block *b0, *b1;
  7133. switch (type) {
  7134. case A_METAC:
  7135. /* Get all packets in Meta signalling Circuit */
  7136. if (!is_atm)
  7137. bpf_error("'metac' supported only on raw ATM");
  7138. b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
  7139. b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
  7140. gen_and(b0, b1);
  7141. break;
  7142. case A_BCC:
  7143. /* Get all packets in Broadcast Circuit*/
  7144. if (!is_atm)
  7145. bpf_error("'bcc' supported only on raw ATM");
  7146. b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
  7147. b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
  7148. gen_and(b0, b1);
  7149. break;
  7150. case A_OAMF4SC:
  7151. /* Get all cells in Segment OAM F4 circuit*/
  7152. if (!is_atm)
  7153. bpf_error("'oam4sc' supported only on raw ATM");
  7154. b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
  7155. b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
  7156. gen_and(b0, b1);
  7157. break;
  7158. case A_OAMF4EC:
  7159. /* Get all cells in End-to-End OAM F4 Circuit*/
  7160. if (!is_atm)
  7161. bpf_error("'oam4ec' supported only on raw ATM");
  7162. b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
  7163. b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
  7164. gen_and(b0, b1);
  7165. break;
  7166. case A_SC:
  7167. /* Get all packets in connection Signalling Circuit */
  7168. if (!is_atm)
  7169. bpf_error("'sc' supported only on raw ATM");
  7170. b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
  7171. b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
  7172. gen_and(b0, b1);
  7173. break;
  7174. case A_ILMIC:
  7175. /* Get all packets in ILMI Circuit */
  7176. if (!is_atm)
  7177. bpf_error("'ilmic' supported only on raw ATM");
  7178. b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
  7179. b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
  7180. gen_and(b0, b1);
  7181. break;
  7182. case A_LANE:
  7183. /* Get all LANE packets */
  7184. if (!is_atm)
  7185. bpf_error("'lane' supported only on raw ATM");
  7186. b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
  7187. /*
  7188. * Arrange that all subsequent tests assume LANE
  7189. * rather than LLC-encapsulated packets, and set
  7190. * the offsets appropriately for LANE-encapsulated
  7191. * Ethernet.
  7192. *
  7193. * "off_mac" is the offset of the Ethernet header,
  7194. * which is 2 bytes past the ATM pseudo-header
  7195. * (skipping the pseudo-header and 2-byte LE Client
  7196. * field). The other offsets are Ethernet offsets
  7197. * relative to "off_mac".
  7198. */
  7199. is_lane = 1;
  7200. off_mac = off_payload + 2; /* MAC header */
  7201. off_linktype = off_mac + 12;
  7202. off_macpl = off_mac + 14; /* Ethernet */
  7203. off_nl = 0; /* Ethernet II */
  7204. off_nl_nosnap = 3; /* 802.3+802.2 */
  7205. break;
  7206. case A_LLC:
  7207. /* Get all LLC-encapsulated packets */
  7208. if (!is_atm)
  7209. bpf_error("'llc' supported only on raw ATM");
  7210. b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
  7211. is_lane = 0;
  7212. break;
  7213. default:
  7214. abort();
  7215. }
  7216. return b1;
  7217. }
  7218. /*
  7219. * Filtering for MTP2 messages based on li value
  7220. * FISU, length is null
  7221. * LSSU, length is 1 or 2
  7222. * MSU, length is 3 or more
  7223. * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
  7224. */
  7225. struct block *
  7226. gen_mtp2type_abbrev(type)
  7227. int type;
  7228. {
  7229. struct block *b0, *b1;
  7230. switch (type) {
  7231. case M_FISU:
  7232. if ( (linktype != DLT_MTP2) &&
  7233. (linktype != DLT_ERF) &&
  7234. (linktype != DLT_MTP2_WITH_PHDR) )
  7235. bpf_error("'fisu' supported only on MTP2");
  7236. /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
  7237. b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
  7238. break;
  7239. case M_LSSU:
  7240. if ( (linktype != DLT_MTP2) &&
  7241. (linktype != DLT_ERF) &&
  7242. (linktype != DLT_MTP2_WITH_PHDR) )
  7243. bpf_error("'lssu' supported only on MTP2");
  7244. b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
  7245. b1 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
  7246. gen_and(b1, b0);
  7247. break;
  7248. case M_MSU:
  7249. if ( (linktype != DLT_MTP2) &&
  7250. (linktype != DLT_ERF) &&
  7251. (linktype != DLT_MTP2_WITH_PHDR) )
  7252. bpf_error("'msu' supported only on MTP2");
  7253. b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
  7254. break;
  7255. case MH_FISU:
  7256. if ( (linktype != DLT_MTP2) &&
  7257. (linktype != DLT_ERF) &&
  7258. (linktype != DLT_MTP2_WITH_PHDR) )
  7259. bpf_error("'hfisu' supported only on MTP2_HSL");
  7260. /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
  7261. b0 = gen_ncmp(OR_PACKET, off_li_hsl, BPF_H, 0xff80, BPF_JEQ, 0, 0);
  7262. break;
  7263. case MH_LSSU:
  7264. if ( (linktype != DLT_MTP2) &&
  7265. (linktype != DLT_ERF) &&
  7266. (linktype != DLT_MTP2_WITH_PHDR) )
  7267. bpf_error("'hlssu' supported only on MTP2_HSL");
  7268. b0 = gen_ncmp(OR_PACKET, off_li_hsl, BPF_H, 0xff80, BPF_JGT, 1, 0x0100);
  7269. b1 = gen_ncmp(OR_PACKET, off_li_hsl, BPF_H, 0xff80, BPF_JGT, 0, 0);
  7270. gen_and(b1, b0);
  7271. break;
  7272. case MH_MSU:
  7273. if ( (linktype != DLT_MTP2) &&
  7274. (linktype != DLT_ERF) &&
  7275. (linktype != DLT_MTP2_WITH_PHDR) )
  7276. bpf_error("'hmsu' supported only on MTP2_HSL");
  7277. b0 = gen_ncmp(OR_PACKET, off_li_hsl, BPF_H, 0xff80, BPF_JGT, 0, 0x0100);
  7278. break;
  7279. default:
  7280. abort();
  7281. }
  7282. return b0;
  7283. }
  7284. struct block *
  7285. gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
  7286. int mtp3field;
  7287. bpf_u_int32 jvalue;
  7288. bpf_u_int32 jtype;
  7289. int reverse;
  7290. {
  7291. struct block *b0;
  7292. bpf_u_int32 val1 , val2 , val3;
  7293. u_int newoff_sio=off_sio;
  7294. u_int newoff_opc=off_opc;
  7295. u_int newoff_dpc=off_dpc;
  7296. u_int newoff_sls=off_sls;
  7297. switch (mtp3field) {
  7298. case MH_SIO:
  7299. newoff_sio += 3; /* offset for MTP2_HSL */
  7300. /* FALLTHROUGH */
  7301. case M_SIO:
  7302. if (off_sio == (u_int)-1)
  7303. bpf_error("'sio' supported only on SS7");
  7304. /* sio coded on 1 byte so max value 255 */
  7305. if(jvalue > 255)
  7306. bpf_error("sio value %u too big; max value = 255",
  7307. jvalue);
  7308. b0 = gen_ncmp(OR_PACKET, newoff_sio, BPF_B, 0xffffffff,
  7309. (u_int)jtype, reverse, (u_int)jvalue);
  7310. break;
  7311. case MH_OPC:
  7312. newoff_opc+=3;
  7313. case M_OPC:
  7314. if (off_opc == (u_int)-1)
  7315. bpf_error("'opc' supported only on SS7");
  7316. /* opc coded on 14 bits so max value 16383 */
  7317. if (jvalue > 16383)
  7318. bpf_error("opc value %u too big; max value = 16383",
  7319. jvalue);
  7320. /* the following instructions are made to convert jvalue
  7321. * to the form used to write opc in an ss7 message*/
  7322. val1 = jvalue & 0x00003c00;
  7323. val1 = val1 >>10;
  7324. val2 = jvalue & 0x000003fc;
  7325. val2 = val2 <<6;
  7326. val3 = jvalue & 0x00000003;
  7327. val3 = val3 <<22;
  7328. jvalue = val1 + val2 + val3;
  7329. b0 = gen_ncmp(OR_PACKET, newoff_opc, BPF_W, 0x00c0ff0f,
  7330. (u_int)jtype, reverse, (u_int)jvalue);
  7331. break;
  7332. case MH_DPC:
  7333. newoff_dpc += 3;
  7334. /* FALLTHROUGH */
  7335. case M_DPC:
  7336. if (off_dpc == (u_int)-1)
  7337. bpf_error("'dpc' supported only on SS7");
  7338. /* dpc coded on 14 bits so max value 16383 */
  7339. if (jvalue > 16383)
  7340. bpf_error("dpc value %u too big; max value = 16383",
  7341. jvalue);
  7342. /* the following instructions are made to convert jvalue
  7343. * to the forme used to write dpc in an ss7 message*/
  7344. val1 = jvalue & 0x000000ff;
  7345. val1 = val1 << 24;
  7346. val2 = jvalue & 0x00003f00;
  7347. val2 = val2 << 8;
  7348. jvalue = val1 + val2;
  7349. b0 = gen_ncmp(OR_PACKET, newoff_dpc, BPF_W, 0xff3f0000,
  7350. (u_int)jtype, reverse, (u_int)jvalue);
  7351. break;
  7352. case MH_SLS:
  7353. newoff_sls+=3;
  7354. case M_SLS:
  7355. if (off_sls == (u_int)-1)
  7356. bpf_error("'sls' supported only on SS7");
  7357. /* sls coded on 4 bits so max value 15 */
  7358. if (jvalue > 15)
  7359. bpf_error("sls value %u too big; max value = 15",
  7360. jvalue);
  7361. /* the following instruction is made to convert jvalue
  7362. * to the forme used to write sls in an ss7 message*/
  7363. jvalue = jvalue << 4;
  7364. b0 = gen_ncmp(OR_PACKET, newoff_sls, BPF_B, 0xf0,
  7365. (u_int)jtype,reverse, (u_int)jvalue);
  7366. break;
  7367. default:
  7368. abort();
  7369. }
  7370. return b0;
  7371. }
  7372. static struct block *
  7373. gen_msg_abbrev(type)
  7374. int type;
  7375. {
  7376. struct block *b1;
  7377. /*
  7378. * Q.2931 signalling protocol messages for handling virtual circuits
  7379. * establishment and teardown
  7380. */
  7381. switch (type) {
  7382. case A_SETUP:
  7383. b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
  7384. break;
  7385. case A_CALLPROCEED:
  7386. b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
  7387. break;
  7388. case A_CONNECT:
  7389. b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
  7390. break;
  7391. case A_CONNECTACK:
  7392. b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
  7393. break;
  7394. case A_RELEASE:
  7395. b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
  7396. break;
  7397. case A_RELEASE_DONE:
  7398. b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
  7399. break;
  7400. default:
  7401. abort();
  7402. }
  7403. return b1;
  7404. }
  7405. struct block *
  7406. gen_atmmulti_abbrev(type)
  7407. int type;
  7408. {
  7409. struct block *b0, *b1;
  7410. switch (type) {
  7411. case A_OAM:
  7412. if (!is_atm)
  7413. bpf_error("'oam' supported only on raw ATM");
  7414. b1 = gen_atmmulti_abbrev(A_OAMF4);
  7415. break;
  7416. case A_OAMF4:
  7417. if (!is_atm)
  7418. bpf_error("'oamf4' supported only on raw ATM");
  7419. /* OAM F4 type */
  7420. b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
  7421. b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
  7422. gen_or(b0, b1);
  7423. b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
  7424. gen_and(b0, b1);
  7425. break;
  7426. case A_CONNECTMSG:
  7427. /*
  7428. * Get Q.2931 signalling messages for switched
  7429. * virtual connection
  7430. */
  7431. if (!is_atm)
  7432. bpf_error("'connectmsg' supported only on raw ATM");
  7433. b0 = gen_msg_abbrev(A_SETUP);
  7434. b1 = gen_msg_abbrev(A_CALLPROCEED);
  7435. gen_or(b0, b1);
  7436. b0 = gen_msg_abbrev(A_CONNECT);
  7437. gen_or(b0, b1);
  7438. b0 = gen_msg_abbrev(A_CONNECTACK);
  7439. gen_or(b0, b1);
  7440. b0 = gen_msg_abbrev(A_RELEASE);
  7441. gen_or(b0, b1);
  7442. b0 = gen_msg_abbrev(A_RELEASE_DONE);
  7443. gen_or(b0, b1);
  7444. b0 = gen_atmtype_abbrev(A_SC);
  7445. gen_and(b0, b1);
  7446. break;
  7447. case A_METACONNECT:
  7448. if (!is_atm)
  7449. bpf_error("'metaconnect' supported only on raw ATM");
  7450. b0 = gen_msg_abbrev(A_SETUP);
  7451. b1 = gen_msg_abbrev(A_CALLPROCEED);
  7452. gen_or(b0, b1);
  7453. b0 = gen_msg_abbrev(A_CONNECT);
  7454. gen_or(b0, b1);
  7455. b0 = gen_msg_abbrev(A_RELEASE);
  7456. gen_or(b0, b1);
  7457. b0 = gen_msg_abbrev(A_RELEASE_DONE);
  7458. gen_or(b0, b1);
  7459. b0 = gen_atmtype_abbrev(A_METAC);
  7460. gen_and(b0, b1);
  7461. break;
  7462. default:
  7463. abort();
  7464. }
  7465. return b1;
  7466. }