1 /* $FreeBSD: src/sys/netinet6/esp_output.c,v 1.1.2.4 2003/05/06 06:46:58 suz Exp $ */
2 /* $DragonFly: src/sys/netinet6/esp_output.c,v 1.9 2006/10/24 08:18:42 hsu Exp $ */
3 /* $KAME: esp_output.c,v 1.44 2001/07/26 06:53:15 jinmei Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
35 #include "opt_inet6.h"
38 * RFC1827/2406 Encapsulated Security Payload.
41 #include <sys/param.h>
42 #include <sys/systm.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>
50 #include <sys/syslog.h>
53 #include <net/route.h>
55 #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 <netinet6/esp.h>
76 #include <netinet6/esp6.h>
78 #include <netproto/key/key.h>
79 #include <netproto/key/keydb.h>
81 #include <net/net_osdep.h>
83 static int esp_output (struct mbuf
*, u_char
*, struct mbuf
*,
84 struct ipsecrequest
*, int);
87 * compute ESP header size.
90 esp_hdrsiz(struct ipsecrequest
*isr
)
93 const struct esp_algorithm
*algo
;
94 const struct ah_algorithm
*aalgo
;
101 panic("esp_hdrsiz: NULL was passed.");
105 if (isr
->saidx
.proto
!= IPPROTO_ESP
)
106 panic("unsupported mode passed to esp_hdrsiz");
110 if (sav
->state
!= SADB_SASTATE_MATURE
&&
111 sav
->state
!= SADB_SASTATE_DYING
)
114 /* we need transport mode ESP. */
115 algo
= esp_algorithm_lookup(sav
->alg_enc
);
124 * right now we don't calcurate the padding size. simply
125 * treat the padding size as constant, for simplicity.
127 * XXX variable size padding support
129 if (sav
->flags
& SADB_X_EXT_OLD
) {
131 hdrsiz
= sizeof(struct esp
) + ivlen
+ 9;
134 aalgo
= ah_algorithm_lookup(sav
->alg_auth
);
135 if (aalgo
&& sav
->replay
&& sav
->key_auth
)
136 authlen
= (aalgo
->sumsiz
)(sav
);
139 hdrsiz
= sizeof(struct newesp
) + ivlen
+ 9 + authlen
;
147 * sizeof(struct newesp) > sizeof(struct esp).
148 * esp_max_ivlen() = max ivlen for CBC mode
149 * 9 = (maximum padding length without random padding length)
150 * + (Pad Length field) + (Next Header field).
151 * 16 = maximum ICV we support.
153 return sizeof(struct newesp
) + esp_max_ivlen() + 9 + 16;
157 * Modify the packet so that the payload is encrypted.
158 * The mbuf (m) must start with IPv4 or IPv6 header.
159 * On failure, free the given mbuf and return NULL.
164 * IP ......... payload
165 * during the encryption:
166 * m nexthdrp mprev md
168 * IP ............... esp iv payload pad padlen nxthdr
169 * <--><-><------><--------------->
170 * esplen plen extendsiz
174 * <-----------------> espoff
177 esp_output(struct mbuf
*m
, u_char
*nexthdrp
, struct mbuf
*md
,
178 struct ipsecrequest
*isr
, int af
)
183 struct esptail
*esptail
;
184 struct secasvar
*sav
= isr
->sav
;
185 const struct esp_algorithm
*algo
;
188 size_t plen
; /* payload length to be encrypted */
194 struct ipsecstat
*stat
;
210 ipseclog((LOG_ERR
, "esp_output: unsupported af %d\n", af
));
211 return 0; /* no change at all */
214 /* some sanity check */
215 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && !sav
->replay
) {
222 ip
= mtod(m
, struct ip
*);
223 ipseclog((LOG_DEBUG
, "esp4_output: internal error: "
224 "sav->replay is null: %x->%x, SPI=%u\n",
225 (u_int32_t
)ntohl(ip
->ip_src
.s_addr
),
226 (u_int32_t
)ntohl(ip
->ip_dst
.s_addr
),
227 (u_int32_t
)ntohl(sav
->spi
)));
228 ipsecstat
.out_inval
++;
234 ipseclog((LOG_DEBUG
, "esp6_output: internal error: "
235 "sav->replay is null: SPI=%u\n",
236 (u_int32_t
)ntohl(sav
->spi
)));
237 ipsec6stat
.out_inval
++;
241 panic("esp_output: should not reach here");
247 algo
= esp_algorithm_lookup(sav
->alg_enc
);
249 ipseclog((LOG_ERR
, "esp_output: unsupported algorithm: "
250 "SPI=%u\n", (u_int32_t
)ntohl(sav
->spi
)));
258 panic("invalid ivlen");
264 * XXX inserts ESP header right after IPv4 header. should
265 * chase the header chain.
266 * XXX sequential number
269 struct ip
*ip
= NULL
;
272 struct ip6_hdr
*ip6
= NULL
;
274 size_t esplen
; /* sizeof(struct esp/newesp) */
275 size_t esphlen
; /* sizeof(struct esp/newesp) + ivlen */
276 size_t hlen
= 0; /* ip header len */
278 if (sav
->flags
& SADB_X_EXT_OLD
) {
280 esplen
= sizeof(struct esp
);
283 if (sav
->flags
& SADB_X_EXT_DERIV
)
284 esplen
= sizeof(struct esp
);
286 esplen
= sizeof(struct newesp
);
288 esphlen
= esplen
+ ivlen
;
290 for (mprev
= m
; mprev
&& mprev
->m_next
!= md
; mprev
= mprev
->m_next
)
292 if (mprev
== NULL
|| mprev
->m_next
!= md
) {
293 ipseclog((LOG_DEBUG
, "esp%d_output: md is not in chain\n",
300 for (n
= md
; n
; n
= n
->m_next
)
306 ip
= mtod(m
, struct ip
*);
308 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
310 hlen
= ip
->ip_hl
<< 2;
316 ip6
= mtod(m
, struct ip6_hdr
*);
322 /* make the packet over-writable */
323 mprev
->m_next
= NULL
;
324 if ((md
= ipsec_copypkt(md
)) == NULL
) {
331 espoff
= m
->m_pkthdr
.len
- plen
;
334 * grow the mbuf to accomodate ESP header.
335 * before: IP ... payload
336 * after: IP ... ESP IV payload
338 if (M_LEADINGSPACE(md
) < esphlen
|| (md
->m_flags
& M_EXT
)) {
339 MGET(n
, MB_DONTWAIT
, MT_DATA
);
348 m
->m_pkthdr
.len
+= esphlen
;
349 esp
= mtod(n
, struct esp
*);
351 md
->m_len
+= esphlen
;
352 md
->m_data
-= esphlen
;
353 m
->m_pkthdr
.len
+= esphlen
;
354 esp
= mtod(md
, struct esp
*);
358 *nexthdrp
= IPPROTO_ESP
;
362 if (esphlen
< (IP_MAXPACKET
- ntohs(ip
->ip_len
)))
363 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + esphlen
);
366 "IPv4 ESP output: size exceeds limit\n"));
367 ipsecstat
.out_inval
++;
376 /* total packet length will be computed in ip6_output() */
382 /* initialize esp header. */
384 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0) {
386 nesp
= (struct newesp
*)esp
;
387 if (sav
->replay
->count
== ~0) {
388 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0) {
389 /* XXX Is it noisy ? */
390 ipseclog((LOG_WARNING
,
391 "replay counter overflowed. %s\n",
392 ipsec_logsastr(sav
)));
398 sav
->replay
->count
++;
400 * XXX sequence number must not be cycled, if the SA is
401 * installed by IKE daemon.
403 nesp
->esp_seq
= htonl(sav
->replay
->count
& 0xffffffff);
408 * find the last mbuf. make some room for ESP trailer.
411 struct ip
*ip
= NULL
;
419 padbound
= algo
->padbound
;
422 /* ESP packet, including nxthdr field, must be length of 4n */
426 extendsiz
= padbound
- (plen
% padbound
);
428 extendsiz
= padbound
+ 1;
434 randpadmax
= ip4_esp_randpad
;
439 randpadmax
= ip6_esp_randpad
;
446 if (randpadmax
< 0 || plen
+ extendsiz
>= randpadmax
)
452 randpadmax
= (randpadmax
/ padbound
) * padbound
;
453 n
= (randpadmax
- plen
+ extendsiz
) / padbound
;
456 n
= (krandom() % n
) * padbound
;
461 * make sure we do not pad too much.
462 * MLEN limitation comes from the trailer attachment
464 * 256 limitation comes from sequential padding.
465 * also, the 1-octet length field in ESP trailer imposes
466 * limitation (but is less strict than sequential padding
467 * as length field do not count the last 2 octets).
469 if (extendsiz
+ n
<= MLEN
&& extendsiz
+ n
< 256)
474 if (extendsiz
> MLEN
|| extendsiz
>= 256)
475 panic("extendsiz too big in esp_output");
483 * if M_EXT, the external mbuf data may be shared among
484 * two consequtive TCP packets, and it may be unsafe to use the
487 if (!(n
->m_flags
& M_EXT
) && extendsiz
< M_TRAILINGSPACE(n
)) {
488 extend
= mtod(n
, u_char
*) + n
->m_len
;
489 n
->m_len
+= extendsiz
;
490 m
->m_pkthdr
.len
+= extendsiz
;
494 MGET(nn
, MB_DONTWAIT
, MT_DATA
);
496 ipseclog((LOG_DEBUG
, "esp%d_output: can't alloc mbuf",
502 extend
= mtod(nn
, u_char
*);
503 nn
->m_len
= extendsiz
;
507 m
->m_pkthdr
.len
+= extendsiz
;
509 switch (sav
->flags
& SADB_X_EXT_PMASK
) {
510 case SADB_X_EXT_PRAND
:
511 key_randomfill(extend
, extendsiz
);
513 case SADB_X_EXT_PZERO
:
514 bzero(extend
, extendsiz
);
516 case SADB_X_EXT_PSEQ
:
517 for (i
= 0; i
< extendsiz
; i
++)
518 extend
[i
] = (i
+ 1) & 0xff;
522 /* initialize esp trailer. */
523 esptail
= (struct esptail
*)
524 (mtod(n
, u_int8_t
*) + n
->m_len
- sizeof(struct esptail
));
525 esptail
->esp_nxt
= nxt
;
526 esptail
->esp_padlen
= extendsiz
- 2;
528 /* modify IP header (for ESP header part only) */
532 ip
= mtod(m
, struct ip
*);
533 if (extendsiz
< (IP_MAXPACKET
- ntohs(ip
->ip_len
)))
534 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + extendsiz
);
537 "IPv4 ESP output: size exceeds limit\n"));
538 ipsecstat
.out_inval
++;
547 /* total packet length will be computed in ip6_output() */
554 * pre-compute and cache intermediate key
556 error
= esp_schedule(algo
, sav
);
564 * encrypt the packet, based on security association
565 * and the algorithm specified.
568 panic("internal error: no encrypt function");
569 if ((*algo
->encrypt
)(m
, espoff
, plen
+ extendsiz
, sav
, algo
, ivlen
)) {
570 /* m is already freed */
571 ipseclog((LOG_ERR
, "packet encryption failure\n"));
578 * calculate ICV if required.
584 if (sav
->alg_auth
== SADB_AALG_NONE
)
588 const struct ah_algorithm
*aalgo
;
589 u_char authbuf
[AH_MAXSUMSIZE
];
597 aalgo
= ah_algorithm_lookup(sav
->alg_auth
);
600 siz
= ((aalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1);
601 if (AH_MAXSUMSIZE
< siz
)
602 panic("assertion failed for AH_MAXSUMSIZE");
604 if (esp_auth(m
, espoff
, m
->m_pkthdr
.len
- espoff
, sav
, authbuf
)) {
605 ipseclog((LOG_ERR
, "ESP checksum generation failure\n"));
616 if (!(n
->m_flags
& M_EXT
) && siz
< M_TRAILINGSPACE(n
)) { /* XXX */
618 m
->m_pkthdr
.len
+= siz
;
619 p
= mtod(n
, u_char
*) + n
->m_len
- siz
;
623 MGET(nn
, MB_DONTWAIT
, MT_DATA
);
625 ipseclog((LOG_DEBUG
, "can't alloc mbuf in esp%d_output",
635 m
->m_pkthdr
.len
+= siz
;
636 p
= mtod(nn
, u_char
*);
638 bcopy(authbuf
, p
, siz
);
640 /* modify IP header (for ESP header part only) */
644 ip
= mtod(m
, struct ip
*);
645 if (siz
< (IP_MAXPACKET
- ntohs(ip
->ip_len
)))
646 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + siz
);
649 "IPv4 ESP output: size exceeds limit\n"));
650 ipsecstat
.out_inval
++;
659 /* total packet length will be computed in ip6_output() */
668 "NULL mbuf after encryption in esp%d_output", afnumber
));
671 stat
->out_esphist
[sav
->alg_enc
]++;
672 key_sa_recordxfer(sav
, m
);
679 panic("something bad in esp_output");
685 esp4_output(struct mbuf
*m
, struct ipsecrequest
*isr
)
688 if (m
->m_len
< sizeof(struct ip
)) {
689 ipseclog((LOG_DEBUG
, "esp4_output: first mbuf too short\n"));
693 ip
= mtod(m
, struct ip
*);
694 /* XXX assumes that m->m_next points to payload */
695 return esp_output(m
, &ip
->ip_p
, m
->m_next
, isr
, AF_INET
);
701 esp6_output(struct mbuf
*m
, u_char
*nexthdrp
, struct mbuf
*md
,
702 struct ipsecrequest
*isr
)
704 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
705 ipseclog((LOG_DEBUG
, "esp6_output: first mbuf too short\n"));
709 return esp_output(m
, nexthdrp
, md
, isr
, AF_INET6
);