PageRenderTime 189ms CodeModel.GetById 103ms 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

Large files files are truncated, but you can click here to view the full 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 SS…

Large files files are truncated, but you can click here to view the full file