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