ipsec_output.c revision 1.30 1 1.30 drochner /* $NetBSD: ipsec_output.c,v 1.30 2011/02/10 20:24:27 drochner 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.30 drochner __KERNEL_RCSID(0, "$NetBSD: ipsec_output.c,v 1.30 2011/02/10 20:24:27 drochner 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 #ifdef IPSEC_NAT_T
76 1.22 degroote #include <netinet/udp.h>
77 1.22 degroote #endif
78 1.1 jonathan
79 1.1 jonathan #include <netipsec/ipsec.h>
80 1.13 jonathan #include <netipsec/ipsec_var.h>
81 1.27 thorpej #include <netipsec/ipsec_private.h>
82 1.1 jonathan #ifdef INET6
83 1.1 jonathan #include <netipsec/ipsec6.h>
84 1.1 jonathan #endif
85 1.1 jonathan #include <netipsec/ah_var.h>
86 1.1 jonathan #include <netipsec/esp_var.h>
87 1.1 jonathan #include <netipsec/ipcomp_var.h>
88 1.1 jonathan
89 1.1 jonathan #include <netipsec/xform.h>
90 1.1 jonathan
91 1.7 tls #include <netipsec/key.h>
92 1.7 tls #include <netipsec/keydb.h>
93 1.7 tls #include <netipsec/key_debug.h>
94 1.1 jonathan #include <netipsec/ipsec_osdep.h>
95 1.1 jonathan
96 1.10 jonathan #include <net/net_osdep.h> /* ovbcopy() in ipsec6_encapsulate() */
97 1.10 jonathan
98 1.25 degroote
99 1.25 degroote /*
100 1.25 degroote * Add a IPSEC_OUT_DONE tag to mark that we have finished the ipsec processing
101 1.25 degroote * It will be used by ip{,6}_output to check if we have already or not
102 1.25 degroote * processed this packet.
103 1.25 degroote */
104 1.25 degroote static int
105 1.25 degroote ipsec_register_done(struct mbuf *m, int * error)
106 1.25 degroote {
107 1.25 degroote struct m_tag *mtag;
108 1.25 degroote
109 1.25 degroote mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, 0, M_NOWAIT);
110 1.25 degroote if (mtag == NULL) {
111 1.25 degroote DPRINTF(("ipsec_register_done: could not get packet tag\n"));
112 1.25 degroote *error = ENOMEM;
113 1.25 degroote return -1;
114 1.25 degroote }
115 1.25 degroote
116 1.25 degroote m_tag_prepend(m, mtag);
117 1.25 degroote return 0;
118 1.25 degroote }
119 1.25 degroote
120 1.26 degroote static int
121 1.26 degroote ipsec_reinject_ipstack(struct mbuf *m, int af)
122 1.26 degroote {
123 1.26 degroote #ifdef INET
124 1.26 degroote struct ip * ip;
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.26 degroote ip = mtod(m, struct ip *);
134 1.26 degroote #ifdef __FreeBSD__
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.30 drochner rv = ip_output(m, NULL, NULL, IP_RAWOUTPUT,
141 1.26 degroote (struct ip_moptions *)NULL, (struct socket *)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 #ifdef IPSEC_NAT_T
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.22 degroote #endif /* IPSEC_NAT_T */
181 1.1 jonathan
182 1.1 jonathan IPSEC_SPLASSERT_SOFTNET("ipsec_process_done");
183 1.1 jonathan
184 1.1 jonathan IPSEC_ASSERT(m != NULL, ("ipsec_process_done: null mbuf"));
185 1.1 jonathan IPSEC_ASSERT(isr != NULL, ("ipsec_process_done: null ISR"));
186 1.1 jonathan sav = isr->sav;
187 1.1 jonathan IPSEC_ASSERT(sav != NULL, ("ipsec_process_done: null SA"));
188 1.1 jonathan IPSEC_ASSERT(sav->sah != NULL, ("ipsec_process_done: null SAH"));
189 1.1 jonathan
190 1.1 jonathan saidx = &sav->sah->saidx;
191 1.22 degroote
192 1.22 degroote #ifdef IPSEC_NAT_T
193 1.22 degroote if(sav->natt_type != 0) {
194 1.22 degroote ip = mtod(m, struct ip *);
195 1.22 degroote
196 1.22 degroote hlen = sizeof(struct udphdr);
197 1.22 degroote if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
198 1.22 degroote hlen += sizeof(uint64_t);
199 1.22 degroote
200 1.22 degroote mo = m_makespace(m, sizeof(struct ip), hlen, &roff);
201 1.22 degroote if (mo == NULL) {
202 1.22 degroote DPRINTF(("ipsec_process_done : failed to inject"
203 1.22 degroote "%u byte UDP for SA %s/%08lx\n",
204 1.22 degroote hlen, ipsec_address(&saidx->dst),
205 1.22 degroote (u_long) ntohl(sav->spi)));
206 1.22 degroote error = ENOBUFS;
207 1.22 degroote goto bad;
208 1.22 degroote }
209 1.22 degroote
210 1.22 degroote udp = (struct udphdr*) (mtod(mo, char*) + roff);
211 1.22 degroote data = (uint64_t*) (udp + 1);
212 1.22 degroote
213 1.22 degroote if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
214 1.22 degroote *data = 0; /* NON-IKE Marker */
215 1.22 degroote
216 1.22 degroote if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
217 1.22 degroote udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
218 1.22 degroote else
219 1.22 degroote udp->uh_sport = key_portfromsaddr(&saidx->src);
220 1.22 degroote
221 1.22 degroote udp->uh_dport = key_portfromsaddr(&saidx->dst);
222 1.22 degroote udp->uh_sum = 0;
223 1.29 dyoung udp->uh_ulen = htons(m->m_pkthdr.len - (ip->ip_hl << 2));
224 1.22 degroote }
225 1.22 degroote #endif /* IPSEC_NAT_T */
226 1.22 degroote
227 1.1 jonathan switch (saidx->dst.sa.sa_family) {
228 1.1 jonathan #ifdef INET
229 1.1 jonathan case AF_INET:
230 1.1 jonathan /* Fix the header length, for AH processing. */
231 1.22 degroote ip = mtod(m, struct ip *);
232 1.22 degroote ip->ip_len = htons(m->m_pkthdr.len);
233 1.22 degroote #ifdef IPSEC_NAT_T
234 1.22 degroote if (sav->natt_type != 0)
235 1.22 degroote ip->ip_p = IPPROTO_UDP;
236 1.22 degroote #endif /* IPSEC_NAT_T */
237 1.1 jonathan break;
238 1.1 jonathan #endif /* INET */
239 1.1 jonathan #ifdef INET6
240 1.1 jonathan case AF_INET6:
241 1.1 jonathan /* Fix the header length, for AH processing. */
242 1.1 jonathan if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
243 1.1 jonathan error = ENXIO;
244 1.1 jonathan goto bad;
245 1.1 jonathan }
246 1.1 jonathan if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
247 1.1 jonathan /* No jumbogram support. */
248 1.1 jonathan error = ENXIO; /*?*/
249 1.1 jonathan goto bad;
250 1.1 jonathan }
251 1.22 degroote ip6 = mtod(m, struct ip6_hdr *);
252 1.22 degroote ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
253 1.22 degroote #ifdef IPSEC_NAT_T
254 1.22 degroote if (sav->natt_type != 0)
255 1.22 degroote ip6->ip6_nxt = IPPROTO_UDP;
256 1.22 degroote #endif /* IPSEC_NAT_T */
257 1.1 jonathan break;
258 1.1 jonathan #endif /* INET6 */
259 1.1 jonathan default:
260 1.1 jonathan DPRINTF(("ipsec_process_done: unknown protocol family %u\n",
261 1.1 jonathan saidx->dst.sa.sa_family));
262 1.1 jonathan error = ENXIO;
263 1.1 jonathan goto bad;
264 1.1 jonathan }
265 1.1 jonathan
266 1.1 jonathan /*
267 1.1 jonathan * If there's another (bundled) SA to apply, do so.
268 1.1 jonathan * Note that this puts a burden on the kernel stack size.
269 1.1 jonathan * If this is a problem we'll need to introduce a queue
270 1.1 jonathan * to set the packet on so we can unwind the stack before
271 1.1 jonathan * doing further processing.
272 1.1 jonathan */
273 1.1 jonathan if (isr->next) {
274 1.27 thorpej IPSEC_STATINC(IPSEC_STAT_OUT_BUNDLESA);
275 1.29 dyoung switch ( saidx->dst.sa.sa_family ) {
276 1.21 degroote #ifdef INET
277 1.29 dyoung case AF_INET:
278 1.21 degroote return ipsec4_process_packet(m, isr->next, 0,0);
279 1.21 degroote #endif /* INET */
280 1.21 degroote #ifdef INET6
281 1.21 degroote case AF_INET6:
282 1.29 dyoung return ipsec6_process_packet(m,isr->next);
283 1.21 degroote #endif /* INET6 */
284 1.21 degroote default :
285 1.21 degroote DPRINTF(("ipsec_process_done: unknown protocol family %u\n",
286 1.29 dyoung saidx->dst.sa.sa_family));
287 1.21 degroote error = ENXIO;
288 1.21 degroote goto bad;
289 1.29 dyoung }
290 1.1 jonathan }
291 1.1 jonathan
292 1.1 jonathan /*
293 1.25 degroote * We're done with IPsec processing,
294 1.25 degroote * mark that we have already processed the packet
295 1.25 degroote * transmit it packet using the appropriate network protocol (IP or IPv6).
296 1.1 jonathan */
297 1.25 degroote
298 1.25 degroote if (ipsec_register_done(m, &error) < 0)
299 1.25 degroote goto bad;
300 1.25 degroote
301 1.26 degroote return ipsec_reinject_ipstack(m, saidx->dst.sa.sa_family);
302 1.1 jonathan bad:
303 1.1 jonathan m_freem(m);
304 1.1 jonathan KEY_FREESAV(&sav);
305 1.1 jonathan return (error);
306 1.1 jonathan }
307 1.1 jonathan
308 1.26 degroote /*
309 1.26 degroote * ipsec_nextisr can return :
310 1.26 degroote * - isr == NULL and error != 0 => something is bad : the packet must be
311 1.26 degroote * discarded
312 1.26 degroote * - isr == NULL and error == 0 => no more rules to apply, ipsec processing
313 1.26 degroote * is done, reinject it in ip stack
314 1.26 degroote * - isr != NULL (error == 0) => we need to apply one rule to the packet
315 1.26 degroote */
316 1.1 jonathan static struct ipsecrequest *
317 1.1 jonathan ipsec_nextisr(
318 1.1 jonathan struct mbuf *m,
319 1.1 jonathan struct ipsecrequest *isr,
320 1.1 jonathan int af,
321 1.1 jonathan struct secasindex *saidx,
322 1.1 jonathan int *error
323 1.1 jonathan )
324 1.1 jonathan {
325 1.27 thorpej #define IPSEC_OSTAT(x, y, z) \
326 1.27 thorpej do { \
327 1.27 thorpej switch (isr->saidx.proto) { \
328 1.27 thorpej case IPPROTO_ESP: \
329 1.27 thorpej ESP_STATINC(x); \
330 1.27 thorpej break; \
331 1.27 thorpej case IPPROTO_AH: \
332 1.27 thorpej AH_STATINC(y); \
333 1.27 thorpej break; \
334 1.27 thorpej default: \
335 1.27 thorpej IPCOMP_STATINC(z); \
336 1.27 thorpej break; \
337 1.27 thorpej } \
338 1.27 thorpej } while (/*CONSTCOND*/0)
339 1.27 thorpej
340 1.1 jonathan struct secasvar *sav;
341 1.1 jonathan
342 1.1 jonathan IPSEC_SPLASSERT_SOFTNET("ipsec_nextisr");
343 1.1 jonathan IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
344 1.1 jonathan ("ipsec_nextisr: invalid address family %u", af));
345 1.1 jonathan again:
346 1.1 jonathan /*
347 1.1 jonathan * Craft SA index to search for proper SA. Note that
348 1.1 jonathan * we only fillin unspecified SA peers for transport
349 1.1 jonathan * mode; for tunnel mode they must already be filled in.
350 1.1 jonathan */
351 1.1 jonathan *saidx = isr->saidx;
352 1.1 jonathan if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
353 1.1 jonathan /* Fillin unspecified SA peers only for transport mode */
354 1.1 jonathan if (af == AF_INET) {
355 1.1 jonathan struct sockaddr_in *sin;
356 1.1 jonathan struct ip *ip = mtod(m, struct ip *);
357 1.1 jonathan
358 1.1 jonathan if (saidx->src.sa.sa_len == 0) {
359 1.1 jonathan sin = &saidx->src.sin;
360 1.1 jonathan sin->sin_len = sizeof(*sin);
361 1.1 jonathan sin->sin_family = AF_INET;
362 1.1 jonathan sin->sin_port = IPSEC_PORT_ANY;
363 1.1 jonathan sin->sin_addr = ip->ip_src;
364 1.1 jonathan }
365 1.1 jonathan if (saidx->dst.sa.sa_len == 0) {
366 1.1 jonathan sin = &saidx->dst.sin;
367 1.1 jonathan sin->sin_len = sizeof(*sin);
368 1.1 jonathan sin->sin_family = AF_INET;
369 1.1 jonathan sin->sin_port = IPSEC_PORT_ANY;
370 1.1 jonathan sin->sin_addr = ip->ip_dst;
371 1.1 jonathan }
372 1.1 jonathan } else {
373 1.1 jonathan struct sockaddr_in6 *sin6;
374 1.1 jonathan struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
375 1.1 jonathan
376 1.1 jonathan if (saidx->src.sin6.sin6_len == 0) {
377 1.1 jonathan sin6 = (struct sockaddr_in6 *)&saidx->src;
378 1.1 jonathan sin6->sin6_len = sizeof(*sin6);
379 1.1 jonathan sin6->sin6_family = AF_INET6;
380 1.1 jonathan sin6->sin6_port = IPSEC_PORT_ANY;
381 1.1 jonathan sin6->sin6_addr = ip6->ip6_src;
382 1.1 jonathan if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
383 1.1 jonathan /* fix scope id for comparing SPD */
384 1.1 jonathan sin6->sin6_addr.s6_addr16[1] = 0;
385 1.1 jonathan sin6->sin6_scope_id =
386 1.1 jonathan ntohs(ip6->ip6_src.s6_addr16[1]);
387 1.1 jonathan }
388 1.1 jonathan }
389 1.1 jonathan if (saidx->dst.sin6.sin6_len == 0) {
390 1.1 jonathan sin6 = (struct sockaddr_in6 *)&saidx->dst;
391 1.1 jonathan sin6->sin6_len = sizeof(*sin6);
392 1.1 jonathan sin6->sin6_family = AF_INET6;
393 1.1 jonathan sin6->sin6_port = IPSEC_PORT_ANY;
394 1.1 jonathan sin6->sin6_addr = ip6->ip6_dst;
395 1.1 jonathan if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
396 1.1 jonathan /* fix scope id for comparing SPD */
397 1.1 jonathan sin6->sin6_addr.s6_addr16[1] = 0;
398 1.1 jonathan sin6->sin6_scope_id =
399 1.1 jonathan ntohs(ip6->ip6_dst.s6_addr16[1]);
400 1.1 jonathan }
401 1.1 jonathan }
402 1.1 jonathan }
403 1.1 jonathan }
404 1.1 jonathan
405 1.1 jonathan /*
406 1.1 jonathan * Lookup SA and validate it.
407 1.1 jonathan */
408 1.1 jonathan *error = key_checkrequest(isr, saidx);
409 1.1 jonathan if (*error != 0) {
410 1.1 jonathan /*
411 1.1 jonathan * IPsec processing is required, but no SA found.
412 1.1 jonathan * I assume that key_acquire() had been called
413 1.1 jonathan * to get/establish the SA. Here I discard
414 1.1 jonathan * this packet because it is responsibility for
415 1.1 jonathan * upper layer to retransmit the packet.
416 1.1 jonathan */
417 1.27 thorpej IPSEC_STATINC(IPSEC_STAT_OUT_NOSA);
418 1.1 jonathan goto bad;
419 1.1 jonathan }
420 1.1 jonathan sav = isr->sav;
421 1.26 degroote /* sav may be NULL here if we have an USE rule */
422 1.26 degroote if (sav == NULL) {
423 1.1 jonathan IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
424 1.1 jonathan ("ipsec_nextisr: no SA found, but required; level %u",
425 1.1 jonathan ipsec_get_reqlevel(isr)));
426 1.1 jonathan isr = isr->next;
427 1.26 degroote /*
428 1.26 degroote * No more rules to apply, return NULL isr and no error
429 1.26 degroote * It can happen when the last rules are USE rules
430 1.26 degroote * */
431 1.1 jonathan if (isr == NULL) {
432 1.26 degroote *error = 0;
433 1.1 jonathan return isr;
434 1.1 jonathan }
435 1.1 jonathan goto again;
436 1.1 jonathan }
437 1.1 jonathan
438 1.1 jonathan /*
439 1.1 jonathan * Check system global policy controls.
440 1.1 jonathan */
441 1.1 jonathan if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
442 1.1 jonathan (isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
443 1.1 jonathan (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
444 1.1 jonathan DPRINTF(("ipsec_nextisr: IPsec outbound packet dropped due"
445 1.1 jonathan " to policy (check your sysctls)\n"));
446 1.27 thorpej IPSEC_OSTAT(ESP_STAT_PDROPS, AH_STAT_PDROPS,
447 1.27 thorpej IPCOMP_STAT_PDROPS);
448 1.1 jonathan *error = EHOSTUNREACH;
449 1.1 jonathan goto bad;
450 1.1 jonathan }
451 1.1 jonathan
452 1.1 jonathan /*
453 1.1 jonathan * Sanity check the SA contents for the caller
454 1.1 jonathan * before they invoke the xform output method.
455 1.1 jonathan */
456 1.1 jonathan if (sav->tdb_xform == NULL) {
457 1.1 jonathan DPRINTF(("ipsec_nextisr: no transform for SA\n"));
458 1.27 thorpej IPSEC_OSTAT(ESP_STAT_NOXFORM, AH_STAT_NOXFORM,
459 1.27 thorpej IPCOMP_STAT_NOXFORM);
460 1.1 jonathan *error = EHOSTUNREACH;
461 1.1 jonathan goto bad;
462 1.1 jonathan }
463 1.1 jonathan return isr;
464 1.1 jonathan bad:
465 1.1 jonathan IPSEC_ASSERT(*error != 0, ("ipsec_nextisr: error return w/ no error code"));
466 1.1 jonathan return NULL;
467 1.1 jonathan #undef IPSEC_OSTAT
468 1.1 jonathan }
469 1.1 jonathan
470 1.1 jonathan #ifdef INET
471 1.1 jonathan /*
472 1.1 jonathan * IPsec output logic for IPv4.
473 1.1 jonathan */
474 1.1 jonathan int
475 1.1 jonathan ipsec4_process_packet(
476 1.15 christos struct mbuf *m,
477 1.15 christos struct ipsecrequest *isr,
478 1.16 christos int flags,
479 1.15 christos int tunalready
480 1.15 christos )
481 1.1 jonathan {
482 1.1 jonathan struct secasindex saidx;
483 1.1 jonathan struct secasvar *sav;
484 1.1 jonathan struct ip *ip;
485 1.1 jonathan int s, error, i, off;
486 1.1 jonathan
487 1.1 jonathan IPSEC_ASSERT(m != NULL, ("ipsec4_process_packet: null mbuf"));
488 1.1 jonathan IPSEC_ASSERT(isr != NULL, ("ipsec4_process_packet: null isr"));
489 1.1 jonathan
490 1.1 jonathan s = splsoftnet(); /* insure SA contents don't change */
491 1.1 jonathan
492 1.1 jonathan isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
493 1.26 degroote if (isr == NULL) {
494 1.26 degroote if (error != 0) {
495 1.26 degroote goto bad;
496 1.26 degroote } else {
497 1.26 degroote if (ipsec_register_done(m, &error) < 0)
498 1.26 degroote goto bad;
499 1.26 degroote
500 1.26 degroote splx(s);
501 1.26 degroote return ipsec_reinject_ipstack(m, AF_INET);
502 1.26 degroote }
503 1.26 degroote }
504 1.1 jonathan
505 1.1 jonathan sav = isr->sav;
506 1.1 jonathan if (!tunalready) {
507 1.1 jonathan union sockaddr_union *dst = &sav->sah->saidx.dst;
508 1.1 jonathan int setdf;
509 1.1 jonathan
510 1.1 jonathan /*
511 1.1 jonathan * Collect IP_DF state from the outer header.
512 1.1 jonathan */
513 1.1 jonathan if (dst->sa.sa_family == AF_INET) {
514 1.1 jonathan if (m->m_len < sizeof (struct ip) &&
515 1.1 jonathan (m = m_pullup(m, sizeof (struct ip))) == NULL) {
516 1.1 jonathan error = ENOBUFS;
517 1.1 jonathan goto bad;
518 1.1 jonathan }
519 1.1 jonathan ip = mtod(m, struct ip *);
520 1.1 jonathan /* Honor system-wide control of how to handle IP_DF */
521 1.1 jonathan switch (ip4_ipsec_dfbit) {
522 1.1 jonathan case 0: /* clear in outer header */
523 1.1 jonathan case 1: /* set in outer header */
524 1.1 jonathan setdf = ip4_ipsec_dfbit;
525 1.1 jonathan break;
526 1.1 jonathan default: /* propagate to outer header */
527 1.3 jonathan setdf = ip->ip_off;
528 1.3 jonathan #ifndef __FreeBSD__
529 1.3 jonathan /* On FreeBSD, ip_off and ip_len assumed in host endian. */
530 1.3 jonathan setdf = ntohs(setdf);
531 1.3 jonathan #endif
532 1.3 jonathan setdf = htons(setdf & IP_DF);
533 1.1 jonathan break;
534 1.1 jonathan }
535 1.1 jonathan } else {
536 1.1 jonathan ip = NULL; /* keep compiler happy */
537 1.1 jonathan setdf = 0;
538 1.1 jonathan }
539 1.1 jonathan /* Do the appropriate encapsulation, if necessary */
540 1.1 jonathan if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
541 1.1 jonathan dst->sa.sa_family != AF_INET || /* PF mismatch */
542 1.1 jonathan #if 0
543 1.1 jonathan (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
544 1.1 jonathan sav->tdb_xform->xf_type == XF_IP4 || /* ditto */
545 1.1 jonathan #endif
546 1.1 jonathan (dst->sa.sa_family == AF_INET && /* Proxy */
547 1.1 jonathan dst->sin.sin_addr.s_addr != INADDR_ANY &&
548 1.1 jonathan dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
549 1.1 jonathan struct mbuf *mp;
550 1.1 jonathan
551 1.1 jonathan /* Fix IPv4 header checksum and length */
552 1.1 jonathan if (m->m_len < sizeof (struct ip) &&
553 1.1 jonathan (m = m_pullup(m, sizeof (struct ip))) == NULL) {
554 1.1 jonathan error = ENOBUFS;
555 1.1 jonathan goto bad;
556 1.1 jonathan }
557 1.1 jonathan ip = mtod(m, struct ip *);
558 1.1 jonathan ip->ip_len = htons(m->m_pkthdr.len);
559 1.1 jonathan ip->ip_sum = 0;
560 1.1 jonathan ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
561 1.1 jonathan
562 1.1 jonathan /* Encapsulate the packet */
563 1.1 jonathan error = ipip_output(m, isr, &mp, 0, 0);
564 1.1 jonathan if (mp == NULL && !error) {
565 1.1 jonathan /* Should never happen. */
566 1.1 jonathan DPRINTF(("ipsec4_process_packet: ipip_output "
567 1.1 jonathan "returns no mbuf and no error!"));
568 1.1 jonathan error = EFAULT;
569 1.1 jonathan }
570 1.1 jonathan if (error) {
571 1.8 scw if (mp) {
572 1.8 scw /* XXX: Should never happen! */
573 1.1 jonathan m_freem(mp);
574 1.8 scw }
575 1.8 scw m = NULL; /* ipip_output() already freed it */
576 1.1 jonathan goto bad;
577 1.1 jonathan }
578 1.1 jonathan m = mp, mp = NULL;
579 1.1 jonathan /*
580 1.1 jonathan * ipip_output clears IP_DF in the new header. If
581 1.1 jonathan * we need to propagate IP_DF from the outer header,
582 1.1 jonathan * then we have to do it here.
583 1.1 jonathan *
584 1.1 jonathan * XXX shouldn't assume what ipip_output does.
585 1.1 jonathan */
586 1.1 jonathan if (dst->sa.sa_family == AF_INET && setdf) {
587 1.1 jonathan if (m->m_len < sizeof (struct ip) &&
588 1.1 jonathan (m = m_pullup(m, sizeof (struct ip))) == NULL) {
589 1.1 jonathan error = ENOBUFS;
590 1.1 jonathan goto bad;
591 1.1 jonathan }
592 1.1 jonathan ip = mtod(m, struct ip *);
593 1.23 adrianp ip->ip_off |= IP_OFF_CONVERT(IP_DF);
594 1.1 jonathan }
595 1.1 jonathan }
596 1.1 jonathan }
597 1.1 jonathan
598 1.1 jonathan /*
599 1.1 jonathan * Dispatch to the appropriate IPsec transform logic. The
600 1.1 jonathan * packet will be returned for transmission after crypto
601 1.1 jonathan * processing, etc. are completed. For encapsulation we
602 1.1 jonathan * bypass this call because of the explicit call done above
603 1.1 jonathan * (necessary to deal with IP_DF handling for IPv4).
604 1.1 jonathan *
605 1.1 jonathan * NB: m & sav are ``passed to caller'' who's reponsible for
606 1.1 jonathan * for reclaiming their resources.
607 1.1 jonathan */
608 1.1 jonathan if (sav->tdb_xform->xf_type != XF_IP4) {
609 1.1 jonathan ip = mtod(m, struct ip *);
610 1.1 jonathan i = ip->ip_hl << 2;
611 1.1 jonathan off = offsetof(struct ip, ip_p);
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.1 jonathan int
628 1.21 degroote ipsec6_process_packet(
629 1.21 degroote struct mbuf *m,
630 1.21 degroote struct ipsecrequest *isr
631 1.21 degroote )
632 1.1 jonathan {
633 1.1 jonathan struct secasindex saidx;
634 1.21 degroote struct secasvar *sav;
635 1.21 degroote struct ip6_hdr *ip6;
636 1.21 degroote int s, error, i, off;
637 1.1 jonathan
638 1.21 degroote IPSEC_ASSERT(m != NULL, ("ipsec6_process_packet: null mbuf"));
639 1.21 degroote IPSEC_ASSERT(isr != NULL, ("ipsec6_process_packet: null isr"));
640 1.1 jonathan
641 1.21 degroote s = splsoftnet(); /* insure SA contents don't change */
642 1.1 jonathan isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
643 1.1 jonathan if (isr == NULL) {
644 1.26 degroote if (error != 0) {
645 1.26 degroote // XXX Should we send a notification ?
646 1.26 degroote goto bad;
647 1.26 degroote } else {
648 1.26 degroote if (ipsec_register_done(m, &error) < 0)
649 1.26 degroote goto bad;
650 1.26 degroote
651 1.26 degroote splx(s);
652 1.28 degroote return ipsec_reinject_ipstack(m, AF_INET6);
653 1.26 degroote }
654 1.1 jonathan }
655 1.1 jonathan
656 1.21 degroote sav = isr->sav;
657 1.21 degroote if (sav->tdb_xform->xf_type != XF_IP4) {
658 1.21 degroote i = sizeof(struct ip6_hdr);
659 1.21 degroote off = offsetof(struct ip6_hdr, ip6_nxt);
660 1.21 degroote error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
661 1.21 degroote } else {
662 1.21 degroote union sockaddr_union *dst = &sav->sah->saidx.dst;
663 1.1 jonathan
664 1.21 degroote ip6 = mtod(m, struct ip6_hdr *);
665 1.1 jonathan
666 1.21 degroote /* Do the appropriate encapsulation, if necessary */
667 1.21 degroote if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
668 1.21 degroote dst->sa.sa_family != AF_INET6 || /* PF mismatch */
669 1.21 degroote ((dst->sa.sa_family == AF_INET6) &&
670 1.21 degroote (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
671 1.21 degroote (!IN6_ARE_ADDR_EQUAL(&dst->sin6.sin6_addr,
672 1.21 degroote &ip6->ip6_dst)))
673 1.21 degroote )
674 1.21 degroote {
675 1.21 degroote struct mbuf *mp;
676 1.21 degroote /* Fix IPv6 header payload length. */
677 1.21 degroote if (m->m_len < sizeof(struct ip6_hdr))
678 1.21 degroote if ((m = m_pullup(m,sizeof(struct ip6_hdr))) == NULL)
679 1.21 degroote return ENOBUFS;
680 1.21 degroote
681 1.21 degroote if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
682 1.21 degroote /* No jumbogram support. */
683 1.21 degroote m_freem(m);
684 1.21 degroote return ENXIO; /*XXX*/
685 1.21 degroote }
686 1.21 degroote ip6 = mtod(m, struct ip6_hdr *);
687 1.21 degroote ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
688 1.1 jonathan
689 1.21 degroote /* Encapsulate the packet */
690 1.21 degroote error = ipip_output(m, isr, &mp, 0, 0);
691 1.21 degroote if (mp == NULL && !error) {
692 1.21 degroote /* Should never happen. */
693 1.21 degroote DPRINTF(("ipsec6_process_packet: ipip_output "
694 1.21 degroote "returns no mbuf and no error!"));
695 1.21 degroote error = EFAULT;
696 1.21 degroote }
697 1.1 jonathan
698 1.21 degroote if (error) {
699 1.21 degroote if (mp) {
700 1.21 degroote /* XXX: Should never happen! */
701 1.21 degroote m_freem(mp);
702 1.21 degroote }
703 1.21 degroote m = NULL; /* ipip_output() already freed it */
704 1.19 joerg goto bad;
705 1.19 joerg }
706 1.21 degroote
707 1.21 degroote m = mp;
708 1.21 degroote mp = NULL;
709 1.1 jonathan }
710 1.21 degroote
711 1.21 degroote error = ipsec_process_done(m,isr);
712 1.1 jonathan }
713 1.21 degroote splx(s);
714 1.21 degroote return error;
715 1.1 jonathan bad:
716 1.21 degroote splx(s);
717 1.1 jonathan if (m)
718 1.1 jonathan m_freem(m);
719 1.1 jonathan return error;
720 1.1 jonathan }
721 1.1 jonathan #endif /*INET6*/
722