]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_faith.c
xnu-517.7.21.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.21 2001/02/20 07:59:26 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 * $FreeBSD: src/sys/net/if_faith.c,v 1.3.2.2 2001/07/05 14:46:25 ume Exp $
57 */
58 /*
59 * derived from
60 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93
61 * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp
62 */
63
64 /*
65 * Loopback interface driver for protocol testing and timing.
66 */
67
68 #include "faith.h"
69 #if NFAITH > 0
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
74 #include <sys/mbuf.h>
75 #include <sys/socket.h>
76 #include <sys/errno.h>
77 #include <sys/sockio.h>
78 #include <sys/time.h>
79 #include <sys/queue.h>
80
81 #include <net/if.h>
82 #include <net/if_types.h>
83 #include <net/netisr.h>
84 #include <net/route.h>
85 #include <net/bpf.h>
86 #include <net/if_faith.h>
87
88 #if INET
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip.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 <netinet/ip6.h>
101 #include <netinet6/ip6_var.h>
102 #endif
103
104 #include <net/dlil.h>
105 #include "bpf.h"
106
107 #include <net/net_osdep.h>
108
109 static int faithioctl __P((struct ifnet *, u_long, void*));
110 int faith_pre_output __P((struct ifnet *, register struct mbuf **, struct sockaddr *,
111 caddr_t, char *, char *, u_long));
112 static void faithrtrequest __P((int, struct rtentry *, struct sockaddr *));
113
114 void faithattach __P((void));
115 #ifndef __APPLE__
116 PSEUDO_SET(faithattach, if_faith);
117 #endif
118
119 static struct ifnet faithif[NFAITH];
120 static struct if_proto *faith_array[NFAITH];
121 static int faith_count = 0;
122
123 #define FAITHMTU 1500
124
125 static
126 int faith_add_if(struct ifnet *ifp)
127 {
128 ifp->if_demux = 0;
129 ifp->if_framer = 0;
130 return 0;
131 }
132
133 static
134 int faith_del_if(struct ifnet *ifp)
135 {
136 return 0;
137 }
138
139 static
140 int faith_add_proto(struct ddesc_head_str *desc_head, struct if_proto *proto, u_long dl_tag)
141 {
142 int i;
143
144 for (i=0; i < faith_count; i++)
145 if (faith_array[i] == 0) {
146 faith_array[faith_count] = proto;
147 return 0;
148 }
149
150 if ((i == faith_count) && (faith_count == NFAITH))
151 panic("faith_add_proto -- Too many attachments\n");
152
153 faith_array[faith_count++] = proto;
154
155 return (0);
156 }
157
158 static
159 int faith_del_proto(struct if_proto *proto, u_long dl_tag)
160 {
161 int i;
162
163
164 for (i=0; i < faith_count; i++)
165 if (faith_array[i] == proto) {
166 faith_array[i] = 0;
167 return 0;
168 }
169
170 return ENOENT;
171 }
172
173 int faith_shutdown()
174 {
175 return 0;
176 }
177
178 int faith_attach_inet(struct ifnet *ifp, u_long *dl_tag)
179 {
180 struct dlil_proto_reg_str reg;
181 struct dlil_demux_desc desc;
182 short native=0;
183 int stat;
184 int i;
185
186 for (i=0; i < faith_count; i++) {
187 if (faith_array[i] && (faith_array[i]->ifp == ifp) &&
188 (faith_array[i]->protocol_family == PF_INET)) {
189 #if 0
190 kprintf("faith_array for %s%d found dl_tag=%d\n",
191 ifp->if_name, ifp->if_unit, faith_array[i]->dl_tag);
192 #endif
193 *dl_tag = faith_array[i]->dl_tag;
194 return 0;
195
196 }
197 }
198
199 TAILQ_INIT(&reg.demux_desc_head);
200 desc.type = DLIL_DESC_RAW;
201 desc.variants.bitmask.proto_id_length = 0;
202 desc.variants.bitmask.proto_id = 0;
203 desc.variants.bitmask.proto_id_mask = 0;
204 desc.native_type = (char *) &native;
205 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc, next);
206 reg.interface_family = ifp->if_family;
207 reg.unit_number = ifp->if_unit;
208 reg.input = 0;
209 reg.pre_output = faith_pre_output;
210 reg.event = 0;
211 reg.offer = 0;
212 reg.ioctl = 0;
213 reg.default_proto = 0;
214 reg.protocol_family = PF_INET;
215
216 stat = dlil_attach_protocol(&reg, dl_tag);
217 if (stat) {
218 panic("faith_attach_inet can't attach interface\n");
219 }
220
221 return stat;
222 }
223
224 void faith_reg_if_mods()
225 {
226 struct dlil_ifmod_reg_str faith_ifmod;
227 struct dlil_protomod_reg_str faith_protoreg;
228 int error;
229
230 bzero(&faith_ifmod, sizeof(faith_ifmod));
231 faith_ifmod.add_if = faith_add_if;
232 faith_ifmod.del_if = faith_del_if;
233 faith_ifmod.add_proto = faith_add_proto;
234 faith_ifmod.del_proto = faith_del_proto;
235 faith_ifmod.ifmod_ioctl = 0;
236 faith_ifmod.shutdown = faith_shutdown;
237
238
239 if (dlil_reg_if_modules(APPLE_IF_FAM_FAITH, &faith_ifmod))
240 panic("Couldn't register faith modules\n");
241
242 /* Register protocol registration functions */
243
244 bzero(&faith_protoreg, sizeof(faith_protoreg));
245 faith_protoreg.attach_proto = faith_attach_inet;
246 faith_protoreg.detach_proto = 0;
247
248 if ( error = dlil_reg_proto_module(PF_INET, APPLE_IF_FAM_FAITH, &faith_protoreg) != 0)
249 kprintf("dlil_reg_proto_module failed for AF_INET error=%d\n", error);
250
251
252 }
253
254 void
255 faithattach(void)
256 {
257 struct ifnet *ifp;
258 int i;
259
260 faith_reg_if_mods(); /* DLIL modules */
261
262 for (i = 0; i < NFAITH; i++) {
263 ifp = &faithif[i];
264 bzero(ifp, sizeof(faithif[i]));
265 ifp->if_name = "faith";
266 ifp->if_unit = i;
267 ifp->if_family = APPLE_IF_FAM_FAITH;
268 ifp->if_mtu = FAITHMTU;
269 /* LOOPBACK commented out to announce IPv6 routes to faith */
270 ifp->if_flags = /* IFF_LOOPBACK | */ IFF_MULTICAST;
271 ifp->if_ioctl = faithioctl;
272 ifp->if_output = NULL;
273 ifp->if_type = IFT_FAITH;
274 ifp->if_hdrlen = 0;
275 ifp->if_addrlen = 0;
276 dlil_if_attach(ifp);
277 #if NBPFILTER > 0
278 #ifdef HAVE_OLD_BPF
279 bpfattach(ifp, DLT_NULL, sizeof(u_int));
280 #else
281 bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int));
282 #endif
283 #endif
284 }
285 }
286
287 int
288 faith_pre_output(ifp, m0, dst, route_entry, frame_type, dst_addr, dl_tag)
289 struct ifnet *ifp;
290 register struct mbuf **m0;
291 struct sockaddr *dst;
292 caddr_t route_entry;
293 char *frame_type;
294 char *dst_addr;
295 u_long dl_tag;
296 {
297 int s, isr;
298 register struct ifqueue *ifq = 0;
299 register struct mbuf *m = *m0;
300 struct rtentry *rt = (struct rtentry*)route_entry;
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_int32_t af = dst->sa_family;
323
324 m0.m_next = m;
325 m0.m_len = 4;
326 m0.m_data = (char *)&af;
327
328 #ifdef 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 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
338 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
339 }
340 ifp->if_opackets++;
341 ifp->if_obytes += m->m_pkthdr.len;
342 switch (dst->sa_family) {
343 #if INET
344 case AF_INET:
345 ifq = &ipintrq;
346 isr = NETISR_IP;
347 break;
348 #endif
349 #if INET6
350 case AF_INET6:
351 ifq = &ip6intrq;
352 isr = NETISR_IPV6;
353 break;
354 #endif
355 default:
356 return EAFNOSUPPORT;
357 }
358
359 /* XXX do we need more sanity checks? */
360
361 m->m_pkthdr.rcvif = ifp;
362 s = splimp();
363 if (IF_QFULL(ifq)) {
364 IF_DROP(ifq);
365 splx(s);
366 return (ENOBUFS);
367 }
368 IF_ENQUEUE(ifq, m);
369 schednetisr(isr);
370 ifp->if_ipackets++;
371 ifp->if_ibytes += m->m_pkthdr.len;
372 splx(s);
373 return (EJUSTRETURN);
374 }
375
376 /* ARGSUSED */
377 static void
378 faithrtrequest(cmd, rt, sa)
379 int cmd;
380 struct rtentry *rt;
381 struct sockaddr *sa;
382 {
383 if (rt) {
384 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
385 /*
386 * For optimal performance, the send and receive buffers
387 * should be at least twice the MTU plus a little more for
388 * overhead.
389 */
390 rt->rt_rmx.rmx_recvpipe =
391 rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU;
392 }
393 }
394
395 /*
396 * Process an ioctl request.
397 */
398 /* ARGSUSED */
399 static int
400 faithioctl(ifp, cmd, data)
401 struct ifnet *ifp;
402 u_long cmd;
403 void* data;
404 {
405 struct ifaddr *ifa;
406 struct ifreq *ifr = (struct ifreq *)data;
407 int error = 0;
408
409 switch (cmd) {
410
411 case SIOCSIFADDR:
412 ifp->if_flags |= IFF_UP | IFF_RUNNING;
413 ifa = (struct ifaddr *)data;
414 ifa->ifa_rtrequest = faithrtrequest;
415 /*
416 * Everything else is done at a higher level.
417 */
418 break;
419
420 case SIOCADDMULTI:
421 case SIOCDELMULTI:
422 if (ifr == 0) {
423 error = EAFNOSUPPORT; /* XXX */
424 break;
425 }
426 switch (ifr->ifr_addr.sa_family) {
427 #if INET
428 case AF_INET:
429 break;
430 #endif
431 #if INET6
432 case AF_INET6:
433 break;
434 #endif
435
436 default:
437 error = EAFNOSUPPORT;
438 break;
439 }
440 break;
441
442 #ifdef SIOCSIFMTU
443 case SIOCSIFMTU:
444 ifp->if_mtu = ifr->ifr_mtu;
445 break;
446 #endif
447
448 case SIOCSIFFLAGS:
449 break;
450
451 default:
452 error = EINVAL;
453 }
454 return (error);
455 }
456
457 #if INET6
458 /*
459 * XXX could be slow
460 * XXX could be layer violation to call sys/net from sys/netinet6
461 */
462 int
463 faithprefix(in6)
464 struct in6_addr *in6;
465 {
466 struct rtentry *rt;
467 struct sockaddr_in6 sin6;
468 int ret;
469
470 if (ip6_keepfaith == 0)
471 return 0;
472
473 bzero(&sin6, sizeof(sin6));
474 sin6.sin6_family = AF_INET6;
475 sin6.sin6_len = sizeof(struct sockaddr_in6);
476 sin6.sin6_addr = *in6;
477 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
478 if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
479 (rt->rt_ifp->if_flags & IFF_UP) != 0)
480 ret = 1;
481 else
482 ret = 0;
483 if (rt)
484 rtfree(rt);
485 return ret;
486 }
487 #endif
488 #endif /* NFAITH > 0 */