xref: /netbsd-src/sys/net/if_loop.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: if_loop.c,v 1.20 1997/08/14 01:12:35 jonathan Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)if_loop.c	8.1 (Berkeley) 6/10/93
36  */
37 
38 /*
39  * Loopback interface driver for protocol testing and timing.
40  */
41 
42 #include "bpfilter.h"
43 #include "loop.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/errno.h>
51 #include <sys/ioctl.h>
52 #include <sys/time.h>
53 
54 #include <machine/cpu.h>
55 
56 #include <net/if.h>
57 #include <net/if_types.h>
58 #include <net/netisr.h>
59 #include <net/route.h>
60 
61 #ifdef	INET
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip.h>
66 #endif
67 
68 #ifdef NS
69 #include <netns/ns.h>
70 #include <netns/ns_if.h>
71 #endif
72 
73 #ifdef ISO
74 #include <netiso/iso.h>
75 #include <netiso/iso_var.h>
76 #endif
77 
78 #ifdef NETATALK
79 #include <netatalk/at.h>
80 #include <netatalk/at_var.h>
81 #endif
82 
83 #if NBPFILTER > 0
84 #include <net/bpf.h>
85 #endif
86 
87 #define	LOMTU	(32768 +  MHLEN + MLEN)
88 
89 struct	ifnet loif[NLOOP];
90 
91 void
92 loopattach(n)
93 	int n;
94 {
95 	register int i;
96 	register struct ifnet *ifp;
97 
98 	for (i = 0; i < NLOOP; i++) {
99 		ifp = &loif[i];
100 		sprintf(ifp->if_xname, "lo%d", i);
101 		ifp->if_softc = NULL;
102 		ifp->if_mtu = LOMTU;
103 		ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
104 		ifp->if_ioctl = loioctl;
105 		ifp->if_output = looutput;
106 		ifp->if_type = IFT_LOOP;
107 		ifp->if_hdrlen = 0;
108 		ifp->if_addrlen = 0;
109 		if_attach(ifp);
110 #if NBPFILTER > 0
111 		bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int));
112 #endif
113 	}
114 }
115 
116 int
117 looutput(ifp, m, dst, rt)
118 	struct ifnet *ifp;
119 	register struct mbuf *m;
120 	struct sockaddr *dst;
121 	register struct rtentry *rt;
122 {
123 	int s, isr;
124 	register struct ifqueue *ifq = 0;
125 
126 	if ((m->m_flags & M_PKTHDR) == 0)
127 		panic("looutput: no header mbuf");
128 	ifp->if_lastchange = time;
129 #if NBPFILTER > 0
130 	if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK)) {
131 		/*
132 		 * We need to prepend the address family as
133 		 * a four byte field.  Cons up a dummy header
134 		 * to pacify bpf.  This is safe because bpf
135 		 * will only read from the mbuf (i.e., it won't
136 		 * try to free it or keep a pointer to it).
137 		 */
138 		struct mbuf m0;
139 		u_int af = dst->sa_family;
140 
141 		m0.m_next = m;
142 		m0.m_len = 4;
143 		m0.m_data = (char *)&af;
144 
145 		bpf_mtap(ifp->if_bpf, &m0);
146 	}
147 #endif
148 	m->m_pkthdr.rcvif = ifp;
149 
150 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
151 		m_freem(m);
152 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
153 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
154 	}
155 	ifp->if_opackets++;
156 	ifp->if_obytes += m->m_pkthdr.len;
157 	switch (dst->sa_family) {
158 
159 #ifdef INET
160 	case AF_INET:
161 		ifq = &ipintrq;
162 		isr = NETISR_IP;
163 		break;
164 #endif
165 #ifdef NS
166 	case AF_NS:
167 		ifq = &nsintrq;
168 		isr = NETISR_NS;
169 		break;
170 #endif
171 #ifdef ISO
172 	case AF_ISO:
173 		ifq = &clnlintrq;
174 		isr = NETISR_ISO;
175 		break;
176 #endif
177 #ifdef NETATALK
178 	case AF_APPLETALK:
179 	        ifq = &atintrq2;
180 		isr = NETISR_ATALK;
181 		break;
182 #endif
183 	default:
184 		printf("%s: can't handle af%d\n", ifp->if_xname,
185 		    dst->sa_family);
186 		m_freem(m);
187 		return (EAFNOSUPPORT);
188 	}
189 	s = splimp();
190 	if (IF_QFULL(ifq)) {
191 		IF_DROP(ifq);
192 		m_freem(m);
193 		splx(s);
194 		return (ENOBUFS);
195 	}
196 	IF_ENQUEUE(ifq, m);
197 	schednetisr(isr);
198 	ifp->if_ipackets++;
199 	ifp->if_ibytes += m->m_pkthdr.len;
200 	splx(s);
201 	return (0);
202 }
203 
204 /* ARGSUSED */
205 void
206 lortrequest(cmd, rt, sa)
207 	int cmd;
208 	struct rtentry *rt;
209 	struct sockaddr *sa;
210 {
211 
212 	if (rt)
213 		rt->rt_rmx.rmx_mtu = LOMTU;
214 }
215 
216 /*
217  * Process an ioctl request.
218  */
219 /* ARGSUSED */
220 int
221 loioctl(ifp, cmd, data)
222 	register struct ifnet *ifp;
223 	u_long cmd;
224 	caddr_t data;
225 {
226 	register struct ifaddr *ifa;
227 	register struct ifreq *ifr;
228 	register int error = 0;
229 
230 	switch (cmd) {
231 
232 	case SIOCSIFADDR:
233 		ifp->if_flags |= IFF_UP;
234 		ifa = (struct ifaddr *)data;
235 		if (ifa != 0 && ifa->ifa_addr->sa_family == AF_ISO)
236 			ifa->ifa_rtrequest = lortrequest;
237 		/*
238 		 * Everything else is done at a higher level.
239 		 */
240 		break;
241 
242 	case SIOCADDMULTI:
243 	case SIOCDELMULTI:
244 		ifr = (struct ifreq *)data;
245 		if (ifr == 0) {
246 			error = EAFNOSUPPORT;		/* XXX */
247 			break;
248 		}
249 		switch (ifr->ifr_addr.sa_family) {
250 
251 #ifdef INET
252 		case AF_INET:
253 			break;
254 #endif
255 
256 		default:
257 			error = EAFNOSUPPORT;
258 			break;
259 		}
260 		break;
261 
262 	default:
263 		error = EINVAL;
264 	}
265 	return (error);
266 }
267