gencode.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. */
  21. #include "pcap/funcattrs.h"
  22. /*
  23. * ATM support:
  24. *
  25. * Copyright (c) 1997 Yen Yen Lim and North Dakota State University
  26. * All rights reserved.
  27. *
  28. * Redistribution and use in source and binary forms, with or without
  29. * modification, are permitted provided that the following conditions
  30. * are met:
  31. * 1. Redistributions of source code must retain the above copyright
  32. * notice, this list of conditions and the following disclaimer.
  33. * 2. Redistributions in binary form must reproduce the above copyright
  34. * notice, this list of conditions and the following disclaimer in the
  35. * documentation and/or other materials provided with the distribution.
  36. * 3. All advertising materials mentioning features or use of this software
  37. * must display the following acknowledgement:
  38. * This product includes software developed by Yen Yen Lim and
  39. * North Dakota State University
  40. * 4. The name of the author may not be used to endorse or promote products
  41. * derived from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  44. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  45. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  46. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  47. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  48. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  49. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  50. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  51. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  52. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  53. * POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. /* Address qualifiers. */
  56. #define Q_HOST 1
  57. #define Q_NET 2
  58. #define Q_PORT 3
  59. #define Q_GATEWAY 4
  60. #define Q_PROTO 5
  61. #define Q_PROTOCHAIN 6
  62. #define Q_PORTRANGE 7
  63. /* Protocol qualifiers. */
  64. #define Q_LINK 1
  65. #define Q_IP 2
  66. #define Q_ARP 3
  67. #define Q_RARP 4
  68. #define Q_SCTP 5
  69. #define Q_TCP 6
  70. #define Q_UDP 7
  71. #define Q_ICMP 8
  72. #define Q_IGMP 9
  73. #define Q_IGRP 10
  74. #define Q_ATALK 11
  75. #define Q_DECNET 12
  76. #define Q_LAT 13
  77. #define Q_SCA 14
  78. #define Q_MOPRC 15
  79. #define Q_MOPDL 16
  80. #define Q_IPV6 17
  81. #define Q_ICMPV6 18
  82. #define Q_AH 19
  83. #define Q_ESP 20
  84. #define Q_PIM 21
  85. #define Q_VRRP 22
  86. #define Q_AARP 23
  87. #define Q_ISO 24
  88. #define Q_ESIS 25
  89. #define Q_ISIS 26
  90. #define Q_CLNP 27
  91. #define Q_STP 28
  92. #define Q_IPX 29
  93. #define Q_NETBEUI 30
  94. /* IS-IS Levels */
  95. #define Q_ISIS_L1 31
  96. #define Q_ISIS_L2 32
  97. /* PDU types */
  98. #define Q_ISIS_IIH 33
  99. #define Q_ISIS_LAN_IIH 34
  100. #define Q_ISIS_PTP_IIH 35
  101. #define Q_ISIS_SNP 36
  102. #define Q_ISIS_CSNP 37
  103. #define Q_ISIS_PSNP 38
  104. #define Q_ISIS_LSP 39
  105. #define Q_RADIO 40
  106. #define Q_CARP 41
  107. /* Directional qualifiers. */
  108. #define Q_SRC 1
  109. #define Q_DST 2
  110. #define Q_OR 3
  111. #define Q_AND 4
  112. #define Q_ADDR1 5
  113. #define Q_ADDR2 6
  114. #define Q_ADDR3 7
  115. #define Q_ADDR4 8
  116. #define Q_RA 9
  117. #define Q_TA 10
  118. #define Q_DEFAULT 0
  119. #define Q_UNDEF 255
  120. /* ATM types */
  121. #define A_METAC 22 /* Meta signalling Circuit */
  122. #define A_BCC 23 /* Broadcast Circuit */
  123. #define A_OAMF4SC 24 /* Segment OAM F4 Circuit */
  124. #define A_OAMF4EC 25 /* End-to-End OAM F4 Circuit */
  125. #define A_SC 26 /* Signalling Circuit*/
  126. #define A_ILMIC 27 /* ILMI Circuit */
  127. #define A_OAM 28 /* OAM cells : F4 only */
  128. #define A_OAMF4 29 /* OAM F4 cells: Segment + End-to-end */
  129. #define A_LANE 30 /* LANE traffic */
  130. #define A_LLC 31 /* LLC-encapsulated traffic */
  131. /* Based on Q.2931 signalling protocol */
  132. #define A_SETUP 41 /* Setup message */
  133. #define A_CALLPROCEED 42 /* Call proceeding message */
  134. #define A_CONNECT 43 /* Connect message */
  135. #define A_CONNECTACK 44 /* Connect Ack message */
  136. #define A_RELEASE 45 /* Release message */
  137. #define A_RELEASE_DONE 46 /* Release message */
  138. /* ATM field types */
  139. #define A_VPI 51
  140. #define A_VCI 52
  141. #define A_PROTOTYPE 53
  142. #define A_MSGTYPE 54
  143. #define A_CALLREFTYPE 55
  144. #define A_CONNECTMSG 70 /* returns Q.2931 signalling messages for
  145. establishing and destroying switched
  146. virtual connection */
  147. #define A_METACONNECT 71 /* returns Q.2931 signalling messages for
  148. establishing and destroying predefined
  149. virtual circuits, such as broadcast
  150. circuit, oamf4 segment circuit, oamf4
  151. end-to-end circuits, ILMI circuits or
  152. connection signalling circuit. */
  153. /* MTP2 types */
  154. #define M_FISU 22 /* FISU */
  155. #define M_LSSU 23 /* LSSU */
  156. #define M_MSU 24 /* MSU */
  157. /* MTP2 HSL types */
  158. #define MH_FISU 25 /* FISU for HSL */
  159. #define MH_LSSU 26 /* LSSU */
  160. #define MH_MSU 27 /* MSU */
  161. /* MTP3 field types */
  162. #define M_SIO 1
  163. #define M_OPC 2
  164. #define M_DPC 3
  165. #define M_SLS 4
  166. /* MTP3 field types in case of MTP2 HSL */
  167. #define MH_SIO 5
  168. #define MH_OPC 6
  169. #define MH_DPC 7
  170. #define MH_SLS 8
  171. struct slist;
  172. struct stmt {
  173. int code;
  174. struct slist *jt; /*only for relative jump in block*/
  175. struct slist *jf; /*only for relative jump in block*/
  176. bpf_int32 k;
  177. };
  178. struct slist {
  179. struct stmt s;
  180. struct slist *next;
  181. };
  182. /*
  183. * A bit vector to represent definition sets. We assume TOT_REGISTERS
  184. * is smaller than 8*sizeof(atomset).
  185. */
  186. typedef bpf_u_int32 atomset;
  187. #define ATOMMASK(n) (1 << (n))
  188. #define ATOMELEM(d, n) (d & ATOMMASK(n))
  189. /*
  190. * An unbounded set.
  191. */
  192. typedef bpf_u_int32 *uset;
  193. /*
  194. * Total number of atomic entities, including accumulator (A) and index (X).
  195. * We treat all these guys similarly during flow analysis.
  196. */
  197. #define N_ATOMS (BPF_MEMWORDS+2)
  198. struct edge {
  199. int id;
  200. int code;
  201. uset edom;
  202. struct block *succ;
  203. struct block *pred;
  204. struct edge *next; /* link list of incoming edges for a node */
  205. };
  206. struct block {
  207. int id;
  208. struct slist *stmts; /* side effect stmts */
  209. struct stmt s; /* branch stmt */
  210. int mark;
  211. u_int longjt; /* jt branch requires long jump */
  212. u_int longjf; /* jf branch requires long jump */
  213. int level;
  214. int offset;
  215. int sense;
  216. struct edge et;
  217. struct edge ef;
  218. struct block *head;
  219. struct block *link; /* link field used by optimizer */
  220. uset dom;
  221. uset closure;
  222. struct edge *in_edges;
  223. atomset def, kill;
  224. atomset in_use;
  225. atomset out_use;
  226. int oval;
  227. int val[N_ATOMS];
  228. };
  229. /*
  230. * A value of 0 for val[i] means the value is unknown.
  231. */
  232. #define VAL_UNKNOWN 0
  233. struct arth {
  234. struct block *b; /* protocol checks */
  235. struct slist *s; /* stmt list */
  236. int regno; /* virtual register number of result */
  237. };
  238. struct qual {
  239. unsigned char addr;
  240. unsigned char proto;
  241. unsigned char dir;
  242. unsigned char pad;
  243. };
  244. struct _compiler_state;
  245. typedef struct _compiler_state compiler_state_t;
  246. struct arth *gen_loadi(compiler_state_t *, int);
  247. struct arth *gen_load(compiler_state_t *, int, struct arth *, int);
  248. struct arth *gen_loadlen(compiler_state_t *);
  249. struct arth *gen_neg(compiler_state_t *, struct arth *);
  250. struct arth *gen_arth(compiler_state_t *, int, struct arth *, struct arth *);
  251. void gen_and(struct block *, struct block *);
  252. void gen_or(struct block *, struct block *);
  253. void gen_not(struct block *);
  254. struct block *gen_scode(compiler_state_t *, const char *, struct qual);
  255. struct block *gen_ecode(compiler_state_t *, const u_char *, struct qual);
  256. struct block *gen_acode(compiler_state_t *, const u_char *, struct qual);
  257. struct block *gen_mcode(compiler_state_t *, const char *, const char *,
  258. unsigned int, struct qual);
  259. #ifdef INET6
  260. struct block *gen_mcode6(compiler_state_t *, const char *, const char *,
  261. unsigned int, struct qual);
  262. #endif
  263. struct block *gen_ncode(compiler_state_t *, const char *, bpf_u_int32,
  264. struct qual);
  265. struct block *gen_proto_abbrev(compiler_state_t *, int);
  266. struct block *gen_relation(compiler_state_t *, int, struct arth *,
  267. struct arth *, int);
  268. struct block *gen_less(compiler_state_t *, int);
  269. struct block *gen_greater(compiler_state_t *, int);
  270. struct block *gen_byteop(compiler_state_t *, int, int, int);
  271. struct block *gen_broadcast(compiler_state_t *, int);
  272. struct block *gen_multicast(compiler_state_t *, int);
  273. struct block *gen_inbound(compiler_state_t *, int);
  274. struct block *gen_llc(compiler_state_t *);
  275. struct block *gen_llc_i(compiler_state_t *);
  276. struct block *gen_llc_s(compiler_state_t *);
  277. struct block *gen_llc_u(compiler_state_t *);
  278. struct block *gen_llc_s_subtype(compiler_state_t *, bpf_u_int32);
  279. struct block *gen_llc_u_subtype(compiler_state_t *, bpf_u_int32);
  280. struct block *gen_vlan(compiler_state_t *, int);
  281. struct block *gen_mpls(compiler_state_t *, int);
  282. struct block *gen_pppoed(compiler_state_t *);
  283. struct block *gen_pppoes(compiler_state_t *, int);
  284. struct block *gen_geneve(compiler_state_t *, int);
  285. struct block *gen_atmfield_code(compiler_state_t *, int, bpf_int32,
  286. bpf_u_int32, int);
  287. struct block *gen_atmtype_abbrev(compiler_state_t *, int type);
  288. struct block *gen_atmmulti_abbrev(compiler_state_t *, int type);
  289. struct block *gen_mtp2type_abbrev(compiler_state_t *, int type);
  290. struct block *gen_mtp3field_code(compiler_state_t *, int, bpf_u_int32,
  291. bpf_u_int32, int);
  292. #ifndef HAVE_NET_PFVAR_H
  293. PCAP_NORETURN
  294. #endif
  295. struct block *gen_pf_ifname(compiler_state_t *, const char *);
  296. #ifndef HAVE_NET_PFVAR_H
  297. PCAP_NORETURN
  298. #endif
  299. struct block *gen_pf_rnr(compiler_state_t *, int);
  300. #ifndef HAVE_NET_PFVAR_H
  301. PCAP_NORETURN
  302. #endif
  303. struct block *gen_pf_srnr(compiler_state_t *, int);
  304. #ifndef HAVE_NET_PFVAR_H
  305. PCAP_NORETURN
  306. #endif
  307. struct block *gen_pf_ruleset(compiler_state_t *, char *);
  308. #ifndef HAVE_NET_PFVAR_H
  309. PCAP_NORETURN
  310. #endif
  311. struct block *gen_pf_reason(compiler_state_t *, int);
  312. #ifndef HAVE_NET_PFVAR_H
  313. PCAP_NORETURN
  314. #endif
  315. struct block *gen_pf_action(compiler_state_t *, int);
  316. struct block *gen_p80211_type(compiler_state_t *, int, int);
  317. struct block *gen_p80211_fcdir(compiler_state_t *, int);
  318. /*
  319. * Representation of a program as a tree of blocks, plus current mark.
  320. * A block is marked if only if its mark equals the current mark.
  321. * Rather than traverse the code array, marking each item, 'cur_mark'
  322. * is incremented. This automatically makes each element unmarked.
  323. */
  324. #define isMarked(icp, p) ((p)->mark == (icp)->cur_mark)
  325. #define unMarkAll(icp) (icp)->cur_mark += 1
  326. #define Mark(icp, p) ((p)->mark = (icp)->cur_mark)
  327. struct icode {
  328. struct block *root;
  329. int cur_mark;
  330. };
  331. void bpf_optimize(compiler_state_t *, struct icode *ic);
  332. void PCAP_NORETURN bpf_syntax_error(compiler_state_t *, const char *);
  333. void PCAP_NORETURN bpf_error(compiler_state_t *, const char *, ...)
  334. PCAP_PRINTFLIKE(2, 3);
  335. void finish_parse(compiler_state_t *, struct block *);
  336. char *sdup(compiler_state_t *, const char *);
  337. struct bpf_insn *icode_to_fcode(compiler_state_t *, struct icode *,
  338. struct block *, u_int *);
  339. void sappend(struct slist *, struct slist *);
  340. /*
  341. * Older versions of Bison don't put this declaration in
  342. * grammar.h.
  343. */
  344. int pcap_parse(void *, compiler_state_t *);
  345. /* XXX */
  346. #define JT(b) ((b)->et.succ)
  347. #define JF(b) ((b)->ef.succ)