ipsec_output.c revision 1.33 1 1.33 drochner /* $NetBSD: ipsec_output.c,v 1.33 2011/06/06 16:48:35 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.33 drochner __KERNEL_RCSID(0, "$NetBSD: ipsec_output.c,v 1.33 2011/06/06 16:48:35 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.31 drochner rv = ip_output(m, NULL, NULL, IP_RAWOUTPUT|IP_NOIPNEWID,
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.32 drochner key_sa_recordxfer(sav, m);
267 1.32 drochner
268 1.1 jonathan /*
269 1.1 jonathan * If there's another (bundled) SA to apply, do so.
270 1.1 jonathan * Note that this puts a burden on the kernel stack size.
271 1.1 jonathan * If this is a problem we'll need to introduce a queue
272 1.1 jonathan * to set the packet on so we can unwind the stack before
273 1.1 jonathan * doing further processing.
274 1.1 jonathan */
275 1.1 jonathan if (isr->next) {
276 1.27 thorpej IPSEC_STATINC(IPSEC_STAT_OUT_BUNDLESA);
277 1.29 dyoung switch ( saidx->dst.sa.sa_family ) {
278 1.21 degroote #ifdef INET
279 1.29 dyoung case AF_INET:
280 1.21 degroote return ipsec4_process_packet(m, isr->next, 0,0);
281 1.21 degroote #endif /* INET */
282 1.21 degroote #ifdef INET6
283 1.21 degroote case AF_INET6:
284 1.29 dyoung return ipsec6_process_packet(m,isr->next);
285 1.21 degroote #endif /* INET6 */
286 1.21 degroote default :
287 1.21 degroote DPRINTF(("ipsec_process_done: unknown protocol family %u\n",
288 1.29 dyoung saidx->dst.sa.sa_family));
289 1.21 degroote error = ENXIO;
290 1.21 degroote goto bad;
291 1.29 dyoung }
292 1.1 jonathan }
293 1.1 jonathan
294 1.1 jonathan /*
295 1.25 degroote * We're done with IPsec processing,
296 1.25 degroote * mark that we have already processed the packet
297 1.25 degroote * transmit it packet using the appropriate network protocol (IP or IPv6).
298 1.1 jonathan */
299 1.25 degroote
300 1.25 degroote if (ipsec_register_done(m, &error) < 0)
301 1.25 degroote goto bad;
302 1.25 degroote
303 1.26 degroote return ipsec_reinject_ipstack(m, saidx->dst.sa.sa_family);
304 1.1 jonathan bad:
305 1.1 jonathan m_freem(m);
306 1.1 jonathan KEY_FREESAV(&sav);
307 1.1 jonathan return (error);
308 1.1 jonathan }
309 1.1 jonathan
310 1.26 degroote /*
311 1.26 degroote * ipsec_nextisr can return :
312 1.26 degroote * - isr == NULL and error != 0 => something is bad : the packet must be
313 1.26 degroote * discarded
314 1.26 degroote * - isr == NULL and error == 0 => no more rules to apply, ipsec processing
315 1.26 degroote * is done, reinject it in ip stack
316 1.26 degroote * - isr != NULL (error == 0) => we need to apply one rule to the packet
317 1.26 degroote */
318 1.1 jonathan static struct ipsecrequest *
319 1.1 jonathan ipsec_nextisr(
320 1.1 jonathan struct mbuf *m,
321 1.1 jonathan struct ipsecrequest *isr,
322 1.1 jonathan int af,
323 1.1 jonathan struct secasindex *saidx,
324 1.1 jonathan int *error
325 1.1 jonathan )
326 1.1 jonathan {
327 1.27 thorpej #define IPSEC_OSTAT(x, y, z) \
328 1.27 thorpej do { \
329 1.27 thorpej switch (isr->saidx.proto) { \
330 1.27 thorpej case IPPROTO_ESP: \
331 1.27 thorpej ESP_STATINC(x); \
332 1.27 thorpej break; \
333 1.27 thorpej case IPPROTO_AH: \
334 1.27 thorpej AH_STATINC(y); \
335 1.27 thorpej break; \
336 1.27 thorpej default: \
337 1.27 thorpej IPCOMP_STATINC(z); \
338 1.27 thorpej break; \
339 1.27 thorpej } \
340 1.27 thorpej } while (/*CONSTCOND*/0)
341 1.27 thorpej
342 1.1 jonathan struct secasvar *sav;
343 1.1 jonathan
344 1.1 jonathan IPSEC_SPLASSERT_SOFTNET("ipsec_nextisr");
345 1.1 jonathan IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
346 1.1 jonathan ("ipsec_nextisr: invalid address family %u", af));
347 1.1 jonathan again:
348 1.1 jonathan /*
349 1.1 jonathan * Craft SA index to search for proper SA. Note that
350 1.1 jonathan * we only fillin unspecified SA peers for transport
351 1.1 jonathan * mode; for tunnel mode they must already be filled in.
352 1.1 jonathan */
353 1.1 jonathan *saidx = isr->saidx;
354 1.1 jonathan if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
355 1.1 jonathan /* Fillin unspecified SA peers only for transport mode */
356 1.1 jonathan if (af == AF_INET) {
357 1.1 jonathan struct sockaddr_in *sin;
358 1.1 jonathan struct ip *ip = mtod(m, struct ip *);
359 1.1 jonathan
360 1.1 jonathan if (saidx->src.sa.sa_len == 0) {
361 1.1 jonathan sin = &saidx->src.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_src;
366 1.1 jonathan }
367 1.1 jonathan if (saidx->dst.sa.sa_len == 0) {
368 1.1 jonathan sin = &saidx->dst.sin;
369 1.1 jonathan sin->sin_len = sizeof(*sin);
370 1.1 jonathan sin->sin_family = AF_INET;
371 1.1 jonathan sin->sin_port = IPSEC_PORT_ANY;
372 1.1 jonathan sin->sin_addr = ip->ip_dst;
373 1.1 jonathan }
374 1.1 jonathan } else {
375 1.1 jonathan struct sockaddr_in6 *sin6;
376 1.1 jonathan struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
377 1.1 jonathan
378 1.1 jonathan if (saidx->src.sin6.sin6_len == 0) {
379 1.1 jonathan sin6 = (struct sockaddr_in6 *)&saidx->src;
380 1.1 jonathan sin6->sin6_len = sizeof(*sin6);
381 1.1 jonathan sin6->sin6_family = AF_INET6;
382 1.1 jonathan sin6->sin6_port = IPSEC_PORT_ANY;
383 1.1 jonathan sin6->sin6_addr = ip6->ip6_src;
384 1.1 jonathan if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
385 1.1 jonathan /* fix scope id for comparing SPD */
386 1.1 jonathan sin6->sin6_addr.s6_addr16[1] = 0;
387 1.1 jonathan sin6->sin6_scope_id =
388 1.1 jonathan ntohs(ip6->ip6_src.s6_addr16[1]);
389 1.1 jonathan }
390 1.1 jonathan }
391 1.1 jonathan if (saidx->dst.sin6.sin6_len == 0) {
392 1.1 jonathan sin6 = (struct sockaddr_in6 *)&saidx->dst;
393 1.1 jonathan sin6->sin6_len = sizeof(*sin6);
394 1.1 jonathan sin6->sin6_family = AF_INET6;
395 1.1 jonathan sin6->sin6_port = IPSEC_PORT_ANY;
396 1.1 jonathan sin6->sin6_addr = ip6->ip6_dst;
397 1.1 jonathan if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
398 1.1 jonathan /* fix scope id for comparing SPD */
399 1.1 jonathan sin6->sin6_addr.s6_addr16[1] = 0;
400 1.1 jonathan sin6->sin6_scope_id =
401 1.1 jonathan ntohs(ip6->ip6_dst.s6_addr16[1]);
402 1.1 jonathan }
403 1.1 jonathan }
404 1.1 jonathan }
405 1.1 jonathan }
406 1.1 jonathan
407 1.1 jonathan /*
408 1.1 jonathan * Lookup SA and validate it.
409 1.1 jonathan */
410 1.1 jonathan *error = key_checkrequest(isr, saidx);
411 1.1 jonathan if (*error != 0) {
412 1.1 jonathan /*
413 1.1 jonathan * IPsec processing is required, but no SA found.
414 1.1 jonathan * I assume that key_acquire() had been called
415 1.1 jonathan * to get/establish the SA. Here I discard
416 1.1 jonathan * this packet because it is responsibility for
417 1.1 jonathan * upper layer to retransmit the packet.
418 1.1 jonathan */
419 1.27 thorpej IPSEC_STATINC(IPSEC_STAT_OUT_NOSA);
420 1.1 jonathan goto bad;
421 1.1 jonathan }
422 1.1 jonathan sav = isr->sav;
423 1.26 degroote /* sav may be NULL here if we have an USE rule */
424 1.26 degroote if (sav == NULL) {
425 1.1 jonathan IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
426 1.1 jonathan ("ipsec_nextisr: no SA found, but required; level %u",
427 1.1 jonathan ipsec_get_reqlevel(isr)));
428 1.1 jonathan isr = isr->next;
429 1.26 degroote /*
430 1.26 degroote * No more rules to apply, return NULL isr and no error
431 1.26 degroote * It can happen when the last rules are USE rules
432 1.26 degroote * */
433 1.1 jonathan if (isr == NULL) {
434 1.26 degroote *error = 0;
435 1.1 jonathan return isr;
436 1.1 jonathan }
437 1.1 jonathan goto again;
438 1.1 jonathan }
439 1.1 jonathan
440 1.1 jonathan /*
441 1.1 jonathan * Check system global policy controls.
442 1.1 jonathan */
443 1.1 jonathan if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
444 1.1 jonathan (isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
445 1.1 jonathan (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
446 1.1 jonathan DPRINTF(("ipsec_nextisr: IPsec outbound packet dropped due"
447 1.1 jonathan " to policy (check your sysctls)\n"));
448 1.27 thorpej IPSEC_OSTAT(ESP_STAT_PDROPS, AH_STAT_PDROPS,
449 1.27 thorpej IPCOMP_STAT_PDROPS);
450 1.1 jonathan *error = EHOSTUNREACH;
451 1.1 jonathan goto bad;
452 1.1 jonathan }
453 1.1 jonathan
454 1.1 jonathan /*
455 1.1 jonathan * Sanity check the SA contents for the caller
456 1.1 jonathan * before they invoke the xform output method.
457 1.1 jonathan */
458 1.1 jonathan if (sav->tdb_xform == NULL) {
459 1.1 jonathan DPRINTF(("ipsec_nextisr: no transform for SA\n"));
460 1.27 thorpej IPSEC_OSTAT(ESP_STAT_NOXFORM, AH_STAT_NOXFORM,
461 1.27 thorpej IPCOMP_STAT_NOXFORM);
462 1.1 jonathan *error = EHOSTUNREACH;
463 1.1 jonathan goto bad;
464 1.1 jonathan }
465 1.1 jonathan return isr;
466 1.1 jonathan bad:
467 1.1 jonathan IPSEC_ASSERT(*error != 0, ("ipsec_nextisr: error return w/ no error code"));
468 1.1 jonathan return NULL;
469 1.1 jonathan #undef IPSEC_OSTAT
470 1.1 jonathan }
471 1.1 jonathan
472 1.1 jonathan #ifdef INET
473 1.1 jonathan /*
474 1.1 jonathan * IPsec output logic for IPv4.
475 1.1 jonathan */
476 1.1 jonathan int
477 1.1 jonathan ipsec4_process_packet(
478 1.15 christos struct mbuf *m,
479 1.15 christos struct ipsecrequest *isr,
480 1.16 christos int flags,
481 1.15 christos int tunalready
482 1.15 christos )
483 1.1 jonathan {
484 1.1 jonathan struct secasindex saidx;
485 1.1 jonathan struct secasvar *sav;
486 1.1 jonathan struct ip *ip;
487 1.1 jonathan int s, error, i, off;
488 1.1 jonathan
489 1.1 jonathan IPSEC_ASSERT(m != NULL, ("ipsec4_process_packet: null mbuf"));
490 1.1 jonathan IPSEC_ASSERT(isr != NULL, ("ipsec4_process_packet: null isr"));
491 1.1 jonathan
492 1.1 jonathan s = splsoftnet(); /* insure SA contents don't change */
493 1.1 jonathan
494 1.1 jonathan isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
495 1.26 degroote if (isr == NULL) {
496 1.26 degroote if (error != 0) {
497 1.26 degroote goto bad;
498 1.26 degroote } else {
499 1.26 degroote if (ipsec_register_done(m, &error) < 0)
500 1.26 degroote goto bad;
501 1.26 degroote
502 1.26 degroote splx(s);
503 1.26 degroote return ipsec_reinject_ipstack(m, AF_INET);
504 1.26 degroote }
505 1.26 degroote }
506 1.1 jonathan
507 1.1 jonathan sav = isr->sav;
508 1.1 jonathan if (!tunalready) {
509 1.1 jonathan union sockaddr_union *dst = &sav->sah->saidx.dst;
510 1.1 jonathan int setdf;
511 1.1 jonathan
512 1.1 jonathan /*
513 1.1 jonathan * Collect IP_DF state from the outer header.
514 1.1 jonathan */
515 1.1 jonathan if (dst->sa.sa_family == AF_INET) {
516 1.1 jonathan if (m->m_len < sizeof (struct ip) &&
517 1.1 jonathan (m = m_pullup(m, sizeof (struct ip))) == NULL) {
518 1.1 jonathan error = ENOBUFS;
519 1.1 jonathan goto bad;
520 1.1 jonathan }
521 1.1 jonathan ip = mtod(m, struct ip *);
522 1.1 jonathan /* Honor system-wide control of how to handle IP_DF */
523 1.1 jonathan switch (ip4_ipsec_dfbit) {
524 1.1 jonathan case 0: /* clear in outer header */
525 1.1 jonathan case 1: /* set in outer header */
526 1.1 jonathan setdf = ip4_ipsec_dfbit;
527 1.1 jonathan break;
528 1.1 jonathan default: /* propagate to outer header */
529 1.3 jonathan setdf = ip->ip_off;
530 1.3 jonathan #ifndef __FreeBSD__
531 1.3 jonathan /* On FreeBSD, ip_off and ip_len assumed in host endian. */
532 1.3 jonathan setdf = ntohs(setdf);
533 1.3 jonathan #endif
534 1.3 jonathan setdf = htons(setdf & IP_DF);
535 1.1 jonathan break;
536 1.1 jonathan }
537 1.1 jonathan } else {
538 1.1 jonathan ip = NULL; /* keep compiler happy */
539 1.1 jonathan setdf = 0;
540 1.1 jonathan }
541 1.1 jonathan /* Do the appropriate encapsulation, if necessary */
542 1.1 jonathan if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
543 1.1 jonathan dst->sa.sa_family != AF_INET || /* PF mismatch */
544 1.1 jonathan #if 0
545 1.1 jonathan (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
546 1.1 jonathan sav->tdb_xform->xf_type == XF_IP4 || /* ditto */
547 1.1 jonathan #endif
548 1.1 jonathan (dst->sa.sa_family == AF_INET && /* Proxy */
549 1.1 jonathan dst->sin.sin_addr.s_addr != INADDR_ANY &&
550 1.1 jonathan dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
551 1.1 jonathan struct mbuf *mp;
552 1.1 jonathan
553 1.1 jonathan /* Fix IPv4 header checksum and length */
554 1.1 jonathan if (m->m_len < sizeof (struct ip) &&
555 1.1 jonathan (m = m_pullup(m, sizeof (struct ip))) == NULL) {
556 1.1 jonathan error = ENOBUFS;
557 1.1 jonathan goto bad;
558 1.1 jonathan }
559 1.1 jonathan ip = mtod(m, struct ip *);
560 1.1 jonathan ip->ip_len = htons(m->m_pkthdr.len);
561 1.1 jonathan ip->ip_sum = 0;
562 1.1 jonathan ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
563 1.1 jonathan
564 1.1 jonathan /* Encapsulate the packet */
565 1.1 jonathan error = ipip_output(m, isr, &mp, 0, 0);
566 1.1 jonathan if (mp == NULL && !error) {
567 1.1 jonathan /* Should never happen. */
568 1.1 jonathan DPRINTF(("ipsec4_process_packet: ipip_output "
569 1.1 jonathan "returns no mbuf and no error!"));
570 1.1 jonathan error = EFAULT;
571 1.1 jonathan }
572 1.1 jonathan if (error) {
573 1.8 scw if (mp) {
574 1.8 scw /* XXX: Should never happen! */
575 1.1 jonathan m_freem(mp);
576 1.8 scw }
577 1.8 scw m = NULL; /* ipip_output() already freed it */
578 1.1 jonathan goto bad;
579 1.1 jonathan }
580 1.1 jonathan m = mp, mp = NULL;
581 1.1 jonathan /*
582 1.1 jonathan * ipip_output clears IP_DF in the new header. If
583 1.1 jonathan * we need to propagate IP_DF from the outer header,
584 1.1 jonathan * then we have to do it here.
585 1.1 jonathan *
586 1.1 jonathan * XXX shouldn't assume what ipip_output does.
587 1.1 jonathan */
588 1.1 jonathan if (dst->sa.sa_family == AF_INET && setdf) {
589 1.1 jonathan if (m->m_len < sizeof (struct ip) &&
590 1.1 jonathan (m = m_pullup(m, sizeof (struct ip))) == NULL) {
591 1.1 jonathan error = ENOBUFS;
592 1.1 jonathan goto bad;
593 1.1 jonathan }
594 1.1 jonathan ip = mtod(m, struct ip *);
595 1.23 adrianp ip->ip_off |= IP_OFF_CONVERT(IP_DF);
596 1.1 jonathan }
597 1.1 jonathan }
598 1.1 jonathan }
599 1.1 jonathan
600 1.1 jonathan /*
601 1.1 jonathan * Dispatch to the appropriate IPsec transform logic. The
602 1.1 jonathan * packet will be returned for transmission after crypto
603 1.1 jonathan * processing, etc. are completed. For encapsulation we
604 1.1 jonathan * bypass this call because of the explicit call done above
605 1.1 jonathan * (necessary to deal with IP_DF handling for IPv4).
606 1.1 jonathan *
607 1.1 jonathan * NB: m & sav are ``passed to caller'' who's reponsible for
608 1.1 jonathan * for reclaiming their resources.
609 1.1 jonathan */
610 1.1 jonathan if (sav->tdb_xform->xf_type != XF_IP4) {
611 1.33 drochner union sockaddr_union *dst = &sav->sah->saidx.dst;
612 1.33 drochner if (dst->sa.sa_family == AF_INET) {
613 1.33 drochner ip = mtod(m, struct ip *);
614 1.33 drochner i = ip->ip_hl << 2;
615 1.33 drochner off = offsetof(struct ip, ip_p);
616 1.33 drochner } else {
617 1.33 drochner i = sizeof(struct ip6_hdr);
618 1.33 drochner off = offsetof(struct ip6_hdr, ip6_nxt);
619 1.33 drochner }
620 1.1 jonathan error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
621 1.1 jonathan } else {
622 1.1 jonathan error = ipsec_process_done(m, isr);
623 1.1 jonathan }
624 1.1 jonathan splx(s);
625 1.1 jonathan return error;
626 1.1 jonathan bad:
627 1.1 jonathan splx(s);
628 1.1 jonathan if (m)
629 1.1 jonathan m_freem(m);
630 1.1 jonathan return error;
631 1.1 jonathan }
632 1.1 jonathan #endif
633 1.1 jonathan
634 1.1 jonathan #ifdef INET6
635 1.1 jonathan int
636 1.21 degroote ipsec6_process_packet(
637 1.21 degroote struct mbuf *m,
638 1.21 degroote struct ipsecrequest *isr
639 1.21 degroote )
640 1.1 jonathan {
641 1.1 jonathan struct secasindex saidx;
642 1.21 degroote struct secasvar *sav;
643 1.21 degroote struct ip6_hdr *ip6;
644 1.21 degroote int s, error, i, off;
645 1.1 jonathan
646 1.21 degroote IPSEC_ASSERT(m != NULL, ("ipsec6_process_packet: null mbuf"));
647 1.21 degroote IPSEC_ASSERT(isr != NULL, ("ipsec6_process_packet: null isr"));
648 1.1 jonathan
649 1.21 degroote s = splsoftnet(); /* insure SA contents don't change */
650 1.1 jonathan isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
651 1.1 jonathan if (isr == NULL) {
652 1.26 degroote if (error != 0) {
653 1.26 degroote // XXX Should we send a notification ?
654 1.26 degroote goto bad;
655 1.26 degroote } else {
656 1.26 degroote if (ipsec_register_done(m, &error) < 0)
657 1.26 degroote goto bad;
658 1.26 degroote
659 1.26 degroote splx(s);
660 1.28 degroote return ipsec_reinject_ipstack(m, AF_INET6);
661 1.26 degroote }
662 1.1 jonathan }
663 1.1 jonathan
664 1.21 degroote sav = isr->sav;
665 1.21 degroote if (sav->tdb_xform->xf_type != XF_IP4) {
666 1.21 degroote i = sizeof(struct ip6_hdr);
667 1.21 degroote off = offsetof(struct ip6_hdr, ip6_nxt);
668 1.21 degroote error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
669 1.21 degroote } else {
670 1.21 degroote union sockaddr_union *dst = &sav->sah->saidx.dst;
671 1.1 jonathan
672 1.21 degroote ip6 = mtod(m, struct ip6_hdr *);
673 1.1 jonathan
674 1.21 degroote /* Do the appropriate encapsulation, if necessary */
675 1.21 degroote if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
676 1.21 degroote dst->sa.sa_family != AF_INET6 || /* PF mismatch */
677 1.21 degroote ((dst->sa.sa_family == AF_INET6) &&
678 1.21 degroote (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
679 1.21 degroote (!IN6_ARE_ADDR_EQUAL(&dst->sin6.sin6_addr,
680 1.21 degroote &ip6->ip6_dst)))
681 1.21 degroote )
682 1.21 degroote {
683 1.21 degroote struct mbuf *mp;
684 1.21 degroote /* Fix IPv6 header payload length. */
685 1.21 degroote if (m->m_len < sizeof(struct ip6_hdr))
686 1.21 degroote if ((m = m_pullup(m,sizeof(struct ip6_hdr))) == NULL)
687 1.21 degroote return ENOBUFS;
688 1.21 degroote
689 1.21 degroote if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
690 1.21 degroote /* No jumbogram support. */
691 1.21 degroote m_freem(m);
692 1.21 degroote return ENXIO; /*XXX*/
693 1.21 degroote }
694 1.21 degroote ip6 = mtod(m, struct ip6_hdr *);
695 1.21 degroote ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
696 1.1 jonathan
697 1.21 degroote /* Encapsulate the packet */
698 1.21 degroote error = ipip_output(m, isr, &mp, 0, 0);
699 1.21 degroote if (mp == NULL && !error) {
700 1.21 degroote /* Should never happen. */
701 1.21 degroote DPRINTF(("ipsec6_process_packet: ipip_output "
702 1.21 degroote "returns no mbuf and no error!"));
703 1.21 degroote error = EFAULT;
704 1.21 degroote }
705 1.1 jonathan
706 1.21 degroote if (error) {
707 1.21 degroote if (mp) {
708 1.21 degroote /* XXX: Should never happen! */
709 1.21 degroote m_freem(mp);
710 1.21 degroote }
711 1.21 degroote m = NULL; /* ipip_output() already freed it */
712 1.19 joerg goto bad;
713 1.19 joerg }
714 1.21 degroote
715 1.21 degroote m = mp;
716 1.21 degroote mp = NULL;
717 1.1 jonathan }
718 1.21 degroote
719 1.21 degroote error = ipsec_process_done(m,isr);
720 1.1 jonathan }
721 1.21 degroote splx(s);
722 1.21 degroote return error;
723 1.1 jonathan bad:
724 1.21 degroote splx(s);
725 1.1 jonathan if (m)
726 1.1 jonathan m_freem(m);
727 1.1 jonathan return error;
728 1.1 jonathan }
729 1.1 jonathan #endif /*INET6*/
730