OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [tcpip/] [v2_0/] [src/] [sys/] [net/] [if_loop.c] - Blame information for rev 27

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      sys/net/if_loop.c
4
//
5
//     
6
//
7
//==========================================================================
8
//####BSDCOPYRIGHTBEGIN####
9
//
10
// -------------------------------------------
11
//
12
// Portions of this software may have been derived from OpenBSD or other sources,
13
// and are covered by the appropriate copyright disclaimers included herein.
14
//
15
// -------------------------------------------
16
//
17
//####BSDCOPYRIGHTEND####
18
//==========================================================================
19
//#####DESCRIPTIONBEGIN####
20
//
21
// Author(s):    gthomas
22
// Contributors: gthomas
23
// Date:         2000-01-10
24
// Purpose:      
25
// Description:  
26
//              
27
//
28
//####DESCRIPTIONEND####
29
//
30
//==========================================================================
31
 
32
 
33
/*      $OpenBSD: if_loop.c,v 1.12 1999/12/08 06:50:18 itojun Exp $     */
34
/*      $NetBSD: if_loop.c,v 1.15 1996/05/07 02:40:33 thorpej Exp $     */
35
 
36
/*
37
 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
38
 * All rights reserved.
39
 *
40
 * Redistribution and use in source and binary forms, with or without
41
 * modification, are permitted provided that the following conditions
42
 * are met:
43
 * 1. Redistributions of source code must retain the above copyright
44
 *    notice, this list of conditions and the following disclaimer.
45
 * 2. Redistributions in binary form must reproduce the above copyright
46
 *    notice, this list of conditions and the following disclaimer in the
47
 *    documentation and/or other materials provided with the distribution.
48
 * 3. Neither the name of the project nor the names of its contributors
49
 *    may be used to endorse or promote products derived from this software
50
 *    without specific prior written permission.
51
 *
52
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
53
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
56
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62
 * SUCH DAMAGE.
63
 */
64
 
65
/*
66
 * Copyright (c) 1982, 1986, 1993
67
 *      The Regents of the University of California.  All rights reserved.
68
 *
69
 * Redistribution and use in source and binary forms, with or without
70
 * modification, are permitted provided that the following conditions
71
 * are met:
72
 * 1. Redistributions of source code must retain the above copyright
73
 *    notice, this list of conditions and the following disclaimer.
74
 * 2. Redistributions in binary form must reproduce the above copyright
75
 *    notice, this list of conditions and the following disclaimer in the
76
 *    documentation and/or other materials provided with the distribution.
77
 * 3. All advertising materials mentioning features or use of this software
78
 *    must display the following acknowledgement:
79
 *      This product includes software developed by the University of
80
 *      California, Berkeley and its contributors.
81
 * 4. Neither the name of the University nor the names of its contributors
82
 *    may be used to endorse or promote products derived from this software
83
 *    without specific prior written permission.
84
 *
85
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95
 * SUCH DAMAGE.
96
 *
97
 *      @(#)if_loop.c   8.1 (Berkeley) 6/10/93
98
 */
99
 
100
/*
101
%%% portions-copyright-nrl-95
102
Portions of this software are Copyright 1995-1998 by Randall Atkinson,
103
Ronald Lee, Daniel McDonald, Bao Phan, and Chris Winters. All Rights
104
Reserved. All rights under this copyright have been assigned to the US
105
Naval Research Laboratory (NRL). The NRL Copyright Notice and License
106
Agreement Version 1.1 (January 17, 1995) applies to these portions of the
107
software.
108
You should have received a copy of the license with this software. If you
109
didn't get a copy, you may request one from <license@ipv6.nrl.navy.mil>.
110
*/
111
 
112
/*
113
 * Loopback interface driver for protocol testing and timing.
114
 */
115
 
116
#ifndef __ECOS
117
#include "bpfilter.h"
118
#include "loop.h"
119
#endif
120
 
121
#include <sys/param.h>
122
#ifndef __ECOS
123
#include <sys/systm.h>
124
#endif
125
#include <sys/kernel.h>
126
#include <sys/mbuf.h>
127
#include <sys/socket.h>
128
#include <sys/errno.h>
129
#include <sys/ioctl.h>
130
#include <sys/time.h>
131
 
132
#include <machine/cpu.h>
133
 
134
#include <net/if.h>
135
#include <net/if_types.h>
136
#include <net/netisr.h>
137
#include <net/route.h>
138
 
139
#ifdef  INET
140
#include <netinet/in.h>
141
#include <netinet/in_systm.h>
142
#include <netinet/in_var.h>
143
#include <netinet/ip.h>
144
#endif
145
 
146
#ifdef INET6
147
#ifndef INET
148
#include <netinet/in.h>
149
#endif
150
#include <netinet6/in6_var.h>
151
#include <netinet6/ip6.h>
152
#endif
153
 
154
#ifdef NS
155
#include <netns/ns.h>
156
#include <netns/ns_if.h>
157
#endif
158
 
159
#ifdef IPX
160
#include <netipx/ipx.h>
161
#include <netipx/ipx_if.h>
162
#endif
163
 
164
#ifdef ISO
165
#include <netiso/iso.h>
166
#include <netiso/iso_var.h>
167
#endif
168
 
169
#ifdef NETATALK
170
#include <netinet/if_ether.h>
171
#include <netatalk/at.h>
172
#include <netatalk/at_var.h>
173
#endif
174
 
175
#if NBPFILTER > 0
176
#include <net/bpf.h>
177
#endif
178
 
179
#if defined(LARGE_LOMTU)
180
#define LOMTU   (131072 +  MHLEN + MLEN)
181
#else
182
#define LOMTU   (32768 +  MHLEN + MLEN)
183
#endif
184
 
185
#ifndef __ECOS
186
#include <stdio.h>    // for 'sprintf()'
187
#endif
188
 
189
struct  ifnet loif[NLOOP];
190
 
191
void
192
loopattach(n)
193
        int n;
194
{
195
        register int i;
196
        register struct ifnet *ifp;
197
 
198
        for (i = NLOOP; i--; ) {
199
                ifp = &loif[i];
200
#if !defined(__ECOS) || (CYGPKG_NET_NLOOP > 1)
201
                sprintf(ifp->if_xname, "lo%d", i);
202
#else
203
                strcpy(ifp->if_xname, "lo0");
204
#endif                
205
                ifp->if_softc = NULL;
206
                ifp->if_mtu = LOMTU;
207
                ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
208
                ifp->if_ioctl = loioctl;
209
                ifp->if_output = looutput;
210
                ifp->if_type = IFT_LOOP;
211
                ifp->if_hdrlen = sizeof(u_int32_t);
212
                ifp->if_addrlen = 0;
213
                if_attachhead(ifp);
214
#if NBPFILTER > 0
215
                bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t));
216
#endif
217
        }
218
}
219
 
220
int
221
looutput(ifp, m, dst, rt)
222
        struct ifnet *ifp;
223
        register struct mbuf *m;
224
        struct sockaddr *dst;
225
        register struct rtentry *rt;
226
{
227
        int s, isr;
228
        register struct ifqueue *ifq = 0;
229
 
230
        if ((m->m_flags & M_PKTHDR) == 0)
231
                panic("looutput: no header mbuf");
232
        ifp->if_lastchange = time;
233
#if NBPFILTER > 0
234
        /*
235
         * only send packets to bpf if they are real loopback packets;
236
         * looutput() is also called for SIMPLEX interfaces to duplicate
237
         * packets for local use. But don't dup them to bpf.
238
         */
239
        if (ifp->if_bpf && (ifp->if_flags&IFF_LOOPBACK)) {
240
                /*
241
                 * We need to prepend the address family as
242
                 * a four byte field.  Cons up a dummy header
243
                 * to pacify bpf.  This is safe because bpf
244
                 * will only read from the mbuf (i.e., it won't
245
                 * try to free it or keep a pointer to it).
246
                 */
247
                struct mbuf m0;
248
                u_int32_t af = htonl(dst->sa_family);
249
 
250
                m0.m_next = m;
251
                m0.m_len = sizeof(af);
252
                m0.m_data = (char *)&af;
253
 
254
                bpf_mtap(ifp->if_bpf, &m0);
255
        }
256
#endif
257
        m->m_pkthdr.rcvif = ifp;
258
 
259
        if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
260
                m_freem(m);
261
                return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
262
                        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
263
        }
264
 
265
#ifndef PULLDOWN_TEST
266
        /*
267
         * KAME requires that the packet to be contiguous on the
268
         * mbuf.  We need to make that sure.
269
         * this kind of code should be avoided.
270
         * XXX other conditions to avoid running this part?
271
         */
272
        if (m && m->m_next != NULL) {
273
                struct mbuf *n;
274
 
275
                MGETHDR(n, M_DONTWAIT, MT_HEADER);
276
                if (n) {
277
                        MCLGET(n, M_DONTWAIT);
278
                        if ((n->m_flags & M_EXT) == 0) {
279
                                m_free(n);
280
                                n = NULL;
281
                        }
282
                }
283
                if (!n) {
284
#ifdef __ECOS
285
                        diag_printf("looutput: mbuf allocation failed\n");
286
#else
287
                        printf("looutput: mbuf allocation failed\n");
288
#endif
289
                        m_freem(m);
290
                        return ENOBUFS;
291
                }
292
 
293
                n->m_pkthdr.rcvif = m->m_pkthdr.rcvif;
294
                n->m_pkthdr.len = m->m_pkthdr.len;
295
                if (m->m_pkthdr.len <= MCLBYTES) {
296
                        m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
297
                        n->m_len = m->m_pkthdr.len;
298
                        m_freem(m);
299
                } else {
300
                        m_copydata(m, 0, MCLBYTES, mtod(n, caddr_t));
301
                        m_adj(m, MCLBYTES);
302
                        n->m_len = MCLBYTES;
303
                        n->m_next = m;
304
                        m->m_flags &= ~M_PKTHDR;
305
                }
306
                m = n;
307
        }
308
#if 0
309
        if (m && m->m_next != NULL) {
310
                printf("loop: not contiguous...\n");
311
                m_freem(m);
312
                return ENOBUFS;
313
        }
314
#endif
315
#endif
316
 
317
        ifp->if_opackets++;
318
        ifp->if_obytes += m->m_pkthdr.len;
319
        switch (dst->sa_family) {
320
 
321
#ifdef INET
322
        case AF_INET:
323
                ifq = &ipintrq;
324
                isr = NETISR_IP;
325
                break;
326
#endif
327
#ifdef INET6
328
        case AF_INET6:
329
                ifq = &ip6intrq;
330
                isr = NETISR_IPV6;
331
                break;
332
#endif /* INET6 */
333
#ifdef NS
334
        case AF_NS:
335
                ifq = &nsintrq;
336
                isr = NETISR_NS;
337
                break;
338
#endif
339
#ifdef IPX
340
        case AF_IPX:
341
                ifq = &ipxintrq;
342
                isr = NETISR_IPX;
343
                break;
344
#endif
345
#ifdef ISO
346
        case AF_ISO:
347
                ifq = &clnlintrq;
348
                isr = NETISR_ISO;
349
                break;
350
#endif
351
#ifdef NETATALK
352
        case AF_APPLETALK:
353
                ifq = &atintrq2;
354
                isr = NETISR_ATALK;
355
                break;
356
#endif
357
        default:
358
#ifdef __ECOS
359
                diag_printf("%s: can't handle af%d\n", ifp->if_xname,
360
                            dst->sa_family);
361
#else
362
                printf("%s: can't handle af%d\n", ifp->if_xname,
363
                        dst->sa_family);
364
#endif
365
                m_freem(m);
366
                return (EAFNOSUPPORT);
367
        }
368
        s = splimp();
369
        if (IF_QFULL(ifq)) {
370
                IF_DROP(ifq);
371
                m_freem(m);
372
                splx(s);
373
                return (ENOBUFS);
374
        }
375
        IF_ENQUEUE(ifq, m);
376
        schednetisr(isr);
377
        ifp->if_ipackets++;
378
        ifp->if_ibytes += m->m_pkthdr.len;
379
        splx(s);
380
        return (0);
381
}
382
 
383
/* ARGSUSED */
384
void
385
lortrequest(cmd, rt, sa)
386
        int cmd;
387
        struct rtentry *rt;
388
        struct sockaddr *sa;
389
{
390
 
391
        if (rt)
392
                rt->rt_rmx.rmx_mtu = LOMTU;
393
}
394
 
395
/*
396
 * Process an ioctl request.
397
 */
398
/* ARGSUSED */
399
int
400
loioctl(ifp, cmd, data)
401
        register struct ifnet *ifp;
402
        u_long cmd;
403
        caddr_t data;
404
{
405
        register struct ifaddr *ifa;
406
        register struct ifreq *ifr;
407
        register int error = 0;
408
 
409
        switch (cmd) {
410
 
411
        case SIOCSIFADDR:
412
                ifp->if_flags |= IFF_UP;
413
                ifa = (struct ifaddr *)data;
414
                if (ifa != 0 /*&& ifa->ifa_addr->sa_family == AF_ISO*/)
415
                        ifa->ifa_rtrequest = lortrequest;
416
                /*
417
                 * Everything else is done at a higher level.
418
                 */
419
                break;
420
 
421
        case SIOCADDMULTI:
422
        case SIOCDELMULTI:
423
                ifr = (struct ifreq *)data;
424
                if (ifr == 0) {
425
                        error = EAFNOSUPPORT;           /* XXX */
426
                        break;
427
                }
428
                switch (ifr->ifr_addr.sa_family) {
429
 
430
#ifdef INET
431
                case AF_INET:
432
                        break;
433
#endif
434
#ifdef INET6
435
                case AF_INET6:
436
                        break;
437
#endif /* INET6 */
438
 
439
                default:
440
                        error = EAFNOSUPPORT;
441
                        break;
442
                }
443
                break;
444
 
445
        default:
446
                error = EINVAL;
447
        }
448
        return (error);
449
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.