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.8 2003/06/01 01:46:11 silby Exp $
35 * $DragonFly: src/sys/net/if_loop.c,v 1.11 2004/06/02 14:42:57 eirikn 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>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
56 #include <net/if_types.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
60 #include <net/bpfdesc.h>
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
68 #include <netproto/ipx/ipx.h>
69 #include <netproto/ipx/ipx_if.h>
74 #include <netinet/in.h>
76 #include <netinet6/in6_var.h>
77 #include <netinet/ip6.h>
82 #include <netns/ns_if.h>
86 #include <netproto/atalk/at.h>
87 #include <netproto/atalk/at_var.h>
90 int loioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
91 static void lortrequest (int, struct rtentry *, struct rt_addrinfo *);
93 static void loopattach (void *);
94 PSEUDO_SET(loopattach, if_loop);
96 int looutput (struct ifnet *ifp,
97 struct mbuf *m, struct sockaddr *dst, struct rtentry *rt);
100 #define LOMTU (1024+512)
101 #elif defined(LARGE_LOMTU)
107 struct ifnet loif[NLOOP];
117 for (ifp = loif; i < NLOOP; ifp++) {
118 if_initname(ifp, "lo", i++);
120 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
121 ifp->if_ioctl = loioctl;
122 ifp->if_output = looutput;
123 ifp->if_type = IFT_LOOP;
124 ifp->if_snd.ifq_maxlen = ifqmaxlen;
126 bpfattach(ifp, DLT_NULL, sizeof(u_int));
131 looutput(ifp, m, dst, rt)
134 struct sockaddr *dst;
139 if ((m->m_flags & M_PKTHDR) == 0)
140 panic("looutput no HDR");
142 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
144 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
145 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
148 * KAME requires that the packet to be contiguous on the
149 * mbuf. We need to make that sure.
150 * this kind of code should be avoided.
152 * XXX: KAME may no longer need contiguous packets. Once
153 * that has been verified, the following code _should_ be
156 if (m && m->m_next != NULL) {
158 n = m_defrag(m, MB_DONTWAIT);
169 ifp->if_obytes += m->m_pkthdr.len;
171 switch (dst->sa_family) {
179 printf("looutput: af=%d unexpected\n", dst->sa_family);
181 return (EAFNOSUPPORT);
184 return(if_simloop(ifp, m, dst->sa_family, 0));
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.
195 * This function expects the packet to include the media header of length hlen.
199 if_simloop(ifp, m, af, hlen)
207 KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
208 m->m_pkthdr.rcvif = ifp;
210 /* BPF write needs to be handled specially */
211 if (af == AF_UNSPEC) {
212 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
213 af = *(mtod(m, int *));
214 m->m_len -= sizeof(int);
215 m->m_pkthdr.len -= sizeof(int);
216 m->m_data += sizeof(int);
219 /* Let BPF see incoming packet */
221 struct mbuf m0, *n = m;
223 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
225 * We need to prepend the address family as
226 * a four byte field. Cons up a dummy header
227 * to pacify bpf. This is safe because bpf
228 * will only read from the mbuf (i.e., it won't
229 * try to free it or keep a pointer a to it).
233 m0.m_data = (char *)⁡
239 /* Strip away media header */
243 /* The alpha doesn't like unaligned data.
244 * We move data down in the first mbuf */
245 if (mtod(m, vm_offset_t) & 3) {
246 KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
248 (char *)(mtod(m, vm_offset_t)
249 - (mtod(m, vm_offset_t) & 3)),
251 mtod(m,vm_offset_t) -= (mtod(m, vm_offset_t) & 3);
256 /* Deliver to upper layer protocol */
265 m->m_flags |= M_LOOP;
285 printf("if_simloop: can't handle af=%d\n", af);
287 return (EAFNOSUPPORT);
291 ifp->if_ibytes += m->m_pkthdr.len;
292 netisr_queue(isr, m);
298 lortrequest(cmd, rt, info)
301 struct rt_addrinfo *info;
304 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
306 * For optimal performance, the send and receive buffers
307 * should be at least twice the MTU plus a little more for
310 rt->rt_rmx.rmx_recvpipe =
311 rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
316 * Process an ioctl request.
320 loioctl(ifp, cmd, data, cr)
327 struct ifreq *ifr = (struct ifreq *)data;
333 ifp->if_flags |= IFF_UP | IFF_RUNNING;
334 ifa = (struct ifaddr *)data;
335 ifa->ifa_rtrequest = lortrequest;
337 * Everything else is done at a higher level.
344 error = EAFNOSUPPORT; /* XXX */
347 switch (ifr->ifr_addr.sa_family) {
359 error = EAFNOSUPPORT;
365 ifp->if_mtu = ifr->ifr_mtu;