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