1 /* $FreeBSD: src/sys/netinet6/esp_output.c,v 1.1.2.3 2002/04/28 05:40:26 suz 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
36 * RFC1827/2406 Encapsulated Security Payload.
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.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/kernel.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>
59 #include <netinet/udp.h> /* for nat traversal */
62 #include <netinet/ip6.h>
63 #include <netinet6/ip6_var.h>
64 #include <netinet/icmp6.h>
67 #include <netinet6/ipsec.h>
69 #include <netinet6/ipsec6.h>
71 #include <netinet6/ah.h>
73 #include <netinet6/ah6.h>
75 #include <netinet6/esp.h>
77 #include <netinet6/esp6.h>
79 #include <netkey/key.h>
80 #include <netkey/keydb.h>
82 #include <net/net_osdep.h>
84 #include <sys/kdebug.h>
85 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
86 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
87 #define DBG_FNC_ESPOUT NETDBG_CODE(DBG_NETIPSEC, (4 << 8))
88 #define DBG_FNC_ENCRYPT NETDBG_CODE(DBG_NETIPSEC, (5 << 8))
90 static int esp_output(struct mbuf
*, u_char
*, struct mbuf
*,
91 int, struct secasvar
*sav
);
93 extern int esp_udp_encap_port
;
94 extern u_int32_t natt_now
;
96 extern lck_mtx_t
*sadb_mutex
;
99 * compute ESP header size.
103 struct ipsecrequest
*isr
;
108 panic("esp_hdrsiz: NULL was passed.\n");
112 lck_mtx_lock(sadb_mutex
);
114 struct secasvar
*sav
;
115 const struct esp_algorithm
*algo
;
116 const struct ah_algorithm
*aalgo
;
122 /*%%%% this needs to change - no sav in ipsecrequest any more */
125 if (isr
->saidx
.proto
!= IPPROTO_ESP
)
126 panic("unsupported mode passed to esp_hdrsiz");
130 if (sav
->state
!= SADB_SASTATE_MATURE
131 && sav
->state
!= SADB_SASTATE_DYING
)
134 /* we need transport mode ESP. */
135 algo
= esp_algorithm_lookup(sav
->alg_enc
);
143 maxpad
= algo
->padbound
;
146 maxpad
+= 1; /* maximum 'extendsiz' is padbound + 1, see esp_output */
148 if (sav
->flags
& SADB_X_EXT_OLD
) {
150 hdrsiz
= sizeof(struct esp
) + ivlen
+ maxpad
;
153 aalgo
= ah_algorithm_lookup(sav
->alg_auth
);
154 if (aalgo
&& sav
->replay
&& sav
->key_auth
)
155 authlen
= (aalgo
->sumsiz
)(sav
);
158 hdrsiz
= sizeof(struct newesp
) + ivlen
+ maxpad
+ authlen
;
162 * If the security association indicates that NATT is required,
163 * add the size of the NATT encapsulation header:
165 if ((sav
->flags
& SADB_X_EXT_NATT
) != 0) hdrsiz
+= sizeof(struct udphdr
) + 4;
167 lck_mtx_unlock(sadb_mutex
);
171 lck_mtx_unlock(sadb_mutex
);
175 * sizeof(struct newesp) > sizeof(struct esp). (8)
176 * esp_max_ivlen() = max ivlen for CBC mode
177 * 17 = (maximum padding length without random padding length)
178 * + (Pad Length field) + (Next Header field).
179 * 16 = maximum ICV we support.
180 * sizeof(struct udphdr) in case NAT traversal is used
182 return sizeof(struct newesp
) + esp_max_ivlen() + 17 + 16 + sizeof(struct udphdr
);
186 * Modify the packet so that the payload is encrypted.
187 * The mbuf (m) must start with IPv4 or IPv6 header.
188 * On failure, free the given mbuf and return NULL.
193 * IP ......... payload
194 * during the encryption:
195 * m nexthdrp mprev md
197 * IP ............... esp iv payload pad padlen nxthdr
198 * <--><-><------><--------------->
199 * esplen plen extendsiz
203 * <-----------------> espoff
206 esp_output(m
, nexthdrp
, md
, af
, sav
)
211 struct secasvar
*sav
;
216 struct esptail
*esptail
;
217 const struct esp_algorithm
*algo
;
220 size_t plen
; /*payload length to be encrypted*/
226 struct ipsecstat
*stat
;
227 struct udphdr
*udp
= NULL
;
228 int udp_encapsulate
= (sav
->flags
& SADB_X_EXT_NATT
&& af
== AF_INET
&&
229 (esp_udp_encap_port
& 0xFFFF) != 0);
231 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_START
, sav
->ivlen
,0,0,0,0);
246 ipseclog((LOG_ERR
, "esp_output: unsupported af %d\n", af
));
247 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_END
, 1,0,0,0,0);
248 return 0; /* no change at all */
251 /* some sanity check */
252 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0 && !sav
->replay
) {
259 ip
= mtod(m
, struct ip
*);
260 ipseclog((LOG_DEBUG
, "esp4_output: internal error: "
261 "sav->replay is null: %x->%x, SPI=%u\n",
262 (u_int32_t
)ntohl(ip
->ip_src
.s_addr
),
263 (u_int32_t
)ntohl(ip
->ip_dst
.s_addr
),
264 (u_int32_t
)ntohl(sav
->spi
)));
265 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
271 ipseclog((LOG_DEBUG
, "esp6_output: internal error: "
272 "sav->replay is null: SPI=%u\n",
273 (u_int32_t
)ntohl(sav
->spi
)));
274 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
278 panic("esp_output: should not reach here");
281 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_END
, 2,0,0,0,0);
285 algo
= esp_algorithm_lookup(sav
->alg_enc
);
287 ipseclog((LOG_ERR
, "esp_output: unsupported algorithm: "
288 "SPI=%u\n", (u_int32_t
)ntohl(sav
->spi
)));
290 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_END
, 3,0,0,0,0);
297 panic("invalid ivlen");
303 * XXX inserts ESP header right after IPv4 header. should
304 * chase the header chain.
305 * XXX sequential number
308 struct ip
*ip
= NULL
;
311 struct ip6_hdr
*ip6
= NULL
;
313 size_t esplen
; /* sizeof(struct esp/newesp) */
314 size_t esphlen
; /* sizeof(struct esp/newesp) + ivlen */
315 size_t hlen
= 0; /* ip header len */
317 if (sav
->flags
& SADB_X_EXT_OLD
) {
319 esplen
= sizeof(struct esp
);
322 if (sav
->flags
& SADB_X_EXT_DERIV
)
323 esplen
= sizeof(struct esp
);
325 esplen
= sizeof(struct newesp
);
327 esphlen
= esplen
+ ivlen
;
329 for (mprev
= m
; mprev
&& mprev
->m_next
!= md
; mprev
= mprev
->m_next
)
331 if (mprev
== NULL
|| mprev
->m_next
!= md
) {
332 ipseclog((LOG_DEBUG
, "esp%d_output: md is not in chain\n",
335 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_END
, 4,0,0,0,0);
340 for (n
= md
; n
; n
= n
->m_next
)
346 ip
= mtod(m
, struct ip
*);
348 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
350 hlen
= ip
->ip_hl
<< 2;
356 ip6
= mtod(m
, struct ip6_hdr
*);
362 /* make the packet over-writable */
363 mprev
->m_next
= NULL
;
364 if ((md
= ipsec_copypkt(md
)) == NULL
) {
372 * Translate UDP source port back to its original value.
373 * SADB_X_EXT_NATT_MULTIPLEUSERS is only set for transort mode.
375 if ((sav
->flags
& SADB_X_EXT_NATT_MULTIPLEUSERS
) != 0) {
376 /* if not UDP - drop it */
377 if (ip
->ip_p
!= IPPROTO_UDP
) {
378 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
384 udp
= mtod(md
, struct udphdr
*);
386 /* if src port not set in sav - find it */
387 if (sav
->natt_encapsulated_src_port
== 0)
388 if (key_natt_get_translated_port(sav
) == 0) {
393 if (sav
->remote_ike_port
== htons(udp
->uh_dport
)) {
394 /* translate UDP port */
395 udp
->uh_dport
= sav
->natt_encapsulated_src_port
;
396 udp
->uh_sum
= 0; /* don't need checksum with ESP auth */
398 /* drop the packet - can't translate the port */
399 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
407 espoff
= m
->m_pkthdr
.len
- plen
;
409 if (udp_encapsulate
) {
410 esphlen
+= sizeof(struct udphdr
);
411 espoff
+= sizeof(struct udphdr
);
415 * grow the mbuf to accomodate ESP header.
416 * before: IP ... payload
417 * after: IP ... [UDP] ESP IV payload
419 if (M_LEADINGSPACE(md
) < esphlen
|| (md
->m_flags
& M_EXT
) != 0) {
420 MGET(n
, M_DONTWAIT
, MT_DATA
);
429 m
->m_pkthdr
.len
+= esphlen
;
430 if (udp_encapsulate
) {
431 udp
= mtod(n
, struct udphdr
*);
432 esp
= (struct esp
*)((caddr_t
)udp
+ sizeof(struct udphdr
));
434 esp
= mtod(n
, struct esp
*);
437 md
->m_len
+= esphlen
;
438 md
->m_data
-= esphlen
;
439 m
->m_pkthdr
.len
+= esphlen
;
440 esp
= mtod(md
, struct esp
*);
441 if (udp_encapsulate
) {
442 udp
= mtod(md
, struct udphdr
*);
443 esp
= (struct esp
*)((caddr_t
)udp
+ sizeof(struct udphdr
));
445 esp
= mtod(md
, struct esp
*);
452 if (esphlen
< (IP_MAXPACKET
- ntohs(ip
->ip_len
)))
453 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + esphlen
);
456 "IPv4 ESP output: size exceeds limit\n"));
457 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
466 /* total packet length will be computed in ip6_output() */
472 /* initialize esp header. */
474 if ((sav
->flags
& SADB_X_EXT_OLD
) == 0) {
476 nesp
= (struct newesp
*)esp
;
477 if (sav
->replay
->count
== ~0) {
478 if ((sav
->flags
& SADB_X_EXT_CYCSEQ
) == 0) {
479 /* XXX Is it noisy ? */
480 ipseclog((LOG_WARNING
,
481 "replay counter overflowed. %s\n",
482 ipsec_logsastr(sav
)));
483 IPSEC_STAT_INCREMENT(stat
->out_inval
);
485 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_END
, 5,0,0,0,0);
489 lck_mtx_lock(sadb_mutex
);
490 sav
->replay
->count
++;
491 lck_mtx_unlock(sadb_mutex
);
493 * XXX sequence number must not be cycled, if the SA is
494 * installed by IKE daemon.
496 nesp
->esp_seq
= htonl(sav
->replay
->count
);
501 * find the last mbuf. make some room for ESP trailer.
504 struct ip
*ip
= NULL
;
512 padbound
= algo
->padbound
;
515 /* ESP packet, including nxthdr field, must be length of 4n */
519 extendsiz
= padbound
- (plen
% padbound
);
521 extendsiz
= padbound
+ 1;
527 randpadmax
= ip4_esp_randpad
;
532 randpadmax
= ip6_esp_randpad
;
539 if (randpadmax
< 0 || plen
+ extendsiz
>= randpadmax
)
545 randpadmax
= (randpadmax
/ padbound
) * padbound
;
546 pad
= (randpadmax
- plen
+ extendsiz
) / padbound
;
549 pad
= (random() % pad
) * padbound
;
554 * make sure we do not pad too much.
555 * MLEN limitation comes from the trailer attachment
557 * 256 limitation comes from sequential padding.
558 * also, the 1-octet length field in ESP trailer imposes
559 * limitation (but is less strict than sequential padding
560 * as length field do not count the last 2 octets).
562 if (extendsiz
+ pad
<= MLEN
&& extendsiz
+ pad
< 256)
567 if (extendsiz
> MLEN
|| extendsiz
>= 256)
568 panic("extendsiz too big in esp_output");
576 * if M_EXT, the external mbuf data may be shared among
577 * two consequtive TCP packets, and it may be unsafe to use the
580 if (!(n
->m_flags
& M_EXT
) && extendsiz
< M_TRAILINGSPACE(n
)) {
581 extend
= mtod(n
, u_char
*) + n
->m_len
;
582 n
->m_len
+= extendsiz
;
583 m
->m_pkthdr
.len
+= extendsiz
;
587 MGET(nn
, M_DONTWAIT
, MT_DATA
);
589 ipseclog((LOG_DEBUG
, "esp%d_output: can't alloc mbuf",
595 extend
= mtod(nn
, u_char
*);
596 nn
->m_len
= extendsiz
;
600 m
->m_pkthdr
.len
+= extendsiz
;
602 switch (sav
->flags
& SADB_X_EXT_PMASK
) {
603 case SADB_X_EXT_PRAND
:
604 key_randomfill(extend
, extendsiz
);
606 case SADB_X_EXT_PZERO
:
607 bzero(extend
, extendsiz
);
609 case SADB_X_EXT_PSEQ
:
610 for (i
= 0; i
< extendsiz
; i
++)
611 extend
[i
] = (i
+ 1) & 0xff;
616 if (udp_encapsulate
) {
617 *nexthdrp
= IPPROTO_UDP
;
619 /* Fill out the UDP header */
620 udp
->uh_sport
= ntohs((u_short
)esp_udp_encap_port
);
621 udp
->uh_dport
= ntohs(sav
->remote_ike_port
);
622 // udp->uh_len set later, after all length tweaks are complete
625 /* Update last sent so we know if we need to send keepalive */
626 sav
->natt_last_activity
= natt_now
;
628 *nexthdrp
= IPPROTO_ESP
;
631 /* initialize esp trailer. */
632 esptail
= (struct esptail
*)
633 (mtod(n
, u_int8_t
*) + n
->m_len
- sizeof(struct esptail
));
634 esptail
->esp_nxt
= nxt
;
635 esptail
->esp_padlen
= extendsiz
- 2;
637 /* modify IP header (for ESP header part only) */
641 ip
= mtod(m
, struct ip
*);
642 if (extendsiz
< (IP_MAXPACKET
- ntohs(ip
->ip_len
)))
643 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + extendsiz
);
646 "IPv4 ESP output: size exceeds limit\n"));
647 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
656 /* total packet length will be computed in ip6_output() */
663 * pre-compute and cache intermediate key
665 error
= esp_schedule(algo
, sav
);
668 IPSEC_STAT_INCREMENT(stat
->out_inval
);
673 * encrypt the packet, based on security association
674 * and the algorithm specified.
677 panic("internal error: no encrypt function");
678 KERNEL_DEBUG(DBG_FNC_ENCRYPT
| DBG_FUNC_START
, 0,0,0,0,0);
679 if ((*algo
->encrypt
)(m
, espoff
, plen
+ extendsiz
, sav
, algo
, ivlen
)) {
680 /* m is already freed */
681 ipseclog((LOG_ERR
, "packet encryption failure\n"));
682 IPSEC_STAT_INCREMENT(stat
->out_inval
);
684 KERNEL_DEBUG(DBG_FNC_ENCRYPT
| DBG_FUNC_END
, 1,error
,0,0,0);
687 KERNEL_DEBUG(DBG_FNC_ENCRYPT
| DBG_FUNC_END
, 2,0,0,0,0);
690 * calculate ICV if required.
696 if (sav
->key_auth
== SADB_AALG_NONE
)
700 const struct ah_algorithm
*aalgo
;
701 u_char authbuf
[AH_MAXSUMSIZE
];
708 aalgo
= ah_algorithm_lookup(sav
->alg_auth
);
711 siz
= ((aalgo
->sumsiz
)(sav
) + 3) & ~(4 - 1);
712 if (AH_MAXSUMSIZE
< siz
)
713 panic("assertion failed for AH_MAXSUMSIZE");
715 if (esp_auth(m
, espoff
, m
->m_pkthdr
.len
- espoff
, sav
, authbuf
)) {
716 ipseclog((LOG_ERR
, "ESP checksum generation failure\n"));
719 IPSEC_STAT_INCREMENT(stat
->out_inval
);
727 if (!(n
->m_flags
& M_EXT
) && siz
< M_TRAILINGSPACE(n
)) { /* XXX */
729 m
->m_pkthdr
.len
+= siz
;
730 p
= mtod(n
, u_char
*) + n
->m_len
- siz
;
734 MGET(nn
, M_DONTWAIT
, MT_DATA
);
736 ipseclog((LOG_DEBUG
, "can't alloc mbuf in esp%d_output",
746 m
->m_pkthdr
.len
+= siz
;
747 p
= mtod(nn
, u_char
*);
749 bcopy(authbuf
, p
, siz
);
751 /* modify IP header (for ESP header part only) */
755 ip
= mtod(m
, struct ip
*);
756 if (siz
< (IP_MAXPACKET
- ntohs(ip
->ip_len
)))
757 ip
->ip_len
= htons(ntohs(ip
->ip_len
) + siz
);
760 "IPv4 ESP output: size exceeds limit\n"));
761 IPSEC_STAT_INCREMENT(ipsecstat
.out_inval
);
770 /* total packet length will be computed in ip6_output() */
776 if (udp_encapsulate
) {
778 ip
= mtod(m
, struct ip
*);
779 udp
->uh_ulen
= htons(ntohs(ip
->ip_len
) - (IP_VHL_HL(ip
->ip_vhl
) << 2));
784 lck_mtx_lock(sadb_mutex
);
787 "NULL mbuf after encryption in esp%d_output", afnumber
));
790 stat
->out_esphist
[sav
->alg_enc
]++;
791 lck_mtx_unlock(sadb_mutex
);
792 key_sa_recordxfer(sav
, m
);
793 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_END
, 6,0,0,0,0);
798 KERNEL_DEBUG(DBG_FNC_ESPOUT
| DBG_FUNC_END
, 7,error
,0,0,0);
801 panic("something bad in esp_output");
809 struct secasvar
*sav
;
812 if (m
->m_len
< sizeof(struct ip
)) {
813 ipseclog((LOG_DEBUG
, "esp4_output: first mbuf too short\n"));
817 ip
= mtod(m
, struct ip
*);
818 /* XXX assumes that m->m_next points to payload */
819 return esp_output(m
, &ip
->ip_p
, m
->m_next
, AF_INET
, sav
);
825 esp6_output(m
, nexthdrp
, md
, sav
)
829 struct secasvar
*sav
;
831 if (m
->m_len
< sizeof(struct ip6_hdr
)) {
832 ipseclog((LOG_DEBUG
, "esp6_output: first mbuf too short\n"));
836 return esp_output(m
, nexthdrp
, md
, AF_INET6
, sav
);