- Staticize lo{output,ioctl}
[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 34 * $FreeBSD: src/sys/net/if_loop.c,v 1.47.2.9 2004/02/08 08:40:24 silby Exp $
7d982e78 35 * $DragonFly: src/sys/net/if_loop.c,v 1.25 2008/09/06 06:09:42 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
7d982e78
SZ
91static void loopattach(void *);
92static int looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
93 struct rtentry *);
94static int loioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
95static void lortrequest(int, struct rtentry *, struct rt_addrinfo *);
4d723e5a 96#ifdef ALTQ
7d982e78 97static void lo_altqstart(struct ifnet *);
4d723e5a 98#endif
984263bc
MD
99PSEUDO_SET(loopattach, if_loop);
100
984263bc
MD
101#ifdef TINY_LOMTU
102#define LOMTU (1024+512)
103#elif defined(LARGE_LOMTU)
104#define LOMTU 131072
105#else
106#define LOMTU 16384
107#endif
108
72662a67
SZ
109#define LO_CSUM_FEATURES (CSUM_IP | CSUM_UDP | CSUM_TCP)
110
984263bc
MD
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;
72662a67
SZ
124 ifp->if_capabilities = IFCAP_HWCSUM;
125 ifp->if_hwassist = LO_CSUM_FEATURES;
126 ifp->if_capenable = ifp->if_capabilities;
f23061d4
JH
127 ifp->if_ioctl = loioctl;
128 ifp->if_output = looutput;
129 ifp->if_type = IFT_LOOP;
4d723e5a
JS
130 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
131 ifq_set_ready(&ifp->if_snd);
132#ifdef ALTQ
133 ifp->if_start = lo_altqstart;
134#endif
78195a76 135 if_attach(ifp, NULL);
f23061d4 136 bpfattach(ifp, DLT_NULL, sizeof(u_int));
984263bc
MD
137 }
138}
139
7d982e78 140static int
2d2b761b
SZ
141looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
142 struct rtentry *rt)
984263bc 143{
2d2b761b 144 M_ASSERTPKTHDR(m);
984263bc
MD
145
146 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
147 m_freem(m);
148 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
149 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
150 }
984263bc
MD
151
152 ifp->if_opackets++;
153 ifp->if_obytes += m->m_pkthdr.len;
154#if 1 /* XXX */
155 switch (dst->sa_family) {
156 case AF_INET:
157 case AF_INET6:
158 case AF_IPX:
159 case AF_NS:
160 case AF_APPLETALK:
161 break;
162 default:
4b1cf444 163 kprintf("looutput: af=%d unexpected\n", dst->sa_family);
984263bc
MD
164 m_freem(m);
165 return (EAFNOSUPPORT);
166 }
167#endif
72662a67
SZ
168
169 if (ifp->if_capenable & IFCAP_RXCSUM) {
2d2b761b
SZ
170 int csum_flags = 0;
171
72662a67
SZ
172 if (m->m_pkthdr.csum_flags & CSUM_IP)
173 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
174 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
175 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
176
177 m->m_pkthdr.csum_flags |= csum_flags;
178 if (csum_flags & CSUM_DATA_VALID)
179 m->m_pkthdr.csum_data = 0xffff;
180 }
f23061d4 181 return (if_simloop(ifp, m, dst->sa_family, 0));
984263bc
MD
182}
183
184/*
185 * if_simloop()
186 *
187 * This function is to support software emulation of hardware loopback,
188 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
189 * hear their own broadcasts, we create a copy of the packet that we
190 * would normally receive via a hardware loopback.
191 *
192 * This function expects the packet to include the media header of length hlen.
193 */
984263bc 194int
f23061d4 195if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
984263bc 196{
8bde602d 197 int isr;
984263bc
MD
198
199 KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
200 m->m_pkthdr.rcvif = ifp;
201
202 /* BPF write needs to be handled specially */
203 if (af == AF_UNSPEC) {
204 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
205 af = *(mtod(m, int *));
206 m->m_len -= sizeof(int);
207 m->m_pkthdr.len -= sizeof(int);
208 m->m_data += sizeof(int);
209 }
210
984263bc 211 if (ifp->if_bpf) {
984263bc 212 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
1f8e62c9
JS
213 uint32_t bpf_af = (uint32_t)af;
214 bpf_ptap(ifp->if_bpf, m, &bpf_af, 4);
215 }
216 else {
217 bpf_mtap(ifp->if_bpf, m);
984263bc 218 }
984263bc
MD
219 }
220
221 /* Strip away media header */
469ac2dc 222 if (hlen > 0)
984263bc 223 m_adj(m, hlen);
4d723e5a
JS
224
225#ifdef ALTQ
226 /*
227 * altq for loop is just for debugging.
228 * only used when called for loop interface (not for
229 * a simplex interface).
230 */
231 if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) {
232 struct altq_pktattr pktattr;
233 int32_t *afp;
4986965b 234 int error;
4d723e5a
JS
235
236 /*
237 * if the queueing discipline needs packet classification,
238 * do it before prepending link headers.
239 */
240 ifq_classify(&ifp->if_snd, m, af, &pktattr);
241
242 M_PREPEND(m, sizeof(int32_t), MB_DONTWAIT);
243 if (m == 0)
244 return(ENOBUFS);
245 afp = mtod(m, int32_t *);
246 *afp = (int32_t)af;
247
78195a76
MD
248 /*
249 * A critical section is needed for subsystems protected by
250 * the MP lock, and the serializer is assumed to already
251 * be held for MPSAFE subsystems.
252 */
4986965b 253 crit_enter();
4d723e5a 254 error = ifq_enqueue(&ifp->if_snd, m, &pktattr);
9db4b353
SZ
255 lwkt_serialize_enter(ifp->if_serializer);
256 ifp->if_start(ifp);
257 lwkt_serialize_exit(ifp->if_serializer);
4986965b 258 crit_exit();
4d723e5a
JS
259 return (error);
260 }
261#endif /* ALTQ */
984263bc
MD
262
263 /* Deliver to upper layer protocol */
264 switch (af) {
265#ifdef INET
266 case AF_INET:
984263bc
MD
267 isr = NETISR_IP;
268 break;
269#endif
270#ifdef INET6
271 case AF_INET6:
272 m->m_flags |= M_LOOP;
984263bc
MD
273 isr = NETISR_IPV6;
274 break;
275#endif
276#ifdef IPX
277 case AF_IPX:
984263bc
MD
278 isr = NETISR_IPX;
279 break;
280#endif
281#ifdef NS
282 case AF_NS:
984263bc
MD
283 isr = NETISR_NS;
284 break;
285#endif
286#ifdef NETATALK
287 case AF_APPLETALK:
8bde602d 288 isr = NETISR_ATALK2;
984263bc 289 break;
f7ea898e 290#endif
984263bc 291 default:
4b1cf444 292 kprintf("if_simloop: can't handle af=%d\n", af);
984263bc
MD
293 m_freem(m);
294 return (EAFNOSUPPORT);
295 }
8bde602d 296
984263bc
MD
297 ifp->if_ipackets++;
298 ifp->if_ibytes += m->m_pkthdr.len;
8bde602d 299 netisr_queue(isr, m);
984263bc
MD
300 return (0);
301}
302
4d723e5a
JS
303#ifdef ALTQ
304static void
305lo_altqstart(struct ifnet *ifp)
306{
307 struct mbuf *m;
308 int32_t af, *afp;
4986965b 309 int isr;
4d723e5a
JS
310
311 while (1) {
4986965b 312 crit_enter();
d2c71fa0 313 m = ifq_dequeue(&ifp->if_snd, NULL);
4986965b 314 crit_exit();
4d723e5a
JS
315 if (m == NULL)
316 return;
317
318 afp = mtod(m, int32_t *);
319 af = *afp;
320 m_adj(m, sizeof(int32_t));
321
322 switch (af) {
323#ifdef INET
324 case AF_INET:
325 isr = NETISR_IP;
326 break;
327#endif
328#ifdef INET6
329 case AF_INET6:
330 m->m_flags |= M_LOOP;
331 isr = NETISR_IPV6;
332 break;
333#endif
334#ifdef IPX
335 case AF_IPX:
336 isr = NETISR_IPX;
337 break;
338#endif
339#ifdef NS
340 case AF_NS:
341 isr = NETISR_NS;
342 break;
343#endif
344#ifdef ISO
345 case AF_ISO:
346 isr = NETISR_ISO;
347 break;
348#endif
349#ifdef NETATALK
350 case AF_APPLETALK:
351 isr = NETISR_ATALK2;
352 break;
ffd619d1 353#endif
4d723e5a 354 default:
4b1cf444 355 kprintf("lo_altqstart: can't handle af%d\n", af);
4d723e5a
JS
356 m_freem(m);
357 return;
358 }
359
360 ifp->if_ipackets++;
361 ifp->if_ibytes += m->m_pkthdr.len;
362 netisr_queue(isr, m);
363 }
364}
365#endif /* ALTQ */
366
984263bc
MD
367/* ARGSUSED */
368static void
f23061d4 369lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
984263bc
MD
370{
371 if (rt) {
372 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
373 /*
374 * For optimal performance, the send and receive buffers
375 * should be at least twice the MTU plus a little more for
376 * overhead.
377 */
f23061d4 378 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
984263bc
MD
379 }
380}
381
382/*
383 * Process an ioctl request.
384 */
385/* ARGSUSED */
7d982e78 386static int
f23061d4 387loioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
984263bc 388{
82ed7fc2
RG
389 struct ifaddr *ifa;
390 struct ifreq *ifr = (struct ifreq *)data;
72662a67 391 int error = 0, mask;
984263bc
MD
392
393 switch (cmd) {
984263bc
MD
394 case SIOCSIFADDR:
395 ifp->if_flags |= IFF_UP | IFF_RUNNING;
396 ifa = (struct ifaddr *)data;
397 ifa->ifa_rtrequest = lortrequest;
398 /*
399 * Everything else is done at a higher level.
400 */
401 break;
402
403 case SIOCADDMULTI:
404 case SIOCDELMULTI:
405 if (ifr == 0) {
406 error = EAFNOSUPPORT; /* XXX */
407 break;
408 }
409 switch (ifr->ifr_addr.sa_family) {
410
411#ifdef INET
412 case AF_INET:
413 break;
414#endif
415#ifdef INET6
416 case AF_INET6:
417 break;
418#endif
419
420 default:
421 error = EAFNOSUPPORT;
422 break;
423 }
424 break;
425
426 case SIOCSIFMTU:
427 ifp->if_mtu = ifr->ifr_mtu;
428 break;
429
430 case SIOCSIFFLAGS:
431 break;
432
72662a67
SZ
433 case SIOCSIFCAP:
434 mask = (ifr->ifr_reqcap ^ ifp->if_capenable) & IFCAP_HWCSUM;
435 if (mask) {
436 ifp->if_capenable ^= mask;
437 if (IFCAP_TXCSUM & ifp->if_capenable)
438 ifp->if_hwassist = LO_CSUM_FEATURES;
439 else
440 ifp->if_hwassist = 0;
441 }
442 break;
443
984263bc
MD
444 default:
445 error = EINVAL;
446 }
447 return (error);
448}