2010-05-20 00:41:59 +04:00
|
|
|
/* $NetBSD: if_etherip.c,v 1.30 2010/05/19 20:41:59 christos Exp $ */
|
2006-11-23 07:07:07 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2006, Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of Hans Rosenfeld nor the names of his
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*
|
2008-04-24 15:38:36 +04:00
|
|
|
* Copyright (c) 2003, 2004, 2008 The NetBSD Foundation.
|
2006-11-23 07:07:07 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2007-09-10 14:35:51 +04:00
|
|
|
* 3. Neither the name of The NetBSD Foundation nor the names of its
|
2006-11-23 07:07:07 +03:00
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2010-05-20 00:41:59 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: if_etherip.c,v 1.30 2010/05/19 20:41:59 christos Exp $");
|
2006-11-23 07:07:07 +03:00
|
|
|
|
|
|
|
#include "opt_inet.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/kauth.h>
|
|
|
|
#include <sys/socket.h>
|
2008-04-24 15:38:36 +04:00
|
|
|
#include <sys/socketvar.h>
|
2007-10-08 20:18:02 +04:00
|
|
|
#include <sys/intr.h>
|
2006-11-23 07:07:07 +03:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <net/if_ether.h>
|
|
|
|
#include <net/if_media.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if_etherip.h>
|
|
|
|
#include <net/bpf.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#ifdef INET
|
|
|
|
#include <netinet/in_var.h>
|
|
|
|
#endif /* INET */
|
|
|
|
#include <netinet/ip_etherip.h>
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet6/ip6_etherip.h>
|
|
|
|
#ifndef INET
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
#include <netinet6/in6_var.h>
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netinet6/ip6_var.h>
|
|
|
|
#include <netinet6/in6_gif.h>
|
|
|
|
#include <netinet6/ip6protosw.h>
|
|
|
|
#endif /* INET6 */
|
|
|
|
|
2007-05-30 01:32:27 +04:00
|
|
|
#include <compat/sys/sockio.h>
|
|
|
|
|
2006-11-23 07:07:07 +03:00
|
|
|
static int etherip_node;
|
|
|
|
static int etherip_sysctl_handler(SYSCTLFN_PROTO);
|
|
|
|
SYSCTL_SETUP_PROTO(sysctl_etherip_setup);
|
|
|
|
|
|
|
|
void etheripattach(int);
|
|
|
|
|
2008-10-25 01:41:04 +04:00
|
|
|
static int etherip_match(device_t, cfdata_t, void *);
|
|
|
|
static void etherip_attach(device_t, device_t, void *);
|
|
|
|
static int etherip_detach(device_t, int);
|
2006-11-23 07:07:07 +03:00
|
|
|
|
2008-07-09 17:18:41 +04:00
|
|
|
CFATTACH_DECL_NEW(etherip, sizeof(struct etherip_softc),
|
2006-11-23 07:07:07 +03:00
|
|
|
etherip_match, etherip_attach, etherip_detach, NULL);
|
|
|
|
extern struct cfdriver etherip_cd;
|
|
|
|
|
|
|
|
static void etherip_start(struct ifnet *);
|
|
|
|
static void etherip_stop(struct ifnet *, int);
|
|
|
|
static int etherip_init(struct ifnet *);
|
2007-03-04 08:59:00 +03:00
|
|
|
static int etherip_ioctl(struct ifnet *, u_long, void *);
|
2006-11-23 07:07:07 +03:00
|
|
|
|
|
|
|
static int etherip_mediachange(struct ifnet *);
|
|
|
|
static void etherip_mediastatus(struct ifnet *, struct ifmediareq *);
|
|
|
|
|
|
|
|
static int etherip_clone_create(struct if_clone *, int);
|
|
|
|
static int etherip_clone_destroy(struct ifnet *);
|
|
|
|
|
|
|
|
static struct if_clone etherip_cloners = IF_CLONE_INITIALIZER(
|
|
|
|
"etherip", etherip_clone_create, etherip_clone_destroy);
|
|
|
|
|
|
|
|
static int etherip_set_tunnel(struct ifnet *,
|
|
|
|
struct sockaddr *, struct sockaddr *);
|
|
|
|
static void etherip_delete_tunnel(struct ifnet *);
|
|
|
|
static void etheripintr(void *);
|
|
|
|
|
|
|
|
void
|
|
|
|
etheripattach(int count)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = config_cfattach_attach(etherip_cd.cd_name, ðerip_ca);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
aprint_error("%s: unable to register cfattach\n",
|
|
|
|
etherip_cd.cd_name);
|
|
|
|
(void)config_cfdriver_detach(ðerip_cd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIST_INIT(ðerip_softc_list);
|
|
|
|
if_clone_attach(ðerip_cloners);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pretty much useless for a pseudo-device */
|
|
|
|
static int
|
2008-10-25 01:41:04 +04:00
|
|
|
etherip_match(device_t self, cfdata_t cfdata, void *arg)
|
2006-11-23 07:07:07 +03:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-07-09 17:18:41 +04:00
|
|
|
etherip_attach(device_t parent, device_t self, void *aux)
|
2006-11-23 07:07:07 +03:00
|
|
|
{
|
2008-07-09 17:18:41 +04:00
|
|
|
struct etherip_softc *sc = device_private(self);
|
2006-11-23 07:07:07 +03:00
|
|
|
struct ifnet *ifp;
|
|
|
|
const struct sysctlnode *node;
|
2008-02-20 20:05:52 +03:00
|
|
|
uint8_t enaddr[ETHER_ADDR_LEN] =
|
2006-11-23 07:07:07 +03:00
|
|
|
{ 0xf2, 0x0b, 0xa5, 0xff, 0xff, 0xff };
|
|
|
|
char enaddrstr[3 * ETHER_ADDR_LEN];
|
|
|
|
struct timeval tv;
|
|
|
|
uint32_t ui;
|
|
|
|
int error;
|
|
|
|
|
2008-10-25 01:41:04 +04:00
|
|
|
sc->sc_dev = self;
|
2006-11-23 07:07:07 +03:00
|
|
|
sc->sc_si = NULL;
|
|
|
|
sc->sc_src = NULL;
|
|
|
|
sc->sc_dst = NULL;
|
|
|
|
|
2008-11-02 17:46:55 +03:00
|
|
|
if (!pmf_device_register(self, NULL, NULL))
|
|
|
|
aprint_error_dev(self, "couldn't establish power handler\n");
|
|
|
|
|
2006-11-23 07:07:07 +03:00
|
|
|
/*
|
|
|
|
* In order to obtain unique initial Ethernet address on a host,
|
|
|
|
* do some randomisation using the current uptime. It's not meant
|
|
|
|
* for anything but avoiding hard-coding an address.
|
|
|
|
*/
|
|
|
|
getmicrouptime(&tv);
|
|
|
|
ui = (tv.tv_sec ^ tv.tv_usec) & 0xffffff;
|
2008-02-20 20:05:52 +03:00
|
|
|
memcpy(enaddr+3, (uint8_t *)&ui, 3);
|
2006-11-23 07:07:07 +03:00
|
|
|
|
2008-07-09 17:18:41 +04:00
|
|
|
aprint_verbose_dev(self, "Ethernet address %s\n",
|
2006-11-23 07:07:07 +03:00
|
|
|
ether_snprintf(enaddrstr, sizeof(enaddrstr), enaddr));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Why 1000baseT? Why not? You can add more.
|
|
|
|
*
|
|
|
|
* Note that there are 3 steps: init, one or several additions to
|
|
|
|
* list of supported media, and in the end, the selection of one
|
|
|
|
* of them.
|
|
|
|
*/
|
|
|
|
ifmedia_init(&sc->sc_im, 0, etherip_mediachange, etherip_mediastatus);
|
|
|
|
ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_1000_T, 0, NULL);
|
|
|
|
ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_1000_T|IFM_FDX, 0, NULL);
|
|
|
|
ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_100_TX, 0, NULL);
|
|
|
|
ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
|
|
|
|
ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_10_T, 0, NULL);
|
|
|
|
ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
|
|
|
|
ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_AUTO, 0, NULL);
|
|
|
|
ifmedia_set(&sc->sc_im, IFM_ETHER|IFM_AUTO);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* One should note that an interface must do multicast in order
|
|
|
|
* to support IPv6.
|
|
|
|
*/
|
|
|
|
ifp = &sc->sc_ec.ec_if;
|
2008-07-10 09:15:32 +04:00
|
|
|
strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
|
2006-11-23 07:07:07 +03:00
|
|
|
ifp->if_softc = sc;
|
|
|
|
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
|
|
|
|
ifp->if_ioctl = etherip_ioctl;
|
|
|
|
ifp->if_start = etherip_start;
|
|
|
|
ifp->if_stop = etherip_stop;
|
|
|
|
ifp->if_init = etherip_init;
|
|
|
|
IFQ_SET_READY(&ifp->if_snd);
|
|
|
|
|
|
|
|
sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Those steps are mandatory for an Ethernet driver, the first call
|
|
|
|
* being common to all network interface drivers.
|
|
|
|
*/
|
|
|
|
if_attach(ifp);
|
|
|
|
ether_ifattach(ifp, enaddr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a sysctl node for that interface.
|
|
|
|
*
|
|
|
|
* The pointer transmitted is not a string, but instead a pointer to
|
|
|
|
* the softc structure, which we can use to build the string value on
|
|
|
|
* the fly in the helper function of the node. See the comments for
|
|
|
|
* etherip_sysctl_handler for details.
|
|
|
|
*/
|
|
|
|
error = sysctl_createv(NULL, 0, NULL, &node, CTLFLAG_READWRITE,
|
2008-07-10 09:15:32 +04:00
|
|
|
CTLTYPE_STRING, device_xname(self), NULL,
|
2006-11-23 07:07:07 +03:00
|
|
|
etherip_sysctl_handler, 0, sc, 18, CTL_NET,
|
2008-07-10 09:15:32 +04:00
|
|
|
AF_LINK, etherip_node, device_unit(self),
|
2006-11-23 07:07:07 +03:00
|
|
|
CTL_EOL);
|
|
|
|
if (error)
|
2008-07-09 17:18:41 +04:00
|
|
|
aprint_error_dev(self, "sysctl_createv returned %d, ignoring\n",
|
2008-04-05 17:49:36 +04:00
|
|
|
error);
|
2006-11-23 07:07:07 +03:00
|
|
|
|
|
|
|
/* insert into etherip_softc_list */
|
|
|
|
LIST_INSERT_HEAD(ðerip_softc_list, sc, etherip_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When detaching, we do the inverse of what is done in the attach
|
|
|
|
* routine, in reversed order.
|
|
|
|
*/
|
|
|
|
static int
|
2008-07-09 17:18:41 +04:00
|
|
|
etherip_detach(device_t self, int flags)
|
2006-11-23 07:07:07 +03:00
|
|
|
{
|
2008-07-09 17:18:41 +04:00
|
|
|
struct etherip_softc *sc = device_private(self);
|
2006-11-23 07:07:07 +03:00
|
|
|
struct ifnet *ifp = &sc->sc_ec.ec_if;
|
|
|
|
int error, s;
|
|
|
|
|
|
|
|
s = splnet();
|
|
|
|
etherip_stop(ifp, 1);
|
|
|
|
if_down(ifp);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroying a single leaf is a very straightforward operation using
|
|
|
|
* sysctl_destroyv. One should be sure to always end the path with
|
|
|
|
* CTL_EOL.
|
|
|
|
*/
|
|
|
|
error = sysctl_destroyv(NULL, CTL_NET, AF_LINK, etherip_node,
|
2008-07-10 09:15:32 +04:00
|
|
|
device_unit(self), CTL_EOL);
|
2006-11-23 07:07:07 +03:00
|
|
|
if (error)
|
2008-07-09 17:18:41 +04:00
|
|
|
aprint_error_dev(self, "sysctl_destroyv returned %d, ignoring\n",
|
2008-04-05 17:49:36 +04:00
|
|
|
error);
|
2006-11-23 07:07:07 +03:00
|
|
|
|
|
|
|
LIST_REMOVE(sc, etherip_list);
|
|
|
|
etherip_delete_tunnel(ifp);
|
|
|
|
ether_ifdetach(ifp);
|
|
|
|
if_detach(ifp);
|
2007-04-23 09:05:03 +04:00
|
|
|
rtcache_free(&sc->sc_ro);
|
2006-11-23 07:07:07 +03:00
|
|
|
ifmedia_delete_instance(&sc->sc_im, IFM_INST_ANY);
|
|
|
|
|
2008-11-03 03:52:07 +03:00
|
|
|
pmf_device_deregister(self);
|
|
|
|
|
2006-11-23 07:07:07 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called by the ifmedia layer to notify the driver
|
|
|
|
* that the user requested a media change. A real driver would
|
|
|
|
* reconfigure the hardware.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
etherip_mediachange(struct ifnet *ifp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here the user asks for the currently used media.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
etherip_mediastatus(struct ifnet *ifp, struct ifmediareq *imr)
|
|
|
|
{
|
2008-10-25 01:41:04 +04:00
|
|
|
struct etherip_softc *sc = ifp->if_softc;
|
2006-11-23 07:07:07 +03:00
|
|
|
|
|
|
|
imr->ifm_active = sc->sc_im.ifm_cur->ifm_media;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
etherip_start(struct ifnet *ifp)
|
|
|
|
{
|
2008-10-25 01:41:04 +04:00
|
|
|
struct etherip_softc *sc = ifp->if_softc;
|
2006-11-23 07:07:07 +03:00
|
|
|
|
|
|
|
if(sc->sc_si)
|
2007-10-08 20:18:02 +04:00
|
|
|
softint_schedule(sc->sc_si);
|
2006-11-23 07:07:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
etheripintr(void *arg)
|
|
|
|
{
|
2008-07-09 17:18:41 +04:00
|
|
|
struct etherip_softc *sc = (struct etherip_softc *)arg;
|
|
|
|
struct ifnet *ifp = &sc->sc_ec.ec_if;
|
2006-11-23 07:07:07 +03:00
|
|
|
struct mbuf *m;
|
|
|
|
int s, error;
|
|
|
|
|
2008-04-24 15:38:36 +04:00
|
|
|
mutex_enter(softnet_lock);
|
2006-11-23 07:07:07 +03:00
|
|
|
for (;;) {
|
|
|
|
s = splnet();
|
|
|
|
IFQ_DEQUEUE(&ifp->if_snd, m);
|
|
|
|
splx(s);
|
|
|
|
if (m == NULL)
|
2008-04-24 15:38:36 +04:00
|
|
|
break;
|
2006-11-23 07:07:07 +03:00
|
|
|
|
2010-04-05 11:19:28 +04:00
|
|
|
bpf_mtap(ifp, m);
|
2006-11-23 07:07:07 +03:00
|
|
|
|
|
|
|
ifp->if_opackets++;
|
|
|
|
if (sc->sc_src && sc->sc_dst) {
|
|
|
|
ifp->if_flags |= IFF_OACTIVE;
|
|
|
|
switch (sc->sc_src->sa_family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
|
|
|
error = ip_etherip_output(ifp, m);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
error = ip6_etherip_output(ifp, m);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
error = ENETDOWN;
|
|
|
|
}
|
|
|
|
ifp->if_flags &= ~IFF_OACTIVE;
|
|
|
|
} else m_freem(m);
|
|
|
|
}
|
2008-04-24 15:38:36 +04:00
|
|
|
mutex_exit(softnet_lock);
|
2006-11-23 07:07:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-03-04 08:59:00 +03:00
|
|
|
etherip_ioctl(struct ifnet *ifp, u_long cmd, void *data)
|
2006-11-23 07:07:07 +03:00
|
|
|
{
|
2008-10-25 01:41:04 +04:00
|
|
|
struct etherip_softc *sc = ifp->if_softc;
|
2007-05-30 01:32:27 +04:00
|
|
|
struct ifreq *ifr = data;
|
2006-11-23 07:07:07 +03:00
|
|
|
struct sockaddr *src, *dst;
|
|
|
|
int s, error;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case SIOCSLIFPHYADDR:
|
|
|
|
src = (struct sockaddr *)
|
|
|
|
&(((struct if_laddrreq *)data)->addr);
|
|
|
|
dst = (struct sockaddr *)
|
|
|
|
&(((struct if_laddrreq *)data)->dstaddr);
|
|
|
|
|
|
|
|
/* sa_family must be equal */
|
|
|
|
if (src->sa_family != dst->sa_family)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* validate sa_len */
|
|
|
|
switch (src->sa_family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
|
|
|
if (src->sa_len != sizeof(struct sockaddr_in) ||
|
|
|
|
dst->sa_len != sizeof(struct sockaddr_in))
|
|
|
|
return EINVAL;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
if (src->sa_len != sizeof(struct sockaddr_in6) ||
|
|
|
|
dst->sa_len != sizeof(struct sockaddr_in6))
|
|
|
|
return EINVAL;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return EAFNOSUPPORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = etherip_set_tunnel(ifp, src, dst);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIOCDIFPHYADDR:
|
|
|
|
etherip_delete_tunnel(ifp);
|
|
|
|
error = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIOCGLIFPHYADDR:
|
|
|
|
if (sc->sc_src == NULL || sc->sc_dst == NULL)
|
|
|
|
return EADDRNOTAVAIL;
|
|
|
|
|
|
|
|
/* copy src */
|
|
|
|
src = sc->sc_src;
|
|
|
|
dst = (struct sockaddr *)
|
|
|
|
&(((struct if_laddrreq *)data)->addr);
|
|
|
|
if (src->sa_len > sizeof(((struct if_laddrreq *)data)->addr))
|
|
|
|
return EINVAL;
|
|
|
|
memcpy(dst, src, src->sa_len);
|
|
|
|
|
|
|
|
/* copy dst */
|
|
|
|
src = sc->sc_dst;
|
|
|
|
dst = (struct sockaddr *)
|
|
|
|
&(((struct if_laddrreq *)data)->dstaddr);
|
|
|
|
if (src->sa_len > sizeof(((struct if_laddrreq *)data)->dstaddr))
|
|
|
|
return EINVAL;
|
|
|
|
memcpy(dst, src, src->sa_len);
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
break;
|
|
|
|
|
2007-05-30 01:32:27 +04:00
|
|
|
#ifdef OSIOCSIFMEDIA
|
|
|
|
case OSIOCSIFMEDIA:
|
|
|
|
#endif
|
2006-11-23 07:07:07 +03:00
|
|
|
case SIOCSIFMEDIA:
|
|
|
|
case SIOCGIFMEDIA:
|
|
|
|
s = splnet();
|
|
|
|
error = ifmedia_ioctl(ifp, ifr, &sc->sc_im, cmd);
|
|
|
|
splx(s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
s = splnet();
|
|
|
|
error = ether_ioctl(ifp, cmd, data);
|
|
|
|
splx(s);
|
|
|
|
if (error == ENETRESET)
|
|
|
|
error = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
etherip_set_tunnel(struct ifnet *ifp,
|
|
|
|
struct sockaddr *src,
|
|
|
|
struct sockaddr *dst)
|
|
|
|
{
|
2008-10-25 01:41:04 +04:00
|
|
|
struct etherip_softc *sc = ifp->if_softc;
|
2006-11-23 07:07:07 +03:00
|
|
|
struct etherip_softc *sc2;
|
|
|
|
struct sockaddr *osrc, *odst;
|
|
|
|
int s, error = 0;
|
|
|
|
|
|
|
|
s = splsoftnet();
|
|
|
|
|
|
|
|
LIST_FOREACH(sc2, ðerip_softc_list, etherip_list) {
|
|
|
|
if (sc2 == sc)
|
|
|
|
continue;
|
|
|
|
if (!sc2->sc_dst || !sc2->sc_src)
|
|
|
|
continue;
|
|
|
|
if (sc2->sc_dst->sa_family != dst->sa_family ||
|
|
|
|
sc2->sc_dst->sa_len != dst->sa_len ||
|
|
|
|
sc2->sc_src->sa_family != src->sa_family ||
|
|
|
|
sc2->sc_src->sa_len != src->sa_len)
|
|
|
|
continue;
|
|
|
|
/* can't configure same pair of address onto two tunnels */
|
2009-03-18 18:14:29 +03:00
|
|
|
if (memcmp(sc2->sc_dst, dst, dst->sa_len) == 0 &&
|
|
|
|
memcmp(sc2->sc_src, src, src->sa_len) == 0) {
|
2006-11-23 07:07:07 +03:00
|
|
|
error = EADDRNOTAVAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* XXX both end must be valid? (I mean, not 0.0.0.0) */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sc->sc_si) {
|
2007-10-08 20:18:02 +04:00
|
|
|
softint_disestablish(sc->sc_si);
|
2006-11-23 07:07:07 +03:00
|
|
|
sc->sc_si = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ifp->if_flags &= ~IFF_RUNNING;
|
|
|
|
|
|
|
|
osrc = sc->sc_src; sc->sc_src = NULL;
|
|
|
|
odst = sc->sc_dst; sc->sc_dst = NULL;
|
|
|
|
|
2007-09-16 06:19:44 +04:00
|
|
|
sc->sc_src = sockaddr_dup(src, M_WAITOK);
|
|
|
|
if (osrc)
|
|
|
|
sockaddr_free(osrc);
|
2006-11-23 07:07:07 +03:00
|
|
|
|
2007-09-16 06:19:44 +04:00
|
|
|
sc->sc_dst = sockaddr_dup(dst, M_WAITOK);
|
|
|
|
if (odst)
|
|
|
|
sockaddr_free(odst);
|
2006-11-23 07:07:07 +03:00
|
|
|
|
|
|
|
ifp->if_flags |= IFF_RUNNING;
|
|
|
|
|
2007-10-08 20:18:02 +04:00
|
|
|
sc->sc_si = softint_establish(SOFTINT_NET, etheripintr, sc);
|
2006-11-23 07:07:07 +03:00
|
|
|
if (sc->sc_si == NULL)
|
|
|
|
error = ENOMEM;
|
|
|
|
|
|
|
|
out:
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
return(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
etherip_delete_tunnel(struct ifnet *ifp)
|
|
|
|
{
|
2008-10-25 01:41:04 +04:00
|
|
|
struct etherip_softc *sc = ifp->if_softc;
|
2006-11-23 07:07:07 +03:00
|
|
|
int s;
|
|
|
|
|
|
|
|
s = splsoftnet();
|
|
|
|
|
|
|
|
if (sc->sc_si) {
|
2007-10-08 20:18:02 +04:00
|
|
|
softint_disestablish(sc->sc_si);
|
2006-11-23 07:07:07 +03:00
|
|
|
sc->sc_si = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sc->sc_src) {
|
2007-09-16 06:19:44 +04:00
|
|
|
sockaddr_free(sc->sc_src);
|
2006-11-23 07:07:07 +03:00
|
|
|
sc->sc_src = NULL;
|
|
|
|
}
|
|
|
|
if (sc->sc_dst) {
|
2007-09-16 06:19:44 +04:00
|
|
|
sockaddr_free(sc->sc_dst);
|
2006-11-23 07:07:07 +03:00
|
|
|
sc->sc_dst = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ifp->if_flags &= ~IFF_RUNNING;
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
etherip_init(struct ifnet *ifp)
|
|
|
|
{
|
|
|
|
struct etherip_softc *sc = ifp->if_softc;
|
|
|
|
|
|
|
|
if (sc->sc_si == NULL)
|
2007-10-08 20:18:02 +04:00
|
|
|
sc->sc_si = softint_establish(SOFTINT_NET, etheripintr, sc);
|
2006-11-23 07:07:07 +03:00
|
|
|
|
|
|
|
if (sc->sc_si == NULL)
|
|
|
|
return(ENOMEM);
|
|
|
|
|
|
|
|
ifp->if_flags |= IFF_RUNNING;
|
|
|
|
etherip_start(ifp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
etherip_stop(struct ifnet *ifp, int disable)
|
|
|
|
{
|
|
|
|
ifp->if_flags &= ~IFF_RUNNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
etherip_clone_create(struct if_clone *ifc, int unit)
|
|
|
|
{
|
2008-10-25 01:41:04 +04:00
|
|
|
cfdata_t cf;
|
2006-11-23 07:07:07 +03:00
|
|
|
|
2008-12-17 23:51:31 +03:00
|
|
|
cf = malloc(sizeof(struct cfdata), M_DEVBUF, M_WAITOK);
|
2006-11-23 07:07:07 +03:00
|
|
|
cf->cf_name = etherip_cd.cd_name;
|
|
|
|
cf->cf_atname = etherip_ca.ca_name;
|
|
|
|
cf->cf_unit = unit;
|
|
|
|
cf->cf_fstate = FSTATE_STAR;
|
|
|
|
|
|
|
|
if (config_attach_pseudo(cf) == NULL) {
|
|
|
|
aprint_error("%s%d: unable to attach an instance\n",
|
|
|
|
etherip_cd.cd_name, unit);
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
etherip_clone_destroy(struct ifnet *ifp)
|
|
|
|
{
|
2008-10-25 01:41:04 +04:00
|
|
|
struct etherip_softc *sc = ifp->if_softc;
|
|
|
|
cfdata_t cf = device_cfdata(sc->sc_dev);
|
2006-11-23 07:07:07 +03:00
|
|
|
int error;
|
|
|
|
|
2008-10-25 01:41:04 +04:00
|
|
|
if ((error = config_detach(sc->sc_dev, 0)) != 0)
|
|
|
|
aprint_error_dev(sc->sc_dev, "unable to detach instance\n");
|
2008-12-17 23:51:31 +03:00
|
|
|
free(cf, M_DEVBUF);
|
2006-11-23 07:07:07 +03:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSCTL_SETUP(sysctl_etherip_setup, "sysctl net.link.etherip subtree setup")
|
|
|
|
{
|
|
|
|
const struct sysctlnode *node;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
error = sysctl_createv(clog, 0, NULL, NULL,
|
|
|
|
CTLFLAG_PERMANENT,
|
|
|
|
CTLTYPE_NODE, "net", NULL,
|
|
|
|
NULL, 0, NULL, 0,
|
|
|
|
CTL_NET, CTL_EOL);
|
|
|
|
if (error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
error = sysctl_createv(clog, 0, NULL, NULL,
|
|
|
|
CTLFLAG_PERMANENT,
|
|
|
|
CTLTYPE_NODE, "link", NULL,
|
|
|
|
NULL, 0, NULL, 0,
|
|
|
|
CTL_NET, AF_LINK, CTL_EOL);
|
|
|
|
if (error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
error = sysctl_createv(clog, 0, NULL, &node,
|
|
|
|
CTLFLAG_PERMANENT,
|
|
|
|
CTLTYPE_NODE, "etherip", NULL,
|
|
|
|
NULL, 0, NULL, 0,
|
|
|
|
CTL_NET, AF_LINK, CTL_CREATE, CTL_EOL);
|
|
|
|
if (error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
etherip_node = node->sysctl_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
etherip_sysctl_handler(SYSCTLFN_ARGS)
|
|
|
|
{
|
|
|
|
struct sysctlnode node;
|
|
|
|
struct etherip_softc *sc;
|
|
|
|
struct ifnet *ifp;
|
|
|
|
int error;
|
|
|
|
size_t len;
|
|
|
|
char addr[3 * ETHER_ADDR_LEN];
|
Use malloc(9) for sockaddrs instead of pool(9), and remove dom_sa_pool
and dom_sa_len members from struct domain. Pools of fixed-size
objects are too rigid for sockaddr_dls, whose size can vary over
a wide range.
Return sockaddr_dl to its "historical" size. Now that I'm using
malloc(9) instead of pool(9) to allocate sockaddr_dl, I can create
a sockaddr_dl of any size in the kernel, so expanding sockaddr_dl
is useless.
Avoid using sizeof(struct sockaddr_dl) in the kernel.
Introduce sockaddr_dl_alloc() for allocating & initializing an
arbitrary sockaddr_dl on the heap.
Add an argument, the sockaddr length, to sockaddr_alloc(),
sockaddr_copy(), and sockaddr_dl_setaddr().
Constify: LLADDR() -> CLLADDR().
Where the kernel overwrites LLADDR(), use sockaddr_dl_setaddr(),
instead. Used properly, sockaddr_dl_setaddr() will not overrun
the end of the sockaddr.
2007-08-30 06:17:34 +04:00
|
|
|
char enaddr[ETHER_ADDR_LEN];
|
2006-11-23 07:07:07 +03:00
|
|
|
|
|
|
|
node = *rnode;
|
|
|
|
sc = node.sysctl_data;
|
|
|
|
ifp = &sc->sc_ec.ec_if;
|
2007-08-27 02:59:08 +04:00
|
|
|
(void)ether_snprintf(addr, sizeof(addr), CLLADDR(ifp->if_sadl));
|
2006-11-23 07:07:07 +03:00
|
|
|
node.sysctl_data = addr;
|
|
|
|
error = sysctl_lookup(SYSCTLFN_CALL(&node));
|
|
|
|
if (error || newp == NULL)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
len = strlen(addr);
|
|
|
|
if (len < 11 || len > 17)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* Commit change */
|
2010-05-20 00:41:59 +04:00
|
|
|
if (ether_aton_r(enaddr, sizeof(enaddr), addr) != 0)
|
2006-11-23 07:07:07 +03:00
|
|
|
return EINVAL;
|
|
|
|
|
*** Summary ***
When a link-layer address changes (e.g., ifconfig ex0 link
02:de:ad:be:ef:02 active), send a gratuitous ARP and/or a Neighbor
Advertisement to update the network-/link-layer address bindings
on our LAN peers.
Refuse a change of ethernet address to the address 00:00:00:00:00:00
or to any multicast/broadcast address. (Thanks matt@.)
Reorder ifnet ioctl operations so that driver ioctls may inherit
the functions of their "class"---ether_ioctl(), fddi_ioctl(), et
cetera---and the class ioctls may inherit from the generic ioctl,
ifioctl_common(), but both driver- and class-ioctls may override
the generic behavior. Make network drivers share more code.
Distinguish a "factory" link-layer address from others for the
purposes of both protecting that address from deletion and computing
EUI64.
Return consistent, appropriate error codes from network drivers.
Improve readability. KNF.
*** Details ***
In if_attach(), always initialize the interface ioctl routine,
ifnet->if_ioctl, if the driver has not already initialized it.
Delete if_ioctl == NULL tests everywhere else, because it cannot
happen.
In the ioctl routines of network interfaces, inherit common ioctl
behaviors by calling either ifioctl_common() or whichever ioctl
routine is appropriate for the class of interface---e.g., ether_ioctl()
for ethernets.
Stop (ab)using SIOCSIFADDR and start to use SIOCINITIFADDR. In
the user->kernel interface, SIOCSIFADDR's argument was an ifreq,
but on the protocol->ifnet interface, SIOCSIFADDR's argument was
an ifaddr. That was confusing, and it would work against me as I
make it possible for a network interface to overload most ioctls.
On the protocol->ifnet interface, replace SIOCSIFADDR with
SIOCINITIFADDR. In ifioctl(), return EPERM if userland tries to
invoke SIOCINITIFADDR.
In ifioctl(), give the interface the first shot at handling most
interface ioctls, and give the protocol the second shot, instead
of the other way around. Finally, let compatibility code (COMPAT_OSOCK)
take a shot.
Pull device initialization out of switch statements under
SIOCINITIFADDR. For example, pull ..._init() out of any switch
statement that looks like this:
switch (...->sa_family) {
case ...:
..._init();
...
break;
...
default:
..._init();
...
break;
}
Rewrite many if-else clauses that handle all permutations of IFF_UP
and IFF_RUNNING to use a switch statement,
switch (x & (IFF_UP|IFF_RUNNING)) {
case 0:
...
break;
case IFF_RUNNING:
...
break;
case IFF_UP:
...
break;
case IFF_UP|IFF_RUNNING:
...
break;
}
unifdef lots of code containing #ifdef FreeBSD, #ifdef NetBSD, and
#ifdef SIOCSIFMTU, especially in fwip(4) and in ndis(4).
In ipw(4), remove an if_set_sadl() call that is out of place.
In nfe(4), reuse the jumbo MTU logic in ether_ioctl().
Let ethernets register a callback for setting h/w state such as
promiscuous mode and the multicast filter in accord with a change
in the if_flags: ether_set_ifflags_cb() registers a callback that
returns ENETRESET if the caller should reset the ethernet by calling
if_init(), 0 on success, != 0 on failure. Pull common code from
ex(4), gem(4), nfe(4), sip(4), tlp(4), vge(4) into ether_ioctl(),
and register if_flags callbacks for those drivers.
Return ENOTTY instead of EINVAL for inappropriate ioctls. In
zyd(4), use ENXIO instead of ENOTTY to indicate that the device is
not any longer attached.
Add to if_set_sadl() a boolean 'factory' argument that indicates
whether a link-layer address was assigned by the factory or some
other source. In a comment, recommend using the factory address
for generating an EUI64, and update in6_get_hw_ifid() to prefer a
factory address to any other link-layer address.
Add a routing message, RTM_LLINFO_UPD, that tells protocols to
update the binding of network-layer addresses to link-layer addresses.
Implement this message in IPv4 and IPv6 by sending a gratuitous
ARP or a neighbor advertisement, respectively. Generate RTM_LLINFO_UPD
messages on a change of an interface's link-layer address.
In ether_ioctl(), do not let SIOCALIFADDR set a link-layer address
that is broadcast/multicast or equal to 00:00:00:00:00:00.
Make ether_ioctl() call ifioctl_common() to handle ioctls that it
does not understand.
In gif(4), initialize if_softc and use it, instead of assuming that
the gif_softc and ifp overlap.
Let ifioctl_common() handle SIOCGIFADDR.
Sprinkle rtcache_invariants(), which checks on DIAGNOSTIC kernels
that certain invariants on a struct route are satisfied.
In agr(4), rewrite agr_ioctl_filter() to be a bit more explicit
about the ioctls that we do not allow on an agr(4) member interface.
bzero -> memset. Delete unnecessary casts to void *. Use
sockaddr_in_init() and sockaddr_in6_init(). Compare pointers with
NULL instead of "testing truth". Replace some instances of (type
*)0 with NULL. Change some K&R prototypes to ANSI C, and join
lines.
2008-11-07 03:20:01 +03:00
|
|
|
if_set_sadl(ifp, enaddr, ETHER_ADDR_LEN, false);
|
2006-11-23 07:07:07 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|