pfkeyv2_convert.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /* $OpenBSD: pfkeyv2_convert.c,v 1.54 2015/06/11 15:59:17 mikeb Exp $ */
  2. /*
  3. * The author of this code is Angelos D. Keromytis (angelos@keromytis.org)
  4. *
  5. * Part of this code is based on code written by Craig Metz (cmetz@inner.net)
  6. * for NRL. Those licenses follow this one.
  7. *
  8. * Copyright (c) 2001 Angelos D. Keromytis.
  9. *
  10. * Permission to use, copy, and modify this software with or without fee
  11. * is hereby granted, provided that this entire notice is included in
  12. * all copies of any software which is or includes a copy or
  13. * modification of this software.
  14. * You may use this code under the GNU public license if you so wish. Please
  15. * contribute changes back to the authors under this freer than GPL license
  16. * so that we may further the use of strong encryption without limitations to
  17. * all.
  18. *
  19. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
  20. * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
  21. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
  22. * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
  23. * PURPOSE.
  24. */
  25. /*
  26. * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995
  27. *
  28. * NRL grants permission for redistribution and use in source and binary
  29. * forms, with or without modification, of the software and documentation
  30. * created at NRL provided that the following conditions are met:
  31. *
  32. * 1. Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. * 2. Redistributions in binary form must reproduce the above copyright
  35. * notice, this list of conditions and the following disclaimer in the
  36. * documentation and/or other materials provided with the distribution.
  37. * 3. All advertising materials mentioning features or use of this software
  38. * must display the following acknowledgements:
  39. * This product includes software developed by the University of
  40. * California, Berkeley and its contributors.
  41. * This product includes software developed at the Information
  42. * Technology Division, US Naval Research Laboratory.
  43. * 4. Neither the name of the NRL nor the names of its contributors
  44. * may be used to endorse or promote products derived from this software
  45. * without specific prior written permission.
  46. *
  47. * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
  48. * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  49. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  50. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
  51. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  52. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  53. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  54. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  55. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  56. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  57. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  58. *
  59. * The views and conclusions contained in the software and documentation
  60. * are those of the authors and should not be interpreted as representing
  61. * official policies, either expressed or implied, of the US Naval
  62. * Research Laboratory (NRL).
  63. */
  64. /*
  65. * Copyright (c) 1995, 1996, 1997, 1998, 1999 Craig Metz. All rights reserved.
  66. *
  67. * Redistribution and use in source and binary forms, with or without
  68. * modification, are permitted provided that the following conditions
  69. * are met:
  70. * 1. Redistributions of source code must retain the above copyright
  71. * notice, this list of conditions and the following disclaimer.
  72. * 2. Redistributions in binary form must reproduce the above copyright
  73. * notice, this list of conditions and the following disclaimer in the
  74. * documentation and/or other materials provided with the distribution.
  75. * 3. Neither the name of the author nor the names of any contributors
  76. * may be used to endorse or promote products derived from this software
  77. * without specific prior written permission.
  78. *
  79. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  80. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  81. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  82. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  83. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  84. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  85. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  86. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  87. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  88. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  89. * SUCH DAMAGE.
  90. */
  91. #include "pf.h"
  92. #include <sys/types.h>
  93. #include <sys/param.h>
  94. #include <sys/systm.h>
  95. #include <sys/mbuf.h>
  96. #include <sys/kernel.h>
  97. #include <sys/socket.h>
  98. #include <sys/timeout.h>
  99. #include <net/route.h>
  100. #include <net/if.h>
  101. #include <netinet/in.h>
  102. #include <netinet/ip_ipsp.h>
  103. #include <net/pfkeyv2.h>
  104. #include <crypto/cryptodev.h>
  105. #include <crypto/xform.h>
  106. #if NPF > 0
  107. #include <net/pfvar.h>
  108. #endif
  109. /*
  110. * (Partly) Initialize a TDB based on an SADB_SA payload. Other parts
  111. * of the TDB will be initialized by other import routines, and tdb_init().
  112. */
  113. void
  114. import_sa(struct tdb *tdb, struct sadb_sa *sadb_sa, struct ipsecinit *ii)
  115. {
  116. if (!sadb_sa)
  117. return;
  118. if (ii) {
  119. ii->ii_encalg = sadb_sa->sadb_sa_encrypt;
  120. ii->ii_authalg = sadb_sa->sadb_sa_auth;
  121. ii->ii_compalg = sadb_sa->sadb_sa_encrypt; /* Yeurk! */
  122. tdb->tdb_spi = sadb_sa->sadb_sa_spi;
  123. tdb->tdb_wnd = sadb_sa->sadb_sa_replay;
  124. if (sadb_sa->sadb_sa_flags & SADB_SAFLAGS_PFS)
  125. tdb->tdb_flags |= TDBF_PFS;
  126. if (sadb_sa->sadb_sa_flags & SADB_X_SAFLAGS_TUNNEL)
  127. tdb->tdb_flags |= TDBF_TUNNELING;
  128. if (sadb_sa->sadb_sa_flags & SADB_X_SAFLAGS_UDPENCAP)
  129. tdb->tdb_flags |= TDBF_UDPENCAP;
  130. if (sadb_sa->sadb_sa_flags & SADB_X_SAFLAGS_ESN)
  131. tdb->tdb_flags |= TDBF_ESN;
  132. }
  133. if (sadb_sa->sadb_sa_state != SADB_SASTATE_MATURE)
  134. tdb->tdb_flags |= TDBF_INVALID;
  135. }
  136. /*
  137. * Export some of the information on a TDB.
  138. */
  139. void
  140. export_sa(void **p, struct tdb *tdb)
  141. {
  142. struct sadb_sa *sadb_sa = (struct sadb_sa *) *p;
  143. sadb_sa->sadb_sa_len = sizeof(struct sadb_sa) / sizeof(uint64_t);
  144. sadb_sa->sadb_sa_spi = tdb->tdb_spi;
  145. sadb_sa->sadb_sa_replay = tdb->tdb_wnd;
  146. if (tdb->tdb_flags & TDBF_INVALID)
  147. sadb_sa->sadb_sa_state = SADB_SASTATE_LARVAL;
  148. else
  149. sadb_sa->sadb_sa_state = SADB_SASTATE_MATURE;
  150. if (tdb->tdb_sproto == IPPROTO_IPCOMP &&
  151. tdb->tdb_compalgxform != NULL) {
  152. switch (tdb->tdb_compalgxform->type) {
  153. case CRYPTO_DEFLATE_COMP:
  154. sadb_sa->sadb_sa_encrypt = SADB_X_CALG_DEFLATE;
  155. break;
  156. case CRYPTO_LZS_COMP:
  157. sadb_sa->sadb_sa_encrypt = SADB_X_CALG_LZS;
  158. break;
  159. }
  160. }
  161. if (tdb->tdb_authalgxform) {
  162. switch (tdb->tdb_authalgxform->type) {
  163. case CRYPTO_MD5_HMAC:
  164. sadb_sa->sadb_sa_auth = SADB_AALG_MD5HMAC;
  165. break;
  166. case CRYPTO_SHA1_HMAC:
  167. sadb_sa->sadb_sa_auth = SADB_AALG_SHA1HMAC;
  168. break;
  169. case CRYPTO_RIPEMD160_HMAC:
  170. sadb_sa->sadb_sa_auth = SADB_X_AALG_RIPEMD160HMAC;
  171. break;
  172. case CRYPTO_SHA2_256_HMAC:
  173. sadb_sa->sadb_sa_auth = SADB_X_AALG_SHA2_256;
  174. break;
  175. case CRYPTO_SHA2_384_HMAC:
  176. sadb_sa->sadb_sa_auth = SADB_X_AALG_SHA2_384;
  177. break;
  178. case CRYPTO_SHA2_512_HMAC:
  179. sadb_sa->sadb_sa_auth = SADB_X_AALG_SHA2_512;
  180. break;
  181. case CRYPTO_AES_128_GMAC:
  182. sadb_sa->sadb_sa_auth = SADB_X_AALG_AES128GMAC;
  183. break;
  184. case CRYPTO_AES_192_GMAC:
  185. sadb_sa->sadb_sa_auth = SADB_X_AALG_AES192GMAC;
  186. break;
  187. case CRYPTO_AES_256_GMAC:
  188. sadb_sa->sadb_sa_auth = SADB_X_AALG_AES256GMAC;
  189. break;
  190. }
  191. }
  192. if (tdb->tdb_encalgxform) {
  193. switch (tdb->tdb_encalgxform->type) {
  194. case CRYPTO_NULL:
  195. sadb_sa->sadb_sa_encrypt = SADB_EALG_NULL;
  196. break;
  197. case CRYPTO_DES_CBC:
  198. sadb_sa->sadb_sa_encrypt = SADB_EALG_DESCBC;
  199. break;
  200. case CRYPTO_3DES_CBC:
  201. sadb_sa->sadb_sa_encrypt = SADB_EALG_3DESCBC;
  202. break;
  203. case CRYPTO_AES_CBC:
  204. sadb_sa->sadb_sa_encrypt = SADB_X_EALG_AES;
  205. break;
  206. case CRYPTO_AES_CTR:
  207. sadb_sa->sadb_sa_encrypt = SADB_X_EALG_AESCTR;
  208. break;
  209. case CRYPTO_AES_GCM_16:
  210. sadb_sa->sadb_sa_encrypt = SADB_X_EALG_AESGCM16;
  211. break;
  212. case CRYPTO_AES_GMAC:
  213. sadb_sa->sadb_sa_encrypt = SADB_X_EALG_AESGMAC;
  214. break;
  215. case CRYPTO_CAST_CBC:
  216. sadb_sa->sadb_sa_encrypt = SADB_X_EALG_CAST;
  217. break;
  218. case CRYPTO_BLF_CBC:
  219. sadb_sa->sadb_sa_encrypt = SADB_X_EALG_BLF;
  220. break;
  221. }
  222. }
  223. if (tdb->tdb_flags & TDBF_PFS)
  224. sadb_sa->sadb_sa_flags |= SADB_SAFLAGS_PFS;
  225. if (tdb->tdb_flags & TDBF_TUNNELING)
  226. sadb_sa->sadb_sa_flags |= SADB_X_SAFLAGS_TUNNEL;
  227. if (tdb->tdb_flags & TDBF_UDPENCAP)
  228. sadb_sa->sadb_sa_flags |= SADB_X_SAFLAGS_UDPENCAP;
  229. if (tdb->tdb_flags & TDBF_ESN)
  230. sadb_sa->sadb_sa_flags |= SADB_X_SAFLAGS_ESN;
  231. *p += sizeof(struct sadb_sa);
  232. }
  233. /*
  234. * Initialize expirations and counters based on lifetime payload.
  235. */
  236. void
  237. import_lifetime(struct tdb *tdb, struct sadb_lifetime *sadb_lifetime, int type)
  238. {
  239. if (!sadb_lifetime)
  240. return;
  241. switch (type) {
  242. case PFKEYV2_LIFETIME_HARD:
  243. if ((tdb->tdb_exp_allocations =
  244. sadb_lifetime->sadb_lifetime_allocations) != 0)
  245. tdb->tdb_flags |= TDBF_ALLOCATIONS;
  246. else
  247. tdb->tdb_flags &= ~TDBF_ALLOCATIONS;
  248. if ((tdb->tdb_exp_bytes =
  249. sadb_lifetime->sadb_lifetime_bytes) != 0)
  250. tdb->tdb_flags |= TDBF_BYTES;
  251. else
  252. tdb->tdb_flags &= ~TDBF_BYTES;
  253. if ((tdb->tdb_exp_timeout =
  254. sadb_lifetime->sadb_lifetime_addtime) != 0) {
  255. tdb->tdb_flags |= TDBF_TIMER;
  256. timeout_add_sec(&tdb->tdb_timer_tmo,
  257. tdb->tdb_exp_timeout);
  258. } else
  259. tdb->tdb_flags &= ~TDBF_TIMER;
  260. if ((tdb->tdb_exp_first_use =
  261. sadb_lifetime->sadb_lifetime_usetime) != 0)
  262. tdb->tdb_flags |= TDBF_FIRSTUSE;
  263. else
  264. tdb->tdb_flags &= ~TDBF_FIRSTUSE;
  265. break;
  266. case PFKEYV2_LIFETIME_SOFT:
  267. if ((tdb->tdb_soft_allocations =
  268. sadb_lifetime->sadb_lifetime_allocations) != 0)
  269. tdb->tdb_flags |= TDBF_SOFT_ALLOCATIONS;
  270. else
  271. tdb->tdb_flags &= ~TDBF_SOFT_ALLOCATIONS;
  272. if ((tdb->tdb_soft_bytes =
  273. sadb_lifetime->sadb_lifetime_bytes) != 0)
  274. tdb->tdb_flags |= TDBF_SOFT_BYTES;
  275. else
  276. tdb->tdb_flags &= ~TDBF_SOFT_BYTES;
  277. if ((tdb->tdb_soft_timeout =
  278. sadb_lifetime->sadb_lifetime_addtime) != 0) {
  279. tdb->tdb_flags |= TDBF_SOFT_TIMER;
  280. timeout_add_sec(&tdb->tdb_stimer_tmo,
  281. tdb->tdb_soft_timeout);
  282. } else
  283. tdb->tdb_flags &= ~TDBF_SOFT_TIMER;
  284. if ((tdb->tdb_soft_first_use =
  285. sadb_lifetime->sadb_lifetime_usetime) != 0)
  286. tdb->tdb_flags |= TDBF_SOFT_FIRSTUSE;
  287. else
  288. tdb->tdb_flags &= ~TDBF_SOFT_FIRSTUSE;
  289. break;
  290. case PFKEYV2_LIFETIME_CURRENT: /* Nothing fancy here. */
  291. tdb->tdb_cur_allocations =
  292. sadb_lifetime->sadb_lifetime_allocations;
  293. tdb->tdb_cur_bytes = sadb_lifetime->sadb_lifetime_bytes;
  294. tdb->tdb_established = sadb_lifetime->sadb_lifetime_addtime;
  295. tdb->tdb_first_use = sadb_lifetime->sadb_lifetime_usetime;
  296. }
  297. }
  298. /*
  299. * Export TDB expiration information.
  300. */
  301. void
  302. export_lifetime(void **p, struct tdb *tdb, int type)
  303. {
  304. struct sadb_lifetime *sadb_lifetime = (struct sadb_lifetime *) *p;
  305. sadb_lifetime->sadb_lifetime_len = sizeof(struct sadb_lifetime) /
  306. sizeof(uint64_t);
  307. switch (type) {
  308. case PFKEYV2_LIFETIME_HARD:
  309. if (tdb->tdb_flags & TDBF_ALLOCATIONS)
  310. sadb_lifetime->sadb_lifetime_allocations =
  311. tdb->tdb_exp_allocations;
  312. if (tdb->tdb_flags & TDBF_BYTES)
  313. sadb_lifetime->sadb_lifetime_bytes =
  314. tdb->tdb_exp_bytes;
  315. if (tdb->tdb_flags & TDBF_TIMER)
  316. sadb_lifetime->sadb_lifetime_addtime =
  317. tdb->tdb_exp_timeout;
  318. if (tdb->tdb_flags & TDBF_FIRSTUSE)
  319. sadb_lifetime->sadb_lifetime_usetime =
  320. tdb->tdb_exp_first_use;
  321. break;
  322. case PFKEYV2_LIFETIME_SOFT:
  323. if (tdb->tdb_flags & TDBF_SOFT_ALLOCATIONS)
  324. sadb_lifetime->sadb_lifetime_allocations =
  325. tdb->tdb_soft_allocations;
  326. if (tdb->tdb_flags & TDBF_SOFT_BYTES)
  327. sadb_lifetime->sadb_lifetime_bytes =
  328. tdb->tdb_soft_bytes;
  329. if (tdb->tdb_flags & TDBF_SOFT_TIMER)
  330. sadb_lifetime->sadb_lifetime_addtime =
  331. tdb->tdb_soft_timeout;
  332. if (tdb->tdb_flags & TDBF_SOFT_FIRSTUSE)
  333. sadb_lifetime->sadb_lifetime_usetime =
  334. tdb->tdb_soft_first_use;
  335. break;
  336. case PFKEYV2_LIFETIME_CURRENT:
  337. sadb_lifetime->sadb_lifetime_allocations =
  338. tdb->tdb_cur_allocations;
  339. sadb_lifetime->sadb_lifetime_bytes = tdb->tdb_cur_bytes;
  340. sadb_lifetime->sadb_lifetime_addtime = tdb->tdb_established;
  341. sadb_lifetime->sadb_lifetime_usetime = tdb->tdb_first_use;
  342. break;
  343. case PFKEYV2_LIFETIME_LASTUSE:
  344. sadb_lifetime->sadb_lifetime_allocations = 0;
  345. sadb_lifetime->sadb_lifetime_bytes = 0;
  346. sadb_lifetime->sadb_lifetime_addtime = 0;
  347. sadb_lifetime->sadb_lifetime_usetime = tdb->tdb_last_used;
  348. break;
  349. }
  350. *p += sizeof(struct sadb_lifetime);
  351. }
  352. /*
  353. * Import flow information to two struct sockaddr_encap's. Either
  354. * all or none of the address arguments are NULL.
  355. */
  356. void
  357. import_flow(struct sockaddr_encap *flow, struct sockaddr_encap *flowmask,
  358. struct sadb_address *ssrc, struct sadb_address *ssrcmask,
  359. struct sadb_address *ddst, struct sadb_address *ddstmask,
  360. struct sadb_protocol *sab, struct sadb_protocol *ftype)
  361. {
  362. u_int8_t transproto = 0;
  363. union sockaddr_union *src = (union sockaddr_union *)(ssrc + 1);
  364. union sockaddr_union *dst = (union sockaddr_union *)(ddst + 1);
  365. union sockaddr_union *srcmask = (union sockaddr_union *)(ssrcmask + 1);
  366. union sockaddr_union *dstmask = (union sockaddr_union *)(ddstmask + 1);
  367. if (ssrc == NULL)
  368. return; /* There wasn't any information to begin with. */
  369. bzero(flow, sizeof(*flow));
  370. bzero(flowmask, sizeof(*flowmask));
  371. if (sab != NULL)
  372. transproto = sab->sadb_protocol_proto;
  373. /*
  374. * Check that all the address families match. We know they are
  375. * valid and supported because pfkeyv2_parsemessage() checked that.
  376. */
  377. if ((src->sa.sa_family != dst->sa.sa_family) ||
  378. (src->sa.sa_family != srcmask->sa.sa_family) ||
  379. (src->sa.sa_family != dstmask->sa.sa_family))
  380. return;
  381. /*
  382. * We set these as an indication that tdb_filter/tdb_filtermask are
  383. * in fact initialized.
  384. */
  385. flow->sen_family = flowmask->sen_family = PF_KEY;
  386. flow->sen_len = flowmask->sen_len = SENT_LEN;
  387. switch (src->sa.sa_family) {
  388. case AF_INET:
  389. /* netmask handling */
  390. rt_maskedcopy(&src->sa, &src->sa, &srcmask->sa);
  391. rt_maskedcopy(&dst->sa, &dst->sa, &dstmask->sa);
  392. flow->sen_type = SENT_IP4;
  393. flow->sen_direction = ftype->sadb_protocol_direction;
  394. flow->sen_ip_src = src->sin.sin_addr;
  395. flow->sen_ip_dst = dst->sin.sin_addr;
  396. flow->sen_proto = transproto;
  397. flow->sen_sport = src->sin.sin_port;
  398. flow->sen_dport = dst->sin.sin_port;
  399. flowmask->sen_type = SENT_IP4;
  400. flowmask->sen_direction = 0xff;
  401. flowmask->sen_ip_src = srcmask->sin.sin_addr;
  402. flowmask->sen_ip_dst = dstmask->sin.sin_addr;
  403. flowmask->sen_sport = srcmask->sin.sin_port;
  404. flowmask->sen_dport = dstmask->sin.sin_port;
  405. if (transproto)
  406. flowmask->sen_proto = 0xff;
  407. break;
  408. #ifdef INET6
  409. case AF_INET6:
  410. in6_embedscope(&src->sin6.sin6_addr, &src->sin6,
  411. NULL, NULL);
  412. in6_embedscope(&dst->sin6.sin6_addr, &dst->sin6,
  413. NULL, NULL);
  414. /* netmask handling */
  415. rt_maskedcopy(&src->sa, &src->sa, &srcmask->sa);
  416. rt_maskedcopy(&dst->sa, &dst->sa, &dstmask->sa);
  417. flow->sen_type = SENT_IP6;
  418. flow->sen_ip6_direction = ftype->sadb_protocol_direction;
  419. flow->sen_ip6_src = src->sin6.sin6_addr;
  420. flow->sen_ip6_dst = dst->sin6.sin6_addr;
  421. flow->sen_ip6_proto = transproto;
  422. flow->sen_ip6_sport = src->sin6.sin6_port;
  423. flow->sen_ip6_dport = dst->sin6.sin6_port;
  424. flowmask->sen_type = SENT_IP6;
  425. flowmask->sen_ip6_direction = 0xff;
  426. flowmask->sen_ip6_src = srcmask->sin6.sin6_addr;
  427. flowmask->sen_ip6_dst = dstmask->sin6.sin6_addr;
  428. flowmask->sen_ip6_sport = srcmask->sin6.sin6_port;
  429. flowmask->sen_ip6_dport = dstmask->sin6.sin6_port;
  430. if (transproto)
  431. flowmask->sen_ip6_proto = 0xff;
  432. break;
  433. #endif /* INET6 */
  434. }
  435. }
  436. /*
  437. * Helper to export addresses from an struct sockaddr_encap.
  438. */
  439. static void
  440. export_encap(void **p, struct sockaddr_encap *encap, int type)
  441. {
  442. struct sadb_address *saddr = (struct sadb_address *)*p;
  443. union sockaddr_union *sunion;
  444. *p += sizeof(struct sadb_address);
  445. sunion = (union sockaddr_union *)*p;
  446. switch (encap->sen_type) {
  447. case SENT_IP4:
  448. saddr->sadb_address_len = (sizeof(struct sadb_address) +
  449. PADUP(sizeof(struct sockaddr_in))) / sizeof(uint64_t);
  450. sunion->sa.sa_len = sizeof(struct sockaddr_in);
  451. sunion->sa.sa_family = AF_INET;
  452. if (type == SADB_X_EXT_SRC_FLOW ||
  453. type == SADB_X_EXT_SRC_MASK) {
  454. sunion->sin.sin_addr = encap->sen_ip_src;
  455. sunion->sin.sin_port = encap->sen_sport;
  456. } else {
  457. sunion->sin.sin_addr = encap->sen_ip_dst;
  458. sunion->sin.sin_port = encap->sen_dport;
  459. }
  460. *p += PADUP(sizeof(struct sockaddr_in));
  461. break;
  462. case SENT_IP6:
  463. saddr->sadb_address_len = (sizeof(struct sadb_address)
  464. + PADUP(sizeof(struct sockaddr_in6))) / sizeof(uint64_t);
  465. sunion->sa.sa_len = sizeof(struct sockaddr_in6);
  466. sunion->sa.sa_family = AF_INET6;
  467. if (type == SADB_X_EXT_SRC_FLOW ||
  468. type == SADB_X_EXT_SRC_MASK) {
  469. sunion->sin6.sin6_addr = encap->sen_ip6_src;
  470. sunion->sin6.sin6_port = encap->sen_ip6_sport;
  471. } else {
  472. sunion->sin6.sin6_addr = encap->sen_ip6_dst;
  473. sunion->sin6.sin6_port = encap->sen_ip6_dport;
  474. }
  475. *p += PADUP(sizeof(struct sockaddr_in6));
  476. break;
  477. }
  478. }
  479. /*
  480. * Export flow information from two struct sockaddr_encap's.
  481. */
  482. void
  483. export_flow(void **p, u_int8_t ftype, struct sockaddr_encap *flow,
  484. struct sockaddr_encap *flowmask, void **headers)
  485. {
  486. struct sadb_protocol *sab;
  487. headers[SADB_X_EXT_FLOW_TYPE] = *p;
  488. sab = (struct sadb_protocol *)*p;
  489. sab->sadb_protocol_len = sizeof(struct sadb_protocol) /
  490. sizeof(uint64_t);
  491. switch (ftype) {
  492. case IPSP_IPSEC_USE:
  493. sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_USE;
  494. break;
  495. case IPSP_IPSEC_ACQUIRE:
  496. sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_ACQUIRE;
  497. break;
  498. case IPSP_IPSEC_REQUIRE:
  499. sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_REQUIRE;
  500. break;
  501. case IPSP_DENY:
  502. sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_DENY;
  503. break;
  504. case IPSP_PERMIT:
  505. sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_BYPASS;
  506. break;
  507. case IPSP_IPSEC_DONTACQ:
  508. sab->sadb_protocol_proto = SADB_X_FLOW_TYPE_DONTACQ;
  509. break;
  510. default:
  511. sab->sadb_protocol_proto = 0;
  512. break;
  513. }
  514. switch (flow->sen_type) {
  515. case SENT_IP4:
  516. sab->sadb_protocol_direction = flow->sen_direction;
  517. break;
  518. #ifdef INET6
  519. case SENT_IP6:
  520. sab->sadb_protocol_direction = flow->sen_ip6_direction;
  521. break;
  522. #endif /* INET6 */
  523. }
  524. *p += sizeof(struct sadb_protocol);
  525. headers[SADB_X_EXT_PROTOCOL] = *p;
  526. sab = (struct sadb_protocol *)*p;
  527. sab->sadb_protocol_len = sizeof(struct sadb_protocol) /
  528. sizeof(uint64_t);
  529. switch (flow->sen_type) {
  530. case SENT_IP4:
  531. sab->sadb_protocol_proto = flow->sen_proto;
  532. break;
  533. #ifdef INET6
  534. case SENT_IP6:
  535. sab->sadb_protocol_proto = flow->sen_ip6_proto;
  536. break;
  537. #endif /* INET6 */
  538. }
  539. *p += sizeof(struct sadb_protocol);
  540. headers[SADB_X_EXT_SRC_FLOW] = *p;
  541. export_encap(p, flow, SADB_X_EXT_SRC_FLOW);
  542. headers[SADB_X_EXT_SRC_MASK] = *p;
  543. export_encap(p, flowmask, SADB_X_EXT_SRC_MASK);
  544. headers[SADB_X_EXT_DST_FLOW] = *p;
  545. export_encap(p, flow, SADB_X_EXT_DST_FLOW);
  546. headers[SADB_X_EXT_DST_MASK] = *p;
  547. export_encap(p, flowmask, SADB_X_EXT_DST_MASK);
  548. }
  549. /*
  550. * Copy an SADB_ADDRESS payload to a struct sockaddr.
  551. */
  552. void
  553. import_address(struct sockaddr *sa, struct sadb_address *sadb_address)
  554. {
  555. int salen;
  556. struct sockaddr *ssa = (struct sockaddr *)((void *) sadb_address +
  557. sizeof(struct sadb_address));
  558. if (!sadb_address)
  559. return;
  560. if (ssa->sa_len)
  561. salen = ssa->sa_len;
  562. else
  563. switch (ssa->sa_family) {
  564. case AF_INET:
  565. salen = sizeof(struct sockaddr_in);
  566. break;
  567. #ifdef INET6
  568. case AF_INET6:
  569. salen = sizeof(struct sockaddr_in6);
  570. break;
  571. #endif /* INET6 */
  572. default:
  573. return;
  574. }
  575. bcopy(ssa, sa, salen);
  576. sa->sa_len = salen;
  577. }
  578. /*
  579. * Export a struct sockaddr as an SADB_ADDRESS payload.
  580. */
  581. void
  582. export_address(void **p, struct sockaddr *sa)
  583. {
  584. struct sadb_address *sadb_address = (struct sadb_address *) *p;
  585. sadb_address->sadb_address_len = (sizeof(struct sadb_address) +
  586. PADUP(SA_LEN(sa))) / sizeof(uint64_t);
  587. *p += sizeof(struct sadb_address);
  588. bcopy(sa, *p, SA_LEN(sa));
  589. ((struct sockaddr *) *p)->sa_family = sa->sa_family;
  590. *p += PADUP(SA_LEN(sa));
  591. }
  592. /*
  593. * Import an identity payload into the TDB.
  594. */
  595. static void
  596. import_identity(struct ipsec_id **id, struct sadb_ident *sadb_ident)
  597. {
  598. if (!sadb_ident) {
  599. *id = NULL;
  600. return;
  601. }
  602. *id = malloc(EXTLEN(sadb_ident) - sizeof(struct sadb_ident) +
  603. sizeof(struct ipsec_id), M_CREDENTIALS, M_WAITOK);
  604. (*id)->len = EXTLEN(sadb_ident) - sizeof(struct sadb_ident);
  605. switch (sadb_ident->sadb_ident_type) {
  606. case SADB_IDENTTYPE_PREFIX:
  607. (*id)->type = IPSP_IDENTITY_PREFIX;
  608. break;
  609. case SADB_IDENTTYPE_FQDN:
  610. (*id)->type = IPSP_IDENTITY_FQDN;
  611. break;
  612. case SADB_IDENTTYPE_USERFQDN:
  613. (*id)->type = IPSP_IDENTITY_USERFQDN;
  614. break;
  615. default:
  616. free(*id, M_CREDENTIALS, 0);
  617. *id = NULL;
  618. return;
  619. }
  620. bcopy((void *) sadb_ident + sizeof(struct sadb_ident), (*id) + 1,
  621. (*id)->len);
  622. }
  623. void
  624. import_identities(struct ipsec_ids **ids, int swapped,
  625. struct sadb_ident *srcid, struct sadb_ident *dstid)
  626. {
  627. struct ipsec_ids *tmp;
  628. *ids = NULL;
  629. tmp = malloc(sizeof(struct ipsec_ids), M_CREDENTIALS, M_WAITOK);
  630. import_identity(&tmp->id_local, swapped ? dstid: srcid);
  631. import_identity(&tmp->id_remote, swapped ? srcid: dstid);
  632. if (tmp->id_local != NULL && tmp->id_remote != NULL) {
  633. *ids = ipsp_ids_insert(tmp);
  634. if (*ids == tmp)
  635. return;
  636. }
  637. free(tmp->id_local, M_CREDENTIALS, 0);
  638. free(tmp->id_remote, M_CREDENTIALS, 0);
  639. free(tmp, M_CREDENTIALS, 0);
  640. }
  641. static void
  642. export_identity(void **p, struct ipsec_id *id)
  643. {
  644. struct sadb_ident *sadb_ident = (struct sadb_ident *) *p;
  645. sadb_ident->sadb_ident_len = (sizeof(struct sadb_ident) +
  646. PADUP(id->len)) / sizeof(uint64_t);
  647. switch (id->type) {
  648. case IPSP_IDENTITY_PREFIX:
  649. sadb_ident->sadb_ident_type = SADB_IDENTTYPE_PREFIX;
  650. break;
  651. case IPSP_IDENTITY_FQDN:
  652. sadb_ident->sadb_ident_type = SADB_IDENTTYPE_FQDN;
  653. break;
  654. case IPSP_IDENTITY_USERFQDN:
  655. sadb_ident->sadb_ident_type = SADB_IDENTTYPE_USERFQDN;
  656. break;
  657. }
  658. *p += sizeof(struct sadb_ident);
  659. bcopy(id + 1, *p, id->len);
  660. *p += PADUP(id->len);
  661. }
  662. void
  663. export_identities(void **p, struct ipsec_ids *ids, int swapped,
  664. void **headers)
  665. {
  666. headers[SADB_EXT_IDENTITY_SRC] = *p;
  667. export_identity(p, swapped ? ids->id_remote : ids->id_local);
  668. headers[SADB_EXT_IDENTITY_DST] = *p;
  669. export_identity(p, swapped ? ids->id_local : ids->id_remote);
  670. }
  671. /* ... */
  672. void
  673. import_key(struct ipsecinit *ii, struct sadb_key *sadb_key, int type)
  674. {
  675. if (!sadb_key)
  676. return;
  677. if (type == PFKEYV2_ENCRYPTION_KEY) { /* Encryption key */
  678. ii->ii_enckeylen = sadb_key->sadb_key_bits / 8;
  679. ii->ii_enckey = (void *)sadb_key + sizeof(struct sadb_key);
  680. } else {
  681. ii->ii_authkeylen = sadb_key->sadb_key_bits / 8;
  682. ii->ii_authkey = (void *)sadb_key + sizeof(struct sadb_key);
  683. }
  684. }
  685. void
  686. export_key(void **p, struct tdb *tdb, int type)
  687. {
  688. struct sadb_key *sadb_key = (struct sadb_key *) *p;
  689. if (type == PFKEYV2_ENCRYPTION_KEY) {
  690. sadb_key->sadb_key_len = (sizeof(struct sadb_key) +
  691. PADUP(tdb->tdb_emxkeylen)) /
  692. sizeof(uint64_t);
  693. sadb_key->sadb_key_bits = tdb->tdb_emxkeylen * 8;
  694. *p += sizeof(struct sadb_key);
  695. bcopy(tdb->tdb_emxkey, *p, tdb->tdb_emxkeylen);
  696. *p += PADUP(tdb->tdb_emxkeylen);
  697. } else {
  698. sadb_key->sadb_key_len = (sizeof(struct sadb_key) +
  699. PADUP(tdb->tdb_amxkeylen)) /
  700. sizeof(uint64_t);
  701. sadb_key->sadb_key_bits = tdb->tdb_amxkeylen * 8;
  702. *p += sizeof(struct sadb_key);
  703. bcopy(tdb->tdb_amxkey, *p, tdb->tdb_amxkeylen);
  704. *p += PADUP(tdb->tdb_amxkeylen);
  705. }
  706. }
  707. /* Import/Export remote port for UDP Encapsulation */
  708. void
  709. import_udpencap(struct tdb *tdb, struct sadb_x_udpencap *sadb_udpencap)
  710. {
  711. if (sadb_udpencap)
  712. tdb->tdb_udpencap_port = sadb_udpencap->sadb_x_udpencap_port;
  713. }
  714. void
  715. export_udpencap(void **p, struct tdb *tdb)
  716. {
  717. struct sadb_x_udpencap *sadb_udpencap = (struct sadb_x_udpencap *) *p;
  718. sadb_udpencap->sadb_x_udpencap_port = tdb->tdb_udpencap_port;
  719. sadb_udpencap->sadb_x_udpencap_reserved = 0;
  720. sadb_udpencap->sadb_x_udpencap_len =
  721. sizeof(struct sadb_x_udpencap) / sizeof(uint64_t);
  722. *p += sizeof(struct sadb_x_udpencap);
  723. }
  724. #if NPF > 0
  725. /* Import PF tag information for SA */
  726. void
  727. import_tag(struct tdb *tdb, struct sadb_x_tag *stag)
  728. {
  729. char *s;
  730. if (stag) {
  731. s = (char *)(stag + 1);
  732. tdb->tdb_tag = pf_tagname2tag(s, 1);
  733. }
  734. }
  735. /* Export PF tag information for SA */
  736. void
  737. export_tag(void **p, struct tdb *tdb)
  738. {
  739. struct sadb_x_tag *stag = (struct sadb_x_tag *)*p;
  740. char *s = (char *)(stag + 1);
  741. pf_tag2tagname(tdb->tdb_tag, s);
  742. stag->sadb_x_tag_taglen = strlen(s) + 1;
  743. stag->sadb_x_tag_len = (sizeof(struct sadb_x_tag) +
  744. PADUP(stag->sadb_x_tag_taglen)) / sizeof(uint64_t);
  745. *p += sizeof(struct sadb_x_tag) + PADUP(stag->sadb_x_tag_taglen);
  746. }
  747. /* Import enc(4) tap device information for SA */
  748. void
  749. import_tap(struct tdb *tdb, struct sadb_x_tap *stap)
  750. {
  751. if (stap)
  752. tdb->tdb_tap = stap->sadb_x_tap_unit;
  753. }
  754. /* Export enc(4) tap device information for SA */
  755. void
  756. export_tap(void **p, struct tdb *tdb)
  757. {
  758. struct sadb_x_tap *stag = (struct sadb_x_tap *)*p;
  759. stag->sadb_x_tap_unit = tdb->tdb_tap;
  760. stag->sadb_x_tap_len = sizeof(struct sadb_x_tap) / sizeof(uint64_t);
  761. *p += sizeof(struct sadb_x_tap);
  762. }
  763. #endif