PageRenderTime 104ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/libpcap/gencode.c

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