if_faith.c revision 1.22 1 /* $NetBSD: if_faith.c,v 1.22 2001/11/12 23:49:37 lukem Exp $ */
2 /* $KAME: if_faith.c,v 1.21 2001/02/20 07:59:26 itojun Exp $ */
3
4 /*
5 * Copyright (c) 1982, 1986, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36 /*
37 * derived from
38 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93
39 * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp
40 */
41
42 /*
43 * Loopback interface driver for protocol testing and timing.
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: if_faith.c,v 1.22 2001/11/12 23:49:37 lukem Exp $");
48
49 #include "opt_inet.h"
50
51 #include "faith.h"
52 #if NFAITH > 0
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/mbuf.h>
58 #include <sys/socket.h>
59 #include <sys/errno.h>
60 #include <sys/ioctl.h>
61 #include <sys/time.h>
62 #include <sys/queue.h>
63
64 #include <machine/cpu.h>
65
66 #include <net/if.h>
67 #include <net/if_types.h>
68 #include <net/netisr.h>
69 #include <net/route.h>
70 #include <net/bpf.h>
71 #include <net/if_faith.h>
72
73 #ifdef INET
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip.h>
78 #endif
79
80 #ifdef INET6
81 #ifndef INET
82 #include <netinet/in.h>
83 #endif
84 #include <netinet6/in6_var.h>
85 #include <netinet/ip6.h>
86 #include <netinet6/ip6_var.h>
87 #endif
88
89 #include "bpfilter.h"
90
91 #include <net/net_osdep.h>
92
93 struct faith_softc {
94 struct ifnet sc_if; /* must be first */
95 LIST_ENTRY(faith_softc) sc_list;
96 };
97
98 static int faithioctl __P((struct ifnet *, u_long, caddr_t));
99 int faithoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
100 struct rtentry *));
101 static void faithrtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
102
103 void faithattach __P((int));
104
105 LIST_HEAD(, faith_softc) faith_softc_list;
106
107 int faith_clone_create __P((struct if_clone *, int));
108 void faith_clone_destroy __P((struct ifnet *));
109
110 struct if_clone faith_cloner =
111 IF_CLONE_INITIALIZER("faith", faith_clone_create, faith_clone_destroy);
112
113 #define FAITHMTU 1500
114
115 /* ARGSUSED */
116 void
117 faithattach(count)
118 int count;
119 {
120
121 LIST_INIT(&faith_softc_list);
122 if_clone_attach(&faith_cloner);
123 }
124
125 int
126 faith_clone_create(ifc, unit)
127 struct if_clone *ifc;
128 int unit;
129 {
130 struct faith_softc *sc;
131
132 sc = malloc(sizeof(struct faith_softc), M_DEVBUF, M_WAITOK);
133 memset(sc, 0, sizeof(struct faith_softc));
134
135 sprintf(sc->sc_if.if_xname, "%s%d", ifc->ifc_name, unit);
136
137 sc->sc_if.if_mtu = FAITHMTU;
138 /* Change to BROADCAST experimentaly to announce its prefix. */
139 sc->sc_if.if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
140 sc->sc_if.if_ioctl = faithioctl;
141 sc->sc_if.if_output = faithoutput;
142 sc->sc_if.if_type = IFT_FAITH;
143 sc->sc_if.if_hdrlen = 0;
144 sc->sc_if.if_addrlen = 0;
145 sc->sc_if.if_dlt = DLT_NULL;
146 if_attach(&sc->sc_if);
147 if_alloc_sadl(&sc->sc_if);
148 #if NBPFILTER > 0
149 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
150 #endif
151 LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list);
152 return (0);
153 }
154
155 void
156 faith_clone_destroy(ifp)
157 struct ifnet *ifp;
158 {
159 struct faith_softc *sc = (void *) ifp;
160
161 LIST_REMOVE(sc, sc_list);
162 #if NBPFILTER > 0
163 bpfdetach(ifp);
164 #endif
165 if_detach(ifp);
166 free(sc, M_DEVBUF);
167 }
168
169 int
170 faithoutput(ifp, m, dst, rt)
171 struct ifnet *ifp;
172 struct mbuf *m;
173 struct sockaddr *dst;
174 struct rtentry *rt;
175 {
176 int s, isr;
177 struct ifqueue *ifq = 0;
178
179 if ((m->m_flags & M_PKTHDR) == 0)
180 panic("faithoutput no HDR");
181 #if NBPFILTER > 0
182 /* BPF write needs to be handled specially */
183 if (dst->sa_family == AF_UNSPEC) {
184 dst->sa_family = *(mtod(m, int *));
185 m->m_len -= sizeof(int);
186 m->m_pkthdr.len -= sizeof(int);
187 m->m_data += sizeof(int);
188 }
189
190 if (ifp->if_bpf) {
191 /*
192 * We need to prepend the address family as
193 * a four byte field. Cons up a faith header
194 * to pacify bpf. This is safe because bpf
195 * will only read from the mbuf (i.e., it won't
196 * try to free it or keep a pointer a to it).
197 */
198 struct mbuf m0;
199 u_int32_t af = dst->sa_family;
200
201 m0.m_next = m;
202 m0.m_len = 4;
203 m0.m_data = (char *)⁡
204
205 #ifdef HAVE_OLD_BPF
206 bpf_mtap(ifp, &m0);
207 #else
208 bpf_mtap(ifp->if_bpf, &m0);
209 #endif
210 }
211 #endif
212
213 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
214 m_freem(m);
215 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
216 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
217 }
218 ifp->if_opackets++;
219 ifp->if_obytes += m->m_pkthdr.len;
220 switch (dst->sa_family) {
221 #ifdef INET
222 case AF_INET:
223 ifq = &ipintrq;
224 isr = NETISR_IP;
225 break;
226 #endif
227 #ifdef INET6
228 case AF_INET6:
229 ifq = &ip6intrq;
230 isr = NETISR_IPV6;
231 break;
232 #endif
233 default:
234 m_freem(m);
235 return EAFNOSUPPORT;
236 }
237
238 /* XXX do we need more sanity checks? */
239
240 m->m_pkthdr.rcvif = ifp;
241 s = splnet();
242 if (IF_QFULL(ifq)) {
243 IF_DROP(ifq);
244 m_freem(m);
245 splx(s);
246 return (ENOBUFS);
247 }
248 IF_ENQUEUE(ifq, m);
249 schednetisr(isr);
250 ifp->if_ipackets++;
251 ifp->if_ibytes += m->m_pkthdr.len;
252 splx(s);
253 return (0);
254 }
255
256 /* ARGSUSED */
257 static void
258 faithrtrequest(cmd, rt, info)
259 int cmd;
260 struct rtentry *rt;
261 struct rt_addrinfo *info;
262 {
263 if (rt) {
264 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
265 /*
266 * For optimal performance, the send and receive buffers
267 * should be at least twice the MTU plus a little more for
268 * overhead.
269 */
270 rt->rt_rmx.rmx_recvpipe =
271 rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU;
272 }
273 }
274
275 /*
276 * Process an ioctl request.
277 */
278 /* ARGSUSED */
279 static int
280 faithioctl(ifp, cmd, data)
281 struct ifnet *ifp;
282 u_long cmd;
283 caddr_t data;
284 {
285 struct ifaddr *ifa;
286 struct ifreq *ifr = (struct ifreq *)data;
287 int error = 0;
288
289 switch (cmd) {
290
291 case SIOCSIFADDR:
292 ifp->if_flags |= IFF_UP | IFF_RUNNING;
293 ifa = (struct ifaddr *)data;
294 ifa->ifa_rtrequest = faithrtrequest;
295 /*
296 * Everything else is done at a higher level.
297 */
298 break;
299
300 case SIOCADDMULTI:
301 case SIOCDELMULTI:
302 if (ifr == 0) {
303 error = EAFNOSUPPORT; /* XXX */
304 break;
305 }
306 switch (ifr->ifr_addr.sa_family) {
307 #ifdef INET
308 case AF_INET:
309 break;
310 #endif
311 #ifdef INET6
312 case AF_INET6:
313 break;
314 #endif
315
316 default:
317 error = EAFNOSUPPORT;
318 break;
319 }
320 break;
321
322 #ifdef SIOCSIFMTU
323 case SIOCSIFMTU:
324 ifp->if_mtu = ifr->ifr_mtu;
325 break;
326 #endif
327
328 case SIOCSIFFLAGS:
329 break;
330
331 default:
332 error = EINVAL;
333 }
334 return (error);
335 }
336
337 /*
338 * XXX could be slow
339 * XXX could be layer violation to call sys/net from sys/netinet6
340 */
341 int
342 faithprefix(in6)
343 struct in6_addr *in6;
344 {
345 struct rtentry *rt;
346 struct sockaddr_in6 sin6;
347 int ret;
348
349 if (ip6_keepfaith == 0)
350 return 0;
351
352 memset(&sin6, 0, sizeof(sin6));
353 sin6.sin6_family = AF_INET6;
354 sin6.sin6_len = sizeof(struct sockaddr_in6);
355 sin6.sin6_addr = *in6;
356 rt = rtalloc1((struct sockaddr *)&sin6, 0);
357 if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
358 (rt->rt_ifp->if_flags & IFF_UP) != 0)
359 ret = 1;
360 else
361 ret = 0;
362 if (rt)
363 RTFREE(rt);
364 return ret;
365 }
366 #endif /* NFAITH > 0 */
367