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