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