If the current thread is not a network thread then rejecting the packet back
[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.9 2006/10/24 06:18:42 hsu 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 & 0xffffffff);
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         mah = m_getb(ahlen, MB_DONTWAIT, MT_DATA, 0);
388         if (mah == NULL) {
389                 m_freem(m);
390                 return ENOBUFS;
391         }
392         mah->m_len = ahlen;
393         mah->m_next = md;
394         mprev->m_next = mah;
395         m->m_pkthdr.len += ahlen;
396
397         /* fix plen */
398         if (m->m_pkthdr.len - sizeof(struct ip6_hdr) > IPV6_MAXPACKET) {
399                 ipseclog((LOG_ERR,
400                     "ah6_output: AH with IPv6 jumbogram is not supported\n"));
401                 m_freem(m);
402                 return EINVAL;
403         }
404         ip6 = mtod(m, struct ip6_hdr *);
405         ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
406
407         if ((sav->flags & SADB_X_EXT_OLD) == 0 && !sav->replay) {
408                 ipseclog((LOG_DEBUG, "ah6_output: internal error: "
409                         "sav->replay is null: SPI=%u\n",
410                         (u_int32_t)ntohl(sav->spi)));
411                 ipsec6stat.out_inval++;
412                 m_freem(m);
413                 return EINVAL;
414         }
415
416         algo = ah_algorithm_lookup(sav->alg_auth);
417         if (!algo) {
418                 ipseclog((LOG_ERR, "ah6_output: unsupported algorithm: "
419                     "SPI=%u\n", (u_int32_t)ntohl(sav->spi)));
420                 ipsec6stat.out_inval++;
421                 m_freem(m);
422                 return EINVAL;
423         }
424         spi = sav->spi;
425
426         /*
427          * initialize AH.
428          */
429         if (sav->flags & SADB_X_EXT_OLD) {
430                 struct ah *ahdr = mtod(mah, struct ah *);
431
432                 plen = mah->m_len - sizeof(struct ah);
433                 ahsumpos = (u_char *)(ahdr + 1);
434                 ahdr->ah_nxt = *nexthdrp;
435                 *nexthdrp = IPPROTO_AH;
436                 ahdr->ah_len = plen >> 2;
437                 ahdr->ah_reserve = htons(0);
438                 ahdr->ah_spi = spi;
439                 bzero(ahdr + 1, plen);
440         } else {
441                 struct newah *ahdr = mtod(mah, struct newah *);
442
443                 plen = mah->m_len - sizeof(struct newah);
444                 ahsumpos = (u_char *)(ahdr + 1);
445                 ahdr->ah_nxt = *nexthdrp;
446                 *nexthdrp = IPPROTO_AH;
447                 ahdr->ah_len = (plen >> 2) + 1; /* plus one for seq# */
448                 ahdr->ah_reserve = htons(0);
449                 ahdr->ah_spi = spi;
450                 if (sav->replay->count == ~0) {
451                         if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
452                                 /* XXX Is it noisy ? */
453                                 ipseclog((LOG_WARNING,
454                                     "replay counter overflowed. %s\n",
455                                     ipsec_logsastr(sav)));
456                                 ipsec6stat.out_inval++;
457                                 m_freem(m);
458                                 return EINVAL;
459                         }
460                 }
461                 sav->replay->count++;
462                 /*
463                  * XXX sequence number must not be cycled, if the SA is
464                  * installed by IKE daemon.
465                  */
466                 ahdr->ah_seq = htonl(sav->replay->count);
467                 bzero(ahdr + 1, plen);
468         }
469
470         /*
471          * calcurate the checksum, based on security association
472          * and the algorithm specified.
473          */
474         error = ah6_calccksum(m, (caddr_t)ahsumpos, plen, algo, sav);
475         if (error) {
476                 ipsec6stat.out_inval++;
477                 m_freem(m);
478         } else {
479                 ipsec6stat.out_success++;
480                 key_sa_recordxfer(sav, m);
481         }
482         ipsec6stat.out_ahhist[sav->alg_auth]++;
483
484         return (error);
485 }
486 #endif
487
488 #ifdef INET
489 /*
490  * Find the final destination if there is loose/strict source routing option.
491  * Returns NULL if there's no source routing options.
492  * Returns NULL on errors too.
493  * Note that this function will return a pointer INTO the given parameter,
494  * struct mbuf *m.
495  * The mbuf must be pulled up toward, at least, ip option part.
496  */
497 static struct in_addr *
498 ah4_finaldst(struct mbuf *m)
499 {
500         struct ip *ip;
501         int optlen;
502         u_char *q;
503         int i;
504         int hlen;
505
506         if (!m)
507                 panic("ah4_finaldst: m == NULL");
508         ip = mtod(m, struct ip *);
509         hlen = (ip->ip_hl << 2);
510
511         if (m->m_len < hlen) {
512                 ipseclog((LOG_DEBUG,
513                     "ah4_finaldst: parameter mbuf wrong (not pulled up)\n"));
514                 return NULL;
515         }
516
517         if (hlen == sizeof(struct ip))
518                 return NULL;
519
520         optlen = hlen - sizeof(struct ip);
521         if (optlen < 0) {
522                 ipseclog((LOG_DEBUG, "ah4_finaldst: wrong optlen %d\n",
523                     optlen));
524                 return NULL;
525         }
526
527         q = (u_char *)(ip + 1);
528         i = 0;
529         while (i < optlen) {
530                 if (i + IPOPT_OPTVAL >= optlen)
531                         return NULL;
532                 if (q[i + IPOPT_OPTVAL] == IPOPT_EOL ||
533                     q[i + IPOPT_OPTVAL] == IPOPT_NOP ||
534                     i + IPOPT_OLEN < optlen)
535                         ;
536                 else
537                         return NULL;
538
539                 switch (q[i + IPOPT_OPTVAL]) {
540                 case IPOPT_EOL:
541                         i = optlen;     /* bye */
542                         break;
543                 case IPOPT_NOP:
544                         i++;
545                         break;
546                 case IPOPT_LSRR:
547                 case IPOPT_SSRR:
548                         if (q[i + IPOPT_OLEN] < 2 + sizeof(struct in_addr) ||
549                             optlen - i < q[i + IPOPT_OLEN]) {
550                                 ipseclog((LOG_ERR,
551                                     "ip_finaldst: invalid IP option "
552                                     "(code=%02x len=%02x)\n",
553                                     q[i + IPOPT_OPTVAL], q[i + IPOPT_OLEN]));
554                                 return NULL;
555                         }
556                         i += q[i + IPOPT_OLEN] - sizeof(struct in_addr);
557                         return (struct in_addr *)(q + i);
558                 default:
559                         if (q[i + IPOPT_OLEN] < 2 ||
560                             optlen - i < q[i + IPOPT_OLEN]) {
561                                 ipseclog((LOG_ERR,
562                                     "ip_finaldst: invalid IP option "
563                                     "(code=%02x len=%02x)\n",
564                                     q[i + IPOPT_OPTVAL], q[i + IPOPT_OLEN]));
565                                 return NULL;
566                         }
567                         i += q[i + IPOPT_OLEN];
568                         break;
569                 }
570         }
571         return NULL;
572 }
573 #endif