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