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