ip_spd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /* $OpenBSD: ip_spd.c,v 1.86 2015/07/17 18:31:08 blambert Exp $ */
  2. /*
  3. * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
  4. *
  5. * Copyright (c) 2000-2001 Angelos D. Keromytis.
  6. *
  7. * Permission to use, copy, and modify this software with or without fee
  8. * is hereby granted, provided that this entire notice is included in
  9. * all copies of any software which is or includes a copy or
  10. * modification of this software.
  11. * You may use this code under the GNU public license if you so wish. Please
  12. * contribute changes back to the authors under this freer than GPL license
  13. * so that we may further the use of strong encryption without limitations to
  14. * all.
  15. *
  16. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
  17. * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
  18. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
  19. * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
  20. * PURPOSE.
  21. */
  22. #include <sys/param.h>
  23. #include <sys/systm.h>
  24. #include <sys/mbuf.h>
  25. #include <sys/socket.h>
  26. #include <sys/kernel.h>
  27. #include <sys/socketvar.h>
  28. #include <sys/domain.h>
  29. #include <sys/protosw.h>
  30. #include <sys/pool.h>
  31. #include <sys/timeout.h>
  32. #include <net/route.h>
  33. #include <net/netisr.h>
  34. #include <netinet/in.h>
  35. #include <netinet/ip.h>
  36. #include <netinet/ip_var.h>
  37. #include <netinet/in_pcb.h>
  38. #ifdef INET6
  39. #endif /* INET6 */
  40. #include <netinet/ip_ipsp.h>
  41. #include <net/pfkeyv2.h>
  42. int ipsp_acquire_sa(struct ipsec_policy *, union sockaddr_union *,
  43. union sockaddr_union *, struct sockaddr_encap *, struct mbuf *);
  44. struct ipsec_acquire *ipsp_pending_acquire(struct ipsec_policy *,
  45. union sockaddr_union *);
  46. void ipsp_delete_acquire(void *);
  47. #ifdef ENCDEBUG
  48. #define DPRINTF(x) if (encdebug) printf x
  49. #else
  50. #define DPRINTF(x)
  51. #endif
  52. struct pool ipsec_policy_pool;
  53. struct pool ipsec_acquire_pool;
  54. int ipsec_policy_pool_initialized = 0;
  55. int ipsec_acquire_pool_initialized = 0;
  56. struct radix_node_head **spd_tables;
  57. unsigned int spd_table_max;
  58. struct radix_node_head *
  59. spd_table_get(unsigned int rtableid)
  60. {
  61. unsigned int rdomain;
  62. if (spd_tables == NULL)
  63. return (NULL);
  64. rdomain = rtable_l2(rtableid);
  65. if (rdomain > spd_table_max)
  66. return (NULL);
  67. return (spd_tables[rdomain]);
  68. }
  69. struct radix_node_head *
  70. spd_table_add(unsigned int rtableid)
  71. {
  72. extern struct domain pfkeydomain;
  73. struct radix_node_head *rnh = NULL;
  74. unsigned int rdomain;
  75. void *p;
  76. rdomain = rtable_l2(rtableid);
  77. if (spd_tables == NULL || rdomain > spd_table_max) {
  78. if ((p = mallocarray(rdomain + 1, sizeof(*rnh),
  79. M_RTABLE, M_NOWAIT|M_ZERO)) == NULL)
  80. return (NULL);
  81. if (spd_tables != NULL) {
  82. bcopy(spd_tables, p, sizeof(*rnh) * (spd_table_max+1));
  83. free(spd_tables, M_RTABLE, 0);
  84. }
  85. spd_tables = p;
  86. spd_table_max = rdomain;
  87. }
  88. if (spd_tables[rdomain] == NULL) {
  89. if (rn_inithead((void **)&rnh, pfkeydomain.dom_rtoffset) == 0)
  90. rnh = NULL;
  91. spd_tables[rdomain] = rnh;
  92. }
  93. return (spd_tables[rdomain]);
  94. }
  95. /*
  96. * Lookup at the SPD based on the headers contained on the mbuf. The second
  97. * argument indicates what protocol family the header at the beginning of
  98. * the mbuf is. hlen is the offset of the transport protocol header
  99. * in the mbuf.
  100. *
  101. * Return combinations (of return value and in *error):
  102. * - NULL/0 -> no IPsec required on packet
  103. * - NULL/-EINVAL -> silently drop the packet
  104. * - NULL/errno -> drop packet and return error
  105. * or a pointer to a TDB (and 0 in *error).
  106. *
  107. * In the case of incoming flows, only the first three combinations are
  108. * returned.
  109. */
  110. struct tdb *
  111. ipsp_spd_lookup(struct mbuf *m, int af, int hlen, int *error, int direction,
  112. struct tdb *tdbp, struct inpcb *inp, u_int32_t ipsecflowinfo)
  113. {
  114. struct radix_node_head *rnh;
  115. struct radix_node *rn;
  116. union sockaddr_union sdst, ssrc;
  117. struct sockaddr_encap *ddst, dst;
  118. struct ipsec_policy *ipo;
  119. struct ipsec_ids *ids = NULL;
  120. int signore = 0, dignore = 0;
  121. u_int rdomain = rtable_l2(m->m_pkthdr.ph_rtableid);
  122. /*
  123. * If there are no flows in place, there's no point
  124. * continuing with the SPD lookup.
  125. */
  126. if (!ipsec_in_use && inp == NULL) {
  127. *error = 0;
  128. return NULL;
  129. }
  130. /*
  131. * If an input packet is destined to a BYPASS socket, just accept it.
  132. */
  133. if ((inp != NULL) && (direction == IPSP_DIRECTION_IN) &&
  134. (inp->inp_seclevel[SL_ESP_TRANS] == IPSEC_LEVEL_BYPASS) &&
  135. (inp->inp_seclevel[SL_ESP_NETWORK] == IPSEC_LEVEL_BYPASS) &&
  136. (inp->inp_seclevel[SL_AUTH] == IPSEC_LEVEL_BYPASS)) {
  137. *error = 0;
  138. return NULL;
  139. }
  140. memset(&dst, 0, sizeof(dst));
  141. memset(&sdst, 0, sizeof(union sockaddr_union));
  142. memset(&ssrc, 0, sizeof(union sockaddr_union));
  143. ddst = (struct sockaddr_encap *)&dst;
  144. ddst->sen_family = PF_KEY;
  145. ddst->sen_len = SENT_LEN;
  146. switch (af) {
  147. case AF_INET:
  148. if (hlen < sizeof (struct ip) || m->m_pkthdr.len < hlen) {
  149. *error = EINVAL;
  150. return NULL;
  151. }
  152. ddst->sen_direction = direction;
  153. ddst->sen_type = SENT_IP4;
  154. m_copydata(m, offsetof(struct ip, ip_src),
  155. sizeof(struct in_addr), (caddr_t) &(ddst->sen_ip_src));
  156. m_copydata(m, offsetof(struct ip, ip_dst),
  157. sizeof(struct in_addr), (caddr_t) &(ddst->sen_ip_dst));
  158. m_copydata(m, offsetof(struct ip, ip_p), sizeof(u_int8_t),
  159. (caddr_t) &(ddst->sen_proto));
  160. sdst.sin.sin_family = ssrc.sin.sin_family = AF_INET;
  161. sdst.sin.sin_len = ssrc.sin.sin_len =
  162. sizeof(struct sockaddr_in);
  163. ssrc.sin.sin_addr = ddst->sen_ip_src;
  164. sdst.sin.sin_addr = ddst->sen_ip_dst;
  165. /*
  166. * If TCP/UDP, extract the port numbers to use in the lookup.
  167. */
  168. switch (ddst->sen_proto) {
  169. case IPPROTO_UDP:
  170. case IPPROTO_TCP:
  171. /* Make sure there's enough data in the packet. */
  172. if (m->m_pkthdr.len < hlen + 2 * sizeof(u_int16_t)) {
  173. *error = EINVAL;
  174. return NULL;
  175. }
  176. /*
  177. * Luckily, the offset of the src/dst ports in
  178. * both the UDP and TCP headers is the same (first
  179. * two 16-bit values in the respective headers),
  180. * so we can just copy them.
  181. */
  182. m_copydata(m, hlen, sizeof(u_int16_t),
  183. (caddr_t) &(ddst->sen_sport));
  184. m_copydata(m, hlen + sizeof(u_int16_t), sizeof(u_int16_t),
  185. (caddr_t) &(ddst->sen_dport));
  186. break;
  187. default:
  188. ddst->sen_sport = 0;
  189. ddst->sen_dport = 0;
  190. }
  191. break;
  192. #ifdef INET6
  193. case AF_INET6:
  194. if (hlen < sizeof (struct ip6_hdr) || m->m_pkthdr.len < hlen) {
  195. *error = EINVAL;
  196. return NULL;
  197. }
  198. ddst->sen_type = SENT_IP6;
  199. ddst->sen_ip6_direction = direction;
  200. m_copydata(m, offsetof(struct ip6_hdr, ip6_src),
  201. sizeof(struct in6_addr),
  202. (caddr_t) &(ddst->sen_ip6_src));
  203. m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
  204. sizeof(struct in6_addr),
  205. (caddr_t) &(ddst->sen_ip6_dst));
  206. m_copydata(m, offsetof(struct ip6_hdr, ip6_nxt),
  207. sizeof(u_int8_t),
  208. (caddr_t) &(ddst->sen_ip6_proto));
  209. sdst.sin6.sin6_family = ssrc.sin6.sin6_family = AF_INET6;
  210. sdst.sin6.sin6_len = ssrc.sin6.sin6_len =
  211. sizeof(struct sockaddr_in6);
  212. in6_recoverscope(&ssrc.sin6, &ddst->sen_ip6_src, NULL);
  213. in6_recoverscope(&sdst.sin6, &ddst->sen_ip6_dst, NULL);
  214. /*
  215. * If TCP/UDP, extract the port numbers to use in the lookup.
  216. */
  217. switch (ddst->sen_ip6_proto) {
  218. case IPPROTO_UDP:
  219. case IPPROTO_TCP:
  220. /* Make sure there's enough data in the packet. */
  221. if (m->m_pkthdr.len < hlen + 2 * sizeof(u_int16_t)) {
  222. *error = EINVAL;
  223. return NULL;
  224. }
  225. /*
  226. * Luckily, the offset of the src/dst ports in
  227. * both the UDP and TCP headers is the same
  228. * (first two 16-bit values in the respective
  229. * headers), so we can just copy them.
  230. */
  231. m_copydata(m, hlen, sizeof(u_int16_t),
  232. (caddr_t) &(ddst->sen_ip6_sport));
  233. m_copydata(m, hlen + sizeof(u_int16_t), sizeof(u_int16_t),
  234. (caddr_t) &(ddst->sen_ip6_dport));
  235. break;
  236. default:
  237. ddst->sen_ip6_sport = 0;
  238. ddst->sen_ip6_dport = 0;
  239. }
  240. break;
  241. #endif /* INET6 */
  242. default:
  243. *error = EAFNOSUPPORT;
  244. return NULL;
  245. }
  246. /* Actual SPD lookup. */
  247. if ((rnh = spd_table_get(rdomain)) == NULL ||
  248. (rn = rn_match((caddr_t)&dst, rnh)) == NULL) {
  249. /*
  250. * Return whatever the socket requirements are, there are no
  251. * system-wide policies.
  252. */
  253. *error = 0;
  254. return ipsp_spd_inp(m, af, hlen, error, direction,
  255. tdbp, inp, NULL);
  256. }
  257. ipo = (struct ipsec_policy *)rn;
  258. switch (ipo->ipo_type) {
  259. case IPSP_PERMIT:
  260. *error = 0;
  261. return ipsp_spd_inp(m, af, hlen, error, direction, tdbp,
  262. inp, ipo);
  263. case IPSP_DENY:
  264. *error = EHOSTUNREACH;
  265. return NULL;
  266. case IPSP_IPSEC_USE:
  267. case IPSP_IPSEC_ACQUIRE:
  268. case IPSP_IPSEC_REQUIRE:
  269. case IPSP_IPSEC_DONTACQ:
  270. /* Nothing more needed here. */
  271. break;
  272. default:
  273. *error = EINVAL;
  274. return NULL;
  275. }
  276. /* Check for non-specific destination in the policy. */
  277. switch (ipo->ipo_dst.sa.sa_family) {
  278. case AF_INET:
  279. if ((ipo->ipo_dst.sin.sin_addr.s_addr == INADDR_ANY) ||
  280. (ipo->ipo_dst.sin.sin_addr.s_addr == INADDR_BROADCAST))
  281. dignore = 1;
  282. break;
  283. #ifdef INET6
  284. case AF_INET6:
  285. if ((IN6_IS_ADDR_UNSPECIFIED(&ipo->ipo_dst.sin6.sin6_addr)) ||
  286. (memcmp(&ipo->ipo_dst.sin6.sin6_addr, &in6mask128,
  287. sizeof(in6mask128)) == 0))
  288. dignore = 1;
  289. break;
  290. #endif /* INET6 */
  291. }
  292. /* Likewise for source. */
  293. switch (ipo->ipo_src.sa.sa_family) {
  294. case AF_INET:
  295. if (ipo->ipo_src.sin.sin_addr.s_addr == INADDR_ANY)
  296. signore = 1;
  297. break;
  298. #ifdef INET6
  299. case AF_INET6:
  300. if (IN6_IS_ADDR_UNSPECIFIED(&ipo->ipo_src.sin6.sin6_addr))
  301. signore = 1;
  302. break;
  303. #endif /* INET6 */
  304. }
  305. /* Do we have a cached entry ? If so, check if it's still valid. */
  306. if ((ipo->ipo_tdb) && (ipo->ipo_tdb->tdb_flags & TDBF_INVALID)) {
  307. TAILQ_REMOVE(&ipo->ipo_tdb->tdb_policy_head, ipo,
  308. ipo_tdb_next);
  309. ipo->ipo_tdb = NULL;
  310. }
  311. /* Outgoing packet policy check. */
  312. if (direction == IPSP_DIRECTION_OUT) {
  313. /*
  314. * If the packet is destined for the policy-specified
  315. * gateway/endhost, and the socket has the BYPASS
  316. * option set, skip IPsec processing.
  317. */
  318. if ((inp != NULL) &&
  319. (inp->inp_seclevel[SL_ESP_TRANS] == IPSEC_LEVEL_BYPASS) &&
  320. (inp->inp_seclevel[SL_ESP_NETWORK] ==
  321. IPSEC_LEVEL_BYPASS) &&
  322. (inp->inp_seclevel[SL_AUTH] == IPSEC_LEVEL_BYPASS)) {
  323. /* Direct match. */
  324. if (dignore ||
  325. !memcmp(&sdst, &ipo->ipo_dst, sdst.sa.sa_len)) {
  326. *error = 0;
  327. return NULL;
  328. }
  329. }
  330. if (ipsecflowinfo)
  331. ids = ipsp_ids_lookup(ipsecflowinfo);
  332. /* Check that the cached TDB (if present), is appropriate. */
  333. if (ipo->ipo_tdb) {
  334. if ((ipo->ipo_last_searched <= ipsec_last_added) ||
  335. (ipo->ipo_sproto != ipo->ipo_tdb->tdb_sproto) ||
  336. memcmp(dignore ? &sdst : &ipo->ipo_dst,
  337. &ipo->ipo_tdb->tdb_dst,
  338. ipo->ipo_tdb->tdb_dst.sa.sa_len))
  339. goto nomatchout;
  340. if (!ipsp_aux_match(ipo->ipo_tdb,
  341. ids ? ids : ipo->ipo_ids,
  342. &ipo->ipo_addr, &ipo->ipo_mask))
  343. goto nomatchout;
  344. /* Cached entry is good. */
  345. *error = 0;
  346. return ipsp_spd_inp(m, af, hlen, error, direction,
  347. tdbp, inp, ipo);
  348. nomatchout:
  349. /* Cached TDB was not good. */
  350. TAILQ_REMOVE(&ipo->ipo_tdb->tdb_policy_head, ipo,
  351. ipo_tdb_next);
  352. ipo->ipo_tdb = NULL;
  353. ipo->ipo_last_searched = 0;
  354. }
  355. /*
  356. * If no SA has been added since the last time we did a
  357. * lookup, there's no point searching for one. However, if the
  358. * destination gateway is left unspecified (or is all-1's),
  359. * always lookup since this is a generic-match rule
  360. * (otherwise, we can have situations where SAs to some
  361. * destinations exist but are not used, possibly leading to an
  362. * explosion in the number of acquired SAs).
  363. */
  364. if (ipo->ipo_last_searched <= ipsec_last_added) {
  365. /* "Touch" the entry. */
  366. if (dignore == 0)
  367. ipo->ipo_last_searched = time_second;
  368. /* Find an appropriate SA from the existing ones. */
  369. ipo->ipo_tdb =
  370. gettdbbydst(rdomain,
  371. dignore ? &sdst : &ipo->ipo_dst,
  372. ipo->ipo_sproto,
  373. ids ? ids: ipo->ipo_ids,
  374. &ipo->ipo_addr, &ipo->ipo_mask);
  375. if (ipo->ipo_tdb) {
  376. TAILQ_INSERT_TAIL(&ipo->ipo_tdb->tdb_policy_head,
  377. ipo, ipo_tdb_next);
  378. *error = 0;
  379. return ipsp_spd_inp(m, af, hlen, error,
  380. direction, tdbp, inp, ipo);
  381. }
  382. }
  383. /* So, we don't have an SA -- just a policy. */
  384. switch (ipo->ipo_type) {
  385. case IPSP_IPSEC_REQUIRE:
  386. /* Acquire SA through key management. */
  387. if (ipsp_acquire_sa(ipo,
  388. dignore ? &sdst : &ipo->ipo_dst,
  389. signore ? NULL : &ipo->ipo_src, ddst, m) != 0) {
  390. *error = EACCES;
  391. return NULL;
  392. }
  393. /* FALLTHROUGH */
  394. case IPSP_IPSEC_DONTACQ:
  395. *error = -EINVAL; /* Silently drop packet. */
  396. return NULL;
  397. case IPSP_IPSEC_ACQUIRE:
  398. /* Acquire SA through key management. */
  399. ipsp_acquire_sa(ipo, dignore ? &sdst : &ipo->ipo_dst,
  400. signore ? NULL : &ipo->ipo_src, ddst, NULL);
  401. /* FALLTHROUGH */
  402. case IPSP_IPSEC_USE:
  403. *error = 0;
  404. return ipsp_spd_inp(m, af, hlen, error, direction,
  405. tdbp, inp, ipo);
  406. }
  407. } else { /* IPSP_DIRECTION_IN */
  408. if (tdbp != NULL) {
  409. /* Direct match in the cache. */
  410. if (ipo->ipo_tdb == tdbp) {
  411. *error = 0;
  412. return ipsp_spd_inp(m, af, hlen, error,
  413. direction, tdbp, inp, ipo);
  414. }
  415. if (memcmp(dignore ? &ssrc : &ipo->ipo_dst,
  416. &tdbp->tdb_src, tdbp->tdb_src.sa.sa_len) ||
  417. (ipo->ipo_sproto != tdbp->tdb_sproto))
  418. goto nomatchin;
  419. /* Match source/dest IDs. */
  420. if (ipo->ipo_ids)
  421. if (tdbp->tdb_ids == NULL ||
  422. !ipsp_ids_match(ipo->ipo_ids, tdbp->tdb_ids))
  423. goto nomatchin;
  424. /* Add it to the cache. */
  425. if (ipo->ipo_tdb)
  426. TAILQ_REMOVE(&ipo->ipo_tdb->tdb_policy_head,
  427. ipo, ipo_tdb_next);
  428. ipo->ipo_tdb = tdbp;
  429. TAILQ_INSERT_TAIL(&tdbp->tdb_policy_head, ipo,
  430. ipo_tdb_next);
  431. *error = 0;
  432. return ipsp_spd_inp(m, af, hlen, error, direction,
  433. tdbp, inp, ipo);
  434. nomatchin: /* Nothing needed here, falling through */
  435. ;
  436. }
  437. /* Check whether cached entry applies. */
  438. if (ipo->ipo_tdb) {
  439. /*
  440. * We only need to check that the correct
  441. * security protocol and security gateway are
  442. * set; IDs will be the same since the cached
  443. * entry is linked on this policy.
  444. */
  445. if (ipo->ipo_sproto == ipo->ipo_tdb->tdb_sproto &&
  446. !memcmp(&ipo->ipo_tdb->tdb_src,
  447. dignore ? &ssrc : &ipo->ipo_dst,
  448. ipo->ipo_tdb->tdb_src.sa.sa_len))
  449. goto skipinputsearch;
  450. /* Not applicable, unlink. */
  451. TAILQ_REMOVE(&ipo->ipo_tdb->tdb_policy_head, ipo,
  452. ipo_tdb_next);
  453. ipo->ipo_last_searched = 0;
  454. ipo->ipo_tdb = NULL;
  455. }
  456. /* Find whether there exists an appropriate SA. */
  457. if (ipo->ipo_last_searched <= ipsec_last_added) {
  458. if (dignore == 0)
  459. ipo->ipo_last_searched = time_second;
  460. ipo->ipo_tdb =
  461. gettdbbysrc(rdomain,
  462. dignore ? &ssrc : &ipo->ipo_dst,
  463. ipo->ipo_sproto, ipo->ipo_ids,
  464. &ipo->ipo_addr, &ipo->ipo_mask);
  465. if (ipo->ipo_tdb)
  466. TAILQ_INSERT_TAIL(&ipo->ipo_tdb->tdb_policy_head,
  467. ipo, ipo_tdb_next);
  468. }
  469. skipinputsearch:
  470. switch (ipo->ipo_type) {
  471. case IPSP_IPSEC_REQUIRE:
  472. /* If appropriate SA exists, don't acquire another. */
  473. if (ipo->ipo_tdb) {
  474. *error = -EINVAL;
  475. return NULL;
  476. }
  477. /* Acquire SA through key management. */
  478. if ((*error = ipsp_acquire_sa(ipo,
  479. dignore ? &ssrc : &ipo->ipo_dst,
  480. signore ? NULL : &ipo->ipo_src, ddst, m)) != 0)
  481. return NULL;
  482. /* FALLTHROUGH */
  483. case IPSP_IPSEC_DONTACQ:
  484. /* Drop packet. */
  485. *error = -EINVAL;
  486. return NULL;
  487. case IPSP_IPSEC_ACQUIRE:
  488. /* If appropriate SA exists, don't acquire another. */
  489. if (ipo->ipo_tdb) {
  490. *error = 0;
  491. return ipsp_spd_inp(m, af, hlen, error,
  492. direction, tdbp, inp, ipo);
  493. }
  494. /* Acquire SA through key management. */
  495. ipsp_acquire_sa(ipo, dignore ? &ssrc : &ipo->ipo_dst,
  496. signore ? NULL : &ipo->ipo_src, ddst, NULL);
  497. /* FALLTHROUGH */
  498. case IPSP_IPSEC_USE:
  499. *error = 0;
  500. return ipsp_spd_inp(m, af, hlen, error, direction,
  501. tdbp, inp, ipo);
  502. }
  503. }
  504. /* Shouldn't ever get this far. */
  505. *error = EINVAL;
  506. return NULL;
  507. }
  508. /*
  509. * Delete a policy from the SPD.
  510. */
  511. int
  512. ipsec_delete_policy(struct ipsec_policy *ipo)
  513. {
  514. struct ipsec_acquire *ipa;
  515. struct radix_node_head *rnh;
  516. struct radix_node *rn = (struct radix_node *)ipo;
  517. int err = 0;
  518. if (--ipo->ipo_ref_count > 0)
  519. return 0;
  520. /* Delete from SPD. */
  521. if ((rnh = spd_table_get(ipo->ipo_rdomain)) == NULL ||
  522. rn_delete(&ipo->ipo_addr, &ipo->ipo_mask, rnh, rn) == NULL)
  523. return (ESRCH);
  524. if (ipo->ipo_tdb != NULL)
  525. TAILQ_REMOVE(&ipo->ipo_tdb->tdb_policy_head, ipo,
  526. ipo_tdb_next);
  527. while ((ipa = TAILQ_FIRST(&ipo->ipo_acquires)) != NULL)
  528. ipsp_delete_acquire(ipa);
  529. TAILQ_REMOVE(&ipsec_policy_head, ipo, ipo_list);
  530. if (ipo->ipo_ids)
  531. ipsp_ids_free(ipo->ipo_ids);
  532. ipsec_in_use--;
  533. pool_put(&ipsec_policy_pool, ipo);
  534. return err;
  535. }
  536. /*
  537. * Delete a pending IPsec acquire record.
  538. */
  539. void
  540. ipsp_delete_acquire(void *v)
  541. {
  542. struct ipsec_acquire *ipa = v;
  543. timeout_del(&ipa->ipa_timeout);
  544. TAILQ_REMOVE(&ipsec_acquire_head, ipa, ipa_next);
  545. if (ipa->ipa_policy != NULL)
  546. TAILQ_REMOVE(&ipa->ipa_policy->ipo_acquires, ipa,
  547. ipa_ipo_next);
  548. pool_put(&ipsec_acquire_pool, ipa);
  549. }
  550. /*
  551. * Find out if there's an ACQUIRE pending.
  552. * XXX Need a better structure.
  553. */
  554. struct ipsec_acquire *
  555. ipsp_pending_acquire(struct ipsec_policy *ipo, union sockaddr_union *gw)
  556. {
  557. struct ipsec_acquire *ipa;
  558. TAILQ_FOREACH (ipa, &ipo->ipo_acquires, ipa_ipo_next) {
  559. if (!memcmp(gw, &ipa->ipa_addr, gw->sa.sa_len))
  560. return ipa;
  561. }
  562. return NULL;
  563. }
  564. /*
  565. * Signal key management that we need an SA.
  566. * XXX For outgoing policies, we could try to hold on to the mbuf.
  567. */
  568. int
  569. ipsp_acquire_sa(struct ipsec_policy *ipo, union sockaddr_union *gw,
  570. union sockaddr_union *laddr, struct sockaddr_encap *ddst, struct mbuf *m)
  571. {
  572. struct ipsec_acquire *ipa;
  573. /* Check whether request has been made already. */
  574. if ((ipa = ipsp_pending_acquire(ipo, gw)) != NULL)
  575. return 0;
  576. /* Add request in cache and proceed. */
  577. if (ipsec_acquire_pool_initialized == 0) {
  578. ipsec_acquire_pool_initialized = 1;
  579. pool_init(&ipsec_acquire_pool, sizeof(struct ipsec_acquire),
  580. 0, 0, 0, "ipsec acquire", NULL);
  581. }
  582. ipa = pool_get(&ipsec_acquire_pool, PR_NOWAIT|PR_ZERO);
  583. if (ipa == NULL)
  584. return ENOMEM;
  585. bcopy(gw, &ipa->ipa_addr, sizeof(union sockaddr_union));
  586. timeout_set(&ipa->ipa_timeout, ipsp_delete_acquire, ipa);
  587. ipa->ipa_info.sen_len = ipa->ipa_mask.sen_len = SENT_LEN;
  588. ipa->ipa_info.sen_family = ipa->ipa_mask.sen_family = PF_KEY;
  589. /* Just copy the right information. */
  590. switch (ipo->ipo_addr.sen_type) {
  591. case SENT_IP4:
  592. ipa->ipa_info.sen_type = ipa->ipa_mask.sen_type = SENT_IP4;
  593. ipa->ipa_info.sen_direction = ipo->ipo_addr.sen_direction;
  594. ipa->ipa_mask.sen_direction = ipo->ipo_mask.sen_direction;
  595. if (ipsp_is_unspecified(ipo->ipo_dst)) {
  596. ipa->ipa_info.sen_ip_src = ddst->sen_ip_src;
  597. ipa->ipa_mask.sen_ip_src.s_addr = INADDR_BROADCAST;
  598. ipa->ipa_info.sen_ip_dst = ddst->sen_ip_dst;
  599. ipa->ipa_mask.sen_ip_dst.s_addr = INADDR_BROADCAST;
  600. } else {
  601. ipa->ipa_info.sen_ip_src = ipo->ipo_addr.sen_ip_src;
  602. ipa->ipa_mask.sen_ip_src = ipo->ipo_mask.sen_ip_src;
  603. ipa->ipa_info.sen_ip_dst = ipo->ipo_addr.sen_ip_dst;
  604. ipa->ipa_mask.sen_ip_dst = ipo->ipo_mask.sen_ip_dst;
  605. }
  606. ipa->ipa_info.sen_proto = ipo->ipo_addr.sen_proto;
  607. ipa->ipa_mask.sen_proto = ipo->ipo_mask.sen_proto;
  608. if (ipo->ipo_addr.sen_proto) {
  609. ipa->ipa_info.sen_sport = ipo->ipo_addr.sen_sport;
  610. ipa->ipa_mask.sen_sport = ipo->ipo_mask.sen_sport;
  611. ipa->ipa_info.sen_dport = ipo->ipo_addr.sen_dport;
  612. ipa->ipa_mask.sen_dport = ipo->ipo_mask.sen_dport;
  613. }
  614. break;
  615. #ifdef INET6
  616. case SENT_IP6:
  617. ipa->ipa_info.sen_type = ipa->ipa_mask.sen_type = SENT_IP6;
  618. ipa->ipa_info.sen_ip6_direction =
  619. ipo->ipo_addr.sen_ip6_direction;
  620. ipa->ipa_mask.sen_ip6_direction =
  621. ipo->ipo_mask.sen_ip6_direction;
  622. if (ipsp_is_unspecified(ipo->ipo_dst)) {
  623. ipa->ipa_info.sen_ip6_src = ddst->sen_ip6_src;
  624. ipa->ipa_mask.sen_ip6_src = in6mask128;
  625. ipa->ipa_info.sen_ip6_dst = ddst->sen_ip6_dst;
  626. ipa->ipa_mask.sen_ip6_dst = in6mask128;
  627. } else {
  628. ipa->ipa_info.sen_ip6_src = ipo->ipo_addr.sen_ip6_src;
  629. ipa->ipa_mask.sen_ip6_src = ipo->ipo_mask.sen_ip6_src;
  630. ipa->ipa_info.sen_ip6_dst = ipo->ipo_addr.sen_ip6_dst;
  631. ipa->ipa_mask.sen_ip6_dst = ipo->ipo_mask.sen_ip6_dst;
  632. }
  633. ipa->ipa_info.sen_ip6_proto = ipo->ipo_addr.sen_ip6_proto;
  634. ipa->ipa_mask.sen_ip6_proto = ipo->ipo_mask.sen_ip6_proto;
  635. if (ipo->ipo_mask.sen_ip6_proto) {
  636. ipa->ipa_info.sen_ip6_sport =
  637. ipo->ipo_addr.sen_ip6_sport;
  638. ipa->ipa_mask.sen_ip6_sport =
  639. ipo->ipo_mask.sen_ip6_sport;
  640. ipa->ipa_info.sen_ip6_dport =
  641. ipo->ipo_addr.sen_ip6_dport;
  642. ipa->ipa_mask.sen_ip6_dport =
  643. ipo->ipo_mask.sen_ip6_dport;
  644. }
  645. break;
  646. #endif /* INET6 */
  647. default:
  648. pool_put(&ipsec_acquire_pool, ipa);
  649. return 0;
  650. }
  651. timeout_add_sec(&ipa->ipa_timeout, ipsec_expire_acquire);
  652. TAILQ_INSERT_TAIL(&ipsec_acquire_head, ipa, ipa_next);
  653. TAILQ_INSERT_TAIL(&ipo->ipo_acquires, ipa, ipa_ipo_next);
  654. ipa->ipa_policy = ipo;
  655. /* PF_KEYv2 notification message. */
  656. return pfkeyv2_acquire(ipo, gw, laddr, &ipa->ipa_seq, ddst);
  657. }
  658. /*
  659. * Deal with PCB security requirements.
  660. */
  661. struct tdb *
  662. ipsp_spd_inp(struct mbuf *m, int af, int hlen, int *error, int direction,
  663. struct tdb *tdbp, struct inpcb *inp, struct ipsec_policy *ipo)
  664. {
  665. /* Sanity check. */
  666. if (inp == NULL)
  667. goto justreturn;
  668. /* We only support IPSEC_LEVEL_BYPASS or IPSEC_LEVEL_AVAIL */
  669. if (inp->inp_seclevel[SL_ESP_TRANS] == IPSEC_LEVEL_BYPASS &&
  670. inp->inp_seclevel[SL_ESP_NETWORK] == IPSEC_LEVEL_BYPASS &&
  671. inp->inp_seclevel[SL_AUTH] == IPSEC_LEVEL_BYPASS)
  672. goto justreturn;
  673. if (inp->inp_seclevel[SL_ESP_TRANS] == IPSEC_LEVEL_AVAIL &&
  674. inp->inp_seclevel[SL_ESP_NETWORK] == IPSEC_LEVEL_AVAIL &&
  675. inp->inp_seclevel[SL_AUTH] == IPSEC_LEVEL_AVAIL)
  676. goto justreturn;
  677. *error = -EINVAL;
  678. return NULL;
  679. justreturn:
  680. if (ipo != NULL)
  681. return ipo->ipo_tdb;
  682. else
  683. return NULL;
  684. }
  685. /*
  686. * Find a pending ACQUIRE record based on its sequence number.
  687. * XXX Need to use a better data structure.
  688. */
  689. struct ipsec_acquire *
  690. ipsec_get_acquire(u_int32_t seq)
  691. {
  692. struct ipsec_acquire *ipa;
  693. TAILQ_FOREACH (ipa, &ipsec_acquire_head, ipa_next)
  694. if (ipa->ipa_seq == seq)
  695. return ipa;
  696. return NULL;
  697. }