]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/in6_gif.c
xnu-1228.tar.gz
[apple/xnu.git] / bsd / netinet6 / in6_gif.c
CommitLineData
9bccf70c
A
1/* $FreeBSD: src/sys/netinet6/in6_gif.c,v 1.2.2.3 2001/07/03 11:01:52 ume Exp $ */
2/* $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $ */
1c79356b
A
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
1c79356b
A
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/socket.h>
37#include <sys/sockio.h>
38#include <sys/mbuf.h>
39#include <sys/errno.h>
9bccf70c
A
40#include <sys/queue.h>
41#include <sys/syslog.h>
42
1c79356b
A
43#include <sys/malloc.h>
44#include <sys/protosw.h>
45
46#include <net/if.h>
47#include <net/route.h>
48
49#include <netinet/in.h>
50#include <netinet/in_systm.h>
51#if INET
52#include <netinet/ip.h>
53#endif
54#include <netinet/ip_encap.h>
55#if INET6
56#include <netinet/ip6.h>
57#include <netinet6/ip6_var.h>
58#include <netinet6/in6_gif.h>
59#include <netinet6/in6_var.h>
1c79356b
A
60#endif
61#include <netinet/ip_ecn.h>
9bccf70c
A
62#if INET6
63#include <netinet6/ip6_ecn.h>
64#endif
1c79356b
A
65
66#include <net/if_gif.h>
67
68#include <net/net_osdep.h>
69
2d21ac55
A
70static __inline__ void*
71_cast_non_const(const void * ptr) {
72 union {
73 const void* cval;
74 void* val;
75 } ret;
76
77 ret.cval = ptr;
78 return (ret.val);
79}
80
1c79356b 81int
91447636
A
82in6_gif_output(
83 struct ifnet *ifp,
84 int family, /* family of the packet to be encapsulate. */
85 struct mbuf *m,
2d21ac55 86 __unused struct rtentry *rt)
1c79356b 87{
2d21ac55 88 struct gif_softc *sc = ifnet_softc(ifp);
1c79356b
A
89 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
90 struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
91 struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
92 struct ip6_hdr *ip6;
93 int proto;
94 u_int8_t itos, otos;
95
96 if (sin6_src == NULL || sin6_dst == NULL ||
97 sin6_src->sin6_family != AF_INET6 ||
98 sin6_dst->sin6_family != AF_INET6) {
99 m_freem(m);
100 return EAFNOSUPPORT;
101 }
102
103 switch (family) {
104#if INET
105 case AF_INET:
106 {
107 struct ip *ip;
108
109 proto = IPPROTO_IPV4;
2d21ac55 110 if (mbuf_len(m) < sizeof(*ip)) {
1c79356b
A
111 m = m_pullup(m, sizeof(*ip));
112 if (!m)
113 return ENOBUFS;
114 }
115 ip = mtod(m, struct ip *);
116 itos = ip->ip_tos;
117 break;
118 }
119#endif
120#if INET6
121 case AF_INET6:
122 {
1c79356b 123 proto = IPPROTO_IPV6;
2d21ac55 124 if (mbuf_len(m) < sizeof(*ip6)) {
1c79356b
A
125 m = m_pullup(m, sizeof(*ip6));
126 if (!m)
127 return ENOBUFS;
128 }
129 ip6 = mtod(m, struct ip6_hdr *);
130 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
131 break;
132 }
133#endif
134 default:
135#if DEBUG
136 printf("in6_gif_output: warning: unknown family %d passed\n",
137 family);
138#endif
139 m_freem(m);
140 return EAFNOSUPPORT;
141 }
142
143 /* prepend new IP header */
144 M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
2d21ac55 145 if (m && mbuf_len(m) < sizeof(struct ip6_hdr))
1c79356b
A
146 m = m_pullup(m, sizeof(struct ip6_hdr));
147 if (m == NULL) {
148 printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
149 return ENOBUFS;
150 }
151
152 ip6 = mtod(m, struct ip6_hdr *);
153 ip6->ip6_flow = 0;
154 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
155 ip6->ip6_vfc |= IPV6_VERSION;
156 ip6->ip6_plen = htons((u_short)m->m_pkthdr.len);
157 ip6->ip6_nxt = proto;
158 ip6->ip6_hlim = ip6_gif_hlim;
159 ip6->ip6_src = sin6_src->sin6_addr;
9bccf70c
A
160 /* bidirectional configured tunnel mode */
161 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
162 ip6->ip6_dst = sin6_dst->sin6_addr;
163 else {
164 m_freem(m);
165 return ENETUNREACH;
1c79356b 166 }
9bccf70c 167 if (ifp->if_flags & IFF_LINK1)
1c79356b 168 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
9bccf70c
A
169 else
170 ip_ecn_ingress(ECN_NOCARE, &otos, &itos);
171 ip6->ip6_flow &= ~ntohl(0xff00000);
172 ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
1c79356b
A
173
174 if (dst->sin6_family != sin6_dst->sin6_family ||
175 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
176 /* cache route doesn't match */
177 bzero(dst, sizeof(*dst));
178 dst->sin6_family = sin6_dst->sin6_family;
179 dst->sin6_len = sizeof(struct sockaddr_in6);
180 dst->sin6_addr = sin6_dst->sin6_addr;
181 if (sc->gif_ro6.ro_rt) {
9bccf70c 182 rtfree(sc->gif_ro6.ro_rt);
1c79356b
A
183 sc->gif_ro6.ro_rt = NULL;
184 }
185#if 0
186 sc->gif_if.if_mtu = GIF_MTU;
187#endif
188 }
189
190 if (sc->gif_ro6.ro_rt == NULL) {
191 rtalloc((struct route *)&sc->gif_ro6);
192 if (sc->gif_ro6.ro_rt == NULL) {
193 m_freem(m);
194 return ENETUNREACH;
195 }
9bccf70c
A
196
197 /* if it constitutes infinite encapsulation, punt. */
198 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
199 m_freem(m);
200 return ENETUNREACH; /*XXX*/
201 }
1c79356b
A
202#if 0
203 ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
204 - sizeof(struct ip6_hdr);
205#endif
206 }
207
9bccf70c
A
208#if IPV6_MINMTU
209 /*
210 * force fragmentation to minimum MTU, to avoid path MTU discovery.
211 * it is too painful to ask for resend of inner packet, to achieve
212 * path MTU discovery for encapsulated packets.
213 */
91447636 214 return(ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, 0));
9bccf70c 215#else
91447636 216 return(ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, 0));
9bccf70c 217#endif
1c79356b
A
218}
219
9bccf70c 220int in6_gif_input(mp, offp)
1c79356b 221 struct mbuf **mp;
9bccf70c 222 int *offp;
1c79356b
A
223{
224 struct mbuf *m = *mp;
1c79356b
A
225 struct ifnet *gifp = NULL;
226 struct ip6_hdr *ip6;
1c79356b
A
227 int af = 0;
228 u_int32_t otos;
9bccf70c 229 u_int8_t proto;
1c79356b
A
230
231 ip6 = mtod(m, struct ip6_hdr *);
232
2d21ac55 233 gifp = ((struct gif_softc*)encap_getarg(m))->gif_if;
1c79356b 234
9bccf70c 235 if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
1c79356b
A
236 m_freem(m);
237 ip6stat.ip6s_nogif++;
238 return IPPROTO_DONE;
239 }
240
9bccf70c 241 proto = ip6->ip6_nxt;
1c79356b
A
242 otos = ip6->ip6_flow;
243 m_adj(m, *offp);
244
245 switch (proto) {
246#if INET
247 case IPPROTO_IPV4:
248 {
249 struct ip *ip;
250 u_int8_t otos8;
251 af = AF_INET;
252 otos8 = (ntohl(otos) >> 20) & 0xff;
2d21ac55 253 if (mbuf_len(m) < sizeof(*ip)) {
1c79356b
A
254 m = m_pullup(m, sizeof(*ip));
255 if (!m)
256 return IPPROTO_DONE;
257 }
258 ip = mtod(m, struct ip *);
259 if (gifp->if_flags & IFF_LINK1)
260 ip_ecn_egress(ECN_ALLOWED, &otos8, &ip->ip_tos);
9bccf70c
A
261 else
262 ip_ecn_egress(ECN_NOCARE, &otos8, &ip->ip_tos);
1c79356b
A
263 break;
264 }
265#endif /* INET */
266#if INET6
267 case IPPROTO_IPV6:
268 {
1c79356b 269 af = AF_INET6;
2d21ac55 270 if (mbuf_len(m) < sizeof(*ip6)) {
1c79356b
A
271 m = m_pullup(m, sizeof(*ip6));
272 if (!m)
273 return IPPROTO_DONE;
274 }
275 ip6 = mtod(m, struct ip6_hdr *);
276 if (gifp->if_flags & IFF_LINK1)
277 ip6_ecn_egress(ECN_ALLOWED, &otos, &ip6->ip6_flow);
9bccf70c
A
278 else
279 ip6_ecn_egress(ECN_NOCARE, &otos, &ip6->ip6_flow);
1c79356b
A
280 break;
281 }
282#endif
283 default:
284 ip6stat.ip6s_nogif++;
285 m_freem(m);
286 return IPPROTO_DONE;
287 }
2d21ac55
A
288
289 if (m->m_pkthdr.rcvif) /* replace the rcvif by gifp for ifnet_input to route it correctly */
290 m->m_pkthdr.rcvif = gifp;
291
292 ifnet_input(gifp, m, NULL);
1c79356b
A
293 return IPPROTO_DONE;
294}
295
9bccf70c 296/*
55e303ae 297 * validate outer address.
9bccf70c 298 */
55e303ae 299static int
2d21ac55
A
300gif_validate6(
301 const struct ip6_hdr *ip6,
302 struct gif_softc *sc,
303 struct ifnet *ifp)
55e303ae 304{
9bccf70c 305 struct sockaddr_in6 *src, *dst;
9bccf70c 306
9bccf70c
A
307 src = (struct sockaddr_in6 *)sc->gif_psrc;
308 dst = (struct sockaddr_in6 *)sc->gif_pdst;
309
55e303ae
A
310 /*
311 * Check for address match. Note that the check is for an incoming
312 * packet. We should compare the *source* address in our configuration
313 * and the *destination* address of the packet, and vice versa.
314 */
315 if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
316 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
9bccf70c
A
317 return 0;
318
319 /* martian filters on outer source - done in ip6_input */
320
321 /* ingress filters on outer source */
2d21ac55 322 if ((ifnet_flags(sc->gif_if) & IFF_LINK2) == 0 && ifp) {
9bccf70c
A
323 struct sockaddr_in6 sin6;
324 struct rtentry *rt;
325
326 bzero(&sin6, sizeof(sin6));
327 sin6.sin6_family = AF_INET6;
328 sin6.sin6_len = sizeof(struct sockaddr_in6);
55e303ae
A
329 sin6.sin6_addr = ip6->ip6_src;
330#ifndef SCOPEDROUTING
331 sin6.sin6_scope_id = 0; /* XXX */
332#endif
333
9bccf70c 334 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
55e303ae 335 if (!rt || rt->rt_ifp != ifp) {
9bccf70c
A
336#if 0
337 log(LOG_WARNING, "%s: packet from %s dropped "
338 "due to ingress filter\n", if_name(&sc->gif_if),
339 ip6_sprintf(&sin6.sin6_addr));
1c79356b 340#endif
9bccf70c
A
341 if (rt)
342 rtfree(rt);
343 return 0;
1c79356b 344 }
9bccf70c 345 rtfree(rt);
1c79356b
A
346 }
347
9bccf70c 348 return 128 * 2;
1c79356b 349}
55e303ae
A
350
351/*
352 * we know that we are in IFF_UP, outer address available, and outer family
353 * matched the physical addr family. see gif_encapcheck().
354 * sanity check for arg should have been done in the caller.
355 */
356int
2d21ac55
A
357gif_encapcheck6(
358 const struct mbuf *m,
359 __unused int off,
360 __unused int proto,
361 void *arg)
55e303ae
A
362{
363 struct ip6_hdr ip6;
364 struct gif_softc *sc;
365 struct ifnet *ifp;
366
367 /* sanity check done in caller */
368 sc = (struct gif_softc *)arg;
369
2d21ac55 370 mbuf_copydata(m, 0, sizeof(ip6), &ip6);
55e303ae
A
371 ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
372
373 return gif_validate6(&ip6, sc, ifp);
374}