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