FreeBSD/Linux Kernel Cross Reference
sys/net/if_disc.c
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: releng/5.2/sys/net/if_disc.c 121816 2003-10-31 18:32:15Z brooks $
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/malloc.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 #define DISCNAME "disc"
66
67 struct disc_softc {
68 struct ifnet sc_if; /* must be first */
69 LIST_ENTRY(disc_softc) sc_list;
70 };
71
72 static int discoutput(struct ifnet *, struct mbuf *,
73 struct sockaddr *, struct rtentry *);
74 static void discrtrequest(int, struct rtentry *, struct rt_addrinfo *);
75 static int discioctl(struct ifnet *, u_long, caddr_t);
76 static int disc_clone_create(struct if_clone *, int);
77 static void disc_clone_destroy(struct ifnet *);
78
79 static MALLOC_DEFINE(M_DISC, DISCNAME, "Discard interface");
80 static LIST_HEAD(, disc_softc) disc_softc_list;
81 static struct if_clone disc_cloner = IF_CLONE_INITIALIZER(DISCNAME,
82 disc_clone_create, disc_clone_destroy, 0, IF_MAXUNIT);
83
84 static int
85 disc_clone_create(struct if_clone *ifc, int unit)
86 {
87 struct ifnet *ifp;
88 struct disc_softc *sc;
89
90 sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK);
91 bzero(sc, sizeof(struct disc_softc));
92
93 ifp = &sc->sc_if;
94
95 ifp->if_softc = sc;
96 if_initname(ifp, ifc->ifc_name, unit);
97 ifp->if_mtu = DSMTU;
98 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
99 ifp->if_ioctl = discioctl;
100 ifp->if_output = discoutput;
101 ifp->if_type = IFT_LOOP;
102 ifp->if_hdrlen = 0;
103 ifp->if_addrlen = 0;
104 ifp->if_snd.ifq_maxlen = 20;
105 if_attach(ifp);
106 bpfattach(ifp, DLT_NULL, sizeof(u_int));
107 LIST_INSERT_HEAD(&disc_softc_list, sc, sc_list);
108
109 return (0);
110 }
111
112 static void
113 disc_clone_destroy(struct ifnet *ifp)
114 {
115 struct disc_softc *sc;
116
117 sc = ifp->if_softc;
118
119 LIST_REMOVE(sc, sc_list);
120 bpfdetach(ifp);
121 if_detach(ifp);
122
123 free(sc, M_DISC);
124 }
125
126 static int
127 disc_modevent(module_t mod, int type, void *data)
128 {
129 switch (type) {
130 case MOD_LOAD:
131 LIST_INIT(&disc_softc_list);
132 if_clone_attach(&disc_cloner);
133 break;
134 case MOD_UNLOAD:
135 if_clone_detach(&disc_cloner);
136
137 while (!LIST_EMPTY(&disc_softc_list))
138 disc_clone_destroy(
139 &LIST_FIRST(&disc_softc_list)->sc_if);
140 break;
141 }
142 return 0;
143 }
144
145 static moduledata_t disc_mod = {
146 "if_disc",
147 disc_modevent,
148 NULL
149 };
150
151 DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
152
153 static int
154 discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
155 struct rtentry *rt)
156 {
157 M_ASSERTPKTHDR(m);
158 /* BPF write needs to be handled specially */
159 if (dst->sa_family == AF_UNSPEC) {
160 dst->sa_family = *(mtod(m, int *));
161 m->m_len -= sizeof(int);
162 m->m_pkthdr.len -= sizeof(int);
163 m->m_data += sizeof(int);
164 }
165
166 if (ifp->if_bpf) {
167 /*
168 * We need to prepend the address family as
169 * a four byte field. Cons up a dummy header
170 * to pacify bpf. This is safe because bpf
171 * will only read from the mbuf (i.e., it won't
172 * try to free it or keep a pointer a to it).
173 */
174 struct mbuf m0;
175 u_int af = dst->sa_family;
176
177 m0.m_next = m;
178 m0.m_len = 4;
179 m0.m_data = (char *)⁡
180
181 BPF_MTAP(ifp, &m0);
182 }
183 m->m_pkthdr.rcvif = ifp;
184
185 ifp->if_opackets++;
186 ifp->if_obytes += m->m_pkthdr.len;
187
188 m_freem(m);
189 return 0;
190 }
191
192 /* ARGSUSED */
193 static void
194 discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
195 {
196 RT_LOCK_ASSERT(rt);
197
198 if (rt)
199 rt->rt_rmx.rmx_mtu = DSMTU;
200 }
201
202 /*
203 * Process an ioctl request.
204 */
205 static int
206 discioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
207 {
208 struct ifaddr *ifa;
209 struct ifreq *ifr = (struct ifreq *)data;
210 int error = 0;
211
212 switch (cmd) {
213
214 case SIOCSIFADDR:
215 ifp->if_flags |= IFF_UP;
216 ifa = (struct ifaddr *)data;
217 if (ifa != 0)
218 ifa->ifa_rtrequest = discrtrequest;
219 /*
220 * Everything else is done at a higher level.
221 */
222 break;
223
224 case SIOCADDMULTI:
225 case SIOCDELMULTI:
226 if (ifr == 0) {
227 error = EAFNOSUPPORT; /* XXX */
228 break;
229 }
230 switch (ifr->ifr_addr.sa_family) {
231
232 #ifdef INET
233 case AF_INET:
234 break;
235 #endif
236 #ifdef INET6
237 case AF_INET6:
238 break;
239 #endif
240
241 default:
242 error = EAFNOSUPPORT;
243 break;
244 }
245 break;
246
247 case SIOCSIFMTU:
248 ifp->if_mtu = ifr->ifr_mtu;
249 break;
250
251 default:
252 error = EINVAL;
253 }
254 return (error);
255 }
Cache object: 9bcae69fdc21c570dd467c208041ee3f
|