ipsec_output.c revision 1.53 1 1.53 ozaki /* $NetBSD: ipsec_output.c,v 1.53 2017/07/13 01:48:52 ozaki-r 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.53 ozaki __KERNEL_RCSID(0, "$NetBSD: ipsec_output.c,v 1.53 2017/07/13 01:48:52 ozaki-r Exp $");
33 1.1 jonathan
34 1.1 jonathan /*
35 1.1 jonathan * IPsec output processing.
36 1.1 jonathan */
37 1.42 ozaki #if defined(_KERNEL_OPT)
38 1.1 jonathan #include "opt_inet.h"
39 1.42 ozaki #endif
40 1.1 jonathan
41 1.1 jonathan #include <sys/param.h>
42 1.1 jonathan #include <sys/systm.h>
43 1.1 jonathan #include <sys/mbuf.h>
44 1.1 jonathan #include <sys/domain.h>
45 1.1 jonathan #include <sys/protosw.h>
46 1.1 jonathan #include <sys/socket.h>
47 1.1 jonathan #include <sys/errno.h>
48 1.1 jonathan #include <sys/syslog.h>
49 1.1 jonathan
50 1.1 jonathan #include <net/if.h>
51 1.1 jonathan #include <net/route.h>
52 1.1 jonathan
53 1.1 jonathan #include <netinet/in.h>
54 1.1 jonathan #include <netinet/in_systm.h>
55 1.1 jonathan #include <netinet/ip.h>
56 1.1 jonathan #include <netinet/ip_var.h>
57 1.1 jonathan #include <netinet/in_var.h>
58 1.1 jonathan #include <netinet/ip_ecn.h>
59 1.1 jonathan
60 1.1 jonathan #include <netinet/ip6.h>
61 1.1 jonathan #ifdef INET6
62 1.1 jonathan #include <netinet6/ip6_var.h>
63 1.1 jonathan #endif
64 1.1 jonathan #include <netinet/in_pcb.h>
65 1.1 jonathan #ifdef INET6
66 1.1 jonathan #include <netinet/icmp6.h>
67 1.1 jonathan #endif
68 1.22 degroote #include <netinet/udp.h>
69 1.1 jonathan
70 1.1 jonathan #include <netipsec/ipsec.h>
71 1.13 jonathan #include <netipsec/ipsec_var.h>
72 1.27 thorpej #include <netipsec/ipsec_private.h>
73 1.1 jonathan #ifdef INET6
74 1.1 jonathan #include <netipsec/ipsec6.h>
75 1.1 jonathan #endif
76 1.1 jonathan #include <netipsec/ah_var.h>
77 1.1 jonathan #include <netipsec/esp_var.h>
78 1.1 jonathan #include <netipsec/ipcomp_var.h>
79 1.1 jonathan
80 1.1 jonathan #include <netipsec/xform.h>
81 1.1 jonathan
82 1.7 tls #include <netipsec/key.h>
83 1.7 tls #include <netipsec/keydb.h>
84 1.7 tls #include <netipsec/key_debug.h>
85 1.1 jonathan
86 1.10 jonathan #include <net/net_osdep.h> /* ovbcopy() in ipsec6_encapsulate() */
87 1.10 jonathan
88 1.25 degroote
89 1.25 degroote /*
90 1.25 degroote * Add a IPSEC_OUT_DONE tag to mark that we have finished the ipsec processing
91 1.25 degroote * It will be used by ip{,6}_output to check if we have already or not
92 1.25 degroote * processed this packet.
93 1.25 degroote */
94 1.25 degroote static int
95 1.25 degroote ipsec_register_done(struct mbuf *m, int * error)
96 1.25 degroote {
97 1.25 degroote struct m_tag *mtag;
98 1.25 degroote
99 1.25 degroote mtag = m_tag_get(PACKET_TAG_IPSEC_OUT_DONE, 0, M_NOWAIT);
100 1.25 degroote if (mtag == NULL) {
101 1.48 ozaki IPSECLOG(LOG_DEBUG, "could not get packet tag\n");
102 1.25 degroote *error = ENOMEM;
103 1.25 degroote return -1;
104 1.25 degroote }
105 1.25 degroote
106 1.25 degroote m_tag_prepend(m, mtag);
107 1.25 degroote return 0;
108 1.25 degroote }
109 1.25 degroote
110 1.26 degroote static int
111 1.26 degroote ipsec_reinject_ipstack(struct mbuf *m, int af)
112 1.26 degroote {
113 1.30 drochner #if defined(INET) || defined(INET6)
114 1.30 drochner int rv;
115 1.30 drochner #endif
116 1.26 degroote
117 1.26 degroote switch (af) {
118 1.26 degroote #ifdef INET
119 1.26 degroote case AF_INET:
120 1.30 drochner KERNEL_LOCK(1, NULL);
121 1.31 drochner rv = ip_output(m, NULL, NULL, IP_RAWOUTPUT|IP_NOIPNEWID,
122 1.37 plunky NULL, NULL);
123 1.30 drochner KERNEL_UNLOCK_ONE(NULL);
124 1.30 drochner return rv;
125 1.26 degroote
126 1.26 degroote #endif /* INET */
127 1.26 degroote #ifdef INET6
128 1.26 degroote case AF_INET6:
129 1.26 degroote /*
130 1.26 degroote * We don't need massage, IPv6 header fields are always in
131 1.26 degroote * net endian.
132 1.26 degroote */
133 1.30 drochner KERNEL_LOCK(1, NULL);
134 1.30 drochner rv = ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
135 1.30 drochner KERNEL_UNLOCK_ONE(NULL);
136 1.30 drochner return rv;
137 1.26 degroote #endif /* INET6 */
138 1.26 degroote }
139 1.26 degroote
140 1.26 degroote panic("ipsec_reinject_ipstack : iunknown protocol family %u\n", af);
141 1.26 degroote return -1; /* NOTREACHED */
142 1.26 degroote }
143 1.26 degroote
144 1.1 jonathan int
145 1.1 jonathan ipsec_process_done(struct mbuf *m, struct ipsecrequest *isr)
146 1.1 jonathan {
147 1.1 jonathan struct secasvar *sav;
148 1.1 jonathan struct secasindex *saidx;
149 1.1 jonathan int error;
150 1.22 degroote #ifdef INET
151 1.22 degroote struct ip * ip;
152 1.22 degroote #endif /* INET */
153 1.22 degroote #ifdef INET6
154 1.22 degroote struct ip6_hdr * ip6;
155 1.22 degroote #endif /* INET6 */
156 1.22 degroote struct mbuf * mo;
157 1.22 degroote struct udphdr *udp = NULL;
158 1.22 degroote uint64_t * data = NULL;
159 1.22 degroote int hlen, roff;
160 1.1 jonathan
161 1.1 jonathan IPSEC_SPLASSERT_SOFTNET("ipsec_process_done");
162 1.1 jonathan
163 1.44 ozaki KASSERT(m != NULL);
164 1.44 ozaki KASSERT(isr != NULL);
165 1.1 jonathan sav = isr->sav;
166 1.44 ozaki KASSERT(sav != NULL);
167 1.1 jonathan
168 1.1 jonathan saidx = &sav->sah->saidx;
169 1.22 degroote
170 1.22 degroote if(sav->natt_type != 0) {
171 1.22 degroote ip = mtod(m, struct ip *);
172 1.22 degroote
173 1.22 degroote hlen = sizeof(struct udphdr);
174 1.22 degroote if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
175 1.22 degroote hlen += sizeof(uint64_t);
176 1.22 degroote
177 1.22 degroote mo = m_makespace(m, sizeof(struct ip), hlen, &roff);
178 1.22 degroote if (mo == NULL) {
179 1.47 ryo char buf[IPSEC_ADDRSTRLEN];
180 1.48 ozaki IPSECLOG(LOG_DEBUG,
181 1.48 ozaki "failed to inject %u byte UDP for SA %s/%08lx\n",
182 1.47 ryo hlen, ipsec_address(&saidx->dst, buf, sizeof(buf)),
183 1.48 ozaki (u_long) ntohl(sav->spi));
184 1.22 degroote error = ENOBUFS;
185 1.22 degroote goto bad;
186 1.22 degroote }
187 1.22 degroote
188 1.22 degroote udp = (struct udphdr*) (mtod(mo, char*) + roff);
189 1.22 degroote data = (uint64_t*) (udp + 1);
190 1.22 degroote
191 1.22 degroote if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
192 1.22 degroote *data = 0; /* NON-IKE Marker */
193 1.22 degroote
194 1.22 degroote if (sav->natt_type == UDP_ENCAP_ESPINUDP_NON_IKE)
195 1.22 degroote udp->uh_sport = htons(UDP_ENCAP_ESPINUDP_PORT);
196 1.22 degroote else
197 1.22 degroote udp->uh_sport = key_portfromsaddr(&saidx->src);
198 1.22 degroote
199 1.22 degroote udp->uh_dport = key_portfromsaddr(&saidx->dst);
200 1.22 degroote udp->uh_sum = 0;
201 1.29 dyoung udp->uh_ulen = htons(m->m_pkthdr.len - (ip->ip_hl << 2));
202 1.22 degroote }
203 1.22 degroote
204 1.1 jonathan switch (saidx->dst.sa.sa_family) {
205 1.1 jonathan #ifdef INET
206 1.1 jonathan case AF_INET:
207 1.1 jonathan /* Fix the header length, for AH processing. */
208 1.22 degroote ip = mtod(m, struct ip *);
209 1.22 degroote ip->ip_len = htons(m->m_pkthdr.len);
210 1.22 degroote if (sav->natt_type != 0)
211 1.22 degroote ip->ip_p = IPPROTO_UDP;
212 1.1 jonathan break;
213 1.1 jonathan #endif /* INET */
214 1.1 jonathan #ifdef INET6
215 1.1 jonathan case AF_INET6:
216 1.1 jonathan /* Fix the header length, for AH processing. */
217 1.1 jonathan if (m->m_pkthdr.len < sizeof (struct ip6_hdr)) {
218 1.1 jonathan error = ENXIO;
219 1.1 jonathan goto bad;
220 1.1 jonathan }
221 1.1 jonathan if (m->m_pkthdr.len - sizeof (struct ip6_hdr) > IPV6_MAXPACKET) {
222 1.1 jonathan /* No jumbogram support. */
223 1.1 jonathan error = ENXIO; /*?*/
224 1.1 jonathan goto bad;
225 1.1 jonathan }
226 1.22 degroote ip6 = mtod(m, struct ip6_hdr *);
227 1.22 degroote ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
228 1.22 degroote if (sav->natt_type != 0)
229 1.22 degroote ip6->ip6_nxt = IPPROTO_UDP;
230 1.1 jonathan break;
231 1.1 jonathan #endif /* INET6 */
232 1.1 jonathan default:
233 1.48 ozaki IPSECLOG(LOG_DEBUG, "unknown protocol family %u\n",
234 1.48 ozaki saidx->dst.sa.sa_family);
235 1.1 jonathan error = ENXIO;
236 1.1 jonathan goto bad;
237 1.1 jonathan }
238 1.1 jonathan
239 1.32 drochner key_sa_recordxfer(sav, m);
240 1.32 drochner
241 1.1 jonathan /*
242 1.1 jonathan * If there's another (bundled) SA to apply, do so.
243 1.1 jonathan * Note that this puts a burden on the kernel stack size.
244 1.1 jonathan * If this is a problem we'll need to introduce a queue
245 1.1 jonathan * to set the packet on so we can unwind the stack before
246 1.1 jonathan * doing further processing.
247 1.1 jonathan */
248 1.1 jonathan if (isr->next) {
249 1.27 thorpej IPSEC_STATINC(IPSEC_STAT_OUT_BUNDLESA);
250 1.29 dyoung switch ( saidx->dst.sa.sa_family ) {
251 1.21 degroote #ifdef INET
252 1.29 dyoung case AF_INET:
253 1.46 ozaki return ipsec4_process_packet(m, isr->next);
254 1.21 degroote #endif /* INET */
255 1.21 degroote #ifdef INET6
256 1.21 degroote case AF_INET6:
257 1.29 dyoung return ipsec6_process_packet(m,isr->next);
258 1.21 degroote #endif /* INET6 */
259 1.21 degroote default :
260 1.48 ozaki IPSECLOG(LOG_DEBUG, "unknown protocol family %u\n",
261 1.48 ozaki saidx->dst.sa.sa_family);
262 1.21 degroote error = ENXIO;
263 1.21 degroote goto bad;
264 1.29 dyoung }
265 1.1 jonathan }
266 1.1 jonathan
267 1.1 jonathan /*
268 1.25 degroote * We're done with IPsec processing,
269 1.25 degroote * mark that we have already processed the packet
270 1.25 degroote * transmit it packet using the appropriate network protocol (IP or IPv6).
271 1.1 jonathan */
272 1.25 degroote
273 1.25 degroote if (ipsec_register_done(m, &error) < 0)
274 1.25 degroote goto bad;
275 1.25 degroote
276 1.26 degroote return ipsec_reinject_ipstack(m, saidx->dst.sa.sa_family);
277 1.1 jonathan bad:
278 1.1 jonathan m_freem(m);
279 1.1 jonathan KEY_FREESAV(&sav);
280 1.1 jonathan return (error);
281 1.1 jonathan }
282 1.1 jonathan
283 1.26 degroote /*
284 1.26 degroote * ipsec_nextisr can return :
285 1.26 degroote * - isr == NULL and error != 0 => something is bad : the packet must be
286 1.26 degroote * discarded
287 1.26 degroote * - isr == NULL and error == 0 => no more rules to apply, ipsec processing
288 1.26 degroote * is done, reinject it in ip stack
289 1.26 degroote * - isr != NULL (error == 0) => we need to apply one rule to the packet
290 1.26 degroote */
291 1.1 jonathan static struct ipsecrequest *
292 1.1 jonathan ipsec_nextisr(
293 1.1 jonathan struct mbuf *m,
294 1.1 jonathan struct ipsecrequest *isr,
295 1.1 jonathan int af,
296 1.1 jonathan int *error
297 1.1 jonathan )
298 1.1 jonathan {
299 1.49 ozaki #define IPSEC_OSTAT(type) \
300 1.27 thorpej do { \
301 1.27 thorpej switch (isr->saidx.proto) { \
302 1.27 thorpej case IPPROTO_ESP: \
303 1.49 ozaki ESP_STATINC(ESP_STAT_ ## type); \
304 1.27 thorpej break; \
305 1.27 thorpej case IPPROTO_AH: \
306 1.49 ozaki AH_STATINC(AH_STAT_ ## type); \
307 1.27 thorpej break; \
308 1.27 thorpej default: \
309 1.49 ozaki IPCOMP_STATINC(IPCOMP_STAT_ ## type); \
310 1.27 thorpej break; \
311 1.27 thorpej } \
312 1.27 thorpej } while (/*CONSTCOND*/0)
313 1.27 thorpej
314 1.1 jonathan struct secasvar *sav;
315 1.52 ozaki struct secasindex *saidx;
316 1.1 jonathan
317 1.1 jonathan IPSEC_SPLASSERT_SOFTNET("ipsec_nextisr");
318 1.44 ozaki KASSERTMSG(af == AF_INET || af == AF_INET6,
319 1.44 ozaki "invalid address family %u", af);
320 1.1 jonathan again:
321 1.1 jonathan /*
322 1.1 jonathan * Craft SA index to search for proper SA. Note that
323 1.1 jonathan * we only fillin unspecified SA peers for transport
324 1.1 jonathan * mode; for tunnel mode they must already be filled in.
325 1.1 jonathan */
326 1.52 ozaki saidx = &isr->saidx;
327 1.1 jonathan if (isr->saidx.mode == IPSEC_MODE_TRANSPORT) {
328 1.1 jonathan /* Fillin unspecified SA peers only for transport mode */
329 1.1 jonathan if (af == AF_INET) {
330 1.1 jonathan struct sockaddr_in *sin;
331 1.1 jonathan struct ip *ip = mtod(m, struct ip *);
332 1.1 jonathan
333 1.1 jonathan if (saidx->src.sa.sa_len == 0) {
334 1.1 jonathan sin = &saidx->src.sin;
335 1.1 jonathan sin->sin_len = sizeof(*sin);
336 1.1 jonathan sin->sin_family = AF_INET;
337 1.1 jonathan sin->sin_port = IPSEC_PORT_ANY;
338 1.1 jonathan sin->sin_addr = ip->ip_src;
339 1.1 jonathan }
340 1.1 jonathan if (saidx->dst.sa.sa_len == 0) {
341 1.1 jonathan sin = &saidx->dst.sin;
342 1.1 jonathan sin->sin_len = sizeof(*sin);
343 1.1 jonathan sin->sin_family = AF_INET;
344 1.1 jonathan sin->sin_port = IPSEC_PORT_ANY;
345 1.1 jonathan sin->sin_addr = ip->ip_dst;
346 1.1 jonathan }
347 1.1 jonathan } else {
348 1.1 jonathan struct sockaddr_in6 *sin6;
349 1.1 jonathan struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
350 1.1 jonathan
351 1.1 jonathan if (saidx->src.sin6.sin6_len == 0) {
352 1.1 jonathan sin6 = (struct sockaddr_in6 *)&saidx->src;
353 1.1 jonathan sin6->sin6_len = sizeof(*sin6);
354 1.1 jonathan sin6->sin6_family = AF_INET6;
355 1.1 jonathan sin6->sin6_port = IPSEC_PORT_ANY;
356 1.1 jonathan sin6->sin6_addr = ip6->ip6_src;
357 1.1 jonathan if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
358 1.1 jonathan /* fix scope id for comparing SPD */
359 1.1 jonathan sin6->sin6_addr.s6_addr16[1] = 0;
360 1.1 jonathan sin6->sin6_scope_id =
361 1.1 jonathan ntohs(ip6->ip6_src.s6_addr16[1]);
362 1.1 jonathan }
363 1.1 jonathan }
364 1.1 jonathan if (saidx->dst.sin6.sin6_len == 0) {
365 1.1 jonathan sin6 = (struct sockaddr_in6 *)&saidx->dst;
366 1.1 jonathan sin6->sin6_len = sizeof(*sin6);
367 1.1 jonathan sin6->sin6_family = AF_INET6;
368 1.1 jonathan sin6->sin6_port = IPSEC_PORT_ANY;
369 1.1 jonathan sin6->sin6_addr = ip6->ip6_dst;
370 1.1 jonathan if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
371 1.1 jonathan /* fix scope id for comparing SPD */
372 1.1 jonathan sin6->sin6_addr.s6_addr16[1] = 0;
373 1.1 jonathan sin6->sin6_scope_id =
374 1.1 jonathan ntohs(ip6->ip6_dst.s6_addr16[1]);
375 1.1 jonathan }
376 1.1 jonathan }
377 1.1 jonathan }
378 1.1 jonathan }
379 1.1 jonathan
380 1.1 jonathan /*
381 1.1 jonathan * Lookup SA and validate it.
382 1.1 jonathan */
383 1.52 ozaki *error = key_checkrequest(isr);
384 1.1 jonathan if (*error != 0) {
385 1.1 jonathan /*
386 1.1 jonathan * IPsec processing is required, but no SA found.
387 1.1 jonathan * I assume that key_acquire() had been called
388 1.1 jonathan * to get/establish the SA. Here I discard
389 1.1 jonathan * this packet because it is responsibility for
390 1.1 jonathan * upper layer to retransmit the packet.
391 1.1 jonathan */
392 1.27 thorpej IPSEC_STATINC(IPSEC_STAT_OUT_NOSA);
393 1.1 jonathan goto bad;
394 1.1 jonathan }
395 1.1 jonathan sav = isr->sav;
396 1.26 degroote /* sav may be NULL here if we have an USE rule */
397 1.26 degroote if (sav == NULL) {
398 1.44 ozaki KASSERTMSG(ipsec_get_reqlevel(isr) == IPSEC_LEVEL_USE,
399 1.44 ozaki "no SA found, but required; level %u",
400 1.44 ozaki ipsec_get_reqlevel(isr));
401 1.1 jonathan isr = isr->next;
402 1.26 degroote /*
403 1.26 degroote * No more rules to apply, return NULL isr and no error
404 1.26 degroote * It can happen when the last rules are USE rules
405 1.26 degroote * */
406 1.1 jonathan if (isr == NULL) {
407 1.26 degroote *error = 0;
408 1.1 jonathan return isr;
409 1.1 jonathan }
410 1.1 jonathan goto again;
411 1.1 jonathan }
412 1.1 jonathan
413 1.1 jonathan /*
414 1.1 jonathan * Check system global policy controls.
415 1.1 jonathan */
416 1.1 jonathan if ((isr->saidx.proto == IPPROTO_ESP && !esp_enable) ||
417 1.1 jonathan (isr->saidx.proto == IPPROTO_AH && !ah_enable) ||
418 1.1 jonathan (isr->saidx.proto == IPPROTO_IPCOMP && !ipcomp_enable)) {
419 1.48 ozaki IPSECLOG(LOG_DEBUG, "IPsec outbound packet dropped due"
420 1.48 ozaki " to policy (check your sysctls)\n");
421 1.49 ozaki IPSEC_OSTAT(PDROPS);
422 1.1 jonathan *error = EHOSTUNREACH;
423 1.1 jonathan goto bad;
424 1.1 jonathan }
425 1.1 jonathan
426 1.1 jonathan /*
427 1.1 jonathan * Sanity check the SA contents for the caller
428 1.1 jonathan * before they invoke the xform output method.
429 1.1 jonathan */
430 1.50 ozaki KASSERT(sav->tdb_xform != NULL);
431 1.1 jonathan return isr;
432 1.1 jonathan bad:
433 1.44 ozaki KASSERTMSG(*error != 0, "error return w/ no error code");
434 1.1 jonathan return NULL;
435 1.1 jonathan #undef IPSEC_OSTAT
436 1.1 jonathan }
437 1.1 jonathan
438 1.1 jonathan #ifdef INET
439 1.1 jonathan /*
440 1.1 jonathan * IPsec output logic for IPv4.
441 1.1 jonathan */
442 1.1 jonathan int
443 1.46 ozaki ipsec4_process_packet(struct mbuf *m, struct ipsecrequest *isr)
444 1.1 jonathan {
445 1.1 jonathan struct secasvar *sav;
446 1.1 jonathan struct ip *ip;
447 1.1 jonathan int s, error, i, off;
448 1.46 ozaki union sockaddr_union *dst;
449 1.46 ozaki int setdf;
450 1.1 jonathan
451 1.44 ozaki KASSERT(m != NULL);
452 1.44 ozaki KASSERT(isr != NULL);
453 1.1 jonathan
454 1.1 jonathan s = splsoftnet(); /* insure SA contents don't change */
455 1.1 jonathan
456 1.52 ozaki isr = ipsec_nextisr(m, isr, AF_INET, &error);
457 1.26 degroote if (isr == NULL) {
458 1.26 degroote if (error != 0) {
459 1.26 degroote goto bad;
460 1.26 degroote } else {
461 1.26 degroote if (ipsec_register_done(m, &error) < 0)
462 1.26 degroote goto bad;
463 1.26 degroote
464 1.26 degroote splx(s);
465 1.26 degroote return ipsec_reinject_ipstack(m, AF_INET);
466 1.26 degroote }
467 1.26 degroote }
468 1.1 jonathan
469 1.1 jonathan sav = isr->sav;
470 1.46 ozaki dst = &sav->sah->saidx.dst;
471 1.1 jonathan
472 1.46 ozaki /*
473 1.46 ozaki * Collect IP_DF state from the outer header.
474 1.46 ozaki */
475 1.46 ozaki if (dst->sa.sa_family == AF_INET) {
476 1.46 ozaki if (m->m_len < sizeof (struct ip) &&
477 1.46 ozaki (m = m_pullup(m, sizeof (struct ip))) == NULL) {
478 1.46 ozaki error = ENOBUFS;
479 1.46 ozaki goto bad;
480 1.46 ozaki }
481 1.46 ozaki ip = mtod(m, struct ip *);
482 1.46 ozaki /* Honor system-wide control of how to handle IP_DF */
483 1.46 ozaki switch (ip4_ipsec_dfbit) {
484 1.46 ozaki case 0: /* clear in outer header */
485 1.46 ozaki case 1: /* set in outer header */
486 1.46 ozaki setdf = ip4_ipsec_dfbit;
487 1.46 ozaki break;
488 1.46 ozaki default: /* propagate to outer header */
489 1.46 ozaki setdf = ip->ip_off;
490 1.46 ozaki setdf = ntohs(setdf);
491 1.46 ozaki setdf = htons(setdf & IP_DF);
492 1.46 ozaki break;
493 1.1 jonathan }
494 1.46 ozaki } else {
495 1.46 ozaki ip = NULL; /* keep compiler happy */
496 1.46 ozaki setdf = 0;
497 1.46 ozaki }
498 1.46 ozaki /* Do the appropriate encapsulation, if necessary */
499 1.46 ozaki if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
500 1.46 ozaki dst->sa.sa_family != AF_INET || /* PF mismatch */
501 1.1 jonathan #if 0
502 1.46 ozaki (sav->flags & SADB_X_SAFLAGS_TUNNEL) || /* Tunnel requ'd */
503 1.46 ozaki sav->tdb_xform->xf_type == XF_IP4 || /* ditto */
504 1.1 jonathan #endif
505 1.46 ozaki (dst->sa.sa_family == AF_INET && /* Proxy */
506 1.46 ozaki dst->sin.sin_addr.s_addr != INADDR_ANY &&
507 1.46 ozaki dst->sin.sin_addr.s_addr != ip->ip_dst.s_addr)) {
508 1.46 ozaki struct mbuf *mp;
509 1.46 ozaki
510 1.46 ozaki /* Fix IPv4 header checksum and length */
511 1.46 ozaki if (m->m_len < sizeof (struct ip) &&
512 1.46 ozaki (m = m_pullup(m, sizeof (struct ip))) == NULL) {
513 1.46 ozaki error = ENOBUFS;
514 1.46 ozaki goto bad;
515 1.46 ozaki }
516 1.46 ozaki ip = mtod(m, struct ip *);
517 1.46 ozaki ip->ip_len = htons(m->m_pkthdr.len);
518 1.46 ozaki ip->ip_sum = 0;
519 1.46 ozaki ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
520 1.1 jonathan
521 1.46 ozaki /* Encapsulate the packet */
522 1.46 ozaki error = ipip_output(m, isr, &mp, 0, 0);
523 1.46 ozaki if (mp == NULL && !error) {
524 1.46 ozaki /* Should never happen. */
525 1.48 ozaki IPSECLOG(LOG_DEBUG,
526 1.48 ozaki "ipip_output returns no mbuf and no error!");
527 1.46 ozaki error = EFAULT;
528 1.46 ozaki }
529 1.46 ozaki if (error) {
530 1.46 ozaki if (mp) {
531 1.46 ozaki /* XXX: Should never happen! */
532 1.46 ozaki m_freem(mp);
533 1.46 ozaki }
534 1.46 ozaki m = NULL; /* ipip_output() already freed it */
535 1.46 ozaki goto bad;
536 1.46 ozaki }
537 1.46 ozaki m = mp, mp = NULL;
538 1.46 ozaki /*
539 1.46 ozaki * ipip_output clears IP_DF in the new header. If
540 1.46 ozaki * we need to propagate IP_DF from the outer header,
541 1.46 ozaki * then we have to do it here.
542 1.46 ozaki *
543 1.46 ozaki * XXX shouldn't assume what ipip_output does.
544 1.46 ozaki */
545 1.46 ozaki if (dst->sa.sa_family == AF_INET && setdf) {
546 1.1 jonathan if (m->m_len < sizeof (struct ip) &&
547 1.1 jonathan (m = m_pullup(m, sizeof (struct ip))) == NULL) {
548 1.1 jonathan error = ENOBUFS;
549 1.1 jonathan goto bad;
550 1.1 jonathan }
551 1.1 jonathan ip = mtod(m, struct ip *);
552 1.46 ozaki ip->ip_off |= htons(IP_DF);
553 1.1 jonathan }
554 1.1 jonathan }
555 1.1 jonathan
556 1.1 jonathan /*
557 1.1 jonathan * Dispatch to the appropriate IPsec transform logic. The
558 1.1 jonathan * packet will be returned for transmission after crypto
559 1.1 jonathan * processing, etc. are completed. For encapsulation we
560 1.1 jonathan * bypass this call because of the explicit call done above
561 1.1 jonathan * (necessary to deal with IP_DF handling for IPv4).
562 1.1 jonathan *
563 1.1 jonathan * NB: m & sav are ``passed to caller'' who's reponsible for
564 1.1 jonathan * for reclaiming their resources.
565 1.1 jonathan */
566 1.1 jonathan if (sav->tdb_xform->xf_type != XF_IP4) {
567 1.33 drochner if (dst->sa.sa_family == AF_INET) {
568 1.33 drochner ip = mtod(m, struct ip *);
569 1.33 drochner i = ip->ip_hl << 2;
570 1.33 drochner off = offsetof(struct ip, ip_p);
571 1.33 drochner } else {
572 1.33 drochner i = sizeof(struct ip6_hdr);
573 1.33 drochner off = offsetof(struct ip6_hdr, ip6_nxt);
574 1.33 drochner }
575 1.1 jonathan error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
576 1.1 jonathan } else {
577 1.1 jonathan error = ipsec_process_done(m, isr);
578 1.1 jonathan }
579 1.1 jonathan splx(s);
580 1.1 jonathan return error;
581 1.1 jonathan bad:
582 1.1 jonathan splx(s);
583 1.1 jonathan if (m)
584 1.1 jonathan m_freem(m);
585 1.1 jonathan return error;
586 1.1 jonathan }
587 1.1 jonathan #endif
588 1.1 jonathan
589 1.1 jonathan #ifdef INET6
590 1.38 drochner static void
591 1.38 drochner compute_ipsec_pos(struct mbuf *m, int *i, int *off)
592 1.38 drochner {
593 1.38 drochner int nxt;
594 1.38 drochner struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr*);
595 1.38 drochner struct ip6_ext ip6e;
596 1.38 drochner int dstopt = 0;
597 1.38 drochner
598 1.38 drochner *i = sizeof(struct ip6_hdr);
599 1.38 drochner *off = offsetof(struct ip6_hdr, ip6_nxt);
600 1.38 drochner nxt = ip6->ip6_nxt;
601 1.38 drochner
602 1.38 drochner /*
603 1.38 drochner * chase mbuf chain to find the appropriate place to
604 1.38 drochner * put AH/ESP/IPcomp header.
605 1.38 drochner * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
606 1.38 drochner */
607 1.38 drochner do {
608 1.38 drochner switch (nxt) {
609 1.38 drochner case IPPROTO_AH:
610 1.38 drochner case IPPROTO_ESP:
611 1.38 drochner case IPPROTO_IPCOMP:
612 1.38 drochner /*
613 1.38 drochner * we should not skip security header added
614 1.38 drochner * beforehand.
615 1.38 drochner */
616 1.38 drochner return;
617 1.38 drochner
618 1.38 drochner case IPPROTO_HOPOPTS:
619 1.38 drochner case IPPROTO_DSTOPTS:
620 1.38 drochner case IPPROTO_ROUTING:
621 1.38 drochner /*
622 1.38 drochner * if we see 2nd destination option header,
623 1.38 drochner * we should stop there.
624 1.38 drochner */
625 1.38 drochner if (nxt == IPPROTO_DSTOPTS && dstopt)
626 1.38 drochner return;
627 1.38 drochner
628 1.38 drochner if (nxt == IPPROTO_DSTOPTS) {
629 1.38 drochner /*
630 1.38 drochner * seen 1st or 2nd destination option.
631 1.38 drochner * next time we see one, it must be 2nd.
632 1.38 drochner */
633 1.38 drochner dstopt = 1;
634 1.38 drochner } else if (nxt == IPPROTO_ROUTING) {
635 1.38 drochner /*
636 1.38 drochner * if we see destionation option next
637 1.38 drochner * time, it must be dest2.
638 1.38 drochner */
639 1.38 drochner dstopt = 2;
640 1.38 drochner }
641 1.38 drochner
642 1.38 drochner /* skip this header */
643 1.38 drochner m_copydata(m, *i, sizeof(ip6e), &ip6e);
644 1.38 drochner nxt = ip6e.ip6e_nxt;
645 1.38 drochner *off = *i + offsetof(struct ip6_ext, ip6e_nxt);
646 1.38 drochner /*
647 1.38 drochner * we will never see nxt == IPPROTO_AH
648 1.38 drochner * so it is safe to omit AH case.
649 1.38 drochner */
650 1.38 drochner *i += (ip6e.ip6e_len + 1) << 3;
651 1.38 drochner break;
652 1.38 drochner default:
653 1.38 drochner return;
654 1.38 drochner }
655 1.38 drochner } while (*i < m->m_pkthdr.len);
656 1.38 drochner }
657 1.38 drochner
658 1.36 drochner static int
659 1.36 drochner in6_sa_equal_addrwithscope(const struct sockaddr_in6 *sa, const struct in6_addr *ia)
660 1.36 drochner {
661 1.36 drochner struct in6_addr ia2;
662 1.36 drochner
663 1.36 drochner memcpy(&ia2, &sa->sin6_addr, sizeof(ia2));
664 1.36 drochner if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6_addr))
665 1.36 drochner ia2.s6_addr16[1] = htons(sa->sin6_scope_id);
666 1.36 drochner
667 1.36 drochner return IN6_ARE_ADDR_EQUAL(ia, &ia2);
668 1.36 drochner }
669 1.36 drochner
670 1.1 jonathan int
671 1.21 degroote ipsec6_process_packet(
672 1.21 degroote struct mbuf *m,
673 1.21 degroote struct ipsecrequest *isr
674 1.21 degroote )
675 1.1 jonathan {
676 1.21 degroote struct secasvar *sav;
677 1.21 degroote struct ip6_hdr *ip6;
678 1.35 drochner int s, error, i, off;
679 1.35 drochner union sockaddr_union *dst;
680 1.1 jonathan
681 1.44 ozaki KASSERT(m != NULL);
682 1.44 ozaki KASSERT(isr != NULL);
683 1.1 jonathan
684 1.21 degroote s = splsoftnet(); /* insure SA contents don't change */
685 1.34 drochner
686 1.52 ozaki isr = ipsec_nextisr(m, isr, AF_INET6, &error);
687 1.1 jonathan if (isr == NULL) {
688 1.26 degroote if (error != 0) {
689 1.34 drochner /* XXX Should we send a notification ? */
690 1.26 degroote goto bad;
691 1.26 degroote } else {
692 1.26 degroote if (ipsec_register_done(m, &error) < 0)
693 1.26 degroote goto bad;
694 1.26 degroote
695 1.26 degroote splx(s);
696 1.28 degroote return ipsec_reinject_ipstack(m, AF_INET6);
697 1.26 degroote }
698 1.1 jonathan }
699 1.1 jonathan
700 1.21 degroote sav = isr->sav;
701 1.35 drochner dst = &sav->sah->saidx.dst;
702 1.1 jonathan
703 1.35 drochner ip6 = mtod(m, struct ip6_hdr *); /* XXX */
704 1.1 jonathan
705 1.35 drochner /* Do the appropriate encapsulation, if necessary */
706 1.35 drochner if (isr->saidx.mode == IPSEC_MODE_TUNNEL || /* Tunnel requ'd */
707 1.35 drochner dst->sa.sa_family != AF_INET6 || /* PF mismatch */
708 1.35 drochner ((dst->sa.sa_family == AF_INET6) &&
709 1.35 drochner (!IN6_IS_ADDR_UNSPECIFIED(&dst->sin6.sin6_addr)) &&
710 1.36 drochner (!in6_sa_equal_addrwithscope(&dst->sin6,
711 1.35 drochner &ip6->ip6_dst)))) {
712 1.35 drochner struct mbuf *mp;
713 1.35 drochner
714 1.35 drochner /* Fix IPv6 header payload length. */
715 1.53 ozaki if (m->m_len < sizeof(struct ip6_hdr)) {
716 1.53 ozaki if ((m = m_pullup(m,sizeof(struct ip6_hdr))) == NULL) {
717 1.53 ozaki error = ENOBUFS;
718 1.53 ozaki goto bad;
719 1.53 ozaki }
720 1.53 ozaki }
721 1.34 drochner
722 1.35 drochner if (m->m_pkthdr.len - sizeof(*ip6) > IPV6_MAXPACKET) {
723 1.35 drochner /* No jumbogram support. */
724 1.53 ozaki error = ENXIO; /*XXX*/
725 1.53 ozaki goto bad;
726 1.35 drochner }
727 1.34 drochner
728 1.35 drochner ip6 = mtod(m, struct ip6_hdr *);
729 1.35 drochner ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6));
730 1.1 jonathan
731 1.35 drochner /* Encapsulate the packet */
732 1.35 drochner error = ipip_output(m, isr, &mp, 0, 0);
733 1.35 drochner if (mp == NULL && !error) {
734 1.35 drochner /* Should never happen. */
735 1.48 ozaki IPSECLOG(LOG_DEBUG,
736 1.48 ozaki "ipip_output returns no mbuf and no error!");
737 1.35 drochner error = EFAULT;
738 1.35 drochner }
739 1.35 drochner
740 1.35 drochner if (error) {
741 1.35 drochner if (mp) {
742 1.35 drochner /* XXX: Should never happen! */
743 1.35 drochner m_freem(mp);
744 1.21 degroote }
745 1.35 drochner m = NULL; /* ipip_output() already freed it */
746 1.35 drochner goto bad;
747 1.35 drochner }
748 1.35 drochner
749 1.35 drochner m = mp;
750 1.35 drochner mp = NULL;
751 1.35 drochner }
752 1.1 jonathan
753 1.35 drochner if (dst->sa.sa_family == AF_INET) {
754 1.35 drochner struct ip *ip;
755 1.35 drochner ip = mtod(m, struct ip *);
756 1.35 drochner i = ip->ip_hl << 2;
757 1.35 drochner off = offsetof(struct ip, ip_p);
758 1.35 drochner } else {
759 1.38 drochner compute_ipsec_pos(m, &i, &off);
760 1.34 drochner }
761 1.35 drochner error = (*sav->tdb_xform->xf_output)(m, isr, NULL, i, off);
762 1.34 drochner splx(s);
763 1.34 drochner return error;
764 1.1 jonathan bad:
765 1.21 degroote splx(s);
766 1.1 jonathan if (m)
767 1.1 jonathan m_freem(m);
768 1.1 jonathan return error;
769 1.1 jonathan }
770 1.1 jonathan #endif /*INET6*/
771