Merge from vendor branch CVS:
[dragonfly.git] / sys / netinet6 / ah_output.c
1 /*      $FreeBSD: src/sys/netinet6/ah_output.c,v 1.1.2.5 2003/05/06 06:46:58 suz Exp $  */
2 /*      $DragonFly: src/sys/netinet6/ah_output.c,v 1.6 2004/06/02 14:43:01 eirikn Exp $ */
3 /*      $KAME: ah_output.c,v 1.31 2001/07/26 06:53:15 jinmei 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/mbuf.h>
44 #include <sys/domain.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.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
55 #include <netinet/in.h>
56
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59 #include <netinet/in_var.h>
60
61 #ifdef INET6
62 #include <netinet/ip6.h>
63 #include <netinet6/ip6_var.h>
64 #include <netinet/icmp6.h>
65 #endif
66
67 #include <netinet6/ipsec.h>
68 #ifdef INET6
69 #include <netinet6/ipsec6.h>
70 #endif
71 #include <netinet6/ah.h>
72 #ifdef INET6
73 #include <netinet6/ah6.h>
74 #endif
75 #include <netproto/key/key.h>
76 #include <netproto/key/keydb.h>
77
78 #include <net/net_osdep.h>
79
80 #ifdef INET
81 static struct in_addr *ah4_finaldst (struct mbuf *);
82 #endif
83
84 /*
85  * compute AH header size.
86  * transport mode only.  for tunnel mode, we should implement
87  * virtual interface, and control MTU/MSS by the interface MTU.
88  */
89 size_t
90 ah_hdrsiz(struct ipsecrequest *isr)
91 {
92         const struct ah_algorithm *algo;
93         size_t hdrsiz;
94
95         /* sanity check */
96         if (isr == NULL)
97                 panic("ah_hdrsiz: NULL was passed.");
98
99         if (isr->saidx.proto != IPPROTO_AH)
100                 panic("unsupported mode passed to ah_hdrsiz");
101
102         if (isr->sav == NULL)
103                 goto estimate;
104         if (isr->sav->state != SADB_SASTATE_MATURE
105          && isr->sav->state != SADB_SASTATE_DYING)
106                 goto estimate;
107
108         /* we need transport mode AH. */
109         algo = ah_algorithm_lookup(isr->sav->alg_auth);
110         if (!algo)
111                 goto estimate;
112
113         /*
114          * XXX
115          * right now we don't calcurate the padding size.  simply
116          * treat the padding size as constant, for simplicity.
117          *
118          * XXX variable size padding support
119          */
120         hdrsiz = (((*algo->sumsiz)(isr->sav) + 3) & ~(4 - 1));
121         if (isr->sav->flags & SADB_X_EXT_OLD)
122                 hdrsiz += sizeof(struct ah);
123         else
124                 hdrsiz += sizeof(struct newah);
125
126         return hdrsiz;
127
128     estimate:
129         /* ASSUMING:
130          *      sizeof(struct newah) > sizeof(struct ah).
131          *      16 = (16 + 3) & ~(4 - 1).
132          */
133         return sizeof(struct newah) + 16;
134 }
135
136 #ifdef INET
137 /*
138  * Modify the packet so that it includes the authentication data.
139  * The mbuf passed must start with IPv4 header.
140  *
141  * assumes that the first mbuf contains IPv4 header + option only.
142  * the function does not modify m.
143  */
144 int
145 ah4_output(struct mbuf *m, struct ipsecrequest *isr)
146 {
147         struct secasvar *sav = isr->sav;
148         const struct ah_algorithm *algo;
149         u_int32_t spi;
150         u_char *ahdrpos;
151         u_char *ahsumpos = NULL;
152         size_t hlen = 0;        /* IP header+option in bytes */
153         size_t plen = 0;        /* AH payload size in bytes */
154         size_t ahlen = 0;       /* plen + sizeof(ah) */
155         struct ip *ip;
156         struct in_addr dst;
157         struct in_addr *finaldst;
158         int error;
159
160         /* sanity checks */
161         if ((sav->flags & SADB_X_EXT_OLD) == 0 && !sav->replay) {
162                 struct ip *ip;
163
164                 ip = mtod(m, struct ip *);
165                 ipseclog((LOG_DEBUG, "ah4_output: internal error: "
166                         "sav->replay is null: %x->%x, SPI=%u\n",
167                         (u_int32_t)ntohl(ip->ip_src.s_addr),
168                         (u_int32_t)ntohl(ip->ip_dst.s_addr),
169                         (u_int32_t)ntohl(sav->spi)));
170                 ipsecstat.out_inval++;
171                 m_freem(m);
172                 return EINVAL;
173         }
174
175         algo = ah_algorithm_lookup(sav->alg_auth);
176         if (!algo) {
177                 ipseclog((LOG_ERR, "ah4_output: unsupported algorithm: "
178                     "SPI=%u\n", (u_int32_t)ntohl(sav->spi)));
179                 ipsecstat.out_inval++;
180                 m_freem(m);
181                 return EINVAL;
182         }
183         spi = sav->spi;
184
185         /*
186          * determine the size to grow.
187          */
188         if (sav->flags & SADB_X_EXT_OLD) {
189                 /* RFC 1826 */
190                 plen = ((*algo->sumsiz)(sav) + 3) & ~(4 - 1); /* XXX pad to 8byte? */
191                 ahlen = plen + sizeof(struct ah);
192         } else {
193                 /* RFC 2402 */
194                 plen = ((*algo->sumsiz)(sav) + 3) & ~(4 - 1); /* XXX pad to 8byte? */
195                 ahlen = plen + sizeof(struct newah);
196         }
197
198         /*
199          * grow the mbuf to accomodate AH.
200          */
201         ip = mtod(m, struct ip *);
202 #ifdef _IP_VHL
203         hlen = IP_VHL_HL(ip->ip_vhl) << 2;
204 #else
205         hlen = ip->ip_hl << 2;
206 #endif
207
208         if (m->m_len != hlen)
209                 panic("ah4_output: assumption failed (first mbuf length)");
210         if (M_LEADINGSPACE(m->m_next) < ahlen) {
211                 struct mbuf *n;
212                 MGET(n, MB_DONTWAIT, MT_DATA);
213                 if (!n) {
214                         ipseclog((LOG_DEBUG, "ENOBUFS in ah4_output %d\n",
215                             __LINE__));
216                         m_freem(m);
217                         return ENOBUFS;
218                 }
219                 n->m_len = ahlen;
220                 n->m_next = m->m_next;
221                 m->m_next = n;
222                 m->m_pkthdr.len += ahlen;
223                 ahdrpos = mtod(n, u_char *);
224         } else {
225                 m->m_next->m_len += ahlen;
226                 m->m_next->m_data -= ahlen;
227                 m->m_pkthdr.len += ahlen;
228                 ahdrpos = mtod(m->m_next, u_char *);
229         }
230
231         ip = mtod(m, struct ip *);      /* just to be sure */
232
233         /*
234          * initialize AH.
235          */
236         if (sav->flags & SADB_X_EXT_OLD) {
237                 struct ah *ahdr;
238
239                 ahdr = (struct ah *)ahdrpos;
240                 ahsumpos = (u_char *)(ahdr + 1);
241                 ahdr->ah_len = plen >> 2;
242                 ahdr->ah_nxt = ip->ip_p;
243                 ahdr->ah_reserve = htons(0);
244                 ahdr->ah_spi = spi;
245                 bzero(ahdr + 1, plen);
246         } else {
247                 struct newah *ahdr;
248
249                 ahdr = (struct newah *)ahdrpos;
250                 ahsumpos = (u_char *)(ahdr + 1);
251                 ahdr->ah_len = (plen >> 2) + 1; /* plus one for seq# */
252                 ahdr->ah_nxt = ip->ip_p;
253                 ahdr->ah_reserve = htons(0);
254                 ahdr->ah_spi = spi;
255                 if (sav->replay->count == ~0) {
256                         if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
257                                 /* XXX Is it noisy ? */
258                                 ipseclog((LOG_WARNING,
259                                     "replay counter overflowed. %s\n",
260                                     ipsec_logsastr(sav)));
261                                 ipsecstat.out_inval++;
262                                 m_freem(m);
263                                 return EINVAL;
264                         }
265                 }
266                 sav->replay->count++;
267                 /*
268                  * XXX sequence number must not be cycled, if the SA is
269                  * installed by IKE daemon.
270                  */
271                 ahdr->ah_seq = htonl(sav->replay->count);
272                 bzero(ahdr + 1, plen);
273         }
274
275         /*
276          * modify IPv4 header.
277          */
278         ip->ip_p = IPPROTO_AH;
279         if (ahlen < (IP_MAXPACKET - ntohs(ip->ip_len)))
280                 ip->ip_len = htons(ntohs(ip->ip_len) + ahlen);
281         else {
282                 ipseclog((LOG_ERR, "IPv4 AH output: size exceeds limit\n"));
283                 ipsecstat.out_inval++;
284                 m_freem(m);
285                 return EMSGSIZE;
286         }
287
288         /*
289          * If there is source routing option, update destination field in
290          * the IPv4 header to the final destination.
291          * Note that we do not need to update source routing option itself
292          * (as done in IPv4 AH processing -- see ip6_output()), since
293          * source routing option is not part of the ICV computation.
294          */
295         finaldst = ah4_finaldst(m);
296         if (finaldst) {
297                 dst.s_addr = ip->ip_dst.s_addr;
298                 ip->ip_dst.s_addr = finaldst->s_addr;
299         }
300
301         /*
302          * calcurate the checksum, based on security association
303          * and the algorithm specified.
304          */
305         error = ah4_calccksum(m, (caddr_t)ahsumpos, plen, algo, sav);
306         if (error) {
307                 ipseclog((LOG_ERR,
308                     "error after ah4_calccksum, called from ah4_output"));
309                 m_freem(m);
310                 m = NULL;
311                 ipsecstat.out_inval++;
312                 return error;
313         }
314
315         if (finaldst) {
316                 ip = mtod(m, struct ip *);      /* just to make sure */
317                 ip->ip_dst.s_addr = dst.s_addr;
318         }
319         ipsecstat.out_success++;
320         ipsecstat.out_ahhist[sav->alg_auth]++;
321         key_sa_recordxfer(sav, m);
322
323         return 0;
324 }
325 #endif
326
327 /* Calculate AH length */
328 int
329 ah_hdrlen(struct secasvar *sav)
330 {
331         const struct ah_algorithm *algo;
332         int plen, ahlen;
333         
334         algo = ah_algorithm_lookup(sav->alg_auth);
335         if (!algo)
336                 return 0;
337         if (sav->flags & SADB_X_EXT_OLD) {
338                 /* RFC 1826 */
339                 plen = ((*algo->sumsiz)(sav) + 3) & ~(4 - 1);   /* XXX pad to 8byte? */
340                 ahlen = plen + sizeof(struct ah);
341         } else {
342                 /* RFC 2402 */
343                 plen = ((*algo->sumsiz)(sav) + 3) & ~(4 - 1);   /* XXX pad to 8byte? */
344                 ahlen = plen + sizeof(struct newah);
345         }
346
347         return(ahlen);
348 }
349
350 #ifdef INET6
351 /*
352  * Fill in the Authentication Header and calculate checksum.
353  */
354 int
355 ah6_output(struct mbuf *m, u_char *nexthdrp, struct mbuf *md,
356            struct ipsecrequest *isr)
357 {
358         struct mbuf *mprev;
359         struct mbuf *mah;
360         struct secasvar *sav = isr->sav;
361         const struct ah_algorithm *algo;
362         u_int32_t spi;
363         u_char *ahsumpos = NULL;
364         size_t plen;    /* AH payload size in bytes */
365         int error = 0;
366         int ahlen;
367         struct ip6_hdr *ip6;
368
369         if (m->m_len < sizeof(struct ip6_hdr)) {
370                 ipseclog((LOG_DEBUG, "ah6_output: first mbuf too short\n"));
371                 m_freem(m);
372                 return EINVAL;
373         }
374
375         ahlen = ah_hdrlen(sav);
376         if (ahlen == 0)
377                 return 0;
378
379         for (mprev = m; mprev && mprev->m_next != md; mprev = mprev->m_next)
380                 ;
381         if (!mprev || mprev->m_next != md) {
382                 ipseclog((LOG_DEBUG, "ah6_output: md is not in chain\n"));
383                 m_freem(m);
384                 return EINVAL;
385         }
386
387         MGET(mah, MB_DONTWAIT, MT_DATA);
388         if (!mah) {
389                 m_freem(m);
390                 return ENOBUFS;
391         }
392         if (ahlen > MLEN) {
393                 MCLGET(mah, MB_DONTWAIT);
394                 if ((mah->m_flags & M_EXT) == 0) {
395                         m_free(mah);
396                         m_freem(m);
397                         return ENOBUFS;
398                 }
399         }
400         mah->m_len = ahlen;
401         mah->m_next = md;
402         mprev->m_next = mah;
403         m->m_pkthdr.len += ahlen;
404
405         /* fix plen */
406         if (m->m_pkthdr.len - sizeof(struct ip6_hdr) > IPV6_MAXPACKET) {
407                 ipseclog((LOG_ERR,
408                     "ip6_output: AH with IPv6 jumbogram is not supported\n"));
409                 m_freem(m);
410                 return EINVAL;
411         }
412         ip6 = mtod(m, struct ip6_hdr *);
413         ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
414
415         if ((sav->flags & SADB_X_EXT_OLD) == 0 && !sav->replay) {
416                 ipseclog((LOG_DEBUG, "ah6_output: internal error: "
417                         "sav->replay is null: SPI=%u\n",
418                         (u_int32_t)ntohl(sav->spi)));
419                 ipsec6stat.out_inval++;
420                 m_freem(m);
421                 return EINVAL;
422         }
423
424         algo = ah_algorithm_lookup(sav->alg_auth);
425         if (!algo) {
426                 ipseclog((LOG_ERR, "ah6_output: unsupported algorithm: "
427                     "SPI=%u\n", (u_int32_t)ntohl(sav->spi)));
428                 ipsec6stat.out_inval++;
429                 m_freem(m);
430                 return EINVAL;
431         }
432         spi = sav->spi;
433
434         /*
435          * initialize AH.
436          */
437         if (sav->flags & SADB_X_EXT_OLD) {
438                 struct ah *ahdr = mtod(mah, struct ah *);
439
440                 plen = mah->m_len - sizeof(struct ah);
441                 ahsumpos = (u_char *)(ahdr + 1);
442                 ahdr->ah_nxt = *nexthdrp;
443                 *nexthdrp = IPPROTO_AH;
444                 ahdr->ah_len = plen >> 2;
445                 ahdr->ah_reserve = htons(0);
446                 ahdr->ah_spi = spi;
447                 bzero(ahdr + 1, plen);
448         } else {
449                 struct newah *ahdr = mtod(mah, struct newah *);
450
451                 plen = mah->m_len - sizeof(struct newah);
452                 ahsumpos = (u_char *)(ahdr + 1);
453                 ahdr->ah_nxt = *nexthdrp;
454                 *nexthdrp = IPPROTO_AH;
455                 ahdr->ah_len = (plen >> 2) + 1; /* plus one for seq# */
456                 ahdr->ah_reserve = htons(0);
457                 ahdr->ah_spi = spi;
458                 if (sav->replay->count == ~0) {
459                         if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
460                                 /* XXX Is it noisy ? */
461                                 ipseclog((LOG_WARNING,
462                                     "replay counter overflowed. %s\n",
463                                     ipsec_logsastr(sav)));
464                                 ipsec6stat.out_inval++;
465                                 m_freem(m);
466                                 return EINVAL;
467                         }
468                 }
469                 sav->replay->count++;
470                 /*
471                  * XXX sequence number must not be cycled, if the SA is
472                  * installed by IKE daemon.
473                  */
474                 ahdr->ah_seq = htonl(sav->replay->count);
475                 bzero(ahdr + 1, plen);
476         }
477
478         /*
479          * calcurate the checksum, based on security association
480          * and the algorithm specified.
481          */
482         error = ah6_calccksum(m, (caddr_t)ahsumpos, plen, algo, sav);
483         if (error) {
484                 ipsec6stat.out_inval++;
485                 m_freem(m);
486         } else {
487                 ipsec6stat.out_success++;
488                 key_sa_recordxfer(sav, m);
489         }
490         ipsec6stat.out_ahhist[sav->alg_auth]++;
491
492         return(error);
493 }
494 #endif
495
496 #ifdef INET
497 /*
498  * Find the final destination if there is loose/strict source routing option.
499  * Returns NULL if there's no source routing options.
500  * Returns NULL on errors too.
501  * Note that this function will return a pointer INTO the given parameter,
502  * struct mbuf *m.
503  * The mbuf must be pulled up toward, at least, ip option part.
504  */
505 static struct in_addr *
506 ah4_finaldst(struct mbuf *m)
507 {
508         struct ip *ip;
509         int optlen;
510         u_char *q;
511         int i;
512         int hlen;
513
514         if (!m)
515                 panic("ah4_finaldst: m == NULL");
516         ip = mtod(m, struct ip *);
517         hlen = (ip->ip_hl << 2);
518
519         if (m->m_len < hlen) {
520                 ipseclog((LOG_DEBUG,
521                     "ah4_finaldst: parameter mbuf wrong (not pulled up)\n"));
522                 return NULL;
523         }
524
525         if (hlen == sizeof(struct ip))
526                 return NULL;
527
528         optlen = hlen - sizeof(struct ip);
529         if (optlen < 0) {
530                 ipseclog((LOG_DEBUG, "ah4_finaldst: wrong optlen %d\n",
531                     optlen));
532                 return NULL;
533         }
534
535         q = (u_char *)(ip + 1);
536         i = 0;
537         while (i < optlen) {
538                 if (i + IPOPT_OPTVAL >= optlen)
539                         return NULL;
540                 if (q[i + IPOPT_OPTVAL] == IPOPT_EOL ||
541                     q[i + IPOPT_OPTVAL] == IPOPT_NOP ||
542                     i + IPOPT_OLEN < optlen)
543                         ;
544                 else
545                         return NULL;
546
547                 switch (q[i + IPOPT_OPTVAL]) {
548                 case IPOPT_EOL:
549                         i = optlen;     /* bye */
550                         break;
551                 case IPOPT_NOP:
552                         i++;
553                         break;
554                 case IPOPT_LSRR:
555                 case IPOPT_SSRR:
556                         if (q[i + IPOPT_OLEN] < 2 + sizeof(struct in_addr) ||
557                             optlen - i < q[i + IPOPT_OLEN]) {
558                                 ipseclog((LOG_ERR,
559                                     "ip_finaldst: invalid IP option "
560                                     "(code=%02x len=%02x)\n",
561                                     q[i + IPOPT_OPTVAL], q[i + IPOPT_OLEN]));
562                                 return NULL;
563                         }
564                         i += q[i + IPOPT_OLEN] - sizeof(struct in_addr);
565                         return (struct in_addr *)(q + i);
566                 default:
567                         if (q[i + IPOPT_OLEN] < 2 ||
568                             optlen - i < q[i + IPOPT_OLEN]) {
569                                 ipseclog((LOG_ERR,
570                                     "ip_finaldst: invalid IP option "
571                                     "(code=%02x len=%02x)\n",
572                                     q[i + IPOPT_OPTVAL], q[i + IPOPT_OLEN]));
573                                 return NULL;
574                         }
575                         i += q[i + IPOPT_OLEN];
576                         break;
577                 }
578         }
579         return NULL;
580 }
581 #endif