ipsec_output.c revision 1.60 1 1.60 ozaki /* $NetBSD: ipsec_output.c,v 1.60 2017/08/10 06:11:24 ozaki-r Exp $ */
2 1.9 thorpej
3 1.9 thorpej /*-
4 1.9 thorpej * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
5 1.9 thorpej * All rights reserved.
6 1.9 thorpej *
7 1.9 thorpej * Redistribution and use in source and binary forms, with or without
8 1.9 thorpej * modification, are permitted provided that the following conditions
9 1.9 thorpej * are met:
10 1.9 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.9 thorpej * notice, this list of conditions and the following disclaimer.
12 1.9 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.9 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.9 thorpej * documentation and/or other materials provided with the distribution.
15 1.9 thorpej *
16 1.9 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.9 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.9 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.9 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.9 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.9 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.9 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.9 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.9 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.9 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.9 thorpej * SUCH DAMAGE.
27 1.9 thorpej *
28 1.9 thorpej * $FreeBSD: /repoman/r/ncvs/src/sys/netipsec/ipsec_output.c,v 1.3.2.2 2003/03/28 20:32:53 sam Exp $
29 1.9 thorpej */
30 1.1 jonathan
31 1.1 jonathan #include <sys/cdefs.h>
32 1.60 ozaki __KERNEL_RCSID(0, "$NetBSD: ipsec_output.c,v 1.60 2017/08/10 06:11:24 ozaki-r Exp $");
33 1.1 jonathan
34 1.1 jonathan /*
35 1.1 jonathan * IPsec output processing.
36 1.1 jonathan */
37 1.42 ozaki #if defined(_KERNEL_OPT)
38 1.1 jonathan #include "opt_inet.h"
39 1.57 ozaki #include "opt_net_mpsafe.h"
40 1.42 ozaki #endif
41 1.1 jonathan
42 1.1 jonathan #include <sys/param.h>
43 1.1 jonathan #include <sys/systm.h>
44 1.1 jonathan #include <sys/mbuf.h>
45 1.1 jonathan #include <sys/domain.h>
46 1.1 jonathan #include <sys/protosw.h>
47 1.1 jonathan #include <sys/socket.h>
48 1.1 jonathan #include <sys/errno.h>
49 1.1 jonathan #include <sys/syslog.h>
50 1.1 jonathan
51 1.1 jonathan #include <net/if.h>
52 1.1 jonathan #include <net/route.h>
53 1.1 jonathan
54 1.1 jonathan #include <netinet/in.h>
55 1.1 jonathan #include <netinet/in_systm.h>
56 1.1 jonathan #include <netinet/ip.h>
57 1.1 jonathan #include <netinet/ip_var.h>
58 1.1 jonathan #include <netinet/in_var.h>
59 1.1 jonathan #include <netinet/ip_ecn.h>
60 1.1 jonathan
61 1.1 jonathan #include <netinet/ip6.h>
62 1.1 jonathan #ifdef INET6
63 1.1 jonathan #include <netinet6/ip6_var.h>
64 1.1 jonathan #endif
65 1.1 jonathan #include <netinet/in_pcb.h>
66 1.1 jonathan #ifdef INET6
67 1.1 jonathan #include <netinet/icmp6.h>
68 1.1 jonathan #endif
69 1.22 degroote #include <netinet/udp.h>
70 1.1 jonathan
71 1.1 jonathan #include <netipsec/ipsec.h>
72 1.13 jonathan #include <netipsec/ipsec_var.h>
73 1.27 thorpej #include <netipsec/ipsec_private.h>
74 1.1 jonathan #ifdef INET6
75 1.1 jonathan #include <netipsec/ipsec6.h>
76 1.1 jonathan #endif
77 1.1 jonathan #include <netipsec/ah_var.h>
78 1.1 jonathan #include <netipsec/esp_var.h>
79 1.1 jonathan #include <netipsec/ipcomp_var.h>
80 1.1 jonathan
81 1.1 jonathan #include <netipsec/xform.h>
82 1.1 jonathan
83 1.7 tls #include <netipsec/key.h>
84 1.7 tls #include <netipsec/keydb.h>
85 1.7 tls #include <netipsec/key_debug.h>
86 1.1 jonathan
87 1.10 jonathan #include <net/net_osdep.h> /* ovbcopy() in ipsec6_encapsulate() */
88 1.10 jonathan
89 1.60 ozaki static percpu_t *ipsec_rtcache_percpu __cacheline_aligned;
90 1.25 degroote
91 1.25 degroote /*
92 1.25 degroote * Add a IPSEC_OUT_DONE tag to mark that we have finished the ipsec processing
93 1.25 degroote * It will be used by ip{,6}_output to check if we have already or not
94 1.25 degroote * processed this packet.
95 1.25 degroote */
96 1.25 degroote static int
97 1.25 degroote ipsec_register_done(struct mbuf *m, int * error)
98 1.25 degroote {
99 1.25 degroote struct m_tag *mtag;
100 1.25 degroote
101 1.25 degroote mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, 0, M_NOWAIT);
102 1.25 degroote if (mtag == NULL) {
103 1.48 ozaki IPSECLOG(LOG_DEBUG, "could not get packet tag\n");
104 1.25 degroote *error = ENOMEM;
105 1.25 degroote return -1;
106 1.25 degroote }
107 1.25 degroote
108 1.25 degroote m_tag_prepend(m, mtag);
109 1.25 degroote return 0;
110 1.25 degroote }
111 1.25 degroote
112 1.26 degroote static int
113 1.26 degroote ipsec_reinject_ipstack(struct mbuf *m, int af)
114 1.26 degroote {
115 1.59 ozaki int rv = -1;
116 1.60 ozaki struct route *ro;
117 1.59 ozaki
118 1.59 ozaki KASSERT(af == AF_INET || af == AF_INET6);
119 1.59 ozaki
120 1.59 ozaki #ifndef NET_MPSAFE
121 1.59 ozaki KERNEL_LOCK(1, NULL);
122 1.30 drochner #endif
123 1.60 ozaki ro = percpu_getref(ipsec_rtcache_percpu);
124 1.26 degroote switch (af) {
125 1.26 degroote #ifdef INET
126 1.26 degroote case AF_INET:
127 1.60 ozaki rv = ip_output(m, NULL, ro, IP_RAWOUTPUT|IP_NOIPNEWID,
128 1.37 plunky NULL, NULL);
129 1.59 ozaki break;
130 1.57 ozaki #endif
131 1.26 degroote #ifdef INET6
132 1.26 degroote case AF_INET6:
133 1.26 degroote /*
134 1.26 degroote * We don't need massage, IPv6 header fields are always in
135 1.26 degroote * net endian.
136 1.26 degroote */
137 1.60 ozaki rv = ip6_output(m, NULL, ro, 0, NULL, NULL, NULL);
138 1.59 ozaki break;
139 1.57 ozaki #endif
140 1.59 ozaki }
141 1.60 ozaki percpu_putref(ipsec_rtcache_percpu);
142 1.57 ozaki #ifndef NET_MPSAFE
143 1.59 ozaki KERNEL_UNLOCK_ONE(NULL);
144 1.57 ozaki #endif
145 1.26 degroote
146 1.59 ozaki return rv;
147 1.26 degroote }
148 1.26 degroote
149 1.1 jonathan int
150 1.54 ozaki ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr,
151 1.54 ozaki struct secasvar *sav)
152 1.1 jonathan {
153 1.1 jonathan struct secasindex *saidx;
154 1.1 jonathan int error;
155 1.22 degroote #ifdef INET
156 1.22 degroote struct ip * ip;
157 1.22 degroote #endif /* INET */
158 1.22 degroote #ifdef INET6
159 1.22 degroote struct ip6_hdr * ip6;
160 1.22 degroote #endif /* INET6 */
161 1.22 degroote struct mbuf * mo;
162 1.22 degroote struct udphdr *udp = NULL;
163 1.22 degroote uint64_t * data = NULL;
164 1.22 degroote int hlen, roff;
165 1.1 jonathan
166 1.1 jonathan IPSEC_SPLASSERT_SOFTNET("ipsec_process_done");
167 1.1 jonathan
168 1.44 ozaki KASSERT(m != NULL);
169 1.44 ozaki KASSERT(isr != NULL);
170 1.44 ozaki KASSERT(sav != NULL);
171 1.1 jonathan
172 1.1 jonathan saidx = &sav->sah->saidx;
173 1.22 degroote
174 1.22 degroote if(sav->natt_type != 0) {
175 1.22 degroote ip = mtod(m, struct ip *);
176 1.22 degroote
177 1.22 degroote hlen = sizeof(struct udphdr);
178 1.22 degroote if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
179 1.22 degroote hlen += sizeof(uint64_t);
180 1.22 degroote
181 1.22 degroote mo = m_makespace(m, sizeof(struct ip), hlen, &roff);
182 1.22 degroote if (mo == NULL) {
183 1.47 ryo char buf[IPSEC_ADDRSTRLEN];
184 1.48 ozaki IPSECLOG(LOG_DEBUG,
185 1.48 ozaki "failed to inject %u byte UDP for SA %s/%08lx\n",
186 1.47 ryo hlen, ipsec_address(&saidx->dst, buf, sizeof(buf)),
187 1.48 ozaki (u_long) ntohl(sav->spi));
188 1.22 degroote error = ENOBUFS;
189 1.22 degroote goto bad;
190 1.22 degroote }
191 1.22 degroote
192 1.22 degroote udp = (struct udphdr*) (mtod(mo, char*) + roff);
193 1.22 degroote data = (uint64_t*) (udp + 1);
194 1.22 degroote
195 1.22 degroote if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
196 1.22 degroote *data = 0; /* NON-IKE Marker */
197 1.22 degroote
198 1.22 degroote if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
199 1.22 degroote udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
200 1.22 degroote else
201 1.22 degroote udp->uh_sport = key_portfromsaddr(&saidx->src);
202 1.22 degroote
203 1.22 degroote udp->uh_dport = key_portfromsaddr(&saidx->dst);
204 1.22 degroote udp->uh_sum = 0;
205 1.29 dyoung udp->uh_ulen = htons(m->m_pkthdr.len - (ip->ip_hl << 2));
206 1.22 degroote }
207 1.22 degroote
208 1.1 jonathan switch (saidx->dst.sa.sa_family) {
209 1.1 jonathan #ifdef INET
210 1.1 jonathan case AF_INET:
211 1.1 jonathan /* Fix the header length, for AH processing. */
212 1.22 degroote ip = mtod(m, struct ip *);
213 1.22 degroote ip->ip_len = htons(m->m_pkthdr.len);
214 1.22 degroote if (sav->natt_type != 0)
215 1.22 degroote ip->ip_p = IPPROTO_UDP;
216 1.1 jonathan break;
217 1.1 jonathan #endif /* INET */
218 1.1 jonathan #ifdef INET6
219 1.1 jonathan case AF_INET6:
220 1.1 jonathan /* Fix the header length, for AH processing. */
221 1.1 jonathan if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
222 1.1 jonathan error = ENXIO;
223 1.1 jonathan goto bad;
224 1.1 jonathan }
225 1.1 jonathan if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
226 1.1 jonathan /* No jumbogram support. */
227 1.1 jonathan error = ENXIO; /*?*/
228 1.1 jonathan goto bad;
229 1.1 jonathan }
230 1.22 degroote ip6 = mtod(m, struct ip6_hdr *);
231 1.22 degroote ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
232 1.22 degroote if (sav->natt_type != 0)
233 1.22 degroote ip6->ip6_nxt = IPPROTO_UDP;
234 1.1 jonathan break;
235 1.1 jonathan #endif /* INET6 */
236 1.1 jonathan default:
237 1.48 ozaki IPSECLOG(LOG_DEBUG, "unknown protocol family %u\n",
238 1.48 ozaki saidx->dst.sa.sa_family);
239 1.1 jonathan error = ENXIO;
240 1.1 jonathan goto bad;
241 1.1 jonathan }
242 1.1 jonathan
243 1.32 drochner key_sa_recordxfer(sav, m);
244 1.32 drochner
245 1.1 jonathan /*
246 1.1 jonathan * If there's another (bundled) SA to apply, do so.
247 1.1 jonathan * Note that this puts a burden on the kernel stack size.
248 1.1 jonathan * If this is a problem we'll need to introduce a queue
249 1.1 jonathan * to set the packet on so we can unwind the stack before
250 1.1 jonathan * doing further processing.
251 1.1 jonathan */
252 1.1 jonathan if (isr->next) {
253 1.27 thorpej IPSEC_STATINC(IPSEC_STAT_OUT_BUNDLESA);
254 1.29 dyoung switch ( saidx->dst.sa.sa_family ) {
255 1.21 degroote #ifdef INET
256 1.29 dyoung case AF_INET:
257 1.56 ozaki return ipsec4_process_packet(m, isr->next, NULL);
258 1.21 degroote #endif /* INET */
259 1.21 degroote #ifdef INET6
260 1.21 degroote case AF_INET6:
261 1.29 dyoung return ipsec6_process_packet(m,isr->next);
262 1.21 degroote #endif /* INET6 */
263 1.21 degroote default :
264 1.48 ozaki IPSECLOG(LOG_DEBUG, "unknown protocol family %u\n",
265 1.48 ozaki saidx->dst.sa.sa_family);
266 1.21 degroote error = ENXIO;
267 1.21 degroote goto bad;
268 1.29 dyoung }
269 1.1 jonathan }
270 1.1 jonathan
271 1.1 jonathan /*
272 1.25 degroote * We're done with IPsec processing,
273 1.25 degroote * mark that we have already processed the packet
274 1.25 degroote * transmit it packet using the appropriate network protocol (IP or IPv6).
275 1.1 jonathan */
276 1.25 degroote
277 1.25 degroote if (ipsec_register_done(m, &error) < 0)
278 1.25 degroote goto bad;
279 1.25 degroote
280 1.26 degroote return ipsec_reinject_ipstack(m, saidx->dst.sa.sa_family);
281 1.1 jonathan bad:
282 1.1 jonathan m_freem(m);
283 1.1 jonathan return (error);
284 1.1 jonathan }
285 1.1 jonathan
286 1.26 degroote /*
287 1.26 degroote * ipsec_nextisr can return :
288 1.26 degroote * - isr == NULL and error != 0 => something is bad : the packet must be
289 1.26 degroote * discarded
290 1.26 degroote * - isr == NULL and error == 0 => no more rules to apply, ipsec processing
291 1.26 degroote * is done, reinject it in ip stack
292 1.26 degroote * - isr != NULL (error == 0) => we need to apply one rule to the packet
293 1.26 degroote */
294 1.1 jonathan static struct ipsecrequest *
295 1.1 jonathan ipsec_nextisr(
296 1.1 jonathan struct mbuf *m,
297 1.1 jonathan struct ipsecrequest *isr,
298 1.1 jonathan int af,
299 1.54 ozaki int *error,
300 1.54 ozaki struct secasvar **ret
301 1.1 jonathan )
302 1.1 jonathan {
303 1.49 ozaki #define IPSEC_OSTAT(type) \
304 1.27 thorpej do { \
305 1.27 thorpej switch (isr->saidx.proto) { \
306 1.27 thorpej case IPPROTO_ESP: \
307 1.49 ozaki ESP_STATINC(ESP_STAT_ ## type); \
308 1.27 thorpej break; \
309 1.27 thorpej case IPPROTO_AH: \
310 1.49 ozaki AH_STATINC(AH_STAT_ ## type); \
311 1.27 thorpej break; \
312 1.27 thorpej default: \
313 1.49 ozaki IPCOMP_STATINC(IPCOMP_STAT_ ## type); \
314 1.27 thorpej break; \
315 1.27 thorpej } \
316 1.27 thorpej } while (/*CONSTCOND*/0)
317 1.27 thorpej
318 1.54 ozaki struct secasvar *sav = NULL;
319 1.52 ozaki struct secasindex *saidx;
320 1.1 jonathan
321 1.1 jonathan IPSEC_SPLASSERT_SOFTNET("ipsec_nextisr");
322 1.44 ozaki KASSERTMSG(af == AF_INET || af == AF_INET6,
323 1.44 ozaki "invalid address family %u", af);
324 1.1 jonathan again:
325 1.1 jonathan /*
326 1.1 jonathan * Craft SA index to search for proper SA. Note that
327 1.1 jonathan * we only fillin unspecified SA peers for transport
328 1.1 jonathan * mode; for tunnel mode they must already be filled in.
329 1.1 jonathan */
330 1.52 ozaki saidx = &isr->saidx;
331 1.1 jonathan if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
332 1.1 jonathan /* Fillin unspecified SA peers only for transport mode */
333 1.1 jonathan if (af == AF_INET) {
334 1.1 jonathan struct sockaddr_in *sin;
335 1.1 jonathan struct ip *ip = mtod(m, struct ip *);
336 1.1 jonathan
337 1.1 jonathan if (saidx->src.sa.sa_len == 0) {
338 1.1 jonathan sin = &saidx->src.sin;
339 1.1 jonathan sin->sin_len = sizeof(*sin);
340 1.1 jonathan sin->sin_family = AF_INET;
341 1.1 jonathan sin->sin_port = IPSEC_PORT_ANY;
342 1.1 jonathan sin->sin_addr = ip->ip_src;
343 1.1 jonathan }
344 1.1 jonathan if (saidx->dst.sa.sa_len == 0) {
345 1.1 jonathan sin = &saidx->dst.sin;
346 1.1 jonathan sin->sin_len = sizeof(*sin);
347 1.1 jonathan sin->sin_family = AF_INET;
348 1.1 jonathan sin->sin_port = IPSEC_PORT_ANY;
349 1.1 jonathan sin->sin_addr = ip->ip_dst;
350 1.1 jonathan }
351 1.1 jonathan } else {
352 1.1 jonathan struct sockaddr_in6 *sin6;
353 1.1 jonathan struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
354 1.1 jonathan
355 1.1 jonathan if (saidx->src.sin6.sin6_len == 0) {
356 1.1 jonathan sin6 = (struct sockaddr_in6 *)&saidx->src;
357 1.1 jonathan sin6->sin6_len = sizeof(*sin6);
358 1.1 jonathan sin6->sin6_family = AF_INET6;
359 1.1 jonathan sin6->sin6_port = IPSEC_PORT_ANY;
360 1.1 jonathan sin6->sin6_addr = ip6->ip6_src;
361 1.1 jonathan if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
362 1.1 jonathan /* fix scope id for comparing SPD */
363 1.1 jonathan sin6->sin6_addr.s6_addr16[1] = 0;
364 1.1 jonathan sin6->sin6_scope_id =
365 1.1 jonathan ntohs(ip6->ip6_src.s6_addr16[1]);
366 1.1 jonathan }
367 1.1 jonathan }
368 1.1 jonathan if (saidx->dst.sin6.sin6_len == 0) {
369 1.1 jonathan sin6 = (struct sockaddr_in6 *)&saidx->dst;
370 1.1 jonathan sin6->sin6_len = sizeof(*sin6);
371 1.1 jonathan sin6->sin6_family = AF_INET6;
372 1.1 jonathan sin6->sin6_port = IPSEC_PORT_ANY;
373 1.1 jonathan sin6->sin6_addr = ip6->ip6_dst;
374 1.1 jonathan if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
375 1.1 jonathan /* fix scope id for comparing SPD */
376 1.1 jonathan sin6->sin6_addr.s6_addr16[1] = 0;
377 1.1 jonathan sin6->sin6_scope_id =
378 1.1 jonathan ntohs(ip6->ip6_dst.s6_addr16[1]);
379 1.1 jonathan }
380 1.1 jonathan }
381 1.1 jonathan }
382 1.1 jonathan }
383 1.1 jonathan
384 1.1 jonathan /*
385 1.1 jonathan * Lookup SA and validate it.
386 1.1 jonathan */
387 1.54 ozaki *error = key_checkrequest(isr, &sav);
388 1.1 jonathan if (*error != 0) {
389 1.1 jonathan /*
390 1.1 jonathan * IPsec processing is required, but no SA found.
391 1.1 jonathan * I assume that key_acquire() had been called
392 1.1 jonathan * to get/establish the SA. Here I discard
393 1.1 jonathan * this packet because it is responsibility for
394 1.1 jonathan * upper layer to retransmit the packet.
395 1.1 jonathan */
396 1.27 thorpej IPSEC_STATINC(IPSEC_STAT_OUT_NOSA);
397 1.1 jonathan goto bad;
398 1.1 jonathan }
399 1.26 degroote /* sav may be NULL here if we have an USE rule */
400 1.26 degroote if (sav == NULL) {
401 1.44 ozaki KASSERTMSG(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
402 1.44 ozaki "no SA found, but required; level %u",
403 1.44 ozaki ipsec_get_reqlevel(isr));
404 1.1 jonathan isr = isr->next;
405 1.26 degroote /*
406 1.26 degroote * No more rules to apply, return NULL isr and no error
407 1.26 degroote * It can happen when the last rules are USE rules
408 1.26 degroote * */
409 1.1 jonathan if (isr == NULL) {
410 1.54 ozaki *ret = NULL;
411 1.26 degroote *error = 0;
412 1.1 jonathan return isr;
413 1.1 jonathan }
414 1.1 jonathan goto again;
415 1.1 jonathan }
416 1.1 jonathan
417 1.1 jonathan /*
418 1.1 jonathan * Check system global policy controls.
419 1.1 jonathan */
420 1.1 jonathan if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
421 1.1 jonathan (isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
422 1.1 jonathan (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
423 1.48 ozaki IPSECLOG(LOG_DEBUG, "IPsec outbound packet dropped due"
424 1.48 ozaki " to policy (check your sysctls)\n");
425 1.49 ozaki IPSEC_OSTAT(PDROPS);
426 1.1 jonathan *error = EHOSTUNREACH;
427 1.58 ozaki KEY_SA_UNREF(&sav);
428 1.1 jonathan goto bad;
429 1.1 jonathan }
430 1.1 jonathan
431 1.1 jonathan /*
432 1.1 jonathan * Sanity check the SA contents for the caller
433 1.1 jonathan * before they invoke the xform output method.
434 1.1 jonathan */
435 1.50 ozaki KASSERT(sav->tdb_xform != NULL);
436 1.54 ozaki *ret = sav;
437 1.1 jonathan return isr;
438 1.1 jonathan bad:
439 1.44 ozaki KASSERTMSG(*error != 0, "error return w/ no error code");
440 1.1 jonathan return NULL;
441 1.1 jonathan #undef IPSEC_OSTAT
442 1.1 jonathan }
443 1.1 jonathan
444 1.1 jonathan #ifdef INET
445 1.1 jonathan /*
446 1.1 jonathan * IPsec output logic for IPv4.
447 1.1 jonathan */
448 1.1 jonathan int
449 1.56 ozaki ipsec4_process_packet(struct mbuf *m, struct ipsecrequest *isr,
450 1.56 ozaki u_long *mtu)
451 1.1 jonathan {
452 1.54 ozaki struct secasvar *sav = NULL;
453 1.1 jonathan struct ip *ip;
454 1.1 jonathan int s, error, i, off;
455 1.46 ozaki union sockaddr_union *dst;
456 1.46 ozaki int setdf;
457 1.1 jonathan
458 1.44 ozaki KASSERT(m != NULL);
459 1.44 ozaki KASSERT(isr != NULL);
460 1.1 jonathan
461 1.1 jonathan s = splsoftnet(); /* insure SA contents don't change */
462 1.1 jonathan
463 1.54 ozaki isr = ipsec_nextisr(m, isr, AF_INET, &error, &sav);
464 1.26 degroote if (isr == NULL) {
465 1.26 degroote if (error != 0) {
466 1.26 degroote goto bad;
467 1.26 degroote } else {
468 1.26 degroote if (ipsec_register_done(m, &error) < 0)
469 1.26 degroote goto bad;
470 1.26 degroote
471 1.26 degroote splx(s);
472 1.26 degroote return ipsec_reinject_ipstack(m, AF_INET);
473 1.26 degroote }
474 1.26 degroote }
475 1.1 jonathan
476 1.54 ozaki KASSERT(sav != NULL);
477 1.56 ozaki /*
478 1.56 ozaki * Check if we need to handle NAT-T fragmentation.
479 1.56 ozaki */
480 1.56 ozaki if (isr == isr->sp->req) { /* Check only if called from ipsec4_output */
481 1.56 ozaki KASSERT(mtu != NULL);
482 1.56 ozaki ip = mtod(m, struct ip *);
483 1.56 ozaki if (!(sav->natt_type &
484 1.56 ozaki (UDP_ENCAP_ESPINUDP|UDP_ENCAP_ESPINUDP_NON_IKE))) {
485 1.56 ozaki goto noneed;
486 1.56 ozaki }
487 1.56 ozaki if (ntohs(ip->ip_len) <= sav->esp_frag)
488 1.56 ozaki goto noneed;
489 1.56 ozaki *mtu = sav->esp_frag;
490 1.58 ozaki KEY_SA_UNREF(&sav);
491 1.56 ozaki splx(s);
492 1.56 ozaki return 0;
493 1.56 ozaki }
494 1.56 ozaki noneed:
495 1.46 ozaki dst = &sav->sah->saidx.dst;
496 1.1 jonathan
497 1.46 ozaki /*
498 1.46 ozaki * Collect IP_DF state from the outer header.
499 1.46 ozaki */
500 1.46 ozaki if (dst->sa.sa_family == AF_INET) {
501 1.46 ozaki if (m->m_len < sizeof (struct ip) &&
502 1.46 ozaki (m = m_pullup(m, sizeof (struct ip))) == NULL) {
503 1.46 ozaki error = ENOBUFS;
504 1.54 ozaki goto unrefsav;
505 1.46 ozaki }
506 1.46 ozaki ip = mtod(m, struct ip *);
507 1.46 ozaki /* Honor system-wide control of how to handle IP_DF */
508 1.46 ozaki switch (ip4_ipsec_dfbit) {
509 1.46 ozaki case 0: /* clear in outer header */
510 1.46 ozaki case 1: /* set in outer header */
511 1.46 ozaki setdf = ip4_ipsec_dfbit;
512 1.46 ozaki break;
513 1.46 ozaki default: /* propagate to outer header */
514 1.46 ozaki setdf = ip->ip_off;
515 1.46 ozaki setdf = ntohs(setdf);
516 1.46 ozaki setdf = htons(setdf & IP_DF);
517 1.46 ozaki break;
518 1.1 jonathan }
519 1.46 ozaki } else {
520 1.46 ozaki ip = NULL; /* keep compiler happy */
521 1.46 ozaki setdf = 0;
522 1.46 ozaki }
523 1.46 ozaki /* Do the appropriate encapsulation, if necessary */
524 1.46 ozaki if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
525 1.46 ozaki dst->sa.sa_family != AF_INET || /* PF mismatch */
526 1.1 jonathan #if 0
527 1.46 ozaki (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
528 1.46 ozaki sav->tdb_xform->xf_type == XF_IP4 || /* ditto */
529 1.1 jonathan #endif
530 1.46 ozaki (dst->sa.sa_family == AF_INET && /* Proxy */
531 1.46 ozaki dst->sin.sin_addr.s_addr != INADDR_ANY &&
532 1.46 ozaki dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
533 1.46 ozaki struct mbuf *mp;
534 1.46 ozaki
535 1.46 ozaki /* Fix IPv4 header checksum and length */
536 1.46 ozaki if (m->m_len < sizeof (struct ip) &&
537 1.46 ozaki (m = m_pullup(m, sizeof (struct ip))) == NULL) {
538 1.46 ozaki error = ENOBUFS;
539 1.54 ozaki goto unrefsav;
540 1.46 ozaki }
541 1.46 ozaki ip = mtod(m, struct ip *);
542 1.46 ozaki ip->ip_len = htons(m->m_pkthdr.len);
543 1.46 ozaki ip->ip_sum = 0;
544 1.46 ozaki ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
545 1.1 jonathan
546 1.46 ozaki /* Encapsulate the packet */
547 1.54 ozaki error = ipip_output(m, isr, sav, &mp, 0, 0);
548 1.46 ozaki if (mp == NULL && !error) {
549 1.46 ozaki /* Should never happen. */
550 1.48 ozaki IPSECLOG(LOG_DEBUG,
551 1.48 ozaki "ipip_output returns no mbuf and no error!");
552 1.46 ozaki error = EFAULT;
553 1.46 ozaki }
554 1.46 ozaki if (error) {
555 1.46 ozaki if (mp) {
556 1.46 ozaki /* XXX: Should never happen! */
557 1.46 ozaki m_freem(mp);
558 1.46 ozaki }
559 1.46 ozaki m = NULL; /* ipip_output() already freed it */
560 1.54 ozaki goto unrefsav;
561 1.46 ozaki }
562 1.46 ozaki m = mp, mp = NULL;
563 1.46 ozaki /*
564 1.46 ozaki * ipip_output clears IP_DF in the new header. If
565 1.46 ozaki * we need to propagate IP_DF from the outer header,
566 1.46 ozaki * then we have to do it here.
567 1.46 ozaki *
568 1.46 ozaki * XXX shouldn't assume what ipip_output does.
569 1.46 ozaki */
570 1.46 ozaki if (dst->sa.sa_family == AF_INET && setdf) {
571 1.1 jonathan if (m->m_len < sizeof (struct ip) &&
572 1.1 jonathan (m = m_pullup(m, sizeof (struct ip))) == NULL) {
573 1.1 jonathan error = ENOBUFS;
574 1.54 ozaki goto unrefsav;
575 1.1 jonathan }
576 1.1 jonathan ip = mtod(m, struct ip *);
577 1.46 ozaki ip->ip_off |= htons(IP_DF);
578 1.1 jonathan }
579 1.1 jonathan }
580 1.1 jonathan
581 1.1 jonathan /*
582 1.1 jonathan * Dispatch to the appropriate IPsec transform logic. The
583 1.1 jonathan * packet will be returned for transmission after crypto
584 1.1 jonathan * processing, etc. are completed. For encapsulation we
585 1.1 jonathan * bypass this call because of the explicit call done above
586 1.1 jonathan * (necessary to deal with IP_DF handling for IPv4).
587 1.1 jonathan *
588 1.1 jonathan * NB: m & sav are ``passed to caller'' who's reponsible for
589 1.1 jonathan * for reclaiming their resources.
590 1.1 jonathan */
591 1.1 jonathan if (sav->tdb_xform->xf_type != XF_IP4) {
592 1.33 drochner if (dst->sa.sa_family == AF_INET) {
593 1.33 drochner ip = mtod(m, struct ip *);
594 1.33 drochner i = ip->ip_hl << 2;
595 1.33 drochner off = offsetof(struct ip, ip_p);
596 1.33 drochner } else {
597 1.33 drochner i = sizeof(struct ip6_hdr);
598 1.33 drochner off = offsetof(struct ip6_hdr, ip6_nxt);
599 1.33 drochner }
600 1.54 ozaki error = (*sav->tdb_xform->xf_output)(m, isr, sav, NULL, i, off);
601 1.1 jonathan } else {
602 1.54 ozaki error = ipsec_process_done(m, isr, sav);
603 1.1 jonathan }
604 1.58 ozaki KEY_SA_UNREF(&sav);
605 1.1 jonathan splx(s);
606 1.1 jonathan return error;
607 1.54 ozaki unrefsav:
608 1.58 ozaki KEY_SA_UNREF(&sav);
609 1.1 jonathan bad:
610 1.1 jonathan splx(s);
611 1.1 jonathan if (m)
612 1.1 jonathan m_freem(m);
613 1.1 jonathan return error;
614 1.1 jonathan }
615 1.1 jonathan #endif
616 1.1 jonathan
617 1.1 jonathan #ifdef INET6
618 1.38 drochner static void
619 1.38 drochner compute_ipsec_pos(struct mbuf *m, int *i, int *off)
620 1.38 drochner {
621 1.38 drochner int nxt;
622 1.38 drochner struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr*);
623 1.38 drochner struct ip6_ext ip6e;
624 1.38 drochner int dstopt = 0;
625 1.38 drochner
626 1.38 drochner *i = sizeof(struct ip6_hdr);
627 1.38 drochner *off = offsetof(struct ip6_hdr, ip6_nxt);
628 1.38 drochner nxt = ip6->ip6_nxt;
629 1.38 drochner
630 1.38 drochner /*
631 1.38 drochner * chase mbuf chain to find the appropriate place to
632 1.38 drochner * put AH/ESP/IPcomp header.
633 1.38 drochner * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
634 1.38 drochner */
635 1.38 drochner do {
636 1.38 drochner switch (nxt) {
637 1.38 drochner case IPPROTO_AH:
638 1.38 drochner case IPPROTO_ESP:
639 1.38 drochner case IPPROTO_IPCOMP:
640 1.38 drochner /*
641 1.38 drochner * we should not skip security header added
642 1.38 drochner * beforehand.
643 1.38 drochner */
644 1.38 drochner return;
645 1.38 drochner
646 1.38 drochner case IPPROTO_HOPOPTS:
647 1.38 drochner case IPPROTO_DSTOPTS:
648 1.38 drochner case IPPROTO_ROUTING:
649 1.38 drochner /*
650 1.38 drochner * if we see 2nd destination option header,
651 1.38 drochner * we should stop there.
652 1.38 drochner */
653 1.38 drochner if (nxt == IPPROTO_DSTOPTS && dstopt)
654 1.38 drochner return;
655 1.38 drochner
656 1.38 drochner if (nxt == IPPROTO_DSTOPTS) {
657 1.38 drochner /*
658 1.38 drochner * seen 1st or 2nd destination option.
659 1.38 drochner * next time we see one, it must be 2nd.
660 1.38 drochner */
661 1.38 drochner dstopt = 1;
662 1.38 drochner } else if (nxt == IPPROTO_ROUTING) {
663 1.38 drochner /*
664 1.38 drochner * if we see destionation option next
665 1.38 drochner * time, it must be dest2.
666 1.38 drochner */
667 1.38 drochner dstopt = 2;
668 1.38 drochner }
669 1.38 drochner
670 1.38 drochner /* skip this header */
671 1.38 drochner m_copydata(m, *i, sizeof(ip6e), &ip6e);
672 1.38 drochner nxt = ip6e.ip6e_nxt;
673 1.38 drochner *off = *i + offsetof(struct ip6_ext, ip6e_nxt);
674 1.38 drochner /*
675 1.38 drochner * we will never see nxt == IPPROTO_AH
676 1.38 drochner * so it is safe to omit AH case.
677 1.38 drochner */
678 1.38 drochner *i += (ip6e.ip6e_len + 1) << 3;
679 1.38 drochner break;
680 1.38 drochner default:
681 1.38 drochner return;
682 1.38 drochner }
683 1.38 drochner } while (*i < m->m_pkthdr.len);
684 1.38 drochner }
685 1.38 drochner
686 1.36 drochner static int
687 1.36 drochner in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia)
688 1.36 drochner {
689 1.36 drochner struct in6_addr ia2;
690 1.36 drochner
691 1.36 drochner memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
692 1.36 drochner if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr))
693 1.36 drochner ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
694 1.36 drochner
695 1.36 drochner return IN6_ARE_ADDR_EQUAL(ia, &ia2);
696 1.36 drochner }
697 1.36 drochner
698 1.1 jonathan int
699 1.21 degroote ipsec6_process_packet(
700 1.21 degroote struct mbuf *m,
701 1.21 degroote struct ipsecrequest *isr
702 1.21 degroote )
703 1.1 jonathan {
704 1.54 ozaki struct secasvar *sav = NULL;
705 1.21 degroote struct ip6_hdr *ip6;
706 1.35 drochner int s, error, i, off;
707 1.35 drochner union sockaddr_union *dst;
708 1.1 jonathan
709 1.44 ozaki KASSERT(m != NULL);
710 1.44 ozaki KASSERT(isr != NULL);
711 1.1 jonathan
712 1.21 degroote s = splsoftnet(); /* insure SA contents don't change */
713 1.34 drochner
714 1.54 ozaki isr = ipsec_nextisr(m, isr, AF_INET6, &error, &sav);
715 1.1 jonathan if (isr == NULL) {
716 1.26 degroote if (error != 0) {
717 1.34 drochner /* XXX Should we send a notification ? */
718 1.26 degroote goto bad;
719 1.26 degroote } else {
720 1.26 degroote if (ipsec_register_done(m, &error) < 0)
721 1.26 degroote goto bad;
722 1.26 degroote
723 1.26 degroote splx(s);
724 1.28 degroote return ipsec_reinject_ipstack(m, AF_INET6);
725 1.26 degroote }
726 1.1 jonathan }
727 1.1 jonathan
728 1.54 ozaki KASSERT(sav != NULL);
729 1.35 drochner dst = &sav->sah->saidx.dst;
730 1.1 jonathan
731 1.35 drochner ip6 = mtod(m, struct ip6_hdr *); /* XXX */
732 1.1 jonathan
733 1.35 drochner /* Do the appropriate encapsulation, if necessary */
734 1.35 drochner if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
735 1.35 drochner dst->sa.sa_family != AF_INET6 || /* PF mismatch */
736 1.35 drochner ((dst->sa.sa_family == AF_INET6) &&
737 1.35 drochner (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
738 1.36 drochner (!in6_sa_equal_addrwithscope(&dst->sin6,
739 1.35 drochner &ip6->ip6_dst)))) {
740 1.35 drochner struct mbuf *mp;
741 1.35 drochner
742 1.35 drochner /* Fix IPv6 header payload length. */
743 1.53 ozaki if (m->m_len < sizeof(struct ip6_hdr)) {
744 1.53 ozaki if ((m = m_pullup(m,sizeof(struct ip6_hdr))) == NULL) {
745 1.53 ozaki error = ENOBUFS;
746 1.54 ozaki goto unrefsav;
747 1.53 ozaki }
748 1.53 ozaki }
749 1.34 drochner
750 1.35 drochner if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
751 1.35 drochner /* No jumbogram support. */
752 1.53 ozaki error = ENXIO; /*XXX*/
753 1.54 ozaki goto unrefsav;
754 1.35 drochner }
755 1.34 drochner
756 1.35 drochner ip6 = mtod(m, struct ip6_hdr *);
757 1.35 drochner ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
758 1.1 jonathan
759 1.35 drochner /* Encapsulate the packet */
760 1.54 ozaki error = ipip_output(m, isr, sav, &mp, 0, 0);
761 1.35 drochner if (mp == NULL && !error) {
762 1.35 drochner /* Should never happen. */
763 1.48 ozaki IPSECLOG(LOG_DEBUG,
764 1.48 ozaki "ipip_output returns no mbuf and no error!");
765 1.35 drochner error = EFAULT;
766 1.35 drochner }
767 1.35 drochner
768 1.35 drochner if (error) {
769 1.35 drochner if (mp) {
770 1.35 drochner /* XXX: Should never happen! */
771 1.35 drochner m_freem(mp);
772 1.21 degroote }
773 1.35 drochner m = NULL; /* ipip_output() already freed it */
774 1.54 ozaki goto unrefsav;
775 1.35 drochner }
776 1.35 drochner
777 1.35 drochner m = mp;
778 1.35 drochner mp = NULL;
779 1.35 drochner }
780 1.1 jonathan
781 1.35 drochner if (dst->sa.sa_family == AF_INET) {
782 1.35 drochner struct ip *ip;
783 1.35 drochner ip = mtod(m, struct ip *);
784 1.35 drochner i = ip->ip_hl << 2;
785 1.35 drochner off = offsetof(struct ip, ip_p);
786 1.35 drochner } else {
787 1.38 drochner compute_ipsec_pos(m, &i, &off);
788 1.34 drochner }
789 1.54 ozaki error = (*sav->tdb_xform->xf_output)(m, isr, sav, NULL, i, off);
790 1.58 ozaki KEY_SA_UNREF(&sav);
791 1.34 drochner splx(s);
792 1.34 drochner return error;
793 1.54 ozaki unrefsav:
794 1.58 ozaki KEY_SA_UNREF(&sav);
795 1.1 jonathan bad:
796 1.21 degroote splx(s);
797 1.1 jonathan if (m)
798 1.1 jonathan m_freem(m);
799 1.1 jonathan return error;
800 1.1 jonathan }
801 1.1 jonathan #endif /*INET6*/
802 1.60 ozaki
803 1.60 ozaki void
804 1.60 ozaki ipsec_output_init(void)
805 1.60 ozaki {
806 1.60 ozaki
807 1.60 ozaki ipsec_rtcache_percpu = percpu_alloc(sizeof(struct route));
808 1.60 ozaki }
809