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