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