if_loop.c revision 1.42 1 /* $NetBSD: if_loop.c,v 1.42 2003/02/26 06:31:13 matt 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. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)if_loop.c 8.2 (Berkeley) 1/9/95
65 */
66
67 /*
68 * Loopback interface driver for protocol testing and timing.
69 */
70
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: if_loop.c,v 1.42 2003/02/26 06:31:13 matt Exp $");
73
74 #include "opt_inet.h"
75 #include "opt_atalk.h"
76 #include "opt_iso.h"
77 #include "opt_ns.h"
78
79 #include "bpfilter.h"
80 #include "loop.h"
81
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/kernel.h>
85 #include <sys/mbuf.h>
86 #include <sys/socket.h>
87 #include <sys/errno.h>
88 #include <sys/ioctl.h>
89 #include <sys/time.h>
90
91 #include <machine/cpu.h>
92
93 #include <net/if.h>
94 #include <net/if_types.h>
95 #include <net/netisr.h>
96 #include <net/route.h>
97
98 #ifdef INET
99 #include <netinet/in.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/in_var.h>
102 #include <netinet/ip.h>
103 #endif
104
105 #ifdef INET6
106 #ifndef INET
107 #include <netinet/in.h>
108 #endif
109 #include <netinet6/in6_var.h>
110 #include <netinet/ip6.h>
111 #endif
112
113 #ifdef NS
114 #include <netns/ns.h>
115 #include <netns/ns_if.h>
116 #endif
117
118 #ifdef IPX
119 #include <netipx/ipx.h>
120 #include <netipx/ipx_if.h>
121 #endif
122
123 #ifdef ISO
124 #include <netiso/iso.h>
125 #include <netiso/iso_var.h>
126 #endif
127
128 #ifdef NETATALK
129 #include <netatalk/at.h>
130 #include <netatalk/at_var.h>
131 #endif
132
133 #if NBPFILTER > 0
134 #include <net/bpf.h>
135 #endif
136
137 #if defined(LARGE_LOMTU)
138 #define LOMTU (131072 + MHLEN + MLEN)
139 #else
140 #define LOMTU (32768 + MHLEN + MLEN)
141 #endif
142
143 struct ifnet loif[NLOOP];
144 #ifdef MBUFTRACE
145 struct mowner lomowner[NLOOP];
146 #endif
147
148 #ifdef ALTQ
149 void lostart(struct ifnet *);
150 #endif
151
152 void
153 loopattach(n)
154 int n;
155 {
156 int i;
157 struct ifnet *ifp;
158
159 for (i = 0; i < NLOOP; i++) {
160 ifp = &loif[i];
161 sprintf(ifp->if_xname, "lo%d", i);
162 ifp->if_softc = NULL;
163 ifp->if_mtu = LOMTU;
164 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
165 ifp->if_ioctl = loioctl;
166 ifp->if_output = looutput;
167 #ifdef ALTQ
168 ifp->if_start = lostart;
169 #endif
170 ifp->if_type = IFT_LOOP;
171 ifp->if_hdrlen = 0;
172 ifp->if_addrlen = 0;
173 ifp->if_dlt = DLT_NULL;
174 IFQ_SET_READY(&ifp->if_snd);
175 if_attach(ifp);
176 if_alloc_sadl(ifp);
177 #if NBPFILTER > 0
178 bpfattach(ifp, DLT_NULL, sizeof(u_int));
179 #endif
180 #ifdef MBUFTRACE
181 ifp->if_mowner = &lomowner[i];
182 strcpy(ifp->if_mowner->mo_name, ifp->if_xname);
183 MOWNER_ATTACH(&lomowner[i]);
184 #endif
185 }
186 }
187
188 int
189 looutput(ifp, m, dst, rt)
190 struct ifnet *ifp;
191 struct mbuf *m;
192 struct sockaddr *dst;
193 struct rtentry *rt;
194 {
195 int s, isr;
196 struct ifqueue *ifq = 0;
197
198 MCLAIM(m, ifp->if_mowner);
199 if ((m->m_flags & M_PKTHDR) == 0)
200 panic("looutput: no header mbuf");
201 #if NBPFILTER > 0
202 if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK)) {
203 /*
204 * We need to prepend the address family as
205 * a four byte field. Cons up a dummy header
206 * to pacify bpf. This is safe because bpf
207 * will only read from the mbuf (i.e., it won't
208 * try to free it or keep a pointer to it).
209 */
210 struct mbuf m0;
211 u_int32_t af = dst->sa_family;
212
213 M_COPY_PKTHDR(&m0, m);
214 m0.m_next = m;
215 m0.m_len = 4;
216 m0.m_data = (char *)⁡
217 m0.m_pkthdr.len += m0.m_len;
218
219 bpf_mtap(ifp->if_bpf, &m0);
220 }
221 #endif
222 m->m_pkthdr.rcvif = ifp;
223
224 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
225 m_freem(m);
226 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
227 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
228 }
229
230 #ifndef PULLDOWN_TEST
231 /*
232 * KAME requires that the packet to be contiguous on the
233 * mbuf. We need to make that sure.
234 * this kind of code should be avoided.
235 * XXX other conditions to avoid running this part?
236 */
237 if (m->m_len != m->m_pkthdr.len) {
238 struct mbuf *n = NULL;
239 int maxlen;
240
241 MGETHDR(n, M_DONTWAIT, MT_HEADER);
242 maxlen = MHLEN;
243 if (n)
244 M_COPY_PKTHDR(n, m);
245 if (n && m->m_pkthdr.len > maxlen) {
246 MCLGET(n, M_DONTWAIT);
247 maxlen = MCLBYTES;
248 if ((n->m_flags & M_EXT) == 0) {
249 m_free(n);
250 n = NULL;
251 }
252 }
253 if (!n) {
254 printf("looutput: mbuf allocation failed\n");
255 m_freem(m);
256 return ENOBUFS;
257 }
258
259 if (m->m_pkthdr.len <= maxlen) {
260 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
261 n->m_len = m->m_pkthdr.len;
262 n->m_next = NULL;
263 m_freem(m);
264 } else {
265 m_copydata(m, 0, maxlen, mtod(n, caddr_t));
266 m_adj(m, maxlen);
267 n->m_len = maxlen;
268 n->m_next = m;
269 m->m_flags &= ~M_PKTHDR;
270 }
271 m = n;
272 }
273 #if 0
274 if (m && m->m_next != NULL) {
275 printf("loop: not contiguous...\n");
276 m_freem(m);
277 return ENOBUFS;
278 }
279 #endif
280 #endif
281
282 ifp->if_opackets++;
283 ifp->if_obytes += m->m_pkthdr.len;
284
285 #ifdef ALTQ
286 /*
287 * ALTQ on the loopback interface is just for debugging. It's
288 * used only for loopback interfaces, not for a simplex interface.
289 */
290 if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) &&
291 ifp->if_start == lostart) {
292 struct altq_pktattr pktattr;
293 int error;
294
295 /*
296 * If the queueing discipline needs packet classification,
297 * do it before prepending the link headers.
298 */
299 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
300
301 M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
302 if (m == NULL)
303 return (ENOBUFS);
304 *(mtod(m, uint32_t *)) = dst->sa_family;
305
306 s = splnet();
307 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
308 (*ifp->if_start)(ifp);
309 splx(s);
310 return (error);
311 }
312 #endif /* ALTQ */
313
314 switch (dst->sa_family) {
315
316 #ifdef INET
317 case AF_INET:
318 ifq = &ipintrq;
319 isr = NETISR_IP;
320 break;
321 #endif
322 #ifdef INET6
323 case AF_INET6:
324 m->m_flags |= M_LOOP;
325 ifq = &ip6intrq;
326 isr = NETISR_IPV6;
327 break;
328 #endif
329 #ifdef NS
330 case AF_NS:
331 ifq = &nsintrq;
332 isr = NETISR_NS;
333 break;
334 #endif
335 #ifdef ISO
336 case AF_ISO:
337 ifq = &clnlintrq;
338 isr = NETISR_ISO;
339 break;
340 #endif
341 #ifdef IPX
342 case AF_IPX:
343 ifq = &ipxintrq;
344 isr = NETISR_IPX;
345 break;
346 #endif
347 #ifdef NETATALK
348 case AF_APPLETALK:
349 ifq = &atintrq2;
350 isr = NETISR_ATALK;
351 break;
352 #endif
353 default:
354 printf("%s: can't handle af%d\n", ifp->if_xname,
355 dst->sa_family);
356 m_freem(m);
357 return (EAFNOSUPPORT);
358 }
359 s = splnet();
360 if (IF_QFULL(ifq)) {
361 IF_DROP(ifq);
362 m_freem(m);
363 splx(s);
364 return (ENOBUFS);
365 }
366 IF_ENQUEUE(ifq, m);
367 schednetisr(isr);
368 ifp->if_ipackets++;
369 ifp->if_ibytes += m->m_pkthdr.len;
370 splx(s);
371 return (0);
372 }
373
374 #ifdef ALTQ
375 void
376 lostart(struct ifnet *ifp)
377 {
378 struct ifqueue *ifq;
379 struct mbuf *m;
380 uint32_t af;
381 int s, isr;
382
383 for (;;) {
384 IFQ_DEQUEUE(&ifp->if_snd, m);
385 if (m == NULL)
386 return;
387
388 af = *(mtod(m, uint32_t *));
389 m_adj(m, sizeof(uint32_t));
390
391 switch (af) {
392 #ifdef INET
393 case AF_INET:
394 ifq = &ipintrq;
395 isr = NETISR_IP;
396 break;
397 #endif
398 #ifdef INET6
399 case AF_INET6:
400 m->m_flags |= M_LOOP;
401 ifq = &ip6intrq;
402 isr = NETISR_IPV6;
403 break;
404 #endif
405 #ifdef IPX
406 case AF_IPX:
407 ifq = &ipxintrq;
408 isr = NETISR_IPX;
409 break;
410 #endif
411 #ifdef NS
412 case AF_NS:
413 ifq = &nsintrq;
414 isr = NETISR_NS;
415 break;
416 #endif
417 #ifdef ISO
418 case AF_ISO:
419 ifq = &clnlintrq;
420 isr = NETISR_ISO;
421 break;
422 #endif
423 #ifdef NETATALK
424 case AF_APPLETALK:
425 ifq = &atintrq2;
426 isr = NETISR_ATALK;
427 break;
428 #endif
429 default:
430 printf("%s: can't handle af%d\n", ifp->if_xname, af);
431 m_freem(m);
432 return;
433 }
434
435 s = splnet();
436 if (IF_QFULL(ifq)) {
437 IF_DROP(ifq);
438 splx(s);
439 m_freem(m);
440 return;
441 }
442 IF_ENQUEUE(ifq, m);
443 schednetisr(isr);
444 ifp->if_ipackets++;
445 ifp->if_ibytes += m->m_pkthdr.len;
446 splx(s);
447 }
448 }
449 #endif /* ALTQ */
450
451 /* ARGSUSED */
452 void
453 lortrequest(cmd, rt, info)
454 int cmd;
455 struct rtentry *rt;
456 struct rt_addrinfo *info;
457 {
458
459 if (rt)
460 rt->rt_rmx.rmx_mtu = LOMTU;
461 }
462
463 /*
464 * Process an ioctl request.
465 */
466 /* ARGSUSED */
467 int
468 loioctl(ifp, cmd, data)
469 struct ifnet *ifp;
470 u_long cmd;
471 caddr_t data;
472 {
473 struct ifaddr *ifa;
474 struct ifreq *ifr;
475 int error = 0;
476
477 switch (cmd) {
478
479 case SIOCSIFADDR:
480 ifp->if_flags |= IFF_UP;
481 ifa = (struct ifaddr *)data;
482 if (ifa != 0 /*&& ifa->ifa_addr->sa_family == AF_ISO*/)
483 ifa->ifa_rtrequest = lortrequest;
484 /*
485 * Everything else is done at a higher level.
486 */
487 break;
488
489 case SIOCADDMULTI:
490 case SIOCDELMULTI:
491 ifr = (struct ifreq *)data;
492 if (ifr == 0) {
493 error = EAFNOSUPPORT; /* XXX */
494 break;
495 }
496 switch (ifr->ifr_addr.sa_family) {
497
498 #ifdef INET
499 case AF_INET:
500 break;
501 #endif
502 #ifdef INET6
503 case AF_INET6:
504 break;
505 #endif
506
507 default:
508 error = EAFNOSUPPORT;
509 break;
510 }
511 break;
512
513 default:
514 error = EINVAL;
515 }
516 return (error);
517 }
518