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