1/* $FreeBSD: head/sys/net/if_stf.c 129880 2004-05-30 20:27:19Z phk $ */
2/* $KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi Exp $ */
3
4/*
5 * Copyright (C) 2000 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
33/*
34 * 6to4 interface, based on RFC3056.
35 *
36 * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
37 * There is no address mapping defined from IPv6 multicast address to IPv4
38 * address. Therefore, we do not have IFF_MULTICAST on the interface.
39 *
40 * Due to the lack of address mapping for link-local addresses, we cannot
41 * throw packets toward link-local addresses (fe80::x). Also, we cannot throw
42 * packets to link-local multicast addresses (ff02::x).
43 *
44 * Here are interesting symptoms due to the lack of link-local address:
45 *
46 * Unicast routing exchange:
47 * - RIPng: Impossible. Uses link-local multicast packet toward ff02::9,
48 * and link-local addresses as nexthop.
49 * - OSPFv6: Impossible. OSPFv6 assumes that there's link-local address
50 * assigned to the link, and makes use of them. Also, HELLO packets use
51 * link-local multicast addresses (ff02::5 and ff02::6).
52 * - BGP4+: Maybe. You can only use global address as nexthop, and global
53 * address as TCP endpoint address.
54 *
55 * Multicast routing protocols:
56 * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
57 * Adjacent PIM routers must be configured manually (is it really spec-wise
58 * correct thing to do?).
59 *
60 * ICMPv6:
61 * - Redirects cannot be used due to the lack of link-local address.
62 *
63 * stf interface does not have, and will not need, a link-local address.
64 * It seems to have no real benefit and does not help the above symptoms much.
65 * Even if we assign link-locals to interface, we cannot really
66 * use link-local unicast/multicast on top of 6to4 cloud (since there's no
67 * encapsulation defined for link-local address), and the above analysis does
68 * not change. RFC3056 does not mandate the assignment of link-local address
69 * either.
70 *
71 * 6to4 interface has security issues. Refer to
72 * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
73 * for details. The code tries to filter out some of malicious packets.
74 * Note that there is no way to be 100% secure.
75 */
76
77#include "opt_inet.h"
78#include "opt_inet6.h"
79#include "opt_mac.h"
80
81#include <sys/param.h>
82#include <sys/systm.h>
83#include <sys/socket.h>
84#include <sys/sockio.h>
85#include <sys/mac.h>
86#include <sys/mbuf.h>
87#include <sys/errno.h>
88#include <sys/kernel.h>
89#include <sys/module.h>
90#include <sys/protosw.h>
91#include <sys/queue.h>
92#include <machine/cpu.h>
93
94#include <sys/malloc.h>
95
96#include <net/if.h>
97#include <net/route.h>
98#include <net/netisr.h>
99#include <net/if_types.h>
100#include <net/if_stf.h>
101
102#include <netinet/in.h>
103#include <netinet/in_systm.h>
104#include <netinet/ip.h>
105#include <netinet/ip_var.h>
106#include <netinet/in_var.h>
107
108#include <netinet/ip6.h>
109#include <netinet6/ip6_var.h>
110#include <netinet6/in6_var.h>
111#include <netinet/ip_ecn.h>
112
113#include <netinet/ip_encap.h>
114
115#include <machine/stdarg.h>
116
117#include <net/net_osdep.h>
118
119#include <net/bpf.h>
120
121#define STFNAME "stf"
122
123#define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002)
124
125/*
126 * XXX: Return a pointer with 16-bit aligned. Don't cast it to
127 * struct in_addr *; use bcopy() instead.
128 */
129#define GET_V4(x) ((caddr_t)(&(x)->s6_addr16[1]))
130
131struct stf_softc {
132 struct ifnet sc_if; /* common area */
133 union {
134 struct route __sc_ro4;
135 struct route_in6 __sc_ro6; /* just for safety */
136 } __sc_ro46;
137#define sc_ro __sc_ro46.__sc_ro4
138 const struct encaptab *encap_cookie;
139 LIST_ENTRY(stf_softc) sc_list; /* all stf's are linked */
140};
141
142/*
143 * All mutable global variables in if_stf.c are protected by stf_mtx.
144 * XXXRW: Note that mutable fields in the softc are not currently locked:
145 * in particular, sc_ro needs to be protected from concurrent entrance
146 * of stf_output().
147 */
148static struct mtx stf_mtx;
149static LIST_HEAD(, stf_softc) stf_softc_list;
150
151static MALLOC_DEFINE(M_STF, STFNAME, "6to4 Tunnel Interface");
152static const int ip_stf_ttl = 40;
153
154extern struct domain inetdomain;
155struct protosw in_stf_protosw =
156{ SOCK_RAW, &inetdomain, IPPROTO_IPV6, PR_ATOMIC|PR_ADDR,
157 in_stf_input, (pr_output_t*)rip_output, 0, rip_ctloutput,
158 0,
159 0, 0, 0, 0,
160 &rip_usrreqs
161};
162
163static int stfmodevent(module_t, int, void *);
164static int stf_encapcheck(const struct mbuf *, int, int, void *);
165static struct in6_ifaddr *stf_getsrcifa6(struct ifnet *);
166static int stf_output(struct ifnet *, struct mbuf *, struct sockaddr *,
167 struct rtentry *);
168static int isrfc1918addr(struct in_addr *);
169static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
170 struct ifnet *);
171static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
172 struct ifnet *);
173static void stf_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
174static int stf_ioctl(struct ifnet *, u_long, caddr_t);
175
176static int stf_clone_create(struct if_clone *, int);
177static void stf_clone_destroy(struct ifnet *);
178
179/* only one clone is currently allowed */
180struct if_clone stf_cloner =
181 IF_CLONE_INITIALIZER(STFNAME, stf_clone_create, stf_clone_destroy, 0, 0);
182
183static int
184stf_clone_create(ifc, unit)
185 struct if_clone *ifc;
186 int unit;
187{
188 struct stf_softc *sc;
189 struct ifnet *ifp;
190
191 sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO);
192 ifp = &sc->sc_if;
193 if_initname(ifp, ifc->ifc_name, unit);
194
195 sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
196 stf_encapcheck, &in_stf_protosw, sc);
197 if (sc->encap_cookie == NULL) {
198 if_printf(ifp, "attach failed\n");
199 free(sc, M_STF);
200 return (ENOMEM);
201 }
202
203 ifp->if_mtu = IPV6_MMTU;
204 ifp->if_ioctl = stf_ioctl;
205 ifp->if_output = stf_output;
206 ifp->if_type = IFT_STF;
207 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
208 if_attach(ifp);
209 bpfattach(ifp, DLT_NULL, sizeof(u_int));
210 mtx_lock(&stf_mtx);
211 LIST_INSERT_HEAD(&stf_softc_list, sc, sc_list);
212 mtx_unlock(&stf_mtx);
213 return (0);
214}
215
216static void
217stf_destroy(struct stf_softc *sc)
218{
219 int err;
220
221 err = encap_detach(sc->encap_cookie);
222 KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
223 bpfdetach(&sc->sc_if);
224 if_detach(&sc->sc_if);
225
226 free(sc, M_STF);
227}
228
229static void
230stf_clone_destroy(ifp)
231 struct ifnet *ifp;
232{
233 struct stf_softc *sc = (void *) ifp;
234
235 mtx_lock(&stf_mtx);
236 LIST_REMOVE(sc, sc_list);
237 mtx_unlock(&stf_mtx);
238
239 stf_destroy(sc);
240}
241
242static int
243stfmodevent(mod, type, data)
244 module_t mod;
245 int type;
246 void *data;
247{
248 struct stf_softc *sc;
249
250 switch (type) {
251 case MOD_LOAD:
252 mtx_init(&stf_mtx, "stf_mtx", NULL, MTX_DEF);
253 LIST_INIT(&stf_softc_list);
254 if_clone_attach(&stf_cloner);
255
256 break;
257 case MOD_UNLOAD:
258 if_clone_detach(&stf_cloner);
259
260 mtx_lock(&stf_mtx);
261 while ((sc = LIST_FIRST(&stf_softc_list)) != NULL) {
262 LIST_REMOVE(sc, sc_list);
263 mtx_unlock(&stf_mtx);
264 stf_destroy(sc);
265 mtx_lock(&stf_mtx);
266 }
267 mtx_unlock(&stf_mtx);
268 mtx_destroy(&stf_mtx);
269 break;
270 }
271
272 return (0);
273}
274
275static moduledata_t stf_mod = {
276 "if_stf",
277 stfmodevent,
278 0
279};
280
281DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
282
283static int
284stf_encapcheck(m, off, proto, arg)
285 const struct mbuf *m;
286 int off;
287 int proto;
288 void *arg;
289{
290 struct ip ip;
291 struct in6_ifaddr *ia6;
292 struct stf_softc *sc;
293 struct in_addr a, b, mask;
294
295 sc = (struct stf_softc *)arg;
296 if (sc == NULL)
297 return 0;
298
299 if ((sc->sc_if.if_flags & IFF_UP) == 0)
300 return 0;
301
302 /* IFF_LINK0 means "no decapsulation" */
303 if ((sc->sc_if.if_flags & IFF_LINK0) != 0)
304 return 0;
305
306 if (proto != IPPROTO_IPV6)
307 return 0;
308
309 /* LINTED const cast */
310 m_copydata((struct mbuf *)(uintptr_t)m, 0, sizeof(ip), (caddr_t)&ip);
311
312 if (ip.ip_v != 4)
313 return 0;
314
315 ia6 = stf_getsrcifa6(&sc->sc_if);
316 if (ia6 == NULL)
317 return 0;
318
319 /*
320 * check if IPv4 dst matches the IPv4 address derived from the
321 * local 6to4 address.
322 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
323 */
324 if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
325 sizeof(ip.ip_dst)) != 0)
326 return 0;
327
328 /*
329 * check if IPv4 src matches the IPv4 address derived from the
330 * local 6to4 address masked by prefixmask.
331 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
332 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
333 */
334 bzero(&a, sizeof(a));
335 bcopy(GET_V4(&ia6->ia_addr.sin6_addr), &a, sizeof(a));
336 bcopy(GET_V4(&ia6->ia_prefixmask.sin6_addr), &mask, sizeof(mask));
337 a.s_addr &= mask.s_addr;
338 b = ip.ip_src;
339 b.s_addr &= mask.s_addr;
340 if (a.s_addr != b.s_addr)
341 return 0;
342
343 /* stf interface makes single side match only */
344 return 32;
345}
346
347static struct in6_ifaddr *
348stf_getsrcifa6(ifp)
349 struct ifnet *ifp;
350{
351 struct ifaddr *ia;
352 struct in_ifaddr *ia4;
353 struct sockaddr_in6 *sin6;
354 struct in_addr in;
355
356 for (ia = TAILQ_FIRST(&ifp->if_addrlist);
357 ia;
358 ia = TAILQ_NEXT(ia, ifa_list))
359 {
360 if (ia->ifa_addr == NULL)
361 continue;
362 if (ia->ifa_addr->sa_family != AF_INET6)
363 continue;
364 sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
365 if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
366 continue;
367
368 bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
369 LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
370 if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
371 break;
372 if (ia4 == NULL)
373 continue;
374
375 return (struct in6_ifaddr *)ia;
376 }
377
378 return NULL;
379}
380
381static int
382stf_output(ifp, m, dst, rt)
383 struct ifnet *ifp;
384 struct mbuf *m;
385 struct sockaddr *dst;
386 struct rtentry *rt;
387{
388 struct stf_softc *sc;
389 struct sockaddr_in6 *dst6;
390 struct in_addr in4;
391 caddr_t ptr;
392 struct sockaddr_in *dst4;
393 u_int8_t tos;
394 struct ip *ip;
395 struct ip6_hdr *ip6;
396 struct in6_ifaddr *ia6;
397#ifdef MAC
398 int error;
399
400 error = mac_check_ifnet_transmit(ifp, m);
401 if (error) {
402 m_freem(m);
403 return (error);
404 }
405#endif
406
407 sc = (struct stf_softc*)ifp;
408 dst6 = (struct sockaddr_in6 *)dst;
409
410 /* just in case */
411 if ((ifp->if_flags & IFF_UP) == 0) {
412 m_freem(m);
413 ifp->if_oerrors++;
414 return ENETDOWN;
415 }
416
417 /*
418 * If we don't have an ip4 address that match my inner ip6 address,
419 * we shouldn't generate output. Without this check, we'll end up
420 * using wrong IPv4 source.
421 */
422 ia6 = stf_getsrcifa6(ifp);
423 if (ia6 == NULL) {
424 m_freem(m);
425 ifp->if_oerrors++;
426 return ENETDOWN;
427 }
428
429 if (m->m_len < sizeof(*ip6)) {
430 m = m_pullup(m, sizeof(*ip6));
431 if (!m) {
432 ifp->if_oerrors++;
433 return ENOBUFS;
434 }
435 }
436 ip6 = mtod(m, struct ip6_hdr *);
437 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
438
439 /*
440 * Pickup the right outer dst addr from the list of candidates.
441 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
442 */
443 ptr = NULL;
444 if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
445 ptr = GET_V4(&ip6->ip6_dst);
446 else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
447 ptr = GET_V4(&dst6->sin6_addr);
448 else {
449 m_freem(m);
450 ifp->if_oerrors++;
451 return ENETUNREACH;
452 }
453 bcopy(ptr, &in4, sizeof(in4));
454
455#if NBPFILTER > 0
456 if (ifp->if_bpf) {
457 /*
458 * We need to prepend the address family as
459 * a four byte field. Cons up a dummy header
460 * to pacify bpf. This is safe because bpf
461 * will only read from the mbuf (i.e., it won't
462 * try to free it or keep a pointer a to it).
463 */
464 u_int32_t af = AF_INET6;
465#ifdef HAVE_OLD_BPF
466 struct mbuf m0;
467
468 m0.m_next = m;
469 m0.m_len = 4;
470 m0.m_data = (char *)⁡
471
472 BPF_MTAP(ifp, &m0);
473#else
474 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
475#endif
476 }
477#endif /*NBPFILTER > 0*/
478
479 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
480 if (m && m->m_len < sizeof(struct ip))
481 m = m_pullup(m, sizeof(struct ip));
482 if (m == NULL) {
483 ifp->if_oerrors++;
484 return ENOBUFS;
485 }
486 ip = mtod(m, struct ip *);
487
488 bzero(ip, sizeof(*ip));
489
490 bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
491 &ip->ip_src, sizeof(ip->ip_src));
492 bcopy(&in4, &ip->ip_dst, sizeof(ip->ip_dst));
493 ip->ip_p = IPPROTO_IPV6;
494 ip->ip_ttl = ip_stf_ttl;
495 ip->ip_len = m->m_pkthdr.len; /*host order*/
496 if (ifp->if_flags & IFF_LINK1)
497 ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
498 else
499 ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
500
501 /*
502 * XXXRW: Locking of sc_ro required.
503 */
504 dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
505 if (dst4->sin_family != AF_INET ||
506 bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
507 /* cache route doesn't match */
508 dst4->sin_family = AF_INET;
509 dst4->sin_len = sizeof(struct sockaddr_in);
510 bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
511 if (sc->sc_ro.ro_rt) {
512 RTFREE(sc->sc_ro.ro_rt);
513 sc->sc_ro.ro_rt = NULL;
514 }
515 }
516
517 if (sc->sc_ro.ro_rt == NULL) {
518 rtalloc(&sc->sc_ro);
519 if (sc->sc_ro.ro_rt == NULL) {
520 m_freem(m);
521 ifp->if_oerrors++;
522 return ENETUNREACH;
523 }
524 }
525
526 ifp->if_opackets++;
527 return ip_output(m, NULL, &sc->sc_ro, 0, NULL, NULL);
528}
529
530static int
531isrfc1918addr(in)
532 struct in_addr *in;
533{
534 /*
535 * returns 1 if private address range:
536 * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
537 */
538 if ((ntohl(in->s_addr) & 0xff000000) >> 24 == 10 ||
539 (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 ||
540 (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168)
541 return 1;
542
543 return 0;
544}
545
546static int
547stf_checkaddr4(sc, in, inifp)
548 struct stf_softc *sc;
549 struct in_addr *in;
550 struct ifnet *inifp; /* incoming interface */
551{
552 struct in_ifaddr *ia4;
553
554 /*
555 * reject packets with the following address:
556 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
557 */
558 if (IN_MULTICAST(ntohl(in->s_addr)))
559 return -1;
560 switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
561 case 0: case 127: case 255:
562 return -1;
563 }
564
565 /*
566 * reject packets with private address range.
567 * (requirement from RFC3056 section 2 1st paragraph)
568 */
569 if (isrfc1918addr(in))
570 return -1;
571
572 /*
573 * reject packets with broadcast
574 */
575 for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
576 ia4;
577 ia4 = TAILQ_NEXT(ia4, ia_link))
578 {
579 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
580 continue;
581 if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
582 return -1;
583 }
584
585 /*
586 * perform ingress filter
587 */
588 if (sc && (sc->sc_if.if_flags & IFF_LINK2) == 0 && inifp) {
589 struct sockaddr_in sin;
590 struct rtentry *rt;
591
592 bzero(&sin, sizeof(sin));
593 sin.sin_family = AF_INET;
594 sin.sin_len = sizeof(struct sockaddr_in);
595 sin.sin_addr = *in;
596 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
597 if (!rt || rt->rt_ifp != inifp) {
598#if 0
599 log(LOG_WARNING, "%s: packet from 0x%x dropped "
600 "due to ingress filter\n", if_name(&sc->sc_if),
601 (u_int32_t)ntohl(sin.sin_addr.s_addr));
602#endif
603 if (rt)
604 rtfree(rt);
605 return -1;
606 }
607 rtfree(rt);
608 }
609
610 return 0;
611}
612
613static int
614stf_checkaddr6(sc, in6, inifp)
615 struct stf_softc *sc;
616 struct in6_addr *in6;
617 struct ifnet *inifp; /* incoming interface */
618{
619 /*
620 * check 6to4 addresses
621 */
622 if (IN6_IS_ADDR_6TO4(in6)) {
623 struct in_addr in4;
624 bcopy(GET_V4(in6), &in4, sizeof(in4));
625 return stf_checkaddr4(sc, &in4, inifp);
626 }
627
628 /*
629 * reject anything that look suspicious. the test is implemented
630 * in ip6_input too, but we check here as well to
631 * (1) reject bad packets earlier, and
632 * (2) to be safe against future ip6_input change.
633 */
634 if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
635 return -1;
636
637 return 0;
638}
639
640void
641in_stf_input(m, off)
642 struct mbuf *m;
643 int off;
644{
645 int proto;
646 struct stf_softc *sc;
647 struct ip *ip;
648 struct ip6_hdr *ip6;
649 u_int8_t otos, itos;
650 struct ifnet *ifp;
651
652 proto = mtod(m, struct ip *)->ip_p;
653
654 if (proto != IPPROTO_IPV6) {
655 m_freem(m);
656 return;
657 }
658
659 ip = mtod(m, struct ip *);
660
661 sc = (struct stf_softc *)encap_getarg(m);
662
663 if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
664 m_freem(m);
665 return;
666 }
667
668 ifp = &sc->sc_if;
669
670#ifdef MAC
671 mac_create_mbuf_from_ifnet(ifp, m);
672#endif
673
674 /*
675 * perform sanity check against outer src/dst.
676 * for source, perform ingress filter as well.
677 */
678 if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
679 stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
680 m_freem(m);
681 return;
682 }
683
684 otos = ip->ip_tos;
685 m_adj(m, off);
686
687 if (m->m_len < sizeof(*ip6)) {
688 m = m_pullup(m, sizeof(*ip6));
689 if (!m)
690 return;
691 }
692 ip6 = mtod(m, struct ip6_hdr *);
693
694 /*
695 * perform sanity check against inner src/dst.
696 * for source, perform ingress filter as well.
697 */
698 if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
699 stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
700 m_freem(m);
701 return;
702 }
703
704 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
705 if ((ifp->if_flags & IFF_LINK1) != 0)
706 ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
707 else
708 ip_ecn_egress(ECN_NOCARE, &otos, &itos);
709 ip6->ip6_flow &= ~htonl(0xff << 20);
710 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
711
712 m->m_pkthdr.rcvif = ifp;
713
714 if (ifp->if_bpf) {
715 /*
716 * We need to prepend the address family as
717 * a four byte field. Cons up a dummy header
718 * to pacify bpf. This is safe because bpf
719 * will only read from the mbuf (i.e., it won't
720 * try to free it or keep a pointer a to it).
721 */
722 u_int32_t af = AF_INET6;
723#ifdef HAVE_OLD_BPF
724 struct mbuf m0;
725
726 m0.m_next = m;
727 m0.m_len = 4;
728 m0.m_data = (char *)⁡
729
730 BPF_MTAP(ifp, &m0);
731#else
732 bpf_mtap2(ifp->if_bpf, &af, sizeof(ah), m);
733#endif
734 }
735
736 /*
737 * Put the packet to the network layer input queue according to the
738 * specified address family.
739 * See net/if_gif.c for possible issues with packet processing
740 * reorder due to extra queueing.
741 */
742 ifp->if_ipackets++;
743 ifp->if_ibytes += m->m_pkthdr.len;
744 netisr_dispatch(NETISR_IPV6, m);
745}
746
747/* ARGSUSED */
748static void
749stf_rtrequest(cmd, rt, info)
750 int cmd;
751 struct rtentry *rt;
752 struct rt_addrinfo *info;
753{
754 RT_LOCK_ASSERT(rt);
755
756 if (rt)
757 rt->rt_rmx.rmx_mtu = IPV6_MMTU;
758}
759
760static int
761stf_ioctl(ifp, cmd, data)
762 struct ifnet *ifp;
763 u_long cmd;
764 caddr_t data;
765{
766 struct ifaddr *ifa;
767 struct ifreq *ifr;
768 struct sockaddr_in6 *sin6;
769 struct in_addr addr;
770 int error;
771
772 error = 0;
773 switch (cmd) {
774 case SIOCSIFADDR:
775 ifa = (struct ifaddr *)data;
776 if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
777 error = EAFNOSUPPORT;
778 break;
779 }
780 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
781 if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
782 error = EINVAL;
783 break;
784 }
785 bcopy(GET_V4(&sin6->sin6_addr), &addr, sizeof(addr));
786 if (isrfc1918addr(&addr)) {
787 error = EINVAL;
788 break;
789 }
790
791 ifa->ifa_rtrequest = stf_rtrequest;
792 ifp->if_flags |= IFF_UP;
793 break;
794
795 case SIOCADDMULTI:
796 case SIOCDELMULTI:
797 ifr = (struct ifreq *)data;
798 if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
799 ;
800 else
801 error = EAFNOSUPPORT;
802 break;
803
804 default:
805 error = EINVAL;
806 break;
807 }
808
809 return error;
810}
2/* $KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi Exp $ */
3
4/*
5 * Copyright (C) 2000 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
33/*
34 * 6to4 interface, based on RFC3056.
35 *
36 * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
37 * There is no address mapping defined from IPv6 multicast address to IPv4
38 * address. Therefore, we do not have IFF_MULTICAST on the interface.
39 *
40 * Due to the lack of address mapping for link-local addresses, we cannot
41 * throw packets toward link-local addresses (fe80::x). Also, we cannot throw
42 * packets to link-local multicast addresses (ff02::x).
43 *
44 * Here are interesting symptoms due to the lack of link-local address:
45 *
46 * Unicast routing exchange:
47 * - RIPng: Impossible. Uses link-local multicast packet toward ff02::9,
48 * and link-local addresses as nexthop.
49 * - OSPFv6: Impossible. OSPFv6 assumes that there's link-local address
50 * assigned to the link, and makes use of them. Also, HELLO packets use
51 * link-local multicast addresses (ff02::5 and ff02::6).
52 * - BGP4+: Maybe. You can only use global address as nexthop, and global
53 * address as TCP endpoint address.
54 *
55 * Multicast routing protocols:
56 * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
57 * Adjacent PIM routers must be configured manually (is it really spec-wise
58 * correct thing to do?).
59 *
60 * ICMPv6:
61 * - Redirects cannot be used due to the lack of link-local address.
62 *
63 * stf interface does not have, and will not need, a link-local address.
64 * It seems to have no real benefit and does not help the above symptoms much.
65 * Even if we assign link-locals to interface, we cannot really
66 * use link-local unicast/multicast on top of 6to4 cloud (since there's no
67 * encapsulation defined for link-local address), and the above analysis does
68 * not change. RFC3056 does not mandate the assignment of link-local address
69 * either.
70 *
71 * 6to4 interface has security issues. Refer to
72 * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
73 * for details. The code tries to filter out some of malicious packets.
74 * Note that there is no way to be 100% secure.
75 */
76
77#include "opt_inet.h"
78#include "opt_inet6.h"
79#include "opt_mac.h"
80
81#include <sys/param.h>
82#include <sys/systm.h>
83#include <sys/socket.h>
84#include <sys/sockio.h>
85#include <sys/mac.h>
86#include <sys/mbuf.h>
87#include <sys/errno.h>
88#include <sys/kernel.h>
89#include <sys/module.h>
90#include <sys/protosw.h>
91#include <sys/queue.h>
92#include <machine/cpu.h>
93
94#include <sys/malloc.h>
95
96#include <net/if.h>
97#include <net/route.h>
98#include <net/netisr.h>
99#include <net/if_types.h>
100#include <net/if_stf.h>
101
102#include <netinet/in.h>
103#include <netinet/in_systm.h>
104#include <netinet/ip.h>
105#include <netinet/ip_var.h>
106#include <netinet/in_var.h>
107
108#include <netinet/ip6.h>
109#include <netinet6/ip6_var.h>
110#include <netinet6/in6_var.h>
111#include <netinet/ip_ecn.h>
112
113#include <netinet/ip_encap.h>
114
115#include <machine/stdarg.h>
116
117#include <net/net_osdep.h>
118
119#include <net/bpf.h>
120
121#define STFNAME "stf"
122
123#define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002)
124
125/*
126 * XXX: Return a pointer with 16-bit aligned. Don't cast it to
127 * struct in_addr *; use bcopy() instead.
128 */
129#define GET_V4(x) ((caddr_t)(&(x)->s6_addr16[1]))
130
131struct stf_softc {
132 struct ifnet sc_if; /* common area */
133 union {
134 struct route __sc_ro4;
135 struct route_in6 __sc_ro6; /* just for safety */
136 } __sc_ro46;
137#define sc_ro __sc_ro46.__sc_ro4
138 const struct encaptab *encap_cookie;
139 LIST_ENTRY(stf_softc) sc_list; /* all stf's are linked */
140};
141
142/*
143 * All mutable global variables in if_stf.c are protected by stf_mtx.
144 * XXXRW: Note that mutable fields in the softc are not currently locked:
145 * in particular, sc_ro needs to be protected from concurrent entrance
146 * of stf_output().
147 */
148static struct mtx stf_mtx;
149static LIST_HEAD(, stf_softc) stf_softc_list;
150
151static MALLOC_DEFINE(M_STF, STFNAME, "6to4 Tunnel Interface");
152static const int ip_stf_ttl = 40;
153
154extern struct domain inetdomain;
155struct protosw in_stf_protosw =
156{ SOCK_RAW, &inetdomain, IPPROTO_IPV6, PR_ATOMIC|PR_ADDR,
157 in_stf_input, (pr_output_t*)rip_output, 0, rip_ctloutput,
158 0,
159 0, 0, 0, 0,
160 &rip_usrreqs
161};
162
163static int stfmodevent(module_t, int, void *);
164static int stf_encapcheck(const struct mbuf *, int, int, void *);
165static struct in6_ifaddr *stf_getsrcifa6(struct ifnet *);
166static int stf_output(struct ifnet *, struct mbuf *, struct sockaddr *,
167 struct rtentry *);
168static int isrfc1918addr(struct in_addr *);
169static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
170 struct ifnet *);
171static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
172 struct ifnet *);
173static void stf_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
174static int stf_ioctl(struct ifnet *, u_long, caddr_t);
175
176static int stf_clone_create(struct if_clone *, int);
177static void stf_clone_destroy(struct ifnet *);
178
179/* only one clone is currently allowed */
180struct if_clone stf_cloner =
181 IF_CLONE_INITIALIZER(STFNAME, stf_clone_create, stf_clone_destroy, 0, 0);
182
183static int
184stf_clone_create(ifc, unit)
185 struct if_clone *ifc;
186 int unit;
187{
188 struct stf_softc *sc;
189 struct ifnet *ifp;
190
191 sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO);
192 ifp = &sc->sc_if;
193 if_initname(ifp, ifc->ifc_name, unit);
194
195 sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
196 stf_encapcheck, &in_stf_protosw, sc);
197 if (sc->encap_cookie == NULL) {
198 if_printf(ifp, "attach failed\n");
199 free(sc, M_STF);
200 return (ENOMEM);
201 }
202
203 ifp->if_mtu = IPV6_MMTU;
204 ifp->if_ioctl = stf_ioctl;
205 ifp->if_output = stf_output;
206 ifp->if_type = IFT_STF;
207 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
208 if_attach(ifp);
209 bpfattach(ifp, DLT_NULL, sizeof(u_int));
210 mtx_lock(&stf_mtx);
211 LIST_INSERT_HEAD(&stf_softc_list, sc, sc_list);
212 mtx_unlock(&stf_mtx);
213 return (0);
214}
215
216static void
217stf_destroy(struct stf_softc *sc)
218{
219 int err;
220
221 err = encap_detach(sc->encap_cookie);
222 KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
223 bpfdetach(&sc->sc_if);
224 if_detach(&sc->sc_if);
225
226 free(sc, M_STF);
227}
228
229static void
230stf_clone_destroy(ifp)
231 struct ifnet *ifp;
232{
233 struct stf_softc *sc = (void *) ifp;
234
235 mtx_lock(&stf_mtx);
236 LIST_REMOVE(sc, sc_list);
237 mtx_unlock(&stf_mtx);
238
239 stf_destroy(sc);
240}
241
242static int
243stfmodevent(mod, type, data)
244 module_t mod;
245 int type;
246 void *data;
247{
248 struct stf_softc *sc;
249
250 switch (type) {
251 case MOD_LOAD:
252 mtx_init(&stf_mtx, "stf_mtx", NULL, MTX_DEF);
253 LIST_INIT(&stf_softc_list);
254 if_clone_attach(&stf_cloner);
255
256 break;
257 case MOD_UNLOAD:
258 if_clone_detach(&stf_cloner);
259
260 mtx_lock(&stf_mtx);
261 while ((sc = LIST_FIRST(&stf_softc_list)) != NULL) {
262 LIST_REMOVE(sc, sc_list);
263 mtx_unlock(&stf_mtx);
264 stf_destroy(sc);
265 mtx_lock(&stf_mtx);
266 }
267 mtx_unlock(&stf_mtx);
268 mtx_destroy(&stf_mtx);
269 break;
270 }
271
272 return (0);
273}
274
275static moduledata_t stf_mod = {
276 "if_stf",
277 stfmodevent,
278 0
279};
280
281DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
282
283static int
284stf_encapcheck(m, off, proto, arg)
285 const struct mbuf *m;
286 int off;
287 int proto;
288 void *arg;
289{
290 struct ip ip;
291 struct in6_ifaddr *ia6;
292 struct stf_softc *sc;
293 struct in_addr a, b, mask;
294
295 sc = (struct stf_softc *)arg;
296 if (sc == NULL)
297 return 0;
298
299 if ((sc->sc_if.if_flags & IFF_UP) == 0)
300 return 0;
301
302 /* IFF_LINK0 means "no decapsulation" */
303 if ((sc->sc_if.if_flags & IFF_LINK0) != 0)
304 return 0;
305
306 if (proto != IPPROTO_IPV6)
307 return 0;
308
309 /* LINTED const cast */
310 m_copydata((struct mbuf *)(uintptr_t)m, 0, sizeof(ip), (caddr_t)&ip);
311
312 if (ip.ip_v != 4)
313 return 0;
314
315 ia6 = stf_getsrcifa6(&sc->sc_if);
316 if (ia6 == NULL)
317 return 0;
318
319 /*
320 * check if IPv4 dst matches the IPv4 address derived from the
321 * local 6to4 address.
322 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
323 */
324 if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
325 sizeof(ip.ip_dst)) != 0)
326 return 0;
327
328 /*
329 * check if IPv4 src matches the IPv4 address derived from the
330 * local 6to4 address masked by prefixmask.
331 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
332 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
333 */
334 bzero(&a, sizeof(a));
335 bcopy(GET_V4(&ia6->ia_addr.sin6_addr), &a, sizeof(a));
336 bcopy(GET_V4(&ia6->ia_prefixmask.sin6_addr), &mask, sizeof(mask));
337 a.s_addr &= mask.s_addr;
338 b = ip.ip_src;
339 b.s_addr &= mask.s_addr;
340 if (a.s_addr != b.s_addr)
341 return 0;
342
343 /* stf interface makes single side match only */
344 return 32;
345}
346
347static struct in6_ifaddr *
348stf_getsrcifa6(ifp)
349 struct ifnet *ifp;
350{
351 struct ifaddr *ia;
352 struct in_ifaddr *ia4;
353 struct sockaddr_in6 *sin6;
354 struct in_addr in;
355
356 for (ia = TAILQ_FIRST(&ifp->if_addrlist);
357 ia;
358 ia = TAILQ_NEXT(ia, ifa_list))
359 {
360 if (ia->ifa_addr == NULL)
361 continue;
362 if (ia->ifa_addr->sa_family != AF_INET6)
363 continue;
364 sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
365 if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
366 continue;
367
368 bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
369 LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
370 if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
371 break;
372 if (ia4 == NULL)
373 continue;
374
375 return (struct in6_ifaddr *)ia;
376 }
377
378 return NULL;
379}
380
381static int
382stf_output(ifp, m, dst, rt)
383 struct ifnet *ifp;
384 struct mbuf *m;
385 struct sockaddr *dst;
386 struct rtentry *rt;
387{
388 struct stf_softc *sc;
389 struct sockaddr_in6 *dst6;
390 struct in_addr in4;
391 caddr_t ptr;
392 struct sockaddr_in *dst4;
393 u_int8_t tos;
394 struct ip *ip;
395 struct ip6_hdr *ip6;
396 struct in6_ifaddr *ia6;
397#ifdef MAC
398 int error;
399
400 error = mac_check_ifnet_transmit(ifp, m);
401 if (error) {
402 m_freem(m);
403 return (error);
404 }
405#endif
406
407 sc = (struct stf_softc*)ifp;
408 dst6 = (struct sockaddr_in6 *)dst;
409
410 /* just in case */
411 if ((ifp->if_flags & IFF_UP) == 0) {
412 m_freem(m);
413 ifp->if_oerrors++;
414 return ENETDOWN;
415 }
416
417 /*
418 * If we don't have an ip4 address that match my inner ip6 address,
419 * we shouldn't generate output. Without this check, we'll end up
420 * using wrong IPv4 source.
421 */
422 ia6 = stf_getsrcifa6(ifp);
423 if (ia6 == NULL) {
424 m_freem(m);
425 ifp->if_oerrors++;
426 return ENETDOWN;
427 }
428
429 if (m->m_len < sizeof(*ip6)) {
430 m = m_pullup(m, sizeof(*ip6));
431 if (!m) {
432 ifp->if_oerrors++;
433 return ENOBUFS;
434 }
435 }
436 ip6 = mtod(m, struct ip6_hdr *);
437 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
438
439 /*
440 * Pickup the right outer dst addr from the list of candidates.
441 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
442 */
443 ptr = NULL;
444 if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
445 ptr = GET_V4(&ip6->ip6_dst);
446 else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
447 ptr = GET_V4(&dst6->sin6_addr);
448 else {
449 m_freem(m);
450 ifp->if_oerrors++;
451 return ENETUNREACH;
452 }
453 bcopy(ptr, &in4, sizeof(in4));
454
455#if NBPFILTER > 0
456 if (ifp->if_bpf) {
457 /*
458 * We need to prepend the address family as
459 * a four byte field. Cons up a dummy header
460 * to pacify bpf. This is safe because bpf
461 * will only read from the mbuf (i.e., it won't
462 * try to free it or keep a pointer a to it).
463 */
464 u_int32_t af = AF_INET6;
465#ifdef HAVE_OLD_BPF
466 struct mbuf m0;
467
468 m0.m_next = m;
469 m0.m_len = 4;
470 m0.m_data = (char *)⁡
471
472 BPF_MTAP(ifp, &m0);
473#else
474 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
475#endif
476 }
477#endif /*NBPFILTER > 0*/
478
479 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
480 if (m && m->m_len < sizeof(struct ip))
481 m = m_pullup(m, sizeof(struct ip));
482 if (m == NULL) {
483 ifp->if_oerrors++;
484 return ENOBUFS;
485 }
486 ip = mtod(m, struct ip *);
487
488 bzero(ip, sizeof(*ip));
489
490 bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
491 &ip->ip_src, sizeof(ip->ip_src));
492 bcopy(&in4, &ip->ip_dst, sizeof(ip->ip_dst));
493 ip->ip_p = IPPROTO_IPV6;
494 ip->ip_ttl = ip_stf_ttl;
495 ip->ip_len = m->m_pkthdr.len; /*host order*/
496 if (ifp->if_flags & IFF_LINK1)
497 ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
498 else
499 ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
500
501 /*
502 * XXXRW: Locking of sc_ro required.
503 */
504 dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
505 if (dst4->sin_family != AF_INET ||
506 bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
507 /* cache route doesn't match */
508 dst4->sin_family = AF_INET;
509 dst4->sin_len = sizeof(struct sockaddr_in);
510 bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
511 if (sc->sc_ro.ro_rt) {
512 RTFREE(sc->sc_ro.ro_rt);
513 sc->sc_ro.ro_rt = NULL;
514 }
515 }
516
517 if (sc->sc_ro.ro_rt == NULL) {
518 rtalloc(&sc->sc_ro);
519 if (sc->sc_ro.ro_rt == NULL) {
520 m_freem(m);
521 ifp->if_oerrors++;
522 return ENETUNREACH;
523 }
524 }
525
526 ifp->if_opackets++;
527 return ip_output(m, NULL, &sc->sc_ro, 0, NULL, NULL);
528}
529
530static int
531isrfc1918addr(in)
532 struct in_addr *in;
533{
534 /*
535 * returns 1 if private address range:
536 * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
537 */
538 if ((ntohl(in->s_addr) & 0xff000000) >> 24 == 10 ||
539 (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 ||
540 (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168)
541 return 1;
542
543 return 0;
544}
545
546static int
547stf_checkaddr4(sc, in, inifp)
548 struct stf_softc *sc;
549 struct in_addr *in;
550 struct ifnet *inifp; /* incoming interface */
551{
552 struct in_ifaddr *ia4;
553
554 /*
555 * reject packets with the following address:
556 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
557 */
558 if (IN_MULTICAST(ntohl(in->s_addr)))
559 return -1;
560 switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
561 case 0: case 127: case 255:
562 return -1;
563 }
564
565 /*
566 * reject packets with private address range.
567 * (requirement from RFC3056 section 2 1st paragraph)
568 */
569 if (isrfc1918addr(in))
570 return -1;
571
572 /*
573 * reject packets with broadcast
574 */
575 for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
576 ia4;
577 ia4 = TAILQ_NEXT(ia4, ia_link))
578 {
579 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
580 continue;
581 if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
582 return -1;
583 }
584
585 /*
586 * perform ingress filter
587 */
588 if (sc && (sc->sc_if.if_flags & IFF_LINK2) == 0 && inifp) {
589 struct sockaddr_in sin;
590 struct rtentry *rt;
591
592 bzero(&sin, sizeof(sin));
593 sin.sin_family = AF_INET;
594 sin.sin_len = sizeof(struct sockaddr_in);
595 sin.sin_addr = *in;
596 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
597 if (!rt || rt->rt_ifp != inifp) {
598#if 0
599 log(LOG_WARNING, "%s: packet from 0x%x dropped "
600 "due to ingress filter\n", if_name(&sc->sc_if),
601 (u_int32_t)ntohl(sin.sin_addr.s_addr));
602#endif
603 if (rt)
604 rtfree(rt);
605 return -1;
606 }
607 rtfree(rt);
608 }
609
610 return 0;
611}
612
613static int
614stf_checkaddr6(sc, in6, inifp)
615 struct stf_softc *sc;
616 struct in6_addr *in6;
617 struct ifnet *inifp; /* incoming interface */
618{
619 /*
620 * check 6to4 addresses
621 */
622 if (IN6_IS_ADDR_6TO4(in6)) {
623 struct in_addr in4;
624 bcopy(GET_V4(in6), &in4, sizeof(in4));
625 return stf_checkaddr4(sc, &in4, inifp);
626 }
627
628 /*
629 * reject anything that look suspicious. the test is implemented
630 * in ip6_input too, but we check here as well to
631 * (1) reject bad packets earlier, and
632 * (2) to be safe against future ip6_input change.
633 */
634 if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
635 return -1;
636
637 return 0;
638}
639
640void
641in_stf_input(m, off)
642 struct mbuf *m;
643 int off;
644{
645 int proto;
646 struct stf_softc *sc;
647 struct ip *ip;
648 struct ip6_hdr *ip6;
649 u_int8_t otos, itos;
650 struct ifnet *ifp;
651
652 proto = mtod(m, struct ip *)->ip_p;
653
654 if (proto != IPPROTO_IPV6) {
655 m_freem(m);
656 return;
657 }
658
659 ip = mtod(m, struct ip *);
660
661 sc = (struct stf_softc *)encap_getarg(m);
662
663 if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
664 m_freem(m);
665 return;
666 }
667
668 ifp = &sc->sc_if;
669
670#ifdef MAC
671 mac_create_mbuf_from_ifnet(ifp, m);
672#endif
673
674 /*
675 * perform sanity check against outer src/dst.
676 * for source, perform ingress filter as well.
677 */
678 if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
679 stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
680 m_freem(m);
681 return;
682 }
683
684 otos = ip->ip_tos;
685 m_adj(m, off);
686
687 if (m->m_len < sizeof(*ip6)) {
688 m = m_pullup(m, sizeof(*ip6));
689 if (!m)
690 return;
691 }
692 ip6 = mtod(m, struct ip6_hdr *);
693
694 /*
695 * perform sanity check against inner src/dst.
696 * for source, perform ingress filter as well.
697 */
698 if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
699 stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
700 m_freem(m);
701 return;
702 }
703
704 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
705 if ((ifp->if_flags & IFF_LINK1) != 0)
706 ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
707 else
708 ip_ecn_egress(ECN_NOCARE, &otos, &itos);
709 ip6->ip6_flow &= ~htonl(0xff << 20);
710 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
711
712 m->m_pkthdr.rcvif = ifp;
713
714 if (ifp->if_bpf) {
715 /*
716 * We need to prepend the address family as
717 * a four byte field. Cons up a dummy header
718 * to pacify bpf. This is safe because bpf
719 * will only read from the mbuf (i.e., it won't
720 * try to free it or keep a pointer a to it).
721 */
722 u_int32_t af = AF_INET6;
723#ifdef HAVE_OLD_BPF
724 struct mbuf m0;
725
726 m0.m_next = m;
727 m0.m_len = 4;
728 m0.m_data = (char *)⁡
729
730 BPF_MTAP(ifp, &m0);
731#else
732 bpf_mtap2(ifp->if_bpf, &af, sizeof(ah), m);
733#endif
734 }
735
736 /*
737 * Put the packet to the network layer input queue according to the
738 * specified address family.
739 * See net/if_gif.c for possible issues with packet processing
740 * reorder due to extra queueing.
741 */
742 ifp->if_ipackets++;
743 ifp->if_ibytes += m->m_pkthdr.len;
744 netisr_dispatch(NETISR_IPV6, m);
745}
746
747/* ARGSUSED */
748static void
749stf_rtrequest(cmd, rt, info)
750 int cmd;
751 struct rtentry *rt;
752 struct rt_addrinfo *info;
753{
754 RT_LOCK_ASSERT(rt);
755
756 if (rt)
757 rt->rt_rmx.rmx_mtu = IPV6_MMTU;
758}
759
760static int
761stf_ioctl(ifp, cmd, data)
762 struct ifnet *ifp;
763 u_long cmd;
764 caddr_t data;
765{
766 struct ifaddr *ifa;
767 struct ifreq *ifr;
768 struct sockaddr_in6 *sin6;
769 struct in_addr addr;
770 int error;
771
772 error = 0;
773 switch (cmd) {
774 case SIOCSIFADDR:
775 ifa = (struct ifaddr *)data;
776 if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
777 error = EAFNOSUPPORT;
778 break;
779 }
780 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
781 if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
782 error = EINVAL;
783 break;
784 }
785 bcopy(GET_V4(&sin6->sin6_addr), &addr, sizeof(addr));
786 if (isrfc1918addr(&addr)) {
787 error = EINVAL;
788 break;
789 }
790
791 ifa->ifa_rtrequest = stf_rtrequest;
792 ifp->if_flags |= IFF_UP;
793 break;
794
795 case SIOCADDMULTI:
796 case SIOCDELMULTI:
797 ifr = (struct ifreq *)data;
798 if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
799 ;
800 else
801 error = EAFNOSUPPORT;
802 break;
803
804 default:
805 error = EINVAL;
806 break;
807 }
808
809 return error;
810}