2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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 $
39 * Loopback interface driver for protocol testing and timing.
43 #include "opt_atalk.h"
45 #include "opt_inet6.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
57 #include <net/if_types.h>
58 #include <net/ifq_var.h>
59 #include <net/netisr.h>
60 #include <net/route.h>
62 #include <net/bpfdesc.h>
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
70 #include <netproto/ipx/ipx.h>
71 #include <netproto/ipx/ipx_if.h>
76 #include <netinet/in.h>
78 #include <netinet6/in6_var.h>
79 #include <netinet/ip6.h>
84 #include <netns/ns_if.h>
88 #include <netproto/atalk/at.h>
89 #include <netproto/atalk/at_var.h>
92 static void loopattach(void *);
93 static int looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
95 static int loioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
96 static void lortrequest(int, struct rtentry *, struct rt_addrinfo *);
98 static void lo_altqstart(struct ifnet *);
100 PSEUDO_SET(loopattach, if_loop);
103 #define LOMTU (1024+512)
104 #elif defined(LARGE_LOMTU)
110 #define LO_CSUM_FEATURES (CSUM_IP | CSUM_UDP | CSUM_TCP)
112 struct ifnet loif[NLOOP];
116 loopattach(void *dummy)
121 for (i = 0, ifp = loif; i < NLOOP; i++, ifp++) {
122 if_initname(ifp, "lo", i);
124 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
125 ifp->if_capabilities = IFCAP_HWCSUM;
126 ifp->if_hwassist = LO_CSUM_FEATURES;
127 ifp->if_capenable = ifp->if_capabilities;
128 ifp->if_ioctl = loioctl;
129 ifp->if_output = looutput;
130 ifp->if_type = IFT_LOOP;
131 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
132 ifq_set_ready(&ifp->if_snd);
134 ifp->if_start = lo_altqstart;
136 if_attach(ifp, NULL);
137 bpfattach(ifp, DLT_NULL, sizeof(u_int));
142 looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
147 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
149 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
150 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
154 ifp->if_obytes += m->m_pkthdr.len;
156 switch (dst->sa_family) {
164 kprintf("looutput: af=%d unexpected\n", dst->sa_family);
166 return (EAFNOSUPPORT);
170 if (ifp->if_capenable & IFCAP_RXCSUM) {
173 if (m->m_pkthdr.csum_flags & CSUM_IP)
174 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
175 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
176 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
178 m->m_pkthdr.csum_flags |= csum_flags;
179 if (csum_flags & CSUM_DATA_VALID)
180 m->m_pkthdr.csum_data = 0xffff;
182 return (if_simloop(ifp, m, dst->sa_family, 0));
188 * This function is to support software emulation of hardware loopback,
189 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
190 * hear their own broadcasts, we create a copy of the packet that we
191 * would normally receive via a hardware loopback.
193 * This function expects the packet to include the media header of length hlen.
196 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
200 KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
201 m->m_pkthdr.rcvif = ifp;
203 /* BPF write needs to be handled specially */
204 if (af == AF_UNSPEC) {
205 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
206 af = *(mtod(m, int *));
207 m->m_len -= sizeof(int);
208 m->m_pkthdr.len -= sizeof(int);
209 m->m_data += sizeof(int);
216 if (ifp->if_bpf == NULL)
219 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
220 uint32_t bpf_af = (uint32_t)af;
221 bpf_ptap(ifp->if_bpf, m, &bpf_af, 4);
223 bpf_mtap(ifp->if_bpf, m);
229 /* Strip away media header */
235 * altq for loop is just for debugging.
236 * only used when called for loop interface (not for
237 * a simplex interface).
239 if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) {
240 struct altq_pktattr pktattr;
245 * if the queueing discipline needs packet classification,
246 * do it before prepending link headers.
248 ifq_classify(&ifp->if_snd, m, af, &pktattr);
250 M_PREPEND(m, sizeof(int32_t), MB_DONTWAIT);
253 afp = mtod(m, int32_t *);
257 * A critical section is needed for subsystems protected by
258 * the MP lock, and the serializer is assumed to already
259 * be held for MPSAFE subsystems.
262 error = ifq_enqueue(&ifp->if_snd, m, &pktattr);
263 ifnet_serialize_tx(ifp);
265 ifnet_deserialize_tx(ifp);
271 /* Deliver to upper layer protocol */
280 m->m_flags |= M_LOOP;
300 kprintf("if_simloop: can't handle af=%d\n", af);
302 return (EAFNOSUPPORT);
306 ifp->if_ibytes += m->m_pkthdr.len;
307 netisr_queue(isr, m);
313 lo_altqstart(struct ifnet *ifp)
321 m = ifq_dequeue(&ifp->if_snd, NULL);
326 afp = mtod(m, int32_t *);
328 m_adj(m, sizeof(int32_t));
338 m->m_flags |= M_LOOP;
363 kprintf("lo_altqstart: can't handle af%d\n", af);
369 ifp->if_ibytes += m->m_pkthdr.len;
370 netisr_queue(isr, m);
377 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
380 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
382 * For optimal performance, the send and receive buffers
383 * should be at least twice the MTU plus a little more for
386 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
391 * Process an ioctl request.
395 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
398 struct ifreq *ifr = (struct ifreq *)data;
403 ifp->if_flags |= IFF_UP | IFF_RUNNING;
404 ifa = (struct ifaddr *)data;
405 ifa->ifa_rtrequest = lortrequest;
407 * Everything else is done at a higher level.
414 error = EAFNOSUPPORT; /* XXX */
417 switch (ifr->ifr_addr.sa_family) {
429 error = EAFNOSUPPORT;
435 ifp->if_mtu = ifr->ifr_mtu;
442 mask = (ifr->ifr_reqcap ^ ifp->if_capenable) & IFCAP_HWCSUM;
444 ifp->if_capenable ^= mask;
445 if (IFCAP_TXCSUM & ifp->if_capenable)
446 ifp->if_hwassist = LO_CSUM_FEATURES;
448 ifp->if_hwassist = 0;