Home | History | Annotate | Line # | Download | only in net
if_gre.c revision 1.49.2.1.2.2
      1  1.49.2.1.2.2       riz /*	$NetBSD: if_gre.c,v 1.49.2.1.2.2 2005/05/24 19:34:16 riz Exp $ */
      2           1.4   thorpej 
      3           1.1       hwr /*
      4           1.4   thorpej  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5           1.1       hwr  * All rights reserved.
      6           1.1       hwr  *
      7           1.1       hwr  * This code is derived from software contributed to The NetBSD Foundation
      8           1.1       hwr  * by Heiko W.Rupp <hwr (at) pilhuhn.de>
      9           1.1       hwr  *
     10  1.49.2.1.2.1       snj  * IPv6-over-GRE contributed by Gert Doering <gert (at) greenie.muc.de>
     11  1.49.2.1.2.1       snj  *
     12           1.1       hwr  * Redistribution and use in source and binary forms, with or without
     13           1.1       hwr  * modification, are permitted provided that the following conditions
     14           1.1       hwr  * are met:
     15           1.1       hwr  * 1. Redistributions of source code must retain the above copyright
     16           1.1       hwr  *    notice, this list of conditions and the following disclaimer.
     17           1.1       hwr  * 2. Redistributions in binary form must reproduce the above copyright
     18           1.1       hwr  *    notice, this list of conditions and the following disclaimer in the
     19           1.1       hwr  *    documentation and/or other materials provided with the distribution.
     20           1.1       hwr  * 3. All advertising materials mentioning features or use of this software
     21           1.1       hwr  *    must display the following acknowledgement:
     22           1.1       hwr  *        This product includes software developed by the NetBSD
     23           1.1       hwr  *        Foundation, Inc. and its contributors.
     24           1.1       hwr  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25           1.1       hwr  *    contributors may be used to endorse or promote products derived
     26           1.1       hwr  *    from this software without specific prior written permission.
     27           1.1       hwr  *
     28           1.1       hwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29           1.1       hwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30           1.1       hwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31           1.1       hwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32           1.1       hwr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33           1.1       hwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34           1.1       hwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35           1.1       hwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36           1.1       hwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37           1.1       hwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38           1.1       hwr  * POSSIBILITY OF SUCH DAMAGE.
     39           1.1       hwr  */
     40           1.1       hwr 
     41           1.1       hwr /*
     42           1.1       hwr  * Encapsulate L3 protocols into IP
     43           1.1       hwr  * See RFC 1701 and 1702 for more details.
     44           1.1       hwr  * If_gre is compatible with Cisco GRE tunnels, so you can
     45           1.1       hwr  * have a NetBSD box as the other end of a tunnel interface of a Cisco
     46           1.1       hwr  * router. See gre(4) for more details.
     47           1.6       hwr  * Also supported:  IP in IP encaps (proto 55) as of RFC 2004
     48           1.1       hwr  */
     49          1.22     lukem 
     50          1.22     lukem #include <sys/cdefs.h>
     51  1.49.2.1.2.2       riz __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.49.2.1.2.2 2005/05/24 19:34:16 riz Exp $");
     52           1.1       hwr 
     53           1.1       hwr #include "opt_inet.h"
     54           1.9  drochner #include "opt_ns.h"
     55           1.1       hwr #include "bpfilter.h"
     56           1.1       hwr 
     57           1.1       hwr #include <sys/param.h>
     58           1.1       hwr #include <sys/malloc.h>
     59           1.1       hwr #include <sys/mbuf.h>
     60          1.13    martin #include <sys/proc.h>
     61           1.1       hwr #include <sys/protosw.h>
     62           1.1       hwr #include <sys/socket.h>
     63           1.1       hwr #include <sys/ioctl.h>
     64          1.10   thorpej #include <sys/queue.h>
     65           1.1       hwr #if __NetBSD__
     66           1.1       hwr #include <sys/systm.h>
     67           1.1       hwr #endif
     68           1.1       hwr 
     69           1.1       hwr #include <machine/cpu.h>
     70           1.1       hwr 
     71           1.1       hwr #include <net/ethertypes.h>
     72           1.1       hwr #include <net/if.h>
     73           1.1       hwr #include <net/if_types.h>
     74           1.1       hwr #include <net/netisr.h>
     75           1.1       hwr #include <net/route.h>
     76           1.1       hwr 
     77           1.1       hwr #ifdef INET
     78           1.1       hwr #include <netinet/in.h>
     79           1.1       hwr #include <netinet/in_systm.h>
     80           1.1       hwr #include <netinet/in_var.h>
     81           1.1       hwr #include <netinet/ip.h>
     82           1.1       hwr #include <netinet/ip_var.h>
     83           1.1       hwr #else
     84           1.4   thorpej #error "Huh? if_gre without inet?"
     85           1.1       hwr #endif
     86           1.1       hwr 
     87           1.1       hwr #ifdef NS
     88           1.1       hwr #include <netns/ns.h>
     89           1.1       hwr #include <netns/ns_if.h>
     90           1.1       hwr #endif
     91           1.1       hwr 
     92           1.1       hwr #ifdef NETATALK
     93           1.1       hwr #include <netatalk/at.h>
     94           1.1       hwr #include <netatalk/at_var.h>
     95           1.1       hwr #include <netatalk/at_extern.h>
     96           1.1       hwr #endif
     97           1.1       hwr 
     98           1.1       hwr #if NBPFILTER > 0
     99           1.1       hwr #include <sys/time.h>
    100           1.1       hwr #include <net/bpf.h>
    101           1.1       hwr #endif
    102           1.1       hwr 
    103           1.1       hwr #include <net/if_gre.h>
    104           1.1       hwr 
    105          1.20    itojun /*
    106          1.27    martin  * It is not easy to calculate the right value for a GRE MTU.
    107          1.27    martin  * We leave this task to the admin and use the same default that
    108          1.27    martin  * other vendors use.
    109          1.20    itojun  */
    110          1.27    martin #define GREMTU 1476
    111           1.1       hwr 
    112          1.11   thorpej struct gre_softc_head gre_softc_list;
    113          1.26    martin int ip_gre_ttl = GRE_TTL;
    114           1.1       hwr 
    115          1.10   thorpej int	gre_clone_create __P((struct if_clone *, int));
    116          1.10   thorpej void	gre_clone_destroy __P((struct ifnet *));
    117          1.10   thorpej 
    118          1.10   thorpej struct if_clone gre_cloner =
    119          1.10   thorpej     IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
    120           1.1       hwr 
    121          1.25    martin int gre_compute_route(struct gre_softc *sc);
    122           1.1       hwr 
    123          1.10   thorpej void	greattach __P((int));
    124          1.10   thorpej 
    125          1.10   thorpej /* ARGSUSED */
    126           1.8  explorer void
    127          1.10   thorpej greattach(count)
    128          1.10   thorpej 	int count;
    129          1.10   thorpej {
    130          1.10   thorpej 
    131          1.10   thorpej 	LIST_INIT(&gre_softc_list);
    132          1.10   thorpej 	if_clone_attach(&gre_cloner);
    133          1.10   thorpej }
    134          1.10   thorpej 
    135          1.10   thorpej int
    136          1.10   thorpej gre_clone_create(ifc, unit)
    137          1.10   thorpej 	struct if_clone *ifc;
    138          1.10   thorpej 	int unit;
    139           1.1       hwr {
    140           1.8  explorer 	struct gre_softc *sc;
    141           1.1       hwr 
    142          1.10   thorpej 	sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK);
    143          1.10   thorpej 	memset(sc, 0, sizeof(struct gre_softc));
    144          1.10   thorpej 
    145          1.10   thorpej 	sprintf(sc->sc_if.if_xname, "%s%d", ifc->ifc_name, unit);
    146          1.10   thorpej 	sc->sc_if.if_softc = sc;
    147      1.49.2.1     grant 	sc->sc_if.if_type = IFT_TUNNEL;
    148          1.34    itojun 	sc->sc_if.if_addrlen = 0;
    149          1.10   thorpej 	sc->sc_if.if_hdrlen = 24; /* IP + GRE */
    150          1.15   thorpej 	sc->sc_if.if_dlt = DLT_NULL;
    151          1.20    itojun 	sc->sc_if.if_mtu = GREMTU;
    152          1.10   thorpej 	sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
    153          1.10   thorpej 	sc->sc_if.if_output = gre_output;
    154          1.10   thorpej 	sc->sc_if.if_ioctl = gre_ioctl;
    155          1.10   thorpej 	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
    156          1.10   thorpej 	sc->g_proto = IPPROTO_GRE;
    157          1.35    itojun 	sc->sc_if.if_flags |= IFF_LINK0;
    158          1.10   thorpej 	if_attach(&sc->sc_if);
    159          1.16   thorpej 	if_alloc_sadl(&sc->sc_if);
    160           1.1       hwr #if NBPFILTER > 0
    161          1.14   thorpej 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
    162           1.1       hwr #endif
    163          1.10   thorpej 	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
    164          1.10   thorpej 	return (0);
    165          1.10   thorpej }
    166           1.1       hwr 
    167          1.10   thorpej void
    168          1.10   thorpej gre_clone_destroy(ifp)
    169          1.10   thorpej 	struct ifnet *ifp;
    170          1.10   thorpej {
    171          1.10   thorpej 	struct gre_softc *sc = ifp->if_softc;
    172          1.10   thorpej 
    173          1.10   thorpej 	LIST_REMOVE(sc, sc_list);
    174          1.10   thorpej #if NBPFILTER > 0
    175          1.10   thorpej 	bpfdetach(ifp);
    176          1.10   thorpej #endif
    177          1.10   thorpej 	if_detach(ifp);
    178          1.10   thorpej 	free(sc, M_DEVBUF);
    179           1.1       hwr }
    180           1.1       hwr 
    181          1.20    itojun /*
    182           1.1       hwr  * The output routine. Takes a packet and encapsulates it in the protocol
    183           1.6       hwr  * given by sc->g_proto. See also RFC 1701 and RFC 2004
    184           1.1       hwr  */
    185           1.1       hwr int
    186           1.8  explorer gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
    187           1.8  explorer 	   struct rtentry *rt)
    188           1.1       hwr {
    189           1.8  explorer 	int error = 0;
    190          1.10   thorpej 	struct gre_softc *sc = ifp->if_softc;
    191           1.1       hwr 	struct greip *gh;
    192          1.41    itojun 	struct ip *ip;
    193  1.49.2.1.2.1       snj 	u_int8_t ip_tos = 0;
    194          1.48    itojun 	u_int16_t etype = 0;
    195          1.20    itojun 	struct mobile_h mob_h;
    196          1.24    martin 
    197          1.39    itojun 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
    198          1.37    itojun 	    sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
    199          1.37    itojun 		m_freem(m);
    200          1.38    itojun 		error = ENETDOWN;
    201          1.38    itojun 		goto end;
    202          1.37    itojun 	}
    203           1.1       hwr 
    204           1.8  explorer 	gh = NULL;
    205          1.41    itojun 	ip = NULL;
    206           1.1       hwr 
    207           1.1       hwr #if NBPFILTER >0
    208          1.14   thorpej 	if (ifp->if_bpf) {
    209           1.1       hwr 		/* see comment of other if_foo.c files */
    210           1.1       hwr 		struct mbuf m0;
    211          1.17    itojun 		u_int32_t af = dst->sa_family;
    212           1.1       hwr 
    213           1.8  explorer 		m0.m_next = m;
    214           1.8  explorer 		m0.m_len = 4;
    215           1.1       hwr 		m0.m_data = (char *)&af;
    216          1.20    itojun 
    217          1.14   thorpej 		bpf_mtap(ifp->if_bpf, &m0);
    218           1.1       hwr 	}
    219           1.1       hwr #endif
    220           1.1       hwr 
    221          1.26    martin 	m->m_flags &= ~(M_BCAST|M_MCAST);
    222           1.1       hwr 
    223           1.5   thorpej 	if (sc->g_proto == IPPROTO_MOBILE) {
    224           1.3       hwr 		if (dst->sa_family == AF_INET) {
    225           1.3       hwr 			struct mbuf *m0;
    226           1.3       hwr 			int msiz;
    227           1.3       hwr 
    228          1.41    itojun 			ip = mtod(m, struct ip *);
    229           1.3       hwr 
    230           1.8  explorer 			memset(&mob_h, 0, MOB_H_SIZ_L);
    231          1.41    itojun 			mob_h.proto = (ip->ip_p) << 8;
    232          1.41    itojun 			mob_h.odst = ip->ip_dst.s_addr;
    233          1.41    itojun 			ip->ip_dst.s_addr = sc->g_dst.s_addr;
    234           1.3       hwr 
    235           1.3       hwr 			/*
    236           1.8  explorer 			 * If the packet comes from our host, we only change
    237           1.8  explorer 			 * the destination address in the IP header.
    238           1.8  explorer 			 * Else we also need to save and change the source
    239           1.3       hwr 			 */
    240          1.41    itojun 			if (in_hosteq(ip->ip_src, sc->g_src)) {
    241           1.8  explorer 				msiz = MOB_H_SIZ_S;
    242           1.3       hwr 			} else {
    243           1.3       hwr 				mob_h.proto |= MOB_H_SBIT;
    244          1.41    itojun 				mob_h.osrc = ip->ip_src.s_addr;
    245          1.41    itojun 				ip->ip_src.s_addr = sc->g_src.s_addr;
    246           1.8  explorer 				msiz = MOB_H_SIZ_L;
    247           1.3       hwr 			}
    248           1.3       hwr 			HTONS(mob_h.proto);
    249          1.48    itojun 			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
    250           1.3       hwr 
    251           1.3       hwr 			if ((m->m_data - msiz) < m->m_pktdat) {
    252           1.3       hwr 				/* need new mbuf */
    253           1.3       hwr 				MGETHDR(m0, M_DONTWAIT, MT_HEADER);
    254           1.8  explorer 				if (m0 == NULL) {
    255           1.3       hwr 					IF_DROP(&ifp->if_snd);
    256           1.3       hwr 					m_freem(m);
    257          1.38    itojun 					error = ENOBUFS;
    258          1.38    itojun 					goto end;
    259           1.3       hwr 				}
    260           1.8  explorer 				m0->m_next = m;
    261           1.3       hwr 				m->m_data += sizeof(struct ip);
    262           1.3       hwr 				m->m_len -= sizeof(struct ip);
    263           1.8  explorer 				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
    264           1.3       hwr 				m0->m_len = msiz + sizeof(struct ip);
    265           1.3       hwr 				m0->m_data += max_linkhdr;
    266          1.41    itojun 				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
    267           1.8  explorer 				       sizeof(struct ip));
    268           1.8  explorer 				m = m0;
    269          1.20    itojun 			} else {  /* we have some space left in the old one */
    270           1.8  explorer 				m->m_data -= msiz;
    271           1.8  explorer 				m->m_len += msiz;
    272           1.8  explorer 				m->m_pkthdr.len += msiz;
    273          1.41    itojun 				memmove(mtod(m, caddr_t), ip,
    274           1.8  explorer 					sizeof(struct ip));
    275           1.3       hwr 			}
    276          1.41    itojun 			ip = mtod(m, struct ip *);
    277          1.41    itojun 			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
    278          1.42    itojun 			ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
    279           1.3       hwr 		} else {  /* AF_INET */
    280           1.3       hwr 			IF_DROP(&ifp->if_snd);
    281           1.3       hwr 			m_freem(m);
    282          1.38    itojun 			error = EINVAL;
    283          1.38    itojun 			goto end;
    284           1.3       hwr 		}
    285           1.1       hwr 	} else if (sc->g_proto == IPPROTO_GRE) {
    286  1.49.2.1.2.1       snj #ifdef GRE_DEBUG
    287  1.49.2.1.2.1       snj                printf( "start gre_output/GRE, dst->sa_family=%d\n",
    288  1.49.2.1.2.1       snj                         dst->sa_family );
    289  1.49.2.1.2.1       snj #endif
    290          1.20    itojun 		switch (dst->sa_family) {
    291           1.1       hwr 		case AF_INET:
    292          1.41    itojun 			ip = mtod(m, struct ip *);
    293  1.49.2.1.2.1       snj 			ip_tos = ip->ip_tos;
    294           1.8  explorer 			etype = ETHERTYPE_IP;
    295           1.1       hwr 			break;
    296           1.1       hwr #ifdef NETATALK
    297           1.1       hwr 		case AF_APPLETALK:
    298           1.8  explorer 			etype = ETHERTYPE_ATALK;
    299           1.1       hwr 			break;
    300           1.1       hwr #endif
    301           1.1       hwr #ifdef NS
    302           1.1       hwr 		case AF_NS:
    303           1.8  explorer 			etype = ETHERTYPE_NS;
    304           1.1       hwr 			break;
    305           1.1       hwr #endif
    306  1.49.2.1.2.1       snj #ifdef INET6
    307  1.49.2.1.2.1       snj 		case AF_INET6:
    308  1.49.2.1.2.1       snj 			etype = ETHERTYPE_IPV6;
    309  1.49.2.1.2.1       snj 			break;
    310  1.49.2.1.2.1       snj #endif
    311           1.1       hwr 		default:
    312           1.1       hwr 			IF_DROP(&ifp->if_snd);
    313           1.1       hwr 			m_freem(m);
    314          1.38    itojun 			error = EAFNOSUPPORT;
    315          1.38    itojun 			goto end;
    316           1.1       hwr 		}
    317           1.8  explorer 		M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
    318           1.1       hwr 	} else {
    319           1.1       hwr 		IF_DROP(&ifp->if_snd);
    320           1.1       hwr 		m_freem(m);
    321          1.38    itojun 		error = EINVAL;
    322          1.38    itojun 		goto end;
    323           1.1       hwr 	}
    324           1.1       hwr 
    325          1.38    itojun 	if (m == NULL) {	/* impossible */
    326           1.1       hwr 		IF_DROP(&ifp->if_snd);
    327          1.38    itojun 		error = ENOBUFS;
    328          1.38    itojun 		goto end;
    329           1.1       hwr 	}
    330           1.1       hwr 
    331           1.8  explorer 	gh = mtod(m, struct greip *);
    332           1.8  explorer 	if (sc->g_proto == IPPROTO_GRE) {
    333           1.1       hwr 		/* we don't have any GRE flags for now */
    334           1.1       hwr 
    335           1.8  explorer 		memset((void *)&gh->gi_g, 0, sizeof(struct gre_h));
    336           1.8  explorer 		gh->gi_ptype = htons(etype);
    337           1.1       hwr 	}
    338           1.1       hwr 
    339           1.1       hwr 	gh->gi_pr = sc->g_proto;
    340           1.3       hwr 	if (sc->g_proto != IPPROTO_MOBILE) {
    341           1.3       hwr 		gh->gi_src = sc->g_src;
    342           1.3       hwr 		gh->gi_dst = sc->g_dst;
    343          1.20    itojun 		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
    344          1.26    martin 		((struct ip*)gh)->ip_ttl = ip_gre_ttl;
    345  1.49.2.1.2.1       snj 		((struct ip*)gh)->ip_tos = ip_tos;
    346          1.49    itojun 		gh->gi_len = htons(m->m_pkthdr.len);
    347           1.3       hwr 	}
    348           1.1       hwr 
    349           1.1       hwr 	ifp->if_opackets++;
    350           1.8  explorer 	ifp->if_obytes += m->m_pkthdr.len;
    351           1.1       hwr 	/* send it off */
    352          1.46  jonathan 	error = ip_output(m, NULL, &sc->route, 0,
    353          1.47    itojun 	    (struct ip_moptions *)NULL, (struct socket *)NULL);
    354          1.38    itojun   end:
    355           1.8  explorer 	if (error)
    356           1.1       hwr 		ifp->if_oerrors++;
    357           1.8  explorer 	return (error);
    358           1.1       hwr }
    359           1.1       hwr 
    360           1.1       hwr int
    361           1.8  explorer gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    362           1.1       hwr {
    363          1.13    martin 	struct proc *p = curproc;	/* XXX */
    364           1.8  explorer 	struct ifreq *ifr = (struct ifreq *)data;
    365          1.28    itojun 	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
    366           1.8  explorer 	struct gre_softc *sc = ifp->if_softc;
    367           1.1       hwr 	int s;
    368           1.1       hwr 	struct sockaddr_in si;
    369           1.8  explorer 	struct sockaddr *sa = NULL;
    370           1.1       hwr 	int error;
    371           1.1       hwr 
    372           1.8  explorer 	error = 0;
    373           1.1       hwr 
    374          1.18   thorpej 	s = splnet();
    375          1.20    itojun 	switch (cmd) {
    376          1.20    itojun 	case SIOCSIFADDR:
    377          1.36    itojun 		ifp->if_flags |= IFF_UP;
    378          1.36    itojun 		break;
    379          1.20    itojun 	case SIOCSIFDSTADDR:
    380           1.1       hwr 		break;
    381           1.1       hwr 	case SIOCSIFFLAGS:
    382          1.13    martin 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    383          1.13    martin 			break;
    384          1.35    itojun 		if ((ifr->ifr_flags & IFF_LINK0) != 0)
    385          1.35    itojun 			sc->g_proto = IPPROTO_GRE;
    386          1.35    itojun 		else
    387          1.35    itojun 			sc->g_proto = IPPROTO_MOBILE;
    388           1.1       hwr 		break;
    389          1.20    itojun 	case SIOCSIFMTU:
    390          1.13    martin 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    391          1.13    martin 			break;
    392          1.27    martin 		if (ifr->ifr_mtu < 576) {
    393           1.1       hwr 			error = EINVAL;
    394           1.1       hwr 			break;
    395           1.1       hwr 		}
    396           1.1       hwr 		ifp->if_mtu = ifr->ifr_mtu;
    397           1.1       hwr 		break;
    398           1.1       hwr 	case SIOCGIFMTU:
    399           1.1       hwr 		ifr->ifr_mtu = sc->sc_if.if_mtu;
    400           1.1       hwr 		break;
    401           1.1       hwr 	case SIOCADDMULTI:
    402           1.1       hwr 	case SIOCDELMULTI:
    403           1.8  explorer 		if (ifr == 0) {
    404           1.1       hwr 			error = EAFNOSUPPORT;
    405           1.1       hwr 			break;
    406           1.1       hwr 		}
    407           1.8  explorer 		switch (ifr->ifr_addr.sa_family) {
    408           1.1       hwr #ifdef INET
    409           1.1       hwr 		case AF_INET:
    410           1.1       hwr 			break;
    411           1.1       hwr #endif
    412  1.49.2.1.2.1       snj #ifdef INET6
    413  1.49.2.1.2.1       snj 		case AF_INET6:
    414  1.49.2.1.2.1       snj 			break;
    415  1.49.2.1.2.1       snj #endif
    416           1.1       hwr 		default:
    417           1.1       hwr 			error = EAFNOSUPPORT;
    418           1.1       hwr 			break;
    419           1.1       hwr 		}
    420           1.1       hwr 		break;
    421           1.1       hwr 	case GRESPROTO:
    422          1.13    martin 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    423          1.13    martin 			break;
    424           1.1       hwr 		sc->g_proto = ifr->ifr_flags;
    425           1.1       hwr 		switch (sc->g_proto) {
    426          1.40    itojun 		case IPPROTO_GRE:
    427           1.3       hwr 			ifp->if_flags |= IFF_LINK0;
    428           1.1       hwr 			break;
    429          1.40    itojun 		case IPPROTO_MOBILE:
    430          1.35    itojun 			ifp->if_flags &= ~IFF_LINK0;
    431           1.1       hwr 			break;
    432           1.1       hwr 		default:
    433          1.40    itojun 			error = EPROTONOSUPPORT;
    434          1.35    itojun 			break;
    435           1.1       hwr 		}
    436           1.1       hwr 		break;
    437           1.1       hwr 	case GREGPROTO:
    438           1.1       hwr 		ifr->ifr_flags = sc->g_proto;
    439           1.1       hwr 		break;
    440           1.1       hwr 	case GRESADDRS:
    441           1.1       hwr 	case GRESADDRD:
    442          1.13    martin 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    443          1.13    martin 			break;
    444           1.1       hwr 		/*
    445          1.20    itojun 		 * set tunnel endpoints, compute a less specific route
    446          1.20    itojun 		 * to the remote end and mark if as up
    447          1.20    itojun 		 */
    448           1.1       hwr 		sa = &ifr->ifr_addr;
    449          1.30    itojun 		if (cmd == GRESADDRS)
    450           1.1       hwr 			sc->g_src = (satosin(sa))->sin_addr;
    451          1.30    itojun 		if (cmd == GRESADDRD)
    452           1.1       hwr 			sc->g_dst = (satosin(sa))->sin_addr;
    453          1.30    itojun 	recompute:
    454           1.1       hwr 		if ((sc->g_src.s_addr != INADDR_ANY) &&
    455           1.1       hwr 		    (sc->g_dst.s_addr != INADDR_ANY)) {
    456           1.1       hwr 			if (sc->route.ro_rt != 0) /* free old route */
    457           1.1       hwr 				RTFREE(sc->route.ro_rt);
    458          1.25    martin 			if (gre_compute_route(sc) == 0)
    459          1.39    itojun 				ifp->if_flags |= IFF_RUNNING;
    460          1.39    itojun 			else
    461          1.39    itojun 				ifp->if_flags &= ~IFF_RUNNING;
    462           1.1       hwr 		}
    463           1.1       hwr 		break;
    464           1.1       hwr 	case GREGADDRS:
    465          1.33    itojun 		memset(&si, 0, sizeof(si));
    466          1.28    itojun 		si.sin_family = AF_INET;
    467          1.28    itojun 		si.sin_len = sizeof(struct sockaddr_in);
    468           1.1       hwr 		si.sin_addr.s_addr = sc->g_src.s_addr;
    469           1.8  explorer 		sa = sintosa(&si);
    470           1.1       hwr 		ifr->ifr_addr = *sa;
    471           1.1       hwr 		break;
    472           1.1       hwr 	case GREGADDRD:
    473          1.33    itojun 		memset(&si, 0, sizeof(si));
    474          1.28    itojun 		si.sin_family = AF_INET;
    475          1.28    itojun 		si.sin_len = sizeof(struct sockaddr_in);
    476           1.1       hwr 		si.sin_addr.s_addr = sc->g_dst.s_addr;
    477           1.8  explorer 		sa = sintosa(&si);
    478           1.1       hwr 		ifr->ifr_addr = *sa;
    479          1.28    itojun 		break;
    480          1.28    itojun 	case SIOCSLIFPHYADDR:
    481          1.28    itojun 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    482          1.28    itojun 			break;
    483          1.28    itojun 		if (lifr->addr.ss_family != AF_INET ||
    484          1.31    itojun 		    lifr->dstaddr.ss_family != AF_INET) {
    485          1.31    itojun 			error = EAFNOSUPPORT;
    486          1.31    itojun 			break;
    487          1.31    itojun 		}
    488          1.28    itojun 		if (lifr->addr.ss_len != sizeof(si) ||
    489          1.31    itojun 		    lifr->dstaddr.ss_len != sizeof(si)) {
    490          1.31    itojun 			error = EINVAL;
    491          1.31    itojun 			break;
    492          1.31    itojun 		}
    493          1.28    itojun 		sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
    494          1.28    itojun 		sc->g_dst =
    495          1.28    itojun 		    (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
    496          1.30    itojun 		goto recompute;
    497          1.28    itojun 	case SIOCDIFPHYADDR:
    498          1.28    itojun 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    499          1.28    itojun 			break;
    500          1.28    itojun 		sc->g_src.s_addr = INADDR_ANY;
    501          1.28    itojun 		sc->g_dst.s_addr = INADDR_ANY;
    502          1.28    itojun 		break;
    503          1.28    itojun 	case SIOCGLIFPHYADDR:
    504          1.28    itojun 		if (sc->g_src.s_addr == INADDR_ANY ||
    505          1.28    itojun 		    sc->g_dst.s_addr == INADDR_ANY) {
    506          1.28    itojun 			error = EADDRNOTAVAIL;
    507          1.28    itojun 			break;
    508          1.28    itojun 		}
    509          1.33    itojun 		memset(&si, 0, sizeof(si));
    510          1.28    itojun 		si.sin_family = AF_INET;
    511          1.28    itojun 		si.sin_len = sizeof(struct sockaddr_in);
    512          1.28    itojun 		si.sin_addr.s_addr = sc->g_src.s_addr;
    513          1.33    itojun 		memcpy(&lifr->addr, &si, sizeof(si));
    514          1.28    itojun 		si.sin_addr.s_addr = sc->g_dst.s_addr;
    515          1.33    itojun 		memcpy(&lifr->dstaddr, &si, sizeof(si));
    516           1.1       hwr 		break;
    517           1.1       hwr 	default:
    518           1.1       hwr 		error = EINVAL;
    519          1.31    itojun 		break;
    520           1.1       hwr 	}
    521           1.1       hwr 
    522           1.1       hwr 	splx(s);
    523           1.8  explorer 	return (error);
    524           1.1       hwr }
    525           1.1       hwr 
    526          1.20    itojun /*
    527           1.1       hwr  * computes a route to our destination that is not the one
    528           1.1       hwr  * which would be taken by ip_output(), as this one will loop back to
    529           1.1       hwr  * us. If the interface is p2p as  a--->b, then a routing entry exists
    530           1.1       hwr  * If we now send a packet to b (e.g. ping b), this will come down here
    531          1.43       wiz  * gets src=a, dst=b tacked on and would from ip_output() sent back to
    532           1.1       hwr  * if_gre.
    533           1.1       hwr  * Goal here is to compute a route to b that is less specific than
    534           1.1       hwr  * a-->b. We know that this one exists as in normal operation we have
    535           1.1       hwr  * at least a default route which matches.
    536           1.1       hwr  */
    537          1.25    martin int
    538           1.8  explorer gre_compute_route(struct gre_softc *sc)
    539           1.1       hwr {
    540           1.1       hwr 	struct route *ro;
    541           1.8  explorer 	u_int32_t a, b, c;
    542           1.1       hwr 
    543           1.8  explorer 	ro = &sc->route;
    544          1.20    itojun 
    545           1.8  explorer 	memset(ro, 0, sizeof(struct route));
    546           1.8  explorer 	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
    547           1.8  explorer 	ro->ro_dst.sa_family = AF_INET;
    548           1.8  explorer 	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
    549           1.8  explorer 
    550           1.1       hwr 	/*
    551           1.1       hwr 	 * toggle last bit, so our interface is not found, but a less
    552          1.20    itojun 	 * specific route. I'd rather like to specify a shorter mask,
    553          1.20    itojun 	 * but this is not possible. Should work though. XXX
    554           1.1       hwr 	 * there is a simpler way ...
    555          1.20    itojun 	 */
    556           1.8  explorer 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
    557           1.8  explorer 		a = ntohl(sc->g_dst.s_addr);
    558           1.8  explorer 		b = a & 0x01;
    559           1.8  explorer 		c = a & 0xfffffffe;
    560           1.8  explorer 		b = b ^ 0x01;
    561           1.8  explorer 		a = b | c;
    562           1.8  explorer 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
    563          1.20    itojun 		    = htonl(a);
    564           1.8  explorer 	}
    565           1.1       hwr 
    566           1.1       hwr #ifdef DIAGNOSTIC
    567          1.45     grant 	printf("%s: searching for a route to %s", sc->sc_if.if_xname,
    568          1.19    itojun 	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
    569           1.1       hwr #endif
    570           1.1       hwr 
    571           1.1       hwr 	rtalloc(ro);
    572           1.1       hwr 
    573           1.1       hwr 	/*
    574          1.25    martin 	 * check if this returned a route at all and this route is no
    575          1.25    martin 	 * recursion to ourself
    576          1.25    martin 	 */
    577          1.25    martin 	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
    578          1.25    martin #ifdef DIAGNOSTIC
    579          1.25    martin 		if (ro->ro_rt == NULL)
    580          1.25    martin 			printf(" - no route found!\n");
    581          1.25    martin 		else
    582          1.25    martin 			printf(" - route loops back to ourself!\n");
    583          1.25    martin #endif
    584          1.25    martin 		return EADDRNOTAVAIL;
    585          1.25    martin 	}
    586          1.25    martin 
    587          1.25    martin 	/*
    588          1.20    itojun 	 * now change it back - else ip_output will just drop
    589          1.20    itojun 	 * the route and search one to this interface ...
    590          1.20    itojun 	 */
    591           1.8  explorer 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
    592           1.8  explorer 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
    593           1.1       hwr 
    594           1.1       hwr #ifdef DIAGNOSTIC
    595          1.19    itojun 	printf(", choosing %s with gateway %s", ro->ro_rt->rt_ifp->if_xname,
    596          1.19    itojun 	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
    597           1.8  explorer 	printf("\n");
    598           1.1       hwr #endif
    599          1.25    martin 
    600          1.25    martin 	return 0;
    601           1.1       hwr }
    602           1.1       hwr 
    603           1.3       hwr /*
    604          1.20    itojun  * do a checksum of a buffer - much like in_cksum, which operates on
    605          1.20    itojun  * mbufs.
    606           1.3       hwr  */
    607          1.48    itojun u_int16_t
    608          1.48    itojun gre_in_cksum(u_int16_t *p, u_int len)
    609           1.3       hwr {
    610          1.48    itojun 	u_int32_t sum = 0;
    611           1.3       hwr 	int nwords = len >> 1;
    612          1.20    itojun 
    613           1.3       hwr 	while (nwords-- != 0)
    614           1.3       hwr 		sum += *p++;
    615          1.20    itojun 
    616          1.20    itojun 	if (len & 1) {
    617          1.20    itojun 		union {
    618          1.20    itojun 			u_short w;
    619          1.20    itojun 			u_char c[2];
    620          1.20    itojun 		} u;
    621          1.20    itojun 		u.c[0] = *(u_char *)p;
    622          1.20    itojun 		u.c[1] = 0;
    623          1.20    itojun 		sum += u.w;
    624          1.20    itojun 	}
    625          1.20    itojun 
    626          1.20    itojun 	/* end-around-carry */
    627          1.20    itojun 	sum = (sum >> 16) + (sum & 0xffff);
    628          1.20    itojun 	sum += (sum >> 16);
    629          1.20    itojun 	return (~sum);
    630           1.3       hwr }
    631