kern.{post,pre}.mk: Clean up a bit.
[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.
dc71b7ab 13 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93
1fd46b85 30 * $FreeBSD: src/sys/net/if_loop.c,v 1.47.2.9 2004/02/08 08:40:24 silby Exp $
984263bc
MD
31 */
32
33/*
34 * Loopback interface driver for protocol testing and timing.
35 */
1f2de5d4 36#include "use_loop.h"
984263bc 37
984263bc
MD
38#include "opt_inet.h"
39#include "opt_inet6.h"
984263bc
MD
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
82c6f048 44#include <sys/lock.h>
805c8e8e 45#include <sys/malloc.h>
984263bc
MD
46#include <sys/mbuf.h>
47#include <sys/socket.h>
48#include <sys/sockio.h>
49
684a93c4
MD
50#include <sys/mplock2.h>
51
984263bc
MD
52#include <net/if.h>
53#include <net/if_types.h>
860b6b42 54#include <net/if_clone.h>
4d723e5a 55#include <net/ifq_var.h>
984263bc
MD
56#include <net/netisr.h>
57#include <net/route.h>
58#include <net/bpf.h>
59#include <net/bpfdesc.h>
60
426d6ad0 61#ifdef INET
984263bc
MD
62#include <netinet/in.h>
63#include <netinet/in_var.h>
64#endif
65
984263bc
MD
66#ifdef INET6
67#ifndef INET
68#include <netinet/in.h>
69#endif
70#include <netinet6/in6_var.h>
71#include <netinet/ip6.h>
72#endif
73
860b6b42
SZ
74static int lo_clone_create(struct if_clone *, int, caddr_t);
75static int lo_clone_destroy(struct ifnet *);
76
00ee3f5e
SZ
77static int lo_output(struct ifnet *, struct mbuf *, struct sockaddr *,
78 struct rtentry *);
79static int lo_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
80static void lo_rtrequest(int, struct rtentry *);
4d723e5a 81#ifdef ALTQ
f0a26983 82static void lo_altqstart(struct ifnet *, struct ifaltq_subque *);
4d723e5a 83#endif
984263bc 84
426d6ad0 85#if defined(TINY_LOMTU)
984263bc
MD
86#define LOMTU (1024+512)
87#elif defined(LARGE_LOMTU)
88#define LOMTU 131072
89#else
90#define LOMTU 16384
91#endif
92
72662a67
SZ
93#define LO_CSUM_FEATURES (CSUM_IP | CSUM_UDP | CSUM_TCP)
94
860b6b42
SZ
95struct ifnet *loif;
96
97static struct if_clone lo_cloner = IF_CLONE_INITIALIZER("lo",
98 lo_clone_create, lo_clone_destroy, NLOOP, IF_MAXUNIT);
984263bc 99
984263bc 100static void
860b6b42
SZ
101lo_sysinit(void *dummy __unused)
102{
860b6b42
SZ
103 if_clone_attach(&lo_cloner);
104}
105SYSINIT(lo_sysinit, SI_SUB_PSEUDO, SI_ORDER_ANY, lo_sysinit, NULL);
106
107static int
426d6ad0 108lo_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
984263bc 109{
82ed7fc2 110 struct ifnet *ifp;
860b6b42
SZ
111
112 ifp = kmalloc(sizeof(*ifp), M_IFNET, M_WAITOK | M_ZERO);
426d6ad0 113 if_initname(ifp, ifc->ifc_name, unit);
860b6b42
SZ
114 ifp->if_mtu = LOMTU;
115 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
116 ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_RSS;
117 ifp->if_hwassist = LO_CSUM_FEATURES;
118 ifp->if_capenable = ifp->if_capabilities;
00ee3f5e
SZ
119 ifp->if_ioctl = lo_ioctl;
120 ifp->if_output = lo_output;
860b6b42
SZ
121 ifp->if_type = IFT_LOOP;
122 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
123 ifq_set_ready(&ifp->if_snd);
4d723e5a 124#ifdef ALTQ
860b6b42 125 ifp->if_start = lo_altqstart;
4d723e5a 126#endif
860b6b42
SZ
127 if_attach(ifp, NULL);
128 bpfattach(ifp, DLT_NULL, sizeof(u_int));
129
130 if (loif == NULL) {
131 KASSERT(unit == 0, ("loif is %s", ifp->if_xname));
132 loif = ifp;
984263bc 133 }
860b6b42
SZ
134 return (0);
135}
136
137static int
138lo_clone_destroy(struct ifnet *ifp)
139{
860b6b42
SZ
140 if (loif == ifp)
141 return (EPERM);
142
143 bpfdetach(ifp);
144 if_detach(ifp);
7be2b76d 145 kfree(ifp, M_IFNET);
860b6b42 146 return (0);
984263bc
MD
147}
148
7d982e78 149static int
00ee3f5e
SZ
150lo_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
151 struct rtentry *rt)
984263bc 152{
2d2b761b 153 M_ASSERTPKTHDR(m);
984263bc
MD
154
155 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
156 m_freem(m);
157 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
426d6ad0 158 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
984263bc 159 }
984263bc 160
d40991ef
SZ
161 IFNET_STAT_INC(ifp, opackets, 1);
162 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len);
984263bc
MD
163#if 1 /* XXX */
164 switch (dst->sa_family) {
165 case AF_INET:
166 case AF_INET6:
984263bc
MD
167 break;
168 default:
00ee3f5e 169 kprintf("lo_output: af=%d unexpected\n", dst->sa_family);
984263bc
MD
170 m_freem(m);
171 return (EAFNOSUPPORT);
172 }
173#endif
72662a67
SZ
174
175 if (ifp->if_capenable & IFCAP_RXCSUM) {
2d2b761b
SZ
176 int csum_flags = 0;
177
72662a67
SZ
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 }
8d4ec101
SZ
187 if ((ifp->if_capenable & IFCAP_RSS) == 0)
188 m->m_flags &= ~M_HASH;
f23061d4 189 return (if_simloop(ifp, m, dst->sa_family, 0));
984263bc
MD
190}
191
192/*
193 * if_simloop()
194 *
195 * This function is to support software emulation of hardware loopback,
196 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
197 * hear their own broadcasts, we create a copy of the packet that we
198 * would normally receive via a hardware loopback.
199 *
200 * This function expects the packet to include the media header of length hlen.
201 */
984263bc 202int
f23061d4 203if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
984263bc 204{
8bde602d 205 int isr;
984263bc
MD
206
207 KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
208 m->m_pkthdr.rcvif = ifp;
209
210 /* BPF write needs to be handled specially */
211 if (af == AF_UNSPEC) {
212 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
213 af = *(mtod(m, int *));
214 m->m_len -= sizeof(int);
215 m->m_pkthdr.len -= sizeof(int);
216 m->m_data += sizeof(int);
217 }
218
984263bc 219 if (ifp->if_bpf) {
fda7d388 220 bpf_gettoken();
82c6f048
SZ
221
222 /* Re-check */
223 if (ifp->if_bpf == NULL)
224 goto rel;
225
984263bc 226 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
1f8e62c9
JS
227 uint32_t bpf_af = (uint32_t)af;
228 bpf_ptap(ifp->if_bpf, m, &bpf_af, 4);
82c6f048 229 } else {
1f8e62c9 230 bpf_mtap(ifp->if_bpf, m);
984263bc 231 }
82c6f048 232rel:
fda7d388 233 bpf_reltoken();
984263bc
MD
234 }
235
236 /* Strip away media header */
469ac2dc 237 if (hlen > 0)
984263bc 238 m_adj(m, hlen);
4d723e5a
JS
239
240#ifdef ALTQ
241 /*
242 * altq for loop is just for debugging.
243 * only used when called for loop interface (not for
244 * a simplex interface).
245 */
246 if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) {
247 struct altq_pktattr pktattr;
248 int32_t *afp;
4d723e5a
JS
249
250 /*
251 * if the queueing discipline needs packet classification,
252 * do it before prepending link headers.
253 */
254 ifq_classify(&ifp->if_snd, m, af, &pktattr);
255
b5523eac 256 M_PREPEND(m, sizeof(int32_t), M_NOWAIT);
4090d6ff 257 if (m == NULL)
4d723e5a
JS
258 return(ENOBUFS);
259 afp = mtod(m, int32_t *);
260 *afp = (int32_t)af;
261
3c4cd924 262 return ifq_dispatch(ifp, m, &pktattr);
4d723e5a
JS
263 }
264#endif /* ALTQ */
984263bc
MD
265
266 /* Deliver to upper layer protocol */
267 switch (af) {
268#ifdef INET
269 case AF_INET:
984263bc
MD
270 isr = NETISR_IP;
271 break;
272#endif
273#ifdef INET6
274 case AF_INET6:
275 m->m_flags |= M_LOOP;
984263bc
MD
276 isr = NETISR_IPV6;
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
d40991ef
SZ
285 IFNET_STAT_INC(ifp, ipackets, 1);
286 IFNET_STAT_INC(ifp, 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
f0a26983 293lo_altqstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
4d723e5a
JS
294{
295 struct mbuf *m;
296 int32_t af, *afp;
4986965b 297 int isr;
4d723e5a
JS
298
299 while (1) {
4986965b 300 crit_enter();
ac9843a1 301 m = ifsq_dequeue(ifsq);
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;
ffd619d1 321#endif
4d723e5a 322 default:
4b1cf444 323 kprintf("lo_altqstart: can't handle af%d\n", af);
4d723e5a
JS
324 m_freem(m);
325 return;
326 }
327
d40991ef
SZ
328 IFNET_STAT_INC(ifp, ipackets, 1);
329 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
4d723e5a
JS
330 netisr_queue(isr, m);
331 }
332}
333#endif /* ALTQ */
334
984263bc
MD
335/* ARGSUSED */
336static void
00ee3f5e 337lo_rtrequest(int cmd, struct rtentry *rt)
984263bc
MD
338{
339 if (rt) {
340 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
341 /*
342 * For optimal performance, the send and receive buffers
343 * should be at least twice the MTU plus a little more for
344 * overhead.
345 */
f23061d4 346 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
984263bc
MD
347 }
348}
349
350/*
351 * Process an ioctl request.
352 */
353/* ARGSUSED */
7d982e78 354static int
00ee3f5e 355lo_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
984263bc 356{
82ed7fc2
RG
357 struct ifaddr *ifa;
358 struct ifreq *ifr = (struct ifreq *)data;
72662a67 359 int error = 0, mask;
984263bc
MD
360
361 switch (cmd) {
984263bc
MD
362 case SIOCSIFADDR:
363 ifp->if_flags |= IFF_UP | IFF_RUNNING;
364 ifa = (struct ifaddr *)data;
00ee3f5e 365 ifa->ifa_rtrequest = lo_rtrequest;
984263bc
MD
366 /*
367 * Everything else is done at a higher level.
368 */
369 break;
370
371 case SIOCADDMULTI:
372 case SIOCDELMULTI:
4090d6ff 373 if (ifr == NULL) {
984263bc
MD
374 error = EAFNOSUPPORT; /* XXX */
375 break;
376 }
377 switch (ifr->ifr_addr.sa_family) {
378
379#ifdef INET
380 case AF_INET:
381 break;
382#endif
383#ifdef INET6
384 case AF_INET6:
385 break;
386#endif
387
388 default:
389 error = EAFNOSUPPORT;
390 break;
391 }
392 break;
393
394 case SIOCSIFMTU:
395 ifp->if_mtu = ifr->ifr_mtu;
396 break;
397
398 case SIOCSIFFLAGS:
399 break;
400
72662a67 401 case SIOCSIFCAP:
8d4ec101
SZ
402 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
403 if (mask & IFCAP_HWCSUM) {
404 ifp->if_capenable ^= (mask & IFCAP_HWCSUM);
72662a67
SZ
405 if (IFCAP_TXCSUM & ifp->if_capenable)
406 ifp->if_hwassist = LO_CSUM_FEATURES;
407 else
408 ifp->if_hwassist = 0;
409 }
8d4ec101
SZ
410 if (mask & IFCAP_RSS)
411 ifp->if_capenable ^= IFCAP_RSS;
72662a67
SZ
412 break;
413
984263bc
MD
414 default:
415 error = EINVAL;
416 }
417 return (error);
418}