]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_stf.c
sys: Remove $FreeBSD$: one-line .c comment pattern
[FreeBSD/FreeBSD.git] / sys / net / if_stf.c
1 /*      $KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi Exp $        */
2
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
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 <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/socket.h>
81 #include <sys/sockio.h>
82 #include <sys/mbuf.h>
83 #include <sys/errno.h>
84 #include <sys/kernel.h>
85 #include <sys/lock.h>
86 #include <sys/module.h>
87 #include <sys/proc.h>
88 #include <sys/queue.h>
89 #include <sys/rmlock.h>
90 #include <sys/sysctl.h>
91 #include <machine/cpu.h>
92
93 #include <sys/malloc.h>
94
95 #include <net/if.h>
96 #include <net/if_var.h>
97 #include <net/if_clone.h>
98 #include <net/route.h>
99 #include <net/route/nhop.h>
100 #include <net/netisr.h>
101 #include <net/if_types.h>
102 #include <net/vnet.h>
103
104 #include <netinet/in.h>
105 #include <netinet/in_fib.h>
106 #include <netinet/in_systm.h>
107 #include <netinet/ip.h>
108 #include <netinet/ip_var.h>
109 #include <netinet/in_var.h>
110
111 #include <netinet/ip6.h>
112 #include <netinet6/ip6_var.h>
113 #include <netinet6/in6_var.h>
114 #include <netinet/ip_ecn.h>
115
116 #include <netinet/ip_encap.h>
117
118 #include <machine/stdarg.h>
119
120 #include <net/bpf.h>
121
122 #include <security/mac/mac_framework.h>
123
124 SYSCTL_DECL(_net_link);
125 static SYSCTL_NODE(_net_link, IFT_STF, stf, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
126     "6to4 Interface");
127
128 static int stf_permit_rfc1918 = 0;
129 SYSCTL_INT(_net_link_stf, OID_AUTO, permit_rfc1918, CTLFLAG_RWTUN,
130     &stf_permit_rfc1918, 0, "Permit the use of private IPv4 addresses");
131
132 #define STFUNIT         0
133
134 #define IN6_IS_ADDR_6TO4(x)     (ntohs((x)->s6_addr16[0]) == 0x2002)
135
136 /*
137  * XXX: Return a pointer with 16-bit aligned.  Don't cast it to
138  * struct in_addr *; use bcopy() instead.
139  */
140 #define GET_V4(x)       (&(x)->s6_addr16[1])
141
142 struct stf_softc {
143         struct ifnet    *sc_ifp;
144         u_int   sc_fibnum;
145         const struct encaptab *encap_cookie;
146 };
147 #define STF2IFP(sc)     ((sc)->sc_ifp)
148
149 static const char stfname[] = "stf";
150
151 static MALLOC_DEFINE(M_STF, stfname, "6to4 Tunnel Interface");
152 static const int ip_stf_ttl = 40;
153
154 static int in_stf_input(struct mbuf *, int, int, void *);
155 static char *stfnames[] = {"stf0", "stf", "6to4", NULL};
156
157 static int stfmodevent(module_t, int, void *);
158 static int stf_encapcheck(const struct mbuf *, int, int, void *);
159 static int stf_getsrcifa6(struct ifnet *, struct in6_addr *, struct in6_addr *);
160 static int stf_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
161         struct route *);
162 static int isrfc1918addr(struct in_addr *);
163 static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
164         struct ifnet *);
165 static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
166         struct ifnet *);
167 static int stf_ioctl(struct ifnet *, u_long, caddr_t);
168
169 static int stf_clone_match(struct if_clone *, const char *);
170 static int stf_clone_create(struct if_clone *, char *, size_t, caddr_t);
171 static int stf_clone_destroy(struct if_clone *, struct ifnet *);
172 VNET_DEFINE_STATIC(struct if_clone *, stf_cloner);
173 #define V_stf_cloner    VNET(stf_cloner)
174
175 static const struct encap_config ipv4_encap_cfg = {
176         .proto = IPPROTO_IPV6,
177         .min_length = sizeof(struct ip),
178         .exact_match = (sizeof(in_addr_t) << 3) + 8,
179         .check = stf_encapcheck,
180         .input = in_stf_input
181 };
182
183 static int
184 stf_clone_match(struct if_clone *ifc, const char *name)
185 {
186         int i;
187
188         for(i = 0; stfnames[i] != NULL; i++) {
189                 if (strcmp(stfnames[i], name) == 0)
190                         return (1);
191         }
192
193         return (0);
194 }
195
196 static int
197 stf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
198 {
199         char *dp;
200         int err, unit, wildcard;
201         struct stf_softc *sc;
202         struct ifnet *ifp;
203
204         err = ifc_name2unit(name, &unit);
205         if (err != 0)
206                 return (err);
207         wildcard = (unit < 0);
208
209         /*
210          * We can only have one unit, but since unit allocation is
211          * already locked, we use it to keep from allocating extra
212          * interfaces.
213          */
214         unit = STFUNIT;
215         err = ifc_alloc_unit(ifc, &unit);
216         if (err != 0)
217                 return (err);
218
219         sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO);
220         ifp = STF2IFP(sc) = if_alloc(IFT_STF);
221         if (ifp == NULL) {
222                 free(sc, M_STF);
223                 ifc_free_unit(ifc, unit);
224                 return (ENOSPC);
225         }
226         ifp->if_softc = sc;
227         sc->sc_fibnum = curthread->td_proc->p_fibnum;
228
229         /*
230          * Set the name manually rather then using if_initname because
231          * we don't conform to the default naming convention for interfaces.
232          * In the wildcard case, we need to update the name.
233          */
234         if (wildcard) {
235                 for (dp = name; *dp != '\0'; dp++);
236                 if (snprintf(dp, len - (dp-name), "%d", unit) >
237                     len - (dp-name) - 1) {
238                         /*
239                          * This can only be a programmer error and
240                          * there's no straightforward way to recover if
241                          * it happens.
242                          */
243                         panic("if_clone_create(): interface name too long");
244                 }
245         }
246         strlcpy(ifp->if_xname, name, IFNAMSIZ);
247         ifp->if_dname = stfname;
248         ifp->if_dunit = IF_DUNIT_NONE;
249
250         sc->encap_cookie = ip_encap_attach(&ipv4_encap_cfg, sc, M_WAITOK);
251
252         ifp->if_mtu    = IPV6_MMTU;
253         ifp->if_ioctl  = stf_ioctl;
254         ifp->if_output = stf_output;
255         ifp->if_snd.ifq_maxlen = ifqmaxlen;
256         if_attach(ifp);
257         bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
258         return (0);
259 }
260
261 static int
262 stf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
263 {
264         struct stf_softc *sc = ifp->if_softc;
265         int err __unused;
266
267         err = ip_encap_detach(sc->encap_cookie);
268         KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
269         bpfdetach(ifp);
270         if_detach(ifp);
271         if_free(ifp);
272
273         free(sc, M_STF);
274         ifc_free_unit(ifc, STFUNIT);
275
276         return (0);
277 }
278
279 static void
280 vnet_stf_init(const void *unused __unused)
281 {
282         V_stf_cloner = if_clone_advanced(stfname, 0, stf_clone_match,
283             stf_clone_create, stf_clone_destroy);
284 }
285 VNET_SYSINIT(vnet_stf_init, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_stf_init, NULL);
286
287 static void
288 vnet_stf_uninit(const void *unused __unused)
289 {
290         if_clone_detach(V_stf_cloner);
291         V_stf_cloner = NULL;
292 }
293 VNET_SYSUNINIT(vnet_stf_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_stf_uninit,
294     NULL);
295
296 static int
297 stfmodevent(module_t mod, int type, void *data)
298 {
299
300         switch (type) {
301         case MOD_LOAD:
302                 /* Done in vnet_stf_init() */
303                 break;
304         case MOD_UNLOAD:
305                 /* Done in vnet_stf_uninit() */
306                 break;
307         default:
308                 return (EOPNOTSUPP);
309         }
310
311         return (0);
312 }
313
314 static moduledata_t stf_mod = {
315         "if_stf",
316         stfmodevent,
317         0
318 };
319
320 DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
321
322 static int
323 stf_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
324 {
325         struct ip ip;
326         struct stf_softc *sc;
327         struct in_addr a, b, mask;
328         struct in6_addr addr6, mask6;
329
330         sc = (struct stf_softc *)arg;
331         if (sc == NULL)
332                 return (0);
333
334         if ((STF2IFP(sc)->if_flags & IFF_UP) == 0)
335                 return (0);
336
337         /* IFF_LINK0 means "no decapsulation" */
338         if ((STF2IFP(sc)->if_flags & IFF_LINK0) != 0)
339                 return (0);
340
341         if (proto != IPPROTO_IPV6)
342                 return (0);
343
344         m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
345
346         if (ip.ip_v != 4)
347                 return (0);
348
349         if (stf_getsrcifa6(STF2IFP(sc), &addr6, &mask6) != 0)
350                 return (0);
351
352         /*
353          * check if IPv4 dst matches the IPv4 address derived from the
354          * local 6to4 address.
355          * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
356          */
357         if (bcmp(GET_V4(&addr6), &ip.ip_dst, sizeof(ip.ip_dst)) != 0)
358                 return (0);
359
360         /*
361          * check if IPv4 src matches the IPv4 address derived from the
362          * local 6to4 address masked by prefixmask.
363          * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
364          * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
365          */
366         bzero(&a, sizeof(a));
367         bcopy(GET_V4(&addr6), &a, sizeof(a));
368         bcopy(GET_V4(&mask6), &mask, sizeof(mask));
369         a.s_addr &= mask.s_addr;
370         b = ip.ip_src;
371         b.s_addr &= mask.s_addr;
372         if (a.s_addr != b.s_addr)
373                 return (0);
374
375         /* stf interface makes single side match only */
376         return (32);
377 }
378
379 static int
380 stf_getsrcifa6(struct ifnet *ifp, struct in6_addr *addr, struct in6_addr *mask)
381 {
382         struct rm_priotracker in_ifa_tracker;
383         struct ifaddr *ia;
384         struct in_ifaddr *ia4;
385         struct in6_ifaddr *ia6;
386         struct sockaddr_in6 *sin6;
387         struct in_addr in;
388
389         NET_EPOCH_ASSERT();
390
391         CK_STAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
392                 if (ia->ifa_addr->sa_family != AF_INET6)
393                         continue;
394                 sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
395                 if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
396                         continue;
397
398                 bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
399                 IN_IFADDR_RLOCK(&in_ifa_tracker);
400                 LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
401                         if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
402                                 break;
403                 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
404                 if (ia4 == NULL)
405                         continue;
406
407                 ia6 = (struct in6_ifaddr *)ia;
408
409                 *addr = sin6->sin6_addr;
410                 *mask = ia6->ia_prefixmask.sin6_addr;
411                 return (0);
412         }
413
414         return (ENOENT);
415 }
416
417 static int
418 stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
419     struct route *ro)
420 {
421         struct stf_softc *sc;
422         const struct sockaddr_in6 *dst6;
423         struct in_addr in4;
424         const void *ptr;
425         u_int8_t tos;
426         struct ip *ip;
427         struct ip6_hdr *ip6;
428         struct in6_addr addr6, mask6;
429         int error;
430
431 #ifdef MAC
432         error = mac_ifnet_check_transmit(ifp, m);
433         if (error) {
434                 m_freem(m);
435                 return (error);
436         }
437 #endif
438
439         sc = ifp->if_softc;
440         dst6 = (const struct sockaddr_in6 *)dst;
441
442         /* just in case */
443         if ((ifp->if_flags & IFF_UP) == 0) {
444                 m_freem(m);
445                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
446                 return (ENETDOWN);
447         }
448
449         /*
450          * If we don't have an ip4 address that match my inner ip6 address,
451          * we shouldn't generate output.  Without this check, we'll end up
452          * using wrong IPv4 source.
453          */
454         if (stf_getsrcifa6(ifp, &addr6, &mask6) != 0) {
455                 m_freem(m);
456                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
457                 return (ENETDOWN);
458         }
459
460         if (m->m_len < sizeof(*ip6)) {
461                 m = m_pullup(m, sizeof(*ip6));
462                 if (!m) {
463                         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
464                         return (ENOBUFS);
465                 }
466         }
467         ip6 = mtod(m, struct ip6_hdr *);
468         tos = IPV6_TRAFFIC_CLASS(ip6);
469
470         /*
471          * Pickup the right outer dst addr from the list of candidates.
472          * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
473          */
474         ptr = NULL;
475         if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
476                 ptr = GET_V4(&ip6->ip6_dst);
477         else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
478                 ptr = GET_V4(&dst6->sin6_addr);
479         else {
480                 m_freem(m);
481                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
482                 return (ENETUNREACH);
483         }
484         bcopy(ptr, &in4, sizeof(in4));
485
486         if (bpf_peers_present(ifp->if_bpf)) {
487                 /*
488                  * We need to prepend the address family as
489                  * a four byte field.  Cons up a dummy header
490                  * to pacify bpf.  This is safe because bpf
491                  * will only read from the mbuf (i.e., it won't
492                  * try to free it or keep a pointer a to it).
493                  */
494                 u_int af = AF_INET6;
495                 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
496         }
497
498         M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
499         if (m == NULL) {
500                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
501                 return (ENOBUFS);
502         }
503         ip = mtod(m, struct ip *);
504
505         bzero(ip, sizeof(*ip));
506
507         bcopy(GET_V4(&addr6), &ip->ip_src, sizeof(ip->ip_src));
508         bcopy(&in4, &ip->ip_dst, sizeof(ip->ip_dst));
509         ip->ip_p = IPPROTO_IPV6;
510         ip->ip_ttl = ip_stf_ttl;
511         ip->ip_len = htons(m->m_pkthdr.len);
512         if (ifp->if_flags & IFF_LINK1)
513                 ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
514         else
515                 ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
516
517         M_SETFIB(m, sc->sc_fibnum);
518         if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
519         error = ip_output(m, NULL, NULL, 0, NULL, NULL);
520
521         return (error);
522 }
523
524 static int
525 isrfc1918addr(struct in_addr *in)
526 {
527         /*
528          * returns 1 if private address range:
529          * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
530          */
531         if (stf_permit_rfc1918 == 0 && (
532             (ntohl(in->s_addr) & 0xff000000) >> 24 == 10 ||
533             (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 ||
534             (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168))
535                 return (1);
536
537         return (0);
538 }
539
540 static int
541 stf_checkaddr4(struct stf_softc *sc, struct in_addr *in, struct ifnet *inifp)
542 {
543         struct rm_priotracker in_ifa_tracker;
544         struct in_ifaddr *ia4;
545
546         /*
547          * reject packets with the following address:
548          * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
549          */
550         if (IN_MULTICAST(ntohl(in->s_addr)))
551                 return (-1);
552         switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
553         case 0: case 127: case 255:
554                 return (-1);
555         }
556
557         /*
558          * reject packets with private address range.
559          * (requirement from RFC3056 section 2 1st paragraph)
560          */
561         if (isrfc1918addr(in))
562                 return (-1);
563
564         /*
565          * reject packets with broadcast
566          */
567         IN_IFADDR_RLOCK(&in_ifa_tracker);
568         CK_STAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
569                 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
570                         continue;
571                 if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
572                         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
573                         return (-1);
574                 }
575         }
576         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
577
578         /*
579          * perform ingress filter
580          */
581         if (sc && (STF2IFP(sc)->if_flags & IFF_LINK2) == 0 && inifp) {
582                 struct nhop_object *nh;
583
584                 NET_EPOCH_ASSERT();
585                 nh = fib4_lookup(sc->sc_fibnum, *in, 0, 0, 0);
586                 if (nh == NULL)
587                         return (-1);
588
589                 if (nh->nh_ifp != inifp)
590                         return (-1);
591         }
592
593         return (0);
594 }
595
596 static int
597 stf_checkaddr6(struct stf_softc *sc, struct in6_addr *in6, struct ifnet *inifp)
598 {
599         /*
600          * check 6to4 addresses
601          */
602         if (IN6_IS_ADDR_6TO4(in6)) {
603                 struct in_addr in4;
604                 bcopy(GET_V4(in6), &in4, sizeof(in4));
605                 return (stf_checkaddr4(sc, &in4, inifp));
606         }
607
608         /*
609          * reject anything that look suspicious.  the test is implemented
610          * in ip6_input too, but we check here as well to
611          * (1) reject bad packets earlier, and
612          * (2) to be safe against future ip6_input change.
613          */
614         if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
615                 return (-1);
616
617         return (0);
618 }
619
620 static int
621 in_stf_input(struct mbuf *m, int off, int proto, void *arg)
622 {
623         struct stf_softc *sc = arg;
624         struct ip *ip;
625         struct ip6_hdr *ip6;
626         u_int8_t otos, itos;
627         struct ifnet *ifp;
628
629         NET_EPOCH_ASSERT();
630
631         if (proto != IPPROTO_IPV6) {
632                 m_freem(m);
633                 return (IPPROTO_DONE);
634         }
635
636         ip = mtod(m, struct ip *);
637         if (sc == NULL || (STF2IFP(sc)->if_flags & IFF_UP) == 0) {
638                 m_freem(m);
639                 return (IPPROTO_DONE);
640         }
641
642         ifp = STF2IFP(sc);
643
644 #ifdef MAC
645         mac_ifnet_create_mbuf(ifp, m);
646 #endif
647
648         /*
649          * perform sanity check against outer src/dst.
650          * for source, perform ingress filter as well.
651          */
652         if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
653             stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
654                 m_freem(m);
655                 return (IPPROTO_DONE);
656         }
657
658         otos = ip->ip_tos;
659         m_adj(m, off);
660
661         if (m->m_len < sizeof(*ip6)) {
662                 m = m_pullup(m, sizeof(*ip6));
663                 if (!m)
664                         return (IPPROTO_DONE);
665         }
666         ip6 = mtod(m, struct ip6_hdr *);
667
668         /*
669          * perform sanity check against inner src/dst.
670          * for source, perform ingress filter as well.
671          */
672         if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
673             stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
674                 m_freem(m);
675                 return (IPPROTO_DONE);
676         }
677
678         itos = IPV6_TRAFFIC_CLASS(ip6);
679         if ((ifp->if_flags & IFF_LINK1) != 0)
680                 ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
681         else
682                 ip_ecn_egress(ECN_NOCARE, &otos, &itos);
683         ip6->ip6_flow &= ~htonl(0xff << 20);
684         ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
685
686         m->m_pkthdr.rcvif = ifp;
687
688         if (bpf_peers_present(ifp->if_bpf)) {
689                 /*
690                  * We need to prepend the address family as
691                  * a four byte field.  Cons up a dummy header
692                  * to pacify bpf.  This is safe because bpf
693                  * will only read from the mbuf (i.e., it won't
694                  * try to free it or keep a pointer a to it).
695                  */
696                 u_int32_t af = AF_INET6;
697                 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
698         }
699
700         /*
701          * Put the packet to the network layer input queue according to the
702          * specified address family.
703          * See net/if_gif.c for possible issues with packet processing
704          * reorder due to extra queueing.
705          */
706         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
707         if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
708         M_SETFIB(m, ifp->if_fib);
709         netisr_dispatch(NETISR_IPV6, m);
710         return (IPPROTO_DONE);
711 }
712
713 static int
714 stf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
715 {
716         struct ifaddr *ifa;
717         struct ifreq *ifr;
718         struct sockaddr_in6 *sin6;
719         struct in_addr addr;
720         int error, mtu;
721
722         error = 0;
723         switch (cmd) {
724         case SIOCSIFADDR:
725                 ifa = (struct ifaddr *)data;
726                 if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
727                         error = EAFNOSUPPORT;
728                         break;
729                 }
730                 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
731                 if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
732                         error = EINVAL;
733                         break;
734                 }
735                 bcopy(GET_V4(&sin6->sin6_addr), &addr, sizeof(addr));
736                 if (isrfc1918addr(&addr)) {
737                         error = EINVAL;
738                         break;
739                 }
740
741                 ifp->if_flags |= IFF_UP;
742                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
743                 break;
744
745         case SIOCADDMULTI:
746         case SIOCDELMULTI:
747                 ifr = (struct ifreq *)data;
748                 if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
749                         ;
750                 else
751                         error = EAFNOSUPPORT;
752                 break;
753
754         case SIOCGIFMTU:
755                 break;
756
757         case SIOCSIFMTU:
758                 ifr = (struct ifreq *)data;
759                 mtu = ifr->ifr_mtu;
760                 /* RFC 4213 3.2 ideal world MTU */
761                 if (mtu < IPV6_MINMTU || mtu > IF_MAXMTU - 20)
762                         return (EINVAL);
763                 ifp->if_mtu = mtu;
764                 break;
765
766         default:
767                 error = EINVAL;
768                 break;
769         }
770
771         return (error);
772 }