Home | History | Annotate | Line # | Download | only in net
if_gif.c revision 1.16
      1  1.16   itojun /*	$NetBSD: if_gif.c,v 1.16 2000/10/07 04:18:04 itojun Exp $	*/
      2  1.16   itojun /*	$KAME: if_gif.c,v 1.34 2000/10/07 03:58:53 itojun 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.2   itojun 
     33   1.2   itojun #include "opt_inet.h"
     34   1.2   itojun 
     35   1.2   itojun #include <sys/param.h>
     36   1.2   itojun #include <sys/systm.h>
     37   1.2   itojun #include <sys/kernel.h>
     38   1.2   itojun #include <sys/mbuf.h>
     39   1.2   itojun #include <sys/socket.h>
     40   1.2   itojun #include <sys/sockio.h>
     41   1.2   itojun #include <sys/errno.h>
     42   1.2   itojun #include <sys/ioctl.h>
     43   1.2   itojun #include <sys/time.h>
     44   1.2   itojun #include <sys/syslog.h>
     45   1.9   itojun #include <sys/protosw.h>
     46   1.2   itojun #include <machine/cpu.h>
     47   1.2   itojun 
     48   1.2   itojun #include <net/if.h>
     49   1.2   itojun #include <net/if_types.h>
     50   1.2   itojun #include <net/netisr.h>
     51   1.2   itojun #include <net/route.h>
     52   1.2   itojun #include <net/bpf.h>
     53   1.2   itojun 
     54   1.2   itojun #include <netinet/in.h>
     55   1.2   itojun #include <netinet/in_systm.h>
     56  1.15   itojun #include <netinet/ip.h>
     57  1.15   itojun #ifdef	INET
     58   1.2   itojun #include <netinet/in_var.h>
     59   1.2   itojun #include <netinet/in_gif.h>
     60   1.2   itojun #endif	/* INET */
     61   1.2   itojun 
     62   1.2   itojun #ifdef INET6
     63   1.2   itojun #ifndef INET
     64   1.2   itojun #include <netinet/in.h>
     65   1.2   itojun #endif
     66   1.2   itojun #include <netinet6/in6_var.h>
     67   1.2   itojun #include <netinet/ip6.h>
     68   1.2   itojun #include <netinet6/ip6_var.h>
     69   1.2   itojun #include <netinet6/in6_gif.h>
     70   1.9   itojun #include <netinet6/ip6protosw.h>
     71   1.2   itojun #endif /* INET6 */
     72   1.2   itojun 
     73   1.9   itojun #include <netinet/ip_encap.h>
     74   1.2   itojun #include <net/if_gif.h>
     75   1.2   itojun 
     76   1.2   itojun #include "gif.h"
     77   1.2   itojun #include "bpfilter.h"
     78   1.2   itojun 
     79   1.4   itojun #include <net/net_osdep.h>
     80   1.4   itojun 
     81   1.2   itojun #if NGIF > 0
     82   1.2   itojun 
     83   1.4   itojun void gifattach __P((int));
     84   1.9   itojun static int gif_encapcheck __P((const struct mbuf *, int, int, void *));
     85   1.9   itojun #ifdef INET
     86   1.9   itojun extern struct protosw in_gif_protosw;
     87   1.9   itojun #endif
     88   1.9   itojun #ifdef INET6
     89   1.9   itojun extern struct ip6protosw in6_gif_protosw;
     90   1.9   itojun #endif
     91   1.2   itojun 
     92   1.2   itojun /*
     93   1.2   itojun  * gif global variable definitions
     94   1.2   itojun  */
     95  1.12  thorpej LIST_HEAD(, gif_softc) gif_softc_list;
     96  1.12  thorpej 
     97  1.12  thorpej int	gif_clone_create __P((struct if_clone *, int));
     98  1.12  thorpej void	gif_clone_destroy __P((struct ifnet *));
     99  1.12  thorpej 
    100  1.12  thorpej struct if_clone gif_cloner =
    101  1.12  thorpej     IF_CLONE_INITIALIZER("gif", gif_clone_create, gif_clone_destroy);
    102  1.12  thorpej 
    103  1.12  thorpej void	gif_delete_tunnel __P((struct gif_softc *));
    104   1.9   itojun 
    105   1.9   itojun #ifndef MAX_GIF_NEST
    106   1.9   itojun /*
    107   1.9   itojun  * This macro controls the upper limitation on nesting of gif tunnels.
    108   1.9   itojun  * Since, setting a large value to this macro with a careless configuration
    109   1.9   itojun  * may introduce system crash, we don't allow any nestings by default.
    110   1.9   itojun  * If you need to configure nested gif tunnels, you can define this macro
    111   1.9   itojun  * in your kernel configuration file. However, if you do so, please be
    112   1.9   itojun  * careful to configure the tunnels so that it won't make a loop.
    113   1.9   itojun  */
    114   1.9   itojun #define MAX_GIF_NEST 1
    115   1.9   itojun #endif
    116   1.9   itojun static int max_gif_nesting = MAX_GIF_NEST;
    117   1.2   itojun 
    118  1.12  thorpej /* ARGSUSED */
    119   1.2   itojun void
    120  1.12  thorpej gifattach(count)
    121  1.12  thorpej 	int count;
    122  1.12  thorpej {
    123  1.12  thorpej 
    124  1.12  thorpej 	LIST_INIT(&gif_softc_list);
    125  1.12  thorpej 	if_clone_attach(&gif_cloner);
    126  1.12  thorpej }
    127  1.12  thorpej 
    128  1.12  thorpej int
    129  1.12  thorpej gif_clone_create(ifc, unit)
    130  1.12  thorpej 	struct if_clone *ifc;
    131  1.12  thorpej 	int unit;
    132   1.2   itojun {
    133  1.12  thorpej 	struct gif_softc *sc;
    134  1.12  thorpej 
    135  1.12  thorpej 	sc = malloc(sizeof(struct gif_softc), M_DEVBUF, M_WAIT);
    136  1.12  thorpej 	bzero(sc, sizeof(struct gif_softc));
    137   1.2   itojun 
    138  1.12  thorpej 	sprintf(sc->gif_if.if_xname, "%s%d", ifc->ifc_name, unit);
    139   1.9   itojun 
    140  1.12  thorpej 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
    141   1.9   itojun #ifdef INET
    142  1.12  thorpej 	sc->encap_cookie4 = encap_attach_func(AF_INET, -1,
    143  1.12  thorpej 	    gif_encapcheck, &in_gif_protosw, sc);
    144  1.12  thorpej 	if (sc->encap_cookie4 == NULL) {
    145  1.12  thorpej 		printf("%s: unable to attach encap4\n", if_name(&sc->gif_if));
    146  1.13  thorpej 		free(sc, M_DEVBUF);
    147  1.12  thorpej 		return (EIO);	/* XXX */
    148  1.12  thorpej 	}
    149   1.9   itojun #endif
    150   1.9   itojun #ifdef INET6
    151  1.12  thorpej 	sc->encap_cookie6 = encap_attach_func(AF_INET6, -1,
    152  1.12  thorpej 	    gif_encapcheck, (struct protosw *)&in6_gif_protosw, sc);
    153  1.12  thorpej 	if (sc->encap_cookie6 == NULL) {
    154  1.12  thorpej 		if (sc->encap_cookie4) {
    155  1.12  thorpej 			encap_detach(sc->encap_cookie4);
    156  1.12  thorpej 			sc->encap_cookie4 = NULL;
    157   1.9   itojun 		}
    158  1.12  thorpej 		printf("%s: unable to attach encap6\n", if_name(&sc->gif_if));
    159  1.13  thorpej 		free(sc, M_DEVBUF);
    160  1.12  thorpej 		return (EIO);	/* XXX */
    161  1.12  thorpej 	}
    162   1.9   itojun #endif
    163   1.9   itojun 
    164  1.12  thorpej 	sc->gif_if.if_mtu    = GIF_MTU;
    165  1.12  thorpej 	sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
    166  1.12  thorpej 	sc->gif_if.if_ioctl  = gif_ioctl;
    167  1.12  thorpej 	sc->gif_if.if_output = gif_output;
    168  1.12  thorpej 	sc->gif_if.if_type   = IFT_GIF;
    169  1.12  thorpej 	if_attach(&sc->gif_if);
    170   1.2   itojun #if NBPFILTER > 0
    171   1.9   itojun #ifdef HAVE_OLD_BPF
    172  1.12  thorpej 	bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
    173   1.9   itojun #else
    174  1.12  thorpej 	bpfattach(&sc->gif_if.if_bpf, &sc->gif_if, DLT_NULL, sizeof(u_int));
    175   1.2   itojun #endif
    176   1.9   itojun #endif
    177  1.12  thorpej 	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
    178  1.12  thorpej 	return (0);
    179  1.12  thorpej }
    180  1.12  thorpej 
    181  1.12  thorpej void
    182  1.12  thorpej gif_clone_destroy(ifp)
    183  1.12  thorpej 	struct ifnet *ifp;
    184  1.12  thorpej {
    185  1.12  thorpej 	struct gif_softc *sc = (void *) ifp;
    186  1.12  thorpej 
    187  1.12  thorpej 	gif_delete_tunnel(sc);
    188  1.12  thorpej 	LIST_REMOVE(sc, gif_list);
    189  1.12  thorpej #ifdef INET6
    190  1.12  thorpej 	encap_detach(sc->encap_cookie6);
    191  1.12  thorpej #endif
    192  1.12  thorpej #ifdef INET
    193  1.12  thorpej 	encap_detach(sc->encap_cookie4);
    194  1.12  thorpej #endif
    195  1.12  thorpej 
    196  1.12  thorpej #if NBPFILTER > 0
    197  1.12  thorpej 	bpfdetach(ifp);
    198  1.12  thorpej #endif
    199  1.12  thorpej 	if_detach(ifp);
    200  1.12  thorpej 
    201  1.12  thorpej 	free(sc, M_DEVBUF);
    202   1.9   itojun }
    203   1.9   itojun 
    204   1.9   itojun static int
    205   1.9   itojun gif_encapcheck(m, off, proto, arg)
    206   1.9   itojun 	const struct mbuf *m;
    207   1.9   itojun 	int off;
    208   1.9   itojun 	int proto;
    209   1.9   itojun 	void *arg;
    210   1.9   itojun {
    211   1.9   itojun 	struct ip ip;
    212   1.9   itojun 	struct gif_softc *sc;
    213   1.9   itojun 
    214   1.9   itojun 	sc = (struct gif_softc *)arg;
    215   1.9   itojun 	if (sc == NULL)
    216   1.9   itojun 		return 0;
    217   1.9   itojun 
    218   1.9   itojun 	if ((sc->gif_if.if_flags & IFF_UP) == 0)
    219   1.9   itojun 		return 0;
    220   1.9   itojun 
    221   1.9   itojun 	/* no physical address */
    222   1.9   itojun 	if (!sc->gif_psrc || !sc->gif_pdst)
    223   1.9   itojun 		return 0;
    224   1.9   itojun 
    225   1.9   itojun 	switch (proto) {
    226   1.9   itojun #ifdef INET
    227   1.9   itojun 	case IPPROTO_IPV4:
    228   1.9   itojun 		break;
    229   1.9   itojun #endif
    230   1.9   itojun #ifdef INET6
    231   1.9   itojun 	case IPPROTO_IPV6:
    232   1.9   itojun 		break;
    233   1.9   itojun #endif
    234   1.9   itojun 	default:
    235   1.9   itojun 		return 0;
    236   1.9   itojun 	}
    237   1.9   itojun 
    238   1.9   itojun 	/* LINTED const cast */
    239   1.9   itojun 	m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
    240   1.9   itojun 
    241   1.9   itojun 	switch (ip.ip_v) {
    242   1.9   itojun #ifdef INET
    243   1.9   itojun 	case 4:
    244   1.9   itojun 		if (sc->gif_psrc->sa_family != AF_INET ||
    245   1.9   itojun 		    sc->gif_pdst->sa_family != AF_INET)
    246   1.9   itojun 			return 0;
    247   1.9   itojun 		return gif_encapcheck4(m, off, proto, arg);
    248   1.9   itojun #endif
    249   1.9   itojun #ifdef INET6
    250   1.9   itojun 	case 6:
    251   1.9   itojun 		if (sc->gif_psrc->sa_family != AF_INET6 ||
    252   1.9   itojun 		    sc->gif_pdst->sa_family != AF_INET6)
    253   1.9   itojun 			return 0;
    254   1.9   itojun 		return gif_encapcheck6(m, off, proto, arg);
    255   1.9   itojun #endif
    256   1.9   itojun 	default:
    257   1.9   itojun 		return 0;
    258   1.2   itojun 	}
    259   1.2   itojun }
    260   1.2   itojun 
    261   1.2   itojun int
    262   1.2   itojun gif_output(ifp, m, dst, rt)
    263   1.2   itojun 	struct ifnet *ifp;
    264   1.2   itojun 	struct mbuf *m;
    265   1.2   itojun 	struct sockaddr *dst;
    266   1.2   itojun 	struct rtentry *rt;	/* added in net2 */
    267   1.2   itojun {
    268   1.9   itojun 	register struct gif_softc *sc = (struct gif_softc*)ifp;
    269   1.2   itojun 	int error = 0;
    270   1.2   itojun 	static int called = 0;	/* XXX: MUTEX */
    271   1.2   itojun 
    272   1.2   itojun 	/*
    273   1.2   itojun 	 * gif may cause infinite recursion calls when misconfigured.
    274   1.2   itojun 	 * We'll prevent this by introducing upper limit.
    275   1.2   itojun 	 * XXX: this mechanism may introduce another problem about
    276   1.2   itojun 	 *      mutual exclusion of the variable CALLED, especially if we
    277   1.2   itojun 	 *      use kernel thread.
    278   1.2   itojun 	 */
    279   1.9   itojun 	if (++called > max_gif_nesting) {
    280   1.2   itojun 		log(LOG_NOTICE,
    281   1.2   itojun 		    "gif_output: recursively called too many times(%d)\n",
    282   1.2   itojun 		    called);
    283   1.2   itojun 		m_freem(m);
    284   1.2   itojun 		error = EIO;	/* is there better errno? */
    285   1.2   itojun 		goto end;
    286   1.2   itojun 	}
    287   1.2   itojun 
    288   1.2   itojun 	ifp->if_lastchange = time;
    289   1.2   itojun 	m->m_flags &= ~(M_BCAST|M_MCAST);
    290   1.2   itojun 	if (!(ifp->if_flags & IFF_UP) ||
    291   1.2   itojun 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
    292   1.2   itojun 		m_freem(m);
    293   1.2   itojun 		error = ENETDOWN;
    294   1.2   itojun 		goto end;
    295   1.2   itojun 	}
    296   1.2   itojun 
    297   1.2   itojun #if NBPFILTER > 0
    298   1.2   itojun 	if (ifp->if_bpf) {
    299   1.2   itojun 		/*
    300   1.2   itojun 		 * We need to prepend the address family as
    301   1.2   itojun 		 * a four byte field.  Cons up a dummy header
    302   1.2   itojun 		 * to pacify bpf.  This is safe because bpf
    303   1.2   itojun 		 * will only read from the mbuf (i.e., it won't
    304   1.2   itojun 		 * try to free it or keep a pointer a to it).
    305   1.2   itojun 		 */
    306   1.2   itojun 		struct mbuf m0;
    307   1.2   itojun 		u_int af = dst->sa_family;
    308   1.2   itojun 
    309   1.2   itojun 		m0.m_next = m;
    310   1.2   itojun 		m0.m_len = 4;
    311   1.2   itojun 		m0.m_data = (char *)&af;
    312   1.2   itojun 
    313   1.9   itojun #ifdef HAVE_OLD_BPF
    314   1.9   itojun 		bpf_mtap(ifp, &m0);
    315   1.9   itojun #else
    316   1.2   itojun 		bpf_mtap(ifp->if_bpf, &m0);
    317   1.9   itojun #endif
    318   1.2   itojun 	}
    319   1.2   itojun #endif
    320   1.2   itojun 	ifp->if_opackets++;
    321   1.2   itojun 	ifp->if_obytes += m->m_pkthdr.len;
    322   1.9   itojun 
    323   1.9   itojun 	/* XXX should we check if our outer source is legal? */
    324   1.2   itojun 
    325   1.2   itojun 	switch (sc->gif_psrc->sa_family) {
    326   1.2   itojun #ifdef INET
    327   1.2   itojun 	case AF_INET:
    328   1.2   itojun 		error = in_gif_output(ifp, dst->sa_family, m, rt);
    329   1.2   itojun 		break;
    330   1.2   itojun #endif
    331   1.2   itojun #ifdef INET6
    332   1.2   itojun 	case AF_INET6:
    333   1.2   itojun 		error = in6_gif_output(ifp, dst->sa_family, m, rt);
    334   1.2   itojun 		break;
    335   1.2   itojun #endif
    336   1.2   itojun 	default:
    337   1.2   itojun 		m_freem(m);
    338   1.2   itojun 		error = ENETDOWN;
    339   1.2   itojun 	}
    340   1.2   itojun 
    341   1.2   itojun   end:
    342   1.2   itojun 	called = 0;		/* reset recursion counter */
    343   1.2   itojun 	if (error) ifp->if_oerrors++;
    344   1.2   itojun 	return error;
    345   1.2   itojun }
    346   1.2   itojun 
    347   1.2   itojun void
    348   1.2   itojun gif_input(m, af, gifp)
    349   1.2   itojun 	struct mbuf *m;
    350   1.2   itojun 	int af;
    351   1.2   itojun 	struct ifnet *gifp;
    352   1.2   itojun {
    353   1.2   itojun 	int s, isr;
    354   1.9   itojun 	register struct ifqueue *ifq = 0;
    355   1.2   itojun 
    356   1.2   itojun 	if (gifp == NULL) {
    357   1.2   itojun 		/* just in case */
    358   1.2   itojun 		m_freem(m);
    359   1.2   itojun 		return;
    360   1.2   itojun 	}
    361   1.2   itojun 
    362  1.10   itojun 	m->m_pkthdr.rcvif = gifp;
    363   1.2   itojun 
    364   1.2   itojun #if NBPFILTER > 0
    365   1.2   itojun 	if (gifp->if_bpf) {
    366   1.2   itojun 		/*
    367   1.2   itojun 		 * We need to prepend the address family as
    368   1.2   itojun 		 * a four byte field.  Cons up a dummy header
    369   1.2   itojun 		 * to pacify bpf.  This is safe because bpf
    370   1.2   itojun 		 * will only read from the mbuf (i.e., it won't
    371   1.2   itojun 		 * try to free it or keep a pointer a to it).
    372   1.2   itojun 		 */
    373   1.2   itojun 		struct mbuf m0;
    374   1.2   itojun 		u_int af = AF_INET6;
    375   1.2   itojun 
    376   1.2   itojun 		m0.m_next = m;
    377   1.2   itojun 		m0.m_len = 4;
    378   1.2   itojun 		m0.m_data = (char *)&af;
    379   1.2   itojun 
    380   1.9   itojun #ifdef HAVE_OLD_BPF
    381   1.9   itojun 		bpf_mtap(gifp, &m0);
    382   1.9   itojun #else
    383   1.2   itojun 		bpf_mtap(gifp->if_bpf, &m0);
    384   1.9   itojun #endif
    385   1.2   itojun 	}
    386   1.2   itojun #endif /*NBPFILTER > 0*/
    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.2   itojun 	 * specified address family.
    391   1.2   itojun 	 * Note: older versions of gif_input directly called network layer
    392   1.2   itojun 	 * input functions, e.g. ip6_input, here. We changed the policy to
    393   1.2   itojun 	 * prevent too many recursive calls of such input functions, which
    394   1.2   itojun 	 * might cause kernel panic. But the change may introduce another
    395   1.2   itojun 	 * problem; if the input queue is full, packets are discarded.
    396   1.2   itojun 	 * We believed it rarely occurs and changed the policy. If we find
    397   1.2   itojun 	 * it occurs more times than we thought, we may change the policy
    398   1.2   itojun 	 * again.
    399   1.2   itojun 	 */
    400   1.2   itojun 	switch (af) {
    401   1.2   itojun #ifdef INET
    402   1.2   itojun 	case AF_INET:
    403   1.2   itojun 		ifq = &ipintrq;
    404   1.2   itojun 		isr = NETISR_IP;
    405   1.2   itojun 		break;
    406   1.2   itojun #endif
    407   1.2   itojun #ifdef INET6
    408   1.2   itojun 	case AF_INET6:
    409   1.2   itojun 		ifq = &ip6intrq;
    410   1.2   itojun 		isr = NETISR_IPV6;
    411   1.2   itojun 		break;
    412   1.2   itojun #endif
    413   1.2   itojun 	default:
    414   1.2   itojun 		m_freem(m);
    415   1.2   itojun 		return;
    416   1.2   itojun 	}
    417   1.2   itojun 
    418   1.2   itojun 	s = splimp();
    419   1.2   itojun 	if (IF_QFULL(ifq)) {
    420   1.2   itojun 		IF_DROP(ifq);	/* update statistics */
    421   1.2   itojun 		m_freem(m);
    422   1.2   itojun 		splx(s);
    423   1.2   itojun 		return;
    424   1.2   itojun 	}
    425   1.2   itojun 	IF_ENQUEUE(ifq, m);
    426   1.2   itojun 	/* we need schednetisr since the address family may change */
    427   1.2   itojun 	schednetisr(isr);
    428   1.2   itojun 	gifp->if_ipackets++;
    429   1.2   itojun 	gifp->if_ibytes += m->m_pkthdr.len;
    430   1.2   itojun 	splx(s);
    431   1.2   itojun 
    432   1.2   itojun 	return;
    433   1.2   itojun }
    434   1.2   itojun 
    435   1.9   itojun /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
    436   1.2   itojun int
    437   1.2   itojun gif_ioctl(ifp, cmd, data)
    438   1.2   itojun 	struct ifnet *ifp;
    439   1.2   itojun 	u_long cmd;
    440   1.2   itojun 	caddr_t data;
    441   1.2   itojun {
    442   1.2   itojun 	struct gif_softc *sc  = (struct gif_softc*)ifp;
    443   1.2   itojun 	struct ifreq     *ifr = (struct ifreq*)data;
    444   1.2   itojun 	int error = 0, size;
    445   1.9   itojun 	struct sockaddr *dst, *src;
    446   1.9   itojun 	struct sockaddr *sa;
    447   1.9   itojun 	struct gif_softc *sc2;
    448   1.2   itojun 
    449   1.2   itojun 	switch (cmd) {
    450   1.2   itojun 	case SIOCSIFADDR:
    451   1.2   itojun 		break;
    452   1.2   itojun 
    453   1.2   itojun 	case SIOCSIFDSTADDR:
    454   1.2   itojun 		break;
    455   1.2   itojun 
    456   1.2   itojun 	case SIOCADDMULTI:
    457   1.2   itojun 	case SIOCDELMULTI:
    458   1.2   itojun 		switch (ifr->ifr_addr.sa_family) {
    459   1.2   itojun #ifdef INET
    460   1.2   itojun 		case AF_INET:	/* IP supports Multicast */
    461   1.2   itojun 			break;
    462   1.2   itojun #endif /* INET */
    463   1.2   itojun #ifdef INET6
    464   1.2   itojun 		case AF_INET6:	/* IP6 supports Multicast */
    465   1.2   itojun 			break;
    466   1.2   itojun #endif /* INET6 */
    467   1.2   itojun 		default:  /* Other protocols doesn't support Multicast */
    468   1.2   itojun 			error = EAFNOSUPPORT;
    469   1.2   itojun 			break;
    470   1.2   itojun 		}
    471   1.2   itojun 		break;
    472   1.2   itojun 
    473   1.2   itojun #ifdef	SIOCSIFMTU /* xxx */
    474   1.2   itojun 	case SIOCGIFMTU:
    475   1.2   itojun 		break;
    476  1.11   itojun 
    477   1.2   itojun 	case SIOCSIFMTU:
    478   1.2   itojun 		{
    479   1.2   itojun 			u_long mtu;
    480   1.2   itojun 			mtu = ifr->ifr_mtu;
    481   1.2   itojun 			if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
    482   1.2   itojun 				return (EINVAL);
    483   1.2   itojun 			}
    484   1.2   itojun 			ifp->if_mtu = mtu;
    485   1.2   itojun 		}
    486   1.2   itojun 		break;
    487   1.2   itojun #endif /* SIOCSIFMTU */
    488   1.2   itojun 
    489   1.2   itojun 	case SIOCSIFPHYADDR:
    490   1.2   itojun #ifdef INET6
    491   1.2   itojun 	case SIOCSIFPHYADDR_IN6:
    492   1.2   itojun #endif /* INET6 */
    493  1.11   itojun 		switch (cmd) {
    494  1.15   itojun #ifdef INET
    495  1.11   itojun 		case SIOCSIFPHYADDR:
    496  1.11   itojun 			src = (struct sockaddr *)
    497  1.11   itojun 				&(((struct in_aliasreq *)data)->ifra_addr);
    498  1.11   itojun 			dst = (struct sockaddr *)
    499  1.11   itojun 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
    500  1.16   itojun 			if (src->sa_len != sizeof(struct sockaddr_in) ||
    501  1.16   itojun 			    dst->sa_len != sizeof(struct sockaddr_in))
    502  1.16   itojun 				return EINVAL;
    503  1.16   itojun 			if (src->sa_family != AF_INET ||
    504  1.16   itojun 			    dst->sa_family != AF_INET)
    505  1.16   itojun 				return EAFNOSUPPORT;
    506  1.11   itojun 			break;
    507  1.15   itojun #endif
    508  1.11   itojun #ifdef INET6
    509  1.11   itojun 		case SIOCSIFPHYADDR_IN6:
    510  1.11   itojun 			src = (struct sockaddr *)
    511  1.11   itojun 				&(((struct in6_aliasreq *)data)->ifra_addr);
    512  1.11   itojun 			dst = (struct sockaddr *)
    513  1.11   itojun 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
    514  1.16   itojun 			if (src->sa_len != sizeof(struct sockaddr_in6) ||
    515  1.16   itojun 			    dst->sa_len != sizeof(struct sockaddr_in6))
    516  1.16   itojun 				return EINVAL;
    517  1.16   itojun 			if (src->sa_family != AF_INET6 ||
    518  1.16   itojun 			    dst->sa_family != AF_INET6)
    519  1.16   itojun 				return EAFNOSUPPORT;
    520  1.11   itojun 			break;
    521  1.11   itojun #endif
    522  1.11   itojun 		}
    523  1.11   itojun 
    524  1.12  thorpej 		for (sc2 = LIST_FIRST(&gif_softc_list); sc2 != NULL;
    525  1.12  thorpej 		     sc2 = LIST_NEXT(sc2, gif_list)) {
    526   1.9   itojun 			if (sc2 == sc)
    527   1.9   itojun 				continue;
    528   1.9   itojun 			if (!sc2->gif_pdst || !sc2->gif_psrc)
    529   1.9   itojun 				continue;
    530  1.10   itojun 			if (sc2->gif_pdst->sa_family != dst->sa_family ||
    531  1.10   itojun 			    sc2->gif_pdst->sa_len != dst->sa_len ||
    532  1.10   itojun 			    sc2->gif_psrc->sa_family != src->sa_family ||
    533  1.10   itojun 			    sc2->gif_psrc->sa_len != src->sa_len)
    534  1.10   itojun 				continue;
    535  1.10   itojun 
    536  1.10   itojun 			/* can't configure same pair of address onto two gifs */
    537  1.10   itojun 			if (bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
    538   1.9   itojun 			    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
    539   1.9   itojun 				error = EADDRNOTAVAIL;
    540   1.9   itojun 				goto bad;
    541   1.9   itojun 			}
    542  1.10   itojun 
    543  1.10   itojun 			/* can't configure multiple multi-dest interfaces */
    544  1.10   itojun #define multidest(x) \
    545  1.10   itojun 	(((struct sockaddr_in *)(x))->sin_addr.s_addr == INADDR_ANY)
    546  1.10   itojun #ifdef INET6
    547  1.10   itojun #define multidest6(x) \
    548  1.10   itojun 	(IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(x))->sin6_addr))
    549  1.10   itojun #endif
    550  1.10   itojun 			if (dst->sa_family == AF_INET &&
    551  1.10   itojun 			    multidest(dst) && multidest(sc2->gif_pdst)) {
    552  1.10   itojun 				error = EADDRNOTAVAIL;
    553  1.10   itojun 				goto bad;
    554  1.10   itojun 			}
    555  1.10   itojun #ifdef INET6
    556  1.10   itojun 			if (dst->sa_family == AF_INET6 &&
    557  1.10   itojun 			    multidest6(dst) && multidest6(sc2->gif_pdst)) {
    558  1.10   itojun 				error = EADDRNOTAVAIL;
    559  1.10   itojun 				goto bad;
    560  1.10   itojun 			}
    561  1.10   itojun #endif
    562   1.9   itojun 		}
    563   1.9   itojun 
    564   1.9   itojun 		if (src->sa_family != dst->sa_family ||
    565   1.9   itojun 		    src->sa_len != dst->sa_len) {
    566   1.9   itojun 			error = EINVAL;
    567   1.9   itojun 			break;
    568   1.9   itojun 		}
    569   1.9   itojun 		switch (src->sa_family) {
    570   1.2   itojun #ifdef INET
    571   1.2   itojun 		case AF_INET:
    572   1.2   itojun 			size = sizeof(struct sockaddr_in);
    573   1.2   itojun 			break;
    574   1.9   itojun #endif
    575   1.2   itojun #ifdef INET6
    576   1.2   itojun 		case AF_INET6:
    577   1.2   itojun 			size = sizeof(struct sockaddr_in6);
    578   1.2   itojun 			break;
    579   1.9   itojun #endif
    580   1.2   itojun 		default:
    581   1.9   itojun 			error = EAFNOSUPPORT;
    582   1.2   itojun 			goto bad;
    583   1.9   itojun 		}
    584   1.9   itojun 		if (src->sa_len != size) {
    585   1.9   itojun 			error = EINVAL;
    586   1.2   itojun 			break;
    587   1.2   itojun 		}
    588   1.9   itojun 
    589   1.9   itojun 		if (sc->gif_psrc)
    590   1.2   itojun 			free((caddr_t)sc->gif_psrc, M_IFADDR);
    591   1.2   itojun 		sa = (struct sockaddr *)malloc(size, M_IFADDR, M_WAITOK);
    592   1.2   itojun 		bcopy((caddr_t)src, (caddr_t)sa, size);
    593   1.2   itojun 		sc->gif_psrc = sa;
    594   1.9   itojun 
    595   1.9   itojun 		if (sc->gif_pdst)
    596   1.9   itojun 			free((caddr_t)sc->gif_pdst, M_IFADDR);
    597   1.2   itojun 		sa = (struct sockaddr *)malloc(size, M_IFADDR, M_WAITOK);
    598   1.2   itojun 		bcopy((caddr_t)dst, (caddr_t)sa, size);
    599   1.2   itojun 		sc->gif_pdst = sa;
    600   1.9   itojun 
    601   1.7   itojun 		ifp->if_flags |= IFF_UP;
    602   1.2   itojun 		if_up(ifp);		/* send up RTM_IFINFO */
    603   1.2   itojun 
    604   1.9   itojun 		error = 0;
    605   1.9   itojun 		break;
    606   1.9   itojun 
    607   1.9   itojun #ifdef SIOCDIFPHYADDR
    608   1.9   itojun 	case SIOCDIFPHYADDR:
    609  1.12  thorpej 		gif_delete_tunnel(sc);
    610   1.2   itojun 		break;
    611   1.9   itojun #endif
    612   1.2   itojun 
    613   1.2   itojun 	case SIOCGIFPSRCADDR:
    614   1.2   itojun #ifdef INET6
    615   1.2   itojun 	case SIOCGIFPSRCADDR_IN6:
    616   1.2   itojun #endif /* INET6 */
    617   1.2   itojun 		if (sc->gif_psrc == NULL) {
    618   1.2   itojun 			error = EADDRNOTAVAIL;
    619   1.2   itojun 			goto bad;
    620   1.2   itojun 		}
    621   1.2   itojun 		src = sc->gif_psrc;
    622  1.16   itojun 		switch (cmd) {
    623   1.2   itojun #ifdef INET
    624  1.16   itojun 		case SIOCGIFPSRCADDR:
    625   1.2   itojun 			dst = &ifr->ifr_addr;
    626  1.16   itojun 			size = sizeof(ifr->ifr_addr);
    627   1.2   itojun 			break;
    628   1.2   itojun #endif /* INET */
    629   1.2   itojun #ifdef INET6
    630  1.16   itojun 		case SIOCGIFPSRCADDR_IN6:
    631   1.2   itojun 			dst = (struct sockaddr *)
    632   1.2   itojun 				&(((struct in6_ifreq *)data)->ifr_addr);
    633  1.16   itojun 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    634   1.2   itojun 			break;
    635   1.2   itojun #endif /* INET6 */
    636   1.2   itojun 		default:
    637   1.2   itojun 			error = EADDRNOTAVAIL;
    638   1.2   itojun 			goto bad;
    639   1.2   itojun 		}
    640  1.16   itojun 		if (src->sa_len > size)
    641  1.16   itojun 			return EINVAL;
    642  1.16   itojun 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
    643   1.2   itojun 		break;
    644   1.2   itojun 
    645   1.2   itojun 	case SIOCGIFPDSTADDR:
    646   1.2   itojun #ifdef INET6
    647   1.2   itojun 	case SIOCGIFPDSTADDR_IN6:
    648   1.2   itojun #endif /* INET6 */
    649   1.2   itojun 		if (sc->gif_pdst == NULL) {
    650   1.2   itojun 			error = EADDRNOTAVAIL;
    651   1.2   itojun 			goto bad;
    652   1.2   itojun 		}
    653   1.2   itojun 		src = sc->gif_pdst;
    654  1.16   itojun 		switch (cmd) {
    655   1.2   itojun #ifdef INET
    656  1.16   itojun 		case SIOCGIFPDSTADDR:
    657   1.2   itojun 			dst = &ifr->ifr_addr;
    658  1.16   itojun 			size = sizeof(ifr->ifr_addr);
    659   1.2   itojun 			break;
    660   1.2   itojun #endif /* INET */
    661   1.2   itojun #ifdef INET6
    662  1.16   itojun 		case SIOCGIFPDSTADDR_IN6:
    663   1.2   itojun 			dst = (struct sockaddr *)
    664   1.2   itojun 				&(((struct in6_ifreq *)data)->ifr_addr);
    665  1.16   itojun 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
    666   1.2   itojun 			break;
    667   1.2   itojun #endif /* INET6 */
    668   1.2   itojun 		default:
    669   1.2   itojun 			error = EADDRNOTAVAIL;
    670   1.2   itojun 			goto bad;
    671   1.2   itojun 		}
    672  1.16   itojun 		if (src->sa_len > size)
    673  1.16   itojun 			return EINVAL;
    674  1.16   itojun 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
    675   1.2   itojun 		break;
    676   1.2   itojun 
    677   1.2   itojun 	case SIOCSIFFLAGS:
    678   1.9   itojun 		/* if_ioctl() takes care of it */
    679   1.2   itojun 		break;
    680   1.2   itojun 
    681   1.2   itojun 	default:
    682   1.2   itojun 		error = EINVAL;
    683   1.2   itojun 		break;
    684   1.2   itojun 	}
    685   1.2   itojun  bad:
    686   1.2   itojun 	return error;
    687  1.12  thorpej }
    688  1.12  thorpej 
    689  1.12  thorpej void
    690  1.12  thorpej gif_delete_tunnel(sc)
    691  1.12  thorpej 	struct gif_softc *sc;
    692  1.12  thorpej {
    693  1.12  thorpej 	int s;
    694  1.12  thorpej 
    695  1.12  thorpej 	s = splsoftnet();
    696  1.12  thorpej 
    697  1.12  thorpej 	if (sc->gif_psrc) {
    698  1.12  thorpej 		free((caddr_t)sc->gif_psrc, M_IFADDR);
    699  1.12  thorpej 		sc->gif_psrc = NULL;
    700  1.12  thorpej 	}
    701  1.12  thorpej 	if (sc->gif_pdst) {
    702  1.12  thorpej 		free((caddr_t)sc->gif_pdst, M_IFADDR);
    703  1.12  thorpej 		sc->gif_pdst = NULL;
    704  1.12  thorpej 	}
    705  1.12  thorpej 	/* change the IFF_UP flag as well? */
    706  1.12  thorpej 
    707  1.12  thorpej 	splx(s);
    708   1.2   itojun }
    709   1.2   itojun #endif /*NGIF > 0*/
    710