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