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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [bsd_tcpip/] [current/] [src/] [sys/] [netinet/] [if_ether.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      src/sys/netinet/if_ether.c
4
//
5
//==========================================================================
6
// ####BSDCOPYRIGHTBEGIN####                                    
7
// -------------------------------------------                  
8
// This file is part of eCos, the Embedded Configurable Operating System.
9
//
10
// Portions of this software may have been derived from FreeBSD 
11
// or other sources, and if so are covered by the appropriate copyright
12
// and license included herein.                                 
13
//
14
// Portions created by the Free Software Foundation are         
15
// Copyright (C) 2002 Free Software Foundation, Inc.            
16
// -------------------------------------------                  
17
// ####BSDCOPYRIGHTEND####                                      
18
//==========================================================================
19
 
20
/*
21
 * Copyright (c) 1982, 1986, 1988, 1993
22
 *      The Regents of the University of California.  All rights reserved.
23
 *
24
 * Redistribution and use in source and binary forms, with or without
25
 * modification, are permitted provided that the following conditions
26
 * are met:
27
 * 1. Redistributions of source code must retain the above copyright
28
 *    notice, this list of conditions and the following disclaimer.
29
 * 2. Redistributions in binary form must reproduce the above copyright
30
 *    notice, this list of conditions and the following disclaimer in the
31
 *    documentation and/or other materials provided with the distribution.
32
 * 3. All advertising materials mentioning features or use of this software
33
 *    must display the following acknowledgement:
34
 *      This product includes software developed by the University of
35
 *      California, Berkeley and its contributors.
36
 * 4. Neither the name of the University nor the names of its contributors
37
 *    may be used to endorse or promote products derived from this software
38
 *    without specific prior written permission.
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 *
52
 *      @(#)if_ether.c  8.1 (Berkeley) 6/10/93
53
 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.11 2001/07/25 17:27:56 jlemon Exp $
54
 */
55
 
56
/*
57
 * Ethernet address resolution protocol.
58
 * TODO:
59
 *      add "inuse/lock" bit (or ref. count) along with valid bit
60
 */
61
 
62
#include <sys/param.h>
63
#include <sys/queue.h>
64
#include <sys/sysctl.h>
65
#include <sys/mbuf.h>
66
#include <sys/malloc.h>
67
#include <sys/socket.h>
68
 
69
#include <net/if.h>
70
#include <net/if_dl.h>
71
#include <net/if_types.h>
72
#include <net/route.h>
73
#include <net/netisr.h>
74
#include <net/if_llc.h>
75
#ifdef BRIDGE
76
#include <net/ethernet.h>
77
#include <net/bridge.h>
78
#endif
79
 
80
#include <netinet/in.h>
81
#include <netinet/in_var.h>
82
#include <netinet/if_ether.h>
83
 
84
#include <net/iso88025.h>
85
 
86
#define SIN(s) ((struct sockaddr_in *)s)
87
#define SDL(s) ((struct sockaddr_dl *)s)
88
 
89
SYSCTL_DECL(_net_link_ether);
90
SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
91
 
92
/* timer values */
93
static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
94
static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
95
static int arpt_down = 20;      /* once declared down, don't send for 20 sec */
96
 
97
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
98
           &arpt_prune, 0, "");
99
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
100
           &arpt_keep, 0, "");
101
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
102
           &arpt_down, 0, "");
103
 
104
#define rt_expire rt_rmx.rmx_expire
105
 
106
struct llinfo_arp {
107
        LIST_ENTRY(llinfo_arp) la_le;
108
        struct  rtentry *la_rt;
109
        struct  mbuf *la_hold;          /* last packet until resolved/timeout */
110
        long    la_asked;               /* last time we QUERIED for this addr */
111
#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
112
};
113
 
114
static  LIST_HEAD(, llinfo_arp) llinfo_arp;
115
 
116
struct  ifqueue arpintrq = {0, 0, 0, 50};
117
static int      arp_inuse, arp_allocated;
118
 
119
static int      arp_maxtries = 5;
120
static int      useloopback = 1; /* use loopback interface for local traffic */
121
static int      arp_proxyall = 0;
122
 
123
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
124
           &arp_maxtries, 0, "");
125
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
126
           &useloopback, 0, "");
127
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
128
           &arp_proxyall, 0, "");
129
 
130
static void     arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));
131
static void     arprequest __P((struct arpcom *,
132
                        struct in_addr *, struct in_addr *, u_char *));
133
static void     arpintr __P((void));
134
static void     arptfree __P((struct llinfo_arp *));
135
static void     arptimer __P((void *));
136
static struct llinfo_arp
137
                *arplookup __P((u_long, int, int));
138
#ifdef INET
139
static void     in_arpinput __P((struct mbuf *));
140
#endif
141
 
142
/*
143
 * Timeout routine.  Age arp_tab entries periodically.
144
 */
145
/* ARGSUSED */
146
static void
147
arptimer(ignored_arg)
148
        void *ignored_arg;
149
{
150
        int s = splnet();
151
        register struct llinfo_arp *la = llinfo_arp.lh_first;
152
        struct llinfo_arp *ola;
153
 
154
        timeout(arptimer, (caddr_t)0, arpt_prune * hz);
155
        while ((ola = la) != 0) {
156
                register struct rtentry *rt = la->la_rt;
157
                la = la->la_le.le_next;
158
                if (rt->rt_expire && rt->rt_expire <= time_second)
159
                        arptfree(ola); /* timer has expired, clear */
160
        }
161
        splx(s);
162
}
163
 
164
/*
165
 * Parallel to llc_rtrequest.
166
 */
167
static void
168
arp_rtrequest(req, rt, sa)
169
        int req;
170
        register struct rtentry *rt;
171
        struct sockaddr *sa;
172
{
173
        register struct sockaddr *gate = rt->rt_gateway;
174
        register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
175
        static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
176
        static int arpinit_done;
177
 
178
        if (!arpinit_done) {
179
                arpinit_done = 1;
180
                LIST_INIT(&llinfo_arp);
181
                timeout(arptimer, (caddr_t)0, hz);
182
                register_netisr(NETISR_ARP, arpintr);
183
        }
184
        if (rt->rt_flags & RTF_GATEWAY)
185
                return;
186
        switch (req) {
187
 
188
        case RTM_ADD:
189
                /*
190
                 * XXX: If this is a manually added route to interface
191
                 * such as older version of routed or gated might provide,
192
                 * restore cloning bit.
193
                 */
194
                if ((rt->rt_flags & RTF_HOST) == 0 &&
195
                    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
196
                        rt->rt_flags |= RTF_CLONING;
197
                if (rt->rt_flags & RTF_CLONING) {
198
                        /*
199
                         * Case 1: This route should come from a route to iface.
200
                         */
201
                        rt_setgate(rt, rt_key(rt),
202
                                        (struct sockaddr *)&null_sdl);
203
                        gate = rt->rt_gateway;
204
                        SDL(gate)->sdl_type = rt->rt_ifp->if_type;
205
                        SDL(gate)->sdl_index = rt->rt_ifp->if_index;
206
                        rt->rt_expire = time_second;
207
                        break;
208
                }
209
                /* Announce a new entry if requested. */
210
                if (rt->rt_flags & RTF_ANNOUNCE)
211
                        arprequest((struct arpcom *)rt->rt_ifp,
212
                            &SIN(rt_key(rt))->sin_addr,
213
                            &SIN(rt_key(rt))->sin_addr,
214
                            (u_char *)LLADDR(SDL(gate)));
215
                /*FALLTHROUGH*/
216
        case RTM_RESOLVE:
217
                if (gate->sa_family != AF_LINK ||
218
                    gate->sa_len < sizeof(null_sdl)) {
219
                        log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
220
                        break;
221
                }
222
                SDL(gate)->sdl_type = rt->rt_ifp->if_type;
223
                SDL(gate)->sdl_index = rt->rt_ifp->if_index;
224
                if (la != 0)
225
                        break; /* This happens on a route change */
226
                /*
227
                 * Case 2:  This route may come from cloning, or a manual route
228
                 * add with a LL address.
229
                 */
230
                R_Malloc(la, struct llinfo_arp *, sizeof(*la));
231
                rt->rt_llinfo = (caddr_t)la;
232
                if (la == 0) {
233
                        log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
234
                        break;
235
                }
236
                arp_inuse++, arp_allocated++;
237
                Bzero(la, sizeof(*la));
238
                la->la_rt = rt;
239
                rt->rt_flags |= RTF_LLINFO;
240
                LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
241
 
242
#ifdef INET
243
                /*
244
                 * This keeps the multicast addresses from showing up
245
                 * in `arp -a' listings as unresolved.  It's not actually
246
                 * functional.  Then the same for broadcast.
247
                 */
248
                if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
249
                        ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
250
                                               LLADDR(SDL(gate)));
251
                        SDL(gate)->sdl_alen = 6;
252
                        rt->rt_expire = 0;
253
                }
254
                if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
255
                        memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6);
256
                        SDL(gate)->sdl_alen = 6;
257
                        rt->rt_expire = 0;
258
                }
259
#endif
260
 
261
                if (SIN(rt_key(rt))->sin_addr.s_addr ==
262
                    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
263
                    /*
264
                     * This test used to be
265
                     *  if (loif.if_flags & IFF_UP)
266
                     * It allowed local traffic to be forced
267
                     * through the hardware by configuring the loopback down.
268
                     * However, it causes problems during network configuration
269
                     * for boards that can't receive packets they send.
270
                     * It is now necessary to clear "useloopback" and remove
271
                     * the route to force traffic out to the hardware.
272
                     */
273
                        rt->rt_expire = 0;
274
                        Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
275
                                LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
276
                        if (useloopback)
277
                                rt->rt_ifp = loif;
278
 
279
                }
280
                break;
281
 
282
        case RTM_DELETE:
283
                if (la == 0)
284
                        break;
285
                arp_inuse--;
286
                LIST_REMOVE(la, la_le);
287
                rt->rt_llinfo = 0;
288
                rt->rt_flags &= ~RTF_LLINFO;
289
                if (la->la_hold)
290
                        m_freem(la->la_hold);
291
                R_Free((caddr_t)la);
292
        }
293
}
294
 
295
/*
296
 * Broadcast an ARP request. Caller specifies:
297
 *      - arp header source ip address
298
 *      - arp header target ip address
299
 *      - arp header source ethernet address
300
 */
301
static void
302
arprequest(ac, sip, tip, enaddr)
303
        register struct arpcom *ac;
304
        register struct in_addr *sip, *tip;
305
        register u_char *enaddr;
306
{
307
        register struct mbuf *m;
308
        register struct ether_header *eh;
309
        register struct ether_arp *ea;
310
        struct sockaddr sa;
311
        static u_char   llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP,
312
                                   LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 };
313
 
314
        if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
315
                return;
316
        m->m_pkthdr.rcvif = (struct ifnet *)0;
317
        switch (ac->ac_if.if_type) {
318
        case IFT_ISO88025:
319
                m->m_len = sizeof(*ea) + sizeof(llcx);
320
                m->m_pkthdr.len = sizeof(*ea) + sizeof(llcx);
321
                MH_ALIGN(m, sizeof(*ea) + sizeof(llcx));
322
                (void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx));
323
                (void)memcpy(sa.sa_data, etherbroadcastaddr, 6);
324
                (void)memcpy(sa.sa_data + 6, enaddr, 6);
325
                sa.sa_data[6] |= TR_RII;
326
                sa.sa_data[12] = TR_AC;
327
                sa.sa_data[13] = TR_LLC_FRAME;
328
                ea = (struct ether_arp *)(mtod(m, char *) + sizeof(llcx));
329
                bzero((caddr_t)ea, sizeof (*ea));
330
                ea->arp_hrd = htons(ARPHRD_IEEE802);
331
                break;
332
        case IFT_FDDI:
333
        case IFT_ETHER:
334
                /*
335
                 * This may not be correct for types not explicitly
336
                 * listed, but this is our best guess
337
                 */
338
        default:
339
                m->m_len = sizeof(*ea);
340
                m->m_pkthdr.len = sizeof(*ea);
341
                MH_ALIGN(m, sizeof(*ea));
342
                ea = mtod(m, struct ether_arp *);
343
                eh = (struct ether_header *)sa.sa_data;
344
                bzero((caddr_t)ea, sizeof (*ea));
345
                /* if_output will not swap */
346
                eh->ether_type = htons(ETHERTYPE_ARP);
347
                (void)memcpy(eh->ether_dhost, etherbroadcastaddr,
348
                    sizeof(eh->ether_dhost));
349
                ea->arp_hrd = htons(ARPHRD_ETHER);
350
                break;
351
        }
352
        ea->arp_pro = htons(ETHERTYPE_IP);
353
        ea->arp_hln = sizeof(ea->arp_sha);      /* hardware address length */
354
        ea->arp_pln = sizeof(ea->arp_spa);      /* protocol address length */
355
        ea->arp_op = htons(ARPOP_REQUEST);
356
        (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
357
        (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
358
        (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
359
        sa.sa_family = AF_UNSPEC;
360
        sa.sa_len = sizeof(sa);
361
        (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
362
}
363
 
364
/*
365
 * Resolve an IP address into an ethernet address.  If success,
366
 * desten is filled in.  If there is no entry in arptab,
367
 * set one up and broadcast a request for the IP address.
368
 * Hold onto this mbuf and resend it once the address
369
 * is finally resolved.  A return value of 1 indicates
370
 * that desten has been filled in and the packet should be sent
371
 * normally; a 0 return indicates that the packet has been
372
 * taken over here, either now or for later transmission.
373
 */
374
int
375
arpresolve(ac, rt, m, dst, desten, rt0)
376
        register struct arpcom *ac;
377
        register struct rtentry *rt;
378
        struct mbuf *m;
379
        register struct sockaddr *dst;
380
        register u_char *desten;
381
        struct rtentry *rt0;
382
{
383
        struct llinfo_arp *la = 0;
384
        struct sockaddr_dl *sdl;
385
 
386
        if (m->m_flags & M_BCAST) {     /* broadcast */
387
                (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
388
                return (1);
389
        }
390
        if (m->m_flags & M_MCAST) {     /* multicast */
391
                ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
392
                return(1);
393
        }
394
        if (rt)
395
                la = (struct llinfo_arp *)rt->rt_llinfo;
396
        if (la == 0) {
397
                la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
398
                if (la)
399
                        rt = la->la_rt;
400
        }
401
        if (la == 0 || rt == 0) {
402
                log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
403
                        inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
404
                                rt ? "rt" : "");
405
                m_freem(m);
406
                return (0);
407
        }
408
        sdl = SDL(rt->rt_gateway);
409
        /*
410
         * Check the address family and length is valid, the address
411
         * is resolved; otherwise, try to resolve.
412
         */
413
        if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
414
            sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
415
                bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
416
                return 1;
417
        }
418
        /*
419
         * If ARP is disabled on this interface, stop.
420
         * XXX
421
         * Probably should not allocate empty llinfo struct if we are
422
         * not going to be sending out an arp request.
423
         */
424
        if (ac->ac_if.if_flags & IFF_NOARP)
425
                return (0);
426
        /*
427
         * There is an arptab entry, but no ethernet address
428
         * response yet.  Replace the held mbuf with this
429
         * latest one.
430
         */
431
        if (la->la_hold)
432
                m_freem(la->la_hold);
433
        la->la_hold = m;
434
        if (rt->rt_expire) {
435
                rt->rt_flags &= ~RTF_REJECT;
436
                if (la->la_asked == 0 || rt->rt_expire != time_second) {
437
                        rt->rt_expire = time_second;
438
                        if (la->la_asked++ < arp_maxtries)
439
                            arprequest(ac,
440
                                &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
441
                                &SIN(dst)->sin_addr, ac->ac_enaddr);
442
                        else {
443
                                rt->rt_flags |= RTF_REJECT;
444
                                rt->rt_expire += arpt_down;
445
                                la->la_asked = 0;
446
                        }
447
 
448
                }
449
        }
450
        return (0);
451
}
452
 
453
/*
454
 * Common length and type checks are done here,
455
 * then the protocol-specific routine is called.
456
 */
457
static void
458
arpintr()
459
{
460
        register struct mbuf *m;
461
        register struct arphdr *ar;
462
        int s;
463
 
464
        while (arpintrq.ifq_head) {
465
                s = splimp();
466
                IF_DEQUEUE(&arpintrq, m);
467
                splx(s);
468
                if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
469
                        panic("arpintr");
470
 
471
                if (m->m_len < sizeof(struct arphdr) &&
472
                    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
473
                        log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
474
                        continue;
475
                }
476
                ar = mtod(m, struct arphdr *);
477
 
478
                if (ntohs(ar->ar_hrd) != ARPHRD_ETHER
479
                    && ntohs(ar->ar_hrd) != ARPHRD_IEEE802) {
480
                        log(LOG_ERR,
481
                            "arp: unknown hardware address format (0x%d)\n",
482
                            ntohs(ar->ar_hrd));
483
                        m_freem(m);
484
                        continue;
485
                }
486
 
487
                if (m->m_pkthdr.len < sizeof(struct arphdr) + 2 * ar->ar_hln
488
                    + 2 * ar->ar_pln) {
489
                        log(LOG_ERR, "arp: runt packet\n");
490
                        m_freem(m);
491
                        continue;
492
                }
493
 
494
                switch (ntohs(ar->ar_pro)) {
495
#ifdef INET
496
                        case ETHERTYPE_IP:
497
                                in_arpinput(m);
498
                                continue;
499
#endif
500
                }
501
                m_freem(m);
502
        }
503
}
504
 
505
#ifdef INET
506
/*
507
 * ARP for Internet protocols on 10 Mb/s Ethernet.
508
 * Algorithm is that given in RFC 826.
509
 * In addition, a sanity check is performed on the sender
510
 * protocol address, to catch impersonators.
511
 * We no longer handle negotiations for use of trailer protocol:
512
 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
513
 * along with IP replies if we wanted trailers sent to us,
514
 * and also sent them in response to IP replies.
515
 * This allowed either end to announce the desire to receive
516
 * trailer packets.
517
 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
518
 * but formerly didn't normally send requests.
519
 */
520
static int log_arp_wrong_iface = 1;
521
 
522
SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
523
        &log_arp_wrong_iface, 0,
524
        "log arp packets arriving on the wrong interface");
525
 
526
static void
527
in_arpinput(m)
528
        struct mbuf *m;
529
{
530
        register struct ether_arp *ea;
531
        register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
532
        struct ether_header *eh;
533
        struct iso88025_header *th = (struct iso88025_header *)0;
534
        register struct llinfo_arp *la = 0;
535
        register struct rtentry *rt;
536
        struct in_ifaddr *ia, *maybe_ia = 0;
537
        struct sockaddr_dl *sdl;
538
        struct sockaddr sa;
539
        struct in_addr isaddr, itaddr, myaddr;
540
        int op, rif_len;
541
 
542
        if (m->m_len < sizeof(struct ether_arp) &&
543
            (m = m_pullup(m, sizeof(struct ether_arp))) == NULL) {
544
                log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
545
                return;
546
        }
547
 
548
        ea = mtod(m, struct ether_arp *);
549
        op = ntohs(ea->arp_op);
550
        (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr));
551
        (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr));
552
        for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next) {
553
                /*
554
                 * For a bridge, we want to check the address irrespective
555
                 * of the receive interface. (This will change slightly
556
                 * when we have clusters of interfaces).
557
                 */
558
#ifdef BRIDGE
559
#define BRIDGE_TEST (do_bridge)
560
#else
561
#define BRIDGE_TEST (0) /* cc will optimise the test away */
562
#endif
563
                if ((BRIDGE_TEST) || (ia->ia_ifp == &ac->ac_if)) {
564
                        maybe_ia = ia;
565
                        if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
566
                             (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) {
567
                                break;
568
                        }
569
                }
570
        }
571
        if (maybe_ia == 0) {
572
                m_freem(m);
573
                return;
574
        }
575
        myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
576
        if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
577
            sizeof (ea->arp_sha))) {
578
                m_freem(m);     /* it's from me, ignore it. */
579
                return;
580
        }
581
        if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
582
            sizeof (ea->arp_sha))) {
583
                log(LOG_ERR,
584
                    "arp: ether address is broadcast for IP address %s!\n",
585
                    inet_ntoa(isaddr));
586
                m_freem(m);
587
                return;
588
        }
589
        if (isaddr.s_addr == myaddr.s_addr) {
590
                log(LOG_ERR,
591
                   "arp: %6p is using my IP address %s!\n",
592
                   ea->arp_sha, inet_ntoa(isaddr));
593
                itaddr = myaddr;
594
                goto reply;
595
        }
596
        la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
597
        if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
598
                /* the following is not an error when doing bridging */
599
                if (!BRIDGE_TEST && rt->rt_ifp != &ac->ac_if) {
600
                    if (log_arp_wrong_iface)
601
                        log(LOG_ERR, "arp: %s is on %s%d but got reply from %6p on %s%d\n",
602
                            inet_ntoa(isaddr),
603
                            rt->rt_ifp->if_name, rt->rt_ifp->if_unit,
604
                            ea->arp_sha,
605
                            ac->ac_if.if_name, ac->ac_if.if_unit);
606
                    goto reply;
607
                }
608
                if (sdl->sdl_alen &&
609
                    bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) {
610
                        if (rt->rt_expire)
611
                            log(LOG_INFO, "arp: %s moved from %6p to %6p on %s%d\n",
612
                                inet_ntoa(isaddr), (u_char *)LLADDR(sdl),
613
                                ea->arp_sha,
614
                                ac->ac_if.if_name, ac->ac_if.if_unit);
615
                        else {
616
                            log(LOG_ERR,
617
                                "arp: %6p attempts to modify permanent entry for %s on %s%d\n",
618
                                ea->arp_sha, inet_ntoa(isaddr),
619
                                ac->ac_if.if_name, ac->ac_if.if_unit);
620
                            goto reply;
621
                        }
622
                }
623
                (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
624
                sdl->sdl_alen = sizeof(ea->arp_sha);
625
                sdl->sdl_rcf = (u_short)0;
626
                /*
627
                 * If we receive an arp from a token-ring station over
628
                 * a token-ring nic then try to save the source
629
                 * routing info.
630
                 */
631
                if (ac->ac_if.if_type == IFT_ISO88025) {
632
                        th = (struct iso88025_header *)m->m_pkthdr.header;
633
                        rif_len = TR_RCF_RIFLEN(th->rcf);
634
                        if ((th->iso88025_shost[0] & TR_RII) &&
635
                            (rif_len > 2)) {
636
                                sdl->sdl_rcf = th->rcf;
637
                                sdl->sdl_rcf ^= htons(TR_RCF_DIR);
638
                                memcpy(sdl->sdl_route, th->rd, rif_len - 2);
639
                                sdl->sdl_rcf &= ~htons(TR_RCF_BCST_MASK);
640
                                /*
641
                                 * Set up source routing information for
642
                                 * reply packet (XXX)
643
                                 */
644
                                m->m_data -= rif_len;
645
                                m->m_len  += rif_len;
646
                                m->m_pkthdr.len += rif_len;
647
                        } else {
648
                                th->iso88025_shost[0] &= ~TR_RII;
649
                        }
650
                        m->m_data -= 8;
651
                        m->m_len  += 8;
652
                        m->m_pkthdr.len += 8;
653
                        th->rcf = sdl->sdl_rcf;
654
                } else {
655
                        sdl->sdl_rcf = (u_short)0;
656
                }
657
                if (rt->rt_expire)
658
                        rt->rt_expire = time_second + arpt_keep;
659
                rt->rt_flags &= ~RTF_REJECT;
660
                la->la_asked = 0;
661
                if (la->la_hold) {
662
                        (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
663
                                rt_key(rt), rt);
664
                        la->la_hold = 0;
665
                }
666
        }
667
reply:
668
        if (op != ARPOP_REQUEST) {
669
                m_freem(m);
670
                return;
671
        }
672
        if (itaddr.s_addr == myaddr.s_addr) {
673
                /* I am the target */
674
                (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
675
                (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
676
        } else {
677
                la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
678
                if (la == NULL) {
679
                        struct sockaddr_in sin;
680
 
681
                        if (!arp_proxyall) {
682
                                m_freem(m);
683
                                return;
684
                        }
685
 
686
                        bzero(&sin, sizeof sin);
687
                        sin.sin_family = AF_INET;
688
                        sin.sin_len = sizeof sin;
689
                        sin.sin_addr = itaddr;
690
 
691
                        rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
692
                        if (!rt) {
693
                                m_freem(m);
694
                                return;
695
                        }
696
                        /*
697
                         * Don't send proxies for nodes on the same interface
698
                         * as this one came out of, or we'll get into a fight
699
                         * over who claims what Ether address.
700
                         */
701
                        if (rt->rt_ifp == &ac->ac_if) {
702
                                rtfree(rt);
703
                                m_freem(m);
704
                                return;
705
                        }
706
                        (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
707
                        (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
708
                        rtfree(rt);
709
#ifdef DEBUG_PROXY
710
                        printf("arp: proxying for %s\n",
711
                               inet_ntoa(itaddr));
712
#endif
713
                } else {
714
                        rt = la->la_rt;
715
                        (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
716
                        sdl = SDL(rt->rt_gateway);
717
                        (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
718
                }
719
        }
720
 
721
        (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
722
        (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
723
        ea->arp_op = htons(ARPOP_REPLY);
724
        ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
725
        switch (ac->ac_if.if_type) {
726
        case IFT_ISO88025:
727
                /* Re-arrange the source/dest address */
728
                memcpy(th->iso88025_dhost, th->iso88025_shost,
729
                    sizeof(th->iso88025_dhost));
730
                memcpy(th->iso88025_shost, ac->ac_enaddr,
731
                    sizeof(th->iso88025_shost));
732
                /* Set the source routing bit if neccesary */
733
                if (th->iso88025_dhost[0] & TR_RII) {
734
                        th->iso88025_dhost[0] &= ~TR_RII;
735
                        if (TR_RCF_RIFLEN(th->rcf) > 2)
736
                                th->iso88025_shost[0] |= TR_RII;
737
                }
738
                /* Copy the addresses, ac and fc into sa_data */
739
                memcpy(sa.sa_data, th->iso88025_dhost,
740
                    sizeof(th->iso88025_dhost) * 2);
741
                sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC;
742
                sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME;
743
                break;
744
        case IFT_ETHER:
745
        case IFT_FDDI:
746
        /*
747
         * May not be correct for types not explictly
748
         * listed, but it is our best guess.
749
         */
750
        default:
751
                eh = (struct ether_header *)sa.sa_data;
752
                (void)memcpy(eh->ether_dhost, ea->arp_tha,
753
                    sizeof(eh->ether_dhost));
754
                eh->ether_type = htons(ETHERTYPE_ARP);
755
                break;
756
        }
757
        sa.sa_family = AF_UNSPEC;
758
        sa.sa_len = sizeof(sa);
759
        (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
760
        return;
761
}
762
#endif
763
 
764
/*
765
 * Free an arp entry.
766
 */
767
static void
768
arptfree(la)
769
        register struct llinfo_arp *la;
770
{
771
        register struct rtentry *rt = la->la_rt;
772
        register struct sockaddr_dl *sdl;
773
        if (rt == 0)
774
                panic("arptfree");
775
        if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
776
            sdl->sdl_family == AF_LINK) {
777
                sdl->sdl_alen = 0;
778
                la->la_asked = 0;
779
                rt->rt_flags &= ~RTF_REJECT;
780
                return;
781
        }
782
        rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
783
                        0, (struct rtentry **)0);
784
}
785
/*
786
 * Lookup or enter a new address in arptab.
787
 */
788
static struct llinfo_arp *
789
arplookup(addr, create, proxy)
790
        u_long addr;
791
        int create, proxy;
792
{
793
        register struct rtentry *rt;
794
        static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
795
        const char *why = 0;
796
 
797
        sin.sin_addr.s_addr = addr;
798
        sin.sin_other = proxy ? SIN_PROXY : 0;
799
        rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
800
        if (rt == 0)
801
                return (0);
802
        rt->rt_refcnt--;
803
 
804
        if (rt->rt_flags & RTF_GATEWAY)
805
                why = "host is not on local network";
806
        else if ((rt->rt_flags & RTF_LLINFO) == 0)
807
                why = "could not allocate llinfo";
808
        else if (rt->rt_gateway->sa_family != AF_LINK)
809
                why = "gateway route is not ours";
810
 
811
        if (why) {
812
                if (create) {
813
                        log(LOG_DEBUG, "arplookup %s failed: %s\n",
814
                            inet_ntoa(sin.sin_addr), why);
815
                        /*
816
                         * If there are no references to this Layer 2 route,
817
                         * and it is a cloned route, and not static, and
818
                         * arplookup() is creating the route, then purge
819
                         * it from the routing table as it is probably bogus.
820
                         */
821
                        if (((rt->rt_flags & (RTF_STATIC | RTF_WASCLONED)) ==
822
                            RTF_WASCLONED) && (rt->rt_refcnt == 0))
823
                                rtrequest(RTM_DELETE,
824
                                    (struct sockaddr *)rt_key(rt),
825
                                    rt->rt_gateway, rt_mask(rt),
826
                                    rt->rt_flags, 0);
827
                }
828
                return (0);
829
        }
830
        return ((struct llinfo_arp *)rt->rt_llinfo);
831
}
832
 
833
void
834
arp_ifinit(ac, ifa)
835
        struct arpcom *ac;
836
        struct ifaddr *ifa;
837
{
838
        if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
839
                arprequest(ac, &IA_SIN(ifa)->sin_addr,
840
                               &IA_SIN(ifa)->sin_addr, ac->ac_enaddr);
841
        ifa->ifa_rtrequest = arp_rtrequest;
842
        ifa->ifa_flags |= RTF_CLONING;
843
}

powered by: WebSVN 2.1.0

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