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