PageRenderTime 69ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 1ms

/libpcap/gencode.c

https://gitlab.com/g10h4ck/nmap-gsoc2015
C | 8904 lines | 5428 code | 1157 blank | 2319 comment | 563 complexity | 6839f0c4377588f19354fc3bdbd986af MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, LGPL-2.0, LGPL-2.1, MIT

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

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