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