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