PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 1ms

/external/bsd/libpcap/dist/gencode.c

https://github.com/rumpkernel/netbsd-userspace-src
C | 8542 lines | 5204 code | 1076 blank | 2262 comment | 551 complexity | a9abca5d0b506d3e7ee682545d00488d MD5 | raw file

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

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

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