if_disc.c revision 7117
199461Sobrien/*
299461Sobrien * Copyright (c) 1982, 1986, 1993
399461Sobrien *	The Regents of the University of California.  All rights reserved.
499461Sobrien *
599461Sobrien * Redistribution and use in source and binary forms, with or without
699461Sobrien * modification, are permitted provided that the following conditions
799461Sobrien * are met:
899461Sobrien * 1. Redistributions of source code must retain the above copyright
999461Sobrien *    notice, this list of conditions and the following disclaimer.
1099461Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1199461Sobrien *    notice, this list of conditions and the following disclaimer in the
1299461Sobrien *    documentation and/or other materials provided with the distribution.
1399461Sobrien * 3. All advertising materials mentioning features or use of this software
1499461Sobrien *    must display the following acknowledgement:
1599461Sobrien *	This product includes software developed by the University of
1699461Sobrien *	California, Berkeley and its contributors.
1799461Sobrien * 4. Neither the name of the University nor the names of its contributors
1899461Sobrien *    may be used to endorse or promote products derived from this software
1999461Sobrien *    without specific prior written permission.
2099461Sobrien *
2199461Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2299461Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2399461Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2499461Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2599461Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2699461Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2799461Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2899461Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2999461Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3099461Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3199461Sobrien * SUCH DAMAGE.
3299461Sobrien *
3399461Sobrien *	From: @(#)if_loop.c	8.1 (Berkeley) 6/10/93
3499461Sobrien *	$Id: if_disc.c,v 1.1 1994/12/22 21:00:05 wollman Exp $
3599461Sobrien */
3699461Sobrien
3799461Sobrien/*
3899461Sobrien * Discard interface driver for protocol testing and timing.
3999461Sobrien * (Based on the loopback.)
4099461Sobrien */
4199461Sobrien
4299461Sobrien#include <sys/param.h>
4399461Sobrien#include <sys/systm.h>
4499461Sobrien#include <sys/kernel.h>
4599461Sobrien#include <sys/mbuf.h>
4699461Sobrien#include <sys/socket.h>
4799461Sobrien#include <sys/errno.h>
4899461Sobrien#include <sys/ioctl.h>
4999461Sobrien#include <sys/time.h>
5099461Sobrien#include <machine/cpu.h>
5199461Sobrien
5299461Sobrien#include <net/if.h>
5399461Sobrien#include <net/if_types.h>
5499461Sobrien#include <net/netisr.h>
5599461Sobrien#include <net/route.h>
5699461Sobrien#include <net/bpf.h>
5799461Sobrien
5899461Sobrien#ifdef	INET
5999461Sobrien#include <netinet/in.h>
6099461Sobrien#include <netinet/in_systm.h>
6199461Sobrien#include <netinet/in_var.h>
6299461Sobrien#include <netinet/ip.h>
6399461Sobrien#endif
6499461Sobrien
6599461Sobrien#ifdef NS
6699461Sobrien#include <netns/ns.h>
6799461Sobrien#include <netns/ns_if.h>
6899461Sobrien#endif
6999461Sobrien
7099461Sobrien#ifdef ISO
7199461Sobrien#include <netiso/iso.h>
7299461Sobrien#include <netiso/iso_var.h>
7399461Sobrien#endif
7499461Sobrien
7599461Sobrien#include "bpfilter.h"
7699461Sobrien
7799461Sobrien#ifdef TINY_DSMTU
7899461Sobrien#define	DSMTU	(1024+512)
7999461Sobrien#else
8099461Sobrien#define DSMTU	65532
8199461Sobrien#endif
8299461Sobrien
8399461Sobrienstatic struct	ifnet dsif;
8499461Sobrienstatic int dsoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
8599461Sobrien		    struct rtentry *);
8699461Sobrienstatic int dsioctl(struct ifnet *, int, caddr_t);
8799461Sobrien
8899461Sobrien/* ARGSUSED */
8999461Sobrienstatic void
9099461Sobriendiscattach(void)
9199461Sobrien{
9299461Sobrien	register struct ifnet *ifp = &dsif;
9399461Sobrien
9499461Sobrien	ifp->if_name = "ds";
9599461Sobrien	ifp->if_mtu = DSMTU;
9699461Sobrien	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
9799461Sobrien	ifp->if_ioctl = dsioctl;
9899461Sobrien	ifp->if_output = dsoutput;
9999461Sobrien	ifp->if_type = IFT_LOOP;
10099461Sobrien	ifp->if_hdrlen = 0;
10199461Sobrien	ifp->if_addrlen = 0;
10299461Sobrien	if_attach(ifp);
10399461Sobrien#if NBPFILTER > 0
10499461Sobrien	bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int));
10599461Sobrien#endif
10699461Sobrien}
10799461Sobrien
10899461SobrienPSEUDO_SET(discattach);
10999461Sobrien
11099461Sobrienstatic int
11199461Sobriendsoutput(ifp, m, dst, rt)
11299461Sobrien	struct ifnet *ifp;
11399461Sobrien	register struct mbuf *m;
11499461Sobrien	struct sockaddr *dst;
11599461Sobrien	register struct rtentry *rt;
116{
117	int s, isr;
118	register struct ifqueue *ifq = 0;
119
120	if ((m->m_flags & M_PKTHDR) == 0)
121		panic("dsoutput no HDR");
122	ifp->if_lastchange = time;
123#if NBPFILTER > 0
124	if (dsif.if_bpf) {
125		/*
126		 * We need to prepend the address family as
127		 * a four byte field.  Cons up a dummy header
128		 * to pacify bpf.  This is safe because bpf
129		 * will only read from the mbuf (i.e., it won't
130		 * try to free it or keep a pointer a to it).
131		 */
132		struct mbuf m0;
133		u_int af = dst->sa_family;
134
135		m0.m_next = m;
136		m0.m_len = 4;
137		m0.m_data = (char *)&af;
138
139		bpf_mtap(dsif.if_bpf, &m0);
140	}
141#endif
142	m->m_pkthdr.rcvif = ifp;
143
144	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
145		m_freem(m);
146		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
147		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
148	}
149	ifp->if_opackets++;
150	ifp->if_obytes += m->m_pkthdr.len;
151
152	m_freem(m);
153	return 0;
154}
155
156/* ARGSUSED */
157static void
158dsrtrequest(cmd, rt, sa)
159	int cmd;
160	struct rtentry *rt;
161	struct sockaddr *sa;
162{
163	if (rt)
164		rt->rt_rmx.rmx_mtu = DSMTU;
165}
166
167/*
168 * Process an ioctl request.
169 */
170/* ARGSUSED */
171int
172dsioctl(ifp, cmd, data)
173	register struct ifnet *ifp;
174	int cmd;
175	caddr_t data;
176{
177	register struct ifaddr *ifa;
178	register struct ifreq *ifr = (struct ifreq *)data;
179	register int error = 0;
180
181	switch (cmd) {
182
183	case SIOCSIFADDR:
184		ifp->if_flags |= IFF_UP;
185		ifa = (struct ifaddr *)data;
186		if (ifa != 0)
187			ifa->ifa_rtrequest = dsrtrequest;
188		/*
189		 * Everything else is done at a higher level.
190		 */
191		break;
192
193	case SIOCADDMULTI:
194	case SIOCDELMULTI:
195		if (ifr == 0) {
196			error = EAFNOSUPPORT;		/* XXX */
197			break;
198		}
199		switch (ifr->ifr_addr.sa_family) {
200
201#ifdef INET
202		case AF_INET:
203			break;
204#endif
205
206		default:
207			error = EAFNOSUPPORT;
208			break;
209		}
210		break;
211
212	case SIOCSIFMTU:
213		ifp->if_mtu = ifr->ifr_mtu;
214		break;
215
216	default:
217		error = EINVAL;
218	}
219	return (error);
220}
221