xref: /netbsd-src/sys/net/if_loop.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: if_loop.c,v 1.30 2000/03/30 09:45:36 augustss 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 <netinet/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 	int i;
147 	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 	struct mbuf *m;
171 	struct sockaddr *dst;
172 	struct rtentry *rt;
173 {
174 	int s, isr;
175 	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 #ifndef PULLDOWN_TEST
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 other conditions to avoid running this part?
213 	 */
214 	if (m->m_len != m->m_pkthdr.len) {
215 		struct mbuf *n = NULL;
216 		int maxlen;
217 
218 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
219 		maxlen = MHLEN;
220 		if (n)
221 			M_COPY_PKTHDR(n, m);
222 		if (n && m->m_pkthdr.len > maxlen) {
223 			MCLGET(n, M_DONTWAIT);
224 			maxlen = MCLBYTES;
225 			if ((n->m_flags & M_EXT) == 0) {
226 				m_free(n);
227 				n = NULL;
228 			}
229 		}
230 		if (!n) {
231 			printf("looutput: mbuf allocation failed\n");
232 			m_freem(m);
233 			return ENOBUFS;
234 		}
235 
236 		if (m->m_pkthdr.len <= maxlen) {
237 			m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
238 			n->m_len = m->m_pkthdr.len;
239 			n->m_next = NULL;
240 			m_freem(m);
241 		} else {
242 			m_copydata(m, 0, maxlen, mtod(n, caddr_t));
243 			m_adj(m, maxlen);
244 			n->m_len = maxlen;
245 			n->m_next = m;
246 			m->m_flags &= ~M_PKTHDR;
247 		}
248 		m = n;
249 	}
250 #if 0
251 	if (m && m->m_next != NULL) {
252 		printf("loop: not contiguous...\n");
253 		m_freem(m);
254 		return ENOBUFS;
255 	}
256 #endif
257 #endif
258 
259 	ifp->if_opackets++;
260 	ifp->if_obytes += m->m_pkthdr.len;
261 	switch (dst->sa_family) {
262 
263 #ifdef INET
264 	case AF_INET:
265 		ifq = &ipintrq;
266 		isr = NETISR_IP;
267 		break;
268 #endif
269 #ifdef INET6
270 	case AF_INET6:
271 		m->m_flags |= M_LOOP;
272 		ifq = &ip6intrq;
273 		isr = NETISR_IPV6;
274 		break;
275 #endif
276 #ifdef NS
277 	case AF_NS:
278 		ifq = &nsintrq;
279 		isr = NETISR_NS;
280 		break;
281 #endif
282 #ifdef ISO
283 	case AF_ISO:
284 		ifq = &clnlintrq;
285 		isr = NETISR_ISO;
286 		break;
287 #endif
288 #ifdef IPX
289 	case AF_IPX:
290 		ifq = &ipxintrq;
291 		isr = NETISR_IPX;
292 		break;
293 #endif
294 #ifdef NETATALK
295 	case AF_APPLETALK:
296 	        ifq = &atintrq2;
297 		isr = NETISR_ATALK;
298 		break;
299 #endif
300 	default:
301 		printf("%s: can't handle af%d\n", ifp->if_xname,
302 		    dst->sa_family);
303 		m_freem(m);
304 		return (EAFNOSUPPORT);
305 	}
306 	s = splimp();
307 	if (IF_QFULL(ifq)) {
308 		IF_DROP(ifq);
309 		m_freem(m);
310 		splx(s);
311 		return (ENOBUFS);
312 	}
313 	IF_ENQUEUE(ifq, m);
314 	schednetisr(isr);
315 	ifp->if_ipackets++;
316 	ifp->if_ibytes += m->m_pkthdr.len;
317 	splx(s);
318 	return (0);
319 }
320 
321 /* ARGSUSED */
322 void
323 lortrequest(cmd, rt, sa)
324 	int cmd;
325 	struct rtentry *rt;
326 	struct sockaddr *sa;
327 {
328 
329 	if (rt)
330 		rt->rt_rmx.rmx_mtu = LOMTU;
331 }
332 
333 /*
334  * Process an ioctl request.
335  */
336 /* ARGSUSED */
337 int
338 loioctl(ifp, cmd, data)
339 	struct ifnet *ifp;
340 	u_long cmd;
341 	caddr_t data;
342 {
343 	struct ifaddr *ifa;
344 	struct ifreq *ifr;
345 	int error = 0;
346 
347 	switch (cmd) {
348 
349 	case SIOCSIFADDR:
350 		ifp->if_flags |= IFF_UP;
351 		ifa = (struct ifaddr *)data;
352 		if (ifa != 0 /*&& ifa->ifa_addr->sa_family == AF_ISO*/)
353 			ifa->ifa_rtrequest = lortrequest;
354 		/*
355 		 * Everything else is done at a higher level.
356 		 */
357 		break;
358 
359 	case SIOCADDMULTI:
360 	case SIOCDELMULTI:
361 		ifr = (struct ifreq *)data;
362 		if (ifr == 0) {
363 			error = EAFNOSUPPORT;		/* XXX */
364 			break;
365 		}
366 		switch (ifr->ifr_addr.sa_family) {
367 
368 #ifdef INET
369 		case AF_INET:
370 			break;
371 #endif
372 #ifdef INET6
373 		case AF_INET6:
374 			break;
375 #endif
376 
377 		default:
378 			error = EAFNOSUPPORT;
379 			break;
380 		}
381 		break;
382 
383 	default:
384 		error = EINVAL;
385 	}
386 	return (error);
387 }
388