source: rtems/cpukit/libnetworking/net/if_loop.c @ 0262dee

4.104.114.84.95
Last change on this file since 0262dee was 0262dee, checked in by Ralf Corsepius <ralf.corsepius@…>, on 10/22/04 at 10:03:53

2004-10-22 Ralf Corsepius <ralf_corsepius@…>

  • libnetworking/opt_atalk.h, libnetworking/opt_bdg.h, libnetworking/opt_inet.h, libnetworking/opt_inet6.h, libnetworking/opt_ipx.h, libnetworking/opt_mac.h, libnetworking/opt_netgraph.h: New (Stubs).
  • libnetworking/net/if_loop.c: Cosmetical update from FreeBSD.
  • 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.102 2004/08/27 18:33:07 andre 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 <sys/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 /* NETATALK */
87
88#include "bpfilter.h"
89
90static int loioctl __P((struct ifnet *, int, caddr_t));
91static void lortrequest __P((int, struct rtentry *, struct sockaddr *));
92
93       void rtems_bsdnet_loopattach __P((void *));
94PSEUDO_SET(loopattach, if_loop);
95
96#ifdef TINY_LOMTU
97#define LOMTU   (1024+512)
98#elif defined(LARGE_LOMTU)
99#define LOMTU  131072
100#else
101#define LOMTU   16384
102#endif
103
104struct  ifnet loif[NLOOP];
105
106/* ARGSUSED */
107void
108rtems_bsdnet_loopattach(dummy)
109        void *dummy;
110{
111        register struct ifnet *ifp;
112        register int i = 0;
113
114        for (ifp = loif; i < NLOOP; ifp++) {
115            ifp->if_name = "lo";
116            ifp->if_next = NULL;
117            ifp->if_unit = i++;
118            ifp->if_mtu = LOMTU;
119            ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
120            ifp->if_ioctl = loioctl;
121            ifp->if_output = looutput;
122            ifp->if_type = IFT_LOOP;
123            ifp->if_hdrlen = 0;
124            ifp->if_addrlen = 0;
125            ifp->if_snd.ifq_maxlen = ifqmaxlen;
126            if_attach(ifp);
127#if NBPFILTER > 0
128            bpfattach(ifp, DLT_NULL, sizeof(u_int));
129#endif
130        }
131}
132
133int
134looutput(ifp, m, dst, rt)
135        struct ifnet *ifp;
136        register struct mbuf *m;
137        struct sockaddr *dst;
138        register struct rtentry *rt;
139{
140        int s, isr;
141        register struct ifqueue *ifq = 0;
142
143        if ((m->m_flags & M_PKTHDR) == 0)
144                panic("looutput no HDR");
145#if NBPFILTER > 0
146        /* BPF write needs to be handled specially */
147        if (dst->sa_family == AF_UNSPEC) {
148                dst->sa_family = *(mtod(m, int *));
149                m->m_len -= sizeof(int);
150                m->m_pkthdr.len -= sizeof(int);
151                m->m_data += sizeof(int);
152        }
153
154        /* Let BPF see incoming packet */
155        if (ifp->if_bpf) {
156                /*
157                 * We need to prepend the address family as
158                 * a four byte field.  Cons up a dummy header
159                 * to pacify bpf.  This is safe because bpf
160                 * will only read from the mbuf (i.e., it won't
161                 * try to free it or keep a pointer a to it).
162                 */
163                struct mbuf m0;
164                u_int af = dst->sa_family;
165
166                m0.m_next = m;
167                m0.m_len = 4;
168                m0.m_data = (char *)&af;
169
170                bpf_mtap(ifp, &m0);
171        }
172#endif
173        m->m_pkthdr.rcvif = ifp;
174
175        if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
176                m_freem(m);
177                return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
178                        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
179        }
180        ifp->if_opackets++;
181        ifp->if_obytes += m->m_pkthdr.len;
182        switch (dst->sa_family) {
183
184#ifdef INET
185        case AF_INET:
186                ifq = &ipintrq;
187                isr = NETISR_IP;
188                break;
189#endif
190#ifdef IPX
191        case AF_IPX:
192                ifq = &ipxintrq;
193                isr = NETISR_IPX;
194                break;
195#endif
196#ifdef NETATALK
197        case AF_APPLETALK:
198                ifq = &atintrq2;
199                isr = NETISR_ATALK;
200                break;
201#endif /* NETATALK */
202        default:
203                printf("lo%d: can't handle af%d\n", ifp->if_unit,
204                        dst->sa_family);
205                m_freem(m);
206                return (EAFNOSUPPORT);
207        }
208        s = splimp();
209        if (IF_QFULL(ifq)) {
210                IF_DROP(ifq);
211                m_freem(m);
212                splx(s);
213                return (ENOBUFS);
214        }
215        IF_ENQUEUE(ifq, m);
216        schednetisr(isr);
217        ifp->if_ipackets++;
218        ifp->if_ibytes += m->m_pkthdr.len;
219        splx(s);
220        return (0);
221}
222
223/* ARGSUSED */
224static void
225lortrequest(cmd, rt, sa)
226        int cmd;
227        struct rtentry *rt;
228        struct sockaddr *sa;
229{
230        if (rt) {
231                rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
232                /*
233                 * For optimal performance, the send and receive buffers
234                 * should be at least twice the MTU plus a little more for
235                 * overhead.
236                 */
237                rt->rt_rmx.rmx_recvpipe =
238                        rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
239        }
240}
241
242/*
243 * Process an ioctl request.
244 */
245/* ARGSUSED */
246static int
247loioctl(ifp, cmd, data)
248        register struct ifnet *ifp;
249        int cmd;
250        caddr_t data;
251{
252        register struct ifaddr *ifa;
253        register struct ifreq *ifr = (struct ifreq *)data;
254        register int error = 0;
255
256        switch (cmd) {
257
258        case SIOCSIFADDR:
259                ifp->if_flags |= IFF_UP | IFF_RUNNING;
260                ifa = (struct ifaddr *)data;
261                ifa->ifa_rtrequest = lortrequest;
262                /*
263                 * Everything else is done at a higher level.
264                 */
265                break;
266
267        case SIOCADDMULTI:
268        case SIOCDELMULTI:
269                if (ifr == 0) {
270                        error = EAFNOSUPPORT;           /* XXX */
271                        break;
272                }
273                switch (ifr->ifr_addr.sa_family) {
274
275#ifdef INET
276                case AF_INET:
277                        break;
278#endif
279
280                default:
281                        error = EAFNOSUPPORT;
282                        break;
283                }
284                break;
285
286        case SIOCSIFMTU:
287                ifp->if_mtu = ifr->ifr_mtu;
288                break;
289
290        default:
291                error = EINVAL;
292        }
293        return (error);
294}
295#endif /* NLOOP > 0 */
Note: See TracBrowser for help on using the repository browser.