PageRenderTime 71ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/gencode.c

https://github.com/Longinus00/libpcap
C | 8536 lines | 5201 code | 1074 blank | 2261 comment | 551 complexity | 5aacc376ef720bc61ce850915b6c971b MD5 | raw file

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.309 2008-12-23 20:13:29 guy Exp $ (LBL)";
  25. #endif
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #ifdef WIN32
  30. #include <pcap-stdinc.h>
  31. #else /* WIN32 */
  32. #if HAVE_INTTYPES_H
  33. #include <inttypes.h>
  34. #elif HAVE_STDINT_H
  35. #include <stdint.h>
  36. #endif
  37. #ifdef HAVE_SYS_BITYPES_H
  38. #include <sys/bitypes.h>
  39. #endif
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #endif /* WIN32 */
  43. /*
  44. * XXX - why was this included even on UNIX?
  45. */
  46. #ifdef __MINGW32__
  47. #include "ip6_misc.h"
  48. #endif
  49. #ifndef WIN32
  50. #ifdef __NetBSD__
  51. #include <sys/param.h>
  52. #endif
  53. #include <netinet/in.h>
  54. #include <arpa/inet.h>
  55. #endif /* WIN32 */
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #include <memory.h>
  59. #include <setjmp.h>
  60. #include <stdarg.h>
  61. #ifdef MSDOS
  62. #include "pcap-dos.h"
  63. #endif
  64. #include "pcap-int.h"
  65. #include "ethertype.h"
  66. #include "nlpid.h"
  67. #include "llc.h"
  68. #include "gencode.h"
  69. #include "ieee80211.h"
  70. #include "atmuni31.h"
  71. #include "sunatmpos.h"
  72. #include "ppp.h"
  73. #include "pcap/sll.h"
  74. #include "pcap/ipnet.h"
  75. #include "arcnet.h"
  76. #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
  77. #include <linux/types.h>
  78. #include <linux/if_packet.h>
  79. #include <linux/filter.h>
  80. #endif
  81. #ifdef HAVE_NET_PFVAR_H
  82. #include <sys/socket.h>
  83. #include <net/if.h>
  84. #include <net/pfvar.h>
  85. #include <net/if_pflog.h>
  86. #endif
  87. #ifndef offsetof
  88. #define offsetof(s, e) ((size_t)&((s *)0)->e)
  89. #endif
  90. #ifdef INET6
  91. #ifndef WIN32
  92. #include <netdb.h> /* for "struct addrinfo" */
  93. #endif /* WIN32 */
  94. #endif /*INET6*/
  95. #include <pcap/namedb.h>
  96. #define ETHERMTU 1500
  97. #ifndef IPPROTO_HOPOPTS
  98. #define IPPROTO_HOPOPTS 0
  99. #endif
  100. #ifndef IPPROTO_ROUTING
  101. #define IPPROTO_ROUTING 43
  102. #endif
  103. #ifndef IPPROTO_FRAGMENT
  104. #define IPPROTO_FRAGMENT 44
  105. #endif
  106. #ifndef IPPROTO_DSTOPTS
  107. #define IPPROTO_DSTOPTS 60
  108. #endif
  109. #ifndef IPPROTO_SCTP
  110. #define IPPROTO_SCTP 132
  111. #endif
  112. #ifdef HAVE_OS_PROTO_H
  113. #include "os-proto.h"
  114. #endif
  115. #define JMP(c) ((c)|BPF_JMP|BPF_K)
  116. /* Locals */
  117. static jmp_buf top_ctx;
  118. static pcap_t *bpf_pcap;
  119. /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
  120. #ifdef WIN32
  121. static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
  122. #else
  123. static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
  124. #endif
  125. /* XXX */
  126. static int pcap_fddipad;
  127. /* VARARGS */
  128. void
  129. bpf_error(const char *fmt, ...)
  130. {
  131. va_list ap;
  132. va_start(ap, fmt);
  133. if (bpf_pcap != NULL)
  134. (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
  135. fmt, ap);
  136. va_end(ap);
  137. longjmp(top_ctx, 1);
  138. /* NOTREACHED */
  139. }
  140. static void init_linktype(pcap_t *);
  141. static void init_regs(void);
  142. static int alloc_reg(void);
  143. static void free_reg(int);
  144. static struct block *root;
  145. /*
  146. * Value passed to gen_load_a() to indicate what the offset argument
  147. * is relative to.
  148. */
  149. enum e_offrel {
  150. OR_PACKET, /* relative to the beginning of the packet */
  151. OR_LINK, /* relative to the beginning of the link-layer header */
  152. OR_MACPL, /* relative to the end of the MAC-layer header */
  153. OR_NET, /* relative to the network-layer header */
  154. OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
  155. OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
  156. OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
  157. };
  158. #ifdef INET6
  159. /*
  160. * As errors are handled by a longjmp, anything allocated must be freed
  161. * in the longjmp handler, so it must be reachable from that handler.
  162. * One thing that's allocated is the result of pcap_nametoaddrinfo();
  163. * it must be freed with freeaddrinfo(). This variable points to any
  164. * addrinfo structure that would need to be freed.
  165. */
  166. static struct addrinfo *ai;
  167. #endif
  168. /*
  169. * We divy out chunks of memory rather than call malloc each time so
  170. * we don't have to worry about leaking memory. It's probably
  171. * not a big deal if all this memory was wasted but if this ever
  172. * goes into a library that would probably not be a good idea.
  173. *
  174. * XXX - this *is* in a library....
  175. */
  176. #define NCHUNKS 16
  177. #define CHUNK0SIZE 1024
  178. struct chunk {
  179. u_int n_left;
  180. void *m;
  181. };
  182. static struct chunk chunks[NCHUNKS];
  183. static int cur_chunk;
  184. static void *newchunk(u_int);
  185. static void freechunks(void);
  186. static inline struct block *new_block(int);
  187. static inline struct slist *new_stmt(int);
  188. static struct block *gen_retblk(int);
  189. static inline void syntax(void);
  190. static void backpatch(struct block *, struct block *);
  191. static void merge(struct block *, struct block *);
  192. static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
  193. static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
  194. static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
  195. static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
  196. static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
  197. static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
  198. bpf_u_int32);
  199. static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
  200. static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
  201. bpf_u_int32, bpf_u_int32, int, bpf_int32);
  202. static struct slist *gen_load_llrel(u_int, u_int);
  203. static struct slist *gen_load_macplrel(u_int, u_int);
  204. static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
  205. static struct slist *gen_loadx_iphdrlen(void);
  206. static struct block *gen_uncond(int);
  207. static inline struct block *gen_true(void);
  208. static inline struct block *gen_false(void);
  209. static struct block *gen_ether_linktype(int);
  210. static struct block *gen_ipnet_linktype(int);
  211. static struct block *gen_linux_sll_linktype(int);
  212. static struct slist *gen_load_prism_llprefixlen(void);
  213. static struct slist *gen_load_avs_llprefixlen(void);
  214. static struct slist *gen_load_radiotap_llprefixlen(void);
  215. static struct slist *gen_load_ppi_llprefixlen(void);
  216. static void insert_compute_vloffsets(struct block *);
  217. static struct slist *gen_llprefixlen(void);
  218. static struct slist *gen_off_macpl(void);
  219. static int ethertype_to_ppptype(int);
  220. static struct block *gen_linktype(int);
  221. static struct block *gen_snap(bpf_u_int32, bpf_u_int32);
  222. static struct block *gen_llc_linktype(int);
  223. static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
  224. #ifdef INET6
  225. static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
  226. #endif
  227. static struct block *gen_ahostop(const u_char *, int);
  228. static struct block *gen_ehostop(const u_char *, int);
  229. static struct block *gen_fhostop(const u_char *, int);
  230. static struct block *gen_thostop(const u_char *, int);
  231. static struct block *gen_wlanhostop(const u_char *, int);
  232. static struct block *gen_ipfchostop(const u_char *, int);
  233. static struct block *gen_dnhostop(bpf_u_int32, int);
  234. static struct block *gen_mpls_linktype(int);
  235. static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
  236. #ifdef INET6
  237. static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
  238. #endif
  239. #ifndef INET6
  240. static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
  241. #endif
  242. static struct block *gen_ipfrag(void);
  243. static struct block *gen_portatom(int, bpf_int32);
  244. static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
  245. static struct block *gen_portatom6(int, bpf_int32);
  246. static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
  247. struct block *gen_portop(int, int, int);
  248. static struct block *gen_port(int, int, int);
  249. struct block *gen_portrangeop(int, int, int, int);
  250. static struct block *gen_portrange(int, int, int, int);
  251. struct block *gen_portop6(int, int, int);
  252. static struct block *gen_port6(int, int, int);
  253. struct block *gen_portrangeop6(int, int, int, int);
  254. static struct block *gen_portrange6(int, int, int, int);
  255. static int lookup_proto(const char *, int);
  256. static struct block *gen_protochain(int, int, int);
  257. static struct block *gen_proto(int, int, int);
  258. static struct slist *xfer_to_x(struct arth *);
  259. static struct slist *xfer_to_a(struct arth *);
  260. static struct block *gen_mac_multicast(int);
  261. static struct block *gen_len(int, int);
  262. static struct block *gen_check_802_11_data_frame(void);
  263. static struct block *gen_ppi_dlt_check(void);
  264. static struct block *gen_msg_abbrev(int type);
  265. static void *
  266. newchunk(n)
  267. u_int n;
  268. {
  269. struct chunk *cp;
  270. int k;
  271. size_t size;
  272. #ifndef __NetBSD__
  273. /* XXX Round up to nearest long. */
  274. n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
  275. #else
  276. /* XXX Round up to structure boundary. */
  277. n = ALIGN(n);
  278. #endif
  279. cp = &chunks[cur_chunk];
  280. if (n > cp->n_left) {
  281. ++cp, k = ++cur_chunk;
  282. if (k >= NCHUNKS)
  283. bpf_error("out of memory");
  284. size = CHUNK0SIZE << k;
  285. cp->m = (void *)malloc(size);
  286. if (cp->m == NULL)
  287. bpf_error("out of memory");
  288. memset((char *)cp->m, 0, size);
  289. cp->n_left = size;
  290. if (n > size)
  291. bpf_error("out of memory");
  292. }
  293. cp->n_left -= n;
  294. return (void *)((char *)cp->m + cp->n_left);
  295. }
  296. static void
  297. freechunks()
  298. {
  299. int i;
  300. cur_chunk = 0;
  301. for (i = 0; i < NCHUNKS; ++i)
  302. if (chunks[i].m != NULL) {
  303. free(chunks[i].m);
  304. chunks[i].m = NULL;
  305. }
  306. }
  307. /*
  308. * A strdup whose allocations are freed after code generation is over.
  309. */
  310. char *
  311. sdup(s)
  312. register const char *s;
  313. {
  314. int n = strlen(s) + 1;
  315. char *cp = newchunk(n);
  316. strlcpy(cp, s, n);
  317. return (cp);
  318. }
  319. static inline struct block *
  320. new_block(code)
  321. int code;
  322. {
  323. struct block *p;
  324. p = (struct block *)newchunk(sizeof(*p));
  325. p->s.code = code;
  326. p->head = p;
  327. return p;
  328. }
  329. static inline struct slist *
  330. new_stmt(code)
  331. int code;
  332. {
  333. struct slist *p;
  334. p = (struct slist *)newchunk(sizeof(*p));
  335. p->s.code = code;
  336. return p;
  337. }
  338. static struct block *
  339. gen_retblk(v)
  340. int v;
  341. {
  342. struct block *b = new_block(BPF_RET|BPF_K);
  343. b->s.k = v;
  344. return b;
  345. }
  346. static inline void
  347. syntax()
  348. {
  349. bpf_error("syntax error in filter expression");
  350. }
  351. static bpf_u_int32 netmask;
  352. static int snaplen;
  353. int no_optimize;
  354. #ifdef WIN32
  355. static int
  356. pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
  357. const char *buf, int optimize, bpf_u_int32 mask);
  358. int
  359. pcap_compile(pcap_t *p, struct bpf_program *program,
  360. const char *buf, int optimize, bpf_u_int32 mask)
  361. {
  362. int result;
  363. EnterCriticalSection(&g_PcapCompileCriticalSection);
  364. result = pcap_compile_unsafe(p, program, buf, optimize, mask);
  365. LeaveCriticalSection(&g_PcapCompileCriticalSection);
  366. return result;
  367. }
  368. static int
  369. pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
  370. const char *buf, int optimize, bpf_u_int32 mask)
  371. #else /* WIN32 */
  372. int
  373. pcap_compile(pcap_t *p, struct bpf_program *program,
  374. const char *buf, int optimize, bpf_u_int32 mask)
  375. #endif /* WIN32 */
  376. {
  377. extern int n_errors;
  378. const char * volatile xbuf = buf;
  379. u_int len;
  380. /*
  381. * If this pcap_t hasn't been activated, it doesn't have a
  382. * link-layer type, so we can't use it.
  383. */
  384. if (!p->activated) {
  385. snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  386. "not-yet-activated pcap_t passed to pcap_compile");
  387. return (-1);
  388. }
  389. no_optimize = 0;
  390. n_errors = 0;
  391. root = NULL;
  392. bpf_pcap = p;
  393. init_regs();
  394. if (setjmp(top_ctx)) {
  395. #ifdef INET6
  396. if (ai != NULL) {
  397. freeaddrinfo(ai);
  398. ai = NULL;
  399. }
  400. #endif
  401. lex_cleanup();
  402. freechunks();
  403. return (-1);
  404. }
  405. netmask = mask;
  406. snaplen = pcap_snapshot(p);
  407. if (snaplen == 0) {
  408. snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  409. "snaplen of 0 rejects all packets");
  410. return -1;
  411. }
  412. lex_init(xbuf ? xbuf : "");
  413. init_linktype(p);
  414. (void)pcap_parse();
  415. if (n_errors)
  416. syntax();
  417. if (root == NULL)
  418. root = gen_retblk(snaplen);
  419. if (optimize && !no_optimize) {
  420. bpf_optimize(&root);
  421. if (root == NULL ||
  422. (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
  423. bpf_error("expression rejects all packets");
  424. }
  425. program->bf_insns = icode_to_fcode(root, &len);
  426. program->bf_len = len;
  427. lex_cleanup();
  428. freechunks();
  429. return (0);
  430. }
  431. /*
  432. * entry point for using the compiler with no pcap open
  433. * pass in all the stuff that is needed explicitly instead.
  434. */
  435. int
  436. pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
  437. struct bpf_program *program,
  438. const char *buf, int optimize, bpf_u_int32 mask)
  439. {
  440. pcap_t *p;
  441. int ret;
  442. p = pcap_open_dead(linktype_arg, snaplen_arg);
  443. if (p == NULL)
  444. return (-1);
  445. ret = pcap_compile(p, program, buf, optimize, mask);
  446. pcap_close(p);
  447. return (ret);
  448. }
  449. /*
  450. * Clean up a "struct bpf_program" by freeing all the memory allocated
  451. * in it.
  452. */
  453. void
  454. pcap_freecode(struct bpf_program *program)
  455. {
  456. program->bf_len = 0;
  457. if (program->bf_insns != NULL) {
  458. free((char *)program->bf_insns);
  459. program->bf_insns = NULL;
  460. }
  461. }
  462. /*
  463. * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
  464. * which of the jt and jf fields has been resolved and which is a pointer
  465. * back to another unresolved block (or nil). At least one of the fields
  466. * in each block is already resolved.
  467. */
  468. static void
  469. backpatch(list, target)
  470. struct block *list, *target;
  471. {
  472. struct block *next;
  473. while (list) {
  474. if (!list->sense) {
  475. next = JT(list);
  476. JT(list) = target;
  477. } else {
  478. next = JF(list);
  479. JF(list) = target;
  480. }
  481. list = next;
  482. }
  483. }
  484. /*
  485. * Merge the lists in b0 and b1, using the 'sense' field to indicate
  486. * which of jt and jf is the link.
  487. */
  488. static void
  489. merge(b0, b1)
  490. struct block *b0, *b1;
  491. {
  492. register struct block **p = &b0;
  493. /* Find end of list. */
  494. while (*p)
  495. p = !((*p)->sense) ? &JT(*p) : &JF(*p);
  496. /* Concatenate the lists. */
  497. *p = b1;
  498. }
  499. void
  500. finish_parse(p)
  501. struct block *p;
  502. {
  503. struct block *ppi_dlt_check;
  504. /*
  505. * Insert before the statements of the first (root) block any
  506. * statements needed to load the lengths of any variable-length
  507. * headers into registers.
  508. *
  509. * XXX - a fancier strategy would be to insert those before the
  510. * statements of all blocks that use those lengths and that
  511. * have no predecessors that use them, so that we only compute
  512. * the lengths if we need them. There might be even better
  513. * approaches than that.
  514. *
  515. * However, those strategies would be more complicated, and
  516. * as we don't generate code to compute a length if the
  517. * program has no tests that use the length, and as most
  518. * tests will probably use those lengths, we would just
  519. * postpone computing the lengths so that it's not done
  520. * for tests that fail early, and it's not clear that's
  521. * worth the effort.
  522. */
  523. insert_compute_vloffsets(p->head);
  524. /*
  525. * For DLT_PPI captures, generate a check of the per-packet
  526. * DLT value to make sure it's DLT_IEEE802_11.
  527. */
  528. ppi_dlt_check = gen_ppi_dlt_check();
  529. if (ppi_dlt_check != NULL)
  530. gen_and(ppi_dlt_check, p);
  531. backpatch(p, gen_retblk(snaplen));
  532. p->sense = !p->sense;
  533. backpatch(p, gen_retblk(0));
  534. root = p->head;
  535. }
  536. void
  537. gen_and(b0, b1)
  538. struct block *b0, *b1;
  539. {
  540. backpatch(b0, b1->head);
  541. b0->sense = !b0->sense;
  542. b1->sense = !b1->sense;
  543. merge(b1, b0);
  544. b1->sense = !b1->sense;
  545. b1->head = b0->head;
  546. }
  547. void
  548. gen_or(b0, b1)
  549. struct block *b0, *b1;
  550. {
  551. b0->sense = !b0->sense;
  552. backpatch(b0, b1->head);
  553. b0->sense = !b0->sense;
  554. merge(b1, b0);
  555. b1->head = b0->head;
  556. }
  557. void
  558. gen_not(b)
  559. struct block *b;
  560. {
  561. b->sense = !b->sense;
  562. }
  563. static struct block *
  564. gen_cmp(offrel, offset, size, v)
  565. enum e_offrel offrel;
  566. u_int offset, size;
  567. bpf_int32 v;
  568. {
  569. return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
  570. }
  571. static struct block *
  572. gen_cmp_gt(offrel, offset, size, v)
  573. enum e_offrel offrel;
  574. u_int offset, size;
  575. bpf_int32 v;
  576. {
  577. return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
  578. }
  579. static struct block *
  580. gen_cmp_ge(offrel, offset, size, v)
  581. enum e_offrel offrel;
  582. u_int offset, size;
  583. bpf_int32 v;
  584. {
  585. return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
  586. }
  587. static struct block *
  588. gen_cmp_lt(offrel, offset, size, v)
  589. enum e_offrel offrel;
  590. u_int offset, size;
  591. bpf_int32 v;
  592. {
  593. return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
  594. }
  595. static struct block *
  596. gen_cmp_le(offrel, offset, size, v)
  597. enum e_offrel offrel;
  598. u_int offset, size;
  599. bpf_int32 v;
  600. {
  601. return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
  602. }
  603. static struct block *
  604. gen_mcmp(offrel, offset, size, v, mask)
  605. enum e_offrel offrel;
  606. u_int offset, size;
  607. bpf_int32 v;
  608. bpf_u_int32 mask;
  609. {
  610. return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
  611. }
  612. static struct block *
  613. gen_bcmp(offrel, offset, size, v)
  614. enum e_offrel offrel;
  615. register u_int offset, size;
  616. register const u_char *v;
  617. {
  618. register struct block *b, *tmp;
  619. b = NULL;
  620. while (size >= 4) {
  621. register const u_char *p = &v[size - 4];
  622. bpf_int32 w = ((bpf_int32)p[0] << 24) |
  623. ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
  624. tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
  625. if (b != NULL)
  626. gen_and(b, tmp);
  627. b = tmp;
  628. size -= 4;
  629. }
  630. while (size >= 2) {
  631. register const u_char *p = &v[size - 2];
  632. bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
  633. tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
  634. if (b != NULL)
  635. gen_and(b, tmp);
  636. b = tmp;
  637. size -= 2;
  638. }
  639. if (size > 0) {
  640. tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
  641. if (b != NULL)
  642. gen_and(b, tmp);
  643. b = tmp;
  644. }
  645. return b;
  646. }
  647. /*
  648. * AND the field of size "size" at offset "offset" relative to the header
  649. * specified by "offrel" with "mask", and compare it with the value "v"
  650. * with the test specified by "jtype"; if "reverse" is true, the test
  651. * should test the opposite of "jtype".
  652. */
  653. static struct block *
  654. gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
  655. enum e_offrel offrel;
  656. bpf_int32 v;
  657. bpf_u_int32 offset, size, mask, jtype;
  658. int reverse;
  659. {
  660. struct slist *s, *s2;
  661. struct block *b;
  662. s = gen_load_a(offrel, offset, size);
  663. if (mask != 0xffffffff) {
  664. s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
  665. s2->s.k = mask;
  666. sappend(s, s2);
  667. }
  668. b = new_block(JMP(jtype));
  669. b->stmts = s;
  670. b->s.k = v;
  671. if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
  672. gen_not(b);
  673. return b;
  674. }
  675. /*
  676. * Various code constructs need to know the layout of the data link
  677. * layer. These variables give the necessary offsets from the beginning
  678. * of the packet data.
  679. */
  680. /*
  681. * This is the offset of the beginning of the link-layer header from
  682. * the beginning of the raw packet data.
  683. *
  684. * It's usually 0, except for 802.11 with a fixed-length radio header.
  685. * (For 802.11 with a variable-length radio header, we have to generate
  686. * code to compute that offset; off_ll is 0 in that case.)
  687. */
  688. static u_int off_ll;
  689. /*
  690. * If there's a variable-length header preceding the link-layer header,
  691. * "reg_off_ll" is the register number for a register containing the
  692. * length of that header, and therefore the offset of the link-layer
  693. * header from the beginning of the raw packet data. Otherwise,
  694. * "reg_off_ll" is -1.
  695. */
  696. static int reg_off_ll;
  697. /*
  698. * This is the offset of the beginning of the MAC-layer header from
  699. * the beginning of the link-layer header.
  700. * It's usually 0, except for ATM LANE, where it's the offset, relative
  701. * to the beginning of the raw packet data, of the Ethernet header, and
  702. * for Ethernet with various additional information.
  703. */
  704. static u_int off_mac;
  705. /*
  706. * This is the offset of the beginning of the MAC-layer payload,
  707. * from the beginning of the raw packet data.
  708. *
  709. * I.e., it's the sum of the length of the link-layer header (without,
  710. * for example, any 802.2 LLC header, so it's the MAC-layer
  711. * portion of that header), plus any prefix preceding the
  712. * link-layer header.
  713. */
  714. static u_int off_macpl;
  715. /*
  716. * This is 1 if the offset of the beginning of the MAC-layer payload
  717. * from the beginning of the link-layer header is variable-length.
  718. */
  719. static int off_macpl_is_variable;
  720. /*
  721. * If the link layer has variable_length headers, "reg_off_macpl"
  722. * is the register number for a register containing the length of the
  723. * link-layer header plus the length of any variable-length header
  724. * preceding the link-layer header. Otherwise, "reg_off_macpl"
  725. * is -1.
  726. */
  727. static int reg_off_macpl;
  728. /*
  729. * "off_linktype" is the offset to information in the link-layer header
  730. * giving the packet type. This offset is relative to the beginning
  731. * of the link-layer header (i.e., it doesn't include off_ll).
  732. *
  733. * For Ethernet, it's the offset of the Ethernet type field.
  734. *
  735. * For link-layer types that always use 802.2 headers, it's the
  736. * offset of the LLC header.
  737. *
  738. * For PPP, it's the offset of the PPP type field.
  739. *
  740. * For Cisco HDLC, it's the offset of the CHDLC type field.
  741. *
  742. * For BSD loopback, it's the offset of the AF_ value.
  743. *
  744. * For Linux cooked sockets, it's the offset of the type field.
  745. *
  746. * It's set to -1 for no encapsulation, in which case, IP is assumed.
  747. */
  748. static u_int off_linktype;
  749. /*
  750. * TRUE if "pppoes" appeared in the filter; it causes link-layer type
  751. * checks to check the PPP header, assumed to follow a LAN-style link-
  752. * layer header and a PPPoE session header.
  753. */
  754. static int is_pppoes = 0;
  755. /*
  756. * TRUE if the link layer includes an ATM pseudo-header.
  757. */
  758. static int is_atm = 0;
  759. /*
  760. * TRUE if "lane" appeared in the filter; it causes us to generate
  761. * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
  762. */
  763. static int is_lane = 0;
  764. /*
  765. * These are offsets for the ATM pseudo-header.
  766. */
  767. static u_int off_vpi;
  768. static u_int off_vci;
  769. static u_int off_proto;
  770. /*
  771. * These are offsets for the MTP2 fields.
  772. */
  773. static u_int off_li;
  774. static u_int off_li_hsl;
  775. /*
  776. * These are offsets for the MTP3 fields.
  777. */
  778. static u_int off_sio;
  779. static u_int off_opc;
  780. static u_int off_dpc;
  781. static u_int off_sls;
  782. /*
  783. * This is the offset of the first byte after the ATM pseudo_header,
  784. * or -1 if there is no ATM pseudo-header.
  785. */
  786. static u_int off_payload;
  787. /*
  788. * These are offsets to the beginning of the network-layer header.
  789. * They are relative to the beginning of the MAC-layer payload (i.e.,
  790. * they don't include off_ll or off_macpl).
  791. *
  792. * If the link layer never uses 802.2 LLC:
  793. *
  794. * "off_nl" and "off_nl_nosnap" are the same.
  795. *
  796. * If the link layer always uses 802.2 LLC:
  797. *
  798. * "off_nl" is the offset if there's a SNAP header following
  799. * the 802.2 header;
  800. *
  801. * "off_nl_nosnap" is the offset if there's no SNAP header.
  802. *
  803. * If the link layer is Ethernet:
  804. *
  805. * "off_nl" is the offset if the packet is an Ethernet II packet
  806. * (we assume no 802.3+802.2+SNAP);
  807. *
  808. * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
  809. * with an 802.2 header following it.
  810. */
  811. static u_int off_nl;
  812. static u_int off_nl_nosnap;
  813. static int linktype;
  814. static void
  815. init_linktype(p)
  816. pcap_t *p;
  817. {
  818. linktype = pcap_datalink(p);
  819. pcap_fddipad = p->fddipad;
  820. /*
  821. * Assume it's not raw ATM with a pseudo-header, for now.
  822. */
  823. off_mac = 0;
  824. is_atm = 0;
  825. is_lane = 0;
  826. off_vpi = -1;
  827. off_vci = -1;
  828. off_proto = -1;
  829. off_payload = -1;
  830. /*
  831. * And that we're not doing PPPoE.
  832. */
  833. is_pppoes = 0;
  834. /*
  835. * And assume we're not doing SS7.
  836. */
  837. off_li = -1;
  838. off_li_hsl = -1;
  839. off_sio = -1;
  840. off_opc = -1;
  841. off_dpc = -1;
  842. off_sls = -1;
  843. /*
  844. * Also assume it's not 802.11.
  845. */
  846. off_ll = 0;
  847. off_macpl = 0;
  848. off_macpl_is_variable = 0;
  849. orig_linktype = -1;
  850. orig_nl = -1;
  851. label_stack_depth = 0;
  852. reg_off_ll = -1;
  853. reg_off_macpl = -1;
  854. switch (linktype) {
  855. case DLT_ARCNET:
  856. off_linktype = 2;
  857. off_macpl = 6;
  858. off_nl = 0; /* XXX in reality, variable! */
  859. off_nl_nosnap = 0; /* no 802.2 LLC */
  860. return;
  861. case DLT_ARCNET_LINUX:
  862. off_linktype = 4;
  863. off_macpl = 8;
  864. off_nl = 0; /* XXX in reality, variable! */
  865. off_nl_nosnap = 0; /* no 802.2 LLC */
  866. return;
  867. case DLT_EN10MB:
  868. off_linktype = 12;
  869. off_macpl = 14; /* Ethernet header length */
  870. off_nl = 0; /* Ethernet II */
  871. off_nl_nosnap = 3; /* 802.3+802.2 */
  872. return;
  873. case DLT_SLIP:
  874. /*
  875. * SLIP doesn't have a link level type. The 16 byte
  876. * header is hacked into our SLIP driver.
  877. */
  878. off_linktype = -1;
  879. off_macpl = 16;
  880. off_nl = 0;
  881. off_nl_nosnap = 0; /* no 802.2 LLC */
  882. return;
  883. case DLT_SLIP_BSDOS:
  884. /* XXX this may be the same as the DLT_PPP_BSDOS case */
  885. off_linktype = -1;
  886. /* XXX end */
  887. off_macpl = 24;
  888. off_nl = 0;
  889. off_nl_nosnap = 0; /* no 802.2 LLC */
  890. return;
  891. case DLT_NULL:
  892. case DLT_LOOP:
  893. off_linktype = 0;
  894. off_macpl = 4;
  895. off_nl = 0;
  896. off_nl_nosnap = 0; /* no 802.2 LLC */
  897. return;
  898. case DLT_ENC:
  899. off_linktype = 0;
  900. off_macpl = 12;
  901. off_nl = 0;
  902. off_nl_nosnap = 0; /* no 802.2 LLC */
  903. return;
  904. case DLT_PPP:
  905. case DLT_PPP_PPPD:
  906. case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
  907. case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
  908. off_linktype = 2;
  909. off_macpl = 4;
  910. off_nl = 0;
  911. off_nl_nosnap = 0; /* no 802.2 LLC */
  912. return;
  913. case DLT_PPP_ETHER:
  914. /*
  915. * This does no include the Ethernet header, and
  916. * only covers session state.
  917. */
  918. off_linktype = 6;
  919. off_macpl = 8;
  920. off_nl = 0;
  921. off_nl_nosnap = 0; /* no 802.2 LLC */
  922. return;
  923. case DLT_PPP_BSDOS:
  924. off_linktype = 5;
  925. off_macpl = 24;
  926. off_nl = 0;
  927. off_nl_nosnap = 0; /* no 802.2 LLC */
  928. return;
  929. case DLT_FDDI:
  930. /*
  931. * FDDI doesn't really have a link-level type field.
  932. * We set "off_linktype" to the offset of the LLC header.
  933. *
  934. * To check for Ethernet types, we assume that SSAP = SNAP
  935. * is being used and pick out the encapsulated Ethernet type.
  936. * XXX - should we generate code to check for SNAP?
  937. */
  938. off_linktype = 13;
  939. off_linktype += pcap_fddipad;
  940. off_macpl = 13; /* FDDI MAC header length */
  941. off_macpl += pcap_fddipad;
  942. off_nl = 8; /* 802.2+SNAP */
  943. off_nl_nosnap = 3; /* 802.2 */
  944. return;
  945. case DLT_IEEE802:
  946. /*
  947. * Token Ring doesn't really have a link-level type field.
  948. * We set "off_linktype" to the offset of the LLC header.
  949. *
  950. * To check for Ethernet types, we assume that SSAP = SNAP
  951. * is being used and pick out the encapsulated Ethernet type.
  952. * XXX - should we generate code to check for SNAP?
  953. *
  954. * XXX - the header is actually variable-length.
  955. * Some various Linux patched versions gave 38
  956. * as "off_linktype" and 40 as "off_nl"; however,
  957. * if a token ring packet has *no* routing
  958. * information, i.e. is not source-routed, the correct
  959. * values are 20 and 22, as they are in the vanilla code.
  960. *
  961. * A packet is source-routed iff the uppermost bit
  962. * of the first byte of the source address, at an
  963. * offset of 8, has the uppermost bit set. If the
  964. * packet is source-routed, the total number of bytes
  965. * of routing information is 2 plus bits 0x1F00 of
  966. * the 16-bit value at an offset of 14 (shifted right
  967. * 8 - figure out which byte that is).
  968. */
  969. off_linktype = 14;
  970. off_macpl = 14; /* Token Ring MAC header length */
  971. off_nl = 8; /* 802.2+SNAP */
  972. off_nl_nosnap = 3; /* 802.2 */
  973. return;
  974. case DLT_IEEE802_11:
  975. case DLT_PRISM_HEADER:
  976. case DLT_IEEE802_11_RADIO_AVS:
  977. case DLT_IEEE802_11_RADIO:
  978. /*
  979. * 802.11 doesn't really have a link-level type field.
  980. * We set "off_linktype" to the offset of the LLC header.
  981. *
  982. * To check for Ethernet types, we assume that SSAP = SNAP
  983. * is being used and pick out the encapsulated Ethernet type.
  984. * XXX - should we generate code to check for SNAP?
  985. *
  986. * We also handle variable-length radio headers here.
  987. * The Prism header is in theory variable-length, but in
  988. * practice it's always 144 bytes long. However, some
  989. * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
  990. * sometimes or always supply an AVS header, so we
  991. * have to check whether the radio header is a Prism
  992. * header or an AVS header, so, in practice, it's
  993. * variable-length.
  994. */
  995. off_linktype = 24;
  996. off_macpl = 0; /* link-layer header is variable-length */
  997. off_macpl_is_variable = 1;
  998. off_nl = 8; /* 802.2+SNAP */
  999. off_nl_nosnap = 3; /* 802.2 */
  1000. return;
  1001. case DLT_PPI:
  1002. /*
  1003. * At the moment we treat PPI the same way that we treat
  1004. * normal Radiotap encoded packets. The difference is in
  1005. * the function that generates the code at the beginning
  1006. * to compute the header length. Since this code generator
  1007. * of PPI supports bare 802.11 encapsulation only (i.e.
  1008. * the encapsulated DLT should be DLT_IEEE802_11) we
  1009. * generate code to check for this too.
  1010. */
  1011. off_linktype = 24;
  1012. off_macpl = 0; /* link-layer header is variable-length */
  1013. off_macpl_is_variable = 1;
  1014. off_nl = 8; /* 802.2+SNAP */
  1015. off_nl_nosnap = 3; /* 802.2 */
  1016. return;
  1017. case DLT_ATM_RFC1483:
  1018. case DLT_ATM_CLIP: /* Linux ATM defines this */
  1019. /*
  1020. * assume routed, non-ISO PDUs
  1021. * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
  1022. *
  1023. * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
  1024. * or PPP with the PPP NLPID (e.g., PPPoA)? The
  1025. * latter would presumably be treated the way PPPoE
  1026. * should be, so you can do "pppoe and udp port 2049"
  1027. * or "pppoa and tcp port 80" and have it check for
  1028. * PPPo{A,E} and a PPP protocol of IP and....
  1029. */
  1030. off_linktype = 0;
  1031. off_macpl = 0; /* packet begins with LLC header */
  1032. off_nl = 8; /* 802.2+SNAP */
  1033. off_nl_nosnap = 3; /* 802.2 */
  1034. return;
  1035. case DLT_SUNATM:
  1036. /*
  1037. * Full Frontal ATM; you get AALn PDUs with an ATM
  1038. * pseudo-header.
  1039. */
  1040. is_atm = 1;
  1041. off_vpi = SUNATM_VPI_POS;
  1042. off_vci = SUNATM_VCI_POS;
  1043. off_proto = PROTO_POS;
  1044. off_mac = -1; /* assume LLC-encapsulated, so no MAC-layer header */
  1045. off_payload = SUNATM_PKT_BEGIN_POS;
  1046. off_linktype = off_payload;
  1047. off_macpl = off_payload; /* if LLC-encapsulated */
  1048. off_nl = 8; /* 802.2+SNAP */
  1049. off_nl_nosnap = 3; /* 802.2 */
  1050. return;
  1051. case DLT_RAW:
  1052. case DLT_IPV4:
  1053. case DLT_IPV6:
  1054. off_linktype = -1;
  1055. off_macpl = 0;
  1056. off_nl = 0;
  1057. off_nl_nosnap = 0; /* no 802.2 LLC */
  1058. return;
  1059. case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
  1060. off_linktype = 14;
  1061. off_macpl = 16;
  1062. off_nl = 0;
  1063. off_nl_nosnap = 0; /* no 802.2 LLC */
  1064. return;
  1065. case DLT_LTALK:
  1066. /*
  1067. * LocalTalk does have a 1-byte type field in the LLAP header,
  1068. * but really it just indicates whether there is a "short" or
  1069. * "long" DDP packet following.
  1070. */
  1071. off_linktype = -1;
  1072. off_macpl = 0;
  1073. off_nl = 0;
  1074. off_nl_nosnap = 0; /* no 802.2 LLC */
  1075. return;
  1076. case DLT_IP_OVER_FC:
  1077. /*
  1078. * RFC 2625 IP-over-Fibre-Channel doesn't really have a
  1079. * link-level type field. We set "off_linktype" to the
  1080. * offset of the LLC header.
  1081. *
  1082. * To check for Ethernet types, we assume that SSAP = SNAP
  1083. * is being used and pick out the encapsulated Ethernet type.
  1084. * XXX - should we generate code to check for SNAP? RFC
  1085. * 2625 says SNAP should be used.
  1086. */
  1087. off_linktype = 16;
  1088. off_macpl = 16;
  1089. off_nl = 8; /* 802.2+SNAP */
  1090. off_nl_nosnap = 3; /* 802.2 */
  1091. return;
  1092. case DLT_FRELAY:
  1093. /*
  1094. * XXX - we should set this to handle SNAP-encapsulated
  1095. * frames (NLPID of 0x80).
  1096. */
  1097. off_linktype = -1;
  1098. off_macpl = 0;
  1099. off_nl = 0;
  1100. off_nl_nosnap = 0; /* no 802.2 LLC */
  1101. return;
  1102. /*
  1103. * the only BPF-interesting FRF.16 frames are non-control frames;
  1104. * Frame Relay has a variable length link-layer
  1105. * so lets start with offset 4 for now and increments later on (FIXME);
  1106. */
  1107. case DLT_MFR:
  1108. off_linktype = -1;
  1109. off_macpl = 0;
  1110. off_nl = 4;
  1111. off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
  1112. return;
  1113. case DLT_APPLE_IP_OVER_IEEE1394:
  1114. off_linktype = 16;
  1115. off_macpl = 18;
  1116. off_nl = 0;
  1117. off_nl_nosnap = 0; /* no 802.2 LLC */
  1118. return;
  1119. case DLT_SYMANTEC_FIREWALL:
  1120. off_linktype = 6;
  1121. off_macpl = 44;
  1122. off_nl = 0; /* Ethernet II */
  1123. off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
  1124. return;
  1125. #ifdef HAVE_NET_PFVAR_H
  1126. case DLT_PFLOG:
  1127. off_linktype = 0;
  1128. off_macpl = PFLOG_HDRLEN;
  1129. off_nl = 0;
  1130. off_nl_nosnap = 0; /* no 802.2 LLC */
  1131. return;
  1132. #endif
  1133. case DLT_JUNIPER_MFR:
  1134. case DLT_JUNIPER_MLFR:
  1135. case DLT_JUNIPER_MLPPP:
  1136. case DLT_JUNIPER_PPP:
  1137. case DLT_JUNIPER_CHDLC:
  1138. case DLT_JUNIPER_FRELAY:
  1139. off_linktype = 4;
  1140. off_macpl = 4;
  1141. off_nl = 0;
  1142. off_nl_nosnap = -1; /* no 802.2 LLC */
  1143. return;
  1144. case DLT_JUNIPER_ATM1:
  1145. off_linktype = 4; /* in reality variable between 4-8 */
  1146. off_macpl = 4; /* in reality variable between 4-8 */
  1147. off_nl = 0;
  1148. off_nl_nosnap = 10;
  1149. return;
  1150. case DLT_JUNIPER_ATM2:
  1151. off_linktype = 8; /* in reality variable between 8-12 */
  1152. off_macpl = 8; /* in reality variable between 8-12 */
  1153. off_nl = 0;
  1154. off_nl_nosnap = 10;
  1155. return;
  1156. /* frames captured on a Juniper PPPoE service PIC
  1157. * contain raw ethernet frames */
  1158. case DLT_JUNIPER_PPPOE:
  1159. case DLT_JUNIPER_ETHER:
  1160. off_macpl = 14;
  1161. off_linktype = 16;
  1162. off_nl = 18; /* Ethernet II */
  1163. off_nl_nosnap = 21; /* 802.3+802.2 */
  1164. return;
  1165. case DLT_JUNIPER_PPPOE_ATM:
  1166. off_linktype = 4;
  1167. off_macpl = 6;
  1168. off_nl = 0;
  1169. off_nl_nosnap = -1; /* no 802.2 LLC */
  1170. return;
  1171. case DLT_JUNIPER_GGSN:
  1172. off_linktype = 6;
  1173. off_macpl = 12;
  1174. off_nl = 0;
  1175. off_nl_nosnap = -1; /* no 802.2 LLC */
  1176. return;
  1177. case DLT_JUNIPER_ES:
  1178. off_linktype = 6;
  1179. off_macpl = -1; /* not really a network layer but raw IP addresses */
  1180. off_nl = -1; /* not really a network layer but raw IP addresses */
  1181. off_nl_nosnap = -1; /* no 802.2 LLC */
  1182. return;
  1183. case DLT_JUNIPER_MONITOR:
  1184. off_linktype = 12;
  1185. off_macpl = 12;
  1186. off_nl = 0; /* raw IP/IP6 header */
  1187. off_nl_nosnap = -1; /* no 802.2 LLC */
  1188. return;
  1189. case DLT_BACNET_MS_TP:
  1190. off_linktype = -1;
  1191. off_macpl = -1;
  1192. off_nl = -1;
  1193. off_nl_nosnap = -1;
  1194. return;
  1195. case DLT_JUNIPER_SERVICES:
  1196. off_linktype = 12;
  1197. off_macpl = -1; /* L3 proto location dep. on cookie type */
  1198. off_nl = -1; /* L3 proto location dep. on cookie type */
  1199. off_nl_nosnap = -1; /* no 802.2 LLC */
  1200. return;
  1201. case DLT_JUNIPER_VP:
  1202. off_linktype = 18;
  1203. off_macpl = -1;
  1204. off_nl = -1;
  1205. off_nl_nosnap = -1;
  1206. return;
  1207. case DLT_JUNIPER_ST:
  1208. off_linktype = 18;
  1209. off_macpl = -1;
  1210. off_nl = -1;
  1211. off_nl_nosnap = -1;
  1212. return;
  1213. case DLT_JUNIPER_ISM:
  1214. off_linktype = 8;
  1215. off_macpl = -1;
  1216. off_nl = -1;
  1217. off_nl_nosnap = -1;
  1218. return;
  1219. case DLT_JUNIPER_VS:
  1220. case DLT_JUNIPER_SRX_E2E:
  1221. case DLT_JUNIPER_FIBRECHANNEL:
  1222. case DLT_JUNIPER_ATM_CEMIC:
  1223. off_linktype = 8;
  1224. off_macpl = -1;
  1225. off_nl = -1;
  1226. off_nl_nosnap = -1;
  1227. return;
  1228. case DLT_MTP2:
  1229. off_li = 2;
  1230. off_li_hsl = 4;
  1231. off_sio = 3;
  1232. off_opc = 4;
  1233. off_dpc = 4;
  1234. off_sls = 7;
  1235. off_linktype = -1;
  1236. off_macpl = -1;
  1237. off_nl = -1;
  1238. off_nl_nosnap = -1;
  1239. return;
  1240. case DLT_MTP2_WITH_PHDR:
  1241. off_li = 6;
  1242. off_li_hsl = 8;
  1243. off_sio = 7;
  1244. off_opc = 8;
  1245. off_dpc = 8;
  1246. off_sls = 11;
  1247. off_linktype = -1;
  1248. off_macpl = -1;
  1249. off_nl = -1;
  1250. off_nl_nosnap = -1;
  1251. return;
  1252. case DLT_ERF:
  1253. off_li = 22;
  1254. off_li_hsl = 24;
  1255. off_sio = 23;
  1256. off_opc = 24;
  1257. off_dpc = 24;
  1258. off_sls = 27;
  1259. off_linktype = -1;
  1260. off_macpl = -1;
  1261. off_nl = -1;
  1262. off_nl_nosnap = -1;
  1263. return;
  1264. case DLT_PFSYNC:
  1265. off_linktype = -1;
  1266. off_macpl = 4;
  1267. off_nl = 0;
  1268. off_nl_nosnap = 0;
  1269. return;
  1270. case DLT_AX25_KISS:
  1271. /*
  1272. * Currently, only raw "link[N:M]" filtering is supported.
  1273. */
  1274. off_linktype = -1; /* variable, min 15, max 71 steps of 7 */
  1275. off_macpl = -1;
  1276. off_nl = -1; /* variable, min 16, max 71 steps of 7 */
  1277. off_nl_nosnap = -1; /* no 802.2 LLC */
  1278. off_mac = 1; /* step over the kiss length byte */
  1279. return;
  1280. case DLT_IPNET:
  1281. off_linktype = 1;
  1282. off_macpl = 24; /* ipnet header length */
  1283. off_nl = 0;
  1284. off_nl_nosnap = -1;
  1285. return;
  1286. case DLT_NETANALYZER:
  1287. off_mac = 4; /* MAC header is past 4-byte pseudo-header */
  1288. off_linktype = 16; /* includes 4-byte pseudo-header */
  1289. off_macpl = 18; /* pseudo-header+Ethernet header length */
  1290. off_nl = 0; /* Ethernet II */
  1291. off_nl_nosnap = 3; /* 802.3+802.2 */
  1292. return;
  1293. case DLT_NETANALYZER_TRANSPARENT:
  1294. off_mac = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
  1295. off_linktype = 24; /* includes 4-byte pseudo-header+preamble+SFD */
  1296. off_macpl = 26; /* pseudo-header+preamble+SFD+Ethernet header length */
  1297. off_nl = 0; /* Ethernet II */
  1298. off_nl_nosnap = 3; /* 802.3+802.2 */
  1299. return;
  1300. default:
  1301. /*
  1302. * For values in the range in which we've assigned new
  1303. * DLT_ values, only raw "link[N:M]" filtering is supported.
  1304. */
  1305. if (linktype >= DLT_MATCHING_MIN &&
  1306. linktype <= DLT_MATCHING_MAX) {
  1307. off_linktype = -1;
  1308. off_macpl = -1;
  1309. off_nl = -1;
  1310. off_nl_nosnap = -1;
  1311. return;
  1312. }
  1313. }
  1314. bpf_error("unknown data link type %d", linktype);
  1315. /* NOTREACHED */
  1316. }
  1317. /*
  1318. * Load a value relative to the beginning of the link-layer header.
  1319. * The link-layer header doesn't necessarily begin at the beginning
  1320. * of the packet data; there might be a variable-length prefix containing
  1321. * radio information.
  1322. */
  1323. static struct slist *
  1324. gen_load_llrel(offset, size)
  1325. u_int offset, size;
  1326. {
  1327. struct slist *s, *s2;
  1328. s = gen_llprefixlen();
  1329. /*
  1330. * If "s" is non-null, it has code to arrange that the X register
  1331. * contains the length of the prefix preceding the link-layer
  1332. * header.
  1333. *
  1334. * Otherwise, the length of the prefix preceding the link-layer
  1335. * header is "off_ll".
  1336. */
  1337. if (s != NULL) {
  1338. /*
  1339. * There's a variable-length prefix preceding the
  1340. * link-layer header. "s" points to a list of statements
  1341. * that put the length of that prefix into the X register.
  1342. * do an indirect load, to use the X register as an offset.
  1343. */
  1344. s2 = new_stmt(BPF_LD|BPF_IND|size);
  1345. s2->s.k = offset;
  1346. sappend(s, s2);
  1347. } else {
  1348. /*
  1349. * There is no variable-length header preceding the
  1350. * link-layer header; add in off_ll, which, if there's
  1351. * a fixed-length header preceding the link-layer header,
  1352. * is the length of that header.
  1353. */
  1354. s = new_stmt(BPF_LD|BPF_ABS|size);
  1355. s->s.k = offset + off_ll;
  1356. }
  1357. return s;
  1358. }
  1359. /*
  1360. * Load a value relative to the beginning of the MAC-layer payload.
  1361. */
  1362. static struct slist *
  1363. gen_load_macplrel(offset, size)
  1364. u_int offset, size;
  1365. {
  1366. struct slist *s, *s2;
  1367. s = gen_off_macpl();
  1368. /*
  1369. * If s is non-null, the offset of the MAC-layer payload is
  1370. * variable, and s points to a list of instructions that
  1371. * arrange that the X register contains that offset.
  1372. *
  1373. * Otherwise, the offset of the MAC-layer payload is constant,
  1374. * and is in off_macpl.
  1375. */
  1376. if (s != NULL) {
  1377. /*
  1378. * The offset of the MAC-layer payload is in the X
  1379. * register. Do an indirect load, to use the X register
  1380. * as an offset.
  1381. */
  1382. s2 = new_stmt(BPF_LD|BPF_IND|size);
  1383. s2->s.k = offset;
  1384. sappend(s, s2);
  1385. } else {
  1386. /*
  1387. * The offset of the MAC-layer payload is constant,
  1388. * and is in off_macpl; load the value at that offset
  1389. * plus the specified offset.
  1390. */
  1391. s = new_stmt(BPF_LD|BPF_ABS|size);
  1392. s->s.k = off_macpl + offset;
  1393. }
  1394. return s;
  1395. }
  1396. /*
  1397. * Load a value relative to the beginning of the specified header.
  1398. */
  1399. static struct slist *
  1400. gen_load_a(offrel, offset, size)
  1401. enum e_offrel offrel;
  1402. u_int offset, size;
  1403. {
  1404. struct slist *s, *s2;
  1405. switch (offrel) {
  1406. case OR_PACKET:
  1407. s = new_stmt(BPF_LD|BPF_ABS|size);
  1408. s->s.k = offset;
  1409. break;
  1410. case OR_LINK:
  1411. s = gen_load_llrel(offset, size);
  1412. break;
  1413. case OR_MACPL:
  1414. s = gen_load_macplrel(offset, size);
  1415. break;
  1416. case OR_NET:
  1417. s = gen_load_macplrel(off_nl + offset, size);
  1418. break;
  1419. case OR_NET_NOSNAP:
  1420. s = gen_load_macplrel(off_nl_nosnap + offset, size);
  1421. break;
  1422. case OR_TRAN_IPV4:
  1423. /*
  1424. * Load the X register with the length of the IPv4 header
  1425. * (plus the offset of the link-layer header, if it's
  1426. * preceded by a variable-length header such as a radio
  1427. * header), in bytes.
  1428. */
  1429. s = gen_loadx_iphdrlen();
  1430. /*
  1431. * Load the item at {offset of the MAC-layer payload} +
  1432. * {offset, relative to the start of the MAC-layer
  1433. * paylod, of the IPv4 header} + {length of the IPv4 header} +
  1434. * {specified offset}.
  1435. *
  1436. * (If the offset of the MAC-layer payload is variable,
  1437. * it's included in the value in the X register, and
  1438. * off_macpl is 0.)
  1439. */
  1440. s2 = new_stmt(BPF_LD|BPF_IND|size);
  1441. s2->s.k = off_macpl + off_nl + offset;
  1442. sappend(s, s2);
  1443. break;
  1444. case OR_TRAN_IPV6:
  1445. s = gen_load_macplrel(off_nl + 40 + offset, size);
  1446. break;
  1447. default:
  1448. abort();
  1449. return NULL;
  1450. }
  1451. return s;
  1452. }
  1453. /*
  1454. * Generate code to load into the X register the sum of the length of
  1455. * the IPv4 header and any variable-length header preceding the link-layer
  1456. * header.
  1457. */
  1458. static struct slist *
  1459. gen_loadx_iphdrlen()
  1460. {
  1461. struct slist *s, *s2;
  1462. s = gen_off_macpl();
  1463. if (s != NULL) {
  1464. /*
  1465. * There's a variable-length prefix preceding the
  1466. * link-layer header, or the link-layer header is itself
  1467. * variable-length. "s" points to a list of statements
  1468. * that put the offset of the MAC-layer payload into
  1469. * the X register.
  1470. *
  1471. * The 4*([k]&0xf) addressing mode can't be used, as we
  1472. * don't have a constant offset, so we have to load the
  1473. * value in question into the A register and add to it
  1474. * the value from the X register.
  1475. */
  1476. s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
  1477. s2->s.k = off_nl;
  1478. sappend(s, s2);
  1479. s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
  1480. s2->s.k = 0xf;
  1481. sappend(s, s2);
  1482. s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
  1483. s2->s.k = 2;
  1484. sappend(s, s2);
  1485. /*
  1486. * The A register now contains the length of the
  1487. * IP header. We need to add to it the offset of
  1488. * the MAC-layer payload, which is still in the X
  1489. * register, and move the result into the X register.
  1490. */
  1491. sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
  1492. sappend(s, new_stmt(BPF_MISC|BPF_TAX));
  1493. } else {
  1494. /*
  1495. * There is no variable-length header preceding the
  1496. * link-layer header, and the link-layer header is
  1497. * fixed-length; load the length of the IPv4 header,
  1498. * which is at an offset of off_nl from the beginning
  1499. * of the MAC-layer payload, and thus at an offset
  1500. * of off_mac_pl + off_nl from the beginning of the
  1501. * raw packet data.
  1502. */
  1503. s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
  1504. s->s.k = off_macpl + off_nl;
  1505. }
  1506. return s;
  1507. }
  1508. static struct block *
  1509. gen_uncond(rsense)
  1510. int rsense;
  1511. {
  1512. struct block *b;
  1513. struct slist *s;
  1514. s = new_stmt(BPF_LD|BPF_IMM);
  1515. s->s.k = !rsense;
  1516. b = new_block(JMP(BPF_JEQ));
  1517. b->stmts = s;
  1518. return b;
  1519. }
  1520. static inline struct block *
  1521. gen_true()
  1522. {
  1523. return gen_uncond(1);
  1524. }
  1525. static inline struct block *
  1526. gen_false()
  1527. {
  1528. return gen_uncond(0);
  1529. }
  1530. /*
  1531. * Byte-swap a 32-bit number.
  1532. * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
  1533. * big-endian platforms.)
  1534. */
  1535. #define SWAPLONG(y) \
  1536. ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
  1537. /*
  1538. * Generate code to match a particular packet type.
  1539. *
  1540. * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
  1541. * value, if <= ETHERMTU. We use that to determine whether to
  1542. * match the type/length field or to check the type/length field for
  1543. * a value <= ETHERMTU to see whether it's a type field and then do
  1544. * the appropriate test.
  1545. */
  1546. static struct block *
  1547. gen_ether_linktype(proto)
  1548. register int proto;
  1549. {
  1550. struct block *b0, *b1;
  1551. switch (proto) {
  1552. case LLCSAP_ISONS:
  1553. case LLCSAP_IP:
  1554. case LLCSAP_NETBEUI:
  1555. /*
  1556. * OSI protocols and NetBEUI always use 802.2 encapsulation,
  1557. * so we check the DSAP and SSAP.
  1558. *
  1559. * LLCSAP_IP checks for IP-over-802.2, rather
  1560. * than IP-over-Ethernet or IP-over-SNAP.
  1561. *
  1562. * XXX - should we check both the DSAP and the
  1563. * SSAP, like this, or should we check just the
  1564. * DSAP, as we do for other types <= ETHERMTU
  1565. * (i.e., other SAP values)?
  1566. */
  1567. b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
  1568. gen_not(b0);
  1569. b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
  1570. ((proto << 8) | proto));
  1571. gen_and(b0, b1);
  1572. return b1;
  1573. case LLCSAP_IPX:
  1574. /*
  1575. * Check for;
  1576. *
  1577. * Ethernet_II frames, which are Ethernet
  1578. * frames with a frame type of ETHERTYPE_IPX;
  1579. *
  1580. * Ethernet_802.3 frames, which are 802.3
  1581. * frames (i.e., the type/length field is
  1582. * a length field, <= ETHERMTU, rather than
  1583. * a type field) with the first two bytes
  1584. * after the Ethernet/802.3 header being
  1585. * 0xFFFF;
  1586. *
  1587. * Ethernet_802.2 frames, which are 802.3
  1588. * frames with an 802.2 LLC header and
  1589. * with the IPX LSAP as the DSAP in the LLC
  1590. * header;
  1591. *
  1592. * Ethernet_SNAP frames, which are 802.3
  1593. * frames with an LLC header and a SNAP
  1594. * header and with an OUI of 0x000000
  1595. * (encapsulated Ethernet) and a protocol
  1596. * ID of ETHERTYPE_IPX in the SNAP header.
  1597. *
  1598. * XXX - should we generate the same code both
  1599. * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
  1600. */
  1601. /*
  1602. * This generates code to check both for the
  1603. * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
  1604. */
  1605. b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
  1606. b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)0xFFFF);
  1607. gen_or(b0, b1);
  1608. /*
  1609. * Now we add code to check for SNAP frames with
  1610. * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
  1611. */
  1612. b0 = gen_snap(0x000000, ETHERTYPE_IPX);
  1613. gen_or(b0, b1);
  1614. /*
  1615. * Now we generate code to check for 802.3
  1616. * frames in general.
  1617. */
  1618. b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
  1619. gen_not(b0);
  1620. /*
  1621. * Now add the check for 802.3 frames before the
  1622. * check for Ethernet_802.2 and Ethernet_802.3,
  1623. * as those checks should only be done on 802.3
  1624. * frames, not on Ethernet frames.
  1625. */
  1626. gen_and(b0, b1);
  1627. /*
  1628. * Now add the check for Ethernet_II frames, and
  1629. * do that before checking for the other frame
  1630. * types.
  1631. */
  1632. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
  1633. (bpf_int32)ETHERTYPE_IPX);
  1634. gen_or(b0, b1);
  1635. return b1;
  1636. case ETHERTYPE_ATALK:
  1637. case ETHERTYPE_AARP:
  1638. /*
  1639. * EtherTalk (AppleTalk protocols on Ethernet link
  1640. * layer) may use 802.2 encapsulation.
  1641. */
  1642. /*
  1643. * Check for 802.2 encapsulation (EtherTalk phase 2?);
  1644. * we check for an Ethernet type field less than
  1645. * 1500, which means it's an 802.3 length field.
  1646. */
  1647. b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
  1648. gen_not(b0);
  1649. /*
  1650. * 802.2-encapsulated ETHERTYPE_ATALK packets are
  1651. * SNAP packets with an organization code of
  1652. * 0x080007 (Apple, for Appletalk) and a protocol
  1653. * type of ETHERTYPE_ATALK (Appletalk).
  1654. *
  1655. * 802.2-encapsulated ETHERTYPE_AARP packets are
  1656. * SNAP packets with an organization code of
  1657. * 0x000000 (encapsulated Ethernet) and a protocol
  1658. * type of ETHERTYPE_AARP (Appletalk ARP).
  1659. */
  1660. if (proto == ETHERTYPE_ATALK)
  1661. b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
  1662. else /* proto == ETHERTYPE_AARP */
  1663. b1 = gen_snap(0x000000, ETHERTYPE_AARP);
  1664. gen_and(b0, b1);
  1665. /*
  1666. * Check for Ethernet encapsulation (Ethertalk
  1667. * phase 1?); we just check for the Ethernet
  1668. * protocol type.
  1669. */
  1670. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
  1671. gen_or(b0, b1);
  1672. return b1;
  1673. default:
  1674. if (proto <= ETHERMTU) {
  1675. /*
  1676. * This is an LLC SAP value, so the frames
  1677. * that match would be 802.2 frames.
  1678. * Check that the frame is an 802.2 frame
  1679. * (i.e., that the length/type field is
  1680. * a length field, <= ETHERMTU) and
  1681. * then check the DSAP.
  1682. */
  1683. b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
  1684. gen_not(b0);
  1685. b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
  1686. (bpf_int32)proto);
  1687. gen_and(b0, b1);
  1688. return b1;
  1689. } else {
  1690. /*
  1691. * This is an Ethernet type, so compare
  1692. * the length/type field with it (if
  1693. * the frame is an 802.2 frame, the length
  1694. * field will be <= ETHERMTU, and, as
  1695. * "proto" is > ETHERMTU, this test
  1696. * will fail and the frame won't match,
  1697. * which is what we want).
  1698. */
  1699. return gen_cmp(OR_LINK, off_linktype, BPF_H,
  1700. (bpf_int32)proto);
  1701. }
  1702. }
  1703. }
  1704. /*
  1705. * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
  1706. * or IPv6 then we have an error.
  1707. */
  1708. static struct block *
  1709. gen_ipnet_linktype(proto)
  1710. register int proto;
  1711. {
  1712. switch (proto) {
  1713. case ETHERTYPE_IP:
  1714. return gen_cmp(OR_LINK, off_linktype, BPF_B,
  1715. (bpf_int32)IPH_AF_INET);
  1716. /* NOTREACHED */
  1717. case ETHERTYPE_IPV6:
  1718. return gen_cmp(OR_LINK, off_linktype, BPF_B,
  1719. (bpf_int32)IPH_AF_INET6);
  1720. /* NOTREACHED */
  1721. default:
  1722. break;
  1723. }
  1724. return gen_false();
  1725. }
  1726. /*
  1727. * Generate code to match a particular packet type.
  1728. *
  1729. * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
  1730. * value, if <= ETHERMTU. We use that to determine whether to
  1731. * match the type field or to check the type field for the special
  1732. * LINUX_SLL_P_802_2 value and then do the appropriate test.
  1733. */
  1734. static struct block *
  1735. gen_linux_sll_linktype(proto)
  1736. register int proto;
  1737. {
  1738. struct block *b0, *b1;
  1739. switch (proto) {
  1740. case LLCSAP_ISONS:
  1741. case LLCSAP_IP:
  1742. case LLCSAP_NETBEUI:
  1743. /*
  1744. * OSI protocols and NetBEUI always use 802.2 encapsulation,
  1745. * so we check the DSAP and SSAP.
  1746. *
  1747. * LLCSAP_IP checks for IP-over-802.2, rather
  1748. * than IP-over-Ethernet or IP-over-SNAP.
  1749. *
  1750. * XXX - should we check both the DSAP and the
  1751. * SSAP, like this, or should we check just the
  1752. * DSAP, as we do for other types <= ETHERMTU
  1753. * (i.e., other SAP values)?
  1754. */
  1755. b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
  1756. b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
  1757. ((proto << 8) | proto));
  1758. gen_and(b0, b1);
  1759. return b1;
  1760. case LLCSAP_IPX:
  1761. /*
  1762. * Ethernet_II frames, which are Ethernet
  1763. * frames with a frame type of ETHERTYPE_IPX;
  1764. *
  1765. *…

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