1 /* $FreeBSD: src/sys/netipsec/ipsec_input.c,v 1.2.4.2 2003/03/28 20:32:53 sam Exp $ */
2 /* $DragonFly: src/sys/netproto/ipsec/ipsec_input.c,v 1.12 2007/10/20 10:28:44 sephe Exp $ */
3 /* $OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $ */
5 * The authors of this code are John Ioannidis (ji@tla.org),
6 * Angelos D. Keromytis (kermit@csd.uch.gr) and
7 * Niels Provos (provos@physnet.uni-hamburg.de).
9 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
12 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
13 * by Angelos D. Keromytis.
15 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
18 * Additional features in 1999 by Angelos D. Keromytis.
20 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
21 * Angelos D. Keromytis and Niels Provos.
22 * Copyright (c) 2001, Angelos D. Keromytis.
24 * Permission to use, copy, and modify this software with or without fee
25 * is hereby granted, provided that this entire notice is included in
26 * all copies of any software which is or includes a copy or
27 * modification of this software.
28 * You may use this code under the GNU public license if you so wish. Please
29 * contribute changes back to the authors under this freer than GPL license
30 * so that we may further the use of strong encryption without limitations to
33 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
34 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
35 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
36 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
41 * IPsec input processing.
45 #include "opt_inet6.h"
46 #include "opt_ipsec.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
52 #include <sys/domain.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/errno.h>
56 #include <sys/syslog.h>
57 #include <sys/in_cksum.h>
60 #include <net/route.h>
61 #include <net/netisr.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/in_var.h>
69 #include <netinet/ip6.h>
71 #include <netinet6/ip6_var.h>
73 #include <netinet/in_pcb.h>
75 #include <netinet/icmp6.h>
78 #include <netproto/ipsec/ipsec.h>
80 #include <netproto/ipsec/ipsec6.h>
82 #include <netproto/ipsec/ah_var.h>
83 #include <netproto/ipsec/esp.h>
84 #include <netproto/ipsec/esp_var.h>
85 #include <netproto/ipsec/ipcomp_var.h>
87 #include <netproto/ipsec/key.h>
88 #include <netproto/ipsec/keydb.h>
90 #include <netproto/ipsec/xform.h>
91 #include <netinet6/ip6protosw.h>
93 #include <machine/stdarg.h>
95 #include <net/net_osdep.h>
97 #define IPSEC_ISTAT(p,x,y,z) ((p) == IPPROTO_ESP ? (x)++ : \
98 (p) == IPPROTO_AH ? (y)++ : (z)++)
101 * ipsec_common_input gets called when an IPsec-protected packet
102 * is received by IPv4 or IPv6. It's job is to find the right SA
103 # and call the appropriate transform. The transform callback
104 * takes care of further processing (like ingress filtering).
107 ipsec_common_input(struct mbuf
*m
, int skip
, int protoff
, int af
, int sproto
)
109 union sockaddr_union dst_address
;
110 struct secasvar
*sav
;
114 IPSEC_ISTAT(sproto
, espstat
.esps_input
, ahstat
.ahs_input
,
115 ipcompstat
.ipcomps_input
);
117 KASSERT(m
!= NULL
, ("ipsec_common_input: null packet"));
119 if ((sproto
== IPPROTO_ESP
&& !esp_enable
) ||
120 (sproto
== IPPROTO_AH
&& !ah_enable
) ||
121 (sproto
== IPPROTO_IPCOMP
&& !ipcomp_enable
)) {
123 IPSEC_ISTAT(sproto
, espstat
.esps_pdrops
, ahstat
.ahs_pdrops
,
124 ipcompstat
.ipcomps_pdrops
);
128 if (m
->m_pkthdr
.len
- skip
< 2 * sizeof (u_int32_t
)) {
130 IPSEC_ISTAT(sproto
, espstat
.esps_hdrops
, ahstat
.ahs_hdrops
,
131 ipcompstat
.ipcomps_hdrops
);
132 DPRINTF(("ipsec_common_input: packet too small\n"));
136 /* Retrieve the SPI from the relevant IPsec header */
137 if (sproto
== IPPROTO_ESP
)
138 m_copydata(m
, skip
, sizeof(u_int32_t
), (caddr_t
) &spi
);
139 else if (sproto
== IPPROTO_AH
)
140 m_copydata(m
, skip
+ sizeof(u_int32_t
), sizeof(u_int32_t
),
142 else if (sproto
== IPPROTO_IPCOMP
) {
144 m_copydata(m
, skip
+ sizeof(u_int16_t
), sizeof(u_int16_t
),
146 spi
= ntohl(htons(cpi
));
150 * Find the SA and (indirectly) call the appropriate
151 * kernel crypto routine. The resulting mbuf chain is a valid
152 * IP packet ready to go through input processing.
154 bzero(&dst_address
, sizeof (dst_address
));
155 dst_address
.sa
.sa_family
= af
;
159 dst_address
.sin
.sin_len
= sizeof(struct sockaddr_in
);
160 m_copydata(m
, offsetof(struct ip
, ip_dst
),
161 sizeof(struct in_addr
),
162 (caddr_t
) &dst_address
.sin
.sin_addr
);
167 dst_address
.sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
168 m_copydata(m
, offsetof(struct ip6_hdr
, ip6_dst
),
169 sizeof(struct in6_addr
),
170 (caddr_t
) &dst_address
.sin6
.sin6_addr
);
174 DPRINTF(("ipsec_common_input: unsupported protocol "
177 IPSEC_ISTAT(sproto
, espstat
.esps_nopf
, ahstat
.ahs_nopf
,
178 ipcompstat
.ipcomps_nopf
);
184 /* NB: only pass dst since key_allocsa follows RFC2401 */
185 sav
= KEY_ALLOCSA(&dst_address
, sproto
, spi
);
187 DPRINTF(("ipsec_common_input: no key association found for"
189 ipsec_address(&dst_address
),
190 (u_long
) ntohl(spi
), sproto
));
191 IPSEC_ISTAT(sproto
, espstat
.esps_notdb
, ahstat
.ahs_notdb
,
192 ipcompstat
.ipcomps_notdb
);
198 if (sav
->tdb_xform
== NULL
) {
199 DPRINTF(("ipsec_common_input: attempted to use uninitialized"
201 ipsec_address(&dst_address
),
202 (u_long
) ntohl(spi
), sproto
));
203 IPSEC_ISTAT(sproto
, espstat
.esps_noxform
, ahstat
.ahs_noxform
,
204 ipcompstat
.ipcomps_noxform
);
212 * Call appropriate transform and return -- callback takes care of
215 error
= (*sav
->tdb_xform
->xf_input
)(m
, sav
, skip
, protoff
);
223 * Common input handler for IPv4 AH, ESP, and IPCOMP.
226 ipsec4_common_input(struct mbuf
*m
, ...)
232 off
= __va_arg(ap
, int);
233 nxt
= __va_arg(ap
, int);
236 return ipsec_common_input(m
, off
, offsetof(struct ip
, ip_p
),
241 * IPsec input callback for INET protocols.
242 * This routine is called as the transform callback.
243 * Takes care of filtering and other sanity checks on
244 * the processed packet.
247 ipsec4_common_input_cb(struct mbuf
*m
, struct secasvar
*sav
,
248 int skip
, int protoff
, struct m_tag
*mt
)
250 int prot
, af
, sproto
;
253 struct tdb_ident
*tdbi
;
254 struct secasindex
*saidx
;
257 KASSERT(m
!= NULL
, ("ipsec4_common_input_cb: null mbuf"));
258 KASSERT(sav
!= NULL
, ("ipsec4_common_input_cb: null SA"));
259 KASSERT(sav
->sah
!= NULL
, ("ipsec4_common_input_cb: null SAH"));
260 saidx
= &sav
->sah
->saidx
;
261 af
= saidx
->dst
.sa
.sa_family
;
262 KASSERT(af
== AF_INET
, ("ipsec4_common_input_cb: unexpected af %u",af
));
263 sproto
= saidx
->proto
;
264 KASSERT(sproto
== IPPROTO_ESP
|| sproto
== IPPROTO_AH
||
265 sproto
== IPPROTO_IPCOMP
,
266 ("ipsec4_common_input_cb: unexpected security protocol %u",
271 DPRINTF(("ipsec4_common_input_cb: null mbuf"));
272 IPSEC_ISTAT(sproto
, espstat
.esps_badkcr
, ahstat
.ahs_badkcr
,
273 ipcompstat
.ipcomps_badkcr
);
279 /* Fix IPv4 header */
280 if (m
->m_len
< skip
&& (m
= m_pullup(m
, skip
)) == NULL
) {
281 DPRINTF(("ipsec4_common_input_cb: processing failed "
283 ipsec_address(&sav
->sah
->saidx
.dst
),
284 (u_long
) ntohl(sav
->spi
)));
285 IPSEC_ISTAT(sproto
, espstat
.esps_hdrops
, ahstat
.ahs_hdrops
,
286 ipcompstat
.ipcomps_hdrops
);
291 ip
= mtod(m
, struct ip
*);
292 ip
->ip_len
= htons(m
->m_pkthdr
.len
);
293 ip
->ip_off
= htons(ip
->ip_off
);
295 ip
->ip_sum
= in_cksum(m
, ip
->ip_hl
<< 2);
297 ip
= mtod(m
, struct ip
*);
301 /* IP-in-IP encapsulation */
302 if (prot
== IPPROTO_IPIP
) {
305 if (m
->m_pkthdr
.len
- skip
< sizeof(struct ip
)) {
306 IPSEC_ISTAT(sproto
, espstat
.esps_hdrops
,
307 ahstat
.ahs_hdrops
, ipcompstat
.ipcomps_hdrops
);
312 /* ipn will now contain the inner IPv4 header */
313 m_copydata(m
, skip
, sizeof(struct ip
), (caddr_t
) &ipn
);
316 /* XXX PROXY address isn't recorded in SAH */
318 * Check that the inner source address is the same as
319 * the proxy address, if available.
321 if ((saidx
->proxy
.sa
.sa_family
== AF_INET
&&
322 saidx
->proxy
.sin
.sin_addr
.s_addr
!=
325 saidx
->proxy
.sin
.sin_addr
.s_addr
) ||
326 (saidx
->proxy
.sa
.sa_family
!= AF_INET
&&
327 saidx
->proxy
.sa
.sa_family
!= 0)) {
329 DPRINTF(("ipsec4_common_input_cb: inner "
330 "source address %s doesn't correspond to "
331 "expected proxy source %s, SA %s/%08lx\n",
332 inet_ntoa4(ipn
.ip_src
),
333 ipsp_address(saidx
->proxy
),
334 ipsp_address(saidx
->dst
),
335 (u_long
) ntohl(sav
->spi
)));
337 IPSEC_ISTAT(sproto
, espstat
.esps_pdrops
,
339 ipcompstat
.ipcomps_pdrops
);
346 /* IPv6-in-IP encapsulation. */
347 if (prot
== IPPROTO_IPV6
) {
350 if (m
->m_pkthdr
.len
- skip
< sizeof(struct ip6_hdr
)) {
351 IPSEC_ISTAT(sproto
, espstat
.esps_hdrops
,
352 ahstat
.ahs_hdrops
, ipcompstat
.ipcomps_hdrops
);
357 /* ip6n will now contain the inner IPv6 header. */
358 m_copydata(m
, skip
, sizeof(struct ip6_hdr
), (caddr_t
) &ip6n
);
362 * Check that the inner source address is the same as
363 * the proxy address, if available.
365 if ((saidx
->proxy
.sa
.sa_family
== AF_INET6
&&
366 !IN6_IS_ADDR_UNSPECIFIED(&saidx
->proxy
.sin6
.sin6_addr
) &&
367 !IN6_ARE_ADDR_EQUAL(&ip6n
.ip6_src
,
368 &saidx
->proxy
.sin6
.sin6_addr
)) ||
369 (saidx
->proxy
.sa
.sa_family
!= AF_INET6
&&
370 saidx
->proxy
.sa
.sa_family
!= 0)) {
372 DPRINTF(("ipsec4_common_input_cb: inner "
373 "source address %s doesn't correspond to "
374 "expected proxy source %s, SA %s/%08lx\n",
375 ip6_sprintf(&ip6n
.ip6_src
),
376 ipsec_address(&saidx
->proxy
),
377 ipsec_address(&saidx
->dst
),
378 (u_long
) ntohl(sav
->spi
)));
380 IPSEC_ISTAT(sproto
, espstat
.esps_pdrops
,
382 ipcompstat
.ipcomps_pdrops
);
391 * Record what we've done to the packet (under what SA it was
392 * processed). If we've been passed an mtag, it means the packet
393 * was already processed by an ethernet/crypto combo card and
394 * thus has a tag attached with all the right information, but
395 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
396 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
398 if (mt
== NULL
&& sproto
!= IPPROTO_IPCOMP
) {
399 mtag
= m_tag_get(PACKET_TAG_IPSEC_IN_DONE
,
400 sizeof(struct tdb_ident
), MB_DONTWAIT
);
402 DPRINTF(("ipsec4_common_input_cb: failed to get tag\n"));
403 IPSEC_ISTAT(sproto
, espstat
.esps_hdrops
,
404 ahstat
.ahs_hdrops
, ipcompstat
.ipcomps_hdrops
);
409 tdbi
= (struct tdb_ident
*)m_tag_data(mtag
);
410 bcopy(&saidx
->dst
, &tdbi
->dst
, saidx
->dst
.sa
.sa_len
);
411 tdbi
->proto
= sproto
;
412 tdbi
->spi
= sav
->spi
;
414 m_tag_prepend(m
, mtag
);
416 mt
->m_tag_id
= PACKET_TAG_IPSEC_IN_DONE
;
417 /* XXX do we need to mark m_flags??? */
420 key_sa_recordxfer(sav
, m
); /* record data transfer */
423 * Re-dispatch via software interrupt.
425 if (netisr_queue(NETISR_IP
, m
)) {
426 IPSEC_ISTAT(sproto
, espstat
.esps_qfull
, ahstat
.ahs_qfull
,
427 ipcompstat
.ipcomps_qfull
);
429 DPRINTF(("ipsec4_common_input_cb: queue full; "
430 "proto %u packet dropped\n", sproto
));
441 /* IPv6 AH wrapper. */
443 ipsec6_common_input(struct mbuf
**mp
, int *offp
, int proto
)
449 if (*offp
< sizeof(struct ip6_hdr
)) {
450 DPRINTF(("ipsec6_common_input: bad offset %u\n", *offp
));
452 } else if (*offp
== sizeof(struct ip6_hdr
)) {
453 protoff
= offsetof(struct ip6_hdr
, ip6_nxt
);
455 /* Chase down the header chain... */
456 protoff
= sizeof(struct ip6_hdr
);
460 m_copydata(*mp
, protoff
, sizeof(ip6e
),
463 if (ip6e
.ip6e_nxt
== IPPROTO_AH
)
464 l
= (ip6e
.ip6e_len
+ 2) << 2;
466 l
= (ip6e
.ip6e_len
+ 1) << 3;
467 KASSERT(l
> 0, ("ah6_input: l went zero or negative"));
468 } while (protoff
+ l
< *offp
);
470 /* Malformed packet check */
471 if (protoff
+ l
!= *offp
) {
472 DPRINTF(("ipsec6_common_input: bad packet header chain, "
473 "protoff %u, l %u, off %u\n", protoff
, l
, *offp
));
474 IPSEC_ISTAT(proto
, espstat
.esps_hdrops
,
476 ipcompstat
.ipcomps_hdrops
);
481 protoff
+= offsetof(struct ip6_ext
, ip6e_nxt
);
483 ipsec_common_input(*mp
, *offp
, protoff
, AF_INET6
, proto
);
488 esp6_ctlinput(int cmd
, struct sockaddr
*sa
, void *d
)
490 if (sa
->sa_family
!= AF_INET6
||
491 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
493 if ((unsigned)cmd
>= PRC_NCMDS
)
496 /* if the parameter is from icmp6, decode it. */
498 struct ip6ctlparam
*ip6cp
= (struct ip6ctlparam
*)d
;
499 struct mbuf
*m
= ip6cp
->ip6c_m
;
500 int off
= ip6cp
->ip6c_off
;
502 struct ip6ctlparam ip6cp1
;
505 * Notify the error to all possible sockets via pfctlinput2.
506 * Since the upper layer information (such as protocol type,
507 * source and destination ports) is embedded in the encrypted
508 * data and might have been cut, we can't directly call
509 * an upper layer ctlinput function. However, the pcbnotify
510 * function will consider source and destination addresses
511 * as well as the flow info value, and may be able to find
512 * some PCB that should be notified.
513 * Although pfctlinput2 will call esp6_ctlinput(), there is
514 * no possibility of an infinite loop of function calls,
515 * because we don't pass the inner IPv6 header.
517 bzero(&ip6cp1
, sizeof(ip6cp1
));
518 ip6cp1
.ip6c_src
= ip6cp
->ip6c_src
;
519 pfctlinput2(cmd
, sa
, (void *)&ip6cp1
);
522 * Then go to special cases that need ESP header information.
523 * XXX: We assume that when ip6 is non NULL,
524 * M and OFF are valid.
527 if (cmd
== PRC_MSGSIZE
) {
528 struct secasvar
*sav
;
532 /* check header length before using m_copydata */
533 if (m
->m_pkthdr
.len
< off
+ sizeof (struct esp
))
535 m_copydata(m
, off
+ offsetof(struct esp
, esp_spi
),
536 sizeof(u_int32_t
), (caddr_t
) &spi
);
538 * Check to see if we have a valid SA corresponding to
539 * the address in the ICMP message payload.
541 sav
= KEY_ALLOCSA((union sockaddr_union
*)sa
,
543 valid
= (sav
!= NULL
);
547 /* XXX Further validation? */
550 * Depending on whether the SA is "valid" and
551 * routing table size (mtudisc_{hi,lo}wat), we will:
552 * - recalcurate the new MTU and create the
553 * corresponding routing entry, or
554 * - ignore the MTU change notification.
556 icmp6_mtudisc_update(ip6cp
, valid
);
559 /* we normally notify any pcb here */
563 extern struct ip6protosw inet6sw
[];
564 extern u_char ip6_protox
[];
567 * IPsec input callback, called by the transform callback. Takes care of
568 * filtering and other sanity checks on the processed packet.
571 ipsec6_common_input_cb(struct mbuf
*m
, struct secasvar
*sav
, int skip
, int protoff
,
574 int prot
, af
, sproto
;
577 struct tdb_ident
*tdbi
;
578 struct secasindex
*saidx
;
583 KASSERT(m
!= NULL
, ("ipsec6_common_input_cb: null mbuf"));
584 KASSERT(sav
!= NULL
, ("ipsec6_common_input_cb: null SA"));
585 KASSERT(sav
->sah
!= NULL
, ("ipsec6_common_input_cb: null SAH"));
586 saidx
= &sav
->sah
->saidx
;
587 af
= saidx
->dst
.sa
.sa_family
;
588 KASSERT(af
== AF_INET6
,
589 ("ipsec6_common_input_cb: unexpected af %u", af
));
590 sproto
= saidx
->proto
;
591 KASSERT(sproto
== IPPROTO_ESP
|| sproto
== IPPROTO_AH
||
592 sproto
== IPPROTO_IPCOMP
,
593 ("ipsec6_common_input_cb: unexpected security protocol %u",
598 DPRINTF(("ipsec4_common_input_cb: null mbuf"));
599 IPSEC_ISTAT(sproto
, espstat
.esps_badkcr
, ahstat
.ahs_badkcr
,
600 ipcompstat
.ipcomps_badkcr
);
605 /* Fix IPv6 header */
606 if (m
->m_len
< sizeof(struct ip6_hdr
) &&
607 (m
= m_pullup(m
, sizeof(struct ip6_hdr
))) == NULL
) {
609 DPRINTF(("ipsec_common_input_cb: processing failed "
610 "for SA %s/%08lx\n", ipsec_address(&sav
->sah
->saidx
.dst
),
611 (u_long
) ntohl(sav
->spi
)));
613 IPSEC_ISTAT(sproto
, espstat
.esps_hdrops
, ahstat
.ahs_hdrops
,
614 ipcompstat
.ipcomps_hdrops
);
619 ip6
= mtod(m
, struct ip6_hdr
*);
620 ip6
->ip6_plen
= htons(m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
));
623 m_copydata(m
, protoff
, 1, (unsigned char *) &prot
);
626 /* IP-in-IP encapsulation */
627 if (prot
== IPPROTO_IPIP
) {
630 if (m
->m_pkthdr
.len
- skip
< sizeof(struct ip
)) {
631 IPSEC_ISTAT(sproto
, espstat
.esps_hdrops
,
632 ahstat
.ahs_hdrops
, ipcompstat
.ipcomps_hdrops
);
637 /* ipn will now contain the inner IPv4 header */
638 m_copydata(m
, skip
, sizeof(struct ip
), (caddr_t
) &ipn
);
642 * Check that the inner source address is the same as
643 * the proxy address, if available.
645 if ((saidx
->proxy
.sa
.sa_family
== AF_INET
&&
646 saidx
->proxy
.sin
.sin_addr
.s_addr
!= INADDR_ANY
&&
647 ipn
.ip_src
.s_addr
!= saidx
->proxy
.sin
.sin_addr
.s_addr
) ||
648 (saidx
->proxy
.sa
.sa_family
!= AF_INET
&&
649 saidx
->proxy
.sa
.sa_family
!= 0)) {
651 DPRINTF(("ipsec_common_input_cb: inner "
652 "source address %s doesn't correspond to "
653 "expected proxy source %s, SA %s/%08lx\n",
654 inet_ntoa4(ipn
.ip_src
),
655 ipsec_address(&saidx
->proxy
),
656 ipsec_address(&saidx
->dst
),
657 (u_long
) ntohl(sav
->spi
)));
659 IPSEC_ISTATsproto
, (espstat
.esps_pdrops
,
660 ahstat
.ahs_pdrops
, ipcompstat
.ipcomps_pdrops
);
668 /* IPv6-in-IP encapsulation */
669 if (prot
== IPPROTO_IPV6
) {
672 if (m
->m_pkthdr
.len
- skip
< sizeof(struct ip6_hdr
)) {
673 IPSEC_ISTAT(sproto
, espstat
.esps_hdrops
,
674 ahstat
.ahs_hdrops
, ipcompstat
.ipcomps_hdrops
);
679 /* ip6n will now contain the inner IPv6 header. */
680 m_copydata(m
, skip
, sizeof(struct ip6_hdr
), (caddr_t
) &ip6n
);
684 * Check that the inner source address is the same as
685 * the proxy address, if available.
687 if ((saidx
->proxy
.sa
.sa_family
== AF_INET6
&&
688 !IN6_IS_ADDR_UNSPECIFIED(&saidx
->proxy
.sin6
.sin6_addr
) &&
689 !IN6_ARE_ADDR_EQUAL(&ip6n
.ip6_src
,
690 &saidx
->proxy
.sin6
.sin6_addr
)) ||
691 (saidx
->proxy
.sa
.sa_family
!= AF_INET6
&&
692 saidx
->proxy
.sa
.sa_family
!= 0)) {
694 DPRINTF(("ipsec_common_input_cb: inner "
695 "source address %s doesn't correspond to "
696 "expected proxy source %s, SA %s/%08lx\n",
697 ip6_sprintf(&ip6n
.ip6_src
),
698 ipsec_address(&saidx
->proxy
),
699 ipsec_address(&saidx
->dst
),
700 (u_long
) ntohl(sav
->spi
)));
702 IPSEC_ISTAT(sproto
, espstat
.esps_pdrops
,
703 ahstat
.ahs_pdrops
, ipcompstat
.ipcomps_pdrops
);
711 * Record what we've done to the packet (under what SA it was
712 * processed). If we've been passed an mtag, it means the packet
713 * was already processed by an ethernet/crypto combo card and
714 * thus has a tag attached with all the right information, but
715 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
716 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
718 if (mt
== NULL
&& sproto
!= IPPROTO_IPCOMP
) {
719 mtag
= m_tag_get(PACKET_TAG_IPSEC_IN_DONE
,
720 sizeof(struct tdb_ident
), MB_DONTWAIT
);
722 DPRINTF(("ipsec_common_input_cb: failed to "
724 IPSEC_ISTAT(sproto
, espstat
.esps_hdrops
,
725 ahstat
.ahs_hdrops
, ipcompstat
.ipcomps_hdrops
);
730 tdbi
= (struct tdb_ident
*)m_tag_data(mtag
);
731 bcopy(&saidx
->dst
, &tdbi
->dst
, sizeof(union sockaddr_union
));
732 tdbi
->proto
= sproto
;
733 tdbi
->spi
= sav
->spi
;
735 m_tag_prepend(m
, mtag
);
737 mt
->m_tag_id
= PACKET_TAG_IPSEC_IN_DONE
;
738 /* XXX do we need to mark m_flags??? */
741 key_sa_recordxfer(sav
, m
);
743 /* Retrieve new protocol */
744 m_copydata(m
, protoff
, sizeof(u_int8_t
), (caddr_t
) &nxt8
);
747 * See the end of ip6_input for this logic.
748 * IPPROTO_IPV[46] case will be processed just like other ones
752 while (nxt
!= IPPROTO_DONE
) {
753 if (ip6_hdrnestlimit
&& (++nest
> ip6_hdrnestlimit
)) {
754 ip6stat
.ip6s_toomanyhdr
++;
760 * Protection against faulty packet - there should be
761 * more sanity checks in header chain processing.
763 if (m
->m_pkthdr
.len
< skip
) {
764 ip6stat
.ip6s_tooshort
++;
765 in6_ifstat_inc(m
->m_pkthdr
.rcvif
, ifs6_in_truncated
);
770 * Enforce IPsec policy checking if we are seeing last header.
771 * note that we do not visit this with protocols with pcb layer
772 * code - like udp/tcp/raw ip.
774 if ((inet6sw
[ip6_protox
[nxt
]].pr_flags
& PR_LASTHDR
) != 0 &&
775 ipsec6_in_reject(m
, NULL
)) {
779 nxt
= (*inet6sw
[ip6_protox
[nxt
]].pr_input
)(&m
, &skip
, nxt
);