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