source: rtems-libbsd/freebsd/sys/net/if_gif.c @ 09bbedc

55-freebsd-126-freebsd-12
Last change on this file since 09bbedc 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: 24.9 KB
Line 
1#include <machine/rtems-bsd-kernel-space.h>
2
3/*-
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *      $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD$");
36
37#include <rtems/bsd/local/opt_inet.h>
38#include <rtems/bsd/local/opt_inet6.h>
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/jail.h>
43#include <sys/kernel.h>
44#include <sys/lock.h>
45#include <sys/malloc.h>
46#include <sys/mbuf.h>
47#include <sys/module.h>
48#include <sys/rmlock.h>
49#include <sys/socket.h>
50#include <sys/sockio.h>
51#include <sys/sx.h>
52#include <sys/errno.h>
53#include <sys/time.h>
54#include <sys/sysctl.h>
55#include <sys/syslog.h>
56#include <sys/priv.h>
57#include <sys/proc.h>
58#include <sys/protosw.h>
59#include <sys/conf.h>
60#include <machine/cpu.h>
61
62#include <net/if.h>
63#include <net/if_var.h>
64#include <net/if_clone.h>
65#include <net/if_types.h>
66#include <net/netisr.h>
67#include <net/route.h>
68#include <net/bpf.h>
69#include <net/vnet.h>
70
71#include <netinet/in.h>
72#include <netinet/in_systm.h>
73#include <netinet/ip.h>
74#include <netinet/ip_ecn.h>
75#ifdef  INET
76#include <netinet/in_var.h>
77#include <netinet/ip_var.h>
78#endif  /* INET */
79
80#ifdef INET6
81#ifndef INET
82#include <netinet/in.h>
83#endif
84#include <netinet6/in6_var.h>
85#include <netinet/ip6.h>
86#include <netinet6/ip6_ecn.h>
87#include <netinet6/ip6_var.h>
88#include <netinet6/scope6_var.h>
89#include <netinet6/ip6protosw.h>
90#endif /* INET6 */
91
92#include <netinet/ip_encap.h>
93#include <net/ethernet.h>
94#include <net/if_bridgevar.h>
95#include <net/if_gif.h>
96
97#include <security/mac/mac_framework.h>
98
99static const char gifname[] = "gif";
100
101/*
102 * gif_mtx protects a per-vnet gif_softc_list.
103 */
104static VNET_DEFINE(struct mtx, gif_mtx);
105#define V_gif_mtx               VNET(gif_mtx)
106static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
107static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list);
108#define V_gif_softc_list        VNET(gif_softc_list)
109static struct sx gif_ioctl_sx;
110SX_SYSINIT(gif_ioctl_sx, &gif_ioctl_sx, "gif_ioctl");
111
112#define GIF_LIST_LOCK_INIT(x)           mtx_init(&V_gif_mtx, "gif_mtx", \
113                                            NULL, MTX_DEF)
114#define GIF_LIST_LOCK_DESTROY(x)        mtx_destroy(&V_gif_mtx)
115#define GIF_LIST_LOCK(x)                mtx_lock(&V_gif_mtx)
116#define GIF_LIST_UNLOCK(x)              mtx_unlock(&V_gif_mtx)
117
118void    (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
119void    (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
120void    (*ng_gif_attach_p)(struct ifnet *ifp);
121void    (*ng_gif_detach_p)(struct ifnet *ifp);
122
123static int      gif_check_nesting(struct ifnet *, struct mbuf *);
124static int      gif_set_tunnel(struct ifnet *, struct sockaddr *,
125    struct sockaddr *);
126static void     gif_delete_tunnel(struct ifnet *);
127static int      gif_ioctl(struct ifnet *, u_long, caddr_t);
128static int      gif_transmit(struct ifnet *, struct mbuf *);
129static void     gif_qflush(struct ifnet *);
130static int      gif_clone_create(struct if_clone *, int, caddr_t);
131static void     gif_clone_destroy(struct ifnet *);
132static VNET_DEFINE(struct if_clone *, gif_cloner);
133#define V_gif_cloner    VNET(gif_cloner)
134
135static int gifmodevent(module_t, int, void *);
136
137SYSCTL_DECL(_net_link);
138static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
139    "Generic Tunnel Interface");
140#ifndef MAX_GIF_NEST
141/*
142 * This macro controls the default upper limitation on nesting of gif tunnels.
143 * Since, setting a large value to this macro with a careless configuration
144 * may introduce system crash, we don't allow any nestings by default.
145 * If you need to configure nested gif tunnels, you can define this macro
146 * in your kernel configuration file.  However, if you do so, please be
147 * careful to configure the tunnels so that it won't make a loop.
148 */
149#define MAX_GIF_NEST 1
150#endif
151static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST;
152#define V_max_gif_nesting       VNET(max_gif_nesting)
153SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW,
154    &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels");
155
156/*
157 * By default, we disallow creation of multiple tunnels between the same
158 * pair of addresses.  Some applications require this functionality so
159 * we allow control over this check here.
160 */
161#ifdef XBONEHACK
162static VNET_DEFINE(int, parallel_tunnels) = 1;
163#else
164static VNET_DEFINE(int, parallel_tunnels) = 0;
165#endif
166#define V_parallel_tunnels      VNET(parallel_tunnels)
167SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels,
168    CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(parallel_tunnels), 0,
169    "Allow parallel tunnels?");
170
171static int
172gif_clone_create(struct if_clone *ifc, int unit, caddr_t params)
173{
174        struct gif_softc *sc;
175
176        sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
177#ifndef __rtems__
178        sc->gif_fibnum = curthread->td_proc->p_fibnum;
179#else /* __rtems__ */
180        sc->gif_fibnum = BSD_DEFAULT_FIB;
181#endif /* __rtems__ */
182        GIF2IFP(sc) = if_alloc(IFT_GIF);
183        GIF_LOCK_INIT(sc);
184        GIF2IFP(sc)->if_softc = sc;
185        if_initname(GIF2IFP(sc), gifname, unit);
186
187        GIF2IFP(sc)->if_addrlen = 0;
188        GIF2IFP(sc)->if_mtu    = GIF_MTU;
189        GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
190#if 0
191        /* turn off ingress filter */
192        GIF2IFP(sc)->if_flags  |= IFF_LINK2;
193#endif
194        GIF2IFP(sc)->if_ioctl  = gif_ioctl;
195        GIF2IFP(sc)->if_transmit  = gif_transmit;
196        GIF2IFP(sc)->if_qflush  = gif_qflush;
197        GIF2IFP(sc)->if_output = gif_output;
198        GIF2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
199        GIF2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
200        if_attach(GIF2IFP(sc));
201        bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
202        if (ng_gif_attach_p != NULL)
203                (*ng_gif_attach_p)(GIF2IFP(sc));
204
205        GIF_LIST_LOCK();
206        LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
207        GIF_LIST_UNLOCK();
208        return (0);
209}
210
211static void
212gif_clone_destroy(struct ifnet *ifp)
213{
214        struct gif_softc *sc;
215
216        sx_xlock(&gif_ioctl_sx);
217        sc = ifp->if_softc;
218        gif_delete_tunnel(ifp);
219        GIF_LIST_LOCK();
220        LIST_REMOVE(sc, gif_list);
221        GIF_LIST_UNLOCK();
222        if (ng_gif_detach_p != NULL)
223                (*ng_gif_detach_p)(ifp);
224        bpfdetach(ifp);
225        if_detach(ifp);
226        ifp->if_softc = NULL;
227        sx_xunlock(&gif_ioctl_sx);
228
229        if_free(ifp);
230        GIF_LOCK_DESTROY(sc);
231        free(sc, M_GIF);
232}
233
234static void
235vnet_gif_init(const void *unused __unused)
236{
237
238        LIST_INIT(&V_gif_softc_list);
239        GIF_LIST_LOCK_INIT();
240        V_gif_cloner = if_clone_simple(gifname, gif_clone_create,
241            gif_clone_destroy, 0);
242}
243VNET_SYSINIT(vnet_gif_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
244    vnet_gif_init, NULL);
245
246static void
247vnet_gif_uninit(const void *unused __unused)
248{
249
250        if_clone_detach(V_gif_cloner);
251        GIF_LIST_LOCK_DESTROY();
252}
253VNET_SYSUNINIT(vnet_gif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
254    vnet_gif_uninit, NULL);
255
256static int
257gifmodevent(module_t mod, int type, void *data)
258{
259
260        switch (type) {
261        case MOD_LOAD:
262        case MOD_UNLOAD:
263                break;
264        default:
265                return (EOPNOTSUPP);
266        }
267        return (0);
268}
269
270static moduledata_t gif_mod = {
271        "if_gif",
272        gifmodevent,
273        0
274};
275
276DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
277MODULE_VERSION(if_gif, 1);
278
279int
280gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
281{
282        GIF_RLOCK_TRACKER;
283        const struct ip *ip;
284        struct gif_softc *sc;
285        int ret;
286
287        sc = (struct gif_softc *)arg;
288        if (sc == NULL || (GIF2IFP(sc)->if_flags & IFF_UP) == 0)
289                return (0);
290
291        ret = 0;
292        GIF_RLOCK(sc);
293
294        /* no physical address */
295        if (sc->gif_family == 0)
296                goto done;
297
298        switch (proto) {
299#ifdef INET
300        case IPPROTO_IPV4:
301#endif
302#ifdef INET6
303        case IPPROTO_IPV6:
304#endif
305        case IPPROTO_ETHERIP:
306                break;
307        default:
308                goto done;
309        }
310
311        /* Bail on short packets */
312        M_ASSERTPKTHDR(m);
313        if (m->m_pkthdr.len < sizeof(struct ip))
314                goto done;
315
316        ip = mtod(m, const struct ip *);
317        switch (ip->ip_v) {
318#ifdef INET
319        case 4:
320                if (sc->gif_family != AF_INET)
321                        goto done;
322                ret = in_gif_encapcheck(m, off, proto, arg);
323                break;
324#endif
325#ifdef INET6
326        case 6:
327                if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
328                        goto done;
329                if (sc->gif_family != AF_INET6)
330                        goto done;
331                ret = in6_gif_encapcheck(m, off, proto, arg);
332                break;
333#endif
334        }
335done:
336        GIF_RUNLOCK(sc);
337        return (ret);
338}
339
340static int
341gif_transmit(struct ifnet *ifp, struct mbuf *m)
342{
343        struct gif_softc *sc;
344        struct etherip_header *eth;
345#ifdef INET
346        struct ip *ip;
347#endif
348#ifdef INET6
349        struct ip6_hdr *ip6;
350        uint32_t t;
351#endif
352        uint32_t af;
353        uint8_t proto, ecn;
354        int error;
355
356#ifdef MAC
357        error = mac_ifnet_check_transmit(ifp, m);
358        if (error) {
359                m_freem(m);
360                goto err;
361        }
362#endif
363        error = ENETDOWN;
364        sc = ifp->if_softc;
365        if ((ifp->if_flags & IFF_MONITOR) != 0 ||
366            (ifp->if_flags & IFF_UP) == 0 ||
367            sc->gif_family == 0 ||
368            (error = gif_check_nesting(ifp, m)) != 0) {
369                m_freem(m);
370                goto err;
371        }
372        /* Now pull back the af that we stashed in the csum_data. */
373        if (ifp->if_bridge)
374                af = AF_LINK;
375        else
376                af = m->m_pkthdr.csum_data;
377        m->m_flags &= ~(M_BCAST|M_MCAST);
378        M_SETFIB(m, sc->gif_fibnum);
379        BPF_MTAP2(ifp, &af, sizeof(af), m);
380        if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
381        if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
382        /* inner AF-specific encapsulation */
383        ecn = 0;
384        switch (af) {
385#ifdef INET
386        case AF_INET:
387                proto = IPPROTO_IPV4;
388                if (m->m_len < sizeof(struct ip))
389                        m = m_pullup(m, sizeof(struct ip));
390                if (m == NULL) {
391                        error = ENOBUFS;
392                        goto err;
393                }
394                ip = mtod(m, struct ip *);
395                ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
396                    ECN_NOCARE, &ecn, &ip->ip_tos);
397                break;
398#endif
399#ifdef INET6
400        case AF_INET6:
401                proto = IPPROTO_IPV6;
402                if (m->m_len < sizeof(struct ip6_hdr))
403                        m = m_pullup(m, sizeof(struct ip6_hdr));
404                if (m == NULL) {
405                        error = ENOBUFS;
406                        goto err;
407                }
408                t = 0;
409                ip6 = mtod(m, struct ip6_hdr *);
410                ip6_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
411                    ECN_NOCARE, &t, &ip6->ip6_flow);
412                ecn = (ntohl(t) >> 20) & 0xff;
413                break;
414#endif
415        case AF_LINK:
416                proto = IPPROTO_ETHERIP;
417                M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT);
418                if (m == NULL) {
419                        error = ENOBUFS;
420                        goto err;
421                }
422                eth = mtod(m, struct etherip_header *);
423                eth->eip_resvh = 0;
424                eth->eip_ver = ETHERIP_VERSION;
425                eth->eip_resvl = 0;
426                break;
427        default:
428                error = EAFNOSUPPORT;
429                m_freem(m);
430                goto err;
431        }
432        /* XXX should we check if our outer source is legal? */
433        /* dispatch to output logic based on outer AF */
434        switch (sc->gif_family) {
435#ifdef INET
436        case AF_INET:
437                error = in_gif_output(ifp, m, proto, ecn);
438                break;
439#endif
440#ifdef INET6
441        case AF_INET6:
442                error = in6_gif_output(ifp, m, proto, ecn);
443                break;
444#endif
445        default:
446                m_freem(m);
447        }
448err:
449        if (error)
450                if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
451        return (error);
452}
453
454static void
455gif_qflush(struct ifnet *ifp __unused)
456{
457
458}
459
460#define MTAG_GIF        1080679712
461static int
462gif_check_nesting(struct ifnet *ifp, struct mbuf *m)
463{
464        struct m_tag *mtag;
465        int count;
466
467        /*
468         * gif may cause infinite recursion calls when misconfigured.
469         * We'll prevent this by detecting loops.
470         *
471         * High nesting level may cause stack exhaustion.
472         * We'll prevent this by introducing upper limit.
473         */
474        count = 1;
475        mtag = NULL;
476        while ((mtag = m_tag_locate(m, MTAG_GIF, 0, mtag)) != NULL) {
477                if (*(struct ifnet **)(mtag + 1) == ifp) {
478                        log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp));
479                        return (EIO);
480                }
481                count++;
482        }
483        if (count > V_max_gif_nesting) {
484                log(LOG_NOTICE,
485                    "%s: if_output recursively called too many times(%d)\n",
486                    if_name(ifp), count);
487                return (EIO);
488        }
489        mtag = m_tag_alloc(MTAG_GIF, 0, sizeof(struct ifnet *), M_NOWAIT);
490        if (mtag == NULL)
491                return (ENOMEM);
492        *(struct ifnet **)(mtag + 1) = ifp;
493        m_tag_prepend(m, mtag);
494        return (0);
495}
496
497int
498gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
499        struct route *ro)
500{
501        uint32_t af;
502
503        if (dst->sa_family == AF_UNSPEC)
504                bcopy(dst->sa_data, &af, sizeof(af));
505        else
506                af = dst->sa_family;
507        /*
508         * Now save the af in the inbound pkt csum data, this is a cheat since
509         * we are using the inbound csum_data field to carry the af over to
510         * the gif_transmit() routine, avoiding using yet another mtag.
511         */
512        m->m_pkthdr.csum_data = af;
513        return (ifp->if_transmit(ifp, m));
514}
515
516void
517gif_input(struct mbuf *m, struct ifnet *ifp, int proto, uint8_t ecn)
518{
519        struct etherip_header *eip;
520#ifdef INET
521        struct ip *ip;
522#endif
523#ifdef INET6
524        struct ip6_hdr *ip6;
525        uint32_t t;
526#endif
527        struct gif_softc *sc;
528        struct ether_header *eh;
529        struct ifnet *oldifp;
530        int isr, n, af;
531
532        if (ifp == NULL) {
533                /* just in case */
534                m_freem(m);
535                return;
536        }
537        sc = ifp->if_softc;
538        m->m_pkthdr.rcvif = ifp;
539        m_clrprotoflags(m);
540        switch (proto) {
541#ifdef INET
542        case IPPROTO_IPV4:
543                af = AF_INET;
544                if (m->m_len < sizeof(struct ip))
545                        m = m_pullup(m, sizeof(struct ip));
546                if (m == NULL)
547                        goto drop;
548                ip = mtod(m, struct ip *);
549                if (ip_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
550                    ECN_NOCARE, &ecn, &ip->ip_tos) == 0) {
551                        m_freem(m);
552                        goto drop;
553                }
554                break;
555#endif
556#ifdef INET6
557        case IPPROTO_IPV6:
558                af = AF_INET6;
559                if (m->m_len < sizeof(struct ip6_hdr))
560                        m = m_pullup(m, sizeof(struct ip6_hdr));
561                if (m == NULL)
562                        goto drop;
563                t = htonl((uint32_t)ecn << 20);
564                ip6 = mtod(m, struct ip6_hdr *);
565                if (ip6_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
566                    ECN_NOCARE, &t, &ip6->ip6_flow) == 0) {
567                        m_freem(m);
568                        goto drop;
569                }
570                break;
571#endif
572        case IPPROTO_ETHERIP:
573                af = AF_LINK;
574                break;
575        default:
576                m_freem(m);
577                goto drop;
578        }
579
580#ifdef MAC
581        mac_ifnet_create_mbuf(ifp, m);
582#endif
583
584        if (bpf_peers_present(ifp->if_bpf)) {
585                uint32_t af1 = af;
586                bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
587        }
588
589        if ((ifp->if_flags & IFF_MONITOR) != 0) {
590                if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
591                if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
592                m_freem(m);
593                return;
594        }
595
596        if (ng_gif_input_p != NULL) {
597                (*ng_gif_input_p)(ifp, &m, af);
598                if (m == NULL)
599                        goto drop;
600        }
601
602        /*
603         * Put the packet to the network layer input queue according to the
604         * specified address family.
605         * Note: older versions of gif_input directly called network layer
606         * input functions, e.g. ip6_input, here.  We changed the policy to
607         * prevent too many recursive calls of such input functions, which
608         * might cause kernel panic.  But the change may introduce another
609         * problem; if the input queue is full, packets are discarded.
610         * The kernel stack overflow really happened, and we believed
611         * queue-full rarely occurs, so we changed the policy.
612         */
613        switch (af) {
614#ifdef INET
615        case AF_INET:
616                isr = NETISR_IP;
617                break;
618#endif
619#ifdef INET6
620        case AF_INET6:
621                isr = NETISR_IPV6;
622                break;
623#endif
624        case AF_LINK:
625                n = sizeof(struct etherip_header) + sizeof(struct ether_header);
626                if (n > m->m_len)
627                        m = m_pullup(m, n);
628                if (m == NULL)
629                        goto drop;
630                eip = mtod(m, struct etherip_header *);
631                if (eip->eip_ver != ETHERIP_VERSION) {
632                        /* discard unknown versions */
633                        m_freem(m);
634                        goto drop;
635                }
636                m_adj(m, sizeof(struct etherip_header));
637
638                m->m_flags &= ~(M_BCAST|M_MCAST);
639                m->m_pkthdr.rcvif = ifp;
640
641                if (ifp->if_bridge) {
642                        oldifp = ifp;
643                        eh = mtod(m, struct ether_header *);
644                        if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
645                                if (ETHER_IS_BROADCAST(eh->ether_dhost))
646                                        m->m_flags |= M_BCAST;
647                                else
648                                        m->m_flags |= M_MCAST;
649                                if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
650                        }
651                        BRIDGE_INPUT(ifp, m);
652
653                        if (m != NULL && ifp != oldifp) {
654                                /*
655                                 * The bridge gave us back itself or one of the
656                                 * members for which the frame is addressed.
657                                 */
658                                ether_demux(ifp, m);
659                                return;
660                        }
661                }
662                if (m != NULL)
663                        m_freem(m);
664                return;
665
666        default:
667                if (ng_gif_input_orphan_p != NULL)
668                        (*ng_gif_input_orphan_p)(ifp, m, af);
669                else
670                        m_freem(m);
671                return;
672        }
673
674        if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
675        if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
676        M_SETFIB(m, ifp->if_fib);
677        netisr_dispatch(isr, m);
678        return;
679drop:
680        if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
681}
682
683/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
684int
685gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
686{
687        GIF_RLOCK_TRACKER;
688        struct ifreq *ifr = (struct ifreq*)data;
689        struct sockaddr *dst, *src;
690        struct gif_softc *sc;
691#ifdef INET
692        struct sockaddr_in *sin = NULL;
693#endif
694#ifdef INET6
695        struct sockaddr_in6 *sin6 = NULL;
696#endif
697        u_int options;
698        int error;
699
700        switch (cmd) {
701        case SIOCSIFADDR:
702                ifp->if_flags |= IFF_UP;
703        case SIOCADDMULTI:
704        case SIOCDELMULTI:
705        case SIOCGIFMTU:
706        case SIOCSIFFLAGS:
707                return (0);
708        case SIOCSIFMTU:
709                if (ifr->ifr_mtu < GIF_MTU_MIN ||
710                    ifr->ifr_mtu > GIF_MTU_MAX)
711                        return (EINVAL);
712                else
713                        ifp->if_mtu = ifr->ifr_mtu;
714                return (0);
715        }
716        sx_xlock(&gif_ioctl_sx);
717        sc = ifp->if_softc;
718        if (sc == NULL) {
719                error = ENXIO;
720                goto bad;
721        }
722        error = 0;
723        switch (cmd) {
724        case SIOCSIFPHYADDR:
725#ifdef INET6
726        case SIOCSIFPHYADDR_IN6:
727#endif
728                error = EINVAL;
729                switch (cmd) {
730#ifdef INET
731                case SIOCSIFPHYADDR:
732                        src = (struct sockaddr *)
733                                &(((struct in_aliasreq *)data)->ifra_addr);
734                        dst = (struct sockaddr *)
735                                &(((struct in_aliasreq *)data)->ifra_dstaddr);
736                        break;
737#endif
738#ifdef INET6
739                case SIOCSIFPHYADDR_IN6:
740                        src = (struct sockaddr *)
741                                &(((struct in6_aliasreq *)data)->ifra_addr);
742                        dst = (struct sockaddr *)
743                                &(((struct in6_aliasreq *)data)->ifra_dstaddr);
744                        break;
745#endif
746                default:
747                        goto bad;
748                }
749                /* sa_family must be equal */
750                if (src->sa_family != dst->sa_family ||
751                    src->sa_len != dst->sa_len)
752                        goto bad;
753
754                /* validate sa_len */
755                /* check sa_family looks sane for the cmd */
756                switch (src->sa_family) {
757#ifdef INET
758                case AF_INET:
759                        if (src->sa_len != sizeof(struct sockaddr_in))
760                                goto bad;
761                        if (cmd != SIOCSIFPHYADDR) {
762                                error = EAFNOSUPPORT;
763                                goto bad;
764                        }
765                        if (satosin(src)->sin_addr.s_addr == INADDR_ANY ||
766                            satosin(dst)->sin_addr.s_addr == INADDR_ANY) {
767                                error = EADDRNOTAVAIL;
768                                goto bad;
769                        }
770                        break;
771#endif
772#ifdef INET6
773                case AF_INET6:
774                        if (src->sa_len != sizeof(struct sockaddr_in6))
775                                goto bad;
776                        if (cmd != SIOCSIFPHYADDR_IN6) {
777                                error = EAFNOSUPPORT;
778                                goto bad;
779                        }
780                        error = EADDRNOTAVAIL;
781                        if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr)
782                            ||
783                            IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr))
784                                goto bad;
785                        /*
786                         * Check validity of the scope zone ID of the
787                         * addresses, and convert it into the kernel
788                         * internal form if necessary.
789                         */
790                        error = sa6_embedscope(satosin6(src), 0);
791                        if (error != 0)
792                                goto bad;
793                        error = sa6_embedscope(satosin6(dst), 0);
794                        if (error != 0)
795                                goto bad;
796                        break;
797#endif
798                default:
799                        error = EAFNOSUPPORT;
800                        goto bad;
801                }
802                error = gif_set_tunnel(ifp, src, dst);
803                break;
804        case SIOCDIFPHYADDR:
805                gif_delete_tunnel(ifp);
806                break;
807        case SIOCGIFPSRCADDR:
808        case SIOCGIFPDSTADDR:
809#ifdef INET6
810        case SIOCGIFPSRCADDR_IN6:
811        case SIOCGIFPDSTADDR_IN6:
812#endif
813                if (sc->gif_family == 0) {
814                        error = EADDRNOTAVAIL;
815                        break;
816                }
817                GIF_RLOCK(sc);
818                switch (cmd) {
819#ifdef INET
820                case SIOCGIFPSRCADDR:
821                case SIOCGIFPDSTADDR:
822                        if (sc->gif_family != AF_INET) {
823                                error = EADDRNOTAVAIL;
824                                break;
825                        }
826                        sin = (struct sockaddr_in *)&ifr->ifr_addr;
827                        memset(sin, 0, sizeof(*sin));
828                        sin->sin_family = AF_INET;
829                        sin->sin_len = sizeof(*sin);
830                        break;
831#endif
832#ifdef INET6
833                case SIOCGIFPSRCADDR_IN6:
834                case SIOCGIFPDSTADDR_IN6:
835                        if (sc->gif_family != AF_INET6) {
836                                error = EADDRNOTAVAIL;
837                                break;
838                        }
839                        sin6 = (struct sockaddr_in6 *)
840                                &(((struct in6_ifreq *)data)->ifr_addr);
841                        memset(sin6, 0, sizeof(*sin6));
842                        sin6->sin6_family = AF_INET6;
843                        sin6->sin6_len = sizeof(*sin6);
844                        break;
845#endif
846                default:
847                        error = EAFNOSUPPORT;
848                }
849                if (error == 0) {
850                        switch (cmd) {
851#ifdef INET
852                        case SIOCGIFPSRCADDR:
853                                sin->sin_addr = sc->gif_iphdr->ip_src;
854                                break;
855                        case SIOCGIFPDSTADDR:
856                                sin->sin_addr = sc->gif_iphdr->ip_dst;
857                                break;
858#endif
859#ifdef INET6
860                        case SIOCGIFPSRCADDR_IN6:
861                                sin6->sin6_addr = sc->gif_ip6hdr->ip6_src;
862                                break;
863                        case SIOCGIFPDSTADDR_IN6:
864                                sin6->sin6_addr = sc->gif_ip6hdr->ip6_dst;
865                                break;
866#endif
867                        }
868                }
869                GIF_RUNLOCK(sc);
870                if (error != 0)
871                        break;
872                switch (cmd) {
873#ifdef INET
874                case SIOCGIFPSRCADDR:
875                case SIOCGIFPDSTADDR:
876                        error = prison_if(curthread->td_ucred,
877                            (struct sockaddr *)sin);
878                        if (error != 0)
879                                memset(sin, 0, sizeof(*sin));
880                        break;
881#endif
882#ifdef INET6
883                case SIOCGIFPSRCADDR_IN6:
884                case SIOCGIFPDSTADDR_IN6:
885                        error = prison_if(curthread->td_ucred,
886                            (struct sockaddr *)sin6);
887                        if (error == 0)
888                                error = sa6_recoverscope(sin6);
889                        if (error != 0)
890                                memset(sin6, 0, sizeof(*sin6));
891#endif
892                }
893                break;
894        case SIOCGTUNFIB:
895                ifr->ifr_fib = sc->gif_fibnum;
896                break;
897        case SIOCSTUNFIB:
898                if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
899                        break;
900                if (ifr->ifr_fib >= rt_numfibs)
901                        error = EINVAL;
902                else
903                        sc->gif_fibnum = ifr->ifr_fib;
904                break;
905        case GIFGOPTS:
906                options = sc->gif_options;
907                error = copyout(&options, ifr->ifr_data, sizeof(options));
908                break;
909        case GIFSOPTS:
910                if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
911                        break;
912                error = copyin(ifr->ifr_data, &options, sizeof(options));
913                if (error)
914                        break;
915                if (options & ~GIF_OPTMASK)
916                        error = EINVAL;
917                else
918                        sc->gif_options = options;
919                break;
920        default:
921                error = EINVAL;
922                break;
923        }
924bad:
925        sx_xunlock(&gif_ioctl_sx);
926        return (error);
927}
928
929static void
930gif_detach(struct gif_softc *sc)
931{
932
933        sx_assert(&gif_ioctl_sx, SA_XLOCKED);
934        if (sc->gif_ecookie != NULL)
935                encap_detach(sc->gif_ecookie);
936        sc->gif_ecookie = NULL;
937}
938
939static int
940gif_attach(struct gif_softc *sc, int af)
941{
942
943        sx_assert(&gif_ioctl_sx, SA_XLOCKED);
944        switch (af) {
945#ifdef INET
946        case AF_INET:
947                return (in_gif_attach(sc));
948#endif
949#ifdef INET6
950        case AF_INET6:
951                return (in6_gif_attach(sc));
952#endif
953        }
954        return (EAFNOSUPPORT);
955}
956
957static int
958gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
959{
960        struct gif_softc *sc = ifp->if_softc;
961        struct gif_softc *tsc;
962#ifdef INET
963        struct ip *ip;
964#endif
965#ifdef INET6
966        struct ip6_hdr *ip6;
967#endif
968        void *hdr;
969        int error = 0;
970
971        if (sc == NULL)
972                return (ENXIO);
973        /* Disallow parallel tunnels unless instructed otherwise. */
974        if (V_parallel_tunnels == 0) {
975                GIF_LIST_LOCK();
976                LIST_FOREACH(tsc, &V_gif_softc_list, gif_list) {
977                        if (tsc == sc || tsc->gif_family != src->sa_family)
978                                continue;
979#ifdef INET
980                        if (tsc->gif_family == AF_INET &&
981                            tsc->gif_iphdr->ip_src.s_addr ==
982                            satosin(src)->sin_addr.s_addr &&
983                            tsc->gif_iphdr->ip_dst.s_addr ==
984                            satosin(dst)->sin_addr.s_addr) {
985                                error = EADDRNOTAVAIL;
986                                GIF_LIST_UNLOCK();
987                                goto bad;
988                        }
989#endif
990#ifdef INET6
991                        if (tsc->gif_family == AF_INET6 &&
992                            IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_src,
993                            &satosin6(src)->sin6_addr) &&
994                            IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_dst,
995                            &satosin6(dst)->sin6_addr)) {
996                                error = EADDRNOTAVAIL;
997                                GIF_LIST_UNLOCK();
998                                goto bad;
999                        }
1000#endif
1001                }
1002                GIF_LIST_UNLOCK();
1003        }
1004        switch (src->sa_family) {
1005#ifdef INET
1006        case AF_INET:
1007                hdr = ip = malloc(sizeof(struct ip), M_GIF,
1008                    M_WAITOK | M_ZERO);
1009                ip->ip_src.s_addr = satosin(src)->sin_addr.s_addr;
1010                ip->ip_dst.s_addr = satosin(dst)->sin_addr.s_addr;
1011                break;
1012#endif
1013#ifdef INET6
1014        case AF_INET6:
1015                hdr = ip6 = malloc(sizeof(struct ip6_hdr), M_GIF,
1016                    M_WAITOK | M_ZERO);
1017                ip6->ip6_src = satosin6(src)->sin6_addr;
1018                ip6->ip6_dst = satosin6(dst)->sin6_addr;
1019                ip6->ip6_vfc = IPV6_VERSION;
1020                break;
1021#endif
1022        default:
1023                return (EAFNOSUPPORT);
1024        }
1025
1026        if (sc->gif_family != src->sa_family)
1027                gif_detach(sc);
1028        if (sc->gif_family == 0 ||
1029            sc->gif_family != src->sa_family)
1030                error = gif_attach(sc, src->sa_family);
1031
1032        GIF_WLOCK(sc);
1033        if (sc->gif_family != 0)
1034                free(sc->gif_hdr, M_GIF);
1035        sc->gif_family = src->sa_family;
1036        sc->gif_hdr = hdr;
1037        GIF_WUNLOCK(sc);
1038#if defined(INET) || defined(INET6)
1039bad:
1040#endif
1041        if (error == 0 && sc->gif_family != 0) {
1042                ifp->if_drv_flags |= IFF_DRV_RUNNING;
1043                if_link_state_change(ifp, LINK_STATE_UP);
1044        } else {
1045                ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1046                if_link_state_change(ifp, LINK_STATE_DOWN);
1047        }
1048        return (error);
1049}
1050
1051static void
1052gif_delete_tunnel(struct ifnet *ifp)
1053{
1054        struct gif_softc *sc = ifp->if_softc;
1055        int family;
1056
1057        if (sc == NULL)
1058                return;
1059
1060        GIF_WLOCK(sc);
1061        family = sc->gif_family;
1062        sc->gif_family = 0;
1063        GIF_WUNLOCK(sc);
1064        if (family != 0) {
1065                gif_detach(sc);
1066                free(sc->gif_hdr, M_GIF);
1067        }
1068        ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1069        if_link_state_change(ifp, LINK_STATE_DOWN);
1070}
Note: See TracBrowser for help on using the repository browser.