Deleted Added
full compact
if_disc.c (167897) if_disc.c (173076)
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 167897 2007-03-26 09:10:28Z yar $
30 * $FreeBSD: head/sys/net/if_disc.c 173076 2007-10-27 19:57:41Z yar $
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>
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_ifp;
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, caddr_t);
73static void disc_clone_destroy(struct ifnet *);
74
75static MALLOC_DEFINE(M_DISC, DISCNAME, "Discard interface");
76
77IFC_SIMPLE_DECLARE(disc, 0);
78
79static int
80disc_clone_create(struct if_clone *ifc, int unit, caddr_t params)
81{
82 struct ifnet *ifp;
83 struct disc_softc *sc;
84
85 sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK | M_ZERO);
86 ifp = sc->sc_ifp = if_alloc(IFT_LOOP);
87 if (ifp == NULL) {
88 free(sc, M_DISC);
89 return (ENOSPC);
90 }
91
92 ifp->if_softc = sc;
93 if_initname(ifp, ifc->ifc_name, unit);
94 ifp->if_mtu = DSMTU;
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>
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_ifp;
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, caddr_t);
73static void disc_clone_destroy(struct ifnet *);
74
75static MALLOC_DEFINE(M_DISC, DISCNAME, "Discard interface");
76
77IFC_SIMPLE_DECLARE(disc, 0);
78
79static int
80disc_clone_create(struct if_clone *ifc, int unit, caddr_t params)
81{
82 struct ifnet *ifp;
83 struct disc_softc *sc;
84
85 sc = malloc(sizeof(struct disc_softc), M_DISC, M_WAITOK | M_ZERO);
86 ifp = sc->sc_ifp = if_alloc(IFT_LOOP);
87 if (ifp == NULL) {
88 free(sc, M_DISC);
89 return (ENOSPC);
90 }
91
92 ifp->if_softc = sc;
93 if_initname(ifp, ifc->ifc_name, unit);
94 ifp->if_mtu = DSMTU;
95 /*
96 * IFF_LOOPBACK should not be removed from disc's flags because
97 * it controls what PF-specific routes are magically added when
98 * a network address is assigned to the interface. Things just
99 * won't work as intended w/o such routes because the output
100 * interface selection for a packet is totally route-driven.
101 * A valid alternative to IFF_LOOPBACK can be IFF_BROADCAST or
102 * IFF_POINTOPOINT, but it would result in different properties
103 * of the interface.
104 */
95 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
96 ifp->if_drv_flags = IFF_DRV_RUNNING;
97 ifp->if_ioctl = discioctl;
98 ifp->if_output = discoutput;
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_int32_t));
104
105 return (0);
106}
107
108static void
109disc_clone_destroy(struct ifnet *ifp)
110{
111 struct disc_softc *sc;
112
113 sc = ifp->if_softc;
114
115 bpfdetach(ifp);
116 if_detach(ifp);
117 if_free(ifp);
118
119 free(sc, M_DISC);
120}
121
122static int
123disc_modevent(module_t mod, int type, void *data)
124{
125
126 switch (type) {
127 case MOD_LOAD:
128 if_clone_attach(&disc_cloner);
129 break;
130 case MOD_UNLOAD:
131 if_clone_detach(&disc_cloner);
132 break;
133 default:
134 return (EOPNOTSUPP);
135 }
136 return (0);
137}
138
139static moduledata_t disc_mod = {
140 "if_disc",
141 disc_modevent,
142 NULL
143};
144
145DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
146
147static int
148discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
149 struct rtentry *rt)
150{
151 u_int32_t af;
152
153 M_ASSERTPKTHDR(m);
154
155 /* BPF writes need to be handled specially. */
156 if (dst->sa_family == AF_UNSPEC) {
157 bcopy(dst->sa_data, &af, sizeof(af));
158 dst->sa_family = af;
159 }
160
161 if (bpf_peers_present(ifp->if_bpf)) {
162 u_int af = dst->sa_family;
163 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
164 }
165 m->m_pkthdr.rcvif = ifp;
166
167 ifp->if_opackets++;
168 ifp->if_obytes += m->m_pkthdr.len;
169
170 m_freem(m);
171 return (0);
172}
173
174/* ARGSUSED */
175static void
176discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
177{
178 RT_LOCK_ASSERT(rt);
179 rt->rt_rmx.rmx_mtu = DSMTU;
180}
181
182/*
183 * Process an ioctl request.
184 */
185static int
186discioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
187{
188 struct ifaddr *ifa;
189 struct ifreq *ifr = (struct ifreq *)data;
190 int error = 0;
191
192 switch (cmd) {
193
194 case SIOCSIFADDR:
195 ifp->if_flags |= IFF_UP;
196 ifa = (struct ifaddr *)data;
197 if (ifa != 0)
198 ifa->ifa_rtrequest = discrtrequest;
199 /*
200 * Everything else is done at a higher level.
201 */
202 break;
203
204 case SIOCADDMULTI:
205 case SIOCDELMULTI:
206 if (ifr == 0) {
207 error = EAFNOSUPPORT; /* XXX */
208 break;
209 }
210 switch (ifr->ifr_addr.sa_family) {
211
212#ifdef INET
213 case AF_INET:
214 break;
215#endif
216#ifdef INET6
217 case AF_INET6:
218 break;
219#endif
220
221 default:
222 error = EAFNOSUPPORT;
223 break;
224 }
225 break;
226
227 case SIOCSIFMTU:
228 ifp->if_mtu = ifr->ifr_mtu;
229 break;
230
231 default:
232 error = EINVAL;
233 }
234 return (error);
235}
105 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
106 ifp->if_drv_flags = IFF_DRV_RUNNING;
107 ifp->if_ioctl = discioctl;
108 ifp->if_output = discoutput;
109 ifp->if_hdrlen = 0;
110 ifp->if_addrlen = 0;
111 ifp->if_snd.ifq_maxlen = 20;
112 if_attach(ifp);
113 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
114
115 return (0);
116}
117
118static void
119disc_clone_destroy(struct ifnet *ifp)
120{
121 struct disc_softc *sc;
122
123 sc = ifp->if_softc;
124
125 bpfdetach(ifp);
126 if_detach(ifp);
127 if_free(ifp);
128
129 free(sc, M_DISC);
130}
131
132static int
133disc_modevent(module_t mod, int type, void *data)
134{
135
136 switch (type) {
137 case MOD_LOAD:
138 if_clone_attach(&disc_cloner);
139 break;
140 case MOD_UNLOAD:
141 if_clone_detach(&disc_cloner);
142 break;
143 default:
144 return (EOPNOTSUPP);
145 }
146 return (0);
147}
148
149static moduledata_t disc_mod = {
150 "if_disc",
151 disc_modevent,
152 NULL
153};
154
155DECLARE_MODULE(if_disc, disc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
156
157static int
158discoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
159 struct rtentry *rt)
160{
161 u_int32_t af;
162
163 M_ASSERTPKTHDR(m);
164
165 /* BPF writes need to be handled specially. */
166 if (dst->sa_family == AF_UNSPEC) {
167 bcopy(dst->sa_data, &af, sizeof(af));
168 dst->sa_family = af;
169 }
170
171 if (bpf_peers_present(ifp->if_bpf)) {
172 u_int af = dst->sa_family;
173 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
174 }
175 m->m_pkthdr.rcvif = ifp;
176
177 ifp->if_opackets++;
178 ifp->if_obytes += m->m_pkthdr.len;
179
180 m_freem(m);
181 return (0);
182}
183
184/* ARGSUSED */
185static void
186discrtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
187{
188 RT_LOCK_ASSERT(rt);
189 rt->rt_rmx.rmx_mtu = DSMTU;
190}
191
192/*
193 * Process an ioctl request.
194 */
195static int
196discioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
197{
198 struct ifaddr *ifa;
199 struct ifreq *ifr = (struct ifreq *)data;
200 int error = 0;
201
202 switch (cmd) {
203
204 case SIOCSIFADDR:
205 ifp->if_flags |= IFF_UP;
206 ifa = (struct ifaddr *)data;
207 if (ifa != 0)
208 ifa->ifa_rtrequest = discrtrequest;
209 /*
210 * Everything else is done at a higher level.
211 */
212 break;
213
214 case SIOCADDMULTI:
215 case SIOCDELMULTI:
216 if (ifr == 0) {
217 error = EAFNOSUPPORT; /* XXX */
218 break;
219 }
220 switch (ifr->ifr_addr.sa_family) {
221
222#ifdef INET
223 case AF_INET:
224 break;
225#endif
226#ifdef INET6
227 case AF_INET6:
228 break;
229#endif
230
231 default:
232 error = EAFNOSUPPORT;
233 break;
234 }
235 break;
236
237 case SIOCSIFMTU:
238 ifp->if_mtu = ifr->ifr_mtu;
239 break;
240
241 default:
242 error = EINVAL;
243 }
244 return (error);
245}