Bring in if_loop.c 1.47.2.9 from FreeBSD (silby@freebsd.org):
[dragonfly.git] / sys / net / if_loop.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
33 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93
1fd46b85
SZ
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.22 2008/09/06 05:07:35 sephe Exp $
984263bc
MD
36 */
37
38/*
39 * Loopback interface driver for protocol testing and timing.
40 */
1f2de5d4 41#include "use_loop.h"
984263bc
MD
42
43#include "opt_atalk.h"
44#include "opt_inet.h"
45#include "opt_inet6.h"
46#include "opt_ipx.h"
47
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/kernel.h>
51#include <sys/mbuf.h>
52#include <sys/socket.h>
53#include <sys/sockio.h>
54
55#include <net/if.h>
56#include <net/if_types.h>
4d723e5a 57#include <net/ifq_var.h>
984263bc
MD
58#include <net/netisr.h>
59#include <net/route.h>
60#include <net/bpf.h>
61#include <net/bpfdesc.h>
62
63#ifdef INET
64#include <netinet/in.h>
65#include <netinet/in_var.h>
66#endif
67
68#ifdef IPX
d2438d69
MD
69#include <netproto/ipx/ipx.h>
70#include <netproto/ipx/ipx_if.h>
984263bc
MD
71#endif
72
73#ifdef INET6
74#ifndef INET
75#include <netinet/in.h>
76#endif
77#include <netinet6/in6_var.h>
78#include <netinet/ip6.h>
79#endif
80
81#ifdef NS
82#include <netns/ns.h>
83#include <netns/ns_if.h>
84#endif
85
86#ifdef NETATALK
d2438d69
MD
87#include <netproto/atalk/at.h>
88#include <netproto/atalk/at_var.h>
f7ea898e 89#endif
984263bc 90
bd4539cc 91int loioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
158abb01 92static void lortrequest (int, struct rtentry *, struct rt_addrinfo *);
984263bc 93
158abb01 94static void loopattach (void *);
4d723e5a
JS
95#ifdef ALTQ
96static void lo_altqstart(struct ifnet *);
97#endif
984263bc
MD
98PSEUDO_SET(loopattach, if_loop);
99
158abb01
RG
100int looutput (struct ifnet *ifp,
101 struct mbuf *m, struct sockaddr *dst, struct rtentry *rt);
984263bc
MD
102
103#ifdef TINY_LOMTU
104#define LOMTU (1024+512)
105#elif defined(LARGE_LOMTU)
106#define LOMTU 131072
107#else
108#define LOMTU 16384
109#endif
110
111struct ifnet loif[NLOOP];
112
113/* ARGSUSED */
114static void
f23061d4 115loopattach(void *dummy)
984263bc 116{
82ed7fc2 117 struct ifnet *ifp;
f23061d4
JH
118 int i;
119
120 for (i = 0, ifp = loif; i < NLOOP; i++, ifp++) {
121 if_initname(ifp, "lo", i);
122 ifp->if_mtu = LOMTU;
123 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
124 ifp->if_ioctl = loioctl;
125 ifp->if_output = looutput;
126 ifp->if_type = IFT_LOOP;
4d723e5a
JS
127 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
128 ifq_set_ready(&ifp->if_snd);
129#ifdef ALTQ
130 ifp->if_start = lo_altqstart;
131#endif
78195a76 132 if_attach(ifp, NULL);
f23061d4 133 bpfattach(ifp, DLT_NULL, sizeof(u_int));
984263bc
MD
134 }
135}
136
137int
f23061d4
JH
138looutput(
139 struct ifnet *ifp,
140 struct mbuf *m,
141 struct sockaddr *dst,
142 struct rtentry *rt)
984263bc 143{
984263bc
MD
144 if ((m->m_flags & M_PKTHDR) == 0)
145 panic("looutput no HDR");
146
147 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
148 m_freem(m);
149 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
150 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
151 }
984263bc
MD
152
153 ifp->if_opackets++;
154 ifp->if_obytes += m->m_pkthdr.len;
155#if 1 /* XXX */
156 switch (dst->sa_family) {
157 case AF_INET:
158 case AF_INET6:
159 case AF_IPX:
160 case AF_NS:
161 case AF_APPLETALK:
162 break;
163 default:
4b1cf444 164 kprintf("looutput: af=%d unexpected\n", dst->sa_family);
984263bc
MD
165 m_freem(m);
166 return (EAFNOSUPPORT);
167 }
168#endif
f23061d4 169 return (if_simloop(ifp, m, dst->sa_family, 0));
984263bc
MD
170}
171
172/*
173 * if_simloop()
174 *
175 * This function is to support software emulation of hardware loopback,
176 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
177 * hear their own broadcasts, we create a copy of the packet that we
178 * would normally receive via a hardware loopback.
179 *
180 * This function expects the packet to include the media header of length hlen.
181 */
984263bc 182int
f23061d4 183if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
984263bc 184{
8bde602d 185 int isr;
984263bc
MD
186
187 KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
188 m->m_pkthdr.rcvif = ifp;
189
190 /* BPF write needs to be handled specially */
191 if (af == AF_UNSPEC) {
192 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
193 af = *(mtod(m, int *));
194 m->m_len -= sizeof(int);
195 m->m_pkthdr.len -= sizeof(int);
196 m->m_data += sizeof(int);
197 }
198
984263bc 199 if (ifp->if_bpf) {
984263bc 200 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
1f8e62c9
JS
201 uint32_t bpf_af = (uint32_t)af;
202 bpf_ptap(ifp->if_bpf, m, &bpf_af, 4);
203 }
204 else {
205 bpf_mtap(ifp->if_bpf, m);
984263bc 206 }
984263bc
MD
207 }
208
209 /* Strip away media header */
469ac2dc 210 if (hlen > 0)
984263bc 211 m_adj(m, hlen);
4d723e5a
JS
212
213#ifdef ALTQ
214 /*
215 * altq for loop is just for debugging.
216 * only used when called for loop interface (not for
217 * a simplex interface).
218 */
219 if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) {
220 struct altq_pktattr pktattr;
221 int32_t *afp;
4986965b 222 int error;
4d723e5a
JS
223
224 /*
225 * if the queueing discipline needs packet classification,
226 * do it before prepending link headers.
227 */
228 ifq_classify(&ifp->if_snd, m, af, &pktattr);
229
230 M_PREPEND(m, sizeof(int32_t), MB_DONTWAIT);
231 if (m == 0)
232 return(ENOBUFS);
233 afp = mtod(m, int32_t *);
234 *afp = (int32_t)af;
235
78195a76
MD
236 /*
237 * A critical section is needed for subsystems protected by
238 * the MP lock, and the serializer is assumed to already
239 * be held for MPSAFE subsystems.
240 */
4986965b 241 crit_enter();
4d723e5a 242 error = ifq_enqueue(&ifp->if_snd, m, &pktattr);
9db4b353
SZ
243 lwkt_serialize_enter(ifp->if_serializer);
244 ifp->if_start(ifp);
245 lwkt_serialize_exit(ifp->if_serializer);
4986965b 246 crit_exit();
4d723e5a
JS
247 return (error);
248 }
249#endif /* ALTQ */
984263bc
MD
250
251 /* Deliver to upper layer protocol */
252 switch (af) {
253#ifdef INET
254 case AF_INET:
984263bc
MD
255 isr = NETISR_IP;
256 break;
257#endif
258#ifdef INET6
259 case AF_INET6:
260 m->m_flags |= M_LOOP;
984263bc
MD
261 isr = NETISR_IPV6;
262 break;
263#endif
264#ifdef IPX
265 case AF_IPX:
984263bc
MD
266 isr = NETISR_IPX;
267 break;
268#endif
269#ifdef NS
270 case AF_NS:
984263bc
MD
271 isr = NETISR_NS;
272 break;
273#endif
274#ifdef NETATALK
275 case AF_APPLETALK:
8bde602d 276 isr = NETISR_ATALK2;
984263bc 277 break;
f7ea898e 278#endif
984263bc 279 default:
4b1cf444 280 kprintf("if_simloop: can't handle af=%d\n", af);
984263bc
MD
281 m_freem(m);
282 return (EAFNOSUPPORT);
283 }
8bde602d 284
984263bc
MD
285 ifp->if_ipackets++;
286 ifp->if_ibytes += m->m_pkthdr.len;
8bde602d 287 netisr_queue(isr, m);
984263bc
MD
288 return (0);
289}
290
4d723e5a
JS
291#ifdef ALTQ
292static void
293lo_altqstart(struct ifnet *ifp)
294{
295 struct mbuf *m;
296 int32_t af, *afp;
4986965b 297 int isr;
4d723e5a
JS
298
299 while (1) {
4986965b 300 crit_enter();
d2c71fa0 301 m = ifq_dequeue(&ifp->if_snd, NULL);
4986965b 302 crit_exit();
4d723e5a
JS
303 if (m == NULL)
304 return;
305
306 afp = mtod(m, int32_t *);
307 af = *afp;
308 m_adj(m, sizeof(int32_t));
309
310 switch (af) {
311#ifdef INET
312 case AF_INET:
313 isr = NETISR_IP;
314 break;
315#endif
316#ifdef INET6
317 case AF_INET6:
318 m->m_flags |= M_LOOP;
319 isr = NETISR_IPV6;
320 break;
321#endif
322#ifdef IPX
323 case AF_IPX:
324 isr = NETISR_IPX;
325 break;
326#endif
327#ifdef NS
328 case AF_NS:
329 isr = NETISR_NS;
330 break;
331#endif
332#ifdef ISO
333 case AF_ISO:
334 isr = NETISR_ISO;
335 break;
336#endif
337#ifdef NETATALK
338 case AF_APPLETALK:
339 isr = NETISR_ATALK2;
340 break;
ffd619d1 341#endif
4d723e5a 342 default:
4b1cf444 343 kprintf("lo_altqstart: can't handle af%d\n", af);
4d723e5a
JS
344 m_freem(m);
345 return;
346 }
347
348 ifp->if_ipackets++;
349 ifp->if_ibytes += m->m_pkthdr.len;
350 netisr_queue(isr, m);
351 }
352}
353#endif /* ALTQ */
354
984263bc
MD
355/* ARGSUSED */
356static void
f23061d4 357lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
984263bc
MD
358{
359 if (rt) {
360 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
361 /*
362 * For optimal performance, the send and receive buffers
363 * should be at least twice the MTU plus a little more for
364 * overhead.
365 */
f23061d4 366 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
984263bc
MD
367 }
368}
369
370/*
371 * Process an ioctl request.
372 */
373/* ARGSUSED */
374int
f23061d4 375loioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
984263bc 376{
82ed7fc2
RG
377 struct ifaddr *ifa;
378 struct ifreq *ifr = (struct ifreq *)data;
379 int error = 0;
984263bc
MD
380
381 switch (cmd) {
382
383 case SIOCSIFADDR:
384 ifp->if_flags |= IFF_UP | IFF_RUNNING;
385 ifa = (struct ifaddr *)data;
386 ifa->ifa_rtrequest = lortrequest;
387 /*
388 * Everything else is done at a higher level.
389 */
390 break;
391
392 case SIOCADDMULTI:
393 case SIOCDELMULTI:
394 if (ifr == 0) {
395 error = EAFNOSUPPORT; /* XXX */
396 break;
397 }
398 switch (ifr->ifr_addr.sa_family) {
399
400#ifdef INET
401 case AF_INET:
402 break;
403#endif
404#ifdef INET6
405 case AF_INET6:
406 break;
407#endif
408
409 default:
410 error = EAFNOSUPPORT;
411 break;
412 }
413 break;
414
415 case SIOCSIFMTU:
416 ifp->if_mtu = ifr->ifr_mtu;
417 break;
418
419 case SIOCSIFFLAGS:
420 break;
421
422 default:
423 error = EINVAL;
424 }
425 return (error);
426}