if_disc.c revision 87912
121308Sache/*
221308Sache * Copyright (c) 1982, 1986, 1993
321308Sache *	The Regents of the University of California.  All rights reserved.
421308Sache *
521308Sache * Redistribution and use in source and binary forms, with or without
621308Sache * modification, are permitted provided that the following conditions
721308Sache * are met:
821308Sache * 1. Redistributions of source code must retain the above copyright
921308Sache *    notice, this list of conditions and the following disclaimer.
1021308Sache * 2. Redistributions in binary form must reproduce the above copyright
1121308Sache *    notice, this list of conditions and the following disclaimer in the
1221308Sache *    documentation and/or other materials provided with the distribution.
1321308Sache * 3. All advertising materials mentioning features or use of this software
1421308Sache *    must display the following acknowledgement:
1521308Sache *	This product includes software developed by the University of
1621308Sache *	California, Berkeley and its contributors.
1721308Sache * 4. Neither the name of the University nor the names of its contributors
1821308Sache *    may be used to endorse or promote products derived from this software
1921308Sache *    without specific prior written permission.
2021308Sache *
2121308Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2221308Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2321308Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2421308Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2521308Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2621308Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2721308Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2821308Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2921308Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3021308Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3121308Sache * SUCH DAMAGE.
3221308Sache *
3321308Sache *	From: @(#)if_loop.c	8.1 (Berkeley) 6/10/93
3421308Sache * $FreeBSD: head/sys/net/if_disc.c 87912 2001-12-14 19:28:06Z jlemon $
3521308Sache */
3621308Sache
3721308Sache/*
3821308Sache * Discard interface driver for protocol testing and timing.
3921308Sache * (Based on the loopback.)
4021308Sache */
4121308Sache
4221308Sache#include <sys/param.h>
4321308Sache#include <sys/systm.h>
4421308Sache#include <sys/kernel.h>
4526497Sache#include <sys/module.h>
4626497Sache#include <sys/mbuf.h>
4726497Sache#include <sys/socket.h>
4826497Sache#include <sys/sockio.h>
4926497Sache
5026497Sache#include <net/if.h>
5126497Sache#include <net/if_types.h>
5226497Sache#include <net/route.h>
5326497Sache#include <net/bpf.h>
5426497Sache
5526497Sache#include "opt_inet.h"
5626497Sache#include "opt_inet6.h"
5721308Sache
5821308Sache#ifdef TINY_DSMTU
5921308Sache#define	DSMTU	(1024+512)
6021308Sache#else
6121308Sache#define DSMTU	65532
6221308Sache#endif
6321308Sache
6421308Sachestatic void discattach(void);
6521308Sache
6621308Sachestatic struct ifnet	discif;
6721308Sachestatic int		discoutput(struct ifnet *, struct mbuf *,
6821308Sache			    struct sockaddr *, struct rtentry *);
6921308Sachestatic void		discrtrequest(int, struct rtentry *, struct rt_addrinfo *);
7021308Sachestatic int		discioctl(struct ifnet *, u_long, caddr_t);
7121308Sache
7221308Sachestatic void
7321308Sachediscattach(void)
74{
75	struct ifnet *ifp = &discif;
76
77	ifp->if_name = "ds";
78	ifp->if_mtu = DSMTU;
79	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
80	ifp->if_ioctl = discioctl;
81	ifp->if_output = discoutput;
82	ifp->if_type = IFT_LOOP;
83	ifp->if_hdrlen = 0;
84	ifp->if_addrlen = 0;
85	ifp->if_snd.ifq_maxlen = 20;
86	if_attach(ifp);
87	bpfattach(ifp, DLT_NULL, sizeof(u_int));
88}
89
90static int
91disc_modevent(module_t mod, int type, void *data)
92{
93	switch (type) {
94	case MOD_LOAD:
95		discattach();
96		break;
97	case MOD_UNLOAD:
98		printf("if_disc module unload - not possible for this module type\n");
99		return EINVAL;
100	}
101	return 0;
102}
103
104static moduledata_t disc_mod = {
105	"if_disc",
106	disc_modevent,
107	NULL
108};
109
110DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
111
112static int
113discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
114    struct rtentry *rt)
115{
116	if ((m->m_flags & M_PKTHDR) == 0)
117		panic("discoutput no HDR");
118	/* BPF write needs to be handled specially */
119	if (dst->sa_family == AF_UNSPEC) {
120		dst->sa_family = *(mtod(m, int *));
121		m->m_len -= sizeof(int);
122		m->m_pkthdr.len -= sizeof(int);
123		m->m_data += sizeof(int);
124	}
125
126	if (discif.if_bpf) {
127		/*
128		 * We need to prepend the address family as
129		 * a four byte field.  Cons up a dummy header
130		 * to pacify bpf.  This is safe because bpf
131		 * will only read from the mbuf (i.e., it won't
132		 * try to free it or keep a pointer a to it).
133		 */
134		struct mbuf m0;
135		u_int af = dst->sa_family;
136
137		m0.m_next = m;
138		m0.m_len = 4;
139		m0.m_data = (char *)&af;
140
141		bpf_mtap(&discif, &m0);
142	}
143	m->m_pkthdr.rcvif = ifp;
144
145	ifp->if_opackets++;
146	ifp->if_obytes += m->m_pkthdr.len;
147
148	m_freem(m);
149	return 0;
150}
151
152/* ARGSUSED */
153static void
154discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
155{
156	if (rt)
157		rt->rt_rmx.rmx_mtu = DSMTU;
158}
159
160/*
161 * Process an ioctl request.
162 */
163static int
164discioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
165{
166	struct ifaddr *ifa;
167	struct ifreq *ifr = (struct ifreq *)data;
168	int error = 0;
169
170	switch (cmd) {
171
172	case SIOCSIFADDR:
173		ifp->if_flags |= IFF_UP;
174		ifa = (struct ifaddr *)data;
175		if (ifa != 0)
176			ifa->ifa_rtrequest = discrtrequest;
177		/*
178		 * Everything else is done at a higher level.
179		 */
180		break;
181
182	case SIOCADDMULTI:
183	case SIOCDELMULTI:
184		if (ifr == 0) {
185			error = EAFNOSUPPORT;		/* XXX */
186			break;
187		}
188		switch (ifr->ifr_addr.sa_family) {
189
190#ifdef INET
191		case AF_INET:
192			break;
193#endif
194#ifdef INET6
195		case AF_INET6:
196			break;
197#endif
198
199		default:
200			error = EAFNOSUPPORT;
201			break;
202		}
203		break;
204
205	case SIOCSIFMTU:
206		ifp->if_mtu = ifr->ifr_mtu;
207		break;
208
209	default:
210		error = EINVAL;
211	}
212	return (error);
213}
214