ipsec_input.c revision 1.17 1 /* $NetBSD: ipsec_input.c,v 1.17 2007/06/27 20:38:33 degroote 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.17 2007/06/27 20:38:33 degroote 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 #include <net/netisr.h>
67
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/ip_var.h>
72 #include <netinet/in_var.h>
73
74 #include <netinet/ip6.h>
75 #ifdef INET6
76 #include <netinet6/ip6_var.h>
77 #endif
78 #include <netinet/in_pcb.h>
79 #ifdef INET6
80 #include <netinet/icmp6.h>
81 #endif
82
83 #include <netipsec/ipsec.h>
84 #ifdef INET6
85 #include <netipsec/ipsec6.h>
86 #endif
87 #include <netipsec/ah_var.h>
88 #include <netipsec/esp.h>
89 #include <netipsec/esp_var.h>
90 #include <netipsec/ipcomp_var.h>
91
92 #include <netipsec/key.h>
93 #include <netipsec/keydb.h>
94
95 #include <netipsec/xform.h>
96 #include <netinet6/ip6protosw.h>
97
98 #include <netipsec/ipsec_osdep.h>
99
100 #include <machine/stdarg.h>
101
102 #include <net/net_osdep.h>
103
104 #define IPSEC_ISTAT(p,x,y,z) ((p) == IPPROTO_ESP ? (x)++ : \
105 (p) == IPPROTO_AH ? (y)++ : (z)++)
106
107 /*
108 * ipsec_common_input gets called when an IPsec-protected packet
109 * is received by IPv4 or IPv6. It's job is to find the right SA
110 # and call the appropriate transform. The transform callback
111 * takes care of further processing (like ingress filtering).
112 */
113 static int
114 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
115 {
116 union sockaddr_union dst_address;
117 struct secasvar *sav;
118 u_int32_t spi;
119 u_int16_t sport = 0;
120 u_int16_t dport = 0;
121 int s, error;
122 #ifdef IPSEC_NAT_T
123 struct m_tag * tag = NULL;
124 #endif
125
126 IPSEC_ISTAT(sproto, espstat.esps_input, ahstat.ahs_input,
127 ipcompstat.ipcomps_input);
128
129 IPSEC_ASSERT(m != NULL, ("ipsec_common_input: null packet"));
130
131 if ((sproto == IPPROTO_ESP && !esp_enable) ||
132 (sproto == IPPROTO_AH && !ah_enable) ||
133 (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) {
134 m_freem(m);
135 IPSEC_ISTAT(sproto, espstat.esps_pdrops, ahstat.ahs_pdrops,
136 ipcompstat.ipcomps_pdrops);
137 return EOPNOTSUPP;
138 }
139
140 if (m->m_pkthdr.len - skip < 2 * sizeof (u_int32_t)) {
141 m_freem(m);
142 IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops,
143 ipcompstat.ipcomps_hdrops);
144 DPRINTF(("ipsec_common_input: packet too small\n"));
145 return EINVAL;
146 }
147
148 /* Retrieve the SPI from the relevant IPsec header */
149 if (sproto == IPPROTO_ESP)
150 m_copydata(m, skip, sizeof(u_int32_t), &spi);
151 else if (sproto == IPPROTO_AH)
152 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t), &spi);
153 else if (sproto == IPPROTO_IPCOMP) {
154 u_int16_t cpi;
155 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t), &cpi);
156 spi = ntohl(htons(cpi));
157 } else {
158 panic("ipsec_common_input called with bad protocol number :"
159 "%d\n", sproto);
160 }
161
162
163 #ifdef IPSEC_NAT_T
164 /* find the source port for NAT-T */
165 if ((tag = m_tag_find(m, PACKET_TAG_IPSEC_NAT_T_PORTS, NULL))) {
166 sport = ((u_int16_t *)(tag + 1))[0];
167 dport = ((u_int16_t *)(tag + 1))[1];
168 }
169 #endif
170
171 /*
172 * Find the SA and (indirectly) call the appropriate
173 * kernel crypto routine. The resulting mbuf chain is a valid
174 * IP packet ready to go through input processing.
175 */
176 bzero(&dst_address, sizeof (dst_address));
177 dst_address.sa.sa_family = af;
178 switch (af) {
179 #ifdef INET
180 case AF_INET:
181 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
182 m_copydata(m, offsetof(struct ip, ip_dst),
183 sizeof(struct in_addr),
184 &dst_address.sin.sin_addr);
185 break;
186 #endif /* INET */
187 #ifdef INET6
188 case AF_INET6:
189 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
190 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
191 sizeof(struct in6_addr),
192 &dst_address.sin6.sin6_addr);
193 break;
194 #endif /* INET6 */
195 default:
196 DPRINTF(("ipsec_common_input: unsupported protocol "
197 "family %u\n", af));
198 m_freem(m);
199 IPSEC_ISTAT(sproto, espstat.esps_nopf, ahstat.ahs_nopf,
200 ipcompstat.ipcomps_nopf);
201 return EPFNOSUPPORT;
202 }
203
204 s = splsoftnet();
205
206 /* NB: only pass dst since key_allocsa follows RFC2401 */
207 sav = KEY_ALLOCSA(&dst_address, sproto, spi, sport, dport);
208 if (sav == NULL) {
209 DPRINTF(("ipsec_common_input: no key association found for"
210 " SA %s/%08lx/%u/%u\n",
211 ipsec_address(&dst_address),
212 (u_long) ntohl(spi), sproto, ntohs(dport)));
213 IPSEC_ISTAT(sproto, espstat.esps_notdb, ahstat.ahs_notdb,
214 ipcompstat.ipcomps_notdb);
215 splx(s);
216 m_freem(m);
217 return ENOENT;
218 }
219
220 if (sav->tdb_xform == NULL) {
221 DPRINTF(("ipsec_common_input: attempted to use uninitialized"
222 " SA %s/%08lx/%u\n",
223 ipsec_address(&dst_address),
224 (u_long) ntohl(spi), sproto));
225 IPSEC_ISTAT(sproto, espstat.esps_noxform, ahstat.ahs_noxform,
226 ipcompstat.ipcomps_noxform);
227 KEY_FREESAV(&sav);
228 splx(s);
229 m_freem(m);
230 return ENXIO;
231 }
232
233 /*
234 * Call appropriate transform and return -- callback takes care of
235 * everything else.
236 */
237 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
238 KEY_FREESAV(&sav);
239 splx(s);
240 return error;
241 }
242
243 #ifdef INET
244 /*
245 * Common input handler for IPv4 AH, ESP, and IPCOMP.
246 */
247 void
248 ipsec4_common_input(struct mbuf *m, ...)
249 {
250 va_list ap;
251 int off, nxt;
252
253 va_start(ap, m);
254 off = va_arg(ap, int);
255 nxt = va_arg(ap, int);
256 va_end(ap);
257
258 (void) ipsec_common_input(m, off, offsetof(struct ip, ip_p),
259 AF_INET, nxt);
260 }
261
262 /*
263 * IPsec input callback for INET protocols.
264 * This routine is called as the transform callback.
265 * Takes care of filtering and other sanity checks on
266 * the processed packet.
267 */
268 int
269 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
270 int skip, int protoff, struct m_tag *mt)
271 {
272 int prot, af, sproto;
273 struct ip *ip;
274 struct m_tag *mtag;
275 struct tdb_ident *tdbi;
276 struct secasindex *saidx;
277 int error;
278
279 IPSEC_SPLASSERT_SOFTNET("ipsec4_common_input_cb");
280
281 IPSEC_ASSERT(m != NULL, ("ipsec4_common_input_cb: null mbuf"));
282 IPSEC_ASSERT(sav != NULL, ("ipsec4_common_input_cb: null SA"));
283 IPSEC_ASSERT(sav->sah != NULL, ("ipsec4_common_input_cb: null SAH"));
284 saidx = &sav->sah->saidx;
285 af = saidx->dst.sa.sa_family;
286 IPSEC_ASSERT(af == AF_INET, ("ipsec4_common_input_cb: unexpected af %u",af));
287 sproto = saidx->proto;
288 IPSEC_ASSERT(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
289 sproto == IPPROTO_IPCOMP,
290 ("ipsec4_common_input_cb: unexpected security protocol %u",
291 sproto));
292
293 /* Sanity check */
294 if (m == NULL) {
295 DPRINTF(("ipsec4_common_input_cb: null mbuf"));
296 IPSEC_ISTAT(sproto, espstat.esps_badkcr, ahstat.ahs_badkcr,
297 ipcompstat.ipcomps_badkcr);
298 KEY_FREESAV(&sav);
299 return EINVAL;
300 }
301
302 if (skip != 0) {
303 /* Fix IPv4 header */
304 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
305 DPRINTF(("ipsec4_common_input_cb: processing failed "
306 "for SA %s/%08lx\n",
307 ipsec_address(&sav->sah->saidx.dst),
308 (u_long) ntohl(sav->spi)));
309 IPSEC_ISTAT(sproto, espstat.esps_hdrops, ahstat.ahs_hdrops,
310 ipcompstat.ipcomps_hdrops);
311 error = ENOBUFS;
312 goto bad;
313 }
314
315 ip = mtod(m, struct ip *);
316 ip->ip_len = htons(m->m_pkthdr.len);
317 #ifdef __FreeBSD__
318 /* On FreeBSD, ip_off and ip_len assumed in host endian. */
319 ip->ip_off = htons(ip->ip_off);
320 #endif
321 ip->ip_sum = 0;
322 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
323 } else {
324 ip = mtod(m, struct ip *);
325 }
326 prot = ip->ip_p;
327
328 /* IP-in-IP encapsulation */
329 if (prot == IPPROTO_IPIP) {
330 struct ip ipn;
331
332 /* ipn will now contain the inner IPv4 header */
333 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip), &ipn);
334
335 #ifdef notyet
336 /* XXX PROXY address isn't recorded in SAH */
337 /*
338 * Check that the inner source address is the same as
339 * the proxy address, if available.
340 */
341 if ((saidx->proxy.sa.sa_family == AF_INET &&
342 saidx->proxy.sin.sin_addr.s_addr !=
343 INADDR_ANY &&
344 ipn.ip_src.s_addr !=
345 saidx->proxy.sin.sin_addr.s_addr) ||
346 (saidx->proxy.sa.sa_family != AF_INET &&
347 saidx->proxy.sa.sa_family != 0)) {
348
349 DPRINTF(("ipsec4_common_input_cb: inner "
350 "source address %s doesn't correspond to "
351 "expected proxy source %s, SA %s/%08lx\n",
352 inet_ntoa4(ipn.ip_src),
353 ipsp_address(saidx->proxy),
354 ipsp_address(saidx->dst),
355 (u_long) ntohl(sav->spi)));
356
357 IPSEC_ISTAT(sproto, espstat.esps_pdrops,
358 ahstat.ahs_pdrops,
359 ipcompstat.ipcomps_pdrops);
360 error = EACCES;
361 goto bad;
362 }
363 #endif /*XXX*/
364 }
365 #if INET6
366 /* IPv6-in-IP encapsulation. */
367 if (prot == IPPROTO_IPV6) {
368 struct ip6_hdr ip6n;
369
370 /* ip6n will now contain the inner IPv6 header. */
371 m_copydata(m, ip->ip_hl << 2, sizeof(struct ip6_hdr), &ip6n);
372
373 #ifdef notyet
374 /*
375 * Check that the inner source address is the same as
376 * the proxy address, if available.
377 */
378 if ((saidx->proxy.sa.sa_family == AF_INET6 &&
379 !IN6_IS_ADDR_UNSPECIFIED(&saidx->proxy.sin6.sin6_addr) &&
380 !IN6_ARE_ADDR_EQUAL(&ip6n.ip6_src,
381 &saidx->proxy.sin6.sin6_addr)) ||
382 (saidx->proxy.sa.sa_family != AF_INET6 &&
383 saidx->proxy.sa.sa_family != 0)) {
384
385 DPRINTF(("ipsec4_common_input_cb: inner "
386 "source address %s doesn't correspond to "
387 "expected proxy source %s, SA %s/%08lx\n",
388 ip6_sprintf(&ip6n.ip6_src),
389 ipsec_address(&saidx->proxy),
390 ipsec_address(&saidx->dst),
391 (u_long) ntohl(sav->spi)));
392
393 IPSEC_ISTAT(sproto, espstat.esps_pdrops,
394 ahstat.ahs_pdrops,
395 ipcompstat.ipcomps_pdrops);
396 error = EACCES;
397 goto bad;
398 }
399 #endif /*XXX*/
400 }
401 #endif /* INET6 */
402
403 /*
404 * Record what we've done to the packet (under what SA it was
405 * processed). If we've been passed an mtag, it means the packet
406 * was already processed by an ethernet/crypto combo card and
407 * thus has a tag attached with all the right information, but
408 * with a PACKET_TAG_IPSEC_IN_CRYPTO_DONE as opposed to
409 * PACKET_TAG_IPSEC_IN_DONE type; in that case, just change the type.
410 */
411 if (mt == NULL && sproto != IPPROTO_IPCOMP) {
412 mtag = m_tag_get(PACKET_TAG_IPSEC_IN_DONE,
413 sizeof(struct tdb_ident), M_NOWAIT);
414 if (mtag == NULL) {
415 DPRINTF(("ipsec4_common_input_cb: failed to get tag\n"));
416 IPSEC_ISTAT(sproto, espstat.esps_hdrops,
417 ahstat.ahs_hdrops, ipcompstat.ipcomps_hdrops);
418 error = ENOMEM;
419 goto bad;
420 }
421
422 tdbi = (struct tdb_ident *)(mtag + 1);
423 bcopy(&saidx->dst, &tdbi->dst, saidx->dst.sa.sa_len);
424 tdbi->proto = sproto;
425 tdbi->spi = sav->spi;
426
427 m_tag_prepend(m, mtag);
428 } else {
429 if (mt != NULL)
430 mt->m_tag_id = PACKET_TAG_IPSEC_IN_DONE;
431 /* XXX do we need to mark m_flags??? */
432 }
433
434 key_sa_recordxfer(sav, m); /* record data transfer */
435
436 /*
437 * Re-dispatch via software interrupt.
438 */
439 if (!IF_HANDOFF(&ipintrq, m, NULL)) {
440 IPSEC_ISTAT(sproto, espstat.esps_qfull, ahstat.ahs_qfull,
441 ipcompstat.ipcomps_qfull);
442
443 DPRINTF(("ipsec4_common_input_cb: queue full; "
444 "proto %u packet dropped\n", sproto));
445 return ENOBUFS;
446 }
447 schednetisr(NETISR_IP);
448 return 0;
449 bad:
450 m_freem(m);
451 return error;
452 }
453 #endif /* INET */
454
455 #ifdef INET6
456 /* IPv6 AH wrapper. */
457 int
458 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
459 {
460 int l = 0;
461 int protoff;
462 struct ip6_ext ip6e;
463
464 if (*offp < sizeof(struct ip6_hdr)) {
465 DPRINTF(("ipsec6_common_input: bad offset %u\n", *offp));
466 return IPPROTO_DONE;
467 } else if (*offp == sizeof(struct ip6_hdr)) {
468 protoff = offsetof(struct ip6_hdr, ip6_nxt);
469 } else {
470 /* Chase down the header chain... */
471 protoff = sizeof(struct ip6_hdr);
472
473 do {
474 protoff += l;
475 m_copydata(*mp, protoff, sizeof(ip6e), &ip6e);
476
477 if (ip6e.ip6e_nxt == IPPROTO_AH)
478 l = (ip6e.ip6e_len + 2) << 2;
479 else
480 l = (ip6e.ip6e_len + 1) << 3;
481 IPSEC_ASSERT(l > 0, ("ah6_input: l went zero or negative"));
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, espstat.esps_hdrops,
489 ahstat.ahs_hdrops,
490 ipcompstat.ipcomps_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 bzero(&ip6cp1, 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 prot, af, 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 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, espstat.esps_badkcr, ahstat.ahs_badkcr,
621 ipcompstat.ipcomps_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, espstat.esps_hdrops, ahstat.ahs_hdrops,
635 ipcompstat.ipcomps_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, (unsigned char *) &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, espstat.esps_pdrops,
674 ahstat.ahs_pdrops, ipcompstat.ipcomps_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, espstat.esps_pdrops,
710 ahstat.ahs_pdrops, ipcompstat.ipcomps_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, espstat.esps_hdrops,
732 ahstat.ahs_hdrops, ipcompstat.ipcomps_hdrops);
733 error = ENOMEM;
734 goto bad;
735 }
736
737 tdbi = (struct tdb_ident *)(mtag + 1);
738 bcopy(&saidx->dst, &tdbi->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 ip6stat.ip6s_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 ip6stat.ip6s_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