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 static int esp_output __P((struct mbuf *, u_char *, struct mbuf *,
81 struct ipsecrequest *, int));
84 * compute ESP header size.
88 struct ipsecrequest *isr;
91 const struct esp_algorithm *algo;
92 const struct ah_algorithm *aalgo;
99 panic("esp_hdrsiz: NULL was passed.");
103 if (isr->saidx.proto != IPPROTO_ESP)
104 panic("unsupported mode passed to esp_hdrsiz");
108 if (sav->state != SADB_SASTATE_MATURE
109 && sav->state != SADB_SASTATE_DYING)
112 /* we need transport mode ESP. */
113 algo = esp_algorithm_lookup(sav->alg_enc);
122 * right now we don't calcurate the padding size. simply
123 * treat the padding size as constant, for simplicity.
125 * XXX variable size padding support
127 if (sav->flags & SADB_X_EXT_OLD) {
129 hdrsiz = sizeof(struct esp) + ivlen + 9;
132 aalgo = ah_algorithm_lookup(sav->alg_auth);
133 if (aalgo && sav->replay && sav->key_auth)
134 authlen = (aalgo->sumsiz)(sav);
137 hdrsiz = sizeof(struct newesp) + ivlen + 9 + authlen;
145 * sizeof(struct newesp) > sizeof(struct esp).
146 * esp_max_ivlen() = max ivlen for CBC mode
147 * 9 = (maximum padding length without random padding length)
148 * + (Pad Length field) + (Next Header field).
149 * 16 = maximum ICV we support.
151 return sizeof(struct newesp) + esp_max_ivlen() + 9 + 16;
155 * Modify the packet so that the payload is encrypted.
156 * The mbuf (m) must start with IPv4 or IPv6 header.
157 * On failure, free the given mbuf and return NULL.
162 * IP ......... payload
163 * during the encryption:
164 * m nexthdrp mprev md
166 * IP ............... esp iv payload pad padlen nxthdr
167 * <--><-><------><--------------->
168 * esplen plen extendsiz
172 * <-----------------> espoff
175 esp_output(m, nexthdrp, md, isr, af)
179 struct ipsecrequest *isr;
185 struct esptail *esptail;
186 struct secasvar *sav = isr->sav;
187 const struct esp_algorithm *algo;
190 size_t plen; /* payload length to be encrypted */
196 struct ipsecstat *stat;
212 ipseclog((LOG_ERR, "esp_output: unsupported af %d\n", af));
213 return 0; /* no change at all */
216 /* some sanity check */
217 if ((sav->flags & SADB_X_EXT_OLD) == 0 && !sav->replay) {
224 ip = mtod(m, struct ip *);
225 ipseclog((LOG_DEBUG, "esp4_output: internal error: "
226 "sav->replay is null: %x->%x, SPI=%u\n",
227 (u_int32_t)ntohl(ip->ip_src.s_addr),
228 (u_int32_t)ntohl(ip->ip_dst.s_addr),
229 (u_int32_t)ntohl(sav->spi)));
230 ipsecstat.out_inval++;
236 ipseclog((LOG_DEBUG, "esp6_output: internal error: "
237 "sav->replay is null: SPI=%u\n",
238 (u_int32_t)ntohl(sav->spi)));
239 ipsec6stat.out_inval++;
243 panic("esp_output: should not reach here");
249 algo = esp_algorithm_lookup(sav->alg_enc);
251 ipseclog((LOG_ERR, "esp_output: unsupported algorithm: "
252 "SPI=%u\n", (u_int32_t)ntohl(sav->spi)));
260 panic("invalid ivlen");
266 * XXX inserts ESP header right after IPv4 header. should
267 * chase the header chain.
268 * XXX sequential number
271 struct ip *ip = NULL;
274 struct ip6_hdr *ip6 = NULL;
276 size_t esplen; /* sizeof(struct esp/newesp) */
277 size_t esphlen; /* sizeof(struct esp/newesp) + ivlen */
278 size_t hlen = 0; /* ip header len */
280 if (sav->flags & SADB_X_EXT_OLD) {
282 esplen = sizeof(struct esp);
285 if (sav->flags & SADB_X_EXT_DERIV)
286 esplen = sizeof(struct esp);
288 esplen = sizeof(struct newesp);
290 esphlen = esplen + ivlen;
292 for (mprev = m; mprev && mprev->m_next != md; mprev = mprev->m_next)
294 if (mprev == NULL || mprev->m_next != md) {
295 ipseclog((LOG_DEBUG, "esp%d_output: md is not in chain\n",
302 for (n = md; n; n = n->m_next)
308 ip = mtod(m, struct ip *);
310 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
312 hlen = ip->ip_hl << 2;
318 ip6 = mtod(m, struct ip6_hdr *);
324 /* make the packet over-writable */
325 mprev->m_next = NULL;
326 if ((md = ipsec_copypkt(md)) == NULL) {
333 espoff = m->m_pkthdr.len - plen;
336 * grow the mbuf to accomodate ESP header.
337 * before: IP ... payload
338 * after: IP ... ESP IV payload
340 if (M_LEADINGSPACE(md) < esphlen || (md->m_flags & M_EXT) != 0) {
341 MGET(n, M_DONTWAIT, MT_DATA);
350 m->m_pkthdr.len += esphlen;
351 esp = mtod(n, struct esp *);
353 md->m_len += esphlen;
354 md->m_data -= esphlen;
355 m->m_pkthdr.len += esphlen;
356 esp = mtod(md, struct esp *);
360 *nexthdrp = IPPROTO_ESP;
364 if (esphlen < (IP_MAXPACKET - ntohs(ip->ip_len)))
365 ip->ip_len = htons(ntohs(ip->ip_len) + esphlen);
368 "IPv4 ESP output: size exceeds limit\n"));
369 ipsecstat.out_inval++;
378 /* total packet length will be computed in ip6_output() */
384 /* initialize esp header. */
386 if ((sav->flags & SADB_X_EXT_OLD) == 0) {
388 nesp = (struct newesp *)esp;
389 if (sav->replay->count == ~0) {
390 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) {
391 /* XXX Is it noisy ? */
392 ipseclog((LOG_WARNING,
393 "replay counter overflowed. %s\n",
394 ipsec_logsastr(sav)));
400 sav->replay->count++;
402 * XXX sequence number must not be cycled, if the SA is
403 * installed by IKE daemon.
405 nesp->esp_seq = htonl(sav->replay->count & 0xffffffff);
410 * find the last mbuf. make some room for ESP trailer.
413 struct ip *ip = NULL;
421 padbound = algo->padbound;
424 /* ESP packet, including nxthdr field, must be length of 4n */
428 extendsiz = padbound - (plen % padbound);
430 extendsiz = padbound + 1;
436 randpadmax = ip4_esp_randpad;
441 randpadmax = ip6_esp_randpad;
448 if (randpadmax < 0 || plen + extendsiz >= randpadmax)
454 randpadmax = (randpadmax / padbound) * padbound;
455 n = (randpadmax - plen + extendsiz) / padbound;
458 n = (random() % n) * padbound;
463 * make sure we do not pad too much.
464 * MLEN limitation comes from the trailer attachment
466 * 256 limitation comes from sequential padding.
467 * also, the 1-octet length field in ESP trailer imposes
468 * limitation (but is less strict than sequential padding
469 * as length field do not count the last 2 octets).
471 if (extendsiz + n <= MLEN && extendsiz + n < 256)
476 if (extendsiz > MLEN || extendsiz >= 256)
477 panic("extendsiz too big in esp_output");
485 * if M_EXT, the external mbuf data may be shared among
486 * two consequtive TCP packets, and it may be unsafe to use the
489 if (!(n->m_flags & M_EXT) && extendsiz < M_TRAILINGSPACE(n)) {
490 extend = mtod(n, u_char *) + n->m_len;
491 n->m_len += extendsiz;
492 m->m_pkthdr.len += extendsiz;
496 MGET(nn, M_DONTWAIT, MT_DATA);
498 ipseclog((LOG_DEBUG, "esp%d_output: can't alloc mbuf",
504 extend = mtod(nn, u_char *);
505 nn->m_len = extendsiz;
509 m->m_pkthdr.len += extendsiz;
511 switch (sav->flags & SADB_X_EXT_PMASK) {
512 case SADB_X_EXT_PRAND:
513 key_randomfill(extend, extendsiz);
515 case SADB_X_EXT_PZERO:
516 bzero(extend, extendsiz);
518 case SADB_X_EXT_PSEQ:
519 for (i = 0; i < extendsiz; i++)
520 extend[i] = (i + 1) & 0xff;
524 /* initialize esp trailer. */
525 esptail = (struct esptail *)
526 (mtod(n, u_int8_t *) + n->m_len - sizeof(struct esptail));
527 esptail->esp_nxt = nxt;
528 esptail->esp_padlen = extendsiz - 2;
530 /* modify IP header (for ESP header part only) */
534 ip = mtod(m, struct ip *);
535 if (extendsiz < (IP_MAXPACKET - ntohs(ip->ip_len)))
536 ip->ip_len = htons(ntohs(ip->ip_len) + extendsiz);
539 "IPv4 ESP output: size exceeds limit\n"));
540 ipsecstat.out_inval++;
549 /* total packet length will be computed in ip6_output() */
556 * pre-compute and cache intermediate key
558 error = esp_schedule(algo, sav);
566 * encrypt the packet, based on security association
567 * and the algorithm specified.
570 panic("internal error: no encrypt function");
571 if ((*algo->encrypt)(m, espoff, plen + extendsiz, sav, algo, ivlen)) {
572 /* m is already freed */
573 ipseclog((LOG_ERR, "packet encryption failure\n"));
580 * calculate ICV if required.
586 if (sav->alg_auth == SADB_AALG_NONE)
590 const struct ah_algorithm *aalgo;
591 u_char authbuf[AH_MAXSUMSIZE];
599 aalgo = ah_algorithm_lookup(sav->alg_auth);
602 siz = ((aalgo->sumsiz)(sav) + 3) & ~(4 - 1);
603 if (AH_MAXSUMSIZE < siz)
604 panic("assertion failed for AH_MAXSUMSIZE");
606 if (esp_auth(m, espoff, m->m_pkthdr.len - espoff, sav, authbuf)) {
607 ipseclog((LOG_ERR, "ESP checksum generation failure\n"));
618 if (!(n->m_flags & M_EXT) && siz < M_TRAILINGSPACE(n)) { /* XXX */
620 m->m_pkthdr.len += siz;
621 p = mtod(n, u_char *) + n->m_len - siz;
625 MGET(nn, M_DONTWAIT, MT_DATA);
627 ipseclog((LOG_DEBUG, "can't alloc mbuf in esp%d_output",
637 m->m_pkthdr.len += siz;
638 p = mtod(nn, u_char *);
640 bcopy(authbuf, p, siz);
642 /* modify IP header (for ESP header part only) */
646 ip = mtod(m, struct ip *);
647 if (siz < (IP_MAXPACKET - ntohs(ip->ip_len)))
648 ip->ip_len = htons(ntohs(ip->ip_len) + siz);
651 "IPv4 ESP output: size exceeds limit\n"));
652 ipsecstat.out_inval++;
661 /* total packet length will be computed in ip6_output() */
670 "NULL mbuf after encryption in esp%d_output", afnumber));
673 stat->out_esphist[sav->alg_enc]++;
674 key_sa_recordxfer(sav, m);
681 panic("something bad in esp_output");
689 struct ipsecrequest *isr;
692 if (m->m_len < sizeof(struct ip)) {
693 ipseclog((LOG_DEBUG, "esp4_output: first mbuf too short\n"));
697 ip = mtod(m, struct ip *);
698 /* XXX assumes that m->m_next points to payload */
699 return esp_output(m, &ip->ip_p, m->m_next, isr, AF_INET);
705 esp6_output(m, nexthdrp, md, isr)
709 struct ipsecrequest *isr;
711 if (m->m_len < sizeof(struct ip6_hdr)) {
712 ipseclog((LOG_DEBUG, "esp6_output: first mbuf too short\n"));
716 return esp_output(m, nexthdrp, md, isr, AF_INET6);