Home | History | Annotate | Line # | Download | only in net
if_gif.c revision 1.83
      1  1.83     rmind /*	$NetBSD: if_gif.c,v 1.83 2014/06/05 23:48:16 rmind Exp $	*/
      2  1.34    itojun /*	$KAME: if_gif.c,v 1.76 2001/08/20 02:01:02 kjc Exp $	*/
      3   1.3    itojun 
      4   1.2    itojun /*
      5   1.2    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6   1.2    itojun  * All rights reserved.
      7   1.9    itojun  *
      8   1.2    itojun  * Redistribution and use in source and binary forms, with or without
      9   1.2    itojun  * modification, are permitted provided that the following conditions
     10   1.2    itojun  * are met:
     11   1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     12   1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     13   1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     15   1.2    itojun  *    documentation and/or other materials provided with the distribution.
     16   1.2    itojun  * 3. Neither the name of the project nor the names of its contributors
     17   1.2    itojun  *    may be used to endorse or promote products derived from this software
     18   1.2    itojun  *    without specific prior written permission.
     19   1.9    itojun  *
     20   1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21   1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24   1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.2    itojun  * SUCH DAMAGE.
     31   1.2    itojun  */
     32  1.36     lukem 
     33  1.36     lukem #include <sys/cdefs.h>
     34  1.83     rmind __KERNEL_RCSID(0, "$NetBSD: if_gif.c,v 1.83 2014/06/05 23:48:16 rmind Exp $");
     35   1.2    itojun 
     36   1.2    itojun #include "opt_inet.h"
     37   1.2    itojun 
     38   1.2    itojun #include <sys/param.h>
     39   1.2    itojun #include <sys/systm.h>
     40   1.2    itojun #include <sys/kernel.h>
     41   1.2    itojun #include <sys/mbuf.h>
     42   1.2    itojun #include <sys/socket.h>
     43   1.2    itojun #include <sys/sockio.h>
     44   1.2    itojun #include <sys/errno.h>
     45   1.2    itojun #include <sys/ioctl.h>
     46   1.2    itojun #include <sys/time.h>
     47   1.2    itojun #include <sys/syslog.h>
     48  1.17    martin #include <sys/proc.h>
     49   1.9    itojun #include <sys/protosw.h>
     50  1.72        ad #include <sys/cpu.h>
     51  1.72        ad #include <sys/intr.h>
     52   1.2    itojun 
     53   1.2    itojun #include <net/if.h>
     54   1.2    itojun #include <net/if_types.h>
     55   1.2    itojun #include <net/netisr.h>
     56   1.2    itojun #include <net/route.h>
     57   1.2    itojun #include <net/bpf.h>
     58   1.2    itojun 
     59   1.2    itojun #include <netinet/in.h>
     60   1.2    itojun #include <netinet/in_systm.h>
     61  1.15    itojun #include <netinet/ip.h>
     62  1.15    itojun #ifdef	INET
     63   1.2    itojun #include <netinet/in_var.h>
     64  1.57  christos #endif	/* INET */
     65   1.2    itojun #include <netinet/in_gif.h>
     66   1.2    itojun 
     67   1.2    itojun #ifdef INET6
     68   1.2    itojun #ifndef INET
     69   1.2    itojun #include <netinet/in.h>
     70   1.2    itojun #endif
     71   1.2    itojun #include <netinet6/in6_var.h>
     72   1.2    itojun #include <netinet/ip6.h>
     73   1.2    itojun #include <netinet6/ip6_var.h>
     74   1.2    itojun #include <netinet6/in6_gif.h>
     75   1.9    itojun #include <netinet6/ip6protosw.h>
     76   1.2    itojun #endif /* INET6 */
     77   1.2    itojun 
     78   1.9    itojun #include <netinet/ip_encap.h>
     79   1.2    itojun #include <net/if_gif.h>
     80   1.2    itojun 
     81   1.2    itojun 
     82   1.4    itojun #include <net/net_osdep.h>
     83   1.4    itojun 
     84  1.56   thorpej void	gifattach(int);
     85  1.56   thorpej static void	gifintr(void *);
     86   1.2    itojun 
     87   1.2    itojun /*
     88   1.2    itojun  * gif global variable definitions
     89   1.2    itojun  */
     90  1.56   thorpej LIST_HEAD(, gif_softc) gif_softc_list;	/* XXX should be static */
     91  1.12   thorpej 
     92  1.56   thorpej static int	gif_clone_create(struct if_clone *, int);
     93  1.56   thorpej static int	gif_clone_destroy(struct ifnet *);
     94  1.12   thorpej 
     95  1.56   thorpej static struct if_clone gif_cloner =
     96  1.12   thorpej     IF_CLONE_INITIALIZER("gif", gif_clone_create, gif_clone_destroy);
     97  1.12   thorpej 
     98   1.9    itojun #ifndef MAX_GIF_NEST
     99   1.9    itojun /*
    100   1.9    itojun  * This macro controls the upper limitation on nesting of gif tunnels.
    101   1.9    itojun  * Since, setting a large value to this macro with a careless configuration
    102   1.9    itojun  * may introduce system crash, we don't allow any nestings by default.
    103   1.9    itojun  * If you need to configure nested gif tunnels, you can define this macro
    104  1.31    itojun  * in your kernel configuration file.  However, if you do so, please be
    105   1.9    itojun  * careful to configure the tunnels so that it won't make a loop.
    106   1.9    itojun  */
    107   1.9    itojun #define MAX_GIF_NEST 1
    108   1.9    itojun #endif
    109   1.9    itojun static int max_gif_nesting = MAX_GIF_NEST;
    110   1.2    itojun 
    111  1.12   thorpej /* ARGSUSED */
    112   1.2    itojun void
    113  1.63  christos gifattach(int count)
    114  1.12   thorpej {
    115  1.12   thorpej 
    116  1.12   thorpej 	LIST_INIT(&gif_softc_list);
    117  1.12   thorpej 	if_clone_attach(&gif_cloner);
    118  1.12   thorpej }
    119  1.12   thorpej 
    120  1.56   thorpej static int
    121  1.56   thorpej gif_clone_create(struct if_clone *ifc, int unit)
    122   1.2    itojun {
    123  1.12   thorpej 	struct gif_softc *sc;
    124  1.12   thorpej 
    125  1.75  christos 	sc = malloc(sizeof(struct gif_softc), M_DEVBUF, M_WAITOK|M_ZERO);
    126   1.2    itojun 
    127  1.75  christos 	if_initname(&sc->gif_if, ifc->ifc_name, unit);
    128   1.9    itojun 
    129  1.31    itojun 	gifattach0(sc);
    130  1.31    itojun 
    131  1.31    itojun 	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
    132  1.31    itojun 	return (0);
    133  1.31    itojun }
    134  1.31    itojun 
    135  1.31    itojun void
    136  1.56   thorpej gifattach0(struct gif_softc *sc)
    137  1.31    itojun {
    138  1.31    itojun 
    139  1.12   thorpej 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
    140   1.9    itojun 
    141  1.31    itojun 	sc->gif_if.if_addrlen = 0;
    142  1.12   thorpej 	sc->gif_if.if_mtu    = GIF_MTU;
    143  1.12   thorpej 	sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
    144  1.12   thorpej 	sc->gif_if.if_ioctl  = gif_ioctl;
    145  1.12   thorpej 	sc->gif_if.if_output = gif_output;
    146  1.12   thorpej 	sc->gif_if.if_type   = IFT_GIF;
    147  1.19   thorpej 	sc->gif_if.if_dlt    = DLT_NULL;
    148  1.76    dyoung 	sc->gif_if.if_softc  = sc;
    149  1.34    itojun 	IFQ_SET_READY(&sc->gif_if.if_snd);
    150  1.12   thorpej 	if_attach(&sc->gif_if);
    151  1.20   thorpej 	if_alloc_sadl(&sc->gif_if);
    152  1.78     joerg 	bpf_attach(&sc->gif_if, DLT_NULL, sizeof(u_int));
    153  1.12   thorpej }
    154  1.12   thorpej 
    155  1.56   thorpej static int
    156  1.56   thorpej gif_clone_destroy(struct ifnet *ifp)
    157  1.12   thorpej {
    158  1.12   thorpej 	struct gif_softc *sc = (void *) ifp;
    159  1.12   thorpej 
    160  1.31    itojun 	gif_delete_tunnel(&sc->gif_if);
    161  1.12   thorpej 	LIST_REMOVE(sc, gif_list);
    162  1.12   thorpej #ifdef INET6
    163  1.12   thorpej 	encap_detach(sc->encap_cookie6);
    164  1.12   thorpej #endif
    165  1.12   thorpej #ifdef INET
    166  1.12   thorpej 	encap_detach(sc->encap_cookie4);
    167  1.12   thorpej #endif
    168  1.12   thorpej 
    169  1.78     joerg 	bpf_detach(ifp);
    170  1.69    dyoung 	if_detach(ifp);
    171  1.66    dyoung 	rtcache_free(&sc->gif_ro);
    172  1.12   thorpej 
    173  1.12   thorpej 	free(sc, M_DEVBUF);
    174  1.47     peter 
    175  1.47     peter 	return (0);
    176   1.9    itojun }
    177   1.9    itojun 
    178  1.42    itojun #ifdef GIF_ENCAPCHECK
    179  1.31    itojun int
    180  1.56   thorpej gif_encapcheck(struct mbuf *m, int off, int proto, void *arg)
    181   1.9    itojun {
    182   1.9    itojun 	struct ip ip;
    183   1.9    itojun 	struct gif_softc *sc;
    184   1.9    itojun 
    185  1.76    dyoung 	sc = arg;
    186   1.9    itojun 	if (sc == NULL)
    187   1.9    itojun 		return 0;
    188   1.9    itojun 
    189   1.9    itojun 	if ((sc->gif_if.if_flags & IFF_UP) == 0)
    190   1.9    itojun 		return 0;
    191   1.9    itojun 
    192   1.9    itojun 	/* no physical address */
    193   1.9    itojun 	if (!sc->gif_psrc || !sc->gif_pdst)
    194   1.9    itojun 		return 0;
    195   1.9    itojun 
    196   1.9    itojun 	switch (proto) {
    197   1.9    itojun #ifdef INET
    198   1.9    itojun 	case IPPROTO_IPV4:
    199   1.9    itojun 		break;
    200   1.9    itojun #endif
    201   1.9    itojun #ifdef INET6
    202   1.9    itojun 	case IPPROTO_IPV6:
    203   1.9    itojun 		break;
    204   1.9    itojun #endif
    205   1.9    itojun 	default:
    206   1.9    itojun 		return 0;
    207   1.9    itojun 	}
    208  1.40  christos 
    209  1.40  christos 	/* Bail on short packets */
    210  1.40  christos 	KASSERT(m->m_flags & M_PKTHDR);
    211  1.40  christos 	if (m->m_pkthdr.len < sizeof(ip))
    212  1.40  christos 		return 0;
    213   1.9    itojun 
    214  1.76    dyoung 	m_copydata(m, 0, sizeof(ip), &ip);
    215   1.9    itojun 
    216   1.9    itojun 	switch (ip.ip_v) {
    217   1.9    itojun #ifdef INET
    218   1.9    itojun 	case 4:
    219   1.9    itojun 		if (sc->gif_psrc->sa_family != AF_INET ||
    220   1.9    itojun 		    sc->gif_pdst->sa_family != AF_INET)
    221   1.9    itojun 			return 0;
    222   1.9    itojun 		return gif_encapcheck4(m, off, proto, arg);
    223   1.9    itojun #endif
    224   1.9    itojun #ifdef INET6
    225   1.9    itojun 	case 6:
    226  1.41    itojun 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
    227  1.41    itojun 			return 0;
    228   1.9    itojun 		if (sc->gif_psrc->sa_family != AF_INET6 ||
    229   1.9    itojun 		    sc->gif_pdst->sa_family != AF_INET6)
    230   1.9    itojun 			return 0;
    231   1.9    itojun 		return gif_encapcheck6(m, off, proto, arg);
    232   1.9    itojun #endif
    233   1.9    itojun 	default:
    234   1.9    itojun 		return 0;
    235   1.2    itojun 	}
    236   1.2    itojun }
    237  1.42    itojun #endif
    238   1.2    itojun 
    239   1.2    itojun int
    240  1.65    dyoung gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    241  1.63  christos     struct rtentry *rt)
    242   1.2    itojun {
    243  1.76    dyoung 	struct gif_softc *sc = ifp->if_softc;
    244   1.2    itojun 	int error = 0;
    245   1.2    itojun 	static int called = 0;	/* XXX: MUTEX */
    246  1.39    itojun 	ALTQ_DECL(struct altq_pktattr pktattr;)
    247  1.33    itojun 	int s;
    248  1.33    itojun 
    249  1.33    itojun 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
    250   1.2    itojun 
    251   1.2    itojun 	/*
    252   1.2    itojun 	 * gif may cause infinite recursion calls when misconfigured.
    253   1.2    itojun 	 * We'll prevent this by introducing upper limit.
    254   1.2    itojun 	 * XXX: this mechanism may introduce another problem about
    255   1.2    itojun 	 *      mutual exclusion of the variable CALLED, especially if we
    256   1.2    itojun 	 *      use kernel thread.
    257   1.2    itojun 	 */
    258   1.9    itojun 	if (++called > max_gif_nesting) {
    259   1.2    itojun 		log(LOG_NOTICE,
    260   1.2    itojun 		    "gif_output: recursively called too many times(%d)\n",
    261   1.2    itojun 		    called);
    262   1.2    itojun 		m_freem(m);
    263   1.2    itojun 		error = EIO;	/* is there better errno? */
    264   1.2    itojun 		goto end;
    265   1.2    itojun 	}
    266   1.2    itojun 
    267   1.2    itojun 	m->m_flags &= ~(M_BCAST|M_MCAST);
    268   1.2    itojun 	if (!(ifp->if_flags & IFF_UP) ||
    269   1.2    itojun 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
    270   1.2    itojun 		m_freem(m);
    271   1.2    itojun 		error = ENETDOWN;
    272   1.2    itojun 		goto end;
    273   1.2    itojun 	}
    274   1.2    itojun 
    275   1.9    itojun 	/* XXX should we check if our outer source is legal? */
    276   1.2    itojun 
    277  1.33    itojun 	/* use DLT_NULL encapsulation here to pass inner af type */
    278  1.33    itojun 	M_PREPEND(m, sizeof(int), M_DONTWAIT);
    279  1.33    itojun 	if (!m) {
    280  1.33    itojun 		error = ENOBUFS;
    281  1.33    itojun 		goto end;
    282  1.33    itojun 	}
    283  1.33    itojun 	*mtod(m, int *) = dst->sa_family;
    284  1.33    itojun 
    285  1.79    dyoung 	/* Clear checksum-offload flags. */
    286  1.79    dyoung 	m->m_pkthdr.csum_flags = 0;
    287  1.79    dyoung 	m->m_pkthdr.csum_data = 0;
    288  1.79    dyoung 
    289  1.33    itojun 	s = splnet();
    290  1.33    itojun 	IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    291  1.33    itojun 	if (error) {
    292  1.33    itojun 		splx(s);
    293  1.31    itojun 		goto end;
    294   1.2    itojun 	}
    295  1.33    itojun 	splx(s);
    296  1.33    itojun 
    297  1.72        ad 	softint_schedule(sc->gif_si);
    298  1.33    itojun 	error = 0;
    299   1.2    itojun 
    300   1.2    itojun   end:
    301   1.2    itojun 	called = 0;		/* reset recursion counter */
    302  1.31    itojun 	if (error)
    303  1.31    itojun 		ifp->if_oerrors++;
    304   1.2    itojun 	return error;
    305   1.2    itojun }
    306   1.2    itojun 
    307  1.56   thorpej static void
    308  1.56   thorpej gifintr(void *arg)
    309  1.33    itojun {
    310  1.33    itojun 	struct gif_softc *sc;
    311  1.33    itojun 	struct ifnet *ifp;
    312  1.33    itojun 	struct mbuf *m;
    313  1.33    itojun 	int family;
    314  1.33    itojun 	int len;
    315  1.33    itojun 	int s;
    316  1.33    itojun 	int error;
    317  1.33    itojun 
    318  1.76    dyoung 	sc = arg;
    319  1.33    itojun 	ifp = &sc->gif_if;
    320  1.33    itojun 
    321  1.33    itojun 	/* output processing */
    322  1.33    itojun 	while (1) {
    323  1.33    itojun 		s = splnet();
    324  1.34    itojun 		IFQ_DEQUEUE(&sc->gif_if.if_snd, m);
    325  1.33    itojun 		splx(s);
    326  1.33    itojun 		if (m == NULL)
    327  1.33    itojun 			break;
    328  1.33    itojun 
    329  1.33    itojun 		/* grab and chop off inner af type */
    330  1.33    itojun 		if (sizeof(int) > m->m_len) {
    331  1.33    itojun 			m = m_pullup(m, sizeof(int));
    332  1.33    itojun 			if (!m) {
    333  1.33    itojun 				ifp->if_oerrors++;
    334  1.33    itojun 				continue;
    335  1.33    itojun 			}
    336  1.33    itojun 		}
    337  1.33    itojun 		family = *mtod(m, int *);
    338  1.78     joerg 		bpf_mtap(ifp, m);
    339  1.33    itojun 		m_adj(m, sizeof(int));
    340  1.33    itojun 
    341  1.33    itojun 		len = m->m_pkthdr.len;
    342  1.33    itojun 
    343  1.33    itojun 		/* dispatch to output logic based on outer AF */
    344  1.33    itojun 		switch (sc->gif_psrc->sa_family) {
    345  1.33    itojun #ifdef INET
    346  1.33    itojun 		case AF_INET:
    347  1.33    itojun 			error = in_gif_output(ifp, family, m);
    348  1.33    itojun 			break;
    349  1.33    itojun #endif
    350  1.33    itojun #ifdef INET6
    351  1.33    itojun 		case AF_INET6:
    352  1.33    itojun 			error = in6_gif_output(ifp, family, m);
    353  1.33    itojun 			break;
    354  1.33    itojun #endif
    355  1.33    itojun 		default:
    356  1.50     perry 			m_freem(m);
    357  1.33    itojun 			error = ENETDOWN;
    358  1.33    itojun 			break;
    359  1.33    itojun 		}
    360  1.33    itojun 
    361  1.33    itojun 		if (error)
    362  1.33    itojun 			ifp->if_oerrors++;
    363  1.33    itojun 		else {
    364  1.50     perry 			ifp->if_opackets++;
    365  1.33    itojun 			ifp->if_obytes += len;
    366  1.33    itojun 		}
    367  1.33    itojun 	}
    368  1.33    itojun }
    369  1.33    itojun 
    370  1.33    itojun void
    371  1.56   thorpej gif_input(struct mbuf *m, int af, struct ifnet *ifp)
    372   1.2    itojun {
    373  1.83     rmind 	pktqueue_t *pktq;
    374  1.83     rmind 	size_t pktlen;
    375  1.83     rmind 	int s;
    376   1.2    itojun 
    377  1.33    itojun 	if (ifp == NULL) {
    378   1.2    itojun 		/* just in case */
    379   1.2    itojun 		m_freem(m);
    380   1.2    itojun 		return;
    381   1.2    itojun 	}
    382   1.2    itojun 
    383  1.33    itojun 	m->m_pkthdr.rcvif = ifp;
    384  1.83     rmind 	pktlen = m->m_pkthdr.len;
    385  1.50     perry 
    386  1.78     joerg 	bpf_mtap_af(ifp, af, m);
    387   1.2    itojun 
    388   1.2    itojun 	/*
    389   1.2    itojun 	 * Put the packet to the network layer input queue according to the
    390  1.83     rmind 	 * specified address family.  Note: we avoid direct call to the
    391  1.83     rmind 	 * input function of the network layer in order to avoid recursion.
    392  1.83     rmind 	 * This may be revisited in the future.
    393   1.2    itojun 	 */
    394   1.2    itojun 	switch (af) {
    395   1.2    itojun #ifdef INET
    396   1.2    itojun 	case AF_INET:
    397  1.83     rmind 		pktq = ip_pktq;
    398   1.2    itojun 		break;
    399   1.2    itojun #endif
    400   1.2    itojun #ifdef INET6
    401   1.2    itojun 	case AF_INET6:
    402  1.83     rmind 		pktq = ip6_pktq;
    403   1.2    itojun 		break;
    404   1.2    itojun #endif
    405   1.2    itojun 	default:
    406   1.2    itojun 		m_freem(m);
    407   1.2    itojun 		return;
    408   1.2    itojun 	}
    409   1.2    itojun 
    410  1.27   thorpej 	s = splnet();
    411  1.83     rmind 	if (__predict_true(pktq_enqueue(pktq, m, 0))) {
    412  1.83     rmind 		ifp->if_ibytes += pktlen;
    413  1.83     rmind 		ifp->if_ipackets++;
    414  1.83     rmind 	} else {
    415   1.2    itojun 		m_freem(m);
    416   1.2    itojun 	}
    417   1.2    itojun 	splx(s);
    418   1.2    itojun }
    419   1.2    itojun 
    420   1.9    itojun /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
    421   1.2    itojun int
    422  1.67  christos gif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    423   1.2    itojun {
    424  1.76    dyoung 	struct gif_softc *sc  = ifp->if_softc;
    425   1.2    itojun 	struct ifreq     *ifr = (struct ifreq*)data;
    426   1.2    itojun 	int error = 0, size;
    427   1.9    itojun 	struct sockaddr *dst, *src;
    428  1.31    itojun 
    429   1.2    itojun 	switch (cmd) {
    430  1.76    dyoung 	case SIOCINITIFADDR:
    431  1.32    itojun 		ifp->if_flags |= IFF_UP;
    432   1.2    itojun 		break;
    433  1.50     perry 
    434   1.2    itojun 	case SIOCADDMULTI:
    435   1.2    itojun 	case SIOCDELMULTI:
    436   1.2    itojun 		switch (ifr->ifr_addr.sa_family) {
    437   1.2    itojun #ifdef INET
    438   1.2    itojun 		case AF_INET:	/* IP supports Multicast */
    439   1.2    itojun 			break;
    440   1.2    itojun #endif /* INET */
    441   1.2    itojun #ifdef INET6
    442   1.2    itojun 		case AF_INET6:	/* IP6 supports Multicast */
    443   1.2    itojun 			break;
    444   1.2    itojun #endif /* INET6 */
    445   1.2    itojun 		default:  /* Other protocols doesn't support Multicast */
    446   1.2    itojun 			error = EAFNOSUPPORT;
    447   1.2    itojun 			break;
    448   1.2    itojun 		}
    449   1.2    itojun 		break;
    450   1.2    itojun 
    451   1.2    itojun 	case SIOCSIFMTU:
    452  1.73    dyoung 		if (ifr->ifr_mtu < GIF_MTU_MIN || ifr->ifr_mtu > GIF_MTU_MAX)
    453  1.73    dyoung 			return EINVAL;
    454  1.73    dyoung 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
    455  1.73    dyoung 			error = 0;
    456   1.2    itojun 		break;
    457   1.2    itojun 
    458  1.31    itojun #ifdef INET
    459   1.2    itojun 	case SIOCSIFPHYADDR:
    460  1.31    itojun #endif
    461   1.2    itojun #ifdef INET6
    462   1.2    itojun 	case SIOCSIFPHYADDR_IN6:
    463   1.2    itojun #endif /* INET6 */
    464  1.25    itojun 	case SIOCSLIFPHYADDR:
    465  1.11    itojun 		switch (cmd) {
    466  1.15    itojun #ifdef INET
    467  1.11    itojun 		case SIOCSIFPHYADDR:
    468  1.11    itojun 			src = (struct sockaddr *)
    469  1.11    itojun 				&(((struct in_aliasreq *)data)->ifra_addr);
    470  1.11    itojun 			dst = (struct sockaddr *)
    471  1.11    itojun 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
    472  1.11    itojun 			break;
    473  1.15    itojun #endif
    474  1.11    itojun #ifdef INET6
    475  1.11    itojun 		case SIOCSIFPHYADDR_IN6:
    476  1.11    itojun 			src = (struct sockaddr *)
    477  1.11    itojun 				&(((struct in6_aliasreq *)data)->ifra_addr);
    478  1.11    itojun 			dst = (struct sockaddr *)
    479  1.11    itojun 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
    480  1.25    itojun 			break;
    481  1.25    itojun #endif
    482  1.25    itojun 		case SIOCSLIFPHYADDR:
    483  1.25    itojun 			src = (struct sockaddr *)
    484  1.25    itojun 				&(((struct if_laddrreq *)data)->addr);
    485  1.25    itojun 			dst = (struct sockaddr *)
    486  1.25    itojun 				&(((struct if_laddrreq *)data)->dstaddr);
    487  1.31    itojun 			break;
    488  1.31    itojun 		default:
    489  1.31    itojun 			return EINVAL;
    490  1.25    itojun 		}
    491  1.25    itojun 
    492  1.25    itojun 		/* sa_family must be equal */
    493  1.25    itojun 		if (src->sa_family != dst->sa_family)
    494  1.25    itojun 			return EINVAL;
    495  1.25    itojun 
    496  1.25    itojun 		/* validate sa_len */
    497  1.25    itojun 		switch (src->sa_family) {
    498  1.25    itojun #ifdef INET
    499  1.25    itojun 		case AF_INET:
    500  1.25    itojun 			if (src->sa_len != sizeof(struct sockaddr_in))
    501  1.16    itojun 				return EINVAL;
    502  1.11    itojun 			break;
    503  1.11    itojun #endif
    504  1.25    itojun #ifdef INET6
    505  1.25    itojun 		case AF_INET6:
    506  1.25    itojun 			if (src->sa_len != sizeof(struct sockaddr_in6))
    507  1.25    itojun 				return EINVAL;
    508  1.25    itojun 			break;
    509  1.25    itojun #endif
    510  1.25    itojun 		default:
    511  1.25    itojun 			return EAFNOSUPPORT;
    512  1.25    itojun 		}
    513  1.25    itojun 		switch (dst->sa_family) {
    514  1.25    itojun #ifdef INET
    515  1.25    itojun 		case AF_INET:
    516  1.25    itojun 			if (dst->sa_len != sizeof(struct sockaddr_in))
    517  1.25    itojun 				return EINVAL;
    518  1.25    itojun 			break;
    519  1.25    itojun #endif
    520  1.25    itojun #ifdef INET6
    521  1.25    itojun 		case AF_INET6:
    522  1.25    itojun 			if (dst->sa_len != sizeof(struct sockaddr_in6))
    523  1.25    itojun 				return EINVAL;
    524  1.25    itojun 			break;
    525  1.25    itojun #endif
    526  1.25    itojun 		default:
    527  1.25    itojun 			return EAFNOSUPPORT;
    528  1.25    itojun 		}
    529  1.25    itojun 
    530  1.25    itojun 		/* check sa_family looks sane for the cmd */
    531  1.25    itojun 		switch (cmd) {
    532  1.25    itojun 		case SIOCSIFPHYADDR:
    533  1.25    itojun 			if (src->sa_family == AF_INET)
    534  1.25    itojun 				break;
    535  1.25    itojun 			return EAFNOSUPPORT;
    536  1.25    itojun #ifdef INET6
    537  1.25    itojun 		case SIOCSIFPHYADDR_IN6:
    538  1.25    itojun 			if (src->sa_family == AF_INET6)
    539  1.25    itojun 				break;
    540  1.25    itojun 			return EAFNOSUPPORT;
    541  1.25    itojun #endif /* INET6 */
    542  1.25    itojun 		case SIOCSLIFPHYADDR:
    543  1.25    itojun 			/* checks done in the above */
    544  1.25    itojun 			break;
    545  1.11    itojun 		}
    546  1.11    itojun 
    547  1.31    itojun 		error = gif_set_tunnel(&sc->gif_if, src, dst);
    548   1.9    itojun 		break;
    549   1.9    itojun 
    550   1.9    itojun #ifdef SIOCDIFPHYADDR
    551   1.9    itojun 	case SIOCDIFPHYADDR:
    552  1.31    itojun 		gif_delete_tunnel(&sc->gif_if);
    553   1.2    itojun 		break;
    554   1.9    itojun #endif
    555  1.50     perry 
    556   1.2    itojun 	case SIOCGIFPSRCADDR:
    557   1.2    itojun #ifdef INET6
    558   1.2    itojun 	case SIOCGIFPSRCADDR_IN6:
    559   1.2    itojun #endif /* INET6 */
    560   1.2    itojun 		if (sc->gif_psrc == NULL) {
    561   1.2    itojun 			error = EADDRNOTAVAIL;
    562   1.2    itojun 			goto bad;
    563   1.2    itojun 		}
    564   1.2    itojun 		src = sc->gif_psrc;
    565  1.16    itojun 		switch (cmd) {
    566   1.2    itojun #ifdef INET
    567  1.16    itojun 		case SIOCGIFPSRCADDR:
    568   1.2    itojun 			dst = &ifr->ifr_addr;
    569  1.16    itojun 			size = sizeof(ifr->ifr_addr);
    570   1.2    itojun 			break;
    571   1.2    itojun #endif /* INET */
    572   1.2    itojun #ifdef INET6
    573  1.16    itojun 		case SIOCGIFPSRCADDR_IN6:
    574   1.2    itojun 			dst = (struct sockaddr *)
    575   1.2    itojun 				&(((struct in6_ifreq *)data)->ifr_addr);
    576  1.16    itojun 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    577   1.2    itojun 			break;
    578   1.2    itojun #endif /* INET6 */
    579   1.2    itojun 		default:
    580   1.2    itojun 			error = EADDRNOTAVAIL;
    581   1.2    itojun 			goto bad;
    582   1.2    itojun 		}
    583  1.16    itojun 		if (src->sa_len > size)
    584  1.16    itojun 			return EINVAL;
    585  1.68    dyoung 		memcpy(dst, src, src->sa_len);
    586   1.2    itojun 		break;
    587  1.50     perry 
    588   1.2    itojun 	case SIOCGIFPDSTADDR:
    589   1.2    itojun #ifdef INET6
    590   1.2    itojun 	case SIOCGIFPDSTADDR_IN6:
    591   1.2    itojun #endif /* INET6 */
    592   1.2    itojun 		if (sc->gif_pdst == NULL) {
    593   1.2    itojun 			error = EADDRNOTAVAIL;
    594   1.2    itojun 			goto bad;
    595   1.2    itojun 		}
    596   1.2    itojun 		src = sc->gif_pdst;
    597  1.16    itojun 		switch (cmd) {
    598   1.2    itojun #ifdef INET
    599  1.16    itojun 		case SIOCGIFPDSTADDR:
    600   1.2    itojun 			dst = &ifr->ifr_addr;
    601  1.16    itojun 			size = sizeof(ifr->ifr_addr);
    602   1.2    itojun 			break;
    603   1.2    itojun #endif /* INET */
    604   1.2    itojun #ifdef INET6
    605  1.16    itojun 		case SIOCGIFPDSTADDR_IN6:
    606   1.2    itojun 			dst = (struct sockaddr *)
    607   1.2    itojun 				&(((struct in6_ifreq *)data)->ifr_addr);
    608  1.16    itojun 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    609   1.2    itojun 			break;
    610   1.2    itojun #endif /* INET6 */
    611   1.2    itojun 		default:
    612   1.2    itojun 			error = EADDRNOTAVAIL;
    613   1.2    itojun 			goto bad;
    614   1.2    itojun 		}
    615  1.25    itojun 		if (src->sa_len > size)
    616  1.25    itojun 			return EINVAL;
    617  1.68    dyoung 		memcpy(dst, src, src->sa_len);
    618  1.25    itojun 		break;
    619  1.25    itojun 
    620  1.25    itojun 	case SIOCGLIFPHYADDR:
    621  1.25    itojun 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
    622  1.25    itojun 			error = EADDRNOTAVAIL;
    623  1.25    itojun 			goto bad;
    624  1.25    itojun 		}
    625  1.25    itojun 
    626  1.25    itojun 		/* copy src */
    627  1.25    itojun 		src = sc->gif_psrc;
    628  1.25    itojun 		dst = (struct sockaddr *)
    629  1.25    itojun 			&(((struct if_laddrreq *)data)->addr);
    630  1.25    itojun 		size = sizeof(((struct if_laddrreq *)data)->addr);
    631  1.25    itojun 		if (src->sa_len > size)
    632  1.25    itojun 			return EINVAL;
    633  1.68    dyoung 		memcpy(dst, src, src->sa_len);
    634  1.25    itojun 
    635  1.25    itojun 		/* copy dst */
    636  1.25    itojun 		src = sc->gif_pdst;
    637  1.25    itojun 		dst = (struct sockaddr *)
    638  1.25    itojun 			&(((struct if_laddrreq *)data)->dstaddr);
    639  1.25    itojun 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
    640  1.16    itojun 		if (src->sa_len > size)
    641  1.16    itojun 			return EINVAL;
    642  1.68    dyoung 		memcpy(dst, src, src->sa_len);
    643   1.2    itojun 		break;
    644   1.2    itojun 
    645   1.2    itojun 	default:
    646  1.76    dyoung 		return ifioctl_common(ifp, cmd, data);
    647   1.2    itojun 	}
    648   1.2    itojun  bad:
    649   1.2    itojun 	return error;
    650  1.12   thorpej }
    651  1.12   thorpej 
    652  1.31    itojun int
    653  1.56   thorpej gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
    654  1.31    itojun {
    655  1.76    dyoung 	struct gif_softc *sc = ifp->if_softc;
    656  1.31    itojun 	struct gif_softc *sc2;
    657  1.71    dyoung 	struct sockaddr *osrc, *odst;
    658  1.31    itojun 	int s;
    659  1.31    itojun 	int error;
    660  1.31    itojun 
    661  1.31    itojun 	s = splsoftnet();
    662  1.31    itojun 
    663  1.68    dyoung 	LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
    664  1.31    itojun 		if (sc2 == sc)
    665  1.31    itojun 			continue;
    666  1.31    itojun 		if (!sc2->gif_pdst || !sc2->gif_psrc)
    667  1.31    itojun 			continue;
    668  1.31    itojun 		/* can't configure same pair of address onto two gifs */
    669  1.71    dyoung 		if (sockaddr_cmp(sc2->gif_pdst, dst) == 0 &&
    670  1.71    dyoung 		    sockaddr_cmp(sc2->gif_psrc, src) == 0) {
    671  1.31    itojun 			error = EADDRNOTAVAIL;
    672  1.31    itojun 			goto bad;
    673  1.31    itojun 		}
    674  1.31    itojun 
    675  1.31    itojun 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
    676  1.31    itojun 	}
    677  1.31    itojun 
    678  1.58   msaitoh 	if (sc->gif_si) {
    679  1.72        ad 		softint_disestablish(sc->gif_si);
    680  1.58   msaitoh 		sc->gif_si = NULL;
    681  1.58   msaitoh 	}
    682  1.58   msaitoh 
    683  1.31    itojun 	/* XXX we can detach from both, but be polite just in case */
    684  1.31    itojun 	if (sc->gif_psrc)
    685  1.31    itojun 		switch (sc->gif_psrc->sa_family) {
    686  1.31    itojun #ifdef INET
    687  1.31    itojun 		case AF_INET:
    688  1.31    itojun 			(void)in_gif_detach(sc);
    689  1.31    itojun 			break;
    690  1.31    itojun #endif
    691  1.31    itojun #ifdef INET6
    692  1.31    itojun 		case AF_INET6:
    693  1.31    itojun 			(void)in6_gif_detach(sc);
    694  1.31    itojun 			break;
    695  1.31    itojun #endif
    696  1.31    itojun 		}
    697  1.31    itojun 
    698  1.72        ad 	sc->gif_si = softint_establish(SOFTINT_NET, gifintr, sc);
    699  1.33    itojun 	if (sc->gif_si == NULL) {
    700  1.33    itojun 		error = ENOMEM;
    701  1.33    itojun 		goto bad;
    702  1.33    itojun 	}
    703  1.33    itojun 
    704  1.31    itojun 	osrc = sc->gif_psrc;
    705  1.71    dyoung 	sc->gif_psrc = sockaddr_dup(src, M_WAITOK);
    706  1.31    itojun 
    707  1.31    itojun 	odst = sc->gif_pdst;
    708  1.71    dyoung 	sc->gif_pdst = sockaddr_dup(dst, M_WAITOK);
    709  1.31    itojun 
    710  1.31    itojun 	switch (sc->gif_psrc->sa_family) {
    711  1.31    itojun #ifdef INET
    712  1.31    itojun 	case AF_INET:
    713  1.31    itojun 		error = in_gif_attach(sc);
    714  1.31    itojun 		break;
    715  1.31    itojun #endif
    716  1.31    itojun #ifdef INET6
    717  1.31    itojun 	case AF_INET6:
    718  1.31    itojun 		error = in6_gif_attach(sc);
    719  1.31    itojun 		break;
    720  1.31    itojun #endif
    721  1.43  christos 	default:
    722  1.44   mycroft 		error = EINVAL;
    723  1.43  christos 		break;
    724  1.31    itojun 	}
    725  1.31    itojun 	if (error) {
    726  1.31    itojun 		/* rollback */
    727  1.71    dyoung 		sockaddr_free(sc->gif_psrc);
    728  1.71    dyoung 		sockaddr_free(sc->gif_pdst);
    729  1.31    itojun 		sc->gif_psrc = osrc;
    730  1.31    itojun 		sc->gif_pdst = odst;
    731  1.31    itojun 		goto bad;
    732  1.31    itojun 	}
    733  1.31    itojun 
    734  1.31    itojun 	if (osrc)
    735  1.71    dyoung 		sockaddr_free(osrc);
    736  1.31    itojun 	if (odst)
    737  1.71    dyoung 		sockaddr_free(odst);
    738  1.31    itojun 
    739  1.33    itojun 	if (sc->gif_psrc && sc->gif_pdst)
    740  1.33    itojun 		ifp->if_flags |= IFF_RUNNING;
    741  1.33    itojun 	else
    742  1.33    itojun 		ifp->if_flags &= ~IFF_RUNNING;
    743  1.33    itojun 	splx(s);
    744  1.33    itojun 
    745  1.33    itojun 	return 0;
    746  1.31    itojun 
    747  1.31    itojun  bad:
    748  1.35    itojun 	if (sc->gif_si) {
    749  1.72        ad 		softint_disestablish(sc->gif_si);
    750  1.35    itojun 		sc->gif_si = NULL;
    751  1.35    itojun 	}
    752  1.31    itojun 	if (sc->gif_psrc && sc->gif_pdst)
    753  1.31    itojun 		ifp->if_flags |= IFF_RUNNING;
    754  1.31    itojun 	else
    755  1.31    itojun 		ifp->if_flags &= ~IFF_RUNNING;
    756  1.31    itojun 	splx(s);
    757  1.31    itojun 
    758  1.31    itojun 	return error;
    759  1.31    itojun }
    760  1.31    itojun 
    761  1.12   thorpej void
    762  1.56   thorpej gif_delete_tunnel(struct ifnet *ifp)
    763  1.12   thorpej {
    764  1.76    dyoung 	struct gif_softc *sc = ifp->if_softc;
    765  1.12   thorpej 	int s;
    766  1.12   thorpej 
    767  1.12   thorpej 	s = splsoftnet();
    768  1.12   thorpej 
    769  1.35    itojun 	if (sc->gif_si) {
    770  1.72        ad 		softint_disestablish(sc->gif_si);
    771  1.35    itojun 		sc->gif_si = NULL;
    772  1.35    itojun 	}
    773  1.12   thorpej 	if (sc->gif_psrc) {
    774  1.71    dyoung 		sockaddr_free(sc->gif_psrc);
    775  1.12   thorpej 		sc->gif_psrc = NULL;
    776  1.12   thorpej 	}
    777  1.12   thorpej 	if (sc->gif_pdst) {
    778  1.71    dyoung 		sockaddr_free(sc->gif_pdst);
    779  1.12   thorpej 		sc->gif_pdst = NULL;
    780  1.12   thorpej 	}
    781  1.31    itojun 	/* it is safe to detach from both */
    782  1.31    itojun #ifdef INET
    783  1.31    itojun 	(void)in_gif_detach(sc);
    784  1.31    itojun #endif
    785  1.31    itojun #ifdef INET6
    786  1.31    itojun 	(void)in6_gif_detach(sc);
    787  1.31    itojun #endif
    788  1.12   thorpej 
    789  1.31    itojun 	if (sc->gif_psrc && sc->gif_pdst)
    790  1.31    itojun 		ifp->if_flags |= IFF_RUNNING;
    791  1.31    itojun 	else
    792  1.31    itojun 		ifp->if_flags &= ~IFF_RUNNING;
    793  1.12   thorpej 	splx(s);
    794   1.2    itojun }
    795