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