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

4.104.11
Last change on this file since b3f8c9e1 was b3f8c9e1, checked in by Ralf Corsepius <ralf.corsepius@…>, on Dec 22, 2008 at 7:47:28 AM

Include <errno.h> (POSIX,C99) instead of <sys/errno.h> (BSD'ism).

  • Property mode set to 100644
File size: 6.5 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 * $Id$
35 */
36
37/*
38 * Loopback interface driver for protocol testing and timing.
39 */
40#include "loop.h"
41#if NLOOP > 0
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/mbuf.h>
52#include <sys/socket.h>
53#include <errno.h>
54#include <sys/ioctl.h>
55#include <sys/time.h>
56
57#include <net/if.h>
58#include <net/if_types.h>
59#include <net/netisr.h>
60#include <net/route.h>
61#include <net/bpf.h>
62
63#ifdef  INET
64#include <netinet/in.h>
65#include <netinet/in_systm.h>
66#include <netinet/in_var.h>
67#include <netinet/ip.h>
68#endif
69
70#ifdef IPX
71#include <netipx/ipx.h>
72#include <netipx/ipx_if.h>
73#endif
74
75#ifdef INET6
76#ifndef INET
77#include <netinet/in.h>
78#endif
79#include <netinet6/in6_var.h>
80#include <netinet/ip6.h>
81#endif
82
83#ifdef NETATALK
84#include <netatalk/at.h>
85#include <netatalk/at_var.h>
86#endif
87
88static int loioctl(struct ifnet *, ioctl_command_t, caddr_t);
89static void lortrequest(int, struct rtentry *, struct sockaddr *);
90
91       void rtems_bsdnet_loopattach(void *);
92PSEUDO_SET(loopattach, if_loop);
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_loopattach(void *dummy)
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.