2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* $KAME: if_faith.c,v 1.21 2001/02/20 07:59:26 itojun Exp $ */
31 * Copyright (c) 1982, 1986, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * $FreeBSD: src/sys/net/if_faith.c,v 1.3.2.2 2001/07/05 14:46:25 ume Exp $
66 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93
67 * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp
71 * Loopback interface driver for protocol testing and timing.
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
81 #include <sys/socket.h>
82 #include <sys/errno.h>
83 #include <sys/sockio.h>
85 #include <sys/queue.h>
88 #include <net/if_types.h>
89 #include <net/route.h>
91 #include <net/if_faith.h>
94 #include <netinet/in.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/in_var.h>
97 #include <netinet/ip.h>
102 #include <netinet/in.h>
104 #include <netinet6/in6_var.h>
105 #include <netinet/ip6.h>
106 #include <netinet6/ip6_var.h>
109 #include <net/dlil.h>
112 #include <net/net_osdep.h>
114 static int faithioctl(struct ifnet
*, u_long
, void*);
115 int faith_pre_output(struct ifnet
*, register struct mbuf
**,
116 const struct sockaddr
*, caddr_t
, char *, char *, u_long
);
117 static void faithrtrequest(int, struct rtentry
*, struct sockaddr
*);
119 void faithattach(void);
121 PSEUDO_SET(faithattach
, if_faith
);
124 static struct ifnet faithif
[NFAITH
];
125 static struct if_proto
*faith_array
[NFAITH
];
126 static int faith_count
= 0;
128 #define FAITHMTU 1500
131 int faith_add_if(struct ifnet
*ifp
)
139 int faith_del_if(struct ifnet
*ifp
)
145 int faith_add_proto(struct ddesc_head_str
*desc_head
, struct if_proto
*proto
, u_long dl_tag
)
149 for (i
=0; i
< faith_count
; i
++)
150 if (faith_array
[i
] == 0) {
151 faith_array
[faith_count
] = proto
;
155 if ((i
== faith_count
) && (faith_count
== NFAITH
))
156 panic("faith_add_proto -- Too many attachments\n");
158 faith_array
[faith_count
++] = proto
;
164 int faith_del_proto(struct if_proto
*proto
, u_long dl_tag
)
169 for (i
=0; i
< faith_count
; i
++)
170 if (faith_array
[i
] == proto
) {
183 int faith_attach_inet(struct ifnet
*ifp
, u_long
*dl_tag
)
185 struct dlil_proto_reg_str reg
;
186 struct dlil_demux_desc desc
;
191 for (i
=0; i
< faith_count
; i
++) {
192 if (faith_array
[i
] && (faith_array
[i
]->ifp
== ifp
) &&
193 (faith_array
[i
]->protocol_family
== PF_INET
)) {
195 kprintf("faith_array for %s%d found dl_tag=%d\n",
196 ifp
->if_name
, ifp
->if_unit
, faith_array
[i
]->dl_tag
);
198 *dl_tag
= faith_array
[i
]->dl_tag
;
204 bzero(®
, sizeof(reg
));
205 bzero(&desc
, sizeof(desc
));
206 TAILQ_INIT(®
.demux_desc_head
);
207 desc
.type
= DLIL_DESC_RAW
;
208 desc
.native_type
= (char *) &native
;
209 TAILQ_INSERT_TAIL(®
.demux_desc_head
, &desc
, next
);
210 reg
.interface_family
= ifp
->if_family
;
211 reg
.unit_number
= ifp
->if_unit
;
212 reg
.pre_output
= faith_pre_output
;
213 reg
.protocol_family
= PF_INET
;
215 stat
= dlil_attach_protocol(®
, dl_tag
);
217 panic("faith_attach_inet can't attach interface\n");
223 void faith_reg_if_mods()
225 struct dlil_ifmod_reg_str faith_ifmod
;
226 struct dlil_protomod_reg_str faith_protoreg
;
229 bzero(&faith_ifmod
, sizeof(faith_ifmod
));
230 faith_ifmod
.add_if
= faith_add_if
;
231 faith_ifmod
.del_if
= faith_del_if
;
232 faith_ifmod
.add_proto
= faith_add_proto
;
233 faith_ifmod
.del_proto
= faith_del_proto
;
234 faith_ifmod
.ifmod_ioctl
= 0;
235 faith_ifmod
.shutdown
= faith_shutdown
;
238 if (dlil_reg_if_modules(APPLE_IF_FAM_FAITH
, &faith_ifmod
))
239 panic("Couldn't register faith modules\n");
241 /* Register protocol registration functions */
243 bzero(&faith_protoreg
, sizeof(faith_protoreg
));
244 faith_protoreg
.attach_proto
= faith_attach_inet
;
245 faith_protoreg
.detach_proto
= 0;
247 if ( error
= dlil_reg_proto_module(PF_INET
, APPLE_IF_FAM_FAITH
, &faith_protoreg
) != 0)
248 kprintf("dlil_reg_proto_module failed for AF_INET error=%d\n", error
);
259 faith_reg_if_mods(); /* DLIL modules */
261 for (i
= 0; i
< NFAITH
; i
++) {
263 bzero(ifp
, sizeof(faithif
[i
]));
264 ifp
->if_name
= "faith";
266 ifp
->if_family
= APPLE_IF_FAM_FAITH
;
267 ifp
->if_mtu
= FAITHMTU
;
268 /* LOOPBACK commented out to announce IPv6 routes to faith */
269 ifp
->if_flags
= /* IFF_LOOPBACK | */ IFF_MULTICAST
;
270 ifp
->if_ioctl
= faithioctl
;
271 ifp
->if_output
= NULL
;
272 ifp
->if_type
= IFT_FAITH
;
278 bpfattach(ifp
, DLT_NULL
, sizeof(u_int
));
280 bpfattach(&ifp
->if_bpf
, ifp
, DLT_NULL
, sizeof(u_int
));
287 faith_pre_output(ifp
, m0
, dst
, route_entry
, frame_type
, dst_addr
, dl_tag
)
289 register struct mbuf
**m0
;
290 const struct sockaddr
*dst
;
296 register struct mbuf
*m
= *m0
;
297 struct rtentry
*rt
= (struct rtentry
*)route_entry
;
299 if ((m
->m_flags
& M_PKTHDR
) == 0)
300 panic("faithoutput no HDR");
302 /* BPF write needs to be handled specially */
303 if (dst
&& dst
->sa_family
== AF_UNSPEC
) {
304 dst
->sa_family
= *(mtod(m
, int *));
305 m
->m_len
-= sizeof(int);
306 m
->m_pkthdr
.len
-= sizeof(int);
307 m
->m_data
+= sizeof(int);
312 * We need to prepend the address family as
313 * a four byte field. Cons up a faith header
314 * to pacify bpf. This is safe because bpf
315 * will only read from the mbuf (i.e., it won't
316 * try to free it or keep a pointer a to it).
319 u_int32_t af
= dst
->sa_family
;
323 m0
.m_data
= (char *)&af
;
328 bpf_mtap(ifp
->if_bpf
, &m0
);
333 if (rt
&& rt
->rt_flags
& (RTF_REJECT
|RTF_BLACKHOLE
)) {
334 return (rt
->rt_flags
& RTF_BLACKHOLE
? 0 :
335 rt
->rt_flags
& RTF_HOST
? EHOSTUNREACH
: ENETUNREACH
);
338 ifp
->if_obytes
+= m
->m_pkthdr
.len
;
339 m
->m_pkthdr
.rcvif
= ifp
;
340 proto_inject(dst
->sa_family
, m
);
342 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
343 return (EJUSTRETURN
);
348 faithrtrequest(cmd
, rt
, sa
)
354 rt
->rt_rmx
.rmx_mtu
= rt
->rt_ifp
->if_mtu
; /* for ISO */
356 * For optimal performance, the send and receive buffers
357 * should be at least twice the MTU plus a little more for
360 rt
->rt_rmx
.rmx_recvpipe
=
361 rt
->rt_rmx
.rmx_sendpipe
= 3 * FAITHMTU
;
366 * Process an ioctl request.
370 faithioctl(ifp
, cmd
, data
)
376 struct ifreq
*ifr
= (struct ifreq
*)data
;
382 ifnet_set_flags(ifp
, IFF_UP
| IFF_RUNNING
, IFF_UP
| IFF_RUNNING
);
383 ifa
= (struct ifaddr
*)data
;
384 ifa
->ifa_rtrequest
= faithrtrequest
;
386 * Everything else is done at a higher level.
393 error
= EAFNOSUPPORT
; /* XXX */
396 switch (ifr
->ifr_addr
.sa_family
) {
407 error
= EAFNOSUPPORT
;
414 ifp
->if_mtu
= ifr
->ifr_mtu
;
430 * XXX could be layer violation to call sys/net from sys/netinet6
434 struct in6_addr
*in6
;
437 struct sockaddr_in6 sin6
;
440 if (ip6_keepfaith
== 0)
443 bzero(&sin6
, sizeof(sin6
));
444 sin6
.sin6_family
= AF_INET6
;
445 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
446 sin6
.sin6_addr
= *in6
;
447 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0UL);
448 if (rt
&& rt
->rt_ifp
&& rt
->rt_ifp
->if_type
== IFT_FAITH
&&
449 (rt
->rt_ifp
->if_flags
& IFF_UP
) != 0)
458 #endif /* NFAITH > 0 */