983a38d14bf42bec015a84ffc83a22e0566fcfe5
[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 /*      $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 <sys/thread2.h>
51 #include <sys/msgport2.h>
52
53 #include <net/if.h>
54 #include <net/route.h>
55 #include <net/netisr.h>
56 #include <machine/cpu.h>
57
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip_ecn.h>
64 #ifdef INET6
65 #include <netinet6/ip6_ecn.h>
66 #endif
67
68 #ifdef INET6
69 #include <netinet/ip6.h>
70 #include <netinet6/in6_pcb.h>
71 #include <netinet6/ip6_var.h>
72 #include <netinet/icmp6.h>
73 #include <netinet6/ip6protosw.h>
74 #endif
75
76 #include <netinet6/ipsec.h>
77 #ifdef INET6
78 #include <netinet6/ipsec6.h>
79 #endif
80 #include <netinet6/ah.h>
81 #ifdef INET6
82 #include <netinet6/ah6.h>
83 #endif
84 #include <netinet6/esp.h>
85 #ifdef INET6
86 #include <netinet6/esp6.h>
87 #endif
88 #include <netproto/key/key.h>
89 #include <netproto/key/keydb.h>
90 #include <netproto/key/key_debug.h>
91
92 #include <machine/stdarg.h>
93
94 #include <net/net_osdep.h>
95
96 #define IPLEN_FLIPPED
97
98 #define ESPMAXLEN \
99         (sizeof(struct esp) < sizeof(struct newesp) \
100                 ? sizeof(struct newesp) : sizeof(struct esp))
101
102 #ifdef INET
103 extern struct protosw inetsw[];
104
105 int
106 esp4_input(struct mbuf **mp, int *offp, int proto)
107 {
108         int off;
109         struct ip *ip;
110         struct esp *esp;
111         struct esptail esptail;
112         struct mbuf *m;
113         u_int32_t spi;
114         struct secasvar *sav = NULL;
115         size_t taillen;
116         u_int16_t nxt;
117         const struct esp_algorithm *algo;
118         int ivlen;
119         size_t hlen;
120         size_t esplen;
121
122         off = *offp;
123         m = *mp;
124         *mp = NULL;
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)) == NULL) {
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                 kprintf("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, "improper 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 < off + esplen + ivlen + sizeof(esptail)) {
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) &&
434                             ipsec4_in_reject(m, NULL)) {
435                                 ipsecstat.in_polvio++;
436                                 goto bad;
437                         }
438                         if (!ip_lengthcheck(&m, 0)) {
439                                 /* freed in ip_lengthcheck() */
440                                 goto bad;
441                         }
442                         *mp = m;
443                         *offp = off;
444                         (*inetsw[ip_protox[nxt]].pr_input)(mp, offp, nxt);
445                 } else {
446                         m_freem(m);
447                 }
448                 m = NULL;
449         }
450
451         if (sav) {
452                 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
453                         kprintf("DP esp4_input call free SA:%p\n", sav));
454                 key_freesav(sav);
455         }
456         ipsecstat.in_success++;
457         return(IPPROTO_DONE);
458
459 bad:
460         if (sav) {
461                 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
462                         kprintf("DP esp4_input call free SA:%p\n", sav));
463                 key_freesav(sav);
464         }
465         if (m)
466                 m_freem(m);
467         return(IPPROTO_DONE);
468 }
469 #endif /* INET */
470
471 #ifdef INET6
472 int
473 esp6_input(struct mbuf **mp, int *offp, int proto)
474 {
475         struct mbuf *m = *mp;
476         int off = *offp;
477         struct ip6_hdr *ip6;
478         struct esp *esp;
479         struct esptail esptail;
480         u_int32_t spi;
481         struct secasvar *sav = NULL;
482         size_t taillen;
483         u_int16_t nxt;
484         const struct esp_algorithm *algo;
485         int ivlen;
486         size_t esplen;
487
488         /* sanity check for alignment. */
489         if (off % 4 != 0 || m->m_pkthdr.len % 4 != 0) {
490                 ipseclog((LOG_ERR, "IPv6 ESP input: packet alignment problem "
491                         "(off=%d, pktlen=%d)\n", off, m->m_pkthdr.len));
492                 ipsec6stat.in_inval++;
493                 goto bad;
494         }
495
496 #ifndef PULLDOWN_TEST
497         IP6_EXTHDR_CHECK(m, off, ESPMAXLEN, IPPROTO_DONE);
498         esp = (struct esp *)(mtod(m, caddr_t) + off);
499 #else
500         IP6_EXTHDR_GET(esp, struct esp *, m, off, ESPMAXLEN);
501         if (esp == NULL) {
502                 ipsec6stat.in_inval++;
503                 return IPPROTO_DONE;
504         }
505 #endif
506         ip6 = mtod(m, struct ip6_hdr *);
507
508         if (ntohs(ip6->ip6_plen) == 0) {
509                 ipseclog((LOG_ERR, "IPv6 ESP input: "
510                     "ESP with IPv6 jumbogram is not supported.\n"));
511                 ipsec6stat.in_inval++;
512                 goto bad;
513         }
514
515         /* find the sassoc. */
516         spi = esp->esp_spi;
517
518         if ((sav = key_allocsa(AF_INET6,
519                               (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
520                               IPPROTO_ESP, spi)) == NULL) {
521                 ipseclog((LOG_WARNING,
522                     "IPv6 ESP input: no key association found for spi %u\n",
523                     (u_int32_t)ntohl(spi)));
524                 ipsec6stat.in_nosa++;
525                 goto bad;
526         }
527         KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
528                 kprintf("DP esp6_input called to allocate SA:%p\n", sav));
529         if (sav->state != SADB_SASTATE_MATURE
530          && sav->state != SADB_SASTATE_DYING) {
531                 ipseclog((LOG_DEBUG,
532                     "IPv6 ESP input: non-mature/dying SA found for spi %u\n",
533                     (u_int32_t)ntohl(spi)));
534                 ipsec6stat.in_badspi++;
535                 goto bad;
536         }
537         algo = esp_algorithm_lookup(sav->alg_enc);
538         if (!algo) {
539                 ipseclog((LOG_DEBUG, "IPv6 ESP input: "
540                     "unsupported encryption algorithm for spi %u\n",
541                     (u_int32_t)ntohl(spi)));
542                 ipsec6stat.in_badspi++;
543                 goto bad;
544         }
545
546         /* check if we have proper ivlen information */
547         ivlen = sav->ivlen;
548         if (ivlen < 0) {
549                 ipseclog((LOG_ERR, "improper ivlen in IPv6 ESP input: %s %s\n",
550                     ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
551                 ipsec6stat.in_badspi++;
552                 goto bad;
553         }
554
555         if (!((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay
556          && (sav->alg_auth && sav->key_auth)))
557                 goto noreplaycheck;
558
559         if (sav->alg_auth == SADB_X_AALG_NULL ||
560             sav->alg_auth == SADB_AALG_NONE)
561                 goto noreplaycheck;
562
563         /*
564          * check for sequence number.
565          */
566         if (ipsec_chkreplay(ntohl(((struct newesp *)esp)->esp_seq), sav))
567                 ; /* okey */
568         else {
569                 ipsec6stat.in_espreplay++;
570                 ipseclog((LOG_WARNING,
571                     "replay packet in IPv6 ESP input: %s %s\n",
572                     ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
573                 goto bad;
574         }
575
576         /* check ICV */
577     {
578         u_char sum0[AH_MAXSUMSIZE];
579         u_char sum[AH_MAXSUMSIZE];
580         const struct ah_algorithm *sumalgo;
581         size_t siz;
582
583         sumalgo = ah_algorithm_lookup(sav->alg_auth);
584         if (!sumalgo)
585                 goto noreplaycheck;
586         siz = (((*sumalgo->sumsiz)(sav) + 3) & ~(4 - 1));
587         if (m->m_pkthdr.len < off + ESPMAXLEN + siz) {
588                 ipsec6stat.in_inval++;
589                 goto bad;
590         }
591         if (AH_MAXSUMSIZE < siz) {
592                 ipseclog((LOG_DEBUG,
593                     "internal error: AH_MAXSUMSIZE must be larger than %lu\n",
594                     (u_long)siz));
595                 ipsec6stat.in_inval++;
596                 goto bad;
597         }
598
599         m_copydata(m, m->m_pkthdr.len - siz, siz, &sum0[0]);
600
601         if (esp_auth(m, off, m->m_pkthdr.len - off - siz, sav, sum)) {
602                 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
603                     ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
604                 ipsec6stat.in_espauthfail++;
605                 goto bad;
606         }
607
608         if (bcmp(sum0, sum, siz) != 0) {
609                 ipseclog((LOG_WARNING, "auth fail in IPv6 ESP input: %s %s\n",
610                     ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
611                 ipsec6stat.in_espauthfail++;
612                 goto bad;
613         }
614
615         /* strip off the authentication data */
616         m_adj(m, -siz);
617         ip6 = mtod(m, struct ip6_hdr *);
618         ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - siz);
619
620         m->m_flags |= M_AUTHIPDGM;
621         ipsec6stat.in_espauthsucc++;
622     }
623
624         /*
625          * update sequence number.
626          */
627         if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
628                 if (ipsec_updatereplay(ntohl(((struct newesp *)esp)->esp_seq), sav)) {
629                         ipsec6stat.in_espreplay++;
630                         goto bad;
631                 }
632         }
633
634 noreplaycheck:
635
636         /* process main esp header. */
637         if (sav->flags & SADB_X_EXT_OLD) {
638                 /* RFC 1827 */
639                 esplen = sizeof(struct esp);
640         } else {
641                 /* RFC 2406 */
642                 if (sav->flags & SADB_X_EXT_DERIV)
643                         esplen = sizeof(struct esp);
644                 else
645                         esplen = sizeof(struct newesp);
646         }
647
648         if (m->m_pkthdr.len < off + esplen + ivlen + sizeof(esptail)) {
649                 ipseclog((LOG_WARNING,
650                     "IPv6 ESP input: packet too short\n"));
651                 ipsec6stat.in_inval++;
652                 goto bad;
653         }
654
655 #ifndef PULLDOWN_TEST
656         IP6_EXTHDR_CHECK(m, off, esplen + ivlen, IPPROTO_DONE); /* XXX */
657 #else
658         IP6_EXTHDR_GET(esp, struct esp *, m, off, esplen + ivlen);
659         if (esp == NULL) {
660                 ipsec6stat.in_inval++;
661                 m = NULL;
662                 goto bad;
663         }
664 #endif
665         ip6 = mtod(m, struct ip6_hdr *);        /* set it again just in case */
666
667         /*
668          * pre-compute and cache intermediate key
669          */
670         if (esp_schedule(algo, sav) != 0) {
671                 ipsec6stat.in_inval++;
672                 goto bad;
673         }
674
675         /*
676          * decrypt the packet.
677          */
678         if (!algo->decrypt)
679                 panic("internal error: no decrypt function");
680         if ((*algo->decrypt)(m, off, sav, algo, ivlen)) {
681                 /* m is already freed */
682                 m = NULL;
683                 ipseclog((LOG_ERR, "decrypt fail in IPv6 ESP input: %s\n",
684                     ipsec_logsastr(sav)));
685                 ipsec6stat.in_inval++;
686                 goto bad;
687         }
688         ipsec6stat.in_esphist[sav->alg_enc]++;
689
690         m->m_flags |= M_DECRYPTED;
691
692         /*
693          * find the trailer of the ESP.
694          */
695         m_copydata(m, m->m_pkthdr.len - sizeof(esptail), sizeof(esptail),
696              (caddr_t)&esptail);
697         nxt = esptail.esp_nxt;
698         taillen = esptail.esp_padlen + sizeof(esptail);
699
700         if (m->m_pkthdr.len < taillen
701          || m->m_pkthdr.len - taillen < sizeof(struct ip6_hdr)) {       /* ? */
702                 ipseclog((LOG_WARNING,
703                     "bad pad length in IPv6 ESP input: %s %s\n",
704                     ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
705                 ipsec6stat.in_inval++;
706                 goto bad;
707         }
708
709         /* strip off the trailing pad area. */
710         m_adj(m, -taillen);
711
712         ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - taillen);
713
714         /* was it transmitted over the IPsec tunnel SA? */
715         if (ipsec6_tunnel_validate(m, off + esplen + ivlen, nxt, sav)) {
716                 /*
717                  * strip off all the headers that precedes ESP header.
718                  *      IP6 xx ESP IP6' payload -> IP6' payload
719                  *
720                  * XXX more sanity checks
721                  * XXX relationship with gif?
722                  */
723                 u_int32_t flowinfo;     /* net endian */
724                 flowinfo = ip6->ip6_flow;
725                 m_adj(m, off + esplen + ivlen);
726                 if (m->m_len < sizeof(*ip6)) {
727 #ifndef PULLDOWN_TEST
728                         /*
729                          * m_pullup is prohibited in KAME IPv6 input processing
730                          * but there's no other way!
731                          */
732 #else
733                         /* okay to pullup in m_pulldown style */
734 #endif
735                         m = m_pullup(m, sizeof(*ip6));
736                         if (!m) {
737                                 ipsec6stat.in_inval++;
738                                 goto bad;
739                         }
740                 }
741                 ip6 = mtod(m, struct ip6_hdr *);
742                 /* ECN consideration. */
743                 ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow);
744                 if (!key_checktunnelsanity(sav, AF_INET6,
745                             (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
746                         ipseclog((LOG_ERR, "ipsec tunnel address mismatch "
747                             "in IPv6 ESP input: %s %s\n",
748                             ipsec6_logpacketstr(ip6, spi),
749                             ipsec_logsastr(sav)));
750                         ipsec6stat.in_inval++;
751                         goto bad;
752                 }
753
754                 key_sa_recordxfer(sav, m);
755                 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0 ||
756                     ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
757                         ipsec6stat.in_nomem++;
758                         goto bad;
759                 }
760
761                 if (netisr_queue(NETISR_IPV6, m)) {
762                         ipsec6stat.in_inval++;
763                         m = NULL;
764                         goto bad;
765                 }
766
767                 nxt = IPPROTO_DONE;
768         } else {
769                 /*
770                  * strip off ESP header and IV.
771                  * even in m_pulldown case, we need to strip off ESP so that
772                  * we can always compute checksum for AH correctly.
773                  */
774                 size_t stripsiz;
775                 char *prvnxtp;
776
777                 /*
778                  * Set the next header field of the previous header correctly.
779                  */
780                 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
781                 *prvnxtp = nxt;
782
783                 stripsiz = esplen + ivlen;
784
785                 ip6 = mtod(m, struct ip6_hdr *);
786                 if (m->m_len >= stripsiz + off) {
787                         ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
788                         m->m_data += stripsiz;
789                         m->m_len -= stripsiz;
790                         m->m_pkthdr.len -= stripsiz;
791                 } else {
792                         /*
793                          * this comes with no copy if the boundary is on
794                          * cluster
795                          */
796                         struct mbuf *n;
797
798                         n = m_split(m, off, MB_DONTWAIT);
799                         if (n == NULL) {
800                                 /* m is retained by m_split */
801                                 goto bad;
802                         }
803                         m_adj(n, stripsiz);
804                         /* m_cat does not update m_pkthdr.len */
805                         m->m_pkthdr.len += n->m_pkthdr.len;
806                         m_cat(m, n);
807                 }
808
809 #ifndef PULLDOWN_TEST
810                 /*
811                  * KAME requires that the packet to be contiguous on the
812                  * mbuf.  We need to make that sure.
813                  * this kind of code should be avoided.
814                  * XXX other conditions to avoid running this part?
815                  */
816                 if (m->m_len != m->m_pkthdr.len) {
817                         struct mbuf *n = NULL;
818                         int maxlen;
819
820                         n = m_getb(m->m_pkthdr.len, MB_DONTWAIT, MT_HEADER,
821                                    M_PKTHDR);
822                         if (!n) {
823                                 kprintf("esp6_input: mbuf allocation failed\n");
824                                 goto bad;
825                         }
826                         M_MOVE_PKTHDR(n, m);
827
828                         maxlen = (n->m_flags & M_EXT) ? MCLBYTES : MHLEN;
829                         if (n->m_pkthdr.len <= maxlen) {
830                                 m_copydata(m, 0, n->m_pkthdr.len, mtod(n, caddr_t));
831                                 n->m_len = n->m_pkthdr.len;
832                                 n->m_next = NULL;
833                                 m_freem(m);
834                         } else {
835                                 m_copydata(m, 0, maxlen, mtod(n, caddr_t));
836                                 n->m_len = maxlen;
837                                 n->m_next = m;
838                                 m_adj(m, maxlen);
839                         }
840                         m = n;
841                 }
842 #endif
843
844                 ip6 = mtod(m, struct ip6_hdr *);
845                 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
846
847                 key_sa_recordxfer(sav, m);
848                 if (ipsec_addhist(m, IPPROTO_ESP, spi) != 0) {
849                         ipsec6stat.in_nomem++;
850                         goto bad;
851                 }
852         }
853
854         *offp = off;
855         *mp = m;
856
857         if (sav) {
858                 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
859                         kprintf("DP esp6_input call free SA:%p\n", sav));
860                 key_freesav(sav);
861         }
862         ipsec6stat.in_success++;
863         return nxt;
864
865 bad:
866         if (sav) {
867                 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
868                         kprintf("DP esp6_input call free SA:%p\n", sav));
869                 key_freesav(sav);
870         }
871         if (m)
872                 m_freem(m);
873         return IPPROTO_DONE;
874 }
875
876 void
877 esp6_ctlinput(netmsg_t msg)
878 {
879         int cmd = msg->ctlinput.nm_cmd;
880         struct sockaddr *sa = msg->ctlinput.nm_arg;
881         void *d = msg->ctlinput.nm_extra;
882         const struct newesp *espp;
883         struct newesp esp;
884         struct ip6ctlparam *ip6cp = NULL, ip6cp1;
885         struct secasvar *sav;
886         struct ip6_hdr *ip6;
887         struct mbuf *m;
888         int off;
889         struct sockaddr_in6 *sa6_src, *sa6_dst;
890
891         if (sa->sa_family != AF_INET6 ||
892             sa->sa_len != sizeof(struct sockaddr_in6))
893                 goto out;
894         if ((unsigned)cmd >= PRC_NCMDS)
895                 goto out;
896
897         /* if the parameter is from icmp6, decode it. */
898         if (d != NULL) {
899                 ip6cp = (struct ip6ctlparam *)d;
900                 m = ip6cp->ip6c_m;
901                 ip6 = ip6cp->ip6c_ip6;
902                 off = ip6cp->ip6c_off;
903         } else {
904                 m = NULL;
905                 ip6 = NULL;
906                 off = 0; /* fix warning */
907         }
908
909         if (ip6) {
910                 /*
911                  * Notify the error to all possible sockets via pfctlinput2.
912                  * Since the upper layer information (such as protocol type,
913                  * source and destination ports) is embedded in the encrypted
914                  * data and might have been cut, we can't directly call
915                  * an upper layer ctlinput function. However, the pcbnotify
916                  * function will consider source and destination addresses
917                  * as well as the flow info value, and may be able to find
918                  * some PCB that should be notified.
919                  * Although kpfctlinput2 will call esp6_ctlinput(), there is
920                  * no possibility of an infinite loop of function calls,
921                  * because we don't pass the inner IPv6 header.
922                  */
923                 bzero(&ip6cp1, sizeof(ip6cp1));
924                 ip6cp1.ip6c_src = ip6cp->ip6c_src;
925                 kpfctlinput2(cmd, sa, (void *)&ip6cp1);
926
927                 /*
928                  * Then go to special cases that need ESP header information.
929                  * XXX: We assume that when ip6 is non NULL,
930                  * M and OFF are valid.
931                  */
932
933                 /* check if we can safely examine src and dst ports */
934                 if (m->m_pkthdr.len < off + sizeof(esp))
935                         goto out;
936
937                 if (m->m_len < off + sizeof(esp)) {
938                         /*
939                          * this should be rare case,
940                          * so we compromise on this copy...
941                          */
942                         m_copydata(m, off, sizeof(esp), (caddr_t)&esp);
943                         espp = &esp;
944                 } else
945                         espp = (struct newesp*)(mtod(m, caddr_t) + off);
946
947                 if (cmd == PRC_MSGSIZE) {
948                         int valid = 0;
949
950                         /*
951                          * Check to see if we have a valid SA corresponding to
952                          * the address in the ICMP message payload.
953                          */
954                         sa6_src = ip6cp->ip6c_src;
955                         sa6_dst = (struct sockaddr_in6 *)sa;
956                         sav = key_allocsa(AF_INET6,
957                                           (caddr_t)&sa6_src->sin6_addr,
958                                           (caddr_t)&sa6_dst->sin6_addr,
959                                           IPPROTO_ESP, espp->esp_spi);
960                         if (sav) {
961                                 if (sav->state == SADB_SASTATE_MATURE ||
962                                     sav->state == SADB_SASTATE_DYING)
963                                         valid++;
964                                 key_freesav(sav);
965                         }
966
967                         /* XXX Further validation? */
968
969                         /*
970                          * Depending on the value of "valid" and routing table
971                          * size (mtudisc_{hi,lo}wat), we will:
972                          * - recalcurate the new MTU and create the
973                          *   corresponding routing entry, or
974                          * - ignore the MTU change notification.
975                          */
976                         icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
977                 }
978         } else {
979                 /* we normally notify any pcb here */
980         }
981 out:
982         lwkt_replymsg(&msg->ctlinput.base.lmsg, 0);
983 }
984 #endif /* INET6 */