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