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