xref: /dragonfly/sys/net/if_loop.c (revision e0ecab34)
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  * $FreeBSD: src/sys/net/if_loop.c,v 1.47.2.9 2004/02/08 08:40:24 silby Exp $
35  * $DragonFly: src/sys/net/if_loop.c,v 1.26 2008/10/04 11:21:10 sephe Exp $
36  */
37 
38 /*
39  * Loopback interface driver for protocol testing and timing.
40  */
41 #include "use_loop.h"
42 
43 #include "opt_atalk.h"
44 #include "opt_inet.h"
45 #include "opt_inet6.h"
46 #include "opt_ipx.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/lock.h>
52 #include <sys/mbuf.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 
56 #include <sys/mplock2.h>
57 
58 #include <net/if.h>
59 #include <net/if_types.h>
60 #include <net/ifq_var.h>
61 #include <net/netisr.h>
62 #include <net/route.h>
63 #include <net/bpf.h>
64 #include <net/bpfdesc.h>
65 
66 #ifdef	INET
67 #include <netinet/in.h>
68 #include <netinet/in_var.h>
69 #endif
70 
71 #ifdef IPX
72 #include <netproto/ipx/ipx.h>
73 #include <netproto/ipx/ipx_if.h>
74 #endif
75 
76 #ifdef INET6
77 #ifndef INET
78 #include <netinet/in.h>
79 #endif
80 #include <netinet6/in6_var.h>
81 #include <netinet/ip6.h>
82 #endif
83 
84 #ifdef NS
85 #include <netns/ns.h>
86 #include <netns/ns_if.h>
87 #endif
88 
89 #ifdef NETATALK
90 #include <netproto/atalk/at.h>
91 #include <netproto/atalk/at_var.h>
92 #endif
93 
94 static void	loopattach(void *);
95 static int	looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
96 			 struct rtentry *);
97 static int	loioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
98 static void	lortrequest(int, struct rtentry *, struct rt_addrinfo *);
99 #ifdef ALTQ
100 static void	lo_altqstart(struct ifnet *);
101 #endif
102 PSEUDO_SET(loopattach, if_loop);
103 
104 #ifdef TINY_LOMTU
105 #define	LOMTU	(1024+512)
106 #elif defined(LARGE_LOMTU)
107 #define LOMTU	131072
108 #else
109 #define LOMTU	16384
110 #endif
111 
112 #define LO_CSUM_FEATURES	(CSUM_IP | CSUM_UDP | CSUM_TCP)
113 
114 struct	ifnet loif[NLOOP];
115 
116 /* ARGSUSED */
117 static void
118 loopattach(void *dummy)
119 {
120 	struct ifnet *ifp;
121 	int i;
122 
123 	for (i = 0, ifp = loif; i < NLOOP; i++, ifp++) {
124 		if_initname(ifp, "lo", i);
125 		ifp->if_mtu = LOMTU;
126 		ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
127 		ifp->if_capabilities = IFCAP_HWCSUM;
128 		ifp->if_hwassist = LO_CSUM_FEATURES;
129 		ifp->if_capenable = ifp->if_capabilities;
130 		ifp->if_ioctl = loioctl;
131 		ifp->if_output = looutput;
132 		ifp->if_type = IFT_LOOP;
133 		ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
134 		ifq_set_ready(&ifp->if_snd);
135 #ifdef ALTQ
136 	        ifp->if_start = lo_altqstart;
137 #endif
138 		if_attach(ifp, NULL);
139 		bpfattach(ifp, DLT_NULL, sizeof(u_int));
140 	}
141 }
142 
143 static int
144 looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
145 	 struct rtentry *rt)
146 {
147 	M_ASSERTPKTHDR(m);
148 
149 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
150 		m_freem(m);
151 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
152 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
153 	}
154 
155 	ifp->if_opackets++;
156 	ifp->if_obytes += m->m_pkthdr.len;
157 #if 1	/* XXX */
158 	switch (dst->sa_family) {
159 	case AF_INET:
160 	case AF_INET6:
161 	case AF_IPX:
162 	case AF_NS:
163 	case AF_APPLETALK:
164 		break;
165 	default:
166 		kprintf("looutput: af=%d unexpected\n", dst->sa_family);
167 		m_freem(m);
168 		return (EAFNOSUPPORT);
169 	}
170 #endif
171 
172 	if (ifp->if_capenable & IFCAP_RXCSUM) {
173 		int csum_flags = 0;
174 
175 		if (m->m_pkthdr.csum_flags & CSUM_IP)
176 			csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
177 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
178 			csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
179 
180 		m->m_pkthdr.csum_flags |= csum_flags;
181 		if (csum_flags & CSUM_DATA_VALID)
182 			m->m_pkthdr.csum_data = 0xffff;
183 	}
184 	return (if_simloop(ifp, m, dst->sa_family, 0));
185 }
186 
187 /*
188  * if_simloop()
189  *
190  * This function is to support software emulation of hardware loopback,
191  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
192  * hear their own broadcasts, we create a copy of the packet that we
193  * would normally receive via a hardware loopback.
194  *
195  * This function expects the packet to include the media header of length hlen.
196  */
197 int
198 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
199 {
200 	int isr;
201 
202 	KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
203 	m->m_pkthdr.rcvif = ifp;
204 
205 	/* BPF write needs to be handled specially */
206 	if (af == AF_UNSPEC) {
207 		KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
208 		af = *(mtod(m, int *));
209 		m->m_len -= sizeof(int);
210 		m->m_pkthdr.len -= sizeof(int);
211 		m->m_data += sizeof(int);
212 	}
213 
214 	if (ifp->if_bpf) {
215 		get_mplock();
216 
217 		/* Re-check */
218 		if (ifp->if_bpf == NULL)
219 			goto rel;
220 
221 		if (ifp->if_bpf->bif_dlt == DLT_NULL) {
222 			uint32_t bpf_af = (uint32_t)af;
223 			bpf_ptap(ifp->if_bpf, m, &bpf_af, 4);
224 		} else {
225 			bpf_mtap(ifp->if_bpf, m);
226 		}
227 rel:
228 		rel_mplock();
229 	}
230 
231 	/* Strip away media header */
232 	if (hlen > 0)
233 		m_adj(m, hlen);
234 
235 #ifdef ALTQ
236 	/*
237 	 * altq for loop is just for debugging.
238 	 * only used when called for loop interface (not for
239 	 * a simplex interface).
240 	 */
241 	if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) {
242 		struct altq_pktattr pktattr;
243 		int32_t *afp;
244 	        int error;
245 
246 		/*
247 		 * if the queueing discipline needs packet classification,
248 		 * do it before prepending link headers.
249 		 */
250 		ifq_classify(&ifp->if_snd, m, af, &pktattr);
251 
252 		M_PREPEND(m, sizeof(int32_t), MB_DONTWAIT);
253 		if (m == 0)
254 			return(ENOBUFS);
255 		afp = mtod(m, int32_t *);
256 		*afp = (int32_t)af;
257 
258 		/*
259 		 * A critical section is needed for subsystems protected by
260 		 * the MP lock, and the serializer is assumed to already
261 		 * be held for MPSAFE subsystems.
262 		 */
263 	        crit_enter();
264 		error = ifq_enqueue(&ifp->if_snd, m, &pktattr);
265 		ifnet_serialize_tx(ifp);
266 		ifp->if_start(ifp);
267 		ifnet_deserialize_tx(ifp);
268 		crit_exit();
269 		return (error);
270 	}
271 #endif /* ALTQ */
272 
273 	/* Deliver to upper layer protocol */
274 	switch (af) {
275 #ifdef INET
276 	case AF_INET:
277 		isr = NETISR_IP;
278 		break;
279 #endif
280 #ifdef INET6
281 	case AF_INET6:
282 		m->m_flags |= M_LOOP;
283 		isr = NETISR_IPV6;
284 		break;
285 #endif
286 #ifdef IPX
287 	case AF_IPX:
288 		isr = NETISR_IPX;
289 		break;
290 #endif
291 #ifdef NS
292 	case AF_NS:
293 		isr = NETISR_NS;
294 		break;
295 #endif
296 #ifdef NETATALK
297 	case AF_APPLETALK:
298 		isr = NETISR_ATALK2;
299 		break;
300 #endif
301 	default:
302 		kprintf("if_simloop: can't handle af=%d\n", af);
303 		m_freem(m);
304 		return (EAFNOSUPPORT);
305 	}
306 
307 	ifp->if_ipackets++;
308 	ifp->if_ibytes += m->m_pkthdr.len;
309 	netisr_queue(isr, m);
310 	return (0);
311 }
312 
313 #ifdef ALTQ
314 static void
315 lo_altqstart(struct ifnet *ifp)
316 {
317 	struct mbuf *m;
318 	int32_t af, *afp;
319 	int isr;
320 
321 	while (1) {
322 		crit_enter();
323 		m = ifq_dequeue(&ifp->if_snd, NULL);
324 		crit_exit();
325 		if (m == NULL)
326 			return;
327 
328 		afp = mtod(m, int32_t *);
329 		af = *afp;
330 		m_adj(m, sizeof(int32_t));
331 
332 		switch (af) {
333 #ifdef INET
334 		case AF_INET:
335 			isr = NETISR_IP;
336 			break;
337 #endif
338 #ifdef INET6
339 		case AF_INET6:
340 			m->m_flags |= M_LOOP;
341 			isr = NETISR_IPV6;
342 			break;
343 #endif
344 #ifdef IPX
345 		case AF_IPX:
346 			isr = NETISR_IPX;
347 			break;
348 #endif
349 #ifdef NS
350 		case AF_NS:
351 			isr = NETISR_NS;
352 			break;
353 #endif
354 #ifdef ISO
355 		case AF_ISO:
356 			isr = NETISR_ISO;
357 			break;
358 #endif
359 #ifdef NETATALK
360 		case AF_APPLETALK:
361 			isr = NETISR_ATALK2;
362 			break;
363 #endif
364 		default:
365 			kprintf("lo_altqstart: can't handle af%d\n", af);
366 			m_freem(m);
367 			return;
368 		}
369 
370 		ifp->if_ipackets++;
371 		ifp->if_ibytes += m->m_pkthdr.len;
372 		netisr_queue(isr, m);
373 	}
374 }
375 #endif /* ALTQ */
376 
377 /* ARGSUSED */
378 static void
379 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
380 {
381 	if (rt) {
382 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
383 		/*
384 		 * For optimal performance, the send and receive buffers
385 		 * should be at least twice the MTU plus a little more for
386 		 * overhead.
387 		 */
388 		rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
389 	}
390 }
391 
392 /*
393  * Process an ioctl request.
394  */
395 /* ARGSUSED */
396 static int
397 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
398 {
399 	struct ifaddr *ifa;
400 	struct ifreq *ifr = (struct ifreq *)data;
401 	int error = 0, mask;
402 
403 	switch (cmd) {
404 	case SIOCSIFADDR:
405 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
406 		ifa = (struct ifaddr *)data;
407 		ifa->ifa_rtrequest = lortrequest;
408 		/*
409 		 * Everything else is done at a higher level.
410 		 */
411 		break;
412 
413 	case SIOCADDMULTI:
414 	case SIOCDELMULTI:
415 		if (ifr == 0) {
416 			error = EAFNOSUPPORT;		/* XXX */
417 			break;
418 		}
419 		switch (ifr->ifr_addr.sa_family) {
420 
421 #ifdef INET
422 		case AF_INET:
423 			break;
424 #endif
425 #ifdef INET6
426 		case AF_INET6:
427 			break;
428 #endif
429 
430 		default:
431 			error = EAFNOSUPPORT;
432 			break;
433 		}
434 		break;
435 
436 	case SIOCSIFMTU:
437 		ifp->if_mtu = ifr->ifr_mtu;
438 		break;
439 
440 	case SIOCSIFFLAGS:
441 		break;
442 
443 	case SIOCSIFCAP:
444 		mask = (ifr->ifr_reqcap ^ ifp->if_capenable) & IFCAP_HWCSUM;
445 		if (mask) {
446 			ifp->if_capenable ^= mask;
447 			if (IFCAP_TXCSUM & ifp->if_capenable)
448 				ifp->if_hwassist = LO_CSUM_FEATURES;
449 			else
450 				ifp->if_hwassist = 0;
451 		}
452 		break;
453 
454 	default:
455 		error = EINVAL;
456 	}
457 	return (error);
458 }
459