PageRenderTime 86ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/libpcap/gencode.c

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