2 * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include "opt_ipsec.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
38 #include <sys/protosw.h>
39 #include <sys/socket.h>
40 #include <sys/sockopt.h>
41 #include <sys/sysctl.h>
43 #include <netinet/in.h>
44 #include <netinet/in_pcb.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip_var.h>
48 #include <netinet/tcp.h>
49 #include <netinet/udp.h>
50 #include <netinet/udp_var.h>
54 #include <netipsec/ipsec.h>
55 #include <netipsec/esp.h>
56 #include <netipsec/esp_var.h>
57 #include <netipsec/xform.h>
59 #include <netipsec/key.h>
60 #include <netipsec/key_debug.h>
61 #include <netipsec/ipsec_support.h>
62 #include <machine/in_cksum.h>
65 * Handle UDP_ENCAP socket option. Always return with released INP_WLOCK.
68 udp_ipsec_pcbctl(struct inpcb *inp, struct sockopt *sopt)
73 INP_WLOCK_ASSERT(inp);
74 if (sopt->sopt_name != UDP_ENCAP) {
80 if (sopt->sopt_dir == SOPT_GET) {
81 if (up->u_flags & UF_ESPINUDP)
82 optval = UDP_ENCAP_ESPINUDP;
86 return (sooptcopyout(sopt, &optval, sizeof(optval)));
90 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
97 up->u_flags &= ~UF_ESPINUDP;
99 case UDP_ENCAP_ESPINUDP:
100 up->u_flags |= UF_ESPINUDP;
110 * Potentially decap ESP in UDP frame. Check for an ESP header.
111 * If present, strip the UDP header and push the result through IPSec.
113 * Returns error if mbuf consumed and/or processed, otherwise 0.
116 udp_ipsec_input(struct mbuf *m, int off, int af)
118 union sockaddr_union dst;
119 struct secasvar *sav;
126 * Just return if packet doesn't have enough data.
127 * We need at least [IP header + UDP header + ESP header].
128 * NAT-Keepalive packet has only one byte of payload, so it
129 * by default will not be processed.
131 if (m->m_pkthdr.len < off + sizeof(struct esp))
134 m_copydata(m, off, sizeof(uint32_t), (caddr_t)&spi);
135 if (spi == 0) /* Non-ESP marker. */
139 * Find SA and check that it is configured for UDP
142 bzero(&dst, sizeof(dst));
143 dst.sa.sa_family = af;
147 dst.sin.sin_len = sizeof(struct sockaddr_in);
148 ip = mtod(m, struct ip *);
149 ip->ip_p = IPPROTO_ESP;
150 off = offsetof(struct ip, ip_p);
151 hlen = ip->ip_hl << 2;
152 dst.sin.sin_addr = ip->ip_dst;
161 ESPSTAT_INC(esps_nopf);
163 return (EPFNOSUPPORT);
166 sav = key_allocsa(&dst, IPPROTO_ESP, spi);
168 ESPSTAT_INC(esps_notdb);
172 udp = mtodo(m, hlen);
173 if (sav->natt == NULL ||
174 sav->natt->sport != udp->uh_sport ||
175 sav->natt->dport != udp->uh_dport) {
176 /* XXXAE: should we check source address? */
177 ESPSTAT_INC(esps_notdb);
183 * Remove the UDP header
186 * +----+------+-----+
188 * +----+------+-----+
196 m_striphdr(m, hlen, sizeof(*udp));
198 * We cannot yet update the cksums so clear any h/w cksum flags
199 * as they are no longer valid.
201 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)
202 m->m_pkthdr.csum_flags &= ~(CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
204 * We can update ip_len and ip_sum here, but ipsec4_input_cb()
205 * will do this anyway, so don't touch them here.
207 ESPSTAT_INC(esps_input);
208 (*sav->tdb_xform->xf_input)(m, sav, hlen, off);
209 return (EINPROGRESS); /* Consumed by IPsec. */
213 udp_ipsec_output(struct mbuf *m, struct secasvar *sav)
220 IPSEC_ASSERT(sav->natt != NULL, ("UDP encapsulation isn't required."));
222 if (sav->sah->saidx.dst.sa.sa_family == AF_INET6)
223 return (EAFNOSUPPORT);
225 ip = mtod(m, struct ip *);
226 hlen = ip->ip_hl << 2;
227 n = m_makespace(m, hlen, sizeof(*udp), &off);
229 DPRINTF(("%s: m_makespace for udphdr failed\n", __func__));
234 udp->uh_dport = sav->natt->dport;
235 udp->uh_sport = sav->natt->sport;
237 udp->uh_ulen = htons(m->m_pkthdr.len - hlen);
239 ip = mtod(m, struct ip *);
240 ip->ip_len = htons(m->m_pkthdr.len);
241 ip->ip_p = IPPROTO_UDP;
246 udp_ipsec_adjust_cksum(struct mbuf *m, struct secasvar *sav, int proto,
252 IPSEC_ASSERT(sav->natt != NULL, ("NAT-T isn't required"));
253 IPSEC_ASSERT(proto == IPPROTO_UDP || proto == IPPROTO_TCP,
254 ("unexpected protocol %u", proto));
256 if (proto == IPPROTO_UDP)
257 off = offsetof(struct udphdr, uh_sum);
259 off = offsetof(struct tcphdr, th_sum);
261 if (V_natt_cksum_policy == 0) { /* auto */
262 if (sav->natt->cksum != 0) {
263 /* Incrementally recompute. */
264 m_copydata(m, skip + off, sizeof(cksum),
266 /* Do not adjust UDP checksum if it is zero. */
267 if (proto == IPPROTO_UDP && cksum == 0)
269 cksum = in_addword(cksum, sav->natt->cksum);
271 /* No OA from IKEd. */
272 if (proto == IPPROTO_TCP) {
273 /* Ignore for TCP. */
274 m->m_pkthdr.csum_data = 0xffff;
275 m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID |
279 cksum = 0; /* Reset for UDP. */
281 m_copyback(m, skip + off, sizeof(cksum), (caddr_t)&cksum);
282 } else { /* Fully recompute */
283 ip = mtod(m, struct ip *);
284 cksum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
285 htons(m->m_pkthdr.len - skip + proto));
286 m_copyback(m, skip + off, sizeof(cksum), (caddr_t)&cksum);
287 m->m_pkthdr.csum_flags =
288 (proto == IPPROTO_UDP) ? CSUM_UDP: CSUM_TCP;
289 m->m_pkthdr.csum_data = off;
291 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;