]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/in_gif.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / bsd / netinet / in_gif.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
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.
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
9bccf70c 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>
1c79356b
A
70
71#include <net/if.h>
72#include <net/route.h>
1c79356b
A
73
74#include <netinet/in.h>
75#include <netinet/in_systm.h>
76#include <netinet/ip.h>
77#include <netinet/ip_var.h>
78#include <netinet/in_gif.h>
79#include <netinet/in_var.h>
80#include <netinet/ip_encap.h>
81#include <netinet/ip_ecn.h>
82
83#if INET6
84#include <netinet/ip6.h>
85#endif
86
87#if MROUTING
88#include <netinet/ip_mroute.h>
89#endif /* MROUTING */
90
91#include <net/if_gif.h>
92
1c79356b
A
93#include <net/net_osdep.h>
94
9bccf70c
A
95int ip_gif_ttl = GIF_TTL;
96SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW,
97 &ip_gif_ttl, 0, "");
1c79356b
A
98
99int
91447636
A
100in_gif_output(
101 struct ifnet *ifp,
102 int family,
103 struct mbuf *m,
104 struct rtentry *rt)
1c79356b 105{
9bccf70c 106 struct gif_softc *sc = (struct gif_softc*)ifp;
1c79356b
A
107 struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
108 struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
109 struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
110 struct ip iphdr; /* capsule IP header, host byte ordered */
111 int proto, error;
112 u_int8_t tos;
113
114 if (sin_src == NULL || sin_dst == NULL ||
115 sin_src->sin_family != AF_INET ||
116 sin_dst->sin_family != AF_INET) {
1c79356b
A
117 m_freem(m);
118 return EAFNOSUPPORT;
119 }
120
121 switch (family) {
122#if INET
123 case AF_INET:
124 {
125 struct ip *ip;
126
127 proto = IPPROTO_IPV4;
128 if (m->m_len < sizeof(*ip)) {
129 m = m_pullup(m, sizeof(*ip));
130 if (!m)
131 return ENOBUFS;
132 }
133 ip = mtod(m, struct ip *);
134 tos = ip->ip_tos;
135 break;
136 }
137#endif /*INET*/
138#if INET6
139 case AF_INET6:
140 {
141 struct ip6_hdr *ip6;
142 proto = IPPROTO_IPV6;
143 if (m->m_len < sizeof(*ip6)) {
144 m = m_pullup(m, sizeof(*ip6));
145 if (!m)
146 return ENOBUFS;
147 }
148 ip6 = mtod(m, struct ip6_hdr *);
149 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
150 break;
151 }
152#endif /*INET6*/
153 default:
154#if DEBUG
155 printf("in_gif_output: warning: unknown family %d passed\n",
156 family);
157#endif
158 m_freem(m);
159 return EAFNOSUPPORT;
160 }
161
162 bzero(&iphdr, sizeof(iphdr));
163 iphdr.ip_src = sin_src->sin_addr;
9bccf70c
A
164 /* bidirectional configured tunnel mode */
165 if (sin_dst->sin_addr.s_addr != INADDR_ANY)
166 iphdr.ip_dst = sin_dst->sin_addr;
167 else {
168 m_freem(m);
169 return ENETUNREACH;
1c79356b
A
170 }
171 iphdr.ip_p = proto;
172 /* version will be set in ip_output() */
173 iphdr.ip_ttl = ip_gif_ttl;
174 iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip);
175 if (ifp->if_flags & IFF_LINK1)
176 ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
9bccf70c
A
177 else
178 ip_ecn_ingress(ECN_NOCARE, &iphdr.ip_tos, &tos);
1c79356b
A
179
180 /* prepend new IP header */
181 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
182 if (m && m->m_len < sizeof(struct ip))
183 m = m_pullup(m, sizeof(struct ip));
184 if (m == NULL) {
185 printf("ENOBUFS in in_gif_output %d\n", __LINE__);
186 return ENOBUFS;
187 }
9bccf70c 188 bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
1c79356b
A
189
190 if (dst->sin_family != sin_dst->sin_family ||
191 dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
192 /* cache route doesn't match */
193 dst->sin_family = sin_dst->sin_family;
194 dst->sin_len = sizeof(struct sockaddr_in);
195 dst->sin_addr = sin_dst->sin_addr;
196 if (sc->gif_ro.ro_rt) {
9bccf70c 197 rtfree(sc->gif_ro.ro_rt);
1c79356b
A
198 sc->gif_ro.ro_rt = NULL;
199 }
200#if 0
201 sc->gif_if.if_mtu = GIF_MTU;
202#endif
203 }
204
205 if (sc->gif_ro.ro_rt == NULL) {
206 rtalloc(&sc->gif_ro);
207 if (sc->gif_ro.ro_rt == NULL) {
208 m_freem(m);
209 return ENETUNREACH;
210 }
9bccf70c
A
211
212 /* if it constitutes infinite encapsulation, punt. */
213 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
214 m_freem(m);
215 return ENETUNREACH; /*XXX*/
216 }
1c79356b
A
217#if 0
218 ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
219 - sizeof(struct ip);
220#endif
221 }
222
1c79356b 223 error = ip_output(m, NULL, &sc->gif_ro, 0, NULL);
1c79356b
A
224 return(error);
225}
226
227void
228in_gif_input(m, off)
229 struct mbuf *m;
230 int off;
231{
1c79356b
A
232 struct ifnet *gifp = NULL;
233 struct ip *ip;
234 int i, af, proto;
235 u_int8_t otos;
236
1c79356b 237 ip = mtod(m, struct ip *);
9bccf70c 238 proto = ip->ip_p;
1c79356b 239
1c79356b 240
1c79356b 241 gifp = (struct ifnet *)encap_getarg(m);
1c79356b 242
9bccf70c 243 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
1c79356b
A
244 m_freem(m);
245 ipstat.ips_nogif++;
246 return;
247 }
248
249 otos = ip->ip_tos;
250 m_adj(m, off);
251
252 switch (proto) {
253#if INET
254 case IPPROTO_IPV4:
255 {
256 struct ip *ip;
257 af = AF_INET;
258 if (m->m_len < 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);
9bccf70c
A
266 else
267 ip_ecn_egress(ECN_NOCARE, &otos, &ip->ip_tos);
1c79356b
A
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 (m->m_len < 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);
9bccf70c
A
286 else
287 ip_ecn_egress(ECN_NOCARE, &otos, &itos);
1c79356b
A
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 ipstat.ips_nogif++;
295 m_freem(m);
296 return;
297 }
9bccf70c
A
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 dlil_input_packet(gifp, m, NULL);
303#else
1c79356b 304 gif_input(m, af, gifp);
9bccf70c 305#endif
1c79356b
A
306 return;
307}
308
9bccf70c
A
309/*
310 * we know that we are in IFF_UP, outer address available, and outer family
311 * matched the physical addr family. see gif_encapcheck().
312 */
1c79356b 313int
9bccf70c
A
314gif_encapcheck4(m, off, proto, arg)
315 const struct mbuf *m;
316 int off;
317 int proto;
318 void *arg;
1c79356b 319{
9bccf70c
A
320 struct ip ip;
321 struct gif_softc *sc;
322 struct sockaddr_in *src, *dst;
323 int addrmatch;
324 struct in_ifaddr *ia4;
325
326 /* sanity check done in caller */
327 sc = (struct gif_softc *)arg;
328 src = (struct sockaddr_in *)sc->gif_psrc;
329 dst = (struct sockaddr_in *)sc->gif_pdst;
330
331 /* LINTED const cast */
332 m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
333
334 /* check for address match */
335 addrmatch = 0;
336 if (src->sin_addr.s_addr == ip.ip_dst.s_addr)
337 addrmatch |= 1;
338 if (dst->sin_addr.s_addr == ip.ip_src.s_addr)
339 addrmatch |= 2;
340 if (addrmatch != 3)
341 return 0;
342
343 /* martian filters on outer source - NOT done in ip_input! */
344 if (IN_MULTICAST(ntohl(ip.ip_src.s_addr)))
345 return 0;
346 switch ((ntohl(ip.ip_src.s_addr) & 0xff000000) >> 24) {
347 case 0: case 127: case 255:
348 return 0;
349 }
350 /* reject packets with broadcast on source */
91447636 351 lck_mtx_lock(rt_mtx);
9bccf70c
A
352 for (ia4 = TAILQ_FIRST(&in_ifaddrhead); ia4;
353 ia4 = TAILQ_NEXT(ia4, ia_link))
354 {
355 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
356 continue;
91447636
A
357 if (ip.ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
358 lck_mtx_unlock(rt_mtx);
9bccf70c 359 return 0;
91447636 360 }
9bccf70c 361 }
91447636 362 lck_mtx_unlock(rt_mtx);
1c79356b 363
9bccf70c
A
364 /* ingress filters on outer source */
365 if ((sc->gif_if.if_flags & IFF_LINK2) == 0 &&
366 (m->m_flags & M_PKTHDR) != 0 && m->m_pkthdr.rcvif) {
367 struct sockaddr_in sin;
368 struct rtentry *rt;
369
370 bzero(&sin, sizeof(sin));
371 sin.sin_family = AF_INET;
372 sin.sin_len = sizeof(struct sockaddr_in);
373 sin.sin_addr = ip.ip_src;
374 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
375 if (!rt || rt->rt_ifp != m->m_pkthdr.rcvif) {
376#if 0
377 log(LOG_WARNING, "%s: packet from 0x%x dropped "
378 "due to ingress filter\n", if_name(&sc->gif_if),
379 (u_int32_t)ntohl(sin.sin_addr.s_addr));
380#endif
381 if (rt)
382 rtfree(rt);
383 return 0;
1c79356b 384 }
9bccf70c 385 rtfree(rt);
1c79356b
A
386 }
387
9bccf70c 388 return 32 * 2;
1c79356b 389}