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