Deleted Added
full compact
if_disc.c (128019) if_disc.c (130933)
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * From: @(#)if_loop.c 8.1 (Berkeley) 6/10/93
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * From: @(#)if_loop.c 8.1 (Berkeley) 6/10/93
30 * $FreeBSD: head/sys/net/if_disc.c 128019 2004-04-07 20:46:16Z imp $
30 * $FreeBSD: head/sys/net/if_disc.c 130933 2004-06-22 20:13:25Z brooks $
31 */
32
33/*
34 * Discard interface driver for protocol testing and timing.
35 * (Based on the loopback.)
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/malloc.h>
42#include <sys/module.h>
43#include <sys/mbuf.h>
44#include <sys/socket.h>
45#include <sys/sockio.h>
46
47#include <net/if.h>
31 */
32
33/*
34 * Discard interface driver for protocol testing and timing.
35 * (Based on the loopback.)
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/malloc.h>
42#include <sys/module.h>
43#include <sys/mbuf.h>
44#include <sys/socket.h>
45#include <sys/sockio.h>
46
47#include <net/if.h>
48#include <net/if_clone.h>
48#include <net/if_types.h>
49#include <net/route.h>
50#include <net/bpf.h>
51
52#include "opt_inet.h"
53#include "opt_inet6.h"
54
55#ifdef TINY_DSMTU
56#define DSMTU (1024+512)
57#else
58#define DSMTU 65532
59#endif
60
61#define DISCNAME "disc"
62
63struct disc_softc {
64 struct ifnet sc_if; /* must be first */
65 LIST_ENTRY(disc_softc) sc_list;
66};
67
68static int discoutput(struct ifnet *, struct mbuf *,
69 struct sockaddr *, struct rtentry *);
70static void discrtrequest(int, struct rtentry *, struct rt_addrinfo *);
71static int discioctl(struct ifnet *, u_long, caddr_t);
72static int disc_clone_create(struct if_clone *, int);
73static void disc_clone_destroy(struct ifnet *);
74
75static struct mtx disc_mtx;
76static MALLOC_DEFINE(M_DISC, DISCNAME, "Discard interface");
77static LIST_HEAD(, disc_softc) disc_softc_list;
49#include <net/if_types.h>
50#include <net/route.h>
51#include <net/bpf.h>
52
53#include "opt_inet.h"
54#include "opt_inet6.h"
55
56#ifdef TINY_DSMTU
57#define DSMTU (1024+512)
58#else
59#define DSMTU 65532
60#endif
61
62#define DISCNAME "disc"
63
64struct disc_softc {
65 struct ifnet sc_if; /* must be first */
66 LIST_ENTRY(disc_softc) sc_list;
67};
68
69static int discoutput(struct ifnet *, struct mbuf *,
70 struct sockaddr *, struct rtentry *);
71static void discrtrequest(int, struct rtentry *, struct rt_addrinfo *);
72static int discioctl(struct ifnet *, u_long, caddr_t);
73static int disc_clone_create(struct if_clone *, int);
74static void disc_clone_destroy(struct ifnet *);
75
76static struct mtx disc_mtx;
77static MALLOC_DEFINE(M_DISC, DISCNAME, "Discard interface");
78static LIST_HEAD(, disc_softc) disc_softc_list;
78static struct if_clone disc_cloner = IF_CLONE_INITIALIZER(DISCNAME,
79 disc_clone_create, disc_clone_destroy, 0, IF_MAXUNIT);
80
79
80IFC_SIMPLE_DECLARE(disc, 0);
81
81static int
82disc_clone_create(struct if_clone *ifc, int unit)
83{
84 struct ifnet *ifp;
85 struct disc_softc *sc;
86
87 sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK);
88 bzero(sc, sizeof(struct disc_softc));
89
90 ifp = &sc->sc_if;
91
92 ifp->if_softc = sc;
93 if_initname(ifp, ifc->ifc_name, unit);
94 ifp->if_mtu = DSMTU;
95 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
96 ifp->if_ioctl = discioctl;
97 ifp->if_output = discoutput;
98 ifp->if_type = IFT_LOOP;
99 ifp->if_hdrlen = 0;
100 ifp->if_addrlen = 0;
101 ifp->if_snd.ifq_maxlen = 20;
102 if_attach(ifp);
103 bpfattach(ifp, DLT_NULL, sizeof(u_int));
104 mtx_lock(&disc_mtx);
105 LIST_INSERT_HEAD(&disc_softc_list, sc, sc_list);
106 mtx_unlock(&disc_mtx);
107
108 return (0);
109}
110
111static void
112disc_destroy(struct disc_softc *sc)
113{
114
115 bpfdetach(&sc->sc_if);
116 if_detach(&sc->sc_if);
117
118 free(sc, M_DISC);
119}
120
121static void
122disc_clone_destroy(struct ifnet *ifp)
123{
124 struct disc_softc *sc;
125
126 sc = ifp->if_softc;
127 mtx_lock(&disc_mtx);
128 LIST_REMOVE(sc, sc_list);
129 mtx_unlock(&disc_mtx);
130
131 disc_destroy(sc);
132}
133
134static int
135disc_modevent(module_t mod, int type, void *data)
136{
137 struct disc_softc *sc;
138
139 switch (type) {
140 case MOD_LOAD:
141 mtx_init(&disc_mtx, "disc_mtx", NULL, MTX_DEF);
142 LIST_INIT(&disc_softc_list);
143 if_clone_attach(&disc_cloner);
144 break;
145 case MOD_UNLOAD:
146 if_clone_detach(&disc_cloner);
147
148 mtx_lock(&disc_mtx);
149 while ((sc = LIST_FIRST(&disc_softc_list)) != NULL) {
150 LIST_REMOVE(sc, sc_list);
151 mtx_unlock(&disc_mtx);
152 disc_destroy(sc);
153 mtx_lock(&disc_mtx);
154 }
155 mtx_unlock(&disc_mtx);
156 mtx_destroy(&disc_mtx);
157 break;
158 }
159 return 0;
160}
161
162static moduledata_t disc_mod = {
163 "if_disc",
164 disc_modevent,
165 NULL
166};
167
168DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
169
170static int
171discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
172 struct rtentry *rt)
173{
174 M_ASSERTPKTHDR(m);
175 /* BPF write needs to be handled specially */
176 if (dst->sa_family == AF_UNSPEC) {
177 dst->sa_family = *(mtod(m, int *));
178 m->m_len -= sizeof(int);
179 m->m_pkthdr.len -= sizeof(int);
180 m->m_data += sizeof(int);
181 }
182
183 if (ifp->if_bpf) {
184 u_int af = dst->sa_family;
185 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
186 }
187 m->m_pkthdr.rcvif = ifp;
188
189 ifp->if_opackets++;
190 ifp->if_obytes += m->m_pkthdr.len;
191
192 m_freem(m);
193 return 0;
194}
195
196/* ARGSUSED */
197static void
198discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
199{
200 RT_LOCK_ASSERT(rt);
201
202 if (rt)
203 rt->rt_rmx.rmx_mtu = DSMTU;
204}
205
206/*
207 * Process an ioctl request.
208 */
209static int
210discioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
211{
212 struct ifaddr *ifa;
213 struct ifreq *ifr = (struct ifreq *)data;
214 int error = 0;
215
216 switch (cmd) {
217
218 case SIOCSIFADDR:
219 ifp->if_flags |= IFF_UP;
220 ifa = (struct ifaddr *)data;
221 if (ifa != 0)
222 ifa->ifa_rtrequest = discrtrequest;
223 /*
224 * Everything else is done at a higher level.
225 */
226 break;
227
228 case SIOCADDMULTI:
229 case SIOCDELMULTI:
230 if (ifr == 0) {
231 error = EAFNOSUPPORT; /* XXX */
232 break;
233 }
234 switch (ifr->ifr_addr.sa_family) {
235
236#ifdef INET
237 case AF_INET:
238 break;
239#endif
240#ifdef INET6
241 case AF_INET6:
242 break;
243#endif
244
245 default:
246 error = EAFNOSUPPORT;
247 break;
248 }
249 break;
250
251 case SIOCSIFMTU:
252 ifp->if_mtu = ifr->ifr_mtu;
253 break;
254
255 default:
256 error = EINVAL;
257 }
258 return (error);
259}
82static int
83disc_clone_create(struct if_clone *ifc, int unit)
84{
85 struct ifnet *ifp;
86 struct disc_softc *sc;
87
88 sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK);
89 bzero(sc, sizeof(struct disc_softc));
90
91 ifp = &sc->sc_if;
92
93 ifp->if_softc = sc;
94 if_initname(ifp, ifc->ifc_name, unit);
95 ifp->if_mtu = DSMTU;
96 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
97 ifp->if_ioctl = discioctl;
98 ifp->if_output = discoutput;
99 ifp->if_type = IFT_LOOP;
100 ifp->if_hdrlen = 0;
101 ifp->if_addrlen = 0;
102 ifp->if_snd.ifq_maxlen = 20;
103 if_attach(ifp);
104 bpfattach(ifp, DLT_NULL, sizeof(u_int));
105 mtx_lock(&disc_mtx);
106 LIST_INSERT_HEAD(&disc_softc_list, sc, sc_list);
107 mtx_unlock(&disc_mtx);
108
109 return (0);
110}
111
112static void
113disc_destroy(struct disc_softc *sc)
114{
115
116 bpfdetach(&sc->sc_if);
117 if_detach(&sc->sc_if);
118
119 free(sc, M_DISC);
120}
121
122static void
123disc_clone_destroy(struct ifnet *ifp)
124{
125 struct disc_softc *sc;
126
127 sc = ifp->if_softc;
128 mtx_lock(&disc_mtx);
129 LIST_REMOVE(sc, sc_list);
130 mtx_unlock(&disc_mtx);
131
132 disc_destroy(sc);
133}
134
135static int
136disc_modevent(module_t mod, int type, void *data)
137{
138 struct disc_softc *sc;
139
140 switch (type) {
141 case MOD_LOAD:
142 mtx_init(&disc_mtx, "disc_mtx", NULL, MTX_DEF);
143 LIST_INIT(&disc_softc_list);
144 if_clone_attach(&disc_cloner);
145 break;
146 case MOD_UNLOAD:
147 if_clone_detach(&disc_cloner);
148
149 mtx_lock(&disc_mtx);
150 while ((sc = LIST_FIRST(&disc_softc_list)) != NULL) {
151 LIST_REMOVE(sc, sc_list);
152 mtx_unlock(&disc_mtx);
153 disc_destroy(sc);
154 mtx_lock(&disc_mtx);
155 }
156 mtx_unlock(&disc_mtx);
157 mtx_destroy(&disc_mtx);
158 break;
159 }
160 return 0;
161}
162
163static moduledata_t disc_mod = {
164 "if_disc",
165 disc_modevent,
166 NULL
167};
168
169DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
170
171static int
172discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
173 struct rtentry *rt)
174{
175 M_ASSERTPKTHDR(m);
176 /* BPF write needs to be handled specially */
177 if (dst->sa_family == AF_UNSPEC) {
178 dst->sa_family = *(mtod(m, int *));
179 m->m_len -= sizeof(int);
180 m->m_pkthdr.len -= sizeof(int);
181 m->m_data += sizeof(int);
182 }
183
184 if (ifp->if_bpf) {
185 u_int af = dst->sa_family;
186 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
187 }
188 m->m_pkthdr.rcvif = ifp;
189
190 ifp->if_opackets++;
191 ifp->if_obytes += m->m_pkthdr.len;
192
193 m_freem(m);
194 return 0;
195}
196
197/* ARGSUSED */
198static void
199discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
200{
201 RT_LOCK_ASSERT(rt);
202
203 if (rt)
204 rt->rt_rmx.rmx_mtu = DSMTU;
205}
206
207/*
208 * Process an ioctl request.
209 */
210static int
211discioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
212{
213 struct ifaddr *ifa;
214 struct ifreq *ifr = (struct ifreq *)data;
215 int error = 0;
216
217 switch (cmd) {
218
219 case SIOCSIFADDR:
220 ifp->if_flags |= IFF_UP;
221 ifa = (struct ifaddr *)data;
222 if (ifa != 0)
223 ifa->ifa_rtrequest = discrtrequest;
224 /*
225 * Everything else is done at a higher level.
226 */
227 break;
228
229 case SIOCADDMULTI:
230 case SIOCDELMULTI:
231 if (ifr == 0) {
232 error = EAFNOSUPPORT; /* XXX */
233 break;
234 }
235 switch (ifr->ifr_addr.sa_family) {
236
237#ifdef INET
238 case AF_INET:
239 break;
240#endif
241#ifdef INET6
242 case AF_INET6:
243 break;
244#endif
245
246 default:
247 error = EAFNOSUPPORT;
248 break;
249 }
250 break;
251
252 case SIOCSIFMTU:
253 ifp->if_mtu = ifr->ifr_mtu;
254 break;
255
256 default:
257 error = EINVAL;
258 }
259 return (error);
260}