xform_ipip.c revision 1.52 1 1.52 ozaki /* $NetBSD: xform_ipip.c,v 1.52 2017/07/14 01:24:23 ozaki-r 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.52 ozaki __KERNEL_RCSID(0, "$NetBSD: xform_ipip.c,v 1.52 2017/07/14 01:24:23 ozaki-r 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.1 jonathan typedef void pr_in_input_t (struct mbuf *m, ...);
92 1.1 jonathan
93 1.1 jonathan /*
94 1.1 jonathan * We can control the acceptance of IP4 packets by altering the sysctl
95 1.1 jonathan * net.inet.ipip.allow value. Zero means drop them, all else is acceptance.
96 1.1 jonathan */
97 1.1 jonathan int ipip_allow = 0;
98 1.22 thorpej
99 1.22 thorpej percpu_t *ipipstat_percpu;
100 1.1 jonathan
101 1.1 jonathan #ifdef SYSCTL_DECL
102 1.1 jonathan SYSCTL_DECL(_net_inet_ipip);
103 1.1 jonathan
104 1.1 jonathan SYSCTL_INT(_net_inet_ipip, OID_AUTO,
105 1.1 jonathan ipip_allow, CTLFLAG_RW, &ipip_allow, 0, "");
106 1.1 jonathan SYSCTL_STRUCT(_net_inet_ipip, IPSECCTL_STATS,
107 1.1 jonathan stats, CTLFLAG_RD, &ipipstat, ipipstat, "");
108 1.1 jonathan
109 1.1 jonathan #endif
110 1.1 jonathan
111 1.1 jonathan void ipe4_attach(void);
112 1.1 jonathan
113 1.1 jonathan
114 1.1 jonathan /* XXX IPCOMP */
115 1.1 jonathan #define M_IPSEC (M_AUTHIPHDR|M_AUTHIPDGM|M_DECRYPTED)
116 1.1 jonathan
117 1.1 jonathan static void _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp);
118 1.1 jonathan
119 1.1 jonathan #ifdef INET6
120 1.1 jonathan /*
121 1.1 jonathan * Really only a wrapper for ipip_input(), for use with IPv6.
122 1.1 jonathan */
123 1.1 jonathan int
124 1.14 christos ip4_input6(struct mbuf **m, int *offp, int proto)
125 1.1 jonathan {
126 1.1 jonathan #if 0
127 1.1 jonathan /* If we do not accept IP-in-IP explicitly, drop. */
128 1.1 jonathan if (!ipip_allow && ((*m)->m_flags & M_IPSEC) == 0) {
129 1.45 christos DPRINTF(("%s: dropped due to policy\n", __func__));
130 1.22 thorpej IPIP_STATINC(IPIP_STAT_PDROPS);
131 1.1 jonathan m_freem(*m);
132 1.1 jonathan return IPPROTO_DONE;
133 1.1 jonathan }
134 1.1 jonathan #endif
135 1.1 jonathan _ipip_input(*m, *offp, NULL);
136 1.1 jonathan return IPPROTO_DONE;
137 1.1 jonathan }
138 1.1 jonathan #endif /* INET6 */
139 1.1 jonathan
140 1.1 jonathan #ifdef INET
141 1.1 jonathan /*
142 1.1 jonathan * Really only a wrapper for ipip_input(), for use with IPv4.
143 1.1 jonathan */
144 1.1 jonathan void
145 1.37 knakahar ip4_input(struct mbuf *m, int off, int proto)
146 1.1 jonathan {
147 1.1 jonathan
148 1.1 jonathan #if 0
149 1.1 jonathan /* If we do not accept IP-in-IP explicitly, drop. */
150 1.1 jonathan if (!ipip_allow && (m->m_flags & M_IPSEC) == 0) {
151 1.45 christos DPRINTF(("%s: dropped due to policy\n", __func__));
152 1.22 thorpej IPIP_STATINC(IPIP_STAT_PDROPS);
153 1.1 jonathan m_freem(m);
154 1.1 jonathan return;
155 1.1 jonathan }
156 1.1 jonathan #endif
157 1.1 jonathan
158 1.37 knakahar _ipip_input(m, off, NULL);
159 1.1 jonathan }
160 1.1 jonathan #endif /* INET */
161 1.1 jonathan
162 1.1 jonathan /*
163 1.1 jonathan * ipip_input gets called when we receive an IP{46} encapsulated packet,
164 1.1 jonathan * either because we got it at a real interface, or because AH or ESP
165 1.1 jonathan * were being used in tunnel mode (in which case the rcvif element will
166 1.1 jonathan * contain the address of the encX interface associated with the tunnel.
167 1.1 jonathan */
168 1.1 jonathan
169 1.1 jonathan static void
170 1.14 christos _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp)
171 1.1 jonathan {
172 1.1 jonathan register struct sockaddr_in *sin;
173 1.1 jonathan register struct ifnet *ifp;
174 1.1 jonathan register struct ifaddr *ifa;
175 1.31 rmind pktqueue_t *pktq = NULL;
176 1.1 jonathan struct ip *ipo;
177 1.1 jonathan #ifdef INET6
178 1.1 jonathan register struct sockaddr_in6 *sin6;
179 1.1 jonathan struct ip6_hdr *ip6 = NULL;
180 1.45 christos uint8_t itos;
181 1.1 jonathan #endif
182 1.45 christos uint8_t otos;
183 1.45 christos uint8_t v;
184 1.1 jonathan int hlen;
185 1.1 jonathan
186 1.22 thorpej IPIP_STATINC(IPIP_STAT_IPACKETS);
187 1.1 jonathan
188 1.1 jonathan m_copydata(m, 0, 1, &v);
189 1.1 jonathan
190 1.1 jonathan switch (v >> 4) {
191 1.1 jonathan #ifdef INET
192 1.1 jonathan case 4:
193 1.1 jonathan hlen = sizeof(struct ip);
194 1.1 jonathan break;
195 1.1 jonathan #endif /* INET */
196 1.1 jonathan #ifdef INET6
197 1.1 jonathan case 6:
198 1.1 jonathan hlen = sizeof(struct ip6_hdr);
199 1.1 jonathan break;
200 1.1 jonathan #endif
201 1.1 jonathan default:
202 1.45 christos DPRINTF(("%s: bad protocol version 0x%x (%u) "
203 1.45 christos "for outer header\n", __func__, v, v>>4));
204 1.22 thorpej IPIP_STATINC(IPIP_STAT_FAMILY);
205 1.1 jonathan m_freem(m);
206 1.1 jonathan return /* EAFNOSUPPORT */;
207 1.1 jonathan }
208 1.1 jonathan
209 1.1 jonathan /* Bring the IP header in the first mbuf, if not there already */
210 1.1 jonathan if (m->m_len < hlen) {
211 1.1 jonathan if ((m = m_pullup(m, hlen)) == NULL) {
212 1.45 christos DPRINTF(("%s: m_pullup (1) failed\n", __func__));
213 1.22 thorpej IPIP_STATINC(IPIP_STAT_HDROPS);
214 1.1 jonathan return;
215 1.1 jonathan }
216 1.1 jonathan }
217 1.1 jonathan
218 1.1 jonathan ipo = mtod(m, struct ip *);
219 1.1 jonathan
220 1.1 jonathan #ifdef MROUTING
221 1.1 jonathan if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) {
222 1.1 jonathan if (IN_MULTICAST(((struct ip *)((char *) ipo + iphlen))->ip_dst.s_addr)) {
223 1.1 jonathan ipip_mroute_input (m, iphlen);
224 1.1 jonathan return;
225 1.1 jonathan }
226 1.1 jonathan }
227 1.1 jonathan #endif /* MROUTING */
228 1.1 jonathan
229 1.1 jonathan /* Keep outer ecn field. */
230 1.1 jonathan switch (v >> 4) {
231 1.1 jonathan #ifdef INET
232 1.1 jonathan case 4:
233 1.1 jonathan otos = ipo->ip_tos;
234 1.1 jonathan break;
235 1.1 jonathan #endif /* INET */
236 1.1 jonathan #ifdef INET6
237 1.1 jonathan case 6:
238 1.1 jonathan otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff;
239 1.1 jonathan break;
240 1.1 jonathan #endif
241 1.1 jonathan default:
242 1.45 christos panic("%s: unknown ip version %u (outer)", __func__, v >> 4);
243 1.1 jonathan }
244 1.1 jonathan
245 1.1 jonathan /* Remove outer IP header */
246 1.1 jonathan m_adj(m, iphlen);
247 1.1 jonathan
248 1.1 jonathan /* Sanity check */
249 1.1 jonathan if (m->m_pkthdr.len < sizeof(struct ip)) {
250 1.22 thorpej IPIP_STATINC(IPIP_STAT_HDROPS);
251 1.1 jonathan m_freem(m);
252 1.1 jonathan return;
253 1.1 jonathan }
254 1.1 jonathan
255 1.1 jonathan m_copydata(m, 0, 1, &v);
256 1.1 jonathan
257 1.1 jonathan switch (v >> 4) {
258 1.1 jonathan #ifdef INET
259 1.1 jonathan case 4:
260 1.1 jonathan hlen = sizeof(struct ip);
261 1.1 jonathan break;
262 1.1 jonathan #endif /* INET */
263 1.1 jonathan
264 1.1 jonathan #ifdef INET6
265 1.1 jonathan case 6:
266 1.1 jonathan hlen = sizeof(struct ip6_hdr);
267 1.1 jonathan break;
268 1.1 jonathan #endif
269 1.1 jonathan default:
270 1.45 christos DPRINTF(("%s: bad protocol version %#x (%u) "
271 1.45 christos "for inner header\n", __func__, v, v >> 4));
272 1.22 thorpej IPIP_STATINC(IPIP_STAT_FAMILY);
273 1.1 jonathan m_freem(m);
274 1.1 jonathan return; /* EAFNOSUPPORT */
275 1.1 jonathan }
276 1.1 jonathan
277 1.1 jonathan /*
278 1.1 jonathan * Bring the inner IP header in the first mbuf, if not there already.
279 1.1 jonathan */
280 1.1 jonathan if (m->m_len < hlen) {
281 1.1 jonathan if ((m = m_pullup(m, hlen)) == NULL) {
282 1.45 christos DPRINTF(("%s: m_pullup (2) failed\n", __func__));
283 1.22 thorpej IPIP_STATINC(IPIP_STAT_HDROPS);
284 1.1 jonathan return;
285 1.1 jonathan }
286 1.1 jonathan }
287 1.1 jonathan
288 1.1 jonathan /*
289 1.1 jonathan * RFC 1853 specifies that the inner TTL should not be touched on
290 1.1 jonathan * decapsulation. There's no reason this comment should be here, but
291 1.1 jonathan * this is as good as any a position.
292 1.1 jonathan */
293 1.1 jonathan
294 1.1 jonathan /* Some sanity checks in the inner IP header */
295 1.1 jonathan switch (v >> 4) {
296 1.1 jonathan #ifdef INET
297 1.1 jonathan case 4:
298 1.1 jonathan ipo = mtod(m, struct ip *);
299 1.1 jonathan ip_ecn_egress(ip4_ipsec_ecn, &otos, &ipo->ip_tos);
300 1.1 jonathan break;
301 1.1 jonathan #endif /* INET */
302 1.1 jonathan #ifdef INET6
303 1.1 jonathan case 6:
304 1.1 jonathan ip6 = (struct ip6_hdr *) ipo;
305 1.1 jonathan itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
306 1.1 jonathan ip_ecn_egress(ip6_ipsec_ecn, &otos, &itos);
307 1.1 jonathan ip6->ip6_flow &= ~htonl(0xff << 20);
308 1.45 christos ip6->ip6_flow |= htonl((uint32_t) itos << 20);
309 1.1 jonathan break;
310 1.1 jonathan #endif
311 1.1 jonathan default:
312 1.45 christos panic("%s: unknown ip version %u (inner)", __func__, v>>4);
313 1.1 jonathan }
314 1.1 jonathan
315 1.1 jonathan /* Check for local address spoofing. */
316 1.39 ozaki if ((m_get_rcvif_NOMPSAFE(m) == NULL ||
317 1.39 ozaki !(m_get_rcvif_NOMPSAFE(m)->if_flags & IFF_LOOPBACK)) &&
318 1.1 jonathan ipip_allow != 2) {
319 1.38 ozaki int s = pserialize_read_enter();
320 1.38 ozaki IFNET_READER_FOREACH(ifp) {
321 1.42 ozaki IFADDR_READER_FOREACH(ifa, ifp) {
322 1.1 jonathan #ifdef INET
323 1.1 jonathan if (ipo) {
324 1.1 jonathan if (ifa->ifa_addr->sa_family !=
325 1.1 jonathan AF_INET)
326 1.1 jonathan continue;
327 1.1 jonathan
328 1.1 jonathan sin = (struct sockaddr_in *) ifa->ifa_addr;
329 1.1 jonathan
330 1.1 jonathan if (sin->sin_addr.s_addr ==
331 1.1 jonathan ipo->ip_src.s_addr) {
332 1.38 ozaki pserialize_read_exit(s);
333 1.22 thorpej IPIP_STATINC(IPIP_STAT_SPOOF);
334 1.1 jonathan m_freem(m);
335 1.1 jonathan return;
336 1.1 jonathan }
337 1.1 jonathan }
338 1.1 jonathan #endif /* INET */
339 1.1 jonathan
340 1.1 jonathan #ifdef INET6
341 1.1 jonathan if (ip6) {
342 1.1 jonathan if (ifa->ifa_addr->sa_family !=
343 1.1 jonathan AF_INET6)
344 1.1 jonathan continue;
345 1.1 jonathan
346 1.1 jonathan sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
347 1.1 jonathan
348 1.1 jonathan if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) {
349 1.38 ozaki pserialize_read_exit(s);
350 1.22 thorpej IPIP_STATINC(IPIP_STAT_SPOOF);
351 1.1 jonathan m_freem(m);
352 1.1 jonathan return;
353 1.1 jonathan }
354 1.1 jonathan
355 1.1 jonathan }
356 1.1 jonathan #endif /* INET6 */
357 1.1 jonathan }
358 1.1 jonathan }
359 1.38 ozaki pserialize_read_exit(s);
360 1.1 jonathan }
361 1.1 jonathan
362 1.1 jonathan /* Statistics */
363 1.22 thorpej IPIP_STATADD(IPIP_STAT_IBYTES, m->m_pkthdr.len - iphlen);
364 1.1 jonathan
365 1.1 jonathan /*
366 1.1 jonathan * Interface pointer stays the same; if no IPsec processing has
367 1.1 jonathan * been done (or will be done), this will point to a normal
368 1.1 jonathan * interface. Otherwise, it'll point to an enc interface, which
369 1.1 jonathan * will allow a packet filter to distinguish between secure and
370 1.1 jonathan * untrusted packets.
371 1.1 jonathan */
372 1.1 jonathan
373 1.1 jonathan switch (v >> 4) {
374 1.1 jonathan #ifdef INET
375 1.1 jonathan case 4:
376 1.31 rmind pktq = ip_pktq;
377 1.1 jonathan break;
378 1.1 jonathan #endif
379 1.1 jonathan #ifdef INET6
380 1.1 jonathan case 6:
381 1.31 rmind pktq = ip6_pktq;
382 1.1 jonathan break;
383 1.1 jonathan #endif
384 1.1 jonathan default:
385 1.45 christos panic("%s: should never reach here", __func__);
386 1.1 jonathan }
387 1.1 jonathan
388 1.31 rmind int s = splnet();
389 1.31 rmind if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
390 1.22 thorpej IPIP_STATINC(IPIP_STAT_QFULL);
391 1.31 rmind m_freem(m);
392 1.1 jonathan }
393 1.31 rmind splx(s);
394 1.1 jonathan }
395 1.1 jonathan
396 1.1 jonathan int
397 1.1 jonathan ipip_output(
398 1.13 christos struct mbuf *m,
399 1.13 christos struct ipsecrequest *isr,
400 1.13 christos struct mbuf **mp,
401 1.14 christos int skip,
402 1.14 christos int protoff
403 1.1 jonathan )
404 1.1 jonathan {
405 1.49 ryo char buf[IPSEC_ADDRSTRLEN];
406 1.52 ozaki struct secasvar *sav;
407 1.45 christos uint8_t tp, otos;
408 1.1 jonathan struct secasindex *saidx;
409 1.1 jonathan int error;
410 1.1 jonathan #ifdef INET
411 1.45 christos uint8_t itos;
412 1.1 jonathan struct ip *ipo;
413 1.1 jonathan #endif /* INET */
414 1.1 jonathan #ifdef INET6
415 1.1 jonathan struct ip6_hdr *ip6, *ip6o;
416 1.1 jonathan #endif /* INET6 */
417 1.1 jonathan
418 1.45 christos IPSEC_SPLASSERT_SOFTNET(__func__);
419 1.1 jonathan
420 1.47 ozaki KASSERT(isr->sav != NULL);
421 1.1 jonathan sav = isr->sav;
422 1.1 jonathan
423 1.1 jonathan /* XXX Deal with empty TDB source/destination addresses. */
424 1.1 jonathan
425 1.1 jonathan m_copydata(m, 0, 1, &tp);
426 1.1 jonathan tp = (tp >> 4) & 0xff; /* Get the IP version number. */
427 1.1 jonathan
428 1.1 jonathan saidx = &sav->sah->saidx;
429 1.1 jonathan switch (saidx->dst.sa.sa_family) {
430 1.1 jonathan #ifdef INET
431 1.1 jonathan case AF_INET:
432 1.1 jonathan if (saidx->src.sa.sa_family != AF_INET ||
433 1.1 jonathan saidx->src.sin.sin_addr.s_addr == INADDR_ANY ||
434 1.1 jonathan saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) {
435 1.45 christos DPRINTF(("%s: unspecified tunnel endpoint "
436 1.45 christos "address in SA %s/%08lx\n", __func__,
437 1.49 ryo ipsec_address(&saidx->dst, buf, sizeof(buf)),
438 1.1 jonathan (u_long) ntohl(sav->spi)));
439 1.22 thorpej IPIP_STATINC(IPIP_STAT_UNSPEC);
440 1.1 jonathan error = EINVAL;
441 1.1 jonathan goto bad;
442 1.1 jonathan }
443 1.1 jonathan
444 1.1 jonathan M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
445 1.1 jonathan if (m == 0) {
446 1.45 christos DPRINTF(("%s: M_PREPEND failed\n", __func__));
447 1.22 thorpej IPIP_STATINC(IPIP_STAT_HDROPS);
448 1.1 jonathan error = ENOBUFS;
449 1.1 jonathan goto bad;
450 1.1 jonathan }
451 1.1 jonathan
452 1.1 jonathan ipo = mtod(m, struct ip *);
453 1.1 jonathan
454 1.1 jonathan ipo->ip_v = IPVERSION;
455 1.1 jonathan ipo->ip_hl = 5;
456 1.1 jonathan ipo->ip_len = htons(m->m_pkthdr.len);
457 1.1 jonathan ipo->ip_ttl = ip_defttl;
458 1.1 jonathan ipo->ip_sum = 0;
459 1.1 jonathan ipo->ip_src = saidx->src.sin.sin_addr;
460 1.1 jonathan ipo->ip_dst = saidx->dst.sin.sin_addr;
461 1.21 degroote ipo->ip_id = ip_newid(NULL);
462 1.1 jonathan
463 1.1 jonathan /* If the inner protocol is IP... */
464 1.1 jonathan if (tp == IPVERSION) {
465 1.1 jonathan /* Save ECN notification */
466 1.1 jonathan m_copydata(m, sizeof(struct ip) +
467 1.1 jonathan offsetof(struct ip, ip_tos),
468 1.45 christos sizeof(uint8_t), &itos);
469 1.1 jonathan
470 1.1 jonathan ipo->ip_p = IPPROTO_IPIP;
471 1.1 jonathan
472 1.1 jonathan /*
473 1.1 jonathan * We should be keeping tunnel soft-state and
474 1.1 jonathan * send back ICMPs if needed.
475 1.1 jonathan */
476 1.1 jonathan m_copydata(m, sizeof(struct ip) +
477 1.1 jonathan offsetof(struct ip, ip_off),
478 1.45 christos sizeof(uint16_t), &ipo->ip_off);
479 1.48 ozaki ipo->ip_off &= ~ htons(IP_DF | IP_MF | IP_OFFMASK);
480 1.1 jonathan }
481 1.1 jonathan #ifdef INET6
482 1.1 jonathan else if (tp == (IPV6_VERSION >> 4)) {
483 1.45 christos uint32_t itos32;
484 1.1 jonathan
485 1.1 jonathan /* Save ECN notification. */
486 1.1 jonathan m_copydata(m, sizeof(struct ip) +
487 1.1 jonathan offsetof(struct ip6_hdr, ip6_flow),
488 1.45 christos sizeof(uint32_t), &itos32);
489 1.1 jonathan itos = ntohl(itos32) >> 20;
490 1.1 jonathan ipo->ip_p = IPPROTO_IPV6;
491 1.1 jonathan ipo->ip_off = 0;
492 1.1 jonathan }
493 1.1 jonathan #endif /* INET6 */
494 1.1 jonathan else {
495 1.1 jonathan goto nofamily;
496 1.1 jonathan }
497 1.1 jonathan
498 1.1 jonathan otos = 0;
499 1.1 jonathan ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
500 1.1 jonathan ipo->ip_tos = otos;
501 1.1 jonathan break;
502 1.1 jonathan #endif /* INET */
503 1.1 jonathan
504 1.1 jonathan #ifdef INET6
505 1.1 jonathan case AF_INET6:
506 1.1 jonathan if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) ||
507 1.1 jonathan saidx->src.sa.sa_family != AF_INET6 ||
508 1.1 jonathan IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) {
509 1.45 christos DPRINTF(("%s: unspecified tunnel endpoint "
510 1.45 christos "address in SA %s/%08lx\n", __func__,
511 1.49 ryo ipsec_address(&saidx->dst, buf, sizeof(buf)),
512 1.1 jonathan (u_long) ntohl(sav->spi)));
513 1.22 thorpej IPIP_STATINC(IPIP_STAT_UNSPEC);
514 1.1 jonathan error = ENOBUFS;
515 1.1 jonathan goto bad;
516 1.1 jonathan }
517 1.1 jonathan
518 1.27 drochner if (tp == (IPV6_VERSION >> 4)) {
519 1.27 drochner /* scoped address handling */
520 1.27 drochner ip6 = mtod(m, struct ip6_hdr *);
521 1.27 drochner if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
522 1.27 drochner ip6->ip6_src.s6_addr16[1] = 0;
523 1.27 drochner if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
524 1.27 drochner ip6->ip6_dst.s6_addr16[1] = 0;
525 1.27 drochner }
526 1.1 jonathan
527 1.1 jonathan M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
528 1.1 jonathan if (m == 0) {
529 1.45 christos DPRINTF(("%s: M_PREPEND failed\n", __func__));
530 1.22 thorpej IPIP_STATINC(IPIP_STAT_HDROPS);
531 1.1 jonathan error = ENOBUFS;
532 1.1 jonathan goto bad;
533 1.1 jonathan }
534 1.1 jonathan
535 1.1 jonathan /* Initialize IPv6 header */
536 1.1 jonathan ip6o = mtod(m, struct ip6_hdr *);
537 1.1 jonathan ip6o->ip6_flow = 0;
538 1.1 jonathan ip6o->ip6_vfc &= ~IPV6_VERSION_MASK;
539 1.1 jonathan ip6o->ip6_vfc |= IPV6_VERSION;
540 1.44 christos ip6o->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6o));
541 1.1 jonathan ip6o->ip6_hlim = ip_defttl;
542 1.1 jonathan ip6o->ip6_dst = saidx->dst.sin6.sin6_addr;
543 1.1 jonathan ip6o->ip6_src = saidx->src.sin6.sin6_addr;
544 1.27 drochner if (IN6_IS_SCOPE_LINKLOCAL(&ip6o->ip6_dst))
545 1.27 drochner ip6o->ip6_dst.s6_addr16[1] = htons(saidx->dst.sin6.sin6_scope_id);
546 1.27 drochner if (IN6_IS_SCOPE_LINKLOCAL(&ip6o->ip6_src))
547 1.27 drochner ip6o->ip6_src.s6_addr16[1] = htons(saidx->src.sin6.sin6_scope_id);
548 1.1 jonathan
549 1.1 jonathan #ifdef INET
550 1.1 jonathan if (tp == IPVERSION) {
551 1.1 jonathan /* Save ECN notification */
552 1.1 jonathan m_copydata(m, sizeof(struct ip6_hdr) +
553 1.45 christos offsetof(struct ip, ip_tos), sizeof(uint8_t),
554 1.17 degroote &itos);
555 1.1 jonathan
556 1.1 jonathan /* This is really IPVERSION. */
557 1.1 jonathan ip6o->ip6_nxt = IPPROTO_IPIP;
558 1.1 jonathan } else
559 1.1 jonathan #endif /* INET */
560 1.1 jonathan if (tp == (IPV6_VERSION >> 4)) {
561 1.45 christos uint32_t itos32;
562 1.1 jonathan
563 1.1 jonathan /* Save ECN notification. */
564 1.1 jonathan m_copydata(m, sizeof(struct ip6_hdr) +
565 1.1 jonathan offsetof(struct ip6_hdr, ip6_flow),
566 1.45 christos sizeof(uint32_t), &itos32);
567 1.1 jonathan itos = ntohl(itos32) >> 20;
568 1.1 jonathan
569 1.1 jonathan ip6o->ip6_nxt = IPPROTO_IPV6;
570 1.1 jonathan } else {
571 1.1 jonathan goto nofamily;
572 1.1 jonathan }
573 1.1 jonathan
574 1.1 jonathan otos = 0;
575 1.1 jonathan ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
576 1.45 christos ip6o->ip6_flow |= htonl((uint32_t) otos << 20);
577 1.1 jonathan break;
578 1.1 jonathan #endif /* INET6 */
579 1.1 jonathan
580 1.1 jonathan default:
581 1.1 jonathan nofamily:
582 1.45 christos DPRINTF(("%s: unsupported protocol family %u\n", __func__,
583 1.1 jonathan saidx->dst.sa.sa_family));
584 1.22 thorpej IPIP_STATINC(IPIP_STAT_FAMILY);
585 1.1 jonathan error = EAFNOSUPPORT; /* XXX diffs from openbsd */
586 1.1 jonathan goto bad;
587 1.1 jonathan }
588 1.1 jonathan
589 1.22 thorpej IPIP_STATINC(IPIP_STAT_OPACKETS);
590 1.1 jonathan *mp = m;
591 1.1 jonathan
592 1.1 jonathan #ifdef INET
593 1.1 jonathan if (saidx->dst.sa.sa_family == AF_INET) {
594 1.1 jonathan #if 0
595 1.1 jonathan if (sav->tdb_xform->xf_type == XF_IP4)
596 1.1 jonathan tdb->tdb_cur_bytes +=
597 1.1 jonathan m->m_pkthdr.len - sizeof(struct ip);
598 1.1 jonathan #endif
599 1.22 thorpej IPIP_STATADD(IPIP_STAT_OBYTES,
600 1.22 thorpej m->m_pkthdr.len - sizeof(struct ip));
601 1.1 jonathan }
602 1.1 jonathan #endif /* INET */
603 1.1 jonathan
604 1.1 jonathan #ifdef INET6
605 1.1 jonathan if (saidx->dst.sa.sa_family == AF_INET6) {
606 1.1 jonathan #if 0
607 1.1 jonathan if (sav->tdb_xform->xf_type == XF_IP4)
608 1.1 jonathan tdb->tdb_cur_bytes +=
609 1.1 jonathan m->m_pkthdr.len - sizeof(struct ip6_hdr);
610 1.1 jonathan #endif
611 1.27 drochner IPIP_STATADD(IPIP_STAT_OBYTES,
612 1.22 thorpej m->m_pkthdr.len - sizeof(struct ip6_hdr));
613 1.1 jonathan }
614 1.1 jonathan #endif /* INET6 */
615 1.1 jonathan
616 1.1 jonathan return 0;
617 1.1 jonathan bad:
618 1.1 jonathan if (m)
619 1.8 scw m_freem(m);
620 1.8 scw *mp = NULL;
621 1.1 jonathan return (error);
622 1.1 jonathan }
623 1.1 jonathan
624 1.1 jonathan static int
625 1.26 drochner ipe4_init(struct secasvar *sav, const struct xformsw *xsp)
626 1.1 jonathan {
627 1.1 jonathan sav->tdb_xform = xsp;
628 1.1 jonathan return 0;
629 1.1 jonathan }
630 1.1 jonathan
631 1.1 jonathan static int
632 1.1 jonathan ipe4_zeroize(struct secasvar *sav)
633 1.1 jonathan {
634 1.1 jonathan sav->tdb_xform = NULL;
635 1.1 jonathan return 0;
636 1.1 jonathan }
637 1.1 jonathan
638 1.1 jonathan static int
639 1.13 christos ipe4_input(
640 1.13 christos struct mbuf *m,
641 1.52 ozaki struct secasvar *sav,
642 1.14 christos int skip,
643 1.14 christos int protoff
644 1.13 christos )
645 1.1 jonathan {
646 1.1 jonathan /* This is a rather serious mistake, so no conditional printing. */
647 1.45 christos printf("%s: should never be called\n", __func__);
648 1.1 jonathan if (m)
649 1.1 jonathan m_freem(m);
650 1.1 jonathan return EOPNOTSUPP;
651 1.1 jonathan }
652 1.1 jonathan
653 1.1 jonathan static struct xformsw ipe4_xformsw = {
654 1.50 ozaki .xf_type = XF_IP4,
655 1.50 ozaki .xf_flags = 0,
656 1.50 ozaki .xf_name = "IPv4 Simple Encapsulation",
657 1.50 ozaki .xf_init = ipe4_init,
658 1.50 ozaki .xf_zeroize = ipe4_zeroize,
659 1.50 ozaki .xf_input = ipe4_input,
660 1.50 ozaki .xf_output = ipip_output,
661 1.50 ozaki .xf_next = NULL,
662 1.1 jonathan };
663 1.1 jonathan
664 1.15 degroote #ifdef INET
665 1.36 knakahar static struct encapsw ipe4_encapsw = {
666 1.36 knakahar .encapsw4 = {
667 1.36 knakahar .pr_input = ip4_input,
668 1.36 knakahar .pr_ctlinput = NULL,
669 1.36 knakahar }
670 1.15 degroote };
671 1.15 degroote #endif
672 1.1 jonathan #ifdef INET6
673 1.36 knakahar static struct encapsw ipe4_encapsw6 = {
674 1.36 knakahar .encapsw6 = {
675 1.36 knakahar .pr_input = ip4_input6,
676 1.36 knakahar .pr_ctlinput = NULL,
677 1.36 knakahar }
678 1.15 degroote };
679 1.1 jonathan #endif
680 1.15 degroote
681 1.1 jonathan /*
682 1.1 jonathan * Check the encapsulated packet to see if we want it
683 1.1 jonathan */
684 1.1 jonathan static int
685 1.13 christos ipe4_encapcheck(struct mbuf *m,
686 1.14 christos int off,
687 1.14 christos int proto,
688 1.14 christos void *arg
689 1.13 christos )
690 1.1 jonathan {
691 1.1 jonathan /*
692 1.1 jonathan * Only take packets coming from IPSEC tunnels; the rest
693 1.1 jonathan * must be handled by the gif tunnel code. Note that we
694 1.1 jonathan * also return a minimum priority when we want the packet
695 1.1 jonathan * so any explicit gif tunnels take precedence.
696 1.1 jonathan */
697 1.1 jonathan return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0);
698 1.1 jonathan }
699 1.1 jonathan
700 1.48 ozaki void
701 1.1 jonathan ipe4_attach(void)
702 1.1 jonathan {
703 1.22 thorpej
704 1.22 thorpej ipipstat_percpu = percpu_alloc(sizeof(uint64_t) * IPIP_NSTATS);
705 1.22 thorpej
706 1.1 jonathan xform_register(&ipe4_xformsw);
707 1.1 jonathan /* attach to encapsulation framework */
708 1.1 jonathan /* XXX save return cookie for detach on module remove */
709 1.41 knakahar
710 1.41 knakahar encapinit();
711 1.41 knakahar /* This function is called before ifinit(). Who else gets lock? */
712 1.41 knakahar (void)encap_lock_enter();
713 1.40 knakahar /* ipe4_encapsw and ipe4_encapsw must be added atomically */
714 1.15 degroote #ifdef INET
715 1.1 jonathan (void) encap_attach_func(AF_INET, -1,
716 1.36 knakahar ipe4_encapcheck, &ipe4_encapsw, NULL);
717 1.15 degroote #endif
718 1.1 jonathan #ifdef INET6
719 1.1 jonathan (void) encap_attach_func(AF_INET6, -1,
720 1.36 knakahar ipe4_encapcheck, &ipe4_encapsw6, NULL);
721 1.1 jonathan #endif
722 1.40 knakahar encap_lock_exit();
723 1.1 jonathan }
724