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 *);
|
|
|
|
|
|
|
|
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
|
|
|
|
2002-05-25 20:20:35 +00:00
|
|
|
sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK);
|
|
|
|
bzero(sc, sizeof(struct disc_softc));
|
|
|
|
|
|
|
|
ifp = &sc->sc_if;
|
|
|
|
|
|
|
|
ifp->if_softc = sc;
|
|
|
|
ifp->if_name = DISCNAME;
|
|
|
|
ifp->if_unit = 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));
|
2002-05-25 20:20:35 +00:00
|
|
|
LIST_INSERT_HEAD(&disc_softc_list, sc, sc_list);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
disc_clone_destroy(struct ifnet *ifp)
|
|
|
|
{
|
|
|
|
struct disc_softc *sc;
|
|
|
|
|
|
|
|
sc = ifp->if_softc;
|
|
|
|
|
|
|
|
LIST_REMOVE(sc, sc_list);
|
|
|
|
bpfdetach(ifp);
|
|
|
|
if_detach(ifp);
|
|
|
|
|
|
|
|
free(sc, M_DISC);
|
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)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case MOD_LOAD:
|
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);
|
|
|
|
|
|
|
|
while (!LIST_EMPTY(&disc_softc_list))
|
|
|
|
disc_clone_destroy(
|
|
|
|
&LIST_FIRST(&disc_softc_list)->sc_if);
|
|
|
|
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
|
|
|
{
|
|
|
|
if ((m->m_flags & M_PKTHDR) == 0)
|
1998-12-14 01:59:16 +00:00
|
|
|
panic("discoutput no HDR");
|
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
|
|
|
/*
|
|
|
|
* We need to prepend the address family as
|
|
|
|
* a four byte field. Cons up a dummy header
|
|
|
|
* to pacify bpf. This is safe because bpf
|
|
|
|
* will only read from the mbuf (i.e., it won't
|
|
|
|
* try to free it or keep a pointer a to it).
|
|
|
|
*/
|
|
|
|
struct mbuf m0;
|
|
|
|
u_int af = dst->sa_family;
|
|
|
|
|
|
|
|
m0.m_next = m;
|
|
|
|
m0.m_len = 4;
|
|
|
|
m0.m_data = (char *)⁡
|
1995-05-30 08:16:23 +00:00
|
|
|
|
2002-11-15 00:00:15 +00:00
|
|
|
BPF_MTAP(ifp, &m0);
|
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
|
|
|
{
|
|
|
|
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
|
|
|
}
|