xref: /freebsd/sys/net/if_loop.c (revision 4a0f765fbf09711e612e86fce8bb09ec43f482d9)
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  *	@(#)if_loop.c	8.1 (Berkeley) 6/10/93
34  * $Id$
35  */
36 
37 /*
38  * Loopback interface driver for protocol testing and timing.
39  */
40 #include "loop.h"
41 #if NLOOP > 0
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/errno.h>
49 #include <sys/ioctl.h>
50 #include <sys/time.h>
51 
52 #include <net/if.h>
53 #include <net/if_types.h>
54 #include <net/netisr.h>
55 #include <net/route.h>
56 #include <net/bpf.h>
57 
58 #ifdef	INET
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #endif
64 
65 #ifdef IPX
66 #include <netipx/ipx.h>
67 #include <netipx/ipx_if.h>
68 #endif
69 
70 #ifdef NS
71 #include <netns/ns.h>
72 #include <netns/ns_if.h>
73 #endif
74 
75 #ifdef ISO
76 #include <netiso/iso.h>
77 #include <netiso/iso_var.h>
78 #endif
79 
80 #ifdef NETATALK
81 #include <netinet/if_ether.h>
82 #include <netatalk/at.h>
83 #include <netatalk/at_var.h>
84 #endif NETATALK
85 
86 #include "bpfilter.h"
87 
88 static int loioctl __P((struct ifnet *, int, caddr_t));
89 static void lortrequest __P((int, struct rtentry *, struct sockaddr *));
90 
91 static void loopattach __P((void *));
92 PSEUDO_SET(loopattach, if_loop);
93 
94 #ifdef TINY_LOMTU
95 #define	LOMTU	(1024+512)
96 #else
97 #define LOMTU	16384
98 #endif
99 
100 struct	ifnet loif[NLOOP];
101 
102 /* ARGSUSED */
103 static void
104 loopattach(dummy)
105 	void *dummy;
106 {
107 	register struct ifnet *ifp;
108 	register int i = 0;
109 
110 	for (ifp = loif; i < NLOOP; ifp++) {
111 	    ifp->if_name = "lo";
112 	    ifp->if_unit = i++;
113 	    ifp->if_mtu = LOMTU;
114 	    ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
115 	    ifp->if_ioctl = loioctl;
116 	    ifp->if_output = looutput;
117 	    ifp->if_type = IFT_LOOP;
118 	    if_attach(ifp);
119 #if NBPFILTER > 0
120 	    bpfattach(ifp, DLT_NULL, sizeof(u_int));
121 #endif
122 	}
123 }
124 
125 int
126 looutput(ifp, m, dst, rt)
127 	struct ifnet *ifp;
128 	register struct mbuf *m;
129 	struct sockaddr *dst;
130 	register struct rtentry *rt;
131 {
132 	int s, isr;
133 	register struct ifqueue *ifq = 0;
134 
135 	if ((m->m_flags & M_PKTHDR) == 0)
136 		panic("looutput no HDR");
137 #if NBPFILTER > 0
138 	/* BPF write needs to be handled specially */
139 	if (dst->sa_family == AF_UNSPEC) {
140 		dst->sa_family = *(mtod(m, int *));
141 		m->m_len -= sizeof(int);
142 		m->m_pkthdr.len -= sizeof(int);
143 		m->m_data += sizeof(int);
144 	}
145 
146 	if (ifp->if_bpf) {
147 		/*
148 		 * We need to prepend the address family as
149 		 * a four byte field.  Cons up a dummy header
150 		 * to pacify bpf.  This is safe because bpf
151 		 * will only read from the mbuf (i.e., it won't
152 		 * try to free it or keep a pointer a to it).
153 		 */
154 		struct mbuf m0;
155 		u_int af = dst->sa_family;
156 
157 		m0.m_next = m;
158 		m0.m_len = 4;
159 		m0.m_data = (char *)&af;
160 
161 		bpf_mtap(ifp, &m0);
162 	}
163 #endif
164 	m->m_pkthdr.rcvif = ifp;
165 
166 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
167 		m_freem(m);
168 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
169 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
170 	}
171 	ifp->if_opackets++;
172 	ifp->if_obytes += m->m_pkthdr.len;
173 	switch (dst->sa_family) {
174 
175 #ifdef INET
176 	case AF_INET:
177 		ifq = &ipintrq;
178 		isr = NETISR_IP;
179 		break;
180 #endif
181 #ifdef IPX
182 	case AF_IPX:
183 		ifq = &ipxintrq;
184 		isr = NETISR_IPX;
185 		break;
186 #endif
187 #ifdef NS
188 	case AF_NS:
189 		ifq = &nsintrq;
190 		isr = NETISR_NS;
191 		break;
192 #endif
193 #ifdef ISO
194 	case AF_ISO:
195 		ifq = &clnlintrq;
196 		isr = NETISR_ISO;
197 		break;
198 #endif
199 #ifdef NETATALK
200 	case AF_APPLETALK:
201 	        ifq = &atintrq2;
202 		isr = NETISR_ATALK;
203 		break;
204 #endif NETATALK
205 	default:
206 		printf("lo%d: can't handle af%d\n", ifp->if_unit,
207 			dst->sa_family);
208 		m_freem(m);
209 		return (EAFNOSUPPORT);
210 	}
211 	s = splimp();
212 	if (IF_QFULL(ifq)) {
213 		IF_DROP(ifq);
214 		m_freem(m);
215 		splx(s);
216 		return (ENOBUFS);
217 	}
218 	IF_ENQUEUE(ifq, m);
219 	schednetisr(isr);
220 	ifp->if_ipackets++;
221 	ifp->if_ibytes += m->m_pkthdr.len;
222 	splx(s);
223 	return (0);
224 }
225 
226 /* ARGSUSED */
227 static void
228 lortrequest(cmd, rt, sa)
229 	int cmd;
230 	struct rtentry *rt;
231 	struct sockaddr *sa;
232 {
233 	if (rt) {
234 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
235 		/*
236 		 * For optimal performance, the send and receive buffers
237 		 * should be at least twice the MTU plus a little more for
238 		 * overhead.
239 		 */
240 		rt->rt_rmx.rmx_recvpipe =
241 			rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
242 	}
243 }
244 
245 /*
246  * Process an ioctl request.
247  */
248 /* ARGSUSED */
249 static int
250 loioctl(ifp, cmd, data)
251 	register struct ifnet *ifp;
252 	int cmd;
253 	caddr_t data;
254 {
255 	register struct ifaddr *ifa;
256 	register struct ifreq *ifr = (struct ifreq *)data;
257 	register int error = 0;
258 
259 	switch (cmd) {
260 
261 	case SIOCSIFADDR:
262 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
263 		ifa = (struct ifaddr *)data;
264 		ifa->ifa_rtrequest = lortrequest;
265 		/*
266 		 * Everything else is done at a higher level.
267 		 */
268 		break;
269 
270 	case SIOCADDMULTI:
271 	case SIOCDELMULTI:
272 		if (ifr == 0) {
273 			error = EAFNOSUPPORT;		/* XXX */
274 			break;
275 		}
276 		switch (ifr->ifr_addr.sa_family) {
277 
278 #ifdef INET
279 		case AF_INET:
280 			break;
281 #endif
282 
283 		default:
284 			error = EAFNOSUPPORT;
285 			break;
286 		}
287 		break;
288 
289 	case SIOCSIFMTU:
290 		ifp->if_mtu = ifr->ifr_mtu;
291 		break;
292 
293 	default:
294 		error = EINVAL;
295 	}
296 	return (error);
297 }
298 #endif /* NLOOP > 0 */
299