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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93
30 * $FreeBSD: src/sys/net/if_loop.c,v 1.47.2.9 2004/02/08 08:40:24 silby Exp $
34 * Loopback interface driver for protocol testing and timing.
39 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
50 #include <sys/mplock2.h>
53 #include <net/if_types.h>
54 #include <net/ifq_var.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
58 #include <net/bpfdesc.h>
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
66 #include <netproto/ipx/ipx.h>
67 #include <netproto/ipx/ipx_if.h>
72 #include <netinet/in.h>
74 #include <netinet6/in6_var.h>
75 #include <netinet/ip6.h>
78 static void loopattach(void *);
79 static int looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
81 static int loioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
82 static void lortrequest(int, struct rtentry *, struct rt_addrinfo *);
84 static void lo_altqstart(struct ifnet *, struct ifaltq_subque *);
86 PSEUDO_SET(loopattach, if_loop);
89 #define LOMTU (1024+512)
90 #elif defined(LARGE_LOMTU)
96 #define LO_CSUM_FEATURES (CSUM_IP | CSUM_UDP | CSUM_TCP)
98 struct ifnet loif[NLOOP];
102 loopattach(void *dummy)
107 for (i = 0, ifp = loif; i < NLOOP; i++, ifp++) {
108 if_initname(ifp, "lo", i);
110 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
111 ifp->if_capabilities = IFCAP_HWCSUM;
112 ifp->if_hwassist = LO_CSUM_FEATURES;
113 ifp->if_capenable = ifp->if_capabilities;
114 ifp->if_ioctl = loioctl;
115 ifp->if_output = looutput;
116 ifp->if_type = IFT_LOOP;
117 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
118 ifq_set_ready(&ifp->if_snd);
120 ifp->if_start = lo_altqstart;
122 if_attach(ifp, NULL);
123 bpfattach(ifp, DLT_NULL, sizeof(u_int));
128 looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
133 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
135 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
136 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
139 IFNET_STAT_INC(ifp, opackets, 1);
140 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len);
142 switch (dst->sa_family) {
149 kprintf("looutput: af=%d unexpected\n", dst->sa_family);
151 return (EAFNOSUPPORT);
155 if (ifp->if_capenable & IFCAP_RXCSUM) {
158 if (m->m_pkthdr.csum_flags & CSUM_IP)
159 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
160 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
161 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
163 m->m_pkthdr.csum_flags |= csum_flags;
164 if (csum_flags & CSUM_DATA_VALID)
165 m->m_pkthdr.csum_data = 0xffff;
167 return (if_simloop(ifp, m, dst->sa_family, 0));
173 * This function is to support software emulation of hardware loopback,
174 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
175 * hear their own broadcasts, we create a copy of the packet that we
176 * would normally receive via a hardware loopback.
178 * This function expects the packet to include the media header of length hlen.
181 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
185 KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
186 m->m_pkthdr.rcvif = ifp;
188 /* BPF write needs to be handled specially */
189 if (af == AF_UNSPEC) {
190 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
191 af = *(mtod(m, int *));
192 m->m_len -= sizeof(int);
193 m->m_pkthdr.len -= sizeof(int);
194 m->m_data += sizeof(int);
201 if (ifp->if_bpf == NULL)
204 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
205 uint32_t bpf_af = (uint32_t)af;
206 bpf_ptap(ifp->if_bpf, m, &bpf_af, 4);
208 bpf_mtap(ifp->if_bpf, m);
214 /* Strip away media header */
220 * altq for loop is just for debugging.
221 * only used when called for loop interface (not for
222 * a simplex interface).
224 if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) {
225 struct altq_pktattr pktattr;
229 * if the queueing discipline needs packet classification,
230 * do it before prepending link headers.
232 ifq_classify(&ifp->if_snd, m, af, &pktattr);
234 M_PREPEND(m, sizeof(int32_t), MB_DONTWAIT);
237 afp = mtod(m, int32_t *);
240 return ifq_dispatch(ifp, m, &pktattr);
244 /* Deliver to upper layer protocol */
253 m->m_flags |= M_LOOP;
263 kprintf("if_simloop: can't handle af=%d\n", af);
265 return (EAFNOSUPPORT);
268 IFNET_STAT_INC(ifp, ipackets, 1);
269 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
270 netisr_queue(isr, m);
276 lo_altqstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
284 m = ifsq_dequeue(ifsq, NULL);
289 afp = mtod(m, int32_t *);
291 m_adj(m, sizeof(int32_t));
301 m->m_flags |= M_LOOP;
316 kprintf("lo_altqstart: can't handle af%d\n", af);
321 IFNET_STAT_INC(ifp, ipackets, 1);
322 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
323 netisr_queue(isr, m);
330 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
333 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
335 * For optimal performance, the send and receive buffers
336 * should be at least twice the MTU plus a little more for
339 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
344 * Process an ioctl request.
348 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
351 struct ifreq *ifr = (struct ifreq *)data;
356 ifp->if_flags |= IFF_UP | IFF_RUNNING;
357 ifa = (struct ifaddr *)data;
358 ifa->ifa_rtrequest = lortrequest;
360 * Everything else is done at a higher level.
367 error = EAFNOSUPPORT; /* XXX */
370 switch (ifr->ifr_addr.sa_family) {
382 error = EAFNOSUPPORT;
388 ifp->if_mtu = ifr->ifr_mtu;
395 mask = (ifr->ifr_reqcap ^ ifp->if_capenable) & IFCAP_HWCSUM;
397 ifp->if_capenable ^= mask;
398 if (IFCAP_TXCSUM & ifp->if_capenable)
399 ifp->if_hwassist = LO_CSUM_FEATURES;
401 ifp->if_hwassist = 0;