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