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