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