1 /* $FreeBSD: releng/6.3/sys/netinet6/esp_input.c 139826 2005-01-07 02:30:35Z imp $ */
2 /* $KAME: esp_input.c,v 1.62 2002/01/07 11:39:57 kjc Exp $ */
3
4 /*-
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * RFC1827/2406 Encapsulated Security Payload.
35 */
36
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/errno.h>
47 #include <sys/time.h>
48 #include <sys/syslog.h>
49
50 #include <net/if.h>
51 #include <net/route.h>
52 #include <net/netisr.h>
53 #include <machine/cpu.h>
54
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/ip_var.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip_ecn.h>
61 #ifdef INET6
62 #include <netinet6/ip6_ecn.h>
63 #endif
64
65 #ifdef INET6
66 #include <netinet/ip6.h>
67 #include <netinet/in_pcb.h>
68 #include <netinet6/in6_pcb.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet/icmp6.h>
71 #include <netinet6/ip6protosw.h>
72 #endif
73
74 #include <netinet6/ipsec.h>
75 #ifdef INET6
76 #include <netinet6/ipsec6.h>
77 #endif
78 #include <netinet6/ah.h>
79 #ifdef INET6
80 #include <netinet6/ah6.h>
81 #endif
82 #include <netinet6/esp.h>
83 #ifdef INET6
84 #include <netinet6/esp6.h>
85 #endif
86 #include <netkey/key.h>
87 #include <netkey/keydb.h>
88 #include <netkey/key_debug.h>
89
90 #include <machine/stdarg.h>
91
92 #include <net/net_osdep.h>
93
94 #define IPLEN_FLIPPED
95
96 #define ESPMAXLEN \
97 (sizeof(struct esp) < sizeof(struct newesp) \
98 ? sizeof(struct newesp) : sizeof(struct esp))
99
100 #ifdef INET
101 extern struct protosw inetsw[];
102
103 void
104 esp4_input(m, off)
105 struct mbuf *m;
106 int off;
107 {
108 struct ip *ip;
109 struct esp *esp;
110 struct esptail esptail;
111 u_int32_t spi;
112 struct secasvar *sav = NULL;
113 size_t taillen;
114 u_int16_t nxt;
115 const struct esp_algorithm *algo;
116 int ivlen;
117 size_t hlen;
118 size_t esplen;
119
120 /* sanity check for alignment. */
121 if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
122 ipseclog((LOG_ERR, "IPv4 ESP input: packet alignment problem "
123 "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
124 ipsecstat.in_inval++;
125 goto bad;
126 }
127
128 if (m->m_len < off + ESPMAXLEN) {
129 m = m_pullup(m, off + ESPMAXLEN);
130 if (!m) {
131 ipseclog((LOG_DEBUG,
132 "IPv4 ESP input: can't pullup in esp4_input\n"));
133 ipsecstat.in_inval++;
134 goto bad;
135 }
136 }
137
138 ip = mtod(m, struct ip *);
139 esp = (struct esp *)(((u_int8_t *)ip) + off);
140 #ifdef _IP_VHL
141 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
142 #else
143 hlen = ip->ip_hl << 2;
144 #endif
145
146 /* find the sassoc. */
147 spi = esp->esp_spi;
148
149 if ((sav = key_allocsa(AF_INET,
150 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst,
151 IPPROTO_ESP, spi)) == 0) {
152 ipseclog((LOG_WARNING,
153 "IPv4 ESP input: no key association found for spi %u\n",
154 (u_int32_t)ntohl(spi)));
155 ipsecstat.in_nosa++;
156 goto bad;
157 }
158 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
159 printf("DP esp4_input called to allocate SA:%p\n", sav));
160 if (sav->state != SADB_SASTATE_MATURE
161 && sav->state != SADB_SASTATE_DYING) {
162 ipseclog((LOG_DEBUG,
163 "IPv4 ESP input: non-mature/dying SA found for spi %u\n",
164 (u_int32_t)ntohl(spi)));
165 ipsecstat.in_badspi++;
166 goto bad;
167 }
168 algo = esp_algorithm_lookup(sav->alg_enc);
169 if (!algo) {
170 ipseclog((LOG_DEBUG, "IPv4 ESP input: "
171 "unsupported encryption algorithm for spi %u\n",
172 (u_int32_t)ntohl(spi)));
173 ipsecstat.in_badspi++;
174 goto bad;
175 }
176
177 /* check if we have proper ivlen information */
178 ivlen = sav->ivlen;
179 if (ivlen < 0) {
180 ipseclog((LOG_ERR, "inproper ivlen in IPv4 ESP input: %s %s\n",
181 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
182 ipsecstat.in_inval++;
183 goto bad;
184 }
185
186 if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
187 && (sav->alg_auth && sav->key_auth)))
188 goto noreplaycheck;
189
190 if (sav->alg_auth == SADB_X_AALG_NULL ||
191 sav->alg_auth == SADB_AALG_NONE)
192 goto noreplaycheck;
193
194 /*
195 * check for sequence number.
196 */
197 if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
198 ; /* okey */
199 else {
200 ipsecstat.in_espreplay++;
201 ipseclog((LOG_WARNING,
202 "replay packet in IPv4 ESP input: %s %s\n",
203 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
204 goto bad;
205 }
206
207 /* check ICV */
208 {
209 u_int8_t sum0[AH_MAXSUMSIZE];
210 u_int8_t sum[AH_MAXSUMSIZE];
211 const struct ah_algorithm *sumalgo;
212 size_t siz;
213
214 sumalgo = ah_algorithm_lookup(sav->alg_auth);
215 if (!sumalgo)
216 goto noreplaycheck;
217 siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
218 if (m->m_pkthdr.len < off + ESPMAXLEN + siz) {
219 ipsecstat.in_inval++;
220 goto bad;
221 }
222 if (AH_MAXSUMSIZE < siz) {
223 ipseclog((LOG_DEBUG,
224 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
225 (u_long)siz));
226 ipsecstat.in_inval++;
227 goto bad;
228 }
229
230 m_copydata(m, m->m_pkthdr.len - siz, siz, (caddr_t)&sum0[0]);
231
232 if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
233 ipseclog((LOG_WARNING, "auth fail in IPv4 ESP input: %s %s\n",
234 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
235 ipsecstat.in_espauthfail++;
236 goto bad;
237 }
238
239 if (bcmp(sum0, sum, siz) != 0) {
240 ipseclog((LOG_WARNING, "auth fail in IPv4 ESP input: %s %s\n",
241 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
242 ipsecstat.in_espauthfail++;
243 goto bad;
244 }
245
246 /* strip off the authentication data */
247 m_adj(m, -siz);
248 ip = mtod(m, struct ip *);
249 #ifdef IPLEN_FLIPPED
250 ip->ip_len = ip->ip_len - siz;
251 #else
252 ip->ip_len = htons(ntohs(ip->ip_len) - siz);
253 #endif
254 m->m_flags |= M_AUTHIPDGM;
255 ipsecstat.in_espauthsucc++;
256 }
257
258 /*
259 * update sequence number.
260 */
261 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
262 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
263 ipsecstat.in_espreplay++;
264 goto bad;
265 }
266 }
267
268 noreplaycheck:
269
270 /* process main esp header. */
271 if (sav->flags & SADB_X_EXT_OLD) {
272 /* RFC 1827 */
273 esplen = sizeof(struct esp);
274 } else {
275 /* RFC 2406 */
276 if (sav->flags & SADB_X_EXT_DERIV)
277 esplen = sizeof(struct esp);
278 else
279 esplen = sizeof(struct newesp);
280 }
281
282 if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
283 ipseclog((LOG_WARNING,
284 "IPv4 ESP input: packet too short\n"));
285 ipsecstat.in_inval++;
286 goto bad;
287 }
288
289 if (m->m_len < off + esplen + ivlen) {
290 m = m_pullup(m, off + esplen + ivlen);
291 if (!m) {
292 ipseclog((LOG_DEBUG,
293 "IPv4 ESP input: can't pullup in esp4_input\n"));
294 ipsecstat.in_inval++;
295 goto bad;
296 }
297 }
298
299 /*
300 * pre-compute and cache intermediate key
301 */
302 if (esp_schedule(algo, sav) != 0) {
303 ipsecstat.in_inval++;
304 goto bad;
305 }
306
307 /*
308 * decrypt the packet.
309 */
310 if (!algo->decrypt)
311 panic("internal error: no decrypt function");
312 if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
313 /* m is already freed */
314 m = NULL;
315 ipseclog((LOG_ERR, "decrypt fail in IPv4 ESP input: %s\n",
316 ipsec_logsastr(sav)));
317 ipsecstat.in_inval++;
318 goto bad;
319 }
320 ipsecstat.in_esphist[sav->alg_enc]++;
321
322 m->m_flags |= M_DECRYPTED;
323
324 /*
325 * find the trailer of the ESP.
326 */
327 m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
328 (caddr_t)&esptail);
329 nxt = esptail.esp_nxt;
330 taillen = esptail.esp_padlen + sizeof(esptail);
331
332 if (m->m_pkthdr.len < taillen ||
333 m->m_pkthdr.len - taillen < off + esplen + ivlen + sizeof(esptail)) {
334 ipseclog((LOG_WARNING,
335 "bad pad length in IPv4 ESP input: %s %s\n",
336 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
337 ipsecstat.in_inval++;
338 goto bad;
339 }
340
341 /* strip off the trailing pad area. */
342 m_adj(m, -taillen);
343
344 #ifdef IPLEN_FLIPPED
345 ip->ip_len = ip->ip_len - taillen;
346 #else
347 ip->ip_len = htons(ntohs(ip->ip_len) - taillen);
348 #endif
349
350 /* was it transmitted over the IPsec tunnel SA? */
351 if (ipsec4_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
352 /*
353 * strip off all the headers that precedes ESP header.
354 * IP4 xx ESP IP4' payload -> IP4' payload
355 *
356 * XXX more sanity checks
357 * XXX relationship with gif?
358 */
359 u_int8_t tos;
360
361 tos = ip->ip_tos;
362 m_adj(m, off + esplen + ivlen);
363 if (m->m_len < sizeof(*ip)) {
364 m = m_pullup(m, sizeof(*ip));
365 if (!m) {
366 ipsecstat.in_inval++;
367 goto bad;
368 }
369 }
370 ip = mtod(m, struct ip *);
371 /* ECN consideration. */
372 if (!ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos)) {
373 ipsecstat.in_inval++;
374 goto bad;
375 }
376 if (!key_checktunnelsanity(sav, AF_INET,
377 (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) {
378 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
379 "in IPv4 ESP input: %s %s\n",
380 ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
381 ipsecstat.in_inval++;
382 goto bad;
383 }
384
385 key_sa_recordxfer(sav, m);
386 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
387 ipsec_addhist(m, IPPROTO_IPV4, 0) != 0) {
388 ipsecstat.in_nomem++;
389 goto bad;
390 }
391
392 if (netisr_queue(NETISR_IP, m)) { /* (0) on success. */
393 ipsecstat.in_inval++;
394 m = NULL;
395 goto bad;
396 }
397 m = NULL;
398 nxt = IPPROTO_DONE;
399 } else {
400 /*
401 * strip off ESP header and IV.
402 * even in m_pulldown case, we need to strip off ESP so that
403 * we can always compute checksum for AH correctly.
404 */
405 size_t stripsiz;
406
407 stripsiz = esplen + ivlen;
408
409 ip = mtod(m, struct ip *);
410 ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), off);
411 m->m_data += stripsiz;
412 m->m_len -= stripsiz;
413 m->m_pkthdr.len -= stripsiz;
414
415 ip = mtod(m, struct ip *);
416 #ifdef IPLEN_FLIPPED
417 ip->ip_len = ip->ip_len - stripsiz;
418 #else
419 ip->ip_len = htons(ntohs(ip->ip_len) - stripsiz);
420 #endif
421 ip->ip_p = nxt;
422
423 key_sa_recordxfer(sav, m);
424 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
425 ipsecstat.in_nomem++;
426 goto bad;
427 }
428
429 if (nxt != IPPROTO_DONE) {
430 if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
431 ipsec4_in_reject(m, NULL)) {
432 ipsecstat.in_polvio++;
433 goto bad;
434 }
435 (*inetsw[ip_protox[nxt]].pr_input)(m, off);
436 } else
437 m_freem(m);
438 m = NULL;
439 }
440
441 if (sav) {
442 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
443 printf("DP esp4_input call free SA:%p\n", sav));
444 key_freesav(sav);
445 }
446 ipsecstat.in_success++;
447 return;
448
449 bad:
450 if (sav) {
451 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
452 printf("DP esp4_input call free SA:%p\n", sav));
453 key_freesav(sav);
454 }
455 if (m)
456 m_freem(m);
457 return;
458 }
459 #endif /* INET */
460
461 #ifdef INET6
462 int
463 esp6_input(mp, offp, proto)
464 struct mbuf **mp;
465 int *offp, proto;
466 {
467 struct mbuf *m = *mp;
468 int off = *offp;
469 struct ip6_hdr *ip6;
470 struct esp *esp;
471 struct esptail esptail;
472 u_int32_t spi;
473 struct secasvar *sav = NULL;
474 size_t taillen;
475 u_int16_t nxt;
476 const struct esp_algorithm *algo;
477 int ivlen;
478 size_t esplen;
479
480 /* sanity check for alignment. */
481 if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
482 ipseclog((LOG_ERR, "IPv6 ESP input: packet alignment problem "
483 "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
484 ipsec6stat.in_inval++;
485 goto bad;
486 }
487
488 #ifndef PULLDOWN_TEST
489 IP6_EXTHDR_CHECK(m, off, ESPMAXLEN, IPPROTO_DONE);
490 esp = (struct esp *)(mtod(m, caddr_t) + off);
491 #else
492 IP6_EXTHDR_GET(esp, struct esp *, m, off, ESPMAXLEN);
493 if (esp == NULL) {
494 ipsec6stat.in_inval++;
495 return IPPROTO_DONE;
496 }
497 #endif
498 ip6 = mtod(m, struct ip6_hdr *);
499
500 if (ntohs(ip6->ip6_plen) == 0) {
501 ipseclog((LOG_ERR, "IPv6 ESP input: "
502 "ESP with IPv6 jumbogram is not supported.\n"));
503 ipsec6stat.in_inval++;
504 goto bad;
505 }
506
507 /* find the sassoc. */
508 spi = esp->esp_spi;
509
510 if ((sav = key_allocsa(AF_INET6,
511 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
512 IPPROTO_ESP, spi)) == 0) {
513 ipseclog((LOG_WARNING,
514 "IPv6 ESP input: no key association found for spi %u\n",
515 (u_int32_t)ntohl(spi)));
516 ipsec6stat.in_nosa++;
517 goto bad;
518 }
519 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
520 printf("DP esp6_input called to allocate SA:%p\n", sav));
521 if (sav->state != SADB_SASTATE_MATURE
522 && sav->state != SADB_SASTATE_DYING) {
523 ipseclog((LOG_DEBUG,
524 "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
525 (u_int32_t)ntohl(spi)));
526 ipsec6stat.in_badspi++;
527 goto bad;
528 }
529 algo = esp_algorithm_lookup(sav->alg_enc);
530 if (!algo) {
531 ipseclog((LOG_DEBUG, "IPv6 ESP input: "
532 "unsupported encryption algorithm for spi %u\n",
533 (u_int32_t)ntohl(spi)));
534 ipsec6stat.in_badspi++;
535 goto bad;
536 }
537
538 /* check if we have proper ivlen information */
539 ivlen = sav->ivlen;
540 if (ivlen < 0) {
541 ipseclog((LOG_ERR, "inproper ivlen in IPv6 ESP input: %s %s\n",
542 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
543 ipsec6stat.in_badspi++;
544 goto bad;
545 }
546
547 if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
548 && (sav->alg_auth && sav->key_auth)))
549 goto noreplaycheck;
550
551 if (sav->alg_auth == SADB_X_AALG_NULL ||
552 sav->alg_auth == SADB_AALG_NONE)
553 goto noreplaycheck;
554
555 /*
556 * check for sequence number.
557 */
558 if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
559 ; /* okey */
560 else {
561 ipsec6stat.in_espreplay++;
562 ipseclog((LOG_WARNING,
563 "replay packet in IPv6 ESP input: %s %s\n",
564 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
565 goto bad;
566 }
567
568 /* check ICV */
569 {
570 u_char sum0[AH_MAXSUMSIZE];
571 u_char sum[AH_MAXSUMSIZE];
572 const struct ah_algorithm *sumalgo;
573 size_t siz;
574
575 sumalgo = ah_algorithm_lookup(sav->alg_auth);
576 if (!sumalgo)
577 goto noreplaycheck;
578 siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
579 if (m->m_pkthdr.len < off + ESPMAXLEN + siz) {
580 ipsec6stat.in_inval++;
581 goto bad;
582 }
583 if (AH_MAXSUMSIZE < siz) {
584 ipseclog((LOG_DEBUG,
585 "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
586 (u_long)siz));
587 ipsec6stat.in_inval++;
588 goto bad;
589 }
590
591 m_copydata(m, m->m_pkthdr.len - siz, siz, (caddr_t)&sum0[0]);
592
593 if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
594 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
595 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
596 ipsec6stat.in_espauthfail++;
597 goto bad;
598 }
599
600 if (bcmp(sum0, sum, siz) != 0) {
601 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
602 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
603 ipsec6stat.in_espauthfail++;
604 goto bad;
605 }
606
607 /* strip off the authentication data */
608 m_adj(m, -siz);
609 ip6 = mtod(m, struct ip6_hdr *);
610 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - siz);
611
612 m->m_flags |= M_AUTHIPDGM;
613 ipsec6stat.in_espauthsucc++;
614 }
615
616 /*
617 * update sequence number.
618 */
619 if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
620 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
621 ipsec6stat.in_espreplay++;
622 goto bad;
623 }
624 }
625
626 noreplaycheck:
627
628 /* process main esp header. */
629 if (sav->flags & SADB_X_EXT_OLD) {
630 /* RFC 1827 */
631 esplen = sizeof(struct esp);
632 } else {
633 /* RFC 2406 */
634 if (sav->flags & SADB_X_EXT_DERIV)
635 esplen = sizeof(struct esp);
636 else
637 esplen = sizeof(struct newesp);
638 }
639
640 if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
641 ipseclog((LOG_WARNING,
642 "IPv6 ESP input: packet too short\n"));
643 ipsec6stat.in_inval++;
644 goto bad;
645 }
646
647 #ifndef PULLDOWN_TEST
648 IP6_EXTHDR_CHECK(m, off, esplen + ivlen, IPPROTO_DONE); /* XXX */
649 #else
650 IP6_EXTHDR_GET(esp, struct esp *, m, off, esplen + ivlen);
651 if (esp == NULL) {
652 ipsec6stat.in_inval++;
653 m = NULL;
654 goto bad;
655 }
656 #endif
657 ip6 = mtod(m, struct ip6_hdr *); /* set it again just in case */
658
659 /*
660 * pre-compute and cache intermediate key
661 */
662 if (esp_schedule(algo, sav) != 0) {
663 ipsec6stat.in_inval++;
664 goto bad;
665 }
666
667 /*
668 * decrypt the packet.
669 */
670 if (!algo->decrypt)
671 panic("internal error: no decrypt function");
672 if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
673 /* m is already freed */
674 m = NULL;
675 ipseclog((LOG_ERR, "decrypt fail in IPv6 ESP input: %s\n",
676 ipsec_logsastr(sav)));
677 ipsec6stat.in_inval++;
678 goto bad;
679 }
680 ipsec6stat.in_esphist[sav->alg_enc]++;
681
682 m->m_flags |= M_DECRYPTED;
683
684 /*
685 * find the trailer of the ESP.
686 */
687 m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
688 (caddr_t)&esptail);
689 nxt = esptail.esp_nxt;
690 taillen = esptail.esp_padlen + sizeof(esptail);
691
692 if (m->m_pkthdr.len < taillen
693 || m->m_pkthdr.len - taillen < sizeof(struct ip6_hdr)) { /* ? */
694 ipseclog((LOG_WARNING,
695 "bad pad length in IPv6 ESP input: %s %s\n",
696 ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
697 ipsec6stat.in_inval++;
698 goto bad;
699 }
700
701 /* strip off the trailing pad area. */
702 m_adj(m, -taillen);
703
704 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - taillen);
705
706 /* was it transmitted over the IPsec tunnel SA? */
707 if (ipsec6_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
708 /*
709 * strip off all the headers that precedes ESP header.
710 * IP6 xx ESP IP6' payload -> IP6' payload
711 *
712 * XXX more sanity checks
713 * XXX relationship with gif?
714 */
715 u_int32_t flowinfo; /* net endian */
716 flowinfo = ip6->ip6_flow;
717 m_adj(m, off + esplen + ivlen);
718 if (m->m_len < sizeof(*ip6)) {
719 m = m_pullup(m, sizeof(*ip6));
720 if (!m) {
721 ipsec6stat.in_inval++;
722 goto bad;
723 }
724 }
725 ip6 = mtod(m, struct ip6_hdr *);
726 /* ECN consideration. */
727 if (!ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow)) {
728 ipsec6stat.in_inval++;
729 goto bad;
730 }
731 if (!key_checktunnelsanity(sav, AF_INET6,
732 (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
733 ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
734 "in IPv6 ESP input: %s %s\n",
735 ipsec6_logpacketstr(ip6, spi),
736 ipsec_logsastr(sav)));
737 ipsec6stat.in_inval++;
738 goto bad;
739 }
740
741 key_sa_recordxfer(sav, m);
742 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
743 ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
744 ipsec6stat.in_nomem++;
745 goto bad;
746 }
747
748 if (netisr_queue(NETISR_IPV6, m)) { /* (0) on success. */
749 ipsec6stat.in_inval++;
750 m = NULL;
751 goto bad;
752 }
753 m = NULL;
754 nxt = IPPROTO_DONE;
755 } else {
756 /*
757 * strip off ESP header and IV.
758 * even in m_pulldown case, we need to strip off ESP so that
759 * we can always compute checksum for AH correctly.
760 */
761 size_t stripsiz;
762 u_int8_t *prvnxtp;
763
764 /*
765 * Set the next header field of the previous header correctly.
766 */
767 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
768 *prvnxtp = nxt;
769
770 stripsiz = esplen + ivlen;
771
772 ip6 = mtod(m, struct ip6_hdr *);
773 if (m->m_len >= stripsiz + off) {
774 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
775 m->m_data += stripsiz;
776 m->m_len -= stripsiz;
777 m->m_pkthdr.len -= stripsiz;
778 } else {
779 /*
780 * this comes with no copy if the boundary is on
781 * cluster
782 */
783 struct mbuf *n;
784
785 n = m_split(m, off, M_DONTWAIT);
786 if (n == NULL) {
787 /* m is retained by m_split */
788 goto bad;
789 }
790 m_adj(n, stripsiz);
791 /* m_cat does not update m_pkthdr.len */
792 m->m_pkthdr.len += n->m_pkthdr.len;
793 m_cat(m, n);
794 }
795
796 #ifndef PULLDOWN_TEST
797 /*
798 * KAME requires that the packet to be contiguous on the
799 * mbuf. We need to make that sure.
800 * this kind of code should be avoided.
801 * XXX other conditions to avoid running this part?
802 */
803 if (m->m_len != m->m_pkthdr.len) {
804 struct mbuf *n = NULL;
805 int maxlen;
806
807 MGETHDR(n, M_DONTWAIT, MT_HEADER);
808 maxlen = MHLEN;
809 if (n)
810 M_MOVE_PKTHDR(n, m);
811 if (n && n->m_pkthdr.len > maxlen) {
812 MCLGET(n, M_DONTWAIT);
813 maxlen = MCLBYTES;
814 if ((n->m_flags & M_EXT) == 0) {
815 m_free(n);
816 n = NULL;
817 }
818 }
819 if (!n) {
820 printf("esp6_input: mbuf allocation failed\n");
821 goto bad;
822 }
823
824 if (n->m_pkthdr.len <= maxlen) {
825 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
826 n->m_len = n->m_pkthdr.len;
827 n->m_next = NULL;
828 m_freem(m);
829 } else {
830 m_copydata(m, 0, maxlen, mtod(n, caddr_t));
831 n->m_len = maxlen;
832 n->m_next = m;
833 m_adj(m, maxlen);
834 }
835 m = n;
836 }
837 #endif
838
839 ip6 = mtod(m, struct ip6_hdr *);
840 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
841
842 key_sa_recordxfer(sav, m);
843 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
844 ipsec6stat.in_nomem++;
845 goto bad;
846 }
847 }
848
849 *offp = off;
850 *mp = m;
851
852 if (sav) {
853 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
854 printf("DP esp6_input call free SA:%p\n", sav));
855 key_freesav(sav);
856 }
857 ipsec6stat.in_success++;
858 return nxt;
859
860 bad:
861 if (sav) {
862 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
863 printf("DP esp6_input call free SA:%p\n", sav));
864 key_freesav(sav);
865 }
866 if (m)
867 m_freem(m);
868 return IPPROTO_DONE;
869 }
870
871 void
872 esp6_ctlinput(cmd, sa, d)
873 int cmd;
874 struct sockaddr *sa;
875 void *d;
876 {
877 const struct newesp *espp;
878 struct newesp esp;
879 struct ip6ctlparam *ip6cp = NULL, ip6cp1;
880 struct secasvar *sav;
881 struct ip6_hdr *ip6;
882 struct mbuf *m;
883 int off;
884 struct sockaddr_in6 *sa6_src, *sa6_dst;
885
886 if (sa->sa_family != AF_INET6 ||
887 sa->sa_len != sizeof(struct sockaddr_in6))
888 return;
889 if ((unsigned)cmd >= PRC_NCMDS)
890 return;
891
892 /* if the parameter is from icmp6, decode it. */
893 if (d != NULL) {
894 ip6cp = (struct ip6ctlparam *)d;
895 m = ip6cp->ip6c_m;
896 ip6 = ip6cp->ip6c_ip6;
897 off = ip6cp->ip6c_off;
898 } else {
899 m = NULL;
900 ip6 = NULL;
901 off = 0; /* calm gcc */
902 }
903
904 if (ip6) {
905 /*
906 * Notify the error to all possible sockets via pfctlinput2.
907 * Since the upper layer information (such as protocol type,
908 * source and destination ports) is embedded in the encrypted
909 * data and might have been cut, we can't directly call
910 * an upper layer ctlinput function. However, the pcbnotify
911 * function will consider source and destination addresses
912 * as well as the flow info value, and may be able to find
913 * some PCB that should be notified.
914 * Although pfctlinput2 will call esp6_ctlinput(), there is
915 * no possibility of an infinite loop of function calls,
916 * because we don't pass the inner IPv6 header.
917 */
918 bzero(&ip6cp1, sizeof(ip6cp1));
919 ip6cp1.ip6c_src = ip6cp->ip6c_src;
920 pfctlinput2(cmd, sa, (void *)&ip6cp1);
921
922 /*
923 * Then go to special cases that need ESP header information.
924 * XXX: We assume that when ip6 is non NULL,
925 * M and OFF are valid.
926 */
927
928 /* check if we can safely examine src and dst ports */
929 if (m->m_pkthdr.len < off + sizeof(esp))
930 return;
931
932 if (m->m_len < off + sizeof(esp)) {
933 /*
934 * this should be rare case,
935 * so we compromise on this copy...
936 */
937 m_copydata(m, off, sizeof(esp), (caddr_t)&esp);
938 espp = &esp;
939 } else
940 espp = (struct newesp*)(mtod(m, caddr_t) + off);
941
942 if (cmd == PRC_MSGSIZE) {
943 int valid = 0;
944
945 /*
946 * Check to see if we have a valid SA corresponding to
947 * the address in the ICMP message payload.
948 */
949 sa6_src = ip6cp->ip6c_src;
950 sa6_dst = (struct sockaddr_in6 *)sa;
951 sav = key_allocsa(AF_INET6,
952 (caddr_t)&sa6_src->sin6_addr,
953 (caddr_t)&sa6_dst->sin6_addr,
954 IPPROTO_ESP, espp->esp_spi);
955 if (sav) {
956 if (sav->state == SADB_SASTATE_MATURE ||
957 sav->state == SADB_SASTATE_DYING)
958 valid++;
959 key_freesav(sav);
960 }
961
962 /* XXX Further validation? */
963
964 /*
965 * Depending on the value of "valid" and routing table
966 * size (mtudisc_{hi,lo}wat), we will:
967 * - recalcurate the new MTU and create the
968 * corresponding routing entry, or
969 * - ignore the MTU change notification.
970 */
971 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
972 }
973 } else {
974 /* we normally notify any pcb here */
975 }
976 }
977 #endif /* INET6 */
Cache object: 5dd62beef4fccd254b0111d342488ee3
|