1 /* $FreeBSD: src/sys/netinet6/esp_output.c,v 1.1.2.4 2003/05/06 06:46:58 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
34 #include "opt_inet6.h"
37 * RFC1827/2406 Encapsulated Security Payload.
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>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/in_var.h>
60 #include <netinet/ip6.h>
61 #include <netinet6/ip6_var.h>
62 #include <netinet/icmp6.h>
65 #include <netinet6/ipsec.h>
67 #include <netinet6/ipsec6.h>
69 #include <netinet6/ah.h>
71 #include <netinet6/ah6.h>
73 #include <netinet6/esp.h>
75 #include <netinet6/esp6.h>
77 #include <netkey/key.h>
78 #include <netkey/keydb.h>
80 #include <net/net_osdep.h>
82 static int esp_output __P((struct mbuf *, u_char *, struct mbuf *,
83 struct ipsecrequest *, int));
86 * compute ESP header size.
90 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(m, nexthdrp, md, isr, af)
181 struct ipsecrequest *isr;
187 struct esptail *esptail;
188 struct secasvar *sav = isr->sav;
189 const struct esp_algorithm *algo;
192 size_t plen; /* payload length to be encrypted */
198 struct ipsecstat *stat;
214 ipseclog((LOG_ERR, "esp_output: unsupported af %d\n", af));
215 return 0; /* no change at all */
218 /* some sanity check */
219 if ((sav->flags & SADB_X_EXT_OLD) == 0 && !sav->replay) {
226 ip = mtod(m, struct ip *);
227 ipseclog((LOG_DEBUG, "esp4_output: internal error: "
228 "sav->replay is null: %x->%x, SPI=%u\n",
229 (u_int32_t)ntohl(ip->ip_src.s_addr),
230 (u_int32_t)ntohl(ip->ip_dst.s_addr),
231 (u_int32_t)ntohl(sav->spi)));
232 ipsecstat.out_inval++;
238 ipseclog((LOG_DEBUG, "esp6_output: internal error: "
239 "sav->replay is null: SPI=%u\n",
240 (u_int32_t)ntohl(sav->spi)));
241 ipsec6stat.out_inval++;
245 panic("esp_output: should not reach here");
251 algo = esp_algorithm_lookup(sav->alg_enc);
253 ipseclog((LOG_ERR, "esp_output: unsupported algorithm: "
254 "SPI=%u\n", (u_int32_t)ntohl(sav->spi)));
262 panic("invalid ivlen");
268 * XXX inserts ESP header right after IPv4 header. should
269 * chase the header chain.
270 * XXX sequential number
273 struct ip *ip = NULL;
276 struct ip6_hdr *ip6 = NULL;
278 size_t esplen; /* sizeof(struct esp/newesp) */
279 size_t esphlen; /* sizeof(struct esp/newesp) + ivlen */
280 size_t hlen = 0; /* ip header len */
282 if (sav->flags & SADB_X_EXT_OLD) {
284 esplen = sizeof(struct esp);
287 if (sav->flags & SADB_X_EXT_DERIV)
288 esplen = sizeof(struct esp);
290 esplen = sizeof(struct newesp);
292 esphlen = esplen + ivlen;
294 for (mprev = m; mprev && mprev->m_next != md; mprev = mprev->m_next)
296 if (mprev == NULL || mprev->m_next != md) {
297 ipseclog((LOG_DEBUG, "esp%d_output: md is not in chain\n",
304 for (n = md; n; n = n->m_next)
310 ip = mtod(m, struct ip *);
312 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
314 hlen = ip->ip_hl << 2;
320 ip6 = mtod(m, struct ip6_hdr *);
326 /* make the packet over-writable */
327 mprev->m_next = NULL;
328 if ((md = ipsec_copypkt(md)) == NULL) {
335 espoff = m->m_pkthdr.len - plen;
338 * grow the mbuf to accomodate ESP header.
339 * before: IP ... payload
340 * after: IP ... ESP IV payload
342 if (M_LEADINGSPACE(md) < esphlen || (md->m_flags & M_EXT) != 0) {
343 MGET(n, M_DONTWAIT, MT_DATA);
352 m->m_pkthdr.len += esphlen;
353 esp = mtod(n, struct esp *);
355 md->m_len += esphlen;
356 md->m_data -= esphlen;
357 m->m_pkthdr.len += esphlen;
358 esp = mtod(md, struct esp *);
362 *nexthdrp = IPPROTO_ESP;
366 if (esphlen < (IP_MAXPACKET - ntohs(ip->ip_len)))
367 ip->ip_len = htons(ntohs(ip->ip_len) + esphlen);
370 "IPv4 ESP output: size exceeds limit\n"));
371 ipsecstat.out_inval++;
380 /* total packet length will be computed in ip6_output() */
386 /* initialize esp header. */
388 if ((sav->flags & SADB_X_EXT_OLD) == 0) {
390 nesp = (struct newesp *)esp;
391 if (sav->replay->count == ~0) {
392 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
393 /* XXX Is it noisy ? */
394 ipseclog((LOG_WARNING,
395 "replay counter overflowed. %s\n",
396 ipsec_logsastr(sav)));
402 sav->replay->count++;
404 * XXX sequence number must not be cycled, if the SA is
405 * installed by IKE daemon.
407 nesp->esp_seq = htonl(sav->replay->count);
412 * find the last mbuf. make some room for ESP trailer.
415 struct ip *ip = NULL;
423 padbound = algo->padbound;
426 /* ESP packet, including nxthdr field, must be length of 4n */
430 extendsiz = padbound - (plen % padbound);
432 extendsiz = padbound + 1;
438 randpadmax = ip4_esp_randpad;
443 randpadmax = ip6_esp_randpad;
450 if (randpadmax < 0 || plen + extendsiz >= randpadmax)
456 randpadmax = (randpadmax / padbound) * padbound;
457 n = (randpadmax - plen + extendsiz) / padbound;
460 n = (random() % n) * padbound;
465 * make sure we do not pad too much.
466 * MLEN limitation comes from the trailer attachment
468 * 256 limitation comes from sequential padding.
469 * also, the 1-octet length field in ESP trailer imposes
470 * limitation (but is less strict than sequential padding
471 * as length field do not count the last 2 octets).
473 if (extendsiz + n <= MLEN && extendsiz + n < 256)
478 if (extendsiz > MLEN || extendsiz >= 256)
479 panic("extendsiz too big in esp_output");
487 * if M_EXT, the external mbuf data may be shared among
488 * two consequtive TCP packets, and it may be unsafe to use the
491 if (!(n->m_flags & M_EXT) && extendsiz < M_TRAILINGSPACE(n)) {
492 extend = mtod(n, u_char *) + n->m_len;
493 n->m_len += extendsiz;
494 m->m_pkthdr.len += extendsiz;
498 MGET(nn, M_DONTWAIT, MT_DATA);
500 ipseclog((LOG_DEBUG, "esp%d_output: can't alloc mbuf",
506 extend = mtod(nn, u_char *);
507 nn->m_len = extendsiz;
511 m->m_pkthdr.len += extendsiz;
513 switch (sav->flags & SADB_X_EXT_PMASK) {
514 case SADB_X_EXT_PRAND:
515 key_randomfill(extend, extendsiz);
517 case SADB_X_EXT_PZERO:
518 bzero(extend, extendsiz);
520 case SADB_X_EXT_PSEQ:
521 for (i = 0; i < extendsiz; i++)
522 extend[i] = (i + 1) & 0xff;
526 /* initialize esp trailer. */
527 esptail = (struct esptail *)
528 (mtod(n, u_int8_t *) + n->m_len - sizeof(struct esptail));
529 esptail->esp_nxt = nxt;
530 esptail->esp_padlen = extendsiz - 2;
532 /* modify IP header (for ESP header part only) */
536 ip = mtod(m, struct ip *);
537 if (extendsiz < (IP_MAXPACKET - ntohs(ip->ip_len)))
538 ip->ip_len = htons(ntohs(ip->ip_len) + extendsiz);
541 "IPv4 ESP output: size exceeds limit\n"));
542 ipsecstat.out_inval++;
551 /* total packet length will be computed in ip6_output() */
558 * pre-compute and cache intermediate key
560 error = esp_schedule(algo, sav);
568 * encrypt the packet, based on security association
569 * and the algorithm specified.
572 panic("internal error: no encrypt function");
573 if ((*algo->encrypt)(m, espoff, plen + extendsiz, sav, algo, ivlen)) {
574 /* m is already freed */
575 ipseclog((LOG_ERR, "packet encryption failure\n"));
582 * calculate ICV if required.
588 if (sav->key_auth == SADB_AALG_NONE)
592 const struct ah_algorithm *aalgo;
593 u_char authbuf[AH_MAXSUMSIZE];
601 aalgo = ah_algorithm_lookup(sav->alg_auth);
604 siz = ((aalgo->sumsiz)(sav) + 3) & ~(4 - 1);
605 if (AH_MAXSUMSIZE < siz)
606 panic("assertion failed for AH_MAXSUMSIZE");
608 if (esp_auth(m, espoff, m->m_pkthdr.len - espoff, sav, authbuf)) {
609 ipseclog((LOG_ERR, "ESP checksum generation failure\n"));
620 if (!(n->m_flags & M_EXT) && siz < M_TRAILINGSPACE(n)) { /* XXX */
622 m->m_pkthdr.len += siz;
623 p = mtod(n, u_char *) + n->m_len - siz;
627 MGET(nn, M_DONTWAIT, MT_DATA);
629 ipseclog((LOG_DEBUG, "can't alloc mbuf in esp%d_output",
639 m->m_pkthdr.len += siz;
640 p = mtod(nn, u_char *);
642 bcopy(authbuf, p, siz);
644 /* modify IP header (for ESP header part only) */
648 ip = mtod(m, struct ip *);
649 if (siz < (IP_MAXPACKET - ntohs(ip->ip_len)))
650 ip->ip_len = htons(ntohs(ip->ip_len) + siz);
653 "IPv4 ESP output: size exceeds limit\n"));
654 ipsecstat.out_inval++;
663 /* total packet length will be computed in ip6_output() */
672 "NULL mbuf after encryption in esp%d_output", afnumber));
675 stat->out_esphist[sav->alg_enc]++;
676 key_sa_recordxfer(sav, m);
683 panic("something bad in esp_output");
691 struct ipsecrequest *isr;
694 if (m->m_len < sizeof(struct ip)) {
695 ipseclog((LOG_DEBUG, "esp4_output: first mbuf too short\n"));
699 ip = mtod(m, struct ip *);
700 /* XXX assumes that m->m_next points to payload */
701 return esp_output(m, &ip->ip_p, m->m_next, isr, AF_INET);
707 esp6_output(m, nexthdrp, md, isr)
711 struct ipsecrequest *isr;
713 if (m->m_len < sizeof(struct ip6_hdr)) {
714 ipseclog((LOG_DEBUG, "esp6_output: first mbuf too short\n"));
718 return esp_output(m, nexthdrp, md, isr, AF_INET6);