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