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