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