]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_faith.c
xnu-201.42.3.tar.gz
[apple/xnu.git] / bsd / net / if_faith.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* $KAME: if_faith.c,v 1.11 2000/02/22 14:01:46 itojun Exp $ */
23
24 /*
25 * Copyright (c) 1982, 1986, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56 /*
57 * derived from
58 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93
59 * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp
60 */
61
62 /*
63 * Loopback interface driver for protocol testing and timing.
64 */
65 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
66 #include "opt_inet.h"
67 #endif
68
69 #include "faith.h"
70 #if NFAITH > 0
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/mbuf.h>
76 #include <sys/socket.h>
77 #include <sys/errno.h>
78 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 || defined(__APPLE__)
79 #include <sys/sockio.h>
80 #else
81 #include <sys/ioctl.h>
82 #endif
83 #include <sys/time.h>
84 #if defined(__bsdi__) || defined(__NetBSD__)
85 #include <machine/cpu.h>
86 #endif
87
88 #include <net/if.h>
89 #include <net/if_types.h>
90 #include <net/netisr.h>
91 #include <net/route.h>
92 #include <net/bpf.h>
93
94 #if INET
95 #include <netinet/in.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/in_var.h>
98 #include <netinet/ip.h>
99 #endif
100
101 #if INET6
102 #ifndef INET
103 #include <netinet/in.h>
104 #endif
105 #include <netinet6/in6_var.h>
106 #include <netinet/ip6.h>
107 #endif
108
109 #include <net/dlil.h>
110 #include "faith.h"
111 #include "bpfilter.h"
112
113 #include <net/net_osdep.h>
114
115 #if defined(__FreeBSD__) && __FreeBSD__ < 3
116 static int faithioctl __P((struct ifnet *, int, caddr_t));
117 #else
118 static int faithioctl __P((struct ifnet *, u_long, caddr_t));
119 #endif
120 int faith_pre_output __P((struct ifnet *, register struct mbuf **, struct sockaddr *,
121 register struct rtentry *, char *, char *, u_long));
122 static void faithrtrequest __P((int, struct rtentry *, struct sockaddr *));
123
124 #if defined(__FreeBSD__) || defined (__APPLE__)
125 void faithattach __P((void *));
126 #else
127 void faithattach __P((int));
128 #endif
129
130 #define HAVE_OLD_BPF 1
131
132 static struct ifnet faithif[NFAITH];
133 static struct if_proto *faith_array[NFAITH];
134 static int faith_count = 0;
135
136 #define FAITHMTU 1500
137
138 static
139 int faith_add_if(struct ifnet *ifp)
140 {
141 ifp->if_demux = 0;
142 ifp->if_framer = 0;
143 return 0;
144 }
145
146 static
147 int faith_del_if(struct ifnet *ifp)
148 {
149 return 0;
150 }
151
152 static
153 int faith_add_proto(struct ddesc_head_str *desc_head, struct if_proto *proto, u_long dl_tag)
154 {
155 int i;
156
157 for (i=0; i < faith_count; i++)
158 if (faith_array[i] == 0) {
159 faith_array[faith_count] = proto;
160 return 0;
161 }
162
163 if ((i == faith_count) && (faith_count == NFAITH))
164 panic("faith_add_proto -- Too many attachments\n");
165
166 faith_array[faith_count++] = proto;
167
168 return (0);
169 }
170
171 static
172 int faith_del_proto(struct if_proto *proto, u_long dl_tag)
173 {
174 int i;
175
176
177 for (i=0; i < faith_count; i++)
178 if (faith_array[i] == proto) {
179 faith_array[i] = 0;
180 return 0;
181 }
182
183 return ENOENT;
184 }
185
186 int faith_shutdown()
187 {
188 return 0;
189 }
190
191 void faith_reg_if_mods()
192 {
193 struct dlil_ifmod_reg_str faith_ifmod;
194
195 faith_ifmod.add_if = faith_add_if;
196 faith_ifmod.del_if = faith_del_if;
197 faith_ifmod.add_proto = faith_add_proto;
198 faith_ifmod.del_proto = faith_del_proto;
199 faith_ifmod.ifmod_ioctl = 0;
200 faith_ifmod.shutdown = faith_shutdown;
201
202
203 if (dlil_reg_if_modules(APPLE_IF_FAM_FAITH, &faith_ifmod))
204 panic("Couldn't register faith modules\n");
205
206 }
207
208 u_long faith_attach_inet(struct ifnet *ifp)
209 {
210 struct dlil_proto_reg_str reg;
211 struct dlil_demux_desc desc;
212 u_long dl_tag=0;
213 short native=0;
214 int stat;
215 int i;
216
217 for (i=0; i < faith_count; i++) {
218 if (faith_array[i] && (faith_array[i]->ifp == ifp) &&
219 (faith_array[i]->protocol_family == PF_INET)) {
220 #if 0
221 kprintf("faith_array for %s%d found dl_tag=%d\n",
222 ifp->if_name, ifp->if_unit, faith_array[i]->dl_tag);
223 #endif
224 return faith_array[i]->dl_tag;
225
226 }
227 }
228
229 TAILQ_INIT(&reg.demux_desc_head);
230 desc.type = DLIL_DESC_RAW;
231 desc.variants.bitmask.proto_id_length = 0;
232 desc.variants.bitmask.proto_id = 0;
233 desc.variants.bitmask.proto_id_mask = 0;
234 desc.native_type = (char *) &native;
235 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc, next);
236 reg.interface_family = ifp->if_family;
237 reg.unit_number = ifp->if_unit;
238 reg.input = 0;
239 reg.pre_output = faith_pre_output;
240 reg.event = 0;
241 reg.offer = 0;
242 reg.ioctl = faithioctl;
243 reg.default_proto = 0;
244 reg.protocol_family = PF_INET;
245
246 stat = dlil_attach_protocol(&reg, &dl_tag);
247 if (stat) {
248 panic("faith_attach_inet can't attach interface\n");
249 }
250
251 return dl_tag;
252 }
253
254 void
255 faithattach(faith)
256 void *faith;
257 {
258 struct ifnet *ifp;
259 int i;
260
261 faith_reg_if_mods(); /* DLIL modules */
262
263 for (i = 0; i < NFAITH; i++) {
264 ifp = &faithif[i];
265 bzero(ifp, sizeof(faithif[i]));
266 ifp->if_name = "faith";
267 ifp->if_unit = i;
268 ifp->if_family = APPLE_IF_FAM_FAITH;
269 ifp->if_mtu = FAITHMTU;
270 /* Change to BROADCAST experimentaly to announce its prefix. */
271 ifp->if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
272 ifp->if_ioctl = faithioctl;
273 ifp->if_output = NULL;
274 ifp->if_type = IFT_FAITH;
275 ifp->if_hdrlen = 0;
276 ifp->if_addrlen = 0;
277 dlil_if_attach(ifp);
278 #if NBPFILTER > 0
279 #ifdef HAVE_OLD_BPF
280 bpfattach(ifp, DLT_NULL, sizeof(u_int));
281 #else
282 bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int));
283 #endif
284 #endif
285 }
286 }
287
288 int
289 faith_pre_output(ifp, m0, dst, rt, frame_type, dst_addr, dl_tag)
290 struct ifnet *ifp;
291 register struct mbuf **m0;
292 struct sockaddr *dst;
293 register struct rtentry *rt;
294 char *frame_type;
295 char *dst_addr;
296 u_long dl_tag;
297 {
298 int s, isr;
299 register struct ifqueue *ifq = 0;
300 register struct mbuf *m = *m0;
301
302 if ((m->m_flags & M_PKTHDR) == 0)
303 panic("faithoutput no HDR");
304 #if NBPFILTER > 0
305 /* BPF write needs to be handled specially */
306 if (dst && dst->sa_family == AF_UNSPEC) {
307 dst->sa_family = *(mtod(m, int *));
308 m->m_len -= sizeof(int);
309 m->m_pkthdr.len -= sizeof(int);
310 m->m_data += sizeof(int);
311 }
312
313 if (ifp->if_bpf) {
314 /*
315 * We need to prepend the address family as
316 * a four byte field. Cons up a faith header
317 * to pacify bpf. This is safe because bpf
318 * will only read from the mbuf (i.e., it won't
319 * try to free it or keep a pointer a to it).
320 */
321 struct mbuf m0;
322 u_int af = dst->sa_family;
323
324 m0.m_next = m;
325 m0.m_len = 4;
326 m0.m_data = (char *)&af;
327
328 #if HAVE_OLD_BPF
329 bpf_mtap(ifp, &m0);
330 #else
331 bpf_mtap(ifp->if_bpf, &m0);
332 #endif
333 }
334 #endif
335
336 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
337 m_freem(m);
338 return (EJUSTRETURN);
339 // return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
340 // rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
341 }
342 ifp->if_opackets++;
343 ifp->if_obytes += m->m_pkthdr.len;
344 switch (dst->sa_family) {
345 #if INET
346 case AF_INET:
347 ifq = &ipintrq;
348 isr = NETISR_IP;
349 break;
350 #endif
351 #if INET6
352 case AF_INET6:
353 ifq = &ip6intrq;
354 isr = NETISR_IPV6;
355 break;
356 #endif
357 default:
358 kprintf("faith_pre_output: m=%x family is unknown...(0x%x\n", m, dst->sa_family);
359 m_freem(m);
360 return EAFNOSUPPORT;
361 }
362
363 /* XXX do we need more sanity checks? */
364
365 m->m_pkthdr.rcvif = ifp;
366 s = splimp();
367 if (IF_QFULL(ifq)) {
368 IF_DROP(ifq);
369 m_freem(m);
370 splx(s);
371 return (EJUSTRETURN);
372 }
373 IF_ENQUEUE(ifq, m);
374 schednetisr(isr);
375 ifp->if_ipackets++;
376 ifp->if_ibytes += m->m_pkthdr.len;
377 splx(s);
378 return (EJUSTRETURN);
379 }
380
381 /* ARGSUSED */
382 static void
383 faithrtrequest(cmd, rt, sa)
384 int cmd;
385 struct rtentry *rt;
386 struct sockaddr *sa;
387 {
388 if (rt) {
389 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
390 /*
391 * For optimal performance, the send and receive buffers
392 * should be at least twice the MTU plus a little more for
393 * overhead.
394 */
395 rt->rt_rmx.rmx_recvpipe =
396 rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU;
397 }
398 }
399
400 /*
401 * Process an ioctl request.
402 */
403 /* ARGSUSED */
404 static int
405 faithioctl(ifp, cmd, data)
406 register struct ifnet *ifp;
407 #if defined(__FreeBSD__) && __FreeBSD__ < 3
408 int cmd;
409 #else
410 u_long cmd;
411 #endif
412 caddr_t data;
413 {
414 register struct ifaddr *ifa;
415 register struct ifreq *ifr = (struct ifreq *)data;
416 register int error = 0;
417
418 switch (cmd) {
419
420 case SIOCSIFADDR:
421 ifp->if_flags |= IFF_UP | IFF_RUNNING;
422 ifa = (struct ifaddr *)data;
423 ifa->ifa_rtrequest = faithrtrequest;
424 /*
425 * Everything else is done at a higher level.
426 */
427 break;
428
429 case SIOCADDMULTI:
430 case SIOCDELMULTI:
431 if (ifr == 0) {
432 error = EAFNOSUPPORT; /* XXX */
433 break;
434 }
435 switch (ifr->ifr_addr.sa_family) {
436 #if INET
437 case AF_INET:
438 break;
439 #endif
440 #if INET6
441 case AF_INET6:
442 break;
443 #endif
444
445 default:
446 error = EAFNOSUPPORT;
447 break;
448 }
449 break;
450
451 #ifdef SIOCSIFMTU
452 #ifndef __OpenBSD__
453 case SIOCSIFMTU:
454 ifp->if_mtu = ifr->ifr_mtu;
455 break;
456 #endif
457 #endif
458
459 case SIOCSIFFLAGS:
460 break;
461
462 default:
463 error = EINVAL;
464 }
465 return (error);
466 }
467 #endif /* NFAITH > 0 */