xform_ipip.c revision 1.60 1 1.60 maxv /* $NetBSD: xform_ipip.c,v 1.60 2018/02/15 10:09:53 maxv Exp $ */
2 1.1 jonathan /* $FreeBSD: src/sys/netipsec/xform_ipip.c,v 1.3.2.1 2003/01/24 05:11:36 sam Exp $ */
3 1.1 jonathan /* $OpenBSD: ip_ipip.c,v 1.25 2002/06/10 18:04:55 itojun Exp $ */
4 1.1 jonathan
5 1.1 jonathan /*
6 1.1 jonathan * The authors of this code are John Ioannidis (ji (at) tla.org),
7 1.1 jonathan * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
8 1.1 jonathan * Niels Provos (provos (at) physnet.uni-hamburg.de).
9 1.1 jonathan *
10 1.1 jonathan * The original version of this code was written by John Ioannidis
11 1.1 jonathan * for BSD/OS in Athens, Greece, in November 1995.
12 1.1 jonathan *
13 1.1 jonathan * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
14 1.1 jonathan * by Angelos D. Keromytis.
15 1.1 jonathan *
16 1.1 jonathan * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
17 1.1 jonathan * and Niels Provos.
18 1.1 jonathan *
19 1.1 jonathan * Additional features in 1999 by Angelos D. Keromytis.
20 1.1 jonathan *
21 1.1 jonathan * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
22 1.1 jonathan * Angelos D. Keromytis and Niels Provos.
23 1.1 jonathan * Copyright (c) 2001, Angelos D. Keromytis.
24 1.1 jonathan *
25 1.1 jonathan * Permission to use, copy, and modify this software with or without fee
26 1.1 jonathan * is hereby granted, provided that this entire notice is included in
27 1.1 jonathan * all copies of any software which is or includes a copy or
28 1.1 jonathan * modification of this software.
29 1.1 jonathan * You may use this code under the GNU public license if you so wish. Please
30 1.1 jonathan * contribute changes back to the authors under this freer than GPL license
31 1.1 jonathan * so that we may further the use of strong encryption without limitations to
32 1.1 jonathan * all.
33 1.1 jonathan *
34 1.1 jonathan * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
35 1.1 jonathan * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
36 1.1 jonathan * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
37 1.1 jonathan * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
38 1.1 jonathan * PURPOSE.
39 1.1 jonathan */
40 1.1 jonathan
41 1.1 jonathan #include <sys/cdefs.h>
42 1.60 maxv __KERNEL_RCSID(0, "$NetBSD: xform_ipip.c,v 1.60 2018/02/15 10:09:53 maxv Exp $");
43 1.1 jonathan
44 1.1 jonathan /*
45 1.1 jonathan * IP-inside-IP processing
46 1.1 jonathan */
47 1.43 ozaki #if defined(_KERNEL_OPT)
48 1.1 jonathan #include "opt_inet.h"
49 1.43 ozaki #endif
50 1.1 jonathan
51 1.1 jonathan #include <sys/param.h>
52 1.1 jonathan #include <sys/systm.h>
53 1.1 jonathan #include <sys/mbuf.h>
54 1.1 jonathan #include <sys/socket.h>
55 1.1 jonathan #include <sys/kernel.h>
56 1.35 riastrad #include <sys/protosw.h>
57 1.1 jonathan #include <sys/sysctl.h>
58 1.1 jonathan
59 1.1 jonathan #include <net/if.h>
60 1.1 jonathan #include <net/route.h>
61 1.1 jonathan #include <net/netisr.h>
62 1.1 jonathan
63 1.1 jonathan #include <netinet/in.h>
64 1.1 jonathan #include <netinet/in_systm.h>
65 1.1 jonathan #include <netinet/in_var.h>
66 1.1 jonathan #include <netinet/ip.h>
67 1.1 jonathan #include <netinet/ip_ecn.h>
68 1.1 jonathan #include <netinet/ip_var.h>
69 1.1 jonathan #include <netinet/ip_encap.h>
70 1.1 jonathan
71 1.1 jonathan #include <netipsec/ipsec.h>
72 1.22 thorpej #include <netipsec/ipsec_private.h>
73 1.1 jonathan #include <netipsec/xform.h>
74 1.1 jonathan
75 1.1 jonathan #include <netipsec/ipip_var.h>
76 1.1 jonathan
77 1.1 jonathan #ifdef MROUTING
78 1.1 jonathan #include <netinet/ip_mroute.h>
79 1.1 jonathan #endif
80 1.1 jonathan
81 1.1 jonathan #ifdef INET6
82 1.1 jonathan #include <netinet/ip6.h>
83 1.1 jonathan #include <netipsec/ipsec6.h>
84 1.1 jonathan #include <netinet6/in6_var.h>
85 1.35 riastrad #include <netinet6/ip6protosw.h>
86 1.1 jonathan #endif
87 1.1 jonathan
88 1.5 tls #include <netipsec/key.h>
89 1.5 tls #include <netipsec/key_debug.h>
90 1.1 jonathan
91 1.59 maxv /* XXX IPCOMP */
92 1.59 maxv #define M_IPSEC (M_AUTHIPHDR|M_AUTHIPDGM|M_DECRYPTED)
93 1.1 jonathan
94 1.59 maxv typedef void pr_in_input_t(struct mbuf *m, ...);
95 1.22 thorpej
96 1.59 maxv int ipip_allow = 0;
97 1.22 thorpej percpu_t *ipipstat_percpu;
98 1.1 jonathan
99 1.1 jonathan void ipe4_attach(void);
100 1.1 jonathan
101 1.1 jonathan static void _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp);
102 1.1 jonathan
103 1.1 jonathan #ifdef INET6
104 1.1 jonathan int
105 1.55 knakahar ip4_input6(struct mbuf **m, int *offp, int proto, void *eparg __unused)
106 1.1 jonathan {
107 1.1 jonathan _ipip_input(*m, *offp, NULL);
108 1.1 jonathan return IPPROTO_DONE;
109 1.1 jonathan }
110 1.59 maxv #endif
111 1.1 jonathan
112 1.1 jonathan #ifdef INET
113 1.1 jonathan void
114 1.55 knakahar ip4_input(struct mbuf *m, int off, int proto, void *eparg __unused)
115 1.1 jonathan {
116 1.37 knakahar _ipip_input(m, off, NULL);
117 1.1 jonathan }
118 1.59 maxv #endif
119 1.1 jonathan
120 1.1 jonathan /*
121 1.1 jonathan * ipip_input gets called when we receive an IP{46} encapsulated packet,
122 1.1 jonathan * either because we got it at a real interface, or because AH or ESP
123 1.1 jonathan * were being used in tunnel mode (in which case the rcvif element will
124 1.59 maxv * contain the address of the encX interface associated with the tunnel).
125 1.1 jonathan */
126 1.1 jonathan static void
127 1.14 christos _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp)
128 1.1 jonathan {
129 1.1 jonathan register struct sockaddr_in *sin;
130 1.1 jonathan register struct ifnet *ifp;
131 1.1 jonathan register struct ifaddr *ifa;
132 1.31 rmind pktqueue_t *pktq = NULL;
133 1.1 jonathan struct ip *ipo;
134 1.1 jonathan #ifdef INET6
135 1.1 jonathan register struct sockaddr_in6 *sin6;
136 1.1 jonathan struct ip6_hdr *ip6 = NULL;
137 1.45 christos uint8_t itos;
138 1.1 jonathan #endif
139 1.45 christos uint8_t otos;
140 1.45 christos uint8_t v;
141 1.1 jonathan int hlen;
142 1.1 jonathan
143 1.22 thorpej IPIP_STATINC(IPIP_STAT_IPACKETS);
144 1.1 jonathan
145 1.1 jonathan m_copydata(m, 0, 1, &v);
146 1.1 jonathan
147 1.1 jonathan switch (v >> 4) {
148 1.1 jonathan #ifdef INET
149 1.58 maxv case 4:
150 1.1 jonathan hlen = sizeof(struct ip);
151 1.1 jonathan break;
152 1.59 maxv #endif
153 1.1 jonathan #ifdef INET6
154 1.58 maxv case 6:
155 1.1 jonathan hlen = sizeof(struct ip6_hdr);
156 1.1 jonathan break;
157 1.1 jonathan #endif
158 1.58 maxv default:
159 1.45 christos DPRINTF(("%s: bad protocol version 0x%x (%u) "
160 1.45 christos "for outer header\n", __func__, v, v>>4));
161 1.22 thorpej IPIP_STATINC(IPIP_STAT_FAMILY);
162 1.1 jonathan m_freem(m);
163 1.59 maxv return;
164 1.1 jonathan }
165 1.1 jonathan
166 1.1 jonathan /* Bring the IP header in the first mbuf, if not there already */
167 1.1 jonathan if (m->m_len < hlen) {
168 1.1 jonathan if ((m = m_pullup(m, hlen)) == NULL) {
169 1.45 christos DPRINTF(("%s: m_pullup (1) failed\n", __func__));
170 1.22 thorpej IPIP_STATINC(IPIP_STAT_HDROPS);
171 1.1 jonathan return;
172 1.1 jonathan }
173 1.1 jonathan }
174 1.1 jonathan
175 1.1 jonathan ipo = mtod(m, struct ip *);
176 1.1 jonathan
177 1.1 jonathan #ifdef MROUTING
178 1.59 maxv /* XXX: DEAD AND BROKEN! */
179 1.1 jonathan if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) {
180 1.59 maxv if (IN_MULTICAST(((struct ip *)((char *)ipo + iphlen))->ip_dst.s_addr)) {
181 1.59 maxv ipip_mroute_input(m, iphlen);
182 1.1 jonathan return;
183 1.1 jonathan }
184 1.1 jonathan }
185 1.59 maxv #endif
186 1.1 jonathan
187 1.1 jonathan /* Keep outer ecn field. */
188 1.1 jonathan switch (v >> 4) {
189 1.1 jonathan #ifdef INET
190 1.1 jonathan case 4:
191 1.1 jonathan otos = ipo->ip_tos;
192 1.1 jonathan break;
193 1.59 maxv #endif
194 1.1 jonathan #ifdef INET6
195 1.1 jonathan case 6:
196 1.1 jonathan otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
197 1.1 jonathan break;
198 1.1 jonathan #endif
199 1.1 jonathan default:
200 1.45 christos panic("%s: unknown ip version %u (outer)", __func__, v >> 4);
201 1.1 jonathan }
202 1.1 jonathan
203 1.1 jonathan /* Remove outer IP header */
204 1.1 jonathan m_adj(m, iphlen);
205 1.1 jonathan
206 1.1 jonathan /* Sanity check */
207 1.1 jonathan if (m->m_pkthdr.len < sizeof(struct ip)) {
208 1.22 thorpej IPIP_STATINC(IPIP_STAT_HDROPS);
209 1.1 jonathan m_freem(m);
210 1.1 jonathan return;
211 1.1 jonathan }
212 1.1 jonathan
213 1.1 jonathan m_copydata(m, 0, 1, &v);
214 1.1 jonathan
215 1.1 jonathan switch (v >> 4) {
216 1.1 jonathan #ifdef INET
217 1.58 maxv case 4:
218 1.1 jonathan hlen = sizeof(struct ip);
219 1.1 jonathan break;
220 1.59 maxv #endif
221 1.1 jonathan #ifdef INET6
222 1.58 maxv case 6:
223 1.1 jonathan hlen = sizeof(struct ip6_hdr);
224 1.1 jonathan break;
225 1.1 jonathan #endif
226 1.1 jonathan default:
227 1.45 christos DPRINTF(("%s: bad protocol version %#x (%u) "
228 1.45 christos "for inner header\n", __func__, v, v >> 4));
229 1.22 thorpej IPIP_STATINC(IPIP_STAT_FAMILY);
230 1.1 jonathan m_freem(m);
231 1.59 maxv return;
232 1.1 jonathan }
233 1.1 jonathan
234 1.1 jonathan /*
235 1.1 jonathan * Bring the inner IP header in the first mbuf, if not there already.
236 1.1 jonathan */
237 1.1 jonathan if (m->m_len < hlen) {
238 1.1 jonathan if ((m = m_pullup(m, hlen)) == NULL) {
239 1.45 christos DPRINTF(("%s: m_pullup (2) failed\n", __func__));
240 1.22 thorpej IPIP_STATINC(IPIP_STAT_HDROPS);
241 1.1 jonathan return;
242 1.1 jonathan }
243 1.1 jonathan }
244 1.1 jonathan
245 1.1 jonathan /*
246 1.1 jonathan * RFC 1853 specifies that the inner TTL should not be touched on
247 1.1 jonathan * decapsulation. There's no reason this comment should be here, but
248 1.1 jonathan * this is as good as any a position.
249 1.1 jonathan */
250 1.1 jonathan
251 1.1 jonathan /* Some sanity checks in the inner IP header */
252 1.1 jonathan switch (v >> 4) {
253 1.1 jonathan #ifdef INET
254 1.58 maxv case 4:
255 1.58 maxv ipo = mtod(m, struct ip *);
256 1.1 jonathan ip_ecn_egress(ip4_ipsec_ecn, &otos, &ipo->ip_tos);
257 1.58 maxv break;
258 1.59 maxv #endif
259 1.1 jonathan #ifdef INET6
260 1.58 maxv case 6:
261 1.57 maxv ipo = NULL;
262 1.57 maxv ip6 = mtod(m, struct ip6_hdr *);
263 1.1 jonathan itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
264 1.1 jonathan ip_ecn_egress(ip6_ipsec_ecn, &otos, &itos);
265 1.1 jonathan ip6->ip6_flow &= ~htonl(0xff << 20);
266 1.45 christos ip6->ip6_flow |= htonl((uint32_t) itos << 20);
267 1.58 maxv break;
268 1.1 jonathan #endif
269 1.1 jonathan default:
270 1.45 christos panic("%s: unknown ip version %u (inner)", __func__, v>>4);
271 1.1 jonathan }
272 1.1 jonathan
273 1.1 jonathan /* Check for local address spoofing. */
274 1.39 ozaki if ((m_get_rcvif_NOMPSAFE(m) == NULL ||
275 1.39 ozaki !(m_get_rcvif_NOMPSAFE(m)->if_flags & IFF_LOOPBACK)) &&
276 1.1 jonathan ipip_allow != 2) {
277 1.38 ozaki int s = pserialize_read_enter();
278 1.38 ozaki IFNET_READER_FOREACH(ifp) {
279 1.42 ozaki IFADDR_READER_FOREACH(ifa, ifp) {
280 1.1 jonathan #ifdef INET
281 1.1 jonathan if (ipo) {
282 1.1 jonathan if (ifa->ifa_addr->sa_family !=
283 1.1 jonathan AF_INET)
284 1.1 jonathan continue;
285 1.1 jonathan
286 1.1 jonathan sin = (struct sockaddr_in *) ifa->ifa_addr;
287 1.1 jonathan
288 1.1 jonathan if (sin->sin_addr.s_addr ==
289 1.1 jonathan ipo->ip_src.s_addr) {
290 1.38 ozaki pserialize_read_exit(s);
291 1.22 thorpej IPIP_STATINC(IPIP_STAT_SPOOF);
292 1.1 jonathan m_freem(m);
293 1.1 jonathan return;
294 1.1 jonathan }
295 1.1 jonathan }
296 1.1 jonathan #endif /* INET */
297 1.1 jonathan
298 1.1 jonathan #ifdef INET6
299 1.1 jonathan if (ip6) {
300 1.1 jonathan if (ifa->ifa_addr->sa_family !=
301 1.1 jonathan AF_INET6)
302 1.1 jonathan continue;
303 1.1 jonathan
304 1.1 jonathan sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
305 1.1 jonathan
306 1.1 jonathan if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
307 1.38 ozaki pserialize_read_exit(s);
308 1.22 thorpej IPIP_STATINC(IPIP_STAT_SPOOF);
309 1.1 jonathan m_freem(m);
310 1.1 jonathan return;
311 1.1 jonathan }
312 1.1 jonathan
313 1.1 jonathan }
314 1.1 jonathan #endif /* INET6 */
315 1.1 jonathan }
316 1.1 jonathan }
317 1.38 ozaki pserialize_read_exit(s);
318 1.1 jonathan }
319 1.1 jonathan
320 1.1 jonathan /* Statistics */
321 1.22 thorpej IPIP_STATADD(IPIP_STAT_IBYTES, m->m_pkthdr.len - iphlen);
322 1.1 jonathan
323 1.1 jonathan /*
324 1.1 jonathan * Interface pointer stays the same; if no IPsec processing has
325 1.1 jonathan * been done (or will be done), this will point to a normal
326 1.1 jonathan * interface. Otherwise, it'll point to an enc interface, which
327 1.1 jonathan * will allow a packet filter to distinguish between secure and
328 1.1 jonathan * untrusted packets.
329 1.1 jonathan */
330 1.1 jonathan
331 1.1 jonathan switch (v >> 4) {
332 1.1 jonathan #ifdef INET
333 1.1 jonathan case 4:
334 1.31 rmind pktq = ip_pktq;
335 1.1 jonathan break;
336 1.1 jonathan #endif
337 1.1 jonathan #ifdef INET6
338 1.1 jonathan case 6:
339 1.31 rmind pktq = ip6_pktq;
340 1.1 jonathan break;
341 1.1 jonathan #endif
342 1.1 jonathan default:
343 1.45 christos panic("%s: should never reach here", __func__);
344 1.1 jonathan }
345 1.1 jonathan
346 1.31 rmind int s = splnet();
347 1.31 rmind if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
348 1.22 thorpej IPIP_STATINC(IPIP_STAT_QFULL);
349 1.31 rmind m_freem(m);
350 1.1 jonathan }
351 1.31 rmind splx(s);
352 1.1 jonathan }
353 1.1 jonathan
354 1.1 jonathan int
355 1.59 maxv ipip_output(struct mbuf *m, const struct ipsecrequest *isr,
356 1.59 maxv struct secasvar *sav, struct mbuf **mp, int skip, int protoff)
357 1.1 jonathan {
358 1.49 ryo char buf[IPSEC_ADDRSTRLEN];
359 1.45 christos uint8_t tp, otos;
360 1.1 jonathan struct secasindex *saidx;
361 1.60 maxv int error, iphlen;
362 1.1 jonathan #ifdef INET
363 1.45 christos uint8_t itos;
364 1.1 jonathan struct ip *ipo;
365 1.59 maxv #endif
366 1.1 jonathan #ifdef INET6
367 1.1 jonathan struct ip6_hdr *ip6, *ip6o;
368 1.59 maxv #endif
369 1.1 jonathan
370 1.45 christos IPSEC_SPLASSERT_SOFTNET(__func__);
371 1.53 ozaki KASSERT(sav != NULL);
372 1.1 jonathan
373 1.1 jonathan /* XXX Deal with empty TDB source/destination addresses. */
374 1.1 jonathan
375 1.1 jonathan m_copydata(m, 0, 1, &tp);
376 1.1 jonathan tp = (tp >> 4) & 0xff; /* Get the IP version number. */
377 1.1 jonathan
378 1.1 jonathan saidx = &sav->sah->saidx;
379 1.1 jonathan switch (saidx->dst.sa.sa_family) {
380 1.1 jonathan #ifdef INET
381 1.1 jonathan case AF_INET:
382 1.1 jonathan if (saidx->src.sa.sa_family != AF_INET ||
383 1.1 jonathan saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
384 1.1 jonathan saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
385 1.45 christos DPRINTF(("%s: unspecified tunnel endpoint "
386 1.45 christos "address in SA %s/%08lx\n", __func__,
387 1.49 ryo ipsec_address(&saidx->dst, buf, sizeof(buf)),
388 1.1 jonathan (u_long) ntohl(sav->spi)));
389 1.22 thorpej IPIP_STATINC(IPIP_STAT_UNSPEC);
390 1.1 jonathan error = EINVAL;
391 1.1 jonathan goto bad;
392 1.1 jonathan }
393 1.1 jonathan
394 1.1 jonathan M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
395 1.59 maxv if (m == NULL) {
396 1.45 christos DPRINTF(("%s: M_PREPEND failed\n", __func__));
397 1.22 thorpej IPIP_STATINC(IPIP_STAT_HDROPS);
398 1.1 jonathan error = ENOBUFS;
399 1.1 jonathan goto bad;
400 1.1 jonathan }
401 1.1 jonathan
402 1.60 maxv iphlen = sizeof(struct ip);
403 1.60 maxv
404 1.1 jonathan ipo = mtod(m, struct ip *);
405 1.1 jonathan ipo->ip_v = IPVERSION;
406 1.1 jonathan ipo->ip_hl = 5;
407 1.1 jonathan ipo->ip_len = htons(m->m_pkthdr.len);
408 1.1 jonathan ipo->ip_ttl = ip_defttl;
409 1.1 jonathan ipo->ip_sum = 0;
410 1.1 jonathan ipo->ip_src = saidx->src.sin.sin_addr;
411 1.1 jonathan ipo->ip_dst = saidx->dst.sin.sin_addr;
412 1.21 degroote ipo->ip_id = ip_newid(NULL);
413 1.1 jonathan
414 1.1 jonathan /* If the inner protocol is IP... */
415 1.1 jonathan if (tp == IPVERSION) {
416 1.1 jonathan /* Save ECN notification */
417 1.1 jonathan m_copydata(m, sizeof(struct ip) +
418 1.1 jonathan offsetof(struct ip, ip_tos),
419 1.45 christos sizeof(uint8_t), &itos);
420 1.1 jonathan
421 1.1 jonathan ipo->ip_p = IPPROTO_IPIP;
422 1.1 jonathan
423 1.1 jonathan /*
424 1.1 jonathan * We should be keeping tunnel soft-state and
425 1.1 jonathan * send back ICMPs if needed.
426 1.1 jonathan */
427 1.1 jonathan m_copydata(m, sizeof(struct ip) +
428 1.1 jonathan offsetof(struct ip, ip_off),
429 1.45 christos sizeof(uint16_t), &ipo->ip_off);
430 1.48 ozaki ipo->ip_off &= ~ htons(IP_DF | IP_MF | IP_OFFMASK);
431 1.1 jonathan }
432 1.1 jonathan #ifdef INET6
433 1.1 jonathan else if (tp == (IPV6_VERSION >> 4)) {
434 1.45 christos uint32_t itos32;
435 1.1 jonathan
436 1.1 jonathan /* Save ECN notification. */
437 1.1 jonathan m_copydata(m, sizeof(struct ip) +
438 1.1 jonathan offsetof(struct ip6_hdr, ip6_flow),
439 1.45 christos sizeof(uint32_t), &itos32);
440 1.1 jonathan itos = ntohl(itos32) >> 20;
441 1.1 jonathan ipo->ip_p = IPPROTO_IPV6;
442 1.1 jonathan ipo->ip_off = 0;
443 1.1 jonathan }
444 1.1 jonathan #endif /* INET6 */
445 1.1 jonathan else {
446 1.1 jonathan goto nofamily;
447 1.1 jonathan }
448 1.1 jonathan
449 1.1 jonathan otos = 0;
450 1.1 jonathan ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
451 1.1 jonathan ipo->ip_tos = otos;
452 1.1 jonathan break;
453 1.1 jonathan #endif /* INET */
454 1.1 jonathan
455 1.1 jonathan #ifdef INET6
456 1.1 jonathan case AF_INET6:
457 1.1 jonathan if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
458 1.1 jonathan saidx->src.sa.sa_family != AF_INET6 ||
459 1.1 jonathan IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
460 1.45 christos DPRINTF(("%s: unspecified tunnel endpoint "
461 1.45 christos "address in SA %s/%08lx\n", __func__,
462 1.49 ryo ipsec_address(&saidx->dst, buf, sizeof(buf)),
463 1.1 jonathan (u_long) ntohl(sav->spi)));
464 1.22 thorpej IPIP_STATINC(IPIP_STAT_UNSPEC);
465 1.1 jonathan error = ENOBUFS;
466 1.1 jonathan goto bad;
467 1.1 jonathan }
468 1.1 jonathan
469 1.27 drochner if (tp == (IPV6_VERSION >> 4)) {
470 1.27 drochner /* scoped address handling */
471 1.27 drochner ip6 = mtod(m, struct ip6_hdr *);
472 1.27 drochner if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
473 1.27 drochner ip6->ip6_src.s6_addr16[1] = 0;
474 1.27 drochner if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
475 1.27 drochner ip6->ip6_dst.s6_addr16[1] = 0;
476 1.27 drochner }
477 1.1 jonathan
478 1.1 jonathan M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
479 1.59 maxv if (m == NULL) {
480 1.45 christos DPRINTF(("%s: M_PREPEND failed\n", __func__));
481 1.22 thorpej IPIP_STATINC(IPIP_STAT_HDROPS);
482 1.1 jonathan error = ENOBUFS;
483 1.1 jonathan goto bad;
484 1.1 jonathan }
485 1.1 jonathan
486 1.60 maxv iphlen = sizeof(struct ip6_hdr);
487 1.60 maxv
488 1.1 jonathan /* Initialize IPv6 header */
489 1.1 jonathan ip6o = mtod(m, struct ip6_hdr *);
490 1.1 jonathan ip6o->ip6_flow = 0;
491 1.1 jonathan ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
492 1.1 jonathan ip6o->ip6_vfc |= IPV6_VERSION;
493 1.44 christos ip6o->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6o));
494 1.1 jonathan ip6o->ip6_hlim = ip_defttl;
495 1.1 jonathan ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
496 1.1 jonathan ip6o->ip6_src = saidx->src.sin6.sin6_addr;
497 1.27 drochner if (IN6_IS_SCOPE_LINKLOCAL(&ip6o->ip6_dst))
498 1.27 drochner ip6o->ip6_dst.s6_addr16[1] = htons(saidx->dst.sin6.sin6_scope_id);
499 1.27 drochner if (IN6_IS_SCOPE_LINKLOCAL(&ip6o->ip6_src))
500 1.27 drochner ip6o->ip6_src.s6_addr16[1] = htons(saidx->src.sin6.sin6_scope_id);
501 1.1 jonathan
502 1.1 jonathan #ifdef INET
503 1.1 jonathan if (tp == IPVERSION) {
504 1.1 jonathan /* Save ECN notification */
505 1.1 jonathan m_copydata(m, sizeof(struct ip6_hdr) +
506 1.45 christos offsetof(struct ip, ip_tos), sizeof(uint8_t),
507 1.17 degroote &itos);
508 1.1 jonathan
509 1.1 jonathan /* This is really IPVERSION. */
510 1.1 jonathan ip6o->ip6_nxt = IPPROTO_IPIP;
511 1.1 jonathan } else
512 1.1 jonathan #endif /* INET */
513 1.59 maxv if (tp == (IPV6_VERSION >> 4)) {
514 1.59 maxv uint32_t itos32;
515 1.59 maxv
516 1.59 maxv /* Save ECN notification. */
517 1.59 maxv m_copydata(m, sizeof(struct ip6_hdr) +
518 1.59 maxv offsetof(struct ip6_hdr, ip6_flow),
519 1.59 maxv sizeof(uint32_t), &itos32);
520 1.59 maxv itos = ntohl(itos32) >> 20;
521 1.1 jonathan
522 1.59 maxv ip6o->ip6_nxt = IPPROTO_IPV6;
523 1.59 maxv } else {
524 1.59 maxv goto nofamily;
525 1.59 maxv }
526 1.1 jonathan
527 1.1 jonathan otos = 0;
528 1.1 jonathan ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
529 1.45 christos ip6o->ip6_flow |= htonl((uint32_t) otos << 20);
530 1.1 jonathan break;
531 1.1 jonathan #endif /* INET6 */
532 1.1 jonathan
533 1.1 jonathan default:
534 1.1 jonathan nofamily:
535 1.45 christos DPRINTF(("%s: unsupported protocol family %u\n", __func__,
536 1.1 jonathan saidx->dst.sa.sa_family));
537 1.22 thorpej IPIP_STATINC(IPIP_STAT_FAMILY);
538 1.60 maxv error = EAFNOSUPPORT;
539 1.1 jonathan goto bad;
540 1.1 jonathan }
541 1.1 jonathan
542 1.22 thorpej IPIP_STATINC(IPIP_STAT_OPACKETS);
543 1.60 maxv IPIP_STATADD(IPIP_STAT_OBYTES, m->m_pkthdr.len - iphlen);
544 1.1 jonathan #if 0
545 1.60 maxv if (sav->tdb_xform->xf_type == XF_IP4)
546 1.60 maxv tdb->tdb_cur_bytes += m->m_pkthdr.len - iphlen;
547 1.1 jonathan #endif
548 1.1 jonathan
549 1.60 maxv *mp = m;
550 1.1 jonathan return 0;
551 1.59 maxv
552 1.1 jonathan bad:
553 1.1 jonathan if (m)
554 1.8 scw m_freem(m);
555 1.8 scw *mp = NULL;
556 1.59 maxv return error;
557 1.1 jonathan }
558 1.1 jonathan
559 1.1 jonathan static int
560 1.26 drochner ipe4_init(struct secasvar *sav, const struct xformsw *xsp)
561 1.1 jonathan {
562 1.1 jonathan sav->tdb_xform = xsp;
563 1.1 jonathan return 0;
564 1.1 jonathan }
565 1.1 jonathan
566 1.1 jonathan static int
567 1.1 jonathan ipe4_zeroize(struct secasvar *sav)
568 1.1 jonathan {
569 1.1 jonathan sav->tdb_xform = NULL;
570 1.1 jonathan return 0;
571 1.1 jonathan }
572 1.1 jonathan
573 1.1 jonathan static int
574 1.59 maxv ipe4_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
575 1.1 jonathan {
576 1.1 jonathan /* This is a rather serious mistake, so no conditional printing. */
577 1.45 christos printf("%s: should never be called\n", __func__);
578 1.1 jonathan if (m)
579 1.1 jonathan m_freem(m);
580 1.1 jonathan return EOPNOTSUPP;
581 1.1 jonathan }
582 1.1 jonathan
583 1.1 jonathan static struct xformsw ipe4_xformsw = {
584 1.50 ozaki .xf_type = XF_IP4,
585 1.50 ozaki .xf_flags = 0,
586 1.50 ozaki .xf_name = "IPv4 Simple Encapsulation",
587 1.50 ozaki .xf_init = ipe4_init,
588 1.50 ozaki .xf_zeroize = ipe4_zeroize,
589 1.50 ozaki .xf_input = ipe4_input,
590 1.50 ozaki .xf_output = ipip_output,
591 1.50 ozaki .xf_next = NULL,
592 1.1 jonathan };
593 1.1 jonathan
594 1.15 degroote #ifdef INET
595 1.36 knakahar static struct encapsw ipe4_encapsw = {
596 1.36 knakahar .encapsw4 = {
597 1.36 knakahar .pr_input = ip4_input,
598 1.36 knakahar .pr_ctlinput = NULL,
599 1.36 knakahar }
600 1.15 degroote };
601 1.15 degroote #endif
602 1.1 jonathan #ifdef INET6
603 1.36 knakahar static struct encapsw ipe4_encapsw6 = {
604 1.36 knakahar .encapsw6 = {
605 1.36 knakahar .pr_input = ip4_input6,
606 1.36 knakahar .pr_ctlinput = NULL,
607 1.36 knakahar }
608 1.15 degroote };
609 1.1 jonathan #endif
610 1.15 degroote
611 1.1 jonathan /*
612 1.1 jonathan * Check the encapsulated packet to see if we want it
613 1.1 jonathan */
614 1.1 jonathan static int
615 1.59 maxv ipe4_encapcheck(struct mbuf *m, int off, int proto, void *arg)
616 1.1 jonathan {
617 1.1 jonathan /*
618 1.1 jonathan * Only take packets coming from IPSEC tunnels; the rest
619 1.1 jonathan * must be handled by the gif tunnel code. Note that we
620 1.1 jonathan * also return a minimum priority when we want the packet
621 1.1 jonathan * so any explicit gif tunnels take precedence.
622 1.1 jonathan */
623 1.1 jonathan return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
624 1.1 jonathan }
625 1.1 jonathan
626 1.48 ozaki void
627 1.1 jonathan ipe4_attach(void)
628 1.1 jonathan {
629 1.22 thorpej
630 1.22 thorpej ipipstat_percpu = percpu_alloc(sizeof(uint64_t) * IPIP_NSTATS);
631 1.22 thorpej
632 1.1 jonathan xform_register(&ipe4_xformsw);
633 1.1 jonathan /* attach to encapsulation framework */
634 1.1 jonathan /* XXX save return cookie for detach on module remove */
635 1.41 knakahar
636 1.41 knakahar encapinit();
637 1.41 knakahar /* This function is called before ifinit(). Who else gets lock? */
638 1.41 knakahar (void)encap_lock_enter();
639 1.40 knakahar /* ipe4_encapsw and ipe4_encapsw must be added atomically */
640 1.15 degroote #ifdef INET
641 1.59 maxv (void)encap_attach_func(AF_INET, -1, ipe4_encapcheck, &ipe4_encapsw,
642 1.59 maxv NULL);
643 1.15 degroote #endif
644 1.1 jonathan #ifdef INET6
645 1.59 maxv (void)encap_attach_func(AF_INET6, -1, ipe4_encapcheck, &ipe4_encapsw6,
646 1.59 maxv NULL);
647 1.1 jonathan #endif
648 1.40 knakahar encap_lock_exit();
649 1.1 jonathan }
650