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