PageRenderTime 71ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 1ms

/gencode.c

https://github.com/android/platform_external_libpcap
C | 10058 lines | 5855 code | 1328 blank | 2875 comment | 687 complexity | 86d344dd036209217b4f3c68201b4d17 MD5 | raw file

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

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

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