PageRenderTime 90ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/contrib/libpcap/gencode.c

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