source: rtems/cpukit/libnetworking/net/if_loop.c @ 2dc5992d

4.104.11
Last change on this file since 2dc5992d was 2dc5992d, checked in by Ralf Corsepius <ralf.corsepius@…>, on Feb 11, 2009 at 3:36:07 AM

Remove PSEUDO_SET (Unused).

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