584d70e106ac7fabb7fb9e75c489ffeac711b8ce
[dragonfly.git] / sys / net / if_loop.c
1 /*
2  * Copyright (c) 1982, 1986, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)if_loop.c   8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/net/if_loop.c,v 1.47.2.9 2004/02/08 08:40:24 silby Exp $
35  * $DragonFly: src/sys/net/if_loop.c,v 1.24 2008/09/06 05:46:47 sephe Exp $
36  */
37
38 /*
39  * Loopback interface driver for protocol testing and timing.
40  */
41 #include "use_loop.h"
42
43 #include "opt_atalk.h"
44 #include "opt_inet.h"
45 #include "opt_inet6.h"
46 #include "opt_ipx.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/mbuf.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54
55 #include <net/if.h>
56 #include <net/if_types.h>
57 #include <net/ifq_var.h>
58 #include <net/netisr.h>
59 #include <net/route.h>
60 #include <net/bpf.h>
61 #include <net/bpfdesc.h>
62
63 #ifdef  INET
64 #include <netinet/in.h>
65 #include <netinet/in_var.h>
66 #endif
67
68 #ifdef IPX
69 #include <netproto/ipx/ipx.h>
70 #include <netproto/ipx/ipx_if.h>
71 #endif
72
73 #ifdef INET6
74 #ifndef INET
75 #include <netinet/in.h>
76 #endif
77 #include <netinet6/in6_var.h>
78 #include <netinet/ip6.h>
79 #endif
80
81 #ifdef NS
82 #include <netns/ns.h>
83 #include <netns/ns_if.h>
84 #endif
85
86 #ifdef NETATALK
87 #include <netproto/atalk/at.h>
88 #include <netproto/atalk/at_var.h>
89 #endif
90
91 int loioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
92 static void lortrequest (int, struct rtentry *, struct rt_addrinfo *);
93
94 static void loopattach (void *);
95 #ifdef ALTQ
96 static void lo_altqstart(struct ifnet *);
97 #endif
98 PSEUDO_SET(loopattach, if_loop);
99
100 int looutput (struct ifnet *ifp,
101                 struct mbuf *m, struct sockaddr *dst, struct rtentry *rt);
102
103 #ifdef TINY_LOMTU
104 #define LOMTU   (1024+512)
105 #elif defined(LARGE_LOMTU)
106 #define LOMTU   131072
107 #else
108 #define LOMTU   16384
109 #endif
110
111 #define LO_CSUM_FEATURES        (CSUM_IP | CSUM_UDP | CSUM_TCP)
112
113 struct  ifnet loif[NLOOP];
114
115 /* ARGSUSED */
116 static void
117 loopattach(void *dummy)
118 {
119         struct ifnet *ifp;
120         int i;
121
122         for (i = 0, ifp = loif; i < NLOOP; i++, ifp++) {
123                 if_initname(ifp, "lo", i);
124                 ifp->if_mtu = LOMTU;
125                 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
126                 ifp->if_capabilities = IFCAP_HWCSUM;
127                 ifp->if_hwassist = LO_CSUM_FEATURES;
128                 ifp->if_capenable = ifp->if_capabilities;
129                 ifp->if_ioctl = loioctl;
130                 ifp->if_output = looutput;
131                 ifp->if_type = IFT_LOOP;
132                 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
133                 ifq_set_ready(&ifp->if_snd);
134 #ifdef ALTQ
135                 ifp->if_start = lo_altqstart;
136 #endif
137                 if_attach(ifp, NULL);
138                 bpfattach(ifp, DLT_NULL, sizeof(u_int));
139         }
140 }
141
142 int
143 looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
144          struct rtentry *rt)
145 {
146         M_ASSERTPKTHDR(m);
147
148         if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
149                 m_freem(m);
150                 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
151                         rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
152         }
153
154         ifp->if_opackets++;
155         ifp->if_obytes += m->m_pkthdr.len;
156 #if 1   /* XXX */
157         switch (dst->sa_family) {
158         case AF_INET:
159         case AF_INET6:
160         case AF_IPX:
161         case AF_NS:
162         case AF_APPLETALK:
163                 break;
164         default:
165                 kprintf("looutput: af=%d unexpected\n", dst->sa_family);
166                 m_freem(m);
167                 return (EAFNOSUPPORT);
168         }
169 #endif
170
171         if (ifp->if_capenable & IFCAP_RXCSUM) {
172                 int csum_flags = 0;
173
174                 if (m->m_pkthdr.csum_flags & CSUM_IP)
175                         csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
176                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
177                         csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
178
179                 m->m_pkthdr.csum_flags |= csum_flags;
180                 if (csum_flags & CSUM_DATA_VALID)
181                         m->m_pkthdr.csum_data = 0xffff;
182         }
183         return (if_simloop(ifp, m, dst->sa_family, 0));
184 }
185
186 /*
187  * if_simloop()
188  *
189  * This function is to support software emulation of hardware loopback,
190  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
191  * hear their own broadcasts, we create a copy of the packet that we
192  * would normally receive via a hardware loopback.
193  *
194  * This function expects the packet to include the media header of length hlen.
195  */
196 int
197 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
198 {
199         int isr;
200
201         KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
202         m->m_pkthdr.rcvif = ifp;
203
204         /* BPF write needs to be handled specially */
205         if (af == AF_UNSPEC) {
206                 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
207                 af = *(mtod(m, int *));
208                 m->m_len -= sizeof(int);
209                 m->m_pkthdr.len -= sizeof(int);
210                 m->m_data += sizeof(int);
211         }
212
213         if (ifp->if_bpf) {
214                 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
215                         uint32_t bpf_af = (uint32_t)af;
216                         bpf_ptap(ifp->if_bpf, m, &bpf_af, 4);
217                 }
218                 else {
219                         bpf_mtap(ifp->if_bpf, m);
220                 }
221         }
222
223         /* Strip away media header */
224         if (hlen > 0)
225                 m_adj(m, hlen);
226  
227 #ifdef ALTQ
228         /*
229          * altq for loop is just for debugging.
230          * only used when called for loop interface (not for
231          * a simplex interface).
232          */
233         if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) {
234                 struct altq_pktattr pktattr;
235                 int32_t *afp;
236                 int error;
237
238                 /*
239                  * if the queueing discipline needs packet classification,
240                  * do it before prepending link headers.
241                  */
242                 ifq_classify(&ifp->if_snd, m, af, &pktattr);
243
244                 M_PREPEND(m, sizeof(int32_t), MB_DONTWAIT);
245                 if (m == 0)
246                         return(ENOBUFS);
247                 afp = mtod(m, int32_t *);
248                 *afp = (int32_t)af;
249
250                 /*
251                  * A critical section is needed for subsystems protected by
252                  * the MP lock, and the serializer is assumed to already
253                  * be held for MPSAFE subsystems.
254                  */
255                 crit_enter();
256                 error = ifq_enqueue(&ifp->if_snd, m, &pktattr);
257                 lwkt_serialize_enter(ifp->if_serializer);
258                 ifp->if_start(ifp);
259                 lwkt_serialize_exit(ifp->if_serializer);
260                 crit_exit();
261                 return (error);
262         }
263 #endif /* ALTQ */
264
265         /* Deliver to upper layer protocol */
266         switch (af) {
267 #ifdef INET
268         case AF_INET:
269                 isr = NETISR_IP;
270                 break;
271 #endif
272 #ifdef INET6
273         case AF_INET6:
274                 m->m_flags |= M_LOOP;
275                 isr = NETISR_IPV6;
276                 break;
277 #endif
278 #ifdef IPX
279         case AF_IPX:
280                 isr = NETISR_IPX;
281                 break;
282 #endif
283 #ifdef NS
284         case AF_NS:
285                 isr = NETISR_NS;
286                 break;
287 #endif
288 #ifdef NETATALK
289         case AF_APPLETALK:
290                 isr = NETISR_ATALK2;
291                 break;
292 #endif
293         default:
294                 kprintf("if_simloop: can't handle af=%d\n", af);
295                 m_freem(m);
296                 return (EAFNOSUPPORT);
297         }
298
299         ifp->if_ipackets++;
300         ifp->if_ibytes += m->m_pkthdr.len;
301         netisr_queue(isr, m);
302         return (0);
303 }
304
305 #ifdef ALTQ
306 static void
307 lo_altqstart(struct ifnet *ifp)
308 {
309         struct mbuf *m;
310         int32_t af, *afp;
311         int isr;
312         
313         while (1) {
314                 crit_enter();
315                 m = ifq_dequeue(&ifp->if_snd, NULL);
316                 crit_exit();
317                 if (m == NULL)
318                         return;
319
320                 afp = mtod(m, int32_t *);
321                 af = *afp;
322                 m_adj(m, sizeof(int32_t));
323
324                 switch (af) {
325 #ifdef INET
326                 case AF_INET:
327                         isr = NETISR_IP;
328                         break;
329 #endif
330 #ifdef INET6
331                 case AF_INET6:
332                         m->m_flags |= M_LOOP;
333                         isr = NETISR_IPV6;
334                         break;
335 #endif
336 #ifdef IPX
337                 case AF_IPX:
338                         isr = NETISR_IPX;
339                         break;
340 #endif
341 #ifdef NS
342                 case AF_NS:
343                         isr = NETISR_NS;
344                         break;
345 #endif
346 #ifdef ISO
347                 case AF_ISO:
348                         isr = NETISR_ISO;
349                         break;
350 #endif
351 #ifdef NETATALK
352                 case AF_APPLETALK:
353                         isr = NETISR_ATALK2;
354                         break;
355 #endif
356                 default:
357                         kprintf("lo_altqstart: can't handle af%d\n", af);
358                         m_freem(m);
359                         return;
360                 }
361
362                 ifp->if_ipackets++;
363                 ifp->if_ibytes += m->m_pkthdr.len;
364                 netisr_queue(isr, m);
365         }
366 }
367 #endif /* ALTQ */
368
369 /* ARGSUSED */
370 static void
371 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
372 {
373         if (rt) {
374                 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
375                 /*
376                  * For optimal performance, the send and receive buffers
377                  * should be at least twice the MTU plus a little more for
378                  * overhead.
379                  */
380                 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
381         }
382 }
383
384 /*
385  * Process an ioctl request.
386  */
387 /* ARGSUSED */
388 int
389 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
390 {
391         struct ifaddr *ifa;
392         struct ifreq *ifr = (struct ifreq *)data;
393         int error = 0, mask;
394
395         switch (cmd) {
396         case SIOCSIFADDR:
397                 ifp->if_flags |= IFF_UP | IFF_RUNNING;
398                 ifa = (struct ifaddr *)data;
399                 ifa->ifa_rtrequest = lortrequest;
400                 /*
401                  * Everything else is done at a higher level.
402                  */
403                 break;
404
405         case SIOCADDMULTI:
406         case SIOCDELMULTI:
407                 if (ifr == 0) {
408                         error = EAFNOSUPPORT;           /* XXX */
409                         break;
410                 }
411                 switch (ifr->ifr_addr.sa_family) {
412
413 #ifdef INET
414                 case AF_INET:
415                         break;
416 #endif
417 #ifdef INET6
418                 case AF_INET6:
419                         break;
420 #endif
421
422                 default:
423                         error = EAFNOSUPPORT;
424                         break;
425                 }
426                 break;
427
428         case SIOCSIFMTU:
429                 ifp->if_mtu = ifr->ifr_mtu;
430                 break;
431
432         case SIOCSIFFLAGS:
433                 break;
434
435         case SIOCSIFCAP:
436                 mask = (ifr->ifr_reqcap ^ ifp->if_capenable) & IFCAP_HWCSUM;
437                 if (mask) {
438                         ifp->if_capenable ^= mask;
439                         if (IFCAP_TXCSUM & ifp->if_capenable)
440                                 ifp->if_hwassist = LO_CSUM_FEATURES;
441                         else
442                                 ifp->if_hwassist = 0;
443                 }
444                 break;
445
446         default:
447                 error = EINVAL;
448         }
449         return (error);
450 }