ipsec_input.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. #include "opt_inet.h"
  43. #include "opt_inet6.h"
  44. #include "opt_ipsec.h"
  45. #include <sys/param.h>
  46. #include <sys/systm.h>
  47. #include <sys/malloc.h>
  48. #include <sys/mbuf.h>
  49. #include <sys/domain.h>
  50. #include <sys/protosw.h>
  51. #include <sys/socket.h>
  52. #include <sys/errno.h>
  53. #include <sys/hhook.h>
  54. #include <sys/syslog.h>
  55. #include <net/if.h>
  56. #include <net/if_var.h>
  57. #include <net/if_enc.h>
  58. #include <net/if_private.h>
  59. #include <net/netisr.h>
  60. #include <net/vnet.h>
  61. #include <netinet/in.h>
  62. #include <netinet/in_pcb.h>
  63. #include <netinet/in_systm.h>
  64. #include <netinet/ip.h>
  65. #include <netinet/ip_var.h>
  66. #include <netinet/ip_icmp.h>
  67. #include <netinet/in_var.h>
  68. #include <netinet/tcp_var.h>
  69. #include <netinet/ip6.h>
  70. #ifdef INET6
  71. #include <netinet6/ip6_var.h>
  72. #endif
  73. #include <netinet/in_pcb.h>
  74. #ifdef INET6
  75. #include <netinet/icmp6.h>
  76. #endif
  77. #include <netipsec/ipsec.h>
  78. #ifdef INET6
  79. #include <netipsec/ipsec6.h>
  80. #endif
  81. #include <netipsec/ipsec_support.h>
  82. #include <netipsec/ah_var.h>
  83. #include <netipsec/esp.h>
  84. #include <netipsec/esp_var.h>
  85. #include <netipsec/ipcomp_var.h>
  86. #include <netipsec/ipsec_offload.h>
  87. #include <netipsec/key.h>
  88. #include <netipsec/keydb.h>
  89. #include <netipsec/key_debug.h>
  90. #include <netipsec/xform.h>
  91. #include <machine/in_cksum.h>
  92. #include <machine/stdarg.h>
  93. #define IPSEC_ISTAT(proto, name) do { \
  94. if ((proto) == IPPROTO_ESP) \
  95. ESPSTAT_INC(esps_##name); \
  96. else if ((proto) == IPPROTO_AH) \
  97. AHSTAT_INC(ahs_##name); \
  98. else \
  99. IPCOMPSTAT_INC(ipcomps_##name); \
  100. } while (0)
  101. /*
  102. * ipsec_common_input gets called when an IPsec-protected packet
  103. * is received by IPv4 or IPv6. Its job is to find the right SA
  104. * and call the appropriate transform. The transform callback
  105. * takes care of further processing (like ingress filtering).
  106. */
  107. static int
  108. ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
  109. {
  110. IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
  111. union sockaddr_union dst_address;
  112. struct secasvar *sav;
  113. uint32_t spi;
  114. int error;
  115. IPSEC_ISTAT(sproto, input);
  116. IPSEC_ASSERT(m != NULL, ("null packet"));
  117. IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
  118. sproto == IPPROTO_IPCOMP,
  119. ("unexpected security protocol %u", sproto));
  120. if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
  121. (sproto == IPPROTO_AH && !V_ah_enable) ||
  122. (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
  123. m_freem(m);
  124. IPSEC_ISTAT(sproto, pdrops);
  125. return EOPNOTSUPP;
  126. }
  127. if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
  128. m_freem(m);
  129. IPSEC_ISTAT(sproto, hdrops);
  130. DPRINTF(("%s: packet too small\n", __func__));
  131. return EINVAL;
  132. }
  133. /* Retrieve the SPI from the relevant IPsec header */
  134. if (sproto == IPPROTO_ESP)
  135. m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
  136. else if (sproto == IPPROTO_AH)
  137. m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
  138. (caddr_t) &spi);
  139. else if (sproto == IPPROTO_IPCOMP) {
  140. u_int16_t cpi;
  141. m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
  142. (caddr_t) &cpi);
  143. spi = ntohl(htons(cpi));
  144. }
  145. /*
  146. * Find the SA and (indirectly) call the appropriate
  147. * kernel crypto routine. The resulting mbuf chain is a valid
  148. * IP packet ready to go through input processing.
  149. */
  150. bzero(&dst_address, sizeof (dst_address));
  151. dst_address.sa.sa_family = af;
  152. switch (af) {
  153. #ifdef INET
  154. case AF_INET:
  155. dst_address.sin.sin_len = sizeof(struct sockaddr_in);
  156. m_copydata(m, offsetof(struct ip, ip_dst),
  157. sizeof(struct in_addr),
  158. (caddr_t) &dst_address.sin.sin_addr);
  159. break;
  160. #endif /* INET */
  161. #ifdef INET6
  162. case AF_INET6:
  163. dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
  164. m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
  165. sizeof(struct in6_addr),
  166. (caddr_t) &dst_address.sin6.sin6_addr);
  167. /* We keep addresses in SADB without embedded scope id */
  168. if (IN6_IS_SCOPE_LINKLOCAL(&dst_address.sin6.sin6_addr)) {
  169. /* XXX: sa6_recoverscope() */
  170. dst_address.sin6.sin6_scope_id =
  171. ntohs(dst_address.sin6.sin6_addr.s6_addr16[1]);
  172. dst_address.sin6.sin6_addr.s6_addr16[1] = 0;
  173. }
  174. break;
  175. #endif /* INET6 */
  176. default:
  177. DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
  178. m_freem(m);
  179. IPSEC_ISTAT(sproto, nopf);
  180. return EPFNOSUPPORT;
  181. }
  182. /* NB: only pass dst since key_allocsa follows RFC2401 */
  183. sav = key_allocsa(&dst_address, sproto, spi);
  184. if (sav == NULL) {
  185. DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
  186. __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
  187. (u_long) ntohl(spi), sproto));
  188. IPSEC_ISTAT(sproto, notdb);
  189. m_freem(m);
  190. return ENOENT;
  191. }
  192. if (sav->tdb_xform == NULL) {
  193. DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
  194. __func__, ipsec_address(&dst_address, buf, sizeof(buf)),
  195. (u_long) ntohl(spi), sproto));
  196. IPSEC_ISTAT(sproto, noxform);
  197. key_freesav(&sav);
  198. m_freem(m);
  199. return ENXIO;
  200. }
  201. /*
  202. * Call appropriate transform and return -- callback takes care of
  203. * everything else.
  204. */
  205. error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
  206. return (error);
  207. }
  208. #ifdef INET
  209. /*
  210. * IPSEC_INPUT() method implementation for IPv4.
  211. * 0 - Permitted by inbound security policy for further processing.
  212. * EACCES - Forbidden by inbound security policy.
  213. * EINPROGRESS - consumed by IPsec.
  214. */
  215. int
  216. ipsec4_input(struct mbuf *m, int offset, int proto)
  217. {
  218. int error;
  219. error = ipsec_accel_input(m, offset, proto);
  220. if (error != ENXIO)
  221. return (error);
  222. switch (proto) {
  223. case IPPROTO_AH:
  224. case IPPROTO_ESP:
  225. case IPPROTO_IPCOMP:
  226. /* Do inbound IPsec processing for AH/ESP/IPCOMP */
  227. ipsec_common_input(m, offset,
  228. offsetof(struct ip, ip_p), AF_INET, proto);
  229. return (EINPROGRESS); /* mbuf consumed by IPsec */
  230. default:
  231. /*
  232. * Protocols with further headers get their IPsec treatment
  233. * within the protocol specific processing.
  234. */
  235. switch (proto) {
  236. case IPPROTO_ICMP:
  237. case IPPROTO_IGMP:
  238. case IPPROTO_IPV4:
  239. case IPPROTO_IPV6:
  240. case IPPROTO_RSVP:
  241. case IPPROTO_GRE:
  242. case IPPROTO_MOBILE:
  243. case IPPROTO_ETHERIP:
  244. case IPPROTO_PIM:
  245. case IPPROTO_SCTP:
  246. break;
  247. default:
  248. return (0);
  249. }
  250. };
  251. /*
  252. * Enforce IPsec policy checking if we are seeing last header.
  253. */
  254. if (ipsec4_in_reject(m, NULL) != 0) {
  255. /* Forbidden by inbound security policy */
  256. m_freem(m);
  257. return (EACCES);
  258. }
  259. return (0);
  260. }
  261. int
  262. ipsec4_ctlinput(ipsec_ctlinput_param_t param)
  263. {
  264. struct icmp *icp = param.icmp;
  265. struct ip *ip = &icp->icmp_ip;
  266. struct sockaddr_in icmpsrc = {
  267. .sin_len = sizeof(struct sockaddr_in),
  268. .sin_family = AF_INET,
  269. .sin_addr = ip->ip_dst,
  270. };
  271. struct in_conninfo inc;
  272. struct secasvar *sav;
  273. uint32_t pmtu, spi;
  274. uint32_t max_pmtu;
  275. uint8_t proto;
  276. pmtu = ntohs(icp->icmp_nextmtu);
  277. if (pmtu < V_ip4_ipsec_min_pmtu)
  278. return (EINVAL);
  279. proto = ip->ip_p;
  280. if (proto != IPPROTO_ESP && proto != IPPROTO_AH &&
  281. proto != IPPROTO_IPCOMP)
  282. return (EINVAL);
  283. memcpy(&spi, (caddr_t)ip + (ip->ip_hl << 2), sizeof(spi));
  284. sav = key_allocsa((union sockaddr_union *)&icmpsrc, proto, spi);
  285. if (sav == NULL)
  286. return (ENOENT);
  287. key_freesav(&sav);
  288. memset(&inc, 0, sizeof(inc));
  289. inc.inc_faddr = ip->ip_dst;
  290. /* Update pmtu only if its smaller than the current one. */
  291. max_pmtu = tcp_hc_getmtu(&inc);
  292. if (max_pmtu == 0)
  293. max_pmtu = tcp_maxmtu(&inc, NULL);
  294. if (pmtu < max_pmtu)
  295. tcp_hc_updatemtu(&inc, pmtu);
  296. return (0);
  297. }
  298. /*
  299. * IPsec input callback for INET protocols.
  300. * This routine is called as the transform callback.
  301. * Takes care of filtering and other sanity checks on
  302. * the processed packet.
  303. */
  304. int
  305. ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
  306. int protoff)
  307. {
  308. IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
  309. struct epoch_tracker et;
  310. struct ipsec_ctx_data ctx;
  311. struct xform_history *xh;
  312. struct secasindex *saidx;
  313. struct m_tag *mtag;
  314. struct ip *ip;
  315. int error, prot, af, sproto, isr_prot;
  316. IPSEC_ASSERT(sav != NULL, ("null SA"));
  317. IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
  318. saidx = &sav->sah->saidx;
  319. af = saidx->dst.sa.sa_family;
  320. IPSEC_ASSERT(af == AF_INET, ("unexpected af %u", af));
  321. sproto = saidx->proto;
  322. IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
  323. sproto == IPPROTO_IPCOMP,
  324. ("unexpected security protocol %u", sproto));
  325. if (skip != 0) {
  326. /*
  327. * Fix IPv4 header
  328. */
  329. if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
  330. DPRINTF(("%s: processing failed for SA %s/%08lx\n",
  331. __func__, ipsec_address(&sav->sah->saidx.dst,
  332. buf, sizeof(buf)), (u_long) ntohl(sav->spi)));
  333. IPSEC_ISTAT(sproto, hdrops);
  334. error = ENOBUFS;
  335. goto bad_noepoch;
  336. }
  337. ip = mtod(m, struct ip *);
  338. ip->ip_len = htons(m->m_pkthdr.len);
  339. ip->ip_sum = 0;
  340. ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
  341. } else {
  342. ip = mtod(m, struct ip *);
  343. }
  344. prot = ip->ip_p;
  345. /*
  346. * Check that we have NAT-T enabled and apply transport mode
  347. * decapsulation NAT procedure (RFC3948).
  348. * Do this before invoking into the PFIL.
  349. */
  350. if (sav->natt != NULL &&
  351. (prot == IPPROTO_UDP || prot == IPPROTO_TCP))
  352. udp_ipsec_adjust_cksum(m, sav, prot, skip);
  353. /*
  354. * Needed for ipsec_run_hooks and netisr_queue_src
  355. */
  356. NET_EPOCH_ENTER(et);
  357. IPSEC_INIT_CTX(&ctx, &m, NULL, sav, AF_INET, IPSEC_ENC_BEFORE);
  358. if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
  359. goto bad;
  360. ip = mtod(m, struct ip *); /* update pointer */
  361. /* IP-in-IP encapsulation */
  362. if (prot == IPPROTO_IPIP &&
  363. saidx->mode != IPSEC_MODE_TRANSPORT) {
  364. if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
  365. IPSEC_ISTAT(sproto, hdrops);
  366. error = EINVAL;
  367. goto bad;
  368. }
  369. /* enc0: strip outer IPv4 header */
  370. m_striphdr(m, 0, ip->ip_hl << 2);
  371. }
  372. #ifdef INET6
  373. /* IPv6-in-IP encapsulation. */
  374. else if (prot == IPPROTO_IPV6 &&
  375. saidx->mode != IPSEC_MODE_TRANSPORT) {
  376. if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
  377. IPSEC_ISTAT(sproto, hdrops);
  378. error = EINVAL;
  379. goto bad;
  380. }
  381. /* enc0: strip IPv4 header, keep IPv6 header only */
  382. m_striphdr(m, 0, ip->ip_hl << 2);
  383. }
  384. #endif /* INET6 */
  385. else if (prot != IPPROTO_IPV6 && saidx->mode == IPSEC_MODE_ANY) {
  386. /*
  387. * When mode is wildcard, inner protocol is IPv6 and
  388. * we have no INET6 support - drop this packet a bit later.
  389. * In other cases we assume transport mode. Set prot to
  390. * correctly choose netisr.
  391. */
  392. prot = IPPROTO_IPIP;
  393. }
  394. /*
  395. * Record what we've done to the packet (under what SA it was
  396. * processed).
  397. */
  398. if (sproto != IPPROTO_IPCOMP) {
  399. mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
  400. sizeof(struct xform_history), M_NOWAIT);
  401. if (mtag == NULL) {
  402. DPRINTF(("%s: failed to get tag\n", __func__));
  403. IPSEC_ISTAT(sproto, hdrops);
  404. error = ENOMEM;
  405. goto bad;
  406. }
  407. xh = (struct xform_history *)(mtag + 1);
  408. bcopy(&saidx->dst, &xh->dst, saidx->dst.sa.sa_len);
  409. xh->spi = sav->spi;
  410. xh->proto = sproto;
  411. xh->mode = saidx->mode;
  412. m_tag_prepend(m, mtag);
  413. }
  414. key_sa_recordxfer(sav, m); /* record data transfer */
  415. /*
  416. * In transport mode requeue decrypted mbuf back to IPv4 protocol
  417. * handler. This is necessary to correctly expose rcvif.
  418. */
  419. if (saidx->mode == IPSEC_MODE_TRANSPORT)
  420. prot = IPPROTO_IPIP;
  421. /*
  422. * Re-dispatch via software interrupt.
  423. */
  424. switch (prot) {
  425. case IPPROTO_IPIP:
  426. isr_prot = NETISR_IP;
  427. af = AF_INET;
  428. break;
  429. #ifdef INET6
  430. case IPPROTO_IPV6:
  431. isr_prot = NETISR_IPV6;
  432. af = AF_INET6;
  433. break;
  434. #endif
  435. default:
  436. DPRINTF(("%s: cannot handle inner ip proto %d\n",
  437. __func__, prot));
  438. IPSEC_ISTAT(sproto, nopf);
  439. error = EPFNOSUPPORT;
  440. goto bad;
  441. }
  442. IPSEC_INIT_CTX(&ctx, &m, NULL, sav, af, IPSEC_ENC_AFTER);
  443. if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
  444. goto bad;
  445. /* Handle virtual tunneling interfaces */
  446. if (saidx->mode == IPSEC_MODE_TUNNEL)
  447. error = ipsec_if_input(m, sav, af);
  448. if (error == 0) {
  449. error = netisr_queue_src(isr_prot, (uintptr_t)sav->spi, m);
  450. if (error) {
  451. IPSEC_ISTAT(sproto, qfull);
  452. DPRINTF(("%s: queue full; proto %u packet dropped\n",
  453. __func__, sproto));
  454. }
  455. }
  456. NET_EPOCH_EXIT(et);
  457. key_freesav(&sav);
  458. return (error);
  459. bad:
  460. NET_EPOCH_EXIT(et);
  461. bad_noepoch:
  462. key_freesav(&sav);
  463. if (m != NULL)
  464. m_freem(m);
  465. return (error);
  466. }
  467. #endif /* INET */
  468. #ifdef INET6
  469. static bool
  470. ipsec6_lasthdr(int proto)
  471. {
  472. switch (proto) {
  473. case IPPROTO_IPV4:
  474. case IPPROTO_IPV6:
  475. case IPPROTO_GRE:
  476. case IPPROTO_ICMPV6:
  477. case IPPROTO_ETHERIP:
  478. case IPPROTO_PIM:
  479. case IPPROTO_SCTP:
  480. return (true);
  481. default:
  482. return (false);
  483. };
  484. }
  485. /*
  486. * IPSEC_INPUT() method implementation for IPv6.
  487. * 0 - Permitted by inbound security policy for further processing.
  488. * EACCES - Forbidden by inbound security policy.
  489. * EINPROGRESS - consumed by IPsec.
  490. */
  491. int
  492. ipsec6_input(struct mbuf *m, int offset, int proto)
  493. {
  494. int error;
  495. error = ipsec_accel_input(m, offset, proto);
  496. if (error != ENXIO)
  497. return (error);
  498. switch (proto) {
  499. case IPPROTO_AH:
  500. case IPPROTO_ESP:
  501. case IPPROTO_IPCOMP:
  502. /* Do inbound IPsec processing for AH/ESP/IPCOMP */
  503. ipsec_common_input(m, offset,
  504. offsetof(struct ip6_hdr, ip6_nxt), AF_INET6, proto);
  505. return (EINPROGRESS); /* mbuf consumed by IPsec */
  506. default:
  507. /*
  508. * Protocols with further headers get their IPsec treatment
  509. * within the protocol specific processing.
  510. */
  511. if (!ipsec6_lasthdr(proto))
  512. return (0);
  513. /* FALLTHROUGH */
  514. };
  515. /*
  516. * Enforce IPsec policy checking if we are seeing last header.
  517. */
  518. if (ipsec6_in_reject(m, NULL) != 0) {
  519. /* Forbidden by inbound security policy */
  520. m_freem(m);
  521. return (EACCES);
  522. }
  523. return (0);
  524. }
  525. int
  526. ipsec6_ctlinput(ipsec_ctlinput_param_t param)
  527. {
  528. return (0);
  529. }
  530. extern ipproto_input_t *ip6_protox[];
  531. /*
  532. * IPsec input callback, called by the transform callback. Takes care of
  533. * filtering and other sanity checks on the processed packet.
  534. */
  535. int
  536. ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
  537. int protoff)
  538. {
  539. IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
  540. struct epoch_tracker et;
  541. struct ipsec_ctx_data ctx;
  542. struct xform_history *xh;
  543. struct secasindex *saidx;
  544. struct ip6_hdr *ip6;
  545. struct m_tag *mtag;
  546. int prot, af, sproto;
  547. int nxt, isr_prot;
  548. int error, nest;
  549. uint8_t nxt8;
  550. IPSEC_ASSERT(sav != NULL, ("null SA"));
  551. IPSEC_ASSERT(sav->sah != NULL, ("null SAH"));
  552. saidx = &sav->sah->saidx;
  553. af = saidx->dst.sa.sa_family;
  554. IPSEC_ASSERT(af == AF_INET6, ("unexpected af %u", af));
  555. sproto = saidx->proto;
  556. IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
  557. sproto == IPPROTO_IPCOMP,
  558. ("unexpected security protocol %u", sproto));
  559. NET_EPOCH_ENTER(et);
  560. /* Fix IPv6 header */
  561. if (m->m_len < sizeof(struct ip6_hdr) &&
  562. (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
  563. DPRINTF(("%s: processing failed for SA %s/%08lx\n",
  564. __func__, ipsec_address(&sav->sah->saidx.dst, buf,
  565. sizeof(buf)), (u_long) ntohl(sav->spi)));
  566. IPSEC_ISTAT(sproto, hdrops);
  567. error = EACCES;
  568. goto bad;
  569. }
  570. IPSEC_INIT_CTX(&ctx, &m, NULL, sav, af, IPSEC_ENC_BEFORE);
  571. if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
  572. goto bad;
  573. ip6 = mtod(m, struct ip6_hdr *);
  574. ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
  575. /* Save protocol */
  576. m_copydata(m, protoff, 1, &nxt8);
  577. prot = nxt8;
  578. /*
  579. * Check that we have NAT-T enabled and apply transport mode
  580. * decapsulation NAT procedure (RFC3948).
  581. * Do this before invoking into the PFIL.
  582. */
  583. if (sav->natt != NULL &&
  584. (prot == IPPROTO_UDP || prot == IPPROTO_TCP))
  585. udp_ipsec_adjust_cksum(m, sav, prot, skip);
  586. /* IPv6-in-IP encapsulation */
  587. if (prot == IPPROTO_IPV6 &&
  588. saidx->mode != IPSEC_MODE_TRANSPORT) {
  589. if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
  590. IPSEC_ISTAT(sproto, hdrops);
  591. error = EINVAL;
  592. goto bad;
  593. }
  594. /* ip6n will now contain the inner IPv6 header. */
  595. m_striphdr(m, 0, skip);
  596. skip = 0;
  597. }
  598. #ifdef INET
  599. /* IP-in-IP encapsulation */
  600. else if (prot == IPPROTO_IPIP &&
  601. saidx->mode != IPSEC_MODE_TRANSPORT) {
  602. if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
  603. IPSEC_ISTAT(sproto, hdrops);
  604. error = EINVAL;
  605. goto bad;
  606. }
  607. /* ipn will now contain the inner IPv4 header */
  608. m_striphdr(m, 0, skip);
  609. skip = 0;
  610. }
  611. #endif /* INET */
  612. else {
  613. prot = IPPROTO_IPV6; /* for correct BPF processing */
  614. }
  615. /*
  616. * Record what we've done to the packet (under what SA it was
  617. * processed).
  618. */
  619. if (sproto != IPPROTO_IPCOMP) {
  620. mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
  621. sizeof(struct xform_history), M_NOWAIT);
  622. if (mtag == NULL) {
  623. DPRINTF(("%s: failed to get tag\n", __func__));
  624. IPSEC_ISTAT(sproto, hdrops);
  625. error = ENOMEM;
  626. goto bad;
  627. }
  628. xh = (struct xform_history *)(mtag + 1);
  629. bcopy(&saidx->dst, &xh->dst, saidx->dst.sa.sa_len);
  630. xh->spi = sav->spi;
  631. xh->proto = sproto;
  632. xh->mode = saidx->mode;
  633. m_tag_prepend(m, mtag);
  634. }
  635. key_sa_recordxfer(sav, m);
  636. #ifdef INET
  637. if (prot == IPPROTO_IPIP)
  638. af = AF_INET;
  639. else
  640. #endif
  641. af = AF_INET6;
  642. IPSEC_INIT_CTX(&ctx, &m, NULL, sav, af, IPSEC_ENC_AFTER);
  643. if ((error = ipsec_run_hhooks(&ctx, HHOOK_TYPE_IPSEC_IN)) != 0)
  644. goto bad;
  645. if (skip == 0) {
  646. /*
  647. * We stripped outer IPv6 header.
  648. * Now we should requeue decrypted packet via netisr.
  649. */
  650. switch (prot) {
  651. #ifdef INET
  652. case IPPROTO_IPIP:
  653. isr_prot = NETISR_IP;
  654. break;
  655. #endif
  656. case IPPROTO_IPV6:
  657. isr_prot = NETISR_IPV6;
  658. break;
  659. default:
  660. DPRINTF(("%s: cannot handle inner ip proto %d\n",
  661. __func__, prot));
  662. IPSEC_ISTAT(sproto, nopf);
  663. error = EPFNOSUPPORT;
  664. goto bad;
  665. }
  666. /* Handle virtual tunneling interfaces */
  667. if (saidx->mode == IPSEC_MODE_TUNNEL)
  668. error = ipsec_if_input(m, sav, af);
  669. if (error == 0) {
  670. error = netisr_queue_src(isr_prot,
  671. (uintptr_t)sav->spi, m);
  672. if (error) {
  673. IPSEC_ISTAT(sproto, qfull);
  674. DPRINTF(("%s: queue full; proto %u packet"
  675. " dropped\n", __func__, sproto));
  676. }
  677. }
  678. NET_EPOCH_EXIT(et);
  679. key_freesav(&sav);
  680. return (error);
  681. }
  682. /*
  683. * See the end of ip6_input for this logic.
  684. * IPPROTO_IPV[46] case will be processed just like other ones
  685. */
  686. nest = 0;
  687. nxt = nxt8;
  688. while (nxt != IPPROTO_DONE) {
  689. if (V_ip6_hdrnestlimit && (++nest > V_ip6_hdrnestlimit)) {
  690. IP6STAT_INC(ip6s_toomanyhdr);
  691. error = EINVAL;
  692. goto bad;
  693. }
  694. /*
  695. * Protection against faulty packet - there should be
  696. * more sanity checks in header chain processing.
  697. */
  698. if (m->m_pkthdr.len < skip) {
  699. IP6STAT_INC(ip6s_tooshort);
  700. in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
  701. error = EINVAL;
  702. goto bad;
  703. }
  704. /*
  705. * Enforce IPsec policy checking if we are seeing last header.
  706. * note that we do not visit this with protocols with pcb layer
  707. * code - like udp/tcp/raw ip.
  708. */
  709. if (ipsec6_lasthdr(nxt) && ipsec6_in_reject(m, NULL)) {
  710. error = EINVAL;
  711. goto bad;
  712. }
  713. nxt = ip6_protox[nxt](&m, &skip, nxt);
  714. }
  715. NET_EPOCH_EXIT(et);
  716. key_freesav(&sav);
  717. return (0);
  718. bad:
  719. NET_EPOCH_EXIT(et);
  720. key_freesav(&sav);
  721. if (m)
  722. m_freem(m);
  723. return (error);
  724. }
  725. #endif /* INET6 */