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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 #include <linux/types.h>
62 #include <linux/string.h>
63 #include <linux/kernel.h>
64 #include <linux/sched.h>
65 #include <linux/config.h>
66 #include <linux/socket.h>
67 #include <linux/sockios.h>
68 #include <linux/errno.h>
69 #include <linux/if_arp.h>
70 #include <linux/in.h>
71 #include <linux/mm.h>
72 #include <asm/system.h>
73 #include <asm/segment.h>
74 #include <stdarg.h>
75 #include <linux/inet.h>
76 #include <linux/netdevice.h>
77 #include <linux/etherdevice.h>
78 #include <linux/trdevice.h>
79 #include <net/ip.h>
80 #include <net/route.h>
81 #include <net/protocol.h>
82 #include <net/tcp.h>
83 #include <linux/skbuff.h>
84 #include <net/sock.h>
85 #include <net/arp.h>
86 #ifdef CONFIG_AX25
87 #include <net/ax25.h>
88 #ifdef CONFIG_NETROM
89 #include <net/netrom.h>
90 #endif
91 #endif
92 #include <linux/proc_fs.h>
93 #include <linux/stat.h>
94
95
96
97
98
99
100
101
102
103 struct arp_table
104 {
105 struct arp_table *next;
106 unsigned long last_used;
107 unsigned int flags;
108 u32 ip;
109 u32 mask;
110 unsigned char ha[MAX_ADDR_LEN];
111 unsigned char hlen;
112 unsigned short htype;
113 struct device *dev;
114
115
116
117
118
119 struct timer_list timer;
120 int retries;
121 struct sk_buff_head skb;
122 };
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 #define ARP_RES_TIME (250*(HZ/10))
139
140
141
142
143
144
145 #define ARP_MAX_TRIES 3
146
147
148
149
150
151 #define ARP_TIMEOUT (600*HZ)
152
153
154
155
156
157
158
159 #define ARP_CHECK_INTERVAL (60 * HZ)
160
161 enum proxy {
162 PROXY_EXACT=0,
163 PROXY_ANY,
164 PROXY_NONE,
165 };
166
167
168 static void arp_check_expire (unsigned long);
169 static struct arp_table *arp_lookup(u32 paddr, enum proxy proxy, unsigned short type);
170
171
172 static struct timer_list arp_timer =
173 { NULL, NULL, ARP_CHECK_INTERVAL, 0L, &arp_check_expire };
174
175
176
177
178
179
180
181 #define DEF_ARP_NETMASK (~0)
182
183
184
185
186
187
188
189
190
191 #define ARP_TABLE_SIZE 16
192
193
194
195
196
197
198 #define FULL_ARP_TABLE_SIZE (ARP_TABLE_SIZE+1)
199
200 struct arp_table *arp_tables[FULL_ARP_TABLE_SIZE] =
201 {
202 NULL,
203 };
204
205 unsigned long arp_cache_stamp;
206
207
208
209
210
211
212
213 #define HASH(paddr) (htonl(paddr) & (ARP_TABLE_SIZE - 1))
214 #define PROXY_HASH ARP_TABLE_SIZE
215
216
217
218
219
220
221
222
223
224
225
226 static void arp_check_expire(unsigned long dummy)
227 {
228 int i;
229 unsigned long now = jiffies;
230 unsigned long flags;
231 save_flags(flags);
232 cli();
233
234 for (i = 0; i < FULL_ARP_TABLE_SIZE; i++)
235 {
236 struct arp_table *entry;
237 struct arp_table **pentry = &arp_tables[i];
238
239 while ((entry = *pentry) != NULL)
240 {
241 if ((now - entry->last_used) > ARP_TIMEOUT
242 && !(entry->flags & ATF_PERM))
243 {
244 *pentry = entry->next;
245 arp_cache_stamp++;
246 del_timer(&entry->timer);
247 kfree_s(entry, sizeof(struct arp_table));
248
249
250 }
251 else
252 pentry = &entry->next;
253 }
254 }
255 restore_flags(flags);
256
257
258
259
260
261 del_timer(&arp_timer);
262 arp_timer.expires = jiffies + ARP_CHECK_INTERVAL;
263 add_timer(&arp_timer);
264 }
265
266
267
268
269
270
271 static void arp_release_entry(struct arp_table *entry)
272 {
273 struct sk_buff *skb;
274 unsigned long flags;
275
276 save_flags(flags);
277 cli();
278
279 while ((skb = skb_dequeue(&entry->skb)) != NULL)
280 {
281 skb_device_lock(skb);
282 restore_flags(flags);
283 dev_kfree_skb(skb, FREE_WRITE);
284 cli();
285 }
286 restore_flags(flags);
287 del_timer(&entry->timer);
288 kfree_s(entry, sizeof(struct arp_table));
289 return;
290 }
291
292
293
294
295
296 int arp_device_event(unsigned long event, void *ptr)
297 {
298 struct device *dev=ptr;
299 int i;
300 unsigned long flags;
301
302 if(event!=NETDEV_DOWN)
303 return NOTIFY_DONE;
304
305
306
307
308 save_flags(flags);
309 cli();
310 for (i = 0; i < FULL_ARP_TABLE_SIZE; i++)
311 {
312 struct arp_table *entry;
313 struct arp_table **pentry = &arp_tables[i];
314
315 while ((entry = *pentry) != NULL)
316 {
317 if(entry->dev==dev)
318 {
319 *pentry = entry->next;
320 del_timer(&entry->timer);
321 kfree_s(entry, sizeof(struct arp_table));
322 }
323 else
324 pentry = &entry->next;
325 }
326 }
327 arp_cache_stamp++;
328 restore_flags(flags);
329 return NOTIFY_DONE;
330 }
331
332
333
334
335
336
337
338 void arp_send(int type, int ptype, u32 dest_ip,
339 struct device *dev, u32 src_ip,
340 unsigned char *dest_hw, unsigned char *src_hw)
341 {
342 struct sk_buff *skb;
343 struct arphdr *arp;
344 unsigned char *arp_ptr;
345
346
347
348
349
350 if(dev->flags&IFF_NOARP)
351 return;
352
353
354
355
356
357 skb = alloc_skb(sizeof(struct arphdr)+ 2*(dev->addr_len+4)
358 + dev->hard_header_len, GFP_ATOMIC);
359 if (skb == NULL)
360 {
361 printk("ARP: no memory to send an arp packet\n");
362 return;
363 }
364 skb_reserve(skb, dev->hard_header_len);
365 arp = (struct arphdr *) skb_put(skb,sizeof(struct arphdr) + 2*(dev->addr_len+4));
366 skb->arp = 1;
367 skb->dev = dev;
368 skb->free = 1;
369
370
371
372
373
374 dev->hard_header(skb,dev,ptype,dest_hw?dest_hw:dev->broadcast,src_hw?src_hw:NULL,skb->len);
375
376
377 arp->ar_hrd = htons(dev->type);
378 #ifdef CONFIG_AX25
379 #ifdef CONFIG_NETROM
380 arp->ar_pro = (dev->type == ARPHRD_AX25 || dev->type == ARPHRD_NETROM) ? htons(AX25_P_IP) : htons(ETH_P_IP);
381 #else
382 arp->ar_pro = (dev->type != ARPHRD_AX25)? htons(ETH_P_IP) : htons(AX25_P_IP);
383 #endif
384 #else
385 arp->ar_pro = htons(ETH_P_IP);
386 #endif
387 arp->ar_hln = dev->addr_len;
388 arp->ar_pln = 4;
389 arp->ar_op = htons(type);
390
391 arp_ptr=(unsigned char *)(arp+1);
392
393 memcpy(arp_ptr, src_hw, dev->addr_len);
394 arp_ptr+=dev->addr_len;
395 memcpy(arp_ptr, &src_ip,4);
396 arp_ptr+=4;
397 if (dest_hw != NULL)
398 memcpy(arp_ptr, dest_hw, dev->addr_len);
399 else
400 memset(arp_ptr, 0, dev->addr_len);
401 arp_ptr+=dev->addr_len;
402 memcpy(arp_ptr, &dest_ip, 4);
403
404 dev_queue_xmit(skb, dev, 0);
405 }
406
407
408
409
410
411
412
413 static void arp_expire_request (unsigned long arg)
414 {
415 struct arp_table *entry = (struct arp_table *) arg;
416 struct arp_table **pentry;
417 unsigned long hash;
418 unsigned long flags;
419
420 save_flags(flags);
421 cli();
422
423
424
425
426
427
428
429 if (entry->flags & ATF_COM)
430 {
431 restore_flags(flags);
432 return;
433 }
434
435 if (--entry->retries > 0)
436 {
437 u32 ip = entry->ip;
438 struct device *dev = entry->dev;
439
440
441 del_timer(&entry->timer);
442 entry->timer.expires = jiffies + ARP_RES_TIME;
443 add_timer(&entry->timer);
444 restore_flags(flags);
445 arp_send(ARPOP_REQUEST, ETH_P_ARP, ip, dev, dev->pa_addr,
446 NULL, dev->dev_addr);
447 return;
448 }
449
450
451
452
453
454
455
456
457 hash = HASH(entry->ip);
458
459
460
461
462
463
464
465
466
467 if (entry->flags & ATF_PUBL)
468 pentry = &arp_tables[PROXY_HASH];
469 else
470 pentry = &arp_tables[hash];
471 while (*pentry != NULL)
472 {
473 if (*pentry == entry)
474 {
475 *pentry = entry->next;
476 del_timer(&entry->timer);
477 restore_flags(flags);
478 arp_release_entry(entry);
479 arp_cache_stamp++;
480 return;
481 }
482 pentry = &(*pentry)->next;
483 }
484 restore_flags(flags);
485 printk("Possible ARP queue corruption.\n");
486
487
488
489
490
491
492
493 }
494
495
496
497
498
499
500 static void arp_send_q(struct arp_table *entry, unsigned char *hw_dest)
501 {
502 struct sk_buff *skb;
503
504 unsigned long flags;
505
506
507
508
509
510 if(!(entry->flags&ATF_COM))
511 {
512 printk("arp_send_q: incomplete entry for %s\n",
513 in_ntoa(entry->ip));
514
515
516
517
518 return;
519 }
520
521 save_flags(flags);
522
523 cli();
524 while((skb = skb_dequeue(&entry->skb)) != NULL)
525 {
526 IS_SKB(skb);
527 skb_device_lock(skb);
528 restore_flags(flags);
529 if(!skb->dev->rebuild_header(skb->data,skb->dev,skb->raddr,skb))
530 {
531 skb->arp = 1;
532 if(skb->sk==NULL)
533 dev_queue_xmit(skb, skb->dev, 0);
534 else
535 dev_queue_xmit(skb,skb->dev,skb->sk->priority);
536 }
537 }
538 restore_flags(flags);
539 }
540
541
542
543
544
545
546 void arp_destroy(u32 ip_addr, int force)
547 {
548 int checked_proxies = 0;
549 struct arp_table *entry;
550 struct arp_table **pentry;
551 unsigned long hash = HASH(ip_addr);
552
553 ugly:
554 cli();
555 pentry = &arp_tables[hash];
556 if (! *pentry)
557 pentry = &arp_tables[PROXY_HASH];
558
559 while ((entry = *pentry) != NULL)
560 {
561 if (entry->ip == ip_addr)
562 {
563 if ((entry->flags & ATF_PERM) && !force) {
564 sti();
565 return;
566 }
567 *pentry = entry->next;
568 del_timer(&entry->timer);
569 sti();
570 arp_release_entry(entry);
571
572 goto ugly;
573
574
575
576
577 }
578 pentry = &entry->next;
579 if (!checked_proxies && ! *pentry)
580 {
581
582 checked_proxies = 1;
583 pentry = &arp_tables[PROXY_HASH];
584 }
585 }
586 sti();
587 }
588
589
590
591
592
593
594
595
596 int arp_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
597 {
598
599
600
601
602 struct arphdr *arp = (struct arphdr *)skb->h.raw;
603 unsigned char *arp_ptr= (unsigned char *)(arp+1);
604 struct arp_table *entry;
605 struct arp_table *proxy_entry;
606 int addr_hint,hlen,htype;
607 unsigned long hash;
608 unsigned char ha[MAX_ADDR_LEN];
609 unsigned char *sha,*tha;
610 u32 sip,tip;
611
612
613
614
615
616
617
618
619 if (arp->ar_hln != dev->addr_len ||
620 dev->type != ntohs(arp->ar_hrd) ||
621 dev->flags & IFF_NOARP ||
622 arp->ar_pln != 4)
623 {
624 kfree_skb(skb, FREE_READ);
625 return 0;
626
627
628 }
629
630
631
632
633
634
635
636
637
638 switch(dev->type)
639 {
640 #ifdef CONFIG_AX25
641 case ARPHRD_AX25:
642 if(arp->ar_pro != htons(AX25_P_IP))
643 {
644 kfree_skb(skb, FREE_READ);
645 return 0;
646 }
647 break;
648 #endif
649 #ifdef CONFIG_NETROM
650 case ARPHRD_NETROM:
651 if(arp->ar_pro != htons(AX25_P_IP))
652 {
653 kfree_skb(skb, FREE_READ);
654 return 0;
655 }
656 break;
657 #endif
658 case ARPHRD_ETHER:
659 case ARPHRD_ARCNET:
660 if(arp->ar_pro != htons(ETH_P_IP))
661 {
662 kfree_skb(skb, FREE_READ);
663 return 0;
664 }
665 break;
666
667 case ARPHRD_IEEE802:
668 if(arp->ar_pro != htons(ETH_P_IP))
669 {
670 kfree_skb(skb, FREE_READ);
671 return 0;
672 }
673 break;
674
675 default:
676 printk("ARP: dev->type mangled!\n");
677 kfree_skb(skb, FREE_READ);
678 return 0;
679 }
680
681
682
683
684
685 hlen = dev->addr_len;
686 htype = dev->type;
687
688 sha=arp_ptr;
689 arp_ptr+=hlen;
690 memcpy(&sip,arp_ptr,4);
691 arp_ptr+=4;
692 tha=arp_ptr;
693 arp_ptr+=hlen;
694 memcpy(&tip,arp_ptr,4);
695
696
697
698
699
700 if (LOOPBACK(tip) || MULTICAST(tip))
701 {
702 kfree_skb(skb, FREE_READ);
703 return 0;
704 }
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723 addr_hint = ip_chk_addr(tip);
724
725 if(arp->ar_op == htons(ARPOP_REPLY))
726 {
727 if(addr_hint!=IS_MYADDR)
728 {
729
730
731
732
733
734
735
736 kfree_skb(skb, FREE_READ);
737 return 0;
738 }
739
740
741
742 }
743 else
744 {
745
746
747
748
749
750
751 if(tip!=dev->pa_addr)
752 {
753
754
755
756
757
758 cli();
759 for(proxy_entry=arp_tables[PROXY_HASH];
760 proxy_entry;
761 proxy_entry = proxy_entry->next)
762 {
763
764
765
766
767
768
769
770
771 if (proxy_entry->dev != dev && proxy_entry->htype == htype &&
772 !((proxy_entry->ip^tip)&proxy_entry->mask))
773 break;
774
775 }
776 if (proxy_entry)
777 {
778 memcpy(ha, proxy_entry->ha, hlen);
779 sti();
780 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,ha);
781 kfree_skb(skb, FREE_READ);
782 return 0;
783 }
784 else
785 {
786 sti();
787 kfree_skb(skb, FREE_READ);
788 return 0;
789 }
790 }
791 else
792 {
793
794
795
796 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr);
797 }
798 }
799
800
801
802
803
804
805
806
807 hash = HASH(sip);
808 cli();
809 for(entry=arp_tables[hash];entry;entry=entry->next)
810 if(entry->ip==sip && entry->htype==htype)
811 break;
812
813 if(entry)
814 {
815
816
817
818 if (!(entry->flags & ATF_PERM)) {
819 memcpy(entry->ha, sha, hlen);
820 entry->hlen = hlen;
821 entry->last_used = jiffies;
822 }
823 if (!(entry->flags & ATF_COM))
824 {
825
826
827
828
829 del_timer(&entry->timer);
830 entry->flags |= ATF_COM;
831 sti();
832
833
834
835
836
837 arp_send_q(entry, sha);
838 }
839 else
840 {
841 sti();
842 }
843 }
844 else
845 {
846
847
848
849 entry = (struct arp_table *)kmalloc(sizeof(struct arp_table),GFP_ATOMIC);
850 if(entry == NULL)
851 {
852 sti();
853 printk("ARP: no memory for new arp entry\n");
854
855 kfree_skb(skb, FREE_READ);
856 return 0;
857 }
858
859 entry->mask = DEF_ARP_NETMASK;
860 entry->ip = sip;
861 entry->hlen = hlen;
862 entry->htype = htype;
863 entry->flags = ATF_COM;
864 init_timer(&entry->timer);
865 memcpy(entry->ha, sha, hlen);
866 entry->last_used = jiffies;
867 entry->dev = skb->dev;
868 skb_queue_head_init(&entry->skb);
869 entry->next = arp_tables[hash];
870 arp_tables[hash] = entry;
871 sti();
872 }
873
874
875
876
877 kfree_skb(skb, FREE_READ);
878 return 0;
879 }
880
881
882
883
884
885
886 int arp_query(unsigned char *haddr, u32 paddr, unsigned short type)
887 {
888 struct arp_table *entry;
889 unsigned long hash = HASH(paddr);
890
891
892
893
894 cli();
895
896 for (entry = arp_tables[hash]; entry != NULL; entry = entry->next)
897 if (entry->ip == paddr && entry->htype == type)
898 break;
899
900 if (entry != NULL) {
901
902
903
904
905 entry->last_used = jiffies;
906 memcpy(haddr, entry->ha, entry->hlen);
907 sti();
908 return 1;
909 }
910
911 sti();
912 return 0;
913 }
914
915
916
917
918
919 int arp_find(unsigned char *haddr, u32 paddr, struct device *dev,
920 u32 saddr, struct sk_buff *skb)
921 {
922 struct arp_table *entry;
923 unsigned long hash;
924 #ifdef CONFIG_IP_MULTICAST
925 u32 taddr;
926 #endif
927
928 switch (ip_chk_addr(paddr))
929 {
930 case IS_MYADDR:
931 printk("ARP: arp called for own IP address\n");
932 memcpy(haddr, dev->dev_addr, dev->addr_len);
933 skb->arp = 1;
934 return 0;
935 #ifdef CONFIG_IP_MULTICAST
936 case IS_MULTICAST:
937 if(dev->type==ARPHRD_ETHER || dev->type==ARPHRD_IEEE802)
938 {
939
940 haddr[0]=0x01;
941 haddr[1]=0x00;
942 haddr[2]=0x5e;
943 taddr=ntohl(paddr);
944 haddr[5]=taddr&0xff;
945 taddr=taddr>>8;
946 haddr[4]=taddr&0xff;
947 taddr=taddr>>8;
948 haddr[3]=taddr&0x7f;
949 return 0;
950 }
951
952
953
954 #endif
955
956 case IS_BROADCAST:
957 memcpy(haddr, dev->broadcast, dev->addr_len);
958 skb->arp = 1;
959 return 0;
960 }
961
962 hash = HASH(paddr);
963 cli();
964
965
966
967
968 entry = arp_lookup(paddr, PROXY_NONE, dev->type);
969
970 if (entry != NULL)
971 {
972 if (!(entry->flags & ATF_COM))
973 {
974
975
976
977
978
979 if (skb != NULL)
980 {
981 skb_queue_tail(&entry->skb, skb);
982 skb_device_unlock(skb);
983 }
984 sti();
985 return 1;
986 }
987
988
989
990
991
992 entry->last_used = jiffies;
993 memcpy(haddr, entry->ha, dev->addr_len);
994 if (skb)
995 skb->arp = 1;
996 sti();
997 return 0;
998 }
999
1000
1001
1002
1003
1004 entry = (struct arp_table *) kmalloc(sizeof(struct arp_table),
1005 GFP_ATOMIC);
1006 if (entry != NULL)
1007 {
1008 entry->next = arp_tables[hash];
1009 entry->last_used = jiffies;
1010 entry->flags = 0;
1011 entry->ip = paddr;
1012 entry->mask = DEF_ARP_NETMASK;
1013 memset(entry->ha, 0, dev->addr_len);
1014 entry->hlen = dev->addr_len;
1015 entry->htype = dev->type;
1016 entry->dev = dev;
1017 init_timer(&entry->timer);
1018 entry->timer.function = arp_expire_request;
1019 entry->timer.data = (unsigned long)entry;
1020 entry->timer.expires = jiffies + ARP_RES_TIME;
1021 arp_tables[hash] = entry;
1022 add_timer(&entry->timer);
1023 entry->retries = ARP_MAX_TRIES;
1024 skb_queue_head_init(&entry->skb);
1025 if (skb != NULL)
1026 {
1027 skb_queue_tail(&entry->skb, skb);
1028 skb_device_unlock(skb);
1029 }
1030 }
1031 else
1032 {
1033 if (skb != NULL && skb->free)
1034 kfree_skb(skb, FREE_WRITE);
1035 }
1036 sti();
1037
1038
1039
1040
1041
1042 arp_send(ARPOP_REQUEST, ETH_P_ARP, paddr, dev, saddr, NULL,
1043 dev->dev_addr);
1044
1045 return 1;
1046 }
1047
1048
1049
1050
1051
1052
1053 #define HBUFFERLEN 30
1054
1055 int arp_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1056 {
1057 int len=0;
1058 off_t begin=0;
1059 off_t pos=0;
1060 int size;
1061 struct arp_table *entry;
1062 char hbuffer[HBUFFERLEN];
1063 int i,j,k;
1064 const char hexbuf[] = "0123456789ABCDEF";
1065
1066 size = sprintf(buffer,"IP address HW type Flags HW address Mask\n");
1067
1068 pos+=size;
1069 len+=size;
1070
1071 cli();
1072 for(i=0; i<FULL_ARP_TABLE_SIZE; i++)
1073 {
1074 for(entry=arp_tables[i]; entry!=NULL; entry=entry->next)
1075 {
1076
1077
1078
1079 #ifdef CONFIG_AX25
1080 #ifdef CONFIG_NETROM
1081 if (entry->htype == ARPHRD_AX25 || entry->htype == ARPHRD_NETROM)
1082 strcpy(hbuffer,ax2asc((ax25_address *)entry->ha));
1083 else {
1084 #else
1085 if(entry->htype==ARPHRD_AX25)
1086 strcpy(hbuffer,ax2asc((ax25_address *)entry->ha));
1087 else {
1088 #endif
1089 #endif
1090
1091 for(k=0,j=0;k<HBUFFERLEN-3 && j<entry->hlen;j++)
1092 {
1093 hbuffer[k++]=hexbuf[ (entry->ha[j]>>4)&15 ];
1094 hbuffer[k++]=hexbuf[ entry->ha[j]&15 ];
1095 hbuffer[k++]=':';
1096 }
1097 hbuffer[--k]=0;
1098
1099 #ifdef CONFIG_AX25
1100 }
1101 #endif
1102 size = sprintf(buffer+len,
1103 "%-17s0x%-10x0x%-10x%s",
1104 in_ntoa(entry->ip),
1105 (unsigned int)entry->htype,
1106 entry->flags,
1107 hbuffer);
1108 size += sprintf(buffer+len+size,
1109 " %-17s\n",
1110 entry->mask==DEF_ARP_NETMASK?
1111 "*":in_ntoa(entry->mask));
1112
1113 len+=size;
1114 pos=begin+len;
1115
1116 if(pos<offset)
1117 {
1118 len=0;
1119 begin=pos;
1120 }
1121 if(pos>offset+length)
1122 break;
1123 }
1124 }
1125 sti();
1126
1127 *start=buffer+(offset-begin);
1128 len-=(offset-begin);
1129 if(len>length)
1130 len=length;
1131 return len;
1132 }
1133
1134
1135
1136
1137
1138
1139
1140
1141 static struct arp_table *arp_lookup(u32 paddr, enum proxy proxy, unsigned short type)
1142 {
1143 struct arp_table *entry;
1144 unsigned long hash = HASH(paddr);
1145
1146 for (entry = arp_tables[hash]; entry != NULL; entry = entry->next)
1147 if (entry->ip == paddr && entry->htype == type)
1148 break;
1149
1150
1151 if (!entry && proxy != PROXY_NONE)
1152 for (entry=arp_tables[PROXY_HASH]; entry != NULL; entry = entry->next)
1153 if ((proxy==PROXY_EXACT) ? (entry->ip==paddr)
1154 : !((entry->ip^paddr)&entry->mask))
1155 break;
1156
1157 return entry;
1158 }
1159
1160
1161 int arp_find_cache(unsigned char *dp, u32 daddr, struct device *dev)
1162 {
1163
1164
1165
1166 struct arp_table *entry;
1167 #ifdef CONFIG_IP_MULTICAST
1168 u32 taddr;
1169 #endif
1170
1171 switch (ip_chk_addr(daddr))
1172 {
1173 case IS_MYADDR:
1174 printk("ARP: arp called for own IP address\n");
1175 memcpy(dp, dev->dev_addr, dev->addr_len);
1176 return 1;
1177 #ifdef CONFIG_IP_MULTICAST
1178 case IS_MULTICAST:
1179 if(dev->type==ARPHRD_ETHER || dev->type==ARPHRD_IEEE802)
1180 {
1181 dp[0]=0x01;
1182 dp[1]=0x00;
1183 dp[2]=0x5e;
1184 taddr=ntohl(daddr);
1185 dp[5]=taddr&0xff;
1186 taddr=taddr>>8;
1187 dp[4]=taddr&0xff;
1188 taddr=taddr>>8;
1189 dp[3]=taddr&0x7f;
1190 return 1;
1191 }
1192
1193
1194
1195 #endif
1196
1197 case IS_BROADCAST:
1198 memcpy(dp, dev->broadcast, dev->addr_len);
1199 return 1;
1200
1201 default:
1202 entry=arp_lookup(daddr, PROXY_NONE, dev->type);
1203 if(entry)
1204 {
1205 memcpy(dp,entry->ha, ETH_ALEN);
1206 return 1;
1207 }
1208 }
1209 return 0;
1210 }
1211
1212
1213
1214
1215
1216 static int arp_req_set(struct arpreq *req)
1217 {
1218 struct arpreq r;
1219 struct arp_table *entry;
1220 struct sockaddr_in *si;
1221 int htype, hlen;
1222 struct rtable *rt;
1223 u32 ip;
1224
1225 memcpy_fromfs(&r, req, sizeof(r));
1226
1227
1228 if (r.arp_pa.sa_family != AF_INET)
1229 return -EPFNOSUPPORT;
1230
1231
1232
1233
1234
1235
1236
1237 switch (r.arp_ha.sa_family) {
1238 case ARPHRD_ETHER:
1239 htype = ARPHRD_ETHER;
1240 hlen = ETH_ALEN;
1241 break;
1242
1243 case ARPHRD_ARCNET:
1244 htype = ARPHRD_ARCNET;
1245 hlen = 1;
1246 break;
1247
1248 #ifdef CONFIG_AX25
1249 case ARPHRD_AX25:
1250 htype = ARPHRD_AX25;
1251 hlen = AX25_ADDR_LEN;
1252 break;
1253 #endif
1254 #ifdef CONFIG_NETROM
1255 case ARPHRD_NETROM:
1256 htype = ARPHRD_NETROM;
1257 hlen = AX25_ADDR_LEN;
1258 break;
1259 #endif
1260 case ARPHRD_IEEE802:
1261 htype = ARPHRD_IEEE802;
1262 hlen = TR_ALEN;
1263 break;
1264 default:
1265 return -EPFNOSUPPORT;
1266 }
1267
1268 si = (struct sockaddr_in *) &r.arp_pa;
1269 ip = si->sin_addr.s_addr;
1270 if (ip == 0)
1271 {
1272 printk("ARP: SETARP: requested PA is 0.0.0.0 !\n");
1273 return -EINVAL;
1274 }
1275
1276
1277
1278
1279
1280 rt = ip_rt_route(ip, NULL, NULL);
1281 if (rt == NULL)
1282 return -ENETUNREACH;
1283
1284
1285
1286
1287
1288 cli();
1289
1290
1291
1292
1293 entry = arp_lookup(ip, PROXY_EXACT, htype);
1294 if (entry && (entry->flags & ATF_PUBL) != (r.arp_flags & ATF_PUBL))
1295 {
1296 sti();
1297 arp_destroy(ip,1);
1298 cli();
1299 entry = NULL;
1300 }
1301
1302
1303
1304
1305
1306 if (entry == NULL)
1307 {
1308 unsigned long hash = HASH(ip);
1309 if (r.arp_flags & ATF_PUBL)
1310 hash = PROXY_HASH;
1311
1312 entry = (struct arp_table *) kmalloc(sizeof(struct arp_table),
1313 GFP_ATOMIC);
1314 if (entry == NULL)
1315 {
1316 sti();
1317 return -ENOMEM;
1318 }
1319 entry->ip = ip;
1320 entry->hlen = hlen;
1321 entry->htype = htype;
1322 init_timer(&entry->timer);
1323 entry->next = arp_tables[hash];
1324 arp_tables[hash] = entry;
1325 skb_queue_head_init(&entry->skb);
1326 }
1327
1328
1329
1330
1331 memcpy(&entry->ha, &r.arp_ha.sa_data, hlen);
1332 entry->last_used = jiffies;
1333 entry->flags = r.arp_flags | ATF_COM;
1334 if ((entry->flags & ATF_PUBL) && (entry->flags & ATF_NETMASK))
1335 {
1336 si = (struct sockaddr_in *) &r.arp_netmask;
1337 entry->mask = si->sin_addr.s_addr;
1338 }
1339 else
1340 entry->mask = DEF_ARP_NETMASK;
1341 entry->dev = rt->rt_dev;
1342 arp_cache_stamp++;
1343 sti();
1344
1345 return 0;
1346 }
1347
1348
1349
1350
1351
1352
1353 static int arp_req_get(struct arpreq *req)
1354 {
1355 struct arpreq r;
1356 struct arp_table *entry;
1357 struct sockaddr_in *si;
1358
1359
1360
1361
1362
1363 memcpy_fromfs(&r, req, sizeof(r));
1364
1365 if (r.arp_pa.sa_family != AF_INET)
1366 return -EPFNOSUPPORT;
1367
1368
1369
1370
1371
1372 si = (struct sockaddr_in *) &r.arp_pa;
1373 cli();
1374 entry = arp_lookup(si->sin_addr.s_addr, PROXY_ANY, r.arp_ha.sa_family);
1375
1376 if (entry == NULL)
1377 {
1378 sti();
1379 return -ENXIO;
1380 }
1381
1382
1383
1384
1385
1386 memcpy(r.arp_ha.sa_data, &entry->ha, entry->hlen);
1387 r.arp_ha.sa_family = entry->htype;
1388 r.arp_flags = entry->flags;
1389 sti();
1390
1391
1392
1393
1394
1395 memcpy_tofs(req, &r, sizeof(r));
1396 return 0;
1397 }
1398
1399
1400
1401
1402
1403
1404 int arp_ioctl(unsigned int cmd, void *arg)
1405 {
1406 struct arpreq r;
1407 struct sockaddr_in *si;
1408 int err;
1409
1410 switch(cmd)
1411 {
1412 case SIOCDARP:
1413 if (!suser())
1414 return -EPERM;
1415 err = verify_area(VERIFY_READ, arg, sizeof(struct arpreq));
1416 if(err)
1417 return err;
1418 memcpy_fromfs(&r, arg, sizeof(r));
1419 if (r.arp_pa.sa_family != AF_INET)
1420 return -EPFNOSUPPORT;
1421 si = (struct sockaddr_in *) &r.arp_pa;
1422 arp_destroy(si->sin_addr.s_addr, 1);
1423 return 0;
1424 case SIOCGARP:
1425 err = verify_area(VERIFY_WRITE, arg, sizeof(struct arpreq));
1426 if(err)
1427 return err;
1428 return arp_req_get((struct arpreq *)arg);
1429 case SIOCSARP:
1430 if (!suser())
1431 return -EPERM;
1432 err = verify_area(VERIFY_READ, arg, sizeof(struct arpreq));
1433 if(err)
1434 return err;
1435 return arp_req_set((struct arpreq *)arg);
1436 default:
1437 return -EINVAL;
1438 }
1439
1440 return 0;
1441 }
1442
1443
1444
1445
1446
1447
1448 static struct packet_type arp_packet_type =
1449 {
1450 0,
1451 NULL,
1452 arp_rcv,
1453 NULL,
1454 NULL
1455 };
1456
1457 static struct notifier_block arp_dev_notifier={
1458 arp_device_event,
1459 NULL,
1460 0
1461 };
1462
1463 void arp_init (void)
1464 {
1465
1466 arp_packet_type.type=htons(ETH_P_ARP);
1467 dev_add_pack(&arp_packet_type);
1468
1469 add_timer(&arp_timer);
1470
1471 register_netdevice_notifier(&arp_dev_notifier);
1472
1473 proc_net_register(&(struct proc_dir_entry) {
1474 PROC_NET_ARP, 3, "arp",
1475 S_IFREG | S_IRUGO, 1, 0, 0,
1476 0, &proc_net_inode_operations,
1477 arp_get_info
1478 });
1479 }
1480