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