xref: /dragonfly/sys/net/disc/if_disc.c (revision 1de703da)
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  * $FreeBSD: src/sys/net/if_disc.c,v 1.26.2.2 2001/12/20 10:30:16 ru Exp $
35  * $DragonFly: src/sys/net/disc/if_disc.c,v 1.2 2003/06/17 04:28:47 dillon Exp $
36  */
37 
38 /*
39  * Discard interface driver for protocol testing and timing.
40  * (Based on the loopback.)
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/module.h>
47 #include <sys/mbuf.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 
51 #include <net/if.h>
52 #include <net/if_types.h>
53 #include <net/route.h>
54 #include <net/bpf.h>
55 
56 #include "opt_inet.h"
57 #include "opt_inet6.h"
58 
59 #ifdef TINY_DSMTU
60 #define	DSMTU	(1024+512)
61 #else
62 #define DSMTU	65532
63 #endif
64 
65 static void discattach __P((void));
66 
67 static struct	ifnet discif;
68 static int discoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
69 		    struct rtentry *);
70 static void discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info);
71 static int discioctl(struct ifnet *, u_long, caddr_t);
72 
73 /* ARGSUSED */
74 static void
75 discattach()
76 {
77 	register struct ifnet *ifp = &discif;
78 
79 	ifp->if_name = "ds";
80 	ifp->if_mtu = DSMTU;
81 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
82 	ifp->if_ioctl = discioctl;
83 	ifp->if_output = discoutput;
84 	ifp->if_type = IFT_LOOP;
85 	ifp->if_hdrlen = 0;
86 	ifp->if_addrlen = 0;
87 	ifp->if_snd.ifq_maxlen = 20;
88 	if_attach(ifp);
89 	bpfattach(ifp, DLT_NULL, sizeof(u_int));
90 }
91 
92 static int
93 disc_modevent(module_t mod, int type, void *data)
94 {
95 	switch (type) {
96 	case MOD_LOAD:
97 		discattach();
98 		break;
99 	case MOD_UNLOAD:
100 		printf("if_disc module unload - not possible for this module type\n");
101 		return EINVAL;
102 	}
103 	return 0;
104 }
105 
106 static moduledata_t disc_mod = {
107 	"if_disc",
108 	disc_modevent,
109 	NULL
110 };
111 
112 DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
113 
114 static int
115 discoutput(ifp, m, dst, rt)
116 	struct ifnet *ifp;
117 	register struct mbuf *m;
118 	struct sockaddr *dst;
119 	register struct rtentry *rt;
120 {
121 	if ((m->m_flags & M_PKTHDR) == 0)
122 		panic("discoutput no HDR");
123 	/* BPF write needs to be handled specially */
124 	if (dst->sa_family == AF_UNSPEC) {
125 		dst->sa_family = *(mtod(m, int *));
126 		m->m_len -= sizeof(int);
127 		m->m_pkthdr.len -= sizeof(int);
128 		m->m_data += sizeof(int);
129 	}
130 
131 	if (discif.if_bpf) {
132 		/*
133 		 * We need to prepend the address family as
134 		 * a four byte field.  Cons up a dummy header
135 		 * to pacify bpf.  This is safe because bpf
136 		 * will only read from the mbuf (i.e., it won't
137 		 * try to free it or keep a pointer a to it).
138 		 */
139 		struct mbuf m0;
140 		u_int af = dst->sa_family;
141 
142 		m0.m_next = m;
143 		m0.m_len = 4;
144 		m0.m_data = (char *)&af;
145 
146 		bpf_mtap(&discif, &m0);
147 	}
148 	m->m_pkthdr.rcvif = ifp;
149 
150 	ifp->if_opackets++;
151 	ifp->if_obytes += m->m_pkthdr.len;
152 
153 	m_freem(m);
154 	return 0;
155 }
156 
157 /* ARGSUSED */
158 static void
159 discrtrequest(cmd, rt, info)
160 	int cmd;
161 	struct rtentry *rt;
162 	struct rt_addrinfo *info;
163 {
164 	if (rt)
165 		rt->rt_rmx.rmx_mtu = DSMTU;
166 }
167 
168 /*
169  * Process an ioctl request.
170  */
171 /* ARGSUSED */
172 static int
173 discioctl(ifp, cmd, data)
174 	register struct ifnet *ifp;
175 	u_long cmd;
176 	caddr_t data;
177 {
178 	register struct ifaddr *ifa;
179 	register struct ifreq *ifr = (struct ifreq *)data;
180 	register int error = 0;
181 
182 	switch (cmd) {
183 
184 	case SIOCSIFADDR:
185 		ifp->if_flags |= IFF_UP;
186 		ifa = (struct ifaddr *)data;
187 		if (ifa != 0)
188 			ifa->ifa_rtrequest = discrtrequest;
189 		/*
190 		 * Everything else is done at a higher level.
191 		 */
192 		break;
193 
194 	case SIOCADDMULTI:
195 	case SIOCDELMULTI:
196 		if (ifr == 0) {
197 			error = EAFNOSUPPORT;		/* XXX */
198 			break;
199 		}
200 		switch (ifr->ifr_addr.sa_family) {
201 
202 #ifdef INET
203 		case AF_INET:
204 			break;
205 #endif
206 #ifdef INET6
207 		case AF_INET6:
208 			break;
209 #endif
210 
211 		default:
212 			error = EAFNOSUPPORT;
213 			break;
214 		}
215 		break;
216 
217 	case SIOCSIFMTU:
218 		ifp->if_mtu = ifr->ifr_mtu;
219 		break;
220 
221 	default:
222 		error = EINVAL;
223 	}
224 	return (error);
225 }
226