]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_gif.c
xnu-6153.81.5.tar.gz
[apple/xnu.git] / bsd / netinet6 / in6_gif.c
1 /*
2 * Copyright (c) 2009-2016 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
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 $ */
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
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>
68 #include <sys/queue.h>
69 #include <sys/syslog.h>
70
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>
88 #endif
89 #include <netinet/ip_ecn.h>
90 #if INET6
91 #include <netinet6/ip6_ecn.h>
92 #endif
93
94 #include <net/if_gif.h>
95
96 #include <net/net_osdep.h>
97
98 int
99 in6_gif_output(
100 struct ifnet *ifp,
101 int family, /* family of the packet to be encapsulate. */
102 struct mbuf *m,
103 __unused struct rtentry *rt)
104 {
105 struct gif_softc *sc = ifnet_softc(ifp);
106 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
107 struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)
108 (void *)sc->gif_psrc;
109 struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)
110 (void *)sc->gif_pdst;
111 struct ip6_hdr *ip6;
112 int proto;
113 u_int8_t itos, otos;
114
115 GIF_LOCK_ASSERT(sc);
116
117 if (sin6_src == NULL || sin6_dst == NULL ||
118 sin6_src->sin6_family != AF_INET6 ||
119 sin6_dst->sin6_family != AF_INET6) {
120 m_freem(m);
121 return EAFNOSUPPORT;
122 }
123
124 switch (family) {
125 #if INET
126 case AF_INET:
127 {
128 struct ip *ip;
129
130 proto = IPPROTO_IPV4;
131 if (mbuf_len(m) < sizeof(*ip)) {
132 m = m_pullup(m, sizeof(*ip));
133 if (!m) {
134 return ENOBUFS;
135 }
136 }
137 ip = mtod(m, struct ip *);
138 itos = ip->ip_tos;
139 break;
140 }
141 #endif
142 #if INET6
143 case AF_INET6:
144 {
145 proto = IPPROTO_IPV6;
146 if (mbuf_len(m) < sizeof(*ip6)) {
147 m = m_pullup(m, sizeof(*ip6));
148 if (!m) {
149 return ENOBUFS;
150 }
151 }
152 ip6 = mtod(m, struct ip6_hdr *);
153 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
154 break;
155 }
156 #endif
157 default:
158 #if DEBUG
159 printf("in6_gif_output: warning: unknown family %d passed\n",
160 family);
161 #endif
162 m_freem(m);
163 return EAFNOSUPPORT;
164 }
165
166 /* prepend new IP header */
167 M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT, 1);
168 if (m && mbuf_len(m) < sizeof(struct ip6_hdr)) {
169 m = m_pullup(m, sizeof(struct ip6_hdr));
170 }
171 if (m == NULL) {
172 printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
173 return ENOBUFS;
174 }
175
176 ip6 = mtod(m, struct ip6_hdr *);
177 ip6->ip6_flow = 0;
178 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
179 ip6->ip6_vfc |= IPV6_VERSION;
180 ip6->ip6_plen = htons((u_short)m->m_pkthdr.len);
181 ip6->ip6_nxt = proto;
182 ip6->ip6_hlim = ip6_gif_hlim;
183 ip6->ip6_src = sin6_src->sin6_addr;
184 /* bidirectional configured tunnel mode */
185 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr)) {
186 ip6->ip6_dst = sin6_dst->sin6_addr;
187 } else {
188 m_freem(m);
189 return ENETUNREACH;
190 }
191 ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_NORMAL : ECN_NOCARE,
192 &otos, &itos);
193 ip6->ip6_flow &= ~htonl(0xff << 20);
194 ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
195
196 if (ROUTE_UNUSABLE(&sc->gif_ro6) ||
197 dst->sin6_family != sin6_dst->sin6_family ||
198 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr) ||
199 (sc->gif_ro6.ro_rt != NULL && sc->gif_ro6.ro_rt->rt_ifp == ifp)) {
200 /* cache route doesn't match or recursive route */
201 bzero(dst, sizeof(*dst));
202 dst->sin6_family = sin6_dst->sin6_family;
203 dst->sin6_len = sizeof(struct sockaddr_in6);
204 dst->sin6_addr = sin6_dst->sin6_addr;
205 ROUTE_RELEASE(&sc->gif_ro6);
206 #if 0
207 sc->gif_if.if_mtu = GIF_MTU;
208 #endif
209 }
210
211 if (sc->gif_ro6.ro_rt == NULL) {
212 rtalloc((struct route *)&sc->gif_ro6);
213 if (sc->gif_ro6.ro_rt == NULL) {
214 m_freem(m);
215 return ENETUNREACH;
216 }
217 RT_LOCK(sc->gif_ro6.ro_rt);
218 /* if it constitutes infinite encapsulation, punt. */
219 if (sc->gif_ro6.ro_rt->rt_ifp == ifp) {
220 RT_UNLOCK(sc->gif_ro6.ro_rt);
221 m_freem(m);
222 return ENETUNREACH; /* XXX */
223 }
224 #if 0
225 ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
226 - sizeof(struct ip6_hdr);
227 #endif
228 RT_UNLOCK(sc->gif_ro6.ro_rt);
229 }
230
231 #if IPV6_MINMTU
232 /*
233 * force fragmentation to minimum MTU, to avoid path MTU discovery.
234 * it is too painful to ask for resend of inner packet, to achieve
235 * path MTU discovery for encapsulated packets.
236 */
237 return ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL);
238 #else
239 return ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL);
240 #endif
241 }
242
243 int
244 in6_gif_input(struct mbuf **mp, int *offp, int proto)
245 {
246 struct mbuf *m = *mp;
247 struct ifnet *gifp = NULL;
248 struct ip6_hdr *ip6;
249 int af = 0;
250 u_int32_t otos;
251 int egress_success = 0;
252
253 ip6 = mtod(m, struct ip6_hdr *);
254
255 gifp = ((struct gif_softc *)encap_getarg(m))->gif_if;
256
257 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
258 m_freem(m);
259 ip6stat.ip6s_nogif++;
260 return IPPROTO_DONE;
261 }
262
263 otos = ip6->ip6_flow;
264 m_adj(m, *offp);
265
266 switch (proto) {
267 #if INET
268 case IPPROTO_IPV4:
269 {
270 struct ip *ip;
271 u_int8_t otos8, old_tos;
272 int sum;
273
274 af = AF_INET;
275 otos8 = (ntohl(otos) >> 20) & 0xff;
276 if (mbuf_len(m) < sizeof(*ip)) {
277 m = m_pullup(m, sizeof(*ip));
278 if (!m) {
279 return IPPROTO_DONE;
280 }
281 }
282 ip = mtod(m, struct ip *);
283 if (gifp->if_flags & IFF_LINK1) {
284 old_tos = ip->ip_tos;
285 egress_success = ip_ecn_egress(ECN_NORMAL, &otos8, &ip->ip_tos);
286 if (old_tos != ip->ip_tos) {
287 sum = ~ntohs(ip->ip_sum) & 0xffff;
288 sum += (~old_tos & 0xffff) + ip->ip_tos;
289 sum = (sum >> 16) + (sum & 0xffff);
290 sum += (sum >> 16); /* add carry */
291 ip->ip_sum = htons(~sum & 0xffff);
292 }
293 } else {
294 egress_success = ip_ecn_egress(ECN_NOCARE, &otos8, &ip->ip_tos);
295 }
296 break;
297 }
298 #endif /* INET */
299 #if INET6
300 case IPPROTO_IPV6:
301 {
302 af = AF_INET6;
303 if (mbuf_len(m) < sizeof(*ip6)) {
304 m = m_pullup(m, sizeof(*ip6));
305 if (!m) {
306 return IPPROTO_DONE;
307 }
308 }
309 ip6 = mtod(m, struct ip6_hdr *);
310 if (gifp->if_flags & IFF_LINK1) {
311 egress_success = ip6_ecn_egress(ECN_NORMAL, &otos, &ip6->ip6_flow);
312 } else {
313 egress_success = ip6_ecn_egress(ECN_NOCARE, &otos, &ip6->ip6_flow);
314 }
315 break;
316 }
317 #endif
318 default:
319 ip6stat.ip6s_nogif++;
320 m_freem(m);
321 return IPPROTO_DONE;
322 }
323
324 if (egress_success == 0) {
325 ip6stat.ip6s_nogif++;
326 m_freem(m);
327 return IPPROTO_DONE;
328 }
329
330 /* Replace the rcvif by gifp for ifnet_input to route it correctly */
331 if (m->m_pkthdr.rcvif) {
332 m->m_pkthdr.rcvif = gifp;
333 }
334
335 ifnet_input(gifp, m, NULL);
336 return IPPROTO_DONE;
337 }
338
339 /*
340 * validate outer address.
341 */
342 static int
343 gif_validate6(
344 const struct ip6_hdr *ip6,
345 struct gif_softc *sc,
346 struct ifnet *ifp)
347 {
348 struct sockaddr_in6 *src, *dst;
349
350 src = (struct sockaddr_in6 *)(void *)sc->gif_psrc;
351 dst = (struct sockaddr_in6 *)(void *)sc->gif_pdst;
352
353 /*
354 * Check for address match. Note that the check is for an incoming
355 * packet. We should compare the *source* address in our configuration
356 * and the *destination* address of the packet, and vice versa.
357 */
358 if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
359 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src)) {
360 return 0;
361 }
362
363 /* martian filters on outer source - done in ip6_input */
364
365 /* ingress filters on outer source */
366 if ((ifnet_flags(sc->gif_if) & IFF_LINK2) == 0 && ifp) {
367 struct sockaddr_in6 sin6;
368 struct rtentry *rt;
369
370 bzero(&sin6, sizeof(sin6));
371 sin6.sin6_family = AF_INET6;
372 sin6.sin6_len = sizeof(struct sockaddr_in6);
373 sin6.sin6_addr = ip6->ip6_src;
374
375 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0);
376 if (rt != NULL) {
377 RT_LOCK(rt);
378 }
379 if (!rt || rt->rt_ifp != ifp) {
380 #if 0
381 log(LOG_WARNING, "%s: packet from %s dropped "
382 "due to ingress filter\n", if_name(&sc->gif_if),
383 ip6_sprintf(&sin6.sin6_addr));
384 #endif
385 if (rt != NULL) {
386 RT_UNLOCK(rt);
387 rtfree(rt);
388 }
389 return 0;
390 }
391 RT_UNLOCK(rt);
392 rtfree(rt);
393 }
394
395 return 128 * 2;
396 }
397
398 /*
399 * we know that we are in IFF_UP, outer address available, and outer family
400 * matched the physical addr family. see gif_encapcheck().
401 * sanity check for arg should have been done in the caller.
402 */
403 int
404 gif_encapcheck6(
405 const struct mbuf *m,
406 __unused int off,
407 __unused int proto,
408 void *arg)
409 {
410 struct ip6_hdr ip6;
411 struct gif_softc *sc;
412 struct ifnet *ifp;
413
414 /* sanity check done in caller */
415 sc = (struct gif_softc *)arg;
416
417 GIF_LOCK_ASSERT(sc);
418
419 mbuf_copydata((struct mbuf *)(size_t)m, 0, sizeof(ip6), &ip6);
420 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
421
422 return gif_validate6(&ip6, sc, ifp);
423 }