PageRenderTime 102ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/gencode.c

https://github.com/mcr/libpcap
C | 9993 lines | 5829 code | 1325 blank | 2839 comment | 683 complexity | c2a712309eb78467a324c6dad6d65f1d MD5 | raw 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. #include "pcap-int.h"
  45. #include "extract.h"
  46. #include "ethertype.h"
  47. #include "nlpid.h"
  48. #include "llc.h"
  49. #include "gencode.h"
  50. #include "ieee80211.h"
  51. #include "atmuni31.h"
  52. #include "sunatmpos.h"
  53. #include "ppp.h"
  54. #include "pcap/sll.h"
  55. #include "pcap/ipnet.h"
  56. #include "arcnet.h"
  57. #include "grammar.h"
  58. #include "scanner.h"
  59. #if defined(linux)
  60. #include <linux/types.h>
  61. #include <linux/if_packet.h>
  62. #include <linux/filter.h>
  63. #endif
  64. #ifdef HAVE_NET_PFVAR_H
  65. #include <sys/socket.h>
  66. #include <net/if.h>
  67. #include <net/pfvar.h>
  68. #include <net/if_pflog.h>
  69. #endif
  70. #ifndef offsetof
  71. #define offsetof(s, e) ((size_t)&((s *)0)->e)
  72. #endif
  73. #ifdef _WIN32
  74. #ifdef INET6
  75. #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
  76. /* IPv6 address */
  77. struct in6_addr
  78. {
  79. union
  80. {
  81. uint8_t u6_addr8[16];
  82. uint16_t u6_addr16[8];
  83. uint32_t u6_addr32[4];
  84. } in6_u;
  85. #define s6_addr in6_u.u6_addr8
  86. #define s6_addr16 in6_u.u6_addr16
  87. #define s6_addr32 in6_u.u6_addr32
  88. #define s6_addr64 in6_u.u6_addr64
  89. };
  90. typedef unsigned short sa_family_t;
  91. #define __SOCKADDR_COMMON(sa_prefix) \
  92. sa_family_t sa_prefix##family
  93. /* Ditto, for IPv6. */
  94. struct sockaddr_in6
  95. {
  96. __SOCKADDR_COMMON (sin6_);
  97. uint16_t sin6_port; /* Transport layer port # */
  98. uint32_t sin6_flowinfo; /* IPv6 flow information */
  99. struct in6_addr sin6_addr; /* IPv6 address */
  100. };
  101. #ifndef EAI_ADDRFAMILY
  102. struct addrinfo {
  103. int ai_flags; /* AI_PASSIVE, AI_CANONNAME */
  104. int ai_family; /* PF_xxx */
  105. int ai_socktype; /* SOCK_xxx */
  106. int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
  107. size_t ai_addrlen; /* length of ai_addr */
  108. char *ai_canonname; /* canonical name for hostname */
  109. struct sockaddr *ai_addr; /* binary address */
  110. struct addrinfo *ai_next; /* next structure in linked list */
  111. };
  112. #endif /* EAI_ADDRFAMILY */
  113. #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
  114. #endif /* INET6 */
  115. #else /* _WIN32 */
  116. #include <netdb.h> /* for "struct addrinfo" */
  117. #endif /* _WIN32 */
  118. #include <pcap/namedb.h>
  119. #include "nametoaddr.h"
  120. #define ETHERMTU 1500
  121. #ifndef IPPROTO_HOPOPTS
  122. #define IPPROTO_HOPOPTS 0
  123. #endif
  124. #ifndef IPPROTO_ROUTING
  125. #define IPPROTO_ROUTING 43
  126. #endif
  127. #ifndef IPPROTO_FRAGMENT
  128. #define IPPROTO_FRAGMENT 44
  129. #endif
  130. #ifndef IPPROTO_DSTOPTS
  131. #define IPPROTO_DSTOPTS 60
  132. #endif
  133. #ifndef IPPROTO_SCTP
  134. #define IPPROTO_SCTP 132
  135. #endif
  136. #define GENEVE_PORT 6081
  137. #ifdef HAVE_OS_PROTO_H
  138. #include "os-proto.h"
  139. #endif
  140. #define JMP(c) ((c)|BPF_JMP|BPF_K)
  141. /*
  142. * "Push" the current value of the link-layer header type and link-layer
  143. * header offset onto a "stack", and set a new value. (It's not a
  144. * full-blown stack; we keep only the top two items.)
  145. */
  146. #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
  147. { \
  148. (cs)->prevlinktype = (cs)->linktype; \
  149. (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
  150. (cs)->linktype = (new_linktype); \
  151. (cs)->off_linkhdr.is_variable = (new_is_variable); \
  152. (cs)->off_linkhdr.constant_part = (new_constant_part); \
  153. (cs)->off_linkhdr.reg = (new_reg); \
  154. (cs)->is_geneve = 0; \
  155. }
  156. /*
  157. * Offset "not set" value.
  158. */
  159. #define OFFSET_NOT_SET 0xffffffffU
  160. /*
  161. * Absolute offsets, which are offsets from the beginning of the raw
  162. * packet data, are, in the general case, the sum of a variable value
  163. * and a constant value; the variable value may be absent, in which
  164. * case the offset is only the constant value, and the constant value
  165. * may be zero, in which case the offset is only the variable value.
  166. *
  167. * bpf_abs_offset is a structure containing all that information:
  168. *
  169. * is_variable is 1 if there's a variable part.
  170. *
  171. * constant_part is the constant part of the value, possibly zero;
  172. *
  173. * if is_variable is 1, reg is the register number for a register
  174. * containing the variable value if the register has been assigned,
  175. * and -1 otherwise.
  176. */
  177. typedef struct {
  178. int is_variable;
  179. u_int constant_part;
  180. int reg;
  181. } bpf_abs_offset;
  182. /*
  183. * Value passed to gen_load_a() to indicate what the offset argument
  184. * is relative to the beginning of.
  185. */
  186. enum e_offrel {
  187. OR_PACKET, /* full packet data */
  188. OR_LINKHDR, /* link-layer header */
  189. OR_PREVLINKHDR, /* previous link-layer header */
  190. OR_LLC, /* 802.2 LLC header */
  191. OR_PREVMPLSHDR, /* previous MPLS header */
  192. OR_LINKTYPE, /* link-layer type */
  193. OR_LINKPL, /* link-layer payload */
  194. OR_LINKPL_NOSNAP, /* link-layer payload, with no SNAP header at the link layer */
  195. OR_TRAN_IPV4, /* transport-layer header, with IPv4 network layer */
  196. OR_TRAN_IPV6 /* transport-layer header, with IPv6 network layer */
  197. };
  198. /*
  199. * We divy out chunks of memory rather than call malloc each time so
  200. * we don't have to worry about leaking memory. It's probably
  201. * not a big deal if all this memory was wasted but if this ever
  202. * goes into a library that would probably not be a good idea.
  203. *
  204. * XXX - this *is* in a library....
  205. */
  206. #define NCHUNKS 16
  207. #define CHUNK0SIZE 1024
  208. struct chunk {
  209. size_t n_left;
  210. void *m;
  211. };
  212. /* Code generator state */
  213. struct _compiler_state {
  214. jmp_buf top_ctx;
  215. pcap_t *bpf_pcap;
  216. int error_set;
  217. struct icode ic;
  218. int snaplen;
  219. int linktype;
  220. int prevlinktype;
  221. int outermostlinktype;
  222. bpf_u_int32 netmask;
  223. int no_optimize;
  224. /* Hack for handling VLAN and MPLS stacks. */
  225. u_int label_stack_depth;
  226. u_int vlan_stack_depth;
  227. /* XXX */
  228. u_int pcap_fddipad;
  229. /*
  230. * As errors are handled by a longjmp, anything allocated must
  231. * be freed in the longjmp handler, so it must be reachable
  232. * from that handler.
  233. *
  234. * One thing that's allocated is the result of pcap_nametoaddrinfo();
  235. * it must be freed with freeaddrinfo(). This variable points to
  236. * any addrinfo structure that would need to be freed.
  237. */
  238. struct addrinfo *ai;
  239. /*
  240. * Another thing that's allocated is the result of pcap_ether_aton();
  241. * it must be freed with free(). This variable points to any
  242. * address that would need to be freed.
  243. */
  244. u_char *e;
  245. /*
  246. * Various code constructs need to know the layout of the packet.
  247. * These values give the necessary offsets from the beginning
  248. * of the packet data.
  249. */
  250. /*
  251. * Absolute offset of the beginning of the link-layer header.
  252. */
  253. bpf_abs_offset off_linkhdr;
  254. /*
  255. * If we're checking a link-layer header for a packet encapsulated
  256. * in another protocol layer, this is the equivalent information
  257. * for the previous layers' link-layer header from the beginning
  258. * of the raw packet data.
  259. */
  260. bpf_abs_offset off_prevlinkhdr;
  261. /*
  262. * This is the equivalent information for the outermost layers'
  263. * link-layer header.
  264. */
  265. bpf_abs_offset off_outermostlinkhdr;
  266. /*
  267. * Absolute offset of the beginning of the link-layer payload.
  268. */
  269. bpf_abs_offset off_linkpl;
  270. /*
  271. * "off_linktype" is the offset to information in the link-layer
  272. * header giving the packet type. This is an absolute offset
  273. * from the beginning of the packet.
  274. *
  275. * For Ethernet, it's the offset of the Ethernet type field; this
  276. * means that it must have a value that skips VLAN tags.
  277. *
  278. * For link-layer types that always use 802.2 headers, it's the
  279. * offset of the LLC header; this means that it must have a value
  280. * that skips VLAN tags.
  281. *
  282. * For PPP, it's the offset of the PPP type field.
  283. *
  284. * For Cisco HDLC, it's the offset of the CHDLC type field.
  285. *
  286. * For BSD loopback, it's the offset of the AF_ value.
  287. *
  288. * For Linux cooked sockets, it's the offset of the type field.
  289. *
  290. * off_linktype.constant_part is set to OFFSET_NOT_SET for no
  291. * encapsulation, in which case, IP is assumed.
  292. */
  293. bpf_abs_offset off_linktype;
  294. /*
  295. * TRUE if the link layer includes an ATM pseudo-header.
  296. */
  297. int is_atm;
  298. /*
  299. * TRUE if "geneve" appeared in the filter; it causes us to
  300. * generate code that checks for a Geneve header and assume
  301. * that later filters apply to the encapsulated payload.
  302. */
  303. int is_geneve;
  304. /*
  305. * TRUE if we need variable length part of VLAN offset
  306. */
  307. int is_vlan_vloffset;
  308. /*
  309. * These are offsets for the ATM pseudo-header.
  310. */
  311. u_int off_vpi;
  312. u_int off_vci;
  313. u_int off_proto;
  314. /*
  315. * These are offsets for the MTP2 fields.
  316. */
  317. u_int off_li;
  318. u_int off_li_hsl;
  319. /*
  320. * These are offsets for the MTP3 fields.
  321. */
  322. u_int off_sio;
  323. u_int off_opc;
  324. u_int off_dpc;
  325. u_int off_sls;
  326. /*
  327. * This is the offset of the first byte after the ATM pseudo_header,
  328. * or -1 if there is no ATM pseudo-header.
  329. */
  330. u_int off_payload;
  331. /*
  332. * These are offsets to the beginning of the network-layer header.
  333. * They are relative to the beginning of the link-layer payload
  334. * (i.e., they don't include off_linkhdr.constant_part or
  335. * off_linkpl.constant_part).
  336. *
  337. * If the link layer never uses 802.2 LLC:
  338. *
  339. * "off_nl" and "off_nl_nosnap" are the same.
  340. *
  341. * If the link layer always uses 802.2 LLC:
  342. *
  343. * "off_nl" is the offset if there's a SNAP header following
  344. * the 802.2 header;
  345. *
  346. * "off_nl_nosnap" is the offset if there's no SNAP header.
  347. *
  348. * If the link layer is Ethernet:
  349. *
  350. * "off_nl" is the offset if the packet is an Ethernet II packet
  351. * (we assume no 802.3+802.2+SNAP);
  352. *
  353. * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
  354. * with an 802.2 header following it.
  355. */
  356. u_int off_nl;
  357. u_int off_nl_nosnap;
  358. /*
  359. * Here we handle simple allocation of the scratch registers.
  360. * If too many registers are alloc'd, the allocator punts.
  361. */
  362. int regused[BPF_MEMWORDS];
  363. int curreg;
  364. /*
  365. * Memory chunks.
  366. */
  367. struct chunk chunks[NCHUNKS];
  368. int cur_chunk;
  369. };
  370. /*
  371. * For use by routines outside this file.
  372. */
  373. /* VARARGS */
  374. void
  375. bpf_set_error(compiler_state_t *cstate, const char *fmt, ...)
  376. {
  377. va_list ap;
  378. /*
  379. * If we've already set an error, don't override it.
  380. * The lexical analyzer reports some errors by setting
  381. * the error and then returning a LEX_ERROR token, which
  382. * is not recognized by any grammar rule, and thus forces
  383. * the parse to stop. We don't want the error reported
  384. * by the lexical analyzer to be overwritten by the syntax
  385. * error.
  386. */
  387. if (!cstate->error_set) {
  388. va_start(ap, fmt);
  389. (void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE,
  390. fmt, ap);
  391. va_end(ap);
  392. cstate->error_set = 1;
  393. }
  394. }
  395. /*
  396. * For use *ONLY* in routines in this file.
  397. */
  398. static void PCAP_NORETURN bpf_error(compiler_state_t *, const char *, ...)
  399. PCAP_PRINTFLIKE(2, 3);
  400. /* VARARGS */
  401. static void PCAP_NORETURN
  402. bpf_error(compiler_state_t *cstate, const char *fmt, ...)
  403. {
  404. va_list ap;
  405. va_start(ap, fmt);
  406. (void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE,
  407. fmt, ap);
  408. va_end(ap);
  409. longjmp(cstate->top_ctx, 1);
  410. /*NOTREACHED*/
  411. }
  412. static int init_linktype(compiler_state_t *, pcap_t *);
  413. static void init_regs(compiler_state_t *);
  414. static int alloc_reg(compiler_state_t *);
  415. static void free_reg(compiler_state_t *, int);
  416. static void initchunks(compiler_state_t *cstate);
  417. static void *newchunk_nolongjmp(compiler_state_t *cstate, size_t);
  418. static void *newchunk(compiler_state_t *cstate, size_t);
  419. static void freechunks(compiler_state_t *cstate);
  420. static inline struct block *new_block(compiler_state_t *cstate, int);
  421. static inline struct slist *new_stmt(compiler_state_t *cstate, int);
  422. static struct block *gen_retblk(compiler_state_t *cstate, int);
  423. static inline void syntax(compiler_state_t *cstate);
  424. static void backpatch(struct block *, struct block *);
  425. static void merge(struct block *, struct block *);
  426. static struct block *gen_cmp(compiler_state_t *, enum e_offrel, u_int,
  427. u_int, bpf_u_int32);
  428. static struct block *gen_cmp_gt(compiler_state_t *, enum e_offrel, u_int,
  429. u_int, bpf_u_int32);
  430. static struct block *gen_cmp_ge(compiler_state_t *, enum e_offrel, u_int,
  431. u_int, bpf_u_int32);
  432. static struct block *gen_cmp_lt(compiler_state_t *, enum e_offrel, u_int,
  433. u_int, bpf_u_int32);
  434. static struct block *gen_cmp_le(compiler_state_t *, enum e_offrel, u_int,
  435. u_int, bpf_u_int32);
  436. static struct block *gen_mcmp(compiler_state_t *, enum e_offrel, u_int,
  437. u_int, bpf_u_int32, bpf_u_int32);
  438. static struct block *gen_bcmp(compiler_state_t *, enum e_offrel, u_int,
  439. u_int, const u_char *);
  440. static struct block *gen_ncmp(compiler_state_t *, enum e_offrel, u_int,
  441. u_int, bpf_u_int32, int, int, bpf_u_int32);
  442. static struct slist *gen_load_absoffsetrel(compiler_state_t *, bpf_abs_offset *,
  443. u_int, u_int);
  444. static struct slist *gen_load_a(compiler_state_t *, enum e_offrel, u_int,
  445. u_int);
  446. static struct slist *gen_loadx_iphdrlen(compiler_state_t *);
  447. static struct block *gen_uncond(compiler_state_t *, int);
  448. static inline struct block *gen_true(compiler_state_t *);
  449. static inline struct block *gen_false(compiler_state_t *);
  450. static struct block *gen_ether_linktype(compiler_state_t *, bpf_u_int32);
  451. static struct block *gen_ipnet_linktype(compiler_state_t *, bpf_u_int32);
  452. static struct block *gen_linux_sll_linktype(compiler_state_t *, bpf_u_int32);
  453. static struct slist *gen_load_prism_llprefixlen(compiler_state_t *);
  454. static struct slist *gen_load_avs_llprefixlen(compiler_state_t *);
  455. static struct slist *gen_load_radiotap_llprefixlen(compiler_state_t *);
  456. static struct slist *gen_load_ppi_llprefixlen(compiler_state_t *);
  457. static void insert_compute_vloffsets(compiler_state_t *, struct block *);
  458. static struct slist *gen_abs_offset_varpart(compiler_state_t *,
  459. bpf_abs_offset *);
  460. static bpf_u_int32 ethertype_to_ppptype(bpf_u_int32);
  461. static struct block *gen_linktype(compiler_state_t *, bpf_u_int32);
  462. static struct block *gen_snap(compiler_state_t *, bpf_u_int32, bpf_u_int32);
  463. static struct block *gen_llc_linktype(compiler_state_t *, bpf_u_int32);
  464. static struct block *gen_hostop(compiler_state_t *, bpf_u_int32, bpf_u_int32,
  465. int, bpf_u_int32, u_int, u_int);
  466. #ifdef INET6
  467. static struct block *gen_hostop6(compiler_state_t *, struct in6_addr *,
  468. struct in6_addr *, int, bpf_u_int32, u_int, u_int);
  469. #endif
  470. static struct block *gen_ahostop(compiler_state_t *, const u_char *, int);
  471. static struct block *gen_ehostop(compiler_state_t *, const u_char *, int);
  472. static struct block *gen_fhostop(compiler_state_t *, const u_char *, int);
  473. static struct block *gen_thostop(compiler_state_t *, const u_char *, int);
  474. static struct block *gen_wlanhostop(compiler_state_t *, const u_char *, int);
  475. static struct block *gen_ipfchostop(compiler_state_t *, const u_char *, int);
  476. static struct block *gen_dnhostop(compiler_state_t *, bpf_u_int32, int);
  477. static struct block *gen_mpls_linktype(compiler_state_t *, bpf_u_int32);
  478. static struct block *gen_host(compiler_state_t *, bpf_u_int32, bpf_u_int32,
  479. int, int, int);
  480. #ifdef INET6
  481. static struct block *gen_host6(compiler_state_t *, struct in6_addr *,
  482. struct in6_addr *, int, int, int);
  483. #endif
  484. #ifndef INET6
  485. static struct block *gen_gateway(compiler_state_t *, const u_char *,
  486. struct addrinfo *, int, int);
  487. #endif
  488. static struct block *gen_ipfrag(compiler_state_t *);
  489. static struct block *gen_portatom(compiler_state_t *, int, bpf_u_int32);
  490. static struct block *gen_portrangeatom(compiler_state_t *, u_int, bpf_u_int32,
  491. bpf_u_int32);
  492. static struct block *gen_portatom6(compiler_state_t *, int, bpf_u_int32);
  493. static struct block *gen_portrangeatom6(compiler_state_t *, u_int, bpf_u_int32,
  494. bpf_u_int32);
  495. static struct block *gen_portop(compiler_state_t *, u_int, u_int, int);
  496. static struct block *gen_port(compiler_state_t *, u_int, int, int);
  497. static struct block *gen_portrangeop(compiler_state_t *, u_int, u_int,
  498. bpf_u_int32, int);
  499. static struct block *gen_portrange(compiler_state_t *, u_int, u_int, int, int);
  500. struct block *gen_portop6(compiler_state_t *, u_int, u_int, int);
  501. static struct block *gen_port6(compiler_state_t *, u_int, int, int);
  502. static struct block *gen_portrangeop6(compiler_state_t *, u_int, u_int,
  503. bpf_u_int32, int);
  504. static struct block *gen_portrange6(compiler_state_t *, u_int, u_int, int, int);
  505. static int lookup_proto(compiler_state_t *, const char *, int);
  506. static struct block *gen_protochain(compiler_state_t *, bpf_u_int32, int);
  507. static struct block *gen_proto(compiler_state_t *, bpf_u_int32, int, int);
  508. static struct slist *xfer_to_x(compiler_state_t *, struct arth *);
  509. static struct slist *xfer_to_a(compiler_state_t *, struct arth *);
  510. static struct block *gen_mac_multicast(compiler_state_t *, int);
  511. static struct block *gen_len(compiler_state_t *, int, int);
  512. static struct block *gen_check_802_11_data_frame(compiler_state_t *);
  513. static struct block *gen_geneve_ll_check(compiler_state_t *cstate);
  514. static struct block *gen_ppi_dlt_check(compiler_state_t *);
  515. static struct block *gen_atmfield_code_internal(compiler_state_t *, int,
  516. bpf_u_int32, int, int);
  517. static struct block *gen_atmtype_llc(compiler_state_t *);
  518. static struct block *gen_msg_abbrev(compiler_state_t *, int type);
  519. static void
  520. initchunks(compiler_state_t *cstate)
  521. {
  522. int i;
  523. for (i = 0; i < NCHUNKS; i++) {
  524. cstate->chunks[i].n_left = 0;
  525. cstate->chunks[i].m = NULL;
  526. }
  527. cstate->cur_chunk = 0;
  528. }
  529. static void *
  530. newchunk_nolongjmp(compiler_state_t *cstate, size_t n)
  531. {
  532. struct chunk *cp;
  533. int k;
  534. size_t size;
  535. #ifndef __NetBSD__
  536. /* XXX Round up to nearest long. */
  537. n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
  538. #else
  539. /* XXX Round up to structure boundary. */
  540. n = ALIGN(n);
  541. #endif
  542. cp = &cstate->chunks[cstate->cur_chunk];
  543. if (n > cp->n_left) {
  544. ++cp;
  545. k = ++cstate->cur_chunk;
  546. if (k >= NCHUNKS) {
  547. bpf_set_error(cstate, "out of memory");
  548. return (NULL);
  549. }
  550. size = CHUNK0SIZE << k;
  551. cp->m = (void *)malloc(size);
  552. if (cp->m == NULL) {
  553. bpf_set_error(cstate, "out of memory");
  554. return (NULL);
  555. }
  556. memset((char *)cp->m, 0, size);
  557. cp->n_left = size;
  558. if (n > size) {
  559. bpf_set_error(cstate, "out of memory");
  560. return (NULL);
  561. }
  562. }
  563. cp->n_left -= n;
  564. return (void *)((char *)cp->m + cp->n_left);
  565. }
  566. static void *
  567. newchunk(compiler_state_t *cstate, size_t n)
  568. {
  569. void *p;
  570. p = newchunk_nolongjmp(cstate, n);
  571. if (p == NULL) {
  572. longjmp(cstate->top_ctx, 1);
  573. /*NOTREACHED*/
  574. }
  575. return (p);
  576. }
  577. static void
  578. freechunks(compiler_state_t *cstate)
  579. {
  580. int i;
  581. for (i = 0; i < NCHUNKS; ++i)
  582. if (cstate->chunks[i].m != NULL)
  583. free(cstate->chunks[i].m);
  584. }
  585. /*
  586. * A strdup whose allocations are freed after code generation is over.
  587. * This is used by the lexical analyzer, so it can't longjmp; it just
  588. * returns NULL on an allocation error, and the callers must check
  589. * for it.
  590. */
  591. char *
  592. sdup(compiler_state_t *cstate, const char *s)
  593. {
  594. size_t n = strlen(s) + 1;
  595. char *cp = newchunk_nolongjmp(cstate, n);
  596. if (cp == NULL)
  597. return (NULL);
  598. pcap_strlcpy(cp, s, n);
  599. return (cp);
  600. }
  601. static inline struct block *
  602. new_block(compiler_state_t *cstate, int code)
  603. {
  604. struct block *p;
  605. p = (struct block *)newchunk(cstate, sizeof(*p));
  606. p->s.code = code;
  607. p->head = p;
  608. return p;
  609. }
  610. static inline struct slist *
  611. new_stmt(compiler_state_t *cstate, int code)
  612. {
  613. struct slist *p;
  614. p = (struct slist *)newchunk(cstate, sizeof(*p));
  615. p->s.code = code;
  616. return p;
  617. }
  618. static struct block *
  619. gen_retblk(compiler_state_t *cstate, int v)
  620. {
  621. struct block *b = new_block(cstate, BPF_RET|BPF_K);
  622. b->s.k = v;
  623. return b;
  624. }
  625. static inline PCAP_NORETURN_DEF void
  626. syntax(compiler_state_t *cstate)
  627. {
  628. bpf_error(cstate, "syntax error in filter expression");
  629. }
  630. int
  631. pcap_compile(pcap_t *p, struct bpf_program *program,
  632. const char *buf, int optimize, bpf_u_int32 mask)
  633. {
  634. #ifdef _WIN32
  635. static int done = 0;
  636. #endif
  637. compiler_state_t cstate;
  638. const char * volatile xbuf = buf;
  639. yyscan_t scanner = NULL;
  640. volatile YY_BUFFER_STATE in_buffer = NULL;
  641. u_int len;
  642. int rc;
  643. /*
  644. * If this pcap_t hasn't been activated, it doesn't have a
  645. * link-layer type, so we can't use it.
  646. */
  647. if (!p->activated) {
  648. snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  649. "not-yet-activated pcap_t passed to pcap_compile");
  650. return (-1);
  651. }
  652. #ifdef _WIN32
  653. if (!done)
  654. pcap_wsockinit();
  655. done = 1;
  656. #endif
  657. #ifdef ENABLE_REMOTE
  658. /*
  659. * If the device on which we're capturing need to be notified
  660. * that a new filter is being compiled, do so.
  661. *
  662. * This allows them to save a copy of it, in case, for example,
  663. * they're implementing a form of remote packet capture, and
  664. * want the remote machine to filter out the packets in which
  665. * it's sending the packets it's captured.
  666. *
  667. * XXX - the fact that we happen to be compiling a filter
  668. * doesn't necessarily mean we'll be installing it as the
  669. * filter for this pcap_t; we might be running it from userland
  670. * on captured packets to do packet classification. We really
  671. * need a better way of handling this, but this is all that
  672. * the WinPcap remote capture code did.
  673. */
  674. if (p->save_current_filter_op != NULL)
  675. (p->save_current_filter_op)(p, buf);
  676. #endif
  677. initchunks(&cstate);
  678. cstate.no_optimize = 0;
  679. #ifdef INET6
  680. cstate.ai = NULL;
  681. #endif
  682. cstate.e = NULL;
  683. cstate.ic.root = NULL;
  684. cstate.ic.cur_mark = 0;
  685. cstate.bpf_pcap = p;
  686. cstate.error_set = 0;
  687. init_regs(&cstate);
  688. cstate.netmask = mask;
  689. cstate.snaplen = pcap_snapshot(p);
  690. if (cstate.snaplen == 0) {
  691. snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  692. "snaplen of 0 rejects all packets");
  693. rc = -1;
  694. goto quit;
  695. }
  696. if (pcap_lex_init(&scanner) != 0)
  697. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  698. errno, "can't initialize scanner");
  699. in_buffer = pcap__scan_string(xbuf ? xbuf : "", scanner);
  700. /*
  701. * Associate the compiler state with the lexical analyzer
  702. * state.
  703. */
  704. pcap_set_extra(&cstate, scanner);
  705. if (init_linktype(&cstate, p) == -1) {
  706. rc = -1;
  707. goto quit;
  708. }
  709. if (pcap_parse(scanner, &cstate) != 0) {
  710. #ifdef INET6
  711. if (cstate.ai != NULL)
  712. freeaddrinfo(cstate.ai);
  713. #endif
  714. if (cstate.e != NULL)
  715. free(cstate.e);
  716. rc = -1;
  717. goto quit;
  718. }
  719. if (cstate.ic.root == NULL) {
  720. /*
  721. * Catch errors reported by gen_retblk().
  722. */
  723. if (setjmp(cstate.top_ctx)) {
  724. rc = -1;
  725. goto quit;
  726. }
  727. cstate.ic.root = gen_retblk(&cstate, cstate.snaplen);
  728. }
  729. if (optimize && !cstate.no_optimize) {
  730. if (bpf_optimize(&cstate.ic, p->errbuf) == -1) {
  731. /* Failure */
  732. rc = -1;
  733. goto quit;
  734. }
  735. if (cstate.ic.root == NULL ||
  736. (cstate.ic.root->s.code == (BPF_RET|BPF_K) && cstate.ic.root->s.k == 0)) {
  737. (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  738. "expression rejects all packets");
  739. rc = -1;
  740. goto quit;
  741. }
  742. }
  743. program->bf_insns = icode_to_fcode(&cstate.ic,
  744. cstate.ic.root, &len, p->errbuf);
  745. if (program->bf_insns == NULL) {
  746. /* Failure */
  747. rc = -1;
  748. goto quit;
  749. }
  750. program->bf_len = len;
  751. rc = 0; /* We're all okay */
  752. quit:
  753. /*
  754. * Clean up everything for the lexical analyzer.
  755. */
  756. if (in_buffer != NULL)
  757. pcap__delete_buffer(in_buffer, scanner);
  758. if (scanner != NULL)
  759. pcap_lex_destroy(scanner);
  760. /*
  761. * Clean up our own allocated memory.
  762. */
  763. freechunks(&cstate);
  764. return (rc);
  765. }
  766. /*
  767. * entry point for using the compiler with no pcap open
  768. * pass in all the stuff that is needed explicitly instead.
  769. */
  770. int
  771. pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
  772. struct bpf_program *program,
  773. const char *buf, int optimize, bpf_u_int32 mask)
  774. {
  775. pcap_t *p;
  776. int ret;
  777. p = pcap_open_dead(linktype_arg, snaplen_arg);
  778. if (p == NULL)
  779. return (-1);
  780. ret = pcap_compile(p, program, buf, optimize, mask);
  781. pcap_close(p);
  782. return (ret);
  783. }
  784. /*
  785. * Clean up a "struct bpf_program" by freeing all the memory allocated
  786. * in it.
  787. */
  788. void
  789. pcap_freecode(struct bpf_program *program)
  790. {
  791. program->bf_len = 0;
  792. if (program->bf_insns != NULL) {
  793. free((char *)program->bf_insns);
  794. program->bf_insns = NULL;
  795. }
  796. }
  797. /*
  798. * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
  799. * which of the jt and jf fields has been resolved and which is a pointer
  800. * back to another unresolved block (or nil). At least one of the fields
  801. * in each block is already resolved.
  802. */
  803. static void
  804. backpatch(struct block *list, struct block *target)
  805. {
  806. struct block *next;
  807. while (list) {
  808. if (!list->sense) {
  809. next = JT(list);
  810. JT(list) = target;
  811. } else {
  812. next = JF(list);
  813. JF(list) = target;
  814. }
  815. list = next;
  816. }
  817. }
  818. /*
  819. * Merge the lists in b0 and b1, using the 'sense' field to indicate
  820. * which of jt and jf is the link.
  821. */
  822. static void
  823. merge(struct block *b0, struct block *b1)
  824. {
  825. register struct block **p = &b0;
  826. /* Find end of list. */
  827. while (*p)
  828. p = !((*p)->sense) ? &JT(*p) : &JF(*p);
  829. /* Concatenate the lists. */
  830. *p = b1;
  831. }
  832. int
  833. finish_parse(compiler_state_t *cstate, struct block *p)
  834. {
  835. struct block *ppi_dlt_check;
  836. /*
  837. * Catch errors reported by us and routines below us, and return -1
  838. * on an error.
  839. */
  840. if (setjmp(cstate->top_ctx))
  841. return (-1);
  842. /*
  843. * Insert before the statements of the first (root) block any
  844. * statements needed to load the lengths of any variable-length
  845. * headers into registers.
  846. *
  847. * XXX - a fancier strategy would be to insert those before the
  848. * statements of all blocks that use those lengths and that
  849. * have no predecessors that use them, so that we only compute
  850. * the lengths if we need them. There might be even better
  851. * approaches than that.
  852. *
  853. * However, those strategies would be more complicated, and
  854. * as we don't generate code to compute a length if the
  855. * program has no tests that use the length, and as most
  856. * tests will probably use those lengths, we would just
  857. * postpone computing the lengths so that it's not done
  858. * for tests that fail early, and it's not clear that's
  859. * worth the effort.
  860. */
  861. insert_compute_vloffsets(cstate, p->head);
  862. /*
  863. * For DLT_PPI captures, generate a check of the per-packet
  864. * DLT value to make sure it's DLT_IEEE802_11.
  865. *
  866. * XXX - TurboCap cards use DLT_PPI for Ethernet.
  867. * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
  868. * with appropriate Ethernet information and use that rather
  869. * than using something such as DLT_PPI where you don't know
  870. * the link-layer header type until runtime, which, in the
  871. * general case, would force us to generate both Ethernet *and*
  872. * 802.11 code (*and* anything else for which PPI is used)
  873. * and choose between them early in the BPF program?
  874. */
  875. ppi_dlt_check = gen_ppi_dlt_check(cstate);
  876. if (ppi_dlt_check != NULL)
  877. gen_and(ppi_dlt_check, p);
  878. backpatch(p, gen_retblk(cstate, cstate->snaplen));
  879. p->sense = !p->sense;
  880. backpatch(p, gen_retblk(cstate, 0));
  881. cstate->ic.root = p->head;
  882. return (0);
  883. }
  884. void
  885. gen_and(struct block *b0, struct block *b1)
  886. {
  887. backpatch(b0, b1->head);
  888. b0->sense = !b0->sense;
  889. b1->sense = !b1->sense;
  890. merge(b1, b0);
  891. b1->sense = !b1->sense;
  892. b1->head = b0->head;
  893. }
  894. void
  895. gen_or(struct block *b0, struct block *b1)
  896. {
  897. b0->sense = !b0->sense;
  898. backpatch(b0, b1->head);
  899. b0->sense = !b0->sense;
  900. merge(b1, b0);
  901. b1->head = b0->head;
  902. }
  903. void
  904. gen_not(struct block *b)
  905. {
  906. b->sense = !b->sense;
  907. }
  908. static struct block *
  909. gen_cmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  910. u_int size, bpf_u_int32 v)
  911. {
  912. return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
  913. }
  914. static struct block *
  915. gen_cmp_gt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  916. u_int size, bpf_u_int32 v)
  917. {
  918. return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
  919. }
  920. static struct block *
  921. gen_cmp_ge(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  922. u_int size, bpf_u_int32 v)
  923. {
  924. return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
  925. }
  926. static struct block *
  927. gen_cmp_lt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  928. u_int size, bpf_u_int32 v)
  929. {
  930. return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
  931. }
  932. static struct block *
  933. gen_cmp_le(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  934. u_int size, bpf_u_int32 v)
  935. {
  936. return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
  937. }
  938. static struct block *
  939. gen_mcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  940. u_int size, bpf_u_int32 v, bpf_u_int32 mask)
  941. {
  942. return gen_ncmp(cstate, offrel, offset, size, mask, BPF_JEQ, 0, v);
  943. }
  944. static struct block *
  945. gen_bcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  946. u_int size, const u_char *v)
  947. {
  948. register struct block *b, *tmp;
  949. b = NULL;
  950. while (size >= 4) {
  951. register const u_char *p = &v[size - 4];
  952. tmp = gen_cmp(cstate, offrel, offset + size - 4, BPF_W,
  953. EXTRACT_BE_U_4(p));
  954. if (b != NULL)
  955. gen_and(b, tmp);
  956. b = tmp;
  957. size -= 4;
  958. }
  959. while (size >= 2) {
  960. register const u_char *p = &v[size - 2];
  961. tmp = gen_cmp(cstate, offrel, offset + size - 2, BPF_H,
  962. EXTRACT_BE_U_2(p));
  963. if (b != NULL)
  964. gen_and(b, tmp);
  965. b = tmp;
  966. size -= 2;
  967. }
  968. if (size > 0) {
  969. tmp = gen_cmp(cstate, offrel, offset, BPF_B, v[0]);
  970. if (b != NULL)
  971. gen_and(b, tmp);
  972. b = tmp;
  973. }
  974. return b;
  975. }
  976. /*
  977. * AND the field of size "size" at offset "offset" relative to the header
  978. * specified by "offrel" with "mask", and compare it with the value "v"
  979. * with the test specified by "jtype"; if "reverse" is true, the test
  980. * should test the opposite of "jtype".
  981. */
  982. static struct block *
  983. gen_ncmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  984. u_int size, bpf_u_int32 mask, int jtype, int reverse,
  985. bpf_u_int32 v)
  986. {
  987. struct slist *s, *s2;
  988. struct block *b;
  989. s = gen_load_a(cstate, offrel, offset, size);
  990. if (mask != 0xffffffff) {
  991. s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
  992. s2->s.k = mask;
  993. sappend(s, s2);
  994. }
  995. b = new_block(cstate, JMP(jtype));
  996. b->stmts = s;
  997. b->s.k = v;
  998. if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
  999. gen_not(b);
  1000. return b;
  1001. }
  1002. static int
  1003. init_linktype(compiler_state_t *cstate, pcap_t *p)
  1004. {
  1005. cstate->pcap_fddipad = p->fddipad;
  1006. /*
  1007. * We start out with only one link-layer header.
  1008. */
  1009. cstate->outermostlinktype = pcap_datalink(p);
  1010. cstate->off_outermostlinkhdr.constant_part = 0;
  1011. cstate->off_outermostlinkhdr.is_variable = 0;
  1012. cstate->off_outermostlinkhdr.reg = -1;
  1013. cstate->prevlinktype = cstate->outermostlinktype;
  1014. cstate->off_prevlinkhdr.constant_part = 0;
  1015. cstate->off_prevlinkhdr.is_variable = 0;
  1016. cstate->off_prevlinkhdr.reg = -1;
  1017. cstate->linktype = cstate->outermostlinktype;
  1018. cstate->off_linkhdr.constant_part = 0;
  1019. cstate->off_linkhdr.is_variable = 0;
  1020. cstate->off_linkhdr.reg = -1;
  1021. /*
  1022. * XXX
  1023. */
  1024. cstate->off_linkpl.constant_part = 0;
  1025. cstate->off_linkpl.is_variable = 0;
  1026. cstate->off_linkpl.reg = -1;
  1027. cstate->off_linktype.constant_part = 0;
  1028. cstate->off_linktype.is_variable = 0;
  1029. cstate->off_linktype.reg = -1;
  1030. /*
  1031. * Assume it's not raw ATM with a pseudo-header, for now.
  1032. */
  1033. cstate->is_atm = 0;
  1034. cstate->off_vpi = OFFSET_NOT_SET;
  1035. cstate->off_vci = OFFSET_NOT_SET;
  1036. cstate->off_proto = OFFSET_NOT_SET;
  1037. cstate->off_payload = OFFSET_NOT_SET;
  1038. /*
  1039. * And not Geneve.
  1040. */
  1041. cstate->is_geneve = 0;
  1042. /*
  1043. * No variable length VLAN offset by default
  1044. */
  1045. cstate->is_vlan_vloffset = 0;
  1046. /*
  1047. * And assume we're not doing SS7.
  1048. */
  1049. cstate->off_li = OFFSET_NOT_SET;
  1050. cstate->off_li_hsl = OFFSET_NOT_SET;
  1051. cstate->off_sio = OFFSET_NOT_SET;
  1052. cstate->off_opc = OFFSET_NOT_SET;
  1053. cstate->off_dpc = OFFSET_NOT_SET;
  1054. cstate->off_sls = OFFSET_NOT_SET;
  1055. cstate->label_stack_depth = 0;
  1056. cstate->vlan_stack_depth = 0;
  1057. switch (cstate->linktype) {
  1058. case DLT_ARCNET:
  1059. cstate->off_linktype.constant_part = 2;
  1060. cstate->off_linkpl.constant_part = 6;
  1061. cstate->off_nl = 0; /* XXX in reality, variable! */
  1062. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1063. break;
  1064. case DLT_ARCNET_LINUX:
  1065. cstate->off_linktype.constant_part = 4;
  1066. cstate->off_linkpl.constant_part = 8;
  1067. cstate->off_nl = 0; /* XXX in reality, variable! */
  1068. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1069. break;
  1070. case DLT_EN10MB:
  1071. cstate->off_linktype.constant_part = 12;
  1072. cstate->off_linkpl.constant_part = 14; /* Ethernet header length */
  1073. cstate->off_nl = 0; /* Ethernet II */
  1074. cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
  1075. break;
  1076. case DLT_SLIP:
  1077. /*
  1078. * SLIP doesn't have a link level type. The 16 byte
  1079. * header is hacked into our SLIP driver.
  1080. */
  1081. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1082. cstate->off_linkpl.constant_part = 16;
  1083. cstate->off_nl = 0;
  1084. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1085. break;
  1086. case DLT_SLIP_BSDOS:
  1087. /* XXX this may be the same as the DLT_PPP_BSDOS case */
  1088. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1089. /* XXX end */
  1090. cstate->off_linkpl.constant_part = 24;
  1091. cstate->off_nl = 0;
  1092. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1093. break;
  1094. case DLT_NULL:
  1095. case DLT_LOOP:
  1096. cstate->off_linktype.constant_part = 0;
  1097. cstate->off_linkpl.constant_part = 4;
  1098. cstate->off_nl = 0;
  1099. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1100. break;
  1101. case DLT_ENC:
  1102. cstate->off_linktype.constant_part = 0;
  1103. cstate->off_linkpl.constant_part = 12;
  1104. cstate->off_nl = 0;
  1105. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1106. break;
  1107. case DLT_PPP:
  1108. case DLT_PPP_PPPD:
  1109. case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
  1110. case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
  1111. cstate->off_linktype.constant_part = 2; /* skip HDLC-like framing */
  1112. cstate->off_linkpl.constant_part = 4; /* skip HDLC-like framing and protocol field */
  1113. cstate->off_nl = 0;
  1114. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1115. break;
  1116. case DLT_PPP_ETHER:
  1117. /*
  1118. * This does no include the Ethernet header, and
  1119. * only covers session state.
  1120. */
  1121. cstate->off_linktype.constant_part = 6;
  1122. cstate->off_linkpl.constant_part = 8;
  1123. cstate->off_nl = 0;
  1124. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1125. break;
  1126. case DLT_PPP_BSDOS:
  1127. cstate->off_linktype.constant_part = 5;
  1128. cstate->off_linkpl.constant_part = 24;
  1129. cstate->off_nl = 0;
  1130. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1131. break;
  1132. case DLT_FDDI:
  1133. /*
  1134. * FDDI doesn't really have a link-level type field.
  1135. * We set "off_linktype" to the offset of the LLC header.
  1136. *
  1137. * To check for Ethernet types, we assume that SSAP = SNAP
  1138. * is being used and pick out the encapsulated Ethernet type.
  1139. * XXX - should we generate code to check for SNAP?
  1140. */
  1141. cstate->off_linktype.constant_part = 13;
  1142. cstate->off_linktype.constant_part += cstate->pcap_fddipad;
  1143. cstate->off_linkpl.constant_part = 13; /* FDDI MAC header length */
  1144. cstate->off_linkpl.constant_part += cstate->pcap_fddipad;
  1145. cstate->off_nl = 8; /* 802.2+SNAP */
  1146. cstate->off_nl_nosnap = 3; /* 802.2 */
  1147. break;
  1148. case DLT_IEEE802:
  1149. /*
  1150. * Token Ring doesn't really have a link-level type field.
  1151. * We set "off_linktype" to the offset of the LLC header.
  1152. *
  1153. * To check for Ethernet types, we assume that SSAP = SNAP
  1154. * is being used and pick out the encapsulated Ethernet type.
  1155. * XXX - should we generate code to check for SNAP?
  1156. *
  1157. * XXX - the header is actually variable-length.
  1158. * Some various Linux patched versions gave 38
  1159. * as "off_linktype" and 40 as "off_nl"; however,
  1160. * if a token ring packet has *no* routing
  1161. * information, i.e. is not source-routed, the correct
  1162. * values are 20 and 22, as they are in the vanilla code.
  1163. *
  1164. * A packet is source-routed iff the uppermost bit
  1165. * of the first byte of the source address, at an
  1166. * offset of 8, has the uppermost bit set. If the
  1167. * packet is source-routed, the total number of bytes
  1168. * of routing information is 2 plus bits 0x1F00 of
  1169. * the 16-bit value at an offset of 14 (shifted right
  1170. * 8 - figure out which byte that is).
  1171. */
  1172. cstate->off_linktype.constant_part = 14;
  1173. cstate->off_linkpl.constant_part = 14; /* Token Ring MAC header length */
  1174. cstate->off_nl = 8; /* 802.2+SNAP */
  1175. cstate->off_nl_nosnap = 3; /* 802.2 */
  1176. break;
  1177. case DLT_PRISM_HEADER:
  1178. case DLT_IEEE802_11_RADIO_AVS:
  1179. case DLT_IEEE802_11_RADIO:
  1180. cstate->off_linkhdr.is_variable = 1;
  1181. /* Fall through, 802.11 doesn't have a variable link
  1182. * prefix but is otherwise the same. */
  1183. /* FALLTHROUGH */
  1184. case DLT_IEEE802_11:
  1185. /*
  1186. * 802.11 doesn't really have a link-level type field.
  1187. * We set "off_linktype.constant_part" to the offset of
  1188. * the LLC header.
  1189. *
  1190. * To check for Ethernet types, we assume that SSAP = SNAP
  1191. * is being used and pick out the encapsulated Ethernet type.
  1192. * XXX - should we generate code to check for SNAP?
  1193. *
  1194. * We also handle variable-length radio headers here.
  1195. * The Prism header is in theory variable-length, but in
  1196. * practice it's always 144 bytes long. However, some
  1197. * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
  1198. * sometimes or always supply an AVS header, so we
  1199. * have to check whether the radio header is a Prism
  1200. * header or an AVS header, so, in practice, it's
  1201. * variable-length.
  1202. */
  1203. cstate->off_linktype.constant_part = 24;
  1204. cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */
  1205. cstate->off_linkpl.is_variable = 1;
  1206. cstate->off_nl = 8; /* 802.2+SNAP */
  1207. cstate->off_nl_nosnap = 3; /* 802.2 */
  1208. break;
  1209. case DLT_PPI:
  1210. /*
  1211. * At the moment we treat PPI the same way that we treat
  1212. * normal Radiotap encoded packets. The difference is in
  1213. * the function that generates the code at the beginning
  1214. * to compute the header length. Since this code generator
  1215. * of PPI supports bare 802.11 encapsulation only (i.e.
  1216. * the encapsulated DLT should be DLT_IEEE802_11) we
  1217. * generate code to check for this too.
  1218. */
  1219. cstate->off_linktype.constant_part = 24;
  1220. cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */
  1221. cstate->off_linkpl.is_variable = 1;
  1222. cstate->off_linkhdr.is_variable = 1;
  1223. cstate->off_nl = 8; /* 802.2+SNAP */
  1224. cstate->off_nl_nosnap = 3; /* 802.2 */
  1225. break;
  1226. case DLT_ATM_RFC1483:
  1227. case DLT_ATM_CLIP: /* Linux ATM defines this */
  1228. /*
  1229. * assume routed, non-ISO PDUs
  1230. * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
  1231. *
  1232. * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
  1233. * or PPP with the PPP NLPID (e.g., PPPoA)? The
  1234. * latter would presumably be treated the way PPPoE
  1235. * should be, so you can do "pppoe and udp port 2049"
  1236. * or "pppoa and tcp port 80" and have it check for
  1237. * PPPo{A,E} and a PPP protocol of IP and....
  1238. */
  1239. cstate->off_linktype.constant_part = 0;
  1240. cstate->off_linkpl.constant_part = 0; /* packet begins with LLC header */
  1241. cstate->off_nl = 8; /* 802.2+SNAP */
  1242. cstate->off_nl_nosnap = 3; /* 802.2 */
  1243. break;
  1244. case DLT_SUNATM:
  1245. /*
  1246. * Full Frontal ATM; you get AALn PDUs with an ATM
  1247. * pseudo-header.
  1248. */
  1249. cstate->is_atm = 1;
  1250. cstate->off_vpi = SUNATM_VPI_POS;
  1251. cstate->off_vci = SUNATM_VCI_POS;
  1252. cstate->off_proto = PROTO_POS;
  1253. cstate->off_payload = SUNATM_PKT_BEGIN_POS;
  1254. cstate->off_linktype.constant_part = cstate->off_payload;
  1255. cstate->off_linkpl.constant_part = cstate->off_payload; /* if LLC-encapsulated */
  1256. cstate->off_nl = 8; /* 802.2+SNAP */
  1257. cstate->off_nl_nosnap = 3; /* 802.2 */
  1258. break;
  1259. case DLT_RAW:
  1260. case DLT_IPV4:
  1261. case DLT_IPV6:
  1262. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1263. cstate->off_linkpl.constant_part = 0;
  1264. cstate->off_nl = 0;
  1265. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1266. break;
  1267. case DLT_LINUX_SLL: /* fake header for Linux cooked socket v1 */
  1268. cstate->off_linktype.constant_part = 14;
  1269. cstate->off_linkpl.constant_part = 16;
  1270. cstate->off_nl = 0;
  1271. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1272. break;
  1273. case DLT_LINUX_SLL2: /* fake header for Linux cooked socket v2 */
  1274. cstate->off_linktype.constant_part = 0;
  1275. cstate->off_linkpl.constant_part = 20;
  1276. cstate->off_nl = 0;
  1277. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1278. break;
  1279. case DLT_LTALK:
  1280. /*
  1281. * LocalTalk does have a 1-byte type field in the LLAP header,
  1282. * but really it just indicates whether there is a "short" or
  1283. * "long" DDP packet following.
  1284. */
  1285. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1286. cstate->off_linkpl.constant_part = 0;
  1287. cstate->off_nl = 0;
  1288. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1289. break;
  1290. case DLT_IP_OVER_FC:
  1291. /*
  1292. * RFC 2625 IP-over-Fibre-Channel doesn't really have a
  1293. * link-level type field. We set "off_linktype" to the
  1294. * offset of the LLC header.
  1295. *
  1296. * To check for Ethernet types, we assume that SSAP = SNAP
  1297. * is being used and pick out the encapsulated Ethernet type.
  1298. * XXX - should we generate code to check for SNAP? RFC
  1299. * 2625 says SNAP should be used.
  1300. */
  1301. cstate->off_linktype.constant_part = 16;
  1302. cstate->off_linkpl.constant_part = 16;
  1303. cstate->off_nl = 8; /* 802.2+SNAP */
  1304. cstate->off_nl_nosnap = 3; /* 802.2 */
  1305. break;
  1306. case DLT_FRELAY:
  1307. /*
  1308. * XXX - we should set this to handle SNAP-encapsulated
  1309. * frames (NLPID of 0x80).
  1310. */
  1311. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1312. cstate->off_linkpl.constant_part = 0;
  1313. cstate->off_nl = 0;
  1314. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1315. break;
  1316. /*
  1317. * the only BPF-interesting FRF.16 frames are non-control frames;
  1318. * Frame Relay has a variable length link-layer
  1319. * so lets start with offset 4 for now and increments later on (FIXME);
  1320. */
  1321. case DLT_MFR:
  1322. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1323. cstate->off_linkpl.constant_part = 0;
  1324. cstate->off_nl = 4;
  1325. cstate->off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
  1326. break;
  1327. case DLT_APPLE_IP_OVER_IEEE1394:
  1328. cstate->off_linktype.constant_part = 16;
  1329. cstate->off_linkpl.constant_part = 18;
  1330. cstate->off_nl = 0;
  1331. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1332. break;
  1333. case DLT_SYMANTEC_FIREWALL:
  1334. cstate->off_linktype.constant_part = 6;
  1335. cstate->off_linkpl.constant_part = 44;
  1336. cstate->off_nl = 0; /* Ethernet II */
  1337. cstate->off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
  1338. break;
  1339. #ifdef HAVE_NET_PFVAR_H
  1340. case DLT_PFLOG:
  1341. cstate->off_linktype.constant_part = 0;
  1342. cstate->off_linkpl.constant_part = PFLOG_HDRLEN;
  1343. cstate->off_nl = 0;
  1344. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  1345. break;
  1346. #endif
  1347. case DLT_JUNIPER_MFR:
  1348. case DLT_JUNIPER_MLFR:
  1349. case DLT_JUNIPER_MLPPP:
  1350. case DLT_JUNIPER_PPP:
  1351. case DLT_JUNIPER_CHDLC:
  1352. case DLT_JUNIPER_FRELAY:
  1353. cstate->off_linktype.constant_part = 4;
  1354. cstate->off_linkpl.constant_part = 4;
  1355. cstate->off_nl = 0;
  1356. cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
  1357. break;
  1358. case DLT_JUNIPER_ATM1:
  1359. cstate->off_linktype.constant_part = 4; /* in reality variable between 4-8 */
  1360. cstate->off_linkpl.constant_part = 4; /* in reality variable between 4-8 */
  1361. cstate->off_nl = 0;
  1362. cstate->off_nl_nosnap = 10;
  1363. break;
  1364. case DLT_JUNIPER_ATM2:
  1365. cstate->off_linktype.constant_part = 8; /* in reality variable between 8-12 */
  1366. cstate->off_linkpl.constant_part = 8; /* in reality variable between 8-12 */
  1367. cstate->off_nl = 0;
  1368. cstate->off_nl_nosnap = 10;
  1369. break;
  1370. /* frames captured on a Juniper PPPoE service PIC
  1371. * contain raw ethernet frames */
  1372. case DLT_JUNIPER_PPPOE:
  1373. case DLT_JUNIPER_ETHER:
  1374. cstate->off_linkpl.constant_part = 14;
  1375. cstate->off_linktype.constant_part = 16;
  1376. cstate->off_nl = 18; /* Ethernet II */
  1377. cstate->off_nl_nosnap = 21; /* 802.3+802.2 */
  1378. break;
  1379. case DLT_JUNIPER_PPPOE_ATM:
  1380. cstate->off_linktype.constant_part = 4;
  1381. cstate->off_linkpl.constant_part = 6;
  1382. cstate->off_nl = 0;
  1383. cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
  1384. break;
  1385. case DLT_JUNIPER_GGSN:
  1386. cstate->off_linktype.constant_part = 6;
  1387. cstate->off_linkpl.constant_part = 12;
  1388. cstate->off_nl = 0;
  1389. cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
  1390. break;
  1391. case DLT_JUNIPER_ES:
  1392. cstate->off_linktype.constant_part = 6;
  1393. cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */
  1394. cstate->off_nl = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */
  1395. cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
  1396. break;
  1397. case DLT_JUNIPER_MONITOR:
  1398. cstate->off_linktype.constant_part = 12;
  1399. cstate->off_linkpl.constant_part = 12;
  1400. cstate->off_nl = 0; /* raw IP/IP6 header */
  1401. cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
  1402. break;
  1403. case DLT_BACNET_MS_TP:
  1404. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1405. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1406. cstate->off_nl = OFFSET_NOT_SET;
  1407. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1408. break;
  1409. case DLT_JUNIPER_SERVICES:
  1410. cstate->off_linktype.constant_part = 12;
  1411. cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */
  1412. cstate->off_nl = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */
  1413. cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
  1414. break;
  1415. case DLT_JUNIPER_VP:
  1416. cstate->off_linktype.constant_part = 18;
  1417. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1418. cstate->off_nl = OFFSET_NOT_SET;
  1419. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1420. break;
  1421. case DLT_JUNIPER_ST:
  1422. cstate->off_linktype.constant_part = 18;
  1423. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1424. cstate->off_nl = OFFSET_NOT_SET;
  1425. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1426. break;
  1427. case DLT_JUNIPER_ISM:
  1428. cstate->off_linktype.constant_part = 8;
  1429. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1430. cstate->off_nl = OFFSET_NOT_SET;
  1431. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1432. break;
  1433. case DLT_JUNIPER_VS:
  1434. case DLT_JUNIPER_SRX_E2E:
  1435. case DLT_JUNIPER_FIBRECHANNEL:
  1436. case DLT_JUNIPER_ATM_CEMIC:
  1437. cstate->off_linktype.constant_part = 8;
  1438. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1439. cstate->off_nl = OFFSET_NOT_SET;
  1440. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1441. break;
  1442. case DLT_MTP2:
  1443. cstate->off_li = 2;
  1444. cstate->off_li_hsl = 4;
  1445. cstate->off_sio = 3;
  1446. cstate->off_opc = 4;
  1447. cstate->off_dpc = 4;
  1448. cstate->off_sls = 7;
  1449. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1450. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1451. cstate->off_nl = OFFSET_NOT_SET;
  1452. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1453. break;
  1454. case DLT_MTP2_WITH_PHDR:
  1455. cstate->off_li = 6;
  1456. cstate->off_li_hsl = 8;
  1457. cstate->off_sio = 7;
  1458. cstate->off_opc = 8;
  1459. cstate->off_dpc = 8;
  1460. cstate->off_sls = 11;
  1461. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1462. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1463. cstate->off_nl = OFFSET_NOT_SET;
  1464. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1465. break;
  1466. case DLT_ERF:
  1467. cstate->off_li = 22;
  1468. cstate->off_li_hsl = 24;
  1469. cstate->off_sio = 23;
  1470. cstate->off_opc = 24;
  1471. cstate->off_dpc = 24;
  1472. cstate->off_sls = 27;
  1473. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1474. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1475. cstate->off_nl = OFFSET_NOT_SET;
  1476. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1477. break;
  1478. case DLT_PFSYNC:
  1479. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1480. cstate->off_linkpl.constant_part = 4;
  1481. cstate->off_nl = 0;
  1482. cstate->off_nl_nosnap = 0;
  1483. break;
  1484. case DLT_AX25_KISS:
  1485. /*
  1486. * Currently, only raw "link[N:M]" filtering is supported.
  1487. */
  1488. cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */
  1489. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1490. cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */
  1491. cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */
  1492. break;
  1493. case DLT_IPNET:
  1494. cstate->off_linktype.constant_part = 1;
  1495. cstate->off_linkpl.constant_part = 24; /* ipnet header length */
  1496. cstate->off_nl = 0;
  1497. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1498. break;
  1499. case DLT_NETANALYZER:
  1500. cstate->off_linkhdr.constant_part = 4; /* Ethernet header is past 4-byte pseudo-header */
  1501. cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
  1502. cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+Ethernet header length */
  1503. cstate->off_nl = 0; /* Ethernet II */
  1504. cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
  1505. break;
  1506. case DLT_NETANALYZER_TRANSPARENT:
  1507. cstate->off_linkhdr.constant_part = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
  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+preamble+SFD+Ethernet header length */
  1510. cstate->off_nl = 0; /* Ethernet II */
  1511. cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
  1512. break;
  1513. default:
  1514. /*
  1515. * For values in the range in which we've assigned new
  1516. * DLT_ values, only raw "link[N:M]" filtering is supported.
  1517. */
  1518. if (cstate->linktype >= DLT_MATCHING_MIN &&
  1519. cstate->linktype <= DLT_MATCHING_MAX) {
  1520. cstate->off_linktype.constant_part = OFFSET_NOT_SET;
  1521. cstate->off_linkpl.constant_part = OFFSET_NOT_SET;
  1522. cstate->off_nl = OFFSET_NOT_SET;
  1523. cstate->off_nl_nosnap = OFFSET_NOT_SET;
  1524. } else {
  1525. bpf_set_error(cstate, "unknown data link type %d", cstate->linktype);
  1526. return (-1);
  1527. }
  1528. break;
  1529. }
  1530. cstate->off_outermostlinkhdr = cstate->off_prevlinkhdr = cstate->off_linkhdr;
  1531. return (0);
  1532. }
  1533. /*
  1534. * Load a value relative to the specified absolute offset.
  1535. */
  1536. static struct slist *
  1537. gen_load_absoffsetrel(compiler_state_t *cstate, bpf_abs_offset *abs_offset,
  1538. u_int offset, u_int size)
  1539. {
  1540. struct slist *s, *s2;
  1541. s = gen_abs_offset_varpart(cstate, abs_offset);
  1542. /*
  1543. * If "s" is non-null, it has code to arrange that the X register
  1544. * contains the variable part of the absolute offset, so we
  1545. * generate a load relative to that, with an offset of
  1546. * abs_offset->constant_part + offset.
  1547. *
  1548. * Otherwise, we can do an absolute load with an offset of
  1549. * abs_offset->constant_part + offset.
  1550. */
  1551. if (s != NULL) {
  1552. /*
  1553. * "s" points to a list of statements that puts the
  1554. * variable part of the absolute offset into the X register.
  1555. * Do an indirect load, to use the X register as an offset.
  1556. */
  1557. s2 = new_stmt(cstate, BPF_LD|BPF_IND|size);
  1558. s2->s.k = abs_offset->constant_part + offset;
  1559. sappend(s, s2);
  1560. } else {
  1561. /*
  1562. * There is no variable part of the absolute offset, so
  1563. * just do an absolute load.
  1564. */
  1565. s = new_stmt(cstate, BPF_LD|BPF_ABS|size);
  1566. s->s.k = abs_offset->constant_part + offset;
  1567. }
  1568. return s;
  1569. }
  1570. /*
  1571. * Load a value relative to the beginning of the specified header.
  1572. */
  1573. static struct slist *
  1574. gen_load_a(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
  1575. u_int size)
  1576. {
  1577. struct slist *s, *s2;
  1578. /*
  1579. * Squelch warnings from compilers that *don't* assume that
  1580. * offrel always has a valid enum value and therefore don't
  1581. * assume that we'll always go through one of the case arms.
  1582. *
  1583. * If we have a default case, compilers that *do* assume that
  1584. * will then complain about the default case code being
  1585. * unreachable.
  1586. *
  1587. * Damned if you do, damned if you don't.
  1588. */
  1589. s = NULL;
  1590. switch (offrel) {
  1591. case OR_PACKET:
  1592. s = new_stmt(cstate, BPF_LD|BPF_ABS|size);
  1593. s->s.k = offset;
  1594. break;
  1595. case OR_LINKHDR:
  1596. s = gen_load_absoffsetrel(cstate, &cstate->off_linkhdr, offset, size);
  1597. break;
  1598. case OR_PREVLINKHDR:
  1599. s = gen_load_absoffsetrel(cstate, &cstate->off_prevlinkhdr, offset, size);
  1600. break;
  1601. case OR_LLC:
  1602. s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, offset, size);
  1603. break;
  1604. case OR_PREVMPLSHDR:
  1605. s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl - 4 + offset, size);
  1606. break;
  1607. case OR_LINKPL:
  1608. s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + offset, size);
  1609. break;
  1610. case OR_LINKPL_NOSNAP:
  1611. s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl_nosnap + offset, size);
  1612. break;
  1613. case OR_LINKTYPE:
  1614. s = gen_load_absoffsetrel(cstate, &cstate->off_linktype, offset, size);
  1615. break;
  1616. case OR_TRAN_IPV4:
  1617. /*
  1618. * Load the X register with the length of the IPv4 header
  1619. * (plus the offset of the link-layer header, if it's
  1620. * preceded by a variable-length header such as a radio
  1621. * header), in bytes.
  1622. */
  1623. s = gen_loadx_iphdrlen(cstate);
  1624. /*
  1625. * Load the item at {offset of the link-layer payload} +
  1626. * {offset, relative to the start of the link-layer
  1627. * paylod, of the IPv4 header} + {length of the IPv4 header} +
  1628. * {specified offset}.
  1629. *
  1630. * If the offset of the link-layer payload is variable,
  1631. * the variable part of that offset is included in the
  1632. * value in the X register, and we include the constant
  1633. * part in the offset of the load.
  1634. */
  1635. s2 = new_stmt(cstate, BPF_LD|BPF_IND|size);
  1636. s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + offset;
  1637. sappend(s, s2);
  1638. break;
  1639. case OR_TRAN_IPV6:
  1640. s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + 40 + offset, size);
  1641. break;
  1642. }
  1643. return s;
  1644. }
  1645. /*
  1646. * Generate code to load into the X register the sum of the length of
  1647. * the IPv4 header and the variable part of the offset of the link-layer
  1648. * payload.
  1649. */
  1650. static struct slist *
  1651. gen_loadx_iphdrlen(compiler_state_t *cstate)
  1652. {
  1653. struct slist *s, *s2;
  1654. s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
  1655. if (s != NULL) {
  1656. /*
  1657. * The offset of the link-layer payload has a variable
  1658. * part. "s" points to a list of statements that put
  1659. * the variable part of that offset into the X register.
  1660. *
  1661. * The 4*([k]&0xf) addressing mode can't be used, as we
  1662. * don't have a constant offset, so we have to load the
  1663. * value in question into the A register and add to it
  1664. * the value from the X register.
  1665. */
  1666. s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
  1667. s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  1668. sappend(s, s2);
  1669. s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
  1670. s2->s.k = 0xf;
  1671. sappend(s, s2);
  1672. s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
  1673. s2->s.k = 2;
  1674. sappend(s, s2);
  1675. /*
  1676. * The A register now contains the length of the IP header.
  1677. * We need to add to it the variable part of the offset of
  1678. * the link-layer payload, which is still in the X
  1679. * register, and move the result into the X register.
  1680. */
  1681. sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
  1682. sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
  1683. } else {
  1684. /*
  1685. * The offset of the link-layer payload is a constant,
  1686. * so no code was generated to load the (non-existent)
  1687. * variable part of that offset.
  1688. *
  1689. * This means we can use the 4*([k]&0xf) addressing
  1690. * mode. Load the length of the IPv4 header, which
  1691. * is at an offset of cstate->off_nl from the beginning of
  1692. * the link-layer payload, and thus at an offset of
  1693. * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
  1694. * of the raw packet data, using that addressing mode.
  1695. */
  1696. s = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);
  1697. s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  1698. }
  1699. return s;
  1700. }
  1701. static struct block *
  1702. gen_uncond(compiler_state_t *cstate, int rsense)
  1703. {
  1704. struct block *b;
  1705. struct slist *s;
  1706. s = new_stmt(cstate, BPF_LD|BPF_IMM);
  1707. s->s.k = !rsense;
  1708. b = new_block(cstate, JMP(BPF_JEQ));
  1709. b->stmts = s;
  1710. return b;
  1711. }
  1712. static inline struct block *
  1713. gen_true(compiler_state_t *cstate)
  1714. {
  1715. return gen_uncond(cstate, 1);
  1716. }
  1717. static inline struct block *
  1718. gen_false(compiler_state_t *cstate)
  1719. {
  1720. return gen_uncond(cstate, 0);
  1721. }
  1722. /*
  1723. * Byte-swap a 32-bit number.
  1724. * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
  1725. * big-endian platforms.)
  1726. */
  1727. #define SWAPLONG(y) \
  1728. ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
  1729. /*
  1730. * Generate code to match a particular packet type.
  1731. *
  1732. * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
  1733. * value, if <= ETHERMTU. We use that to determine whether to
  1734. * match the type/length field or to check the type/length field for
  1735. * a value <= ETHERMTU to see whether it's a type field and then do
  1736. * the appropriate test.
  1737. */
  1738. static struct block *
  1739. gen_ether_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
  1740. {
  1741. struct block *b0, *b1;
  1742. switch (ll_proto) {
  1743. case LLCSAP_ISONS:
  1744. case LLCSAP_IP:
  1745. case LLCSAP_NETBEUI:
  1746. /*
  1747. * OSI protocols and NetBEUI always use 802.2 encapsulation,
  1748. * so we check the DSAP and SSAP.
  1749. *
  1750. * LLCSAP_IP checks for IP-over-802.2, rather
  1751. * than IP-over-Ethernet or IP-over-SNAP.
  1752. *
  1753. * XXX - should we check both the DSAP and the
  1754. * SSAP, like this, or should we check just the
  1755. * DSAP, as we do for other types <= ETHERMTU
  1756. * (i.e., other SAP values)?
  1757. */
  1758. b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
  1759. gen_not(b0);
  1760. b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto);
  1761. gen_and(b0, b1);
  1762. return b1;
  1763. case LLCSAP_IPX:
  1764. /*
  1765. * Check for;
  1766. *
  1767. * Ethernet_II frames, which are Ethernet
  1768. * frames with a frame type of ETHERTYPE_IPX;
  1769. *
  1770. * Ethernet_802.3 frames, which are 802.3
  1771. * frames (i.e., the type/length field is
  1772. * a length field, <= ETHERMTU, rather than
  1773. * a type field) with the first two bytes
  1774. * after the Ethernet/802.3 header being
  1775. * 0xFFFF;
  1776. *
  1777. * Ethernet_802.2 frames, which are 802.3
  1778. * frames with an 802.2 LLC header and
  1779. * with the IPX LSAP as the DSAP in the LLC
  1780. * header;
  1781. *
  1782. * Ethernet_SNAP frames, which are 802.3
  1783. * frames with an LLC header and a SNAP
  1784. * header and with an OUI of 0x000000
  1785. * (encapsulated Ethernet) and a protocol
  1786. * ID of ETHERTYPE_IPX in the SNAP header.
  1787. *
  1788. * XXX - should we generate the same code both
  1789. * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
  1790. */
  1791. /*
  1792. * This generates code to check both for the
  1793. * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
  1794. */
  1795. b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);
  1796. b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF);
  1797. gen_or(b0, b1);
  1798. /*
  1799. * Now we add code to check for SNAP frames with
  1800. * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
  1801. */
  1802. b0 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX);
  1803. gen_or(b0, b1);
  1804. /*
  1805. * Now we generate code to check for 802.3
  1806. * frames in general.
  1807. */
  1808. b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
  1809. gen_not(b0);
  1810. /*
  1811. * Now add the check for 802.3 frames before the
  1812. * check for Ethernet_802.2 and Ethernet_802.3,
  1813. * as those checks should only be done on 802.3
  1814. * frames, not on Ethernet frames.
  1815. */
  1816. gen_and(b0, b1);
  1817. /*
  1818. * Now add the check for Ethernet_II frames, and
  1819. * do that before checking for the other frame
  1820. * types.
  1821. */
  1822. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX);
  1823. gen_or(b0, b1);
  1824. return b1;
  1825. case ETHERTYPE_ATALK:
  1826. case ETHERTYPE_AARP:
  1827. /*
  1828. * EtherTalk (AppleTalk protocols on Ethernet link
  1829. * layer) may use 802.2 encapsulation.
  1830. */
  1831. /*
  1832. * Check for 802.2 encapsulation (EtherTalk phase 2?);
  1833. * we check for an Ethernet type field less than
  1834. * 1500, which means it's an 802.3 length field.
  1835. */
  1836. b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
  1837. gen_not(b0);
  1838. /*
  1839. * 802.2-encapsulated ETHERTYPE_ATALK packets are
  1840. * SNAP packets with an organization code of
  1841. * 0x080007 (Apple, for Appletalk) and a protocol
  1842. * type of ETHERTYPE_ATALK (Appletalk).
  1843. *
  1844. * 802.2-encapsulated ETHERTYPE_AARP packets are
  1845. * SNAP packets with an organization code of
  1846. * 0x000000 (encapsulated Ethernet) and a protocol
  1847. * type of ETHERTYPE_AARP (Appletalk ARP).
  1848. */
  1849. if (ll_proto == ETHERTYPE_ATALK)
  1850. b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
  1851. else /* ll_proto == ETHERTYPE_AARP */
  1852. b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP);
  1853. gen_and(b0, b1);
  1854. /*
  1855. * Check for Ethernet encapsulation (Ethertalk
  1856. * phase 1?); we just check for the Ethernet
  1857. * protocol type.
  1858. */
  1859. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
  1860. gen_or(b0, b1);
  1861. return b1;
  1862. default:
  1863. if (ll_proto <= ETHERMTU) {
  1864. /*
  1865. * This is an LLC SAP value, so the frames
  1866. * that match would be 802.2 frames.
  1867. * Check that the frame is an 802.2 frame
  1868. * (i.e., that the length/type field is
  1869. * a length field, <= ETHERMTU) and
  1870. * then check the DSAP.
  1871. */
  1872. b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
  1873. gen_not(b0);
  1874. b1 = gen_cmp(cstate, OR_LINKTYPE, 2, BPF_B, ll_proto);
  1875. gen_and(b0, b1);
  1876. return b1;
  1877. } else {
  1878. /*
  1879. * This is an Ethernet type, so compare
  1880. * the length/type field with it (if
  1881. * the frame is an 802.2 frame, the length
  1882. * field will be <= ETHERMTU, and, as
  1883. * "ll_proto" is > ETHERMTU, this test
  1884. * will fail and the frame won't match,
  1885. * which is what we want).
  1886. */
  1887. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
  1888. }
  1889. }
  1890. }
  1891. static struct block *
  1892. gen_loopback_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
  1893. {
  1894. /*
  1895. * For DLT_NULL, the link-layer header is a 32-bit word
  1896. * containing an AF_ value in *host* byte order, and for
  1897. * DLT_ENC, the link-layer header begins with a 32-bit
  1898. * word containing an AF_ value in host byte order.
  1899. *
  1900. * In addition, if we're reading a saved capture file,
  1901. * the host byte order in the capture may not be the
  1902. * same as the host byte order on this machine.
  1903. *
  1904. * For DLT_LOOP, the link-layer header is a 32-bit
  1905. * word containing an AF_ value in *network* byte order.
  1906. */
  1907. if (cstate->linktype == DLT_NULL || cstate->linktype == DLT_ENC) {
  1908. /*
  1909. * The AF_ value is in host byte order, but the BPF
  1910. * interpreter will convert it to network byte order.
  1911. *
  1912. * If this is a save file, and it's from a machine
  1913. * with the opposite byte order to ours, we byte-swap
  1914. * the AF_ value.
  1915. *
  1916. * Then we run it through "htonl()", and generate
  1917. * code to compare against the result.
  1918. */
  1919. if (cstate->bpf_pcap->rfile != NULL && cstate->bpf_pcap->swapped)
  1920. ll_proto = SWAPLONG(ll_proto);
  1921. ll_proto = htonl(ll_proto);
  1922. }
  1923. return (gen_cmp(cstate, OR_LINKHDR, 0, BPF_W, ll_proto));
  1924. }
  1925. /*
  1926. * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
  1927. * or IPv6 then we have an error.
  1928. */
  1929. static struct block *
  1930. gen_ipnet_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
  1931. {
  1932. switch (ll_proto) {
  1933. case ETHERTYPE_IP:
  1934. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET);
  1935. /*NOTREACHED*/
  1936. case ETHERTYPE_IPV6:
  1937. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET6);
  1938. /*NOTREACHED*/
  1939. default:
  1940. break;
  1941. }
  1942. return gen_false(cstate);
  1943. }
  1944. /*
  1945. * Generate code to match a particular packet type.
  1946. *
  1947. * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
  1948. * value, if <= ETHERMTU. We use that to determine whether to
  1949. * match the type field or to check the type field for the special
  1950. * LINUX_SLL_P_802_2 value and then do the appropriate test.
  1951. */
  1952. static struct block *
  1953. gen_linux_sll_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
  1954. {
  1955. struct block *b0, *b1;
  1956. switch (ll_proto) {
  1957. case LLCSAP_ISONS:
  1958. case LLCSAP_IP:
  1959. case LLCSAP_NETBEUI:
  1960. /*
  1961. * OSI protocols and NetBEUI always use 802.2 encapsulation,
  1962. * so we check the DSAP and SSAP.
  1963. *
  1964. * LLCSAP_IP checks for IP-over-802.2, rather
  1965. * than IP-over-Ethernet or IP-over-SNAP.
  1966. *
  1967. * XXX - should we check both the DSAP and the
  1968. * SSAP, like this, or should we check just the
  1969. * DSAP, as we do for other types <= ETHERMTU
  1970. * (i.e., other SAP values)?
  1971. */
  1972. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
  1973. b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto);
  1974. gen_and(b0, b1);
  1975. return b1;
  1976. case LLCSAP_IPX:
  1977. /*
  1978. * Ethernet_II frames, which are Ethernet
  1979. * frames with a frame type of ETHERTYPE_IPX;
  1980. *
  1981. * Ethernet_802.3 frames, which have a frame
  1982. * type of LINUX_SLL_P_802_3;
  1983. *
  1984. * Ethernet_802.2 frames, which are 802.3
  1985. * frames with an 802.2 LLC header (i.e, have
  1986. * a frame type of LINUX_SLL_P_802_2) and
  1987. * with the IPX LSAP as the DSAP in the LLC
  1988. * header;
  1989. *
  1990. * Ethernet_SNAP frames, which are 802.3
  1991. * frames with an LLC header and a SNAP
  1992. * header and with an OUI of 0x000000
  1993. * (encapsulated Ethernet) and a protocol
  1994. * ID of ETHERTYPE_IPX in the SNAP header.
  1995. *
  1996. * First, do the checks on LINUX_SLL_P_802_2
  1997. * frames; generate the check for either
  1998. * Ethernet_802.2 or Ethernet_SNAP frames, and
  1999. * then put a check for LINUX_SLL_P_802_2 frames
  2000. * before it.
  2001. */
  2002. b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);
  2003. b1 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX);
  2004. gen_or(b0, b1);
  2005. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
  2006. gen_and(b0, b1);
  2007. /*
  2008. * Now check for 802.3 frames and OR that with
  2009. * the previous test.
  2010. */
  2011. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_3);
  2012. gen_or(b0, b1);
  2013. /*
  2014. * Now add the check for Ethernet_II frames, and
  2015. * do that before checking for the other frame
  2016. * types.
  2017. */
  2018. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX);
  2019. gen_or(b0, b1);
  2020. return b1;
  2021. case ETHERTYPE_ATALK:
  2022. case ETHERTYPE_AARP:
  2023. /*
  2024. * EtherTalk (AppleTalk protocols on Ethernet link
  2025. * layer) may use 802.2 encapsulation.
  2026. */
  2027. /*
  2028. * Check for 802.2 encapsulation (EtherTalk phase 2?);
  2029. * we check for the 802.2 protocol type in the
  2030. * "Ethernet type" field.
  2031. */
  2032. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
  2033. /*
  2034. * 802.2-encapsulated ETHERTYPE_ATALK packets are
  2035. * SNAP packets with an organization code of
  2036. * 0x080007 (Apple, for Appletalk) and a protocol
  2037. * type of ETHERTYPE_ATALK (Appletalk).
  2038. *
  2039. * 802.2-encapsulated ETHERTYPE_AARP packets are
  2040. * SNAP packets with an organization code of
  2041. * 0x000000 (encapsulated Ethernet) and a protocol
  2042. * type of ETHERTYPE_AARP (Appletalk ARP).
  2043. */
  2044. if (ll_proto == ETHERTYPE_ATALK)
  2045. b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
  2046. else /* ll_proto == ETHERTYPE_AARP */
  2047. b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP);
  2048. gen_and(b0, b1);
  2049. /*
  2050. * Check for Ethernet encapsulation (Ethertalk
  2051. * phase 1?); we just check for the Ethernet
  2052. * protocol type.
  2053. */
  2054. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
  2055. gen_or(b0, b1);
  2056. return b1;
  2057. default:
  2058. if (ll_proto <= ETHERMTU) {
  2059. /*
  2060. * This is an LLC SAP value, so the frames
  2061. * that match would be 802.2 frames.
  2062. * Check for the 802.2 protocol type
  2063. * in the "Ethernet type" field, and
  2064. * then check the DSAP.
  2065. */
  2066. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);
  2067. b1 = gen_cmp(cstate, OR_LINKHDR, cstate->off_linkpl.constant_part, BPF_B,
  2068. ll_proto);
  2069. gen_and(b0, b1);
  2070. return b1;
  2071. } else {
  2072. /*
  2073. * This is an Ethernet type, so compare
  2074. * the length/type field with it (if
  2075. * the frame is an 802.2 frame, the length
  2076. * field will be <= ETHERMTU, and, as
  2077. * "ll_proto" is > ETHERMTU, this test
  2078. * will fail and the frame won't match,
  2079. * which is what we want).
  2080. */
  2081. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
  2082. }
  2083. }
  2084. }
  2085. static struct slist *
  2086. gen_load_prism_llprefixlen(compiler_state_t *cstate)
  2087. {
  2088. struct slist *s1, *s2;
  2089. struct slist *sjeq_avs_cookie;
  2090. struct slist *sjcommon;
  2091. /*
  2092. * This code is not compatible with the optimizer, as
  2093. * we are generating jmp instructions within a normal
  2094. * slist of instructions
  2095. */
  2096. cstate->no_optimize = 1;
  2097. /*
  2098. * Generate code to load the length of the radio header into
  2099. * the register assigned to hold that length, if one has been
  2100. * assigned. (If one hasn't been assigned, no code we've
  2101. * generated uses that prefix, so we don't need to generate any
  2102. * code to load it.)
  2103. *
  2104. * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
  2105. * or always use the AVS header rather than the Prism header.
  2106. * We load a 4-byte big-endian value at the beginning of the
  2107. * raw packet data, and see whether, when masked with 0xFFFFF000,
  2108. * it's equal to 0x80211000. If so, that indicates that it's
  2109. * an AVS header (the masked-out bits are the version number).
  2110. * Otherwise, it's a Prism header.
  2111. *
  2112. * XXX - the Prism header is also, in theory, variable-length,
  2113. * but no known software generates headers that aren't 144
  2114. * bytes long.
  2115. */
  2116. if (cstate->off_linkhdr.reg != -1) {
  2117. /*
  2118. * Load the cookie.
  2119. */
  2120. s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
  2121. s1->s.k = 0;
  2122. /*
  2123. * AND it with 0xFFFFF000.
  2124. */
  2125. s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
  2126. s2->s.k = 0xFFFFF000;
  2127. sappend(s1, s2);
  2128. /*
  2129. * Compare with 0x80211000.
  2130. */
  2131. sjeq_avs_cookie = new_stmt(cstate, JMP(BPF_JEQ));
  2132. sjeq_avs_cookie->s.k = 0x80211000;
  2133. sappend(s1, sjeq_avs_cookie);
  2134. /*
  2135. * If it's AVS:
  2136. *
  2137. * The 4 bytes at an offset of 4 from the beginning of
  2138. * the AVS header are the length of the AVS header.
  2139. * That field is big-endian.
  2140. */
  2141. s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
  2142. s2->s.k = 4;
  2143. sappend(s1, s2);
  2144. sjeq_avs_cookie->s.jt = s2;
  2145. /*
  2146. * Now jump to the code to allocate a register
  2147. * into which to save the header length and
  2148. * store the length there. (The "jump always"
  2149. * instruction needs to have the k field set;
  2150. * it's added to the PC, so, as we're jumping
  2151. * over a single instruction, it should be 1.)
  2152. */
  2153. sjcommon = new_stmt(cstate, JMP(BPF_JA));
  2154. sjcommon->s.k = 1;
  2155. sappend(s1, sjcommon);
  2156. /*
  2157. * Now for the code that handles the Prism header.
  2158. * Just load the length of the Prism header (144)
  2159. * into the A register. Have the test for an AVS
  2160. * header branch here if we don't have an AVS header.
  2161. */
  2162. s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM);
  2163. s2->s.k = 144;
  2164. sappend(s1, s2);
  2165. sjeq_avs_cookie->s.jf = s2;
  2166. /*
  2167. * Now allocate a register to hold that value and store
  2168. * it. The code for the AVS header will jump here after
  2169. * loading the length of the AVS header.
  2170. */
  2171. s2 = new_stmt(cstate, BPF_ST);
  2172. s2->s.k = cstate->off_linkhdr.reg;
  2173. sappend(s1, s2);
  2174. sjcommon->s.jf = s2;
  2175. /*
  2176. * Now move it into the X register.
  2177. */
  2178. s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  2179. sappend(s1, s2);
  2180. return (s1);
  2181. } else
  2182. return (NULL);
  2183. }
  2184. static struct slist *
  2185. gen_load_avs_llprefixlen(compiler_state_t *cstate)
  2186. {
  2187. struct slist *s1, *s2;
  2188. /*
  2189. * Generate code to load the length of the AVS header into
  2190. * the register assigned to hold that length, if one has been
  2191. * assigned. (If one hasn't been assigned, no code we've
  2192. * generated uses that prefix, so we don't need to generate any
  2193. * code to load it.)
  2194. */
  2195. if (cstate->off_linkhdr.reg != -1) {
  2196. /*
  2197. * The 4 bytes at an offset of 4 from the beginning of
  2198. * the AVS header are the length of the AVS header.
  2199. * That field is big-endian.
  2200. */
  2201. s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
  2202. s1->s.k = 4;
  2203. /*
  2204. * Now allocate a register to hold that value and store
  2205. * it.
  2206. */
  2207. s2 = new_stmt(cstate, BPF_ST);
  2208. s2->s.k = cstate->off_linkhdr.reg;
  2209. sappend(s1, s2);
  2210. /*
  2211. * Now move it into the X register.
  2212. */
  2213. s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  2214. sappend(s1, s2);
  2215. return (s1);
  2216. } else
  2217. return (NULL);
  2218. }
  2219. static struct slist *
  2220. gen_load_radiotap_llprefixlen(compiler_state_t *cstate)
  2221. {
  2222. struct slist *s1, *s2;
  2223. /*
  2224. * Generate code to load the length of the radiotap header into
  2225. * the register assigned to hold that length, if one has been
  2226. * assigned. (If one hasn't been assigned, no code we've
  2227. * generated uses that prefix, so we don't need to generate any
  2228. * code to load it.)
  2229. */
  2230. if (cstate->off_linkhdr.reg != -1) {
  2231. /*
  2232. * The 2 bytes at offsets of 2 and 3 from the beginning
  2233. * of the radiotap header are the length of the radiotap
  2234. * header; unfortunately, it's little-endian, so we have
  2235. * to load it a byte at a time and construct the value.
  2236. */
  2237. /*
  2238. * Load the high-order byte, at an offset of 3, shift it
  2239. * left a byte, and put the result in the X register.
  2240. */
  2241. s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
  2242. s1->s.k = 3;
  2243. s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
  2244. sappend(s1, s2);
  2245. s2->s.k = 8;
  2246. s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  2247. sappend(s1, s2);
  2248. /*
  2249. * Load the next byte, at an offset of 2, and OR the
  2250. * value from the X register into it.
  2251. */
  2252. s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
  2253. sappend(s1, s2);
  2254. s2->s.k = 2;
  2255. s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X);
  2256. sappend(s1, s2);
  2257. /*
  2258. * Now allocate a register to hold that value and store
  2259. * it.
  2260. */
  2261. s2 = new_stmt(cstate, BPF_ST);
  2262. s2->s.k = cstate->off_linkhdr.reg;
  2263. sappend(s1, s2);
  2264. /*
  2265. * Now move it into the X register.
  2266. */
  2267. s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  2268. sappend(s1, s2);
  2269. return (s1);
  2270. } else
  2271. return (NULL);
  2272. }
  2273. /*
  2274. * At the moment we treat PPI as normal Radiotap encoded
  2275. * packets. The difference is in the function that generates
  2276. * the code at the beginning to compute the header length.
  2277. * Since this code generator of PPI supports bare 802.11
  2278. * encapsulation only (i.e. the encapsulated DLT should be
  2279. * DLT_IEEE802_11) we generate code to check for this too;
  2280. * that's done in finish_parse().
  2281. */
  2282. static struct slist *
  2283. gen_load_ppi_llprefixlen(compiler_state_t *cstate)
  2284. {
  2285. struct slist *s1, *s2;
  2286. /*
  2287. * Generate code to load the length of the radiotap header
  2288. * into the register assigned to hold that length, if one has
  2289. * been assigned.
  2290. */
  2291. if (cstate->off_linkhdr.reg != -1) {
  2292. /*
  2293. * The 2 bytes at offsets of 2 and 3 from the beginning
  2294. * of the radiotap header are the length of the radiotap
  2295. * header; unfortunately, it's little-endian, so we have
  2296. * to load it a byte at a time and construct the value.
  2297. */
  2298. /*
  2299. * Load the high-order byte, at an offset of 3, shift it
  2300. * left a byte, and put the result in the X register.
  2301. */
  2302. s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
  2303. s1->s.k = 3;
  2304. s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);
  2305. sappend(s1, s2);
  2306. s2->s.k = 8;
  2307. s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  2308. sappend(s1, s2);
  2309. /*
  2310. * Load the next byte, at an offset of 2, and OR the
  2311. * value from the X register into it.
  2312. */
  2313. s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
  2314. sappend(s1, s2);
  2315. s2->s.k = 2;
  2316. s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X);
  2317. sappend(s1, s2);
  2318. /*
  2319. * Now allocate a register to hold that value and store
  2320. * it.
  2321. */
  2322. s2 = new_stmt(cstate, BPF_ST);
  2323. s2->s.k = cstate->off_linkhdr.reg;
  2324. sappend(s1, s2);
  2325. /*
  2326. * Now move it into the X register.
  2327. */
  2328. s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  2329. sappend(s1, s2);
  2330. return (s1);
  2331. } else
  2332. return (NULL);
  2333. }
  2334. /*
  2335. * Load a value relative to the beginning of the link-layer header after the 802.11
  2336. * header, i.e. LLC_SNAP.
  2337. * The link-layer header doesn't necessarily begin at the beginning
  2338. * of the packet data; there might be a variable-length prefix containing
  2339. * radio information.
  2340. */
  2341. static struct slist *
  2342. gen_load_802_11_header_len(compiler_state_t *cstate, struct slist *s, struct slist *snext)
  2343. {
  2344. struct slist *s2;
  2345. struct slist *sjset_data_frame_1;
  2346. struct slist *sjset_data_frame_2;
  2347. struct slist *sjset_qos;
  2348. struct slist *sjset_radiotap_flags_present;
  2349. struct slist *sjset_radiotap_ext_present;
  2350. struct slist *sjset_radiotap_tsft_present;
  2351. struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
  2352. struct slist *s_roundup;
  2353. if (cstate->off_linkpl.reg == -1) {
  2354. /*
  2355. * No register has been assigned to the offset of
  2356. * the link-layer payload, which means nobody needs
  2357. * it; don't bother computing it - just return
  2358. * what we already have.
  2359. */
  2360. return (s);
  2361. }
  2362. /*
  2363. * This code is not compatible with the optimizer, as
  2364. * we are generating jmp instructions within a normal
  2365. * slist of instructions
  2366. */
  2367. cstate->no_optimize = 1;
  2368. /*
  2369. * If "s" is non-null, it has code to arrange that the X register
  2370. * contains the length of the prefix preceding the link-layer
  2371. * header.
  2372. *
  2373. * Otherwise, the length of the prefix preceding the link-layer
  2374. * header is "off_outermostlinkhdr.constant_part".
  2375. */
  2376. if (s == NULL) {
  2377. /*
  2378. * There is no variable-length header preceding the
  2379. * link-layer header.
  2380. *
  2381. * Load the length of the fixed-length prefix preceding
  2382. * the link-layer header (if any) into the X register,
  2383. * and store it in the cstate->off_linkpl.reg register.
  2384. * That length is off_outermostlinkhdr.constant_part.
  2385. */
  2386. s = new_stmt(cstate, BPF_LDX|BPF_IMM);
  2387. s->s.k = cstate->off_outermostlinkhdr.constant_part;
  2388. }
  2389. /*
  2390. * The X register contains the offset of the beginning of the
  2391. * link-layer header; add 24, which is the minimum length
  2392. * of the MAC header for a data frame, to that, and store it
  2393. * in cstate->off_linkpl.reg, and then load the Frame Control field,
  2394. * which is at the offset in the X register, with an indexed load.
  2395. */
  2396. s2 = new_stmt(cstate, BPF_MISC|BPF_TXA);
  2397. sappend(s, s2);
  2398. s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  2399. s2->s.k = 24;
  2400. sappend(s, s2);
  2401. s2 = new_stmt(cstate, BPF_ST);
  2402. s2->s.k = cstate->off_linkpl.reg;
  2403. sappend(s, s2);
  2404. s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
  2405. s2->s.k = 0;
  2406. sappend(s, s2);
  2407. /*
  2408. * Check the Frame Control field to see if this is a data frame;
  2409. * a data frame has the 0x08 bit (b3) in that field set and the
  2410. * 0x04 bit (b2) clear.
  2411. */
  2412. sjset_data_frame_1 = new_stmt(cstate, JMP(BPF_JSET));
  2413. sjset_data_frame_1->s.k = 0x08;
  2414. sappend(s, sjset_data_frame_1);
  2415. /*
  2416. * If b3 is set, test b2, otherwise go to the first statement of
  2417. * the rest of the program.
  2418. */
  2419. sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(cstate, JMP(BPF_JSET));
  2420. sjset_data_frame_2->s.k = 0x04;
  2421. sappend(s, sjset_data_frame_2);
  2422. sjset_data_frame_1->s.jf = snext;
  2423. /*
  2424. * If b2 is not set, this is a data frame; test the QoS bit.
  2425. * Otherwise, go to the first statement of the rest of the
  2426. * program.
  2427. */
  2428. sjset_data_frame_2->s.jt = snext;
  2429. sjset_data_frame_2->s.jf = sjset_qos = new_stmt(cstate, JMP(BPF_JSET));
  2430. sjset_qos->s.k = 0x80; /* QoS bit */
  2431. sappend(s, sjset_qos);
  2432. /*
  2433. * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
  2434. * field.
  2435. * Otherwise, go to the first statement of the rest of the
  2436. * program.
  2437. */
  2438. sjset_qos->s.jt = s2 = new_stmt(cstate, BPF_LD|BPF_MEM);
  2439. s2->s.k = cstate->off_linkpl.reg;
  2440. sappend(s, s2);
  2441. s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
  2442. s2->s.k = 2;
  2443. sappend(s, s2);
  2444. s2 = new_stmt(cstate, BPF_ST);
  2445. s2->s.k = cstate->off_linkpl.reg;
  2446. sappend(s, s2);
  2447. /*
  2448. * If we have a radiotap header, look at it to see whether
  2449. * there's Atheros padding between the MAC-layer header
  2450. * and the payload.
  2451. *
  2452. * Note: all of the fields in the radiotap header are
  2453. * little-endian, so we byte-swap all of the values
  2454. * we test against, as they will be loaded as big-endian
  2455. * values.
  2456. *
  2457. * XXX - in the general case, we would have to scan through
  2458. * *all* the presence bits, if there's more than one word of
  2459. * presence bits. That would require a loop, meaning that
  2460. * we wouldn't be able to run the filter in the kernel.
  2461. *
  2462. * We assume here that the Atheros adapters that insert the
  2463. * annoying padding don't have multiple antennae and therefore
  2464. * do not generate radiotap headers with multiple presence words.
  2465. */
  2466. if (cstate->linktype == DLT_IEEE802_11_RADIO) {
  2467. /*
  2468. * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
  2469. * in the first presence flag word?
  2470. */
  2471. sjset_qos->s.jf = s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_W);
  2472. s2->s.k = 4;
  2473. sappend(s, s2);
  2474. sjset_radiotap_flags_present = new_stmt(cstate, JMP(BPF_JSET));
  2475. sjset_radiotap_flags_present->s.k = SWAPLONG(0x00000002);
  2476. sappend(s, sjset_radiotap_flags_present);
  2477. /*
  2478. * If not, skip all of this.
  2479. */
  2480. sjset_radiotap_flags_present->s.jf = snext;
  2481. /*
  2482. * Otherwise, is the "extension" bit set in that word?
  2483. */
  2484. sjset_radiotap_ext_present = new_stmt(cstate, JMP(BPF_JSET));
  2485. sjset_radiotap_ext_present->s.k = SWAPLONG(0x80000000);
  2486. sappend(s, sjset_radiotap_ext_present);
  2487. sjset_radiotap_flags_present->s.jt = sjset_radiotap_ext_present;
  2488. /*
  2489. * If so, skip all of this.
  2490. */
  2491. sjset_radiotap_ext_present->s.jt = snext;
  2492. /*
  2493. * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
  2494. */
  2495. sjset_radiotap_tsft_present = new_stmt(cstate, JMP(BPF_JSET));
  2496. sjset_radiotap_tsft_present->s.k = SWAPLONG(0x00000001);
  2497. sappend(s, sjset_radiotap_tsft_present);
  2498. sjset_radiotap_ext_present->s.jf = sjset_radiotap_tsft_present;
  2499. /*
  2500. * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
  2501. * at an offset of 16 from the beginning of the raw packet
  2502. * data (8 bytes for the radiotap header and 8 bytes for
  2503. * the TSFT field).
  2504. *
  2505. * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
  2506. * is set.
  2507. */
  2508. s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
  2509. s2->s.k = 16;
  2510. sappend(s, s2);
  2511. sjset_radiotap_tsft_present->s.jt = s2;
  2512. sjset_tsft_datapad = new_stmt(cstate, JMP(BPF_JSET));
  2513. sjset_tsft_datapad->s.k = 0x20;
  2514. sappend(s, sjset_tsft_datapad);
  2515. /*
  2516. * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
  2517. * at an offset of 8 from the beginning of the raw packet
  2518. * data (8 bytes for the radiotap header).
  2519. *
  2520. * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
  2521. * is set.
  2522. */
  2523. s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
  2524. s2->s.k = 8;
  2525. sappend(s, s2);
  2526. sjset_radiotap_tsft_present->s.jf = s2;
  2527. sjset_notsft_datapad = new_stmt(cstate, JMP(BPF_JSET));
  2528. sjset_notsft_datapad->s.k = 0x20;
  2529. sappend(s, sjset_notsft_datapad);
  2530. /*
  2531. * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
  2532. * set, round the length of the 802.11 header to
  2533. * a multiple of 4. Do that by adding 3 and then
  2534. * dividing by and multiplying by 4, which we do by
  2535. * ANDing with ~3.
  2536. */
  2537. s_roundup = new_stmt(cstate, BPF_LD|BPF_MEM);
  2538. s_roundup->s.k = cstate->off_linkpl.reg;
  2539. sappend(s, s_roundup);
  2540. s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
  2541. s2->s.k = 3;
  2542. sappend(s, s2);
  2543. s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_IMM);
  2544. s2->s.k = (bpf_u_int32)~3;
  2545. sappend(s, s2);
  2546. s2 = new_stmt(cstate, BPF_ST);
  2547. s2->s.k = cstate->off_linkpl.reg;
  2548. sappend(s, s2);
  2549. sjset_tsft_datapad->s.jt = s_roundup;
  2550. sjset_tsft_datapad->s.jf = snext;
  2551. sjset_notsft_datapad->s.jt = s_roundup;
  2552. sjset_notsft_datapad->s.jf = snext;
  2553. } else
  2554. sjset_qos->s.jf = snext;
  2555. return s;
  2556. }
  2557. static void
  2558. insert_compute_vloffsets(compiler_state_t *cstate, struct block *b)
  2559. {
  2560. struct slist *s;
  2561. /* There is an implicit dependency between the link
  2562. * payload and link header since the payload computation
  2563. * includes the variable part of the header. Therefore,
  2564. * if nobody else has allocated a register for the link
  2565. * header and we need it, do it now. */
  2566. if (cstate->off_linkpl.reg != -1 && cstate->off_linkhdr.is_variable &&
  2567. cstate->off_linkhdr.reg == -1)
  2568. cstate->off_linkhdr.reg = alloc_reg(cstate);
  2569. /*
  2570. * For link-layer types that have a variable-length header
  2571. * preceding the link-layer header, generate code to load
  2572. * the offset of the link-layer header into the register
  2573. * assigned to that offset, if any.
  2574. *
  2575. * XXX - this, and the next switch statement, won't handle
  2576. * encapsulation of 802.11 or 802.11+radio information in
  2577. * some other protocol stack. That's significantly more
  2578. * complicated.
  2579. */
  2580. switch (cstate->outermostlinktype) {
  2581. case DLT_PRISM_HEADER:
  2582. s = gen_load_prism_llprefixlen(cstate);
  2583. break;
  2584. case DLT_IEEE802_11_RADIO_AVS:
  2585. s = gen_load_avs_llprefixlen(cstate);
  2586. break;
  2587. case DLT_IEEE802_11_RADIO:
  2588. s = gen_load_radiotap_llprefixlen(cstate);
  2589. break;
  2590. case DLT_PPI:
  2591. s = gen_load_ppi_llprefixlen(cstate);
  2592. break;
  2593. default:
  2594. s = NULL;
  2595. break;
  2596. }
  2597. /*
  2598. * For link-layer types that have a variable-length link-layer
  2599. * header, generate code to load the offset of the link-layer
  2600. * payload into the register assigned to that offset, if any.
  2601. */
  2602. switch (cstate->outermostlinktype) {
  2603. case DLT_IEEE802_11:
  2604. case DLT_PRISM_HEADER:
  2605. case DLT_IEEE802_11_RADIO_AVS:
  2606. case DLT_IEEE802_11_RADIO:
  2607. case DLT_PPI:
  2608. s = gen_load_802_11_header_len(cstate, s, b->stmts);
  2609. break;
  2610. }
  2611. /*
  2612. * If there there is no initialization yet and we need variable
  2613. * length offsets for VLAN, initialize them to zero
  2614. */
  2615. if (s == NULL && cstate->is_vlan_vloffset) {
  2616. struct slist *s2;
  2617. if (cstate->off_linkpl.reg == -1)
  2618. cstate->off_linkpl.reg = alloc_reg(cstate);
  2619. if (cstate->off_linktype.reg == -1)
  2620. cstate->off_linktype.reg = alloc_reg(cstate);
  2621. s = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM);
  2622. s->s.k = 0;
  2623. s2 = new_stmt(cstate, BPF_ST);
  2624. s2->s.k = cstate->off_linkpl.reg;
  2625. sappend(s, s2);
  2626. s2 = new_stmt(cstate, BPF_ST);
  2627. s2->s.k = cstate->off_linktype.reg;
  2628. sappend(s, s2);
  2629. }
  2630. /*
  2631. * If we have any offset-loading code, append all the
  2632. * existing statements in the block to those statements,
  2633. * and make the resulting list the list of statements
  2634. * for the block.
  2635. */
  2636. if (s != NULL) {
  2637. sappend(s, b->stmts);
  2638. b->stmts = s;
  2639. }
  2640. }
  2641. static struct block *
  2642. gen_ppi_dlt_check(compiler_state_t *cstate)
  2643. {
  2644. struct slist *s_load_dlt;
  2645. struct block *b;
  2646. if (cstate->linktype == DLT_PPI)
  2647. {
  2648. /* Create the statements that check for the DLT
  2649. */
  2650. s_load_dlt = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);
  2651. s_load_dlt->s.k = 4;
  2652. b = new_block(cstate, JMP(BPF_JEQ));
  2653. b->stmts = s_load_dlt;
  2654. b->s.k = SWAPLONG(DLT_IEEE802_11);
  2655. }
  2656. else
  2657. {
  2658. b = NULL;
  2659. }
  2660. return b;
  2661. }
  2662. /*
  2663. * Take an absolute offset, and:
  2664. *
  2665. * if it has no variable part, return NULL;
  2666. *
  2667. * if it has a variable part, generate code to load the register
  2668. * containing that variable part into the X register, returning
  2669. * a pointer to that code - if no register for that offset has
  2670. * been allocated, allocate it first.
  2671. *
  2672. * (The code to set that register will be generated later, but will
  2673. * be placed earlier in the code sequence.)
  2674. */
  2675. static struct slist *
  2676. gen_abs_offset_varpart(compiler_state_t *cstate, bpf_abs_offset *off)
  2677. {
  2678. struct slist *s;
  2679. if (off->is_variable) {
  2680. if (off->reg == -1) {
  2681. /*
  2682. * We haven't yet assigned a register for the
  2683. * variable part of the offset of the link-layer
  2684. * header; allocate one.
  2685. */
  2686. off->reg = alloc_reg(cstate);
  2687. }
  2688. /*
  2689. * Load the register containing the variable part of the
  2690. * offset of the link-layer header into the X register.
  2691. */
  2692. s = new_stmt(cstate, BPF_LDX|BPF_MEM);
  2693. s->s.k = off->reg;
  2694. return s;
  2695. } else {
  2696. /*
  2697. * That offset isn't variable, there's no variable part,
  2698. * so we don't need to generate any code.
  2699. */
  2700. return NULL;
  2701. }
  2702. }
  2703. /*
  2704. * Map an Ethernet type to the equivalent PPP type.
  2705. */
  2706. static bpf_u_int32
  2707. ethertype_to_ppptype(bpf_u_int32 ll_proto)
  2708. {
  2709. switch (ll_proto) {
  2710. case ETHERTYPE_IP:
  2711. ll_proto = PPP_IP;
  2712. break;
  2713. case ETHERTYPE_IPV6:
  2714. ll_proto = PPP_IPV6;
  2715. break;
  2716. case ETHERTYPE_DN:
  2717. ll_proto = PPP_DECNET;
  2718. break;
  2719. case ETHERTYPE_ATALK:
  2720. ll_proto = PPP_APPLE;
  2721. break;
  2722. case ETHERTYPE_NS:
  2723. ll_proto = PPP_NS;
  2724. break;
  2725. case LLCSAP_ISONS:
  2726. ll_proto = PPP_OSI;
  2727. break;
  2728. case LLCSAP_8021D:
  2729. /*
  2730. * I'm assuming the "Bridging PDU"s that go
  2731. * over PPP are Spanning Tree Protocol
  2732. * Bridging PDUs.
  2733. */
  2734. ll_proto = PPP_BRPDU;
  2735. break;
  2736. case LLCSAP_IPX:
  2737. ll_proto = PPP_IPX;
  2738. break;
  2739. }
  2740. return (ll_proto);
  2741. }
  2742. /*
  2743. * Generate any tests that, for encapsulation of a link-layer packet
  2744. * inside another protocol stack, need to be done to check for those
  2745. * link-layer packets (and that haven't already been done by a check
  2746. * for that encapsulation).
  2747. */
  2748. static struct block *
  2749. gen_prevlinkhdr_check(compiler_state_t *cstate)
  2750. {
  2751. struct block *b0;
  2752. if (cstate->is_geneve)
  2753. return gen_geneve_ll_check(cstate);
  2754. switch (cstate->prevlinktype) {
  2755. case DLT_SUNATM:
  2756. /*
  2757. * This is LANE-encapsulated Ethernet; check that the LANE
  2758. * packet doesn't begin with an LE Control marker, i.e.
  2759. * that it's data, not a control message.
  2760. *
  2761. * (We've already generated a test for LANE.)
  2762. */
  2763. b0 = gen_cmp(cstate, OR_PREVLINKHDR, SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
  2764. gen_not(b0);
  2765. return b0;
  2766. default:
  2767. /*
  2768. * No such tests are necessary.
  2769. */
  2770. return NULL;
  2771. }
  2772. /*NOTREACHED*/
  2773. }
  2774. /*
  2775. * The three different values we should check for when checking for an
  2776. * IPv6 packet with DLT_NULL.
  2777. */
  2778. #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
  2779. #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
  2780. #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
  2781. /*
  2782. * Generate code to match a particular packet type by matching the
  2783. * link-layer type field or fields in the 802.2 LLC header.
  2784. *
  2785. * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
  2786. * value, if <= ETHERMTU.
  2787. */
  2788. static struct block *
  2789. gen_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
  2790. {
  2791. struct block *b0, *b1, *b2;
  2792. const char *description;
  2793. /* are we checking MPLS-encapsulated packets? */
  2794. if (cstate->label_stack_depth > 0)
  2795. return gen_mpls_linktype(cstate, ll_proto);
  2796. switch (cstate->linktype) {
  2797. case DLT_EN10MB:
  2798. case DLT_NETANALYZER:
  2799. case DLT_NETANALYZER_TRANSPARENT:
  2800. /* Geneve has an EtherType regardless of whether there is an
  2801. * L2 header. */
  2802. if (!cstate->is_geneve)
  2803. b0 = gen_prevlinkhdr_check(cstate);
  2804. else
  2805. b0 = NULL;
  2806. b1 = gen_ether_linktype(cstate, ll_proto);
  2807. if (b0 != NULL)
  2808. gen_and(b0, b1);
  2809. return b1;
  2810. /*NOTREACHED*/
  2811. case DLT_C_HDLC:
  2812. switch (ll_proto) {
  2813. case LLCSAP_ISONS:
  2814. ll_proto = (ll_proto << 8 | LLCSAP_ISONS);
  2815. /* fall through */
  2816. default:
  2817. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
  2818. /*NOTREACHED*/
  2819. }
  2820. case DLT_IEEE802_11:
  2821. case DLT_PRISM_HEADER:
  2822. case DLT_IEEE802_11_RADIO_AVS:
  2823. case DLT_IEEE802_11_RADIO:
  2824. case DLT_PPI:
  2825. /*
  2826. * Check that we have a data frame.
  2827. */
  2828. b0 = gen_check_802_11_data_frame(cstate);
  2829. /*
  2830. * Now check for the specified link-layer type.
  2831. */
  2832. b1 = gen_llc_linktype(cstate, ll_proto);
  2833. gen_and(b0, b1);
  2834. return b1;
  2835. /*NOTREACHED*/
  2836. case DLT_FDDI:
  2837. /*
  2838. * XXX - check for LLC frames.
  2839. */
  2840. return gen_llc_linktype(cstate, ll_proto);
  2841. /*NOTREACHED*/
  2842. case DLT_IEEE802:
  2843. /*
  2844. * XXX - check for LLC PDUs, as per IEEE 802.5.
  2845. */
  2846. return gen_llc_linktype(cstate, ll_proto);
  2847. /*NOTREACHED*/
  2848. case DLT_ATM_RFC1483:
  2849. case DLT_ATM_CLIP:
  2850. case DLT_IP_OVER_FC:
  2851. return gen_llc_linktype(cstate, ll_proto);
  2852. /*NOTREACHED*/
  2853. case DLT_SUNATM:
  2854. /*
  2855. * Check for an LLC-encapsulated version of this protocol;
  2856. * if we were checking for LANE, linktype would no longer
  2857. * be DLT_SUNATM.
  2858. *
  2859. * Check for LLC encapsulation and then check the protocol.
  2860. */
  2861. b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
  2862. b1 = gen_llc_linktype(cstate, ll_proto);
  2863. gen_and(b0, b1);
  2864. return b1;
  2865. /*NOTREACHED*/
  2866. case DLT_LINUX_SLL:
  2867. return gen_linux_sll_linktype(cstate, ll_proto);
  2868. /*NOTREACHED*/
  2869. case DLT_SLIP:
  2870. case DLT_SLIP_BSDOS:
  2871. case DLT_RAW:
  2872. /*
  2873. * These types don't provide any type field; packets
  2874. * are always IPv4 or IPv6.
  2875. *
  2876. * XXX - for IPv4, check for a version number of 4, and,
  2877. * for IPv6, check for a version number of 6?
  2878. */
  2879. switch (ll_proto) {
  2880. case ETHERTYPE_IP:
  2881. /* Check for a version number of 4. */
  2882. return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x40, 0xF0);
  2883. case ETHERTYPE_IPV6:
  2884. /* Check for a version number of 6. */
  2885. return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x60, 0xF0);
  2886. default:
  2887. return gen_false(cstate); /* always false */
  2888. }
  2889. /*NOTREACHED*/
  2890. case DLT_IPV4:
  2891. /*
  2892. * Raw IPv4, so no type field.
  2893. */
  2894. if (ll_proto == ETHERTYPE_IP)
  2895. return gen_true(cstate); /* always true */
  2896. /* Checking for something other than IPv4; always false */
  2897. return gen_false(cstate);
  2898. /*NOTREACHED*/
  2899. case DLT_IPV6:
  2900. /*
  2901. * Raw IPv6, so no type field.
  2902. */
  2903. if (ll_proto == ETHERTYPE_IPV6)
  2904. return gen_true(cstate); /* always true */
  2905. /* Checking for something other than IPv6; always false */
  2906. return gen_false(cstate);
  2907. /*NOTREACHED*/
  2908. case DLT_PPP:
  2909. case DLT_PPP_PPPD:
  2910. case DLT_PPP_SERIAL:
  2911. case DLT_PPP_ETHER:
  2912. /*
  2913. * We use Ethernet protocol types inside libpcap;
  2914. * map them to the corresponding PPP protocol types.
  2915. */
  2916. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H,
  2917. ethertype_to_ppptype(ll_proto));
  2918. /*NOTREACHED*/
  2919. case DLT_PPP_BSDOS:
  2920. /*
  2921. * We use Ethernet protocol types inside libpcap;
  2922. * map them to the corresponding PPP protocol types.
  2923. */
  2924. switch (ll_proto) {
  2925. case ETHERTYPE_IP:
  2926. /*
  2927. * Also check for Van Jacobson-compressed IP.
  2928. * XXX - do this for other forms of PPP?
  2929. */
  2930. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_IP);
  2931. b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJC);
  2932. gen_or(b0, b1);
  2933. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJNC);
  2934. gen_or(b1, b0);
  2935. return b0;
  2936. default:
  2937. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H,
  2938. ethertype_to_ppptype(ll_proto));
  2939. }
  2940. /*NOTREACHED*/
  2941. case DLT_NULL:
  2942. case DLT_LOOP:
  2943. case DLT_ENC:
  2944. switch (ll_proto) {
  2945. case ETHERTYPE_IP:
  2946. return (gen_loopback_linktype(cstate, AF_INET));
  2947. case ETHERTYPE_IPV6:
  2948. /*
  2949. * AF_ values may, unfortunately, be platform-
  2950. * dependent; AF_INET isn't, because everybody
  2951. * used 4.2BSD's value, but AF_INET6 is, because
  2952. * 4.2BSD didn't have a value for it (given that
  2953. * IPv6 didn't exist back in the early 1980's),
  2954. * and they all picked their own values.
  2955. *
  2956. * This means that, if we're reading from a
  2957. * savefile, we need to check for all the
  2958. * possible values.
  2959. *
  2960. * If we're doing a live capture, we only need
  2961. * to check for this platform's value; however,
  2962. * Npcap uses 24, which isn't Windows's AF_INET6
  2963. * value. (Given the multiple different values,
  2964. * programs that read pcap files shouldn't be
  2965. * checking for their platform's AF_INET6 value
  2966. * anyway, they should check for all of the
  2967. * possible values. and they might as well do
  2968. * that even for live captures.)
  2969. */
  2970. if (cstate->bpf_pcap->rfile != NULL) {
  2971. /*
  2972. * Savefile - check for all three
  2973. * possible IPv6 values.
  2974. */
  2975. b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_BSD);
  2976. b1 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_FREEBSD);
  2977. gen_or(b0, b1);
  2978. b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_DARWIN);
  2979. gen_or(b0, b1);
  2980. return (b1);
  2981. } else {
  2982. /*
  2983. * Live capture, so we only need to
  2984. * check for the value used on this
  2985. * platform.
  2986. */
  2987. #ifdef _WIN32
  2988. /*
  2989. * Npcap doesn't use Windows's AF_INET6,
  2990. * as that collides with AF_IPX on
  2991. * some BSDs (both have the value 23).
  2992. * Instead, it uses 24.
  2993. */
  2994. return (gen_loopback_linktype(cstate, 24));
  2995. #else /* _WIN32 */
  2996. #ifdef AF_INET6
  2997. return (gen_loopback_linktype(cstate, AF_INET6));
  2998. #else /* AF_INET6 */
  2999. /*
  3000. * I guess this platform doesn't support
  3001. * IPv6, so we just reject all packets.
  3002. */
  3003. return gen_false(cstate);
  3004. #endif /* AF_INET6 */
  3005. #endif /* _WIN32 */
  3006. }
  3007. default:
  3008. /*
  3009. * Not a type on which we support filtering.
  3010. * XXX - support those that have AF_ values
  3011. * #defined on this platform, at least?
  3012. */
  3013. return gen_false(cstate);
  3014. }
  3015. #ifdef HAVE_NET_PFVAR_H
  3016. case DLT_PFLOG:
  3017. /*
  3018. * af field is host byte order in contrast to the rest of
  3019. * the packet.
  3020. */
  3021. if (ll_proto == ETHERTYPE_IP)
  3022. return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af),
  3023. BPF_B, AF_INET));
  3024. else if (ll_proto == ETHERTYPE_IPV6)
  3025. return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af),
  3026. BPF_B, AF_INET6));
  3027. else
  3028. return gen_false(cstate);
  3029. /*NOTREACHED*/
  3030. #endif /* HAVE_NET_PFVAR_H */
  3031. case DLT_ARCNET:
  3032. case DLT_ARCNET_LINUX:
  3033. /*
  3034. * XXX should we check for first fragment if the protocol
  3035. * uses PHDS?
  3036. */
  3037. switch (ll_proto) {
  3038. default:
  3039. return gen_false(cstate);
  3040. case ETHERTYPE_IPV6:
  3041. return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  3042. ARCTYPE_INET6));
  3043. case ETHERTYPE_IP:
  3044. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  3045. ARCTYPE_IP);
  3046. b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  3047. ARCTYPE_IP_OLD);
  3048. gen_or(b0, b1);
  3049. return (b1);
  3050. case ETHERTYPE_ARP:
  3051. b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  3052. ARCTYPE_ARP);
  3053. b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  3054. ARCTYPE_ARP_OLD);
  3055. gen_or(b0, b1);
  3056. return (b1);
  3057. case ETHERTYPE_REVARP:
  3058. return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  3059. ARCTYPE_REVARP));
  3060. case ETHERTYPE_ATALK:
  3061. return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,
  3062. ARCTYPE_ATALK));
  3063. }
  3064. /*NOTREACHED*/
  3065. case DLT_LTALK:
  3066. switch (ll_proto) {
  3067. case ETHERTYPE_ATALK:
  3068. return gen_true(cstate);
  3069. default:
  3070. return gen_false(cstate);
  3071. }
  3072. /*NOTREACHED*/
  3073. case DLT_FRELAY:
  3074. /*
  3075. * XXX - assumes a 2-byte Frame Relay header with
  3076. * DLCI and flags. What if the address is longer?
  3077. */
  3078. switch (ll_proto) {
  3079. case ETHERTYPE_IP:
  3080. /*
  3081. * Check for the special NLPID for IP.
  3082. */
  3083. return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0xcc);
  3084. case ETHERTYPE_IPV6:
  3085. /*
  3086. * Check for the special NLPID for IPv6.
  3087. */
  3088. return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0x8e);
  3089. case LLCSAP_ISONS:
  3090. /*
  3091. * Check for several OSI protocols.
  3092. *
  3093. * Frame Relay packets typically have an OSI
  3094. * NLPID at the beginning; we check for each
  3095. * of them.
  3096. *
  3097. * What we check for is the NLPID and a frame
  3098. * control field of UI, i.e. 0x03 followed
  3099. * by the NLPID.
  3100. */
  3101. b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
  3102. b1 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
  3103. b2 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
  3104. gen_or(b1, b2);
  3105. gen_or(b0, b2);
  3106. return b2;
  3107. default:
  3108. return gen_false(cstate);
  3109. }
  3110. /*NOTREACHED*/
  3111. case DLT_MFR:
  3112. bpf_error(cstate, "Multi-link Frame Relay link-layer type filtering not implemented");
  3113. case DLT_JUNIPER_MFR:
  3114. case DLT_JUNIPER_MLFR:
  3115. case DLT_JUNIPER_MLPPP:
  3116. case DLT_JUNIPER_ATM1:
  3117. case DLT_JUNIPER_ATM2:
  3118. case DLT_JUNIPER_PPPOE:
  3119. case DLT_JUNIPER_PPPOE_ATM:
  3120. case DLT_JUNIPER_GGSN:
  3121. case DLT_JUNIPER_ES:
  3122. case DLT_JUNIPER_MONITOR:
  3123. case DLT_JUNIPER_SERVICES:
  3124. case DLT_JUNIPER_ETHER:
  3125. case DLT_JUNIPER_PPP:
  3126. case DLT_JUNIPER_FRELAY:
  3127. case DLT_JUNIPER_CHDLC:
  3128. case DLT_JUNIPER_VP:
  3129. case DLT_JUNIPER_ST:
  3130. case DLT_JUNIPER_ISM:
  3131. case DLT_JUNIPER_VS:
  3132. case DLT_JUNIPER_SRX_E2E:
  3133. case DLT_JUNIPER_FIBRECHANNEL:
  3134. case DLT_JUNIPER_ATM_CEMIC:
  3135. /* just lets verify the magic number for now -
  3136. * on ATM we may have up to 6 different encapsulations on the wire
  3137. * and need a lot of heuristics to figure out that the payload
  3138. * might be;
  3139. *
  3140. * FIXME encapsulation specific BPF_ filters
  3141. */
  3142. return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
  3143. case DLT_BACNET_MS_TP:
  3144. return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x55FF0000, 0xffff0000);
  3145. case DLT_IPNET:
  3146. return gen_ipnet_linktype(cstate, ll_proto);
  3147. case DLT_LINUX_IRDA:
  3148. bpf_error(cstate, "IrDA link-layer type filtering not implemented");
  3149. case DLT_DOCSIS:
  3150. bpf_error(cstate, "DOCSIS link-layer type filtering not implemented");
  3151. case DLT_MTP2:
  3152. case DLT_MTP2_WITH_PHDR:
  3153. bpf_error(cstate, "MTP2 link-layer type filtering not implemented");
  3154. case DLT_ERF:
  3155. bpf_error(cstate, "ERF link-layer type filtering not implemented");
  3156. case DLT_PFSYNC:
  3157. bpf_error(cstate, "PFSYNC link-layer type filtering not implemented");
  3158. case DLT_LINUX_LAPD:
  3159. bpf_error(cstate, "LAPD link-layer type filtering not implemented");
  3160. case DLT_USB_FREEBSD:
  3161. case DLT_USB_LINUX:
  3162. case DLT_USB_LINUX_MMAPPED:
  3163. case DLT_USBPCAP:
  3164. bpf_error(cstate, "USB link-layer type filtering not implemented");
  3165. case DLT_BLUETOOTH_HCI_H4:
  3166. case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
  3167. bpf_error(cstate, "Bluetooth link-layer type filtering not implemented");
  3168. case DLT_CAN20B:
  3169. case DLT_CAN_SOCKETCAN:
  3170. bpf_error(cstate, "CAN link-layer type filtering not implemented");
  3171. case DLT_IEEE802_15_4:
  3172. case DLT_IEEE802_15_4_LINUX:
  3173. case DLT_IEEE802_15_4_NONASK_PHY:
  3174. case DLT_IEEE802_15_4_NOFCS:
  3175. case DLT_IEEE802_15_4_TAP:
  3176. bpf_error(cstate, "IEEE 802.15.4 link-layer type filtering not implemented");
  3177. case DLT_IEEE802_16_MAC_CPS_RADIO:
  3178. bpf_error(cstate, "IEEE 802.16 link-layer type filtering not implemented");
  3179. case DLT_SITA:
  3180. bpf_error(cstate, "SITA link-layer type filtering not implemented");
  3181. case DLT_RAIF1:
  3182. bpf_error(cstate, "RAIF1 link-layer type filtering not implemented");
  3183. case DLT_IPMB_KONTRON:
  3184. case DLT_IPMB_LINUX:
  3185. bpf_error(cstate, "IPMB link-layer type filtering not implemented");
  3186. case DLT_AX25_KISS:
  3187. bpf_error(cstate, "AX.25 link-layer type filtering not implemented");
  3188. case DLT_NFLOG:
  3189. /* Using the fixed-size NFLOG header it is possible to tell only
  3190. * the address family of the packet, other meaningful data is
  3191. * either missing or behind TLVs.
  3192. */
  3193. bpf_error(cstate, "NFLOG link-layer type filtering not implemented");
  3194. default:
  3195. /*
  3196. * Does this link-layer header type have a field
  3197. * indicating the type of the next protocol? If
  3198. * so, off_linktype.constant_part will be the offset of that
  3199. * field in the packet; if not, it will be OFFSET_NOT_SET.
  3200. */
  3201. if (cstate->off_linktype.constant_part != OFFSET_NOT_SET) {
  3202. /*
  3203. * Yes; assume it's an Ethernet type. (If
  3204. * it's not, it needs to be handled specially
  3205. * above.)
  3206. */
  3207. return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);
  3208. /*NOTREACHED */
  3209. } else {
  3210. /*
  3211. * No; report an error.
  3212. */
  3213. description = pcap_datalink_val_to_description_or_dlt(cstate->linktype);
  3214. bpf_error(cstate, "%s link-layer type filtering not implemented",
  3215. description);
  3216. /*NOTREACHED */
  3217. }
  3218. }
  3219. }
  3220. /*
  3221. * Check for an LLC SNAP packet with a given organization code and
  3222. * protocol type; we check the entire contents of the 802.2 LLC and
  3223. * snap headers, checking for DSAP and SSAP of SNAP and a control
  3224. * field of 0x03 in the LLC header, and for the specified organization
  3225. * code and protocol type in the SNAP header.
  3226. */
  3227. static struct block *
  3228. gen_snap(compiler_state_t *cstate, bpf_u_int32 orgcode, bpf_u_int32 ptype)
  3229. {
  3230. u_char snapblock[8];
  3231. snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
  3232. snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
  3233. snapblock[2] = 0x03; /* control = UI */
  3234. snapblock[3] = (u_char)(orgcode >> 16); /* upper 8 bits of organization code */
  3235. snapblock[4] = (u_char)(orgcode >> 8); /* middle 8 bits of organization code */
  3236. snapblock[5] = (u_char)(orgcode >> 0); /* lower 8 bits of organization code */
  3237. snapblock[6] = (u_char)(ptype >> 8); /* upper 8 bits of protocol type */
  3238. snapblock[7] = (u_char)(ptype >> 0); /* lower 8 bits of protocol type */
  3239. return gen_bcmp(cstate, OR_LLC, 0, 8, snapblock);
  3240. }
  3241. /*
  3242. * Generate code to match frames with an LLC header.
  3243. */
  3244. static struct block *
  3245. gen_llc_internal(compiler_state_t *cstate)
  3246. {
  3247. struct block *b0, *b1;
  3248. switch (cstate->linktype) {
  3249. case DLT_EN10MB:
  3250. /*
  3251. * We check for an Ethernet type field less than
  3252. * 1500, which means it's an 802.3 length field.
  3253. */
  3254. b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);
  3255. gen_not(b0);
  3256. /*
  3257. * Now check for the purported DSAP and SSAP not being
  3258. * 0xFF, to rule out NetWare-over-802.3.
  3259. */
  3260. b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF);
  3261. gen_not(b1);
  3262. gen_and(b0, b1);
  3263. return b1;
  3264. case DLT_SUNATM:
  3265. /*
  3266. * We check for LLC traffic.
  3267. */
  3268. b0 = gen_atmtype_llc(cstate);
  3269. return b0;
  3270. case DLT_IEEE802: /* Token Ring */
  3271. /*
  3272. * XXX - check for LLC frames.
  3273. */
  3274. return gen_true(cstate);
  3275. case DLT_FDDI:
  3276. /*
  3277. * XXX - check for LLC frames.
  3278. */
  3279. return gen_true(cstate);
  3280. case DLT_ATM_RFC1483:
  3281. /*
  3282. * For LLC encapsulation, these are defined to have an
  3283. * 802.2 LLC header.
  3284. *
  3285. * For VC encapsulation, they don't, but there's no
  3286. * way to check for that; the protocol used on the VC
  3287. * is negotiated out of band.
  3288. */
  3289. return gen_true(cstate);
  3290. case DLT_IEEE802_11:
  3291. case DLT_PRISM_HEADER:
  3292. case DLT_IEEE802_11_RADIO:
  3293. case DLT_IEEE802_11_RADIO_AVS:
  3294. case DLT_PPI:
  3295. /*
  3296. * Check that we have a data frame.
  3297. */
  3298. b0 = gen_check_802_11_data_frame(cstate);
  3299. return b0;
  3300. default:
  3301. bpf_error(cstate, "'llc' not supported for %s",
  3302. pcap_datalink_val_to_description_or_dlt(cstate->linktype));
  3303. /*NOTREACHED*/
  3304. }
  3305. }
  3306. struct block *
  3307. gen_llc(compiler_state_t *cstate)
  3308. {
  3309. /*
  3310. * Catch errors reported by us and routines below us, and return NULL
  3311. * on an error.
  3312. */
  3313. if (setjmp(cstate->top_ctx))
  3314. return (NULL);
  3315. return gen_llc_internal(cstate);
  3316. }
  3317. struct block *
  3318. gen_llc_i(compiler_state_t *cstate)
  3319. {
  3320. struct block *b0, *b1;
  3321. struct slist *s;
  3322. /*
  3323. * Catch errors reported by us and routines below us, and return NULL
  3324. * on an error.
  3325. */
  3326. if (setjmp(cstate->top_ctx))
  3327. return (NULL);
  3328. /*
  3329. * Check whether this is an LLC frame.
  3330. */
  3331. b0 = gen_llc_internal(cstate);
  3332. /*
  3333. * Load the control byte and test the low-order bit; it must
  3334. * be clear for I frames.
  3335. */
  3336. s = gen_load_a(cstate, OR_LLC, 2, BPF_B);
  3337. b1 = new_block(cstate, JMP(BPF_JSET));
  3338. b1->s.k = 0x01;
  3339. b1->stmts = s;
  3340. gen_not(b1);
  3341. gen_and(b0, b1);
  3342. return b1;
  3343. }
  3344. struct block *
  3345. gen_llc_s(compiler_state_t *cstate)
  3346. {
  3347. struct block *b0, *b1;
  3348. /*
  3349. * Catch errors reported by us and routines below us, and return NULL
  3350. * on an error.
  3351. */
  3352. if (setjmp(cstate->top_ctx))
  3353. return (NULL);
  3354. /*
  3355. * Check whether this is an LLC frame.
  3356. */
  3357. b0 = gen_llc_internal(cstate);
  3358. /*
  3359. * Now compare the low-order 2 bit of the control byte against
  3360. * the appropriate value for S frames.
  3361. */
  3362. b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_S_FMT, 0x03);
  3363. gen_and(b0, b1);
  3364. return b1;
  3365. }
  3366. struct block *
  3367. gen_llc_u(compiler_state_t *cstate)
  3368. {
  3369. struct block *b0, *b1;
  3370. /*
  3371. * Catch errors reported by us and routines below us, and return NULL
  3372. * on an error.
  3373. */
  3374. if (setjmp(cstate->top_ctx))
  3375. return (NULL);
  3376. /*
  3377. * Check whether this is an LLC frame.
  3378. */
  3379. b0 = gen_llc_internal(cstate);
  3380. /*
  3381. * Now compare the low-order 2 bit of the control byte against
  3382. * the appropriate value for U frames.
  3383. */
  3384. b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_U_FMT, 0x03);
  3385. gen_and(b0, b1);
  3386. return b1;
  3387. }
  3388. struct block *
  3389. gen_llc_s_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)
  3390. {
  3391. struct block *b0, *b1;
  3392. /*
  3393. * Catch errors reported by us and routines below us, and return NULL
  3394. * on an error.
  3395. */
  3396. if (setjmp(cstate->top_ctx))
  3397. return (NULL);
  3398. /*
  3399. * Check whether this is an LLC frame.
  3400. */
  3401. b0 = gen_llc_internal(cstate);
  3402. /*
  3403. * Now check for an S frame with the appropriate type.
  3404. */
  3405. b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_S_CMD_MASK);
  3406. gen_and(b0, b1);
  3407. return b1;
  3408. }
  3409. struct block *
  3410. gen_llc_u_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)
  3411. {
  3412. struct block *b0, *b1;
  3413. /*
  3414. * Catch errors reported by us and routines below us, and return NULL
  3415. * on an error.
  3416. */
  3417. if (setjmp(cstate->top_ctx))
  3418. return (NULL);
  3419. /*
  3420. * Check whether this is an LLC frame.
  3421. */
  3422. b0 = gen_llc_internal(cstate);
  3423. /*
  3424. * Now check for a U frame with the appropriate type.
  3425. */
  3426. b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_U_CMD_MASK);
  3427. gen_and(b0, b1);
  3428. return b1;
  3429. }
  3430. /*
  3431. * Generate code to match a particular packet type, for link-layer types
  3432. * using 802.2 LLC headers.
  3433. *
  3434. * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
  3435. * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
  3436. *
  3437. * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
  3438. * value, if <= ETHERMTU. We use that to determine whether to
  3439. * match the DSAP or both DSAP and LSAP or to check the OUI and
  3440. * protocol ID in a SNAP header.
  3441. */
  3442. static struct block *
  3443. gen_llc_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
  3444. {
  3445. /*
  3446. * XXX - handle token-ring variable-length header.
  3447. */
  3448. switch (ll_proto) {
  3449. case LLCSAP_IP:
  3450. case LLCSAP_ISONS:
  3451. case LLCSAP_NETBEUI:
  3452. /*
  3453. * XXX - should we check both the DSAP and the
  3454. * SSAP, like this, or should we check just the
  3455. * DSAP, as we do for other SAP values?
  3456. */
  3457. return gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_u_int32)
  3458. ((ll_proto << 8) | ll_proto));
  3459. case LLCSAP_IPX:
  3460. /*
  3461. * XXX - are there ever SNAP frames for IPX on
  3462. * non-Ethernet 802.x networks?
  3463. */
  3464. return gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);
  3465. case ETHERTYPE_ATALK:
  3466. /*
  3467. * 802.2-encapsulated ETHERTYPE_ATALK packets are
  3468. * SNAP packets with an organization code of
  3469. * 0x080007 (Apple, for Appletalk) and a protocol
  3470. * type of ETHERTYPE_ATALK (Appletalk).
  3471. *
  3472. * XXX - check for an organization code of
  3473. * encapsulated Ethernet as well?
  3474. */
  3475. return gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);
  3476. default:
  3477. /*
  3478. * XXX - we don't have to check for IPX 802.3
  3479. * here, but should we check for the IPX Ethertype?
  3480. */
  3481. if (ll_proto <= ETHERMTU) {
  3482. /*
  3483. * This is an LLC SAP value, so check
  3484. * the DSAP.
  3485. */
  3486. return gen_cmp(cstate, OR_LLC, 0, BPF_B, ll_proto);
  3487. } else {
  3488. /*
  3489. * This is an Ethernet type; we assume that it's
  3490. * unlikely that it'll appear in the right place
  3491. * at random, and therefore check only the
  3492. * location that would hold the Ethernet type
  3493. * in a SNAP frame with an organization code of
  3494. * 0x000000 (encapsulated Ethernet).
  3495. *
  3496. * XXX - if we were to check for the SNAP DSAP and
  3497. * LSAP, as per XXX, and were also to check for an
  3498. * organization code of 0x000000 (encapsulated
  3499. * Ethernet), we'd do
  3500. *
  3501. * return gen_snap(cstate, 0x000000, ll_proto);
  3502. *
  3503. * here; for now, we don't, as per the above.
  3504. * I don't know whether it's worth the extra CPU
  3505. * time to do the right check or not.
  3506. */
  3507. return gen_cmp(cstate, OR_LLC, 6, BPF_H, ll_proto);
  3508. }
  3509. }
  3510. }
  3511. static struct block *
  3512. gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
  3513. int dir, bpf_u_int32 ll_proto, u_int src_off, u_int dst_off)
  3514. {
  3515. struct block *b0, *b1;
  3516. u_int offset;
  3517. switch (dir) {
  3518. case Q_SRC:
  3519. offset = src_off;
  3520. break;
  3521. case Q_DST:
  3522. offset = dst_off;
  3523. break;
  3524. case Q_AND:
  3525. b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
  3526. b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
  3527. gen_and(b0, b1);
  3528. return b1;
  3529. case Q_DEFAULT:
  3530. case Q_OR:
  3531. b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
  3532. b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
  3533. gen_or(b0, b1);
  3534. return b1;
  3535. case Q_ADDR1:
  3536. bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3537. /*NOTREACHED*/
  3538. case Q_ADDR2:
  3539. bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3540. /*NOTREACHED*/
  3541. case Q_ADDR3:
  3542. bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3543. /*NOTREACHED*/
  3544. case Q_ADDR4:
  3545. bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3546. /*NOTREACHED*/
  3547. case Q_RA:
  3548. bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
  3549. /*NOTREACHED*/
  3550. case Q_TA:
  3551. bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
  3552. /*NOTREACHED*/
  3553. default:
  3554. abort();
  3555. /*NOTREACHED*/
  3556. }
  3557. b0 = gen_linktype(cstate, ll_proto);
  3558. b1 = gen_mcmp(cstate, OR_LINKPL, offset, BPF_W, addr, mask);
  3559. gen_and(b0, b1);
  3560. return b1;
  3561. }
  3562. #ifdef INET6
  3563. static struct block *
  3564. gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr,
  3565. struct in6_addr *mask, int dir, bpf_u_int32 ll_proto, u_int src_off,
  3566. u_int dst_off)
  3567. {
  3568. struct block *b0, *b1;
  3569. u_int offset;
  3570. uint32_t *a, *m;
  3571. switch (dir) {
  3572. case Q_SRC:
  3573. offset = src_off;
  3574. break;
  3575. case Q_DST:
  3576. offset = dst_off;
  3577. break;
  3578. case Q_AND:
  3579. b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
  3580. b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
  3581. gen_and(b0, b1);
  3582. return b1;
  3583. case Q_DEFAULT:
  3584. case Q_OR:
  3585. b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);
  3586. b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);
  3587. gen_or(b0, b1);
  3588. return b1;
  3589. case Q_ADDR1:
  3590. bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3591. /*NOTREACHED*/
  3592. case Q_ADDR2:
  3593. bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3594. /*NOTREACHED*/
  3595. case Q_ADDR3:
  3596. bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3597. /*NOTREACHED*/
  3598. case Q_ADDR4:
  3599. bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  3600. /*NOTREACHED*/
  3601. case Q_RA:
  3602. bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
  3603. /*NOTREACHED*/
  3604. case Q_TA:
  3605. bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
  3606. /*NOTREACHED*/
  3607. default:
  3608. abort();
  3609. /*NOTREACHED*/
  3610. }
  3611. /* this order is important */
  3612. a = (uint32_t *)addr;
  3613. m = (uint32_t *)mask;
  3614. b1 = gen_mcmp(cstate, OR_LINKPL, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
  3615. b0 = gen_mcmp(cstate, OR_LINKPL, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
  3616. gen_and(b0, b1);
  3617. b0 = gen_mcmp(cstate, OR_LINKPL, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
  3618. gen_and(b0, b1);
  3619. b0 = gen_mcmp(cstate, OR_LINKPL, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
  3620. gen_and(b0, b1);
  3621. b0 = gen_linktype(cstate, ll_proto);
  3622. gen_and(b0, b1);
  3623. return b1;
  3624. }
  3625. #endif
  3626. static struct block *
  3627. gen_ehostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
  3628. {
  3629. register struct block *b0, *b1;
  3630. switch (dir) {
  3631. case Q_SRC:
  3632. return gen_bcmp(cstate, OR_LINKHDR, 6, 6, eaddr);
  3633. case Q_DST:
  3634. return gen_bcmp(cstate, OR_LINKHDR, 0, 6, eaddr);
  3635. case Q_AND:
  3636. b0 = gen_ehostop(cstate, eaddr, Q_SRC);
  3637. b1 = gen_ehostop(cstate, eaddr, Q_DST);
  3638. gen_and(b0, b1);
  3639. return b1;
  3640. case Q_DEFAULT:
  3641. case Q_OR:
  3642. b0 = gen_ehostop(cstate, eaddr, Q_SRC);
  3643. b1 = gen_ehostop(cstate, eaddr, Q_DST);
  3644. gen_or(b0, b1);
  3645. return b1;
  3646. case Q_ADDR1:
  3647. bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
  3648. /*NOTREACHED*/
  3649. case Q_ADDR2:
  3650. bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
  3651. /*NOTREACHED*/
  3652. case Q_ADDR3:
  3653. bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
  3654. /*NOTREACHED*/
  3655. case Q_ADDR4:
  3656. bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
  3657. /*NOTREACHED*/
  3658. case Q_RA:
  3659. bpf_error(cstate, "'ra' is only supported on 802.11 with 802.11 headers");
  3660. /*NOTREACHED*/
  3661. case Q_TA:
  3662. bpf_error(cstate, "'ta' is only supported on 802.11 with 802.11 headers");
  3663. /*NOTREACHED*/
  3664. }
  3665. abort();
  3666. /*NOTREACHED*/
  3667. }
  3668. /*
  3669. * Like gen_ehostop, but for DLT_FDDI
  3670. */
  3671. static struct block *
  3672. gen_fhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
  3673. {
  3674. struct block *b0, *b1;
  3675. switch (dir) {
  3676. case Q_SRC:
  3677. return gen_bcmp(cstate, OR_LINKHDR, 6 + 1 + cstate->pcap_fddipad, 6, eaddr);
  3678. case Q_DST:
  3679. return gen_bcmp(cstate, OR_LINKHDR, 0 + 1 + cstate->pcap_fddipad, 6, eaddr);
  3680. case Q_AND:
  3681. b0 = gen_fhostop(cstate, eaddr, Q_SRC);
  3682. b1 = gen_fhostop(cstate, eaddr, Q_DST);
  3683. gen_and(b0, b1);
  3684. return b1;
  3685. case Q_DEFAULT:
  3686. case Q_OR:
  3687. b0 = gen_fhostop(cstate, eaddr, Q_SRC);
  3688. b1 = gen_fhostop(cstate, eaddr, Q_DST);
  3689. gen_or(b0, b1);
  3690. return b1;
  3691. case Q_ADDR1:
  3692. bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
  3693. /*NOTREACHED*/
  3694. case Q_ADDR2:
  3695. bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
  3696. /*NOTREACHED*/
  3697. case Q_ADDR3:
  3698. bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
  3699. /*NOTREACHED*/
  3700. case Q_ADDR4:
  3701. bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
  3702. /*NOTREACHED*/
  3703. case Q_RA:
  3704. bpf_error(cstate, "'ra' is only supported on 802.11");
  3705. /*NOTREACHED*/
  3706. case Q_TA:
  3707. bpf_error(cstate, "'ta' is only supported on 802.11");
  3708. /*NOTREACHED*/
  3709. }
  3710. abort();
  3711. /*NOTREACHED*/
  3712. }
  3713. /*
  3714. * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
  3715. */
  3716. static struct block *
  3717. gen_thostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
  3718. {
  3719. register struct block *b0, *b1;
  3720. switch (dir) {
  3721. case Q_SRC:
  3722. return gen_bcmp(cstate, OR_LINKHDR, 8, 6, eaddr);
  3723. case Q_DST:
  3724. return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);
  3725. case Q_AND:
  3726. b0 = gen_thostop(cstate, eaddr, Q_SRC);
  3727. b1 = gen_thostop(cstate, eaddr, Q_DST);
  3728. gen_and(b0, b1);
  3729. return b1;
  3730. case Q_DEFAULT:
  3731. case Q_OR:
  3732. b0 = gen_thostop(cstate, eaddr, Q_SRC);
  3733. b1 = gen_thostop(cstate, eaddr, Q_DST);
  3734. gen_or(b0, b1);
  3735. return b1;
  3736. case Q_ADDR1:
  3737. bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
  3738. /*NOTREACHED*/
  3739. case Q_ADDR2:
  3740. bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
  3741. /*NOTREACHED*/
  3742. case Q_ADDR3:
  3743. bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
  3744. /*NOTREACHED*/
  3745. case Q_ADDR4:
  3746. bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
  3747. /*NOTREACHED*/
  3748. case Q_RA:
  3749. bpf_error(cstate, "'ra' is only supported on 802.11");
  3750. /*NOTREACHED*/
  3751. case Q_TA:
  3752. bpf_error(cstate, "'ta' is only supported on 802.11");
  3753. /*NOTREACHED*/
  3754. }
  3755. abort();
  3756. /*NOTREACHED*/
  3757. }
  3758. /*
  3759. * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
  3760. * various 802.11 + radio headers.
  3761. */
  3762. static struct block *
  3763. gen_wlanhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
  3764. {
  3765. register struct block *b0, *b1, *b2;
  3766. register struct slist *s;
  3767. #ifdef ENABLE_WLAN_FILTERING_PATCH
  3768. /*
  3769. * TODO GV 20070613
  3770. * We need to disable the optimizer because the optimizer is buggy
  3771. * and wipes out some LD instructions generated by the below
  3772. * code to validate the Frame Control bits
  3773. */
  3774. cstate->no_optimize = 1;
  3775. #endif /* ENABLE_WLAN_FILTERING_PATCH */
  3776. switch (dir) {
  3777. case Q_SRC:
  3778. /*
  3779. * Oh, yuk.
  3780. *
  3781. * For control frames, there is no SA.
  3782. *
  3783. * For management frames, SA is at an
  3784. * offset of 10 from the beginning of
  3785. * the packet.
  3786. *
  3787. * For data frames, SA is at an offset
  3788. * of 10 from the beginning of the packet
  3789. * if From DS is clear, at an offset of
  3790. * 16 from the beginning of the packet
  3791. * if From DS is set and To DS is clear,
  3792. * and an offset of 24 from the beginning
  3793. * of the packet if From DS is set and To DS
  3794. * is set.
  3795. */
  3796. /*
  3797. * Generate the tests to be done for data frames
  3798. * with From DS set.
  3799. *
  3800. * First, check for To DS set, i.e. check "link[1] & 0x01".
  3801. */
  3802. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  3803. b1 = new_block(cstate, JMP(BPF_JSET));
  3804. b1->s.k = 0x01; /* To DS */
  3805. b1->stmts = s;
  3806. /*
  3807. * If To DS is set, the SA is at 24.
  3808. */
  3809. b0 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
  3810. gen_and(b1, b0);
  3811. /*
  3812. * Now, check for To DS not set, i.e. check
  3813. * "!(link[1] & 0x01)".
  3814. */
  3815. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  3816. b2 = new_block(cstate, JMP(BPF_JSET));
  3817. b2->s.k = 0x01; /* To DS */
  3818. b2->stmts = s;
  3819. gen_not(b2);
  3820. /*
  3821. * If To DS is not set, the SA is at 16.
  3822. */
  3823. b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
  3824. gen_and(b2, b1);
  3825. /*
  3826. * Now OR together the last two checks. That gives
  3827. * the complete set of checks for data frames with
  3828. * From DS set.
  3829. */
  3830. gen_or(b1, b0);
  3831. /*
  3832. * Now check for From DS being set, and AND that with
  3833. * the ORed-together checks.
  3834. */
  3835. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  3836. b1 = new_block(cstate, JMP(BPF_JSET));
  3837. b1->s.k = 0x02; /* From DS */
  3838. b1->stmts = s;
  3839. gen_and(b1, b0);
  3840. /*
  3841. * Now check for data frames with From DS not set.
  3842. */
  3843. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  3844. b2 = new_block(cstate, JMP(BPF_JSET));
  3845. b2->s.k = 0x02; /* From DS */
  3846. b2->stmts = s;
  3847. gen_not(b2);
  3848. /*
  3849. * If From DS isn't set, the SA is at 10.
  3850. */
  3851. b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
  3852. gen_and(b2, b1);
  3853. /*
  3854. * Now OR together the checks for data frames with
  3855. * From DS not set and for data frames with From DS
  3856. * set; that gives the checks done for data frames.
  3857. */
  3858. gen_or(b1, b0);
  3859. /*
  3860. * Now check for a data frame.
  3861. * I.e, check "link[0] & 0x08".
  3862. */
  3863. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  3864. b1 = new_block(cstate, JMP(BPF_JSET));
  3865. b1->s.k = 0x08;
  3866. b1->stmts = s;
  3867. /*
  3868. * AND that with the checks done for data frames.
  3869. */
  3870. gen_and(b1, b0);
  3871. /*
  3872. * If the high-order bit of the type value is 0, this
  3873. * is a management frame.
  3874. * I.e, check "!(link[0] & 0x08)".
  3875. */
  3876. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  3877. b2 = new_block(cstate, JMP(BPF_JSET));
  3878. b2->s.k = 0x08;
  3879. b2->stmts = s;
  3880. gen_not(b2);
  3881. /*
  3882. * For management frames, the SA is at 10.
  3883. */
  3884. b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
  3885. gen_and(b2, b1);
  3886. /*
  3887. * OR that with the checks done for data frames.
  3888. * That gives the checks done for management and
  3889. * data frames.
  3890. */
  3891. gen_or(b1, b0);
  3892. /*
  3893. * If the low-order bit of the type value is 1,
  3894. * this is either a control frame or a frame
  3895. * with a reserved type, and thus not a
  3896. * frame with an SA.
  3897. *
  3898. * I.e., check "!(link[0] & 0x04)".
  3899. */
  3900. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  3901. b1 = new_block(cstate, JMP(BPF_JSET));
  3902. b1->s.k = 0x04;
  3903. b1->stmts = s;
  3904. gen_not(b1);
  3905. /*
  3906. * AND that with the checks for data and management
  3907. * frames.
  3908. */
  3909. gen_and(b1, b0);
  3910. return b0;
  3911. case Q_DST:
  3912. /*
  3913. * Oh, yuk.
  3914. *
  3915. * For control frames, there is no DA.
  3916. *
  3917. * For management frames, DA is at an
  3918. * offset of 4 from the beginning of
  3919. * the packet.
  3920. *
  3921. * For data frames, DA is at an offset
  3922. * of 4 from the beginning of the packet
  3923. * if To DS is clear and at an offset of
  3924. * 16 from the beginning of the packet
  3925. * if To DS is set.
  3926. */
  3927. /*
  3928. * Generate the tests to be done for data frames.
  3929. *
  3930. * First, check for To DS set, i.e. "link[1] & 0x01".
  3931. */
  3932. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  3933. b1 = new_block(cstate, JMP(BPF_JSET));
  3934. b1->s.k = 0x01; /* To DS */
  3935. b1->stmts = s;
  3936. /*
  3937. * If To DS is set, the DA is at 16.
  3938. */
  3939. b0 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
  3940. gen_and(b1, b0);
  3941. /*
  3942. * Now, check for To DS not set, i.e. check
  3943. * "!(link[1] & 0x01)".
  3944. */
  3945. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  3946. b2 = new_block(cstate, JMP(BPF_JSET));
  3947. b2->s.k = 0x01; /* To DS */
  3948. b2->stmts = s;
  3949. gen_not(b2);
  3950. /*
  3951. * If To DS is not set, the DA is at 4.
  3952. */
  3953. b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
  3954. gen_and(b2, b1);
  3955. /*
  3956. * Now OR together the last two checks. That gives
  3957. * the complete set of checks for data frames.
  3958. */
  3959. gen_or(b1, b0);
  3960. /*
  3961. * Now check for a data frame.
  3962. * I.e, check "link[0] & 0x08".
  3963. */
  3964. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  3965. b1 = new_block(cstate, JMP(BPF_JSET));
  3966. b1->s.k = 0x08;
  3967. b1->stmts = s;
  3968. /*
  3969. * AND that with the checks done for data frames.
  3970. */
  3971. gen_and(b1, b0);
  3972. /*
  3973. * If the high-order bit of the type value is 0, this
  3974. * is a management frame.
  3975. * I.e, check "!(link[0] & 0x08)".
  3976. */
  3977. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  3978. b2 = new_block(cstate, JMP(BPF_JSET));
  3979. b2->s.k = 0x08;
  3980. b2->stmts = s;
  3981. gen_not(b2);
  3982. /*
  3983. * For management frames, the DA is at 4.
  3984. */
  3985. b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
  3986. gen_and(b2, b1);
  3987. /*
  3988. * OR that with the checks done for data frames.
  3989. * That gives the checks done for management and
  3990. * data frames.
  3991. */
  3992. gen_or(b1, b0);
  3993. /*
  3994. * If the low-order bit of the type value is 1,
  3995. * this is either a control frame or a frame
  3996. * with a reserved type, and thus not a
  3997. * frame with an SA.
  3998. *
  3999. * I.e., check "!(link[0] & 0x04)".
  4000. */
  4001. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  4002. b1 = new_block(cstate, JMP(BPF_JSET));
  4003. b1->s.k = 0x04;
  4004. b1->stmts = s;
  4005. gen_not(b1);
  4006. /*
  4007. * AND that with the checks for data and management
  4008. * frames.
  4009. */
  4010. gen_and(b1, b0);
  4011. return b0;
  4012. case Q_AND:
  4013. b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
  4014. b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
  4015. gen_and(b0, b1);
  4016. return b1;
  4017. case Q_DEFAULT:
  4018. case Q_OR:
  4019. b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);
  4020. b1 = gen_wlanhostop(cstate, eaddr, Q_DST);
  4021. gen_or(b0, b1);
  4022. return b1;
  4023. /*
  4024. * XXX - add BSSID keyword?
  4025. */
  4026. case Q_ADDR1:
  4027. return (gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr));
  4028. case Q_ADDR2:
  4029. /*
  4030. * Not present in CTS or ACK control frames.
  4031. */
  4032. b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
  4033. IEEE80211_FC0_TYPE_MASK);
  4034. gen_not(b0);
  4035. b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
  4036. IEEE80211_FC0_SUBTYPE_MASK);
  4037. gen_not(b1);
  4038. b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
  4039. IEEE80211_FC0_SUBTYPE_MASK);
  4040. gen_not(b2);
  4041. gen_and(b1, b2);
  4042. gen_or(b0, b2);
  4043. b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
  4044. gen_and(b2, b1);
  4045. return b1;
  4046. case Q_ADDR3:
  4047. /*
  4048. * Not present in control frames.
  4049. */
  4050. b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
  4051. IEEE80211_FC0_TYPE_MASK);
  4052. gen_not(b0);
  4053. b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);
  4054. gen_and(b0, b1);
  4055. return b1;
  4056. case Q_ADDR4:
  4057. /*
  4058. * Present only if the direction mask has both "From DS"
  4059. * and "To DS" set. Neither control frames nor management
  4060. * frames should have both of those set, so we don't
  4061. * check the frame type.
  4062. */
  4063. b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B,
  4064. IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
  4065. b1 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);
  4066. gen_and(b0, b1);
  4067. return b1;
  4068. case Q_RA:
  4069. /*
  4070. * Not present in management frames; addr1 in other
  4071. * frames.
  4072. */
  4073. /*
  4074. * If the high-order bit of the type value is 0, this
  4075. * is a management frame.
  4076. * I.e, check "(link[0] & 0x08)".
  4077. */
  4078. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  4079. b1 = new_block(cstate, JMP(BPF_JSET));
  4080. b1->s.k = 0x08;
  4081. b1->stmts = s;
  4082. /*
  4083. * Check addr1.
  4084. */
  4085. b0 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);
  4086. /*
  4087. * AND that with the check of addr1.
  4088. */
  4089. gen_and(b1, b0);
  4090. return (b0);
  4091. case Q_TA:
  4092. /*
  4093. * Not present in management frames; addr2, if present,
  4094. * in other frames.
  4095. */
  4096. /*
  4097. * Not present in CTS or ACK control frames.
  4098. */
  4099. b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
  4100. IEEE80211_FC0_TYPE_MASK);
  4101. gen_not(b0);
  4102. b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
  4103. IEEE80211_FC0_SUBTYPE_MASK);
  4104. gen_not(b1);
  4105. b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
  4106. IEEE80211_FC0_SUBTYPE_MASK);
  4107. gen_not(b2);
  4108. gen_and(b1, b2);
  4109. gen_or(b0, b2);
  4110. /*
  4111. * If the high-order bit of the type value is 0, this
  4112. * is a management frame.
  4113. * I.e, check "(link[0] & 0x08)".
  4114. */
  4115. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  4116. b1 = new_block(cstate, JMP(BPF_JSET));
  4117. b1->s.k = 0x08;
  4118. b1->stmts = s;
  4119. /*
  4120. * AND that with the check for frames other than
  4121. * CTS and ACK frames.
  4122. */
  4123. gen_and(b1, b2);
  4124. /*
  4125. * Check addr2.
  4126. */
  4127. b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
  4128. gen_and(b2, b1);
  4129. return b1;
  4130. }
  4131. abort();
  4132. /*NOTREACHED*/
  4133. }
  4134. /*
  4135. * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
  4136. * (We assume that the addresses are IEEE 48-bit MAC addresses,
  4137. * as the RFC states.)
  4138. */
  4139. static struct block *
  4140. gen_ipfchostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
  4141. {
  4142. register struct block *b0, *b1;
  4143. switch (dir) {
  4144. case Q_SRC:
  4145. return gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);
  4146. case Q_DST:
  4147. return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);
  4148. case Q_AND:
  4149. b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);
  4150. b1 = gen_ipfchostop(cstate, eaddr, Q_DST);
  4151. gen_and(b0, b1);
  4152. return b1;
  4153. case Q_DEFAULT:
  4154. case Q_OR:
  4155. b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);
  4156. b1 = gen_ipfchostop(cstate, eaddr, Q_DST);
  4157. gen_or(b0, b1);
  4158. return b1;
  4159. case Q_ADDR1:
  4160. bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
  4161. /*NOTREACHED*/
  4162. case Q_ADDR2:
  4163. bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
  4164. /*NOTREACHED*/
  4165. case Q_ADDR3:
  4166. bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
  4167. /*NOTREACHED*/
  4168. case Q_ADDR4:
  4169. bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
  4170. /*NOTREACHED*/
  4171. case Q_RA:
  4172. bpf_error(cstate, "'ra' is only supported on 802.11");
  4173. /*NOTREACHED*/
  4174. case Q_TA:
  4175. bpf_error(cstate, "'ta' is only supported on 802.11");
  4176. /*NOTREACHED*/
  4177. }
  4178. abort();
  4179. /*NOTREACHED*/
  4180. }
  4181. /*
  4182. * This is quite tricky because there may be pad bytes in front of the
  4183. * DECNET header, and then there are two possible data packet formats that
  4184. * carry both src and dst addresses, plus 5 packet types in a format that
  4185. * carries only the src node, plus 2 types that use a different format and
  4186. * also carry just the src node.
  4187. *
  4188. * Yuck.
  4189. *
  4190. * Instead of doing those all right, we just look for data packets with
  4191. * 0 or 1 bytes of padding. If you want to look at other packets, that
  4192. * will require a lot more hacking.
  4193. *
  4194. * To add support for filtering on DECNET "areas" (network numbers)
  4195. * one would want to add a "mask" argument to this routine. That would
  4196. * make the filter even more inefficient, although one could be clever
  4197. * and not generate masking instructions if the mask is 0xFFFF.
  4198. */
  4199. static struct block *
  4200. gen_dnhostop(compiler_state_t *cstate, bpf_u_int32 addr, int dir)
  4201. {
  4202. struct block *b0, *b1, *b2, *tmp;
  4203. u_int offset_lh; /* offset if long header is received */
  4204. u_int offset_sh; /* offset if short header is received */
  4205. switch (dir) {
  4206. case Q_DST:
  4207. offset_sh = 1; /* follows flags */
  4208. offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
  4209. break;
  4210. case Q_SRC:
  4211. offset_sh = 3; /* follows flags, dstnode */
  4212. offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
  4213. break;
  4214. case Q_AND:
  4215. /* Inefficient because we do our Calvinball dance twice */
  4216. b0 = gen_dnhostop(cstate, addr, Q_SRC);
  4217. b1 = gen_dnhostop(cstate, addr, Q_DST);
  4218. gen_and(b0, b1);
  4219. return b1;
  4220. case Q_DEFAULT:
  4221. case Q_OR:
  4222. /* Inefficient because we do our Calvinball dance twice */
  4223. b0 = gen_dnhostop(cstate, addr, Q_SRC);
  4224. b1 = gen_dnhostop(cstate, addr, Q_DST);
  4225. gen_or(b0, b1);
  4226. return b1;
  4227. case Q_ADDR1:
  4228. bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  4229. /*NOTREACHED*/
  4230. case Q_ADDR2:
  4231. bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  4232. /*NOTREACHED*/
  4233. case Q_ADDR3:
  4234. bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  4235. /*NOTREACHED*/
  4236. case Q_ADDR4:
  4237. bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
  4238. /*NOTREACHED*/
  4239. case Q_RA:
  4240. bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
  4241. /*NOTREACHED*/
  4242. case Q_TA:
  4243. bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
  4244. /*NOTREACHED*/
  4245. default:
  4246. abort();
  4247. /*NOTREACHED*/
  4248. }
  4249. b0 = gen_linktype(cstate, ETHERTYPE_DN);
  4250. /* Check for pad = 1, long header case */
  4251. tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,
  4252. (bpf_u_int32)ntohs(0x0681), (bpf_u_int32)ntohs(0x07FF));
  4253. b1 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_lh,
  4254. BPF_H, (bpf_u_int32)ntohs((u_short)addr));
  4255. gen_and(tmp, b1);
  4256. /* Check for pad = 0, long header case */
  4257. tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_u_int32)0x06,
  4258. (bpf_u_int32)0x7);
  4259. b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_lh, BPF_H,
  4260. (bpf_u_int32)ntohs((u_short)addr));
  4261. gen_and(tmp, b2);
  4262. gen_or(b2, b1);
  4263. /* Check for pad = 1, short header case */
  4264. tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,
  4265. (bpf_u_int32)ntohs(0x0281), (bpf_u_int32)ntohs(0x07FF));
  4266. b2 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_sh, BPF_H,
  4267. (bpf_u_int32)ntohs((u_short)addr));
  4268. gen_and(tmp, b2);
  4269. gen_or(b2, b1);
  4270. /* Check for pad = 0, short header case */
  4271. tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_u_int32)0x02,
  4272. (bpf_u_int32)0x7);
  4273. b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_sh, BPF_H,
  4274. (bpf_u_int32)ntohs((u_short)addr));
  4275. gen_and(tmp, b2);
  4276. gen_or(b2, b1);
  4277. /* Combine with test for cstate->linktype */
  4278. gen_and(b0, b1);
  4279. return b1;
  4280. }
  4281. /*
  4282. * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
  4283. * test the bottom-of-stack bit, and then check the version number
  4284. * field in the IP header.
  4285. */
  4286. static struct block *
  4287. gen_mpls_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)
  4288. {
  4289. struct block *b0, *b1;
  4290. switch (ll_proto) {
  4291. case ETHERTYPE_IP:
  4292. /* match the bottom-of-stack bit */
  4293. b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);
  4294. /* match the IPv4 version number */
  4295. b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x40, 0xf0);
  4296. gen_and(b0, b1);
  4297. return b1;
  4298. case ETHERTYPE_IPV6:
  4299. /* match the bottom-of-stack bit */
  4300. b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);
  4301. /* match the IPv4 version number */
  4302. b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x60, 0xf0);
  4303. gen_and(b0, b1);
  4304. return b1;
  4305. default:
  4306. /* FIXME add other L3 proto IDs */
  4307. bpf_error(cstate, "unsupported protocol over mpls");
  4308. /*NOTREACHED*/
  4309. }
  4310. }
  4311. static struct block *
  4312. gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,
  4313. int proto, int dir, int type)
  4314. {
  4315. struct block *b0, *b1;
  4316. const char *typestr;
  4317. if (type == Q_NET)
  4318. typestr = "net";
  4319. else
  4320. typestr = "host";
  4321. switch (proto) {
  4322. case Q_DEFAULT:
  4323. b0 = gen_host(cstate, addr, mask, Q_IP, dir, type);
  4324. /*
  4325. * Only check for non-IPv4 addresses if we're not
  4326. * checking MPLS-encapsulated packets.
  4327. */
  4328. if (cstate->label_stack_depth == 0) {
  4329. b1 = gen_host(cstate, addr, mask, Q_ARP, dir, type);
  4330. gen_or(b0, b1);
  4331. b0 = gen_host(cstate, addr, mask, Q_RARP, dir, type);
  4332. gen_or(b1, b0);
  4333. }
  4334. return b0;
  4335. case Q_LINK:
  4336. bpf_error(cstate, "link-layer modifier applied to %s", typestr);
  4337. case Q_IP:
  4338. return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_IP, 12, 16);
  4339. case Q_RARP:
  4340. return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
  4341. case Q_ARP:
  4342. return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_ARP, 14, 24);
  4343. case Q_SCTP:
  4344. bpf_error(cstate, "'sctp' modifier applied to %s", typestr);
  4345. case Q_TCP:
  4346. bpf_error(cstate, "'tcp' modifier applied to %s", typestr);
  4347. case Q_UDP:
  4348. bpf_error(cstate, "'udp' modifier applied to %s", typestr);
  4349. case Q_ICMP:
  4350. bpf_error(cstate, "'icmp' modifier applied to %s", typestr);
  4351. case Q_IGMP:
  4352. bpf_error(cstate, "'igmp' modifier applied to %s", typestr);
  4353. case Q_IGRP:
  4354. bpf_error(cstate, "'igrp' modifier applied to %s", typestr);
  4355. case Q_ATALK:
  4356. bpf_error(cstate, "AppleTalk host filtering not implemented");
  4357. case Q_DECNET:
  4358. return gen_dnhostop(cstate, addr, dir);
  4359. case Q_LAT:
  4360. bpf_error(cstate, "LAT host filtering not implemented");
  4361. case Q_SCA:
  4362. bpf_error(cstate, "SCA host filtering not implemented");
  4363. case Q_MOPRC:
  4364. bpf_error(cstate, "MOPRC host filtering not implemented");
  4365. case Q_MOPDL:
  4366. bpf_error(cstate, "MOPDL host filtering not implemented");
  4367. case Q_IPV6:
  4368. bpf_error(cstate, "'ip6' modifier applied to ip host");
  4369. case Q_ICMPV6:
  4370. bpf_error(cstate, "'icmp6' modifier applied to %s", typestr);
  4371. case Q_AH:
  4372. bpf_error(cstate, "'ah' modifier applied to %s", typestr);
  4373. case Q_ESP:
  4374. bpf_error(cstate, "'esp' modifier applied to %s", typestr);
  4375. case Q_PIM:
  4376. bpf_error(cstate, "'pim' modifier applied to %s", typestr);
  4377. case Q_VRRP:
  4378. bpf_error(cstate, "'vrrp' modifier applied to %s", typestr);
  4379. case Q_AARP:
  4380. bpf_error(cstate, "AARP host filtering not implemented");
  4381. case Q_ISO:
  4382. bpf_error(cstate, "ISO host filtering not implemented");
  4383. case Q_ESIS:
  4384. bpf_error(cstate, "'esis' modifier applied to %s", typestr);
  4385. case Q_ISIS:
  4386. bpf_error(cstate, "'isis' modifier applied to %s", typestr);
  4387. case Q_CLNP:
  4388. bpf_error(cstate, "'clnp' modifier applied to %s", typestr);
  4389. case Q_STP:
  4390. bpf_error(cstate, "'stp' modifier applied to %s", typestr);
  4391. case Q_IPX:
  4392. bpf_error(cstate, "IPX host filtering not implemented");
  4393. case Q_NETBEUI:
  4394. bpf_error(cstate, "'netbeui' modifier applied to %s", typestr);
  4395. case Q_ISIS_L1:
  4396. bpf_error(cstate, "'l1' modifier applied to %s", typestr);
  4397. case Q_ISIS_L2:
  4398. bpf_error(cstate, "'l2' modifier applied to %s", typestr);
  4399. case Q_ISIS_IIH:
  4400. bpf_error(cstate, "'iih' modifier applied to %s", typestr);
  4401. case Q_ISIS_SNP:
  4402. bpf_error(cstate, "'snp' modifier applied to %s", typestr);
  4403. case Q_ISIS_CSNP:
  4404. bpf_error(cstate, "'csnp' modifier applied to %s", typestr);
  4405. case Q_ISIS_PSNP:
  4406. bpf_error(cstate, "'psnp' modifier applied to %s", typestr);
  4407. case Q_ISIS_LSP:
  4408. bpf_error(cstate, "'lsp' modifier applied to %s", typestr);
  4409. case Q_RADIO:
  4410. bpf_error(cstate, "'radio' modifier applied to %s", typestr);
  4411. case Q_CARP:
  4412. bpf_error(cstate, "'carp' modifier applied to %s", typestr);
  4413. default:
  4414. abort();
  4415. }
  4416. /*NOTREACHED*/
  4417. }
  4418. #ifdef INET6
  4419. static struct block *
  4420. gen_host6(compiler_state_t *cstate, struct in6_addr *addr,
  4421. struct in6_addr *mask, int proto, int dir, int type)
  4422. {
  4423. const char *typestr;
  4424. if (type == Q_NET)
  4425. typestr = "net";
  4426. else
  4427. typestr = "host";
  4428. switch (proto) {
  4429. case Q_DEFAULT:
  4430. return gen_host6(cstate, addr, mask, Q_IPV6, dir, type);
  4431. case Q_LINK:
  4432. bpf_error(cstate, "link-layer modifier applied to ip6 %s", typestr);
  4433. case Q_IP:
  4434. bpf_error(cstate, "'ip' modifier applied to ip6 %s", typestr);
  4435. case Q_RARP:
  4436. bpf_error(cstate, "'rarp' modifier applied to ip6 %s", typestr);
  4437. case Q_ARP:
  4438. bpf_error(cstate, "'arp' modifier applied to ip6 %s", typestr);
  4439. case Q_SCTP:
  4440. bpf_error(cstate, "'sctp' modifier applied to ip6 %s", typestr);
  4441. case Q_TCP:
  4442. bpf_error(cstate, "'tcp' modifier applied to ip6 %s", typestr);
  4443. case Q_UDP:
  4444. bpf_error(cstate, "'udp' modifier applied to ip6 %s", typestr);
  4445. case Q_ICMP:
  4446. bpf_error(cstate, "'icmp' modifier applied to ip6 %s", typestr);
  4447. case Q_IGMP:
  4448. bpf_error(cstate, "'igmp' modifier applied to ip6 %s", typestr);
  4449. case Q_IGRP:
  4450. bpf_error(cstate, "'igrp' modifier applied to ip6 %s", typestr);
  4451. case Q_ATALK:
  4452. bpf_error(cstate, "AppleTalk modifier applied to ip6 %s", typestr);
  4453. case Q_DECNET:
  4454. bpf_error(cstate, "'decnet' modifier applied to ip6 %s", typestr);
  4455. case Q_LAT:
  4456. bpf_error(cstate, "'lat' modifier applied to ip6 %s", typestr);
  4457. case Q_SCA:
  4458. bpf_error(cstate, "'sca' modifier applied to ip6 %s", typestr);
  4459. case Q_MOPRC:
  4460. bpf_error(cstate, "'moprc' modifier applied to ip6 %s", typestr);
  4461. case Q_MOPDL:
  4462. bpf_error(cstate, "'mopdl' modifier applied to ip6 %s", typestr);
  4463. case Q_IPV6:
  4464. return gen_hostop6(cstate, addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
  4465. case Q_ICMPV6:
  4466. bpf_error(cstate, "'icmp6' modifier applied to ip6 %s", typestr);
  4467. case Q_AH:
  4468. bpf_error(cstate, "'ah' modifier applied to ip6 %s", typestr);
  4469. case Q_ESP:
  4470. bpf_error(cstate, "'esp' modifier applied to ip6 %s", typestr);
  4471. case Q_PIM:
  4472. bpf_error(cstate, "'pim' modifier applied to ip6 %s", typestr);
  4473. case Q_VRRP:
  4474. bpf_error(cstate, "'vrrp' modifier applied to ip6 %s", typestr);
  4475. case Q_AARP:
  4476. bpf_error(cstate, "'aarp' modifier applied to ip6 %s", typestr);
  4477. case Q_ISO:
  4478. bpf_error(cstate, "'iso' modifier applied to ip6 %s", typestr);
  4479. case Q_ESIS:
  4480. bpf_error(cstate, "'esis' modifier applied to ip6 %s", typestr);
  4481. case Q_ISIS:
  4482. bpf_error(cstate, "'isis' modifier applied to ip6 %s", typestr);
  4483. case Q_CLNP:
  4484. bpf_error(cstate, "'clnp' modifier applied to ip6 %s", typestr);
  4485. case Q_STP:
  4486. bpf_error(cstate, "'stp' modifier applied to ip6 %s", typestr);
  4487. case Q_IPX:
  4488. bpf_error(cstate, "'ipx' modifier applied to ip6 %s", typestr);
  4489. case Q_NETBEUI:
  4490. bpf_error(cstate, "'netbeui' modifier applied to ip6 %s", typestr);
  4491. case Q_ISIS_L1:
  4492. bpf_error(cstate, "'l1' modifier applied to ip6 %s", typestr);
  4493. case Q_ISIS_L2:
  4494. bpf_error(cstate, "'l2' modifier applied to ip6 %s", typestr);
  4495. case Q_ISIS_IIH:
  4496. bpf_error(cstate, "'iih' modifier applied to ip6 %s", typestr);
  4497. case Q_ISIS_SNP:
  4498. bpf_error(cstate, "'snp' modifier applied to ip6 %s", typestr);
  4499. case Q_ISIS_CSNP:
  4500. bpf_error(cstate, "'csnp' modifier applied to ip6 %s", typestr);
  4501. case Q_ISIS_PSNP:
  4502. bpf_error(cstate, "'psnp' modifier applied to ip6 %s", typestr);
  4503. case Q_ISIS_LSP:
  4504. bpf_error(cstate, "'lsp' modifier applied to ip6 %s", typestr);
  4505. case Q_RADIO:
  4506. bpf_error(cstate, "'radio' modifier applied to ip6 %s", typestr);
  4507. case Q_CARP:
  4508. bpf_error(cstate, "'carp' modifier applied to ip6 %s", typestr);
  4509. default:
  4510. abort();
  4511. }
  4512. /*NOTREACHED*/
  4513. }
  4514. #endif
  4515. #ifndef INET6
  4516. static struct block *
  4517. gen_gateway(compiler_state_t *cstate, const u_char *eaddr,
  4518. struct addrinfo *alist, int proto, int dir)
  4519. {
  4520. struct block *b0, *b1, *tmp;
  4521. struct addrinfo *ai;
  4522. struct sockaddr_in *sin;
  4523. if (dir != 0)
  4524. bpf_error(cstate, "direction applied to 'gateway'");
  4525. switch (proto) {
  4526. case Q_DEFAULT:
  4527. case Q_IP:
  4528. case Q_ARP:
  4529. case Q_RARP:
  4530. switch (cstate->linktype) {
  4531. case DLT_EN10MB:
  4532. case DLT_NETANALYZER:
  4533. case DLT_NETANALYZER_TRANSPARENT:
  4534. b1 = gen_prevlinkhdr_check(cstate);
  4535. b0 = gen_ehostop(cstate, eaddr, Q_OR);
  4536. if (b1 != NULL)
  4537. gen_and(b1, b0);
  4538. break;
  4539. case DLT_FDDI:
  4540. b0 = gen_fhostop(cstate, eaddr, Q_OR);
  4541. break;
  4542. case DLT_IEEE802:
  4543. b0 = gen_thostop(cstate, eaddr, Q_OR);
  4544. break;
  4545. case DLT_IEEE802_11:
  4546. case DLT_PRISM_HEADER:
  4547. case DLT_IEEE802_11_RADIO_AVS:
  4548. case DLT_IEEE802_11_RADIO:
  4549. case DLT_PPI:
  4550. b0 = gen_wlanhostop(cstate, eaddr, Q_OR);
  4551. break;
  4552. case DLT_SUNATM:
  4553. /*
  4554. * This is LLC-multiplexed traffic; if it were
  4555. * LANE, cstate->linktype would have been set to
  4556. * DLT_EN10MB.
  4557. */
  4558. bpf_error(cstate,
  4559. "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
  4560. break;
  4561. case DLT_IP_OVER_FC:
  4562. b0 = gen_ipfchostop(cstate, eaddr, Q_OR);
  4563. break;
  4564. default:
  4565. bpf_error(cstate,
  4566. "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
  4567. }
  4568. b1 = NULL;
  4569. for (ai = alist; ai != NULL; ai = ai->ai_next) {
  4570. /*
  4571. * Does it have an address?
  4572. */
  4573. if (ai->ai_addr != NULL) {
  4574. /*
  4575. * Yes. Is it an IPv4 address?
  4576. */
  4577. if (ai->ai_addr->sa_family == AF_INET) {
  4578. /*
  4579. * Generate an entry for it.
  4580. */
  4581. sin = (struct sockaddr_in *)ai->ai_addr;
  4582. tmp = gen_host(cstate,
  4583. ntohl(sin->sin_addr.s_addr),
  4584. 0xffffffff, proto, Q_OR, Q_HOST);
  4585. /*
  4586. * Is it the *first* IPv4 address?
  4587. */
  4588. if (b1 == NULL) {
  4589. /*
  4590. * Yes, so start with it.
  4591. */
  4592. b1 = tmp;
  4593. } else {
  4594. /*
  4595. * No, so OR it into the
  4596. * existing set of
  4597. * addresses.
  4598. */
  4599. gen_or(b1, tmp);
  4600. b1 = tmp;
  4601. }
  4602. }
  4603. }
  4604. }
  4605. if (b1 == NULL) {
  4606. /*
  4607. * No IPv4 addresses found.
  4608. */
  4609. return (NULL);
  4610. }
  4611. gen_not(b1);
  4612. gen_and(b0, b1);
  4613. return b1;
  4614. }
  4615. bpf_error(cstate, "illegal modifier of 'gateway'");
  4616. /*NOTREACHED*/
  4617. }
  4618. #endif
  4619. static struct block *
  4620. gen_proto_abbrev_internal(compiler_state_t *cstate, int proto)
  4621. {
  4622. struct block *b0;
  4623. struct block *b1;
  4624. switch (proto) {
  4625. case Q_SCTP:
  4626. b1 = gen_proto(cstate, IPPROTO_SCTP, Q_IP, Q_DEFAULT);
  4627. b0 = gen_proto(cstate, IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
  4628. gen_or(b0, b1);
  4629. break;
  4630. case Q_TCP:
  4631. b1 = gen_proto(cstate, IPPROTO_TCP, Q_IP, Q_DEFAULT);
  4632. b0 = gen_proto(cstate, IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
  4633. gen_or(b0, b1);
  4634. break;
  4635. case Q_UDP:
  4636. b1 = gen_proto(cstate, IPPROTO_UDP, Q_IP, Q_DEFAULT);
  4637. b0 = gen_proto(cstate, IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
  4638. gen_or(b0, b1);
  4639. break;
  4640. case Q_ICMP:
  4641. b1 = gen_proto(cstate, IPPROTO_ICMP, Q_IP, Q_DEFAULT);
  4642. break;
  4643. #ifndef IPPROTO_IGMP
  4644. #define IPPROTO_IGMP 2
  4645. #endif
  4646. case Q_IGMP:
  4647. b1 = gen_proto(cstate, IPPROTO_IGMP, Q_IP, Q_DEFAULT);
  4648. break;
  4649. #ifndef IPPROTO_IGRP
  4650. #define IPPROTO_IGRP 9
  4651. #endif
  4652. case Q_IGRP:
  4653. b1 = gen_proto(cstate, IPPROTO_IGRP, Q_IP, Q_DEFAULT);
  4654. break;
  4655. #ifndef IPPROTO_PIM
  4656. #define IPPROTO_PIM 103
  4657. #endif
  4658. case Q_PIM:
  4659. b1 = gen_proto(cstate, IPPROTO_PIM, Q_IP, Q_DEFAULT);
  4660. b0 = gen_proto(cstate, IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
  4661. gen_or(b0, b1);
  4662. break;
  4663. #ifndef IPPROTO_VRRP
  4664. #define IPPROTO_VRRP 112
  4665. #endif
  4666. case Q_VRRP:
  4667. b1 = gen_proto(cstate, IPPROTO_VRRP, Q_IP, Q_DEFAULT);
  4668. break;
  4669. #ifndef IPPROTO_CARP
  4670. #define IPPROTO_CARP 112
  4671. #endif
  4672. case Q_CARP:
  4673. b1 = gen_proto(cstate, IPPROTO_CARP, Q_IP, Q_DEFAULT);
  4674. break;
  4675. case Q_IP:
  4676. b1 = gen_linktype(cstate, ETHERTYPE_IP);
  4677. break;
  4678. case Q_ARP:
  4679. b1 = gen_linktype(cstate, ETHERTYPE_ARP);
  4680. break;
  4681. case Q_RARP:
  4682. b1 = gen_linktype(cstate, ETHERTYPE_REVARP);
  4683. break;
  4684. case Q_LINK:
  4685. bpf_error(cstate, "link layer applied in wrong context");
  4686. case Q_ATALK:
  4687. b1 = gen_linktype(cstate, ETHERTYPE_ATALK);
  4688. break;
  4689. case Q_AARP:
  4690. b1 = gen_linktype(cstate, ETHERTYPE_AARP);
  4691. break;
  4692. case Q_DECNET:
  4693. b1 = gen_linktype(cstate, ETHERTYPE_DN);
  4694. break;
  4695. case Q_SCA:
  4696. b1 = gen_linktype(cstate, ETHERTYPE_SCA);
  4697. break;
  4698. case Q_LAT:
  4699. b1 = gen_linktype(cstate, ETHERTYPE_LAT);
  4700. break;
  4701. case Q_MOPDL:
  4702. b1 = gen_linktype(cstate, ETHERTYPE_MOPDL);
  4703. break;
  4704. case Q_MOPRC:
  4705. b1 = gen_linktype(cstate, ETHERTYPE_MOPRC);
  4706. break;
  4707. case Q_IPV6:
  4708. b1 = gen_linktype(cstate, ETHERTYPE_IPV6);
  4709. break;
  4710. #ifndef IPPROTO_ICMPV6
  4711. #define IPPROTO_ICMPV6 58
  4712. #endif
  4713. case Q_ICMPV6:
  4714. b1 = gen_proto(cstate, IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
  4715. break;
  4716. #ifndef IPPROTO_AH
  4717. #define IPPROTO_AH 51
  4718. #endif
  4719. case Q_AH:
  4720. b1 = gen_proto(cstate, IPPROTO_AH, Q_IP, Q_DEFAULT);
  4721. b0 = gen_proto(cstate, IPPROTO_AH, Q_IPV6, Q_DEFAULT);
  4722. gen_or(b0, b1);
  4723. break;
  4724. #ifndef IPPROTO_ESP
  4725. #define IPPROTO_ESP 50
  4726. #endif
  4727. case Q_ESP:
  4728. b1 = gen_proto(cstate, IPPROTO_ESP, Q_IP, Q_DEFAULT);
  4729. b0 = gen_proto(cstate, IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
  4730. gen_or(b0, b1);
  4731. break;
  4732. case Q_ISO:
  4733. b1 = gen_linktype(cstate, LLCSAP_ISONS);
  4734. break;
  4735. case Q_ESIS:
  4736. b1 = gen_proto(cstate, ISO9542_ESIS, Q_ISO, Q_DEFAULT);
  4737. break;
  4738. case Q_ISIS:
  4739. b1 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
  4740. break;
  4741. case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
  4742. b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
  4743. b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
  4744. gen_or(b0, b1);
  4745. b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
  4746. gen_or(b0, b1);
  4747. b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
  4748. gen_or(b0, b1);
  4749. b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
  4750. gen_or(b0, b1);
  4751. break;
  4752. case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
  4753. b0 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
  4754. b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
  4755. gen_or(b0, b1);
  4756. b0 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
  4757. gen_or(b0, b1);
  4758. b0 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
  4759. gen_or(b0, b1);
  4760. b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
  4761. gen_or(b0, b1);
  4762. break;
  4763. case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
  4764. b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
  4765. b1 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
  4766. gen_or(b0, b1);
  4767. b0 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
  4768. gen_or(b0, b1);
  4769. break;
  4770. case Q_ISIS_LSP:
  4771. b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
  4772. b1 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
  4773. gen_or(b0, b1);
  4774. break;
  4775. case Q_ISIS_SNP:
  4776. b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
  4777. b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
  4778. gen_or(b0, b1);
  4779. b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
  4780. gen_or(b0, b1);
  4781. b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
  4782. gen_or(b0, b1);
  4783. break;
  4784. case Q_ISIS_CSNP:
  4785. b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
  4786. b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
  4787. gen_or(b0, b1);
  4788. break;
  4789. case Q_ISIS_PSNP:
  4790. b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
  4791. b1 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
  4792. gen_or(b0, b1);
  4793. break;
  4794. case Q_CLNP:
  4795. b1 = gen_proto(cstate, ISO8473_CLNP, Q_ISO, Q_DEFAULT);
  4796. break;
  4797. case Q_STP:
  4798. b1 = gen_linktype(cstate, LLCSAP_8021D);
  4799. break;
  4800. case Q_IPX:
  4801. b1 = gen_linktype(cstate, LLCSAP_IPX);
  4802. break;
  4803. case Q_NETBEUI:
  4804. b1 = gen_linktype(cstate, LLCSAP_NETBEUI);
  4805. break;
  4806. case Q_RADIO:
  4807. bpf_error(cstate, "'radio' is not a valid protocol type");
  4808. default:
  4809. abort();
  4810. }
  4811. return b1;
  4812. }
  4813. struct block *
  4814. gen_proto_abbrev(compiler_state_t *cstate, int proto)
  4815. {
  4816. /*
  4817. * Catch errors reported by us and routines below us, and return NULL
  4818. * on an error.
  4819. */
  4820. if (setjmp(cstate->top_ctx))
  4821. return (NULL);
  4822. return gen_proto_abbrev_internal(cstate, proto);
  4823. }
  4824. static struct block *
  4825. gen_ipfrag(compiler_state_t *cstate)
  4826. {
  4827. struct slist *s;
  4828. struct block *b;
  4829. /* not IPv4 frag other than the first frag */
  4830. s = gen_load_a(cstate, OR_LINKPL, 6, BPF_H);
  4831. b = new_block(cstate, JMP(BPF_JSET));
  4832. b->s.k = 0x1fff;
  4833. b->stmts = s;
  4834. gen_not(b);
  4835. return b;
  4836. }
  4837. /*
  4838. * Generate a comparison to a port value in the transport-layer header
  4839. * at the specified offset from the beginning of that header.
  4840. *
  4841. * XXX - this handles a variable-length prefix preceding the link-layer
  4842. * header, such as the radiotap or AVS radio prefix, but doesn't handle
  4843. * variable-length link-layer headers (such as Token Ring or 802.11
  4844. * headers).
  4845. */
  4846. static struct block *
  4847. gen_portatom(compiler_state_t *cstate, int off, bpf_u_int32 v)
  4848. {
  4849. return gen_cmp(cstate, OR_TRAN_IPV4, off, BPF_H, v);
  4850. }
  4851. static struct block *
  4852. gen_portatom6(compiler_state_t *cstate, int off, bpf_u_int32 v)
  4853. {
  4854. return gen_cmp(cstate, OR_TRAN_IPV6, off, BPF_H, v);
  4855. }
  4856. static struct block *
  4857. gen_portop(compiler_state_t *cstate, u_int port, u_int proto, int dir)
  4858. {
  4859. struct block *b0, *b1, *tmp;
  4860. /* ip proto 'proto' and not a fragment other than the first fragment */
  4861. tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto);
  4862. b0 = gen_ipfrag(cstate);
  4863. gen_and(tmp, b0);
  4864. switch (dir) {
  4865. case Q_SRC:
  4866. b1 = gen_portatom(cstate, 0, port);
  4867. break;
  4868. case Q_DST:
  4869. b1 = gen_portatom(cstate, 2, port);
  4870. break;
  4871. case Q_AND:
  4872. tmp = gen_portatom(cstate, 0, port);
  4873. b1 = gen_portatom(cstate, 2, port);
  4874. gen_and(tmp, b1);
  4875. break;
  4876. case Q_DEFAULT:
  4877. case Q_OR:
  4878. tmp = gen_portatom(cstate, 0, port);
  4879. b1 = gen_portatom(cstate, 2, port);
  4880. gen_or(tmp, b1);
  4881. break;
  4882. case Q_ADDR1:
  4883. bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for ports");
  4884. /*NOTREACHED*/
  4885. case Q_ADDR2:
  4886. bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for ports");
  4887. /*NOTREACHED*/
  4888. case Q_ADDR3:
  4889. bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for ports");
  4890. /*NOTREACHED*/
  4891. case Q_ADDR4:
  4892. bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for ports");
  4893. /*NOTREACHED*/
  4894. case Q_RA:
  4895. bpf_error(cstate, "'ra' is not a valid qualifier for ports");
  4896. /*NOTREACHED*/
  4897. case Q_TA:
  4898. bpf_error(cstate, "'ta' is not a valid qualifier for ports");
  4899. /*NOTREACHED*/
  4900. default:
  4901. abort();
  4902. /*NOTREACHED*/
  4903. }
  4904. gen_and(b0, b1);
  4905. return b1;
  4906. }
  4907. static struct block *
  4908. gen_port(compiler_state_t *cstate, u_int port, int ip_proto, int dir)
  4909. {
  4910. struct block *b0, *b1, *tmp;
  4911. /*
  4912. * ether proto ip
  4913. *
  4914. * For FDDI, RFC 1188 says that SNAP encapsulation is used,
  4915. * not LLC encapsulation with LLCSAP_IP.
  4916. *
  4917. * For IEEE 802 networks - which includes 802.5 token ring
  4918. * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
  4919. * says that SNAP encapsulation is used, not LLC encapsulation
  4920. * with LLCSAP_IP.
  4921. *
  4922. * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
  4923. * RFC 2225 say that SNAP encapsulation is used, not LLC
  4924. * encapsulation with LLCSAP_IP.
  4925. *
  4926. * So we always check for ETHERTYPE_IP.
  4927. */
  4928. b0 = gen_linktype(cstate, ETHERTYPE_IP);
  4929. switch (ip_proto) {
  4930. case IPPROTO_UDP:
  4931. case IPPROTO_TCP:
  4932. case IPPROTO_SCTP:
  4933. b1 = gen_portop(cstate, port, (u_int)ip_proto, dir);
  4934. break;
  4935. case PROTO_UNDEF:
  4936. tmp = gen_portop(cstate, port, IPPROTO_TCP, dir);
  4937. b1 = gen_portop(cstate, port, IPPROTO_UDP, dir);
  4938. gen_or(tmp, b1);
  4939. tmp = gen_portop(cstate, port, IPPROTO_SCTP, dir);
  4940. gen_or(tmp, b1);
  4941. break;
  4942. default:
  4943. abort();
  4944. }
  4945. gen_and(b0, b1);
  4946. return b1;
  4947. }
  4948. struct block *
  4949. gen_portop6(compiler_state_t *cstate, u_int port, u_int proto, int dir)
  4950. {
  4951. struct block *b0, *b1, *tmp;
  4952. /* ip6 proto 'proto' */
  4953. /* XXX - catch the first fragment of a fragmented packet? */
  4954. b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto);
  4955. switch (dir) {
  4956. case Q_SRC:
  4957. b1 = gen_portatom6(cstate, 0, port);
  4958. break;
  4959. case Q_DST:
  4960. b1 = gen_portatom6(cstate, 2, port);
  4961. break;
  4962. case Q_AND:
  4963. tmp = gen_portatom6(cstate, 0, port);
  4964. b1 = gen_portatom6(cstate, 2, port);
  4965. gen_and(tmp, b1);
  4966. break;
  4967. case Q_DEFAULT:
  4968. case Q_OR:
  4969. tmp = gen_portatom6(cstate, 0, port);
  4970. b1 = gen_portatom6(cstate, 2, port);
  4971. gen_or(tmp, b1);
  4972. break;
  4973. default:
  4974. abort();
  4975. }
  4976. gen_and(b0, b1);
  4977. return b1;
  4978. }
  4979. static struct block *
  4980. gen_port6(compiler_state_t *cstate, u_int port, int ip_proto, int dir)
  4981. {
  4982. struct block *b0, *b1, *tmp;
  4983. /* link proto ip6 */
  4984. b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
  4985. switch (ip_proto) {
  4986. case IPPROTO_UDP:
  4987. case IPPROTO_TCP:
  4988. case IPPROTO_SCTP:
  4989. b1 = gen_portop6(cstate, port, (u_int)ip_proto, dir);
  4990. break;
  4991. case PROTO_UNDEF:
  4992. tmp = gen_portop6(cstate, port, IPPROTO_TCP, dir);
  4993. b1 = gen_portop6(cstate, port, IPPROTO_UDP, dir);
  4994. gen_or(tmp, b1);
  4995. tmp = gen_portop6(cstate, port, IPPROTO_SCTP, dir);
  4996. gen_or(tmp, b1);
  4997. break;
  4998. default:
  4999. abort();
  5000. }
  5001. gen_and(b0, b1);
  5002. return b1;
  5003. }
  5004. /* gen_portrange code */
  5005. static struct block *
  5006. gen_portrangeatom(compiler_state_t *cstate, u_int off, bpf_u_int32 v1,
  5007. bpf_u_int32 v2)
  5008. {
  5009. struct block *b1, *b2;
  5010. if (v1 > v2) {
  5011. /*
  5012. * Reverse the order of the ports, so v1 is the lower one.
  5013. */
  5014. bpf_u_int32 vtemp;
  5015. vtemp = v1;
  5016. v1 = v2;
  5017. v2 = vtemp;
  5018. }
  5019. b1 = gen_cmp_ge(cstate, OR_TRAN_IPV4, off, BPF_H, v1);
  5020. b2 = gen_cmp_le(cstate, OR_TRAN_IPV4, off, BPF_H, v2);
  5021. gen_and(b1, b2);
  5022. return b2;
  5023. }
  5024. static struct block *
  5025. gen_portrangeop(compiler_state_t *cstate, u_int port1, u_int port2,
  5026. bpf_u_int32 proto, int dir)
  5027. {
  5028. struct block *b0, *b1, *tmp;
  5029. /* ip proto 'proto' and not a fragment other than the first fragment */
  5030. tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto);
  5031. b0 = gen_ipfrag(cstate);
  5032. gen_and(tmp, b0);
  5033. switch (dir) {
  5034. case Q_SRC:
  5035. b1 = gen_portrangeatom(cstate, 0, port1, port2);
  5036. break;
  5037. case Q_DST:
  5038. b1 = gen_portrangeatom(cstate, 2, port1, port2);
  5039. break;
  5040. case Q_AND:
  5041. tmp = gen_portrangeatom(cstate, 0, port1, port2);
  5042. b1 = gen_portrangeatom(cstate, 2, port1, port2);
  5043. gen_and(tmp, b1);
  5044. break;
  5045. case Q_DEFAULT:
  5046. case Q_OR:
  5047. tmp = gen_portrangeatom(cstate, 0, port1, port2);
  5048. b1 = gen_portrangeatom(cstate, 2, port1, port2);
  5049. gen_or(tmp, b1);
  5050. break;
  5051. case Q_ADDR1:
  5052. bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for port ranges");
  5053. /*NOTREACHED*/
  5054. case Q_ADDR2:
  5055. bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for port ranges");
  5056. /*NOTREACHED*/
  5057. case Q_ADDR3:
  5058. bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for port ranges");
  5059. /*NOTREACHED*/
  5060. case Q_ADDR4:
  5061. bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for port ranges");
  5062. /*NOTREACHED*/
  5063. case Q_RA:
  5064. bpf_error(cstate, "'ra' is not a valid qualifier for port ranges");
  5065. /*NOTREACHED*/
  5066. case Q_TA:
  5067. bpf_error(cstate, "'ta' is not a valid qualifier for port ranges");
  5068. /*NOTREACHED*/
  5069. default:
  5070. abort();
  5071. /*NOTREACHED*/
  5072. }
  5073. gen_and(b0, b1);
  5074. return b1;
  5075. }
  5076. static struct block *
  5077. gen_portrange(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto,
  5078. int dir)
  5079. {
  5080. struct block *b0, *b1, *tmp;
  5081. /* link proto ip */
  5082. b0 = gen_linktype(cstate, ETHERTYPE_IP);
  5083. switch (ip_proto) {
  5084. case IPPROTO_UDP:
  5085. case IPPROTO_TCP:
  5086. case IPPROTO_SCTP:
  5087. b1 = gen_portrangeop(cstate, port1, port2, (bpf_u_int32)ip_proto,
  5088. dir);
  5089. break;
  5090. case PROTO_UNDEF:
  5091. tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_TCP, dir);
  5092. b1 = gen_portrangeop(cstate, port1, port2, IPPROTO_UDP, dir);
  5093. gen_or(tmp, b1);
  5094. tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_SCTP, dir);
  5095. gen_or(tmp, b1);
  5096. break;
  5097. default:
  5098. abort();
  5099. }
  5100. gen_and(b0, b1);
  5101. return b1;
  5102. }
  5103. static struct block *
  5104. gen_portrangeatom6(compiler_state_t *cstate, u_int off, bpf_u_int32 v1,
  5105. bpf_u_int32 v2)
  5106. {
  5107. struct block *b1, *b2;
  5108. if (v1 > v2) {
  5109. /*
  5110. * Reverse the order of the ports, so v1 is the lower one.
  5111. */
  5112. bpf_u_int32 vtemp;
  5113. vtemp = v1;
  5114. v1 = v2;
  5115. v2 = vtemp;
  5116. }
  5117. b1 = gen_cmp_ge(cstate, OR_TRAN_IPV6, off, BPF_H, v1);
  5118. b2 = gen_cmp_le(cstate, OR_TRAN_IPV6, off, BPF_H, v2);
  5119. gen_and(b1, b2);
  5120. return b2;
  5121. }
  5122. static struct block *
  5123. gen_portrangeop6(compiler_state_t *cstate, u_int port1, u_int port2,
  5124. bpf_u_int32 proto, int dir)
  5125. {
  5126. struct block *b0, *b1, *tmp;
  5127. /* ip6 proto 'proto' */
  5128. /* XXX - catch the first fragment of a fragmented packet? */
  5129. b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto);
  5130. switch (dir) {
  5131. case Q_SRC:
  5132. b1 = gen_portrangeatom6(cstate, 0, port1, port2);
  5133. break;
  5134. case Q_DST:
  5135. b1 = gen_portrangeatom6(cstate, 2, port1, port2);
  5136. break;
  5137. case Q_AND:
  5138. tmp = gen_portrangeatom6(cstate, 0, port1, port2);
  5139. b1 = gen_portrangeatom6(cstate, 2, port1, port2);
  5140. gen_and(tmp, b1);
  5141. break;
  5142. case Q_DEFAULT:
  5143. case Q_OR:
  5144. tmp = gen_portrangeatom6(cstate, 0, port1, port2);
  5145. b1 = gen_portrangeatom6(cstate, 2, port1, port2);
  5146. gen_or(tmp, b1);
  5147. break;
  5148. default:
  5149. abort();
  5150. }
  5151. gen_and(b0, b1);
  5152. return b1;
  5153. }
  5154. static struct block *
  5155. gen_portrange6(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto,
  5156. int dir)
  5157. {
  5158. struct block *b0, *b1, *tmp;
  5159. /* link proto ip6 */
  5160. b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
  5161. switch (ip_proto) {
  5162. case IPPROTO_UDP:
  5163. case IPPROTO_TCP:
  5164. case IPPROTO_SCTP:
  5165. b1 = gen_portrangeop6(cstate, port1, port2, (bpf_u_int32)ip_proto,
  5166. dir);
  5167. break;
  5168. case PROTO_UNDEF:
  5169. tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_TCP, dir);
  5170. b1 = gen_portrangeop6(cstate, port1, port2, IPPROTO_UDP, dir);
  5171. gen_or(tmp, b1);
  5172. tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_SCTP, dir);
  5173. gen_or(tmp, b1);
  5174. break;
  5175. default:
  5176. abort();
  5177. }
  5178. gen_and(b0, b1);
  5179. return b1;
  5180. }
  5181. static int
  5182. lookup_proto(compiler_state_t *cstate, const char *name, int proto)
  5183. {
  5184. register int v;
  5185. switch (proto) {
  5186. case Q_DEFAULT:
  5187. case Q_IP:
  5188. case Q_IPV6:
  5189. v = pcap_nametoproto(name);
  5190. if (v == PROTO_UNDEF)
  5191. bpf_error(cstate, "unknown ip proto '%s'", name);
  5192. break;
  5193. case Q_LINK:
  5194. /* XXX should look up h/w protocol type based on cstate->linktype */
  5195. v = pcap_nametoeproto(name);
  5196. if (v == PROTO_UNDEF) {
  5197. v = pcap_nametollc(name);
  5198. if (v == PROTO_UNDEF)
  5199. bpf_error(cstate, "unknown ether proto '%s'", name);
  5200. }
  5201. break;
  5202. case Q_ISO:
  5203. if (strcmp(name, "esis") == 0)
  5204. v = ISO9542_ESIS;
  5205. else if (strcmp(name, "isis") == 0)
  5206. v = ISO10589_ISIS;
  5207. else if (strcmp(name, "clnp") == 0)
  5208. v = ISO8473_CLNP;
  5209. else
  5210. bpf_error(cstate, "unknown osi proto '%s'", name);
  5211. break;
  5212. default:
  5213. v = PROTO_UNDEF;
  5214. break;
  5215. }
  5216. return v;
  5217. }
  5218. #if 0
  5219. struct stmt *
  5220. gen_joinsp(struct stmt **s, int n)
  5221. {
  5222. return NULL;
  5223. }
  5224. #endif
  5225. static struct block *
  5226. gen_protochain(compiler_state_t *cstate, bpf_u_int32 v, int proto)
  5227. {
  5228. #ifdef NO_PROTOCHAIN
  5229. return gen_proto(cstate, v, proto);
  5230. #else
  5231. struct block *b0, *b;
  5232. struct slist *s[100];
  5233. int fix2, fix3, fix4, fix5;
  5234. int ahcheck, again, end;
  5235. int i, max;
  5236. int reg2 = alloc_reg(cstate);
  5237. memset(s, 0, sizeof(s));
  5238. fix3 = fix4 = fix5 = 0;
  5239. switch (proto) {
  5240. case Q_IP:
  5241. case Q_IPV6:
  5242. break;
  5243. case Q_DEFAULT:
  5244. b0 = gen_protochain(cstate, v, Q_IP);
  5245. b = gen_protochain(cstate, v, Q_IPV6);
  5246. gen_or(b0, b);
  5247. return b;
  5248. default:
  5249. bpf_error(cstate, "bad protocol applied for 'protochain'");
  5250. /*NOTREACHED*/
  5251. }
  5252. /*
  5253. * We don't handle variable-length prefixes before the link-layer
  5254. * header, or variable-length link-layer headers, here yet.
  5255. * We might want to add BPF instructions to do the protochain
  5256. * work, to simplify that and, on platforms that have a BPF
  5257. * interpreter with the new instructions, let the filtering
  5258. * be done in the kernel. (We already require a modified BPF
  5259. * engine to do the protochain stuff, to support backward
  5260. * branches, and backward branch support is unlikely to appear
  5261. * in kernel BPF engines.)
  5262. */
  5263. if (cstate->off_linkpl.is_variable)
  5264. bpf_error(cstate, "'protochain' not supported with variable length headers");
  5265. cstate->no_optimize = 1; /* this code is not compatible with optimizer yet */
  5266. /*
  5267. * s[0] is a dummy entry to protect other BPF insn from damage
  5268. * by s[fix] = foo with uninitialized variable "fix". It is somewhat
  5269. * hard to find interdependency made by jump table fixup.
  5270. */
  5271. i = 0;
  5272. s[i] = new_stmt(cstate, 0); /*dummy*/
  5273. i++;
  5274. switch (proto) {
  5275. case Q_IP:
  5276. b0 = gen_linktype(cstate, ETHERTYPE_IP);
  5277. /* A = ip->ip_p */
  5278. s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
  5279. s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 9;
  5280. i++;
  5281. /* X = ip->ip_hl << 2 */
  5282. s[i] = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);
  5283. s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  5284. i++;
  5285. break;
  5286. case Q_IPV6:
  5287. b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
  5288. /* A = ip6->ip_nxt */
  5289. s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);
  5290. s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 6;
  5291. i++;
  5292. /* X = sizeof(struct ip6_hdr) */
  5293. s[i] = new_stmt(cstate, BPF_LDX|BPF_IMM);
  5294. s[i]->s.k = 40;
  5295. i++;
  5296. break;
  5297. default:
  5298. bpf_error(cstate, "unsupported proto to gen_protochain");
  5299. /*NOTREACHED*/
  5300. }
  5301. /* again: if (A == v) goto end; else fall through; */
  5302. again = i;
  5303. s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
  5304. s[i]->s.k = v;
  5305. s[i]->s.jt = NULL; /*later*/
  5306. s[i]->s.jf = NULL; /*update in next stmt*/
  5307. fix5 = i;
  5308. i++;
  5309. #ifndef IPPROTO_NONE
  5310. #define IPPROTO_NONE 59
  5311. #endif
  5312. /* if (A == IPPROTO_NONE) goto end */
  5313. s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
  5314. s[i]->s.jt = NULL; /*later*/
  5315. s[i]->s.jf = NULL; /*update in next stmt*/
  5316. s[i]->s.k = IPPROTO_NONE;
  5317. s[fix5]->s.jf = s[i];
  5318. fix2 = i;
  5319. i++;
  5320. if (proto == Q_IPV6) {
  5321. int v6start, v6end, v6advance, j;
  5322. v6start = i;
  5323. /* if (A == IPPROTO_HOPOPTS) goto v6advance */
  5324. s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
  5325. s[i]->s.jt = NULL; /*later*/
  5326. s[i]->s.jf = NULL; /*update in next stmt*/
  5327. s[i]->s.k = IPPROTO_HOPOPTS;
  5328. s[fix2]->s.jf = s[i];
  5329. i++;
  5330. /* if (A == IPPROTO_DSTOPTS) goto v6advance */
  5331. s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
  5332. s[i]->s.jt = NULL; /*later*/
  5333. s[i]->s.jf = NULL; /*update in next stmt*/
  5334. s[i]->s.k = IPPROTO_DSTOPTS;
  5335. i++;
  5336. /* if (A == IPPROTO_ROUTING) goto v6advance */
  5337. s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
  5338. s[i]->s.jt = NULL; /*later*/
  5339. s[i]->s.jf = NULL; /*update in next stmt*/
  5340. s[i]->s.k = IPPROTO_ROUTING;
  5341. i++;
  5342. /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
  5343. s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
  5344. s[i]->s.jt = NULL; /*later*/
  5345. s[i]->s.jf = NULL; /*later*/
  5346. s[i]->s.k = IPPROTO_FRAGMENT;
  5347. fix3 = i;
  5348. v6end = i;
  5349. i++;
  5350. /* v6advance: */
  5351. v6advance = i;
  5352. /*
  5353. * in short,
  5354. * A = P[X + packet head];
  5355. * X = X + (P[X + packet head + 1] + 1) * 8;
  5356. */
  5357. /* A = P[X + packet head] */
  5358. s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
  5359. s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  5360. i++;
  5361. /* MEM[reg2] = A */
  5362. s[i] = new_stmt(cstate, BPF_ST);
  5363. s[i]->s.k = reg2;
  5364. i++;
  5365. /* A = P[X + packet head + 1]; */
  5366. s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
  5367. s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 1;
  5368. i++;
  5369. /* A += 1 */
  5370. s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  5371. s[i]->s.k = 1;
  5372. i++;
  5373. /* A *= 8 */
  5374. s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
  5375. s[i]->s.k = 8;
  5376. i++;
  5377. /* A += X */
  5378. s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
  5379. s[i]->s.k = 0;
  5380. i++;
  5381. /* X = A; */
  5382. s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
  5383. i++;
  5384. /* A = MEM[reg2] */
  5385. s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
  5386. s[i]->s.k = reg2;
  5387. i++;
  5388. /* goto again; (must use BPF_JA for backward jump) */
  5389. s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
  5390. s[i]->s.k = again - i - 1;
  5391. s[i - 1]->s.jf = s[i];
  5392. i++;
  5393. /* fixup */
  5394. for (j = v6start; j <= v6end; j++)
  5395. s[j]->s.jt = s[v6advance];
  5396. } else {
  5397. /* nop */
  5398. s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  5399. s[i]->s.k = 0;
  5400. s[fix2]->s.jf = s[i];
  5401. i++;
  5402. }
  5403. /* ahcheck: */
  5404. ahcheck = i;
  5405. /* if (A == IPPROTO_AH) then fall through; else goto end; */
  5406. s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);
  5407. s[i]->s.jt = NULL; /*later*/
  5408. s[i]->s.jf = NULL; /*later*/
  5409. s[i]->s.k = IPPROTO_AH;
  5410. if (fix3)
  5411. s[fix3]->s.jf = s[ahcheck];
  5412. fix4 = i;
  5413. i++;
  5414. /*
  5415. * in short,
  5416. * A = P[X];
  5417. * X = X + (P[X + 1] + 2) * 4;
  5418. */
  5419. /* A = X */
  5420. s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
  5421. i++;
  5422. /* A = P[X + packet head]; */
  5423. s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
  5424. s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  5425. i++;
  5426. /* MEM[reg2] = A */
  5427. s[i] = new_stmt(cstate, BPF_ST);
  5428. s[i]->s.k = reg2;
  5429. i++;
  5430. /* A = X */
  5431. s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);
  5432. i++;
  5433. /* A += 1 */
  5434. s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  5435. s[i]->s.k = 1;
  5436. i++;
  5437. /* X = A */
  5438. s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
  5439. i++;
  5440. /* A = P[X + packet head] */
  5441. s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
  5442. s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  5443. i++;
  5444. /* A += 2 */
  5445. s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  5446. s[i]->s.k = 2;
  5447. i++;
  5448. /* A *= 4 */
  5449. s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
  5450. s[i]->s.k = 4;
  5451. i++;
  5452. /* X = A; */
  5453. s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);
  5454. i++;
  5455. /* A = MEM[reg2] */
  5456. s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);
  5457. s[i]->s.k = reg2;
  5458. i++;
  5459. /* goto again; (must use BPF_JA for backward jump) */
  5460. s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);
  5461. s[i]->s.k = again - i - 1;
  5462. i++;
  5463. /* end: nop */
  5464. end = i;
  5465. s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  5466. s[i]->s.k = 0;
  5467. s[fix2]->s.jt = s[end];
  5468. s[fix4]->s.jf = s[end];
  5469. s[fix5]->s.jt = s[end];
  5470. i++;
  5471. /*
  5472. * make slist chain
  5473. */
  5474. max = i;
  5475. for (i = 0; i < max - 1; i++)
  5476. s[i]->next = s[i + 1];
  5477. s[max - 1]->next = NULL;
  5478. /*
  5479. * emit final check
  5480. */
  5481. b = new_block(cstate, JMP(BPF_JEQ));
  5482. b->stmts = s[1]; /*remember, s[0] is dummy*/
  5483. b->s.k = v;
  5484. free_reg(cstate, reg2);
  5485. gen_and(b0, b);
  5486. return b;
  5487. #endif
  5488. }
  5489. static struct block *
  5490. gen_check_802_11_data_frame(compiler_state_t *cstate)
  5491. {
  5492. struct slist *s;
  5493. struct block *b0, *b1;
  5494. /*
  5495. * A data frame has the 0x08 bit (b3) in the frame control field set
  5496. * and the 0x04 bit (b2) clear.
  5497. */
  5498. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  5499. b0 = new_block(cstate, JMP(BPF_JSET));
  5500. b0->s.k = 0x08;
  5501. b0->stmts = s;
  5502. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  5503. b1 = new_block(cstate, JMP(BPF_JSET));
  5504. b1->s.k = 0x04;
  5505. b1->stmts = s;
  5506. gen_not(b1);
  5507. gen_and(b1, b0);
  5508. return b0;
  5509. }
  5510. /*
  5511. * Generate code that checks whether the packet is a packet for protocol
  5512. * <proto> and whether the type field in that protocol's header has
  5513. * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
  5514. * IP packet and checks the protocol number in the IP header against <v>.
  5515. *
  5516. * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
  5517. * against Q_IP and Q_IPV6.
  5518. */
  5519. static struct block *
  5520. gen_proto(compiler_state_t *cstate, bpf_u_int32 v, int proto, int dir)
  5521. {
  5522. struct block *b0, *b1;
  5523. #ifndef CHASE_CHAIN
  5524. struct block *b2;
  5525. #endif
  5526. if (dir != Q_DEFAULT)
  5527. bpf_error(cstate, "direction applied to 'proto'");
  5528. switch (proto) {
  5529. case Q_DEFAULT:
  5530. b0 = gen_proto(cstate, v, Q_IP, dir);
  5531. b1 = gen_proto(cstate, v, Q_IPV6, dir);
  5532. gen_or(b0, b1);
  5533. return b1;
  5534. case Q_LINK:
  5535. return gen_linktype(cstate, v);
  5536. case Q_IP:
  5537. /*
  5538. * For FDDI, RFC 1188 says that SNAP encapsulation is used,
  5539. * not LLC encapsulation with LLCSAP_IP.
  5540. *
  5541. * For IEEE 802 networks - which includes 802.5 token ring
  5542. * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
  5543. * says that SNAP encapsulation is used, not LLC encapsulation
  5544. * with LLCSAP_IP.
  5545. *
  5546. * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
  5547. * RFC 2225 say that SNAP encapsulation is used, not LLC
  5548. * encapsulation with LLCSAP_IP.
  5549. *
  5550. * So we always check for ETHERTYPE_IP.
  5551. */
  5552. b0 = gen_linktype(cstate, ETHERTYPE_IP);
  5553. #ifndef CHASE_CHAIN
  5554. b1 = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, v);
  5555. #else
  5556. b1 = gen_protochain(cstate, v, Q_IP);
  5557. #endif
  5558. gen_and(b0, b1);
  5559. return b1;
  5560. case Q_ARP:
  5561. bpf_error(cstate, "arp does not encapsulate another protocol");
  5562. /*NOTREACHED*/
  5563. case Q_RARP:
  5564. bpf_error(cstate, "rarp does not encapsulate another protocol");
  5565. /*NOTREACHED*/
  5566. case Q_SCTP:
  5567. bpf_error(cstate, "'sctp proto' is bogus");
  5568. /*NOTREACHED*/
  5569. case Q_TCP:
  5570. bpf_error(cstate, "'tcp proto' is bogus");
  5571. /*NOTREACHED*/
  5572. case Q_UDP:
  5573. bpf_error(cstate, "'udp proto' is bogus");
  5574. /*NOTREACHED*/
  5575. case Q_ICMP:
  5576. bpf_error(cstate, "'icmp proto' is bogus");
  5577. /*NOTREACHED*/
  5578. case Q_IGMP:
  5579. bpf_error(cstate, "'igmp proto' is bogus");
  5580. /*NOTREACHED*/
  5581. case Q_IGRP:
  5582. bpf_error(cstate, "'igrp proto' is bogus");
  5583. /*NOTREACHED*/
  5584. case Q_ATALK:
  5585. bpf_error(cstate, "AppleTalk encapsulation is not specifiable");
  5586. /*NOTREACHED*/
  5587. case Q_DECNET:
  5588. bpf_error(cstate, "DECNET encapsulation is not specifiable");
  5589. /*NOTREACHED*/
  5590. case Q_LAT:
  5591. bpf_error(cstate, "LAT does not encapsulate another protocol");
  5592. /*NOTREACHED*/
  5593. case Q_SCA:
  5594. bpf_error(cstate, "SCA does not encapsulate another protocol");
  5595. /*NOTREACHED*/
  5596. case Q_MOPRC:
  5597. bpf_error(cstate, "MOPRC does not encapsulate another protocol");
  5598. /*NOTREACHED*/
  5599. case Q_MOPDL:
  5600. bpf_error(cstate, "MOPDL does not encapsulate another protocol");
  5601. /*NOTREACHED*/
  5602. case Q_IPV6:
  5603. b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
  5604. #ifndef CHASE_CHAIN
  5605. /*
  5606. * Also check for a fragment header before the final
  5607. * header.
  5608. */
  5609. b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, IPPROTO_FRAGMENT);
  5610. b1 = gen_cmp(cstate, OR_LINKPL, 40, BPF_B, v);
  5611. gen_and(b2, b1);
  5612. b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, v);
  5613. gen_or(b2, b1);
  5614. #else
  5615. b1 = gen_protochain(cstate, v, Q_IPV6);
  5616. #endif
  5617. gen_and(b0, b1);
  5618. return b1;
  5619. case Q_ICMPV6:
  5620. bpf_error(cstate, "'icmp6 proto' is bogus");
  5621. /*NOTREACHED*/
  5622. case Q_AH:
  5623. bpf_error(cstate, "'ah proto' is bogus");
  5624. /*NOTREACHED*/
  5625. case Q_ESP:
  5626. bpf_error(cstate, "'ah proto' is bogus");
  5627. /*NOTREACHED*/
  5628. case Q_PIM:
  5629. bpf_error(cstate, "'pim proto' is bogus");
  5630. /*NOTREACHED*/
  5631. case Q_VRRP:
  5632. bpf_error(cstate, "'vrrp proto' is bogus");
  5633. /*NOTREACHED*/
  5634. case Q_AARP:
  5635. bpf_error(cstate, "'aarp proto' is bogus");
  5636. /*NOTREACHED*/
  5637. case Q_ISO:
  5638. switch (cstate->linktype) {
  5639. case DLT_FRELAY:
  5640. /*
  5641. * Frame Relay packets typically have an OSI
  5642. * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
  5643. * generates code to check for all the OSI
  5644. * NLPIDs, so calling it and then adding a check
  5645. * for the particular NLPID for which we're
  5646. * looking is bogus, as we can just check for
  5647. * the NLPID.
  5648. *
  5649. * What we check for is the NLPID and a frame
  5650. * control field value of UI, i.e. 0x03 followed
  5651. * by the NLPID.
  5652. *
  5653. * XXX - assumes a 2-byte Frame Relay header with
  5654. * DLCI and flags. What if the address is longer?
  5655. *
  5656. * XXX - what about SNAP-encapsulated frames?
  5657. */
  5658. return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | v);
  5659. /*NOTREACHED*/
  5660. case DLT_C_HDLC:
  5661. /*
  5662. * Cisco uses an Ethertype lookalike - for OSI,
  5663. * it's 0xfefe.
  5664. */
  5665. b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS);
  5666. /* OSI in C-HDLC is stuffed with a fudge byte */
  5667. b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, v);
  5668. gen_and(b0, b1);
  5669. return b1;
  5670. default:
  5671. b0 = gen_linktype(cstate, LLCSAP_ISONS);
  5672. b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, v);
  5673. gen_and(b0, b1);
  5674. return b1;
  5675. }
  5676. case Q_ESIS:
  5677. bpf_error(cstate, "'esis proto' is bogus");
  5678. /*NOTREACHED*/
  5679. case Q_ISIS:
  5680. b0 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);
  5681. /*
  5682. * 4 is the offset of the PDU type relative to the IS-IS
  5683. * header.
  5684. */
  5685. b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, v);
  5686. gen_and(b0, b1);
  5687. return b1;
  5688. case Q_CLNP:
  5689. bpf_error(cstate, "'clnp proto' is not supported");
  5690. /*NOTREACHED*/
  5691. case Q_STP:
  5692. bpf_error(cstate, "'stp proto' is bogus");
  5693. /*NOTREACHED*/
  5694. case Q_IPX:
  5695. bpf_error(cstate, "'ipx proto' is bogus");
  5696. /*NOTREACHED*/
  5697. case Q_NETBEUI:
  5698. bpf_error(cstate, "'netbeui proto' is bogus");
  5699. /*NOTREACHED*/
  5700. case Q_ISIS_L1:
  5701. bpf_error(cstate, "'l1 proto' is bogus");
  5702. /*NOTREACHED*/
  5703. case Q_ISIS_L2:
  5704. bpf_error(cstate, "'l2 proto' is bogus");
  5705. /*NOTREACHED*/
  5706. case Q_ISIS_IIH:
  5707. bpf_error(cstate, "'iih proto' is bogus");
  5708. /*NOTREACHED*/
  5709. case Q_ISIS_SNP:
  5710. bpf_error(cstate, "'snp proto' is bogus");
  5711. /*NOTREACHED*/
  5712. case Q_ISIS_CSNP:
  5713. bpf_error(cstate, "'csnp proto' is bogus");
  5714. /*NOTREACHED*/
  5715. case Q_ISIS_PSNP:
  5716. bpf_error(cstate, "'psnp proto' is bogus");
  5717. /*NOTREACHED*/
  5718. case Q_ISIS_LSP:
  5719. bpf_error(cstate, "'lsp proto' is bogus");
  5720. /*NOTREACHED*/
  5721. case Q_RADIO:
  5722. bpf_error(cstate, "'radio proto' is bogus");
  5723. /*NOTREACHED*/
  5724. case Q_CARP:
  5725. bpf_error(cstate, "'carp proto' is bogus");
  5726. /*NOTREACHED*/
  5727. default:
  5728. abort();
  5729. /*NOTREACHED*/
  5730. }
  5731. /*NOTREACHED*/
  5732. }
  5733. struct block *
  5734. gen_scode(compiler_state_t *cstate, const char *name, struct qual q)
  5735. {
  5736. int proto = q.proto;
  5737. int dir = q.dir;
  5738. int tproto;
  5739. u_char *eaddr;
  5740. bpf_u_int32 mask, addr;
  5741. struct addrinfo *res, *res0;
  5742. struct sockaddr_in *sin4;
  5743. #ifdef INET6
  5744. int tproto6;
  5745. struct sockaddr_in6 *sin6;
  5746. struct in6_addr mask128;
  5747. #endif /*INET6*/
  5748. struct block *b, *tmp;
  5749. int port, real_proto;
  5750. int port1, port2;
  5751. /*
  5752. * Catch errors reported by us and routines below us, and return NULL
  5753. * on an error.
  5754. */
  5755. if (setjmp(cstate->top_ctx))
  5756. return (NULL);
  5757. switch (q.addr) {
  5758. case Q_NET:
  5759. addr = pcap_nametonetaddr(name);
  5760. if (addr == 0)
  5761. bpf_error(cstate, "unknown network '%s'", name);
  5762. /* Left justify network addr and calculate its network mask */
  5763. mask = 0xffffffff;
  5764. while (addr && (addr & 0xff000000) == 0) {
  5765. addr <<= 8;
  5766. mask <<= 8;
  5767. }
  5768. return gen_host(cstate, addr, mask, proto, dir, q.addr);
  5769. case Q_DEFAULT:
  5770. case Q_HOST:
  5771. if (proto == Q_LINK) {
  5772. switch (cstate->linktype) {
  5773. case DLT_EN10MB:
  5774. case DLT_NETANALYZER:
  5775. case DLT_NETANALYZER_TRANSPARENT:
  5776. eaddr = pcap_ether_hostton(name);
  5777. if (eaddr == NULL)
  5778. bpf_error(cstate,
  5779. "unknown ether host '%s'", name);
  5780. tmp = gen_prevlinkhdr_check(cstate);
  5781. b = gen_ehostop(cstate, eaddr, dir);
  5782. if (tmp != NULL)
  5783. gen_and(tmp, b);
  5784. free(eaddr);
  5785. return b;
  5786. case DLT_FDDI:
  5787. eaddr = pcap_ether_hostton(name);
  5788. if (eaddr == NULL)
  5789. bpf_error(cstate,
  5790. "unknown FDDI host '%s'", name);
  5791. b = gen_fhostop(cstate, eaddr, dir);
  5792. free(eaddr);
  5793. return b;
  5794. case DLT_IEEE802:
  5795. eaddr = pcap_ether_hostton(name);
  5796. if (eaddr == NULL)
  5797. bpf_error(cstate,
  5798. "unknown token ring host '%s'", name);
  5799. b = gen_thostop(cstate, eaddr, dir);
  5800. free(eaddr);
  5801. return b;
  5802. case DLT_IEEE802_11:
  5803. case DLT_PRISM_HEADER:
  5804. case DLT_IEEE802_11_RADIO_AVS:
  5805. case DLT_IEEE802_11_RADIO:
  5806. case DLT_PPI:
  5807. eaddr = pcap_ether_hostton(name);
  5808. if (eaddr == NULL)
  5809. bpf_error(cstate,
  5810. "unknown 802.11 host '%s'", name);
  5811. b = gen_wlanhostop(cstate, eaddr, dir);
  5812. free(eaddr);
  5813. return b;
  5814. case DLT_IP_OVER_FC:
  5815. eaddr = pcap_ether_hostton(name);
  5816. if (eaddr == NULL)
  5817. bpf_error(cstate,
  5818. "unknown Fibre Channel host '%s'", name);
  5819. b = gen_ipfchostop(cstate, eaddr, dir);
  5820. free(eaddr);
  5821. return b;
  5822. }
  5823. bpf_error(cstate, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
  5824. } else if (proto == Q_DECNET) {
  5825. unsigned short dn_addr;
  5826. if (!__pcap_nametodnaddr(name, &dn_addr)) {
  5827. #ifdef DECNETLIB
  5828. bpf_error(cstate, "unknown decnet host name '%s'\n", name);
  5829. #else
  5830. bpf_error(cstate, "decnet name support not included, '%s' cannot be translated\n",
  5831. name);
  5832. #endif
  5833. }
  5834. /*
  5835. * I don't think DECNET hosts can be multihomed, so
  5836. * there is no need to build up a list of addresses
  5837. */
  5838. return (gen_host(cstate, dn_addr, 0, proto, dir, q.addr));
  5839. } else {
  5840. #ifdef INET6
  5841. memset(&mask128, 0xff, sizeof(mask128));
  5842. #endif
  5843. res0 = res = pcap_nametoaddrinfo(name);
  5844. if (res == NULL)
  5845. bpf_error(cstate, "unknown host '%s'", name);
  5846. cstate->ai = res;
  5847. b = tmp = NULL;
  5848. tproto = proto;
  5849. #ifdef INET6
  5850. tproto6 = proto;
  5851. #endif
  5852. if (cstate->off_linktype.constant_part == OFFSET_NOT_SET &&
  5853. tproto == Q_DEFAULT) {
  5854. tproto = Q_IP;
  5855. #ifdef INET6
  5856. tproto6 = Q_IPV6;
  5857. #endif
  5858. }
  5859. for (res = res0; res; res = res->ai_next) {
  5860. switch (res->ai_family) {
  5861. case AF_INET:
  5862. #ifdef INET6
  5863. if (tproto == Q_IPV6)
  5864. continue;
  5865. #endif
  5866. sin4 = (struct sockaddr_in *)
  5867. res->ai_addr;
  5868. tmp = gen_host(cstate, ntohl(sin4->sin_addr.s_addr),
  5869. 0xffffffff, tproto, dir, q.addr);
  5870. break;
  5871. #ifdef INET6
  5872. case AF_INET6:
  5873. if (tproto6 == Q_IP)
  5874. continue;
  5875. sin6 = (struct sockaddr_in6 *)
  5876. res->ai_addr;
  5877. tmp = gen_host6(cstate, &sin6->sin6_addr,
  5878. &mask128, tproto6, dir, q.addr);
  5879. break;
  5880. #endif
  5881. default:
  5882. continue;
  5883. }
  5884. if (b)
  5885. gen_or(b, tmp);
  5886. b = tmp;
  5887. }
  5888. cstate->ai = NULL;
  5889. freeaddrinfo(res0);
  5890. if (b == NULL) {
  5891. bpf_error(cstate, "unknown host '%s'%s", name,
  5892. (proto == Q_DEFAULT)
  5893. ? ""
  5894. : " for specified address family");
  5895. }
  5896. return b;
  5897. }
  5898. case Q_PORT:
  5899. if (proto != Q_DEFAULT &&
  5900. proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
  5901. bpf_error(cstate, "illegal qualifier of 'port'");
  5902. if (pcap_nametoport(name, &port, &real_proto) == 0)
  5903. bpf_error(cstate, "unknown port '%s'", name);
  5904. if (proto == Q_UDP) {
  5905. if (real_proto == IPPROTO_TCP)
  5906. bpf_error(cstate, "port '%s' is tcp", name);
  5907. else if (real_proto == IPPROTO_SCTP)
  5908. bpf_error(cstate, "port '%s' is sctp", name);
  5909. else
  5910. /* override PROTO_UNDEF */
  5911. real_proto = IPPROTO_UDP;
  5912. }
  5913. if (proto == Q_TCP) {
  5914. if (real_proto == IPPROTO_UDP)
  5915. bpf_error(cstate, "port '%s' is udp", name);
  5916. else if (real_proto == IPPROTO_SCTP)
  5917. bpf_error(cstate, "port '%s' is sctp", name);
  5918. else
  5919. /* override PROTO_UNDEF */
  5920. real_proto = IPPROTO_TCP;
  5921. }
  5922. if (proto == Q_SCTP) {
  5923. if (real_proto == IPPROTO_UDP)
  5924. bpf_error(cstate, "port '%s' is udp", name);
  5925. else if (real_proto == IPPROTO_TCP)
  5926. bpf_error(cstate, "port '%s' is tcp", name);
  5927. else
  5928. /* override PROTO_UNDEF */
  5929. real_proto = IPPROTO_SCTP;
  5930. }
  5931. if (port < 0)
  5932. bpf_error(cstate, "illegal port number %d < 0", port);
  5933. if (port > 65535)
  5934. bpf_error(cstate, "illegal port number %d > 65535", port);
  5935. b = gen_port(cstate, port, real_proto, dir);
  5936. gen_or(gen_port6(cstate, port, real_proto, dir), b);
  5937. return b;
  5938. case Q_PORTRANGE:
  5939. if (proto != Q_DEFAULT &&
  5940. proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
  5941. bpf_error(cstate, "illegal qualifier of 'portrange'");
  5942. if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
  5943. bpf_error(cstate, "unknown port in range '%s'", name);
  5944. if (proto == Q_UDP) {
  5945. if (real_proto == IPPROTO_TCP)
  5946. bpf_error(cstate, "port in range '%s' is tcp", name);
  5947. else if (real_proto == IPPROTO_SCTP)
  5948. bpf_error(cstate, "port in range '%s' is sctp", name);
  5949. else
  5950. /* override PROTO_UNDEF */
  5951. real_proto = IPPROTO_UDP;
  5952. }
  5953. if (proto == Q_TCP) {
  5954. if (real_proto == IPPROTO_UDP)
  5955. bpf_error(cstate, "port in range '%s' is udp", name);
  5956. else if (real_proto == IPPROTO_SCTP)
  5957. bpf_error(cstate, "port in range '%s' is sctp", name);
  5958. else
  5959. /* override PROTO_UNDEF */
  5960. real_proto = IPPROTO_TCP;
  5961. }
  5962. if (proto == Q_SCTP) {
  5963. if (real_proto == IPPROTO_UDP)
  5964. bpf_error(cstate, "port in range '%s' is udp", name);
  5965. else if (real_proto == IPPROTO_TCP)
  5966. bpf_error(cstate, "port in range '%s' is tcp", name);
  5967. else
  5968. /* override PROTO_UNDEF */
  5969. real_proto = IPPROTO_SCTP;
  5970. }
  5971. if (port1 < 0)
  5972. bpf_error(cstate, "illegal port number %d < 0", port1);
  5973. if (port1 > 65535)
  5974. bpf_error(cstate, "illegal port number %d > 65535", port1);
  5975. if (port2 < 0)
  5976. bpf_error(cstate, "illegal port number %d < 0", port2);
  5977. if (port2 > 65535)
  5978. bpf_error(cstate, "illegal port number %d > 65535", port2);
  5979. b = gen_portrange(cstate, port1, port2, real_proto, dir);
  5980. gen_or(gen_portrange6(cstate, port1, port2, real_proto, dir), b);
  5981. return b;
  5982. case Q_GATEWAY:
  5983. #ifndef INET6
  5984. eaddr = pcap_ether_hostton(name);
  5985. if (eaddr == NULL)
  5986. bpf_error(cstate, "unknown ether host: %s", name);
  5987. res = pcap_nametoaddrinfo(name);
  5988. cstate->ai = res;
  5989. if (res == NULL)
  5990. bpf_error(cstate, "unknown host '%s'", name);
  5991. b = gen_gateway(cstate, eaddr, res, proto, dir);
  5992. cstate->ai = NULL;
  5993. freeaddrinfo(res);
  5994. if (b == NULL)
  5995. bpf_error(cstate, "unknown host '%s'", name);
  5996. return b;
  5997. #else
  5998. bpf_error(cstate, "'gateway' not supported in this configuration");
  5999. #endif /*INET6*/
  6000. case Q_PROTO:
  6001. real_proto = lookup_proto(cstate, name, proto);
  6002. if (real_proto >= 0)
  6003. return gen_proto(cstate, real_proto, proto, dir);
  6004. else
  6005. bpf_error(cstate, "unknown protocol: %s", name);
  6006. case Q_PROTOCHAIN:
  6007. real_proto = lookup_proto(cstate, name, proto);
  6008. if (real_proto >= 0)
  6009. return gen_protochain(cstate, real_proto, proto);
  6010. else
  6011. bpf_error(cstate, "unknown protocol: %s", name);
  6012. case Q_UNDEF:
  6013. syntax(cstate);
  6014. /*NOTREACHED*/
  6015. }
  6016. abort();
  6017. /*NOTREACHED*/
  6018. }
  6019. struct block *
  6020. gen_mcode(compiler_state_t *cstate, const char *s1, const char *s2,
  6021. bpf_u_int32 masklen, struct qual q)
  6022. {
  6023. register int nlen, mlen;
  6024. bpf_u_int32 n, m;
  6025. /*
  6026. * Catch errors reported by us and routines below us, and return NULL
  6027. * on an error.
  6028. */
  6029. if (setjmp(cstate->top_ctx))
  6030. return (NULL);
  6031. nlen = __pcap_atoin(s1, &n);
  6032. if (nlen < 0)
  6033. bpf_error(cstate, "invalid IPv4 address '%s'", s1);
  6034. /* Promote short ipaddr */
  6035. n <<= 32 - nlen;
  6036. if (s2 != NULL) {
  6037. mlen = __pcap_atoin(s2, &m);
  6038. if (mlen < 0)
  6039. bpf_error(cstate, "invalid IPv4 address '%s'", s2);
  6040. /* Promote short ipaddr */
  6041. m <<= 32 - mlen;
  6042. if ((n & ~m) != 0)
  6043. bpf_error(cstate, "non-network bits set in \"%s mask %s\"",
  6044. s1, s2);
  6045. } else {
  6046. /* Convert mask len to mask */
  6047. if (masklen > 32)
  6048. bpf_error(cstate, "mask length must be <= 32");
  6049. if (masklen == 0) {
  6050. /*
  6051. * X << 32 is not guaranteed by C to be 0; it's
  6052. * undefined.
  6053. */
  6054. m = 0;
  6055. } else
  6056. m = 0xffffffff << (32 - masklen);
  6057. if ((n & ~m) != 0)
  6058. bpf_error(cstate, "non-network bits set in \"%s/%d\"",
  6059. s1, masklen);
  6060. }
  6061. switch (q.addr) {
  6062. case Q_NET:
  6063. return gen_host(cstate, n, m, q.proto, q.dir, q.addr);
  6064. default:
  6065. bpf_error(cstate, "Mask syntax for networks only");
  6066. /*NOTREACHED*/
  6067. }
  6068. /*NOTREACHED*/
  6069. }
  6070. struct block *
  6071. gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q)
  6072. {
  6073. bpf_u_int32 mask;
  6074. int proto;
  6075. int dir;
  6076. register int vlen;
  6077. /*
  6078. * Catch errors reported by us and routines below us, and return NULL
  6079. * on an error.
  6080. */
  6081. if (setjmp(cstate->top_ctx))
  6082. return (NULL);
  6083. proto = q.proto;
  6084. dir = q.dir;
  6085. if (s == NULL)
  6086. vlen = 32;
  6087. else if (q.proto == Q_DECNET) {
  6088. vlen = __pcap_atodn(s, &v);
  6089. if (vlen == 0)
  6090. bpf_error(cstate, "malformed decnet address '%s'", s);
  6091. } else {
  6092. vlen = __pcap_atoin(s, &v);
  6093. if (vlen < 0)
  6094. bpf_error(cstate, "invalid IPv4 address '%s'", s);
  6095. }
  6096. switch (q.addr) {
  6097. case Q_DEFAULT:
  6098. case Q_HOST:
  6099. case Q_NET:
  6100. if (proto == Q_DECNET)
  6101. return gen_host(cstate, v, 0, proto, dir, q.addr);
  6102. else if (proto == Q_LINK) {
  6103. bpf_error(cstate, "illegal link layer address");
  6104. } else {
  6105. mask = 0xffffffff;
  6106. if (s == NULL && q.addr == Q_NET) {
  6107. /* Promote short net number */
  6108. while (v && (v & 0xff000000) == 0) {
  6109. v <<= 8;
  6110. mask <<= 8;
  6111. }
  6112. } else {
  6113. /* Promote short ipaddr */
  6114. v <<= 32 - vlen;
  6115. mask <<= 32 - vlen ;
  6116. }
  6117. return gen_host(cstate, v, mask, proto, dir, q.addr);
  6118. }
  6119. case Q_PORT:
  6120. if (proto == Q_UDP)
  6121. proto = IPPROTO_UDP;
  6122. else if (proto == Q_TCP)
  6123. proto = IPPROTO_TCP;
  6124. else if (proto == Q_SCTP)
  6125. proto = IPPROTO_SCTP;
  6126. else if (proto == Q_DEFAULT)
  6127. proto = PROTO_UNDEF;
  6128. else
  6129. bpf_error(cstate, "illegal qualifier of 'port'");
  6130. if (v > 65535)
  6131. bpf_error(cstate, "illegal port number %u > 65535", v);
  6132. {
  6133. struct block *b;
  6134. b = gen_port(cstate, v, proto, dir);
  6135. gen_or(gen_port6(cstate, v, proto, dir), b);
  6136. return b;
  6137. }
  6138. case Q_PORTRANGE:
  6139. if (proto == Q_UDP)
  6140. proto = IPPROTO_UDP;
  6141. else if (proto == Q_TCP)
  6142. proto = IPPROTO_TCP;
  6143. else if (proto == Q_SCTP)
  6144. proto = IPPROTO_SCTP;
  6145. else if (proto == Q_DEFAULT)
  6146. proto = PROTO_UNDEF;
  6147. else
  6148. bpf_error(cstate, "illegal qualifier of 'portrange'");
  6149. if (v > 65535)
  6150. bpf_error(cstate, "illegal port number %u > 65535", v);
  6151. {
  6152. struct block *b;
  6153. b = gen_portrange(cstate, v, v, proto, dir);
  6154. gen_or(gen_portrange6(cstate, v, v, proto, dir), b);
  6155. return b;
  6156. }
  6157. case Q_GATEWAY:
  6158. bpf_error(cstate, "'gateway' requires a name");
  6159. /*NOTREACHED*/
  6160. case Q_PROTO:
  6161. return gen_proto(cstate, v, proto, dir);
  6162. case Q_PROTOCHAIN:
  6163. return gen_protochain(cstate, v, proto);
  6164. case Q_UNDEF:
  6165. syntax(cstate);
  6166. /*NOTREACHED*/
  6167. default:
  6168. abort();
  6169. /*NOTREACHED*/
  6170. }
  6171. /*NOTREACHED*/
  6172. }
  6173. #ifdef INET6
  6174. struct block *
  6175. gen_mcode6(compiler_state_t *cstate, const char *s1, const char *s2,
  6176. bpf_u_int32 masklen, struct qual q)
  6177. {
  6178. struct addrinfo *res;
  6179. struct in6_addr *addr;
  6180. struct in6_addr mask;
  6181. struct block *b;
  6182. uint32_t *a, *m;
  6183. /*
  6184. * Catch errors reported by us and routines below us, and return NULL
  6185. * on an error.
  6186. */
  6187. if (setjmp(cstate->top_ctx))
  6188. return (NULL);
  6189. if (s2)
  6190. bpf_error(cstate, "no mask %s supported", s2);
  6191. res = pcap_nametoaddrinfo(s1);
  6192. if (!res)
  6193. bpf_error(cstate, "invalid ip6 address %s", s1);
  6194. cstate->ai = res;
  6195. if (res->ai_next)
  6196. bpf_error(cstate, "%s resolved to multiple address", s1);
  6197. addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
  6198. if (masklen > sizeof(mask.s6_addr) * 8)
  6199. bpf_error(cstate, "mask length must be <= %u", (unsigned int)(sizeof(mask.s6_addr) * 8));
  6200. memset(&mask, 0, sizeof(mask));
  6201. memset(&mask.s6_addr, 0xff, masklen / 8);
  6202. if (masklen % 8) {
  6203. mask.s6_addr[masklen / 8] =
  6204. (0xff << (8 - masklen % 8)) & 0xff;
  6205. }
  6206. a = (uint32_t *)addr;
  6207. m = (uint32_t *)&mask;
  6208. if ((a[0] & ~m[0]) || (a[1] & ~m[1])
  6209. || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
  6210. bpf_error(cstate, "non-network bits set in \"%s/%d\"", s1, masklen);
  6211. }
  6212. switch (q.addr) {
  6213. case Q_DEFAULT:
  6214. case Q_HOST:
  6215. if (masklen != 128)
  6216. bpf_error(cstate, "Mask syntax for networks only");
  6217. /* FALLTHROUGH */
  6218. case Q_NET:
  6219. b = gen_host6(cstate, addr, &mask, q.proto, q.dir, q.addr);
  6220. cstate->ai = NULL;
  6221. freeaddrinfo(res);
  6222. return b;
  6223. default:
  6224. bpf_error(cstate, "invalid qualifier against IPv6 address");
  6225. /*NOTREACHED*/
  6226. }
  6227. }
  6228. #endif /*INET6*/
  6229. struct block *
  6230. gen_ecode(compiler_state_t *cstate, const char *s, struct qual q)
  6231. {
  6232. struct block *b, *tmp;
  6233. /*
  6234. * Catch errors reported by us and routines below us, and return NULL
  6235. * on an error.
  6236. */
  6237. if (setjmp(cstate->top_ctx))
  6238. return (NULL);
  6239. if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
  6240. cstate->e = pcap_ether_aton(s);
  6241. if (cstate->e == NULL)
  6242. bpf_error(cstate, "malloc");
  6243. switch (cstate->linktype) {
  6244. case DLT_EN10MB:
  6245. case DLT_NETANALYZER:
  6246. case DLT_NETANALYZER_TRANSPARENT:
  6247. tmp = gen_prevlinkhdr_check(cstate);
  6248. b = gen_ehostop(cstate, cstate->e, (int)q.dir);
  6249. if (tmp != NULL)
  6250. gen_and(tmp, b);
  6251. break;
  6252. case DLT_FDDI:
  6253. b = gen_fhostop(cstate, cstate->e, (int)q.dir);
  6254. break;
  6255. case DLT_IEEE802:
  6256. b = gen_thostop(cstate, cstate->e, (int)q.dir);
  6257. break;
  6258. case DLT_IEEE802_11:
  6259. case DLT_PRISM_HEADER:
  6260. case DLT_IEEE802_11_RADIO_AVS:
  6261. case DLT_IEEE802_11_RADIO:
  6262. case DLT_PPI:
  6263. b = gen_wlanhostop(cstate, cstate->e, (int)q.dir);
  6264. break;
  6265. case DLT_IP_OVER_FC:
  6266. b = gen_ipfchostop(cstate, cstate->e, (int)q.dir);
  6267. break;
  6268. default:
  6269. free(cstate->e);
  6270. cstate->e = NULL;
  6271. bpf_error(cstate, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
  6272. /*NOTREACHED*/
  6273. }
  6274. free(cstate->e);
  6275. cstate->e = NULL;
  6276. return (b);
  6277. }
  6278. bpf_error(cstate, "ethernet address used in non-ether expression");
  6279. /*NOTREACHED*/
  6280. }
  6281. void
  6282. sappend(struct slist *s0, struct slist *s1)
  6283. {
  6284. /*
  6285. * This is definitely not the best way to do this, but the
  6286. * lists will rarely get long.
  6287. */
  6288. while (s0->next)
  6289. s0 = s0->next;
  6290. s0->next = s1;
  6291. }
  6292. static struct slist *
  6293. xfer_to_x(compiler_state_t *cstate, struct arth *a)
  6294. {
  6295. struct slist *s;
  6296. s = new_stmt(cstate, BPF_LDX|BPF_MEM);
  6297. s->s.k = a->regno;
  6298. return s;
  6299. }
  6300. static struct slist *
  6301. xfer_to_a(compiler_state_t *cstate, struct arth *a)
  6302. {
  6303. struct slist *s;
  6304. s = new_stmt(cstate, BPF_LD|BPF_MEM);
  6305. s->s.k = a->regno;
  6306. return s;
  6307. }
  6308. /*
  6309. * Modify "index" to use the value stored into its register as an
  6310. * offset relative to the beginning of the header for the protocol
  6311. * "proto", and allocate a register and put an item "size" bytes long
  6312. * (1, 2, or 4) at that offset into that register, making it the register
  6313. * for "index".
  6314. */
  6315. static struct arth *
  6316. gen_load_internal(compiler_state_t *cstate, int proto, struct arth *inst,
  6317. bpf_u_int32 size)
  6318. {
  6319. int size_code;
  6320. struct slist *s, *tmp;
  6321. struct block *b;
  6322. int regno = alloc_reg(cstate);
  6323. free_reg(cstate, inst->regno);
  6324. switch (size) {
  6325. default:
  6326. bpf_error(cstate, "data size must be 1, 2, or 4");
  6327. /*NOTREACHED*/
  6328. case 1:
  6329. size_code = BPF_B;
  6330. break;
  6331. case 2:
  6332. size_code = BPF_H;
  6333. break;
  6334. case 4:
  6335. size_code = BPF_W;
  6336. break;
  6337. }
  6338. switch (proto) {
  6339. default:
  6340. bpf_error(cstate, "unsupported index operation");
  6341. case Q_RADIO:
  6342. /*
  6343. * The offset is relative to the beginning of the packet
  6344. * data, if we have a radio header. (If we don't, this
  6345. * is an error.)
  6346. */
  6347. if (cstate->linktype != DLT_IEEE802_11_RADIO_AVS &&
  6348. cstate->linktype != DLT_IEEE802_11_RADIO &&
  6349. cstate->linktype != DLT_PRISM_HEADER)
  6350. bpf_error(cstate, "radio information not present in capture");
  6351. /*
  6352. * Load into the X register the offset computed into the
  6353. * register specified by "index".
  6354. */
  6355. s = xfer_to_x(cstate, inst);
  6356. /*
  6357. * Load the item at that offset.
  6358. */
  6359. tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
  6360. sappend(s, tmp);
  6361. sappend(inst->s, s);
  6362. break;
  6363. case Q_LINK:
  6364. /*
  6365. * The offset is relative to the beginning of
  6366. * the link-layer header.
  6367. *
  6368. * XXX - what about ATM LANE? Should the index be
  6369. * relative to the beginning of the AAL5 frame, so
  6370. * that 0 refers to the beginning of the LE Control
  6371. * field, or relative to the beginning of the LAN
  6372. * frame, so that 0 refers, for Ethernet LANE, to
  6373. * the beginning of the destination address?
  6374. */
  6375. s = gen_abs_offset_varpart(cstate, &cstate->off_linkhdr);
  6376. /*
  6377. * If "s" is non-null, it has code to arrange that the
  6378. * X register contains the length of the prefix preceding
  6379. * the link-layer header. Add to it the offset computed
  6380. * into the register specified by "index", and move that
  6381. * into the X register. Otherwise, just load into the X
  6382. * register the offset computed into the register specified
  6383. * by "index".
  6384. */
  6385. if (s != NULL) {
  6386. sappend(s, xfer_to_a(cstate, inst));
  6387. sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
  6388. sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
  6389. } else
  6390. s = xfer_to_x(cstate, inst);
  6391. /*
  6392. * Load the item at the sum of the offset we've put in the
  6393. * X register and the offset of the start of the link
  6394. * layer header (which is 0 if the radio header is
  6395. * variable-length; that header length is what we put
  6396. * into the X register and then added to the index).
  6397. */
  6398. tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
  6399. tmp->s.k = cstate->off_linkhdr.constant_part;
  6400. sappend(s, tmp);
  6401. sappend(inst->s, s);
  6402. break;
  6403. case Q_IP:
  6404. case Q_ARP:
  6405. case Q_RARP:
  6406. case Q_ATALK:
  6407. case Q_DECNET:
  6408. case Q_SCA:
  6409. case Q_LAT:
  6410. case Q_MOPRC:
  6411. case Q_MOPDL:
  6412. case Q_IPV6:
  6413. /*
  6414. * The offset is relative to the beginning of
  6415. * the network-layer header.
  6416. * XXX - are there any cases where we want
  6417. * cstate->off_nl_nosnap?
  6418. */
  6419. s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
  6420. /*
  6421. * If "s" is non-null, it has code to arrange that the
  6422. * X register contains the variable part of the offset
  6423. * of the link-layer payload. Add to it the offset
  6424. * computed into the register specified by "index",
  6425. * and move that into the X register. Otherwise, just
  6426. * load into the X register the offset computed into
  6427. * the register specified by "index".
  6428. */
  6429. if (s != NULL) {
  6430. sappend(s, xfer_to_a(cstate, inst));
  6431. sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
  6432. sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
  6433. } else
  6434. s = xfer_to_x(cstate, inst);
  6435. /*
  6436. * Load the item at the sum of the offset we've put in the
  6437. * X register, the offset of the start of the network
  6438. * layer header from the beginning of the link-layer
  6439. * payload, and the constant part of the offset of the
  6440. * start of the link-layer payload.
  6441. */
  6442. tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
  6443. tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  6444. sappend(s, tmp);
  6445. sappend(inst->s, s);
  6446. /*
  6447. * Do the computation only if the packet contains
  6448. * the protocol in question.
  6449. */
  6450. b = gen_proto_abbrev_internal(cstate, proto);
  6451. if (inst->b)
  6452. gen_and(inst->b, b);
  6453. inst->b = b;
  6454. break;
  6455. case Q_SCTP:
  6456. case Q_TCP:
  6457. case Q_UDP:
  6458. case Q_ICMP:
  6459. case Q_IGMP:
  6460. case Q_IGRP:
  6461. case Q_PIM:
  6462. case Q_VRRP:
  6463. case Q_CARP:
  6464. /*
  6465. * The offset is relative to the beginning of
  6466. * the transport-layer header.
  6467. *
  6468. * Load the X register with the length of the IPv4 header
  6469. * (plus the offset of the link-layer header, if it's
  6470. * a variable-length header), in bytes.
  6471. *
  6472. * XXX - are there any cases where we want
  6473. * cstate->off_nl_nosnap?
  6474. * XXX - we should, if we're built with
  6475. * IPv6 support, generate code to load either
  6476. * IPv4, IPv6, or both, as appropriate.
  6477. */
  6478. s = gen_loadx_iphdrlen(cstate);
  6479. /*
  6480. * The X register now contains the sum of the variable
  6481. * part of the offset of the link-layer payload and the
  6482. * length of the network-layer header.
  6483. *
  6484. * Load into the A register the offset relative to
  6485. * the beginning of the transport layer header,
  6486. * add the X register to that, move that to the
  6487. * X register, and load with an offset from the
  6488. * X register equal to the sum of the constant part of
  6489. * the offset of the link-layer payload and the offset,
  6490. * relative to the beginning of the link-layer payload,
  6491. * of the network-layer header.
  6492. */
  6493. sappend(s, xfer_to_a(cstate, inst));
  6494. sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
  6495. sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
  6496. sappend(s, tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code));
  6497. tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;
  6498. sappend(inst->s, s);
  6499. /*
  6500. * Do the computation only if the packet contains
  6501. * the protocol in question - which is true only
  6502. * if this is an IP datagram and is the first or
  6503. * only fragment of that datagram.
  6504. */
  6505. gen_and(gen_proto_abbrev_internal(cstate, proto), b = gen_ipfrag(cstate));
  6506. if (inst->b)
  6507. gen_and(inst->b, b);
  6508. gen_and(gen_proto_abbrev_internal(cstate, Q_IP), b);
  6509. inst->b = b;
  6510. break;
  6511. case Q_ICMPV6:
  6512. /*
  6513. * Do the computation only if the packet contains
  6514. * the protocol in question.
  6515. */
  6516. b = gen_proto_abbrev_internal(cstate, Q_IPV6);
  6517. if (inst->b) {
  6518. gen_and(inst->b, b);
  6519. }
  6520. inst->b = b;
  6521. /*
  6522. * Check if we have an icmp6 next header
  6523. */
  6524. b = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, 58);
  6525. if (inst->b) {
  6526. gen_and(inst->b, b);
  6527. }
  6528. inst->b = b;
  6529. s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
  6530. /*
  6531. * If "s" is non-null, it has code to arrange that the
  6532. * X register contains the variable part of the offset
  6533. * of the link-layer payload. Add to it the offset
  6534. * computed into the register specified by "index",
  6535. * and move that into the X register. Otherwise, just
  6536. * load into the X register the offset computed into
  6537. * the register specified by "index".
  6538. */
  6539. if (s != NULL) {
  6540. sappend(s, xfer_to_a(cstate, inst));
  6541. sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));
  6542. sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));
  6543. } else {
  6544. s = xfer_to_x(cstate, inst);
  6545. }
  6546. /*
  6547. * Load the item at the sum of the offset we've put in the
  6548. * X register, the offset of the start of the network
  6549. * layer header from the beginning of the link-layer
  6550. * payload, and the constant part of the offset of the
  6551. * start of the link-layer payload.
  6552. */
  6553. tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);
  6554. tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 40;
  6555. sappend(s, tmp);
  6556. sappend(inst->s, s);
  6557. break;
  6558. }
  6559. inst->regno = regno;
  6560. s = new_stmt(cstate, BPF_ST);
  6561. s->s.k = regno;
  6562. sappend(inst->s, s);
  6563. return inst;
  6564. }
  6565. struct arth *
  6566. gen_load(compiler_state_t *cstate, int proto, struct arth *inst,
  6567. bpf_u_int32 size)
  6568. {
  6569. /*
  6570. * Catch errors reported by us and routines below us, and return NULL
  6571. * on an error.
  6572. */
  6573. if (setjmp(cstate->top_ctx))
  6574. return (NULL);
  6575. return gen_load_internal(cstate, proto, inst, size);
  6576. }
  6577. static struct block *
  6578. gen_relation_internal(compiler_state_t *cstate, int code, struct arth *a0,
  6579. struct arth *a1, int reversed)
  6580. {
  6581. struct slist *s0, *s1, *s2;
  6582. struct block *b, *tmp;
  6583. s0 = xfer_to_x(cstate, a1);
  6584. s1 = xfer_to_a(cstate, a0);
  6585. if (code == BPF_JEQ) {
  6586. s2 = new_stmt(cstate, BPF_ALU|BPF_SUB|BPF_X);
  6587. b = new_block(cstate, JMP(code));
  6588. sappend(s1, s2);
  6589. }
  6590. else
  6591. b = new_block(cstate, BPF_JMP|code|BPF_X);
  6592. if (reversed)
  6593. gen_not(b);
  6594. sappend(s0, s1);
  6595. sappend(a1->s, s0);
  6596. sappend(a0->s, a1->s);
  6597. b->stmts = a0->s;
  6598. free_reg(cstate, a0->regno);
  6599. free_reg(cstate, a1->regno);
  6600. /* 'and' together protocol checks */
  6601. if (a0->b) {
  6602. if (a1->b) {
  6603. gen_and(a0->b, tmp = a1->b);
  6604. }
  6605. else
  6606. tmp = a0->b;
  6607. } else
  6608. tmp = a1->b;
  6609. if (tmp)
  6610. gen_and(tmp, b);
  6611. return b;
  6612. }
  6613. struct block *
  6614. gen_relation(compiler_state_t *cstate, int code, struct arth *a0,
  6615. struct arth *a1, int reversed)
  6616. {
  6617. /*
  6618. * Catch errors reported by us and routines below us, and return NULL
  6619. * on an error.
  6620. */
  6621. if (setjmp(cstate->top_ctx))
  6622. return (NULL);
  6623. return gen_relation_internal(cstate, code, a0, a1, reversed);
  6624. }
  6625. struct arth *
  6626. gen_loadlen(compiler_state_t *cstate)
  6627. {
  6628. int regno;
  6629. struct arth *a;
  6630. struct slist *s;
  6631. /*
  6632. * Catch errors reported by us and routines below us, and return NULL
  6633. * on an error.
  6634. */
  6635. if (setjmp(cstate->top_ctx))
  6636. return (NULL);
  6637. regno = alloc_reg(cstate);
  6638. a = (struct arth *)newchunk(cstate, sizeof(*a));
  6639. s = new_stmt(cstate, BPF_LD|BPF_LEN);
  6640. s->next = new_stmt(cstate, BPF_ST);
  6641. s->next->s.k = regno;
  6642. a->s = s;
  6643. a->regno = regno;
  6644. return a;
  6645. }
  6646. static struct arth *
  6647. gen_loadi_internal(compiler_state_t *cstate, bpf_u_int32 val)
  6648. {
  6649. struct arth *a;
  6650. struct slist *s;
  6651. int reg;
  6652. a = (struct arth *)newchunk(cstate, sizeof(*a));
  6653. reg = alloc_reg(cstate);
  6654. s = new_stmt(cstate, BPF_LD|BPF_IMM);
  6655. s->s.k = val;
  6656. s->next = new_stmt(cstate, BPF_ST);
  6657. s->next->s.k = reg;
  6658. a->s = s;
  6659. a->regno = reg;
  6660. return a;
  6661. }
  6662. struct arth *
  6663. gen_loadi(compiler_state_t *cstate, bpf_u_int32 val)
  6664. {
  6665. /*
  6666. * Catch errors reported by us and routines below us, and return NULL
  6667. * on an error.
  6668. */
  6669. if (setjmp(cstate->top_ctx))
  6670. return (NULL);
  6671. return gen_loadi_internal(cstate, val);
  6672. }
  6673. /*
  6674. * The a_arg dance is to avoid annoying whining by compilers that
  6675. * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
  6676. * It's not *used* after setjmp returns.
  6677. */
  6678. struct arth *
  6679. gen_neg(compiler_state_t *cstate, struct arth *a_arg)
  6680. {
  6681. struct arth *a = a_arg;
  6682. struct slist *s;
  6683. /*
  6684. * Catch errors reported by us and routines below us, and return NULL
  6685. * on an error.
  6686. */
  6687. if (setjmp(cstate->top_ctx))
  6688. return (NULL);
  6689. s = xfer_to_a(cstate, a);
  6690. sappend(a->s, s);
  6691. s = new_stmt(cstate, BPF_ALU|BPF_NEG);
  6692. s->s.k = 0;
  6693. sappend(a->s, s);
  6694. s = new_stmt(cstate, BPF_ST);
  6695. s->s.k = a->regno;
  6696. sappend(a->s, s);
  6697. return a;
  6698. }
  6699. /*
  6700. * The a0_arg dance is to avoid annoying whining by compilers that
  6701. * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
  6702. * It's not *used* after setjmp returns.
  6703. */
  6704. struct arth *
  6705. gen_arth(compiler_state_t *cstate, int code, struct arth *a0_arg,
  6706. struct arth *a1)
  6707. {
  6708. struct arth *a0 = a0_arg;
  6709. struct slist *s0, *s1, *s2;
  6710. /*
  6711. * Catch errors reported by us and routines below us, and return NULL
  6712. * on an error.
  6713. */
  6714. if (setjmp(cstate->top_ctx))
  6715. return (NULL);
  6716. /*
  6717. * Disallow division by, or modulus by, zero; we do this here
  6718. * so that it gets done even if the optimizer is disabled.
  6719. *
  6720. * Also disallow shifts by a value greater than 31; we do this
  6721. * here, for the same reason.
  6722. */
  6723. if (code == BPF_DIV) {
  6724. if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
  6725. bpf_error(cstate, "division by zero");
  6726. } else if (code == BPF_MOD) {
  6727. if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)
  6728. bpf_error(cstate, "modulus by zero");
  6729. } else if (code == BPF_LSH || code == BPF_RSH) {
  6730. if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k > 31)
  6731. bpf_error(cstate, "shift by more than 31 bits");
  6732. }
  6733. s0 = xfer_to_x(cstate, a1);
  6734. s1 = xfer_to_a(cstate, a0);
  6735. s2 = new_stmt(cstate, BPF_ALU|BPF_X|code);
  6736. sappend(s1, s2);
  6737. sappend(s0, s1);
  6738. sappend(a1->s, s0);
  6739. sappend(a0->s, a1->s);
  6740. free_reg(cstate, a0->regno);
  6741. free_reg(cstate, a1->regno);
  6742. s0 = new_stmt(cstate, BPF_ST);
  6743. a0->regno = s0->s.k = alloc_reg(cstate);
  6744. sappend(a0->s, s0);
  6745. return a0;
  6746. }
  6747. /*
  6748. * Initialize the table of used registers and the current register.
  6749. */
  6750. static void
  6751. init_regs(compiler_state_t *cstate)
  6752. {
  6753. cstate->curreg = 0;
  6754. memset(cstate->regused, 0, sizeof cstate->regused);
  6755. }
  6756. /*
  6757. * Return the next free register.
  6758. */
  6759. static int
  6760. alloc_reg(compiler_state_t *cstate)
  6761. {
  6762. int n = BPF_MEMWORDS;
  6763. while (--n >= 0) {
  6764. if (cstate->regused[cstate->curreg])
  6765. cstate->curreg = (cstate->curreg + 1) % BPF_MEMWORDS;
  6766. else {
  6767. cstate->regused[cstate->curreg] = 1;
  6768. return cstate->curreg;
  6769. }
  6770. }
  6771. bpf_error(cstate, "too many registers needed to evaluate expression");
  6772. /*NOTREACHED*/
  6773. }
  6774. /*
  6775. * Return a register to the table so it can
  6776. * be used later.
  6777. */
  6778. static void
  6779. free_reg(compiler_state_t *cstate, int n)
  6780. {
  6781. cstate->regused[n] = 0;
  6782. }
  6783. static struct block *
  6784. gen_len(compiler_state_t *cstate, int jmp, int n)
  6785. {
  6786. struct slist *s;
  6787. struct block *b;
  6788. s = new_stmt(cstate, BPF_LD|BPF_LEN);
  6789. b = new_block(cstate, JMP(jmp));
  6790. b->stmts = s;
  6791. b->s.k = n;
  6792. return b;
  6793. }
  6794. struct block *
  6795. gen_greater(compiler_state_t *cstate, int n)
  6796. {
  6797. /*
  6798. * Catch errors reported by us and routines below us, and return NULL
  6799. * on an error.
  6800. */
  6801. if (setjmp(cstate->top_ctx))
  6802. return (NULL);
  6803. return gen_len(cstate, BPF_JGE, n);
  6804. }
  6805. /*
  6806. * Actually, this is less than or equal.
  6807. */
  6808. struct block *
  6809. gen_less(compiler_state_t *cstate, int n)
  6810. {
  6811. struct block *b;
  6812. /*
  6813. * Catch errors reported by us and routines below us, and return NULL
  6814. * on an error.
  6815. */
  6816. if (setjmp(cstate->top_ctx))
  6817. return (NULL);
  6818. b = gen_len(cstate, BPF_JGT, n);
  6819. gen_not(b);
  6820. return b;
  6821. }
  6822. /*
  6823. * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
  6824. * the beginning of the link-layer header.
  6825. * XXX - that means you can't test values in the radiotap header, but
  6826. * as that header is difficult if not impossible to parse generally
  6827. * without a loop, that might not be a severe problem. A new keyword
  6828. * "radio" could be added for that, although what you'd really want
  6829. * would be a way of testing particular radio header values, which
  6830. * would generate code appropriate to the radio header in question.
  6831. */
  6832. struct block *
  6833. gen_byteop(compiler_state_t *cstate, int op, int idx, bpf_u_int32 val)
  6834. {
  6835. struct block *b;
  6836. struct slist *s;
  6837. /*
  6838. * Catch errors reported by us and routines below us, and return NULL
  6839. * on an error.
  6840. */
  6841. if (setjmp(cstate->top_ctx))
  6842. return (NULL);
  6843. switch (op) {
  6844. default:
  6845. abort();
  6846. case '=':
  6847. return gen_cmp(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
  6848. case '<':
  6849. b = gen_cmp_lt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
  6850. return b;
  6851. case '>':
  6852. b = gen_cmp_gt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);
  6853. return b;
  6854. case '|':
  6855. s = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_K);
  6856. break;
  6857. case '&':
  6858. s = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
  6859. break;
  6860. }
  6861. s->s.k = val;
  6862. b = new_block(cstate, JMP(BPF_JEQ));
  6863. b->stmts = s;
  6864. gen_not(b);
  6865. return b;
  6866. }
  6867. static const u_char abroadcast[] = { 0x0 };
  6868. struct block *
  6869. gen_broadcast(compiler_state_t *cstate, int proto)
  6870. {
  6871. bpf_u_int32 hostmask;
  6872. struct block *b0, *b1, *b2;
  6873. static const u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  6874. /*
  6875. * Catch errors reported by us and routines below us, and return NULL
  6876. * on an error.
  6877. */
  6878. if (setjmp(cstate->top_ctx))
  6879. return (NULL);
  6880. switch (proto) {
  6881. case Q_DEFAULT:
  6882. case Q_LINK:
  6883. switch (cstate->linktype) {
  6884. case DLT_ARCNET:
  6885. case DLT_ARCNET_LINUX:
  6886. return gen_ahostop(cstate, abroadcast, Q_DST);
  6887. case DLT_EN10MB:
  6888. case DLT_NETANALYZER:
  6889. case DLT_NETANALYZER_TRANSPARENT:
  6890. b1 = gen_prevlinkhdr_check(cstate);
  6891. b0 = gen_ehostop(cstate, ebroadcast, Q_DST);
  6892. if (b1 != NULL)
  6893. gen_and(b1, b0);
  6894. return b0;
  6895. case DLT_FDDI:
  6896. return gen_fhostop(cstate, ebroadcast, Q_DST);
  6897. case DLT_IEEE802:
  6898. return gen_thostop(cstate, ebroadcast, Q_DST);
  6899. case DLT_IEEE802_11:
  6900. case DLT_PRISM_HEADER:
  6901. case DLT_IEEE802_11_RADIO_AVS:
  6902. case DLT_IEEE802_11_RADIO:
  6903. case DLT_PPI:
  6904. return gen_wlanhostop(cstate, ebroadcast, Q_DST);
  6905. case DLT_IP_OVER_FC:
  6906. return gen_ipfchostop(cstate, ebroadcast, Q_DST);
  6907. default:
  6908. bpf_error(cstate, "not a broadcast link");
  6909. }
  6910. /*NOTREACHED*/
  6911. case Q_IP:
  6912. /*
  6913. * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
  6914. * as an indication that we don't know the netmask, and fail
  6915. * in that case.
  6916. */
  6917. if (cstate->netmask == PCAP_NETMASK_UNKNOWN)
  6918. bpf_error(cstate, "netmask not known, so 'ip broadcast' not supported");
  6919. b0 = gen_linktype(cstate, ETHERTYPE_IP);
  6920. hostmask = ~cstate->netmask;
  6921. b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, 0, hostmask);
  6922. b2 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W,
  6923. ~0 & hostmask, hostmask);
  6924. gen_or(b1, b2);
  6925. gen_and(b0, b2);
  6926. return b2;
  6927. }
  6928. bpf_error(cstate, "only link-layer/IP broadcast filters supported");
  6929. /*NOTREACHED*/
  6930. }
  6931. /*
  6932. * Generate code to test the low-order bit of a MAC address (that's
  6933. * the bottom bit of the *first* byte).
  6934. */
  6935. static struct block *
  6936. gen_mac_multicast(compiler_state_t *cstate, int offset)
  6937. {
  6938. register struct block *b0;
  6939. register struct slist *s;
  6940. /* link[offset] & 1 != 0 */
  6941. s = gen_load_a(cstate, OR_LINKHDR, offset, BPF_B);
  6942. b0 = new_block(cstate, JMP(BPF_JSET));
  6943. b0->s.k = 1;
  6944. b0->stmts = s;
  6945. return b0;
  6946. }
  6947. struct block *
  6948. gen_multicast(compiler_state_t *cstate, int proto)
  6949. {
  6950. register struct block *b0, *b1, *b2;
  6951. register struct slist *s;
  6952. /*
  6953. * Catch errors reported by us and routines below us, and return NULL
  6954. * on an error.
  6955. */
  6956. if (setjmp(cstate->top_ctx))
  6957. return (NULL);
  6958. switch (proto) {
  6959. case Q_DEFAULT:
  6960. case Q_LINK:
  6961. switch (cstate->linktype) {
  6962. case DLT_ARCNET:
  6963. case DLT_ARCNET_LINUX:
  6964. /* all ARCnet multicasts use the same address */
  6965. return gen_ahostop(cstate, abroadcast, Q_DST);
  6966. case DLT_EN10MB:
  6967. case DLT_NETANALYZER:
  6968. case DLT_NETANALYZER_TRANSPARENT:
  6969. b1 = gen_prevlinkhdr_check(cstate);
  6970. /* ether[0] & 1 != 0 */
  6971. b0 = gen_mac_multicast(cstate, 0);
  6972. if (b1 != NULL)
  6973. gen_and(b1, b0);
  6974. return b0;
  6975. case DLT_FDDI:
  6976. /*
  6977. * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
  6978. *
  6979. * XXX - was that referring to bit-order issues?
  6980. */
  6981. /* fddi[1] & 1 != 0 */
  6982. return gen_mac_multicast(cstate, 1);
  6983. case DLT_IEEE802:
  6984. /* tr[2] & 1 != 0 */
  6985. return gen_mac_multicast(cstate, 2);
  6986. case DLT_IEEE802_11:
  6987. case DLT_PRISM_HEADER:
  6988. case DLT_IEEE802_11_RADIO_AVS:
  6989. case DLT_IEEE802_11_RADIO:
  6990. case DLT_PPI:
  6991. /*
  6992. * Oh, yuk.
  6993. *
  6994. * For control frames, there is no DA.
  6995. *
  6996. * For management frames, DA is at an
  6997. * offset of 4 from the beginning of
  6998. * the packet.
  6999. *
  7000. * For data frames, DA is at an offset
  7001. * of 4 from the beginning of the packet
  7002. * if To DS is clear and at an offset of
  7003. * 16 from the beginning of the packet
  7004. * if To DS is set.
  7005. */
  7006. /*
  7007. * Generate the tests to be done for data frames.
  7008. *
  7009. * First, check for To DS set, i.e. "link[1] & 0x01".
  7010. */
  7011. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  7012. b1 = new_block(cstate, JMP(BPF_JSET));
  7013. b1->s.k = 0x01; /* To DS */
  7014. b1->stmts = s;
  7015. /*
  7016. * If To DS is set, the DA is at 16.
  7017. */
  7018. b0 = gen_mac_multicast(cstate, 16);
  7019. gen_and(b1, b0);
  7020. /*
  7021. * Now, check for To DS not set, i.e. check
  7022. * "!(link[1] & 0x01)".
  7023. */
  7024. s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);
  7025. b2 = new_block(cstate, JMP(BPF_JSET));
  7026. b2->s.k = 0x01; /* To DS */
  7027. b2->stmts = s;
  7028. gen_not(b2);
  7029. /*
  7030. * If To DS is not set, the DA is at 4.
  7031. */
  7032. b1 = gen_mac_multicast(cstate, 4);
  7033. gen_and(b2, b1);
  7034. /*
  7035. * Now OR together the last two checks. That gives
  7036. * the complete set of checks for data frames.
  7037. */
  7038. gen_or(b1, b0);
  7039. /*
  7040. * Now check for a data frame.
  7041. * I.e, check "link[0] & 0x08".
  7042. */
  7043. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  7044. b1 = new_block(cstate, JMP(BPF_JSET));
  7045. b1->s.k = 0x08;
  7046. b1->stmts = s;
  7047. /*
  7048. * AND that with the checks done for data frames.
  7049. */
  7050. gen_and(b1, b0);
  7051. /*
  7052. * If the high-order bit of the type value is 0, this
  7053. * is a management frame.
  7054. * I.e, check "!(link[0] & 0x08)".
  7055. */
  7056. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  7057. b2 = new_block(cstate, JMP(BPF_JSET));
  7058. b2->s.k = 0x08;
  7059. b2->stmts = s;
  7060. gen_not(b2);
  7061. /*
  7062. * For management frames, the DA is at 4.
  7063. */
  7064. b1 = gen_mac_multicast(cstate, 4);
  7065. gen_and(b2, b1);
  7066. /*
  7067. * OR that with the checks done for data frames.
  7068. * That gives the checks done for management and
  7069. * data frames.
  7070. */
  7071. gen_or(b1, b0);
  7072. /*
  7073. * If the low-order bit of the type value is 1,
  7074. * this is either a control frame or a frame
  7075. * with a reserved type, and thus not a
  7076. * frame with an SA.
  7077. *
  7078. * I.e., check "!(link[0] & 0x04)".
  7079. */
  7080. s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);
  7081. b1 = new_block(cstate, JMP(BPF_JSET));
  7082. b1->s.k = 0x04;
  7083. b1->stmts = s;
  7084. gen_not(b1);
  7085. /*
  7086. * AND that with the checks for data and management
  7087. * frames.
  7088. */
  7089. gen_and(b1, b0);
  7090. return b0;
  7091. case DLT_IP_OVER_FC:
  7092. b0 = gen_mac_multicast(cstate, 2);
  7093. return b0;
  7094. default:
  7095. break;
  7096. }
  7097. /* Link not known to support multicasts */
  7098. break;
  7099. case Q_IP:
  7100. b0 = gen_linktype(cstate, ETHERTYPE_IP);
  7101. b1 = gen_cmp_ge(cstate, OR_LINKPL, 16, BPF_B, 224);
  7102. gen_and(b0, b1);
  7103. return b1;
  7104. case Q_IPV6:
  7105. b0 = gen_linktype(cstate, ETHERTYPE_IPV6);
  7106. b1 = gen_cmp(cstate, OR_LINKPL, 24, BPF_B, 255);
  7107. gen_and(b0, b1);
  7108. return b1;
  7109. }
  7110. bpf_error(cstate, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
  7111. /*NOTREACHED*/
  7112. }
  7113. /*
  7114. * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
  7115. * Outbound traffic is sent by this machine, while inbound traffic is
  7116. * sent by a remote machine (and may include packets destined for a
  7117. * unicast or multicast link-layer address we are not subscribing to).
  7118. * These are the same definitions implemented by pcap_setdirection().
  7119. * Capturing only unicast traffic destined for this host is probably
  7120. * better accomplished using a higher-layer filter.
  7121. */
  7122. struct block *
  7123. gen_inbound(compiler_state_t *cstate, int dir)
  7124. {
  7125. register struct block *b0;
  7126. /*
  7127. * Catch errors reported by us and routines below us, and return NULL
  7128. * on an error.
  7129. */
  7130. if (setjmp(cstate->top_ctx))
  7131. return (NULL);
  7132. /*
  7133. * Only some data link types support inbound/outbound qualifiers.
  7134. */
  7135. switch (cstate->linktype) {
  7136. case DLT_SLIP:
  7137. b0 = gen_relation_internal(cstate, BPF_JEQ,
  7138. gen_load_internal(cstate, Q_LINK, gen_loadi_internal(cstate, 0), 1),
  7139. gen_loadi_internal(cstate, 0),
  7140. dir);
  7141. break;
  7142. case DLT_IPNET:
  7143. if (dir) {
  7144. /* match outgoing packets */
  7145. b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_OUTBOUND);
  7146. } else {
  7147. /* match incoming packets */
  7148. b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_INBOUND);
  7149. }
  7150. break;
  7151. case DLT_LINUX_SLL:
  7152. /* match outgoing packets */
  7153. b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_H, LINUX_SLL_OUTGOING);
  7154. if (!dir) {
  7155. /* to filter on inbound traffic, invert the match */
  7156. gen_not(b0);
  7157. }
  7158. break;
  7159. case DLT_LINUX_SLL2:
  7160. /* match outgoing packets */
  7161. b0 = gen_cmp(cstate, OR_LINKHDR, 10, BPF_B, LINUX_SLL_OUTGOING);
  7162. if (!dir) {
  7163. /* to filter on inbound traffic, invert the match */
  7164. gen_not(b0);
  7165. }
  7166. break;
  7167. #ifdef HAVE_NET_PFVAR_H
  7168. case DLT_PFLOG:
  7169. b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B,
  7170. ((dir == 0) ? PF_IN : PF_OUT));
  7171. break;
  7172. #endif
  7173. case DLT_PPP_PPPD:
  7174. if (dir) {
  7175. /* match outgoing packets */
  7176. b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_OUT);
  7177. } else {
  7178. /* match incoming packets */
  7179. b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_IN);
  7180. }
  7181. break;
  7182. case DLT_JUNIPER_MFR:
  7183. case DLT_JUNIPER_MLFR:
  7184. case DLT_JUNIPER_MLPPP:
  7185. case DLT_JUNIPER_ATM1:
  7186. case DLT_JUNIPER_ATM2:
  7187. case DLT_JUNIPER_PPPOE:
  7188. case DLT_JUNIPER_PPPOE_ATM:
  7189. case DLT_JUNIPER_GGSN:
  7190. case DLT_JUNIPER_ES:
  7191. case DLT_JUNIPER_MONITOR:
  7192. case DLT_JUNIPER_SERVICES:
  7193. case DLT_JUNIPER_ETHER:
  7194. case DLT_JUNIPER_PPP:
  7195. case DLT_JUNIPER_FRELAY:
  7196. case DLT_JUNIPER_CHDLC:
  7197. case DLT_JUNIPER_VP:
  7198. case DLT_JUNIPER_ST:
  7199. case DLT_JUNIPER_ISM:
  7200. case DLT_JUNIPER_VS:
  7201. case DLT_JUNIPER_SRX_E2E:
  7202. case DLT_JUNIPER_FIBRECHANNEL:
  7203. case DLT_JUNIPER_ATM_CEMIC:
  7204. /* juniper flags (including direction) are stored
  7205. * the byte after the 3-byte magic number */
  7206. if (dir) {
  7207. /* match outgoing packets */
  7208. b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 0, 0x01);
  7209. } else {
  7210. /* match incoming packets */
  7211. b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 1, 0x01);
  7212. }
  7213. break;
  7214. default:
  7215. /*
  7216. * If we have packet meta-data indicating a direction,
  7217. * and that metadata can be checked by BPF code, check
  7218. * it. Otherwise, give up, as this link-layer type has
  7219. * nothing in the packet data.
  7220. *
  7221. * Currently, the only platform where a BPF filter can
  7222. * check that metadata is Linux with the in-kernel
  7223. * BPF interpreter. If other packet capture mechanisms
  7224. * and BPF filters also supported this, it would be
  7225. * nice. It would be even better if they made that
  7226. * metadata available so that we could provide it
  7227. * with newer capture APIs, allowing it to be saved
  7228. * in pcapng files.
  7229. */
  7230. #if defined(linux)
  7231. /*
  7232. * This is Linux; we assume it has PF_PACKET support.
  7233. * If this is a *live* capture, we can look at
  7234. * special meta-data in the filter expression;
  7235. * if it's a savefile, we can't.
  7236. */
  7237. if (cstate->bpf_pcap->rfile != NULL) {
  7238. /* We have a FILE *, so this is a savefile */
  7239. bpf_error(cstate, "inbound/outbound not supported on %s when reading savefiles",
  7240. pcap_datalink_val_to_description_or_dlt(cstate->linktype));
  7241. /*NOTREACHED*/
  7242. }
  7243. /* match outgoing packets */
  7244. b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
  7245. PACKET_OUTGOING);
  7246. if (!dir) {
  7247. /* to filter on inbound traffic, invert the match */
  7248. gen_not(b0);
  7249. }
  7250. #else /* defined(linux) */
  7251. bpf_error(cstate, "inbound/outbound not supported on %s",
  7252. pcap_datalink_val_to_description_or_dlt(cstate->linktype));
  7253. /*NOTREACHED*/
  7254. #endif /* defined(linux) */
  7255. }
  7256. return (b0);
  7257. }
  7258. #ifdef HAVE_NET_PFVAR_H
  7259. /* PF firewall log matched interface */
  7260. struct block *
  7261. gen_pf_ifname(compiler_state_t *cstate, const char *ifname)
  7262. {
  7263. struct block *b0;
  7264. u_int len, off;
  7265. /*
  7266. * Catch errors reported by us and routines below us, and return NULL
  7267. * on an error.
  7268. */
  7269. if (setjmp(cstate->top_ctx))
  7270. return (NULL);
  7271. if (cstate->linktype != DLT_PFLOG) {
  7272. bpf_error(cstate, "ifname supported only on PF linktype");
  7273. /*NOTREACHED*/
  7274. }
  7275. len = sizeof(((struct pfloghdr *)0)->ifname);
  7276. off = offsetof(struct pfloghdr, ifname);
  7277. if (strlen(ifname) >= len) {
  7278. bpf_error(cstate, "ifname interface names can only be %d characters",
  7279. len-1);
  7280. /*NOTREACHED*/
  7281. }
  7282. b0 = gen_bcmp(cstate, OR_LINKHDR, off, (u_int)strlen(ifname),
  7283. (const u_char *)ifname);
  7284. return (b0);
  7285. }
  7286. /* PF firewall log ruleset name */
  7287. struct block *
  7288. gen_pf_ruleset(compiler_state_t *cstate, char *ruleset)
  7289. {
  7290. struct block *b0;
  7291. /*
  7292. * Catch errors reported by us and routines below us, and return NULL
  7293. * on an error.
  7294. */
  7295. if (setjmp(cstate->top_ctx))
  7296. return (NULL);
  7297. if (cstate->linktype != DLT_PFLOG) {
  7298. bpf_error(cstate, "ruleset supported only on PF linktype");
  7299. /*NOTREACHED*/
  7300. }
  7301. if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
  7302. bpf_error(cstate, "ruleset names can only be %ld characters",
  7303. (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
  7304. /*NOTREACHED*/
  7305. }
  7306. b0 = gen_bcmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, ruleset),
  7307. (u_int)strlen(ruleset), (const u_char *)ruleset);
  7308. return (b0);
  7309. }
  7310. /* PF firewall log rule number */
  7311. struct block *
  7312. gen_pf_rnr(compiler_state_t *cstate, int rnr)
  7313. {
  7314. struct block *b0;
  7315. /*
  7316. * Catch errors reported by us and routines below us, and return NULL
  7317. * on an error.
  7318. */
  7319. if (setjmp(cstate->top_ctx))
  7320. return (NULL);
  7321. if (cstate->linktype != DLT_PFLOG) {
  7322. bpf_error(cstate, "rnr supported only on PF linktype");
  7323. /*NOTREACHED*/
  7324. }
  7325. b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, rulenr), BPF_W,
  7326. (bpf_u_int32)rnr);
  7327. return (b0);
  7328. }
  7329. /* PF firewall log sub-rule number */
  7330. struct block *
  7331. gen_pf_srnr(compiler_state_t *cstate, int srnr)
  7332. {
  7333. struct block *b0;
  7334. /*
  7335. * Catch errors reported by us and routines below us, and return NULL
  7336. * on an error.
  7337. */
  7338. if (setjmp(cstate->top_ctx))
  7339. return (NULL);
  7340. if (cstate->linktype != DLT_PFLOG) {
  7341. bpf_error(cstate, "srnr supported only on PF linktype");
  7342. /*NOTREACHED*/
  7343. }
  7344. b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, subrulenr), BPF_W,
  7345. (bpf_u_int32)srnr);
  7346. return (b0);
  7347. }
  7348. /* PF firewall log reason code */
  7349. struct block *
  7350. gen_pf_reason(compiler_state_t *cstate, int reason)
  7351. {
  7352. struct block *b0;
  7353. /*
  7354. * Catch errors reported by us and routines below us, and return NULL
  7355. * on an error.
  7356. */
  7357. if (setjmp(cstate->top_ctx))
  7358. return (NULL);
  7359. if (cstate->linktype != DLT_PFLOG) {
  7360. bpf_error(cstate, "reason supported only on PF linktype");
  7361. /*NOTREACHED*/
  7362. }
  7363. b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, reason), BPF_B,
  7364. (bpf_u_int32)reason);
  7365. return (b0);
  7366. }
  7367. /* PF firewall log action */
  7368. struct block *
  7369. gen_pf_action(compiler_state_t *cstate, int action)
  7370. {
  7371. struct block *b0;
  7372. /*
  7373. * Catch errors reported by us and routines below us, and return NULL
  7374. * on an error.
  7375. */
  7376. if (setjmp(cstate->top_ctx))
  7377. return (NULL);
  7378. if (cstate->linktype != DLT_PFLOG) {
  7379. bpf_error(cstate, "action supported only on PF linktype");
  7380. /*NOTREACHED*/
  7381. }
  7382. b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, action), BPF_B,
  7383. (bpf_u_int32)action);
  7384. return (b0);
  7385. }
  7386. #else /* !HAVE_NET_PFVAR_H */
  7387. struct block *
  7388. gen_pf_ifname(compiler_state_t *cstate, const char *ifname _U_)
  7389. {
  7390. /*
  7391. * Catch errors reported by us and routines below us, and return NULL
  7392. * on an error.
  7393. */
  7394. if (setjmp(cstate->top_ctx))
  7395. return (NULL);
  7396. bpf_error(cstate, "libpcap was compiled without pf support");
  7397. /*NOTREACHED*/
  7398. }
  7399. struct block *
  7400. gen_pf_ruleset(compiler_state_t *cstate, char *ruleset _U_)
  7401. {
  7402. /*
  7403. * Catch errors reported by us and routines below us, and return NULL
  7404. * on an error.
  7405. */
  7406. if (setjmp(cstate->top_ctx))
  7407. return (NULL);
  7408. bpf_error(cstate, "libpcap was compiled on a machine without pf support");
  7409. /*NOTREACHED*/
  7410. }
  7411. struct block *
  7412. gen_pf_rnr(compiler_state_t *cstate, int rnr _U_)
  7413. {
  7414. /*
  7415. * Catch errors reported by us and routines below us, and return NULL
  7416. * on an error.
  7417. */
  7418. if (setjmp(cstate->top_ctx))
  7419. return (NULL);
  7420. bpf_error(cstate, "libpcap was compiled on a machine without pf support");
  7421. /*NOTREACHED*/
  7422. }
  7423. struct block *
  7424. gen_pf_srnr(compiler_state_t *cstate, int srnr _U_)
  7425. {
  7426. /*
  7427. * Catch errors reported by us and routines below us, and return NULL
  7428. * on an error.
  7429. */
  7430. if (setjmp(cstate->top_ctx))
  7431. return (NULL);
  7432. bpf_error(cstate, "libpcap was compiled on a machine without pf support");
  7433. /*NOTREACHED*/
  7434. }
  7435. struct block *
  7436. gen_pf_reason(compiler_state_t *cstate, int reason _U_)
  7437. {
  7438. /*
  7439. * Catch errors reported by us and routines below us, and return NULL
  7440. * on an error.
  7441. */
  7442. if (setjmp(cstate->top_ctx))
  7443. return (NULL);
  7444. bpf_error(cstate, "libpcap was compiled on a machine without pf support");
  7445. /*NOTREACHED*/
  7446. }
  7447. struct block *
  7448. gen_pf_action(compiler_state_t *cstate, int action _U_)
  7449. {
  7450. /*
  7451. * Catch errors reported by us and routines below us, and return NULL
  7452. * on an error.
  7453. */
  7454. if (setjmp(cstate->top_ctx))
  7455. return (NULL);
  7456. bpf_error(cstate, "libpcap was compiled on a machine without pf support");
  7457. /*NOTREACHED*/
  7458. }
  7459. #endif /* HAVE_NET_PFVAR_H */
  7460. /* IEEE 802.11 wireless header */
  7461. struct block *
  7462. gen_p80211_type(compiler_state_t *cstate, bpf_u_int32 type, bpf_u_int32 mask)
  7463. {
  7464. struct block *b0;
  7465. /*
  7466. * Catch errors reported by us and routines below us, and return NULL
  7467. * on an error.
  7468. */
  7469. if (setjmp(cstate->top_ctx))
  7470. return (NULL);
  7471. switch (cstate->linktype) {
  7472. case DLT_IEEE802_11:
  7473. case DLT_PRISM_HEADER:
  7474. case DLT_IEEE802_11_RADIO_AVS:
  7475. case DLT_IEEE802_11_RADIO:
  7476. b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, type, mask);
  7477. break;
  7478. default:
  7479. bpf_error(cstate, "802.11 link-layer types supported only on 802.11");
  7480. /*NOTREACHED*/
  7481. }
  7482. return (b0);
  7483. }
  7484. struct block *
  7485. gen_p80211_fcdir(compiler_state_t *cstate, bpf_u_int32 fcdir)
  7486. {
  7487. struct block *b0;
  7488. /*
  7489. * Catch errors reported by us and routines below us, and return NULL
  7490. * on an error.
  7491. */
  7492. if (setjmp(cstate->top_ctx))
  7493. return (NULL);
  7494. switch (cstate->linktype) {
  7495. case DLT_IEEE802_11:
  7496. case DLT_PRISM_HEADER:
  7497. case DLT_IEEE802_11_RADIO_AVS:
  7498. case DLT_IEEE802_11_RADIO:
  7499. break;
  7500. default:
  7501. bpf_error(cstate, "frame direction supported only with 802.11 headers");
  7502. /*NOTREACHED*/
  7503. }
  7504. b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, fcdir,
  7505. IEEE80211_FC1_DIR_MASK);
  7506. return (b0);
  7507. }
  7508. struct block *
  7509. gen_acode(compiler_state_t *cstate, const char *s, struct qual q)
  7510. {
  7511. struct block *b;
  7512. /*
  7513. * Catch errors reported by us and routines below us, and return NULL
  7514. * on an error.
  7515. */
  7516. if (setjmp(cstate->top_ctx))
  7517. return (NULL);
  7518. switch (cstate->linktype) {
  7519. case DLT_ARCNET:
  7520. case DLT_ARCNET_LINUX:
  7521. if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
  7522. q.proto == Q_LINK) {
  7523. cstate->e = pcap_ether_aton(s);
  7524. if (cstate->e == NULL)
  7525. bpf_error(cstate, "malloc");
  7526. b = gen_ahostop(cstate, cstate->e, (int)q.dir);
  7527. free(cstate->e);
  7528. cstate->e = NULL;
  7529. return (b);
  7530. } else
  7531. bpf_error(cstate, "ARCnet address used in non-arc expression");
  7532. /*NOTREACHED*/
  7533. default:
  7534. bpf_error(cstate, "aid supported only on ARCnet");
  7535. /*NOTREACHED*/
  7536. }
  7537. }
  7538. static struct block *
  7539. gen_ahostop(compiler_state_t *cstate, const u_char *eaddr, int dir)
  7540. {
  7541. register struct block *b0, *b1;
  7542. switch (dir) {
  7543. /* src comes first, different from Ethernet */
  7544. case Q_SRC:
  7545. return gen_bcmp(cstate, OR_LINKHDR, 0, 1, eaddr);
  7546. case Q_DST:
  7547. return gen_bcmp(cstate, OR_LINKHDR, 1, 1, eaddr);
  7548. case Q_AND:
  7549. b0 = gen_ahostop(cstate, eaddr, Q_SRC);
  7550. b1 = gen_ahostop(cstate, eaddr, Q_DST);
  7551. gen_and(b0, b1);
  7552. return b1;
  7553. case Q_DEFAULT:
  7554. case Q_OR:
  7555. b0 = gen_ahostop(cstate, eaddr, Q_SRC);
  7556. b1 = gen_ahostop(cstate, eaddr, Q_DST);
  7557. gen_or(b0, b1);
  7558. return b1;
  7559. case Q_ADDR1:
  7560. bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");
  7561. /*NOTREACHED*/
  7562. case Q_ADDR2:
  7563. bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");
  7564. /*NOTREACHED*/
  7565. case Q_ADDR3:
  7566. bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");
  7567. /*NOTREACHED*/
  7568. case Q_ADDR4:
  7569. bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");
  7570. /*NOTREACHED*/
  7571. case Q_RA:
  7572. bpf_error(cstate, "'ra' is only supported on 802.11");
  7573. /*NOTREACHED*/
  7574. case Q_TA:
  7575. bpf_error(cstate, "'ta' is only supported on 802.11");
  7576. /*NOTREACHED*/
  7577. }
  7578. abort();
  7579. /*NOTREACHED*/
  7580. }
  7581. static struct block *
  7582. gen_vlan_tpid_test(compiler_state_t *cstate)
  7583. {
  7584. struct block *b0, *b1;
  7585. /* check for VLAN, including QinQ */
  7586. b0 = gen_linktype(cstate, ETHERTYPE_8021Q);
  7587. b1 = gen_linktype(cstate, ETHERTYPE_8021AD);
  7588. gen_or(b0,b1);
  7589. b0 = b1;
  7590. b1 = gen_linktype(cstate, ETHERTYPE_8021QINQ);
  7591. gen_or(b0,b1);
  7592. return b1;
  7593. }
  7594. static struct block *
  7595. gen_vlan_vid_test(compiler_state_t *cstate, bpf_u_int32 vlan_num)
  7596. {
  7597. if (vlan_num > 0x0fff) {
  7598. bpf_error(cstate, "VLAN tag %u greater than maximum %u",
  7599. vlan_num, 0x0fff);
  7600. }
  7601. return gen_mcmp(cstate, OR_LINKPL, 0, BPF_H, vlan_num, 0x0fff);
  7602. }
  7603. static struct block *
  7604. gen_vlan_no_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num,
  7605. int has_vlan_tag)
  7606. {
  7607. struct block *b0, *b1;
  7608. b0 = gen_vlan_tpid_test(cstate);
  7609. if (has_vlan_tag) {
  7610. b1 = gen_vlan_vid_test(cstate, vlan_num);
  7611. gen_and(b0, b1);
  7612. b0 = b1;
  7613. }
  7614. /*
  7615. * Both payload and link header type follow the VLAN tags so that
  7616. * both need to be updated.
  7617. */
  7618. cstate->off_linkpl.constant_part += 4;
  7619. cstate->off_linktype.constant_part += 4;
  7620. return b0;
  7621. }
  7622. #if defined(SKF_AD_VLAN_TAG_PRESENT)
  7623. /* add v to variable part of off */
  7624. static void
  7625. gen_vlan_vloffset_add(compiler_state_t *cstate, bpf_abs_offset *off,
  7626. bpf_u_int32 v, struct slist *s)
  7627. {
  7628. struct slist *s2;
  7629. if (!off->is_variable)
  7630. off->is_variable = 1;
  7631. if (off->reg == -1)
  7632. off->reg = alloc_reg(cstate);
  7633. s2 = new_stmt(cstate, BPF_LD|BPF_MEM);
  7634. s2->s.k = off->reg;
  7635. sappend(s, s2);
  7636. s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);
  7637. s2->s.k = v;
  7638. sappend(s, s2);
  7639. s2 = new_stmt(cstate, BPF_ST);
  7640. s2->s.k = off->reg;
  7641. sappend(s, s2);
  7642. }
  7643. /*
  7644. * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
  7645. * and link type offsets first
  7646. */
  7647. static void
  7648. gen_vlan_patch_tpid_test(compiler_state_t *cstate, struct block *b_tpid)
  7649. {
  7650. struct slist s;
  7651. /* offset determined at run time, shift variable part */
  7652. s.next = NULL;
  7653. cstate->is_vlan_vloffset = 1;
  7654. gen_vlan_vloffset_add(cstate, &cstate->off_linkpl, 4, &s);
  7655. gen_vlan_vloffset_add(cstate, &cstate->off_linktype, 4, &s);
  7656. /* we get a pointer to a chain of or-ed blocks, patch first of them */
  7657. sappend(s.next, b_tpid->head->stmts);
  7658. b_tpid->head->stmts = s.next;
  7659. }
  7660. /*
  7661. * patch block b_vid (VLAN id test) to load VID value either from packet
  7662. * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
  7663. */
  7664. static void
  7665. gen_vlan_patch_vid_test(compiler_state_t *cstate, struct block *b_vid)
  7666. {
  7667. struct slist *s, *s2, *sjeq;
  7668. unsigned cnt;
  7669. s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
  7670. s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;
  7671. /* true -> next instructions, false -> beginning of b_vid */
  7672. sjeq = new_stmt(cstate, JMP(BPF_JEQ));
  7673. sjeq->s.k = 1;
  7674. sjeq->s.jf = b_vid->stmts;
  7675. sappend(s, sjeq);
  7676. s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
  7677. s2->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG;
  7678. sappend(s, s2);
  7679. sjeq->s.jt = s2;
  7680. /* Jump to the test in b_vid. We need to jump one instruction before
  7681. * the end of the b_vid block so that we only skip loading the TCI
  7682. * from packet data and not the 'and' instruction extractging VID.
  7683. */
  7684. cnt = 0;
  7685. for (s2 = b_vid->stmts; s2; s2 = s2->next)
  7686. cnt++;
  7687. s2 = new_stmt(cstate, JMP(BPF_JA));
  7688. s2->s.k = cnt - 1;
  7689. sappend(s, s2);
  7690. /* insert our statements at the beginning of b_vid */
  7691. sappend(s, b_vid->stmts);
  7692. b_vid->stmts = s;
  7693. }
  7694. /*
  7695. * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
  7696. * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
  7697. * tag can be either in metadata or in packet data; therefore if the
  7698. * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
  7699. * header for VLAN tag. As the decision is done at run time, we need
  7700. * update variable part of the offsets
  7701. */
  7702. static struct block *
  7703. gen_vlan_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num,
  7704. int has_vlan_tag)
  7705. {
  7706. struct block *b0, *b_tpid, *b_vid = NULL;
  7707. struct slist *s;
  7708. /* generate new filter code based on extracting packet
  7709. * metadata */
  7710. s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);
  7711. s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;
  7712. b0 = new_block(cstate, JMP(BPF_JEQ));
  7713. b0->stmts = s;
  7714. b0->s.k = 1;
  7715. /*
  7716. * This is tricky. We need to insert the statements updating variable
  7717. * parts of offsets before the the traditional TPID and VID tests so
  7718. * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
  7719. * we do not want this update to affect those checks. That's why we
  7720. * generate both test blocks first and insert the statements updating
  7721. * variable parts of both offsets after that. This wouldn't work if
  7722. * there already were variable length link header when entering this
  7723. * function but gen_vlan_bpf_extensions() isn't called in that case.
  7724. */
  7725. b_tpid = gen_vlan_tpid_test(cstate);
  7726. if (has_vlan_tag)
  7727. b_vid = gen_vlan_vid_test(cstate, vlan_num);
  7728. gen_vlan_patch_tpid_test(cstate, b_tpid);
  7729. gen_or(b0, b_tpid);
  7730. b0 = b_tpid;
  7731. if (has_vlan_tag) {
  7732. gen_vlan_patch_vid_test(cstate, b_vid);
  7733. gen_and(b0, b_vid);
  7734. b0 = b_vid;
  7735. }
  7736. return b0;
  7737. }
  7738. #endif
  7739. /*
  7740. * support IEEE 802.1Q VLAN trunk over ethernet
  7741. */
  7742. struct block *
  7743. gen_vlan(compiler_state_t *cstate, bpf_u_int32 vlan_num, int has_vlan_tag)
  7744. {
  7745. struct block *b0;
  7746. /*
  7747. * Catch errors reported by us and routines below us, and return NULL
  7748. * on an error.
  7749. */
  7750. if (setjmp(cstate->top_ctx))
  7751. return (NULL);
  7752. /* can't check for VLAN-encapsulated packets inside MPLS */
  7753. if (cstate->label_stack_depth > 0)
  7754. bpf_error(cstate, "no VLAN match after MPLS");
  7755. /*
  7756. * Check for a VLAN packet, and then change the offsets to point
  7757. * to the type and data fields within the VLAN packet. Just
  7758. * increment the offsets, so that we can support a hierarchy, e.g.
  7759. * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
  7760. * VLAN 100.
  7761. *
  7762. * XXX - this is a bit of a kludge. If we were to split the
  7763. * compiler into a parser that parses an expression and
  7764. * generates an expression tree, and a code generator that
  7765. * takes an expression tree (which could come from our
  7766. * parser or from some other parser) and generates BPF code,
  7767. * we could perhaps make the offsets parameters of routines
  7768. * and, in the handler for an "AND" node, pass to subnodes
  7769. * other than the VLAN node the adjusted offsets.
  7770. *
  7771. * This would mean that "vlan" would, instead of changing the
  7772. * behavior of *all* tests after it, change only the behavior
  7773. * of tests ANDed with it. That would change the documented
  7774. * semantics of "vlan", which might break some expressions.
  7775. * However, it would mean that "(vlan and ip) or ip" would check
  7776. * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
  7777. * checking only for VLAN-encapsulated IP, so that could still
  7778. * be considered worth doing; it wouldn't break expressions
  7779. * that are of the form "vlan and ..." or "vlan N and ...",
  7780. * which I suspect are the most common expressions involving
  7781. * "vlan". "vlan or ..." doesn't necessarily do what the user
  7782. * would really want, now, as all the "or ..." tests would
  7783. * be done assuming a VLAN, even though the "or" could be viewed
  7784. * as meaning "or, if this isn't a VLAN packet...".
  7785. */
  7786. switch (cstate->linktype) {
  7787. case DLT_EN10MB:
  7788. case DLT_NETANALYZER:
  7789. case DLT_NETANALYZER_TRANSPARENT:
  7790. #if defined(SKF_AD_VLAN_TAG_PRESENT)
  7791. /* Verify that this is the outer part of the packet and
  7792. * not encapsulated somehow. */
  7793. if (cstate->vlan_stack_depth == 0 && !cstate->off_linkhdr.is_variable &&
  7794. cstate->off_linkhdr.constant_part ==
  7795. cstate->off_outermostlinkhdr.constant_part) {
  7796. /*
  7797. * Do we need special VLAN handling?
  7798. */
  7799. if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING)
  7800. b0 = gen_vlan_bpf_extensions(cstate, vlan_num,
  7801. has_vlan_tag);
  7802. else
  7803. b0 = gen_vlan_no_bpf_extensions(cstate,
  7804. vlan_num, has_vlan_tag);
  7805. } else
  7806. #endif
  7807. b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num,
  7808. has_vlan_tag);
  7809. break;
  7810. case DLT_IEEE802_11:
  7811. case DLT_PRISM_HEADER:
  7812. case DLT_IEEE802_11_RADIO_AVS:
  7813. case DLT_IEEE802_11_RADIO:
  7814. b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, has_vlan_tag);
  7815. break;
  7816. default:
  7817. bpf_error(cstate, "no VLAN support for %s",
  7818. pcap_datalink_val_to_description_or_dlt(cstate->linktype));
  7819. /*NOTREACHED*/
  7820. }
  7821. cstate->vlan_stack_depth++;
  7822. return (b0);
  7823. }
  7824. /*
  7825. * support for MPLS
  7826. *
  7827. * The label_num_arg dance is to avoid annoying whining by compilers that
  7828. * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
  7829. * It's not *used* after setjmp returns.
  7830. */
  7831. struct block *
  7832. gen_mpls(compiler_state_t *cstate, bpf_u_int32 label_num_arg,
  7833. int has_label_num)
  7834. {
  7835. volatile bpf_u_int32 label_num = label_num_arg;
  7836. struct block *b0, *b1;
  7837. /*
  7838. * Catch errors reported by us and routines below us, and return NULL
  7839. * on an error.
  7840. */
  7841. if (setjmp(cstate->top_ctx))
  7842. return (NULL);
  7843. if (cstate->label_stack_depth > 0) {
  7844. /* just match the bottom-of-stack bit clear */
  7845. b0 = gen_mcmp(cstate, OR_PREVMPLSHDR, 2, BPF_B, 0, 0x01);
  7846. } else {
  7847. /*
  7848. * We're not in an MPLS stack yet, so check the link-layer
  7849. * type against MPLS.
  7850. */
  7851. switch (cstate->linktype) {
  7852. case DLT_C_HDLC: /* fall through */
  7853. case DLT_EN10MB:
  7854. case DLT_NETANALYZER:
  7855. case DLT_NETANALYZER_TRANSPARENT:
  7856. b0 = gen_linktype(cstate, ETHERTYPE_MPLS);
  7857. break;
  7858. case DLT_PPP:
  7859. b0 = gen_linktype(cstate, PPP_MPLS_UCAST);
  7860. break;
  7861. /* FIXME add other DLT_s ...
  7862. * for Frame-Relay/and ATM this may get messy due to SNAP headers
  7863. * leave it for now */
  7864. default:
  7865. bpf_error(cstate, "no MPLS support for %s",
  7866. pcap_datalink_val_to_description_or_dlt(cstate->linktype));
  7867. /*NOTREACHED*/
  7868. }
  7869. }
  7870. /* If a specific MPLS label is requested, check it */
  7871. if (has_label_num) {
  7872. if (label_num > 0xFFFFF) {
  7873. bpf_error(cstate, "MPLS label %u greater than maximum %u",
  7874. label_num, 0xFFFFF);
  7875. }
  7876. label_num = label_num << 12; /* label is shifted 12 bits on the wire */
  7877. b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, label_num,
  7878. 0xfffff000); /* only compare the first 20 bits */
  7879. gen_and(b0, b1);
  7880. b0 = b1;
  7881. }
  7882. /*
  7883. * Change the offsets to point to the type and data fields within
  7884. * the MPLS packet. Just increment the offsets, so that we
  7885. * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
  7886. * capture packets with an outer label of 100000 and an inner
  7887. * label of 1024.
  7888. *
  7889. * Increment the MPLS stack depth as well; this indicates that
  7890. * we're checking MPLS-encapsulated headers, to make sure higher
  7891. * level code generators don't try to match against IP-related
  7892. * protocols such as Q_ARP, Q_RARP etc.
  7893. *
  7894. * XXX - this is a bit of a kludge. See comments in gen_vlan().
  7895. */
  7896. cstate->off_nl_nosnap += 4;
  7897. cstate->off_nl += 4;
  7898. cstate->label_stack_depth++;
  7899. return (b0);
  7900. }
  7901. /*
  7902. * Support PPPOE discovery and session.
  7903. */
  7904. struct block *
  7905. gen_pppoed(compiler_state_t *cstate)
  7906. {
  7907. /*
  7908. * Catch errors reported by us and routines below us, and return NULL
  7909. * on an error.
  7910. */
  7911. if (setjmp(cstate->top_ctx))
  7912. return (NULL);
  7913. /* check for PPPoE discovery */
  7914. return gen_linktype(cstate, ETHERTYPE_PPPOED);
  7915. }
  7916. struct block *
  7917. gen_pppoes(compiler_state_t *cstate, bpf_u_int32 sess_num, int has_sess_num)
  7918. {
  7919. struct block *b0, *b1;
  7920. /*
  7921. * Catch errors reported by us and routines below us, and return NULL
  7922. * on an error.
  7923. */
  7924. if (setjmp(cstate->top_ctx))
  7925. return (NULL);
  7926. /*
  7927. * Test against the PPPoE session link-layer type.
  7928. */
  7929. b0 = gen_linktype(cstate, ETHERTYPE_PPPOES);
  7930. /* If a specific session is requested, check PPPoE session id */
  7931. if (has_sess_num) {
  7932. if (sess_num > 0x0000ffff) {
  7933. bpf_error(cstate, "PPPoE session number %u greater than maximum %u",
  7934. sess_num, 0x0000ffff);
  7935. }
  7936. b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, sess_num, 0x0000ffff);
  7937. gen_and(b0, b1);
  7938. b0 = b1;
  7939. }
  7940. /*
  7941. * Change the offsets to point to the type and data fields within
  7942. * the PPP packet, and note that this is PPPoE rather than
  7943. * raw PPP.
  7944. *
  7945. * XXX - this is a bit of a kludge. See the comments in
  7946. * gen_vlan().
  7947. *
  7948. * The "network-layer" protocol is PPPoE, which has a 6-byte
  7949. * PPPoE header, followed by a PPP packet.
  7950. *
  7951. * There is no HDLC encapsulation for the PPP packet (it's
  7952. * encapsulated in PPPoES instead), so the link-layer type
  7953. * starts at the first byte of the PPP packet. For PPPoE,
  7954. * that offset is relative to the beginning of the total
  7955. * link-layer payload, including any 802.2 LLC header, so
  7956. * it's 6 bytes past cstate->off_nl.
  7957. */
  7958. PUSH_LINKHDR(cstate, DLT_PPP, cstate->off_linkpl.is_variable,
  7959. cstate->off_linkpl.constant_part + cstate->off_nl + 6, /* 6 bytes past the PPPoE header */
  7960. cstate->off_linkpl.reg);
  7961. cstate->off_linktype = cstate->off_linkhdr;
  7962. cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 2;
  7963. cstate->off_nl = 0;
  7964. cstate->off_nl_nosnap = 0; /* no 802.2 LLC */
  7965. return b0;
  7966. }
  7967. /* Check that this is Geneve and the VNI is correct if
  7968. * specified. Parameterized to handle both IPv4 and IPv6. */
  7969. static struct block *
  7970. gen_geneve_check(compiler_state_t *cstate,
  7971. struct block *(*gen_portfn)(compiler_state_t *, u_int, int, int),
  7972. enum e_offrel offrel, bpf_u_int32 vni, int has_vni)
  7973. {
  7974. struct block *b0, *b1;
  7975. b0 = gen_portfn(cstate, GENEVE_PORT, IPPROTO_UDP, Q_DST);
  7976. /* Check that we are operating on version 0. Otherwise, we
  7977. * can't decode the rest of the fields. The version is 2 bits
  7978. * in the first byte of the Geneve header. */
  7979. b1 = gen_mcmp(cstate, offrel, 8, BPF_B, 0, 0xc0);
  7980. gen_and(b0, b1);
  7981. b0 = b1;
  7982. if (has_vni) {
  7983. if (vni > 0xffffff) {
  7984. bpf_error(cstate, "Geneve VNI %u greater than maximum %u",
  7985. vni, 0xffffff);
  7986. }
  7987. vni <<= 8; /* VNI is in the upper 3 bytes */
  7988. b1 = gen_mcmp(cstate, offrel, 12, BPF_W, vni, 0xffffff00);
  7989. gen_and(b0, b1);
  7990. b0 = b1;
  7991. }
  7992. return b0;
  7993. }
  7994. /* The IPv4 and IPv6 Geneve checks need to do two things:
  7995. * - Verify that this actually is Geneve with the right VNI.
  7996. * - Place the IP header length (plus variable link prefix if
  7997. * needed) into register A to be used later to compute
  7998. * the inner packet offsets. */
  7999. static struct block *
  8000. gen_geneve4(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
  8001. {
  8002. struct block *b0, *b1;
  8003. struct slist *s, *s1;
  8004. b0 = gen_geneve_check(cstate, gen_port, OR_TRAN_IPV4, vni, has_vni);
  8005. /* Load the IP header length into A. */
  8006. s = gen_loadx_iphdrlen(cstate);
  8007. s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
  8008. sappend(s, s1);
  8009. /* Forcibly append these statements to the true condition
  8010. * of the protocol check by creating a new block that is
  8011. * always true and ANDing them. */
  8012. b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
  8013. b1->stmts = s;
  8014. b1->s.k = 0;
  8015. gen_and(b0, b1);
  8016. return b1;
  8017. }
  8018. static struct block *
  8019. gen_geneve6(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
  8020. {
  8021. struct block *b0, *b1;
  8022. struct slist *s, *s1;
  8023. b0 = gen_geneve_check(cstate, gen_port6, OR_TRAN_IPV6, vni, has_vni);
  8024. /* Load the IP header length. We need to account for a
  8025. * variable length link prefix if there is one. */
  8026. s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);
  8027. if (s) {
  8028. s1 = new_stmt(cstate, BPF_LD|BPF_IMM);
  8029. s1->s.k = 40;
  8030. sappend(s, s1);
  8031. s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
  8032. s1->s.k = 0;
  8033. sappend(s, s1);
  8034. } else {
  8035. s = new_stmt(cstate, BPF_LD|BPF_IMM);
  8036. s->s.k = 40;
  8037. }
  8038. /* Forcibly append these statements to the true condition
  8039. * of the protocol check by creating a new block that is
  8040. * always true and ANDing them. */
  8041. s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  8042. sappend(s, s1);
  8043. b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
  8044. b1->stmts = s;
  8045. b1->s.k = 0;
  8046. gen_and(b0, b1);
  8047. return b1;
  8048. }
  8049. /* We need to store three values based on the Geneve header::
  8050. * - The offset of the linktype.
  8051. * - The offset of the end of the Geneve header.
  8052. * - The offset of the end of the encapsulated MAC header. */
  8053. static struct slist *
  8054. gen_geneve_offsets(compiler_state_t *cstate)
  8055. {
  8056. struct slist *s, *s1, *s_proto;
  8057. /* First we need to calculate the offset of the Geneve header
  8058. * itself. This is composed of the IP header previously calculated
  8059. * (include any variable link prefix) and stored in A plus the
  8060. * fixed sized headers (fixed link prefix, MAC length, and UDP
  8061. * header). */
  8062. s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  8063. s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8;
  8064. /* Stash this in X since we'll need it later. */
  8065. s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  8066. sappend(s, s1);
  8067. /* The EtherType in Geneve is 2 bytes in. Calculate this and
  8068. * store it. */
  8069. s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  8070. s1->s.k = 2;
  8071. sappend(s, s1);
  8072. cstate->off_linktype.reg = alloc_reg(cstate);
  8073. cstate->off_linktype.is_variable = 1;
  8074. cstate->off_linktype.constant_part = 0;
  8075. s1 = new_stmt(cstate, BPF_ST);
  8076. s1->s.k = cstate->off_linktype.reg;
  8077. sappend(s, s1);
  8078. /* Load the Geneve option length and mask and shift to get the
  8079. * number of bytes. It is stored in the first byte of the Geneve
  8080. * header. */
  8081. s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);
  8082. s1->s.k = 0;
  8083. sappend(s, s1);
  8084. s1 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);
  8085. s1->s.k = 0x3f;
  8086. sappend(s, s1);
  8087. s1 = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);
  8088. s1->s.k = 4;
  8089. sappend(s, s1);
  8090. /* Add in the rest of the Geneve base header. */
  8091. s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  8092. s1->s.k = 8;
  8093. sappend(s, s1);
  8094. /* Add the Geneve header length to its offset and store. */
  8095. s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);
  8096. s1->s.k = 0;
  8097. sappend(s, s1);
  8098. /* Set the encapsulated type as Ethernet. Even though we may
  8099. * not actually have Ethernet inside there are two reasons this
  8100. * is useful:
  8101. * - The linktype field is always in EtherType format regardless
  8102. * of whether it is in Geneve or an inner Ethernet frame.
  8103. * - The only link layer that we have specific support for is
  8104. * Ethernet. We will confirm that the packet actually is
  8105. * Ethernet at runtime before executing these checks. */
  8106. PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate));
  8107. s1 = new_stmt(cstate, BPF_ST);
  8108. s1->s.k = cstate->off_linkhdr.reg;
  8109. sappend(s, s1);
  8110. /* Calculate whether we have an Ethernet header or just raw IP/
  8111. * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
  8112. * and linktype by 14 bytes so that the network header can be found
  8113. * seamlessly. Otherwise, keep what we've calculated already. */
  8114. /* We have a bare jmp so we can't use the optimizer. */
  8115. cstate->no_optimize = 1;
  8116. /* Load the EtherType in the Geneve header, 2 bytes in. */
  8117. s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_H);
  8118. s1->s.k = 2;
  8119. sappend(s, s1);
  8120. /* Load X with the end of the Geneve header. */
  8121. s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
  8122. s1->s.k = cstate->off_linkhdr.reg;
  8123. sappend(s, s1);
  8124. /* Check if the EtherType is Transparent Ethernet Bridging. At the
  8125. * end of this check, we should have the total length in X. In
  8126. * the non-Ethernet case, it's already there. */
  8127. s_proto = new_stmt(cstate, JMP(BPF_JEQ));
  8128. s_proto->s.k = ETHERTYPE_TEB;
  8129. sappend(s, s_proto);
  8130. s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);
  8131. sappend(s, s1);
  8132. s_proto->s.jt = s1;
  8133. /* Since this is Ethernet, use the EtherType of the payload
  8134. * directly as the linktype. Overwrite what we already have. */
  8135. s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  8136. s1->s.k = 12;
  8137. sappend(s, s1);
  8138. s1 = new_stmt(cstate, BPF_ST);
  8139. s1->s.k = cstate->off_linktype.reg;
  8140. sappend(s, s1);
  8141. /* Advance two bytes further to get the end of the Ethernet
  8142. * header. */
  8143. s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);
  8144. s1->s.k = 2;
  8145. sappend(s, s1);
  8146. /* Move the result to X. */
  8147. s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);
  8148. sappend(s, s1);
  8149. /* Store the final result of our linkpl calculation. */
  8150. cstate->off_linkpl.reg = alloc_reg(cstate);
  8151. cstate->off_linkpl.is_variable = 1;
  8152. cstate->off_linkpl.constant_part = 0;
  8153. s1 = new_stmt(cstate, BPF_STX);
  8154. s1->s.k = cstate->off_linkpl.reg;
  8155. sappend(s, s1);
  8156. s_proto->s.jf = s1;
  8157. cstate->off_nl = 0;
  8158. return s;
  8159. }
  8160. /* Check to see if this is a Geneve packet. */
  8161. struct block *
  8162. gen_geneve(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)
  8163. {
  8164. struct block *b0, *b1;
  8165. struct slist *s;
  8166. /*
  8167. * Catch errors reported by us and routines below us, and return NULL
  8168. * on an error.
  8169. */
  8170. if (setjmp(cstate->top_ctx))
  8171. return (NULL);
  8172. b0 = gen_geneve4(cstate, vni, has_vni);
  8173. b1 = gen_geneve6(cstate, vni, has_vni);
  8174. gen_or(b0, b1);
  8175. b0 = b1;
  8176. /* Later filters should act on the payload of the Geneve frame,
  8177. * update all of the header pointers. Attach this code so that
  8178. * it gets executed in the event that the Geneve filter matches. */
  8179. s = gen_geneve_offsets(cstate);
  8180. b1 = gen_true(cstate);
  8181. sappend(s, b1->stmts);
  8182. b1->stmts = s;
  8183. gen_and(b0, b1);
  8184. cstate->is_geneve = 1;
  8185. return b1;
  8186. }
  8187. /* Check that the encapsulated frame has a link layer header
  8188. * for Ethernet filters. */
  8189. static struct block *
  8190. gen_geneve_ll_check(compiler_state_t *cstate)
  8191. {
  8192. struct block *b0;
  8193. struct slist *s, *s1;
  8194. /* The easiest way to see if there is a link layer present
  8195. * is to check if the link layer header and payload are not
  8196. * the same. */
  8197. /* Geneve always generates pure variable offsets so we can
  8198. * compare only the registers. */
  8199. s = new_stmt(cstate, BPF_LD|BPF_MEM);
  8200. s->s.k = cstate->off_linkhdr.reg;
  8201. s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);
  8202. s1->s.k = cstate->off_linkpl.reg;
  8203. sappend(s, s1);
  8204. b0 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);
  8205. b0->stmts = s;
  8206. b0->s.k = 0;
  8207. gen_not(b0);
  8208. return b0;
  8209. }
  8210. static struct block *
  8211. gen_atmfield_code_internal(compiler_state_t *cstate, int atmfield,
  8212. bpf_u_int32 jvalue, int jtype, int reverse)
  8213. {
  8214. struct block *b0;
  8215. switch (atmfield) {
  8216. case A_VPI:
  8217. if (!cstate->is_atm)
  8218. bpf_error(cstate, "'vpi' supported only on raw ATM");
  8219. if (cstate->off_vpi == OFFSET_NOT_SET)
  8220. abort();
  8221. b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vpi, BPF_B,
  8222. 0xffffffffU, jtype, reverse, jvalue);
  8223. break;
  8224. case A_VCI:
  8225. if (!cstate->is_atm)
  8226. bpf_error(cstate, "'vci' supported only on raw ATM");
  8227. if (cstate->off_vci == OFFSET_NOT_SET)
  8228. abort();
  8229. b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vci, BPF_H,
  8230. 0xffffffffU, jtype, reverse, jvalue);
  8231. break;
  8232. case A_PROTOTYPE:
  8233. if (cstate->off_proto == OFFSET_NOT_SET)
  8234. abort(); /* XXX - this isn't on FreeBSD */
  8235. b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B,
  8236. 0x0fU, jtype, reverse, jvalue);
  8237. break;
  8238. case A_MSGTYPE:
  8239. if (cstate->off_payload == OFFSET_NOT_SET)
  8240. abort();
  8241. b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_payload + MSG_TYPE_POS, BPF_B,
  8242. 0xffffffffU, jtype, reverse, jvalue);
  8243. break;
  8244. case A_CALLREFTYPE:
  8245. if (!cstate->is_atm)
  8246. bpf_error(cstate, "'callref' supported only on raw ATM");
  8247. if (cstate->off_proto == OFFSET_NOT_SET)
  8248. abort();
  8249. b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B,
  8250. 0xffffffffU, jtype, reverse, jvalue);
  8251. break;
  8252. default:
  8253. abort();
  8254. }
  8255. return b0;
  8256. }
  8257. static struct block *
  8258. gen_atmtype_metac(compiler_state_t *cstate)
  8259. {
  8260. struct block *b0, *b1;
  8261. b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
  8262. b1 = gen_atmfield_code_internal(cstate, A_VCI, 1, BPF_JEQ, 0);
  8263. gen_and(b0, b1);
  8264. return b1;
  8265. }
  8266. static struct block *
  8267. gen_atmtype_sc(compiler_state_t *cstate)
  8268. {
  8269. struct block *b0, *b1;
  8270. b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
  8271. b1 = gen_atmfield_code_internal(cstate, A_VCI, 5, BPF_JEQ, 0);
  8272. gen_and(b0, b1);
  8273. return b1;
  8274. }
  8275. static struct block *
  8276. gen_atmtype_llc(compiler_state_t *cstate)
  8277. {
  8278. struct block *b0;
  8279. b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
  8280. cstate->linktype = cstate->prevlinktype;
  8281. return b0;
  8282. }
  8283. struct block *
  8284. gen_atmfield_code(compiler_state_t *cstate, int atmfield,
  8285. bpf_u_int32 jvalue, int jtype, int reverse)
  8286. {
  8287. /*
  8288. * Catch errors reported by us and routines below us, and return NULL
  8289. * on an error.
  8290. */
  8291. if (setjmp(cstate->top_ctx))
  8292. return (NULL);
  8293. return gen_atmfield_code_internal(cstate, atmfield, jvalue, jtype,
  8294. reverse);
  8295. }
  8296. struct block *
  8297. gen_atmtype_abbrev(compiler_state_t *cstate, int type)
  8298. {
  8299. struct block *b0, *b1;
  8300. /*
  8301. * Catch errors reported by us and routines below us, and return NULL
  8302. * on an error.
  8303. */
  8304. if (setjmp(cstate->top_ctx))
  8305. return (NULL);
  8306. switch (type) {
  8307. case A_METAC:
  8308. /* Get all packets in Meta signalling Circuit */
  8309. if (!cstate->is_atm)
  8310. bpf_error(cstate, "'metac' supported only on raw ATM");
  8311. b1 = gen_atmtype_metac(cstate);
  8312. break;
  8313. case A_BCC:
  8314. /* Get all packets in Broadcast Circuit*/
  8315. if (!cstate->is_atm)
  8316. bpf_error(cstate, "'bcc' supported only on raw ATM");
  8317. b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
  8318. b1 = gen_atmfield_code_internal(cstate, A_VCI, 2, BPF_JEQ, 0);
  8319. gen_and(b0, b1);
  8320. break;
  8321. case A_OAMF4SC:
  8322. /* Get all cells in Segment OAM F4 circuit*/
  8323. if (!cstate->is_atm)
  8324. bpf_error(cstate, "'oam4sc' supported only on raw ATM");
  8325. b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
  8326. b1 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
  8327. gen_and(b0, b1);
  8328. break;
  8329. case A_OAMF4EC:
  8330. /* Get all cells in End-to-End OAM F4 Circuit*/
  8331. if (!cstate->is_atm)
  8332. bpf_error(cstate, "'oam4ec' supported only on raw ATM");
  8333. b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
  8334. b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
  8335. gen_and(b0, b1);
  8336. break;
  8337. case A_SC:
  8338. /* Get all packets in connection Signalling Circuit */
  8339. if (!cstate->is_atm)
  8340. bpf_error(cstate, "'sc' supported only on raw ATM");
  8341. b1 = gen_atmtype_sc(cstate);
  8342. break;
  8343. case A_ILMIC:
  8344. /* Get all packets in ILMI Circuit */
  8345. if (!cstate->is_atm)
  8346. bpf_error(cstate, "'ilmic' supported only on raw ATM");
  8347. b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
  8348. b1 = gen_atmfield_code_internal(cstate, A_VCI, 16, BPF_JEQ, 0);
  8349. gen_and(b0, b1);
  8350. break;
  8351. case A_LANE:
  8352. /* Get all LANE packets */
  8353. if (!cstate->is_atm)
  8354. bpf_error(cstate, "'lane' supported only on raw ATM");
  8355. b1 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
  8356. /*
  8357. * Arrange that all subsequent tests assume LANE
  8358. * rather than LLC-encapsulated packets, and set
  8359. * the offsets appropriately for LANE-encapsulated
  8360. * Ethernet.
  8361. *
  8362. * We assume LANE means Ethernet, not Token Ring.
  8363. */
  8364. PUSH_LINKHDR(cstate, DLT_EN10MB, 0,
  8365. cstate->off_payload + 2, /* Ethernet header */
  8366. -1);
  8367. cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;
  8368. cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* Ethernet */
  8369. cstate->off_nl = 0; /* Ethernet II */
  8370. cstate->off_nl_nosnap = 3; /* 802.3+802.2 */
  8371. break;
  8372. case A_LLC:
  8373. /* Get all LLC-encapsulated packets */
  8374. if (!cstate->is_atm)
  8375. bpf_error(cstate, "'llc' supported only on raw ATM");
  8376. b1 = gen_atmtype_llc(cstate);
  8377. break;
  8378. default:
  8379. abort();
  8380. }
  8381. return b1;
  8382. }
  8383. /*
  8384. * Filtering for MTP2 messages based on li value
  8385. * FISU, length is null
  8386. * LSSU, length is 1 or 2
  8387. * MSU, length is 3 or more
  8388. * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
  8389. */
  8390. struct block *
  8391. gen_mtp2type_abbrev(compiler_state_t *cstate, int type)
  8392. {
  8393. struct block *b0, *b1;
  8394. /*
  8395. * Catch errors reported by us and routines below us, and return NULL
  8396. * on an error.
  8397. */
  8398. if (setjmp(cstate->top_ctx))
  8399. return (NULL);
  8400. switch (type) {
  8401. case M_FISU:
  8402. if ( (cstate->linktype != DLT_MTP2) &&
  8403. (cstate->linktype != DLT_ERF) &&
  8404. (cstate->linktype != DLT_MTP2_WITH_PHDR) )
  8405. bpf_error(cstate, "'fisu' supported only on MTP2");
  8406. /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
  8407. b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
  8408. 0x3fU, BPF_JEQ, 0, 0U);
  8409. break;
  8410. case M_LSSU:
  8411. if ( (cstate->linktype != DLT_MTP2) &&
  8412. (cstate->linktype != DLT_ERF) &&
  8413. (cstate->linktype != DLT_MTP2_WITH_PHDR) )
  8414. bpf_error(cstate, "'lssu' supported only on MTP2");
  8415. b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
  8416. 0x3fU, BPF_JGT, 1, 2U);
  8417. b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
  8418. 0x3fU, BPF_JGT, 0, 0U);
  8419. gen_and(b1, b0);
  8420. break;
  8421. case M_MSU:
  8422. if ( (cstate->linktype != DLT_MTP2) &&
  8423. (cstate->linktype != DLT_ERF) &&
  8424. (cstate->linktype != DLT_MTP2_WITH_PHDR) )
  8425. bpf_error(cstate, "'msu' supported only on MTP2");
  8426. b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,
  8427. 0x3fU, BPF_JGT, 0, 2U);
  8428. break;
  8429. case MH_FISU:
  8430. if ( (cstate->linktype != DLT_MTP2) &&
  8431. (cstate->linktype != DLT_ERF) &&
  8432. (cstate->linktype != DLT_MTP2_WITH_PHDR) )
  8433. bpf_error(cstate, "'hfisu' supported only on MTP2_HSL");
  8434. /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
  8435. b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
  8436. 0xff80U, BPF_JEQ, 0, 0U);
  8437. break;
  8438. case MH_LSSU:
  8439. if ( (cstate->linktype != DLT_MTP2) &&
  8440. (cstate->linktype != DLT_ERF) &&
  8441. (cstate->linktype != DLT_MTP2_WITH_PHDR) )
  8442. bpf_error(cstate, "'hlssu' supported only on MTP2_HSL");
  8443. b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
  8444. 0xff80U, BPF_JGT, 1, 0x0100U);
  8445. b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
  8446. 0xff80U, BPF_JGT, 0, 0U);
  8447. gen_and(b1, b0);
  8448. break;
  8449. case MH_MSU:
  8450. if ( (cstate->linktype != DLT_MTP2) &&
  8451. (cstate->linktype != DLT_ERF) &&
  8452. (cstate->linktype != DLT_MTP2_WITH_PHDR) )
  8453. bpf_error(cstate, "'hmsu' supported only on MTP2_HSL");
  8454. b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,
  8455. 0xff80U, BPF_JGT, 0, 0x0100U);
  8456. break;
  8457. default:
  8458. abort();
  8459. }
  8460. return b0;
  8461. }
  8462. /*
  8463. * The jvalue_arg dance is to avoid annoying whining by compilers that
  8464. * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
  8465. * It's not *used* after setjmp returns.
  8466. */
  8467. struct block *
  8468. gen_mtp3field_code(compiler_state_t *cstate, int mtp3field,
  8469. bpf_u_int32 jvalue_arg, int jtype, int reverse)
  8470. {
  8471. volatile bpf_u_int32 jvalue = jvalue_arg;
  8472. struct block *b0;
  8473. bpf_u_int32 val1 , val2 , val3;
  8474. u_int newoff_sio;
  8475. u_int newoff_opc;
  8476. u_int newoff_dpc;
  8477. u_int newoff_sls;
  8478. /*
  8479. * Catch errors reported by us and routines below us, and return NULL
  8480. * on an error.
  8481. */
  8482. if (setjmp(cstate->top_ctx))
  8483. return (NULL);
  8484. newoff_sio = cstate->off_sio;
  8485. newoff_opc = cstate->off_opc;
  8486. newoff_dpc = cstate->off_dpc;
  8487. newoff_sls = cstate->off_sls;
  8488. switch (mtp3field) {
  8489. case MH_SIO:
  8490. newoff_sio += 3; /* offset for MTP2_HSL */
  8491. /* FALLTHROUGH */
  8492. case M_SIO:
  8493. if (cstate->off_sio == OFFSET_NOT_SET)
  8494. bpf_error(cstate, "'sio' supported only on SS7");
  8495. /* sio coded on 1 byte so max value 255 */
  8496. if(jvalue > 255)
  8497. bpf_error(cstate, "sio value %u too big; max value = 255",
  8498. jvalue);
  8499. b0 = gen_ncmp(cstate, OR_PACKET, newoff_sio, BPF_B, 0xffffffffU,
  8500. jtype, reverse, jvalue);
  8501. break;
  8502. case MH_OPC:
  8503. newoff_opc += 3;
  8504. /* FALLTHROUGH */
  8505. case M_OPC:
  8506. if (cstate->off_opc == OFFSET_NOT_SET)
  8507. bpf_error(cstate, "'opc' supported only on SS7");
  8508. /* opc coded on 14 bits so max value 16383 */
  8509. if (jvalue > 16383)
  8510. bpf_error(cstate, "opc value %u too big; max value = 16383",
  8511. jvalue);
  8512. /* the following instructions are made to convert jvalue
  8513. * to the form used to write opc in an ss7 message*/
  8514. val1 = jvalue & 0x00003c00;
  8515. val1 = val1 >>10;
  8516. val2 = jvalue & 0x000003fc;
  8517. val2 = val2 <<6;
  8518. val3 = jvalue & 0x00000003;
  8519. val3 = val3 <<22;
  8520. jvalue = val1 + val2 + val3;
  8521. b0 = gen_ncmp(cstate, OR_PACKET, newoff_opc, BPF_W, 0x00c0ff0fU,
  8522. jtype, reverse, jvalue);
  8523. break;
  8524. case MH_DPC:
  8525. newoff_dpc += 3;
  8526. /* FALLTHROUGH */
  8527. case M_DPC:
  8528. if (cstate->off_dpc == OFFSET_NOT_SET)
  8529. bpf_error(cstate, "'dpc' supported only on SS7");
  8530. /* dpc coded on 14 bits so max value 16383 */
  8531. if (jvalue > 16383)
  8532. bpf_error(cstate, "dpc value %u too big; max value = 16383",
  8533. jvalue);
  8534. /* the following instructions are made to convert jvalue
  8535. * to the forme used to write dpc in an ss7 message*/
  8536. val1 = jvalue & 0x000000ff;
  8537. val1 = val1 << 24;
  8538. val2 = jvalue & 0x00003f00;
  8539. val2 = val2 << 8;
  8540. jvalue = val1 + val2;
  8541. b0 = gen_ncmp(cstate, OR_PACKET, newoff_dpc, BPF_W, 0xff3f0000U,
  8542. jtype, reverse, jvalue);
  8543. break;
  8544. case MH_SLS:
  8545. newoff_sls += 3;
  8546. /* FALLTHROUGH */
  8547. case M_SLS:
  8548. if (cstate->off_sls == OFFSET_NOT_SET)
  8549. bpf_error(cstate, "'sls' supported only on SS7");
  8550. /* sls coded on 4 bits so max value 15 */
  8551. if (jvalue > 15)
  8552. bpf_error(cstate, "sls value %u too big; max value = 15",
  8553. jvalue);
  8554. /* the following instruction is made to convert jvalue
  8555. * to the forme used to write sls in an ss7 message*/
  8556. jvalue = jvalue << 4;
  8557. b0 = gen_ncmp(cstate, OR_PACKET, newoff_sls, BPF_B, 0xf0U,
  8558. jtype, reverse, jvalue);
  8559. break;
  8560. default:
  8561. abort();
  8562. }
  8563. return b0;
  8564. }
  8565. static struct block *
  8566. gen_msg_abbrev(compiler_state_t *cstate, int type)
  8567. {
  8568. struct block *b1;
  8569. /*
  8570. * Q.2931 signalling protocol messages for handling virtual circuits
  8571. * establishment and teardown
  8572. */
  8573. switch (type) {
  8574. case A_SETUP:
  8575. b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, SETUP, BPF_JEQ, 0);
  8576. break;
  8577. case A_CALLPROCEED:
  8578. b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
  8579. break;
  8580. case A_CONNECT:
  8581. b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT, BPF_JEQ, 0);
  8582. break;
  8583. case A_CONNECTACK:
  8584. b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
  8585. break;
  8586. case A_RELEASE:
  8587. b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE, BPF_JEQ, 0);
  8588. break;
  8589. case A_RELEASE_DONE:
  8590. b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
  8591. break;
  8592. default:
  8593. abort();
  8594. }
  8595. return b1;
  8596. }
  8597. struct block *
  8598. gen_atmmulti_abbrev(compiler_state_t *cstate, int type)
  8599. {
  8600. struct block *b0, *b1;
  8601. /*
  8602. * Catch errors reported by us and routines below us, and return NULL
  8603. * on an error.
  8604. */
  8605. if (setjmp(cstate->top_ctx))
  8606. return (NULL);
  8607. switch (type) {
  8608. case A_OAM:
  8609. if (!cstate->is_atm)
  8610. bpf_error(cstate, "'oam' supported only on raw ATM");
  8611. /* OAM F4 type */
  8612. b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
  8613. b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
  8614. gen_or(b0, b1);
  8615. b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
  8616. gen_and(b0, b1);
  8617. break;
  8618. case A_OAMF4:
  8619. if (!cstate->is_atm)
  8620. bpf_error(cstate, "'oamf4' supported only on raw ATM");
  8621. /* OAM F4 type */
  8622. b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);
  8623. b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);
  8624. gen_or(b0, b1);
  8625. b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);
  8626. gen_and(b0, b1);
  8627. break;
  8628. case A_CONNECTMSG:
  8629. /*
  8630. * Get Q.2931 signalling messages for switched
  8631. * virtual connection
  8632. */
  8633. if (!cstate->is_atm)
  8634. bpf_error(cstate, "'connectmsg' supported only on raw ATM");
  8635. b0 = gen_msg_abbrev(cstate, A_SETUP);
  8636. b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
  8637. gen_or(b0, b1);
  8638. b0 = gen_msg_abbrev(cstate, A_CONNECT);
  8639. gen_or(b0, b1);
  8640. b0 = gen_msg_abbrev(cstate, A_CONNECTACK);
  8641. gen_or(b0, b1);
  8642. b0 = gen_msg_abbrev(cstate, A_RELEASE);
  8643. gen_or(b0, b1);
  8644. b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
  8645. gen_or(b0, b1);
  8646. b0 = gen_atmtype_sc(cstate);
  8647. gen_and(b0, b1);
  8648. break;
  8649. case A_METACONNECT:
  8650. if (!cstate->is_atm)
  8651. bpf_error(cstate, "'metaconnect' supported only on raw ATM");
  8652. b0 = gen_msg_abbrev(cstate, A_SETUP);
  8653. b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);
  8654. gen_or(b0, b1);
  8655. b0 = gen_msg_abbrev(cstate, A_CONNECT);
  8656. gen_or(b0, b1);
  8657. b0 = gen_msg_abbrev(cstate, A_RELEASE);
  8658. gen_or(b0, b1);
  8659. b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);
  8660. gen_or(b0, b1);
  8661. b0 = gen_atmtype_metac(cstate);
  8662. gen_and(b0, b1);
  8663. break;
  8664. default:
  8665. abort();
  8666. }
  8667. return b1;
  8668. }