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