1994-12-22 21:00:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1982, 1986, 1993
|
|
|
|
* The Regents of the University of California. 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. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University 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 REGENTS 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 REGENTS 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.
|
|
|
|
*
|
|
|
|
* From: @(#)if_loop.c 8.1 (Berkeley) 6/10/93
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1994-12-22 21:00:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Discard interface driver for protocol testing and timing.
|
|
|
|
* (Based on the loopback.)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
2002-05-25 20:20:35 +00:00
|
|
|
#include <sys/malloc.h>
|
2001-01-31 07:58:58 +00:00
|
|
|
#include <sys/module.h>
|
1994-12-22 21:00:05 +00:00
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
1997-03-24 11:33:46 +00:00
|
|
|
#include <sys/sockio.h>
|
1994-12-22 21:00:05 +00:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_types.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/bpf.h>
|
|
|
|
|
1998-01-08 23:42:31 +00:00
|
|
|
#include "opt_inet.h"
|
1999-12-07 17:39:16 +00:00
|
|
|
#include "opt_inet6.h"
|
1994-12-22 21:00:05 +00:00
|
|
|
|
|
|
|
#ifdef TINY_DSMTU
|
|
|
|
#define DSMTU (1024+512)
|
|
|
|
#else
|
|
|
|
#define DSMTU 65532
|
|
|
|
#endif
|
|
|
|
|
2002-05-25 20:20:35 +00:00
|
|
|
#define DISCNAME "disc"
|
1995-08-30 00:33:22 +00:00
|
|
|
|
2002-05-25 20:20:35 +00:00
|
|
|
struct disc_softc {
|
|
|
|
struct ifnet sc_if; /* must be first */
|
|
|
|
LIST_ENTRY(disc_softc) sc_list;
|
|
|
|
};
|
1994-12-22 21:00:05 +00:00
|
|
|
|
2002-05-25 20:20:35 +00:00
|
|
|
static int discoutput(struct ifnet *, struct mbuf *,
|
|
|
|
struct sockaddr *, struct rtentry *);
|
|
|
|
static void discrtrequest(int, struct rtentry *, struct rt_addrinfo *);
|
|
|
|
static int discioctl(struct ifnet *, u_long, caddr_t);
|
|
|
|
static int disc_clone_create(struct if_clone *, int);
|
|
|
|
static void disc_clone_destroy(struct ifnet *);
|
|
|
|
|
2004-03-09 16:31:19 +00:00
|
|
|
static struct mtx disc_mtx;
|
2002-05-25 20:20:35 +00:00
|
|
|
static MALLOC_DEFINE(M_DISC, DISCNAME, "Discard interface");
|
|
|
|
static LIST_HEAD(, disc_softc) disc_softc_list;
|
|
|
|
static struct if_clone disc_cloner = IF_CLONE_INITIALIZER(DISCNAME,
|
|
|
|
disc_clone_create, disc_clone_destroy, 0, IF_MAXUNIT);
|
|
|
|
|
|
|
|
static int
|
|
|
|
disc_clone_create(struct if_clone *ifc, int unit)
|
1994-12-22 21:00:05 +00:00
|
|
|
{
|
2002-05-25 20:20:35 +00:00
|
|
|
struct ifnet *ifp;
|
|
|
|
struct disc_softc *sc;
|
1994-12-22 21:00:05 +00:00
|
|
|
|
2003-02-19 05:47:46 +00:00
|
|
|
sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK);
|
2002-05-25 20:20:35 +00:00
|
|
|
bzero(sc, sizeof(struct disc_softc));
|
|
|
|
|
|
|
|
ifp = &sc->sc_if;
|
|
|
|
|
|
|
|
ifp->if_softc = sc;
|
2003-10-31 18:32:15 +00:00
|
|
|
if_initname(ifp, ifc->ifc_name, unit);
|
1994-12-22 21:00:05 +00:00
|
|
|
ifp->if_mtu = DSMTU;
|
|
|
|
ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
|
1998-12-14 01:59:16 +00:00
|
|
|
ifp->if_ioctl = discioctl;
|
|
|
|
ifp->if_output = discoutput;
|
1994-12-22 21:00:05 +00:00
|
|
|
ifp->if_type = IFT_LOOP;
|
|
|
|
ifp->if_hdrlen = 0;
|
|
|
|
ifp->if_addrlen = 0;
|
1999-11-12 19:30:08 +00:00
|
|
|
ifp->if_snd.ifq_maxlen = 20;
|
1994-12-22 21:00:05 +00:00
|
|
|
if_attach(ifp);
|
1996-02-06 18:51:28 +00:00
|
|
|
bpfattach(ifp, DLT_NULL, sizeof(u_int));
|
2004-03-09 16:31:19 +00:00
|
|
|
mtx_lock(&disc_mtx);
|
2002-05-25 20:20:35 +00:00
|
|
|
LIST_INSERT_HEAD(&disc_softc_list, sc, sc_list);
|
2004-03-09 16:31:19 +00:00
|
|
|
mtx_unlock(&disc_mtx);
|
2002-05-25 20:20:35 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-03-09 16:31:19 +00:00
|
|
|
static void
|
|
|
|
disc_destroy(struct disc_softc *sc)
|
|
|
|
{
|
|
|
|
|
|
|
|
bpfdetach(&sc->sc_if);
|
|
|
|
if_detach(&sc->sc_if);
|
|
|
|
|
|
|
|
free(sc, M_DISC);
|
|
|
|
}
|
|
|
|
|
2002-05-25 20:20:35 +00:00
|
|
|
static void
|
|
|
|
disc_clone_destroy(struct ifnet *ifp)
|
|
|
|
{
|
|
|
|
struct disc_softc *sc;
|
|
|
|
|
|
|
|
sc = ifp->if_softc;
|
2004-03-09 16:31:19 +00:00
|
|
|
mtx_lock(&disc_mtx);
|
2002-05-25 20:20:35 +00:00
|
|
|
LIST_REMOVE(sc, sc_list);
|
2004-03-09 16:31:19 +00:00
|
|
|
mtx_unlock(&disc_mtx);
|
2002-05-25 20:20:35 +00:00
|
|
|
|
2004-03-09 16:31:19 +00:00
|
|
|
disc_destroy(sc);
|
1994-12-22 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
2001-01-31 07:58:58 +00:00
|
|
|
static int
|
|
|
|
disc_modevent(module_t mod, int type, void *data)
|
2004-03-09 16:31:19 +00:00
|
|
|
{
|
|
|
|
struct disc_softc *sc;
|
|
|
|
|
2001-01-31 07:58:58 +00:00
|
|
|
switch (type) {
|
|
|
|
case MOD_LOAD:
|
2004-03-09 16:31:19 +00:00
|
|
|
mtx_init(&disc_mtx, "disc_mtx", NULL, MTX_DEF);
|
2002-05-25 20:20:35 +00:00
|
|
|
LIST_INIT(&disc_softc_list);
|
|
|
|
if_clone_attach(&disc_cloner);
|
2001-01-31 07:58:58 +00:00
|
|
|
break;
|
|
|
|
case MOD_UNLOAD:
|
2002-05-25 20:20:35 +00:00
|
|
|
if_clone_detach(&disc_cloner);
|
|
|
|
|
2004-03-09 16:31:19 +00:00
|
|
|
mtx_lock(&disc_mtx);
|
|
|
|
while ((sc = LIST_FIRST(&disc_softc_list)) != NULL) {
|
|
|
|
LIST_REMOVE(sc, sc_list);
|
|
|
|
mtx_unlock(&disc_mtx);
|
|
|
|
disc_destroy(sc);
|
|
|
|
mtx_lock(&disc_mtx);
|
|
|
|
}
|
|
|
|
mtx_unlock(&disc_mtx);
|
|
|
|
mtx_destroy(&disc_mtx);
|
2002-05-25 20:20:35 +00:00
|
|
|
break;
|
2001-01-31 07:58:58 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static moduledata_t disc_mod = {
|
|
|
|
"if_disc",
|
|
|
|
disc_modevent,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
|
|
|
|
|
1994-12-22 21:00:05 +00:00
|
|
|
static int
|
2001-06-16 10:47:34 +00:00
|
|
|
discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
|
|
|
|
struct rtentry *rt)
|
1994-12-22 21:00:05 +00:00
|
|
|
{
|
2003-04-08 14:25:47 +00:00
|
|
|
M_ASSERTPKTHDR(m);
|
1995-09-22 17:57:48 +00:00
|
|
|
/* BPF write needs to be handled specially */
|
|
|
|
if (dst->sa_family == AF_UNSPEC) {
|
|
|
|
dst->sa_family = *(mtod(m, int *));
|
|
|
|
m->m_len -= sizeof(int);
|
|
|
|
m->m_pkthdr.len -= sizeof(int);
|
|
|
|
m->m_data += sizeof(int);
|
|
|
|
}
|
|
|
|
|
2002-05-25 20:20:35 +00:00
|
|
|
if (ifp->if_bpf) {
|
1994-12-22 21:00:05 +00:00
|
|
|
u_int af = dst->sa_family;
|
2003-12-28 03:56:00 +00:00
|
|
|
bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
|
1994-12-22 21:00:05 +00:00
|
|
|
}
|
|
|
|
m->m_pkthdr.rcvif = ifp;
|
|
|
|
|
|
|
|
ifp->if_opackets++;
|
|
|
|
ifp->if_obytes += m->m_pkthdr.len;
|
|
|
|
|
|
|
|
m_freem(m);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
static void
|
2001-10-17 18:07:05 +00:00
|
|
|
discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
|
1994-12-22 21:00:05 +00:00
|
|
|
{
|
2003-10-04 03:44:50 +00:00
|
|
|
RT_LOCK_ASSERT(rt);
|
|
|
|
|
1994-12-22 21:00:05 +00:00
|
|
|
if (rt)
|
|
|
|
rt->rt_rmx.rmx_mtu = DSMTU;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process an ioctl request.
|
|
|
|
*/
|
1995-12-03 19:08:55 +00:00
|
|
|
static int
|
2001-06-16 10:47:34 +00:00
|
|
|
discioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
|
1994-12-22 21:00:05 +00:00
|
|
|
{
|
2001-06-16 10:47:34 +00:00
|
|
|
struct ifaddr *ifa;
|
|
|
|
struct ifreq *ifr = (struct ifreq *)data;
|
|
|
|
int error = 0;
|
1994-12-22 21:00:05 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
|
|
|
|
case SIOCSIFADDR:
|
|
|
|
ifp->if_flags |= IFF_UP;
|
|
|
|
ifa = (struct ifaddr *)data;
|
|
|
|
if (ifa != 0)
|
1998-12-14 01:59:16 +00:00
|
|
|
ifa->ifa_rtrequest = discrtrequest;
|
1994-12-22 21:00:05 +00:00
|
|
|
/*
|
|
|
|
* Everything else is done at a higher level.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIOCADDMULTI:
|
|
|
|
case SIOCDELMULTI:
|
|
|
|
if (ifr == 0) {
|
|
|
|
error = EAFNOSUPPORT; /* XXX */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (ifr->ifr_addr.sa_family) {
|
|
|
|
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
|
|
|
break;
|
|
|
|
#endif
|
1999-12-07 17:39:16 +00:00
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
break;
|
|
|
|
#endif
|
1994-12-22 21:00:05 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
error = EAFNOSUPPORT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIOCSIFMTU:
|
|
|
|
ifp->if_mtu = ifr->ifr_mtu;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
}
|
2001-12-14 19:28:06 +00:00
|
|
|
return (error);
|
1994-12-22 21:00:05 +00:00
|
|
|
}
|