sys/vfs/msdosfs: Sync with FreeBSD (non functional diffs)
[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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)if_loop.c   8.1 (Berkeley) 6/10/93
30  * $FreeBSD: src/sys/net/if_loop.c,v 1.47.2.9 2004/02/08 08:40:24 silby Exp $
31  */
32
33 /*
34  * Loopback interface driver for protocol testing and timing.
35  */
36 #include "use_loop.h"
37
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48
49 #include <sys/mplock2.h>
50
51 #include <net/if.h>
52 #include <net/if_types.h>
53 #include <net/if_clone.h>
54 #include <net/ifq_var.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/bpf.h>
58 #include <net/bpfdesc.h>
59
60 #ifdef INET
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #endif
64
65 #ifdef INET6
66 #ifndef INET
67 #include <netinet/in.h>
68 #endif
69 #include <netinet6/in6_var.h>
70 #include <netinet/ip6.h>
71 #endif
72
73 static int      lo_clone_create(struct if_clone *, int, caddr_t);
74 static int      lo_clone_destroy(struct ifnet *);
75
76 static int      lo_output(struct ifnet *, struct mbuf *, struct sockaddr *,
77                     struct rtentry *);
78 static int      lo_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
79 static void     lo_rtrequest(int, struct rtentry *);
80 #ifdef ALTQ
81 static void     lo_altqstart(struct ifnet *, struct ifaltq_subque *);
82 #endif
83
84 #if defined(TINY_LOMTU)
85 #define LOMTU   (1024+512)
86 #elif defined(LARGE_LOMTU)
87 #define LOMTU   131072
88 #else
89 #define LOMTU   16384
90 #endif
91
92 #define LO_CSUM_FEATURES        (CSUM_IP | CSUM_UDP | CSUM_TCP)
93
94 struct ifnet    *loif;
95
96 static struct if_clone lo_cloner = IF_CLONE_INITIALIZER("lo",
97     lo_clone_create, lo_clone_destroy, NLOOP, IF_MAXUNIT);
98
99 static void
100 lo_sysinit(void *dummy __unused)
101 {
102         if_clone_attach(&lo_cloner);
103 }
104 SYSINIT(lo_sysinit, SI_SUB_PSEUDO, SI_ORDER_ANY, lo_sysinit, NULL);
105
106 static int
107 lo_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
108 {
109         struct ifnet *ifp;
110
111         ifp = kmalloc(sizeof(*ifp), M_IFNET, M_WAITOK | M_ZERO);
112         if_initname(ifp, ifc->ifc_name, unit);
113         ifp->if_mtu = LOMTU;
114         ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
115         ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_RSS;
116         ifp->if_hwassist = LO_CSUM_FEATURES;
117         ifp->if_capenable = ifp->if_capabilities;
118         ifp->if_ioctl = lo_ioctl;
119         ifp->if_output = lo_output;
120         ifp->if_type = IFT_LOOP;
121         ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
122         ifq_set_ready(&ifp->if_snd);
123 #ifdef ALTQ
124         ifp->if_start = lo_altqstart;
125 #endif
126         if_attach(ifp, NULL);
127         bpfattach(ifp, DLT_NULL, sizeof(u_int));
128
129         if (loif == NULL) {
130                 KASSERT(unit == 0, ("loif is %s", ifp->if_xname));
131                 loif = ifp;
132         }
133         return (0);
134 }
135
136 static int
137 lo_clone_destroy(struct ifnet *ifp)
138 {
139         if (loif == ifp)
140                 return (EPERM);
141
142         bpfdetach(ifp);
143         if_detach(ifp);
144         kfree(ifp, M_IFNET);
145         return (0);
146 }
147
148 static int
149 lo_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
150     struct rtentry *rt)
151 {
152         M_ASSERTPKTHDR(m);
153
154         if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
155                 m_freem(m);
156                 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
157                         rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
158         }
159
160         IFNET_STAT_INC(ifp, opackets, 1);
161         IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len);
162 #if 1   /* XXX */
163         switch (dst->sa_family) {
164         case AF_INET:
165         case AF_INET6:
166                 break;
167         default:
168                 kprintf("lo_output: af=%d unexpected\n", dst->sa_family);
169                 m_freem(m);
170                 return (EAFNOSUPPORT);
171         }
172 #endif
173
174         if (ifp->if_capenable & IFCAP_RXCSUM) {
175                 int csum_flags = 0;
176
177                 if (m->m_pkthdr.csum_flags & CSUM_IP)
178                         csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
179                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
180                         csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
181
182                 m->m_pkthdr.csum_flags |= csum_flags;
183                 if (csum_flags & CSUM_DATA_VALID)
184                         m->m_pkthdr.csum_data = 0xffff;
185         }
186         if ((ifp->if_capenable & IFCAP_RSS) == 0)
187                 m->m_flags &= ~M_HASH;
188         return (if_simloop(ifp, m, dst->sa_family, 0));
189 }
190
191 /*
192  * if_simloop()
193  *
194  * This function is to support software emulation of hardware loopback,
195  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
196  * hear their own broadcasts, we create a copy of the packet that we
197  * would normally receive via a hardware loopback.
198  *
199  * This function expects the packet to include the media header of length hlen.
200  */
201 int
202 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
203 {
204         int isr;
205
206         KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
207         m->m_pkthdr.rcvif = ifp;
208
209         /* BPF write needs to be handled specially */
210         if (af == AF_UNSPEC) {
211                 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
212                 af = *(mtod(m, int *));
213                 m->m_len -= sizeof(int);
214                 m->m_pkthdr.len -= sizeof(int);
215                 m->m_data += sizeof(int);
216         }
217
218         if (ifp->if_bpf) {
219                 bpf_gettoken();
220
221                 /* Re-check */
222                 if (ifp->if_bpf == NULL)
223                         goto rel;
224
225                 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
226                         uint32_t bpf_af = (uint32_t)af;
227                         bpf_ptap(ifp->if_bpf, m, &bpf_af, 4);
228                 } else {
229                         bpf_mtap(ifp->if_bpf, m);
230                 }
231 rel:
232                 bpf_reltoken();
233         }
234
235         /* Strip away media header */
236         if (hlen > 0)
237                 m_adj(m, hlen);
238  
239 #ifdef ALTQ
240         /*
241          * altq for loop is just for debugging.
242          * only used when called for loop interface (not for
243          * a simplex interface).
244          */
245         if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) {
246                 struct altq_pktattr pktattr;
247                 int32_t *afp;
248
249                 /*
250                  * if the queueing discipline needs packet classification,
251                  * do it before prepending link headers.
252                  */
253                 ifq_classify(&ifp->if_snd, m, af, &pktattr);
254
255                 M_PREPEND(m, sizeof(int32_t), M_NOWAIT);
256                 if (m == NULL)
257                         return(ENOBUFS);
258                 afp = mtod(m, int32_t *);
259                 *afp = (int32_t)af;
260
261                 return ifq_dispatch(ifp, m, &pktattr);
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         default:
279                 kprintf("if_simloop: can't handle af=%d\n", af);
280                 m_freem(m);
281                 return (EAFNOSUPPORT);
282         }
283
284         IFNET_STAT_INC(ifp, ipackets, 1);
285         IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
286         netisr_queue(isr, m);
287         return (0);
288 }
289
290 #ifdef ALTQ
291 static void
292 lo_altqstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
293 {
294         struct mbuf *m;
295         int32_t af, *afp;
296         int isr;
297         
298         while (1) {
299                 crit_enter();
300                 m = ifsq_dequeue(ifsq);
301                 crit_exit();
302                 if (m == NULL)
303                         return;
304
305                 afp = mtod(m, int32_t *);
306                 af = *afp;
307                 m_adj(m, sizeof(int32_t));
308
309                 switch (af) {
310 #ifdef INET
311                 case AF_INET:
312                         isr = NETISR_IP;
313                         break;
314 #endif
315 #ifdef INET6
316                 case AF_INET6:
317                         m->m_flags |= M_LOOP;
318                         isr = NETISR_IPV6;
319                         break;
320 #endif
321                 default:
322                         kprintf("lo_altqstart: can't handle af%d\n", af);
323                         m_freem(m);
324                         return;
325                 }
326
327                 IFNET_STAT_INC(ifp, ipackets, 1);
328                 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
329                 netisr_queue(isr, m);
330         }
331 }
332 #endif /* ALTQ */
333
334 /* ARGSUSED */
335 static void
336 lo_rtrequest(int cmd, struct rtentry *rt)
337 {
338         if (rt) {
339                 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
340                 /*
341                  * For optimal performance, the send and receive buffers
342                  * should be at least twice the MTU plus a little more for
343                  * overhead.
344                  */
345                 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
346         }
347 }
348
349 /*
350  * Process an ioctl request.
351  */
352 /* ARGSUSED */
353 static int
354 lo_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
355 {
356         struct ifaddr *ifa;
357         struct ifreq *ifr = (struct ifreq *)data;
358         int error = 0, mask;
359
360         switch (cmd) {
361         case SIOCSIFADDR:
362                 ifp->if_flags |= IFF_UP | IFF_RUNNING;
363                 ifa = (struct ifaddr *)data;
364                 ifa->ifa_rtrequest = lo_rtrequest;
365                 /*
366                  * Everything else is done at a higher level.
367                  */
368                 break;
369
370         case SIOCADDMULTI:
371         case SIOCDELMULTI:
372                 if (ifr == NULL) {
373                         error = EAFNOSUPPORT;           /* XXX */
374                         break;
375                 }
376                 switch (ifr->ifr_addr.sa_family) {
377
378 #ifdef INET
379                 case AF_INET:
380                         break;
381 #endif
382 #ifdef INET6
383                 case AF_INET6:
384                         break;
385 #endif
386
387                 default:
388                         error = EAFNOSUPPORT;
389                         break;
390                 }
391                 break;
392
393         case SIOCSIFMTU:
394                 ifp->if_mtu = ifr->ifr_mtu;
395                 break;
396
397         case SIOCSIFFLAGS:
398                 break;
399
400         case SIOCSIFCAP:
401                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
402                 if (mask & IFCAP_HWCSUM) {
403                         ifp->if_capenable ^= (mask & IFCAP_HWCSUM);
404                         if (IFCAP_TXCSUM & ifp->if_capenable)
405                                 ifp->if_hwassist = LO_CSUM_FEATURES;
406                         else
407                                 ifp->if_hwassist = 0;
408                 }
409                 if (mask & IFCAP_RSS)
410                         ifp->if_capenable ^= IFCAP_RSS;
411                 break;
412
413         default:
414                 error = EINVAL;
415         }
416         return (error);
417 }