source: rtems-libbsd/freebsd/sys/net/if_stf.c @ 6d9d7b1

55-freebsd-126-freebsd-12
Last change on this file since 6d9d7b1 was 3c967ca, checked in by Sebastian Huber <sebastian.huber@…>, on 06/08/17 at 11:15:12

Use <sys/lock.h> provided by Newlib

  • Property mode set to 100644
File size: 19.6 KB
Line 
1#include <machine/rtems-bsd-kernel-space.h>
2
3/*      $FreeBSD$       */
4/*      $KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi Exp $        */
5
6/*-
7 * Copyright (C) 2000 WIDE Project.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the project nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35/*
36 * 6to4 interface, based on RFC3056.
37 *
38 * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
39 * There is no address mapping defined from IPv6 multicast address to IPv4
40 * address.  Therefore, we do not have IFF_MULTICAST on the interface.
41 *
42 * Due to the lack of address mapping for link-local addresses, we cannot
43 * throw packets toward link-local addresses (fe80::x).  Also, we cannot throw
44 * packets to link-local multicast addresses (ff02::x).
45 *
46 * Here are interesting symptoms due to the lack of link-local address:
47 *
48 * Unicast routing exchange:
49 * - RIPng: Impossible.  Uses link-local multicast packet toward ff02::9,
50 *   and link-local addresses as nexthop.
51 * - OSPFv6: Impossible.  OSPFv6 assumes that there's link-local address
52 *   assigned to the link, and makes use of them.  Also, HELLO packets use
53 *   link-local multicast addresses (ff02::5 and ff02::6).
54 * - BGP4+: Maybe.  You can only use global address as nexthop, and global
55 *   address as TCP endpoint address.
56 *
57 * Multicast routing protocols:
58 * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
59 *   Adjacent PIM routers must be configured manually (is it really spec-wise
60 *   correct thing to do?).
61 *
62 * ICMPv6:
63 * - Redirects cannot be used due to the lack of link-local address.
64 *
65 * stf interface does not have, and will not need, a link-local address. 
66 * It seems to have no real benefit and does not help the above symptoms much.
67 * Even if we assign link-locals to interface, we cannot really
68 * use link-local unicast/multicast on top of 6to4 cloud (since there's no
69 * encapsulation defined for link-local address), and the above analysis does
70 * not change.  RFC3056 does not mandate the assignment of link-local address
71 * either.
72 *
73 * 6to4 interface has security issues.  Refer to
74 * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
75 * for details.  The code tries to filter out some of malicious packets.
76 * Note that there is no way to be 100% secure.
77 */
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/kernel.h>
86#include <sys/lock.h>
87#include <sys/module.h>
88#include <sys/protosw.h>
89#include <sys/proc.h>
90#include <sys/queue.h>
91#include <sys/rmlock.h>
92#include <sys/sysctl.h>
93#include <machine/cpu.h>
94
95#include <sys/malloc.h>
96
97#include <net/if.h>
98#include <net/if_var.h>
99#include <net/if_clone.h>
100#include <net/route.h>
101#include <net/netisr.h>
102#include <net/if_types.h>
103#include <net/vnet.h>
104
105#include <netinet/in.h>
106#include <netinet/in_fib.h>
107#include <netinet/in_systm.h>
108#include <netinet/ip.h>
109#include <netinet/ip_var.h>
110#include <netinet/in_var.h>
111
112#include <netinet/ip6.h>
113#include <netinet6/ip6_var.h>
114#include <netinet6/in6_var.h>
115#include <netinet/ip_ecn.h>
116
117#include <netinet/ip_encap.h>
118
119#include <machine/stdarg.h>
120
121#include <net/bpf.h>
122
123#include <security/mac/mac_framework.h>
124
125SYSCTL_DECL(_net_link);
126static SYSCTL_NODE(_net_link, IFT_STF, stf, CTLFLAG_RW, 0, "6to4 Interface");
127
128static int stf_permit_rfc1918 = 0;
129SYSCTL_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
142struct stf_softc {
143        struct ifnet    *sc_ifp;
144        struct mtx      sc_ro_mtx;
145        u_int   sc_fibnum;
146        const struct encaptab *encap_cookie;
147};
148#define STF2IFP(sc)     ((sc)->sc_ifp)
149
150static const char stfname[] = "stf";
151
152/*
153 * Note that mutable fields in the softc are not currently locked.
154 * We do lock sc_ro in stf_output though.
155 */
156static MALLOC_DEFINE(M_STF, stfname, "6to4 Tunnel Interface");
157static const int ip_stf_ttl = 40;
158
159extern  struct domain inetdomain;
160static int in_stf_input(struct mbuf **, int *, int);
161static struct protosw in_stf_protosw = {
162        .pr_type =              SOCK_RAW,
163        .pr_domain =            &inetdomain,
164        .pr_protocol =          IPPROTO_IPV6,
165        .pr_flags =             PR_ATOMIC|PR_ADDR,
166        .pr_input =             in_stf_input,
167        .pr_output =            rip_output,
168        .pr_ctloutput =         rip_ctloutput,
169        .pr_usrreqs =           &rip_usrreqs
170};
171
172static char *stfnames[] = {"stf0", "stf", "6to4", NULL};
173
174static int stfmodevent(module_t, int, void *);
175static int stf_encapcheck(const struct mbuf *, int, int, void *);
176static int stf_getsrcifa6(struct ifnet *, struct in6_addr *, struct in6_addr *);
177static int stf_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
178        struct route *);
179static int isrfc1918addr(struct in_addr *);
180static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
181        struct ifnet *);
182static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
183        struct ifnet *);
184static int stf_ioctl(struct ifnet *, u_long, caddr_t);
185
186static int stf_clone_match(struct if_clone *, const char *);
187static int stf_clone_create(struct if_clone *, char *, size_t, caddr_t);
188static int stf_clone_destroy(struct if_clone *, struct ifnet *);
189static struct if_clone *stf_cloner;
190
191static int
192stf_clone_match(struct if_clone *ifc, const char *name)
193{
194        int i;
195
196        for(i = 0; stfnames[i] != NULL; i++) {
197                if (strcmp(stfnames[i], name) == 0)
198                        return (1);
199        }
200
201        return (0);
202}
203
204static int
205stf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
206{
207        char *dp;
208        int err, unit, wildcard;
209        struct stf_softc *sc;
210        struct ifnet *ifp;
211
212        err = ifc_name2unit(name, &unit);
213        if (err != 0)
214                return (err);
215        wildcard = (unit < 0);
216
217        /*
218         * We can only have one unit, but since unit allocation is
219         * already locked, we use it to keep from allocating extra
220         * interfaces.
221         */
222        unit = STFUNIT;
223        err = ifc_alloc_unit(ifc, &unit);
224        if (err != 0)
225                return (err);
226
227        sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO);
228        ifp = STF2IFP(sc) = if_alloc(IFT_STF);
229        if (ifp == NULL) {
230                free(sc, M_STF);
231                ifc_free_unit(ifc, unit);
232                return (ENOSPC);
233        }
234        ifp->if_softc = sc;
235#ifndef __rtems__
236        sc->sc_fibnum = curthread->td_proc->p_fibnum;
237#else /* __rtems__ */
238        sc->sc_fibnum = BSD_DEFAULT_FIB;
239#endif /* __rtems__ */
240
241        /*
242         * Set the name manually rather then using if_initname because
243         * we don't conform to the default naming convention for interfaces.
244         * In the wildcard case, we need to update the name.
245         */
246        if (wildcard) {
247                for (dp = name; *dp != '\0'; dp++);
248                if (snprintf(dp, len - (dp-name), "%d", unit) >
249                    len - (dp-name) - 1) {
250                        /*
251                         * This can only be a programmer error and
252                         * there's no straightforward way to recover if
253                         * it happens.
254                         */
255                        panic("if_clone_create(): interface name too long");
256                }
257        }
258        strlcpy(ifp->if_xname, name, IFNAMSIZ);
259        ifp->if_dname = stfname;
260        ifp->if_dunit = IF_DUNIT_NONE;
261
262        mtx_init(&(sc)->sc_ro_mtx, "stf ro", NULL, MTX_DEF);
263        sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
264            stf_encapcheck, &in_stf_protosw, sc);
265        if (sc->encap_cookie == NULL) {
266                if_printf(ifp, "attach failed\n");
267                free(sc, M_STF);
268                ifc_free_unit(ifc, unit);
269                return (ENOMEM);
270        }
271
272        ifp->if_mtu    = IPV6_MMTU;
273        ifp->if_ioctl  = stf_ioctl;
274        ifp->if_output = stf_output;
275        ifp->if_snd.ifq_maxlen = ifqmaxlen;
276        if_attach(ifp);
277        bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
278        return (0);
279}
280
281static int
282stf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
283{
284        struct stf_softc *sc = ifp->if_softc;
285        int err;
286
287        err = encap_detach(sc->encap_cookie);
288        KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
289        mtx_destroy(&(sc)->sc_ro_mtx);
290        bpfdetach(ifp);
291        if_detach(ifp);
292        if_free(ifp);
293
294        free(sc, M_STF);
295        ifc_free_unit(ifc, STFUNIT);
296
297        return (0);
298}
299
300static int
301stfmodevent(module_t mod, int type, void *data)
302{
303
304        switch (type) {
305        case MOD_LOAD:
306                stf_cloner = if_clone_advanced(stfname, 0, stf_clone_match,
307                    stf_clone_create, stf_clone_destroy);
308                break;
309        case MOD_UNLOAD:
310                if_clone_detach(stf_cloner);
311                break;
312        default:
313                return (EOPNOTSUPP);
314        }
315
316        return (0);
317}
318
319static moduledata_t stf_mod = {
320        "if_stf",
321        stfmodevent,
322        0
323};
324
325DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
326
327static int
328stf_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
329{
330        struct ip ip;
331        struct stf_softc *sc;
332        struct in_addr a, b, mask;
333        struct in6_addr addr6, mask6;
334
335        sc = (struct stf_softc *)arg;
336        if (sc == NULL)
337                return 0;
338
339        if ((STF2IFP(sc)->if_flags & IFF_UP) == 0)
340                return 0;
341
342        /* IFF_LINK0 means "no decapsulation" */
343        if ((STF2IFP(sc)->if_flags & IFF_LINK0) != 0)
344                return 0;
345
346        if (proto != IPPROTO_IPV6)
347                return 0;
348
349        m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
350
351        if (ip.ip_v != 4)
352                return 0;
353
354        if (stf_getsrcifa6(STF2IFP(sc), &addr6, &mask6) != 0)
355                return (0);
356
357        /*
358         * check if IPv4 dst matches the IPv4 address derived from the
359         * local 6to4 address.
360         * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
361         */
362        if (bcmp(GET_V4(&addr6), &ip.ip_dst, sizeof(ip.ip_dst)) != 0)
363                return 0;
364
365        /*
366         * check if IPv4 src matches the IPv4 address derived from the
367         * local 6to4 address masked by prefixmask.
368         * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
369         * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
370         */
371        bzero(&a, sizeof(a));
372        bcopy(GET_V4(&addr6), &a, sizeof(a));
373        bcopy(GET_V4(&mask6), &mask, sizeof(mask));
374        a.s_addr &= mask.s_addr;
375        b = ip.ip_src;
376        b.s_addr &= mask.s_addr;
377        if (a.s_addr != b.s_addr)
378                return 0;
379
380        /* stf interface makes single side match only */
381        return 32;
382}
383
384static int
385stf_getsrcifa6(struct ifnet *ifp, struct in6_addr *addr, struct in6_addr *mask)
386{
387        struct ifaddr *ia;
388        struct in_ifaddr *ia4;
389        struct in6_ifaddr *ia6;
390        struct sockaddr_in6 *sin6;
391        struct in_addr in;
392
393        if_addr_rlock(ifp);
394        TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
395                if (ia->ifa_addr->sa_family != AF_INET6)
396                        continue;
397                sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
398                if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
399                        continue;
400
401                bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
402                LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
403                        if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
404                                break;
405                if (ia4 == NULL)
406                        continue;
407
408                ia6 = (struct in6_ifaddr *)ia;
409
410                *addr = sin6->sin6_addr;
411                *mask = ia6->ia_prefixmask.sin6_addr;
412                if_addr_runlock(ifp);
413                return (0);
414        }
415        if_addr_runlock(ifp);
416
417        return (ENOENT);
418}
419
420static int
421stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
422    struct route *ro)
423{
424        struct stf_softc *sc;
425        const struct sockaddr_in6 *dst6;
426        struct in_addr in4;
427        const void *ptr;
428        u_int8_t tos;
429        struct ip *ip;
430        struct ip6_hdr *ip6;
431        struct in6_addr addr6, mask6;
432        int error;
433
434#ifdef MAC
435        error = mac_ifnet_check_transmit(ifp, m);
436        if (error) {
437                m_freem(m);
438                return (error);
439        }
440#endif
441
442        sc = ifp->if_softc;
443        dst6 = (const struct sockaddr_in6 *)dst;
444
445        /* just in case */
446        if ((ifp->if_flags & IFF_UP) == 0) {
447                m_freem(m);
448                if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
449                return ENETDOWN;
450        }
451
452        /*
453         * If we don't have an ip4 address that match my inner ip6 address,
454         * we shouldn't generate output.  Without this check, we'll end up
455         * using wrong IPv4 source.
456         */
457        if (stf_getsrcifa6(ifp, &addr6, &mask6) != 0) {
458                m_freem(m);
459                if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
460                return ENETDOWN;
461        }
462
463        if (m->m_len < sizeof(*ip6)) {
464                m = m_pullup(m, sizeof(*ip6));
465                if (!m) {
466                        if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
467                        return ENOBUFS;
468                }
469        }
470        ip6 = mtod(m, struct ip6_hdr *);
471        tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
472
473        /*
474         * Pickup the right outer dst addr from the list of candidates.
475         * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
476         */
477        ptr = NULL;
478        if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
479                ptr = GET_V4(&ip6->ip6_dst);
480        else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
481                ptr = GET_V4(&dst6->sin6_addr);
482        else {
483                m_freem(m);
484                if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
485                return ENETUNREACH;
486        }
487        bcopy(ptr, &in4, sizeof(in4));
488
489        if (bpf_peers_present(ifp->if_bpf)) {
490                /*
491                 * We need to prepend the address family as
492                 * a four byte field.  Cons up a dummy header
493                 * to pacify bpf.  This is safe because bpf
494                 * will only read from the mbuf (i.e., it won't
495                 * try to free it or keep a pointer a to it).
496                 */
497                u_int af = AF_INET6;
498                bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
499        }
500
501        M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
502        if (m == NULL) {
503                if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
504                return ENOBUFS;
505        }
506        ip = mtod(m, struct ip *);
507
508        bzero(ip, sizeof(*ip));
509
510        bcopy(GET_V4(&addr6), &ip->ip_src, sizeof(ip->ip_src));
511        bcopy(&in4, &ip->ip_dst, sizeof(ip->ip_dst));
512        ip->ip_p = IPPROTO_IPV6;
513        ip->ip_ttl = ip_stf_ttl;
514        ip->ip_len = htons(m->m_pkthdr.len);
515        if (ifp->if_flags & IFF_LINK1)
516                ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
517        else
518                ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
519
520        M_SETFIB(m, sc->sc_fibnum);
521        if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
522        error = ip_output(m, NULL, NULL, 0, NULL, NULL);
523
524        return error;
525}
526
527static int
528isrfc1918addr(struct in_addr *in)
529{
530        /*
531         * returns 1 if private address range:
532         * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
533         */
534        if (stf_permit_rfc1918 == 0 && (
535            (ntohl(in->s_addr) & 0xff000000) >> 24 == 10 ||
536            (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 ||
537            (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168))
538                return 1;
539
540        return 0;
541}
542
543static int
544stf_checkaddr4(struct stf_softc *sc, struct in_addr *in, struct ifnet *inifp)
545{
546        struct rm_priotracker in_ifa_tracker;
547        struct in_ifaddr *ia4;
548
549        /*
550         * reject packets with the following address:
551         * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
552         */
553        if (IN_MULTICAST(ntohl(in->s_addr)))
554                return -1;
555        switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
556        case 0: case 127: case 255:
557                return -1;
558        }
559
560        /*
561         * reject packets with private address range.
562         * (requirement from RFC3056 section 2 1st paragraph)
563         */
564        if (isrfc1918addr(in))
565                return -1;
566
567        /*
568         * reject packets with broadcast
569         */
570        IN_IFADDR_RLOCK(&in_ifa_tracker);
571        TAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
572                if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
573                        continue;
574                if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
575                        IN_IFADDR_RUNLOCK(&in_ifa_tracker);
576                        return -1;
577                }
578        }
579        IN_IFADDR_RUNLOCK(&in_ifa_tracker);
580
581        /*
582         * perform ingress filter
583         */
584        if (sc && (STF2IFP(sc)->if_flags & IFF_LINK2) == 0 && inifp) {
585                struct nhop4_basic nh4;
586
587                if (fib4_lookup_nh_basic(sc->sc_fibnum, *in, 0, 0, &nh4) != 0)
588                        return (-1);
589
590                if (nh4.nh_ifp != inifp)
591                        return (-1);
592        }
593
594        return 0;
595}
596
597static int
598stf_checkaddr6(struct stf_softc *sc, struct in6_addr *in6, struct ifnet *inifp)
599{
600        /*
601         * check 6to4 addresses
602         */
603        if (IN6_IS_ADDR_6TO4(in6)) {
604                struct in_addr in4;
605                bcopy(GET_V4(in6), &in4, sizeof(in4));
606                return stf_checkaddr4(sc, &in4, inifp);
607        }
608
609        /*
610         * reject anything that look suspicious.  the test is implemented
611         * in ip6_input too, but we check here as well to
612         * (1) reject bad packets earlier, and
613         * (2) to be safe against future ip6_input change.
614         */
615        if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
616                return -1;
617
618        return 0;
619}
620
621static int
622in_stf_input(struct mbuf **mp, int *offp, int proto)
623{
624        struct stf_softc *sc;
625        struct ip *ip;
626        struct ip6_hdr *ip6;
627        struct mbuf *m;
628        u_int8_t otos, itos;
629        struct ifnet *ifp;
630        int off;
631
632        m = *mp;
633        off = *offp;
634
635        if (proto != IPPROTO_IPV6) {
636                m_freem(m);
637                return (IPPROTO_DONE);
638        }
639
640        ip = mtod(m, struct ip *);
641
642        sc = (struct stf_softc *)encap_getarg(m);
643
644        if (sc == NULL || (STF2IFP(sc)->if_flags & IFF_UP) == 0) {
645                m_freem(m);
646                return (IPPROTO_DONE);
647        }
648
649        ifp = STF2IFP(sc);
650
651#ifdef MAC
652        mac_ifnet_create_mbuf(ifp, m);
653#endif
654
655        /*
656         * perform sanity check against outer src/dst.
657         * for source, perform ingress filter as well.
658         */
659        if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
660            stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
661                m_freem(m);
662                return (IPPROTO_DONE);
663        }
664
665        otos = ip->ip_tos;
666        m_adj(m, off);
667
668        if (m->m_len < sizeof(*ip6)) {
669                m = m_pullup(m, sizeof(*ip6));
670                if (!m)
671                        return (IPPROTO_DONE);
672        }
673        ip6 = mtod(m, struct ip6_hdr *);
674
675        /*
676         * perform sanity check against inner src/dst.
677         * for source, perform ingress filter as well.
678         */
679        if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
680            stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
681                m_freem(m);
682                return (IPPROTO_DONE);
683        }
684
685        itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
686        if ((ifp->if_flags & IFF_LINK1) != 0)
687                ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
688        else
689                ip_ecn_egress(ECN_NOCARE, &otos, &itos);
690        ip6->ip6_flow &= ~htonl(0xff << 20);
691        ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
692
693        m->m_pkthdr.rcvif = ifp;
694       
695        if (bpf_peers_present(ifp->if_bpf)) {
696                /*
697                 * We need to prepend the address family as
698                 * a four byte field.  Cons up a dummy header
699                 * to pacify bpf.  This is safe because bpf
700                 * will only read from the mbuf (i.e., it won't
701                 * try to free it or keep a pointer a to it).
702                 */
703                u_int32_t af = AF_INET6;
704                bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
705        }
706
707        /*
708         * Put the packet to the network layer input queue according to the
709         * specified address family.
710         * See net/if_gif.c for possible issues with packet processing
711         * reorder due to extra queueing.
712         */
713        if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
714        if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
715        M_SETFIB(m, ifp->if_fib);
716        netisr_dispatch(NETISR_IPV6, m);
717        return (IPPROTO_DONE);
718}
719
720static int
721stf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
722{
723        struct ifaddr *ifa;
724        struct ifreq *ifr;
725        struct sockaddr_in6 *sin6;
726        struct in_addr addr;
727        int error, mtu;
728
729        error = 0;
730        switch (cmd) {
731        case SIOCSIFADDR:
732                ifa = (struct ifaddr *)data;
733                if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
734                        error = EAFNOSUPPORT;
735                        break;
736                }
737                sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
738                if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
739                        error = EINVAL;
740                        break;
741                }
742                bcopy(GET_V4(&sin6->sin6_addr), &addr, sizeof(addr));
743                if (isrfc1918addr(&addr)) {
744                        error = EINVAL;
745                        break;
746                }
747
748                ifp->if_flags |= IFF_UP;
749                break;
750
751        case SIOCADDMULTI:
752        case SIOCDELMULTI:
753                ifr = (struct ifreq *)data;
754                if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
755                        ;
756                else
757                        error = EAFNOSUPPORT;
758                break;
759
760        case SIOCGIFMTU:
761                break;
762
763        case SIOCSIFMTU:
764                ifr = (struct ifreq *)data;
765                mtu = ifr->ifr_mtu;
766                /* RFC 4213 3.2 ideal world MTU */
767                if (mtu < IPV6_MINMTU || mtu > IF_MAXMTU - 20)
768                        return (EINVAL);
769                ifp->if_mtu = mtu;
770                break;
771
772        default:
773                error = EINVAL;
774                break;
775        }
776
777        return error;
778}
Note: See TracBrowser for help on using the repository browser.