ipsec_output.c revision 1.13 1 1.13 jonathan /* $NetBSD: ipsec_output.c,v 1.13 2004/05/07 00:55:15 jonathan 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.13 jonathan __KERNEL_RCSID(0, "$NetBSD: ipsec_output.c,v 1.13 2004/05/07 00:55:15 jonathan 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.1 jonathan
76 1.1 jonathan #include <netipsec/ipsec.h>
77 1.13 jonathan #include <netipsec/ipsec_var.h>
78 1.1 jonathan #ifdef INET6
79 1.1 jonathan #include <netipsec/ipsec6.h>
80 1.1 jonathan #endif
81 1.1 jonathan #include <netipsec/ah_var.h>
82 1.1 jonathan #include <netipsec/esp_var.h>
83 1.1 jonathan #include <netipsec/ipcomp_var.h>
84 1.1 jonathan
85 1.1 jonathan #include <netipsec/xform.h>
86 1.1 jonathan
87 1.7 tls #include <netipsec/key.h>
88 1.7 tls #include <netipsec/keydb.h>
89 1.7 tls #include <netipsec/key_debug.h>
90 1.1 jonathan #include <netipsec/ipsec_osdep.h>
91 1.1 jonathan
92 1.10 jonathan #include <net/net_osdep.h> /* ovbcopy() in ipsec6_encapsulate() */
93 1.10 jonathan
94 1.1 jonathan int
95 1.1 jonathan ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
96 1.1 jonathan {
97 1.1 jonathan struct tdb_ident *tdbi;
98 1.1 jonathan struct m_tag *mtag;
99 1.1 jonathan struct secasvar *sav;
100 1.1 jonathan struct secasindex *saidx;
101 1.1 jonathan int error;
102 1.1 jonathan
103 1.1 jonathan IPSEC_SPLASSERT_SOFTNET("ipsec_process_done");
104 1.1 jonathan
105 1.1 jonathan IPSEC_ASSERT(m != NULL, ("ipsec_process_done: null mbuf"));
106 1.1 jonathan IPSEC_ASSERT(isr != NULL, ("ipsec_process_done: null ISR"));
107 1.1 jonathan sav = isr->sav;
108 1.1 jonathan IPSEC_ASSERT(sav != NULL, ("ipsec_process_done: null SA"));
109 1.1 jonathan IPSEC_ASSERT(sav->sah != NULL, ("ipsec_process_done: null SAH"));
110 1.1 jonathan
111 1.1 jonathan saidx = &sav->sah->saidx;
112 1.1 jonathan switch (saidx->dst.sa.sa_family) {
113 1.1 jonathan #ifdef INET
114 1.1 jonathan case AF_INET:
115 1.1 jonathan /* Fix the header length, for AH processing. */
116 1.1 jonathan mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
117 1.1 jonathan break;
118 1.1 jonathan #endif /* INET */
119 1.1 jonathan #ifdef INET6
120 1.1 jonathan case AF_INET6:
121 1.1 jonathan /* Fix the header length, for AH processing. */
122 1.1 jonathan if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
123 1.1 jonathan error = ENXIO;
124 1.1 jonathan goto bad;
125 1.1 jonathan }
126 1.1 jonathan if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
127 1.1 jonathan /* No jumbogram support. */
128 1.1 jonathan error = ENXIO; /*?*/
129 1.1 jonathan goto bad;
130 1.1 jonathan }
131 1.1 jonathan mtod(m, struct ip6_hdr *)->ip6_plen =
132 1.1 jonathan htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
133 1.1 jonathan break;
134 1.1 jonathan #endif /* INET6 */
135 1.1 jonathan default:
136 1.1 jonathan DPRINTF(("ipsec_process_done: unknown protocol family %u\n",
137 1.1 jonathan saidx->dst.sa.sa_family));
138 1.1 jonathan error = ENXIO;
139 1.1 jonathan goto bad;
140 1.1 jonathan }
141 1.1 jonathan
142 1.1 jonathan /*
143 1.1 jonathan * Add a record of what we've done or what needs to be done to the
144 1.1 jonathan * packet.
145 1.1 jonathan */
146 1.1 jonathan mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE,
147 1.1 jonathan sizeof(struct tdb_ident), M_NOWAIT);
148 1.1 jonathan if (mtag == NULL) {
149 1.1 jonathan DPRINTF(("ipsec_process_done: could not get packet tag\n"));
150 1.1 jonathan error = ENOMEM;
151 1.1 jonathan goto bad;
152 1.1 jonathan }
153 1.1 jonathan
154 1.1 jonathan tdbi = (struct tdb_ident *)(mtag + 1);
155 1.1 jonathan tdbi->dst = saidx->dst;
156 1.1 jonathan tdbi->proto = saidx->proto;
157 1.1 jonathan tdbi->spi = sav->spi;
158 1.1 jonathan m_tag_prepend(m, mtag);
159 1.1 jonathan
160 1.1 jonathan /*
161 1.1 jonathan * If there's another (bundled) SA to apply, do so.
162 1.1 jonathan * Note that this puts a burden on the kernel stack size.
163 1.1 jonathan * If this is a problem we'll need to introduce a queue
164 1.1 jonathan * to set the packet on so we can unwind the stack before
165 1.1 jonathan * doing further processing.
166 1.1 jonathan */
167 1.1 jonathan if (isr->next) {
168 1.1 jonathan newipsecstat.ips_out_bundlesa++;
169 1.1 jonathan return ipsec4_process_packet(m, isr->next, 0, 0);
170 1.1 jonathan }
171 1.1 jonathan
172 1.1 jonathan /*
173 1.1 jonathan * We're done with IPsec processing, transmit the packet using the
174 1.1 jonathan * appropriate network protocol (IP or IPv6). SPD lookup will be
175 1.1 jonathan * performed again there.
176 1.1 jonathan */
177 1.1 jonathan switch (saidx->dst.sa.sa_family) {
178 1.1 jonathan #ifdef INET
179 1.1 jonathan struct ip *ip;
180 1.1 jonathan case AF_INET:
181 1.1 jonathan ip = mtod(m, struct ip *);
182 1.1 jonathan #ifdef __FreeBSD__
183 1.1 jonathan /* FreeBSD ip_output() expects ip_len, ip_off in host endian */
184 1.1 jonathan ip->ip_len = ntohs(ip->ip_len);
185 1.1 jonathan ip->ip_off = ntohs(ip->ip_off);
186 1.1 jonathan #endif /* __FreeBSD_ */
187 1.2 jonathan return ip_output(m, NULL, NULL, IP_RAWOUTPUT,
188 1.5 itojun (struct ip_moptions *)NULL, (struct socket *)NULL);
189 1.2 jonathan
190 1.1 jonathan #endif /* INET */
191 1.1 jonathan #ifdef INET6
192 1.1 jonathan case AF_INET6:
193 1.1 jonathan /*
194 1.1 jonathan * We don't need massage, IPv6 header fields are always in
195 1.1 jonathan * net endian.
196 1.1 jonathan */
197 1.1 jonathan return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
198 1.1 jonathan #endif /* INET6 */
199 1.1 jonathan }
200 1.1 jonathan panic("ipsec_process_done");
201 1.1 jonathan bad:
202 1.1 jonathan m_freem(m);
203 1.1 jonathan KEY_FREESAV(&sav);
204 1.1 jonathan return (error);
205 1.1 jonathan }
206 1.1 jonathan
207 1.1 jonathan static struct ipsecrequest *
208 1.1 jonathan ipsec_nextisr(
209 1.1 jonathan struct mbuf *m,
210 1.1 jonathan struct ipsecrequest *isr,
211 1.1 jonathan int af,
212 1.1 jonathan struct secasindex *saidx,
213 1.1 jonathan int *error
214 1.1 jonathan )
215 1.1 jonathan {
216 1.1 jonathan #define IPSEC_OSTAT(x,y,z) (isr->saidx.proto == IPPROTO_ESP ? (x)++ : \
217 1.1 jonathan isr->saidx.proto == IPPROTO_AH ? (y)++ : (z)++)
218 1.1 jonathan struct secasvar *sav;
219 1.1 jonathan
220 1.1 jonathan IPSEC_SPLASSERT_SOFTNET("ipsec_nextisr");
221 1.1 jonathan IPSEC_ASSERT(af == AF_INET || af == AF_INET6,
222 1.1 jonathan ("ipsec_nextisr: invalid address family %u", af));
223 1.1 jonathan again:
224 1.1 jonathan /*
225 1.1 jonathan * Craft SA index to search for proper SA. Note that
226 1.1 jonathan * we only fillin unspecified SA peers for transport
227 1.1 jonathan * mode; for tunnel mode they must already be filled in.
228 1.1 jonathan */
229 1.1 jonathan *saidx = isr->saidx;
230 1.1 jonathan if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
231 1.1 jonathan /* Fillin unspecified SA peers only for transport mode */
232 1.1 jonathan if (af == AF_INET) {
233 1.1 jonathan struct sockaddr_in *sin;
234 1.1 jonathan struct ip *ip = mtod(m, struct ip *);
235 1.1 jonathan
236 1.1 jonathan if (saidx->src.sa.sa_len == 0) {
237 1.1 jonathan sin = &saidx->src.sin;
238 1.1 jonathan sin->sin_len = sizeof(*sin);
239 1.1 jonathan sin->sin_family = AF_INET;
240 1.1 jonathan sin->sin_port = IPSEC_PORT_ANY;
241 1.1 jonathan sin->sin_addr = ip->ip_src;
242 1.1 jonathan }
243 1.1 jonathan if (saidx->dst.sa.sa_len == 0) {
244 1.1 jonathan sin = &saidx->dst.sin;
245 1.1 jonathan sin->sin_len = sizeof(*sin);
246 1.1 jonathan sin->sin_family = AF_INET;
247 1.1 jonathan sin->sin_port = IPSEC_PORT_ANY;
248 1.1 jonathan sin->sin_addr = ip->ip_dst;
249 1.1 jonathan }
250 1.1 jonathan } else {
251 1.1 jonathan struct sockaddr_in6 *sin6;
252 1.1 jonathan struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
253 1.1 jonathan
254 1.1 jonathan if (saidx->src.sin6.sin6_len == 0) {
255 1.1 jonathan sin6 = (struct sockaddr_in6 *)&saidx->src;
256 1.1 jonathan sin6->sin6_len = sizeof(*sin6);
257 1.1 jonathan sin6->sin6_family = AF_INET6;
258 1.1 jonathan sin6->sin6_port = IPSEC_PORT_ANY;
259 1.1 jonathan sin6->sin6_addr = ip6->ip6_src;
260 1.1 jonathan if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
261 1.1 jonathan /* fix scope id for comparing SPD */
262 1.1 jonathan sin6->sin6_addr.s6_addr16[1] = 0;
263 1.1 jonathan sin6->sin6_scope_id =
264 1.1 jonathan ntohs(ip6->ip6_src.s6_addr16[1]);
265 1.1 jonathan }
266 1.1 jonathan }
267 1.1 jonathan if (saidx->dst.sin6.sin6_len == 0) {
268 1.1 jonathan sin6 = (struct sockaddr_in6 *)&saidx->dst;
269 1.1 jonathan sin6->sin6_len = sizeof(*sin6);
270 1.1 jonathan sin6->sin6_family = AF_INET6;
271 1.1 jonathan sin6->sin6_port = IPSEC_PORT_ANY;
272 1.1 jonathan sin6->sin6_addr = ip6->ip6_dst;
273 1.1 jonathan if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
274 1.1 jonathan /* fix scope id for comparing SPD */
275 1.1 jonathan sin6->sin6_addr.s6_addr16[1] = 0;
276 1.1 jonathan sin6->sin6_scope_id =
277 1.1 jonathan ntohs(ip6->ip6_dst.s6_addr16[1]);
278 1.1 jonathan }
279 1.1 jonathan }
280 1.1 jonathan }
281 1.1 jonathan }
282 1.1 jonathan
283 1.1 jonathan /*
284 1.1 jonathan * Lookup SA and validate it.
285 1.1 jonathan */
286 1.1 jonathan *error = key_checkrequest(isr, saidx);
287 1.1 jonathan if (*error != 0) {
288 1.1 jonathan /*
289 1.1 jonathan * IPsec processing is required, but no SA found.
290 1.1 jonathan * I assume that key_acquire() had been called
291 1.1 jonathan * to get/establish the SA. Here I discard
292 1.1 jonathan * this packet because it is responsibility for
293 1.1 jonathan * upper layer to retransmit the packet.
294 1.1 jonathan */
295 1.1 jonathan newipsecstat.ips_out_nosa++;
296 1.1 jonathan goto bad;
297 1.1 jonathan }
298 1.1 jonathan sav = isr->sav;
299 1.1 jonathan if (sav == NULL) { /* XXX valid return */
300 1.1 jonathan IPSEC_ASSERT(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
301 1.1 jonathan ("ipsec_nextisr: no SA found, but required; level %u",
302 1.1 jonathan ipsec_get_reqlevel(isr)));
303 1.1 jonathan isr = isr->next;
304 1.1 jonathan if (isr == NULL) {
305 1.1 jonathan /*XXXstatistic??*/
306 1.1 jonathan *error = EINVAL; /*XXX*/
307 1.1 jonathan return isr;
308 1.1 jonathan }
309 1.1 jonathan goto again;
310 1.1 jonathan }
311 1.1 jonathan
312 1.1 jonathan /*
313 1.1 jonathan * Check system global policy controls.
314 1.1 jonathan */
315 1.1 jonathan if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
316 1.1 jonathan (isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
317 1.1 jonathan (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
318 1.1 jonathan DPRINTF(("ipsec_nextisr: IPsec outbound packet dropped due"
319 1.1 jonathan " to policy (check your sysctls)\n"));
320 1.1 jonathan IPSEC_OSTAT(espstat.esps_pdrops, ahstat.ahs_pdrops,
321 1.1 jonathan ipcompstat.ipcomps_pdrops);
322 1.1 jonathan *error = EHOSTUNREACH;
323 1.1 jonathan goto bad;
324 1.1 jonathan }
325 1.1 jonathan
326 1.1 jonathan /*
327 1.1 jonathan * Sanity check the SA contents for the caller
328 1.1 jonathan * before they invoke the xform output method.
329 1.1 jonathan */
330 1.1 jonathan if (sav->tdb_xform == NULL) {
331 1.1 jonathan DPRINTF(("ipsec_nextisr: no transform for SA\n"));
332 1.1 jonathan IPSEC_OSTAT(espstat.esps_noxform, ahstat.ahs_noxform,
333 1.1 jonathan ipcompstat.ipcomps_noxform);
334 1.1 jonathan *error = EHOSTUNREACH;
335 1.1 jonathan goto bad;
336 1.1 jonathan }
337 1.1 jonathan return isr;
338 1.1 jonathan bad:
339 1.1 jonathan IPSEC_ASSERT(*error != 0, ("ipsec_nextisr: error return w/ no error code"));
340 1.1 jonathan return NULL;
341 1.1 jonathan #undef IPSEC_OSTAT
342 1.1 jonathan }
343 1.1 jonathan
344 1.1 jonathan #ifdef INET
345 1.1 jonathan /*
346 1.1 jonathan * IPsec output logic for IPv4.
347 1.1 jonathan */
348 1.1 jonathan int
349 1.1 jonathan ipsec4_process_packet(
350 1.1 jonathan struct mbuf *m,
351 1.1 jonathan struct ipsecrequest *isr,
352 1.1 jonathan int flags,
353 1.1 jonathan int tunalready)
354 1.1 jonathan {
355 1.1 jonathan struct secasindex saidx;
356 1.1 jonathan struct secasvar *sav;
357 1.1 jonathan struct ip *ip;
358 1.1 jonathan int s, error, i, off;
359 1.1 jonathan
360 1.1 jonathan IPSEC_ASSERT(m != NULL, ("ipsec4_process_packet: null mbuf"));
361 1.1 jonathan IPSEC_ASSERT(isr != NULL, ("ipsec4_process_packet: null isr"));
362 1.1 jonathan
363 1.1 jonathan s = splsoftnet(); /* insure SA contents don't change */
364 1.1 jonathan
365 1.1 jonathan isr = ipsec_nextisr(m, isr, AF_INET, &saidx, &error);
366 1.1 jonathan if (isr == NULL)
367 1.1 jonathan goto bad;
368 1.1 jonathan
369 1.1 jonathan sav = isr->sav;
370 1.1 jonathan if (!tunalready) {
371 1.1 jonathan union sockaddr_union *dst = &sav->sah->saidx.dst;
372 1.1 jonathan int setdf;
373 1.1 jonathan
374 1.1 jonathan /*
375 1.1 jonathan * Collect IP_DF state from the outer header.
376 1.1 jonathan */
377 1.1 jonathan if (dst->sa.sa_family == AF_INET) {
378 1.1 jonathan if (m->m_len < sizeof (struct ip) &&
379 1.1 jonathan (m = m_pullup(m, sizeof (struct ip))) == NULL) {
380 1.1 jonathan error = ENOBUFS;
381 1.1 jonathan goto bad;
382 1.1 jonathan }
383 1.1 jonathan ip = mtod(m, struct ip *);
384 1.1 jonathan /* Honor system-wide control of how to handle IP_DF */
385 1.1 jonathan switch (ip4_ipsec_dfbit) {
386 1.1 jonathan case 0: /* clear in outer header */
387 1.1 jonathan case 1: /* set in outer header */
388 1.1 jonathan setdf = ip4_ipsec_dfbit;
389 1.1 jonathan break;
390 1.1 jonathan default: /* propagate to outer header */
391 1.3 jonathan setdf = ip->ip_off;
392 1.3 jonathan #ifndef __FreeBSD__
393 1.3 jonathan /* On FreeBSD, ip_off and ip_len assumed in host endian. */
394 1.3 jonathan setdf = ntohs(setdf);
395 1.3 jonathan #endif
396 1.3 jonathan setdf = htons(setdf & IP_DF);
397 1.1 jonathan break;
398 1.1 jonathan }
399 1.1 jonathan } else {
400 1.1 jonathan ip = NULL; /* keep compiler happy */
401 1.1 jonathan setdf = 0;
402 1.1 jonathan }
403 1.1 jonathan /* Do the appropriate encapsulation, if necessary */
404 1.1 jonathan if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
405 1.1 jonathan dst->sa.sa_family != AF_INET || /* PF mismatch */
406 1.1 jonathan #if 0
407 1.1 jonathan (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
408 1.1 jonathan sav->tdb_xform->xf_type == XF_IP4 || /* ditto */
409 1.1 jonathan #endif
410 1.1 jonathan (dst->sa.sa_family == AF_INET && /* Proxy */
411 1.1 jonathan dst->sin.sin_addr.s_addr != INADDR_ANY &&
412 1.1 jonathan dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
413 1.1 jonathan struct mbuf *mp;
414 1.1 jonathan
415 1.1 jonathan /* Fix IPv4 header checksum and length */
416 1.1 jonathan if (m->m_len < sizeof (struct ip) &&
417 1.1 jonathan (m = m_pullup(m, sizeof (struct ip))) == NULL) {
418 1.1 jonathan error = ENOBUFS;
419 1.1 jonathan goto bad;
420 1.1 jonathan }
421 1.1 jonathan ip = mtod(m, struct ip *);
422 1.1 jonathan ip->ip_len = htons(m->m_pkthdr.len);
423 1.1 jonathan ip->ip_sum = 0;
424 1.1 jonathan #ifdef _IP_VHL
425 1.1 jonathan if (ip->ip_vhl == IP_VHL_BORING)
426 1.1 jonathan ip->ip_sum = in_cksum_hdr(ip);
427 1.1 jonathan else
428 1.1 jonathan ip->ip_sum = in_cksum(m,
429 1.1 jonathan _IP_VHL_HL(ip->ip_vhl) << 2);
430 1.1 jonathan #else
431 1.1 jonathan ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
432 1.1 jonathan #endif
433 1.1 jonathan
434 1.1 jonathan /* Encapsulate the packet */
435 1.1 jonathan error = ipip_output(m, isr, &mp, 0, 0);
436 1.1 jonathan if (mp == NULL && !error) {
437 1.1 jonathan /* Should never happen. */
438 1.1 jonathan DPRINTF(("ipsec4_process_packet: ipip_output "
439 1.1 jonathan "returns no mbuf and no error!"));
440 1.1 jonathan error = EFAULT;
441 1.1 jonathan }
442 1.1 jonathan if (error) {
443 1.8 scw if (mp) {
444 1.8 scw /* XXX: Should never happen! */
445 1.1 jonathan m_freem(mp);
446 1.8 scw }
447 1.8 scw m = NULL; /* ipip_output() already freed it */
448 1.1 jonathan goto bad;
449 1.1 jonathan }
450 1.1 jonathan m = mp, mp = NULL;
451 1.1 jonathan /*
452 1.1 jonathan * ipip_output clears IP_DF in the new header. If
453 1.1 jonathan * we need to propagate IP_DF from the outer header,
454 1.1 jonathan * then we have to do it here.
455 1.1 jonathan *
456 1.1 jonathan * XXX shouldn't assume what ipip_output does.
457 1.1 jonathan */
458 1.1 jonathan if (dst->sa.sa_family == AF_INET && setdf) {
459 1.1 jonathan if (m->m_len < sizeof (struct ip) &&
460 1.1 jonathan (m = m_pullup(m, sizeof (struct ip))) == NULL) {
461 1.1 jonathan error = ENOBUFS;
462 1.1 jonathan goto bad;
463 1.1 jonathan }
464 1.1 jonathan ip = mtod(m, struct ip *);
465 1.1 jonathan ip->ip_off = ntohs(ip->ip_off);
466 1.1 jonathan ip->ip_off |= IP_DF;
467 1.1 jonathan ip->ip_off = htons(ip->ip_off);
468 1.1 jonathan }
469 1.1 jonathan }
470 1.1 jonathan }
471 1.1 jonathan
472 1.1 jonathan /*
473 1.1 jonathan * Dispatch to the appropriate IPsec transform logic. The
474 1.1 jonathan * packet will be returned for transmission after crypto
475 1.1 jonathan * processing, etc. are completed. For encapsulation we
476 1.1 jonathan * bypass this call because of the explicit call done above
477 1.1 jonathan * (necessary to deal with IP_DF handling for IPv4).
478 1.1 jonathan *
479 1.1 jonathan * NB: m & sav are ``passed to caller'' who's reponsible for
480 1.1 jonathan * for reclaiming their resources.
481 1.1 jonathan */
482 1.1 jonathan if (sav->tdb_xform->xf_type != XF_IP4) {
483 1.1 jonathan ip = mtod(m, struct ip *);
484 1.1 jonathan i = ip->ip_hl << 2;
485 1.1 jonathan off = offsetof(struct ip, ip_p);
486 1.1 jonathan error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
487 1.1 jonathan } else {
488 1.1 jonathan error = ipsec_process_done(m, isr);
489 1.1 jonathan }
490 1.1 jonathan splx(s);
491 1.1 jonathan return error;
492 1.1 jonathan bad:
493 1.1 jonathan splx(s);
494 1.1 jonathan if (m)
495 1.1 jonathan m_freem(m);
496 1.1 jonathan return error;
497 1.1 jonathan }
498 1.1 jonathan #endif
499 1.1 jonathan
500 1.1 jonathan #ifdef INET6
501 1.1 jonathan /*
502 1.1 jonathan * Chop IP6 header from the payload.
503 1.1 jonathan */
504 1.1 jonathan static struct mbuf *
505 1.1 jonathan ipsec6_splithdr(struct mbuf *m)
506 1.1 jonathan {
507 1.1 jonathan struct mbuf *mh;
508 1.1 jonathan struct ip6_hdr *ip6;
509 1.1 jonathan int hlen;
510 1.1 jonathan
511 1.1 jonathan IPSEC_ASSERT(m->m_len >= sizeof (struct ip6_hdr),
512 1.1 jonathan ("ipsec6_splithdr: first mbuf too short, len %u", m->m_len));
513 1.1 jonathan ip6 = mtod(m, struct ip6_hdr *);
514 1.1 jonathan hlen = sizeof(struct ip6_hdr);
515 1.1 jonathan if (m->m_len > hlen) {
516 1.1 jonathan MGETHDR(mh, M_DONTWAIT, MT_HEADER);
517 1.1 jonathan if (!mh) {
518 1.1 jonathan m_freem(m);
519 1.1 jonathan return NULL;
520 1.1 jonathan }
521 1.1 jonathan M_MOVE_PKTHDR(mh, m);
522 1.1 jonathan MH_ALIGN(mh, hlen);
523 1.1 jonathan m->m_len -= hlen;
524 1.1 jonathan m->m_data += hlen;
525 1.1 jonathan mh->m_next = m;
526 1.1 jonathan m = mh;
527 1.1 jonathan m->m_len = hlen;
528 1.1 jonathan bcopy((caddr_t)ip6, mtod(m, caddr_t), hlen);
529 1.1 jonathan } else if (m->m_len < hlen) {
530 1.1 jonathan m = m_pullup(m, hlen);
531 1.1 jonathan if (!m)
532 1.1 jonathan return NULL;
533 1.1 jonathan }
534 1.1 jonathan return m;
535 1.1 jonathan }
536 1.1 jonathan
537 1.1 jonathan /*
538 1.1 jonathan * IPsec output logic for IPv6, transport mode.
539 1.1 jonathan */
540 1.1 jonathan int
541 1.1 jonathan ipsec6_output_trans(
542 1.1 jonathan struct ipsec_output_state *state,
543 1.1 jonathan u_char *nexthdrp,
544 1.1 jonathan struct mbuf *mprev,
545 1.1 jonathan struct secpolicy *sp,
546 1.1 jonathan int flags,
547 1.1 jonathan int *tun)
548 1.1 jonathan {
549 1.1 jonathan struct ipsecrequest *isr;
550 1.1 jonathan struct secasindex saidx;
551 1.1 jonathan int error = 0;
552 1.1 jonathan struct mbuf *m;
553 1.1 jonathan
554 1.1 jonathan IPSEC_ASSERT(state != NULL, ("ipsec6_output: null state"));
555 1.1 jonathan IPSEC_ASSERT(state->m != NULL, ("ipsec6_output: null m"));
556 1.1 jonathan IPSEC_ASSERT(nexthdrp != NULL, ("ipsec6_output: null nexthdrp"));
557 1.1 jonathan IPSEC_ASSERT(mprev != NULL, ("ipsec6_output: null mprev"));
558 1.1 jonathan IPSEC_ASSERT(sp != NULL, ("ipsec6_output: null sp"));
559 1.1 jonathan IPSEC_ASSERT(tun != NULL, ("ipsec6_output: null tun"));
560 1.1 jonathan
561 1.1 jonathan KEYDEBUG(KEYDEBUG_IPSEC_DATA,
562 1.1 jonathan printf("ipsec6_output_trans: applyed SP\n");
563 1.1 jonathan kdebug_secpolicy(sp));
564 1.1 jonathan
565 1.1 jonathan isr = sp->req;
566 1.1 jonathan if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
567 1.1 jonathan /* the rest will be handled by ipsec6_output_tunnel() */
568 1.1 jonathan *tun = 1; /* need tunnel-mode processing */
569 1.1 jonathan return 0;
570 1.1 jonathan }
571 1.1 jonathan
572 1.1 jonathan *tun = 0;
573 1.1 jonathan m = state->m;
574 1.1 jonathan
575 1.1 jonathan isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
576 1.1 jonathan if (isr == NULL) {
577 1.1 jonathan #ifdef notdef
578 1.1 jonathan /* XXX should notification be done for all errors ? */
579 1.1 jonathan /*
580 1.1 jonathan * Notify the fact that the packet is discarded
581 1.1 jonathan * to ourselves. I believe this is better than
582 1.1 jonathan * just silently discarding. (jinmei (at) kame.net)
583 1.1 jonathan * XXX: should we restrict the error to TCP packets?
584 1.1 jonathan * XXX: should we directly notify sockets via
585 1.1 jonathan * pfctlinputs?
586 1.1 jonathan */
587 1.1 jonathan icmp6_error(m, ICMP6_DST_UNREACH,
588 1.1 jonathan ICMP6_DST_UNREACH_ADMIN, 0);
589 1.1 jonathan m = NULL; /* NB: icmp6_error frees mbuf */
590 1.1 jonathan #endif
591 1.1 jonathan goto bad;
592 1.1 jonathan }
593 1.1 jonathan
594 1.1 jonathan return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
595 1.1 jonathan sizeof (struct ip6_hdr),
596 1.1 jonathan offsetof(struct ip6_hdr, ip6_nxt));
597 1.1 jonathan bad:
598 1.1 jonathan if (m)
599 1.1 jonathan m_freem(m);
600 1.1 jonathan state->m = NULL;
601 1.1 jonathan return error;
602 1.1 jonathan }
603 1.1 jonathan
604 1.1 jonathan static int
605 1.1 jonathan ipsec6_encapsulate(struct mbuf *m, struct secasvar *sav)
606 1.1 jonathan {
607 1.1 jonathan struct ip6_hdr *oip6;
608 1.1 jonathan struct ip6_hdr *ip6;
609 1.1 jonathan size_t plen;
610 1.1 jonathan
611 1.1 jonathan /* can't tunnel between different AFs */
612 1.1 jonathan if (sav->sah->saidx.src.sa.sa_family != AF_INET6 ||
613 1.1 jonathan sav->sah->saidx.dst.sa.sa_family != AF_INET6) {
614 1.1 jonathan m_freem(m);
615 1.1 jonathan return EINVAL;
616 1.1 jonathan }
617 1.1 jonathan IPSEC_ASSERT(m->m_len != sizeof (struct ip6_hdr),
618 1.1 jonathan ("ipsec6_encapsulate: mbuf wrong size; len %u", m->m_len));
619 1.1 jonathan
620 1.1 jonathan
621 1.1 jonathan /*
622 1.1 jonathan * grow the mbuf to accomodate the new IPv6 header.
623 1.1 jonathan */
624 1.1 jonathan plen = m->m_pkthdr.len;
625 1.1 jonathan if (M_LEADINGSPACE(m->m_next) < sizeof(struct ip6_hdr)) {
626 1.1 jonathan struct mbuf *n;
627 1.1 jonathan MGET(n, M_DONTWAIT, MT_DATA);
628 1.1 jonathan if (!n) {
629 1.1 jonathan m_freem(m);
630 1.1 jonathan return ENOBUFS;
631 1.1 jonathan }
632 1.1 jonathan n->m_len = sizeof(struct ip6_hdr);
633 1.1 jonathan n->m_next = m->m_next;
634 1.1 jonathan m->m_next = n;
635 1.1 jonathan m->m_pkthdr.len += sizeof(struct ip6_hdr);
636 1.1 jonathan oip6 = mtod(n, struct ip6_hdr *);
637 1.1 jonathan } else {
638 1.1 jonathan m->m_next->m_len += sizeof(struct ip6_hdr);
639 1.1 jonathan m->m_next->m_data -= sizeof(struct ip6_hdr);
640 1.1 jonathan m->m_pkthdr.len += sizeof(struct ip6_hdr);
641 1.1 jonathan oip6 = mtod(m->m_next, struct ip6_hdr *);
642 1.1 jonathan }
643 1.1 jonathan ip6 = mtod(m, struct ip6_hdr *);
644 1.1 jonathan ovbcopy((caddr_t)ip6, (caddr_t)oip6, sizeof(struct ip6_hdr));
645 1.1 jonathan
646 1.1 jonathan /* Fake link-local scope-class addresses */
647 1.1 jonathan if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src))
648 1.1 jonathan oip6->ip6_src.s6_addr16[1] = 0;
649 1.1 jonathan if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst))
650 1.1 jonathan oip6->ip6_dst.s6_addr16[1] = 0;
651 1.1 jonathan
652 1.1 jonathan /* construct new IPv6 header. see RFC 2401 5.1.2.2 */
653 1.1 jonathan /* ECN consideration. */
654 1.1 jonathan ip6_ecn_ingress(ip6_ipsec_ecn, &ip6->ip6_flow, &oip6->ip6_flow);
655 1.1 jonathan if (plen < IPV6_MAXPACKET - sizeof(struct ip6_hdr))
656 1.1 jonathan ip6->ip6_plen = htons(plen);
657 1.1 jonathan else {
658 1.1 jonathan /* ip6->ip6_plen will be updated in ip6_output() */
659 1.1 jonathan }
660 1.1 jonathan ip6->ip6_nxt = IPPROTO_IPV6;
661 1.1 jonathan sav->sah->saidx.src.sin6.sin6_addr = ip6->ip6_src;
662 1.1 jonathan sav->sah->saidx.dst.sin6.sin6_addr = ip6->ip6_dst;
663 1.1 jonathan ip6->ip6_hlim = IPV6_DEFHLIM;
664 1.1 jonathan
665 1.1 jonathan /* XXX Should ip6_src be updated later ? */
666 1.1 jonathan
667 1.1 jonathan return 0;
668 1.1 jonathan }
669 1.1 jonathan
670 1.1 jonathan /*
671 1.1 jonathan * IPsec output logic for IPv6, tunnel mode.
672 1.1 jonathan */
673 1.1 jonathan int
674 1.1 jonathan ipsec6_output_tunnel(struct ipsec_output_state *state, struct secpolicy *sp, int flags)
675 1.1 jonathan {
676 1.1 jonathan struct ip6_hdr *ip6;
677 1.1 jonathan struct ipsecrequest *isr;
678 1.1 jonathan struct secasindex saidx;
679 1.1 jonathan int error;
680 1.1 jonathan struct sockaddr_in6* dst6;
681 1.1 jonathan struct mbuf *m;
682 1.1 jonathan
683 1.1 jonathan IPSEC_ASSERT(state != NULL, ("ipsec6_output: null state"));
684 1.1 jonathan IPSEC_ASSERT(state->m != NULL, ("ipsec6_output: null m"));
685 1.1 jonathan IPSEC_ASSERT(sp != NULL, ("ipsec6_output: null sp"));
686 1.1 jonathan
687 1.1 jonathan KEYDEBUG(KEYDEBUG_IPSEC_DATA,
688 1.1 jonathan printf("ipsec6_output_tunnel: applyed SP\n");
689 1.1 jonathan kdebug_secpolicy(sp));
690 1.1 jonathan
691 1.1 jonathan m = state->m;
692 1.1 jonathan /*
693 1.1 jonathan * transport mode ipsec (before the 1st tunnel mode) is already
694 1.1 jonathan * processed by ipsec6_output_trans().
695 1.1 jonathan */
696 1.1 jonathan for (isr = sp->req; isr; isr = isr->next) {
697 1.1 jonathan if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
698 1.1 jonathan break;
699 1.1 jonathan }
700 1.1 jonathan isr = ipsec_nextisr(m, isr, AF_INET6, &saidx, &error);
701 1.1 jonathan if (isr == NULL)
702 1.1 jonathan goto bad;
703 1.1 jonathan
704 1.1 jonathan /*
705 1.1 jonathan * There may be the case that SA status will be changed when
706 1.1 jonathan * we are refering to one. So calling splsoftnet().
707 1.1 jonathan */
708 1.1 jonathan if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
709 1.1 jonathan /*
710 1.1 jonathan * build IPsec tunnel.
711 1.1 jonathan */
712 1.1 jonathan /* XXX should be processed with other familiy */
713 1.1 jonathan if (isr->sav->sah->saidx.src.sa.sa_family != AF_INET6) {
714 1.1 jonathan ipseclog((LOG_ERR, "ipsec6_output_tunnel: "
715 1.1 jonathan "family mismatched between inner and outer, spi=%lu\n",
716 1.11 jonathan (u_long)ntohl(isr->sav->spi)));
717 1.1 jonathan newipsecstat.ips_out_inval++;
718 1.1 jonathan error = EAFNOSUPPORT;
719 1.1 jonathan goto bad;
720 1.1 jonathan }
721 1.1 jonathan
722 1.1 jonathan m = ipsec6_splithdr(m);
723 1.1 jonathan if (!m) {
724 1.1 jonathan newipsecstat.ips_out_nomem++;
725 1.1 jonathan error = ENOMEM;
726 1.1 jonathan goto bad;
727 1.1 jonathan }
728 1.1 jonathan error = ipsec6_encapsulate(m, isr->sav);
729 1.1 jonathan if (error) {
730 1.1 jonathan m = NULL;
731 1.1 jonathan goto bad;
732 1.1 jonathan }
733 1.1 jonathan ip6 = mtod(m, struct ip6_hdr *);
734 1.1 jonathan
735 1.1 jonathan state->ro = &isr->sav->sah->sa_route;
736 1.1 jonathan state->dst = (struct sockaddr *)&state->ro->ro_dst;
737 1.1 jonathan dst6 = (struct sockaddr_in6 *)state->dst;
738 1.1 jonathan if (state->ro->ro_rt
739 1.1 jonathan && ((state->ro->ro_rt->rt_flags & RTF_UP) == 0
740 1.1 jonathan || !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_dst))) {
741 1.1 jonathan RTFREE(state->ro->ro_rt);
742 1.1 jonathan state->ro->ro_rt = NULL;
743 1.1 jonathan }
744 1.1 jonathan if (state->ro->ro_rt == 0) {
745 1.1 jonathan bzero(dst6, sizeof(*dst6));
746 1.1 jonathan dst6->sin6_family = AF_INET6;
747 1.1 jonathan dst6->sin6_len = sizeof(*dst6);
748 1.1 jonathan dst6->sin6_addr = ip6->ip6_dst;
749 1.1 jonathan rtalloc(state->ro);
750 1.1 jonathan }
751 1.1 jonathan if (state->ro->ro_rt == 0) {
752 1.1 jonathan ip6stat.ip6s_noroute++;
753 1.1 jonathan newipsecstat.ips_out_noroute++;
754 1.1 jonathan error = EHOSTUNREACH;
755 1.1 jonathan goto bad;
756 1.1 jonathan }
757 1.1 jonathan
758 1.1 jonathan /* adjust state->dst if tunnel endpoint is offlink */
759 1.1 jonathan if (state->ro->ro_rt->rt_flags & RTF_GATEWAY) {
760 1.1 jonathan state->dst = (struct sockaddr *)state->ro->ro_rt->rt_gateway;
761 1.1 jonathan dst6 = (struct sockaddr_in6 *)state->dst;
762 1.1 jonathan }
763 1.1 jonathan }
764 1.1 jonathan
765 1.1 jonathan m = ipsec6_splithdr(m);
766 1.1 jonathan if (!m) {
767 1.1 jonathan newipsecstat.ips_out_nomem++;
768 1.1 jonathan error = ENOMEM;
769 1.1 jonathan goto bad;
770 1.1 jonathan }
771 1.1 jonathan ip6 = mtod(m, struct ip6_hdr *);
772 1.1 jonathan return (*isr->sav->tdb_xform->xf_output)(m, isr, NULL,
773 1.1 jonathan sizeof (struct ip6_hdr),
774 1.1 jonathan offsetof(struct ip6_hdr, ip6_nxt));
775 1.1 jonathan bad:
776 1.1 jonathan if (m)
777 1.1 jonathan m_freem(m);
778 1.1 jonathan state->m = NULL;
779 1.1 jonathan return error;
780 1.1 jonathan }
781 1.1 jonathan #endif /*INET6*/
782