PageRenderTime 80ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/winpcap/wpcap/libpcap/gencode.c

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