e8c81b01f50979176a644e403c7138a1ea597b81
1 /* $KAME: ah_output.c,v 1.17 2000/03/09 08:54:48 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * RFC1826/2402 authentication header.
37 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/errno.h>
51 #include <sys/kernel.h>
52 #include <sys/syslog.h>
55 #include <net/route.h>
57 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
61 #include <netinet/in_var.h>
64 #include <netinet/ip6.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet/icmp6.h>
69 #include <netinet6/ipsec.h>
70 #include <netinet6/ah.h>
71 #include <netkey/key.h>
72 #include <netkey/keydb.h>
73 #include <netkey/key_debug.h>
75 #include <net/net_osdep.h>
77 static struct in_addr
*ah4_finaldst
__P((struct mbuf
*));
80 * compute AH header size.
81 * transport mode only. for tunnel mode, we should implement
82 * virtual interface, and control MTU/MSS by the interface MTU.
86 struct ipsecrequest
*isr
;
88 struct ah_algorithm
*algo
;
93 panic("ah_hdrsiz: NULL was passed.\n");
95 if (isr
->saidx
.proto
!= IPPROTO_AH
)
96 panic("unsupported mode passed to ah_hdrsiz");
100 if (isr
->sav
->state
!= SADB_SASTATE_MATURE
101 && isr
->sav
->state
!= SADB_SASTATE_DYING
)
104 /* we need transport mode AH. */
105 algo
= &ah_algorithms
[isr
->sav
->alg_auth
];
111 * right now we don't calcurate the padding size. simply
112 * treat the padding size as constant, for simplicity.
114 * XXX variable size padding support
116 hdrsiz
= (((*algo
->sumsiz
)(isr
->sav
) + 3) & ~(4 - 1));
117 if (isr
->sav
->flags
& SADB_X_EXT_OLD
)
118 hdrsiz
+= sizeof(struct ah
);
120 hdrsiz
+= sizeof(struct newah
);
126 * sizeof(struct newah) > sizeof(struct ah).
127 * 16 = (16 + 3) & ~(4 - 1).
129 return sizeof(struct newah
) + 16;
133 * Modify the packet so that it includes the authentication data.
134 * The mbuf passed must start with IPv4 header.
136 * assumes that the first mbuf contains IPv4 header + option only.
137 * the function does not modify m.
142 struct ipsecrequest
*isr
;
144 struct secasvar
*sav
= isr
->sav
;
145 struct ah_algorithm
*algo
;
148 u_char
*ahsumpos
= NULL
;
149 size_t hlen
= 0; /*IP header+option in bytes*/
150 size_t plen
= 0; /*AH payload size in bytes*/
151 size_t ahlen
= 0; /*plen + sizeof(ah)*/
154 struct in_addr
*finaldst
;
158 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && !sav
->replay
) {
161 ip
= mtod(m
, struct ip
*);
162 ipseclog((LOG_DEBUG
, "ah4_output: internal error: "
163 "sav->replay is null: %x->%x, SPI=%u\n",
164 (u_int32_t
)ntohl(ip
->ip_src
.s_addr
),
165 (u_int32_t
)ntohl(ip
->ip_dst
.s_addr
),
166 (u_int32_t
)ntohl(sav
->spi
)));
167 ipsecstat
.out_inval
++;
172 algo
= &ah_algorithms
[sav
->alg_auth
];
176 * determine the size to grow.
178 if (sav
->flags
& SADB_X_EXT_OLD
) {
180 plen
= ((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1); /*XXX pad to 8byte?*/
181 ahlen
= plen
+ sizeof(struct ah
);
184 plen
= ((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1); /*XXX pad to 8byte?*/
185 ahlen
= plen
+ sizeof(struct newah
);
189 * grow the mbuf to accomodate AH.
191 ip
= mtod(m
, struct ip
*);
193 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
195 hlen
= ip
->ip_hl
<< 2;
198 if (m
->m_len
!= hlen
)
199 panic("ah4_output: assumption failed (first mbuf length)");
200 if (M_LEADINGSPACE(m
->m_next
) < ahlen
) {
202 MGET(n
, M_DONTWAIT
, MT_DATA
);
204 ipseclog((LOG_DEBUG
, "ENOBUFS in ah4_output %d\n",
210 n
->m_next
= m
->m_next
;
212 m
->m_pkthdr
.len
+= ahlen
;
213 ahdrpos
= mtod(n
, u_char
*);
215 m
->m_next
->m_len
+= ahlen
;
216 m
->m_next
->m_data
-= ahlen
;
217 m
->m_pkthdr
.len
+= ahlen
;
218 ahdrpos
= mtod(m
->m_next
, u_char
*);
221 ip
= mtod(m
, struct ip
*); /*just to be sure*/
226 if (sav
->flags
& SADB_X_EXT_OLD
) {
229 ahdr
= (struct ah
*)ahdrpos
;
230 ahsumpos
= (u_char
*)(ahdr
+ 1);
231 ahdr
->ah_len
= plen
>> 2;
232 ahdr
->ah_nxt
= ip
->ip_p
;
233 ahdr
->ah_reserve
= htons(0);
235 bzero(ahdr
+ 1, plen
);
239 ahdr
= (struct newah
*)ahdrpos
;
240 ahsumpos
= (u_char
*)(ahdr
+ 1);
241 ahdr
->ah_len
= (plen
>> 2) + 1; /* plus one for seq# */
242 ahdr
->ah_nxt
= ip
->ip_p
;
243 ahdr
->ah_reserve
= htons(0);
245 if (sav
->replay
->count
== ~0) {
246 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0) {
247 /* XXX Is it noisy ? */
248 ipseclog((LOG_WARNING
,
249 "replay counter overflowed. %s\n",
250 ipsec_logsastr(sav
)));
251 ipsecstat
.out_inval
++;
256 sav
->replay
->count
++;
258 * XXX sequence number must not be cycled, if the SA is
259 * installed by IKE daemon.
261 ahdr
->ah_seq
= htonl(sav
->replay
->count
);
262 bzero(ahdr
+ 1, plen
);
266 * modify IPv4 header.
268 ip
->ip_p
= IPPROTO_AH
;
269 if (ahlen
< (IP_MAXPACKET
- ntohs(ip
->ip_len
)))
270 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + ahlen
);
272 ipseclog((LOG_ERR
, "IPv4 AH output: size exceeds limit\n"));
273 ipsecstat
.out_inval
++;
279 * If there is source routing option, update destination field in
280 * the IPv4 header to the final destination.
281 * Note that we do not need to update source routing option itself
282 * (as done in IPv4 AH processing -- see ip6_output()), since
283 * source routing option is not part of the ICV computation.
285 finaldst
= ah4_finaldst(m
);
287 dst
.s_addr
= ip
->ip_dst
.s_addr
;
288 ip
->ip_dst
.s_addr
= finaldst
->s_addr
;
292 * calcurate the checksum, based on security association
293 * and the algorithm specified.
295 error
= ah4_calccksum(m
, (caddr_t
)ahsumpos
, algo
, sav
);
298 "error after ah4_calccksum, called from ah4_output"));
300 ipsecstat
.out_inval
++;
305 ip
= mtod(m
, struct ip
*); /*just to make sure*/
306 ip
->ip_dst
.s_addr
= dst
.s_addr
;
308 ipsecstat
.out_success
++;
309 ipsecstat
.out_ahhist
[sav
->alg_auth
]++;
310 key_sa_recordxfer(sav
, m
);
315 /* Calculate AH length */
318 struct secasvar
*sav
;
320 struct ah_algorithm
*algo
;
323 algo
= &ah_algorithms
[sav
->alg_auth
];
324 if (sav
->flags
& SADB_X_EXT_OLD
) {
326 plen
= ((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1); /*XXX pad to 8byte?*/
327 ahlen
= plen
+ sizeof(struct ah
);
330 plen
= ((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1); /*XXX pad to 8byte?*/
331 ahlen
= plen
+ sizeof(struct newah
);
339 * Fill in the Authentication Header and calculate checksum.
342 ah6_output(m
, nexthdrp
, md
, isr
)
346 struct ipsecrequest
*isr
;
350 struct secasvar
*sav
= isr
->sav
;
351 struct ah_algorithm
*algo
;
353 u_char
*ahsumpos
= NULL
;
354 size_t plen
; /*AH payload size in bytes*/
359 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
360 ipseclog((LOG_DEBUG
, "ah6_output: first mbuf too short\n"));
365 ahlen
= ah_hdrlen(sav
);
369 for (mprev
= m
; mprev
&& mprev
->m_next
!= md
; mprev
= mprev
->m_next
)
371 if (!mprev
|| mprev
->m_next
!= md
) {
372 ipseclog((LOG_DEBUG
, "ah6_output: md is not in chain\n"));
377 MGET(mah
, M_DONTWAIT
, MT_DATA
);
383 MCLGET(mah
, M_DONTWAIT
);
384 if ((mah
->m_flags
& M_EXT
) == 0) {
393 m
->m_pkthdr
.len
+= ahlen
;
396 if (m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
) > IPV6_MAXPACKET
) {
398 "ip6_output: AH with IPv6 jumbogram is not supported\n"));
402 ip6
= mtod(m
, struct ip6_hdr
*);
403 ip6
->ip6_plen
= htons(m
->m_pkthdr
.len
- sizeof(struct ip6_hdr
));
405 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && !sav
->replay
) {
406 ipseclog((LOG_DEBUG
, "ah6_output: internal error: "
407 "sav->replay is null: SPI=%u\n",
408 (u_int32_t
)ntohl(sav
->spi
)));
409 ipsec6stat
.out_inval
++;
414 algo
= &ah_algorithms
[sav
->alg_auth
];
420 if (sav
->flags
& SADB_X_EXT_OLD
) {
421 struct ah
*ahdr
= mtod(mah
, struct ah
*);
423 plen
= mah
->m_len
- sizeof(struct ah
);
424 ahsumpos
= (u_char
*)(ahdr
+ 1);
425 ahdr
->ah_nxt
= *nexthdrp
;
426 *nexthdrp
= IPPROTO_AH
;
427 ahdr
->ah_len
= plen
>> 2;
428 ahdr
->ah_reserve
= htons(0);
430 bzero(ahdr
+ 1, plen
);
432 struct newah
*ahdr
= mtod(mah
, struct newah
*);
434 plen
= mah
->m_len
- sizeof(struct newah
);
435 ahsumpos
= (u_char
*)(ahdr
+ 1);
436 ahdr
->ah_nxt
= *nexthdrp
;
437 *nexthdrp
= IPPROTO_AH
;
438 ahdr
->ah_len
= (plen
>> 2) + 1; /* plus one for seq# */
439 ahdr
->ah_reserve
= htons(0);
441 if (sav
->replay
->count
== ~0) {
442 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0) {
443 /* XXX Is it noisy ? */
444 ipseclog((LOG_WARNING
,
445 "replay counter overflowed. %s\n",
446 ipsec_logsastr(sav
)));
447 ipsecstat
.out_inval
++;
452 sav
->replay
->count
++;
454 * XXX sequence number must not be cycled, if the SA is
455 * installed by IKE daemon.
457 ahdr
->ah_seq
= htonl(sav
->replay
->count
);
458 bzero(ahdr
+ 1, plen
);
462 * calcurate the checksum, based on security association
463 * and the algorithm specified.
465 error
= ah6_calccksum(m
, (caddr_t
)ahsumpos
, algo
, sav
);
467 ipsec6stat
.out_inval
++;
470 ipsec6stat
.out_success
++;
471 key_sa_recordxfer(sav
, m
);
473 ipsec6stat
.out_ahhist
[sav
->alg_auth
]++;
480 * Find the final destination if there is loose/strict source routing option.
481 * Returns NULL if there's no source routing options.
482 * Returns NULL on errors too.
483 * Note that this function will return a pointer INTO the given parameter,
485 * The mbuf must be pulled up toward, at least, ip option part.
487 static struct in_addr
*
498 panic("ah4_finaldst: m == NULL");
499 ip
= mtod(m
, struct ip
*);
501 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
503 hlen
= ip
->ip_hl
<< 2;
506 if (m
->m_len
< hlen
) {
508 "ah4_finaldst: parameter mbuf wrong (not pulled up)\n"));
512 if (hlen
== sizeof(struct ip
))
515 optlen
= hlen
- sizeof(struct ip
);
517 ipseclog((LOG_DEBUG
, "ah4_finaldst: wrong optlen %d\n",
522 q
= (u_char
*)(ip
+ 1);
525 switch (q
[i
+ IPOPT_OPTVAL
]) {
527 i
= optlen
; /* bye */
534 if (q
[i
+ IPOPT_OLEN
] <= 0
535 || optlen
- i
< q
[i
+ IPOPT_OLEN
]) {
537 "ip_finaldst: invalid IP option "
538 "(code=%02x len=%02x)\n",
539 q
[i
+ IPOPT_OPTVAL
], q
[i
+ IPOPT_OLEN
]));
542 i
+= q
[i
+ IPOPT_OLEN
] - sizeof(struct in_addr
);
543 return (struct in_addr
*)(q
+ i
);
545 if (q
[i
+ IPOPT_OLEN
] <= 0
546 || optlen
- i
< q
[i
+ IPOPT_OLEN
]) {
548 "ip_finaldst: invalid IP option "
549 "(code=%02x len=%02x)\n",
550 q
[i
+ IPOPT_OPTVAL
], q
[i
+ IPOPT_OLEN
]));
553 i
+= q
[i
+ IPOPT_OLEN
];