pf.conf.5: Describe the use of interface group
[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>
984263bc
MD
45#include <sys/mbuf.h>
46#include <sys/socket.h>
47#include <sys/sockio.h>
48
684a93c4
MD
49#include <sys/mplock2.h>
50
984263bc
MD
51#include <net/if.h>
52#include <net/if_types.h>
860b6b42 53#include <net/if_clone.h>
4d723e5a 54#include <net/ifq_var.h>
984263bc
MD
55#include <net/netisr.h>
56#include <net/route.h>
57#include <net/bpf.h>
58#include <net/bpfdesc.h>
59
60#ifdef INET
61#include <netinet/in.h>
62#include <netinet/in_var.h>
63#endif
64
984263bc
MD
65#ifdef INET6
66#ifndef INET
67#include <netinet/in.h>
68#endif
69#include <netinet6/in6_var.h>
70#include <netinet/ip6.h>
71#endif
72
860b6b42
SZ
73static int lo_clone_create(struct if_clone *, int, caddr_t);
74static int lo_clone_destroy(struct ifnet *);
75
00ee3f5e
SZ
76static int lo_output(struct ifnet *, struct mbuf *, struct sockaddr *,
77 struct rtentry *);
78static int lo_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
79static void lo_rtrequest(int, struct rtentry *);
4d723e5a 80#ifdef ALTQ
f0a26983 81static void lo_altqstart(struct ifnet *, struct ifaltq_subque *);
4d723e5a 82#endif
984263bc 83
984263bc
MD
84#ifdef TINY_LOMTU
85#define LOMTU (1024+512)
86#elif defined(LARGE_LOMTU)
87#define LOMTU 131072
88#else
89#define LOMTU 16384
90#endif
91
72662a67
SZ
92#define LO_CSUM_FEATURES (CSUM_IP | CSUM_UDP | CSUM_TCP)
93
860b6b42
SZ
94struct ifnet *loif;
95
96static struct if_clone lo_cloner = IF_CLONE_INITIALIZER("lo",
97 lo_clone_create, lo_clone_destroy, NLOOP, IF_MAXUNIT);
984263bc 98
984263bc 99static void
860b6b42
SZ
100lo_sysinit(void *dummy __unused)
101{
102
103 if_clone_attach(&lo_cloner);
104}
105SYSINIT(lo_sysinit, SI_SUB_PSEUDO, SI_ORDER_ANY, lo_sysinit, NULL);
106
107static int
108lo_clone_create(struct if_clone *ifc __unused, 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);
113
114 if_initname(ifp, "lo", unit);
115 ifp->if_mtu = LOMTU;
116 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
117 ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_RSS;
118 ifp->if_hwassist = LO_CSUM_FEATURES;
119 ifp->if_capenable = ifp->if_capabilities;
00ee3f5e
SZ
120 ifp->if_ioctl = lo_ioctl;
121 ifp->if_output = lo_output;
860b6b42
SZ
122 ifp->if_type = IFT_LOOP;
123 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
124 ifq_set_ready(&ifp->if_snd);
4d723e5a 125#ifdef ALTQ
860b6b42 126 ifp->if_start = lo_altqstart;
4d723e5a 127#endif
860b6b42
SZ
128 if_attach(ifp, NULL);
129 bpfattach(ifp, DLT_NULL, sizeof(u_int));
130
131 if (loif == NULL) {
132 KASSERT(unit == 0, ("loif is %s", ifp->if_xname));
133 loif = ifp;
984263bc 134 }
860b6b42
SZ
135 return (0);
136}
137
138static int
139lo_clone_destroy(struct ifnet *ifp)
140{
141
142 if (loif == ifp)
143 return (EPERM);
144
145 bpfdetach(ifp);
146 if_detach(ifp);
7be2b76d 147 kfree(ifp, M_IFNET);
860b6b42 148 return (0);
984263bc
MD
149}
150
7d982e78 151static int
00ee3f5e
SZ
152lo_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
153 struct rtentry *rt)
984263bc 154{
2d2b761b 155 M_ASSERTPKTHDR(m);
984263bc
MD
156
157 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
158 m_freem(m);
159 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
160 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
161 }
984263bc 162
d40991ef
SZ
163 IFNET_STAT_INC(ifp, opackets, 1);
164 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len);
984263bc
MD
165#if 1 /* XXX */
166 switch (dst->sa_family) {
167 case AF_INET:
168 case AF_INET6:
984263bc
MD
169 break;
170 default:
00ee3f5e 171 kprintf("lo_output: 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) {
2d2b761b
SZ
178 int csum_flags = 0;
179
72662a67
SZ
180 if (m->m_pkthdr.csum_flags & CSUM_IP)
181 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
182 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
183 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
184
185 m->m_pkthdr.csum_flags |= csum_flags;
186 if (csum_flags & CSUM_DATA_VALID)
187 m->m_pkthdr.csum_data = 0xffff;
188 }
8d4ec101
SZ
189 if ((ifp->if_capenable & IFCAP_RSS) == 0)
190 m->m_flags &= ~M_HASH;
f23061d4 191 return (if_simloop(ifp, m, dst->sa_family, 0));
984263bc
MD
192}
193
194/*
195 * if_simloop()
196 *
197 * This function is to support software emulation of hardware loopback,
198 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
199 * hear their own broadcasts, we create a copy of the packet that we
200 * would normally receive via a hardware loopback.
201 *
202 * This function expects the packet to include the media header of length hlen.
203 */
984263bc 204int
f23061d4 205if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
984263bc 206{
8bde602d 207 int isr;
984263bc
MD
208
209 KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
210 m->m_pkthdr.rcvif = ifp;
211
212 /* BPF write needs to be handled specially */
213 if (af == AF_UNSPEC) {
214 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
215 af = *(mtod(m, int *));
216 m->m_len -= sizeof(int);
217 m->m_pkthdr.len -= sizeof(int);
218 m->m_data += sizeof(int);
219 }
220
984263bc 221 if (ifp->if_bpf) {
fda7d388 222 bpf_gettoken();
82c6f048
SZ
223
224 /* Re-check */
225 if (ifp->if_bpf == NULL)
226 goto rel;
227
984263bc 228 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
1f8e62c9
JS
229 uint32_t bpf_af = (uint32_t)af;
230 bpf_ptap(ifp->if_bpf, m, &bpf_af, 4);
82c6f048 231 } else {
1f8e62c9 232 bpf_mtap(ifp->if_bpf, m);
984263bc 233 }
82c6f048 234rel:
fda7d388 235 bpf_reltoken();
984263bc
MD
236 }
237
238 /* Strip away media header */
469ac2dc 239 if (hlen > 0)
984263bc 240 m_adj(m, hlen);
4d723e5a
JS
241
242#ifdef ALTQ
243 /*
244 * altq for loop is just for debugging.
245 * only used when called for loop interface (not for
246 * a simplex interface).
247 */
248 if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) {
249 struct altq_pktattr pktattr;
250 int32_t *afp;
4d723e5a
JS
251
252 /*
253 * if the queueing discipline needs packet classification,
254 * do it before prepending link headers.
255 */
256 ifq_classify(&ifp->if_snd, m, af, &pktattr);
257
b5523eac 258 M_PREPEND(m, sizeof(int32_t), M_NOWAIT);
4090d6ff 259 if (m == NULL)
4d723e5a
JS
260 return(ENOBUFS);
261 afp = mtod(m, int32_t *);
262 *afp = (int32_t)af;
263
3c4cd924 264 return ifq_dispatch(ifp, m, &pktattr);
4d723e5a
JS
265 }
266#endif /* ALTQ */
984263bc
MD
267
268 /* Deliver to upper layer protocol */
269 switch (af) {
270#ifdef INET
271 case AF_INET:
984263bc
MD
272 isr = NETISR_IP;
273 break;
274#endif
275#ifdef INET6
276 case AF_INET6:
277 m->m_flags |= M_LOOP;
984263bc
MD
278 isr = NETISR_IPV6;
279 break;
f7ea898e 280#endif
984263bc 281 default:
4b1cf444 282 kprintf("if_simloop: can't handle af=%d\n", af);
984263bc
MD
283 m_freem(m);
284 return (EAFNOSUPPORT);
285 }
8bde602d 286
d40991ef
SZ
287 IFNET_STAT_INC(ifp, ipackets, 1);
288 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
8bde602d 289 netisr_queue(isr, m);
984263bc
MD
290 return (0);
291}
292
4d723e5a
JS
293#ifdef ALTQ
294static void
f0a26983 295lo_altqstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
4d723e5a
JS
296{
297 struct mbuf *m;
298 int32_t af, *afp;
4986965b 299 int isr;
4d723e5a
JS
300
301 while (1) {
4986965b 302 crit_enter();
ac9843a1 303 m = ifsq_dequeue(ifsq);
4986965b 304 crit_exit();
4d723e5a
JS
305 if (m == NULL)
306 return;
307
308 afp = mtod(m, int32_t *);
309 af = *afp;
310 m_adj(m, sizeof(int32_t));
311
312 switch (af) {
313#ifdef INET
314 case AF_INET:
315 isr = NETISR_IP;
316 break;
317#endif
318#ifdef INET6
319 case AF_INET6:
320 m->m_flags |= M_LOOP;
321 isr = NETISR_IPV6;
322 break;
ffd619d1 323#endif
4d723e5a 324 default:
4b1cf444 325 kprintf("lo_altqstart: can't handle af%d\n", af);
4d723e5a
JS
326 m_freem(m);
327 return;
328 }
329
d40991ef
SZ
330 IFNET_STAT_INC(ifp, ipackets, 1);
331 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
4d723e5a
JS
332 netisr_queue(isr, m);
333 }
334}
335#endif /* ALTQ */
336
984263bc
MD
337/* ARGSUSED */
338static void
00ee3f5e 339lo_rtrequest(int cmd, struct rtentry *rt)
984263bc
MD
340{
341 if (rt) {
342 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
343 /*
344 * For optimal performance, the send and receive buffers
345 * should be at least twice the MTU plus a little more for
346 * overhead.
347 */
f23061d4 348 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
984263bc
MD
349 }
350}
351
352/*
353 * Process an ioctl request.
354 */
355/* ARGSUSED */
7d982e78 356static int
00ee3f5e 357lo_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
984263bc 358{
82ed7fc2
RG
359 struct ifaddr *ifa;
360 struct ifreq *ifr = (struct ifreq *)data;
72662a67 361 int error = 0, mask;
984263bc
MD
362
363 switch (cmd) {
984263bc
MD
364 case SIOCSIFADDR:
365 ifp->if_flags |= IFF_UP | IFF_RUNNING;
366 ifa = (struct ifaddr *)data;
00ee3f5e 367 ifa->ifa_rtrequest = lo_rtrequest;
984263bc
MD
368 /*
369 * Everything else is done at a higher level.
370 */
371 break;
372
373 case SIOCADDMULTI:
374 case SIOCDELMULTI:
4090d6ff 375 if (ifr == NULL) {
984263bc
MD
376 error = EAFNOSUPPORT; /* XXX */
377 break;
378 }
379 switch (ifr->ifr_addr.sa_family) {
380
381#ifdef INET
382 case AF_INET:
383 break;
384#endif
385#ifdef INET6
386 case AF_INET6:
387 break;
388#endif
389
390 default:
391 error = EAFNOSUPPORT;
392 break;
393 }
394 break;
395
396 case SIOCSIFMTU:
397 ifp->if_mtu = ifr->ifr_mtu;
398 break;
399
400 case SIOCSIFFLAGS:
401 break;
402
72662a67 403 case SIOCSIFCAP:
8d4ec101
SZ
404 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
405 if (mask & IFCAP_HWCSUM) {
406 ifp->if_capenable ^= (mask & IFCAP_HWCSUM);
72662a67
SZ
407 if (IFCAP_TXCSUM & ifp->if_capenable)
408 ifp->if_hwassist = LO_CSUM_FEATURES;
409 else
410 ifp->if_hwassist = 0;
411 }
8d4ec101
SZ
412 if (mask & IFCAP_RSS)
413 ifp->if_capenable ^= IFCAP_RSS;
72662a67
SZ
414 break;
415
984263bc
MD
416 default:
417 error = EINVAL;
418 }
419 return (error);
420}