source: rtems/cpukit/libnetworking/net/if_loop.c @ cb68253

5
Last change on this file since cb68253 was cb68253, checked in by Sebastian Huber <sebastian.huber@…>, on 09/07/18 at 04:19:02

network: Use kernel/user space header files

Add and use <machine/rtems-bsd-kernel-space.h> and
<machine/rtems-bsd-user-space.h> similar to the libbsd to avoid command
line defines and defines scattered throught the code base.

Simplify cpukit/libnetworking/Makefile.am.

Update #3375.

  • Property mode set to 100644
File size: 6.5 KB
Line 
1#include <machine/rtems-bsd-kernel-space.h>
2
3/*
4 * Copyright (c) 1982, 1986, 1993
5 *      The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *      @(#)if_loop.c   8.2 (Berkeley) 1/9/95
32 * $FreeBSD: src/sys/net/if_loop.c,v 1.104 2005/02/24 01:34:01 sam Exp $
33 */
34
35
36#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
39
40/*
41 * Loopback interface driver for protocol testing and timing.
42 */
43#include "loop.h"
44#if NLOOP > 0
45
46#include "opt_atalk.h"
47#include "opt_inet.h"
48#include "opt_inet6.h"
49#include "opt_ipx.h"
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/kernel.h>
54#include <sys/mbuf.h>
55#include <sys/socket.h>
56#include <errno.h>
57#include <sys/sockio.h>
58#include <sys/time.h>
59
60#include <net/if.h>
61#include <net/if_types.h>
62#include <net/netisr.h>
63#include <net/route.h>
64#include <net/bpf.h>
65
66#ifdef  INET
67#include <netinet/in.h>
68#include <netinet/in_systm.h>
69#include <netinet/in_var.h>
70#include <netinet/ip.h>
71#endif
72
73#ifdef IPX
74#include <netipx/ipx.h>
75#include <netipx/ipx_if.h>
76#endif
77
78#ifdef INET6
79#ifndef INET
80#include <netinet/in.h>
81#endif
82#include <netinet6/in6_var.h>
83#include <netinet/ip6.h>
84#endif
85
86#ifdef NETATALK
87#include <netatalk/at.h>
88#include <netatalk/at_var.h>
89#endif
90
91static int loioctl(struct ifnet *, ioctl_command_t, caddr_t);
92static void lortrequest(int, struct rtentry *, struct sockaddr *);
93
94#ifdef TINY_LOMTU
95#define LOMTU   (1024+512)
96#elif defined(LARGE_LOMTU)
97#define LOMTU  131072
98#else
99#define LOMTU   16384
100#endif
101
102struct  ifnet loif[NLOOP];
103
104void
105rtems_bsdnet_initialize_loop(void)
106{
107        register struct ifnet *ifp;
108        register int i = 0;
109
110        for (ifp = loif; i < NLOOP; ifp++) {
111            ifp->if_name = "lo";
112            ifp->if_next = NULL;
113            ifp->if_unit = i++;
114            ifp->if_mtu = LOMTU;
115            ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
116            ifp->if_ioctl = loioctl;
117            ifp->if_output = looutput;
118            ifp->if_type = IFT_LOOP;
119            ifp->if_hdrlen = 0;
120            ifp->if_addrlen = 0;
121            ifp->if_snd.ifq_maxlen = ifqmaxlen;
122            if_attach(ifp);
123#if NBPFILTER > 0
124            bpfattach(ifp, DLT_NULL, sizeof(u_int));
125#endif
126        }
127}
128
129int
130looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
131    struct rtentry *rt)
132{
133        int s, isr;
134        register struct ifqueue *ifq = 0;
135
136        if ((m->m_flags & M_PKTHDR) == 0)
137                panic("looutput no HDR");
138#if NBPFILTER > 0
139        /* BPF write needs to be handled specially */
140        if (dst->sa_family == AF_UNSPEC) {
141                dst->sa_family = *(mtod(m, int *));
142                m->m_len -= sizeof(int);
143                m->m_pkthdr.len -= sizeof(int);
144                m->m_data += sizeof(int);
145        }
146
147        /* Let BPF see incoming packet */
148        if (ifp->if_bpf) {
149                /*
150                 * We need to prepend the address family as
151                 * a four byte field.  Cons up a dummy header
152                 * to pacify bpf.  This is safe because bpf
153                 * will only read from the mbuf (i.e., it won't
154                 * try to free it or keep a pointer a to it).
155                 */
156                struct mbuf m0;
157                u_int af = dst->sa_family;
158
159                m0.m_next = m;
160                m0.m_len = 4;
161                m0.m_data = (char *)&af;
162
163                bpf_mtap(ifp, &m0);
164        }
165#endif
166        m->m_pkthdr.rcvif = ifp;
167
168        if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
169                m_freem(m);
170                return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
171                        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
172        }
173        ifp->if_opackets++;
174        ifp->if_obytes += m->m_pkthdr.len;
175        switch (dst->sa_family) {
176
177#ifdef INET
178        case AF_INET:
179                ifq = &ipintrq;
180                isr = NETISR_IP;
181                break;
182#endif
183#ifdef IPX
184        case AF_IPX:
185                ifq = &ipxintrq;
186                isr = NETISR_IPX;
187                break;
188#endif
189#ifdef NETATALK
190        case AF_APPLETALK:
191                ifq = &atintrq2;
192                isr = NETISR_ATALK;
193                break;
194#endif /* NETATALK */
195        default:
196                printf("lo%d: can't handle af%d\n", ifp->if_unit,
197                        dst->sa_family);
198                m_freem(m);
199                return (EAFNOSUPPORT);
200        }
201        s = splimp();
202        if (IF_QFULL(ifq)) {
203                IF_DROP(ifq);
204                m_freem(m);
205                splx(s);
206                return (ENOBUFS);
207        }
208        IF_ENQUEUE(ifq, m);
209        schednetisr(isr);
210        ifp->if_ipackets++;
211        ifp->if_ibytes += m->m_pkthdr.len;
212        splx(s);
213        return (0);
214}
215
216static void
217lortrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
218{
219        if (rt) {
220                rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
221                /*
222                 * For optimal performance, the send and receive buffers
223                 * should be at least twice the MTU plus a little more for
224                 * overhead.
225                 */
226                rt->rt_rmx.rmx_recvpipe =
227                        rt->rt_rmx.rmx_sendpipe = 3L * (long)LOMTU;
228        }
229}
230
231/*
232 * Process an ioctl request.
233 */
234static int
235loioctl(struct ifnet *ifp, ioctl_command_t cmd, caddr_t data)
236{
237        register struct ifaddr *ifa;
238        register struct ifreq *ifr = (struct ifreq *)data;
239        register int error = 0;
240
241        switch (cmd) {
242
243        case SIOCSIFADDR:
244                ifp->if_flags |= IFF_UP | IFF_RUNNING;
245                ifa = (struct ifaddr *)data;
246                ifa->ifa_rtrequest = lortrequest;
247                /*
248                 * Everything else is done at a higher level.
249                 */
250                break;
251
252        case SIOCADDMULTI:
253        case SIOCDELMULTI:
254                if (ifr == 0) {
255                        error = EAFNOSUPPORT;           /* XXX */
256                        break;
257                }
258                switch (ifr->ifr_addr.sa_family) {
259
260#ifdef INET
261                case AF_INET:
262                        break;
263#endif
264#ifdef INET6
265                case AF_INET6:
266                        break;
267#endif
268
269                default:
270                        error = EAFNOSUPPORT;
271                        break;
272                }
273                break;
274
275        case SIOCSIFMTU:
276                ifp->if_mtu = ifr->ifr_mtu;
277                break;
278
279        case SIOCSIFFLAGS:
280                break;
281
282        default:
283                error = EINVAL;
284        }
285        return (error);
286}
287#endif /* NLOOP > 0 */
Note: See TracBrowser for help on using the repository browser.