]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/in_gif.c
xnu-6153.61.1.tar.gz
[apple/xnu.git] / bsd / netinet / in_gif.c
CommitLineData
1c79356b 1/*
a39ff7e2 2 * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 5 *
2d21ac55
A
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.
39236c6e 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39236c6e 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
39236c6e 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
39236c6e 28/* $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $ */
1c79356b
A
29
30/*
31 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
32 * All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. Neither the name of the project nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 */
58
1c79356b
A
59
60#include <sys/param.h>
61#include <sys/systm.h>
62#include <sys/socket.h>
63#include <sys/sockio.h>
1c79356b
A
64#include <sys/mbuf.h>
65#include <sys/errno.h>
1c79356b
A
66#include <sys/kernel.h>
67#include <sys/sysctl.h>
1c79356b 68
1c79356b 69#include <sys/malloc.h>
2d21ac55 70#include <libkern/OSAtomic.h>
1c79356b
A
71
72#include <net/if.h>
73#include <net/route.h>
1c79356b
A
74
75#include <netinet/in.h>
76#include <netinet/in_systm.h>
77#include <netinet/ip.h>
78#include <netinet/ip_var.h>
79#include <netinet/in_gif.h>
80#include <netinet/in_var.h>
81#include <netinet/ip_encap.h>
82#include <netinet/ip_ecn.h>
83
84#if INET6
85#include <netinet/ip6.h>
86#endif
87
39236c6e 88#include <net/if_gif.h>
1c79356b 89
1c79356b
A
90#include <net/net_osdep.h>
91
9bccf70c 92int ip_gif_ttl = GIF_TTL;
6d2010ae 93SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW | CTLFLAG_LOCKED,
0a7de745 94 &ip_gif_ttl, 0, "");
1c79356b
A
95
96int
91447636 97in_gif_output(
0a7de745
A
98 struct ifnet *ifp,
99 int family,
100 struct mbuf *m,
2d21ac55 101 __unused struct rtentry *rt)
1c79356b 102{
2d21ac55 103 struct gif_softc *sc = ifnet_softc(ifp);
39236c6e
A
104 struct sockaddr_in *dst = (struct sockaddr_in *)
105 (void *)&sc->gif_ro.ro_dst;
106 struct sockaddr_in *sin_src = (struct sockaddr_in *)
107 (void *)sc->gif_psrc;
108 struct sockaddr_in *sin_dst = (struct sockaddr_in *)
109 (void *)sc->gif_pdst;
0a7de745 110 struct ip iphdr; /* capsule IP header, host byte ordered */
1c79356b
A
111 int proto, error;
112 u_int8_t tos;
a39ff7e2
A
113 struct ip_out_args ipoa;
114
115 bzero(&ipoa, sizeof(ipoa));
116 ipoa.ipoa_boundif = IFSCOPE_NONE;
117 ipoa.ipoa_flags = IPOAF_SELECT_SRCIF;
118 ipoa.ipoa_sotc = SO_TC_UNSPEC;
119 ipoa.ipoa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
39236c6e
A
120
121 GIF_LOCK_ASSERT(sc);
1c79356b
A
122
123 if (sin_src == NULL || sin_dst == NULL ||
124 sin_src->sin_family != AF_INET ||
125 sin_dst->sin_family != AF_INET) {
1c79356b 126 m_freem(m);
0a7de745 127 return EAFNOSUPPORT;
1c79356b
A
128 }
129
130 switch (family) {
131#if INET
132 case AF_INET:
0a7de745 133 {
1c79356b
A
134 struct ip *ip;
135
136 proto = IPPROTO_IPV4;
0a7de745
A
137 if (mbuf_len(m) < sizeof(*ip)) {
138 m = m_pullup(m, sizeof(*ip));
139 if (!m) {
140 return ENOBUFS;
141 }
1c79356b
A
142 }
143 ip = mtod(m, struct ip *);
144 tos = ip->ip_tos;
145 break;
0a7de745 146 }
39236c6e 147#endif /* INET */
1c79356b
A
148#if INET6
149 case AF_INET6:
0a7de745 150 {
1c79356b
A
151 struct ip6_hdr *ip6;
152 proto = IPPROTO_IPV6;
0a7de745
A
153 if (mbuf_len(m) < sizeof(*ip6)) {
154 m = m_pullup(m, sizeof(*ip6));
155 if (!m) {
156 return ENOBUFS;
157 }
1c79356b
A
158 }
159 ip6 = mtod(m, struct ip6_hdr *);
160 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
161 break;
0a7de745 162 }
39236c6e 163#endif /* INET6 */
1c79356b
A
164 default:
165#if DEBUG
166 printf("in_gif_output: warning: unknown family %d passed\n",
0a7de745 167 family);
1c79356b
A
168#endif
169 m_freem(m);
0a7de745 170 return EAFNOSUPPORT;
1c79356b
A
171 }
172
0a7de745 173 bzero(&iphdr, sizeof(iphdr));
1c79356b 174 iphdr.ip_src = sin_src->sin_addr;
9bccf70c 175 /* bidirectional configured tunnel mode */
0a7de745 176 if (sin_dst->sin_addr.s_addr != INADDR_ANY) {
9bccf70c 177 iphdr.ip_dst = sin_dst->sin_addr;
0a7de745 178 } else {
9bccf70c 179 m_freem(m);
0a7de745 180 return ENETUNREACH;
1c79356b
A
181 }
182 iphdr.ip_p = proto;
183 /* version will be set in ip_output() */
184 iphdr.ip_ttl = ip_gif_ttl;
0a7de745
A
185 iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip);
186 if (ifp->if_flags & IFF_LINK1) {
3e170ce0 187 ip_ecn_ingress(ECN_NORMAL, &iphdr.ip_tos, &tos);
0a7de745 188 } else {
9bccf70c 189 ip_ecn_ingress(ECN_NOCARE, &iphdr.ip_tos, &tos);
0a7de745 190 }
1c79356b
A
191
192 /* prepend new IP header */
0a7de745
A
193 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT, 0);
194 if (m && mbuf_len(m) < sizeof(struct ip)) {
195 m = m_pullup(m, sizeof(struct ip));
196 }
1c79356b
A
197 if (m == NULL) {
198 printf("ENOBUFS in in_gif_output %d\n", __LINE__);
0a7de745 199 return ENOBUFS;
1c79356b 200 }
0a7de745 201 bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
1c79356b 202
39236c6e
A
203 if (ROUTE_UNUSABLE(&sc->gif_ro) ||
204 dst->sin_family != sin_dst->sin_family ||
935ed37a 205 dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr ||
39236c6e 206 (sc->gif_ro.ro_rt != NULL && sc->gif_ro.ro_rt->rt_ifp == ifp)) {
b0d623f7 207 /* cache route doesn't match or recursive route */
1c79356b 208 dst->sin_family = sin_dst->sin_family;
0a7de745 209 dst->sin_len = sizeof(struct sockaddr_in);
1c79356b 210 dst->sin_addr = sin_dst->sin_addr;
39236c6e 211 ROUTE_RELEASE(&sc->gif_ro);
1c79356b
A
212#if 0
213 sc->gif_if.if_mtu = GIF_MTU;
214#endif
215 }
216
217 if (sc->gif_ro.ro_rt == NULL) {
218 rtalloc(&sc->gif_ro);
219 if (sc->gif_ro.ro_rt == NULL) {
220 m_freem(m);
0a7de745 221 return ENETUNREACH;
1c79356b 222 }
9bccf70c
A
223
224 /* if it constitutes infinite encapsulation, punt. */
b0d623f7 225 RT_LOCK(sc->gif_ro.ro_rt);
9bccf70c 226 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
b0d623f7 227 RT_UNLOCK(sc->gif_ro.ro_rt);
9bccf70c 228 m_freem(m);
0a7de745 229 return ENETUNREACH; /* XXX */
9bccf70c 230 }
1c79356b
A
231#if 0
232 ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
0a7de745 233 - sizeof(struct ip);
1c79356b 234#endif
b0d623f7 235 RT_UNLOCK(sc->gif_ro.ro_rt);
1c79356b
A
236 }
237
c910b4d9 238 error = ip_output(m, NULL, &sc->gif_ro, IP_OUTARGS, NULL, &ipoa);
39236c6e 239
0a7de745 240 return error;
1c79356b
A
241}
242
243void
39037602 244in_gif_input(struct mbuf *m, int off)
1c79356b 245{
1c79356b
A
246 struct ifnet *gifp = NULL;
247 struct ip *ip;
2d21ac55 248 int af, proto;
4bd07ac2 249 u_int8_t otos, old_tos;
3e170ce0 250 int egress_success = 0;
4bd07ac2 251 int sum;
1c79356b 252
1c79356b 253 ip = mtod(m, struct ip *);
9bccf70c 254 proto = ip->ip_p;
1c79356b 255
1c79356b 256
39236c6e 257 gifp = ((struct gif_softc *)encap_getarg(m))->gif_if;
1c79356b 258
9bccf70c 259 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
1c79356b 260 m_freem(m);
b0d623f7 261 OSAddAtomic(1, &ipstat.ips_nogif);
1c79356b
A
262 return;
263 }
264
265 otos = ip->ip_tos;
266 m_adj(m, off);
267
268 switch (proto) {
269#if INET
270 case IPPROTO_IPV4:
0a7de745 271 {
1c79356b 272 af = AF_INET;
0a7de745
A
273 if (mbuf_len(m) < sizeof(*ip)) {
274 m = m_pullup(m, sizeof(*ip));
275 if (!m) {
1c79356b 276 return;
0a7de745 277 }
1c79356b
A
278 }
279 ip = mtod(m, struct ip *);
4bd07ac2
A
280 if (gifp->if_flags & IFF_LINK1) {
281 old_tos = ip->ip_tos;
3e170ce0 282 egress_success = ip_ecn_egress(ECN_NORMAL, &otos, &ip->ip_tos);
4bd07ac2 283 if (old_tos != ip->ip_tos) {
0a7de745
A
284 sum = ~ntohs(ip->ip_sum) & 0xffff;
285 sum += (~otos & 0xffff) + ip->ip_tos;
286 sum = (sum >> 16) + (sum & 0xffff);
287 sum += (sum >> 16); /* add carry */
288 ip->ip_sum = htons(~sum & 0xffff);
4bd07ac2 289 }
0a7de745 290 } else {
3e170ce0 291 egress_success = ip_ecn_egress(ECN_NOCARE, &otos, &ip->ip_tos);
0a7de745 292 }
1c79356b 293 break;
0a7de745 294 }
1c79356b
A
295#endif
296#if INET6
297 case IPPROTO_IPV6:
0a7de745 298 {
1c79356b
A
299 struct ip6_hdr *ip6;
300 u_int8_t itos;
301 af = AF_INET6;
0a7de745
A
302 if (mbuf_len(m) < sizeof(*ip6)) {
303 m = m_pullup(m, sizeof(*ip6));
304 if (!m) {
1c79356b 305 return;
0a7de745 306 }
1c79356b
A
307 }
308 ip6 = mtod(m, struct ip6_hdr *);
309 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
0a7de745 310 if (gifp->if_flags & IFF_LINK1) {
3e170ce0 311 egress_success = ip_ecn_egress(ECN_NORMAL, &otos, &itos);
0a7de745 312 } else {
3e170ce0 313 egress_success = ip_ecn_egress(ECN_NOCARE, &otos, &itos);
0a7de745 314 }
1c79356b
A
315 ip6->ip6_flow &= ~htonl(0xff << 20);
316 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
317 break;
0a7de745 318 }
1c79356b
A
319#endif /* INET6 */
320 default:
b0d623f7 321 OSAddAtomic(1, &ipstat.ips_nogif);
1c79356b
A
322 m_freem(m);
323 return;
324 }
3e170ce0
A
325
326 if (egress_success == 0) {
327 OSAddAtomic(1, &ipstat.ips_nogif);
328 m_freem(m);
329 return;
330 }
331
9bccf70c 332#ifdef __APPLE__
39236c6e 333 /* Replace the rcvif by gifp for dlil to route it correctly */
0a7de745 334 if (m->m_pkthdr.rcvif) {
9bccf70c 335 m->m_pkthdr.rcvif = gifp;
0a7de745 336 }
2d21ac55 337 ifnet_input(gifp, m, NULL);
9bccf70c 338#else
1c79356b 339 gif_input(m, af, gifp);
9bccf70c 340#endif
2d21ac55
A
341}
342
9bccf70c 343/*
39236c6e 344 * We know that we are in IFF_UP, outer address available, and outer family
9bccf70c
A
345 * matched the physical addr family. see gif_encapcheck().
346 */
1c79356b 347int
2d21ac55
A
348gif_encapcheck4(
349 const struct mbuf *m,
350 __unused int off,
351 __unused int proto,
352 void *arg)
1c79356b 353{
9bccf70c
A
354 struct ip ip;
355 struct gif_softc *sc;
356 struct sockaddr_in *src, *dst;
357 int addrmatch;
358 struct in_ifaddr *ia4;
359
360 /* sanity check done in caller */
361 sc = (struct gif_softc *)arg;
316670eb
A
362 src = (struct sockaddr_in *)(void *)sc->gif_psrc;
363 dst = (struct sockaddr_in *)(void *)sc->gif_pdst;
9bccf70c 364
39236c6e
A
365 GIF_LOCK_ASSERT(sc);
366
0a7de745 367 mbuf_copydata((struct mbuf *)(size_t)m, 0, sizeof(ip), &ip);
9bccf70c
A
368
369 /* check for address match */
370 addrmatch = 0;
0a7de745 371 if (src->sin_addr.s_addr == ip.ip_dst.s_addr) {
9bccf70c 372 addrmatch |= 1;
0a7de745
A
373 }
374 if (dst->sin_addr.s_addr == ip.ip_src.s_addr) {
9bccf70c 375 addrmatch |= 2;
0a7de745
A
376 }
377 if (addrmatch != 3) {
378 return 0;
379 }
9bccf70c
A
380
381 /* martian filters on outer source - NOT done in ip_input! */
0a7de745
A
382 if (IN_MULTICAST(ntohl(ip.ip_src.s_addr))) {
383 return 0;
384 }
9bccf70c
A
385 switch ((ntohl(ip.ip_src.s_addr) & 0xff000000) >> 24) {
386 case 0: case 127: case 255:
0a7de745 387 return 0;
9bccf70c
A
388 }
389 /* reject packets with broadcast on source */
b0d623f7 390 lck_rw_lock_shared(in_ifaddr_rwlock);
9bccf70c 391 for (ia4 = TAILQ_FIRST(&in_ifaddrhead); ia4;
39236c6e 392 ia4 = TAILQ_NEXT(ia4, ia_link)) {
0a7de745 393 if ((ifnet_flags(ia4->ia_ifa.ifa_ifp) & IFF_BROADCAST) == 0) {
9bccf70c 394 continue;
0a7de745 395 }
6d2010ae 396 IFA_LOCK(&ia4->ia_ifa);
91447636 397 if (ip.ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
6d2010ae 398 IFA_UNLOCK(&ia4->ia_ifa);
b0d623f7 399 lck_rw_done(in_ifaddr_rwlock);
0a7de745 400 return 0;
91447636 401 }
6d2010ae 402 IFA_UNLOCK(&ia4->ia_ifa);
9bccf70c 403 }
b0d623f7 404 lck_rw_done(in_ifaddr_rwlock);
1c79356b 405
9bccf70c 406 /* ingress filters on outer source */
2d21ac55 407 if ((ifnet_flags(sc->gif_if) & IFF_LINK2) == 0 &&
9bccf70c
A
408 (m->m_flags & M_PKTHDR) != 0 && m->m_pkthdr.rcvif) {
409 struct sockaddr_in sin;
410 struct rtentry *rt;
411
0a7de745 412 bzero(&sin, sizeof(sin));
9bccf70c 413 sin.sin_family = AF_INET;
0a7de745 414 sin.sin_len = sizeof(struct sockaddr_in);
9bccf70c 415 sin.sin_addr = ip.ip_src;
b0d623f7 416 rt = rtalloc1_scoped((struct sockaddr *)&sin, 0, 0,
c910b4d9 417 m->m_pkthdr.rcvif->if_index);
0a7de745 418 if (rt != NULL) {
b0d623f7 419 RT_LOCK(rt);
0a7de745 420 }
b0d623f7 421 if (rt == NULL || rt->rt_ifp != m->m_pkthdr.rcvif) {
b0d623f7
A
422 if (rt != NULL) {
423 RT_UNLOCK(rt);
9bccf70c 424 rtfree(rt);
b0d623f7 425 }
0a7de745 426 return 0;
1c79356b 427 }
b0d623f7 428 RT_UNLOCK(rt);
9bccf70c 429 rtfree(rt);
1c79356b
A
430 }
431
0a7de745 432 return 32 * 2;
1c79356b 433}