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