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