xref: /netbsd-src/sys/net/if_loop.c (revision 3b435a73967be44dfb4a27315acd72bfacde430c)
1 /*	$NetBSD: if_loop.c,v 1.26 1999/07/01 08:12:48 itojun Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * 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. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1986, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *	This product includes software developed by the University of
47  *	California, Berkeley and its contributors.
48  * 4. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *	@(#)if_loop.c	8.2 (Berkeley) 1/9/95
65  */
66 
67 /*
68  * Loopback interface driver for protocol testing and timing.
69  */
70 
71 #include "opt_inet.h"
72 #include "opt_atalk.h"
73 #include "opt_iso.h"
74 #include "opt_ns.h"
75 
76 #include "bpfilter.h"
77 #include "loop.h"
78 
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/mbuf.h>
83 #include <sys/socket.h>
84 #include <sys/errno.h>
85 #include <sys/ioctl.h>
86 #include <sys/time.h>
87 
88 #include <machine/cpu.h>
89 
90 #include <net/if.h>
91 #include <net/if_types.h>
92 #include <net/netisr.h>
93 #include <net/route.h>
94 
95 #ifdef	INET
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/in_var.h>
99 #include <netinet/ip.h>
100 #endif
101 
102 #ifdef INET6
103 #ifndef INET
104 #include <netinet/in.h>
105 #endif
106 #include <netinet6/in6_var.h>
107 #include <netinet6/ip6.h>
108 #endif
109 
110 #ifdef NS
111 #include <netns/ns.h>
112 #include <netns/ns_if.h>
113 #endif
114 
115 #ifdef IPX
116 #include <netipx/ipx.h>
117 #include <netipx/ipx_if.h>
118 #endif
119 
120 #ifdef ISO
121 #include <netiso/iso.h>
122 #include <netiso/iso_var.h>
123 #endif
124 
125 #ifdef NETATALK
126 #include <netatalk/at.h>
127 #include <netatalk/at_var.h>
128 #endif
129 
130 #if NBPFILTER > 0
131 #include <net/bpf.h>
132 #endif
133 
134 #if defined(LARGE_LOMTU)
135 #define LOMTU	(131072 +  MHLEN + MLEN)
136 #else
137 #define	LOMTU	(32768 +  MHLEN + MLEN)
138 #endif
139 
140 struct	ifnet loif[NLOOP];
141 
142 void
143 loopattach(n)
144 	int n;
145 {
146 	register int i;
147 	register struct ifnet *ifp;
148 
149 	for (i = 0; i < NLOOP; i++) {
150 		ifp = &loif[i];
151 		sprintf(ifp->if_xname, "lo%d", i);
152 		ifp->if_softc = NULL;
153 		ifp->if_mtu = LOMTU;
154 		ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
155 		ifp->if_ioctl = loioctl;
156 		ifp->if_output = looutput;
157 		ifp->if_type = IFT_LOOP;
158 		ifp->if_hdrlen = 0;
159 		ifp->if_addrlen = 0;
160 		if_attach(ifp);
161 #if NBPFILTER > 0
162 		bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int));
163 #endif
164 	}
165 }
166 
167 int
168 looutput(ifp, m, dst, rt)
169 	struct ifnet *ifp;
170 	register struct mbuf *m;
171 	struct sockaddr *dst;
172 	register struct rtentry *rt;
173 {
174 	int s, isr;
175 	register struct ifqueue *ifq = 0;
176 
177 	if ((m->m_flags & M_PKTHDR) == 0)
178 		panic("looutput: no header mbuf");
179 	ifp->if_lastchange = time;
180 #if NBPFILTER > 0
181 	if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK)) {
182 		/*
183 		 * We need to prepend the address family as
184 		 * a four byte field.  Cons up a dummy header
185 		 * to pacify bpf.  This is safe because bpf
186 		 * will only read from the mbuf (i.e., it won't
187 		 * try to free it or keep a pointer to it).
188 		 */
189 		struct mbuf m0;
190 		u_int af = dst->sa_family;
191 
192 		m0.m_next = m;
193 		m0.m_len = 4;
194 		m0.m_data = (char *)&af;
195 
196 		bpf_mtap(ifp->if_bpf, &m0);
197 	}
198 #endif
199 	m->m_pkthdr.rcvif = ifp;
200 
201 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
202 		m_freem(m);
203 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
204 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
205 	}
206 
207 #ifdef INET6
208 	/*
209 	 * KAME requires that the packet to be contiguous on the
210 	 * mbuf.  We need to make that sure.
211 	 * this kind of code should be avoided.
212 	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
213 	 */
214 	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
215 		struct mbuf *n;
216 
217 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
218 		if (!n)
219 			goto contiguousfail;
220 		MCLGET(n, M_DONTWAIT);
221 		if (! (n->m_flags & M_EXT)) {
222 			m_freem(n);
223 			goto contiguousfail;
224 		}
225 
226 		m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
227 		n->m_pkthdr.rcvif = m->m_pkthdr.rcvif;
228 		n->m_pkthdr.len = m->m_pkthdr.len;
229 		n->m_len = m->m_pkthdr.len;
230 		m_freem(m);
231 		m = n;
232 	}
233 	if (0) {
234 contiguousfail:
235 		printf("looutput: mbuf allocation failed\n");
236 	}
237 #if 0
238 	if (m && m->m_next != NULL)
239 		printf("loop: not contiguous...\n");
240 #endif
241 #endif
242 
243 	ifp->if_opackets++;
244 	ifp->if_obytes += m->m_pkthdr.len;
245 	switch (dst->sa_family) {
246 
247 #ifdef INET
248 	case AF_INET:
249 		ifq = &ipintrq;
250 		isr = NETISR_IP;
251 		break;
252 #endif
253 #ifdef INET6
254 	case AF_INET6:
255 		m->m_flags |= M_LOOP;
256 		ifq = &ip6intrq;
257 		isr = NETISR_IPV6;
258 		break;
259 #endif
260 #ifdef NS
261 	case AF_NS:
262 		ifq = &nsintrq;
263 		isr = NETISR_NS;
264 		break;
265 #endif
266 #ifdef ISO
267 	case AF_ISO:
268 		ifq = &clnlintrq;
269 		isr = NETISR_ISO;
270 		break;
271 #endif
272 #ifdef IPX
273 	case AF_IPX:
274 		ifq = &ipxintrq;
275 		isr = NETISR_IPX;
276 		break;
277 #endif
278 #ifdef NETATALK
279 	case AF_APPLETALK:
280 	        ifq = &atintrq2;
281 		isr = NETISR_ATALK;
282 		break;
283 #endif
284 	default:
285 		printf("%s: can't handle af%d\n", ifp->if_xname,
286 		    dst->sa_family);
287 		m_freem(m);
288 		return (EAFNOSUPPORT);
289 	}
290 	s = splimp();
291 	if (IF_QFULL(ifq)) {
292 		IF_DROP(ifq);
293 		m_freem(m);
294 		splx(s);
295 		return (ENOBUFS);
296 	}
297 	IF_ENQUEUE(ifq, m);
298 	schednetisr(isr);
299 	ifp->if_ipackets++;
300 	ifp->if_ibytes += m->m_pkthdr.len;
301 	splx(s);
302 	return (0);
303 }
304 
305 /* ARGSUSED */
306 void
307 lortrequest(cmd, rt, sa)
308 	int cmd;
309 	struct rtentry *rt;
310 	struct sockaddr *sa;
311 {
312 
313 	if (rt)
314 		rt->rt_rmx.rmx_mtu = LOMTU;
315 }
316 
317 /*
318  * Process an ioctl request.
319  */
320 /* ARGSUSED */
321 int
322 loioctl(ifp, cmd, data)
323 	register struct ifnet *ifp;
324 	u_long cmd;
325 	caddr_t data;
326 {
327 	register struct ifaddr *ifa;
328 	register struct ifreq *ifr;
329 	register int error = 0;
330 
331 	switch (cmd) {
332 
333 	case SIOCSIFADDR:
334 		ifp->if_flags |= IFF_UP;
335 		ifa = (struct ifaddr *)data;
336 		if (ifa != 0 /*&& ifa->ifa_addr->sa_family == AF_ISO*/)
337 			ifa->ifa_rtrequest = lortrequest;
338 		/*
339 		 * Everything else is done at a higher level.
340 		 */
341 		break;
342 
343 	case SIOCADDMULTI:
344 	case SIOCDELMULTI:
345 		ifr = (struct ifreq *)data;
346 		if (ifr == 0) {
347 			error = EAFNOSUPPORT;		/* XXX */
348 			break;
349 		}
350 		switch (ifr->ifr_addr.sa_family) {
351 
352 #ifdef INET
353 		case AF_INET:
354 			break;
355 #endif
356 #ifdef INET6
357 		case AF_INET6:
358 			break;
359 #endif
360 
361 		default:
362 			error = EAFNOSUPPORT;
363 			break;
364 		}
365 		break;
366 
367 	default:
368 		error = EINVAL;
369 	}
370 	return (error);
371 }
372