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