ipfw3: Fix several comments and error messages
[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/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49
50 #include <net/if.h>
51 #include <net/if_types.h>
52 #include <net/if_clone.h>
53 #include <net/ifq_var.h>
54 #include <net/netisr.h>
55 #include <net/route.h>
56 #include <net/bpf.h>
57 #include <net/bpfdesc.h>
58
59 #ifdef INET
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #endif
63
64 #ifdef INET6
65 #ifndef INET
66 #include <netinet/in.h>
67 #endif
68 #include <netinet6/in6_var.h>
69 #include <netinet/ip6.h>
70 #endif
71
72 static int      lo_clone_create(struct if_clone *, int, caddr_t);
73 static int      lo_clone_destroy(struct ifnet *);
74
75 static int      lo_output(struct ifnet *, struct mbuf *, struct sockaddr *,
76                     struct rtentry *);
77 static int      lo_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
78 static void     lo_rtrequest(int, struct rtentry *);
79 #ifdef ALTQ
80 static void     lo_altqstart(struct ifnet *, struct ifaltq_subque *);
81 #endif
82
83 #if defined(TINY_LOMTU)
84 #define LOMTU   (1024+512)
85 #elif defined(LARGE_LOMTU)
86 #define LOMTU   131072
87 #else
88 #define LOMTU   16384
89 #endif
90
91 #define LO_CSUM_FEATURES        (CSUM_IP | CSUM_UDP | CSUM_TCP)
92
93 struct ifnet    *loif;
94
95 static struct if_clone lo_cloner = IF_CLONE_INITIALIZER("lo",
96     lo_clone_create, lo_clone_destroy, NLOOP, IF_MAXUNIT);
97
98 static void
99 lo_sysinit(void *dummy __unused)
100 {
101         if_clone_attach(&lo_cloner);
102 }
103 SYSINIT(lo_sysinit, SI_SUB_PSEUDO, SI_ORDER_ANY, lo_sysinit, NULL);
104
105 static int
106 lo_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
107 {
108         struct ifnet *ifp;
109
110         ifp = kmalloc(sizeof(*ifp), M_IFNET, M_WAITOK | M_ZERO);
111         if_initname(ifp, ifc->ifc_name, unit);
112         ifp->if_mtu = LOMTU;
113         ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
114         ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_RSS;
115         ifp->if_hwassist = LO_CSUM_FEATURES;
116         ifp->if_capenable = ifp->if_capabilities;
117         ifp->if_ioctl = lo_ioctl;
118         ifp->if_output = lo_output;
119         ifp->if_type = IFT_LOOP;
120         ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
121         ifq_set_ready(&ifp->if_snd);
122 #ifdef ALTQ
123         ifp->if_start = lo_altqstart;
124 #endif
125         if_attach(ifp, NULL);
126         bpfattach(ifp, DLT_NULL, sizeof(u_int));
127
128         if (loif == NULL) {
129                 KASSERT(unit == 0, ("loif is %s", ifp->if_xname));
130                 loif = ifp;
131         }
132         return (0);
133 }
134
135 static int
136 lo_clone_destroy(struct ifnet *ifp)
137 {
138         if (loif == ifp)
139                 return (EPERM);
140
141         bpfdetach(ifp);
142         if_detach(ifp);
143         kfree(ifp, M_IFNET);
144         return (0);
145 }
146
147 static int
148 lo_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
149     struct rtentry *rt)
150 {
151         M_ASSERTPKTHDR(m);
152
153         if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
154                 m_freem(m);
155                 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
156                         rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
157         }
158
159         IFNET_STAT_INC(ifp, opackets, 1);
160         IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len);
161 #if 1   /* XXX */
162         switch (dst->sa_family) {
163         case AF_INET:
164         case AF_INET6:
165                 break;
166         default:
167                 kprintf("lo_output: af=%d unexpected\n", dst->sa_family);
168                 m_freem(m);
169                 return (EAFNOSUPPORT);
170         }
171 #endif
172
173         if (ifp->if_capenable & IFCAP_RXCSUM) {
174                 int csum_flags = 0;
175
176                 if (m->m_pkthdr.csum_flags & CSUM_IP)
177                         csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
178                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
179                         csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
180
181                 m->m_pkthdr.csum_flags |= csum_flags;
182                 if (csum_flags & CSUM_DATA_VALID)
183                         m->m_pkthdr.csum_data = 0xffff;
184         }
185         if ((ifp->if_capenable & IFCAP_RSS) == 0)
186                 m->m_flags &= ~M_HASH;
187         return (if_simloop(ifp, m, dst->sa_family, 0));
188 }
189
190 /*
191  * if_simloop()
192  *
193  * This function is to support software emulation of hardware loopback,
194  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
195  * hear their own broadcasts, we create a copy of the packet that we
196  * would normally receive via a hardware loopback.
197  *
198  * This function expects the packet to include the media header of length hlen.
199  */
200 int
201 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
202 {
203         int isr;
204
205         KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
206         m->m_pkthdr.rcvif = ifp;
207
208         /* BPF write needs to be handled specially */
209         if (af == AF_UNSPEC) {
210                 KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
211                 af = *(mtod(m, int *));
212                 m->m_len -= sizeof(int);
213                 m->m_pkthdr.len -= sizeof(int);
214                 m->m_data += sizeof(int);
215         }
216
217         if (ifp->if_bpf) {
218                 bpf_gettoken();
219
220                 /* Re-check */
221                 if (ifp->if_bpf == NULL)
222                         goto rel;
223
224                 if (ifp->if_bpf->bif_dlt == DLT_NULL) {
225                         uint32_t bpf_af = (uint32_t)af;
226                         bpf_ptap(ifp->if_bpf, m, &bpf_af, 4);
227                 } else {
228                         bpf_mtap(ifp->if_bpf, m);
229                 }
230 rel:
231                 bpf_reltoken();
232         }
233
234         /* Strip away media header */
235         if (hlen > 0)
236                 m_adj(m, hlen);
237  
238 #ifdef ALTQ
239         /*
240          * altq for loop is just for debugging.
241          * only used when called for loop interface (not for
242          * a simplex interface).
243          */
244         if (ifq_is_enabled(&ifp->if_snd) && ifp->if_start == lo_altqstart) {
245                 struct altq_pktattr pktattr;
246                 int32_t *afp;
247
248                 /*
249                  * if the queueing discipline needs packet classification,
250                  * do it before prepending link headers.
251                  */
252                 ifq_classify(&ifp->if_snd, m, af, &pktattr);
253
254                 M_PREPEND(m, sizeof(int32_t), M_NOWAIT);
255                 if (m == NULL)
256                         return(ENOBUFS);
257                 afp = mtod(m, int32_t *);
258                 *afp = (int32_t)af;
259
260                 return ifq_dispatch(ifp, m, &pktattr);
261         }
262 #endif /* ALTQ */
263
264         /* Deliver to upper layer protocol */
265         switch (af) {
266 #ifdef INET
267         case AF_INET:
268                 isr = NETISR_IP;
269                 break;
270 #endif
271 #ifdef INET6
272         case AF_INET6:
273                 m->m_flags |= M_LOOP;
274                 isr = NETISR_IPV6;
275                 break;
276 #endif
277         default:
278                 kprintf("if_simloop: can't handle af=%d\n", af);
279                 m_freem(m);
280                 return (EAFNOSUPPORT);
281         }
282
283         IFNET_STAT_INC(ifp, ipackets, 1);
284         IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
285         netisr_queue(isr, m);
286         return (0);
287 }
288
289 #ifdef ALTQ
290 static void
291 lo_altqstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
292 {
293         struct mbuf *m;
294         int32_t af, *afp;
295         int isr;
296         
297         while (1) {
298                 crit_enter();
299                 m = ifsq_dequeue(ifsq);
300                 crit_exit();
301                 if (m == NULL)
302                         return;
303
304                 afp = mtod(m, int32_t *);
305                 af = *afp;
306                 m_adj(m, sizeof(int32_t));
307
308                 switch (af) {
309 #ifdef INET
310                 case AF_INET:
311                         isr = NETISR_IP;
312                         break;
313 #endif
314 #ifdef INET6
315                 case AF_INET6:
316                         m->m_flags |= M_LOOP;
317                         isr = NETISR_IPV6;
318                         break;
319 #endif
320                 default:
321                         kprintf("lo_altqstart: can't handle af%d\n", af);
322                         m_freem(m);
323                         return;
324                 }
325
326                 IFNET_STAT_INC(ifp, ipackets, 1);
327                 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
328                 netisr_queue(isr, m);
329         }
330 }
331 #endif /* ALTQ */
332
333 /* ARGSUSED */
334 static void
335 lo_rtrequest(int cmd, struct rtentry *rt)
336 {
337         if (rt) {
338                 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
339                 /*
340                  * For optimal performance, the send and receive buffers
341                  * should be at least twice the MTU plus a little more for
342                  * overhead.
343                  */
344                 rt->rt_rmx.rmx_recvpipe = rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
345         }
346 }
347
348 /*
349  * Process an ioctl request.
350  */
351 /* ARGSUSED */
352 static int
353 lo_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
354 {
355         struct ifaddr *ifa;
356         struct ifreq *ifr = (struct ifreq *)data;
357         int error = 0, mask;
358
359         switch (cmd) {
360         case SIOCSIFADDR:
361                 ifp->if_flags |= IFF_UP | IFF_RUNNING;
362                 ifa = (struct ifaddr *)data;
363                 ifa->ifa_rtrequest = lo_rtrequest;
364                 /*
365                  * Everything else is done at a higher level.
366                  */
367                 break;
368
369         case SIOCADDMULTI:
370         case SIOCDELMULTI:
371                 if (ifr == NULL) {
372                         error = EAFNOSUPPORT;           /* XXX */
373                         break;
374                 }
375                 switch (ifr->ifr_addr.sa_family) {
376
377 #ifdef INET
378                 case AF_INET:
379                         break;
380 #endif
381 #ifdef INET6
382                 case AF_INET6:
383                         break;
384 #endif
385
386                 default:
387                         error = EAFNOSUPPORT;
388                         break;
389                 }
390                 break;
391
392         case SIOCSIFMTU:
393                 ifp->if_mtu = ifr->ifr_mtu;
394                 break;
395
396         case SIOCSIFFLAGS:
397                 break;
398
399         case SIOCSIFCAP:
400                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
401                 if (mask & IFCAP_HWCSUM) {
402                         ifp->if_capenable ^= (mask & IFCAP_HWCSUM);
403                         if (IFCAP_TXCSUM & ifp->if_capenable)
404                                 ifp->if_hwassist = LO_CSUM_FEATURES;
405                         else
406                                 ifp->if_hwassist = 0;
407                 }
408                 if (mask & IFCAP_RSS)
409                         ifp->if_capenable ^= IFCAP_RSS;
410                 break;
411
412         default:
413                 error = EINVAL;
414         }
415         return (error);
416 }