if_loop.c revision 1.52 1 /* $NetBSD: if_loop.c,v 1.52 2004/12/04 16:10:25 peter Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)if_loop.c 8.2 (Berkeley) 1/9/95
61 */
62
63 /*
64 * Loopback interface driver for protocol testing and timing.
65 */
66
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: if_loop.c,v 1.52 2004/12/04 16:10:25 peter Exp $");
69
70 #include "opt_inet.h"
71 #include "opt_atalk.h"
72 #include "opt_iso.h"
73 #include "opt_ns.h"
74 #include "opt_ipx.h"
75 #include "opt_mbuftrace.h"
76
77 #include "bpfilter.h"
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/mbuf.h>
83 #include <sys/socket.h>
84 #include <sys/errno.h>
85 #include <sys/ioctl.h>
86 #include <sys/time.h>
87
88 #include <machine/cpu.h>
89
90 #include <net/if.h>
91 #include <net/if_types.h>
92 #include <net/netisr.h>
93 #include <net/route.h>
94
95 #ifdef INET
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/in_var.h>
99 #include <netinet/ip.h>
100 #endif
101
102 #ifdef INET6
103 #ifndef INET
104 #include <netinet/in.h>
105 #endif
106 #include <netinet6/in6_var.h>
107 #include <netinet/ip6.h>
108 #endif
109
110 #ifdef NS
111 #include <netns/ns.h>
112 #include <netns/ns_if.h>
113 #endif
114
115 #ifdef IPX
116 #include <netipx/ipx.h>
117 #include <netipx/ipx_if.h>
118 #endif
119
120 #ifdef ISO
121 #include <netiso/iso.h>
122 #include <netiso/iso_var.h>
123 #endif
124
125 #ifdef NETATALK
126 #include <netatalk/at.h>
127 #include <netatalk/at_var.h>
128 #endif
129
130 #if NBPFILTER > 0
131 #include <net/bpf.h>
132 #endif
133
134 #if defined(LARGE_LOMTU)
135 #define LOMTU (131072 + MHLEN + MLEN)
136 #define LOMTU_MAX LOMTU
137 #else
138 #define LOMTU (32768 + MHLEN + MLEN)
139 #define LOMTU_MAX (65536 + MHLEN + MLEN)
140 #endif
141
142 #ifdef ALTQ
143 void lostart(struct ifnet *);
144 #endif
145
146 struct loop_softc {
147 LIST_ENTRY(loop_softc) sc_list;
148 struct ifnet sc_if;
149 };
150
151 LIST_HEAD(, loop_softc) loop_softc_list;
152
153 int loop_clone_create(struct if_clone *, int);
154 void loop_clone_destroy(struct ifnet *);
155
156 struct if_clone loop_cloner =
157 IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy);
158
159 void
160 loopattach(int n)
161 {
162 LIST_INIT(&loop_softc_list);
163
164 (void)loop_clone_create(&loop_cloner, 0); /* lo0 always exists */
165 if_clone_attach(&loop_cloner);
166 }
167
168 int
169 loop_clone_create(struct if_clone *ifc, int unit)
170 {
171 struct loop_softc *sc;
172
173 sc = malloc(sizeof(struct loop_softc), M_DEVBUF, M_WAITOK | M_ZERO);
174
175 snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
176 ifc->ifc_name, unit);
177
178 sc->sc_if.if_softc = sc;
179 sc->sc_if.if_mtu = LOMTU;
180 sc->sc_if.if_flags = IFF_LOOPBACK | IFF_MULTICAST;
181 sc->sc_if.if_ioctl = loioctl;
182 sc->sc_if.if_output = looutput;
183 #ifdef ALTQ
184 sc->sc_if.if_start = lostart;
185 #endif
186 sc->sc_if.if_type = IFT_LOOP;
187 sc->sc_if.if_hdrlen = 0;
188 sc->sc_if.if_addrlen = 0;
189 sc->sc_if.if_dlt = DLT_NULL;
190 IFQ_SET_READY(&sc->sc_if.if_snd);
191 if (unit == 0)
192 lo0ifp = &sc->sc_if;
193 if_attach(&sc->sc_if);
194 if_alloc_sadl(&sc->sc_if);
195 #if NBPFILTER > 0
196 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
197 #endif
198 #ifdef MBUFTRACE
199 sc->sc_if.if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
200 M_WAITOK | M_ZERO);
201 strlcpy(sc->sc_if.if_mowner->mo_name, sc->sc_if.if_xname,
202 sizeof(sc->sc_if.if_mowner->mo_name));
203 MOWNER_ATTACH(sc->sc_if.if_mowner);
204 #endif
205 LIST_INSERT_HEAD(&loop_softc_list, sc, sc_list);
206
207 return (0);
208 }
209
210 void
211 loop_clone_destroy(struct ifnet *ifp)
212 {
213 struct loop_softc *sc = ifp->if_softc;
214
215 if (ifp == lo0ifp) /* don't kill lo0 */
216 return;
217
218 #ifdef MBUFTRACE
219 MOWNER_DETACH(ifp->if_mowner);
220 free(ifp->if_mowner, M_DEVBUF);
221 #endif
222
223 #if NBPFILTER > 0
224 bpfdetach(ifp);
225 #endif
226 if_detach(ifp);
227
228 LIST_REMOVE(sc, sc_list);
229 free(sc, M_DEVBUF);
230 }
231
232 int
233 looutput(ifp, m, dst, rt)
234 struct ifnet *ifp;
235 struct mbuf *m;
236 struct sockaddr *dst;
237 struct rtentry *rt;
238 {
239 int s, isr;
240 struct ifqueue *ifq = 0;
241
242 MCLAIM(m, ifp->if_mowner);
243 if ((m->m_flags & M_PKTHDR) == 0)
244 panic("looutput: no header mbuf");
245 #if NBPFILTER > 0
246 if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK))
247 bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
248 #endif
249 m->m_pkthdr.rcvif = ifp;
250
251 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
252 m_freem(m);
253 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
254 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
255 }
256
257 ifp->if_opackets++;
258 ifp->if_obytes += m->m_pkthdr.len;
259
260 #ifdef ALTQ
261 /*
262 * ALTQ on the loopback interface is just for debugging. It's
263 * used only for loopback interfaces, not for a simplex interface.
264 */
265 if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) &&
266 ifp->if_start == lostart) {
267 struct altq_pktattr pktattr;
268 int error;
269
270 /*
271 * If the queueing discipline needs packet classification,
272 * do it before prepending the link headers.
273 */
274 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
275
276 M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
277 if (m == NULL)
278 return (ENOBUFS);
279 *(mtod(m, uint32_t *)) = dst->sa_family;
280
281 s = splnet();
282 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
283 (*ifp->if_start)(ifp);
284 splx(s);
285 return (error);
286 }
287 #endif /* ALTQ */
288
289 m_tag_delete_nonpersistent(m);
290
291 switch (dst->sa_family) {
292
293 #ifdef INET
294 case AF_INET:
295 ifq = &ipintrq;
296 isr = NETISR_IP;
297 break;
298 #endif
299 #ifdef INET6
300 case AF_INET6:
301 m->m_flags |= M_LOOP;
302 ifq = &ip6intrq;
303 isr = NETISR_IPV6;
304 break;
305 #endif
306 #ifdef NS
307 case AF_NS:
308 ifq = &nsintrq;
309 isr = NETISR_NS;
310 break;
311 #endif
312 #ifdef ISO
313 case AF_ISO:
314 ifq = &clnlintrq;
315 isr = NETISR_ISO;
316 break;
317 #endif
318 #ifdef IPX
319 case AF_IPX:
320 ifq = &ipxintrq;
321 isr = NETISR_IPX;
322 break;
323 #endif
324 #ifdef NETATALK
325 case AF_APPLETALK:
326 ifq = &atintrq2;
327 isr = NETISR_ATALK;
328 break;
329 #endif
330 default:
331 printf("%s: can't handle af%d\n", ifp->if_xname,
332 dst->sa_family);
333 m_freem(m);
334 return (EAFNOSUPPORT);
335 }
336 s = splnet();
337 if (IF_QFULL(ifq)) {
338 IF_DROP(ifq);
339 m_freem(m);
340 splx(s);
341 return (ENOBUFS);
342 }
343 IF_ENQUEUE(ifq, m);
344 schednetisr(isr);
345 ifp->if_ipackets++;
346 ifp->if_ibytes += m->m_pkthdr.len;
347 splx(s);
348 return (0);
349 }
350
351 #ifdef ALTQ
352 void
353 lostart(struct ifnet *ifp)
354 {
355 struct ifqueue *ifq;
356 struct mbuf *m;
357 uint32_t af;
358 int s, isr;
359
360 for (;;) {
361 IFQ_DEQUEUE(&ifp->if_snd, m);
362 if (m == NULL)
363 return;
364
365 af = *(mtod(m, uint32_t *));
366 m_adj(m, sizeof(uint32_t));
367
368 switch (af) {
369 #ifdef INET
370 case AF_INET:
371 ifq = &ipintrq;
372 isr = NETISR_IP;
373 break;
374 #endif
375 #ifdef INET6
376 case AF_INET6:
377 m->m_flags |= M_LOOP;
378 ifq = &ip6intrq;
379 isr = NETISR_IPV6;
380 break;
381 #endif
382 #ifdef IPX
383 case AF_IPX:
384 ifq = &ipxintrq;
385 isr = NETISR_IPX;
386 break;
387 #endif
388 #ifdef NS
389 case AF_NS:
390 ifq = &nsintrq;
391 isr = NETISR_NS;
392 break;
393 #endif
394 #ifdef ISO
395 case AF_ISO:
396 ifq = &clnlintrq;
397 isr = NETISR_ISO;
398 break;
399 #endif
400 #ifdef NETATALK
401 case AF_APPLETALK:
402 ifq = &atintrq2;
403 isr = NETISR_ATALK;
404 break;
405 #endif
406 default:
407 printf("%s: can't handle af%d\n", ifp->if_xname, af);
408 m_freem(m);
409 return;
410 }
411
412 s = splnet();
413 if (IF_QFULL(ifq)) {
414 IF_DROP(ifq);
415 splx(s);
416 m_freem(m);
417 return;
418 }
419 IF_ENQUEUE(ifq, m);
420 schednetisr(isr);
421 ifp->if_ipackets++;
422 ifp->if_ibytes += m->m_pkthdr.len;
423 splx(s);
424 }
425 }
426 #endif /* ALTQ */
427
428 /* ARGSUSED */
429 void
430 lortrequest(cmd, rt, info)
431 int cmd;
432 struct rtentry *rt;
433 struct rt_addrinfo *info;
434 {
435
436 if (rt)
437 rt->rt_rmx.rmx_mtu = lo0ifp->if_mtu;
438
439 }
440
441 /*
442 * Process an ioctl request.
443 */
444 /* ARGSUSED */
445 int
446 loioctl(ifp, cmd, data)
447 struct ifnet *ifp;
448 u_long cmd;
449 caddr_t data;
450 {
451 struct ifaddr *ifa;
452 struct ifreq *ifr;
453 int error = 0;
454
455 switch (cmd) {
456
457 case SIOCSIFADDR:
458 ifp->if_flags |= IFF_UP;
459 ifa = (struct ifaddr *)data;
460 if (ifa != 0 /*&& ifa->ifa_addr->sa_family == AF_ISO*/)
461 ifa->ifa_rtrequest = lortrequest;
462 /*
463 * Everything else is done at a higher level.
464 */
465 break;
466
467 case SIOCSIFMTU:
468 ifr = (struct ifreq *)data;
469 if ((unsigned)ifr->ifr_mtu > LOMTU_MAX)
470 error = EINVAL;
471 else {
472 /* XXX update rt mtu for AF_ISO? */
473 ifp->if_mtu = ifr->ifr_mtu;
474 }
475 break;
476
477 case SIOCADDMULTI:
478 case SIOCDELMULTI:
479 ifr = (struct ifreq *)data;
480 if (ifr == 0) {
481 error = EAFNOSUPPORT; /* XXX */
482 break;
483 }
484 switch (ifr->ifr_addr.sa_family) {
485
486 #ifdef INET
487 case AF_INET:
488 break;
489 #endif
490 #ifdef INET6
491 case AF_INET6:
492 break;
493 #endif
494
495 default:
496 error = EAFNOSUPPORT;
497 break;
498 }
499 break;
500
501 default:
502 error = EINVAL;
503 }
504 return (error);
505 }
506