88c1344bb60fb2bc04f92bdc319b843c8d72ccac
[games.git] / sys / net / stf / if_stf.c
1 /*      $FreeBSD: src/sys/net/if_stf.c,v 1.1.2.11 2003/01/23 21:06:44 sam Exp $ */
2 /*      $DragonFly: src/sys/net/stf/if_stf.c,v 1.19 2007/09/01 20:43:24 pavalos Exp $   */
3 /*      $KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi Exp $        */
4
5 /*
6  * Copyright (C) 2000 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /*
35  * 6to4 interface, based on RFC3056.
36  *
37  * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
38  * There is no address mapping defined from IPv6 multicast address to IPv4
39  * address.  Therefore, we do not have IFF_MULTICAST on the interface.
40  *
41  * Due to the lack of address mapping for link-local addresses, we cannot
42  * throw packets toward link-local addresses (fe80::x).  Also, we cannot throw
43  * packets to link-local multicast addresses (ff02::x).
44  *
45  * Here are interesting symptoms due to the lack of link-local address:
46  *
47  * Unicast routing exchange:
48  * - RIPng: Impossible.  Uses link-local multicast packet toward ff02::9,
49  *   and link-local addresses as nexthop.
50  * - OSPFv6: Impossible.  OSPFv6 assumes that there's link-local address
51  *   assigned to the link, and makes use of them.  Also, HELLO packets use
52  *   link-local multicast addresses (ff02::5 and ff02::6).
53  * - BGP4+: Maybe.  You can only use global address as nexthop, and global
54  *   address as TCP endpoint address.
55  *
56  * Multicast routing protocols:
57  * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
58  *   Adjacent PIM routers must be configured manually (is it really spec-wise
59  *   correct thing to do?).
60  *
61  * ICMPv6:
62  * - Redirects cannot be used due to the lack of link-local address.
63  *
64  * stf interface does not have, and will not need, a link-local address.  
65  * It seems to have no real benefit and does not help the above symptoms much.
66  * Even if we assign link-locals to interface, we cannot really
67  * use link-local unicast/multicast on top of 6to4 cloud (since there's no
68  * encapsulation defined for link-local address), and the above analysis does
69  * not change.  RFC3056 does not mandate the assignment of link-local address
70  * either.
71  *
72  * 6to4 interface has security issues.  Refer to
73  * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
74  * for details.  The code tries to filter out some of malicious packets.
75  * Note that there is no way to be 100% secure.
76  */
77
78 #include "opt_inet.h"
79 #include "opt_inet6.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/mbuf.h>
86 #include <sys/errno.h>
87 #include <sys/protosw.h>
88 #include <sys/kernel.h>
89 #include <machine/cpu.h>
90
91 #include <sys/malloc.h>
92
93 #include <net/if.h>
94 #include <net/route.h>
95 #include <net/netisr.h>
96 #include <net/if_types.h>
97 #include "if_stf.h"
98
99 #include <netinet/in.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/ip.h>
102 #include <netinet/ip_var.h>
103 #include <netinet/in_var.h>
104
105 #include <netinet/ip6.h>
106 #include <netinet6/ip6_var.h>
107 #include <netinet6/in6_var.h>
108 #include <netinet/ip_ecn.h>
109
110 #include <netinet/ip_encap.h>
111
112 #include <machine/stdarg.h>
113
114 #include <net/net_osdep.h>
115
116 #include <net/bpf.h>
117
118 #define IN6_IS_ADDR_6TO4(x)     (ntohs((x)->s6_addr16[0]) == 0x2002)
119 #define GET_V4(x)       ((struct in_addr *)(&(x)->s6_addr16[1]))
120
121 struct stf_softc {
122         struct ifnet    sc_if;     /* common area */
123         union {
124                 struct route  __sc_ro4;
125                 struct route_in6 __sc_ro6; /* just for safety */
126         } __sc_ro46;
127 #define sc_ro   __sc_ro46.__sc_ro4
128         const struct encaptab *encap_cookie;
129 };
130
131 static struct stf_softc *stf;
132
133 static MALLOC_DEFINE(M_STF, "stf", "6to4 Tunnel Interface");
134 static int ip_stf_ttl = 40;
135
136 extern  struct domain inetdomain;
137 struct protosw in_stf_protosw =
138 { SOCK_RAW,     &inetdomain,    IPPROTO_IPV6,   PR_ATOMIC|PR_ADDR,
139   in_stf_input, rip_output,     0,              rip_ctloutput,
140   0,
141   0,            0,              0,              0,
142   &rip_usrreqs
143 };
144
145 static int stfmodevent (module_t, int, void *);
146 static int stf_encapcheck (const struct mbuf *, int, int, void *);
147 static struct in6_ifaddr *stf_getsrcifa6 (struct ifnet *);
148 static int stf_output (struct ifnet *, struct mbuf *, struct sockaddr *,
149         struct rtentry *);
150 static int stf_checkaddr4 (struct stf_softc *, struct in_addr *,
151         struct ifnet *);
152 static int stf_checkaddr6 (struct stf_softc *, struct in6_addr *,
153         struct ifnet *);
154 static void stf_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
155 static int stf_ioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
156
157 static int
158 stfmodevent(module_t mod, int type, void *data)
159 {
160         struct stf_softc *sc;
161         int err;
162         const struct encaptab *p;
163
164         switch (type) {
165         case MOD_LOAD:
166                 stf = kmalloc(sizeof(struct stf_softc), M_STF, M_WAITOK);
167                 bzero(stf, sizeof(struct stf_softc));
168                 sc = stf;
169
170                 bzero(sc, sizeof(*sc));
171                 if_initname(&(sc->sc_if), "stf", 0);
172
173                 p = encap_attach_func(AF_INET, IPPROTO_IPV6, stf_encapcheck,
174                     (void *)&in_stf_protosw, sc);
175                 if (p == NULL) {
176                         kprintf("%s: attach failed\n", if_name(&sc->sc_if));
177                         return (ENOMEM);
178                 }
179                 sc->encap_cookie = p;
180
181                 sc->sc_if.if_mtu    = IPV6_MMTU;
182                 sc->sc_if.if_flags  = 0;
183                 sc->sc_if.if_ioctl  = stf_ioctl;
184                 sc->sc_if.if_output = stf_output;
185                 sc->sc_if.if_type   = IFT_STF;
186 #if 0
187                 /* turn off ingress filter */
188                 sc->sc_if.if_flags  |= IFF_LINK2;
189 #endif
190                 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
191                 if_attach(&sc->sc_if, NULL);
192                 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
193                 break;
194         case MOD_UNLOAD:
195                 sc = stf;
196                 bpfdetach(&sc->sc_if);
197                 if_detach(&sc->sc_if);
198                 err = encap_detach(sc->encap_cookie);
199                 KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
200                 kfree(sc, M_STF);
201                 break;
202         }
203
204         return (0);
205 }
206
207 static moduledata_t stf_mod = {
208         "if_stf",
209         stfmodevent,
210         0
211 };
212
213 DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
214
215 static int
216 stf_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
217 {
218         struct ip ip;
219         struct in6_ifaddr *ia6;
220         struct stf_softc *sc;
221         struct in_addr a, b;
222
223         sc = (struct stf_softc *)arg;
224         if (sc == NULL)
225                 return 0;
226
227         if ((sc->sc_if.if_flags & IFF_UP) == 0)
228                 return 0;
229
230         /* IFF_LINK0 means "no decapsulation" */
231         if ((sc->sc_if.if_flags & IFF_LINK0) != 0)
232                 return 0;
233
234         if (proto != IPPROTO_IPV6)
235                 return 0;
236
237         m_copydata(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
273 static struct in6_ifaddr *
274 stf_getsrcifa6(struct ifnet *ifp)
275 {
276         struct ifaddr *ia;
277         struct in_ifaddr *ia4;
278         struct sockaddr_in6 *sin6;
279         struct in_addr in;
280
281         TAILQ_FOREACH(ia, &ifp->if_addrlist, ifa_list) {
282                 if (ia->ifa_addr == NULL)
283                         continue;
284                 if (ia->ifa_addr->sa_family != AF_INET6)
285                         continue;
286                 sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
287                 if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
288                         continue;
289
290                 bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
291                 LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
292                         if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
293                                 break;
294                 if (ia4 == NULL)
295                         continue;
296
297                 return (struct in6_ifaddr *)ia;
298         }
299
300         return NULL;
301 }
302
303 static int
304 stf_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
305            struct rtentry *rt)
306 {
307         struct stf_softc *sc;
308         struct sockaddr_in6 *dst6;
309         struct in_addr *in4;
310         struct sockaddr_in *dst4;
311         u_int8_t tos;
312         struct ip *ip;
313         struct ip6_hdr *ip6;
314         struct in6_ifaddr *ia6;
315         static const uint32_t af = AF_INET6;
316
317         sc = (struct stf_softc*)ifp;
318         dst6 = (struct sockaddr_in6 *)dst;
319
320         /* just in case */
321         if ((ifp->if_flags & IFF_UP) == 0) {
322                 m_freem(m);
323                 return ENETDOWN;
324         }
325
326         /*
327          * If we don't have an ip4 address that match my inner ip6 address,
328          * we shouldn't generate output.  Without this check, we'll end up
329          * using wrong IPv4 source.
330          */
331         ia6 = stf_getsrcifa6(ifp);
332         if (ia6 == NULL) {
333                 m_freem(m);
334                 return ENETDOWN;
335         }
336
337         if (m->m_len < sizeof(*ip6)) {
338                 m = m_pullup(m, sizeof(*ip6));
339                 if (!m)
340                         return ENOBUFS;
341         }
342         ip6 = mtod(m, struct ip6_hdr *);
343         tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
344
345         /*
346          * Pickup the right outer dst addr from the list of candidates.
347          * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
348          */
349         if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
350                 in4 = GET_V4(&ip6->ip6_dst);
351         else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
352                 in4 = GET_V4(&dst6->sin6_addr);
353         else {
354                 m_freem(m);
355                 return ENETUNREACH;
356         }
357
358         if (ifp->if_bpf)
359                 bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
360
361         M_PREPEND(m, sizeof(struct ip), MB_DONTWAIT);
362         if (m && m->m_len < sizeof(struct ip))
363                 m = m_pullup(m, sizeof(struct ip));
364         if (m == NULL)
365                 return ENOBUFS;
366         ip = mtod(m, struct ip *);
367
368         bzero(ip, sizeof(*ip));
369
370         bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
371             &ip->ip_src, sizeof(ip->ip_src));
372         bcopy(in4, &ip->ip_dst, sizeof(ip->ip_dst));
373         ip->ip_p = IPPROTO_IPV6;
374         ip->ip_ttl = ip_stf_ttl;
375         ip->ip_len = m->m_pkthdr.len;   /*host order*/
376         if (ifp->if_flags & IFF_LINK1)
377                 ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
378         else
379                 ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
380
381         dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
382         if (dst4->sin_family != AF_INET ||
383             bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
384                 /* cache route doesn't match */
385                 dst4->sin_family = AF_INET;
386                 dst4->sin_len = sizeof(struct sockaddr_in);
387                 bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
388                 if (sc->sc_ro.ro_rt) {
389                         RTFREE(sc->sc_ro.ro_rt);
390                         sc->sc_ro.ro_rt = NULL;
391                 }
392         }
393
394         if (sc->sc_ro.ro_rt == NULL) {
395                 rtalloc(&sc->sc_ro);
396                 if (sc->sc_ro.ro_rt == NULL) {
397                         m_freem(m);
398                         return ENETUNREACH;
399                 }
400         }
401
402         return ip_output(m, NULL, &sc->sc_ro, 0, NULL, NULL);
403 }
404
405 /*
406  * Parameters:
407  *      inifp:  incoming interface
408  */
409 static int
410 stf_checkaddr4(struct stf_softc *sc, struct in_addr *in, struct ifnet *inifp)
411 {
412         struct in_ifaddr *ia4;
413
414         /*
415          * reject packets with the following address:
416          * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
417          */
418         if (IN_MULTICAST(ntohl(in->s_addr)))
419                 return -1;
420         switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
421         case 0: case 127: case 255:
422                 return -1;
423         }
424
425         /*
426          * reject packets with broadcast
427          */
428         TAILQ_FOREACH(ia4, &in_ifaddrhead, ia_link)
429         {
430                 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
431                         continue;
432                 if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
433                         return -1;
434         }
435
436         /*
437          * perform ingress filter
438          */
439         if (sc && (sc->sc_if.if_flags & IFF_LINK2) == 0 && inifp) {
440                 struct sockaddr_in sin;
441                 struct rtentry *rt;
442
443                 bzero(&sin, sizeof(sin));
444                 sin.sin_family = AF_INET;
445                 sin.sin_len = sizeof(struct sockaddr_in);
446                 sin.sin_addr = *in;
447                 rt = rtpurelookup((struct sockaddr *)&sin);
448                 if (!rt || rt->rt_ifp != inifp) {
449 #if 0
450                         log(LOG_WARNING, "%s: packet from 0x%x dropped "
451                             "due to ingress filter\n", if_name(&sc->sc_if),
452                             (u_int32_t)ntohl(sin.sin_addr.s_addr));
453 #endif
454                         if (rt)
455                                 rtfree(rt);
456                         return -1;
457                 }
458                 rtfree(rt);
459         }
460
461         return 0;
462 }
463
464 /*
465  * Parameters:
466  *      inifp:  incoming interface
467  */
468 static int
469 stf_checkaddr6(struct stf_softc *sc, struct in6_addr *in6, struct ifnet *inifp)
470 {
471         /*
472          * check 6to4 addresses
473          */
474         if (IN6_IS_ADDR_6TO4(in6))
475                 return stf_checkaddr4(sc, GET_V4(in6), inifp);
476
477         /*
478          * reject anything that look suspicious.  the test is implemented
479          * in ip6_input too, but we check here as well to
480          * (1) reject bad packets earlier, and
481          * (2) to be safe against future ip6_input change.
482          */
483         if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
484                 return -1;
485
486         return 0;
487 }
488
489 void
490 in_stf_input(struct mbuf *m, ...)
491 {
492         struct stf_softc *sc;
493         struct ip *ip;
494         struct ip6_hdr *ip6;
495         u_int8_t otos, itos;
496         struct ifnet *ifp;
497         int off, proto;
498         static const uint32_t af = AF_INET6;
499         __va_list ap;
500
501         __va_start(ap, m);
502         off = __va_arg(ap, int);
503         proto = __va_arg(ap, int);
504         __va_end(ap);
505
506         if (proto != IPPROTO_IPV6) {
507                 m_freem(m);
508                 return;
509         }
510
511         ip = mtod(m, struct ip *);
512
513         sc = (struct stf_softc *)encap_getarg(m);
514
515         if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
516                 m_freem(m);
517                 return;
518         }
519
520         ifp = &sc->sc_if;
521
522         /*
523          * perform sanity check against outer src/dst.
524          * for source, perform ingress filter as well.
525          */
526         if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
527             stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
528                 m_freem(m);
529                 return;
530         }
531
532         otos = ip->ip_tos;
533         m_adj(m, off);
534
535         if (m->m_len < sizeof(*ip6)) {
536                 m = m_pullup(m, sizeof(*ip6));
537                 if (!m)
538                         return;
539         }
540         ip6 = mtod(m, struct ip6_hdr *);
541
542         /*
543          * perform sanity check against inner src/dst.
544          * for source, perform ingress filter as well.
545          */
546         if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
547             stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
548                 m_freem(m);
549                 return;
550         }
551
552         itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
553         if ((ifp->if_flags & IFF_LINK1) != 0)
554                 ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
555         else
556                 ip_ecn_egress(ECN_NOCARE, &otos, &itos);
557         ip6->ip6_flow &= ~htonl(0xff << 20);
558         ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
559
560         m->m_pkthdr.rcvif = ifp;
561
562         if (ifp->if_bpf)
563                 bpf_ptap(ifp->if_bpf, m, &af, sizeof(af));
564
565         /*
566          * Put the packet to the network layer input queue according to the
567          * specified address family.
568          * See net/if_gif.c for possible issues with packet processing
569          * reorder due to extra queueing.
570          */
571         ifp->if_ipackets++;
572         ifp->if_ibytes += m->m_pkthdr.len;
573         netisr_dispatch(NETISR_IPV6, m);
574 }
575
576 /* ARGSUSED */
577 static void
578 stf_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
579 {
580
581         if (rt)
582                 rt->rt_rmx.rmx_mtu = IPV6_MMTU;
583 }
584
585 static int
586 stf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
587 {
588         struct ifaddr *ifa;
589         struct ifreq *ifr;
590         struct sockaddr_in6 *sin6;
591         int error;
592
593         error = 0;
594         switch (cmd) {
595         case SIOCSIFADDR:
596                 ifa = (struct ifaddr *)data;
597                 if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
598                         error = EAFNOSUPPORT;
599                         break;
600                 }
601                 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
602                 if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
603                         ifa->ifa_rtrequest = stf_rtrequest;
604                         ifp->if_flags |= IFF_UP;
605                 } else
606                         error = EINVAL;
607                 break;
608
609         case SIOCADDMULTI:
610         case SIOCDELMULTI:
611                 ifr = (struct ifreq *)data;
612                 if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
613                         ;
614                 else
615                         error = EAFNOSUPPORT;
616                 break;
617
618         default:
619                 error = EINVAL;
620                 break;
621         }
622
623         return error;
624 }