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