This source file includes following definitions.
- arp_check_expire
- arp_release_entry
- arp_device_event
- arp_send
- arp_expire_request
- arp_send_q
- arp_destroy
- arp_rcv
- arp_query
- arp_find
- arp_get_info
- arp_lookup
- arp_find_cache
- arp_req_set
- arp_req_get
- arp_ioctl
- arp_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
79
80
81
82
83
84 struct arp_table
85 {
86 struct arp_table *next;
87 unsigned long last_used;
88 unsigned int flags;
89 u32 ip;
90 u32 mask;
91 unsigned char ha[MAX_ADDR_LEN];
92 unsigned char hlen;
93 unsigned short htype;
94 struct device *dev;
95
96
97
98
99
100 struct timer_list timer;
101 int retries;
102 struct sk_buff_head skb;
103 };
104
105
106
107
108
109
110
111
112
113
114
115 #define ARP_RES_TIME (250*(HZ/10))
116
117
118
119
120
121
122 #define ARP_MAX_TRIES 3
123
124
125
126
127
128 #define ARP_TIMEOUT (600*HZ)
129
130
131
132
133
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
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
154
155
156
157
158 #define DEF_ARP_NETMASK (~0)
159
160
161
162
163
164
165
166
167
168 #define ARP_TABLE_SIZE 16
169
170
171
172
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
187
188
189
190 #define HASH(paddr) (htonl(paddr) & (ARP_TABLE_SIZE - 1))
191 #define PROXY_HASH ARP_TABLE_SIZE
192
193
194
195
196
197
198
199
200
201 static void arp_check_expire(unsigned long dummy)
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;
220 arp_cache_stamp++;
221 del_timer(&entry->timer);
222 kfree_s(entry, sizeof(struct arp_table));
223 }
224 else
225 pentry = &entry->next;
226 }
227 }
228 restore_flags(flags);
229
230
231
232
233
234 del_timer(&arp_timer);
235 arp_timer.expires = ARP_CHECK_INTERVAL;
236 add_timer(&arp_timer);
237 }
238
239
240
241
242
243
244 static void arp_release_entry(struct arp_table *entry)
245 {
246 struct sk_buff *skb;
247 unsigned long flags;
248
249 save_flags(flags);
250 cli();
251
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
266
267
268 int arp_device_event(unsigned long event, void *ptr)
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
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;
292 del_timer(&entry->timer);
293 kfree_s(entry, sizeof(struct arp_table));
294 }
295 else
296 pentry = &entry->next;
297 }
298 }
299 arp_cache_stamp++;
300 restore_flags(flags);
301 return NOTIFY_DONE;
302 }
303
304
305
306
307
308
309
310 void arp_send(int type, int ptype, u32 dest_ip,
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
320
321
322 if(dev->flags&IFF_NOARP)
323 return;
324
325
326
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
344
345
346 dev->hard_header(skb,dev,ptype,dest_hw?dest_hw:dev->broadcast,src_hw?src_hw:NULL,skb->len);
347
348
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
382
383
384
385 static void arp_expire_request (unsigned long arg)
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
397
398
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
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
424
425
426
427
428
429 hash = HASH(entry->ip);
430
431
432
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;
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
455
456 }
457
458
459
460
461
462
463 static void arp_send_q(struct arp_table *entry, unsigned char *hw_dest)
464 {
465 struct sk_buff *skb;
466
467 unsigned long flags;
468
469
470
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
503
504
505 void arp_destroy(u32 ip_addr, int force)
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)
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
529 goto ugly;
530
531
532
533
534 }
535 pentry = &entry->next;
536 if (!checked_proxies && ! *pentry)
537 {
538
539 checked_proxies = 1;
540 pentry = &arp_tables[PROXY_HASH];
541 }
542 }
543 sti();
544 }
545
546
547
548
549
550
551
552
553 int arp_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
554 {
555
556
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];
566 unsigned char *sha,*tha;
567 u32 sip,tip;
568
569
570
571
572
573
574
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
587
588
589
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
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
651
652 if(tip == INADDR_LOOPBACK)
653 {
654 kfree_skb(skb, FREE_READ);
655 return 0;
656 }
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
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
683
684 kfree_skb(skb, FREE_READ);
685 return 0;
686 }
687
688
689
690 }
691 else
692 {
693
694
695
696
697
698
699 if(tip!=dev->pa_addr)
700 {
701
702
703
704
705
706 cli();
707 for(proxy_entry=arp_tables[PROXY_HASH];
708 proxy_entry;
709 proxy_entry = proxy_entry->next)
710 {
711
712
713
714
715
716
717
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
743
744 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr);
745 }
746 }
747
748
749
750
751
752
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
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
773
774
775 del_timer(&entry->timer);
776 entry->flags |= ATF_COM;
777 sti();
778
779
780
781
782
783 arp_send_q(entry, sha);
784 }
785 else
786 {
787 sti();
788 }
789 }
790 else
791 {
792
793
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
822
823 kfree_skb(skb, FREE_READ);
824 return 0;
825 }
826
827
828
829
830
831
832 int arp_query(unsigned char *haddr, u32 paddr, unsigned short type)
833 {
834 struct arp_table *entry;
835 unsigned long hash = HASH(paddr);
836
837
838
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
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
863
864
865 int arp_find(unsigned char *haddr, u32 paddr, struct device *dev,
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
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
912
913 entry = arp_lookup(paddr, PROXY_NONE);
914
915 if (entry != NULL)
916 {
917 if (!(entry->flags & ATF_COM))
918 {
919
920
921
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
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
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
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
996
997
998 #define HBUFFERLEN 30
999
1000 int arp_get_info(char *buffer, char **start, off_t offset, int length)
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
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);
1073 len-=(offset-begin);
1074 if(len>length)
1075 len=length;
1076 return len;
1077 }
1078
1079
1080
1081
1082
1083
1084
1085
1086 static struct arp_table *arp_lookup(u32 paddr, enum proxy proxy)
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
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)
1106 {
1107
1108
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
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
1158
1159
1160 static int arp_req_set(struct arpreq *req)
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
1172 if (r.arp_pa.sa_family != AF_INET)
1173 return -EPFNOSUPPORT;
1174
1175
1176
1177
1178
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;
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
1222
1223
1224 rt = ip_rt_route(ip, NULL, NULL);
1225 if (rt == NULL)
1226 return -ENETUNREACH;
1227
1228
1229
1230
1231
1232 cli();
1233
1234
1235
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
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
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
1295
1296
1297 static int arp_req_get(struct arpreq *req)
1298 {
1299 struct arpreq r;
1300 struct arp_table *entry;
1301 struct sockaddr_in *si;
1302
1303
1304
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
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
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
1337
1338
1339 memcpy_tofs(req, &r, sizeof(r));
1340 return 0;
1341 }
1342
1343
1344
1345
1346
1347
1348 int arp_ioctl(unsigned int cmd, void *arg)
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
1384 return 0;
1385 }
1386
1387
1388
1389
1390
1391
1392 static struct packet_type arp_packet_type =
1393 {
1394 0,
1395 NULL,
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)
1408 {
1409
1410 arp_packet_type.type=htons(ETH_P_ARP);
1411 dev_add_pack(&arp_packet_type);
1412
1413 add_timer(&arp_timer);
1414
1415 register_netdevice_notifier(&arp_dev_notifier);
1416 }
1417