ipsec.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* $KAME: ipsec.h,v 1.53 2001/11/20 08:32:38 itojun Exp $ */
  2. /*-
  3. * SPDX-License-Identifier: BSD-3-Clause
  4. *
  5. * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the project nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. /*
  33. * IPsec controller part.
  34. */
  35. #ifndef _NETIPSEC_IPSEC_H_
  36. #define _NETIPSEC_IPSEC_H_
  37. #include <net/pfkeyv2.h>
  38. #include <netipsec/keydb.h>
  39. #ifdef _KERNEL
  40. #include <sys/_lock.h>
  41. #include <sys/_mutex.h>
  42. #include <sys/_rwlock.h>
  43. #include <sys/sysctl.h>
  44. #define IPSEC_ASSERT(_c,_m) KASSERT(_c, _m)
  45. /*
  46. * Security Policy Index
  47. * Ensure that both address families in the "src" and "dst" are same.
  48. * When the value of the ul_proto is ICMPv6, the port field in "src"
  49. * specifies ICMPv6 type, and the port field in "dst" specifies ICMPv6 code.
  50. */
  51. struct secpolicyindex {
  52. union sockaddr_union src; /* IP src address for SP */
  53. union sockaddr_union dst; /* IP dst address for SP */
  54. uint8_t ul_proto; /* upper layer Protocol */
  55. uint8_t dir; /* direction of packet flow */
  56. uint8_t prefs; /* prefix length in bits for src */
  57. uint8_t prefd; /* prefix length in bits for dst */
  58. };
  59. /* Request for IPsec */
  60. struct ipsecrequest {
  61. struct secasindex saidx;/* hint for search proper SA */
  62. /* if __ss_len == 0 then no address specified.*/
  63. u_int level; /* IPsec level defined below. */
  64. };
  65. struct ipsec_accel_adddel_sp_tq {
  66. struct vnet *adddel_vnet;
  67. struct task adddel_task;
  68. int adddel_scheduled;
  69. };
  70. /* Security Policy Data Base */
  71. struct secpolicy {
  72. TAILQ_ENTRY(secpolicy) chain;
  73. LIST_ENTRY(secpolicy) idhash;
  74. LIST_ENTRY(secpolicy) drainq;
  75. struct secpolicyindex spidx; /* selector */
  76. #define IPSEC_MAXREQ 4
  77. struct ipsecrequest *req[IPSEC_MAXREQ];
  78. u_int tcount; /* IPsec transforms count */
  79. volatile u_int refcnt; /* reference count */
  80. u_int policy; /* policy_type per pfkeyv2.h */
  81. u_int state;
  82. #define IPSEC_SPSTATE_DEAD 0
  83. #define IPSEC_SPSTATE_LARVAL 1
  84. #define IPSEC_SPSTATE_ALIVE 2
  85. #define IPSEC_SPSTATE_PCB 3
  86. #define IPSEC_SPSTATE_IFNET 4
  87. uint32_t priority; /* priority of this policy */
  88. uint32_t id; /* It's unique number on the system. */
  89. /*
  90. * lifetime handler.
  91. * the policy can be used without limitiation if both lifetime and
  92. * validtime are zero.
  93. * "lifetime" is passed by sadb_lifetime.sadb_lifetime_addtime.
  94. * "validtime" is passed by sadb_lifetime.sadb_lifetime_usetime.
  95. */
  96. time_t created; /* time created the policy */
  97. time_t lastused; /* updated every when kernel sends a packet */
  98. long lifetime; /* duration of the lifetime of this policy */
  99. long validtime; /* duration this policy is valid without use */
  100. CK_LIST_HEAD(, ifp_handle_sp) accel_ifps;
  101. struct ipsec_accel_adddel_sp_tq accel_add_tq;
  102. struct ipsec_accel_adddel_sp_tq accel_del_tq;
  103. struct inpcb *ipsec_accel_add_sp_inp;
  104. const char *accel_ifname;
  105. };
  106. /*
  107. * PCB security policies.
  108. * Application can setup private security policies for socket.
  109. * Such policies can have IPSEC, BYPASS and ENTRUST type.
  110. * By default, policies are set to NULL. This means that they have ENTRUST type.
  111. * When application sets BYPASS or IPSEC type policy, the flags field
  112. * is also updated. When flags is not set, the system could store
  113. * used security policy into the sp_in/sp_out pointer to speed up further
  114. * lookups.
  115. */
  116. struct inpcbpolicy {
  117. struct secpolicy *sp_in;
  118. struct secpolicy *sp_out;
  119. uint32_t genid;
  120. uint16_t flags;
  121. #define INP_INBOUND_POLICY 0x0001
  122. #define INP_OUTBOUND_POLICY 0x0002
  123. uint16_t hdrsz;
  124. };
  125. /* SP acquiring list table. */
  126. struct secspacq {
  127. LIST_ENTRY(secspacq) chain;
  128. struct secpolicyindex spidx;
  129. time_t created; /* for lifetime */
  130. int count; /* for lifetime */
  131. /* XXX: here is mbuf place holder to be sent ? */
  132. };
  133. #endif /* _KERNEL */
  134. /* buffer size for formatted output of ipsec address */
  135. #define IPSEC_ADDRSTRLEN (INET6_ADDRSTRLEN + 11)
  136. /* according to IANA assignment, port 0x0000 and proto 0xff are reserved. */
  137. #define IPSEC_PORT_ANY 0
  138. #define IPSEC_ULPROTO_ANY 255
  139. #define IPSEC_PROTO_ANY 255
  140. /* mode of security protocol */
  141. /* NOTE: DON'T use IPSEC_MODE_ANY at SPD. It's only use in SAD */
  142. #define IPSEC_MODE_ANY 0 /* i.e. wildcard. */
  143. #define IPSEC_MODE_TRANSPORT 1
  144. #define IPSEC_MODE_TUNNEL 2
  145. #define IPSEC_MODE_TCPMD5 3 /* TCP MD5 mode */
  146. /*
  147. * Direction of security policy.
  148. * NOTE: Since INVALID is used just as flag.
  149. * The other are used for loop counter too.
  150. */
  151. #define IPSEC_DIR_ANY 0
  152. #define IPSEC_DIR_INBOUND 1
  153. #define IPSEC_DIR_OUTBOUND 2
  154. #define IPSEC_DIR_MAX 3
  155. #define IPSEC_DIR_INVALID 4
  156. /* Policy level */
  157. /*
  158. * IPSEC, ENTRUST and BYPASS are allowed for setsockopt() in PCB,
  159. * DISCARD, IPSEC and NONE are allowed for setkey() in SPD.
  160. * DISCARD and NONE are allowed for system default.
  161. */
  162. #define IPSEC_POLICY_DISCARD 0 /* discarding packet */
  163. #define IPSEC_POLICY_NONE 1 /* through IPsec engine */
  164. #define IPSEC_POLICY_IPSEC 2 /* do IPsec */
  165. #define IPSEC_POLICY_ENTRUST 3 /* consulting SPD if present. */
  166. #define IPSEC_POLICY_BYPASS 4 /* only for privileged socket. */
  167. /* Policy scope */
  168. #define IPSEC_POLICYSCOPE_ANY 0x00 /* unspecified */
  169. #define IPSEC_POLICYSCOPE_GLOBAL 0x01 /* global scope */
  170. #define IPSEC_POLICYSCOPE_IFNET 0x02 /* if_ipsec(4) scope */
  171. #define IPSEC_POLICYSCOPE_PCB 0x04 /* PCB scope */
  172. /* Security protocol level */
  173. #define IPSEC_LEVEL_DEFAULT 0 /* reference to system default */
  174. #define IPSEC_LEVEL_USE 1 /* use SA if present. */
  175. #define IPSEC_LEVEL_REQUIRE 2 /* require SA. */
  176. #define IPSEC_LEVEL_UNIQUE 3 /* unique SA. */
  177. #define IPSEC_MANUAL_REQID_MAX 0x3fff
  178. /*
  179. * if security policy level == unique, this id
  180. * indicate to a relative SA for use, else is
  181. * zero.
  182. * 1 - 0x3fff are reserved for manual keying.
  183. * 0 are reserved for above reason. Others is
  184. * for kernel use.
  185. * Note that this id doesn't identify SA
  186. * by only itself.
  187. */
  188. #define IPSEC_REPLAYWSIZE 32
  189. /* statistics for ipsec processing */
  190. struct ipsecstat {
  191. uint64_t ips_in_polvio; /* input: sec policy violation */
  192. uint64_t ips_in_nomem; /* input: no memory available */
  193. uint64_t ips_in_inval; /* input: generic error */
  194. uint64_t ips_out_polvio; /* output: sec policy violation */
  195. uint64_t ips_out_nosa; /* output: SA unavailable */
  196. uint64_t ips_out_nomem; /* output: no memory available */
  197. uint64_t ips_out_noroute; /* output: no route available */
  198. uint64_t ips_out_inval; /* output: generic error */
  199. uint64_t ips_out_bundlesa; /* output: bundled SA processed */
  200. uint64_t ips_spdcache_hits; /* SPD cache hits */
  201. uint64_t ips_spdcache_misses; /* SPD cache misses */
  202. uint64_t ips_clcopied; /* clusters copied during clone */
  203. uint64_t ips_mbinserted; /* mbufs inserted during makespace */
  204. /*
  205. * Temporary statistics for performance analysis.
  206. */
  207. /* See where ESP/AH/IPCOMP header land in mbuf on input */
  208. uint64_t ips_input_front;
  209. uint64_t ips_input_middle;
  210. uint64_t ips_input_end;
  211. };
  212. /*
  213. * Definitions for IPsec & Key sysctl operations.
  214. */
  215. #define IPSECCTL_STATS 1 /* stats */
  216. #define IPSECCTL_DEF_POLICY 2
  217. #define IPSECCTL_DEF_ESP_TRANSLEV 3 /* int; ESP transport mode */
  218. #define IPSECCTL_DEF_ESP_NETLEV 4 /* int; ESP tunnel mode */
  219. #define IPSECCTL_DEF_AH_TRANSLEV 5 /* int; AH transport mode */
  220. #define IPSECCTL_DEF_AH_NETLEV 6 /* int; AH tunnel mode */
  221. #if 0 /* obsolete, do not reuse */
  222. #define IPSECCTL_INBOUND_CALL_IKE 7
  223. #endif
  224. #define IPSECCTL_AH_CLEARTOS 8
  225. #define IPSECCTL_AH_OFFSETMASK 9
  226. #define IPSECCTL_DFBIT 10
  227. #define IPSECCTL_ECN 11
  228. #define IPSECCTL_DEBUG 12
  229. #define IPSECCTL_ESP_RANDPAD 13
  230. #define IPSECCTL_MIN_PMTU 14
  231. #ifdef _KERNEL
  232. #include <sys/counter.h>
  233. struct ipsec_ctx_data;
  234. #define IPSEC_INIT_CTX(_ctx, _mp, _inp, _sav, _af, _enc) do { \
  235. (_ctx)->mp = (_mp); \
  236. (_ctx)->inp = (_inp); \
  237. (_ctx)->sav = (_sav); \
  238. (_ctx)->af = (_af); \
  239. (_ctx)->enc = (_enc); \
  240. } while(0)
  241. int ipsec_run_hhooks(struct ipsec_ctx_data *ctx, int direction);
  242. VNET_DECLARE(int, ipsec_debug);
  243. #define V_ipsec_debug VNET(ipsec_debug)
  244. #ifdef REGRESSION
  245. VNET_DECLARE(int, ipsec_replay);
  246. VNET_DECLARE(int, ipsec_integrity);
  247. #define V_ipsec_replay VNET(ipsec_replay)
  248. #define V_ipsec_integrity VNET(ipsec_integrity)
  249. #endif
  250. VNET_PCPUSTAT_DECLARE(struct ipsecstat, ipsec4stat);
  251. VNET_DECLARE(int, ip4_esp_trans_deflev);
  252. VNET_DECLARE(int, ip4_esp_net_deflev);
  253. VNET_DECLARE(int, ip4_ah_trans_deflev);
  254. VNET_DECLARE(int, ip4_ah_net_deflev);
  255. VNET_DECLARE(int, ip4_ipsec_dfbit);
  256. VNET_DECLARE(int, ip4_ipsec_min_pmtu);
  257. VNET_DECLARE(int, ip4_ipsec_ecn);
  258. VNET_DECLARE(int, crypto_support);
  259. VNET_DECLARE(int, async_crypto);
  260. VNET_DECLARE(int, natt_cksum_policy);
  261. #define IPSECSTAT_INC(name) \
  262. VNET_PCPUSTAT_ADD(struct ipsecstat, ipsec4stat, name, 1)
  263. #define V_ip4_esp_trans_deflev VNET(ip4_esp_trans_deflev)
  264. #define V_ip4_esp_net_deflev VNET(ip4_esp_net_deflev)
  265. #define V_ip4_ah_trans_deflev VNET(ip4_ah_trans_deflev)
  266. #define V_ip4_ah_net_deflev VNET(ip4_ah_net_deflev)
  267. #define V_ip4_ipsec_dfbit VNET(ip4_ipsec_dfbit)
  268. #define V_ip4_ipsec_min_pmtu VNET(ip4_ipsec_min_pmtu)
  269. #define V_ip4_ipsec_ecn VNET(ip4_ipsec_ecn)
  270. #define V_crypto_support VNET(crypto_support)
  271. #define V_async_crypto VNET(async_crypto)
  272. #define V_natt_cksum_policy VNET(natt_cksum_policy)
  273. #define ipseclog(x) do { if (V_ipsec_debug) log x; } while (0)
  274. /* for openbsd compatibility */
  275. #ifdef IPSEC_DEBUG
  276. #define IPSEC_DEBUG_DECLARE(x) x
  277. #define DPRINTF(x) do { if (V_ipsec_debug) printf x; } while (0)
  278. #else
  279. #define IPSEC_DEBUG_DECLARE(x)
  280. #define DPRINTF(x)
  281. #endif
  282. struct inpcb;
  283. struct m_tag;
  284. struct secasvar;
  285. struct sockopt;
  286. struct tcphdr;
  287. union sockaddr_union;
  288. int ipsec_if_input(struct mbuf *, struct secasvar *, uint32_t);
  289. struct ipsecrequest *ipsec_newisr(void);
  290. void ipsec_delisr(struct ipsecrequest *);
  291. struct secpolicy *ipsec4_checkpolicy(const struct mbuf *, struct inpcb *,
  292. int *, int);
  293. u_int ipsec_get_reqlevel(struct secpolicy *, u_int);
  294. void udp_ipsec_adjust_cksum(struct mbuf *, struct secasvar *, int, int);
  295. int udp_ipsec_output(struct mbuf *, struct secasvar *);
  296. int ipsec_chkreplay(uint32_t, uint32_t *, struct secasvar *);
  297. int ipsec_updatereplay(uint32_t, struct secasvar *);
  298. int ipsec_updateid(struct secasvar *, crypto_session_t *, crypto_session_t *);
  299. int ipsec_initialized(void);
  300. size_t ipsec_hdrsiz_internal(struct secpolicy *);
  301. void ipsec_setspidx_inpcb(struct inpcb *, struct secpolicyindex *, u_int);
  302. void ipsec4_setsockaddrs(const struct mbuf *, union sockaddr_union *,
  303. union sockaddr_union *);
  304. int ipsec4_common_input_cb(struct mbuf *, struct secasvar *, int, int);
  305. int ipsec4_check_pmtu(struct ifnet *, struct mbuf *, struct secpolicy *, int);
  306. int ipsec4_process_packet(struct ifnet *, struct mbuf *, struct secpolicy *,
  307. struct inpcb *, u_long);
  308. int ipsec_process_done(struct mbuf *, struct secpolicy *, struct secasvar *,
  309. u_int);
  310. void m_checkalignment(const char* where, struct mbuf *m0,
  311. int off, int len);
  312. struct mbuf *m_makespace(struct mbuf *m0, int skip, int hlen, int *off);
  313. caddr_t m_pad(struct mbuf *m, int n);
  314. int m_striphdr(struct mbuf *m, int skip, int hlen);
  315. SYSCTL_DECL(_net_inet_ipsec);
  316. SYSCTL_DECL(_net_inet6_ipsec6);
  317. #endif /* _KERNEL */
  318. #ifndef _KERNEL
  319. caddr_t ipsec_set_policy(const char *, int);
  320. int ipsec_get_policylen(c_caddr_t);
  321. char *ipsec_dump_policy(c_caddr_t, const char *);
  322. const char *ipsec_strerror(void);
  323. #endif /* ! KERNEL */
  324. #endif /* _NETIPSEC_IPSEC_H_ */