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