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