if_stf.c revision 71959
1/*	$FreeBSD: head/sys/net/if_stf.c 71959 2001-02-03 11:46:35Z phk $	*/
2/*	$KAME: if_stf.c,v 1.42 2000/08/15 07:24:23 itojun 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 draft-ietf-ngtrans-6to4-06.txt.
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 * Starting from 04 draft, the specification suggests how to construct
64 * link-local address for 6to4 interface.
65 * However, it seems to have no real use and does not help the above symptom
66 * much.  Even if we assign link-locals to interface, we cannot really
67 * use link-local unicast/multicast on top of 6to4 cloud, and the above
68 * analysis does not change.
69 *
70 * 6to4 interface has security issues.  Refer to
71 * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
72 * for details.  The code tries to filter out some of malicious packets.
73 * Note that there is no way to be 100% secure.
74 */
75
76#include "opt_inet.h"
77#include "opt_inet6.h"
78
79#include <sys/param.h>
80#include <sys/systm.h>
81#include <sys/socket.h>
82#include <sys/sockio.h>
83#include <sys/mbuf.h>
84#include <sys/errno.h>
85#include <sys/protosw.h>
86#include <sys/kernel.h>
87#include <machine/cpu.h>
88
89#include <sys/malloc.h>
90
91#include <net/if.h>
92#include <net/route.h>
93#include <net/netisr.h>
94#include <net/if_types.h>
95#include <net/if_stf.h>
96
97#include <netinet/in.h>
98#include <netinet/in_systm.h>
99#include <netinet/ip.h>
100#include <netinet/ip_var.h>
101#include <netinet/in_var.h>
102
103#include <netinet/ip6.h>
104#include <netinet6/ip6_var.h>
105#include <netinet6/in6_gif.h>
106#include <netinet6/in6_var.h>
107#include <netinet/ip_ecn.h>
108
109#include <netinet/ip_encap.h>
110
111#include <machine/stdarg.h>
112
113#include <net/net_osdep.h>
114
115#include "bpf.h"
116#define NBPFILTER	NBPF
117#include "stf.h"
118#include "gif.h"	/*XXX*/
119
120#if NBPFILTER > 0
121#include <net/bpf.h>
122#endif
123
124#if NGIF > 0
125#include <net/if_gif.h>
126#endif
127
128#if NSTF > 0
129#if NSTF != 1
130# error only single stf interface allowed
131#endif
132
133#define IN6_IS_ADDR_6TO4(x)	(ntohs((x)->s6_addr16[0]) == 0x2002)
134#define GET_V4(x)	((struct in_addr *)(&(x)->s6_addr16[1]))
135
136struct stf_softc {
137	struct ifnet	sc_if;	   /* common area */
138	union {
139		struct route  __sc_ro4;
140		struct route_in6 __sc_ro6; /* just for safety */
141	} __sc_ro46;
142#define sc_ro	__sc_ro46.__sc_ro4
143	const struct encaptab *encap_cookie;
144};
145
146static struct stf_softc *stf;
147static int nstf;
148
149#if NGIF > 0
150extern int ip_gif_ttl;	/*XXX*/
151#else
152static int ip_gif_ttl = 40;	/*XXX*/
153#endif
154
155extern struct protosw in_stf_protosw;
156
157void stfattach __P((void *));
158static int stf_encapcheck __P((const struct mbuf *, int, int, void *));
159static struct in6_ifaddr *stf_getsrcifa6 __P((struct ifnet *));
160static int stf_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
161	struct rtentry *));
162static int stf_checkaddr4 __P((struct in_addr *, struct ifnet *));
163static int stf_checkaddr6 __P((struct in6_addr *, struct ifnet *));
164static void stf_rtrequest __P((int, struct rtentry *, struct sockaddr *));
165static int stf_ioctl __P((struct ifnet *, u_long, caddr_t));
166
167void
168stfattach(dummy)
169	void *dummy;
170{
171	struct stf_softc *sc;
172	int i;
173	const struct encaptab *p;
174
175	nstf = NSTF;
176	stf = malloc(nstf * sizeof(struct stf_softc), M_DEVBUF, M_WAITOK);
177	bzero(stf, nstf * sizeof(struct stf_softc));
178	sc = stf;
179
180	/* XXX just in case... */
181	for (i = 0; i < nstf; i++) {
182		sc = &stf[i];
183		bzero(sc, sizeof(*sc));
184		sc->sc_if.if_name = "stf";
185		sc->sc_if.if_unit = i;
186
187		p = encap_attach_func(AF_INET, IPPROTO_IPV6, stf_encapcheck,
188		    &in_stf_protosw, sc);
189		if (p == NULL) {
190			printf("%s: attach failed\n", if_name(&sc->sc_if));
191			continue;
192		}
193		sc->encap_cookie = p;
194
195		sc->sc_if.if_mtu    = IPV6_MMTU;
196		sc->sc_if.if_flags  = 0;
197		sc->sc_if.if_ioctl  = stf_ioctl;
198		sc->sc_if.if_output = stf_output;
199		sc->sc_if.if_type   = IFT_STF;
200		sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
201		if_attach(&sc->sc_if);
202#if NBPFILTER > 0
203#ifdef HAVE_OLD_BPF
204		bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
205#else
206		bpfattach(&sc->sc_if.if_bpf, &sc->sc_if, DLT_NULL, sizeof(u_int));
207#endif
208#endif
209	}
210}
211
212PSEUDO_SET(stfattach, if_stf);
213
214static int
215stf_encapcheck(m, off, proto, arg)
216	const struct mbuf *m;
217	int off;
218	int proto;
219	void *arg;
220{
221	struct ip ip;
222	struct in6_ifaddr *ia6;
223	struct stf_softc *sc;
224	struct in_addr a, b;
225
226	sc = (struct stf_softc *)arg;
227	if (sc == NULL)
228		return 0;
229
230	if ((sc->sc_if.if_flags & IFF_UP) == 0)
231		return 0;
232
233	if (proto != IPPROTO_IPV6)
234		return 0;
235
236	/* LINTED const cast */
237	m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
238
239	if (ip.ip_v != 4)
240		return 0;
241
242	ia6 = stf_getsrcifa6(&sc->sc_if);
243	if (ia6 == NULL)
244		return 0;
245
246	/*
247	 * check if IPv4 dst matches the IPv4 address derived from the
248	 * local 6to4 address.
249	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
250	 */
251	if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
252	    sizeof(ip.ip_dst)) != 0)
253		return 0;
254
255	/*
256	 * check if IPv4 src matches the IPv4 address derived from the
257	 * local 6to4 address masked by prefixmask.
258	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
259	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
260	 */
261	bzero(&a, sizeof(a));
262	a.s_addr = GET_V4(&ia6->ia_addr.sin6_addr)->s_addr;
263	a.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
264	b = ip.ip_src;
265	b.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
266	if (a.s_addr != b.s_addr)
267		return 0;
268
269	/* stf interface makes single side match only */
270	return 32;
271}
272
273static struct in6_ifaddr *
274stf_getsrcifa6(ifp)
275	struct ifnet *ifp;
276{
277	struct ifaddr *ia;
278	struct in_ifaddr *ia4;
279	struct sockaddr_in6 *sin6;
280	struct in_addr in;
281
282	for (ia = TAILQ_FIRST(&ifp->if_addrlist);
283	     ia;
284	     ia = TAILQ_NEXT(ia, ifa_list))
285	{
286		if (ia->ifa_addr == NULL)
287			continue;
288		if (ia->ifa_addr->sa_family != AF_INET6)
289			continue;
290		sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
291		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
292			continue;
293
294		bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
295		for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
296		     ia4;
297		     ia4 = TAILQ_NEXT(ia4, ia_link))
298		{
299			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
300				break;
301		}
302		if (ia4 == NULL)
303			continue;
304
305		return (struct in6_ifaddr *)ia;
306	}
307
308	return NULL;
309}
310
311static int
312stf_output(ifp, m, dst, rt)
313	struct ifnet *ifp;
314	struct mbuf *m;
315	struct sockaddr *dst;
316	struct rtentry *rt;
317{
318	struct stf_softc *sc;
319	struct sockaddr_in6 *dst6;
320	struct sockaddr_in *dst4;
321	u_int8_t tos;
322	struct ip *ip;
323	struct ip6_hdr *ip6;
324	struct in6_ifaddr *ia6;
325
326	sc = (struct stf_softc*)ifp;
327	dst6 = (struct sockaddr_in6 *)dst;
328
329	/* just in case */
330	if ((ifp->if_flags & IFF_UP) == 0) {
331		m_freem(m);
332		return ENETDOWN;
333	}
334
335	/*
336	 * If we don't have an ip4 address that match my inner ip6 address,
337	 * we shouldn't generate output.  Without this check, we'll end up
338	 * using wrong IPv4 source.
339	 */
340	ia6 = stf_getsrcifa6(ifp);
341	if (ia6 == NULL) {
342		m_freem(m);
343		return ENETDOWN;
344	}
345
346	if (m->m_len < sizeof(*ip6)) {
347		m = m_pullup(m, sizeof(*ip6));
348		if (!m)
349			return ENOBUFS;
350	}
351	ip6 = mtod(m, struct ip6_hdr *);
352	tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
353
354	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
355	if (m && m->m_len < sizeof(struct ip))
356		m = m_pullup(m, sizeof(struct ip));
357	if (m == NULL)
358		return ENOBUFS;
359	ip = mtod(m, struct ip *);
360
361	bzero(ip, sizeof(*ip));
362
363	bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
364	    &ip->ip_src, sizeof(ip->ip_src));
365	bcopy(GET_V4(&dst6->sin6_addr), &ip->ip_dst, sizeof(ip->ip_dst));
366	ip->ip_p = IPPROTO_IPV6;
367	ip->ip_ttl = ip_gif_ttl;	/*XXX*/
368	ip->ip_len = m->m_pkthdr.len;	/*host order*/
369	if (ifp->if_flags & IFF_LINK1)
370		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
371
372	dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
373	if (dst4->sin_family != AF_INET ||
374	    bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
375		/* cache route doesn't match */
376		dst4->sin_family = AF_INET;
377		dst4->sin_len = sizeof(struct sockaddr_in);
378		bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
379		if (sc->sc_ro.ro_rt) {
380			RTFREE(sc->sc_ro.ro_rt);
381			sc->sc_ro.ro_rt = NULL;
382		}
383	}
384
385	if (sc->sc_ro.ro_rt == NULL) {
386		rtalloc(&sc->sc_ro);
387		if (sc->sc_ro.ro_rt == NULL) {
388			m_freem(m);
389			return ENETUNREACH;
390		}
391	}
392
393	return ip_output(m, NULL, &sc->sc_ro, 0, NULL);
394}
395
396static int
397stf_checkaddr4(in, ifp)
398	struct in_addr *in;
399	struct ifnet *ifp;	/* incoming interface */
400{
401	struct in_ifaddr *ia4;
402
403	/*
404	 * reject packets with the following address:
405	 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
406	 */
407	if (IN_MULTICAST(ntohl(in->s_addr)))
408		return -1;
409	switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
410	case 0: case 127: case 255:
411		return -1;
412	}
413
414	/*
415	 * reject packets with broadcast
416	 */
417	for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
418	     ia4;
419	     ia4 = TAILQ_NEXT(ia4, ia_link))
420	{
421		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
422			continue;
423		if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
424			return -1;
425	}
426
427	/*
428	 * perform ingress filter
429	 */
430	if (ifp) {
431		struct sockaddr_in sin;
432		struct rtentry *rt;
433
434		bzero(&sin, sizeof(sin));
435		sin.sin_family = AF_INET;
436		sin.sin_len = sizeof(struct sockaddr_in);
437		sin.sin_addr = *in;
438		rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
439		if (!rt)
440			return -1;
441		if (rt->rt_ifp != ifp) {
442			rtfree(rt);
443			return -1;
444		}
445		rtfree(rt);
446	}
447
448	return 0;
449}
450
451static int
452stf_checkaddr6(in6, ifp)
453	struct in6_addr *in6;
454	struct ifnet *ifp;	/* incoming interface */
455{
456	/*
457	 * check 6to4 addresses
458	 */
459	if (IN6_IS_ADDR_6TO4(in6))
460		return stf_checkaddr4(GET_V4(in6), ifp);
461
462	/*
463	 * reject anything that look suspicious.  the test is implemented
464	 * in ip6_input too, but we check here as well to
465	 * (1) reject bad packets earlier, and
466	 * (2) to be safe against future ip6_input change.
467	 */
468	if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
469		return -1;
470
471	return 0;
472}
473
474void
475#if __STDC__
476in_stf_input(struct mbuf *m, ...)
477#else
478in_stf_input(m, va_alist)
479	register struct mbuf *m;
480#endif
481{
482	int off, proto;
483	struct stf_softc *sc;
484	struct ip *ip;
485	struct ip6_hdr *ip6;
486	u_int8_t otos, itos;
487	int len, isr;
488	struct ifqueue *ifq = NULL;
489	struct ifnet *ifp;
490	va_list ap;
491
492	va_start(ap, m);
493	off = va_arg(ap, int);
494	proto = va_arg(ap, int);
495	va_end(ap);
496
497	if (proto != IPPROTO_IPV6) {
498		m_freem(m);
499		return;
500	}
501
502	ip = mtod(m, struct ip *);
503
504	sc = (struct stf_softc *)encap_getarg(m);
505
506	if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
507		m_freem(m);
508		return;
509	}
510
511	ifp = &sc->sc_if;
512
513	/*
514	 * perform sanity check against outer src/dst.
515	 * for source, perform ingress filter as well.
516	 */
517	if (stf_checkaddr4(&ip->ip_dst, NULL) < 0 ||
518	    stf_checkaddr4(&ip->ip_src, m->m_pkthdr.rcvif) < 0) {
519		m_freem(m);
520		return;
521	}
522
523	otos = ip->ip_tos;
524	m_adj(m, off);
525
526	if (m->m_len < sizeof(*ip6)) {
527		m = m_pullup(m, sizeof(*ip6));
528		if (!m)
529			return;
530	}
531	ip6 = mtod(m, struct ip6_hdr *);
532
533	/*
534	 * perform sanity check against inner src/dst.
535	 * for source, perform ingress filter as well.
536	 */
537	if (stf_checkaddr6(&ip6->ip6_dst, NULL) < 0 ||
538	    stf_checkaddr6(&ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
539		m_freem(m);
540		return;
541	}
542
543	itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
544	if ((ifp->if_flags & IFF_LINK1) != 0)
545		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
546	ip6->ip6_flow &= ~htonl(0xff << 20);
547	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
548
549	m->m_pkthdr.rcvif = ifp;
550
551#if NBPFILTER > 0
552	if (ifp->if_bpf) {
553		/*
554		 * We need to prepend the address family as
555		 * a four byte field.  Cons up a dummy header
556		 * to pacify bpf.  This is safe because bpf
557		 * will only read from the mbuf (i.e., it won't
558		 * try to free it or keep a pointer a to it).
559		 */
560		struct mbuf m0;
561		u_int af = AF_INET6;
562
563		m0.m_next = m;
564		m0.m_len = 4;
565		m0.m_data = (char *)&af;
566
567#ifdef HAVE_OLD_BPF
568		bpf_mtap(ifp, &m0);
569#else
570		bpf_mtap(ifp->if_bpf, &m0);
571#endif
572	}
573#endif /*NBPFILTER > 0*/
574
575	/*
576	 * Put the packet to the network layer input queue according to the
577	 * specified address family.
578	 * See net/if_gif.c for possible issues with packet processing
579	 * reorder due to extra queueing.
580	 */
581	ifq = &ip6intrq;
582	isr = NETISR_IPV6;
583
584	len = m->m_pkthdr.len;
585	if (! IF_HANDOFF(ifq, m, NULL))
586		return;
587	schednetisr(isr);
588	ifp->if_ipackets++;
589	ifp->if_ibytes += len;
590}
591
592/* ARGSUSED */
593static void
594stf_rtrequest(cmd, rt, sa)
595	int cmd;
596	struct rtentry *rt;
597#if defined(__bsdi__) && _BSDI_VERSION >= 199802
598	struct rt_addrinfo *sa;
599#else
600	struct sockaddr *sa;
601#endif
602{
603
604	if (rt)
605		rt->rt_rmx.rmx_mtu = IPV6_MMTU;
606}
607
608static int
609stf_ioctl(ifp, cmd, data)
610	struct ifnet *ifp;
611	u_long cmd;
612	caddr_t data;
613{
614	struct ifaddr *ifa;
615	struct ifreq *ifr;
616	struct sockaddr_in6 *sin6;
617	int error;
618
619	error = 0;
620	switch (cmd) {
621	case SIOCSIFADDR:
622		ifa = (struct ifaddr *)data;
623		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
624			error = EAFNOSUPPORT;
625			break;
626		}
627		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
628		if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
629			ifa->ifa_rtrequest = stf_rtrequest;
630			ifp->if_flags |= IFF_UP;
631		} else
632			error = EINVAL;
633		break;
634
635	case SIOCADDMULTI:
636	case SIOCDELMULTI:
637		ifr = (struct ifreq *)data;
638		if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
639			;
640		else
641			error = EAFNOSUPPORT;
642		break;
643
644	default:
645		error = EINVAL;
646		break;
647	}
648
649	return error;
650}
651
652#endif /* NSTF > 0 */
653