if_disc.c revision 27845
1/*
2 * Copyright (c) 1982, 1986, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	From: @(#)if_loop.c	8.1 (Berkeley) 6/10/93
34 *	$Id: if_disc.c,v 1.17 1997/03/24 11:33:10 bde Exp $
35 */
36
37/*
38 * Discard interface driver for protocol testing and timing.
39 * (Based on the loopback.)
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/mbuf.h>
46#include <sys/socket.h>
47#include <sys/sockio.h>
48
49#include <net/if.h>
50#include <net/if_types.h>
51#include <net/route.h>
52#include <net/bpf.h>
53
54#include "bpfilter.h"
55
56#ifdef TINY_DSMTU
57#define	DSMTU	(1024+512)
58#else
59#define DSMTU	65532
60#endif
61
62static void discattach __P((void *dummy));
63PSEUDO_SET(discattach, if_disc);
64
65static struct	ifnet dsif;
66static int dsoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
67		    struct rtentry *);
68static void dsrtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa);
69static int dsioctl(struct ifnet *, int, caddr_t);
70
71/* ARGSUSED */
72static void
73discattach(dummy)
74	void *dummy;
75{
76	register struct ifnet *ifp = &dsif;
77
78	ifp->if_name = "ds";
79	ifp->if_mtu = DSMTU;
80	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
81	ifp->if_ioctl = dsioctl;
82	ifp->if_output = dsoutput;
83	ifp->if_type = IFT_LOOP;
84	ifp->if_hdrlen = 0;
85	ifp->if_addrlen = 0;
86	if_attach(ifp);
87#if NBPFILTER > 0
88	bpfattach(ifp, DLT_NULL, sizeof(u_int));
89#endif
90}
91
92static int
93dsoutput(ifp, m, dst, rt)
94	struct ifnet *ifp;
95	register struct mbuf *m;
96	struct sockaddr *dst;
97	register struct rtentry *rt;
98{
99	if ((m->m_flags & M_PKTHDR) == 0)
100		panic("dsoutput no HDR");
101#if NBPFILTER > 0
102	/* BPF write needs to be handled specially */
103	if (dst->sa_family == AF_UNSPEC) {
104		dst->sa_family = *(mtod(m, int *));
105		m->m_len -= sizeof(int);
106		m->m_pkthdr.len -= sizeof(int);
107		m->m_data += sizeof(int);
108	}
109
110	if (dsif.if_bpf) {
111		/*
112		 * We need to prepend the address family as
113		 * a four byte field.  Cons up a dummy header
114		 * to pacify bpf.  This is safe because bpf
115		 * will only read from the mbuf (i.e., it won't
116		 * try to free it or keep a pointer a to it).
117		 */
118		struct mbuf m0;
119		u_int af = dst->sa_family;
120
121		m0.m_next = m;
122		m0.m_len = 4;
123		m0.m_data = (char *)&af;
124
125		bpf_mtap(&dsif, &m0);
126	}
127#endif
128	m->m_pkthdr.rcvif = ifp;
129
130	ifp->if_opackets++;
131	ifp->if_obytes += m->m_pkthdr.len;
132
133	m_freem(m);
134	return 0;
135}
136
137/* ARGSUSED */
138static void
139dsrtrequest(cmd, rt, sa)
140	int cmd;
141	struct rtentry *rt;
142	struct sockaddr *sa;
143{
144	if (rt)
145		rt->rt_rmx.rmx_mtu = DSMTU;
146}
147
148/*
149 * Process an ioctl request.
150 */
151/* ARGSUSED */
152static int
153dsioctl(ifp, cmd, data)
154	register struct ifnet *ifp;
155	int cmd;
156	caddr_t data;
157{
158	register struct ifaddr *ifa;
159	register struct ifreq *ifr = (struct ifreq *)data;
160	register int error = 0;
161
162	switch (cmd) {
163
164	case SIOCSIFADDR:
165		ifp->if_flags |= IFF_UP;
166		ifa = (struct ifaddr *)data;
167		if (ifa != 0)
168			ifa->ifa_rtrequest = dsrtrequest;
169		/*
170		 * Everything else is done at a higher level.
171		 */
172		break;
173
174	case SIOCADDMULTI:
175	case SIOCDELMULTI:
176		if (ifr == 0) {
177			error = EAFNOSUPPORT;		/* XXX */
178			break;
179		}
180		switch (ifr->ifr_addr.sa_family) {
181
182#ifdef INET
183		case AF_INET:
184			break;
185#endif
186
187		default:
188			error = EAFNOSUPPORT;
189			break;
190		}
191		break;
192
193	case SIOCSIFMTU:
194		ifp->if_mtu = ifr->ifr_mtu;
195		break;
196
197	default:
198		error = EINVAL;
199	}
200	return (error);
201}
202