1 /* $FreeBSD: src/sys/netinet6/ah_output.c,v 1.1.2.5 2003/05/06 06:46:58 suz Exp $ */
2 /* $KAME: ah_output.c,v 1.31 2001/07/26 06:53:15 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 * RFC1826/2402 authentication header.
38 #include "opt_inet6.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/errno.h>
49 #include <sys/syslog.h>
52 #include <net/route.h>
54 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/in_var.h>
61 #include <netinet/ip6.h>
62 #include <netinet6/ip6_var.h>
63 #include <netinet/icmp6.h>
66 #include <netinet6/ipsec.h>
68 #include <netinet6/ipsec6.h>
70 #include <netinet6/ah.h>
72 #include <netinet6/ah6.h>
74 #include <netproto/key/key.h>
75 #include <netproto/key/keydb.h>
77 #include <net/net_osdep.h>
80 static struct in_addr
*ah4_finaldst (struct mbuf
*);
84 * compute AH header size.
85 * transport mode only. for tunnel mode, we should implement
86 * virtual interface, and control MTU/MSS by the interface MTU.
89 ah_hdrsiz(struct ipsecrequest
*isr
)
91 const struct ah_algorithm
*algo
;
96 panic("ah_hdrsiz: NULL was passed.");
98 if (isr
->saidx
.proto
!= IPPROTO_AH
)
99 panic("unsupported mode passed to ah_hdrsiz");
101 if (isr
->sav
== NULL
)
103 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
104 && isr
->sav
->state
!= SADB_SASTATE_DYING
)
107 /* we need transport mode AH. */
108 algo
= ah_algorithm_lookup(isr
->sav
->alg_auth
);
114 * right now we don't calcurate the padding size. simply
115 * treat the padding size as constant, for simplicity.
117 * XXX variable size padding support
119 hdrsiz
= (((*algo
->sumsiz
)(isr
->sav
) + 3) & ~(4 - 1));
120 if (isr
->sav
->flags
& SADB_X_EXT_OLD
)
121 hdrsiz
+= sizeof(struct ah
);
123 hdrsiz
+= sizeof(struct newah
);
129 * sizeof(struct newah) > sizeof(struct ah).
130 * 16 = (16 + 3) & ~(4 - 1).
132 return sizeof(struct newah
) + 16;
137 * Modify the packet so that it includes the authentication data.
138 * The mbuf passed must start with IPv4 header.
140 * assumes that the first mbuf contains IPv4 header + option only.
141 * the function does not modify m.
144 ah4_output(struct mbuf
*m
, struct ipsecrequest
*isr
)
146 struct secasvar
*sav
= isr
->sav
;
147 const struct ah_algorithm
*algo
;
150 u_char
*ahsumpos
= NULL
;
151 size_t hlen
= 0; /* IP header+option in bytes */
152 size_t plen
= 0; /* AH payload size in bytes */
153 size_t ahlen
= 0; /* plen + sizeof(ah) */
156 struct in_addr
*finaldst
;
160 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && !sav
->replay
) {
163 ip
= mtod(m
, struct ip
*);
164 ipseclog((LOG_DEBUG
, "ah4_output: internal error: "
165 "sav->replay is null: %x->%x, SPI=%u\n",
166 (u_int32_t
)ntohl(ip
->ip_src
.s_addr
),
167 (u_int32_t
)ntohl(ip
->ip_dst
.s_addr
),
168 (u_int32_t
)ntohl(sav
->spi
)));
169 ipsecstat
.out_inval
++;
174 algo
= ah_algorithm_lookup(sav
->alg_auth
);
176 ipseclog((LOG_ERR
, "ah4_output: unsupported algorithm: "
177 "SPI=%u\n", (u_int32_t
)ntohl(sav
->spi
)));
178 ipsecstat
.out_inval
++;
185 * determine the size to grow.
187 if (sav
->flags
& SADB_X_EXT_OLD
) {
189 plen
= ((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1); /* XXX pad to 8byte? */
190 ahlen
= plen
+ sizeof(struct ah
);
193 plen
= ((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1); /* XXX pad to 8byte? */
194 ahlen
= plen
+ sizeof(struct newah
);
198 * grow the mbuf to accomodate AH.
200 ip
= mtod(m
, struct ip
*);
202 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
204 hlen
= ip
->ip_hl
<< 2;
207 if (m
->m_len
!= hlen
)
208 panic("ah4_output: assumption failed (first mbuf length)");
209 if (M_LEADINGSPACE(m
->m_next
) < ahlen
) {
211 MGET(n
, M_NOWAIT
, MT_DATA
);
213 ipseclog((LOG_DEBUG
, "ENOBUFS in ah4_output %d\n",
219 n
->m_next
= m
->m_next
;
221 m
->m_pkthdr
.len
+= ahlen
;
222 ahdrpos
= mtod(n
, u_char
*);
224 m
->m_next
->m_len
+= ahlen
;
225 m
->m_next
->m_data
-= ahlen
;
226 m
->m_pkthdr
.len
+= ahlen
;
227 ahdrpos
= mtod(m
->m_next
, u_char
*);
230 ip
= mtod(m
, struct ip
*); /* just to be sure */
235 if (sav
->flags
& SADB_X_EXT_OLD
) {
238 ahdr
= (struct ah
*)ahdrpos
;
239 ahsumpos
= (u_char
*)(ahdr
+ 1);
240 ahdr
->ah_len
= plen
>> 2;
241 ahdr
->ah_nxt
= ip
->ip_p
;
242 ahdr
->ah_reserve
= htons(0);
244 bzero(ahdr
+ 1, plen
);
248 ahdr
= (struct newah
*)ahdrpos
;
249 ahsumpos
= (u_char
*)(ahdr
+ 1);
250 ahdr
->ah_len
= (plen
>> 2) + 1; /* plus one for seq# */
251 ahdr
->ah_nxt
= ip
->ip_p
;
252 ahdr
->ah_reserve
= htons(0);
254 if (sav
->replay
->count
== ~0) {
255 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0) {
256 /* XXX Is it noisy ? */
257 ipseclog((LOG_WARNING
,
258 "replay counter overflowed. %s\n",
259 ipsec_logsastr(sav
)));
260 ipsecstat
.out_inval
++;
265 sav
->replay
->count
++;
267 * XXX sequence number must not be cycled, if the SA is
268 * installed by IKE daemon.
270 ahdr
->ah_seq
= htonl(sav
->replay
->count
& 0xffffffff);
271 bzero(ahdr
+ 1, plen
);
275 * modify IPv4 header.
277 ip
->ip_p
= IPPROTO_AH
;
278 if (ahlen
< (IP_MAXPACKET
- ntohs(ip
->ip_len
)))
279 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + ahlen
);
281 ipseclog((LOG_ERR
, "IPv4 AH output: size exceeds limit\n"));
282 ipsecstat
.out_inval
++;
288 * If there is source routing option, update destination field in
289 * the IPv4 header to the final destination.
290 * Note that we do not need to update source routing option itself
291 * (as done in IPv4 AH processing -- see ip6_output()), since
292 * source routing option is not part of the ICV computation.
294 finaldst
= ah4_finaldst(m
);
296 dst
.s_addr
= ip
->ip_dst
.s_addr
;
297 ip
->ip_dst
.s_addr
= finaldst
->s_addr
;
299 dst
.s_addr
= 0; /* fix compiler warning */
303 * calcurate the checksum, based on security association
304 * and the algorithm specified.
306 error
= ah4_calccksum(m
, (caddr_t
)ahsumpos
, plen
, algo
, sav
);
309 "error after ah4_calccksum, called from ah4_output"));
312 ipsecstat
.out_inval
++;
317 ip
= mtod(m
, struct ip
*); /* just to make sure */
318 ip
->ip_dst
.s_addr
= dst
.s_addr
;
320 ipsecstat
.out_success
++;
321 ipsecstat
.out_ahhist
[sav
->alg_auth
]++;
322 key_sa_recordxfer(sav
, m
);
328 /* Calculate AH length */
330 ah_hdrlen(struct secasvar
*sav
)
332 const struct ah_algorithm
*algo
;
335 algo
= ah_algorithm_lookup(sav
->alg_auth
);
338 if (sav
->flags
& SADB_X_EXT_OLD
) {
340 plen
= ((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1); /* XXX pad to 8byte? */
341 ahlen
= plen
+ sizeof(struct ah
);
344 plen
= ((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1); /* XXX pad to 8byte? */
345 ahlen
= plen
+ sizeof(struct newah
);
353 * Fill in the Authentication Header and calculate checksum.
356 ah6_output(struct mbuf
*m
, u_char
*nexthdrp
, struct mbuf
*md
,
357 struct ipsecrequest
*isr
)
361 struct secasvar
*sav
= isr
->sav
;
362 const struct ah_algorithm
*algo
;
364 u_char
*ahsumpos
= NULL
;
365 size_t plen
; /* AH payload size in bytes */
370 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
371 ipseclog((LOG_DEBUG
, "ah6_output: first mbuf too short\n"));
376 ahlen
= ah_hdrlen(sav
);
380 for (mprev
= m
; mprev
&& mprev
->m_next
!= md
; mprev
= mprev
->m_next
)
382 if (!mprev
|| mprev
->m_next
!= md
) {
383 ipseclog((LOG_DEBUG
, "ah6_output: md is not in chain\n"));
388 mah
= m_getb(ahlen
, M_NOWAIT
, MT_DATA
, 0);
396 m
->m_pkthdr
.len
+= ahlen
;
399 if (m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
) > IPV6_MAXPACKET
) {
401 "ah6_output: AH with IPv6 jumbogram is not supported\n"));
405 ip6
= mtod(m
, struct ip6_hdr
*);
406 ip6
->ip6_plen
= htons(m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
));
408 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && !sav
->replay
) {
409 ipseclog((LOG_DEBUG
, "ah6_output: internal error: "
410 "sav->replay is null: SPI=%u\n",
411 (u_int32_t
)ntohl(sav
->spi
)));
412 ipsec6stat
.out_inval
++;
417 algo
= ah_algorithm_lookup(sav
->alg_auth
);
419 ipseclog((LOG_ERR
, "ah6_output: unsupported algorithm: "
420 "SPI=%u\n", (u_int32_t
)ntohl(sav
->spi
)));
421 ipsec6stat
.out_inval
++;
430 if (sav
->flags
& SADB_X_EXT_OLD
) {
431 struct ah
*ahdr
= mtod(mah
, struct ah
*);
433 plen
= mah
->m_len
- sizeof(struct ah
);
434 ahsumpos
= (u_char
*)(ahdr
+ 1);
435 ahdr
->ah_nxt
= *nexthdrp
;
436 *nexthdrp
= IPPROTO_AH
;
437 ahdr
->ah_len
= plen
>> 2;
438 ahdr
->ah_reserve
= htons(0);
440 bzero(ahdr
+ 1, plen
);
442 struct newah
*ahdr
= mtod(mah
, struct newah
*);
444 plen
= mah
->m_len
- sizeof(struct newah
);
445 ahsumpos
= (u_char
*)(ahdr
+ 1);
446 ahdr
->ah_nxt
= *nexthdrp
;
447 *nexthdrp
= IPPROTO_AH
;
448 ahdr
->ah_len
= (plen
>> 2) + 1; /* plus one for seq# */
449 ahdr
->ah_reserve
= htons(0);
451 if (sav
->replay
->count
== ~0) {
452 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0) {
453 /* XXX Is it noisy ? */
454 ipseclog((LOG_WARNING
,
455 "replay counter overflowed. %s\n",
456 ipsec_logsastr(sav
)));
457 ipsec6stat
.out_inval
++;
462 sav
->replay
->count
++;
464 * XXX sequence number must not be cycled, if the SA is
465 * installed by IKE daemon.
467 ahdr
->ah_seq
= htonl(sav
->replay
->count
);
468 bzero(ahdr
+ 1, plen
);
472 * calcurate the checksum, based on security association
473 * and the algorithm specified.
475 error
= ah6_calccksum(m
, (caddr_t
)ahsumpos
, plen
, algo
, sav
);
477 ipsec6stat
.out_inval
++;
480 ipsec6stat
.out_success
++;
481 key_sa_recordxfer(sav
, m
);
483 ipsec6stat
.out_ahhist
[sav
->alg_auth
]++;
491 * Find the final destination if there is loose/strict source routing option.
492 * Returns NULL if there's no source routing options.
493 * Returns NULL on errors too.
494 * Note that this function will return a pointer INTO the given parameter,
496 * The mbuf must be pulled up toward, at least, ip option part.
498 static struct in_addr
*
499 ah4_finaldst(struct mbuf
*m
)
508 panic("ah4_finaldst: m == NULL");
509 ip
= mtod(m
, struct ip
*);
510 hlen
= (ip
->ip_hl
<< 2);
512 if (m
->m_len
< hlen
) {
514 "ah4_finaldst: parameter mbuf wrong (not pulled up)\n"));
518 if (hlen
== sizeof(struct ip
))
521 optlen
= hlen
- sizeof(struct ip
);
523 ipseclog((LOG_DEBUG
, "ah4_finaldst: wrong optlen %d\n",
528 q
= (u_char
*)(ip
+ 1);
531 if (i
+ IPOPT_OPTVAL
>= optlen
)
533 if (q
[i
+ IPOPT_OPTVAL
] == IPOPT_EOL
||
534 q
[i
+ IPOPT_OPTVAL
] == IPOPT_NOP
||
535 i
+ IPOPT_OLEN
< optlen
)
540 switch (q
[i
+ IPOPT_OPTVAL
]) {
542 i
= optlen
; /* bye */
549 if (q
[i
+ IPOPT_OLEN
] < 2 + sizeof(struct in_addr
) ||
550 optlen
- i
< q
[i
+ IPOPT_OLEN
]) {
552 "ip_finaldst: invalid IP option "
553 "(code=%02x len=%02x)\n",
554 q
[i
+ IPOPT_OPTVAL
], q
[i
+ IPOPT_OLEN
]));
557 i
+= q
[i
+ IPOPT_OLEN
] - sizeof(struct in_addr
);
558 return (struct in_addr
*)(q
+ i
);
560 if (q
[i
+ IPOPT_OLEN
] < 2 ||
561 optlen
- i
< q
[i
+ IPOPT_OLEN
]) {
563 "ip_finaldst: invalid IP option "
564 "(code=%02x len=%02x)\n",
565 q
[i
+ IPOPT_OPTVAL
], q
[i
+ IPOPT_OLEN
]));
568 i
+= q
[i
+ IPOPT_OLEN
];