Deleted Added
sdiff udiff text old ( 249294 ) new ( 252028 )
full compact
1/* $FreeBSD: head/sys/netipsec/ipsec_input.c 252028 2013-06-20 11:44:16Z ae $ */
2/* $OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $ */
3/*-
4 * The authors of this code are John Ioannidis (ji@tla.org),
5 * Angelos D. Keromytis (kermit@csd.uch.gr) and
6 * Niels Provos (provos@physnet.uni-hamburg.de).
7 *
8 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
9 * in November 1995.

--- 84 unchanged lines hidden (view full) ---

94#include <machine/in_cksum.h>
95#include <machine/stdarg.h>
96
97#ifdef DEV_ENC
98#include <net/if_enc.h>
99#endif
100
101
102#define IPSEC_ISTAT(proto, name) do { \
103 if ((proto) == IPPROTO_ESP) \
104 ESPSTAT_INC(esps_##name); \
105 else if ((proto) == IPPROTO_AH) \
106 AHSTAT_INC(ahs_##name); \
107 else \
108 IPCOMPSTAT_INC(ipcomps_##name); \
109} while (0)
110
111#ifdef INET
112static void ipsec4_common_ctlinput(int, struct sockaddr *, void *, int);
113#endif
114
115/*
116 * ipsec_common_input gets called when an IPsec-protected packet
117 * is received by IPv4 or IPv6. Its job is to find the right SA

--- 8 unchanged lines hidden (view full) ---

126 u_int32_t spi;
127 int error;
128#ifdef INET
129#ifdef IPSEC_NAT_T
130 struct m_tag *tag;
131#endif
132#endif
133
134 IPSEC_ISTAT(sproto, input);
135
136 IPSEC_ASSERT(m != NULL, ("null packet"));
137
138 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
139 sproto == IPPROTO_IPCOMP,
140 ("unexpected security protocol %u", sproto));
141
142 if ((sproto == IPPROTO_ESP && !V_esp_enable) ||
143 (sproto == IPPROTO_AH && !V_ah_enable) ||
144 (sproto == IPPROTO_IPCOMP && !V_ipcomp_enable)) {
145 m_freem(m);
146 IPSEC_ISTAT(sproto, pdrops);
147 return EOPNOTSUPP;
148 }
149
150 if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
151 m_freem(m);
152 IPSEC_ISTAT(sproto, hdrops);
153 DPRINTF(("%s: packet too small\n", __func__));
154 return EINVAL;
155 }
156
157 /* Retrieve the SPI from the relevant IPsec header */
158 if (sproto == IPPROTO_ESP)
159 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
160 else if (sproto == IPPROTO_AH)

--- 34 unchanged lines hidden (view full) ---

195 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
196 sizeof(struct in6_addr),
197 (caddr_t) &dst_address.sin6.sin6_addr);
198 break;
199#endif /* INET6 */
200 default:
201 DPRINTF(("%s: unsupported protocol family %u\n", __func__, af));
202 m_freem(m);
203 IPSEC_ISTAT(sproto, nopf);
204 return EPFNOSUPPORT;
205 }
206
207 /* NB: only pass dst since key_allocsa follows RFC2401 */
208 sav = KEY_ALLOCSA(&dst_address, sproto, spi);
209 if (sav == NULL) {
210 DPRINTF(("%s: no key association found for SA %s/%08lx/%u\n",
211 __func__, ipsec_address(&dst_address),
212 (u_long) ntohl(spi), sproto));
213 IPSEC_ISTAT(sproto, notdb);
214 m_freem(m);
215 return ENOENT;
216 }
217
218 if (sav->tdb_xform == NULL) {
219 DPRINTF(("%s: attempted to use uninitialized SA %s/%08lx/%u\n",
220 __func__, ipsec_address(&dst_address),
221 (u_long) ntohl(spi), sproto));
222 IPSEC_ISTAT(sproto, noxform);
223 KEY_FREESAV(&sav);
224 m_freem(m);
225 return ENXIO;
226 }
227
228 /*
229 * Call appropriate transform and return -- callback takes care of
230 * everything else.

--- 85 unchanged lines hidden (view full) ---

316 sproto = saidx->proto;
317 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
318 sproto == IPPROTO_IPCOMP,
319 ("unexpected security protocol %u", sproto));
320
321 /* Sanity check */
322 if (m == NULL) {
323 DPRINTF(("%s: null mbuf", __func__));
324 IPSEC_ISTAT(sproto, badkcr);
325 KEY_FREESAV(&sav);
326 return EINVAL;
327 }
328
329 if (skip != 0) {
330 /*
331 * Fix IPv4 header
332 * XXXGL: do we need this entire block?
333 */
334 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
335 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
336 __func__, ipsec_address(&sav->sah->saidx.dst),
337 (u_long) ntohl(sav->spi)));
338 IPSEC_ISTAT(sproto, hdrops);
339 error = ENOBUFS;
340 goto bad;
341 }
342
343 ip = mtod(m, struct ip *);
344 ip->ip_len = htons(m->m_pkthdr.len);
345 ip->ip_sum = 0;
346 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
347 } else {
348 ip = mtod(m, struct ip *);
349 }
350 prot = ip->ip_p;
351
352#ifdef notyet
353 /* IP-in-IP encapsulation */
354 if (prot == IPPROTO_IPIP) {
355 struct ip ipn;
356
357 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
358 IPSEC_ISTAT(sproto, hdrops);
359 error = EINVAL;
360 goto bad;
361 }
362 /* ipn will now contain the inner IPv4 header */
363 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip),
364 (caddr_t) &ipn);
365
366 /* XXX PROXY address isn't recorded in SAH */

--- 12 unchanged lines hidden (view full) ---

379 DPRINTF(("%s: inner source address %s doesn't "
380 "correspond to expected proxy source %s, "
381 "SA %s/%08lx\n", __func__,
382 inet_ntoa4(ipn.ip_src),
383 ipsp_address(saidx->proxy),
384 ipsp_address(saidx->dst),
385 (u_long) ntohl(sav->spi)));
386
387 IPSEC_ISTAT(sproto, pdrops);
388 error = EACCES;
389 goto bad;
390 }
391 }
392#ifdef INET6
393 /* IPv6-in-IP encapsulation. */
394 if (prot == IPPROTO_IPV6) {
395 struct ip6_hdr ip6n;
396
397 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
398 IPSEC_ISTAT(sproto, hdrops);
399 error = EINVAL;
400 goto bad;
401 }
402 /* ip6n will now contain the inner IPv6 header. */
403 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr),
404 (caddr_t) &ip6n);
405
406 /*

--- 10 unchanged lines hidden (view full) ---

417 DPRINTF(("%s: inner source address %s doesn't "
418 "correspond to expected proxy source %s, "
419 "SA %s/%08lx\n", __func__,
420 ip6_sprintf(ip6buf, &ip6n.ip6_src),
421 ipsec_address(&saidx->proxy),
422 ipsec_address(&saidx->dst),
423 (u_long) ntohl(sav->spi)));
424
425 IPSEC_ISTAT(sproto, pdrops);
426 error = EACCES;
427 goto bad;
428 }
429 }
430#endif /* INET6 */
431#endif /*XXX*/
432
433 /*

--- 4 unchanged lines hidden (view full) ---

438 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
439 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
440 */
441 if (mt == NULL && sproto != IPPROTO_IPCOMP) {
442 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
443 sizeof(struct tdb_ident), M_NOWAIT);
444 if (mtag == NULL) {
445 DPRINTF(("%s: failed to get tag\n", __func__));
446 IPSEC_ISTAT(sproto, hdrops);
447 error = ENOMEM;
448 goto bad;
449 }
450
451 tdbi = (struct tdb_ident *)(mtag + 1);
452 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
453 tdbi->proto = sproto;
454 tdbi->spi = sav->spi;

--- 23 unchanged lines hidden (view full) ---

478 if ((error = ipsec_filter(&m, PFIL_IN, ENC_IN|ENC_BEFORE)) != 0)
479 return (error);
480#endif
481
482 /*
483 * Re-dispatch via software interrupt.
484 */
485 if ((error = netisr_queue_src(NETISR_IP, (uintptr_t)sav->spi, m))) {
486 IPSEC_ISTAT(sproto, qfull);
487 DPRINTF(("%s: queue full; proto %u packet dropped\n",
488 __func__, sproto));
489 return error;
490 }
491 return 0;
492bad:
493 m_freem(m);
494 return error;

--- 35 unchanged lines hidden (view full) ---

530 l = (ip6e.ip6e_len + 1) << 3;
531 IPSEC_ASSERT(l > 0, ("l went zero or negative"));
532 } while (protoff + l < *offp);
533
534 /* Malformed packet check */
535 if (protoff + l != *offp) {
536 DPRINTF(("%s: bad packet header chain, protoff %u, "
537 "l %u, off %u\n", __func__, protoff, l, *offp));
538 IPSEC_ISTAT(proto, hdrops);
539 m_freem(*mp);
540 *mp = NULL;
541 return IPPROTO_DONE;
542 }
543 protoff += offsetof(struct ip6_ext, ip6e_nxt);
544 }
545 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
546 return IPPROTO_DONE;

--- 28 unchanged lines hidden (view full) ---

575 sproto = saidx->proto;
576 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
577 sproto == IPPROTO_IPCOMP,
578 ("unexpected security protocol %u", sproto));
579
580 /* Sanity check */
581 if (m == NULL) {
582 DPRINTF(("%s: null mbuf", __func__));
583 IPSEC_ISTAT(sproto, badkcr);
584 error = EINVAL;
585 goto bad;
586 }
587
588 /* Fix IPv6 header */
589 if (m->m_len < sizeof(struct ip6_hdr) &&
590 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
591
592 DPRINTF(("%s: processing failed for SA %s/%08lx\n",
593 __func__, ipsec_address(&sav->sah->saidx.dst),
594 (u_long) ntohl(sav->spi)));
595
596 IPSEC_ISTAT(sproto, hdrops);
597 error = EACCES;
598 goto bad;
599 }
600
601 ip6 = mtod(m, struct ip6_hdr *);
602 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
603
604 /* Save protocol */
605 m_copydata(m, protoff, 1, (unsigned char *) &prot);
606
607#ifdef notyet
608#ifdef INET
609 /* IP-in-IP encapsulation */
610 if (prot == IPPROTO_IPIP) {
611 struct ip ipn;
612
613 if (m->m_pkthdr.len - skip < sizeof(struct ip)) {
614 IPSEC_ISTAT(sproto, hdrops);
615 error = EINVAL;
616 goto bad;
617 }
618 /* ipn will now contain the inner IPv4 header */
619 m_copydata(m, skip, sizeof(struct ip), (caddr_t) &ipn);
620
621 /*
622 * Check that the inner source address is the same as

--- 8 unchanged lines hidden (view full) ---

631 DPRINTF(("%s: inner source address %s doesn't "
632 "correspond to expected proxy source %s, "
633 "SA %s/%08lx\n", __func__,
634 inet_ntoa4(ipn.ip_src),
635 ipsec_address(&saidx->proxy),
636 ipsec_address(&saidx->dst),
637 (u_long) ntohl(sav->spi)));
638
639 IPSEC_ISTAT(sproto, pdrops);
640 error = EACCES;
641 goto bad;
642 }
643 }
644#endif /* INET */
645
646 /* IPv6-in-IP encapsulation */
647 if (prot == IPPROTO_IPV6) {
648 struct ip6_hdr ip6n;
649
650 if (m->m_pkthdr.len - skip < sizeof(struct ip6_hdr)) {
651 IPSEC_ISTAT(sproto, hdrops);
652 error = EINVAL;
653 goto bad;
654 }
655 /* ip6n will now contain the inner IPv6 header. */
656 m_copydata(m, skip, sizeof(struct ip6_hdr),
657 (caddr_t) &ip6n);
658
659 /*

--- 10 unchanged lines hidden (view full) ---

670 DPRINTF(("%s: inner source address %s doesn't "
671 "correspond to expected proxy source %s, "
672 "SA %s/%08lx\n", __func__,
673 ip6_sprintf(ip6buf, &ip6n.ip6_src),
674 ipsec_address(&saidx->proxy),
675 ipsec_address(&saidx->dst),
676 (u_long) ntohl(sav->spi)));
677
678 IPSEC_ISTAT(sproto, pdrops);
679 error = EACCES;
680 goto bad;
681 }
682 }
683#endif /*XXX*/
684
685 /*
686 * Record what we've done to the packet (under what SA it was
687 * processed). If we've been passed an mtag, it means the packet
688 * was already processed by an ethernet/crypto combo card and
689 * thus has a tag attached with all the right information, but
690 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
691 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
692 */
693 if (mt == NULL && sproto != IPPROTO_IPCOMP) {
694 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
695 sizeof(struct tdb_ident), M_NOWAIT);
696 if (mtag == NULL) {
697 DPRINTF(("%s: failed to get tag\n", __func__));
698 IPSEC_ISTAT(sproto, hdrops);
699 error = ENOMEM;
700 goto bad;
701 }
702
703 tdbi = (struct tdb_ident *)(mtag + 1);
704 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
705 tdbi->proto = sproto;
706 tdbi->spi = sav->spi;

--- 163 unchanged lines hidden ---