loopback: Use ifclone APIs to create loopback interfaces.
[dragonfly.git] / sys / net / if_loop.c
blob6898d865ac6be5d37b242e48b13cacb4e232b1df
1 /*
2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
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.
29 * @(#)if_loop.c 8.1 (Berkeley) 6/10/93
30 * $FreeBSD: src/sys/net/if_loop.c,v 1.47.2.9 2004/02/08 08:40:24 silby Exp $
34 * Loopback interface driver for protocol testing and timing.
36 #include "use_loop.h"
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
49 #include <sys/mplock2.h>
51 #include <net/if.h>
52 #include <net/if_types.h>
53 #include <net/if_clone.h>
54 #include <net/ifq_var.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/bpf.h>
58 #include <net/bpfdesc.h>
60 #ifdef INET
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #endif
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
73 static int lo_clone_create(struct if_clone *, int, caddr_t);
74 static int lo_clone_destroy(struct ifnet *);
76 static int looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
77 struct rtentry *);
78 static int loioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
79 static void lortrequest(int, struct rtentry *);
80 #ifdef ALTQ
81 static void lo_altqstart(struct ifnet *, struct ifaltq_subque *);
82 #endif
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
92 #define LO_CSUM_FEATURES (CSUM_IP | CSUM_UDP | CSUM_TCP)
94 struct ifnet *loif;
96 static struct if_clone lo_cloner = IF_CLONE_INITIALIZER("lo",
97 lo_clone_create, lo_clone_destroy, NLOOP, IF_MAXUNIT);
99 static void
100 lo_sysinit(void *dummy __unused)
103 if_clone_attach(&lo_cloner);
105 SYSINIT(lo_sysinit, SI_SUB_PSEUDO, SI_ORDER_ANY, lo_sysinit, NULL);
107 static int
108 lo_clone_create(struct if_clone *ifc __unused, int unit, caddr_t param __unused)
110 struct ifnet *ifp;
112 ifp = kmalloc(sizeof(*ifp), M_IFNET, M_WAITOK | M_ZERO);
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;
120 ifp->if_ioctl = loioctl;
121 ifp->if_output = looutput;
122 ifp->if_type = IFT_LOOP;
123 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
124 ifq_set_ready(&ifp->if_snd);
125 #ifdef ALTQ
126 ifp->if_start = lo_altqstart;
127 #endif
128 if_attach(ifp, NULL);
129 bpfattach(ifp, DLT_NULL, sizeof(u_int));
131 if (loif == NULL) {
132 KASSERT(unit == 0, ("loif is %s", ifp->if_xname));
133 loif = ifp;
135 return (0);
138 static int
139 lo_clone_destroy(struct ifnet *ifp)
142 if (loif == ifp)
143 return (EPERM);
145 bpfdetach(ifp);
146 if_detach(ifp);
147 return (0);
150 static int
151 looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
152 struct rtentry *rt)
154 M_ASSERTPKTHDR(m);
156 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
157 m_freem(m);
158 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
159 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
162 IFNET_STAT_INC(ifp, opackets, 1);
163 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len);
164 #if 1 /* XXX */
165 switch (dst->sa_family) {
166 case AF_INET:
167 case AF_INET6:
168 break;
169 default:
170 kprintf("looutput: af=%d unexpected\n", dst->sa_family);
171 m_freem(m);
172 return (EAFNOSUPPORT);
174 #endif
176 if (ifp->if_capenable & IFCAP_RXCSUM) {
177 int csum_flags = 0;
179 if (m->m_pkthdr.csum_flags & CSUM_IP)
180 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
181 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
182 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
184 m->m_pkthdr.csum_flags |= csum_flags;
185 if (csum_flags & CSUM_DATA_VALID)
186 m->m_pkthdr.csum_data = 0xffff;
188 if ((ifp->if_capenable & IFCAP_RSS) == 0)
189 m->m_flags &= ~M_HASH;
190 return (if_simloop(ifp, m, dst->sa_family, 0));
194 * if_simloop()
196 * This function is to support software emulation of hardware loopback,
197 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
198 * hear their own broadcasts, we create a copy of the packet that we
199 * would normally receive via a hardware loopback.
201 * This function expects the packet to include the media header of length hlen.
204 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
206 int isr;
208 KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
209 m->m_pkthdr.rcvif = ifp;
211 /* BPF write needs to be handled specially */
212 if (af == AF_UNSPEC) {
213 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
214 af = *(mtod(m, int *));
215 m->m_len -= sizeof(int);
216 m->m_pkthdr.len -= sizeof(int);
217 m->m_data += sizeof(int);
220 if (ifp->if_bpf) {
221 bpf_gettoken();
223 /* Re-check */
224 if (ifp->if_bpf == NULL)
225 goto rel;
227 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
228 uint32_t bpf_af = (uint32_t)af;
229 bpf_ptap(ifp->if_bpf, m, &bpf_af, 4);
230 } else {
231 bpf_mtap(ifp->if_bpf, m);
233 rel:
234 bpf_reltoken();
237 /* Strip away media header */
238 if (hlen > 0)
239 m_adj(m, hlen);
241 #ifdef ALTQ
243 * altq for loop is just for debugging.
244 * only used when called for loop interface (not for
245 * a simplex interface).
247 if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) {
248 struct altq_pktattr pktattr;
249 int32_t *afp;
252 * if the queueing discipline needs packet classification,
253 * do it before prepending link headers.
255 ifq_classify(&ifp->if_snd, m, af, &pktattr);
257 M_PREPEND(m, sizeof(int32_t), M_NOWAIT);
258 if (m == NULL)
259 return(ENOBUFS);
260 afp = mtod(m, int32_t *);
261 *afp = (int32_t)af;
263 return ifq_dispatch(ifp, m, &pktattr);
265 #endif /* ALTQ */
267 /* Deliver to upper layer protocol */
268 switch (af) {
269 #ifdef INET
270 case AF_INET:
271 isr = NETISR_IP;
272 break;
273 #endif
274 #ifdef INET6
275 case AF_INET6:
276 m->m_flags |= M_LOOP;
277 isr = NETISR_IPV6;
278 break;
279 #endif
280 default:
281 kprintf("if_simloop: can't handle af=%d\n", af);
282 m_freem(m);
283 return (EAFNOSUPPORT);
286 IFNET_STAT_INC(ifp, ipackets, 1);
287 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
288 netisr_queue(isr, m);
289 return (0);
292 #ifdef ALTQ
293 static void
294 lo_altqstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
296 struct mbuf *m;
297 int32_t af, *afp;
298 int isr;
300 while (1) {
301 crit_enter();
302 m = ifsq_dequeue(ifsq);
303 crit_exit();
304 if (m == NULL)
305 return;
307 afp = mtod(m, int32_t *);
308 af = *afp;
309 m_adj(m, sizeof(int32_t));
311 switch (af) {
312 #ifdef INET
313 case AF_INET:
314 isr = NETISR_IP;
315 break;
316 #endif
317 #ifdef INET6
318 case AF_INET6:
319 m->m_flags |= M_LOOP;
320 isr = NETISR_IPV6;
321 break;
322 #endif
323 default:
324 kprintf("lo_altqstart: can't handle af%d\n", af);
325 m_freem(m);
326 return;
329 IFNET_STAT_INC(ifp, ipackets, 1);
330 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
331 netisr_queue(isr, m);
334 #endif /* ALTQ */
336 /* ARGSUSED */
337 static void
338 lortrequest(int cmd, struct rtentry *rt)
340 if (rt) {
341 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
343 * For optimal performance, the send and receive buffers
344 * should be at least twice the MTU plus a little more for
345 * overhead.
347 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
352 * Process an ioctl request.
354 /* ARGSUSED */
355 static int
356 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
358 struct ifaddr *ifa;
359 struct ifreq *ifr = (struct ifreq *)data;
360 int error = 0, mask;
362 switch (cmd) {
363 case SIOCSIFADDR:
364 ifp->if_flags |= IFF_UP | IFF_RUNNING;
365 ifa = (struct ifaddr *)data;
366 ifa->ifa_rtrequest = lortrequest;
368 * Everything else is done at a higher level.
370 break;
372 case SIOCADDMULTI:
373 case SIOCDELMULTI:
374 if (ifr == NULL) {
375 error = EAFNOSUPPORT; /* XXX */
376 break;
378 switch (ifr->ifr_addr.sa_family) {
380 #ifdef INET
381 case AF_INET:
382 break;
383 #endif
384 #ifdef INET6
385 case AF_INET6:
386 break;
387 #endif
389 default:
390 error = EAFNOSUPPORT;
391 break;
393 break;
395 case SIOCSIFMTU:
396 ifp->if_mtu = ifr->ifr_mtu;
397 break;
399 case SIOCSIFFLAGS:
400 break;
402 case SIOCSIFCAP:
403 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
404 if (mask & IFCAP_HWCSUM) {
405 ifp->if_capenable ^= (mask & IFCAP_HWCSUM);
406 if (IFCAP_TXCSUM & ifp->if_capenable)
407 ifp->if_hwassist = LO_CSUM_FEATURES;
408 else
409 ifp->if_hwassist = 0;
411 if (mask & IFCAP_RSS)
412 ifp->if_capenable ^= IFCAP_RSS;
413 break;
415 default:
416 error = EINVAL;
418 return (error);