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>
29 #include "opt_ipsec.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
36 #include <sys/protosw.h>
37 #include <sys/socket.h>
38 #include <sys/sockopt.h>
39 #include <sys/sysctl.h>
41 #include <netinet/in.h>
42 #include <netinet/in_pcb.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
45 #include <netinet/ip_var.h>
46 #include <netinet/tcp.h>
47 #include <netinet/udp.h>
48 #include <netinet/udp_var.h>
52 #include <netipsec/ipsec.h>
53 #include <netipsec/esp.h>
54 #include <netipsec/esp_var.h>
55 #include <netipsec/xform.h>
57 #include <netipsec/key.h>
58 #include <netipsec/key_debug.h>
59 #include <netipsec/ipsec_support.h>
60 #include <machine/in_cksum.h>
63 * Handle UDP_ENCAP socket option. Always return with released INP_WLOCK.
66 udp_ipsec_pcbctl(struct inpcb *inp, struct sockopt *sopt)
71 INP_WLOCK_ASSERT(inp);
72 if (sopt->sopt_name != UDP_ENCAP) {
78 if (sopt->sopt_dir == SOPT_GET) {
79 if (up->u_flags & UF_ESPINUDP)
80 optval = UDP_ENCAP_ESPINUDP;
84 return (sooptcopyout(sopt, &optval, sizeof(optval)));
88 error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
95 up->u_flags &= ~UF_ESPINUDP;
97 case UDP_ENCAP_ESPINUDP:
98 up->u_flags |= UF_ESPINUDP;
108 * Potentially decap ESP in UDP frame. Check for an ESP header.
109 * If present, strip the UDP header and push the result through IPSec.
111 * Returns error if mbuf consumed and/or processed, otherwise 0.
114 udp_ipsec_input(struct mbuf *m, int off, int af)
116 union sockaddr_union dst;
117 struct secasvar *sav;
124 * Just return if packet doesn't have enough data.
125 * We need at least [IP header + UDP header + ESP header].
126 * NAT-Keepalive packet has only one byte of payload, so it
127 * by default will not be processed.
129 if (m->m_pkthdr.len < off + sizeof(struct esp))
132 m_copydata(m, off, sizeof(uint32_t), (caddr_t)&spi);
133 if (spi == 0) /* Non-ESP marker. */
137 * Find SA and check that it is configured for UDP
140 bzero(&dst, sizeof(dst));
141 dst.sa.sa_family = af;
145 dst.sin.sin_len = sizeof(struct sockaddr_in);
146 ip = mtod(m, struct ip *);
147 ip->ip_p = IPPROTO_ESP;
148 off = offsetof(struct ip, ip_p);
149 hlen = ip->ip_hl << 2;
150 dst.sin.sin_addr = ip->ip_dst;
159 ESPSTAT_INC(esps_nopf);
161 return (EPFNOSUPPORT);
164 sav = key_allocsa(&dst, IPPROTO_ESP, spi);
166 ESPSTAT_INC(esps_notdb);
170 udp = mtodo(m, hlen);
171 if (sav->natt == NULL ||
172 sav->natt->sport != udp->uh_sport ||
173 sav->natt->dport != udp->uh_dport) {
174 /* XXXAE: should we check source address? */
175 ESPSTAT_INC(esps_notdb);
181 * Remove the UDP header
184 * +----+------+-----+
186 * +----+------+-----+
194 m_striphdr(m, hlen, sizeof(*udp));
196 * We cannot yet update the cksums so clear any h/w cksum flags
197 * as they are no longer valid.
199 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)
200 m->m_pkthdr.csum_flags &= ~(CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
202 * We can update ip_len and ip_sum here, but ipsec4_input_cb()
203 * will do this anyway, so don't touch them here.
205 ESPSTAT_INC(esps_input);
206 (*sav->tdb_xform->xf_input)(m, sav, hlen, off);
207 return (EINPROGRESS); /* Consumed by IPsec. */
211 udp_ipsec_output(struct mbuf *m, struct secasvar *sav)
218 IPSEC_ASSERT(sav->natt != NULL, ("UDP encapsulation isn't required."));
220 if (sav->sah->saidx.dst.sa.sa_family == AF_INET6)
221 return (EAFNOSUPPORT);
223 ip = mtod(m, struct ip *);
224 hlen = ip->ip_hl << 2;
225 n = m_makespace(m, hlen, sizeof(*udp), &off);
227 DPRINTF(("%s: m_makespace for udphdr failed\n", __func__));
232 udp->uh_dport = sav->natt->dport;
233 udp->uh_sport = sav->natt->sport;
235 udp->uh_ulen = htons(m->m_pkthdr.len - hlen);
237 ip = mtod(m, struct ip *);
238 ip->ip_len = htons(m->m_pkthdr.len);
239 ip->ip_p = IPPROTO_UDP;
244 udp_ipsec_adjust_cksum(struct mbuf *m, struct secasvar *sav, int proto,
250 IPSEC_ASSERT(sav->natt != NULL, ("NAT-T isn't required"));
251 IPSEC_ASSERT(proto == IPPROTO_UDP || proto == IPPROTO_TCP,
252 ("unexpected protocol %u", proto));
254 if (proto == IPPROTO_UDP)
255 off = offsetof(struct udphdr, uh_sum);
257 off = offsetof(struct tcphdr, th_sum);
259 if (V_natt_cksum_policy == 0) { /* auto */
260 if (sav->natt->cksum != 0) {
261 /* Incrementally recompute. */
262 m_copydata(m, skip + off, sizeof(cksum),
264 /* Do not adjust UDP checksum if it is zero. */
265 if (proto == IPPROTO_UDP && cksum == 0)
267 cksum = in_addword(cksum, sav->natt->cksum);
269 /* No OA from IKEd. */
270 if (proto == IPPROTO_TCP) {
271 /* Ignore for TCP. */
272 m->m_pkthdr.csum_data = 0xffff;
273 m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID |
277 cksum = 0; /* Reset for UDP. */
279 m_copyback(m, skip + off, sizeof(cksum), (caddr_t)&cksum);
280 } else { /* Fully recompute */
281 ip = mtod(m, struct ip *);
282 cksum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
283 htons(m->m_pkthdr.len - skip + proto));
284 m_copyback(m, skip + off, sizeof(cksum), (caddr_t)&cksum);
285 m->m_pkthdr.csum_flags =
286 (proto == IPPROTO_UDP) ? CSUM_UDP: CSUM_TCP;
287 m->m_pkthdr.csum_data = off;
289 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;