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 * $Id: if_loop.c,v 1.36 1998/06/14 23:53:43 julian Exp $
38 * Loopback interface driver for protocol testing and timing.
43 #include "opt_atalk.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
55 #include <net/if_types.h>
56 #include <net/netisr.h>
57 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
66 #include <netipx/ipx.h>
67 #include <netipx/ipx_if.h>
72 #include <netns/ns_if.h>
76 #include <netiso/iso.h>
77 #include <netiso/iso_var.h>
81 #include <netatalk/at.h>
82 #include <netatalk/at_var.h>
87 #include <net/bpfdesc.h>
90 static int loioctl __P((struct ifnet *, u_long, caddr_t));
91 static void lortrequest __P((int, struct rtentry *, struct sockaddr *));
93 static void loopattach __P((void *));
94 PSEUDO_SET(loopattach, if_loop);
96 static int looutput __P((struct ifnet *ifp,
97 struct mbuf *m, struct sockaddr *dst, struct rtentry *rt));
100 #define LOMTU (1024+512)
105 struct ifnet loif[NLOOP];
112 register struct ifnet *ifp;
115 for (ifp = loif; i < NLOOP; ifp++) {
119 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
120 ifp->if_ioctl = loioctl;
121 ifp->if_output = looutput;
122 ifp->if_type = IFT_LOOP;
125 bpfattach(ifp, DLT_NULL, sizeof(u_int));
131 looutput(ifp, m, dst, rt)
133 register struct mbuf *m;
134 struct sockaddr *dst;
135 register struct rtentry *rt;
137 if ((m->m_flags & M_PKTHDR) == 0)
138 panic("looutput no HDR");
140 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
142 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
143 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
146 ifp->if_obytes += m->m_pkthdr.len;
148 switch (dst->sa_family) {
156 printf("looutput: af=%d unexpected", dst->sa_family);
158 return (EAFNOSUPPORT);
161 return(if_simloop(ifp, m, dst, 0));
167 * This function is to support software emulation of hardware loopback,
168 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
169 * hear their own broadcasts, we create a copy of the packet that we
170 * would normally receive via a hardware loopback.
172 * This function expects the packet to include the media header of length hlen.
176 if_simloop(ifp, m, dst, hlen)
178 register struct mbuf *m;
179 struct sockaddr *dst;
183 register struct ifqueue *ifq = 0;
185 if ((m->m_flags & M_PKTHDR) == 0)
186 panic("if_simloop: no HDR");
187 m->m_pkthdr.rcvif = ifp;
189 /* BPF write needs to be handled specially */
190 if (dst->sa_family == AF_UNSPEC) {
191 dst->sa_family = *(mtod(m, int *));
192 m->m_len -= sizeof(int);
193 m->m_pkthdr.len -= sizeof(int);
194 m->m_data += sizeof(int);
198 struct mbuf m0, *n = m;
199 u_int af = dst->sa_family;
201 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
203 * We need to prepend the address family as
204 * a four byte field. Cons up a dummy header
205 * to pacify bpf. This is safe because bpf
206 * will only read from the mbuf (i.e., it won't
207 * try to free it or keep a pointer a to it).
211 m0.m_data = (char *)⁡
218 /* Strip away media header */
221 /* The alpha doesn't like unaligned data.
222 * We move data down in the first mbuf */
224 bcopy(m->m_data + hlen, m->m_data, m->m_len - hlen);
226 if (m->m_flags & M_PKTHDR)
227 m->m_pkthdr.len -= hlen;
233 switch (dst->sa_family) {
265 printf("if_simloop: can't handle af=%d\n", dst->sa_family);
267 return (EAFNOSUPPORT);
279 ifp->if_ibytes += m->m_pkthdr.len;
286 lortrequest(cmd, rt, sa)
292 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
294 * For optimal performance, the send and receive buffers
295 * should be at least twice the MTU plus a little more for
298 rt->rt_rmx.rmx_recvpipe =
299 rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
304 * Process an ioctl request.
308 loioctl(ifp, cmd, data)
309 register struct ifnet *ifp;
313 register struct ifaddr *ifa;
314 register struct ifreq *ifr = (struct ifreq *)data;
315 register int error = 0;
320 ifp->if_flags |= IFF_UP | IFF_RUNNING;
321 ifa = (struct ifaddr *)data;
322 ifa->ifa_rtrequest = lortrequest;
324 * Everything else is done at a higher level.
331 error = EAFNOSUPPORT; /* XXX */
334 switch (ifr->ifr_addr.sa_family) {
342 error = EAFNOSUPPORT;
348 ifp->if_mtu = ifr->ifr_mtu;
359 #endif /* NLOOP > 0 */