1 /* $NetBSD: esp_output.c,v 1.35 2009/03/18 17:06:52 cegger Exp $ */
2 /* $KAME: esp_output.c,v 1.44 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 * RFC1827/2406 Encapsulated Security Payload.
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: esp_output.c,v 1.35 2009/03/18 17:06:52 cegger Exp $");
41 #include "opt_ipsec.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/errno.h>
53 #include <sys/kernel.h>
54 #include <sys/syslog.h>
57 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #include <netinet/in_var.h>
64 #include <netinet/udp.h>
68 #include <netinet/ip6.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet/icmp6.h>
73 #include <netinet6/ipsec.h>
74 #include <netinet6/ipsec_private.h>
75 #include <netinet6/ah.h>
76 #include <netinet6/esp.h>
77 #include <netkey/key.h>
78 #include <netkey/keydb.h>
80 #include <net/net_osdep.h>
82 static int esp_output(struct mbuf
*, u_char
*, struct mbuf
*,
83 struct ipsecrequest
*, int);
86 * compute ESP header size.
89 esp_hdrsiz(struct ipsecrequest
*isr
)
92 const struct esp_algorithm
*algo
;
93 const struct ah_algorithm
*aalgo
;
100 panic("esp_hdrsiz: NULL was passed.");
104 if (isr
->saidx
.proto
!= IPPROTO_ESP
)
105 panic("unsupported mode passed to esp_hdrsiz");
109 if (sav
->state
!= SADB_SASTATE_MATURE
110 && sav
->state
!= SADB_SASTATE_DYING
)
113 /* we need transport mode ESP. */
114 algo
= esp_algorithm_lookup(sav
->alg_enc
);
125 * right now we don't calcurate the padding size. simply
126 * treat the padding size as constant, for simplicity.
128 * XXX variable size padding support
130 if (sav
->flags
& SADB_X_EXT_OLD
) {
132 hdrsiz
= sizeof(struct esp
) + ivlen
+
133 esp_max_padbound() - 1 + 2;
136 aalgo
= ah_algorithm_lookup(sav
->alg_auth
);
137 if (aalgo
&& sav
->replay
&& sav
->key_auth
)
138 authlen
= (aalgo
->sumsiz
)(sav
);
141 hdrsiz
= sizeof(struct newesp
) + ivlen
+
142 esp_max_padbound() - 1 + 2 + authlen
;
147 * If NAT-T is enabled, add the space for UDP encapsulation
149 if (sav
->natt_type
!= 0) {
150 hdrsiz
+= sizeof(struct udphdr
);
151 if (sav
->natt_type
== UDP_ENCAP_ESPINUDP_NON_IKE
)
152 hdrsiz
+= sizeof(u_int64_t
);
161 * sizeof(struct newesp) > sizeof(struct esp).
162 * esp_max_ivlen() = max ivlen for CBC mode
163 * esp_max_padbound - 1 =
164 * maximum padding length without random padding length
165 * 2 = (Pad Length field) + (Next Header field).
166 * AH_MAXSUMSIZE = maximum ICV we support.
167 * sizeof(u_int64_t) = non IKE marker (NAT-T)
168 * sizeof(struct udphdr) = UDP encapsulation (NAT-T)
170 return sizeof(struct newesp
) + esp_max_ivlen() +
171 esp_max_padbound() - 1 + 2 + AH_MAXSUMSIZE
+
173 sizeof(u_int64_t
) + sizeof(struct udphdr
) +
179 * Modify the packet so that the payload is encrypted.
180 * The mbuf (m) must start with IPv4 or IPv6 header.
181 * On failure, free the given mbuf and return NULL.
186 * IP ......... payload
187 * during the encryption:
188 * m nexthdrp mprev md
190 * IP ............... esp iv payload pad padlen nxthdr
191 * <--><-><------><--------------->
192 * esplen plen extendsiz
196 * <-----------------> espoff
199 esp_output(struct mbuf
*m
, u_char
*nexthdrp
, struct mbuf
*md
,
200 struct ipsecrequest
*isr
, int af
)
205 struct esptail
*esptail
;
206 struct secasvar
*sav
= isr
->sav
;
207 const struct esp_algorithm
*algo
;
210 size_t plen
; /* payload length to be encrypted */
218 struct udphdr
*udp
= NULL
;
225 stat
= ipsecstat_percpu
;
231 stat
= ipsec6stat_percpu
;
235 ipseclog((LOG_ERR
, "esp_output: unsupported af %d\n", af
));
236 return 0; /* no change at all */
239 /* some sanity check */
240 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && !sav
->replay
) {
247 ip
= mtod(m
, struct ip
*);
248 ipseclog((LOG_DEBUG
, "esp4_output: internal error: "
249 "sav->replay is null: %x->%x, SPI=%u\n",
250 (u_int32_t
)ntohl(ip
->ip_src
.s_addr
),
251 (u_int32_t
)ntohl(ip
->ip_dst
.s_addr
),
252 (u_int32_t
)ntohl(sav
->spi
)));
253 IPSEC_STATINC(IPSEC_STAT_OUT_INVAL
);
259 ipseclog((LOG_DEBUG
, "esp6_output: internal error: "
260 "sav->replay is null: SPI=%u\n",
261 (u_int32_t
)ntohl(sav
->spi
)));
262 IPSEC6_STATINC(IPSEC_STAT_OUT_INVAL
);
266 panic("esp_output: should not reach here");
272 algo
= esp_algorithm_lookup(sav
->alg_enc
);
274 ipseclog((LOG_ERR
, "esp_output: unsupported algorithm: "
275 "SPI=%u\n", (u_int32_t
)ntohl(sav
->spi
)));
283 panic("invalid ivlen");
289 * XXX inserts ESP header right after IPv4 header. should
290 * chase the header chain.
291 * XXX sequential number
294 struct ip
*ip
= NULL
;
297 struct ip6_hdr
*ip6
= NULL
;
299 size_t esplen
; /* sizeof(struct esp/newesp) */
300 size_t esphlen
; /* sizeof(struct esp/newesp) + ivlen */
301 size_t hlen
= 0; /* ip header len */
303 if (sav
->flags
& SADB_X_EXT_OLD
) {
305 esplen
= sizeof(struct esp
);
308 if (sav
->flags
& SADB_X_EXT_DERIV
)
309 esplen
= sizeof(struct esp
);
311 esplen
= sizeof(struct newesp
);
313 esphlen
= esplen
+ ivlen
;
315 for (mprev
= m
; mprev
&& mprev
->m_next
!= md
; mprev
= mprev
->m_next
)
317 if (mprev
== NULL
|| mprev
->m_next
!= md
) {
318 ipseclog((LOG_DEBUG
, "esp%d_output: md is not in chain\n",
325 for (n
= md
; n
; n
= n
->m_next
)
331 ip
= mtod(m
, struct ip
*);
332 hlen
= ip
->ip_hl
<< 2;
337 ip6
= mtod(m
, struct ip6_hdr
*);
343 /* make the packet over-writable */
344 mprev
->m_next
= NULL
;
345 if ((md
= ipsec_copypkt(md
)) == NULL
) {
351 espoff
= m
->m_pkthdr
.len
- plen
;
354 if (sav
->natt_type
!= 0) {
355 esphlen
+= sizeof(struct udphdr
);
356 espoff
+= sizeof(struct udphdr
);
358 if (sav
->natt_type
== UDP_ENCAP_ESPINUDP_NON_IKE
) {
360 esphlen
+= sizeof(u_int64_t
);
361 espoff
+= sizeof(u_int64_t
);
367 * grow the mbuf to accommodate ESP header.
368 * before: IP ... payload
369 * after (without NAT-T): IP ... ESP IV payload
370 * after (with older NAT-T): IP ... UDP non-IKE-marker ESP IV payload
371 * after (with newer NAT-T): IP ... UDP ESP IV payload
373 if (M_LEADINGSPACE(md
) < esphlen
|| (md
->m_flags
& M_EXT
) != 0) {
374 MGET(n
, M_DONTWAIT
, MT_DATA
);
382 m
->m_pkthdr
.len
+= esphlen
;
384 esp
= mtod(n
, struct esp
*);
386 md
->m_len
+= esphlen
;
387 md
->m_data
-= esphlen
;
388 m
->m_pkthdr
.len
+= esphlen
;
389 esp
= mtod(md
, struct esp
*);
393 if (sav
->natt_type
!= 0) {
394 udp
= (struct udphdr
*)esp
;
395 esp
= (struct esp
*)(udp
+ 1);
397 if (sav
->natt_type
== UDP_ENCAP_ESPINUDP_NON_IKE
) {
398 u_int64_t
*data
= (u_int64_t
*)esp
;
400 *data
= 0; /* NON-IKE marker */
401 esp
= (struct esp
*)(data
+ 1);
407 *nexthdrp
= IPPROTO_ESP
;
411 if (esphlen
< (IP_MAXPACKET
- ntohs(ip
->ip_len
)))
412 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + esphlen
);
415 "IPv4 ESP output: size exceeds limit\n"));
416 IPSEC_STATINC(IPSEC_STAT_OUT_INVAL
);
424 /* total packet length will be computed in ip6_output() */
430 /* initialize esp header. */
432 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0) {
434 nesp
= (struct newesp
*)esp
;
435 if (sav
->replay
->count
== ~0) {
436 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0) {
437 /* XXX Is it noisy ? */
438 ipseclog((LOG_WARNING
,
439 "replay counter overflowed. %s\n",
440 ipsec_logsastr(sav
)));
441 _NET_STATINC(stat
, IPSEC_STAT_OUT_INVAL
);
446 sav
->replay
->count
++;
448 * XXX sequence number must not be cycled, if the SA is
449 * installed by IKE daemon.
451 nesp
->esp_seq
= htonl(sav
->replay
->count
& 0xffffffff);
456 * find the last mbuf. make some room for ESP trailer.
459 struct ip
*ip
= NULL
;
466 padbound
= algo
->padbound
;
469 /* ESP packet, including nxthdr field, must be length of 4n */
473 extendsiz
= padbound
- (plen
% padbound
);
475 extendsiz
= padbound
+ 1;
482 * if M_EXT, the external mbuf data may be shared among
483 * two consequtive TCP packets, and it may be unsafe to use the
486 if (!(n
->m_flags
& M_EXT
) && extendsiz
< M_TRAILINGSPACE(n
)) {
487 extend
= mtod(n
, u_char
*) + n
->m_len
;
488 n
->m_len
+= extendsiz
;
489 m
->m_pkthdr
.len
+= extendsiz
;
493 MGET(nn
, M_DONTWAIT
, MT_DATA
);
495 ipseclog((LOG_DEBUG
, "esp%d_output: can't alloc mbuf",
500 extend
= mtod(nn
, u_char
*);
501 nn
->m_len
= extendsiz
;
505 m
->m_pkthdr
.len
+= extendsiz
;
507 switch (sav
->flags
& SADB_X_EXT_PMASK
) {
508 case SADB_X_EXT_PRAND
:
509 key_randomfill(extend
, extendsiz
);
511 case SADB_X_EXT_PZERO
:
512 memset(extend
, 0, extendsiz
);
514 case SADB_X_EXT_PSEQ
:
515 for (i
= 0; i
< extendsiz
; i
++)
516 extend
[i
] = (i
+ 1) & 0xff;
521 if (sav
->natt_type
!= 0) {
522 *nexthdrp
= IPPROTO_UDP
;
525 * Create the UDP encapsulation header for NAT-T
526 * uh_len is set later, when the size is known.
528 if (sav
->natt_type
== UDP_ENCAP_ESPINUDP_NON_IKE
)
529 udp
->uh_sport
= htons(UDP_ENCAP_ESPINUDP_PORT
);
532 KEY_PORTFROMSADDR(&sav
->sah
->saidx
.src
);
534 udp
->uh_dport
= KEY_PORTFROMSADDR(&sav
->sah
->saidx
.dst
);
537 *nexthdrp
= IPPROTO_ESP
;
541 /* initialize esp trailer. */
542 esptail
= (struct esptail
*)
543 (mtod(n
, u_int8_t
*) + n
->m_len
- sizeof(struct esptail
));
544 esptail
->esp_nxt
= nxt
;
545 esptail
->esp_padlen
= extendsiz
- 2;
547 /* modify IP header (for ESP header part only) */
551 ip
= mtod(m
, struct ip
*);
552 if (extendsiz
< (IP_MAXPACKET
- ntohs(ip
->ip_len
)))
553 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + extendsiz
);
556 "IPv4 ESP output: size exceeds limit\n"));
557 IPSEC_STATINC(IPSEC_STAT_OUT_INVAL
);
565 /* total packet length will be computed in ip6_output() */
572 * pre-compute and cache intermediate key
574 error
= esp_schedule(algo
, sav
);
576 _NET_STATINC(stat
, IPSEC_STAT_OUT_INVAL
);
581 * encrypt the packet, based on security association
582 * and the algorithm specified.
585 panic("internal error: no encrypt function");
586 if ((*algo
->encrypt
)(m
, espoff
, plen
+ extendsiz
, sav
, algo
, ivlen
)) {
587 /* m is already freed */
589 ipseclog((LOG_ERR
, "packet encryption failure\n"));
590 _NET_STATINC(stat
, IPSEC_STAT_OUT_INVAL
);
596 * calculate ICV if required.
602 if (sav
->key_auth
== SADB_AALG_NONE
)
606 const struct ah_algorithm
*aalgo
;
607 u_char authbuf
[AH_MAXSUMSIZE
];
614 aalgo
= ah_algorithm_lookup(sav
->alg_auth
);
617 siz
= ((aalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1);
618 if (AH_MAXSUMSIZE
< siz
)
619 panic("assertion failed for AH_MAXSUMSIZE");
621 if (esp_auth(m
, espoff
, m
->m_pkthdr
.len
- espoff
, sav
, authbuf
)) {
622 ipseclog((LOG_ERR
, "ESP checksum generation failure\n"));
624 _NET_STATINC(stat
, IPSEC_STAT_OUT_INVAL
);
632 if (!(n
->m_flags
& M_EXT
) && siz
< M_TRAILINGSPACE(n
)) { /* XXX */
634 m
->m_pkthdr
.len
+= siz
;
635 p
= mtod(n
, u_char
*) + n
->m_len
- siz
;
639 MGET(nn
, M_DONTWAIT
, MT_DATA
);
641 ipseclog((LOG_DEBUG
, "can't alloc mbuf in esp%d_output",
650 m
->m_pkthdr
.len
+= siz
;
651 p
= mtod(nn
, u_char
*);
653 memcpy(p
, authbuf
, siz
);
655 /* modify IP header (for ESP header part only) */
659 ip
= mtod(m
, struct ip
*);
660 if (siz
< (IP_MAXPACKET
- ntohs(ip
->ip_len
)))
661 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + siz
);
664 "IPv4 ESP output: size exceeds limit\n"));
665 IPSEC_STATINC(IPSEC_STAT_OUT_INVAL
);
673 /* total packet length will be computed in ip6_output() */
681 if (sav
->natt_type
!= 0) {
683 ip
= mtod(m
, struct ip
*);
684 udp
->uh_ulen
= htons(ntohs(ip
->ip_len
) - (ip
->ip_hl
<< 2));
687 #endif /* IPSEC_NAT_T */
691 "NULL mbuf after encryption in esp%d_output", afnumber
));
693 _NET_STATINC(stat
, IPSEC_STAT_OUT_SUCCESS
);
694 _NET_STATINC(stat
, IPSEC_STAT_OUT_ESPHIST
+ sav
->alg_enc
);
695 key_sa_recordxfer(sav
, m
);
703 panic("something bad in esp_output");
709 esp4_output(struct mbuf
*m
, struct ipsecrequest
*isr
)
712 if (m
->m_len
< sizeof(struct ip
)) {
713 ipseclog((LOG_DEBUG
, "esp4_output: first mbuf too short\n"));
717 ip
= mtod(m
, struct ip
*);
718 /* XXX assumes that m->m_next points to payload */
719 return esp_output(m
, &ip
->ip_p
, m
->m_next
, isr
, AF_INET
);
725 esp6_output(struct mbuf
*m
, u_char
*nexthdrp
,
726 struct mbuf
*md
, struct ipsecrequest
*isr
)
728 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
729 ipseclog((LOG_DEBUG
, "esp6_output: first mbuf too short\n"));
733 return esp_output(m
, nexthdrp
, md
, isr
, AF_INET6
);