if_loop.c revision 1.76 1 /* $NetBSD: if_loop.c,v 1.76 2013/03/01 18:25:56 joerg 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.76 2013/03/01 18:25:56 joerg Exp $");
69
70 #include "opt_inet.h"
71 #include "opt_atalk.h"
72 #include "opt_ipx.h"
73 #include "opt_mbuftrace.h"
74 #include "opt_mpls.h"
75
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/mbuf.h>
81 #include <sys/socket.h>
82 #include <sys/errno.h>
83 #include <sys/ioctl.h>
84 #include <sys/time.h>
85
86 #include <sys/cpu.h>
87
88 #include <net/if.h>
89 #include <net/if_types.h>
90 #include <net/netisr.h>
91 #include <net/route.h>
92
93 #ifdef INET
94 #include <netinet/in.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/in_var.h>
97 #include <netinet/in_offload.h>
98 #include <netinet/ip.h>
99 #endif
100
101 #ifdef INET6
102 #ifndef INET
103 #include <netinet/in.h>
104 #endif
105 #include <netinet6/in6_var.h>
106 #include <netinet6/in6_offload.h>
107 #include <netinet/ip6.h>
108 #endif
109
110 #ifdef IPX
111 #include <netipx/ipx.h>
112 #include <netipx/ipx_if.h>
113 #endif
114
115 #ifdef MPLS
116 #include <netmpls/mpls.h>
117 #include <netmpls/mpls_var.h>
118 #endif
119
120 #ifdef NETATALK
121 #include <netatalk/at.h>
122 #include <netatalk/at_var.h>
123 #endif
124
125 #include <net/bpf.h>
126
127 #if defined(LARGE_LOMTU)
128 #define LOMTU (131072 + MHLEN + MLEN)
129 #define LOMTU_MAX LOMTU
130 #else
131 #define LOMTU (32768 + MHLEN + MLEN)
132 #define LOMTU_MAX (65536 + MHLEN + MLEN)
133 #endif
134
135 #ifdef ALTQ
136 static void lostart(struct ifnet *);
137 #endif
138
139 static int loop_clone_create(struct if_clone *, int);
140 static int loop_clone_destroy(struct ifnet *);
141
142 static struct if_clone loop_cloner =
143 IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy);
144
145 void
146 loopattach(int n)
147 {
148
149 (void)loop_clone_create(&loop_cloner, 0); /* lo0 always exists */
150 if_clone_attach(&loop_cloner);
151 }
152
153 static int
154 loop_clone_create(struct if_clone *ifc, int unit)
155 {
156 struct ifnet *ifp;
157
158 ifp = if_alloc(IFT_LOOP);
159
160 if_initname(ifp, ifc->ifc_name, unit);
161
162 ifp->if_mtu = LOMTU;
163 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST | IFF_RUNNING;
164 ifp->if_ioctl = loioctl;
165 ifp->if_output = looutput;
166 #ifdef ALTQ
167 ifp->if_start = lostart;
168 #endif
169 ifp->if_type = IFT_LOOP;
170 ifp->if_hdrlen = 0;
171 ifp->if_addrlen = 0;
172 ifp->if_dlt = DLT_NULL;
173 IFQ_SET_READY(&ifp->if_snd);
174 if (unit == 0)
175 lo0ifp = ifp;
176 if_attach(ifp);
177 if_alloc_sadl(ifp);
178 bpf_attach(ifp, DLT_NULL, sizeof(u_int));
179 #ifdef MBUFTRACE
180 ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
181 M_WAITOK | M_ZERO);
182 strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
183 sizeof(ifp->if_mowner->mo_name));
184 MOWNER_ATTACH(ifp->if_mowner);
185 #endif
186
187 return (0);
188 }
189
190 static int
191 loop_clone_destroy(struct ifnet *ifp)
192 {
193
194 if (ifp == lo0ifp)
195 return (EPERM);
196
197 #ifdef MBUFTRACE
198 MOWNER_DETACH(ifp->if_mowner);
199 free(ifp->if_mowner, M_DEVBUF);
200 #endif
201
202 bpf_detach(ifp);
203 if_detach(ifp);
204
205 free(ifp, M_DEVBUF);
206
207 return (0);
208 }
209
210 int
211 looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
212 struct rtentry *rt)
213 {
214 int s, isr = -1;
215 struct ifqueue *ifq = NULL;
216 int csum_flags;
217
218 MCLAIM(m, ifp->if_mowner);
219 if ((m->m_flags & M_PKTHDR) == 0)
220 panic("looutput: no header mbuf");
221 if (ifp->if_flags & IFF_LOOPBACK)
222 bpf_mtap_af(ifp, dst->sa_family, m);
223 m->m_pkthdr.rcvif = ifp;
224
225 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
226 m_freem(m);
227 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
228 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
229 }
230
231 ifp->if_opackets++;
232 ifp->if_obytes += m->m_pkthdr.len;
233
234 #ifdef ALTQ
235 /*
236 * ALTQ on the loopback interface is just for debugging. It's
237 * used only for loopback interfaces, not for a simplex interface.
238 */
239 if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) &&
240 ifp->if_start == lostart) {
241 struct altq_pktattr pktattr;
242 int error;
243
244 /*
245 * If the queueing discipline needs packet classification,
246 * do it before prepending the link headers.
247 */
248 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
249
250 M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
251 if (m == NULL)
252 return (ENOBUFS);
253 *(mtod(m, uint32_t *)) = dst->sa_family;
254
255 s = splnet();
256 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
257 (*ifp->if_start)(ifp);
258 splx(s);
259 return (error);
260 }
261 #endif /* ALTQ */
262
263 m_tag_delete_nonpersistent(m);
264
265 #ifdef MPLS
266 if (rt != NULL && rt_gettag(rt) != NULL &&
267 rt_gettag(rt)->sa_family == AF_MPLS &&
268 (m->m_flags & (M_MCAST | M_BCAST)) == 0) {
269 union mpls_shim msh;
270 msh.s_addr = MPLS_GETSADDR(rt);
271 if (msh.shim.label != MPLS_LABEL_IMPLNULL) {
272 ifq = &mplsintrq;
273 isr = NETISR_MPLS;
274 }
275 }
276 if (isr != NETISR_MPLS)
277 #endif
278 switch (dst->sa_family) {
279
280 #ifdef INET
281 case AF_INET:
282 csum_flags = m->m_pkthdr.csum_flags;
283 KASSERT((csum_flags & ~(M_CSUM_IPv4|M_CSUM_UDPv4)) == 0);
284 if (csum_flags != 0 && IN_LOOPBACK_NEED_CHECKSUM(csum_flags)) {
285 ip_undefer_csum(m, 0, csum_flags);
286 }
287 m->m_pkthdr.csum_flags = 0;
288 ifq = &ipintrq;
289 isr = NETISR_IP;
290 break;
291 #endif
292 #ifdef INET6
293 case AF_INET6:
294 csum_flags = m->m_pkthdr.csum_flags;
295 KASSERT((csum_flags & ~M_CSUM_UDPv6) == 0);
296 if (csum_flags != 0 &&
297 IN6_LOOPBACK_NEED_CHECKSUM(csum_flags)) {
298 ip6_undefer_csum(m, 0, csum_flags);
299 }
300 m->m_pkthdr.csum_flags = 0;
301 m->m_flags |= M_LOOP;
302 ifq = &ip6intrq;
303 isr = NETISR_IPV6;
304 break;
305 #endif
306 #ifdef IPX
307 case AF_IPX:
308 ifq = &ipxintrq;
309 isr = NETISR_IPX;
310 break;
311 #endif
312 #ifdef NETATALK
313 case AF_APPLETALK:
314 ifq = &atintrq2;
315 isr = NETISR_ATALK;
316 break;
317 #endif
318 default:
319 printf("%s: can't handle af%d\n", ifp->if_xname,
320 dst->sa_family);
321 m_freem(m);
322 return (EAFNOSUPPORT);
323 }
324 s = splnet();
325 if (IF_QFULL(ifq)) {
326 IF_DROP(ifq);
327 m_freem(m);
328 splx(s);
329 return (ENOBUFS);
330 }
331 IF_ENQUEUE(ifq, m);
332 schednetisr(isr);
333 ifp->if_ipackets++;
334 ifp->if_ibytes += m->m_pkthdr.len;
335 splx(s);
336 return (0);
337 }
338
339 #ifdef ALTQ
340 static void
341 lostart(struct ifnet *ifp)
342 {
343 struct ifqueue *ifq;
344 struct mbuf *m;
345 uint32_t af;
346 int s, isr;
347
348 for (;;) {
349 IFQ_DEQUEUE(&ifp->if_snd, m);
350 if (m == NULL)
351 return;
352
353 af = *(mtod(m, uint32_t *));
354 m_adj(m, sizeof(uint32_t));
355
356 switch (af) {
357 #ifdef INET
358 case AF_INET:
359 ifq = &ipintrq;
360 isr = NETISR_IP;
361 break;
362 #endif
363 #ifdef INET6
364 case AF_INET6:
365 m->m_flags |= M_LOOP;
366 ifq = &ip6intrq;
367 isr = NETISR_IPV6;
368 break;
369 #endif
370 #ifdef IPX
371 case AF_IPX:
372 ifq = &ipxintrq;
373 isr = NETISR_IPX;
374 break;
375 #endif
376 #ifdef NETATALK
377 case AF_APPLETALK:
378 ifq = &atintrq2;
379 isr = NETISR_ATALK;
380 break;
381 #endif
382 default:
383 printf("%s: can't handle af%d\n", ifp->if_xname, af);
384 m_freem(m);
385 return;
386 }
387
388 s = splnet();
389 if (IF_QFULL(ifq)) {
390 IF_DROP(ifq);
391 splx(s);
392 m_freem(m);
393 return;
394 }
395 IF_ENQUEUE(ifq, m);
396 schednetisr(isr);
397 ifp->if_ipackets++;
398 ifp->if_ibytes += m->m_pkthdr.len;
399 splx(s);
400 }
401 }
402 #endif /* ALTQ */
403
404 /* ARGSUSED */
405 void
406 lortrequest(int cmd, struct rtentry *rt,
407 const struct rt_addrinfo *info)
408 {
409
410 if (rt)
411 rt->rt_rmx.rmx_mtu = lo0ifp->if_mtu;
412 }
413
414 /*
415 * Process an ioctl request.
416 */
417 /* ARGSUSED */
418 int
419 loioctl(struct ifnet *ifp, u_long cmd, void *data)
420 {
421 struct ifaddr *ifa;
422 struct ifreq *ifr = data;
423 int error = 0;
424
425 switch (cmd) {
426
427 case SIOCINITIFADDR:
428 ifp->if_flags |= IFF_UP;
429 ifa = (struct ifaddr *)data;
430 if (ifa != NULL)
431 ifa->ifa_rtrequest = lortrequest;
432 /*
433 * Everything else is done at a higher level.
434 */
435 break;
436
437 case SIOCSIFMTU:
438 if ((unsigned)ifr->ifr_mtu > LOMTU_MAX)
439 error = EINVAL;
440 else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET){
441 error = 0;
442 }
443 break;
444
445 case SIOCADDMULTI:
446 case SIOCDELMULTI:
447 if (ifr == NULL) {
448 error = EAFNOSUPPORT; /* XXX */
449 break;
450 }
451 switch (ifreq_getaddr(cmd, ifr)->sa_family) {
452
453 #ifdef INET
454 case AF_INET:
455 break;
456 #endif
457 #ifdef INET6
458 case AF_INET6:
459 break;
460 #endif
461
462 default:
463 error = EAFNOSUPPORT;
464 break;
465 }
466 break;
467
468 default:
469 error = ifioctl_common(ifp, cmd, data);
470 }
471 return (error);
472 }
473