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