xref: /freebsd/sys/net/if_disc.c (revision 47e8d432d5db395e2812c03093b70227e48a1c6d)
1c398230bSWarner Losh /*-
259d8d13fSGarrett Wollman  * Copyright (c) 1982, 1986, 1993
359d8d13fSGarrett Wollman  *	The Regents of the University of California.  All rights reserved.
459d8d13fSGarrett Wollman  *
559d8d13fSGarrett Wollman  * Redistribution and use in source and binary forms, with or without
659d8d13fSGarrett Wollman  * modification, are permitted provided that the following conditions
759d8d13fSGarrett Wollman  * are met:
859d8d13fSGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
959d8d13fSGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
1059d8d13fSGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
1159d8d13fSGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
1259d8d13fSGarrett Wollman  *    documentation and/or other materials provided with the distribution.
1359d8d13fSGarrett Wollman  * 4. Neither the name of the University nor the names of its contributors
1459d8d13fSGarrett Wollman  *    may be used to endorse or promote products derived from this software
1559d8d13fSGarrett Wollman  *    without specific prior written permission.
1659d8d13fSGarrett Wollman  *
1759d8d13fSGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1859d8d13fSGarrett Wollman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1959d8d13fSGarrett Wollman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2059d8d13fSGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2159d8d13fSGarrett Wollman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2259d8d13fSGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2359d8d13fSGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2459d8d13fSGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2559d8d13fSGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2659d8d13fSGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2759d8d13fSGarrett Wollman  * SUCH DAMAGE.
2859d8d13fSGarrett Wollman  *
2959d8d13fSGarrett Wollman  *	From: @(#)if_loop.c	8.1 (Berkeley) 6/10/93
30c3aac50fSPeter Wemm  * $FreeBSD$
3159d8d13fSGarrett Wollman  */
3259d8d13fSGarrett Wollman 
3359d8d13fSGarrett Wollman /*
3459d8d13fSGarrett Wollman  * Discard interface driver for protocol testing and timing.
3559d8d13fSGarrett Wollman  * (Based on the loopback.)
3659d8d13fSGarrett Wollman  */
3759d8d13fSGarrett Wollman 
3859d8d13fSGarrett Wollman #include <sys/param.h>
3959d8d13fSGarrett Wollman #include <sys/systm.h>
4059d8d13fSGarrett Wollman #include <sys/kernel.h>
41c69b7ffeSBrooks Davis #include <sys/malloc.h>
422b120974SPeter Wemm #include <sys/module.h>
4359d8d13fSGarrett Wollman #include <sys/mbuf.h>
4459d8d13fSGarrett Wollman #include <sys/socket.h>
4551a53488SBruce Evans #include <sys/sockio.h>
4659d8d13fSGarrett Wollman 
4759d8d13fSGarrett Wollman #include <net/if.h>
48f889d2efSBrooks Davis #include <net/if_clone.h>
4959d8d13fSGarrett Wollman #include <net/if_types.h>
5059d8d13fSGarrett Wollman #include <net/route.h>
5159d8d13fSGarrett Wollman #include <net/bpf.h>
5259d8d13fSGarrett Wollman 
531d5e9e22SEivind Eklund #include "opt_inet.h"
54cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
5559d8d13fSGarrett Wollman 
5659d8d13fSGarrett Wollman #ifdef TINY_DSMTU
5759d8d13fSGarrett Wollman #define	DSMTU	(1024+512)
5859d8d13fSGarrett Wollman #else
5959d8d13fSGarrett Wollman #define DSMTU	65532
6059d8d13fSGarrett Wollman #endif
6159d8d13fSGarrett Wollman 
62c69b7ffeSBrooks Davis struct disc_softc {
6370e04181SYaroslav Tykhiy 	struct ifnet *sc_ifp;
64c69b7ffeSBrooks Davis };
65c69b7ffeSBrooks Davis 
66eb6bd594SMark Murray static int	discoutput(struct ifnet *, struct mbuf *,
67*47e8d432SGleb Smirnoff 		    const struct sockaddr *, struct route *);
688071913dSRuslan Ermilov static void	discrtrequest(int, struct rtentry *, struct rt_addrinfo *);
69537ad974SEivind Eklund static int	discioctl(struct ifnet *, u_long, caddr_t);
706b7330e2SSam Leffler static int	disc_clone_create(struct if_clone *, int, caddr_t);
71c69b7ffeSBrooks Davis static void	disc_clone_destroy(struct ifnet *);
7259d8d13fSGarrett Wollman 
7342a58907SGleb Smirnoff static const char discname[] = "disc";
7442a58907SGleb Smirnoff static MALLOC_DEFINE(M_DISC, discname, "Discard interface");
75f889d2efSBrooks Davis 
7642a58907SGleb Smirnoff static struct if_clone *disc_cloner;
77c69b7ffeSBrooks Davis 
78c69b7ffeSBrooks Davis static int
796b7330e2SSam Leffler disc_clone_create(struct if_clone *ifc, int unit, caddr_t params)
8059d8d13fSGarrett Wollman {
81c69b7ffeSBrooks Davis 	struct ifnet		*ifp;
82c69b7ffeSBrooks Davis 	struct disc_softc	*sc;
8359d8d13fSGarrett Wollman 
8460323f48SBruce M Simpson 	sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK | M_ZERO);
85fc74a9f9SBrooks Davis 	ifp = sc->sc_ifp = if_alloc(IFT_LOOP);
86fc74a9f9SBrooks Davis 	if (ifp == NULL) {
87fc74a9f9SBrooks Davis 		free(sc, M_DISC);
88fc74a9f9SBrooks Davis 		return (ENOSPC);
89fc74a9f9SBrooks Davis 	}
90c69b7ffeSBrooks Davis 
91c69b7ffeSBrooks Davis 	ifp->if_softc = sc;
9242a58907SGleb Smirnoff 	if_initname(ifp, discname, unit);
9359d8d13fSGarrett Wollman 	ifp->if_mtu = DSMTU;
9468b11e74SYaroslav Tykhiy 	/*
9568b11e74SYaroslav Tykhiy 	 * IFF_LOOPBACK should not be removed from disc's flags because
9668b11e74SYaroslav Tykhiy 	 * it controls what PF-specific routes are magically added when
9768b11e74SYaroslav Tykhiy 	 * a network address is assigned to the interface.  Things just
9868b11e74SYaroslav Tykhiy 	 * won't work as intended w/o such routes because the output
9968b11e74SYaroslav Tykhiy 	 * interface selection for a packet is totally route-driven.
10068b11e74SYaroslav Tykhiy 	 * A valid alternative to IFF_LOOPBACK can be IFF_BROADCAST or
10168b11e74SYaroslav Tykhiy 	 * IFF_POINTOPOINT, but it would result in different properties
10268b11e74SYaroslav Tykhiy 	 * of the interface.
10368b11e74SYaroslav Tykhiy 	 */
10459d8d13fSGarrett Wollman 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
105d314617eSGleb Smirnoff 	ifp->if_drv_flags = IFF_DRV_RUNNING;
106537ad974SEivind Eklund 	ifp->if_ioctl = discioctl;
107537ad974SEivind Eklund 	ifp->if_output = discoutput;
10859d8d13fSGarrett Wollman 	ifp->if_hdrlen = 0;
10959d8d13fSGarrett Wollman 	ifp->if_addrlen = 0;
110422fd76fSPoul-Henning Kamp 	ifp->if_snd.ifq_maxlen = 20;
11159d8d13fSGarrett Wollman 	if_attach(ifp);
11201399f34SDavid Malone 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
113c69b7ffeSBrooks Davis 
114c69b7ffeSBrooks Davis 	return (0);
115c69b7ffeSBrooks Davis }
116c69b7ffeSBrooks Davis 
117c69b7ffeSBrooks Davis static void
118c69b7ffeSBrooks Davis disc_clone_destroy(struct ifnet *ifp)
119c69b7ffeSBrooks Davis {
120c69b7ffeSBrooks Davis 	struct disc_softc	*sc;
121c69b7ffeSBrooks Davis 
122c69b7ffeSBrooks Davis 	sc = ifp->if_softc;
123c69b7ffeSBrooks Davis 
124febd0759SAndrew Thompson 	bpfdetach(ifp);
125febd0759SAndrew Thompson 	if_detach(ifp);
126febd0759SAndrew Thompson 	if_free(ifp);
127febd0759SAndrew Thompson 
128febd0759SAndrew Thompson 	free(sc, M_DISC);
12959d8d13fSGarrett Wollman }
13059d8d13fSGarrett Wollman 
13159d8d13fSGarrett Wollman static int
1322b120974SPeter Wemm disc_modevent(module_t mod, int type, void *data)
1332b120974SPeter Wemm {
134d6e2616aSRobert Watson 
1352b120974SPeter Wemm 	switch (type) {
1362b120974SPeter Wemm 	case MOD_LOAD:
13742a58907SGleb Smirnoff 		disc_cloner = if_clone_simple(discname, disc_clone_create,
13842a58907SGleb Smirnoff 		    disc_clone_destroy, 0);
1392b120974SPeter Wemm 		break;
1402b120974SPeter Wemm 	case MOD_UNLOAD:
14142a58907SGleb Smirnoff 		if_clone_detach(disc_cloner);
142c69b7ffeSBrooks Davis 		break;
1433e019deaSPoul-Henning Kamp 	default:
1443e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
1452b120974SPeter Wemm 	}
146832cb4aeSBruce M Simpson 	return (0);
1472b120974SPeter Wemm }
1482b120974SPeter Wemm 
1492b120974SPeter Wemm static moduledata_t disc_mod = {
1502b120974SPeter Wemm 	"if_disc",
1512b120974SPeter Wemm 	disc_modevent,
1522b120974SPeter Wemm 	NULL
1532b120974SPeter Wemm };
1542b120974SPeter Wemm 
1552b120974SPeter Wemm DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1562b120974SPeter Wemm 
1572b120974SPeter Wemm static int
158*47e8d432SGleb Smirnoff discoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
159279aa3d4SKip Macy     struct route *ro)
16059d8d13fSGarrett Wollman {
16101399f34SDavid Malone 	u_int32_t af;
162832cb4aeSBruce M Simpson 
163fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
164832cb4aeSBruce M Simpson 
16501399f34SDavid Malone 	/* BPF writes need to be handled specially. */
166*47e8d432SGleb Smirnoff 	if (dst->sa_family == AF_UNSPEC)
16701399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
168*47e8d432SGleb Smirnoff 	else
169*47e8d432SGleb Smirnoff 		af = dst->sa_family;
170963e4c2aSGarrett Wollman 
171*47e8d432SGleb Smirnoff 	if (bpf_peers_present(ifp->if_bpf))
172437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
173*47e8d432SGleb Smirnoff 
17459d8d13fSGarrett Wollman 	m->m_pkthdr.rcvif = ifp;
17559d8d13fSGarrett Wollman 
17659d8d13fSGarrett Wollman 	ifp->if_opackets++;
17759d8d13fSGarrett Wollman 	ifp->if_obytes += m->m_pkthdr.len;
17859d8d13fSGarrett Wollman 
17959d8d13fSGarrett Wollman 	m_freem(m);
180832cb4aeSBruce M Simpson 	return (0);
18159d8d13fSGarrett Wollman }
18259d8d13fSGarrett Wollman 
18359d8d13fSGarrett Wollman /* ARGSUSED */
18459d8d13fSGarrett Wollman static void
1858071913dSRuslan Ermilov discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
18659d8d13fSGarrett Wollman {
187d1dd20beSSam Leffler 	RT_LOCK_ASSERT(rt);
18859d8d13fSGarrett Wollman 	rt->rt_rmx.rmx_mtu = DSMTU;
18959d8d13fSGarrett Wollman }
19059d8d13fSGarrett Wollman 
19159d8d13fSGarrett Wollman /*
19259d8d13fSGarrett Wollman  * Process an ioctl request.
19359d8d13fSGarrett Wollman  */
19488e2f526SBruce Evans static int
195eb6bd594SMark Murray discioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
19659d8d13fSGarrett Wollman {
197eb6bd594SMark Murray 	struct ifaddr *ifa;
198eb6bd594SMark Murray 	struct ifreq *ifr = (struct ifreq *)data;
199eb6bd594SMark Murray 	int error = 0;
20059d8d13fSGarrett Wollman 
20159d8d13fSGarrett Wollman 	switch (cmd) {
20259d8d13fSGarrett Wollman 
20359d8d13fSGarrett Wollman 	case SIOCSIFADDR:
20459d8d13fSGarrett Wollman 		ifp->if_flags |= IFF_UP;
20559d8d13fSGarrett Wollman 		ifa = (struct ifaddr *)data;
20659d8d13fSGarrett Wollman 		if (ifa != 0)
207537ad974SEivind Eklund 			ifa->ifa_rtrequest = discrtrequest;
20859d8d13fSGarrett Wollman 		/*
20959d8d13fSGarrett Wollman 		 * Everything else is done at a higher level.
21059d8d13fSGarrett Wollman 		 */
21159d8d13fSGarrett Wollman 		break;
21259d8d13fSGarrett Wollman 
21359d8d13fSGarrett Wollman 	case SIOCADDMULTI:
21459d8d13fSGarrett Wollman 	case SIOCDELMULTI:
21559d8d13fSGarrett Wollman 		if (ifr == 0) {
21659d8d13fSGarrett Wollman 			error = EAFNOSUPPORT;		/* XXX */
21759d8d13fSGarrett Wollman 			break;
21859d8d13fSGarrett Wollman 		}
21959d8d13fSGarrett Wollman 		switch (ifr->ifr_addr.sa_family) {
22059d8d13fSGarrett Wollman 
22159d8d13fSGarrett Wollman #ifdef INET
22259d8d13fSGarrett Wollman 		case AF_INET:
22359d8d13fSGarrett Wollman 			break;
22459d8d13fSGarrett Wollman #endif
225cfa1ca9dSYoshinobu Inoue #ifdef INET6
226cfa1ca9dSYoshinobu Inoue 		case AF_INET6:
227cfa1ca9dSYoshinobu Inoue 			break;
228cfa1ca9dSYoshinobu Inoue #endif
22959d8d13fSGarrett Wollman 
23059d8d13fSGarrett Wollman 		default:
23159d8d13fSGarrett Wollman 			error = EAFNOSUPPORT;
23259d8d13fSGarrett Wollman 			break;
23359d8d13fSGarrett Wollman 		}
23459d8d13fSGarrett Wollman 		break;
23559d8d13fSGarrett Wollman 
23659d8d13fSGarrett Wollman 	case SIOCSIFMTU:
23759d8d13fSGarrett Wollman 		ifp->if_mtu = ifr->ifr_mtu;
23859d8d13fSGarrett Wollman 		break;
23959d8d13fSGarrett Wollman 
24059d8d13fSGarrett Wollman 	default:
24159d8d13fSGarrett Wollman 		error = EINVAL;
24259d8d13fSGarrett Wollman 	}
24364b15424SJonathan Lemon 	return (error);
24459d8d13fSGarrett Wollman }
245