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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [ipv4/] [arp.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/* linux/net/inet/arp.c
2
 *
3
 * Version:     $Id: arp.c,v 1.1.1.1 2004-04-15 01:13:30 phoenix Exp $
4
 *
5
 * Copyright (C) 1994 by Florian  La Roche
6
 *
7
 * This module implements the Address Resolution Protocol ARP (RFC 826),
8
 * which is used to convert IP addresses (or in the future maybe other
9
 * high-level addresses) into a low-level hardware address (like an Ethernet
10
 * address).
11
 *
12
 * This program is free software; you can redistribute it and/or
13
 * modify it under the terms of the GNU General Public License
14
 * as published by the Free Software Foundation; either version
15
 * 2 of the License, or (at your option) any later version.
16
 *
17
 * Fixes:
18
 *              Alan Cox        :       Removed the Ethernet assumptions in
19
 *                                      Florian's code
20
 *              Alan Cox        :       Fixed some small errors in the ARP
21
 *                                      logic
22
 *              Alan Cox        :       Allow >4K in /proc
23
 *              Alan Cox        :       Make ARP add its own protocol entry
24
 *              Ross Martin     :       Rewrote arp_rcv() and arp_get_info()
25
 *              Stephen Henson  :       Add AX25 support to arp_get_info()
26
 *              Alan Cox        :       Drop data when a device is downed.
27
 *              Alan Cox        :       Use init_timer().
28
 *              Alan Cox        :       Double lock fixes.
29
 *              Martin Seine    :       Move the arphdr structure
30
 *                                      to if_arp.h for compatibility.
31
 *                                      with BSD based programs.
32
 *              Andrew Tridgell :       Added ARP netmask code and
33
 *                                      re-arranged proxy handling.
34
 *              Alan Cox        :       Changed to use notifiers.
35
 *              Niibe Yutaka    :       Reply for this device or proxies only.
36
 *              Alan Cox        :       Don't proxy across hardware types!
37
 *              Jonathan Naylor :       Added support for NET/ROM.
38
 *              Mike Shaver     :       RFC1122 checks.
39
 *              Jonathan Naylor :       Only lookup the hardware address for
40
 *                                      the correct hardware type.
41
 *              Germano Caronni :       Assorted subtle races.
42
 *              Craig Schlenter :       Don't modify permanent entry
43
 *                                      during arp_rcv.
44
 *              Russ Nelson     :       Tidied up a few bits.
45
 *              Alexey Kuznetsov:       Major changes to caching and behaviour,
46
 *                                      eg intelligent arp probing and
47
 *                                      generation
48
 *                                      of host down events.
49
 *              Alan Cox        :       Missing unlock in device events.
50
 *              Eckes           :       ARP ioctl control errors.
51
 *              Alexey Kuznetsov:       Arp free fix.
52
 *              Manuel Rodriguez:       Gratuitous ARP.
53
 *              Jonathan Layes  :       Added arpd support through kerneld
54
 *                                      message queue (960314)
55
 *              Mike Shaver     :       /proc/sys/net/ipv4/arp_* support
56
 *              Mike McLagan    :       Routing by source
57
 *              Stuart Cheshire :       Metricom and grat arp fixes
58
 *                                      *** FOR 2.1 clean this up ***
59
 *              Lawrence V. Stefani: (08/12/96) Added FDDI support.
60
 *              Alan Cox        :       Took the AP1000 nasty FDDI hack and
61
 *                                      folded into the mainstream FDDI code.
62
 *                                      Ack spit, Linus how did you allow that
63
 *                                      one in...
64
 *              Jes Sorensen    :       Make FDDI work again in 2.1.x and
65
 *                                      clean up the APFDDI & gen. FDDI bits.
66
 *              Alexey Kuznetsov:       new arp state machine;
67
 *                                      now it is in net/core/neighbour.c.
68
 *              Krzysztof Halasa:       Added Frame Relay ARP support.
69
 *              Shmulik Hen:            Split arp_send to arp_create and
70
 *                                      arp_xmit so intermediate drivers like
71
 *                                      bonding can change the skb before
72
 *                                      sending (e.g. insert 8021q tag).
73
 */
74
 
75
#include <linux/types.h>
76
#include <linux/string.h>
77
#include <linux/kernel.h>
78
#include <linux/sched.h>
79
#include <linux/config.h>
80
#include <linux/socket.h>
81
#include <linux/sockios.h>
82
#include <linux/errno.h>
83
#include <linux/in.h>
84
#include <linux/mm.h>
85
#include <linux/inet.h>
86
#include <linux/netdevice.h>
87
#include <linux/etherdevice.h>
88
#include <linux/fddidevice.h>
89
#include <linux/if_arp.h>
90
#include <linux/trdevice.h>
91
#include <linux/skbuff.h>
92
#include <linux/proc_fs.h>
93
#include <linux/stat.h>
94
#include <linux/init.h>
95
#ifdef CONFIG_SYSCTL
96
#include <linux/sysctl.h>
97
#endif
98
 
99
#include <net/ip.h>
100
#include <net/icmp.h>
101
#include <net/route.h>
102
#include <net/protocol.h>
103
#include <net/tcp.h>
104
#include <net/sock.h>
105
#include <net/arp.h>
106
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
107
#include <net/ax25.h>
108
#if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
109
#include <net/netrom.h>
110
#endif
111
#endif
112
#if defined(CONFIG_ATM_CLIP) || defined(CONFIG_ATM_CLIP_MODULE)
113
#include <net/atmclip.h>
114
struct neigh_table *clip_tbl_hook;
115
#endif
116
 
117
#include <asm/system.h>
118
#include <asm/uaccess.h>
119
 
120
#include <linux/netfilter_arp.h>
121
 
122
/*
123
 *      Interface to generic neighbour cache.
124
 */
125
static u32 arp_hash(const void *pkey, const struct net_device *dev);
126
static int arp_constructor(struct neighbour *neigh);
127
static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb);
128
static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb);
129
static void parp_redo(struct sk_buff *skb);
130
 
131
static struct neigh_ops arp_generic_ops = {
132
        family:                 AF_INET,
133
        solicit:                arp_solicit,
134
        error_report:           arp_error_report,
135
        output:                 neigh_resolve_output,
136
        connected_output:       neigh_connected_output,
137
        hh_output:              dev_queue_xmit,
138
        queue_xmit:             dev_queue_xmit,
139
};
140
 
141
static struct neigh_ops arp_hh_ops = {
142
        family:                 AF_INET,
143
        solicit:                arp_solicit,
144
        error_report:           arp_error_report,
145
        output:                 neigh_resolve_output,
146
        connected_output:       neigh_resolve_output,
147
        hh_output:              dev_queue_xmit,
148
        queue_xmit:             dev_queue_xmit,
149
};
150
 
151
static struct neigh_ops arp_direct_ops = {
152
        family:                 AF_INET,
153
        output:                 dev_queue_xmit,
154
        connected_output:       dev_queue_xmit,
155
        hh_output:              dev_queue_xmit,
156
        queue_xmit:             dev_queue_xmit,
157
};
158
 
159
struct neigh_ops arp_broken_ops = {
160
        family:                 AF_INET,
161
        solicit:                arp_solicit,
162
        error_report:           arp_error_report,
163
        output:                 neigh_compat_output,
164
        connected_output:       neigh_compat_output,
165
        hh_output:              dev_queue_xmit,
166
        queue_xmit:             dev_queue_xmit,
167
};
168
 
169
struct neigh_table arp_tbl = {
170
        family:         AF_INET,
171
        entry_size:     sizeof(struct neighbour) + 4,
172
        key_len:        4,
173
        hash:           arp_hash,
174
        constructor:    arp_constructor,
175
        proxy_redo:     parp_redo,
176
        id:             "arp_cache",
177
        parms: {
178
                tbl:                    &arp_tbl,
179
                base_reachable_time:    30 * HZ,
180
                retrans_time:           1 * HZ,
181
                gc_staletime:           60 * HZ,
182
                reachable_time:         30 * HZ,
183
                delay_probe_time:       5 * HZ,
184
                queue_len:              3,
185
                ucast_probes:           3,
186
                mcast_probes:           3,
187
                anycast_delay:          1 * HZ,
188
                proxy_delay:            (8 * HZ) / 10,
189
                proxy_qlen:             64,
190
                locktime:               1 * HZ,
191
        },
192
        gc_interval:    30 * HZ,
193
        gc_thresh1:     128,
194
        gc_thresh2:     512,
195
        gc_thresh3:     1024,
196
};
197
 
198
int arp_mc_map(u32 addr, u8 *haddr, struct net_device *dev, int dir)
199
{
200
        switch (dev->type) {
201
        case ARPHRD_ETHER:
202
        case ARPHRD_FDDI:
203
        case ARPHRD_IEEE802:
204
                ip_eth_mc_map(addr, haddr);
205
                return 0;
206
        case ARPHRD_IEEE802_TR:
207
                ip_tr_mc_map(addr, haddr);
208
                return 0;
209
        default:
210
                if (dir) {
211
                        memcpy(haddr, dev->broadcast, dev->addr_len);
212
                        return 0;
213
                }
214
        }
215
        return -EINVAL;
216
}
217
 
218
 
219
static u32 arp_hash(const void *pkey, const struct net_device *dev)
220
{
221
        u32 hash_val;
222
 
223
        hash_val = *(u32*)pkey;
224
        hash_val ^= (hash_val>>16);
225
        hash_val ^= hash_val>>8;
226
        hash_val ^= hash_val>>3;
227
        hash_val = (hash_val^dev->ifindex)&NEIGH_HASHMASK;
228
 
229
        return hash_val;
230
}
231
 
232
static int arp_constructor(struct neighbour *neigh)
233
{
234
        u32 addr = *(u32*)neigh->primary_key;
235
        struct net_device *dev = neigh->dev;
236
        struct in_device *in_dev = in_dev_get(dev);
237
 
238
        if (in_dev == NULL)
239
                return -EINVAL;
240
 
241
        neigh->type = inet_addr_type(addr);
242
        if (in_dev->arp_parms)
243
                neigh->parms = in_dev->arp_parms;
244
 
245
        in_dev_put(in_dev);
246
 
247
        if (dev->hard_header == NULL) {
248
                neigh->nud_state = NUD_NOARP;
249
                neigh->ops = &arp_direct_ops;
250
                neigh->output = neigh->ops->queue_xmit;
251
        } else {
252
                /* Good devices (checked by reading texts, but only Ethernet is
253
                   tested)
254
 
255
                   ARPHRD_ETHER: (ethernet, apfddi)
256
                   ARPHRD_FDDI: (fddi)
257
                   ARPHRD_IEEE802: (tr)
258
                   ARPHRD_METRICOM: (strip)
259
                   ARPHRD_ARCNET:
260
                   etc. etc. etc.
261
 
262
                   ARPHRD_IPDDP will also work, if author repairs it.
263
                   I did not it, because this driver does not work even
264
                   in old paradigm.
265
                 */
266
 
267
#if 1
268
                /* So... these "amateur" devices are hopeless.
269
                   The only thing, that I can say now:
270
                   It is very sad that we need to keep ugly obsolete
271
                   code to make them happy.
272
 
273
                   They should be moved to more reasonable state, now
274
                   they use rebuild_header INSTEAD OF hard_start_xmit!!!
275
                   Besides that, they are sort of out of date
276
                   (a lot of redundant clones/copies, useless in 2.1),
277
                   I wonder why people believe that they work.
278
                 */
279
                switch (dev->type) {
280
                default:
281
                        break;
282
                case ARPHRD_ROSE:
283
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
284
                case ARPHRD_AX25:
285
#if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
286
                case ARPHRD_NETROM:
287
#endif
288
                        neigh->ops = &arp_broken_ops;
289
                        neigh->output = neigh->ops->output;
290
                        return 0;
291
#endif
292
                ;}
293
#endif
294
                if (neigh->type == RTN_MULTICAST) {
295
                        neigh->nud_state = NUD_NOARP;
296
                        arp_mc_map(addr, neigh->ha, dev, 1);
297
                } else if (dev->flags&(IFF_NOARP|IFF_LOOPBACK)) {
298
                        neigh->nud_state = NUD_NOARP;
299
                        memcpy(neigh->ha, dev->dev_addr, dev->addr_len);
300
                } else if (neigh->type == RTN_BROADCAST || dev->flags&IFF_POINTOPOINT) {
301
                        neigh->nud_state = NUD_NOARP;
302
                        memcpy(neigh->ha, dev->broadcast, dev->addr_len);
303
                }
304
                if (dev->hard_header_cache)
305
                        neigh->ops = &arp_hh_ops;
306
                else
307
                        neigh->ops = &arp_generic_ops;
308
                if (neigh->nud_state&NUD_VALID)
309
                        neigh->output = neigh->ops->connected_output;
310
                else
311
                        neigh->output = neigh->ops->output;
312
        }
313
        return 0;
314
}
315
 
316
static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb)
317
{
318
        dst_link_failure(skb);
319
        kfree_skb(skb);
320
}
321
 
322
static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
323
{
324
        u32 saddr = 0;
325
        u8  *dst_ha = NULL;
326
        struct net_device *dev = neigh->dev;
327
        u32 target = *(u32*)neigh->primary_key;
328
        int probes = atomic_read(&neigh->probes);
329
        struct in_device *in_dev = in_dev_get(dev);
330
 
331
        if (!in_dev)
332
                return;
333
 
334
        switch (IN_DEV_ARP_ANNOUNCE(in_dev)) {
335
        default:
336
        case 0:          /* By default announce any local IP */
337
                if (skb && inet_addr_type(skb->nh.iph->saddr) == RTN_LOCAL)
338
                        saddr = skb->nh.iph->saddr;
339
                break;
340
        case 1:         /* Restrict announcements of saddr in same subnet */
341
                if (!skb)
342
                        break;
343
                saddr = skb->nh.iph->saddr;
344
                if (inet_addr_type(saddr) == RTN_LOCAL) {
345
                        /* saddr should be known to target */
346
                        if (inet_addr_onlink(in_dev, target, saddr))
347
                                break;
348
                }
349
                saddr = 0;
350
                break;
351
        case 2:         /* Avoid secondary IPs, get a primary/preferred one */
352
                break;
353
        }
354
 
355
        if (in_dev)
356
                in_dev_put(in_dev);
357
        if (!saddr)
358
                saddr = inet_select_addr(dev, target, RT_SCOPE_LINK);
359
 
360
        if ((probes -= neigh->parms->ucast_probes) < 0) {
361
                if (!(neigh->nud_state&NUD_VALID))
362
                        printk(KERN_DEBUG "trying to ucast probe in NUD_INVALID\n");
363
                dst_ha = neigh->ha;
364
                read_lock_bh(&neigh->lock);
365
        } else if ((probes -= neigh->parms->app_probes) < 0) {
366
#ifdef CONFIG_ARPD
367
                neigh_app_ns(neigh);
368
#endif
369
                return;
370
        }
371
 
372
        arp_send(ARPOP_REQUEST, ETH_P_ARP, target, dev, saddr,
373
                 dst_ha, dev->dev_addr, NULL);
374
        if (dst_ha)
375
                read_unlock_bh(&neigh->lock);
376
}
377
 
378
static int arp_ignore(struct in_device *in_dev, struct net_device *dev,
379
                      u32 sip, u32 tip)
380
{
381
        int scope;
382
 
383
        switch (IN_DEV_ARP_IGNORE(in_dev)) {
384
        case 0:  /* Reply, the tip is already validated */
385
                return 0;
386
        case 1: /* Reply only if tip is configured on the incoming interface */
387
                sip = 0;
388
                scope = RT_SCOPE_HOST;
389
                break;
390
        case 2: /*
391
                 * Reply only if tip is configured on the incoming interface
392
                 * and is in same subnet as sip
393
                 */
394
                scope = RT_SCOPE_HOST;
395
                break;
396
        case 3: /* Do not reply for scope host addresses */
397
                sip = 0;
398
                scope = RT_SCOPE_LINK;
399
                dev = NULL;
400
                break;
401
        case 4: /* Reserved */
402
        case 5:
403
        case 6:
404
        case 7:
405
                return 0;
406
        case 8: /* Do not reply */
407
                return 1;
408
        default:
409
                return 0;
410
        }
411
        return !inet_confirm_addr(dev, sip, tip, scope);
412
}
413
 
414
static int arp_filter(__u32 sip, __u32 tip, struct net_device *dev)
415
{
416
        struct rtable *rt;
417
        int flag = 0;
418
        /*unsigned long now; */
419
 
420
        if (ip_route_output(&rt, sip, tip, 0, 0) < 0)
421
                return 1;
422
        if (rt->u.dst.dev != dev) {
423
                NET_INC_STATS_BH(ArpFilter);
424
                flag = 1;
425
        }
426
        ip_rt_put(rt);
427
        return flag;
428
}
429
 
430
/* OBSOLETE FUNCTIONS */
431
 
432
/*
433
 *      Find an arp mapping in the cache. If not found, post a request.
434
 *
435
 *      It is very UGLY routine: it DOES NOT use skb->dst->neighbour,
436
 *      even if it exists. It is supposed that skb->dev was mangled
437
 *      by a virtual device (eql, shaper). Nobody but broken devices
438
 *      is allowed to use this function, it is scheduled to be removed. --ANK
439
 */
440
 
441
static int arp_set_predefined(int addr_hint, unsigned char * haddr, u32 paddr, struct net_device * dev)
442
{
443
        switch (addr_hint) {
444
        case RTN_LOCAL:
445
                printk(KERN_DEBUG "ARP: arp called for own IP address\n");
446
                memcpy(haddr, dev->dev_addr, dev->addr_len);
447
                return 1;
448
        case RTN_MULTICAST:
449
                arp_mc_map(paddr, haddr, dev, 1);
450
                return 1;
451
        case RTN_BROADCAST:
452
                memcpy(haddr, dev->broadcast, dev->addr_len);
453
                return 1;
454
        }
455
        return 0;
456
}
457
 
458
 
459
int arp_find(unsigned char *haddr, struct sk_buff *skb)
460
{
461
        struct net_device *dev = skb->dev;
462
        u32 paddr;
463
        struct neighbour *n;
464
 
465
        if (!skb->dst) {
466
                printk(KERN_DEBUG "arp_find is called with dst==NULL\n");
467
                kfree_skb(skb);
468
                return 1;
469
        }
470
 
471
        paddr = ((struct rtable*)skb->dst)->rt_gateway;
472
 
473
        if (arp_set_predefined(inet_addr_type(paddr), haddr, paddr, dev))
474
                return 0;
475
 
476
        n = __neigh_lookup(&arp_tbl, &paddr, dev, 1);
477
 
478
        if (n) {
479
                n->used = jiffies;
480
                if (n->nud_state&NUD_VALID || neigh_event_send(n, skb) == 0) {
481
                        read_lock_bh(&n->lock);
482
                        memcpy(haddr, n->ha, dev->addr_len);
483
                        read_unlock_bh(&n->lock);
484
                        neigh_release(n);
485
                        return 0;
486
                }
487
                neigh_release(n);
488
        } else
489
                kfree_skb(skb);
490
        return 1;
491
}
492
 
493
/* END OF OBSOLETE FUNCTIONS */
494
 
495
int arp_bind_neighbour(struct dst_entry *dst)
496
{
497
        struct net_device *dev = dst->dev;
498
        struct neighbour *n = dst->neighbour;
499
 
500
        if (dev == NULL)
501
                return -EINVAL;
502
        if (n == NULL) {
503
                u32 nexthop = ((struct rtable*)dst)->rt_gateway;
504
                if (dev->flags&(IFF_LOOPBACK|IFF_POINTOPOINT))
505
                        nexthop = 0;
506
                n = __neigh_lookup_errno(
507
#if defined(CONFIG_ATM_CLIP) || defined(CONFIG_ATM_CLIP_MODULE)
508
                    dev->type == ARPHRD_ATM ? clip_tbl_hook :
509
#endif
510
                    &arp_tbl, &nexthop, dev);
511
                if (IS_ERR(n))
512
                        return PTR_ERR(n);
513
                dst->neighbour = n;
514
        }
515
        return 0;
516
}
517
 
518
/*
519
 * Check if we can use proxy ARP for this path
520
 */
521
 
522
static inline int arp_fwd_proxy(struct in_device *in_dev, struct rtable *rt)
523
{
524
        struct in_device *out_dev;
525
        int imi, omi = -1;
526
 
527
        if (!IN_DEV_PROXY_ARP(in_dev))
528
                return 0;
529
 
530
        if ((imi = IN_DEV_MEDIUM_ID(in_dev)) == 0)
531
                return 1;
532
        if (imi == -1)
533
                return 0;
534
 
535
        /* place to check for proxy_arp for routes */
536
 
537
        if ((out_dev = in_dev_get(rt->u.dst.dev)) != NULL) {
538
                omi = IN_DEV_MEDIUM_ID(out_dev);
539
                in_dev_put(out_dev);
540
        }
541
        return (omi != imi && omi != -1);
542
}
543
 
544
/*
545
 *      Interface to link layer: send routine and receive handler.
546
 */
547
 
548
/*
549
 *      Create an arp packet. If (dest_hw == NULL), we create a broadcast
550
 *      message.
551
 */
552
struct sk_buff *arp_create(int type, int ptype, u32 dest_ip,
553
                           struct net_device *dev, u32 src_ip,
554
                           unsigned char *dest_hw, unsigned char *src_hw,
555
                           unsigned char *target_hw)
556
{
557
        struct sk_buff *skb;
558
        struct arphdr *arp;
559
        unsigned char *arp_ptr;
560
 
561
        /*
562
         *      Allocate a buffer
563
         */
564
 
565
        skb = alloc_skb(sizeof(struct arphdr)+ 2*(dev->addr_len+4)
566
                                + dev->hard_header_len + 15, GFP_ATOMIC);
567
        if (skb == NULL)
568
                return NULL;
569
 
570
        skb_reserve(skb, (dev->hard_header_len+15)&~15);
571
        skb->nh.raw = skb->data;
572
        arp = (struct arphdr *) skb_put(skb,sizeof(struct arphdr) + 2*(dev->addr_len+4));
573
        skb->dev = dev;
574
        skb->protocol = htons (ETH_P_ARP);
575
        if (src_hw == NULL)
576
                src_hw = dev->dev_addr;
577
        if (dest_hw == NULL)
578
                dest_hw = dev->broadcast;
579
 
580
        /*
581
         *      Fill the device header for the ARP frame
582
         */
583
        if (dev->hard_header &&
584
            dev->hard_header(skb,dev,ptype,dest_hw,src_hw,skb->len) < 0)
585
                goto out;
586
 
587
        /*
588
         * Fill out the arp protocol part.
589
         *
590
         * The arp hardware type should match the device type, except for FDDI,
591
         * which (according to RFC 1390) should always equal 1 (Ethernet).
592
         */
593
        /*
594
         *      Exceptions everywhere. AX.25 uses the AX.25 PID value not the
595
         *      DIX code for the protocol. Make these device structure fields.
596
         */
597
        switch (dev->type) {
598
        default:
599
                arp->ar_hrd = htons(dev->type);
600
                arp->ar_pro = htons(ETH_P_IP);
601
                break;
602
 
603
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
604
        case ARPHRD_AX25:
605
                arp->ar_hrd = htons(ARPHRD_AX25);
606
                arp->ar_pro = htons(AX25_P_IP);
607
                break;
608
 
609
#if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
610
        case ARPHRD_NETROM:
611
                arp->ar_hrd = htons(ARPHRD_NETROM);
612
                arp->ar_pro = htons(AX25_P_IP);
613
                break;
614
#endif
615
#endif
616
 
617
#ifdef CONFIG_FDDI
618
        case ARPHRD_FDDI:
619
                arp->ar_hrd = htons(ARPHRD_ETHER);
620
                arp->ar_pro = htons(ETH_P_IP);
621
                break;
622
#endif
623
#ifdef CONFIG_TR
624
        case ARPHRD_IEEE802_TR:
625
                arp->ar_hrd = htons(ARPHRD_IEEE802);
626
                arp->ar_pro = htons(ETH_P_IP);
627
                break;
628
#endif
629
        }
630
 
631
        arp->ar_hln = dev->addr_len;
632
        arp->ar_pln = 4;
633
        arp->ar_op = htons(type);
634
 
635
        arp_ptr=(unsigned char *)(arp+1);
636
 
637
        memcpy(arp_ptr, src_hw, dev->addr_len);
638
        arp_ptr+=dev->addr_len;
639
        memcpy(arp_ptr, &src_ip,4);
640
        arp_ptr+=4;
641
        if (target_hw != NULL)
642
                memcpy(arp_ptr, target_hw, dev->addr_len);
643
        else
644
                memset(arp_ptr, 0, dev->addr_len);
645
        arp_ptr+=dev->addr_len;
646
        memcpy(arp_ptr, &dest_ip, 4);
647
 
648
        return skb;
649
 
650
out:
651
        kfree_skb(skb);
652
        return NULL;
653
}
654
 
655
/*
656
 *      Send an arp packet.
657
 */
658
void arp_xmit(struct sk_buff *skb)
659
{
660
        /* Send it off, maybe filter it using firewalling first.  */
661
        NF_HOOK(NF_ARP, NF_ARP_OUT, skb, NULL, skb->dev, dev_queue_xmit);
662
}
663
 
664
/*
665
 *      Create and send an arp packet.
666
 */
667
void arp_send(int type, int ptype, u32 dest_ip,
668
              struct net_device *dev, u32 src_ip,
669
              unsigned char *dest_hw, unsigned char *src_hw,
670
              unsigned char *target_hw)
671
{
672
        struct sk_buff *skb;
673
 
674
        /*
675
         *      No arp on this interface.
676
         */
677
 
678
        if (dev->flags&IFF_NOARP)
679
                return;
680
 
681
        skb = arp_create(type, ptype, dest_ip, dev, src_ip,
682
                         dest_hw, src_hw, target_hw);
683
        if (skb == NULL) {
684
                return;
685
        }
686
 
687
        arp_xmit(skb);
688
}
689
 
690
static void parp_redo(struct sk_buff *skb)
691
{
692
        arp_rcv(skb, skb->dev, NULL);
693
}
694
 
695
/*
696
 *      Process an arp request.
697
 */
698
 
699
int arp_process(struct sk_buff *skb)
700
{
701
        struct net_device *dev = skb->dev;
702
        struct in_device *in_dev = in_dev_get(dev);
703
        struct arphdr *arp;
704
        unsigned char *arp_ptr;
705
        struct rtable *rt;
706
        unsigned char *sha, *tha;
707
        u32 sip, tip;
708
        u16 dev_type = dev->type;
709
        int addr_type;
710
        struct neighbour *n;
711
 
712
        /* arp_rcv below verifies the ARP header, verifies the device
713
         * is ARP'able, and linearizes the SKB (if needed).
714
         */
715
 
716
        if (in_dev == NULL)
717
                goto out;
718
 
719
        arp = skb->nh.arph;
720
        arp_ptr= (unsigned char *)(arp+1);
721
 
722
        switch (dev_type) {
723
        default:
724
                if (arp->ar_pro != htons(ETH_P_IP))
725
                        goto out;
726
                if (htons(dev_type) != arp->ar_hrd)
727
                        goto out;
728
                break;
729
#ifdef CONFIG_NET_ETHERNET
730
        case ARPHRD_ETHER:
731
                /*
732
                 * ETHERNET devices will accept ARP hardware types of either
733
                 * 1 (Ethernet) or 6 (IEEE 802.2).
734
                 */
735
                if (arp->ar_hrd != htons(ARPHRD_ETHER) &&
736
                    arp->ar_hrd != htons(ARPHRD_IEEE802))
737
                        goto out;
738
                if (arp->ar_pro != htons(ETH_P_IP))
739
                        goto out;
740
                break;
741
#endif
742
#ifdef CONFIG_TR
743
        case ARPHRD_IEEE802_TR:
744
                /*
745
                 * Token ring devices will accept ARP hardware types of either
746
                 * 1 (Ethernet) or 6 (IEEE 802.2).
747
                 */
748
                if (arp->ar_hrd != htons(ARPHRD_ETHER) &&
749
                    arp->ar_hrd != htons(ARPHRD_IEEE802))
750
                        goto out;
751
                if (arp->ar_pro != htons(ETH_P_IP))
752
                        goto out;
753
                break;
754
#endif
755
#ifdef CONFIG_FDDI
756
        case ARPHRD_FDDI:
757
                /*
758
                 * According to RFC 1390, FDDI devices should accept ARP hardware types
759
                 * of 1 (Ethernet).  However, to be more robust, we'll accept hardware
760
                 * types of either 1 (Ethernet) or 6 (IEEE 802.2).
761
                 */
762
                if (arp->ar_hrd != htons(ARPHRD_ETHER) &&
763
                    arp->ar_hrd != htons(ARPHRD_IEEE802))
764
                        goto out;
765
                if (arp->ar_pro != htons(ETH_P_IP))
766
                        goto out;
767
                break;
768
#endif
769
#ifdef CONFIG_NET_FC
770
        case ARPHRD_IEEE802:
771
                /*
772
                 * According to RFC 2625, Fibre Channel devices (which are IEEE
773
                 * 802 devices) should accept ARP hardware types of 6 (IEEE 802)
774
                 * and 1 (Ethernet).
775
                 */
776
                if (arp->ar_hrd != htons(ARPHRD_ETHER) &&
777
                    arp->ar_hrd != htons(ARPHRD_IEEE802))
778
                        goto out;
779
                if (arp->ar_pro != htons(ETH_P_IP))
780
                        goto out;
781
                break;
782
#endif
783
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
784
        case ARPHRD_AX25:
785
                if (arp->ar_pro != htons(AX25_P_IP))
786
                        goto out;
787
                if (arp->ar_hrd != htons(ARPHRD_AX25))
788
                        goto out;
789
                break;
790
#if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
791
        case ARPHRD_NETROM:
792
                if (arp->ar_pro != htons(AX25_P_IP))
793
                        goto out;
794
                if (arp->ar_hrd != htons(ARPHRD_NETROM))
795
                        goto out;
796
                break;
797
#endif
798
#endif
799
        }
800
 
801
        /* Understand only these message types */
802
 
803
        if (arp->ar_op != htons(ARPOP_REPLY) &&
804
            arp->ar_op != htons(ARPOP_REQUEST))
805
                goto out;
806
 
807
/*
808
 *      Extract fields
809
 */
810
        sha=arp_ptr;
811
        arp_ptr += dev->addr_len;
812
        memcpy(&sip, arp_ptr, 4);
813
        arp_ptr += 4;
814
        tha=arp_ptr;
815
        arp_ptr += dev->addr_len;
816
        memcpy(&tip, arp_ptr, 4);
817
/*
818
 *      Check for bad requests for 127.x.x.x and requests for multicast
819
 *      addresses.  If this is one such, delete it.
820
 */
821
        if (LOOPBACK(tip) || MULTICAST(tip))
822
                goto out;
823
 
824
/*
825
 *     Special case: We must set Frame Relay source Q.922 address
826
 */
827
        if (dev_type == ARPHRD_DLCI)
828
                sha = dev->broadcast;
829
 
830
/*
831
 *  Process entry.  The idea here is we want to send a reply if it is a
832
 *  request for us or if it is a request for someone else that we hold
833
 *  a proxy for.  We want to add an entry to our cache if it is a reply
834
 *  to us or if it is a request for our address.
835
 *  (The assumption for this last is that if someone is requesting our
836
 *  address, they are probably intending to talk to us, so it saves time
837
 *  if we cache their address.  Their address is also probably not in
838
 *  our cache, since ours is not in their cache.)
839
 *
840
 *  Putting this another way, we only care about replies if they are to
841
 *  us, in which case we add them to the cache.  For requests, we care
842
 *  about those for us and those for our proxies.  We reply to both,
843
 *  and in the case of requests for us we add the requester to the arp
844
 *  cache.
845
 */
846
 
847
        /* Special case: IPv4 duplicate address detection packet (RFC2131) */
848
        if (sip == 0) {
849
                if (arp->ar_op == htons(ARPOP_REQUEST) &&
850
                    inet_addr_type(tip) == RTN_LOCAL &&
851
                    !arp_ignore(in_dev,dev,sip,tip))
852
                        arp_send(ARPOP_REPLY,ETH_P_ARP,tip,dev,tip,sha,dev->dev_addr,dev->dev_addr);
853
                goto out;
854
        }
855
 
856
        if (arp->ar_op == htons(ARPOP_REQUEST) &&
857
            ip_route_input(skb, tip, sip, 0, dev) == 0) {
858
 
859
                rt = (struct rtable*)skb->dst;
860
                addr_type = rt->rt_type;
861
 
862
                if (addr_type == RTN_LOCAL) {
863
                        n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
864
                        if (n) {
865
                                int dont_send = 0;
866
 
867
                                if (!dont_send)
868
                                        dont_send |= arp_ignore(in_dev,dev,sip,tip);
869
                                if (!dont_send && IN_DEV_ARPFILTER(in_dev))
870
                                        dont_send |= arp_filter(sip,tip,dev);
871
                                if (!dont_send)
872
                                        arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha);
873
 
874
                                neigh_release(n);
875
                        }
876
                        goto out;
877
                } else if (IN_DEV_FORWARD(in_dev)) {
878
                        if ((rt->rt_flags&RTCF_DNAT) ||
879
                            (addr_type == RTN_UNICAST  && rt->u.dst.dev != dev &&
880
                             (arp_fwd_proxy(in_dev, rt) || pneigh_lookup(&arp_tbl, &tip, dev, 0)))) {
881
                                n = neigh_event_ns(&arp_tbl, sha, &sip, dev);
882
                                if (n)
883
                                        neigh_release(n);
884
 
885
                                if (skb->stamp.tv_sec == 0 ||
886
                                    skb->pkt_type == PACKET_HOST ||
887
                                    in_dev->arp_parms->proxy_delay == 0) {
888
                                        arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha);
889
                                } else {
890
                                        pneigh_enqueue(&arp_tbl, in_dev->arp_parms, skb);
891
                                        in_dev_put(in_dev);
892
                                        return 0;
893
                                }
894
                                goto out;
895
                        }
896
                }
897
        }
898
 
899
        /* Update our ARP tables */
900
 
901
        n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
902
 
903
#ifdef CONFIG_IP_ACCEPT_UNSOLICITED_ARP
904
        /* Unsolicited ARP is not accepted by default.
905
           It is possible, that this option should be enabled for some
906
           devices (strip is candidate)
907
         */
908
        if (n == NULL &&
909
            arp->ar_op == htons(ARPOP_REPLY) &&
910
            inet_addr_type(sip) == RTN_UNICAST)
911
                n = __neigh_lookup(&arp_tbl, &sip, dev, -1);
912
#endif
913
 
914
        if (n) {
915
                int state = NUD_REACHABLE;
916
                int override = 0;
917
 
918
                /* If several different ARP replies follows back-to-back,
919
                   use the FIRST one. It is possible, if several proxy
920
                   agents are active. Taking the first reply prevents
921
                   arp trashing and chooses the fastest router.
922
                 */
923
                if (jiffies - n->updated >= n->parms->locktime)
924
                        override = 1;
925
 
926
                /* Broadcast replies and request packets
927
                   do not assert neighbour reachability.
928
                 */
929
                if (arp->ar_op != htons(ARPOP_REPLY) ||
930
                    skb->pkt_type != PACKET_HOST)
931
                        state = NUD_STALE;
932
                neigh_update(n, sha, state, override, 1);
933
                neigh_release(n);
934
        }
935
 
936
out:
937
        if (in_dev)
938
                in_dev_put(in_dev);
939
        kfree_skb(skb);
940
        return 0;
941
}
942
 
943
 
944
/*
945
 *      Receive an arp request from the device layer.
946
 */
947
 
948
int arp_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
949
{
950
        struct arphdr *arp;
951
 
952
        /* ARP header, plus 2 device addresses, plus 2 IP addresses.  */
953
        if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
954
                                 (2 * dev->addr_len) +
955
                                 (2 * sizeof(u32)))))
956
                goto freeskb;
957
 
958
        arp = skb->nh.arph;
959
        if (arp->ar_hln != dev->addr_len ||
960
            dev->flags & IFF_NOARP ||
961
            skb->pkt_type == PACKET_OTHERHOST ||
962
            skb->pkt_type == PACKET_LOOPBACK ||
963
            arp->ar_pln != 4)
964
                goto freeskb;
965
 
966
        if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
967
                goto out_of_mem;
968
 
969
        return NF_HOOK(NF_ARP, NF_ARP_IN, skb, dev, NULL, arp_process);
970
 
971
freeskb:
972
        kfree_skb(skb);
973
out_of_mem:
974
        return 0;
975
}
976
 
977
/*
978
 *      User level interface (ioctl, /proc)
979
 */
980
 
981
/*
982
 *      Set (create) an ARP cache entry.
983
 */
984
 
985
int arp_req_set(struct arpreq *r, struct net_device * dev)
986
{
987
        u32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
988
        struct neighbour *neigh;
989
        int err;
990
 
991
        if (r->arp_flags&ATF_PUBL) {
992
                u32 mask = ((struct sockaddr_in *) &r->arp_netmask)->sin_addr.s_addr;
993
                if (mask && mask != 0xFFFFFFFF)
994
                        return -EINVAL;
995
                if (!dev && (r->arp_flags & ATF_COM)) {
996
                        dev = dev_getbyhwaddr(r->arp_ha.sa_family, r->arp_ha.sa_data);
997
                        if (!dev)
998
                                return -ENODEV;
999
                }
1000
                if (mask) {
1001
                        if (pneigh_lookup(&arp_tbl, &ip, dev, 1) == NULL)
1002
                                return -ENOBUFS;
1003
                        return 0;
1004
                }
1005
                if (dev == NULL) {
1006
                        ipv4_devconf.proxy_arp = 1;
1007
                        return 0;
1008
                }
1009
                if (__in_dev_get(dev)) {
1010
                        __in_dev_get(dev)->cnf.proxy_arp = 1;
1011
                        return 0;
1012
                }
1013
                return -ENXIO;
1014
        }
1015
 
1016
        if (r->arp_flags & ATF_PERM)
1017
                r->arp_flags |= ATF_COM;
1018
        if (dev == NULL) {
1019
                struct rtable * rt;
1020
                if ((err = ip_route_output(&rt, ip, 0, RTO_ONLINK, 0)) != 0)
1021
                        return err;
1022
                dev = rt->u.dst.dev;
1023
                ip_rt_put(rt);
1024
                if (!dev)
1025
                        return -EINVAL;
1026
        }
1027
        if (r->arp_ha.sa_family != dev->type)
1028
                return -EINVAL;
1029
 
1030
        neigh = __neigh_lookup_errno(&arp_tbl, &ip, dev);
1031
        err = PTR_ERR(neigh);
1032
        if (!IS_ERR(neigh)) {
1033
                unsigned state = NUD_STALE;
1034
                if (r->arp_flags & ATF_PERM)
1035
                        state = NUD_PERMANENT;
1036
                err = neigh_update(neigh, (r->arp_flags&ATF_COM) ?
1037
                                   r->arp_ha.sa_data : NULL, state, 1, 0);
1038
                neigh_release(neigh);
1039
        }
1040
        return err;
1041
}
1042
 
1043
static unsigned arp_state_to_flags(struct neighbour *neigh)
1044
{
1045
        unsigned flags = 0;
1046
        if (neigh->nud_state&NUD_PERMANENT)
1047
                flags = ATF_PERM|ATF_COM;
1048
        else if (neigh->nud_state&NUD_VALID)
1049
                flags = ATF_COM;
1050
        return flags;
1051
}
1052
 
1053
/*
1054
 *      Get an ARP cache entry.
1055
 */
1056
 
1057
static int arp_req_get(struct arpreq *r, struct net_device *dev)
1058
{
1059
        u32 ip = ((struct sockaddr_in *) &r->arp_pa)->sin_addr.s_addr;
1060
        struct neighbour *neigh;
1061
        int err = -ENXIO;
1062
 
1063
        neigh = neigh_lookup(&arp_tbl, &ip, dev);
1064
        if (neigh) {
1065
                read_lock_bh(&neigh->lock);
1066
                memcpy(r->arp_ha.sa_data, neigh->ha, dev->addr_len);
1067
                r->arp_flags = arp_state_to_flags(neigh);
1068
                read_unlock_bh(&neigh->lock);
1069
                r->arp_ha.sa_family = dev->type;
1070
                strncpy(r->arp_dev, dev->name, sizeof(r->arp_dev));
1071
                neigh_release(neigh);
1072
                err = 0;
1073
        }
1074
        return err;
1075
}
1076
 
1077
int arp_req_delete(struct arpreq *r, struct net_device * dev)
1078
{
1079
        int err;
1080
        u32 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1081
        struct neighbour *neigh;
1082
 
1083
        if (r->arp_flags & ATF_PUBL) {
1084
                u32 mask = ((struct sockaddr_in *) &r->arp_netmask)->sin_addr.s_addr;
1085
                if (mask == 0xFFFFFFFF)
1086
                        return pneigh_delete(&arp_tbl, &ip, dev);
1087
                if (mask == 0) {
1088
                        if (dev == NULL) {
1089
                                ipv4_devconf.proxy_arp = 0;
1090
                                return 0;
1091
                        }
1092
                        if (__in_dev_get(dev)) {
1093
                                __in_dev_get(dev)->cnf.proxy_arp = 0;
1094
                                return 0;
1095
                        }
1096
                        return -ENXIO;
1097
                }
1098
                return -EINVAL;
1099
        }
1100
 
1101
        if (dev == NULL) {
1102
                struct rtable * rt;
1103
                if ((err = ip_route_output(&rt, ip, 0, RTO_ONLINK, 0)) != 0)
1104
                        return err;
1105
                dev = rt->u.dst.dev;
1106
                ip_rt_put(rt);
1107
                if (!dev)
1108
                        return -EINVAL;
1109
        }
1110
        err = -ENXIO;
1111
        neigh = neigh_lookup(&arp_tbl, &ip, dev);
1112
        if (neigh) {
1113
                if (neigh->nud_state&~NUD_NOARP)
1114
                        err = neigh_update(neigh, NULL, NUD_FAILED, 1, 0);
1115
                neigh_release(neigh);
1116
        }
1117
        return err;
1118
}
1119
 
1120
/*
1121
 *      Handle an ARP layer I/O control request.
1122
 */
1123
 
1124
int arp_ioctl(unsigned int cmd, void *arg)
1125
{
1126
        int err;
1127
        struct arpreq r;
1128
        struct net_device * dev = NULL;
1129
 
1130
        switch(cmd) {
1131
                case SIOCDARP:
1132
                case SIOCSARP:
1133
                        if (!capable(CAP_NET_ADMIN))
1134
                                return -EPERM;
1135
                case SIOCGARP:
1136
                        err = copy_from_user(&r, arg, sizeof(struct arpreq));
1137
                        if (err)
1138
                                return -EFAULT;
1139
                        break;
1140
                default:
1141
                        return -EINVAL;
1142
        }
1143
 
1144
        if (r.arp_pa.sa_family != AF_INET)
1145
                return -EPFNOSUPPORT;
1146
 
1147
        if (!(r.arp_flags & ATF_PUBL) &&
1148
            (r.arp_flags & (ATF_NETMASK|ATF_DONTPUB)))
1149
                return -EINVAL;
1150
        if (!(r.arp_flags & ATF_NETMASK))
1151
                ((struct sockaddr_in *)&r.arp_netmask)->sin_addr.s_addr=htonl(0xFFFFFFFFUL);
1152
 
1153
        rtnl_lock();
1154
        if (r.arp_dev[0]) {
1155
                err = -ENODEV;
1156
                if ((dev = __dev_get_by_name(r.arp_dev)) == NULL)
1157
                        goto out;
1158
 
1159
                /* Mmmm... It is wrong... ARPHRD_NETROM==0 */
1160
                if (!r.arp_ha.sa_family)
1161
                        r.arp_ha.sa_family = dev->type;
1162
                err = -EINVAL;
1163
                if ((r.arp_flags & ATF_COM) && r.arp_ha.sa_family != dev->type)
1164
                        goto out;
1165
        } else if (cmd == SIOCGARP) {
1166
                err = -ENODEV;
1167
                goto out;
1168
        }
1169
 
1170
        switch(cmd) {
1171
        case SIOCDARP:
1172
                err = arp_req_delete(&r, dev);
1173
                break;
1174
        case SIOCSARP:
1175
                err = arp_req_set(&r, dev);
1176
                break;
1177
        case SIOCGARP:
1178
                err = arp_req_get(&r, dev);
1179
                if (!err && copy_to_user(arg, &r, sizeof(r)))
1180
                        err = -EFAULT;
1181
                break;
1182
        }
1183
out:
1184
        rtnl_unlock();
1185
        return err;
1186
}
1187
 
1188
/*
1189
 *      Write the contents of the ARP cache to a PROCfs file.
1190
 */
1191
#ifndef CONFIG_PROC_FS
1192
static int arp_get_info(char *buffer, char **start, off_t offset, int length) { return 0; }
1193
#else
1194
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1195
static char *ax2asc2(ax25_address *a, char *buf);
1196
#endif
1197
#define HBUFFERLEN 30
1198
 
1199
static int arp_get_info(char *buffer, char **start, off_t offset, int length)
1200
{
1201
        int len=0;
1202
        off_t pos=0;
1203
        int size;
1204
        char hbuffer[HBUFFERLEN];
1205
        int i,j,k;
1206
        const char hexbuf[] =  "0123456789ABCDEF";
1207
 
1208
        size = sprintf(buffer,"IP address       HW type     Flags       HW address            Mask     Device\n");
1209
 
1210
        pos+=size;
1211
        len+=size;
1212
 
1213
        for(i=0; i<=NEIGH_HASHMASK; i++) {
1214
                struct neighbour *n;
1215
                read_lock_bh(&arp_tbl.lock);
1216
                for (n=arp_tbl.hash_buckets[i]; n; n=n->next) {
1217
                        struct net_device *dev = n->dev;
1218
                        int hatype = dev->type;
1219
 
1220
                        /* Do not confuse users "arp -a" with magic entries */
1221
                        if (!(n->nud_state&~NUD_NOARP))
1222
                                continue;
1223
 
1224
                        read_lock(&n->lock);
1225
 
1226
/*
1227
 *      Convert hardware address to XX:XX:XX:XX ... form.
1228
 */
1229
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1230
                        if (hatype == ARPHRD_AX25 || hatype == ARPHRD_NETROM)
1231
                                ax2asc2((ax25_address *)n->ha, hbuffer);
1232
                        else {
1233
#endif
1234
                        for (k=0,j=0;k<HBUFFERLEN-3 && j<dev->addr_len;j++) {
1235
                                hbuffer[k++]=hexbuf[(n->ha[j]>>4)&15 ];
1236
                                hbuffer[k++]=hexbuf[n->ha[j]&15     ];
1237
                                hbuffer[k++]=':';
1238
                        }
1239
                        hbuffer[--k]=0;
1240
 
1241
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1242
                }
1243
#endif
1244
 
1245
                        {
1246
                                char tbuf[16];
1247
                                sprintf(tbuf, "%u.%u.%u.%u", NIPQUAD(*(u32*)n->primary_key));
1248
                                size = sprintf(buffer+len, "%-16s 0x%-10x0x%-10x%s"
1249
                                                        "     *        %s\n",
1250
                                        tbuf,
1251
                                        hatype,
1252
                                        arp_state_to_flags(n),
1253
                                        hbuffer,
1254
                                        dev->name);
1255
                        }
1256
 
1257
                        read_unlock(&n->lock);
1258
 
1259
                        len += size;
1260
                        pos += size;
1261
 
1262
                        if (pos <= offset)
1263
                                len=0;
1264
                        if (pos >= offset+length) {
1265
                                read_unlock_bh(&arp_tbl.lock);
1266
                                goto done;
1267
                        }
1268
                }
1269
                read_unlock_bh(&arp_tbl.lock);
1270
        }
1271
 
1272
        for (i=0; i<=PNEIGH_HASHMASK; i++) {
1273
                struct pneigh_entry *n;
1274
                for (n=arp_tbl.phash_buckets[i]; n; n=n->next) {
1275
                        struct net_device *dev = n->dev;
1276
                        int hatype = dev ? dev->type : 0;
1277
 
1278
                        {
1279
                                char tbuf[16];
1280
                                sprintf(tbuf, "%u.%u.%u.%u", NIPQUAD(*(u32*)n->key));
1281
                                size = sprintf(buffer+len, "%-16s 0x%-10x0x%-10x%s"
1282
                                                        "     *        %s\n",
1283
                                        tbuf,
1284
                                        hatype,
1285
                                        ATF_PUBL|ATF_PERM,
1286
                                        "00:00:00:00:00:00",
1287
                                        dev ? dev->name : "*");
1288
                        }
1289
 
1290
                        len += size;
1291
                        pos += size;
1292
 
1293
                        if (pos <= offset)
1294
                                len=0;
1295
                        if (pos >= offset+length)
1296
                                goto done;
1297
                }
1298
        }
1299
 
1300
done:
1301
 
1302
        *start = buffer+len-(pos-offset);       /* Start of wanted data */
1303
        len = pos-offset;                       /* Start slop */
1304
        if (len>length)
1305
                len = length;                   /* Ending slop */
1306
        if (len<0)
1307
                len = 0;
1308
        return len;
1309
}
1310
#endif
1311
 
1312
static int arp_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
1313
{
1314
        struct net_device *dev = ptr;
1315
 
1316
        switch (event) {
1317
        case NETDEV_CHANGEADDR:
1318
                neigh_changeaddr(&arp_tbl, dev);
1319
                rt_cache_flush(0);
1320
                break;
1321
        default:
1322
                break;
1323
        }
1324
 
1325
        return NOTIFY_DONE;
1326
}
1327
 
1328
struct notifier_block arp_netdev_notifier = {
1329
        .notifier_call = arp_netdev_event,
1330
};
1331
 
1332
/* Note, that it is not on notifier chain.
1333
   It is necessary, that this routine was called after route cache will be
1334
   flushed.
1335
 */
1336
void arp_ifdown(struct net_device *dev)
1337
{
1338
        neigh_ifdown(&arp_tbl, dev);
1339
}
1340
 
1341
 
1342
/*
1343
 *      Called once on startup.
1344
 */
1345
 
1346
static struct packet_type arp_packet_type = {
1347
        type:   __constant_htons(ETH_P_ARP),
1348
        func:   arp_rcv,
1349
        data:   (void*) 1, /* understand shared skbs */
1350
};
1351
 
1352
void __init arp_init (void)
1353
{
1354
        neigh_table_init(&arp_tbl);
1355
 
1356
        dev_add_pack(&arp_packet_type);
1357
 
1358
        proc_net_create ("arp", 0, arp_get_info);
1359
 
1360
#ifdef CONFIG_SYSCTL
1361
        neigh_sysctl_register(NULL, &arp_tbl.parms, NET_IPV4, NET_IPV4_NEIGH, "ipv4");
1362
#endif
1363
        register_netdevice_notifier(&arp_netdev_notifier);
1364
}
1365
 
1366
 
1367
#ifdef CONFIG_PROC_FS
1368
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
1369
 
1370
/*
1371
 *      ax25 -> ASCII conversion
1372
 */
1373
char *ax2asc2(ax25_address *a, char *buf)
1374
{
1375
        char c, *s;
1376
        int n;
1377
 
1378
        for (n = 0, s = buf; n < 6; n++) {
1379
                c = (a->ax25_call[n] >> 1) & 0x7F;
1380
 
1381
                if (c != ' ') *s++ = c;
1382
        }
1383
 
1384
        *s++ = '-';
1385
 
1386
        if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
1387
                *s++ = '1';
1388
                n -= 10;
1389
        }
1390
 
1391
        *s++ = n + '0';
1392
        *s++ = '\0';
1393
 
1394
        if (*buf == '\0' || *buf == '-')
1395
           return "*";
1396
 
1397
        return buf;
1398
 
1399
}
1400
 
1401
#endif
1402
#endif

powered by: WebSVN 2.1.0

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