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