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