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