if_disc.c revision 10957
11556Srgrimes/*
21556Srgrimes * Copyright (c) 1982, 1986, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 3. All advertising materials mentioning features or use of this software
141556Srgrimes *    must display the following acknowledgement:
151556Srgrimes *	This product includes software developed by the University of
161556Srgrimes *	California, Berkeley and its contributors.
171556Srgrimes * 4. Neither the name of the University nor the names of its contributors
181556Srgrimes *    may be used to endorse or promote products derived from this software
191556Srgrimes *    without specific prior written permission.
201556Srgrimes *
211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311556Srgrimes * SUCH DAMAGE.
3217987Speter *
3350471Speter *	From: @(#)if_loop.c	8.1 (Berkeley) 6/10/93
341556Srgrimes *	$Id: if_disc.c,v 1.6 1995/09/09 18:10:21 davidg Exp $
351556Srgrimes */
361556Srgrimes
371556Srgrimes/*
381556Srgrimes * Discard interface driver for protocol testing and timing.
391556Srgrimes * (Based on the loopback.)
401556Srgrimes */
411556Srgrimes
421556Srgrimes#include <sys/param.h>
431556Srgrimes#include <sys/systm.h>
441556Srgrimes#include <sys/kernel.h>
451556Srgrimes#include <sys/mbuf.h>
461556Srgrimes#include <sys/socket.h>
471556Srgrimes#include <sys/errno.h>
481556Srgrimes#include <sys/ioctl.h>
491556Srgrimes#include <sys/time.h>
501556Srgrimes#include <machine/cpu.h>
511556Srgrimes
521556Srgrimes#include <net/if.h>
531556Srgrimes#include <net/if_types.h>
54214512Sjilles#include <net/netisr.h>
55214512Sjilles#include <net/route.h>
561556Srgrimes#include <net/bpf.h>
571556Srgrimes
581556Srgrimes#ifdef	INET
5990111Simp#include <netinet/in.h>
6090111Simp#include <netinet/in_systm.h>
61200956Sjilles#include <netinet/in_var.h>
62#include <netinet/ip.h>
63#endif
64
65#ifdef NS
66#include <netns/ns.h>
67#include <netns/ns_if.h>
68#endif
69
70#ifdef ISO
71#include <netiso/iso.h>
72#include <netiso/iso_var.h>
73#endif
74
75#include "bpfilter.h"
76
77#ifdef TINY_DSMTU
78#define	DSMTU	(1024+512)
79#else
80#define DSMTU	65532
81#endif
82
83static void discattach __P((void *udata));
84PSEUDO_SET(discattach, if_disc);
85
86static struct	ifnet dsif;
87static int dsoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
88		    struct rtentry *);
89static int dsioctl(struct ifnet *, int, caddr_t);
90
91/* ARGSUSED */
92static void
93discattach(udata)
94	void *udata;
95{
96	register struct ifnet *ifp = &dsif;
97
98	ifp->if_name = "ds";
99	ifp->if_mtu = DSMTU;
100	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
101	ifp->if_ioctl = dsioctl;
102	ifp->if_output = dsoutput;
103	ifp->if_type = IFT_LOOP;
104	ifp->if_hdrlen = 0;
105	ifp->if_addrlen = 0;
106	if_attach(ifp);
107#if NBPFILTER > 0
108	bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int));
109#endif
110}
111
112static int
113dsoutput(ifp, m, dst, rt)
114	struct ifnet *ifp;
115	register struct mbuf *m;
116	struct sockaddr *dst;
117	register struct rtentry *rt;
118{
119	int s, isr;
120	register struct ifqueue *ifq = 0;
121
122	if ((m->m_flags & M_PKTHDR) == 0)
123		panic("dsoutput no HDR");
124	ifp->if_lastchange = time;
125#if NBPFILTER > 0
126	/* BPF write needs to be handled specially */
127	if (dst->sa_family == AF_UNSPEC) {
128		dst->sa_family = *(mtod(m, int *));
129		m->m_len -= sizeof(int);
130		m->m_pkthdr.len -= sizeof(int);
131		m->m_data += sizeof(int);
132	}
133
134	if (dsif.if_bpf) {
135		/*
136		 * We need to prepend the address family as
137		 * a four byte field.  Cons up a dummy header
138		 * to pacify bpf.  This is safe because bpf
139		 * will only read from the mbuf (i.e., it won't
140		 * try to free it or keep a pointer a to it).
141		 */
142		struct mbuf m0;
143		u_int af = dst->sa_family;
144
145		m0.m_next = m;
146		m0.m_len = 4;
147		m0.m_data = (char *)&af;
148
149		bpf_mtap(dsif.if_bpf, &m0);
150	}
151#endif
152	m->m_pkthdr.rcvif = ifp;
153
154	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
155		m_freem(m);
156		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
157		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
158	}
159	ifp->if_opackets++;
160	ifp->if_obytes += m->m_pkthdr.len;
161
162	m_freem(m);
163	return 0;
164}
165
166/* ARGSUSED */
167static void
168dsrtrequest(cmd, rt, sa)
169	int cmd;
170	struct rtentry *rt;
171	struct sockaddr *sa;
172{
173	if (rt)
174		rt->rt_rmx.rmx_mtu = DSMTU;
175}
176
177/*
178 * Process an ioctl request.
179 */
180/* ARGSUSED */
181int
182dsioctl(ifp, cmd, data)
183	register struct ifnet *ifp;
184	int cmd;
185	caddr_t data;
186{
187	register struct ifaddr *ifa;
188	register struct ifreq *ifr = (struct ifreq *)data;
189	register int error = 0;
190
191	switch (cmd) {
192
193	case SIOCSIFADDR:
194		ifp->if_flags |= IFF_UP;
195		ifa = (struct ifaddr *)data;
196		if (ifa != 0)
197			ifa->ifa_rtrequest = dsrtrequest;
198		/*
199		 * Everything else is done at a higher level.
200		 */
201		break;
202
203	case SIOCADDMULTI:
204	case SIOCDELMULTI:
205		if (ifr == 0) {
206			error = EAFNOSUPPORT;		/* XXX */
207			break;
208		}
209		switch (ifr->ifr_addr.sa_family) {
210
211#ifdef INET
212		case AF_INET:
213			break;
214#endif
215
216		default:
217			error = EAFNOSUPPORT;
218			break;
219		}
220		break;
221
222	case SIOCSIFMTU:
223		ifp->if_mtu = ifr->ifr_mtu;
224		break;
225
226	default:
227		error = EINVAL;
228	}
229	return (error);
230}
231