ipsec_input.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /* $OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $ */
  2. /*-
  3. * The authors of this code are John Ioannidis (ji@tla.org),
  4. * Angelos D. Keromytis (kermit@csd.uch.gr) and
  5. * Niels Provos (provos@physnet.uni-hamburg.de).
  6. *
  7. * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
  8. * in November 1995.
  9. *
  10. * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
  11. * by Angelos D. Keromytis.
  12. *
  13. * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
  14. * and Niels Provos.
  15. *
  16. * Additional features in 1999 by Angelos D. Keromytis.
  17. *
  18. * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
  19. * Angelos D. Keromytis and Niels Provos.
  20. * Copyright (c) 2001, Angelos D. Keromytis.
  21. * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
  22. *
  23. * Permission to use, copy, and modify this software with or without fee
  24. * is hereby granted, provided that this entire notice is included in
  25. * all copies of any software which is or includes a copy or
  26. * modification of this software.
  27. * You may use this code under the GNU public license if you so wish. Please
  28. * contribute changes back to the authors under this freer than GPL license
  29. * so that we may further the use of strong encryption without limitations to
  30. * all.
  31. *
  32. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
  33. * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
  34. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
  35. * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
  36. * PURPOSE.
  37. */
  38. /*
  39. * IPsec input processing.
  40. */
  41. #include <sys/cdefs.h>
  42. __FBSDID("$FreeBSD$");
  43. #include "opt_inet.h"
  44. #include "opt_inet6.h"
  45. #include "opt_ipsec.h"
  46. #include <sys/param.h>
  47. #include <sys/systm.h>
  48. #include <sys/malloc.h>
  49. #include <sys/mbuf.h>
  50. #include <sys/domain.h>
  51. #include <sys/protosw.h>
  52. #include <sys/socket.h>
  53. #include <sys/errno.h>
  54. #include <sys/hhook.h>
  55. #include <sys/syslog.h>
  56. #include <net/if.h>
  57. #include <net/if_var.h>
  58. #include <net/if_enc.h>
  59. #include <net/netisr.h>
  60. #include <net/vnet.h>
  61. #include <netinet/in.h>
  62. #include <netinet/in_systm.h>
  63. #include <netinet/ip.h>
  64. #include <netinet/ip_var.h>
  65. #include <netinet/in_var.h>
  66. #include <netinet/ip6.h>
  67. #ifdef INET6
  68. #include <netinet6/ip6_var.h>
  69. #endif
  70. #include <netinet/in_pcb.h>
  71. #ifdef INET6
  72. #include <netinet/icmp6.h>
  73. #endif
  74. #include <netipsec/ipsec.h>
  75. #ifdef INET6
  76. #include <netipsec/ipsec6.h>
  77. #endif
  78. #include <netipsec/ah_var.h>
  79. #include <netipsec/esp.h>
  80. #include <netipsec/esp_var.h>
  81. #include <netipsec/ipcomp_var.h>
  82. #include <netipsec/key.h>
  83. #include <netipsec/keydb.h>
  84. #include <netipsec/key_debug.h>
  85. #include <netipsec/xform.h>
  86. #include <netinet6/ip6protosw.h>
  87. #include <machine/in_cksum.h>
  88. #include <machine/stdarg.h>
  89. #define IPSEC_ISTAT(proto, name) do { \
  90. if ((proto) == IPPROTO_ESP) \
  91. ESPSTAT_INC(esps_##name); \
  92. else if ((proto) == IPPROTO_AH) \
  93. AHSTAT_INC(ahs_##name); \
  94. else \
  95. IPCOMPSTAT_INC(ipcomps_##name); \
  96. } while (0)
  97. /*
  98. * ipsec_common_input gets called when an IPsec-protected packet
  99. * is received by IPv4 or IPv6. Its job is to find the right SA
  100. * and call the appropriate transform. The transform callback
  101. * takes care of further processing (like ingress filtering).
  102. */
  103. static int
  104. ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
  105. {
  106. IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
  107. union sockaddr_union dst_address;
  108. struct secasvar *sav;
  109. uint32_t spi;
  110. int error;
  111. IPSEC_ISTAT(sproto, input);
  112. IPSEC_ASSERT(m != NULL, ("null packet"));
  113. IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
  114. sproto == IPPROTO_IPCOMP,
  115. ("unexpected security protocol %u", sproto));
  116. if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
  117. (sproto == IPPROTO_AH && !V_ah_enable) ||
  118. (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
  119. m_freem(m);
  120. IPSEC_ISTAT(sproto, pdrops);
  121. return EOPNOTSUPP;
  122. }
  123. if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
  124. m_freem(m);
  125. IPSEC_ISTAT(sproto, hdrops);
  126. DPRINTF(("%s: packet too small\n", __func__));
  127. return EINVAL;
  128. }
  129. /* Retrieve the SPI from the relevant IPsec header */
  130. if (sproto == IPPROTO_ESP)
  131. m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
  132. else if (sproto == IPPROTO_AH)
  133. m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
  134. (caddr_t) &spi);
  135. else if (sproto == IPPROTO_IPCOMP) {
  136. u_int16_t cpi;
  137. m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
  138. (caddr_t) &cpi);
  139. spi = ntohl(htons(cpi));
  140. }
  141. /*
  142. * Find the SA and (indirectly) call the appropriate
  143. * kernel crypto routine. The resulting mbuf chain is a valid
  144. * IP packet ready to go through input processing.
  145. */
  146. bzero(&dst_address, sizeof (dst_address));
  147. dst_address.sa.sa_family = af;
  148. switch (af) {
  149. #ifdef INET
  150. case AF_INET:
  151. dst_address.sin.sin_len = sizeof(struct sockaddr_in);
  152. m_copydata(m, offsetof(struct ip, ip_dst),
  153. sizeof(struct in_addr),
  154. (caddr_t) &dst_address.sin.sin_addr);
  155. break;
  156. #endif /* INET */
  157. #ifdef INET6
  158. case AF_INET6:
  159. dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
  160. m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
  161. sizeof(struct in6_addr),
  162. (caddr_t) &dst_address.sin6.sin6_addr);
  163. /* We keep addresses in SADB without embedded scope id */
  164. if (IN6_IS_SCOPE_LINKLOCAL(&dst_address.sin6.sin6_addr)) {
  165. /* XXX: sa6_recoverscope() */
  166. dst_address.sin6.sin6_scope_id =
  167. ntohs(dst_address.sin6.sin6_addr.s6_addr16[1]);
  168. dst_address.sin6.sin6_addr.s6_addr16[1] = 0;
  169. }
  170. break;
  171. #endif /* INET6 */
  172. default:
  173. DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
  174. m_freem(m);
  175. IPSEC_ISTAT(sproto, nopf);
  176. return EPFNOSUPPORT;
  177. }
  178. /* NB: only pass dst since key_allocsa follows RFC2401 */
  179. sav = key_allocsa(&dst_address, sproto, spi);
  180. if (sav == NULL) {
  181. DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
  182. __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
  183. (u_long) ntohl(spi), sproto));
  184. IPSEC_ISTAT(sproto, notdb);
  185. m_freem(m);
  186. return ENOENT;
  187. }
  188. if (sav->tdb_xform == NULL) {
  189. DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
  190. __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
  191. (u_long) ntohl(spi), sproto));
  192. IPSEC_ISTAT(sproto, noxform);
  193. key_freesav(&sav);
  194. m_freem(m);
  195. return ENXIO;
  196. }
  197. /*
  198. * Call appropriate transform and return -- callback takes care of
  199. * everything else.
  200. */
  201. error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
  202. return (error);
  203. }
  204. #ifdef INET
  205. extern struct protosw inetsw[];
  206. /*
  207. * IPSEC_INPUT() method implementation for IPv4.
  208. * 0 - Permitted by inbound security policy for further processing.
  209. * EACCES - Forbidden by inbound security policy.
  210. * EINPROGRESS - consumed by IPsec.
  211. */
  212. int
  213. ipsec4_input(struct mbuf *m, int offset, int proto)
  214. {
  215. switch (proto) {
  216. case IPPROTO_AH:
  217. case IPPROTO_ESP:
  218. case IPPROTO_IPCOMP:
  219. /* Do inbound IPsec processing for AH/ESP/IPCOMP */
  220. ipsec_common_input(m, offset,
  221. offsetof(struct ip, ip_p), AF_INET, proto);
  222. return (EINPROGRESS); /* mbuf consumed by IPsec */
  223. default:
  224. /*
  225. * Protocols with further headers get their IPsec treatment
  226. * within the protocol specific processing.
  227. */
  228. if ((inetsw[ip_protox[proto]].pr_flags & PR_LASTHDR) == 0)
  229. return (0);
  230. /* FALLTHROUGH */
  231. };
  232. /*
  233. * Enforce IPsec policy checking if we are seeing last header.
  234. */
  235. if (ipsec4_in_reject(m, NULL) != 0) {
  236. /* Forbidden by inbound security policy */
  237. m_freem(m);
  238. return (EACCES);
  239. }
  240. return (0);
  241. }
  242. /*
  243. * IPsec input callback for INET protocols.
  244. * This routine is called as the transform callback.
  245. * Takes care of filtering and other sanity checks on
  246. * the processed packet.
  247. */
  248. int
  249. ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
  250. int protoff)
  251. {
  252. IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
  253. struct epoch_tracker et;
  254. struct ipsec_ctx_data ctx;
  255. struct xform_history *xh;
  256. struct secasindex *saidx;
  257. struct m_tag *mtag;
  258. struct ip *ip;
  259. int error, prot, af, sproto, isr_prot;
  260. IPSEC_ASSERT(sav != NULL, ("null SA"));
  261. IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
  262. saidx = &sav->sah->saidx;
  263. af = saidx->dst.sa.sa_family;
  264. IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
  265. sproto = saidx->proto;
  266. IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
  267. sproto == IPPROTO_IPCOMP,
  268. ("unexpected security protocol %u", sproto));
  269. if (skip != 0) {
  270. /*
  271. * Fix IPv4 header
  272. */
  273. if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
  274. DPRINTF(("%s: processing failed for SA %s/%08lx\n",
  275. __func__, ipsec_address(&sav->sah->saidx.dst,
  276. buf, sizeof(buf)), (u_long) ntohl(sav->spi)));
  277. IPSEC_ISTAT(sproto, hdrops);
  278. error = ENOBUFS;
  279. goto bad;
  280. }
  281. ip = mtod(m, struct ip *);
  282. ip->ip_len = htons(m->m_pkthdr.len);
  283. ip->ip_sum = 0;
  284. ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
  285. } else {
  286. ip = mtod(m, struct ip *);
  287. }
  288. prot = ip->ip_p;
  289. /*
  290. * Check that we have NAT-T enabled and apply transport mode
  291. * decapsulation NAT procedure (RFC3948).
  292. * Do this before invoking into the PFIL.
  293. */
  294. if (sav->natt != NULL &&
  295. (prot == IPPROTO_UDP || prot == IPPROTO_TCP))
  296. udp_ipsec_adjust_cksum(m, sav, prot, skip);
  297. IPSEC_INIT_CTX(&ctx, &m, NULL, sav, AF_INET, IPSEC_ENC_BEFORE);
  298. if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
  299. goto bad;
  300. ip = mtod(m, struct ip *); /* update pointer */
  301. /* IP-in-IP encapsulation */
  302. if (prot == IPPROTO_IPIP &&
  303. saidx->mode != IPSEC_MODE_TRANSPORT) {
  304. if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
  305. IPSEC_ISTAT(sproto, hdrops);
  306. error = EINVAL;
  307. goto bad;
  308. }
  309. /* enc0: strip outer IPv4 header */
  310. m_striphdr(m, 0, ip->ip_hl << 2);
  311. }
  312. #ifdef INET6
  313. /* IPv6-in-IP encapsulation. */
  314. else if (prot == IPPROTO_IPV6 &&
  315. saidx->mode != IPSEC_MODE_TRANSPORT) {
  316. if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
  317. IPSEC_ISTAT(sproto, hdrops);
  318. error = EINVAL;
  319. goto bad;
  320. }
  321. /* enc0: strip IPv4 header, keep IPv6 header only */
  322. m_striphdr(m, 0, ip->ip_hl << 2);
  323. }
  324. #endif /* INET6 */
  325. else if (prot != IPPROTO_IPV6 && saidx->mode == IPSEC_MODE_ANY) {
  326. /*
  327. * When mode is wildcard, inner protocol is IPv6 and
  328. * we have no INET6 support - drop this packet a bit later.
  329. * In other cases we assume transport mode. Set prot to
  330. * correctly choose netisr.
  331. */
  332. prot = IPPROTO_IPIP;
  333. }
  334. /*
  335. * Record what we've done to the packet (under what SA it was
  336. * processed).
  337. */
  338. if (sproto != IPPROTO_IPCOMP) {
  339. mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
  340. sizeof(struct xform_history), M_NOWAIT);
  341. if (mtag == NULL) {
  342. DPRINTF(("%s: failed to get tag\n", __func__));
  343. IPSEC_ISTAT(sproto, hdrops);
  344. error = ENOMEM;
  345. goto bad;
  346. }
  347. xh = (struct xform_history *)(mtag + 1);
  348. bcopy(&saidx->dst, &xh->dst, saidx->dst.sa.sa_len);
  349. xh->spi = sav->spi;
  350. xh->proto = sproto;
  351. xh->mode = saidx->mode;
  352. m_tag_prepend(m, mtag);
  353. }
  354. key_sa_recordxfer(sav, m); /* record data transfer */
  355. /*
  356. * In transport mode requeue decrypted mbuf back to IPv4 protocol
  357. * handler. This is necessary to correctly expose rcvif.
  358. */
  359. if (saidx->mode == IPSEC_MODE_TRANSPORT)
  360. prot = IPPROTO_IPIP;
  361. /*
  362. * Re-dispatch via software interrupt.
  363. */
  364. switch (prot) {
  365. case IPPROTO_IPIP:
  366. isr_prot = NETISR_IP;
  367. af = AF_INET;
  368. break;
  369. #ifdef INET6
  370. case IPPROTO_IPV6:
  371. isr_prot = NETISR_IPV6;
  372. af = AF_INET6;
  373. break;
  374. #endif
  375. default:
  376. DPRINTF(("%s: cannot handle inner ip proto %d\n",
  377. __func__, prot));
  378. IPSEC_ISTAT(sproto, nopf);
  379. error = EPFNOSUPPORT;
  380. goto bad;
  381. }
  382. IPSEC_INIT_CTX(&ctx, &m, NULL, sav, af, IPSEC_ENC_AFTER);
  383. if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
  384. goto bad;
  385. /* Handle virtual tunneling interfaces */
  386. if (saidx->mode == IPSEC_MODE_TUNNEL)
  387. error = ipsec_if_input(m, sav, af);
  388. if (error == 0) {
  389. NET_EPOCH_ENTER(et);
  390. error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
  391. NET_EPOCH_EXIT(et);
  392. if (error) {
  393. IPSEC_ISTAT(sproto, qfull);
  394. DPRINTF(("%s: queue full; proto %u packet dropped\n",
  395. __func__, sproto));
  396. }
  397. }
  398. key_freesav(&sav);
  399. return (error);
  400. bad:
  401. key_freesav(&sav);
  402. if (m != NULL)
  403. m_freem(m);
  404. return (error);
  405. }
  406. #endif /* INET */
  407. #ifdef INET6
  408. /*
  409. * IPSEC_INPUT() method implementation for IPv6.
  410. * 0 - Permitted by inbound security policy for further processing.
  411. * EACCES - Forbidden by inbound security policy.
  412. * EINPROGRESS - consumed by IPsec.
  413. */
  414. int
  415. ipsec6_input(struct mbuf *m, int offset, int proto)
  416. {
  417. switch (proto) {
  418. case IPPROTO_AH:
  419. case IPPROTO_ESP:
  420. case IPPROTO_IPCOMP:
  421. /* Do inbound IPsec processing for AH/ESP/IPCOMP */
  422. ipsec_common_input(m, offset,
  423. offsetof(struct ip6_hdr, ip6_nxt), AF_INET6, proto);
  424. return (EINPROGRESS); /* mbuf consumed by IPsec */
  425. default:
  426. /*
  427. * Protocols with further headers get their IPsec treatment
  428. * within the protocol specific processing.
  429. */
  430. if ((inet6sw[ip6_protox[proto]].pr_flags & PR_LASTHDR) == 0)
  431. return (0);
  432. /* FALLTHROUGH */
  433. };
  434. /*
  435. * Enforce IPsec policy checking if we are seeing last header.
  436. */
  437. if (ipsec6_in_reject(m, NULL) != 0) {
  438. /* Forbidden by inbound security policy */
  439. m_freem(m);
  440. return (EACCES);
  441. }
  442. return (0);
  443. }
  444. /*
  445. * IPsec input callback, called by the transform callback. Takes care of
  446. * filtering and other sanity checks on the processed packet.
  447. */
  448. int
  449. ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
  450. int protoff)
  451. {
  452. IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
  453. struct epoch_tracker et;
  454. struct ipsec_ctx_data ctx;
  455. struct xform_history *xh;
  456. struct secasindex *saidx;
  457. struct ip6_hdr *ip6;
  458. struct m_tag *mtag;
  459. int prot, af, sproto;
  460. int nxt, isr_prot;
  461. int error, nest;
  462. uint8_t nxt8;
  463. IPSEC_ASSERT(sav != NULL, ("null SA"));
  464. IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
  465. saidx = &sav->sah->saidx;
  466. af = saidx->dst.sa.sa_family;
  467. IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
  468. sproto = saidx->proto;
  469. IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
  470. sproto == IPPROTO_IPCOMP,
  471. ("unexpected security protocol %u", sproto));
  472. /* Fix IPv6 header */
  473. if (m->m_len < sizeof(struct ip6_hdr) &&
  474. (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
  475. DPRINTF(("%s: processing failed for SA %s/%08lx\n",
  476. __func__, ipsec_address(&sav->sah->saidx.dst, buf,
  477. sizeof(buf)), (u_long) ntohl(sav->spi)));
  478. IPSEC_ISTAT(sproto, hdrops);
  479. error = EACCES;
  480. goto bad;
  481. }
  482. IPSEC_INIT_CTX(&ctx, &m, NULL, sav, af, IPSEC_ENC_BEFORE);
  483. if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
  484. goto bad;
  485. ip6 = mtod(m, struct ip6_hdr *);
  486. ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
  487. /* Save protocol */
  488. m_copydata(m, protoff, 1, &nxt8);
  489. prot = nxt8;
  490. /* IPv6-in-IP encapsulation */
  491. if (prot == IPPROTO_IPV6 &&
  492. saidx->mode != IPSEC_MODE_TRANSPORT) {
  493. if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
  494. IPSEC_ISTAT(sproto, hdrops);
  495. error = EINVAL;
  496. goto bad;
  497. }
  498. /* ip6n will now contain the inner IPv6 header. */
  499. m_striphdr(m, 0, skip);
  500. skip = 0;
  501. }
  502. #ifdef INET
  503. /* IP-in-IP encapsulation */
  504. else if (prot == IPPROTO_IPIP &&
  505. saidx->mode != IPSEC_MODE_TRANSPORT) {
  506. if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
  507. IPSEC_ISTAT(sproto, hdrops);
  508. error = EINVAL;
  509. goto bad;
  510. }
  511. /* ipn will now contain the inner IPv4 header */
  512. m_striphdr(m, 0, skip);
  513. skip = 0;
  514. }
  515. #endif /* INET */
  516. else {
  517. prot = IPPROTO_IPV6; /* for correct BPF processing */
  518. }
  519. /*
  520. * Record what we've done to the packet (under what SA it was
  521. * processed).
  522. */
  523. if (sproto != IPPROTO_IPCOMP) {
  524. mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
  525. sizeof(struct xform_history), M_NOWAIT);
  526. if (mtag == NULL) {
  527. DPRINTF(("%s: failed to get tag\n", __func__));
  528. IPSEC_ISTAT(sproto, hdrops);
  529. error = ENOMEM;
  530. goto bad;
  531. }
  532. xh = (struct xform_history *)(mtag + 1);
  533. bcopy(&saidx->dst, &xh->dst, saidx->dst.sa.sa_len);
  534. xh->spi = sav->spi;
  535. xh->proto = sproto;
  536. xh->mode = saidx->mode;
  537. m_tag_prepend(m, mtag);
  538. }
  539. key_sa_recordxfer(sav, m);
  540. #ifdef INET
  541. if (prot == IPPROTO_IPIP)
  542. af = AF_INET;
  543. else
  544. #endif
  545. af = AF_INET6;
  546. IPSEC_INIT_CTX(&ctx, &m, NULL, sav, af, IPSEC_ENC_AFTER);
  547. if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
  548. goto bad;
  549. if (skip == 0) {
  550. /*
  551. * We stripped outer IPv6 header.
  552. * Now we should requeue decrypted packet via netisr.
  553. */
  554. switch (prot) {
  555. #ifdef INET
  556. case IPPROTO_IPIP:
  557. isr_prot = NETISR_IP;
  558. break;
  559. #endif
  560. case IPPROTO_IPV6:
  561. isr_prot = NETISR_IPV6;
  562. break;
  563. default:
  564. DPRINTF(("%s: cannot handle inner ip proto %d\n",
  565. __func__, prot));
  566. IPSEC_ISTAT(sproto, nopf);
  567. error = EPFNOSUPPORT;
  568. goto bad;
  569. }
  570. /* Handle virtual tunneling interfaces */
  571. if (saidx->mode == IPSEC_MODE_TUNNEL)
  572. error = ipsec_if_input(m, sav, af);
  573. if (error == 0) {
  574. NET_EPOCH_ENTER(et);
  575. error = netisr_queue_src(isr_prot,
  576. (uintptr_t)sav->spi, m);
  577. NET_EPOCH_EXIT(et);
  578. if (error) {
  579. IPSEC_ISTAT(sproto, qfull);
  580. DPRINTF(("%s: queue full; proto %u packet"
  581. " dropped\n", __func__, sproto));
  582. }
  583. }
  584. key_freesav(&sav);
  585. return (error);
  586. }
  587. /*
  588. * See the end of ip6_input for this logic.
  589. * IPPROTO_IPV[46] case will be processed just like other ones
  590. */
  591. nest = 0;
  592. nxt = nxt8;
  593. NET_EPOCH_ENTER(et);
  594. while (nxt != IPPROTO_DONE) {
  595. if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
  596. IP6STAT_INC(ip6s_toomanyhdr);
  597. error = EINVAL;
  598. goto bad_epoch;
  599. }
  600. /*
  601. * Protection against faulty packet - there should be
  602. * more sanity checks in header chain processing.
  603. */
  604. if (m->m_pkthdr.len < skip) {
  605. IP6STAT_INC(ip6s_tooshort);
  606. in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
  607. error = EINVAL;
  608. goto bad_epoch;
  609. }
  610. /*
  611. * Enforce IPsec policy checking if we are seeing last header.
  612. * note that we do not visit this with protocols with pcb layer
  613. * code - like udp/tcp/raw ip.
  614. */
  615. if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
  616. ipsec6_in_reject(m, NULL)) {
  617. error = EINVAL;
  618. goto bad_epoch;
  619. }
  620. nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
  621. }
  622. NET_EPOCH_EXIT(et);
  623. key_freesav(&sav);
  624. return (0);
  625. bad_epoch:
  626. NET_EPOCH_EXIT(et);
  627. bad:
  628. key_freesav(&sav);
  629. if (m)
  630. m_freem(m);
  631. return (error);
  632. }
  633. #endif /* INET6 */