]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/in_gif.c
xnu-517.12.7.tar.gz
[apple/xnu.git] / bsd / netinet / in_gif.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
e5568f75
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
9bccf70c 22/* $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $ */
1c79356b
A
23
24/*
25 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
26 * All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. Neither the name of the project nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 */
52
1c79356b
A
53
54#include <sys/param.h>
55#include <sys/systm.h>
56#include <sys/socket.h>
57#include <sys/sockio.h>
1c79356b
A
58#include <sys/mbuf.h>
59#include <sys/errno.h>
1c79356b
A
60#include <sys/kernel.h>
61#include <sys/sysctl.h>
1c79356b 62
1c79356b 63#include <sys/malloc.h>
1c79356b
A
64
65#include <net/if.h>
66#include <net/route.h>
1c79356b
A
67
68#include <netinet/in.h>
69#include <netinet/in_systm.h>
70#include <netinet/ip.h>
71#include <netinet/ip_var.h>
72#include <netinet/in_gif.h>
73#include <netinet/in_var.h>
74#include <netinet/ip_encap.h>
75#include <netinet/ip_ecn.h>
76
77#if INET6
78#include <netinet/ip6.h>
79#endif
80
81#if MROUTING
82#include <netinet/ip_mroute.h>
83#endif /* MROUTING */
84
85#include <net/if_gif.h>
86
1c79356b
A
87#include <net/net_osdep.h>
88
9bccf70c
A
89int ip_gif_ttl = GIF_TTL;
90SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW,
91 &ip_gif_ttl, 0, "");
1c79356b
A
92
93int
94in_gif_output(ifp, family, m, rt)
95 struct ifnet *ifp;
96 int family;
97 struct mbuf *m;
98 struct rtentry *rt;
99{
9bccf70c 100 struct gif_softc *sc = (struct gif_softc*)ifp;
1c79356b
A
101 struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
102 struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
103 struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
104 struct ip iphdr; /* capsule IP header, host byte ordered */
105 int proto, error;
106 u_int8_t tos;
107
108 if (sin_src == NULL || sin_dst == NULL ||
109 sin_src->sin_family != AF_INET ||
110 sin_dst->sin_family != AF_INET) {
1c79356b
A
111 m_freem(m);
112 return EAFNOSUPPORT;
113 }
114
115 switch (family) {
116#if INET
117 case AF_INET:
118 {
119 struct ip *ip;
120
121 proto = IPPROTO_IPV4;
122 if (m->m_len < sizeof(*ip)) {
123 m = m_pullup(m, sizeof(*ip));
124 if (!m)
125 return ENOBUFS;
126 }
127 ip = mtod(m, struct ip *);
128 tos = ip->ip_tos;
129 break;
130 }
131#endif /*INET*/
132#if INET6
133 case AF_INET6:
134 {
135 struct ip6_hdr *ip6;
136 proto = IPPROTO_IPV6;
137 if (m->m_len < sizeof(*ip6)) {
138 m = m_pullup(m, sizeof(*ip6));
139 if (!m)
140 return ENOBUFS;
141 }
142 ip6 = mtod(m, struct ip6_hdr *);
143 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
144 break;
145 }
146#endif /*INET6*/
147 default:
148#if DEBUG
149 printf("in_gif_output: warning: unknown family %d passed\n",
150 family);
151#endif
152 m_freem(m);
153 return EAFNOSUPPORT;
154 }
155
156 bzero(&iphdr, sizeof(iphdr));
157 iphdr.ip_src = sin_src->sin_addr;
9bccf70c
A
158 /* bidirectional configured tunnel mode */
159 if (sin_dst->sin_addr.s_addr != INADDR_ANY)
160 iphdr.ip_dst = sin_dst->sin_addr;
161 else {
162 m_freem(m);
163 return ENETUNREACH;
1c79356b
A
164 }
165 iphdr.ip_p = proto;
166 /* version will be set in ip_output() */
167 iphdr.ip_ttl = ip_gif_ttl;
168 iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip);
169 if (ifp->if_flags & IFF_LINK1)
170 ip_ecn_ingress(ECN_ALLOWED, &iphdr.ip_tos, &tos);
9bccf70c
A
171 else
172 ip_ecn_ingress(ECN_NOCARE, &iphdr.ip_tos, &tos);
1c79356b
A
173
174 /* prepend new IP header */
175 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
176 if (m && m->m_len < sizeof(struct ip))
177 m = m_pullup(m, sizeof(struct ip));
178 if (m == NULL) {
179 printf("ENOBUFS in in_gif_output %d\n", __LINE__);
180 return ENOBUFS;
181 }
9bccf70c 182 bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
1c79356b
A
183
184 if (dst->sin_family != sin_dst->sin_family ||
185 dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
186 /* cache route doesn't match */
187 dst->sin_family = sin_dst->sin_family;
188 dst->sin_len = sizeof(struct sockaddr_in);
189 dst->sin_addr = sin_dst->sin_addr;
190 if (sc->gif_ro.ro_rt) {
9bccf70c 191 rtfree(sc->gif_ro.ro_rt);
1c79356b
A
192 sc->gif_ro.ro_rt = NULL;
193 }
194#if 0
195 sc->gif_if.if_mtu = GIF_MTU;
196#endif
197 }
198
199 if (sc->gif_ro.ro_rt == NULL) {
200 rtalloc(&sc->gif_ro);
201 if (sc->gif_ro.ro_rt == NULL) {
202 m_freem(m);
203 return ENETUNREACH;
204 }
9bccf70c
A
205
206 /* if it constitutes infinite encapsulation, punt. */
207 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
208 m_freem(m);
209 return ENETUNREACH; /*XXX*/
210 }
1c79356b
A
211#if 0
212 ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
213 - sizeof(struct ip);
214#endif
215 }
216
1c79356b 217 error = ip_output(m, NULL, &sc->gif_ro, 0, NULL);
1c79356b
A
218 return(error);
219}
220
221void
222in_gif_input(m, off)
223 struct mbuf *m;
224 int off;
225{
1c79356b
A
226 struct ifnet *gifp = NULL;
227 struct ip *ip;
228 int i, af, proto;
229 u_int8_t otos;
230
1c79356b 231 ip = mtod(m, struct ip *);
9bccf70c 232 proto = ip->ip_p;
1c79356b 233
1c79356b 234
1c79356b 235 gifp = (struct ifnet *)encap_getarg(m);
1c79356b 236
9bccf70c 237 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
1c79356b
A
238 m_freem(m);
239 ipstat.ips_nogif++;
240 return;
241 }
242
243 otos = ip->ip_tos;
244 m_adj(m, off);
245
246 switch (proto) {
247#if INET
248 case IPPROTO_IPV4:
249 {
250 struct ip *ip;
251 af = AF_INET;
252 if (m->m_len < sizeof(*ip)) {
253 m = m_pullup(m, sizeof(*ip));
254 if (!m)
255 return;
256 }
257 ip = mtod(m, struct ip *);
258 if (gifp->if_flags & IFF_LINK1)
259 ip_ecn_egress(ECN_ALLOWED, &otos, &ip->ip_tos);
9bccf70c
A
260 else
261 ip_ecn_egress(ECN_NOCARE, &otos, &ip->ip_tos);
1c79356b
A
262 break;
263 }
264#endif
265#if INET6
266 case IPPROTO_IPV6:
267 {
268 struct ip6_hdr *ip6;
269 u_int8_t itos;
270 af = AF_INET6;
271 if (m->m_len < sizeof(*ip6)) {
272 m = m_pullup(m, sizeof(*ip6));
273 if (!m)
274 return;
275 }
276 ip6 = mtod(m, struct ip6_hdr *);
277 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
278 if (gifp->if_flags & IFF_LINK1)
279 ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
9bccf70c
A
280 else
281 ip_ecn_egress(ECN_NOCARE, &otos, &itos);
1c79356b
A
282 ip6->ip6_flow &= ~htonl(0xff << 20);
283 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
284 break;
285 }
286#endif /* INET6 */
287 default:
288 ipstat.ips_nogif++;
289 m_freem(m);
290 return;
291 }
9bccf70c
A
292#ifdef __APPLE__
293 /* Should we free m if dlil_input returns an error? */
294 if (m->m_pkthdr.rcvif) /* replace the rcvif by gifp for dlil to route it correctly */
295 m->m_pkthdr.rcvif = gifp;
296 dlil_input_packet(gifp, m, NULL);
297#else
1c79356b 298 gif_input(m, af, gifp);
9bccf70c 299#endif
1c79356b
A
300 return;
301}
302
9bccf70c
A
303/*
304 * we know that we are in IFF_UP, outer address available, and outer family
305 * matched the physical addr family. see gif_encapcheck().
306 */
1c79356b 307int
9bccf70c
A
308gif_encapcheck4(m, off, proto, arg)
309 const struct mbuf *m;
310 int off;
311 int proto;
312 void *arg;
1c79356b 313{
9bccf70c
A
314 struct ip ip;
315 struct gif_softc *sc;
316 struct sockaddr_in *src, *dst;
317 int addrmatch;
318 struct in_ifaddr *ia4;
319
320 /* sanity check done in caller */
321 sc = (struct gif_softc *)arg;
322 src = (struct sockaddr_in *)sc->gif_psrc;
323 dst = (struct sockaddr_in *)sc->gif_pdst;
324
325 /* LINTED const cast */
326 m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
327
328 /* check for address match */
329 addrmatch = 0;
330 if (src->sin_addr.s_addr == ip.ip_dst.s_addr)
331 addrmatch |= 1;
332 if (dst->sin_addr.s_addr == ip.ip_src.s_addr)
333 addrmatch |= 2;
334 if (addrmatch != 3)
335 return 0;
336
337 /* martian filters on outer source - NOT done in ip_input! */
338 if (IN_MULTICAST(ntohl(ip.ip_src.s_addr)))
339 return 0;
340 switch ((ntohl(ip.ip_src.s_addr) & 0xff000000) >> 24) {
341 case 0: case 127: case 255:
342 return 0;
343 }
344 /* reject packets with broadcast on source */
345 for (ia4 = TAILQ_FIRST(&in_ifaddrhead); ia4;
346 ia4 = TAILQ_NEXT(ia4, ia_link))
347 {
348 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
349 continue;
350 if (ip.ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
351 return 0;
352 }
1c79356b 353
9bccf70c
A
354 /* ingress filters on outer source */
355 if ((sc->gif_if.if_flags & IFF_LINK2) == 0 &&
356 (m->m_flags & M_PKTHDR) != 0 && m->m_pkthdr.rcvif) {
357 struct sockaddr_in sin;
358 struct rtentry *rt;
359
360 bzero(&sin, sizeof(sin));
361 sin.sin_family = AF_INET;
362 sin.sin_len = sizeof(struct sockaddr_in);
363 sin.sin_addr = ip.ip_src;
364 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
365 if (!rt || rt->rt_ifp != m->m_pkthdr.rcvif) {
366#if 0
367 log(LOG_WARNING, "%s: packet from 0x%x dropped "
368 "due to ingress filter\n", if_name(&sc->gif_if),
369 (u_int32_t)ntohl(sin.sin_addr.s_addr));
370#endif
371 if (rt)
372 rtfree(rt);
373 return 0;
1c79356b 374 }
9bccf70c 375 rtfree(rt);
1c79356b
A
376 }
377
9bccf70c 378 return 32 * 2;
1c79356b 379}