]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_faith.c
xnu-792.6.76.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/route.h>
84 #include <net/bpf.h>
85 #include <net/if_faith.h>
86
87 #if INET
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/in_var.h>
91 #include <netinet/ip.h>
92 #endif
93
94 #if INET6
95 #ifndef INET
96 #include <netinet/in.h>
97 #endif
98 #include <netinet6/in6_var.h>
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
101 #endif
102
103 #include <net/dlil.h>
104 #include "bpf.h"
105
106 #include <net/net_osdep.h>
107
108 static int faithioctl(struct ifnet *, u_long, void*);
109 int faith_pre_output(struct ifnet *, register struct mbuf **,
110 const struct sockaddr *, caddr_t, char *, char *, u_long);
111 static void faithrtrequest(int, struct rtentry *, struct sockaddr *);
112
113 void faithattach(void);
114 #ifndef __APPLE__
115 PSEUDO_SET(faithattach, if_faith);
116 #endif
117
118 static struct ifnet faithif[NFAITH];
119 static struct if_proto *faith_array[NFAITH];
120 static int faith_count = 0;
121
122 #define FAITHMTU 1500
123
124 static
125 int faith_add_if(struct ifnet *ifp)
126 {
127 ifp->if_demux = 0;
128 ifp->if_framer = 0;
129 return 0;
130 }
131
132 static
133 int faith_del_if(struct ifnet *ifp)
134 {
135 return 0;
136 }
137
138 static
139 int faith_add_proto(struct ddesc_head_str *desc_head, struct if_proto *proto, u_long dl_tag)
140 {
141 int i;
142
143 for (i=0; i < faith_count; i++)
144 if (faith_array[i] == 0) {
145 faith_array[faith_count] = proto;
146 return 0;
147 }
148
149 if ((i == faith_count) && (faith_count == NFAITH))
150 panic("faith_add_proto -- Too many attachments\n");
151
152 faith_array[faith_count++] = proto;
153
154 return (0);
155 }
156
157 static
158 int faith_del_proto(struct if_proto *proto, u_long dl_tag)
159 {
160 int i;
161
162
163 for (i=0; i < faith_count; i++)
164 if (faith_array[i] == proto) {
165 faith_array[i] = 0;
166 return 0;
167 }
168
169 return ENOENT;
170 }
171
172 int faith_shutdown()
173 {
174 return 0;
175 }
176
177 int faith_attach_inet(struct ifnet *ifp, u_long *dl_tag)
178 {
179 struct dlil_proto_reg_str reg;
180 struct dlil_demux_desc desc;
181 short native=0;
182 int stat;
183 int i;
184
185 for (i=0; i < faith_count; i++) {
186 if (faith_array[i] && (faith_array[i]->ifp == ifp) &&
187 (faith_array[i]->protocol_family == PF_INET)) {
188 #if 0
189 kprintf("faith_array for %s%d found dl_tag=%d\n",
190 ifp->if_name, ifp->if_unit, faith_array[i]->dl_tag);
191 #endif
192 *dl_tag = faith_array[i]->dl_tag;
193 return 0;
194
195 }
196 }
197
198 bzero(&reg, sizeof(reg));
199 bzero(&desc, sizeof(desc));
200 TAILQ_INIT(&reg.demux_desc_head);
201 desc.type = DLIL_DESC_RAW;
202 desc.native_type = (char *) &native;
203 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc, next);
204 reg.interface_family = ifp->if_family;
205 reg.unit_number = ifp->if_unit;
206 reg.pre_output = faith_pre_output;
207 reg.protocol_family = PF_INET;
208
209 stat = dlil_attach_protocol(&reg, dl_tag);
210 if (stat) {
211 panic("faith_attach_inet can't attach interface\n");
212 }
213
214 return stat;
215 }
216
217 void faith_reg_if_mods()
218 {
219 struct dlil_ifmod_reg_str faith_ifmod;
220 struct dlil_protomod_reg_str faith_protoreg;
221 int error;
222
223 bzero(&faith_ifmod, sizeof(faith_ifmod));
224 faith_ifmod.add_if = faith_add_if;
225 faith_ifmod.del_if = faith_del_if;
226 faith_ifmod.add_proto = faith_add_proto;
227 faith_ifmod.del_proto = faith_del_proto;
228 faith_ifmod.ifmod_ioctl = 0;
229 faith_ifmod.shutdown = faith_shutdown;
230
231
232 if (dlil_reg_if_modules(APPLE_IF_FAM_FAITH, &faith_ifmod))
233 panic("Couldn't register faith modules\n");
234
235 /* Register protocol registration functions */
236
237 bzero(&faith_protoreg, sizeof(faith_protoreg));
238 faith_protoreg.attach_proto = faith_attach_inet;
239 faith_protoreg.detach_proto = 0;
240
241 if ( error = dlil_reg_proto_module(PF_INET, APPLE_IF_FAM_FAITH, &faith_protoreg) != 0)
242 kprintf("dlil_reg_proto_module failed for AF_INET error=%d\n", error);
243
244
245 }
246
247 void
248 faithattach(void)
249 {
250 struct ifnet *ifp;
251 int i;
252
253 faith_reg_if_mods(); /* DLIL modules */
254
255 for (i = 0; i < NFAITH; i++) {
256 ifp = &faithif[i];
257 bzero(ifp, sizeof(faithif[i]));
258 ifp->if_name = "faith";
259 ifp->if_unit = i;
260 ifp->if_family = APPLE_IF_FAM_FAITH;
261 ifp->if_mtu = FAITHMTU;
262 /* LOOPBACK commented out to announce IPv6 routes to faith */
263 ifp->if_flags = /* IFF_LOOPBACK | */ IFF_MULTICAST;
264 ifp->if_ioctl = faithioctl;
265 ifp->if_output = NULL;
266 ifp->if_type = IFT_FAITH;
267 ifp->if_hdrlen = 0;
268 ifp->if_addrlen = 0;
269 dlil_if_attach(ifp);
270 #if NBPFILTER > 0
271 #ifdef HAVE_OLD_BPF
272 bpfattach(ifp, DLT_NULL, sizeof(u_int));
273 #else
274 bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int));
275 #endif
276 #endif
277 }
278 }
279
280 int
281 faith_pre_output(ifp, m0, dst, route_entry, frame_type, dst_addr, dl_tag)
282 struct ifnet *ifp;
283 register struct mbuf **m0;
284 const struct sockaddr *dst;
285 caddr_t route_entry;
286 char *frame_type;
287 char *dst_addr;
288 u_long dl_tag;
289 {
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 m->m_pkthdr.rcvif = ifp;
334 proto_inject(dst->sa_family, m);
335 ifp->if_ipackets++;
336 ifp->if_ibytes += m->m_pkthdr.len;
337 return (EJUSTRETURN);
338 }
339
340 /* ARGSUSED */
341 static void
342 faithrtrequest(cmd, rt, sa)
343 int cmd;
344 struct rtentry *rt;
345 struct sockaddr *sa;
346 {
347 if (rt) {
348 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
349 /*
350 * For optimal performance, the send and receive buffers
351 * should be at least twice the MTU plus a little more for
352 * overhead.
353 */
354 rt->rt_rmx.rmx_recvpipe =
355 rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU;
356 }
357 }
358
359 /*
360 * Process an ioctl request.
361 */
362 /* ARGSUSED */
363 static int
364 faithioctl(ifp, cmd, data)
365 struct ifnet *ifp;
366 u_long cmd;
367 void* data;
368 {
369 struct ifaddr *ifa;
370 struct ifreq *ifr = (struct ifreq *)data;
371 int error = 0;
372
373 switch (cmd) {
374
375 case SIOCSIFADDR:
376 ifnet_set_flags(ifp, IFF_UP | IFF_RUNNING, IFF_UP | IFF_RUNNING);
377 ifa = (struct ifaddr *)data;
378 ifa->ifa_rtrequest = faithrtrequest;
379 /*
380 * Everything else is done at a higher level.
381 */
382 break;
383
384 case SIOCADDMULTI:
385 case SIOCDELMULTI:
386 if (ifr == 0) {
387 error = EAFNOSUPPORT; /* XXX */
388 break;
389 }
390 switch (ifr->ifr_addr.sa_family) {
391 #if INET
392 case AF_INET:
393 break;
394 #endif
395 #if INET6
396 case AF_INET6:
397 break;
398 #endif
399
400 default:
401 error = EAFNOSUPPORT;
402 break;
403 }
404 break;
405
406 #ifdef SIOCSIFMTU
407 case SIOCSIFMTU:
408 ifp->if_mtu = ifr->ifr_mtu;
409 break;
410 #endif
411
412 case SIOCSIFFLAGS:
413 break;
414
415 default:
416 error = EINVAL;
417 }
418 return (error);
419 }
420
421 #if INET6
422 /*
423 * XXX could be slow
424 * XXX could be layer violation to call sys/net from sys/netinet6
425 */
426 int
427 faithprefix(in6)
428 struct in6_addr *in6;
429 {
430 struct rtentry *rt;
431 struct sockaddr_in6 sin6;
432 int ret;
433
434 if (ip6_keepfaith == 0)
435 return 0;
436
437 bzero(&sin6, sizeof(sin6));
438 sin6.sin6_family = AF_INET6;
439 sin6.sin6_len = sizeof(struct sockaddr_in6);
440 sin6.sin6_addr = *in6;
441 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
442 if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
443 (rt->rt_ifp->if_flags & IFF_UP) != 0)
444 ret = 1;
445 else
446 ret = 0;
447 if (rt)
448 rtfree(rt);
449 return ret;
450 }
451 #endif
452 #endif /* NFAITH > 0 */