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