source: rtems/cpukit/libnetworking/net/if_loop.c @ 787f51f

5
Last change on this file since 787f51f was 787f51f, checked in by Sebastian Huber <sebastian.huber@…>, on 06/06/17 at 09:08:16

Do not include <sys/ioctl.h> in kernel-space

Update #2833.

  • Property mode set to 100644
File size: 6.4 KB
Line 
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 * 4. 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.2 (Berkeley) 1/9/95
30 * $FreeBSD: src/sys/net/if_loop.c,v 1.104 2005/02/24 01:34:01 sam Exp $
31 */
32
33
34#ifdef HAVE_CONFIG_H
35#include "config.h"
36#endif
37
38/*
39 * Loopback interface driver for protocol testing and timing.
40 */
41#include "loop.h"
42#if NLOOP > 0
43
44#include "opt_atalk.h"
45#include "opt_inet.h"
46#include "opt_inet6.h"
47#include "opt_ipx.h"
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/kernel.h>
52#include <sys/mbuf.h>
53#include <sys/socket.h>
54#include <errno.h>
55#include <sys/sockio.h>
56#include <sys/time.h>
57
58#include <net/if.h>
59#include <net/if_types.h>
60#include <net/netisr.h>
61#include <net/route.h>
62#include <net/bpf.h>
63
64#ifdef  INET
65#include <netinet/in.h>
66#include <netinet/in_systm.h>
67#include <netinet/in_var.h>
68#include <netinet/ip.h>
69#endif
70
71#ifdef IPX
72#include <netipx/ipx.h>
73#include <netipx/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 <netatalk/at.h>
86#include <netatalk/at_var.h>
87#endif
88
89static int loioctl(struct ifnet *, ioctl_command_t, caddr_t);
90static void lortrequest(int, struct rtentry *, struct sockaddr *);
91
92#ifdef TINY_LOMTU
93#define LOMTU   (1024+512)
94#elif defined(LARGE_LOMTU)
95#define LOMTU  131072
96#else
97#define LOMTU   16384
98#endif
99
100struct  ifnet loif[NLOOP];
101
102void
103rtems_bsdnet_initialize_loop(void)
104{
105        register struct ifnet *ifp;
106        register int i = 0;
107
108        for (ifp = loif; i < NLOOP; ifp++) {
109            ifp->if_name = "lo";
110            ifp->if_next = NULL;
111            ifp->if_unit = i++;
112            ifp->if_mtu = LOMTU;
113            ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
114            ifp->if_ioctl = loioctl;
115            ifp->if_output = looutput;
116            ifp->if_type = IFT_LOOP;
117            ifp->if_hdrlen = 0;
118            ifp->if_addrlen = 0;
119            ifp->if_snd.ifq_maxlen = ifqmaxlen;
120            if_attach(ifp);
121#if NBPFILTER > 0
122            bpfattach(ifp, DLT_NULL, sizeof(u_int));
123#endif
124        }
125}
126
127int
128looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
129    struct rtentry *rt)
130{
131        int s, isr;
132        register struct ifqueue *ifq = 0;
133
134        if ((m->m_flags & M_PKTHDR) == 0)
135                panic("looutput no HDR");
136#if NBPFILTER > 0
137        /* BPF write needs to be handled specially */
138        if (dst->sa_family == AF_UNSPEC) {
139                dst->sa_family = *(mtod(m, int *));
140                m->m_len -= sizeof(int);
141                m->m_pkthdr.len -= sizeof(int);
142                m->m_data += sizeof(int);
143        }
144
145        /* Let BPF see incoming packet */
146        if (ifp->if_bpf) {
147                /*
148                 * We need to prepend the address family as
149                 * a four byte field.  Cons up a dummy header
150                 * to pacify bpf.  This is safe because bpf
151                 * will only read from the mbuf (i.e., it won't
152                 * try to free it or keep a pointer a to it).
153                 */
154                struct mbuf m0;
155                u_int af = dst->sa_family;
156
157                m0.m_next = m;
158                m0.m_len = 4;
159                m0.m_data = (char *)&af;
160
161                bpf_mtap(ifp, &m0);
162        }
163#endif
164        m->m_pkthdr.rcvif = ifp;
165
166        if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
167                m_freem(m);
168                return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
169                        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
170        }
171        ifp->if_opackets++;
172        ifp->if_obytes += m->m_pkthdr.len;
173        switch (dst->sa_family) {
174
175#ifdef INET
176        case AF_INET:
177                ifq = &ipintrq;
178                isr = NETISR_IP;
179                break;
180#endif
181#ifdef IPX
182        case AF_IPX:
183                ifq = &ipxintrq;
184                isr = NETISR_IPX;
185                break;
186#endif
187#ifdef NETATALK
188        case AF_APPLETALK:
189                ifq = &atintrq2;
190                isr = NETISR_ATALK;
191                break;
192#endif /* NETATALK */
193        default:
194                printf("lo%d: can't handle af%d\n", ifp->if_unit,
195                        dst->sa_family);
196                m_freem(m);
197                return (EAFNOSUPPORT);
198        }
199        s = splimp();
200        if (IF_QFULL(ifq)) {
201                IF_DROP(ifq);
202                m_freem(m);
203                splx(s);
204                return (ENOBUFS);
205        }
206        IF_ENQUEUE(ifq, m);
207        schednetisr(isr);
208        ifp->if_ipackets++;
209        ifp->if_ibytes += m->m_pkthdr.len;
210        splx(s);
211        return (0);
212}
213
214static void
215lortrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
216{
217        if (rt) {
218                rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
219                /*
220                 * For optimal performance, the send and receive buffers
221                 * should be at least twice the MTU plus a little more for
222                 * overhead.
223                 */
224                rt->rt_rmx.rmx_recvpipe =
225                        rt->rt_rmx.rmx_sendpipe = 3L * (long)LOMTU;
226        }
227}
228
229/*
230 * Process an ioctl request.
231 */
232static int
233loioctl(struct ifnet *ifp, ioctl_command_t cmd, caddr_t data)
234{
235        register struct ifaddr *ifa;
236        register struct ifreq *ifr = (struct ifreq *)data;
237        register int error = 0;
238
239        switch (cmd) {
240
241        case SIOCSIFADDR:
242                ifp->if_flags |= IFF_UP | IFF_RUNNING;
243                ifa = (struct ifaddr *)data;
244                ifa->ifa_rtrequest = lortrequest;
245                /*
246                 * Everything else is done at a higher level.
247                 */
248                break;
249
250        case SIOCADDMULTI:
251        case SIOCDELMULTI:
252                if (ifr == 0) {
253                        error = EAFNOSUPPORT;           /* XXX */
254                        break;
255                }
256                switch (ifr->ifr_addr.sa_family) {
257
258#ifdef INET
259                case AF_INET:
260                        break;
261#endif
262#ifdef INET6
263                case AF_INET6:
264                        break;
265#endif
266
267                default:
268                        error = EAFNOSUPPORT;
269                        break;
270                }
271                break;
272
273        case SIOCSIFMTU:
274                ifp->if_mtu = ifr->ifr_mtu;
275                break;
276
277        case SIOCSIFFLAGS:
278                break;
279
280        default:
281                error = EINVAL;
282        }
283        return (error);
284}
285#endif /* NLOOP > 0 */
Note: See TracBrowser for help on using the repository browser.