]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_gif.c
xnu-4570.51.1.tar.gz
[apple/xnu.git] / bsd / netinet / in_gif.c
1 /*
2 * Copyright (c) 2000-2018 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 /* $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $ */
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
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/socket.h>
63 #include <sys/sockio.h>
64 #include <sys/mbuf.h>
65 #include <sys/errno.h>
66 #include <sys/kernel.h>
67 #include <sys/sysctl.h>
68
69 #include <sys/malloc.h>
70 #include <libkern/OSAtomic.h>
71
72 #include <net/if.h>
73 #include <net/route.h>
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
88 #include <net/if_gif.h>
89
90 #include <net/net_osdep.h>
91
92 int ip_gif_ttl = GIF_TTL;
93 SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW | CTLFLAG_LOCKED,
94 &ip_gif_ttl, 0, "");
95
96 int
97 in_gif_output(
98 struct ifnet *ifp,
99 int family,
100 struct mbuf *m,
101 __unused struct rtentry *rt)
102 {
103 struct gif_softc *sc = ifnet_softc(ifp);
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;
110 struct ip iphdr; /* capsule IP header, host byte ordered */
111 int proto, error;
112 u_int8_t tos;
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;
120
121 GIF_LOCK_ASSERT(sc);
122
123 if (sin_src == NULL || sin_dst == NULL ||
124 sin_src->sin_family != AF_INET ||
125 sin_dst->sin_family != AF_INET) {
126 m_freem(m);
127 return (EAFNOSUPPORT);
128 }
129
130 switch (family) {
131 #if INET
132 case AF_INET:
133 {
134 struct ip *ip;
135
136 proto = IPPROTO_IPV4;
137 if (mbuf_len(m) < sizeof (*ip)) {
138 m = m_pullup(m, sizeof (*ip));
139 if (!m)
140 return (ENOBUFS);
141 }
142 ip = mtod(m, struct ip *);
143 tos = ip->ip_tos;
144 break;
145 }
146 #endif /* INET */
147 #if INET6
148 case AF_INET6:
149 {
150 struct ip6_hdr *ip6;
151 proto = IPPROTO_IPV6;
152 if (mbuf_len(m) < sizeof (*ip6)) {
153 m = m_pullup(m, sizeof (*ip6));
154 if (!m)
155 return (ENOBUFS);
156 }
157 ip6 = mtod(m, struct ip6_hdr *);
158 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
159 break;
160 }
161 #endif /* INET6 */
162 default:
163 #if DEBUG
164 printf("in_gif_output: warning: unknown family %d passed\n",
165 family);
166 #endif
167 m_freem(m);
168 return (EAFNOSUPPORT);
169 }
170
171 bzero(&iphdr, sizeof (iphdr));
172 iphdr.ip_src = sin_src->sin_addr;
173 /* bidirectional configured tunnel mode */
174 if (sin_dst->sin_addr.s_addr != INADDR_ANY)
175 iphdr.ip_dst = sin_dst->sin_addr;
176 else {
177 m_freem(m);
178 return (ENETUNREACH);
179 }
180 iphdr.ip_p = proto;
181 /* version will be set in ip_output() */
182 iphdr.ip_ttl = ip_gif_ttl;
183 iphdr.ip_len = m->m_pkthdr.len + sizeof (struct ip);
184 if (ifp->if_flags & IFF_LINK1)
185 ip_ecn_ingress(ECN_NORMAL, &iphdr.ip_tos, &tos);
186 else
187 ip_ecn_ingress(ECN_NOCARE, &iphdr.ip_tos, &tos);
188
189 /* prepend new IP header */
190 M_PREPEND(m, sizeof (struct ip), M_DONTWAIT, 0);
191 if (m && mbuf_len(m) < sizeof (struct ip))
192 m = m_pullup(m, sizeof (struct ip));
193 if (m == NULL) {
194 printf("ENOBUFS in in_gif_output %d\n", __LINE__);
195 return (ENOBUFS);
196 }
197 bcopy(&iphdr, mtod(m, struct ip *), sizeof (struct ip));
198
199 if (ROUTE_UNUSABLE(&sc->gif_ro) ||
200 dst->sin_family != sin_dst->sin_family ||
201 dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr ||
202 (sc->gif_ro.ro_rt != NULL && sc->gif_ro.ro_rt->rt_ifp == ifp)) {
203 /* cache route doesn't match or recursive route */
204 dst->sin_family = sin_dst->sin_family;
205 dst->sin_len = sizeof (struct sockaddr_in);
206 dst->sin_addr = sin_dst->sin_addr;
207 ROUTE_RELEASE(&sc->gif_ro);
208 #if 0
209 sc->gif_if.if_mtu = GIF_MTU;
210 #endif
211 }
212
213 if (sc->gif_ro.ro_rt == NULL) {
214 rtalloc(&sc->gif_ro);
215 if (sc->gif_ro.ro_rt == NULL) {
216 m_freem(m);
217 return (ENETUNREACH);
218 }
219
220 /* if it constitutes infinite encapsulation, punt. */
221 RT_LOCK(sc->gif_ro.ro_rt);
222 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
223 RT_UNLOCK(sc->gif_ro.ro_rt);
224 m_freem(m);
225 return (ENETUNREACH); /* XXX */
226 }
227 #if 0
228 ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
229 - sizeof (struct ip);
230 #endif
231 RT_UNLOCK(sc->gif_ro.ro_rt);
232 }
233
234 error = ip_output(m, NULL, &sc->gif_ro, IP_OUTARGS, NULL, &ipoa);
235
236 return (error);
237 }
238
239 void
240 in_gif_input(struct mbuf *m, int off)
241 {
242 struct ifnet *gifp = NULL;
243 struct ip *ip;
244 int af, proto;
245 u_int8_t otos, old_tos;
246 int egress_success = 0;
247 int sum;
248
249 ip = mtod(m, struct ip *);
250 proto = ip->ip_p;
251
252
253 gifp = ((struct gif_softc *)encap_getarg(m))->gif_if;
254
255 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
256 m_freem(m);
257 OSAddAtomic(1, &ipstat.ips_nogif);
258 return;
259 }
260
261 otos = ip->ip_tos;
262 m_adj(m, off);
263
264 switch (proto) {
265 #if INET
266 case IPPROTO_IPV4:
267 {
268 af = AF_INET;
269 if (mbuf_len(m) < sizeof (*ip)) {
270 m = m_pullup(m, sizeof (*ip));
271 if (!m)
272 return;
273 }
274 ip = mtod(m, struct ip *);
275 if (gifp->if_flags & IFF_LINK1) {
276 old_tos = ip->ip_tos;
277 egress_success = ip_ecn_egress(ECN_NORMAL, &otos, &ip->ip_tos);
278 if (old_tos != ip->ip_tos) {
279 sum = ~ntohs(ip->ip_sum) & 0xffff;
280 sum += (~otos & 0xffff) + ip->ip_tos;
281 sum = (sum >> 16) + (sum & 0xffff);
282 sum += (sum >> 16); /* add carry */
283 ip->ip_sum = htons(~sum & 0xffff);
284 }
285 } else
286 egress_success = ip_ecn_egress(ECN_NOCARE, &otos, &ip->ip_tos);
287 break;
288 }
289 #endif
290 #if INET6
291 case IPPROTO_IPV6:
292 {
293 struct ip6_hdr *ip6;
294 u_int8_t itos;
295 af = AF_INET6;
296 if (mbuf_len(m) < sizeof (*ip6)) {
297 m = m_pullup(m, sizeof (*ip6));
298 if (!m)
299 return;
300 }
301 ip6 = mtod(m, struct ip6_hdr *);
302 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
303 if (gifp->if_flags & IFF_LINK1)
304 egress_success = ip_ecn_egress(ECN_NORMAL, &otos, &itos);
305 else
306 egress_success = ip_ecn_egress(ECN_NOCARE, &otos, &itos);
307 ip6->ip6_flow &= ~htonl(0xff << 20);
308 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
309 break;
310 }
311 #endif /* INET6 */
312 default:
313 OSAddAtomic(1, &ipstat.ips_nogif);
314 m_freem(m);
315 return;
316 }
317
318 if (egress_success == 0) {
319 OSAddAtomic(1, &ipstat.ips_nogif);
320 m_freem(m);
321 return;
322 }
323
324 #ifdef __APPLE__
325 /* Replace the rcvif by gifp for dlil to route it correctly */
326 if (m->m_pkthdr.rcvif)
327 m->m_pkthdr.rcvif = gifp;
328 ifnet_input(gifp, m, NULL);
329 #else
330 gif_input(m, af, gifp);
331 #endif
332 }
333
334 /*
335 * We know that we are in IFF_UP, outer address available, and outer family
336 * matched the physical addr family. see gif_encapcheck().
337 */
338 int
339 gif_encapcheck4(
340 const struct mbuf *m,
341 __unused int off,
342 __unused int proto,
343 void *arg)
344 {
345 struct ip ip;
346 struct gif_softc *sc;
347 struct sockaddr_in *src, *dst;
348 int addrmatch;
349 struct in_ifaddr *ia4;
350
351 /* sanity check done in caller */
352 sc = (struct gif_softc *)arg;
353 src = (struct sockaddr_in *)(void *)sc->gif_psrc;
354 dst = (struct sockaddr_in *)(void *)sc->gif_pdst;
355
356 GIF_LOCK_ASSERT(sc);
357
358 mbuf_copydata((struct mbuf *)(size_t)m, 0, sizeof (ip), &ip);
359
360 /* check for address match */
361 addrmatch = 0;
362 if (src->sin_addr.s_addr == ip.ip_dst.s_addr)
363 addrmatch |= 1;
364 if (dst->sin_addr.s_addr == ip.ip_src.s_addr)
365 addrmatch |= 2;
366 if (addrmatch != 3)
367 return (0);
368
369 /* martian filters on outer source - NOT done in ip_input! */
370 if (IN_MULTICAST(ntohl(ip.ip_src.s_addr)))
371 return (0);
372 switch ((ntohl(ip.ip_src.s_addr) & 0xff000000) >> 24) {
373 case 0: case 127: case 255:
374 return (0);
375 }
376 /* reject packets with broadcast on source */
377 lck_rw_lock_shared(in_ifaddr_rwlock);
378 for (ia4 = TAILQ_FIRST(&in_ifaddrhead); ia4;
379 ia4 = TAILQ_NEXT(ia4, ia_link)) {
380 if ((ifnet_flags(ia4->ia_ifa.ifa_ifp) & IFF_BROADCAST) == 0)
381 continue;
382 IFA_LOCK(&ia4->ia_ifa);
383 if (ip.ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
384 IFA_UNLOCK(&ia4->ia_ifa);
385 lck_rw_done(in_ifaddr_rwlock);
386 return (0);
387 }
388 IFA_UNLOCK(&ia4->ia_ifa);
389 }
390 lck_rw_done(in_ifaddr_rwlock);
391
392 /* ingress filters on outer source */
393 if ((ifnet_flags(sc->gif_if) & IFF_LINK2) == 0 &&
394 (m->m_flags & M_PKTHDR) != 0 && m->m_pkthdr.rcvif) {
395 struct sockaddr_in sin;
396 struct rtentry *rt;
397
398 bzero(&sin, sizeof (sin));
399 sin.sin_family = AF_INET;
400 sin.sin_len = sizeof (struct sockaddr_in);
401 sin.sin_addr = ip.ip_src;
402 rt = rtalloc1_scoped((struct sockaddr *)&sin, 0, 0,
403 m->m_pkthdr.rcvif->if_index);
404 if (rt != NULL)
405 RT_LOCK(rt);
406 if (rt == NULL || rt->rt_ifp != m->m_pkthdr.rcvif) {
407 if (rt != NULL) {
408 RT_UNLOCK(rt);
409 rtfree(rt);
410 }
411 return (0);
412 }
413 RT_UNLOCK(rt);
414 rtfree(rt);
415 }
416
417 return (32 * 2);
418 }