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