1 /* linux/net/inet/arp.c
2 *
3 * Copyright (C) 1994 by Florian La Roche
4 *
5 * This module implements the Address Resolution Protocol ARP (RFC 826),
6 * which is used to convert IP addresses (or in the future maybe other
7 * high-level addresses into a low-level hardware address (like an Ethernet
8 * address).
9 *
10 * FIXME:
11 * Experiment with better retransmit timers
12 * Clean up the timer deletions
13 * If you create a proxy entry set your interface address to the address
14 * and then delete it, proxies may get out of sync with reality - check this
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
20 *
21 *
22 * Fixes:
23 * Alan Cox : Removed the ethernet assumptions in Florian's code
24 * Alan Cox : Fixed some small errors in the ARP logic
25 * Alan Cox : Allow >4K in /proc
26 * Alan Cox : Make ARP add its own protocol entry
27 *
28 * Ross Martin : Rewrote arp_rcv() and arp_get_info()
29 * Stephen Henson : Add AX25 support to arp_get_info()
30 * Alan Cox : Drop data when a device is downed.
31 * Alan Cox : Use init_timer().
32 * Alan Cox : Double lock fixes.
33 * Martin Seine : Move the arphdr structure
34 * to if_arp.h for compatibility
35 * with BSD based programs.
36 */
37
38 #include <linux/types.h>
39 #include <linux/string.h>
40 #include <linux/kernel.h>
41 #include <linux/sched.h>
42 #include <linux/config.h>
43 #include <linux/socket.h>
44 #include <linux/sockios.h>
45 #include <linux/errno.h>
46 #include <linux/if_arp.h>
47 #include <linux/in.h>
48 #include <asm/system.h>
49 #include <asm/segment.h>
50 #include <stdarg.h>
51 #include <linux/inet.h>
52 #include <linux/netdevice.h>
53 #include <linux/etherdevice.h>
54 #include "ip.h"
55 #include "route.h"
56 #include "protocol.h"
57 #include "tcp.h"
58 #include <linux/skbuff.h>
59 #include "sock.h"
60 #include "arp.h"
61 #ifdef CONFIG_AX25
62 #include "ax25.h"
63 #endif
64
65 /*
66 * This structure defines the ARP mapping cache. As long as we make changes
67 * in this structure, we keep interrupts of. But normally we can copy the
68 * hardware address and the device pointer in a local variable and then make
69 * any "long calls" to send a packet out.
70 */
71
72 struct arp_table
73 {
74 struct arp_table *next; /* Linked entry list */
75 unsigned long last_used; /* For expiry */
76 unsigned int flags; /* Control status */
77 unsigned long ip; /* ip address of entry */
78 unsigned char ha[MAX_ADDR_LEN]; /* Hardware address */
79 unsigned char hlen; /* Length of hardware address */
80 unsigned short htype; /* Type of hardware in use */
81 struct device *dev; /* Device the entry is tied to */
82
83 /*
84 * The following entries are only used for unresolved hw addresses.
85 */
86
87 struct timer_list timer; /* expire timer */
88 int retries; /* remaining retries */
89 struct sk_buff_head skb; /* list of queued packets */
90 };
91
92
93 /*
94 * Configurable Parameters (don't touch unless you know what you are doing
95 */
96
97 /*
98 * If an arp request is send, ARP_RES_TIME is the timeout value until the
99 * next request is send.
100 */
101
102 #define ARP_RES_TIME (250*(HZ/10))
103
104 /*
105 * The number of times an arp request is send, until the host is
106 * considered unreachable.
107 */
108
109 #define ARP_MAX_TRIES 3
110
111 /*
112 * After that time, an unused entry is deleted from the arp table.
113 */
114
115 #define ARP_TIMEOUT (600*HZ)
116
117 /*
118 * How often is the function 'arp_check_retries' called.
119 * An entry is invalidated in the time between ARP_TIMEOUT and
120 * (ARP_TIMEOUT+ARP_CHECK_INTERVAL).
121 */
122
123 #define ARP_CHECK_INTERVAL (60 * HZ)
124
125
126 static void arp_check_expire (unsigned long); /* Forward declaration. */
127
128
129 static struct timer_list arp_timer =
130 { NULL, NULL, ARP_CHECK_INTERVAL, 0L, &arp_check_expire };
131
132 /*
133 * The size of the hash table. Must be a power of two.
134 * Maybe we should remove hashing in the future for arp and concentrate
135 * on Patrick Schaaf's Host-Cache-Lookup...
136 */
137
138 #define ARP_TABLE_SIZE 16
139
140 struct arp_table *arp_tables[ARP_TABLE_SIZE] =
141 {
142 NULL,
143 };
144
145 /*
146 * The last bits in the IP address are used for the cache lookup.
147 */
148
149 #define HASH(paddr) (htonl(paddr) & (ARP_TABLE_SIZE - 1))
150
151 /*
152 * Number of proxy arp entries. This is normally zero and we use it to do
153 * some optimizing for normal uses.
154 */
155
156 static int proxies = 0;
157
158
159 /*
160 * Check if there are too old entries and remove them. If the ATF_PERM
161 * flag is set, they are always left in the arp cache (permanent entry).
162 * Note: Only fully resolved entries, which don't have any packets in
163 * the queue, can be deleted, since ARP_TIMEOUT is much greater than
164 * ARP_MAX_TRIES*ARP_RES_TIME.
165 */
166
167 static void arp_check_expire(unsigned long dummy)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
168 {
169 int i;
170 unsigned long now = jiffies;
171 unsigned long flags;
172 save_flags(flags);
173 cli();
174
175 for (i = 0; i < ARP_TABLE_SIZE; i++)
176 {
177 struct arp_table *entry;
178 struct arp_table **pentry = &arp_tables[i];
179
180 while ((entry = *pentry) != NULL)
181 {
182 if ((now - entry->last_used) > ARP_TIMEOUT
183 && !(entry->flags & ATF_PERM))
184 {
185 *pentry = entry->next; /* remove from list */
186 if (entry->flags & ATF_PUBL)
187 proxies--;
188 del_timer(&entry->timer); /* Paranoia */
189 kfree_s(entry, sizeof(struct arp_table));
190 }
191 else
192 pentry = &entry->next; /* go to next entry */
193 }
194 }
195 restore_flags(flags);
196
197 /*
198 * Set the timer again.
199 */
200
201 del_timer(&arp_timer);
202 arp_timer.expires = ARP_CHECK_INTERVAL;
203 add_timer(&arp_timer);
204 }
205
206
207 /*
208 * Release all linked skb's and the memory for this entry.
209 */
210
211 static void arp_release_entry(struct arp_table *entry)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
212 {
213 struct sk_buff *skb;
214 unsigned long flags;
215
216 if (entry->flags & ATF_PUBL)
217 proxies--;
218
219 save_flags(flags);
220 cli();
221 /* Release the list of `skb' pointers. */
222 while ((skb = skb_dequeue(&entry->skb)) != NULL)
223 {
224 skb_device_lock(skb);
225 restore_flags(flags);
226 dev_kfree_skb(skb, FREE_WRITE);
227 }
228 restore_flags(flags);
229 del_timer(&entry->timer);
230 kfree_s(entry, sizeof(struct arp_table));
231 return;
232 }
233
234 /*
235 * Purge a device from the ARP queue
236 */
237
238 void arp_device_down(struct device *dev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
239 {
240 int i;
241 unsigned long flags;
242
243 /*
244 * This is a bit OTT - maybe we need some arp semaphores instead.
245 */
246 save_flags(flags);
247 cli();
248 for (i = 0; i < ARP_TABLE_SIZE; i++)
249 {
250 struct arp_table *entry;
251 struct arp_table **pentry = &arp_tables[i];
252
253 while ((entry = *pentry) != NULL)
254 {
255 if(entry->dev==dev)
256 {
257 *pentry = entry->next; /* remove from list */
258 if (entry->flags & ATF_PUBL)
259 proxies--;
260 del_timer(&entry->timer); /* Paranoia */
261 kfree_s(entry, sizeof(struct arp_table));
262 }
263 else
264 pentry = &entry->next; /* go to next entry */
265 }
266 }
267 restore_flags(flags);
268 }
269
270
271 /*
272 * Create and send an arp packet. If (dest_hw == NULL), we create a broadcast
273 * message.
274 */
275
276 void arp_send(int type, int ptype, unsigned long dest_ip,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
277 struct device *dev, unsigned long src_ip,
278 unsigned char *dest_hw, unsigned char *src_hw)
279 {
280 struct sk_buff *skb;
281 struct arphdr *arp;
282 unsigned char *arp_ptr;
283
284 /*
285 * No arp on this interface.
286 */
287
288 if(dev->flags&IFF_NOARP)
289 return;
290
291 /*
292 * Allocate a buffer
293 */
294
295 skb = alloc_skb(sizeof(struct arphdr)+ 2*(dev->addr_len+4)
296 + dev->hard_header_len, GFP_ATOMIC);
297 if (skb == NULL)
298 {
299 printk("ARP: no memory to send an arp packet\n");
300 return;
301 }
302 skb->len = sizeof(struct arphdr) + dev->hard_header_len + 2*(dev->addr_len+4);
303 skb->arp = 1;
304 skb->dev = dev;
305 skb->free = 1;
306
307 /*
308 * Fill the device header for the ARP frame
309 */
310
311 dev->hard_header(skb->data,dev,ptype,dest_hw?dest_hw:dev->broadcast,src_hw?src_hw:NULL,skb->len,skb);
312
313 /* Fill out the arp protocol part. */
314 arp = (struct arphdr *) (skb->data + dev->hard_header_len);
315 arp->ar_hrd = htons(dev->type);
316 #ifdef CONFIG_AX25
317 arp->ar_pro = (dev->type != ARPHRD_AX25)? htons(ETH_P_IP) : htons(AX25_P_IP);
318 #else
319 arp->ar_pro = htons(ETH_P_IP);
320 #endif
321 arp->ar_hln = dev->addr_len;
322 arp->ar_pln = 4;
323 arp->ar_op = htons(type);
324
325 arp_ptr=(unsigned char *)(arp+1);
326
327 memcpy(arp_ptr, src_hw, dev->addr_len);
328 arp_ptr+=dev->addr_len;
329 memcpy(arp_ptr, &src_ip,4);
330 arp_ptr+=4;
331 if (dest_hw != NULL)
332 memcpy(arp_ptr, dest_hw, dev->addr_len);
333 else
334 memset(arp_ptr, 0, dev->addr_len);
335 arp_ptr+=dev->addr_len;
336 memcpy(arp_ptr, &dest_ip, 4);
337
338 dev_queue_xmit(skb, dev, 0);
339 }
340
341
342 /*
343 * This function is called, if an entry is not resolved in ARP_RES_TIME.
344 * Either resend a request, or give it up and free the entry.
345 */
346
347 static void arp_expire_request (unsigned long arg)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
348 {
349 struct arp_table *entry = (struct arp_table *) arg;
350 struct arp_table **pentry;
351 unsigned long hash;
352 unsigned long flags;
353
354 save_flags(flags);
355 cli();
356
357 /*
358 * Since all timeouts are handled with interrupts enabled, there is a
359 * small chance, that this entry has just been resolved by an incoming
360 * packet. This is the only race condition, but it is handled...
361 */
362
363 if (entry->flags & ATF_COM)
364 {
365 restore_flags(flags);
366 return;
367 }
368
369 if (--entry->retries > 0)
370 {
371 unsigned long ip = entry->ip;
372 struct device *dev = entry->dev;
373
374 /* Set new timer. */
375 del_timer(&entry->timer);
376 entry->timer.expires = ARP_RES_TIME;
377 add_timer(&entry->timer);
378 restore_flags(flags);
379 arp_send(ARPOP_REQUEST, ETH_P_ARP, ip, dev, dev->pa_addr,
380 NULL, dev->dev_addr);
381 return;
382 }
383
384 /*
385 * Arp request timed out. Delete entry and all waiting packets.
386 * If we give each entry a pointer to itself, we don't have to
387 * loop through everything again. Maybe hash is good enough, but
388 * I will look at it later.
389 */
390
391 hash = HASH(entry->ip);
392 pentry = &arp_tables[hash];
393 while (*pentry != NULL)
394 {
395 if (*pentry == entry)
396 {
397 *pentry = entry->next; /* delete from linked list */
398 del_timer(&entry->timer);
399 restore_flags(flags);
400 arp_release_entry(entry);
401 return;
402 }
403 pentry = &(*pentry)->next;
404 }
405 restore_flags(flags);
406 printk("Possible ARP queue corruption.\n");
407 /*
408 * We should never arrive here.
409 */
410 }
411
412
413 /*
414 * This will try to retransmit everything on the queue.
415 */
416
417 static void arp_send_q(struct arp_table *entry, unsigned char *hw_dest)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
418 {
419 struct sk_buff *skb;
420
421 unsigned long flags;
422
423 /*
424 * Empty the entire queue, building its data up ready to send
425 */
426
427 if(!(entry->flags&ATF_COM))
428 {
429 printk("arp_send_q: incomplete entry for %s\n",
430 in_ntoa(entry->ip));
431 return;
432 }
433
434 save_flags(flags);
435
436 cli();
437 while((skb = skb_dequeue(&entry->skb)) != NULL)
438 {
439 IS_SKB(skb);
440 skb_device_lock(skb);
441 restore_flags(flags);
442 if(!skb->dev->rebuild_header(skb->data,skb->dev,skb->raddr,skb))
443 {
444 skb->arp = 1;
445 if(skb->sk==NULL)
446 dev_queue_xmit(skb, skb->dev, 0);
447 else
448 dev_queue_xmit(skb,skb->dev,skb->sk->priority);
449 }
450 else
451 {
452 /* This routine is only ever called when 'entry' is
453 complete. Thus this can't fail. */
454 printk("arp_send_q: The impossible occurred. Please notify Alan.\n");
455 printk("arp_send_q: active entity %s\n",in_ntoa(entry->ip));
456 printk("arp_send_q: failed to find %s\n",in_ntoa(skb->raddr));
457 }
458 }
459 restore_flags(flags);
460 }
461
462
463 /*
464 * Delete an ARP mapping entry in the cache.
465 */
466
467 void arp_destroy(unsigned long ip_addr, int force)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
468 {
469 struct arp_table *entry;
470 struct arp_table **pentry;
471 unsigned long hash = HASH(ip_addr);
472
473 cli();
474 pentry = &arp_tables[hash];
475 while ((entry = *pentry) != NULL)
476 {
477 if (entry->ip == ip_addr)
478 {
479 if ((entry->flags & ATF_PERM) && !force)
480 return;
481 *pentry = entry->next;
482 del_timer(&entry->timer);
483 sti();
484 arp_release_entry(entry);
485 return;
486 }
487 pentry = &entry->next;
488 }
489 sti();
490 }
491
492
493 /*
494 * Receive an arp request by the device layer. Maybe I rewrite it, to
495 * use the incoming packet for the reply. The time for the current
496 * "overhead" isn't that high...
497 */
498
499 int arp_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
500 {
501 /*
502 * We shouldn't use this type conversion. Check later.
503 */
504
505 struct arphdr *arp = (struct arphdr *)skb->h.raw;
506 unsigned char *arp_ptr= (unsigned char *)(arp+1);
507 struct arp_table *entry;
508 struct arp_table *proxy_entry;
509 int addr_hint,hlen,htype;
510 unsigned long hash,dest_hash;
511 unsigned char ha[MAX_ADDR_LEN]; /* So we can enable ints again. */
512 long sip,tip;
513 unsigned char *sha,*tha;
514
515 /*
516 * The hardware length of the packet should match the hardware length
517 * of the device. Similarly, the hardware types should match. The
518 * device should be ARP-able. Also, if pln is not 4, then the lookup
519 * is not from an IP number. We can't currently handle this, so toss
520 * it.
521 */
522 if (arp->ar_hln != dev->addr_len ||
523 dev->type != ntohs(arp->ar_hrd) ||
524 dev->flags & IFF_NOARP ||
525 arp->ar_pln != 4)
526 {
527 kfree_skb(skb, FREE_READ);
528 return 0;
529 }
530
531 /*
532 * Another test.
533 * The logic here is that the protocol being looked up by arp should
534 * match the protocol the device speaks. If it doesn't, there is a
535 * problem, so toss the packet.
536 */
537 switch(dev->type)
538 {
539 #ifdef CONFIG_AX25
540 case ARPHRD_AX25:
541 if(arp->ar_pro != htons(AX25_P_IP))
542 {
543 kfree_skb(skb, FREE_READ);
544 return 0;
545 }
546 break;
547 #endif
548 case ARPHRD_ETHER:
549 if(arp->ar_pro != htons(ETH_P_IP))
550 {
551 kfree_skb(skb, FREE_READ);
552 return 0;
553 }
554 break;
555
556 default:
557 printk("ARP: dev->type mangled!\n");
558 kfree_skb(skb, FREE_READ);
559 return 0;
560 }
561
562 /*
563 * Extract fields
564 */
565
566 hlen = dev->addr_len;
567 htype = dev->type;
568
569 sha=arp_ptr;
570 arp_ptr+=hlen;
571 memcpy(&sip,arp_ptr,4);
572 arp_ptr+=4;
573 tha=arp_ptr;
574 arp_ptr+=hlen;
575 memcpy(&tip,arp_ptr,4);
576
577 /*
578 * Check for bad requests for 127.0.0.1. If this is one such, delete it.
579 */
580 if(tip == INADDR_LOOPBACK)
581 {
582 kfree_skb(skb, FREE_READ);
583 return 0;
584 }
585
586 /*
587 * Process entry. The idea here is we want to send a reply if it is a
588 * request for us or if it is a request for someone else that we hold
589 * a proxy for. We want to add an entry to our cache if it is a reply
590 * to us or if it is a request for our address.
591 * (The assumption for this last is that if someone is requesting our
592 * address, they are probably intending to talk to us, so it saves time
593 * if we cache their address. Their address is also probably not in
594 * our cache, since ours is not in their cache.)
595 *
596 * Putting this another way, we only care about replies if they are to
597 * us, in which case we add them to the cache. For requests, we care
598 * about those for us and those for our proxies. We reply to both,
599 * and in the case of requests for us we add the requester to the arp
600 * cache.
601 */
602
603 addr_hint = ip_chk_addr(tip);
604
605 if(arp->ar_op == htons(ARPOP_REPLY))
606 {
607 if(addr_hint!=IS_MYADDR)
608 {
609 /*
610 * Replies to other machines get tossed.
611 */
612 kfree_skb(skb, FREE_READ);
613 return 0;
614 }
615 /*
616 * Fall through to code below that adds sender to cache.
617 */
618 }
619 else
620 {
621 /*
622 * It is now an arp request
623 */
624 if(addr_hint != IS_MYADDR)
625 {
626 /*
627 * To get in here, it is a request for someone else. We need to
628 * check if that someone else is one of our proxies. If it isn't,
629 * we can toss it.
630 */
631 if (proxies == 0)
632 {
633 kfree_skb(skb, FREE_READ);
634 return 0;
635 }
636
637 dest_hash = HASH(tip);
638 cli();
639 for(proxy_entry=arp_tables[dest_hash];
640 proxy_entry;
641 proxy_entry = proxy_entry->next)
642 {
643 if(proxy_entry->ip == tip && proxy_entry->htype==htype)
644 break;
645 }
646 if (proxy_entry && (proxy_entry->flags & ATF_PUBL))
647 {
648 memcpy(ha, proxy_entry->ha, hlen);
649 sti();
650 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,ha);
651 kfree_skb(skb, FREE_READ);
652 return 0;
653 }
654 else
655 {
656 sti();
657 kfree_skb(skb, FREE_READ);
658 return 0;
659 }
660 }
661 else
662 {
663 /*
664 * To get here, it must be an arp request for us. We need to reply.
665 */
666 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr);
667 }
668 }
669
670
671 /*
672 * Now all replies are handled. Next, anything that falls through to here
673 * needs to be added to the arp cache, or have its entry updated if it is
674 * there.
675 */
676
677 hash = HASH(sip);
678 cli();
679 for(entry=arp_tables[hash];entry;entry=entry->next)
680 if(entry->ip==sip && entry->htype==htype)
681 break;
682
683 if(entry)
684 {
685 /*
686 * Entry found; update it.
687 */
688 memcpy(entry->ha, sha, hlen);
689 entry->hlen = hlen;
690 entry->last_used = jiffies;
691 if (!(entry->flags & ATF_COM))
692 {
693 /*
694 * This entry was incomplete. Delete the retransmit timer
695 * and switch to complete status.
696 */
697 del_timer(&entry->timer);
698 entry->flags |= ATF_COM;
699 sti();
700 /*
701 * Send out waiting packets. We might have problems, if someone is
702 * manually removing entries right now -- entry might become invalid
703 * underneath us.
704 */
705 arp_send_q(entry, sha);
706 }
707 else
708 {
709 sti();
710 }
711 }
712 else
713 {
714 /*
715 * No entry found. Need to add a new entry to the arp table.
716 */
717 entry = (struct arp_table *)kmalloc(sizeof(struct arp_table),GFP_ATOMIC);
718 if(entry == NULL)
719 {
720 sti();
721 printk("ARP: no memory for new arp entry\n");
722
723 kfree_skb(skb, FREE_READ);
724 return 0;
725 }
726
727 entry->ip = sip;
728 entry->hlen = hlen;
729 entry->htype = htype;
730 entry->flags = ATF_COM;
731 init_timer(&entry->timer);
732 memcpy(entry->ha, sha, hlen);
733 entry->last_used = jiffies;
734 entry->dev = skb->dev;
735 skb_queue_head_init(&entry->skb);
736 entry->next = arp_tables[hash];
737 arp_tables[hash] = entry;
738 sti();
739 }
740
741 /*
742 * Replies have been sent, and entries have been added. All done.
743 */
744 kfree_skb(skb, FREE_READ);
745 return 0;
746 }
747
748
749 /*
750 * Find an arp mapping in the cache. If not found, post a request.
751 */
752
753 int arp_find(unsigned char *haddr, unsigned long paddr, struct device *dev,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
754 unsigned long saddr, struct sk_buff *skb)
755 {
756 struct arp_table *entry;
757 unsigned long hash;
758 switch (ip_chk_addr(paddr))
759 {
760 case IS_MYADDR:
761 printk("ARP: arp called for own IP address\n");
762 memcpy(haddr, dev->dev_addr, dev->addr_len);
763 skb->arp = 1;
764 return 0;
765 case IS_BROADCAST:
766 memcpy(haddr, dev->broadcast, dev->addr_len);
767 skb->arp = 1;
768 return 0;
769 }
770
771 hash = HASH(paddr);
772 cli();
773
774 /*
775 * Find an entry
776 */
777 for (entry = arp_tables[hash]; entry != NULL; entry = entry->next)
778 if (entry->ip == paddr)
779 break;
780
781
782 if (entry != NULL) /* It exists */
783 {
784 if (!(entry->flags & ATF_COM))
785 {
786 /*
787 * A request was already send, but no reply yet. Thus
788 * queue the packet with the previous attempt
789 */
790
791 if (skb != NULL)
792 {
793 skb_queue_tail(&entry->skb, skb);
794 skb_device_unlock(skb);
795 }
796 sti();
797 return 1;
798 }
799
800 /*
801 * Update the record
802 */
803
804 entry->last_used = jiffies;
805 memcpy(haddr, entry->ha, dev->addr_len);
806 if (skb)
807 skb->arp = 1;
808 sti();
809 return 0;
810 }
811
812 /*
813 * Create a new unresolved entry.
814 */
815
816 entry = (struct arp_table *) kmalloc(sizeof(struct arp_table),
817 GFP_ATOMIC);
818 if (entry != NULL)
819 {
820 entry->ip = paddr;
821 entry->hlen = dev->addr_len;
822 entry->htype = dev->type;
823 entry->flags = 0;
824 memset(entry->ha, 0, dev->addr_len);
825 entry->dev = dev;
826 entry->last_used = jiffies;
827 init_timer(&entry->timer);
828 entry->timer.function = arp_expire_request;
829 entry->timer.data = (unsigned long)entry;
830 entry->timer.expires = ARP_RES_TIME;
831 entry->next = arp_tables[hash];
832 arp_tables[hash] = entry;
833 add_timer(&entry->timer);
834 entry->retries = ARP_MAX_TRIES;
835 skb_queue_head_init(&entry->skb);
836 if (skb != NULL)
837 {
838 skb_queue_tail(&entry->skb, skb);
839 skb_device_unlock(skb);
840 }
841 }
842 else
843 {
844 if (skb != NULL && skb->free)
845 kfree_skb(skb, FREE_WRITE);
846 }
847 sti();
848
849 /*
850 * If we didn't find an entry, we will try to send an ARP packet.
851 */
852
853 arp_send(ARPOP_REQUEST, ETH_P_ARP, paddr, dev, saddr, NULL,
854 dev->dev_addr);
855
856 return 1;
857 }
858
859
860 /*
861 * Write the contents of the ARP cache to a PROCfs file.
862 */
863
864 #define HBUFFERLEN 30
865
866 int arp_get_info(char *buffer, char **start, off_t offset, int length)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
867 {
868 int len=0;
869 off_t begin=0;
870 off_t pos=0;
871 int size;
872 struct arp_table *entry;
873 char hbuffer[HBUFFERLEN];
874 int i,j,k;
875 const char hexbuf[] = "0123456789ABCDEF";
876
877 size = sprintf(buffer,"IP address HW type Flags HW address\n");
878 pos+=size;
879 len+=size;
880
881 cli();
882 for(i=0; i<ARP_TABLE_SIZE; i++)
883 {
884 for(entry=arp_tables[i]; entry!=NULL; entry=entry->next)
885 {
886 /*
887 * Convert hardware address to XX:XX:XX:XX ... form.
888 */
889 #ifdef CONFIG_AX25
890
891 if(entry->htype==ARPHRD_AX25)
892 strcpy(hbuffer,ax2asc((ax25_address *)entry->ha));
893 else {
894 #endif
895
896 for(k=0,j=0;k<HBUFFERLEN-3 && j<entry->hlen;j++)
897 {
898 hbuffer[k++]=hexbuf[ (entry->ha[j]>>4)&15 ];
899 hbuffer[k++]=hexbuf[ entry->ha[j]&15 ];
900 hbuffer[k++]=':';
901 }
902 hbuffer[--k]=0;
903
904 #ifdef CONFIG_AX25
905 }
906 #endif
907 size = sprintf(buffer+len,
908 "%-17s0x%-10x0x%-10x%s\n",
909 in_ntoa(entry->ip),
910 (unsigned int)entry->htype,
911 entry->flags,
912 hbuffer);
913
914 len+=size;
915 pos=begin+len;
916
917 if(pos<offset)
918 {
919 len=0;
920 begin=pos;
921 }
922 if(pos>offset+length)
923 break;
924 }
925 }
926 sti();
927
928 *start=buffer+(offset-begin); /* Start of wanted data */
929 len-=(offset-begin); /* Start slop */
930 if(len>length)
931 len=length; /* Ending slop */
932 return len;
933 }
934
935
936 /*
937 * This will find an entry in the ARP table by looking at the IP address.
938 * Be careful, interrupts are turned off on exit!!!
939 */
940
941 static struct arp_table *arp_lookup(unsigned long paddr)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
942 {
943 struct arp_table *entry;
944 unsigned long hash = HASH(paddr);
945
946 cli();
947 for (entry = arp_tables[hash]; entry != NULL; entry = entry->next)
948 if (entry->ip == paddr) break;
949 return entry;
950 }
951
952
953 /*
954 * Set (create) an ARP cache entry.
955 */
956
957 static int arp_req_set(struct arpreq *req)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
958 {
959 struct arpreq r;
960 struct arp_table *entry;
961 struct sockaddr_in *si;
962 int htype, hlen;
963 unsigned long ip, hash;
964 struct rtable *rt;
965
966 memcpy_fromfs(&r, req, sizeof(r));
967
968 /* We only understand about IP addresses... */
969 if (r.arp_pa.sa_family != AF_INET)
970 return -EPFNOSUPPORT;
971
972 /*
973 * Find out about the hardware type.
974 * We have to be compatible with BSD UNIX, so we have to
975 * assume that a "not set" value (i.e. 0) means Ethernet.
976 */
977
978 switch (r.arp_ha.sa_family) {
979 case ARPHRD_ETHER:
980 htype = ARPHRD_ETHER;
981 hlen = ETH_ALEN;
982 break;
983 #ifdef CONFIG_AX25
984 case ARPHRD_AX25:
985 htype = ARPHRD_AX25;
986 hlen = 7;
987 break;
988 #endif
989 default:
990 return -EPFNOSUPPORT;
991 }
992
993 si = (struct sockaddr_in *) &r.arp_pa;
994 ip = si->sin_addr.s_addr;
995 if (ip == 0)
996 {
997 printk("ARP: SETARP: requested PA is 0.0.0.0 !\n");
998 return -EINVAL;
999 }
1000
1001 /*
1002 * Is it reachable directly ?
1003 */
1004
1005 rt = ip_rt_route(ip, NULL, NULL);
1006 if (rt == NULL)
1007 return -ENETUNREACH;
1008
1009 /*
1010 * Is there an existing entry for this address?
1011 */
1012
1013 hash = HASH(ip);
1014 cli();
1015
1016 /*
1017 * Find the entry
1018 */
1019 for (entry = arp_tables[hash]; entry != NULL; entry = entry->next)
1020 if (entry->ip == ip)
1021 break;
1022
1023 /*
1024 * Do we need to create a new entry
1025 */
1026
1027 if (entry == NULL)
1028 {
1029 entry = (struct arp_table *) kmalloc(sizeof(struct arp_table),
1030 GFP_ATOMIC);
1031 if (entry == NULL)
1032 {
1033 sti();
1034 return -ENOMEM;
1035 }
1036 entry->ip = ip;
1037 entry->hlen = hlen;
1038 entry->htype = htype;
1039 init_timer(&entry->timer);
1040 entry->next = arp_tables[hash];
1041 arp_tables[hash] = entry;
1042 skb_queue_head_init(&entry->skb);
1043 }
1044 else
1045 if (entry->flags & ATF_PUBL)
1046 proxies--;
1047 /*
1048 * We now have a pointer to an ARP entry. Update it!
1049 */
1050
1051 memcpy(&entry->ha, &r.arp_ha.sa_data, hlen);
1052 entry->last_used = jiffies;
1053 entry->flags = r.arp_flags | ATF_COM;
1054 if (entry->flags & ATF_PUBL)
1055 proxies++;
1056 entry->dev = rt->rt_dev;
1057 sti();
1058
1059 return 0;
1060 }
1061
1062
1063 /*
1064 * Get an ARP cache entry.
1065 */
1066
1067 static int arp_req_get(struct arpreq *req)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1068 {
1069 struct arpreq r;
1070 struct arp_table *entry;
1071 struct sockaddr_in *si;
1072
1073 /*
1074 * We only understand about IP addresses...
1075 */
1076
1077 memcpy_fromfs(&r, req, sizeof(r));
1078
1079 if (r.arp_pa.sa_family != AF_INET)
1080 return -EPFNOSUPPORT;
1081
1082 /*
1083 * Is there an existing entry for this address?
1084 */
1085
1086 si = (struct sockaddr_in *) &r.arp_pa;
1087 entry = arp_lookup(si->sin_addr.s_addr);
1088
1089 if (entry == NULL)
1090 {
1091 sti();
1092 return -ENXIO;
1093 }
1094
1095 /*
1096 * We found it; copy into structure.
1097 */
1098
1099 memcpy(r.arp_ha.sa_data, &entry->ha, entry->hlen);
1100 r.arp_ha.sa_family = entry->htype;
1101 r.arp_flags = entry->flags;
1102 sti();
1103
1104 /*
1105 * Copy the information back
1106 */
1107
1108 memcpy_tofs(req, &r, sizeof(r));
1109 return 0;
1110 }
1111
1112
1113 /*
1114 * Handle an ARP layer I/O control request.
1115 */
1116
1117 int arp_ioctl(unsigned int cmd, void *arg)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1118 {
1119 struct arpreq r;
1120 struct sockaddr_in *si;
1121 int err;
1122
1123 switch(cmd)
1124 {
1125 case SIOCDARP:
1126 if (!suser())
1127 return -EPERM;
1128 err = verify_area(VERIFY_READ, arg, sizeof(struct arpreq));
1129 if(err)
1130 return err;
1131 memcpy_fromfs(&r, arg, sizeof(r));
1132 if (r.arp_pa.sa_family != AF_INET)
1133 return -EPFNOSUPPORT;
1134 si = (struct sockaddr_in *) &r.arp_pa;
1135 arp_destroy(si->sin_addr.s_addr, 1);
1136 return 0;
1137 case SIOCGARP:
1138 err = verify_area(VERIFY_WRITE, arg, sizeof(struct arpreq));
1139 if(err)
1140 return err;
1141 return arp_req_get((struct arpreq *)arg);
1142 case SIOCSARP:
1143 if (!suser())
1144 return -EPERM;
1145 err = verify_area(VERIFY_READ, arg, sizeof(struct arpreq));
1146 if(err)
1147 return err;
1148 return arp_req_set((struct arpreq *)arg);
1149 default:
1150 return -EINVAL;
1151 }
1152 /*NOTREACHED*/
1153 return 0;
1154 }
1155
1156
1157 /*
1158 * Called once on startup.
1159 */
1160
1161 static struct packet_type arp_packet_type =
1162 {
1163 0, /* Should be: __constant_htons(ETH_P_ARP) - but this _doesn't_ come out constant! */
1164 0, /* copy */
1165 arp_rcv,
1166 NULL,
1167 NULL
1168 };
1169
1170 void arp_init (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1171 {
1172 /* Register the packet type */
1173 arp_packet_type.type=htons(ETH_P_ARP);
1174 dev_add_pack(&arp_packet_type);
1175 /* Start with the regular checks for expired arp entries. */
1176 add_timer(&arp_timer);
1177 }
1178