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