]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_dummy.c
xnu-792.12.6.tar.gz
[apple/xnu.git] / bsd / net / if_dummy.c
CommitLineData
1c79356b 1/*
8ad349bb 2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
1c79356b 3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
1c79356b 5 *
8ad349bb
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
1c79356b
A
29 */
30/*
31 * Copyright (c) 1982, 1986, 1993
32 * The Regents of the University of California. All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 */
62/*
63 * derived from
64 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93
65 * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp
66 */
67
68/*
69 * Loopback interface driver for protocol testing and timing.
70 */
71#if BSD310
72#include "opt_inet.h"
73#endif
74#include "dummy.h"
75#if NDUMMY > 0
76
77#include <sys/param.h>
78#include <sys/systm.h>
79#include <sys/kernel.h>
80#include <sys/mbuf.h>
81#include <sys/socket.h>
82#include <sys/sockio.h>
83#include <sys/errno.h>
84#include <sys/time.h>
85
86#include <net/if.h>
87#include <net/if_types.h>
1c79356b
A
88#include <net/route.h>
89#include <net/bpf.h>
90
91#if INET
92#include <netinet/in.h>
93#include <netinet/in_systm.h>
94#include <netinet/in_var.h>
95#include <netinet/ip.h>
96#endif
97
98#if IPX
99#include <netipx/ipx.h>
100#include <netipx/ipx_if.h>
101#endif
102
103#if INET6
104#ifndef INET
105#include <netinet/in.h>
106#endif
107#include <netinet6/in6_var.h>
108#include <netinet6/ip6.h>
109#endif
110
1c79356b
A
111#if NETATALK
112#include <netinet/if_ether.h>
113#include <netatalk/at.h>
114#include <netatalk/at_var.h>
115#endif NETATALK
116
117#include "bpfilter.h"
118
91447636
A
119static int dummyioctl(struct ifnet *, u_long, caddr_t);
120int dummyoutput(struct ifnet *, register struct mbuf *, struct sockaddr *,
121 register struct rtentry *);
122static void dummyrtrequest(int, struct rtentry *, struct sockaddr *);
1c79356b 123
91447636 124static void dummyattach(void *);
1c79356b
A
125PSEUDO_SET(dummyattach, if_dummy);
126
127#if TINY_DUMMYMTU
128#define DUMMYMTU (1024+512)
129#else
130#define DUMMYMTU 16384
131#endif
132#define HAVE_OLD_BPF 1
133
134static struct ifnet dummyif[NDUMMY];
135
136/* ARGSUSED */
137static void
138dummyattach(dummy)
139 void *dummy;
140{
141 register struct ifnet *ifp;
142 register int i = 0;
143
144 for (i = 0; i < NDUMMY; i++) {
145 ifp = &dummyif[i];
146#if defined(__NetBSD__) || defined(__OpenBSD__)
147 sprintf(ifp->if_xname, "dummy%d", i);
148#else
149 ifp->if_name = "dummy";
150 ifp->if_unit = i;
151#endif
152#ifndef __bsdi__
153 ifp->if_softc = NULL;
154#endif
155 ifp->if_mtu = DUMMYMTU;
156 /* Change to BROADCAST experimentaly to announce its prefix. */
157 ifp->if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
158 ifp->if_ioctl = dummyioctl;
159 ifp->if_output = dummyoutput;
160 ifp->if_type = IFT_DUMMY;
161 ifp->if_hdrlen = 0;
162 ifp->if_addrlen = 0;
163 if_attach(ifp);
164#if NBPFILTER > 0
165#ifdef HAVE_OLD_BPF
166 bpfattach(ifp, DLT_NULL, sizeof(u_int));
167#else
168 bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int));
169#endif
170#endif
171 }
172}
173
174int
175dummyoutput(ifp, m, dst, rt)
176 struct ifnet *ifp;
177 register struct mbuf *m;
178 struct sockaddr *dst;
179 register struct rtentry *rt;
180{
1c79356b
A
181
182 if ((m->m_flags & M_PKTHDR) == 0)
183 panic("dummyoutput no HDR");
184#if NBPFILTER > 0
185 /* BPF write needs to be handled specially */
186 if (dst->sa_family == AF_UNSPEC) {
187 dst->sa_family = *(mtod(m, int *));
188 m->m_len -= sizeof(int);
189 m->m_pkthdr.len -= sizeof(int);
190 m->m_data += sizeof(int);
191 }
192
193 if (ifp->if_bpf) {
194 /*
195 * We need to prepend the address family as
196 * a four byte field. Cons up a dummy header
197 * to pacify bpf. This is safe because bpf
198 * will only read from the mbuf (i.e., it won't
199 * try to free it or keep a pointer a to it).
200 */
201 struct mbuf m0;
202 u_int af = dst->sa_family;
203
204 m0.m_next = m;
205 m0.m_len = 4;
206 m0.m_data = (char *)&af;
207
208#ifdef HAVE_OLD_BPF
209 bpf_mtap(ifp, &m0);
210#else
211 bpf_mtap(ifp->if_bpf, &m0);
212#endif
213 }
214#endif
215 m->m_pkthdr.rcvif = ifp;
216
217 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
218 m_freem(m);
219 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
220 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
221 }
222 ifp->if_opackets++;
223 ifp->if_obytes += m->m_pkthdr.len;
91447636 224 proto_inject(dst->sa_family, m);
1c79356b
A
225 ifp->if_ipackets++;
226 ifp->if_ibytes += m->m_pkthdr.len;
1c79356b
A
227 return (0);
228}
229
230/* ARGSUSED */
231static void
232dummyrtrequest(cmd, rt, sa)
233 int cmd;
234 struct rtentry *rt;
235 struct sockaddr *sa;
236{
237 if (rt) {
238 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
239 /*
240 * For optimal performance, the send and receive buffers
241 * should be at least twice the MTU plus a little more for
242 * overhead.
243 */
244 rt->rt_rmx.rmx_recvpipe =
245 rt->rt_rmx.rmx_sendpipe = 3 * DUMMYMTU;
246 }
247}
248
249/*
250 * Process an ioctl request.
251 */
252/* ARGSUSED */
253static int
254dummyioctl(ifp, cmd, data)
255 register struct ifnet *ifp;
256 u_long cmd;
257 caddr_t data;
258{
259 register struct ifaddr *ifa;
260 register struct ifreq *ifr = (struct ifreq *)data;
261 register int error = 0;
262
263 switch (cmd) {
264
265 case SIOCSIFADDR:
91447636 266 ifnet_set_flags(ifp, IFF_UP | IFF_RUNNING, IFF_UP | IFF_RUNNING);
1c79356b
A
267 ifa = (struct ifaddr *)data;
268 ifa->ifa_rtrequest = dummyrtrequest;
269 /*
270 * Everything else is done at a higher level.
271 */
272 break;
273
274 case SIOCADDMULTI:
275 case SIOCDELMULTI:
276 if (ifr == 0) {
277 error = EAFNOSUPPORT; /* XXX */
278 break;
279 }
280 switch (ifr->ifr_addr.sa_family) {
281
282#if INET
283 case AF_INET:
284 break;
285#endif
286#if INET6
287 case AF_INET6:
288 break;
289#endif
290
291 default:
292 error = EAFNOSUPPORT;
293 break;
294 }
295 break;
296
297 case SIOCSIFMTU:
298 ifp->if_mtu = ifr->ifr_mtu;
299 break;
300
301 case SIOCSIFFLAGS:
302 break;
303
304 default:
305 error = EINVAL;
306 }
307 return (error);
308}
309#endif /* NDUMMY > 0 */