- Staticize lo{output,ioctl}
[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.25 2008/09/06 06:09:42 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 static void     loopattach(void *);
92 static int      looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
93                          struct rtentry *);
94 static int      loioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
95 static void     lortrequest(int, struct rtentry *, struct rt_addrinfo *);
96 #ifdef ALTQ
97 static void     lo_altqstart(struct ifnet *);
98 #endif
99 PSEUDO_SET(loopattach, if_loop);
100
101 #ifdef TINY_LOMTU
102 #define LOMTU   (1024+512)
103 #elif defined(LARGE_LOMTU)
104 #define LOMTU   131072
105 #else
106 #define LOMTU   16384
107 #endif
108
109 #define LO_CSUM_FEATURES        (CSUM_IP | CSUM_UDP | CSUM_TCP)
110
111 struct  ifnet loif[NLOOP];
112
113 /* ARGSUSED */
114 static void
115 loopattach(void *dummy)
116 {
117         struct ifnet *ifp;
118         int i;
119
120         for (i = 0, ifp = loif; i < NLOOP; i++, ifp++) {
121                 if_initname(ifp, "lo", i);
122                 ifp->if_mtu = LOMTU;
123                 ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
124                 ifp->if_capabilities = IFCAP_HWCSUM;
125                 ifp->if_hwassist = LO_CSUM_FEATURES;
126                 ifp->if_capenable = ifp->if_capabilities;
127                 ifp->if_ioctl = loioctl;
128                 ifp->if_output = looutput;
129                 ifp->if_type = IFT_LOOP;
130                 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
131                 ifq_set_ready(&ifp->if_snd);
132 #ifdef ALTQ
133                 ifp->if_start = lo_altqstart;
134 #endif
135                 if_attach(ifp, NULL);
136                 bpfattach(ifp, DLT_NULL, sizeof(u_int));
137         }
138 }
139
140 static int
141 looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
142          struct rtentry *rt)
143 {
144         M_ASSERTPKTHDR(m);
145
146         if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
147                 m_freem(m);
148                 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
149                         rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
150         }
151
152         ifp->if_opackets++;
153         ifp->if_obytes += m->m_pkthdr.len;
154 #if 1   /* XXX */
155         switch (dst->sa_family) {
156         case AF_INET:
157         case AF_INET6:
158         case AF_IPX:
159         case AF_NS:
160         case AF_APPLETALK:
161                 break;
162         default:
163                 kprintf("looutput: af=%d unexpected\n", dst->sa_family);
164                 m_freem(m);
165                 return (EAFNOSUPPORT);
166         }
167 #endif
168
169         if (ifp->if_capenable & IFCAP_RXCSUM) {
170                 int csum_flags = 0;
171
172                 if (m->m_pkthdr.csum_flags & CSUM_IP)
173                         csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
174                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
175                         csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
176
177                 m->m_pkthdr.csum_flags |= csum_flags;
178                 if (csum_flags & CSUM_DATA_VALID)
179                         m->m_pkthdr.csum_data = 0xffff;
180         }
181         return (if_simloop(ifp, m, dst->sa_family, 0));
182 }
183
184 /*
185  * if_simloop()
186  *
187  * This function is to support software emulation of hardware loopback,
188  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
189  * hear their own broadcasts, we create a copy of the packet that we
190  * would normally receive via a hardware loopback.
191  *
192  * This function expects the packet to include the media header of length hlen.
193  */
194 int
195 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
196 {
197         int isr;
198
199         KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
200         m->m_pkthdr.rcvif = ifp;
201
202         /* BPF write needs to be handled specially */
203         if (af == AF_UNSPEC) {
204                 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
205                 af = *(mtod(m, int *));
206                 m->m_len -= sizeof(int);
207                 m->m_pkthdr.len -= sizeof(int);
208                 m->m_data += sizeof(int);
209         }
210
211         if (ifp->if_bpf) {
212                 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
213                         uint32_t bpf_af = (uint32_t)af;
214                         bpf_ptap(ifp->if_bpf, m, &bpf_af, 4);
215                 }
216                 else {
217                         bpf_mtap(ifp->if_bpf, m);
218                 }
219         }
220
221         /* Strip away media header */
222         if (hlen > 0)
223                 m_adj(m, hlen);
224  
225 #ifdef ALTQ
226         /*
227          * altq for loop is just for debugging.
228          * only used when called for loop interface (not for
229          * a simplex interface).
230          */
231         if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) {
232                 struct altq_pktattr pktattr;
233                 int32_t *afp;
234                 int error;
235
236                 /*
237                  * if the queueing discipline needs packet classification,
238                  * do it before prepending link headers.
239                  */
240                 ifq_classify(&ifp->if_snd, m, af, &pktattr);
241
242                 M_PREPEND(m, sizeof(int32_t), MB_DONTWAIT);
243                 if (m == 0)
244                         return(ENOBUFS);
245                 afp = mtod(m, int32_t *);
246                 *afp = (int32_t)af;
247
248                 /*
249                  * A critical section is needed for subsystems protected by
250                  * the MP lock, and the serializer is assumed to already
251                  * be held for MPSAFE subsystems.
252                  */
253                 crit_enter();
254                 error = ifq_enqueue(&ifp->if_snd, m, &pktattr);
255                 lwkt_serialize_enter(ifp->if_serializer);
256                 ifp->if_start(ifp);
257                 lwkt_serialize_exit(ifp->if_serializer);
258                 crit_exit();
259                 return (error);
260         }
261 #endif /* ALTQ */
262
263         /* Deliver to upper layer protocol */
264         switch (af) {
265 #ifdef INET
266         case AF_INET:
267                 isr = NETISR_IP;
268                 break;
269 #endif
270 #ifdef INET6
271         case AF_INET6:
272                 m->m_flags |= M_LOOP;
273                 isr = NETISR_IPV6;
274                 break;
275 #endif
276 #ifdef IPX
277         case AF_IPX:
278                 isr = NETISR_IPX;
279                 break;
280 #endif
281 #ifdef NS
282         case AF_NS:
283                 isr = NETISR_NS;
284                 break;
285 #endif
286 #ifdef NETATALK
287         case AF_APPLETALK:
288                 isr = NETISR_ATALK2;
289                 break;
290 #endif
291         default:
292                 kprintf("if_simloop: can't handle af=%d\n", af);
293                 m_freem(m);
294                 return (EAFNOSUPPORT);
295         }
296
297         ifp->if_ipackets++;
298         ifp->if_ibytes += m->m_pkthdr.len;
299         netisr_queue(isr, m);
300         return (0);
301 }
302
303 #ifdef ALTQ
304 static void
305 lo_altqstart(struct ifnet *ifp)
306 {
307         struct mbuf *m;
308         int32_t af, *afp;
309         int isr;
310         
311         while (1) {
312                 crit_enter();
313                 m = ifq_dequeue(&ifp->if_snd, NULL);
314                 crit_exit();
315                 if (m == NULL)
316                         return;
317
318                 afp = mtod(m, int32_t *);
319                 af = *afp;
320                 m_adj(m, sizeof(int32_t));
321
322                 switch (af) {
323 #ifdef INET
324                 case AF_INET:
325                         isr = NETISR_IP;
326                         break;
327 #endif
328 #ifdef INET6
329                 case AF_INET6:
330                         m->m_flags |= M_LOOP;
331                         isr = NETISR_IPV6;
332                         break;
333 #endif
334 #ifdef IPX
335                 case AF_IPX:
336                         isr = NETISR_IPX;
337                         break;
338 #endif
339 #ifdef NS
340                 case AF_NS:
341                         isr = NETISR_NS;
342                         break;
343 #endif
344 #ifdef ISO
345                 case AF_ISO:
346                         isr = NETISR_ISO;
347                         break;
348 #endif
349 #ifdef NETATALK
350                 case AF_APPLETALK:
351                         isr = NETISR_ATALK2;
352                         break;
353 #endif
354                 default:
355                         kprintf("lo_altqstart: can't handle af%d\n", af);
356                         m_freem(m);
357                         return;
358                 }
359
360                 ifp->if_ipackets++;
361                 ifp->if_ibytes += m->m_pkthdr.len;
362                 netisr_queue(isr, m);
363         }
364 }
365 #endif /* ALTQ */
366
367 /* ARGSUSED */
368 static void
369 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
370 {
371         if (rt) {
372                 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
373                 /*
374                  * For optimal performance, the send and receive buffers
375                  * should be at least twice the MTU plus a little more for
376                  * overhead.
377                  */
378                 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
379         }
380 }
381
382 /*
383  * Process an ioctl request.
384  */
385 /* ARGSUSED */
386 static int
387 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
388 {
389         struct ifaddr *ifa;
390         struct ifreq *ifr = (struct ifreq *)data;
391         int error = 0, mask;
392
393         switch (cmd) {
394         case SIOCSIFADDR:
395                 ifp->if_flags |= IFF_UP | IFF_RUNNING;
396                 ifa = (struct ifaddr *)data;
397                 ifa->ifa_rtrequest = lortrequest;
398                 /*
399                  * Everything else is done at a higher level.
400                  */
401                 break;
402
403         case SIOCADDMULTI:
404         case SIOCDELMULTI:
405                 if (ifr == 0) {
406                         error = EAFNOSUPPORT;           /* XXX */
407                         break;
408                 }
409                 switch (ifr->ifr_addr.sa_family) {
410
411 #ifdef INET
412                 case AF_INET:
413                         break;
414 #endif
415 #ifdef INET6
416                 case AF_INET6:
417                         break;
418 #endif
419
420                 default:
421                         error = EAFNOSUPPORT;
422                         break;
423                 }
424                 break;
425
426         case SIOCSIFMTU:
427                 ifp->if_mtu = ifr->ifr_mtu;
428                 break;
429
430         case SIOCSIFFLAGS:
431                 break;
432
433         case SIOCSIFCAP:
434                 mask = (ifr->ifr_reqcap ^ ifp->if_capenable) & IFCAP_HWCSUM;
435                 if (mask) {
436                         ifp->if_capenable ^= mask;
437                         if (IFCAP_TXCSUM & ifp->if_capenable)
438                                 ifp->if_hwassist = LO_CSUM_FEATURES;
439                         else
440                                 ifp->if_hwassist = 0;
441                 }
442                 break;
443
444         default:
445                 error = EINVAL;
446         }
447         return (error);
448 }