]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/in6_gif.c
xnu-1504.9.17.tar.gz
[apple/xnu.git] / bsd / netinet6 / in6_gif.c
CommitLineData
b0d623f7
A
1/*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
9bccf70c
A
29/* $FreeBSD: src/sys/netinet6/in6_gif.c,v 1.2.2.3 2001/07/03 11:01:52 ume Exp $ */
30/* $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $ */
1c79356b
A
31
32/*
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
1c79356b
A
61
62#include <sys/param.h>
63#include <sys/systm.h>
64#include <sys/socket.h>
65#include <sys/sockio.h>
66#include <sys/mbuf.h>
67#include <sys/errno.h>
9bccf70c
A
68#include <sys/queue.h>
69#include <sys/syslog.h>
70
1c79356b
A
71#include <sys/malloc.h>
72#include <sys/protosw.h>
73
74#include <net/if.h>
75#include <net/route.h>
76
77#include <netinet/in.h>
78#include <netinet/in_systm.h>
79#if INET
80#include <netinet/ip.h>
81#endif
82#include <netinet/ip_encap.h>
83#if INET6
84#include <netinet/ip6.h>
85#include <netinet6/ip6_var.h>
86#include <netinet6/in6_gif.h>
87#include <netinet6/in6_var.h>
1c79356b
A
88#endif
89#include <netinet/ip_ecn.h>
9bccf70c
A
90#if INET6
91#include <netinet6/ip6_ecn.h>
92#endif
1c79356b
A
93
94#include <net/if_gif.h>
95
96#include <net/net_osdep.h>
97
2d21ac55
A
98static __inline__ void*
99_cast_non_const(const void * ptr) {
100 union {
101 const void* cval;
102 void* val;
103 } ret;
104
105 ret.cval = ptr;
106 return (ret.val);
107}
108
1c79356b 109int
91447636
A
110in6_gif_output(
111 struct ifnet *ifp,
112 int family, /* family of the packet to be encapsulate. */
113 struct mbuf *m,
2d21ac55 114 __unused struct rtentry *rt)
1c79356b 115{
2d21ac55 116 struct gif_softc *sc = ifnet_softc(ifp);
1c79356b
A
117 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
118 struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
119 struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
120 struct ip6_hdr *ip6;
121 int proto;
122 u_int8_t itos, otos;
123
124 if (sin6_src == NULL || sin6_dst == NULL ||
125 sin6_src->sin6_family != AF_INET6 ||
126 sin6_dst->sin6_family != AF_INET6) {
127 m_freem(m);
128 return EAFNOSUPPORT;
129 }
130
131 switch (family) {
132#if INET
133 case AF_INET:
134 {
135 struct ip *ip;
136
137 proto = IPPROTO_IPV4;
2d21ac55 138 if (mbuf_len(m) < sizeof(*ip)) {
1c79356b
A
139 m = m_pullup(m, sizeof(*ip));
140 if (!m)
141 return ENOBUFS;
142 }
143 ip = mtod(m, struct ip *);
144 itos = ip->ip_tos;
145 break;
146 }
147#endif
148#if INET6
149 case AF_INET6:
150 {
1c79356b 151 proto = IPPROTO_IPV6;
2d21ac55 152 if (mbuf_len(m) < sizeof(*ip6)) {
1c79356b
A
153 m = m_pullup(m, sizeof(*ip6));
154 if (!m)
155 return ENOBUFS;
156 }
157 ip6 = mtod(m, struct ip6_hdr *);
158 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
159 break;
160 }
161#endif
162 default:
163#if DEBUG
164 printf("in6_gif_output: warning: unknown family %d passed\n",
165 family);
166#endif
167 m_freem(m);
168 return EAFNOSUPPORT;
169 }
170
171 /* prepend new IP header */
172 M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
2d21ac55 173 if (m && mbuf_len(m) < sizeof(struct ip6_hdr))
1c79356b
A
174 m = m_pullup(m, sizeof(struct ip6_hdr));
175 if (m == NULL) {
176 printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
177 return ENOBUFS;
178 }
179
180 ip6 = mtod(m, struct ip6_hdr *);
181 ip6->ip6_flow = 0;
182 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
183 ip6->ip6_vfc |= IPV6_VERSION;
184 ip6->ip6_plen = htons((u_short)m->m_pkthdr.len);
185 ip6->ip6_nxt = proto;
186 ip6->ip6_hlim = ip6_gif_hlim;
187 ip6->ip6_src = sin6_src->sin6_addr;
9bccf70c
A
188 /* bidirectional configured tunnel mode */
189 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
190 ip6->ip6_dst = sin6_dst->sin6_addr;
191 else {
192 m_freem(m);
193 return ENETUNREACH;
1c79356b 194 }
9bccf70c 195 if (ifp->if_flags & IFF_LINK1)
1c79356b 196 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
9bccf70c
A
197 else
198 ip_ecn_ingress(ECN_NOCARE, &otos, &itos);
199 ip6->ip6_flow &= ~ntohl(0xff00000);
200 ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
1c79356b
A
201
202 if (dst->sin6_family != sin6_dst->sin6_family ||
935ed37a
A
203 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr) ||
204 (sc->gif_ro6.ro_rt != NULL &&
205 (sc->gif_ro6.ro_rt->generation_id != route_generation ||
206 sc->gif_ro6.ro_rt->rt_ifp == ifp))) {
b0d623f7 207 /* cache route doesn't match or recursive route */
1c79356b
A
208 bzero(dst, sizeof(*dst));
209 dst->sin6_family = sin6_dst->sin6_family;
210 dst->sin6_len = sizeof(struct sockaddr_in6);
211 dst->sin6_addr = sin6_dst->sin6_addr;
212 if (sc->gif_ro6.ro_rt) {
9bccf70c 213 rtfree(sc->gif_ro6.ro_rt);
1c79356b
A
214 sc->gif_ro6.ro_rt = NULL;
215 }
216#if 0
217 sc->gif_if.if_mtu = GIF_MTU;
218#endif
219 }
220
221 if (sc->gif_ro6.ro_rt == NULL) {
222 rtalloc((struct route *)&sc->gif_ro6);
223 if (sc->gif_ro6.ro_rt == NULL) {
224 m_freem(m);
225 return ENETUNREACH;
226 }
b0d623f7 227 RT_LOCK(sc->gif_ro6.ro_rt);
9bccf70c 228 /* if it constitutes infinite encapsulation, punt. */
935ed37a 229 if (sc->gif_ro6.ro_rt->rt_ifp == ifp) {
b0d623f7 230 RT_UNLOCK(sc->gif_ro6.ro_rt);
9bccf70c
A
231 m_freem(m);
232 return ENETUNREACH; /*XXX*/
233 }
1c79356b
A
234#if 0
235 ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
236 - sizeof(struct ip6_hdr);
237#endif
b0d623f7 238 RT_UNLOCK(sc->gif_ro6.ro_rt);
1c79356b
A
239 }
240
9bccf70c
A
241#if IPV6_MINMTU
242 /*
243 * force fragmentation to minimum MTU, to avoid path MTU discovery.
244 * it is too painful to ask for resend of inner packet, to achieve
245 * path MTU discovery for encapsulated packets.
246 */
91447636 247 return(ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, 0));
9bccf70c 248#else
91447636 249 return(ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, 0));
9bccf70c 250#endif
1c79356b
A
251}
252
9bccf70c 253int in6_gif_input(mp, offp)
1c79356b 254 struct mbuf **mp;
9bccf70c 255 int *offp;
1c79356b
A
256{
257 struct mbuf *m = *mp;
1c79356b
A
258 struct ifnet *gifp = NULL;
259 struct ip6_hdr *ip6;
1c79356b
A
260 int af = 0;
261 u_int32_t otos;
9bccf70c 262 u_int8_t proto;
1c79356b
A
263
264 ip6 = mtod(m, struct ip6_hdr *);
265
2d21ac55 266 gifp = ((struct gif_softc*)encap_getarg(m))->gif_if;
1c79356b 267
9bccf70c 268 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
1c79356b
A
269 m_freem(m);
270 ip6stat.ip6s_nogif++;
271 return IPPROTO_DONE;
272 }
273
9bccf70c 274 proto = ip6->ip6_nxt;
1c79356b
A
275 otos = ip6->ip6_flow;
276 m_adj(m, *offp);
277
278 switch (proto) {
279#if INET
280 case IPPROTO_IPV4:
281 {
282 struct ip *ip;
283 u_int8_t otos8;
284 af = AF_INET;
285 otos8 = (ntohl(otos) >> 20) & 0xff;
2d21ac55 286 if (mbuf_len(m) < sizeof(*ip)) {
1c79356b
A
287 m = m_pullup(m, sizeof(*ip));
288 if (!m)
289 return IPPROTO_DONE;
290 }
291 ip = mtod(m, struct ip *);
292 if (gifp->if_flags & IFF_LINK1)
293 ip_ecn_egress(ECN_ALLOWED, &otos8, &ip->ip_tos);
9bccf70c
A
294 else
295 ip_ecn_egress(ECN_NOCARE, &otos8, &ip->ip_tos);
1c79356b
A
296 break;
297 }
298#endif /* INET */
299#if INET6
300 case IPPROTO_IPV6:
301 {
1c79356b 302 af = AF_INET6;
2d21ac55 303 if (mbuf_len(m) < sizeof(*ip6)) {
1c79356b
A
304 m = m_pullup(m, sizeof(*ip6));
305 if (!m)
306 return IPPROTO_DONE;
307 }
308 ip6 = mtod(m, struct ip6_hdr *);
309 if (gifp->if_flags & IFF_LINK1)
310 ip6_ecn_egress(ECN_ALLOWED, &otos, &ip6->ip6_flow);
9bccf70c
A
311 else
312 ip6_ecn_egress(ECN_NOCARE, &otos, &ip6->ip6_flow);
1c79356b
A
313 break;
314 }
315#endif
316 default:
317 ip6stat.ip6s_nogif++;
318 m_freem(m);
319 return IPPROTO_DONE;
320 }
2d21ac55
A
321
322 if (m->m_pkthdr.rcvif) /* replace the rcvif by gifp for ifnet_input to route it correctly */
323 m->m_pkthdr.rcvif = gifp;
324
325 ifnet_input(gifp, m, NULL);
1c79356b
A
326 return IPPROTO_DONE;
327}
328
9bccf70c 329/*
55e303ae 330 * validate outer address.
9bccf70c 331 */
55e303ae 332static int
2d21ac55
A
333gif_validate6(
334 const struct ip6_hdr *ip6,
335 struct gif_softc *sc,
336 struct ifnet *ifp)
55e303ae 337{
9bccf70c 338 struct sockaddr_in6 *src, *dst;
9bccf70c 339
9bccf70c
A
340 src = (struct sockaddr_in6 *)sc->gif_psrc;
341 dst = (struct sockaddr_in6 *)sc->gif_pdst;
342
55e303ae
A
343 /*
344 * Check for address match. Note that the check is for an incoming
345 * packet. We should compare the *source* address in our configuration
346 * and the *destination* address of the packet, and vice versa.
347 */
348 if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
349 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
9bccf70c
A
350 return 0;
351
352 /* martian filters on outer source - done in ip6_input */
353
354 /* ingress filters on outer source */
2d21ac55 355 if ((ifnet_flags(sc->gif_if) & IFF_LINK2) == 0 && ifp) {
9bccf70c
A
356 struct sockaddr_in6 sin6;
357 struct rtentry *rt;
358
359 bzero(&sin6, sizeof(sin6));
360 sin6.sin6_family = AF_INET6;
361 sin6.sin6_len = sizeof(struct sockaddr_in6);
55e303ae
A
362 sin6.sin6_addr = ip6->ip6_src;
363#ifndef SCOPEDROUTING
364 sin6.sin6_scope_id = 0; /* XXX */
365#endif
366
b0d623f7
A
367 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0);
368 if (rt != NULL)
369 RT_LOCK(rt);
55e303ae 370 if (!rt || rt->rt_ifp != ifp) {
9bccf70c
A
371#if 0
372 log(LOG_WARNING, "%s: packet from %s dropped "
373 "due to ingress filter\n", if_name(&sc->gif_if),
374 ip6_sprintf(&sin6.sin6_addr));
1c79356b 375#endif
b0d623f7
A
376 if (rt != NULL) {
377 RT_UNLOCK(rt);
9bccf70c 378 rtfree(rt);
b0d623f7 379 }
9bccf70c 380 return 0;
1c79356b 381 }
b0d623f7 382 RT_UNLOCK(rt);
9bccf70c 383 rtfree(rt);
1c79356b
A
384 }
385
9bccf70c 386 return 128 * 2;
1c79356b 387}
55e303ae
A
388
389/*
390 * we know that we are in IFF_UP, outer address available, and outer family
391 * matched the physical addr family. see gif_encapcheck().
392 * sanity check for arg should have been done in the caller.
393 */
394int
2d21ac55
A
395gif_encapcheck6(
396 const struct mbuf *m,
397 __unused int off,
398 __unused int proto,
399 void *arg)
55e303ae
A
400{
401 struct ip6_hdr ip6;
402 struct gif_softc *sc;
403 struct ifnet *ifp;
404
405 /* sanity check done in caller */
406 sc = (struct gif_softc *)arg;
407
b0d623f7 408 mbuf_copydata((struct mbuf *)(size_t)m, 0, sizeof(ip6), &ip6);
55e303ae
A
409 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
410
411 return gif_validate6(&ip6, sc, ifp);
412}