PageRenderTime 87ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/gencode.c

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