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