PageRenderTime 60ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 1ms

/contrib/libpcap/gencode.c

https://bitbucket.org/freebsd/freebsd-head/
C | 8537 lines | 5220 code | 1062 blank | 2255 comment | 533 complexity | 0dcead027f87e999a9e9c31bcd598b90 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0

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

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