if_faith.c revision 1.6 1 /* $NetBSD: if_faith.c,v 1.6 1999/12/02 07:22: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 #include <sys/ioctl.h>
58 #include <sys/time.h>
59 #include <machine/cpu.h>
60
61 #include <net/if.h>
62 #include <net/if_types.h>
63 #include <net/netisr.h>
64 #include <net/route.h>
65 #include <net/bpf.h>
66
67 #ifdef INET
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip.h>
72 #endif
73
74 #ifdef INET6
75 #ifndef INET
76 #include <netinet/in.h>
77 #endif
78 #include <netinet6/in6_var.h>
79 #include <netinet6/ip6.h>
80 #endif
81
82 #include "bpfilter.h"
83
84 #ifdef __FreeBSD__
85 static int faithioctl __P((struct ifnet *, int, caddr_t));
86 #else
87 static int faithioctl __P((struct ifnet *, u_long, caddr_t));
88 #endif
89 int faithoutput __P((struct ifnet *, register struct mbuf *, struct sockaddr *,
90 register struct rtentry *));
91 static void faithrtrequest __P((int, struct rtentry *, struct sockaddr *));
92
93 void faithattach __P((void *));
94 #ifdef __FreeBSD__
95 PSEUDO_SET(faithattach, if_faith);
96 #endif
97
98 static struct ifnet faithif[NFAITH];
99
100 #define FAITHMTU 1500
101
102 /* ARGSUSED */
103 void
104 faithattach(faith)
105 void *faith;
106 {
107 register struct ifnet *ifp;
108 register int i;
109
110 for (i = 0; i < NFAITH; i++) {
111 ifp = &faithif[i];
112 bzero(ifp, sizeof(faithif[i]));
113 #ifdef __NetBSD__
114 sprintf(ifp->if_xname, "faith%d", i);
115 #else
116 ifp->if_name = "faith";
117 ifp->if_unit = i;
118 #endif
119 ifp->if_mtu = FAITHMTU;
120 /* Change to BROADCAST experimentaly to announce its prefix. */
121 ifp->if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
122 ifp->if_ioctl = faithioctl;
123 ifp->if_output = faithoutput;
124 ifp->if_type = IFT_FAITH;
125 ifp->if_hdrlen = 0;
126 ifp->if_addrlen = 0;
127 if_attach(ifp);
128 #if NBPFILTER > 0
129 #ifdef __FreeBSD__
130 bpfattach(ifp, DLT_NULL, sizeof(u_int));
131 #else
132 bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int));
133 #endif
134 #endif
135 }
136 }
137
138 int
139 faithoutput(ifp, m, dst, rt)
140 struct ifnet *ifp;
141 register struct mbuf *m;
142 struct sockaddr *dst;
143 register struct rtentry *rt;
144 {
145 int s, isr;
146 register struct ifqueue *ifq = 0;
147
148 if ((m->m_flags & M_PKTHDR) == 0)
149 panic("faithoutput no HDR");
150 #if NBPFILTER > 0
151 /* BPF write needs to be handled specially */
152 if (dst->sa_family == AF_UNSPEC) {
153 dst->sa_family = *(mtod(m, int *));
154 m->m_len -= sizeof(int);
155 m->m_pkthdr.len -= sizeof(int);
156 m->m_data += sizeof(int);
157 }
158
159 if (ifp->if_bpf) {
160 /*
161 * We need to prepend the address family as
162 * a four byte field. Cons up a faith header
163 * to pacify bpf. This is safe because bpf
164 * will only read from the mbuf (i.e., it won't
165 * try to free it or keep a pointer a to it).
166 */
167 struct mbuf m0;
168 u_int af = dst->sa_family;
169
170 m0.m_next = m;
171 m0.m_len = 4;
172 m0.m_data = (char *)⁡
173
174 #ifdef __FreeBSD__
175 bpf_mtap(ifp, &m0);
176 #else
177 bpf_mtap(ifp->if_bpf, &m0);
178 #endif
179 }
180 #endif
181
182 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
183 m_freem(m);
184 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
185 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
186 }
187 ifp->if_opackets++;
188 ifp->if_obytes += m->m_pkthdr.len;
189 switch (dst->sa_family) {
190 #ifdef INET6
191 case AF_INET6:
192 ifq = &ip6intrq;
193 isr = NETISR_IPV6;
194 break;
195 #endif
196 default:
197 m_freem(m);
198 return EAFNOSUPPORT;
199 }
200
201 /* XXX do we need more sanity checks? */
202
203 m->m_pkthdr.rcvif = ifp;
204 s = splimp();
205 if (IF_QFULL(ifq)) {
206 IF_DROP(ifq);
207 m_freem(m);
208 splx(s);
209 return (ENOBUFS);
210 }
211 IF_ENQUEUE(ifq, m);
212 schednetisr(isr);
213 ifp->if_ipackets++;
214 ifp->if_ibytes += m->m_pkthdr.len;
215 splx(s);
216 return (0);
217 }
218
219 /* ARGSUSED */
220 static void
221 faithrtrequest(cmd, rt, sa)
222 int cmd;
223 struct rtentry *rt;
224 struct sockaddr *sa;
225 {
226 if (rt) {
227 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
228 /*
229 * For optimal performance, the send and receive buffers
230 * should be at least twice the MTU plus a little more for
231 * overhead.
232 */
233 rt->rt_rmx.rmx_recvpipe =
234 rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU;
235 }
236 }
237
238 /*
239 * Process an ioctl request.
240 */
241 /* ARGSUSED */
242 static int
243 faithioctl(ifp, cmd, data)
244 register struct ifnet *ifp;
245 #ifdef __FreeBSD__
246 int cmd;
247 #else
248 u_long cmd;
249 #endif
250 caddr_t data;
251 {
252 register struct ifaddr *ifa;
253 register struct ifreq *ifr = (struct ifreq *)data;
254 register int error = 0;
255
256 switch (cmd) {
257
258 case SIOCSIFADDR:
259 ifp->if_flags |= IFF_UP | IFF_RUNNING;
260 ifa = (struct ifaddr *)data;
261 ifa->ifa_rtrequest = faithrtrequest;
262 /*
263 * Everything else is done at a higher level.
264 */
265 break;
266
267 case SIOCADDMULTI:
268 case SIOCDELMULTI:
269 if (ifr == 0) {
270 error = EAFNOSUPPORT; /* XXX */
271 break;
272 }
273 switch (ifr->ifr_addr.sa_family) {
274 #ifdef INET6
275 case AF_INET6:
276 break;
277 #endif
278
279 default:
280 error = EAFNOSUPPORT;
281 break;
282 }
283 break;
284
285 #ifdef SIOCSIFMTU
286 case SIOCSIFMTU:
287 ifp->if_mtu = ifr->ifr_mtu;
288 break;
289 #endif
290
291 case SIOCSIFFLAGS:
292 break;
293
294 default:
295 error = EINVAL;
296 }
297 return (error);
298 }
299 #endif /* NFAITH > 0 */
300