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

4.104.11
Last change on this file since b25b88e7 was b25b88e7, checked in by Ralf Corsepius <ralf.corsepius@…>, on Mar 28, 2010 at 5:50:29 AM

Add HAVE_CONFIG_H support to let files receive configure defines.

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