Fix two serious bugs in the IP demux code. First, if ip_mport() m_pullup()'s
[dragonfly.git] / sys / netinet6 / ah_input.c
1 /*      $FreeBSD: src/sys/netinet6/ah_input.c,v 1.1.2.6 2002/04/28 05:40:26 suz Exp $   */
2 /*      $DragonFly: src/sys/netinet6/ah_input.c,v 1.8 2004/06/03 18:30:04 joerg Exp $   */
3 /*      $KAME: ah_input.c,v 1.67 2002/01/07 11:39:56 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  * RFC1826/2402 authentication header.
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/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/errno.h>
49 #include <sys/time.h>
50 #include <sys/syslog.h>
51
52 #include <net/if.h>
53 #include <net/route.h>
54 #include <net/netisr.h>
55 #include <machine/cpu.h>
56 #include <machine/stdarg.h>
57
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_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/ip6_var.h>
71 #include <netinet6/in6_pcb.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 <netproto/key/key.h>
85 #include <netproto/key/keydb.h>
86 #ifdef IPSEC_DEBUG
87 #include <netproto/key/key_debug.h>
88 #else
89 #define KEYDEBUG(lev,arg)
90 #endif
91
92 #include <machine/stdarg.h>
93
94 #include <net/net_osdep.h>
95
96 #define IPLEN_FLIPPED
97
98 #ifdef INET
99 #include <netinet/ipprotosw.h>
100 extern struct ipprotosw inetsw[];
101
102 void
103 ah4_input(struct mbuf *m, ...)
104 {
105         int off, proto;
106         struct ip *ip;
107         struct ah *ah;
108         u_int32_t spi;
109         const struct ah_algorithm *algo;
110         size_t siz;
111         size_t siz1;
112         u_char *cksum;
113         struct secasvar *sav = NULL;
114         u_int16_t nxt;
115         size_t hlen;
116         size_t stripsiz = 0;
117         __va_list ap;
118
119         __va_start(ap, m);
120         off = __va_arg(ap, int);
121         proto = __va_arg(ap, int);
122         __va_end(ap);
123
124 #ifndef PULLDOWN_TEST
125         if (m->m_len < off + sizeof(struct newah)) {
126                 m = m_pullup(m, off + sizeof(struct newah));
127                 if (!m) {
128                         ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup;"
129                                 "dropping the packet for simplicity\n"));
130                         ipsecstat.in_inval++;
131                         goto fail;
132                 }
133         }
134
135         ip = mtod(m, struct ip *);
136         ah = (struct ah *)(((caddr_t)ip) + off);
137 #else
138         ip = mtod(m, struct ip *);
139         IP6_EXTHDR_GET(ah, struct ah *, m, off, sizeof(struct newah));
140         if (ah == NULL) {
141                 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup;"
142                         "dropping the packet for simplicity\n"));
143                 ipsecstat.in_inval++;
144                 goto fail;
145         }
146 #endif
147         nxt = ah->ah_nxt;
148 #ifdef _IP_VHL
149         hlen = IP_VHL_HL(ip->ip_vhl) << 2;
150 #else
151         hlen = ip->ip_hl << 2;
152 #endif
153
154         /* find the sassoc. */
155         spi = ah->ah_spi;
156
157         if ((sav = key_allocsa(AF_INET,
158                               (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst,
159                               IPPROTO_AH, spi)) == 0) {
160                 ipseclog((LOG_WARNING,
161                     "IPv4 AH input: no key association found for spi %u\n",
162                     (u_int32_t)ntohl(spi)));
163                 ipsecstat.in_nosa++;
164                 goto fail;
165         }
166         KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
167                 printf("DP ah4_input called to allocate SA:%p\n", sav));
168         if (sav->state != SADB_SASTATE_MATURE
169          && sav->state != SADB_SASTATE_DYING) {
170                 ipseclog((LOG_DEBUG,
171                     "IPv4 AH input: non-mature/dying SA found for spi %u\n",
172                     (u_int32_t)ntohl(spi)));
173                 ipsecstat.in_badspi++;
174                 goto fail;
175         }
176
177         algo = ah_algorithm_lookup(sav->alg_auth);
178         if (!algo) {
179                 ipseclog((LOG_DEBUG, "IPv4 AH input: "
180                     "unsupported authentication algorithm for spi %u\n",
181                     (u_int32_t)ntohl(spi)));
182                 ipsecstat.in_badspi++;
183                 goto fail;
184         }
185
186         siz = (*algo->sumsiz)(sav);
187         siz1 = ((siz + 3) & ~(4 - 1));
188
189         /*
190          * sanity checks for header, 1.
191          */
192     {
193         int sizoff;
194
195         sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
196
197         /*
198          * Here, we do not do "siz1 == siz".  This is because the way
199          * RFC240[34] section 2 is written.  They do not require truncation
200          * to 96 bits.
201          * For example, Microsoft IPsec stack attaches 160 bits of
202          * authentication data for both hmac-md5 and hmac-sha1.  For hmac-sha1,
203          * 32 bits of padding is attached.
204          *
205          * There are two downsides to this specification.
206          * They have no real harm, however, they leave us fuzzy feeling.
207          * - if we attach more than 96 bits of authentication data onto AH,
208          *   we will never notice about possible modification by rogue
209          *   intermediate nodes.
210          *   Since extra bits in AH checksum is never used, this constitutes
211          *   no real issue, however, it is wacky.
212          * - even if the peer attaches big authentication data, we will never
213          *   notice the difference, since longer authentication data will just
214          *   work.
215          *
216          * We may need some clarification in the spec.
217          */
218         if (siz1 < siz) {
219                 ipseclog((LOG_NOTICE, "sum length too short in IPv4 AH input "
220                     "(%lu, should be at least %lu): %s\n",
221                     (u_long)siz1, (u_long)siz,
222                     ipsec4_logpacketstr(ip, spi)));
223                 ipsecstat.in_inval++;
224                 goto fail;
225         }
226         if ((ah->ah_len << 2) - sizoff != siz1) {
227                 ipseclog((LOG_NOTICE, "sum length mismatch in IPv4 AH input "
228                     "(%d should be %lu): %s\n",
229                     (ah->ah_len << 2) - sizoff, (u_long)siz1,
230                     ipsec4_logpacketstr(ip, spi)));
231                 ipsecstat.in_inval++;
232                 goto fail;
233         }
234
235 #ifndef PULLDOWN_TEST
236         if (m->m_len < off + sizeof(struct ah) + sizoff + siz1) {
237                 m = m_pullup(m, off + sizeof(struct ah) + sizoff + siz1);
238                 if (!m) {
239                         ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup\n"));
240                         ipsecstat.in_inval++;
241                         goto fail;
242                 }
243
244                 ip = mtod(m, struct ip *);
245                 ah = (struct ah *)(((caddr_t)ip) + off);
246         }
247 #else
248         IP6_EXTHDR_GET(ah, struct ah *, m, off,
249                 sizeof(struct ah) + sizoff + siz1);
250         if (ah == NULL) {
251                 ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup\n"));
252                 ipsecstat.in_inval++;
253                 goto fail;
254         }
255 #endif
256     }
257
258         /*
259          * check for sequence number.
260          */
261         if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
262                 if (ipsec_chkreplay(ntohl(((struct newah *)ah)->ah_seq), sav))
263                         ; /* okey */
264                 else {
265                         ipsecstat.in_ahreplay++;
266                         ipseclog((LOG_WARNING,
267                             "replay packet in IPv4 AH input: %s %s\n",
268                             ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
269                         goto fail;
270                 }
271         }
272
273         /*
274          * alright, it seems sane.  now we are going to check the
275          * cryptographic checksum.
276          */
277         cksum = malloc(siz1, M_TEMP, M_NOWAIT);
278         if (!cksum) {
279                 ipseclog((LOG_DEBUG, "IPv4 AH input: "
280                     "couldn't alloc temporary region for cksum\n"));
281                 ipsecstat.in_inval++;
282                 goto fail;
283         }
284         
285         /*
286          * some of IP header fields are flipped to the host endian.
287          * convert them back to network endian.  VERY stupid.
288          */
289         ip->ip_len = htons(ip->ip_len + hlen);
290         ip->ip_off = htons(ip->ip_off);
291         if (ah4_calccksum(m, (caddr_t)cksum, siz1, algo, sav)) {
292                 free(cksum, M_TEMP);
293                 ipsecstat.in_inval++;
294                 goto fail;
295         }
296         ipsecstat.in_ahhist[sav->alg_auth]++;
297         /*
298          * flip them back.
299          */
300         ip->ip_len = ntohs(ip->ip_len) - hlen;
301         ip->ip_off = ntohs(ip->ip_off);
302
303     {
304         caddr_t sumpos = NULL;
305
306         if (sav->flags & SADB_X_EXT_OLD) {
307                 /* RFC 1826 */
308                 sumpos = (caddr_t)(ah + 1);
309         } else {
310                 /* RFC 2402 */
311                 sumpos = (caddr_t)(((struct newah *)ah) + 1);
312         }
313
314         if (bcmp(sumpos, cksum, siz) != 0) {
315                 ipseclog((LOG_WARNING,
316                     "checksum mismatch in IPv4 AH input: %s %s\n",
317                     ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
318                 free(cksum, M_TEMP);
319                 ipsecstat.in_ahauthfail++;
320                 goto fail;
321         }
322     }
323
324         free(cksum, M_TEMP);
325
326         m->m_flags |= M_AUTHIPHDR;
327         m->m_flags |= M_AUTHIPDGM;
328
329 #if 0
330         /*
331          * looks okey, but we need more sanity check.
332          * XXX should elaborate.
333          */
334         if (ah->ah_nxt == IPPROTO_IPIP || ah->ah_nxt == IPPROTO_IP) {
335                 struct ip *nip;
336                 size_t sizoff;
337
338                 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
339
340                 if (m->m_len < off + sizeof(struct ah) + sizoff + siz1 + hlen) {
341                         m = m_pullup(m, off + sizeof(struct ah)
342                                         + sizoff + siz1 + hlen);
343                         if (!m) {
344                                 ipseclog((LOG_DEBUG,
345                                     "IPv4 AH input: can't pullup\n"));
346                                 ipsecstat.in_inval++;
347                                 goto fail;
348                         }
349                 }
350
351                 nip = (struct ip *)((u_char *)(ah + 1) + sizoff + siz1);
352                 if (nip->ip_src.s_addr != ip->ip_src.s_addr
353                  || nip->ip_dst.s_addr != ip->ip_dst.s_addr) {
354                         m->m_flags &= ~M_AUTHIPHDR;
355                         m->m_flags &= ~M_AUTHIPDGM;
356                 }
357         }
358 #ifdef INET6
359         else if (ah->ah_nxt == IPPROTO_IPV6) {
360                 m->m_flags &= ~M_AUTHIPHDR;
361                 m->m_flags &= ~M_AUTHIPDGM;
362         }
363 #endif /* INET6 */
364 #endif /* 0 */
365
366         if (m->m_flags & M_AUTHIPHDR
367          && m->m_flags & M_AUTHIPDGM) {
368 #if 0
369                 ipseclog((LOG_DEBUG,
370                     "IPv4 AH input: authentication succeess\n"));
371 #endif
372                 ipsecstat.in_ahauthsucc++;
373         } else {
374                 ipseclog((LOG_WARNING,
375                     "authentication failed in IPv4 AH input: %s %s\n",
376                     ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
377                 ipsecstat.in_ahauthfail++;
378                 goto fail;
379         }
380
381         /*
382          * update sequence number.
383          */
384         if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
385                 if (ipsec_updatereplay(ntohl(((struct newah *)ah)->ah_seq), sav)) {
386                         ipsecstat.in_ahreplay++;
387                         goto fail;
388                 }
389         }
390
391         /* was it transmitted over the IPsec tunnel SA? */
392         if (sav->flags & SADB_X_EXT_OLD) {
393                 /* RFC 1826 */
394                 stripsiz = sizeof(struct ah) + siz1;
395         } else {
396                 /* RFC 2402 */
397                 stripsiz = sizeof(struct newah) + siz1;
398         }
399         if (ipsec4_tunnel_validate(m, off + stripsiz, nxt, sav)) {
400                 /*
401                  * strip off all the headers that precedes AH.
402                  *      IP xx AH IP' payload -> IP' payload
403                  *
404                  * XXX more sanity checks
405                  * XXX relationship with gif?
406                  */
407                 u_int8_t tos;
408
409                 tos = ip->ip_tos;
410                 m_adj(m, off + stripsiz);
411                 if (m->m_len < sizeof(*ip)) {
412                         m = m_pullup(m, sizeof(*ip));
413                         if (!m) {
414                                 ipsecstat.in_inval++;
415                                 goto fail;
416                         }
417                 }
418                 ip = mtod(m, struct ip *);
419                 /* ECN consideration. */
420                 ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos);
421                 if (!key_checktunnelsanity(sav, AF_INET,
422                             (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) {
423                         ipseclog((LOG_NOTICE, "ipsec tunnel address mismatch "
424                             "in IPv4 AH input: %s %s\n",
425                             ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav)));
426                         ipsecstat.in_inval++;
427                         goto fail;
428                 }
429
430 #if 1
431                 /*
432                  * Should the inner packet be considered authentic?
433                  * My current answer is: NO.
434                  *
435                  * host1 -- gw1 === gw2 -- host2
436                  *      In this case, gw2 can trust the authenticity of the
437                  *      outer packet, but NOT inner.  Packet may be altered
438                  *      between host1 and gw1.
439                  *
440                  * host1 -- gw1 === host2
441                  *      This case falls into the same scenario as above.
442                  *
443                  * host1 === host2
444                  *      This case is the only case when we may be able to leave
445                  *      M_AUTHIPHDR and M_AUTHIPDGM set.
446                  *      However, if host1 is wrongly configured, and allows
447                  *      attacker to inject some packet with src=host1 and
448                  *      dst=host2, you are in risk.
449                  */
450                 m->m_flags &= ~M_AUTHIPHDR;
451                 m->m_flags &= ~M_AUTHIPDGM;
452 #endif
453
454                 key_sa_recordxfer(sav, m);
455                 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0 ||
456                     ipsec_addhist(m, IPPROTO_IPV4, 0) != 0) {
457                         ipsecstat.in_nomem++;
458                         goto fail;
459                 }
460
461                 if (netisr_queue(NETISR_IP, m)) {
462                         ipsecstat.in_inval++;
463                         m = NULL;
464                         goto fail;
465                 }
466
467                 nxt = IPPROTO_DONE;
468         } else {
469                 /*
470                  * strip off AH.
471                  */
472
473                 ip = mtod(m, struct ip *);
474 #ifndef PULLDOWN_TEST
475                 /*
476                  * We do deep-copy since KAME requires that
477                  * the packet is placed in a single external mbuf.
478                  */
479                 ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), off);
480                 m->m_data += stripsiz;
481                 m->m_len -= stripsiz;
482                 m->m_pkthdr.len -= stripsiz;
483 #else
484                 /*
485                  * even in m_pulldown case, we need to strip off AH so that
486                  * we can compute checksum for multiple AH correctly.
487                  */
488                 if (m->m_len >= stripsiz + off) {
489                         ovbcopy((caddr_t)ip, ((caddr_t)ip) + stripsiz, off);
490                         m->m_data += stripsiz;
491                         m->m_len -= stripsiz;
492                         m->m_pkthdr.len -= stripsiz;
493                 } else {
494                         /*
495                          * this comes with no copy if the boundary is on
496                          * cluster
497                          */
498                         struct mbuf *n;
499
500                         n = m_split(m, off, MB_DONTWAIT);
501                         if (n == NULL) {
502                                 /* m is retained by m_split */
503                                 goto fail;
504                         }
505                         m_adj(n, stripsiz);
506                         m_cat(m, n);
507                         /* m_cat does not update m_pkthdr.len */
508                         m->m_pkthdr.len += n->m_pkthdr.len;
509                 }
510 #endif
511
512                 if (m->m_len < sizeof(*ip)) {
513                         m = m_pullup(m, sizeof(*ip));
514                         if (m == NULL) {
515                                 ipsecstat.in_inval++;
516                                 goto fail;
517                         }
518                 }
519                 ip = mtod(m, struct ip *);
520 #ifdef IPLEN_FLIPPED
521                 ip->ip_len = ip->ip_len - stripsiz;
522 #else
523                 ip->ip_len = htons(ntohs(ip->ip_len) - stripsiz);
524 #endif
525                 ip->ip_p = nxt;
526                 /* forget about IP hdr checksum, the check has already been passed */
527
528                 key_sa_recordxfer(sav, m);
529                 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0) {
530                         ipsecstat.in_nomem++;
531                         goto fail;
532                 }
533
534                 if (nxt != IPPROTO_DONE) {
535                         if ((inetsw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
536                             ipsec4_in_reject(m, NULL)) {
537                                 ipsecstat.in_polvio++;
538                                 goto fail;
539                         }
540                         (*inetsw[ip_protox[nxt]].pr_input)(m, off, nxt);
541                 } else
542                         m_freem(m);
543                 m = NULL;
544         }
545
546         if (sav) {
547                 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
548                         printf("DP ah4_input call free SA:%p\n", sav));
549                 key_freesav(sav);
550         }
551         ipsecstat.in_success++;
552         return;
553
554 fail:
555         if (sav) {
556                 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
557                         printf("DP ah4_input call free SA:%p\n", sav));
558                 key_freesav(sav);
559         }
560         if (m)
561                 m_freem(m);
562         return;
563 }
564 #endif /* INET */
565
566 #ifdef INET6
567 int
568 ah6_input(struct mbuf **mp, int *offp, int proto)
569 {
570         struct mbuf *m = *mp;
571         int off = *offp;
572         struct ip6_hdr *ip6;
573         struct ah *ah;
574         u_int32_t spi;
575         const struct ah_algorithm *algo;
576         size_t siz;
577         size_t siz1;
578         u_char *cksum;
579         struct secasvar *sav = NULL;
580         u_int16_t nxt;
581         size_t stripsiz = 0;
582
583 #ifndef PULLDOWN_TEST
584         IP6_EXTHDR_CHECK(m, off, sizeof(struct ah), IPPROTO_DONE);
585         ah = (struct ah *)(mtod(m, caddr_t) + off);
586 #else
587         IP6_EXTHDR_GET(ah, struct ah *, m, off, sizeof(struct newah));
588         if (ah == NULL) {
589                 ipseclog((LOG_DEBUG, "IPv6 AH input: can't pullup\n"));
590                 ipsec6stat.in_inval++;
591                 return IPPROTO_DONE;
592         }
593 #endif
594         ip6 = mtod(m, struct ip6_hdr *);
595         nxt = ah->ah_nxt;
596
597         /* find the sassoc. */
598         spi = ah->ah_spi;
599
600         if (ntohs(ip6->ip6_plen) == 0) {
601                 ipseclog((LOG_ERR, "IPv6 AH input: "
602                     "AH with IPv6 jumbogram is not supported.\n"));
603                 ipsec6stat.in_inval++;
604                 goto fail;
605         }
606
607         if ((sav = key_allocsa(AF_INET6,
608                               (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst,
609                               IPPROTO_AH, spi)) == 0) {
610                 ipseclog((LOG_WARNING,
611                     "IPv6 AH input: no key association found for spi %u\n",
612                     (u_int32_t)ntohl(spi)));
613                 ipsec6stat.in_nosa++;
614                 goto fail;
615         }
616         KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
617                 printf("DP ah6_input called to allocate SA:%p\n", sav));
618         if (sav->state != SADB_SASTATE_MATURE
619          && sav->state != SADB_SASTATE_DYING) {
620                 ipseclog((LOG_DEBUG,
621                     "IPv6 AH input: non-mature/dying SA found for spi %u; ",
622                     (u_int32_t)ntohl(spi)));
623                 ipsec6stat.in_badspi++;
624                 goto fail;
625         }
626
627         algo = ah_algorithm_lookup(sav->alg_auth);
628         if (!algo) {
629                 ipseclog((LOG_DEBUG, "IPv6 AH input: "
630                     "unsupported authentication algorithm for spi %u\n",
631                     (u_int32_t)ntohl(spi)));
632                 ipsec6stat.in_badspi++;
633                 goto fail;
634         }
635
636         siz = (*algo->sumsiz)(sav);
637         siz1 = ((siz + 3) & ~(4 - 1));
638
639         /*
640          * sanity checks for header, 1.
641          */
642     {
643         int sizoff;
644
645         sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
646
647         /*
648          * Here, we do not do "siz1 == siz".  See ah4_input() for complete
649          * description.
650          */
651         if (siz1 < siz) {
652                 ipseclog((LOG_NOTICE, "sum length too short in IPv6 AH input "
653                     "(%lu, should be at least %lu): %s\n",
654                     (u_long)siz1, (u_long)siz,
655                     ipsec6_logpacketstr(ip6, spi)));
656                 ipsec6stat.in_inval++;
657                 goto fail;
658         }
659         if ((ah->ah_len << 2) - sizoff != siz1) {
660                 ipseclog((LOG_NOTICE, "sum length mismatch in IPv6 AH input "
661                     "(%d should be %lu): %s\n",
662                     (ah->ah_len << 2) - sizoff, (u_long)siz1,
663                     ipsec6_logpacketstr(ip6, spi)));
664                 ipsec6stat.in_inval++;
665                 goto fail;
666         }
667 #ifndef PULLDOWN_TEST
668         IP6_EXTHDR_CHECK(m, off, sizeof(struct ah) + sizoff + siz1, IPPROTO_DONE);
669 #else
670         IP6_EXTHDR_GET(ah, struct ah *, m, off,
671                 sizeof(struct ah) + sizoff + siz1);
672         if (ah == NULL) {
673                 ipseclog((LOG_NOTICE, "couldn't pullup gather IPv6 AH checksum part"));
674                 ipsec6stat.in_inval++;
675                 m = NULL;
676                 goto fail;
677         }
678 #endif
679     }
680
681         /*
682          * check for sequence number.
683          */
684         if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
685                 if (ipsec_chkreplay(ntohl(((struct newah *)ah)->ah_seq), sav))
686                         ; /* okey */
687                 else {
688                         ipsec6stat.in_ahreplay++;
689                         ipseclog((LOG_WARNING,
690                             "replay packet in IPv6 AH input: %s %s\n",
691                             ipsec6_logpacketstr(ip6, spi),
692                             ipsec_logsastr(sav)));
693                         goto fail;
694                 }
695         }
696
697         /*
698          * alright, it seems sane.  now we are going to check the
699          * cryptographic checksum.
700          */
701         cksum = malloc(siz1, M_TEMP, M_NOWAIT);
702         if (!cksum) {
703                 ipseclog((LOG_DEBUG, "IPv6 AH input: "
704                     "couldn't alloc temporary region for cksum\n"));
705                 ipsec6stat.in_inval++;
706                 goto fail;
707         }
708         
709         if (ah6_calccksum(m, (caddr_t)cksum, siz1, algo, sav)) {
710                 free(cksum, M_TEMP);
711                 ipsec6stat.in_inval++;
712                 goto fail;
713         }
714         ipsec6stat.in_ahhist[sav->alg_auth]++;
715
716     {
717         caddr_t sumpos = NULL;
718
719         if (sav->flags & SADB_X_EXT_OLD) {
720                 /* RFC 1826 */
721                 sumpos = (caddr_t)(ah + 1);
722         } else {
723                 /* RFC 2402 */
724                 sumpos = (caddr_t)(((struct newah *)ah) + 1);
725         }
726
727         if (bcmp(sumpos, cksum, siz) != 0) {
728                 ipseclog((LOG_WARNING,
729                     "checksum mismatch in IPv6 AH input: %s %s\n",
730                     ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
731                 free(cksum, M_TEMP);
732                 ipsec6stat.in_ahauthfail++;
733                 goto fail;
734         }
735     }
736
737         free(cksum, M_TEMP);
738
739         m->m_flags |= M_AUTHIPHDR;
740         m->m_flags |= M_AUTHIPDGM;
741
742 #if 0
743         /*
744          * looks okey, but we need more sanity check.
745          * XXX should elaborate.
746          */
747         if (ah->ah_nxt == IPPROTO_IPV6) {
748                 struct ip6_hdr *nip6;
749                 size_t sizoff;
750
751                 sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4;
752
753                 IP6_EXTHDR_CHECK(m, off, sizeof(struct ah) + sizoff + siz1
754                                 + sizeof(struct ip6_hdr), IPPROTO_DONE);
755
756                 nip6 = (struct ip6_hdr *)((u_char *)(ah + 1) + sizoff + siz1);
757                 if (!IN6_ARE_ADDR_EQUAL(&nip6->ip6_src, &ip6->ip6_src)
758                  || !IN6_ARE_ADDR_EQUAL(&nip6->ip6_dst, &ip6->ip6_dst)) {
759                         m->m_flags &= ~M_AUTHIPHDR;
760                         m->m_flags &= ~M_AUTHIPDGM;
761                 }
762         } else if (ah->ah_nxt == IPPROTO_IPIP) {
763                 m->m_flags &= ~M_AUTHIPHDR;
764                 m->m_flags &= ~M_AUTHIPDGM;
765         } else if (ah->ah_nxt == IPPROTO_IP) {
766                 m->m_flags &= ~M_AUTHIPHDR;
767                 m->m_flags &= ~M_AUTHIPDGM;
768         }
769 #endif
770
771         if (m->m_flags & M_AUTHIPHDR
772          && m->m_flags & M_AUTHIPDGM) {
773 #if 0
774                 ipseclog((LOG_DEBUG,
775                     "IPv6 AH input: authentication succeess\n"));
776 #endif
777                 ipsec6stat.in_ahauthsucc++;
778         } else {
779                 ipseclog((LOG_WARNING,
780                     "authentication failed in IPv6 AH input: %s %s\n",
781                     ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav)));
782                 ipsec6stat.in_ahauthfail++;
783                 goto fail;
784         }
785
786         /*
787          * update sequence number.
788          */
789         if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) {
790                 if (ipsec_updatereplay(ntohl(((struct newah *)ah)->ah_seq), sav)) {
791                         ipsec6stat.in_ahreplay++;
792                         goto fail;
793                 }
794         }
795
796         /* was it transmitted over the IPsec tunnel SA? */
797         if (sav->flags & SADB_X_EXT_OLD) {
798                 /* RFC 1826 */
799                 stripsiz = sizeof(struct ah) + siz1;
800         } else {
801                 /* RFC 2402 */
802                 stripsiz = sizeof(struct newah) + siz1;
803         }
804         if (ipsec6_tunnel_validate(m, off + stripsiz, nxt, sav)) {
805                 /*
806                  * strip off all the headers that precedes AH.
807                  *      IP6 xx AH IP6' payload -> IP6' payload
808                  *
809                  * XXX more sanity checks
810                  * XXX relationship with gif?
811                  */
812                 u_int32_t flowinfo;     /* net endian */
813
814                 flowinfo = ip6->ip6_flow;
815                 m_adj(m, off + stripsiz);
816                 if (m->m_len < sizeof(*ip6)) {
817                         /*
818                          * m_pullup is prohibited in KAME IPv6 input processing
819                          * but there's no other way!
820                          */
821                         m = m_pullup(m, sizeof(*ip6));
822                         if (!m) {
823                                 ipsec6stat.in_inval++;
824                                 goto fail;
825                         }
826                 }
827                 ip6 = mtod(m, struct ip6_hdr *);
828                 /* ECN consideration. */
829                 ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow);
830                 if (!key_checktunnelsanity(sav, AF_INET6,
831                             (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) {
832                         ipseclog((LOG_NOTICE, "ipsec tunnel address mismatch "
833                             "in IPv6 AH input: %s %s\n",
834                             ipsec6_logpacketstr(ip6, spi),
835                             ipsec_logsastr(sav)));
836                         ipsec6stat.in_inval++;
837                         goto fail;
838                 }
839
840 #if 1
841                 /*
842                  * should the inner packet be considered authentic?
843                  * see comment in ah4_input().
844                  */
845                 m->m_flags &= ~M_AUTHIPHDR;
846                 m->m_flags &= ~M_AUTHIPDGM;
847 #endif
848
849                 key_sa_recordxfer(sav, m);
850                 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0 ||
851                     ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) {
852                         ipsec6stat.in_nomem++;
853                         goto fail;
854                 }
855
856                 if (netisr_queue(NETISR_IPV6, m)) {
857                         ipsecstat.in_inval++;
858                         m = NULL;
859                         goto fail;
860                 }
861
862                 nxt = IPPROTO_DONE;
863         } else {
864                 /*
865                  * strip off AH.
866                  */
867                 char *prvnxtp;
868
869                 /*
870                  * Copy the value of the next header field of AH to the
871                  * next header field of the previous header.
872                  * This is necessary because AH will be stripped off below.
873                  */
874                 prvnxtp = ip6_get_prevhdr(m, off); /* XXX */
875                 *prvnxtp = nxt;
876
877                 ip6 = mtod(m, struct ip6_hdr *);
878 #ifndef PULLDOWN_TEST
879                 /*
880                  * We do deep-copy since KAME requires that
881                  * the packet is placed in a single mbuf.
882                  */
883                 ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
884                 m->m_data += stripsiz;
885                 m->m_len -= stripsiz;
886                 m->m_pkthdr.len -= stripsiz;
887 #else
888                 /*
889                  * even in m_pulldown case, we need to strip off AH so that
890                  * we can compute checksum for multiple AH correctly.
891                  */
892                 if (m->m_len >= stripsiz + off) {
893                         ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off);
894                         m->m_data += stripsiz;
895                         m->m_len -= stripsiz;
896                         m->m_pkthdr.len -= stripsiz;
897                 } else {
898                         /*
899                          * this comes with no copy if the boundary is on
900                          * cluster
901                          */
902                         struct mbuf *n;
903
904                         n = m_split(m, off, MB_DONTWAIT);
905                         if (n == NULL) {
906                                 /* m is retained by m_split */
907                                 goto fail;
908                         }
909                         m_adj(n, stripsiz);
910                         m_cat(m, n);
911                         /* m_cat does not update m_pkthdr.len */
912                         m->m_pkthdr.len += n->m_pkthdr.len;
913                 }
914 #endif
915                 ip6 = mtod(m, struct ip6_hdr *);
916                 /* XXX jumbogram */
917                 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz);
918
919                 key_sa_recordxfer(sav, m);
920                 if (ipsec_addhist(m, IPPROTO_AH, spi) != 0) {
921                         ipsec6stat.in_nomem++;
922                         goto fail;
923                 }
924         }
925
926         *offp = off;
927         *mp = m;
928
929         if (sav) {
930                 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
931                         printf("DP ah6_input call free SA:%p\n", sav));
932                 key_freesav(sav);
933         }
934         ipsec6stat.in_success++;
935         return nxt;
936
937 fail:
938         if (sav) {
939                 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
940                         printf("DP ah6_input call free SA:%p\n", sav));
941                 key_freesav(sav);
942         }
943         if (m)
944                 m_freem(m);
945         return IPPROTO_DONE;
946 }
947
948 void
949 ah6_ctlinput(int cmd, struct sockaddr *sa, void *d)
950 {
951         const struct newah *ahp;
952         struct newah ah;
953         struct secasvar *sav;
954         struct ip6_hdr *ip6;
955         struct mbuf *m;
956         struct ip6ctlparam *ip6cp = NULL;
957         int off;
958         struct sockaddr_in6 *sa6_src, *sa6_dst;
959
960         if (sa->sa_family != AF_INET6 ||
961             sa->sa_len != sizeof(struct sockaddr_in6))
962                 return;
963         if ((unsigned)cmd >= PRC_NCMDS)
964                 return;
965
966         /* if the parameter is from icmp6, decode it. */
967         if (d != NULL) {
968                 ip6cp = (struct ip6ctlparam *)d;
969                 m = ip6cp->ip6c_m;
970                 ip6 = ip6cp->ip6c_ip6;
971                 off = ip6cp->ip6c_off;
972         } else {
973                 m = NULL;
974                 ip6 = NULL;
975                 off = 0; /* fix warning */
976         }
977
978         if (ip6) {
979                 /*
980                  * XXX: We assume that when ip6 is non NULL,
981                  * M and OFF are valid.
982                  */
983
984                 /* check if we can safely examine src and dst ports */
985                 if (m->m_pkthdr.len < off + sizeof(ah))
986                         return;
987
988                 if (m->m_len < off + sizeof(ah)) {
989                         /*
990                          * this should be rare case,
991                          * so we compromise on this copy...
992                          */
993                         m_copydata(m, off, sizeof(ah), (caddr_t)&ah);
994                         ahp = &ah;
995                 } else
996                         ahp = (struct newah *)(mtod(m, caddr_t) + off);
997
998                 if (cmd == PRC_MSGSIZE) {
999                         int valid = 0;
1000
1001                         /*
1002                          * Check to see if we have a valid SA corresponding to
1003                          * the address in the ICMP message payload.
1004                          */
1005                         sa6_src = ip6cp->ip6c_src;
1006                         sa6_dst = (struct sockaddr_in6 *)sa;
1007                         sav = key_allocsa(AF_INET6,
1008                                           (caddr_t)&sa6_src->sin6_addr,
1009                                           (caddr_t)&sa6_dst->sin6_addr,
1010                                           IPPROTO_AH, ahp->ah_spi);
1011                         if (sav) {
1012                                 if (sav->state == SADB_SASTATE_MATURE ||
1013                                     sav->state == SADB_SASTATE_DYING)
1014                                         valid++;
1015                                 key_freesav(sav);
1016                         }
1017
1018                         /* XXX Further validation? */
1019
1020                         /*
1021                          * Depending on the value of "valid" and routing table
1022                          * size (mtudisc_{hi,lo}wat), we will:
1023                          * - recalcurate the new MTU and create the
1024                          *   corresponding routing entry, or
1025                          * - ignore the MTU change notification.
1026                          */
1027                         icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
1028                 }
1029
1030                 /* we normally notify single pcb here */
1031         } else {
1032                 /* we normally notify any pcb here */
1033         }
1034 }
1035 #endif /* INET6 */