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