PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 1ms

/gencode.c

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