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