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