ipsec_input.c revision 1.1 1 /* $NetBSD: ipsec_input.c,v 1.1 2003/08/13 20:06:50 jonathan Exp $ */
2 /* $FreeBSD: src/sys/netipsec/ipsec_input.c,v 1.2.4.1 2003/01/24 05:11:35 sam Exp $ */
3 /* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */
4
5 #include <sys/cdefs.h>
6 __KERNEL_RCSID(0, "$NetBSD: ipsec_input.c,v 1.1 2003/08/13 20:06:50 jonathan Exp $");
7
8 /*
9 * IPsec input processing.
10 */
11
12 #include "opt_inet.h"
13 #include "opt_inet6.h"
14 #include "opt_ipsec.h"
15
16 #include <sys/param.h>
17 #include <sys/systm.h>
18 #include <sys/malloc.h>
19 #include <sys/mbuf.h>
20 #include <sys/domain.h>
21 #include <sys/protosw.h>
22 #include <sys/socket.h>
23 #include <sys/errno.h>
24 #include <sys/syslog.h>
25
26 #include <net/if.h>
27 #include <net/route.h>
28 #include <net/netisr.h>
29
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/ip.h>
33 #include <netinet/ip_var.h>
34 #include <netinet/in_var.h>
35
36 #include <netinet/ip6.h>
37 #ifdef INET6
38 #include <netinet6/ip6_var.h>
39 #endif
40 #include <netinet/in_pcb.h>
41 #ifdef INET6
42 #include <netinet/icmp6.h>
43 #endif
44
45 #include <netipsec/ipsec.h>
46 #ifdef INET6
47 #include <netipsec/ipsec6.h>
48 #endif
49 #include <netipsec/ah_var.h>
50 #include <netipsec/esp.h>
51 #include <netipsec/esp_var.h>
52 #include <netipsec/ipcomp_var.h>
53
54 #include <netipsec/key.h>
55 #include <netipsec/keydb.h>
56
57 #include <netipsec/xform.h>
58 #include <netinet6/ip6protosw.h>
59
60 #include <netipsec/ipsec_osdep.h>
61
62 #include <machine/stdarg.h>
63
64 #include <net/net_osdep.h>
65
66 #define IPSEC_ISTAT(p,x,y,z) ((p) == IPPROTO_ESP ? (x)++ : \
67 (p) == IPPROTO_AH ? (y)++ : (z)++)
68
69 /*
70 * ipsec_common_input gets called when an IPsec-protected packet
71 * is received by IPv4 or IPv6. It's job is to find the right SA
72 # and call the appropriate transform. The transform callback
73 * takes care of further processing (like ingress filtering).
74 */
75 static int
76 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
77 {
78 union sockaddr_union dst_address;
79 struct secasvar *sav;
80 u_int32_t spi;
81 int s, error;
82
83 IPSEC_ISTAT(sproto, espstat.esps_input, ahstat.ahs_input,
84 ipcompstat.ipcomps_input);
85
86 IPSEC_ASSERT(m != NULL, ("ipsec_common_input: null packet"));
87
88 if ((sproto == IPPROTO_ESP && !esp_enable) ||
89 (sproto == IPPROTO_AH && !ah_enable) ||
90 (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) {
91 m_freem(m);
92 IPSEC_ISTAT(sproto, espstat.esps_pdrops, ahstat.ahs_pdrops,
93 ipcompstat.ipcomps_pdrops);
94 return EOPNOTSUPP;
95 }
96
97 if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
98 m_freem(m);
99 IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops,
100 ipcompstat.ipcomps_hdrops);
101 DPRINTF(("ipsec_common_input: packet too small\n"));
102 return EINVAL;
103 }
104
105 /* Retrieve the SPI from the relevant IPsec header */
106 if (sproto == IPPROTO_ESP)
107 m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
108 else if (sproto == IPPROTO_AH)
109 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t),
110 (caddr_t) &spi);
111 else if (sproto == IPPROTO_IPCOMP) {
112 u_int16_t cpi;
113 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t),
114 (caddr_t) &cpi);
115 spi = ntohl(htons(cpi));
116 }
117
118 /*
119 * Find the SA and (indirectly) call the appropriate
120 * kernel crypto routine. The resulting mbuf chain is a valid
121 * IP packet ready to go through input processing.
122 */
123 bzero(&dst_address, sizeof (dst_address));
124 dst_address.sa.sa_family = af;
125 switch (af) {
126 #ifdef INET
127 case AF_INET:
128 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
129 m_copydata(m, offsetof(struct ip, ip_dst),
130 sizeof(struct in_addr),
131 (caddr_t) &dst_address.sin.sin_addr);
132 break;
133 #endif /* INET */
134 #ifdef INET6
135 case AF_INET6:
136 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
137 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
138 sizeof(struct in6_addr),
139 (caddr_t) &dst_address.sin6.sin6_addr);
140 break;
141 #endif /* INET6 */
142 default:
143 DPRINTF(("ipsec_common_input: unsupported protocol "
144 "family %u\n", af));
145 m_freem(m);
146 IPSEC_ISTAT(sproto, espstat.esps_nopf, ahstat.ahs_nopf,
147 ipcompstat.ipcomps_nopf);
148 return EPFNOSUPPORT;
149 }
150
151 s = splsoftnet();
152
153 /* NB: only pass dst since key_allocsa follows RFC2401 */
154 sav = KEY_ALLOCSA(&dst_address, sproto, spi);
155 if (sav == NULL) {
156 DPRINTF(("ipsec_common_input: no key association found for"
157 " SA %s/%08lx/%u\n",
158 ipsec_address(&dst_address),
159 (u_long) ntohl(spi), sproto));
160 IPSEC_ISTAT(sproto, espstat.esps_notdb, ahstat.ahs_notdb,
161 ipcompstat.ipcomps_notdb);
162 splx(s);
163 m_freem(m);
164 return ENOENT;
165 }
166
167 if (sav->tdb_xform == NULL) {
168 DPRINTF(("ipsec_common_input: attempted to use uninitialized"
169 " SA %s/%08lx/%u\n",
170 ipsec_address(&dst_address),
171 (u_long) ntohl(spi), sproto));
172 IPSEC_ISTAT(sproto, espstat.esps_noxform, ahstat.ahs_noxform,
173 ipcompstat.ipcomps_noxform);
174 KEY_FREESAV(&sav);
175 splx(s);
176 m_freem(m);
177 return ENXIO;
178 }
179
180 /*
181 * Call appropriate transform and return -- callback takes care of
182 * everything else.
183 */
184 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
185 KEY_FREESAV(&sav);
186 splx(s);
187 return error;
188 }
189
190 #ifdef INET
191 /*
192 * Common input handler for IPv4 AH, ESP, and IPCOMP.
193 */
194 int
195 ipsec4_common_input(struct mbuf *m, ...)
196 {
197 va_list ap;
198 int off, nxt;
199
200 va_start(ap, m);
201 off = va_arg(ap, int);
202 nxt = va_arg(ap, int);
203 va_end(ap);
204
205 return ipsec_common_input(m, off, offsetof(struct ip, ip_p),
206 AF_INET, nxt);
207 }
208
209 /*
210 * IPsec input callback for INET protocols.
211 * This routine is called as the transform callback.
212 * Takes care of filtering and other sanity checks on
213 * the processed packet.
214 */
215 int
216 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
217 int skip, int protoff, struct m_tag *mt)
218 {
219 int prot, af, sproto;
220 struct ip *ip;
221 struct m_tag *mtag;
222 struct tdb_ident *tdbi;
223 struct secasindex *saidx;
224 int error;
225
226 IPSEC_SPLASSERT_SOFTNET("ipsec4_common_input_cb");
227
228 IPSEC_ASSERT(m != NULL, ("ipsec4_common_input_cb: null mbuf"));
229 IPSEC_ASSERT(sav != NULL, ("ipsec4_common_input_cb: null SA"));
230 IPSEC_ASSERT(sav->sah != NULL, ("ipsec4_common_input_cb: null SAH"));
231 saidx = &sav->sah->saidx;
232 af = saidx->dst.sa.sa_family;
233 IPSEC_ASSERT(af == AF_INET, ("ipsec4_common_input_cb: unexpected af %u",af));
234 sproto = saidx->proto;
235 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
236 sproto == IPPROTO_IPCOMP,
237 ("ipsec4_common_input_cb: unexpected security protocol %u",
238 sproto));
239
240 /* Sanity check */
241 if (m == NULL) {
242 DPRINTF(("ipsec4_common_input_cb: null mbuf"));
243 IPSEC_ISTAT(sproto, espstat.esps_badkcr, ahstat.ahs_badkcr,
244 ipcompstat.ipcomps_badkcr);
245 KEY_FREESAV(&sav);
246 return EINVAL;
247 }
248
249 if (skip != 0) {
250 /* Fix IPv4 header */
251 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
252 DPRINTF(("ipsec4_common_input_cb: processing failed "
253 "for SA %s/%08lx\n",
254 ipsec_address(&sav->sah->saidx.dst),
255 (u_long) ntohl(sav->spi)));
256 IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops,
257 ipcompstat.ipcomps_hdrops);
258 error = ENOBUFS;
259 goto bad;
260 }
261
262 ip = mtod(m, struct ip *);
263 ip->ip_len = htons(m->m_pkthdr.len);
264 ip->ip_off = htons(ip->ip_off);
265 ip->ip_sum = 0;
266 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
267 } else {
268 ip = mtod(m, struct ip *);
269 }
270 prot = ip->ip_p;
271
272 /* IP-in-IP encapsulation */
273 if (prot == IPPROTO_IPIP) {
274 struct ip ipn;
275
276 /* ipn will now contain the inner IPv4 header */
277 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip),
278 (caddr_t) &ipn);
279
280 #ifdef notyet
281 /* XXX PROXY address isn't recorded in SAH */
282 /*
283 * Check that the inner source address is the same as
284 * the proxy address, if available.
285 */
286 if ((saidx->proxy.sa.sa_family == AF_INET &&
287 saidx->proxy.sin.sin_addr.s_addr !=
288 INADDR_ANY &&
289 ipn.ip_src.s_addr !=
290 saidx->proxy.sin.sin_addr.s_addr) ||
291 (saidx->proxy.sa.sa_family != AF_INET &&
292 saidx->proxy.sa.sa_family != 0)) {
293
294 DPRINTF(("ipsec4_common_input_cb: inner "
295 "source address %s doesn't correspond to "
296 "expected proxy source %s, SA %s/%08lx\n",
297 inet_ntoa4(ipn.ip_src),
298 ipsp_address(saidx->proxy),
299 ipsp_address(saidx->dst),
300 (u_long) ntohl(sav->spi)));
301
302 IPSEC_ISTAT(sproto, espstat.esps_pdrops,
303 ahstat.ahs_pdrops,
304 ipcompstat.ipcomps_pdrops);
305 error = EACCES;
306 goto bad;
307 }
308 #endif /*XXX*/
309 }
310 #if INET6
311 /* IPv6-in-IP encapsulation. */
312 if (prot == IPPROTO_IPV6) {
313 struct ip6_hdr ip6n;
314
315 /* ip6n will now contain the inner IPv6 header. */
316 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr),
317 (caddr_t) &ip6n);
318
319 #ifdef notyet
320 /*
321 * Check that the inner source address is the same as
322 * the proxy address, if available.
323 */
324 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
325 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
326 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
327 &saidx->proxy.sin6.sin6_addr)) ||
328 (saidx->proxy.sa.sa_family != AF_INET6 &&
329 saidx->proxy.sa.sa_family != 0)) {
330
331 DPRINTF(("ipsec4_common_input_cb: inner "
332 "source address %s doesn't correspond to "
333 "expected proxy source %s, SA %s/%08lx\n",
334 ip6_sprintf(&ip6n.ip6_src),
335 ipsec_address(&saidx->proxy),
336 ipsec_address(&saidx->dst),
337 (u_long) ntohl(sav->spi)));
338
339 IPSEC_ISTAT(sproto, espstat.esps_pdrops,
340 ahstat.ahs_pdrops,
341 ipcompstat.ipcomps_pdrops);
342 error = EACCES;
343 goto bad;
344 }
345 #endif /*XXX*/
346 }
347 #endif /* INET6 */
348
349 /*
350 * Record what we've done to the packet (under what SA it was
351 * processed). If we've been passed an mtag, it means the packet
352 * was already processed by an ethernet/crypto combo card and
353 * thus has a tag attached with all the right information, but
354 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
355 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
356 */
357 if (mt == NULL && sproto != IPPROTO_IPCOMP) {
358 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
359 sizeof(struct tdb_ident), M_NOWAIT);
360 if (mtag == NULL) {
361 DPRINTF(("ipsec4_common_input_cb: failed to get tag\n"));
362 IPSEC_ISTAT(sproto, espstat.esps_hdrops,
363 ahstat.ahs_hdrops, ipcompstat.ipcomps_hdrops);
364 error = ENOMEM;
365 goto bad;
366 }
367
368 tdbi = (struct tdb_ident *)(mtag + 1);
369 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
370 tdbi->proto = sproto;
371 tdbi->spi = sav->spi;
372
373 m_tag_prepend(m, mtag);
374 } else {
375 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
376 /* XXX do we need to mark m_flags??? */
377 }
378
379 key_sa_recordxfer(sav, m); /* record data transfer */
380
381 /*
382 * Re-dispatch via software interrupt.
383 */
384 if (!IF_HANDOFF(&ipintrq, m, NULL)) {
385 IPSEC_ISTAT(sproto, espstat.esps_qfull, ahstat.ahs_qfull,
386 ipcompstat.ipcomps_qfull);
387
388 DPRINTF(("ipsec4_common_input_cb: queue full; "
389 "proto %u packet dropped\n", sproto));
390 return ENOBUFS;
391 }
392 schednetisr(NETISR_IP);
393 return 0;
394 bad:
395 m_freem(m);
396 return error;
397 }
398 #endif /* INET */
399
400 #ifdef INET6
401 /* IPv6 AH wrapper. */
402 int
403 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
404 {
405 int l = 0;
406 int protoff;
407 struct ip6_ext ip6e;
408
409 if (*offp < sizeof(struct ip6_hdr)) {
410 DPRINTF(("ipsec6_common_input: bad offset %u\n", *offp));
411 return IPPROTO_DONE;
412 } else if (*offp == sizeof(struct ip6_hdr)) {
413 protoff = offsetof(struct ip6_hdr, ip6_nxt);
414 } else {
415 /* Chase down the header chain... */
416 protoff = sizeof(struct ip6_hdr);
417
418 do {
419 protoff += l;
420 m_copydata(*mp, protoff, sizeof(ip6e),
421 (caddr_t) &ip6e);
422
423 if (ip6e.ip6e_nxt == IPPROTO_AH)
424 l = (ip6e.ip6e_len + 2) << 2;
425 else
426 l = (ip6e.ip6e_len + 1) << 3;
427 IPSEC_ASSERT(l > 0, ("ah6_input: l went zero or negative"));
428 } while (protoff + l < *offp);
429
430 /* Malformed packet check */
431 if (protoff + l != *offp) {
432 DPRINTF(("ipsec6_common_input: bad packet header chain, "
433 "protoff %u, l %u, off %u\n", protoff, l, *offp));
434 IPSEC_ISTAT(proto, espstat.esps_hdrops,
435 ahstat.ahs_hdrops,
436 ipcompstat.ipcomps_hdrops);
437 m_freem(*mp);
438 *mp = NULL;
439 return IPPROTO_DONE;
440 }
441 protoff += offsetof(struct ip6_ext, ip6e_nxt);
442 }
443 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
444 return IPPROTO_DONE;
445 }
446
447 void
448 esp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
449 {
450 if (sa->sa_family != AF_INET6 ||
451 sa->sa_len != sizeof(struct sockaddr_in6))
452 return;
453 if ((unsigned)cmd >= PRC_NCMDS)
454 return;
455
456 /* if the parameter is from icmp6, decode it. */
457 if (d != NULL) {
458 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
459 struct mbuf *m = ip6cp->ip6c_m;
460 int off = ip6cp->ip6c_off;
461
462 struct ip6ctlparam ip6cp1;
463
464 /*
465 * Notify the error to all possible sockets via pfctlinput2.
466 * Since the upper layer information (such as protocol type,
467 * source and destination ports) is embedded in the encrypted
468 * data and might have been cut, we can't directly call
469 * an upper layer ctlinput function. However, the pcbnotify
470 * function will consider source and destination addresses
471 * as well as the flow info value, and may be able to find
472 * some PCB that should be notified.
473 * Although pfctlinput2 will call esp6_ctlinput(), there is
474 * no possibility of an infinite loop of function calls,
475 * because we don't pass the inner IPv6 header.
476 */
477 bzero(&ip6cp1, sizeof(ip6cp1));
478 ip6cp1.ip6c_src = ip6cp->ip6c_src;
479 pfctlinput2(cmd, sa, (void *)&ip6cp1);
480
481 /*
482 * Then go to special cases that need ESP header information.
483 * XXX: We assume that when ip6 is non NULL,
484 * M and OFF are valid.
485 */
486
487 if (cmd == PRC_MSGSIZE) {
488 struct secasvar *sav;
489 u_int32_t spi;
490 int valid;
491
492 /* check header length before using m_copydata */
493 if (m->m_pkthdr.len < off + sizeof (struct esp))
494 return;
495 m_copydata(m, off + offsetof(struct esp, esp_spi),
496 sizeof(u_int32_t), (caddr_t) &spi);
497 /*
498 * Check to see if we have a valid SA corresponding to
499 * the address in the ICMP message payload.
500 */
501 sav = KEY_ALLOCSA((union sockaddr_union *)sa,
502 IPPROTO_ESP, spi);
503 valid = (sav != NULL);
504 if (sav)
505 KEY_FREESAV(&sav);
506
507 /* XXX Further validation? */
508
509 /*
510 * Depending on whether the SA is "valid" and
511 * routing table size (mtudisc_{hi,lo}wat), we will:
512 * - recalcurate the new MTU and create the
513 * corresponding routing entry, or
514 * - ignore the MTU change notification.
515 */
516 icmp6_mtudisc_update(ip6cp, valid);
517 }
518 } else {
519 /* we normally notify any pcb here */
520 }
521 }
522
523 extern struct ip6protosw inet6sw[];
524 extern u_char ip6_protox[];
525
526 /*
527 * IPsec input callback, called by the transform callback. Takes care of
528 * filtering and other sanity checks on the processed packet.
529 */
530 int
531 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip, int protoff,
532 struct m_tag *mt)
533 {
534 int prot, af, sproto;
535 struct ip6_hdr *ip6;
536 struct m_tag *mtag;
537 struct tdb_ident *tdbi;
538 struct secasindex *saidx;
539 int nxt;
540 u_int8_t nxt8;
541 int error, nest;
542
543 IPSEC_ASSERT(m != NULL, ("ipsec6_common_input_cb: null mbuf"));
544 IPSEC_ASSERT(sav != NULL, ("ipsec6_common_input_cb: null SA"));
545 IPSEC_ASSERT(sav->sah != NULL, ("ipsec6_common_input_cb: null SAH"));
546 saidx = &sav->sah->saidx;
547 af = saidx->dst.sa.sa_family;
548 IPSEC_ASSERT(af == AF_INET6,
549 ("ipsec6_common_input_cb: unexpected af %u", af));
550 sproto = saidx->proto;
551 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
552 sproto == IPPROTO_IPCOMP,
553 ("ipsec6_common_input_cb: unexpected security protocol %u",
554 sproto));
555
556 /* Sanity check */
557 if (m == NULL) {
558 DPRINTF(("ipsec4_common_input_cb: null mbuf"));
559 IPSEC_ISTAT(sproto, espstat.esps_badkcr, ahstat.ahs_badkcr,
560 ipcompstat.ipcomps_badkcr);
561 error = EINVAL;
562 goto bad;
563 }
564
565 /* Fix IPv6 header */
566 if (m->m_len < sizeof(struct ip6_hdr) &&
567 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
568
569 DPRINTF(("ipsec_common_input_cb: processing failed "
570 "for SA %s/%08lx\n", ipsec_address(&sav->sah->saidx.dst),
571 (u_long) ntohl(sav->spi)));
572
573 IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops,
574 ipcompstat.ipcomps_hdrops);
575 error = EACCES;
576 goto bad;
577 }
578
579 ip6 = mtod(m, struct ip6_hdr *);
580 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
581
582 /* Save protocol */
583 m_copydata(m, protoff, 1, (unsigned char *) &prot);
584
585 #ifdef INET
586 /* IP-in-IP encapsulation */
587 if (prot == IPPROTO_IPIP) {
588 struct ip ipn;
589
590 /* ipn will now contain the inner IPv4 header */
591 m_copydata(m, skip, sizeof(struct ip), (caddr_t) &ipn);
592
593 #ifdef notyet
594 /*
595 * Check that the inner source address is the same as
596 * the proxy address, if available.
597 */
598 if ((saidx->proxy.sa.sa_family == AF_INET &&
599 saidx->proxy.sin.sin_addr.s_addr != INADDR_ANY &&
600 ipn.ip_src.s_addr != saidx->proxy.sin.sin_addr.s_addr) ||
601 (saidx->proxy.sa.sa_family != AF_INET &&
602 saidx->proxy.sa.sa_family != 0)) {
603
604 DPRINTF(("ipsec_common_input_cb: inner "
605 "source address %s doesn't correspond to "
606 "expected proxy source %s, SA %s/%08lx\n",
607 inet_ntoa4(ipn.ip_src),
608 ipsec_address(&saidx->proxy),
609 ipsec_address(&saidx->dst),
610 (u_long) ntohl(sav->spi)));
611
612 IPSEC_ISTATsproto, (espstat.esps_pdrops,
613 ahstat.ahs_pdrops, ipcompstat.ipcomps_pdrops);
614 error = EACCES;
615 goto bad;
616 }
617 #endif /*XXX*/
618 }
619 #endif /* INET */
620
621 /* IPv6-in-IP encapsulation */
622 if (prot == IPPROTO_IPV6) {
623 struct ip6_hdr ip6n;
624
625 /* ip6n will now contain the inner IPv6 header. */
626 m_copydata(m, skip, sizeof(struct ip6_hdr),
627 (caddr_t) &ip6n);
628
629 #ifdef notyet
630 /*
631 * Check that the inner source address is the same as
632 * the proxy address, if available.
633 */
634 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
635 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
636 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
637 &saidx->proxy.sin6.sin6_addr)) ||
638 (saidx->proxy.sa.sa_family != AF_INET6 &&
639 saidx->proxy.sa.sa_family != 0)) {
640
641 DPRINTF(("ipsec_common_input_cb: inner "
642 "source address %s doesn't correspond to "
643 "expected proxy source %s, SA %s/%08lx\n",
644 ip6_sprintf(&ip6n.ip6_src),
645 ipsec_address(&saidx->proxy),
646 ipsec_address(&saidx->dst),
647 (u_long) ntohl(sav->spi)));
648
649 IPSEC_ISTAT(sproto, espstat.esps_pdrops,
650 ahstat.ahs_pdrops, ipcompstat.ipcomps_pdrops);
651 error = EACCES;
652 goto bad;
653 }
654 #endif /*XXX*/
655 }
656
657 /*
658 * Record what we've done to the packet (under what SA it was
659 * processed). If we've been passed an mtag, it means the packet
660 * was already processed by an ethernet/crypto combo card and
661 * thus has a tag attached with all the right information, but
662 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
663 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
664 */
665 if (mt == NULL && sproto != IPPROTO_IPCOMP) {
666 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
667 sizeof(struct tdb_ident), M_NOWAIT);
668 if (mtag == NULL) {
669 DPRINTF(("ipsec_common_input_cb: failed to "
670 "get tag\n"));
671 IPSEC_ISTAT(sproto, espstat.esps_hdrops,
672 ahstat.ahs_hdrops, ipcompstat.ipcomps_hdrops);
673 error = ENOMEM;
674 goto bad;
675 }
676
677 tdbi = (struct tdb_ident *)(mtag + 1);
678 bcopy(&saidx->dst, &tdbi->dst, sizeof(union sockaddr_union));
679 tdbi->proto = sproto;
680 tdbi->spi = sav->spi;
681
682 m_tag_prepend(m, mtag);
683 } else {
684 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
685 /* XXX do we need to mark m_flags??? */
686 }
687
688 key_sa_recordxfer(sav, m);
689
690 /* Retrieve new protocol */
691 m_copydata(m, protoff, sizeof(u_int8_t), (caddr_t) &nxt8);
692
693 /*
694 * See the end of ip6_input for this logic.
695 * IPPROTO_IPV[46] case will be processed just like other ones
696 */
697 nest = 0;
698 nxt = nxt8;
699 while (nxt != IPPROTO_DONE) {
700 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
701 ip6stat.ip6s_toomanyhdr++;
702 error = EINVAL;
703 goto bad;
704 }
705
706 /*
707 * Protection against faulty packet - there should be
708 * more sanity checks in header chain processing.
709 */
710 if (m->m_pkthdr.len < skip) {
711 ip6stat.ip6s_tooshort++;
712 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
713 error = EINVAL;
714 goto bad;
715 }
716 /*
717 * Enforce IPsec policy checking if we are seeing last header.
718 * note that we do not visit this with protocols with pcb layer
719 * code - like udp/tcp/raw ip.
720 */
721 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
722 ipsec6_in_reject(m, NULL)) {
723 error = EINVAL;
724 goto bad;
725 }
726 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
727 }
728 return 0;
729 bad:
730 if (m)
731 m_freem(m);
732 return error;
733 }
734 #endif /* INET6 */
735