if_loop.c revision 1.33 1 /* $NetBSD: if_loop.c,v 1.33 2000/12/18 19:50:44 thorpej 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 "opt_inet.h"
72 #include "opt_atalk.h"
73 #include "opt_iso.h"
74 #include "opt_ns.h"
75
76 #include "bpfilter.h"
77 #include "loop.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 #else
137 #define LOMTU (32768 + MHLEN + MLEN)
138 #endif
139
140 struct ifnet loif[NLOOP];
141
142 void lostart(struct ifnet *);
143
144 void
145 loopattach(n)
146 int n;
147 {
148 int i;
149 struct ifnet *ifp;
150
151 for (i = 0; i < NLOOP; i++) {
152 ifp = &loif[i];
153 sprintf(ifp->if_xname, "lo%d", i);
154 ifp->if_softc = NULL;
155 ifp->if_mtu = LOMTU;
156 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
157 ifp->if_ioctl = loioctl;
158 ifp->if_output = looutput;
159 ifp->if_start = lostart;
160 ifp->if_type = IFT_LOOP;
161 ifp->if_hdrlen = 0;
162 ifp->if_addrlen = 0;
163 ifp->if_dlt = DLT_NULL;
164 IFQ_SET_READY(&ifp->if_snd);
165 if_attach(ifp);
166 #if NBPFILTER > 0
167 bpfattach(ifp, DLT_NULL, sizeof(u_int));
168 #endif
169 }
170 }
171
172 int
173 looutput(ifp, m, dst, rt)
174 struct ifnet *ifp;
175 struct mbuf *m;
176 struct sockaddr *dst;
177 struct rtentry *rt;
178 {
179 int s, isr;
180 struct ifqueue *ifq = 0;
181
182 if ((m->m_flags & M_PKTHDR) == 0)
183 panic("looutput: no header mbuf");
184 ifp->if_lastchange = time;
185 #if NBPFILTER > 0
186 if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK)) {
187 /*
188 * We need to prepend the address family as
189 * a four byte field. Cons up a dummy header
190 * to pacify bpf. This is safe because bpf
191 * will only read from the mbuf (i.e., it won't
192 * try to free it or keep a pointer to it).
193 */
194 struct mbuf m0;
195 u_int af = dst->sa_family;
196
197 m0.m_next = m;
198 m0.m_len = 4;
199 m0.m_data = (char *)⁡
200
201 bpf_mtap(ifp->if_bpf, &m0);
202 }
203 #endif
204 m->m_pkthdr.rcvif = ifp;
205
206 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
207 m_freem(m);
208 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
209 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
210 }
211
212 #ifndef PULLDOWN_TEST
213 /*
214 * KAME requires that the packet to be contiguous on the
215 * mbuf. We need to make that sure.
216 * this kind of code should be avoided.
217 * XXX other conditions to avoid running this part?
218 */
219 if (m->m_len != m->m_pkthdr.len) {
220 struct mbuf *n = NULL;
221 int maxlen;
222
223 MGETHDR(n, M_DONTWAIT, MT_HEADER);
224 maxlen = MHLEN;
225 if (n)
226 M_COPY_PKTHDR(n, m);
227 if (n && m->m_pkthdr.len > maxlen) {
228 MCLGET(n, M_DONTWAIT);
229 maxlen = MCLBYTES;
230 if ((n->m_flags & M_EXT) == 0) {
231 m_free(n);
232 n = NULL;
233 }
234 }
235 if (!n) {
236 printf("looutput: mbuf allocation failed\n");
237 m_freem(m);
238 return ENOBUFS;
239 }
240
241 if (m->m_pkthdr.len <= maxlen) {
242 m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
243 n->m_len = m->m_pkthdr.len;
244 n->m_next = NULL;
245 m_freem(m);
246 } else {
247 m_copydata(m, 0, maxlen, mtod(n, caddr_t));
248 m_adj(m, maxlen);
249 n->m_len = maxlen;
250 n->m_next = m;
251 m->m_flags &= ~M_PKTHDR;
252 }
253 m = n;
254 }
255 #if 0
256 if (m && m->m_next != NULL) {
257 printf("loop: not contiguous...\n");
258 m_freem(m);
259 return ENOBUFS;
260 }
261 #endif
262 #endif
263
264 ifp->if_opackets++;
265 ifp->if_obytes += m->m_pkthdr.len;
266
267 #ifdef ALTQ
268 /*
269 * ALTQ on the loopback interface is just for debugging. It's
270 * used only for loopback interfaces, not for a simplex interface.
271 */
272 if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) &&
273 ifp->if_start == lostart) {
274 struct altq_pktattr pktattr;
275 int error;
276
277 /*
278 * If the queueing discipline needs packet classification,
279 * do it before prepending the link headers.
280 */
281 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
282
283 M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
284 if (m == NULL)
285 return (ENOBUFS);
286 *(mtod(m, uint32_t *)) = dst->sa_family;
287
288 s = splimp();
289 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
290 (*ifp->if_start)(ifp);
291 splx(s);
292 return (error);
293 }
294 #endif /* ALTQ */
295
296 switch (dst->sa_family) {
297
298 #ifdef INET
299 case AF_INET:
300 ifq = &ipintrq;
301 isr = NETISR_IP;
302 break;
303 #endif
304 #ifdef INET6
305 case AF_INET6:
306 m->m_flags |= M_LOOP;
307 ifq = &ip6intrq;
308 isr = NETISR_IPV6;
309 break;
310 #endif
311 #ifdef NS
312 case AF_NS:
313 ifq = &nsintrq;
314 isr = NETISR_NS;
315 break;
316 #endif
317 #ifdef ISO
318 case AF_ISO:
319 ifq = &clnlintrq;
320 isr = NETISR_ISO;
321 break;
322 #endif
323 #ifdef IPX
324 case AF_IPX:
325 ifq = &ipxintrq;
326 isr = NETISR_IPX;
327 break;
328 #endif
329 #ifdef NETATALK
330 case AF_APPLETALK:
331 ifq = &atintrq2;
332 isr = NETISR_ATALK;
333 break;
334 #endif
335 default:
336 printf("%s: can't handle af%d\n", ifp->if_xname,
337 dst->sa_family);
338 m_freem(m);
339 return (EAFNOSUPPORT);
340 }
341 s = splimp();
342 if (IF_QFULL(ifq)) {
343 IF_DROP(ifq);
344 m_freem(m);
345 splx(s);
346 return (ENOBUFS);
347 }
348 IF_ENQUEUE(ifq, m);
349 schednetisr(isr);
350 ifp->if_ipackets++;
351 ifp->if_ibytes += m->m_pkthdr.len;
352 splx(s);
353 return (0);
354 }
355
356 #ifdef ALTQ
357 void
358 lostart(struct ifnet *ifp)
359 {
360 struct ifqueue *ifq;
361 struct mbuf *m;
362 uint32_t af;
363 int s, isr;
364
365 for (;;) {
366 IFQ_DEQUEUE(&ifp->if_snd, m);
367 if (m == NULL)
368 return;
369
370 af = *(mtod(m, uint32_t *));
371 m_adj(m, sizeof(uint32_t));
372
373 switch (af) {
374 #ifdef INET
375 case AF_INET:
376 ifq = &ipintrq;
377 isr = NETISR_IP;
378 break;
379 #endif
380 #ifdef INET6
381 case AF_INET6:
382 m->m_flags |= M_LOOP;
383 ifq = &ip6intrq;
384 isr = NETISR_IPV6;
385 break;
386 #endif
387 #ifdef IPX
388 case AF_IPX:
389 ifq = &ipxintrq;
390 isr = NETISR_IPX;
391 break;
392 #endif
393 #ifdef NS
394 case AF_NS:
395 ifq = &nsintrq;
396 isr = NETISR_NS;
397 break;
398 #endif
399 #ifdef ISO
400 case AF_ISO:
401 ifq = &clnlintrq;
402 isr = NETISR_ISO;
403 break;
404 #endif
405 #ifdef NETATALK
406 case AF_APPLETALK:
407 ifq = &atintrq2;
408 isr = NETISR_ATALK;
409 break;
410 #endif
411 default:
412 printf("%s: can't handle af%d\n", ifp->if_xname, af);
413 m_freem(m);
414 return;
415 }
416
417 s = splimp();
418 if (IF_QFULL(ifq)) {
419 IF_DROP(ifq);
420 splx(s);
421 m_freem(m);
422 return;
423 }
424 IF_ENQUEUE(ifq, m);
425 schednetisr(isr);
426 ifp->if_ipackets++;
427 ifp->if_ibytes += m->m_pkthdr.len;
428 splx(s);
429 }
430 }
431 #endif /* ALTQ */
432
433 /* ARGSUSED */
434 void
435 lortrequest(cmd, rt, sa)
436 int cmd;
437 struct rtentry *rt;
438 struct sockaddr *sa;
439 {
440
441 if (rt)
442 rt->rt_rmx.rmx_mtu = LOMTU;
443 }
444
445 /*
446 * Process an ioctl request.
447 */
448 /* ARGSUSED */
449 int
450 loioctl(ifp, cmd, data)
451 struct ifnet *ifp;
452 u_long cmd;
453 caddr_t data;
454 {
455 struct ifaddr *ifa;
456 struct ifreq *ifr;
457 int error = 0;
458
459 switch (cmd) {
460
461 case SIOCSIFADDR:
462 ifp->if_flags |= IFF_UP;
463 ifa = (struct ifaddr *)data;
464 if (ifa != 0 /*&& ifa->ifa_addr->sa_family == AF_ISO*/)
465 ifa->ifa_rtrequest = lortrequest;
466 /*
467 * Everything else is done at a higher level.
468 */
469 break;
470
471 case SIOCADDMULTI:
472 case SIOCDELMULTI:
473 ifr = (struct ifreq *)data;
474 if (ifr == 0) {
475 error = EAFNOSUPPORT; /* XXX */
476 break;
477 }
478 switch (ifr->ifr_addr.sa_family) {
479
480 #ifdef INET
481 case AF_INET:
482 break;
483 #endif
484 #ifdef INET6
485 case AF_INET6:
486 break;
487 #endif
488
489 default:
490 error = EAFNOSUPPORT;
491 break;
492 }
493 break;
494
495 default:
496 error = EINVAL;
497 }
498 return (error);
499 }
500