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