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#include <linux/if_arp.h>
33#include <linux/slab.h>
34#include <net/sock.h>
35#include <net/datalink.h>
36#include <net/psnap.h>
37#include <linux/atalk.h>
38#include <linux/delay.h>
39#include <linux/init.h>
40#include <linux/proc_fs.h>
41#include <linux/seq_file.h>
42
43int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME;
44int sysctl_aarp_tick_time = AARP_TICK_TIME;
45int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT;
46int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61struct aarp_entry {
62
63 unsigned long last_sent;
64 struct sk_buff_head packet_queue;
65 int status;
66 unsigned long expires_at;
67 struct atalk_addr target_addr;
68 struct net_device *dev;
69 char hwaddr[6];
70 unsigned short xmit_count;
71 struct aarp_entry *next;
72};
73
74
75static struct aarp_entry *resolved[AARP_HASH_SIZE];
76static struct aarp_entry *unresolved[AARP_HASH_SIZE];
77static struct aarp_entry *proxies[AARP_HASH_SIZE];
78static int unresolved_count;
79
80
81static DEFINE_RWLOCK(aarp_lock);
82
83
84static struct timer_list aarp_timer;
85
86
87
88
89
90
91static void __aarp_expire(struct aarp_entry *a)
92{
93 skb_queue_purge(&a->packet_queue);
94 kfree(a);
95}
96
97
98
99
100
101
102static void __aarp_send_query(struct aarp_entry *a)
103{
104 static unsigned char aarp_eth_multicast[ETH_ALEN] =
105 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
106 struct net_device *dev = a->dev;
107 struct elapaarp *eah;
108 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
109 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
110 struct atalk_addr *sat = atalk_find_dev_addr(dev);
111
112 if (!skb)
113 return;
114
115 if (!sat) {
116 kfree_skb(skb);
117 return;
118 }
119
120
121 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
122 skb_reset_network_header(skb);
123 skb_reset_transport_header(skb);
124 skb_put(skb, sizeof(*eah));
125 skb->protocol = htons(ETH_P_ATALK);
126 skb->dev = dev;
127 eah = aarp_hdr(skb);
128
129
130 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
131 eah->pa_type = htons(ETH_P_ATALK);
132 eah->hw_len = ETH_ALEN;
133 eah->pa_len = AARP_PA_ALEN;
134 eah->function = htons(AARP_REQUEST);
135
136 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
137
138 eah->pa_src_zero = 0;
139 eah->pa_src_net = sat->s_net;
140 eah->pa_src_node = sat->s_node;
141
142 memset(eah->hw_dst, '\0', ETH_ALEN);
143
144 eah->pa_dst_zero = 0;
145 eah->pa_dst_net = a->target_addr.s_net;
146 eah->pa_dst_node = a->target_addr.s_node;
147
148
149 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
150
151 a->xmit_count++;
152 a->last_sent = jiffies;
153}
154
155
156
157static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
158 struct atalk_addr *them, unsigned char *sha)
159{
160 struct elapaarp *eah;
161 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
162 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
163
164 if (!skb)
165 return;
166
167
168 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
169 skb_reset_network_header(skb);
170 skb_reset_transport_header(skb);
171 skb_put(skb, sizeof(*eah));
172 skb->protocol = htons(ETH_P_ATALK);
173 skb->dev = dev;
174 eah = aarp_hdr(skb);
175
176
177 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
178 eah->pa_type = htons(ETH_P_ATALK);
179 eah->hw_len = ETH_ALEN;
180 eah->pa_len = AARP_PA_ALEN;
181 eah->function = htons(AARP_REPLY);
182
183 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
184
185 eah->pa_src_zero = 0;
186 eah->pa_src_net = us->s_net;
187 eah->pa_src_node = us->s_node;
188
189 if (!sha)
190 memset(eah->hw_dst, '\0', ETH_ALEN);
191 else
192 memcpy(eah->hw_dst, sha, ETH_ALEN);
193
194 eah->pa_dst_zero = 0;
195 eah->pa_dst_net = them->s_net;
196 eah->pa_dst_node = them->s_node;
197
198
199 aarp_dl->request(aarp_dl, skb, sha);
200}
201
202
203
204
205
206
207static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
208{
209 struct elapaarp *eah;
210 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
211 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
212 static unsigned char aarp_eth_multicast[ETH_ALEN] =
213 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
214
215 if (!skb)
216 return;
217
218
219 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
220 skb_reset_network_header(skb);
221 skb_reset_transport_header(skb);
222 skb_put(skb, sizeof(*eah));
223 skb->protocol = htons(ETH_P_ATALK);
224 skb->dev = dev;
225 eah = aarp_hdr(skb);
226
227
228 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
229 eah->pa_type = htons(ETH_P_ATALK);
230 eah->hw_len = ETH_ALEN;
231 eah->pa_len = AARP_PA_ALEN;
232 eah->function = htons(AARP_PROBE);
233
234 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
235
236 eah->pa_src_zero = 0;
237 eah->pa_src_net = us->s_net;
238 eah->pa_src_node = us->s_node;
239
240 memset(eah->hw_dst, '\0', ETH_ALEN);
241
242 eah->pa_dst_zero = 0;
243 eah->pa_dst_net = us->s_net;
244 eah->pa_dst_node = us->s_node;
245
246
247 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
248}
249
250
251
252
253
254
255
256static void __aarp_expire_timer(struct aarp_entry **n)
257{
258 struct aarp_entry *t;
259
260 while (*n)
261
262 if (time_after(jiffies, (*n)->expires_at)) {
263 t = *n;
264 *n = (*n)->next;
265 __aarp_expire(t);
266 } else
267 n = &((*n)->next);
268}
269
270
271
272
273
274
275static void __aarp_kick(struct aarp_entry **n)
276{
277 struct aarp_entry *t;
278
279 while (*n)
280
281 if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
282 t = *n;
283 *n = (*n)->next;
284 __aarp_expire(t);
285 } else {
286 __aarp_send_query(*n);
287 n = &((*n)->next);
288 }
289}
290
291
292
293
294
295
296
297static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
298{
299 struct aarp_entry *t;
300
301 while (*n)
302 if ((*n)->dev == dev) {
303 t = *n;
304 *n = (*n)->next;
305 __aarp_expire(t);
306 } else
307 n = &((*n)->next);
308}
309
310
311static void aarp_expire_timeout(unsigned long unused)
312{
313 int ct;
314
315 write_lock_bh(&aarp_lock);
316
317 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
318 __aarp_expire_timer(&resolved[ct]);
319 __aarp_kick(&unresolved[ct]);
320 __aarp_expire_timer(&unresolved[ct]);
321 __aarp_expire_timer(&proxies[ct]);
322 }
323
324 write_unlock_bh(&aarp_lock);
325 mod_timer(&aarp_timer, jiffies +
326 (unresolved_count ? sysctl_aarp_tick_time :
327 sysctl_aarp_expiry_time));
328}
329
330
331static int aarp_device_event(struct notifier_block *this, unsigned long event,
332 void *ptr)
333{
334 struct net_device *dev = ptr;
335 int ct;
336
337 if (!net_eq(dev_net(dev), &init_net))
338 return NOTIFY_DONE;
339
340 if (event == NETDEV_DOWN) {
341 write_lock_bh(&aarp_lock);
342
343 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
344 __aarp_expire_device(&resolved[ct], dev);
345 __aarp_expire_device(&unresolved[ct], dev);
346 __aarp_expire_device(&proxies[ct], dev);
347 }
348
349 write_unlock_bh(&aarp_lock);
350 }
351 return NOTIFY_DONE;
352}
353
354
355static void __aarp_expire_all(struct aarp_entry **n)
356{
357 struct aarp_entry *t;
358
359 while (*n) {
360 t = *n;
361 *n = (*n)->next;
362 __aarp_expire(t);
363 }
364}
365
366
367static void aarp_purge(void)
368{
369 int ct;
370
371 write_lock_bh(&aarp_lock);
372 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
373 __aarp_expire_all(&resolved[ct]);
374 __aarp_expire_all(&unresolved[ct]);
375 __aarp_expire_all(&proxies[ct]);
376 }
377 write_unlock_bh(&aarp_lock);
378}
379
380
381
382
383
384static struct aarp_entry *aarp_alloc(void)
385{
386 struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
387
388 if (a)
389 skb_queue_head_init(&a->packet_queue);
390 return a;
391}
392
393
394
395
396
397
398
399static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
400 struct net_device *dev,
401 struct atalk_addr *sat)
402{
403 while (list) {
404 if (list->target_addr.s_net == sat->s_net &&
405 list->target_addr.s_node == sat->s_node &&
406 list->dev == dev)
407 break;
408 list = list->next;
409 }
410
411 return list;
412}
413
414
415void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
416{
417 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
418 struct aarp_entry *a;
419
420 write_lock_bh(&aarp_lock);
421
422 a = __aarp_find_entry(proxies[hash], dev, sa);
423 if (a)
424 a->expires_at = jiffies - 1;
425
426 write_unlock_bh(&aarp_lock);
427}
428
429
430static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
431 struct atalk_addr *sa)
432{
433 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
434 struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
435
436 return a ? sa : NULL;
437}
438
439
440
441
442
443static void aarp_send_probe_phase1(struct atalk_iface *iface)
444{
445 struct ifreq atreq;
446 struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
447 const struct net_device_ops *ops = iface->dev->netdev_ops;
448
449 sa->sat_addr.s_node = iface->address.s_node;
450 sa->sat_addr.s_net = ntohs(iface->address.s_net);
451
452
453 if (!(ops->ndo_do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
454 ops->ndo_do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
455 if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
456 iface->address.s_node != sa->sat_addr.s_node)
457 iface->status |= ATIF_PROBE_FAIL;
458
459 iface->address.s_net = htons(sa->sat_addr.s_net);
460 iface->address.s_node = sa->sat_addr.s_node;
461 }
462}
463
464
465void aarp_probe_network(struct atalk_iface *atif)
466{
467 if (atif->dev->type == ARPHRD_LOCALTLK ||
468 atif->dev->type == ARPHRD_PPP)
469 aarp_send_probe_phase1(atif);
470 else {
471 unsigned int count;
472
473 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
474 aarp_send_probe(atif->dev, &atif->address);
475
476
477 msleep(100);
478
479 if (atif->status & ATIF_PROBE_FAIL)
480 break;
481 }
482 }
483}
484
485int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
486{
487 int hash, retval = -EPROTONOSUPPORT;
488 struct aarp_entry *entry;
489 unsigned int count;
490
491
492
493
494
495 if (atif->dev->type == ARPHRD_LOCALTLK ||
496 atif->dev->type == ARPHRD_PPP)
497 goto out;
498
499
500
501
502
503 entry = aarp_alloc();
504 retval = -ENOMEM;
505 if (!entry)
506 goto out;
507
508 entry->expires_at = -1;
509 entry->status = ATIF_PROBE;
510 entry->target_addr.s_node = sa->s_node;
511 entry->target_addr.s_net = sa->s_net;
512 entry->dev = atif->dev;
513
514 write_lock_bh(&aarp_lock);
515
516 hash = sa->s_node % (AARP_HASH_SIZE - 1);
517 entry->next = proxies[hash];
518 proxies[hash] = entry;
519
520 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
521 aarp_send_probe(atif->dev, sa);
522
523
524 write_unlock_bh(&aarp_lock);
525 msleep(100);
526 write_lock_bh(&aarp_lock);
527
528 if (entry->status & ATIF_PROBE_FAIL)
529 break;
530 }
531
532 if (entry->status & ATIF_PROBE_FAIL) {
533 entry->expires_at = jiffies - 1;
534 retval = -EADDRINUSE;
535 } else {
536 entry->status &= ~ATIF_PROBE;
537 retval = 1;
538 }
539
540 write_unlock_bh(&aarp_lock);
541out:
542 return retval;
543}
544
545
546int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
547 struct atalk_addr *sa, void *hwaddr)
548{
549 static char ddp_eth_multicast[ETH_ALEN] =
550 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
551 int hash;
552 struct aarp_entry *a;
553
554 skb_reset_network_header(skb);
555
556
557 if (dev->type == ARPHRD_LOCALTLK) {
558 struct atalk_addr *at = atalk_find_dev_addr(dev);
559 struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
560 int ft = 2;
561
562
563
564
565
566
567
568
569 if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
570 (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
571 skb_pull(skb, sizeof(*ddp) - 4);
572
573
574
575
576
577
578 *((__be16 *)skb->data) = htons(skb->len);
579 ft = 1;
580 }
581
582
583
584
585
586 skb_push(skb, 3);
587 skb->data[0] = sa->s_node;
588 skb->data[1] = at->s_node;
589 skb->data[2] = ft;
590 skb->dev = dev;
591 goto sendit;
592 }
593
594
595 if (dev->type == ARPHRD_PPP) {
596 skb->protocol = htons(ETH_P_PPPTALK);
597 skb->dev = dev;
598 goto sendit;
599 }
600
601
602 if (dev->type != ARPHRD_ETHER)
603 goto free_it;
604
605 skb->dev = dev;
606 skb->protocol = htons(ETH_P_ATALK);
607 hash = sa->s_node % (AARP_HASH_SIZE - 1);
608
609
610 if (sa->s_node == ATADDR_BCAST) {
611
612 ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
613 goto sent;
614 }
615
616 write_lock_bh(&aarp_lock);
617 a = __aarp_find_entry(resolved[hash], dev, sa);
618
619 if (a) {
620 a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
621 ddp_dl->request(ddp_dl, skb, a->hwaddr);
622 write_unlock_bh(&aarp_lock);
623 goto sent;
624 }
625
626
627 a = __aarp_find_entry(unresolved[hash], dev, sa);
628 if (a) {
629 skb_queue_tail(&a->packet_queue, skb);
630 goto out_unlock;
631 }
632
633
634 a = aarp_alloc();
635 if (!a) {
636
637 write_unlock_bh(&aarp_lock);
638 goto free_it;
639 }
640
641
642 skb_queue_tail(&a->packet_queue, skb);
643 a->expires_at = jiffies + sysctl_aarp_resolve_time;
644 a->dev = dev;
645 a->next = unresolved[hash];
646 a->target_addr = *sa;
647 a->xmit_count = 0;
648 unresolved[hash] = a;
649 unresolved_count++;
650
651
652 __aarp_send_query(a);
653
654
655
656
657
658
659 if (unresolved_count == 1)
660 mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
661
662
663out_unlock:
664 write_unlock_bh(&aarp_lock);
665
666
667 goto sent;
668
669sendit:
670 if (skb->sk)
671 skb->priority = skb->sk->sk_priority;
672 if (dev_queue_xmit(skb))
673 goto drop;
674sent:
675 return NET_XMIT_SUCCESS;
676free_it:
677 kfree_skb(skb);
678drop:
679 return NET_XMIT_DROP;
680}
681EXPORT_SYMBOL(aarp_send_ddp);
682
683
684
685
686
687
688
689static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
690 int hash)
691{
692 struct sk_buff *skb;
693
694 while (*list)
695 if (*list == a) {
696 unresolved_count--;
697 *list = a->next;
698
699
700 a->next = resolved[hash];
701 resolved[hash] = a;
702
703
704 while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
705 a->expires_at = jiffies +
706 sysctl_aarp_expiry_time * 10;
707 ddp_dl->request(ddp_dl, skb, a->hwaddr);
708 }
709 } else
710 list = &((*list)->next);
711}
712
713
714
715
716
717static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
718 struct packet_type *pt, struct net_device *orig_dev)
719{
720 struct elapaarp *ea = aarp_hdr(skb);
721 int hash, ret = 0;
722 __u16 function;
723 struct aarp_entry *a;
724 struct atalk_addr sa, *ma, da;
725 struct atalk_iface *ifa;
726
727 if (!net_eq(dev_net(dev), &init_net))
728 goto out0;
729
730
731 if (dev->type != ARPHRD_ETHER)
732 goto out0;
733
734
735 if (!skb_pull(skb, sizeof(*ea)))
736 goto out0;
737
738 function = ntohs(ea->function);
739
740
741 if (function < AARP_REQUEST || function > AARP_PROBE ||
742 ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
743 ea->pa_src_zero || ea->pa_dst_zero)
744 goto out0;
745
746
747 hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
748
749
750 sa.s_node = ea->pa_src_node;
751 sa.s_net = ea->pa_src_net;
752
753
754 ifa = atalk_find_dev(dev);
755 if (!ifa)
756 goto out1;
757
758 if (ifa->status & ATIF_PROBE &&
759 ifa->address.s_node == ea->pa_dst_node &&
760 ifa->address.s_net == ea->pa_dst_net) {
761 ifa->status |= ATIF_PROBE_FAIL;
762 goto out1;
763 }
764
765
766 da.s_node = ea->pa_dst_node;
767 da.s_net = ea->pa_dst_net;
768
769 write_lock_bh(&aarp_lock);
770 a = __aarp_find_entry(proxies[hash], dev, &da);
771
772 if (a && a->status & ATIF_PROBE) {
773 a->status |= ATIF_PROBE_FAIL;
774
775
776
777
778 goto unlock;
779 }
780
781 switch (function) {
782 case AARP_REPLY:
783 if (!unresolved_count)
784 break;
785
786
787 a = __aarp_find_entry(unresolved[hash], dev, &sa);
788 if (!a || dev != a->dev)
789 break;
790
791
792 memcpy(a->hwaddr, ea->hw_src, ETH_ALEN);
793 __aarp_resolved(&unresolved[hash], a, hash);
794 if (!unresolved_count)
795 mod_timer(&aarp_timer,
796 jiffies + sysctl_aarp_expiry_time);
797 break;
798
799 case AARP_REQUEST:
800 case AARP_PROBE:
801
802
803
804
805
806
807
808
809
810
811
812
813
814 sa.s_node = ea->pa_dst_node;
815 sa.s_net = ea->pa_dst_net;
816
817
818 ma = __aarp_proxy_find(dev, &sa);
819 if (!ma)
820 ma = &ifa->address;
821 else {
822 da.s_node = sa.s_node;
823 da.s_net = sa.s_net;
824 ma = &da;
825 }
826
827 if (function == AARP_PROBE) {
828
829
830
831
832
833 a = __aarp_find_entry(resolved[sa.s_node %
834 (AARP_HASH_SIZE - 1)],
835 skb->dev, &sa);
836
837
838
839
840
841
842
843 if (a) {
844 a->expires_at = jiffies - 1;
845 mod_timer(&aarp_timer, jiffies +
846 sysctl_aarp_tick_time);
847 }
848 }
849
850 if (sa.s_node != ma->s_node)
851 break;
852
853 if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
854 break;
855
856 sa.s_node = ea->pa_src_node;
857 sa.s_net = ea->pa_src_net;
858
859
860
861 aarp_send_reply(dev, ma, &sa, ea->hw_src);
862 break;
863 }
864
865unlock:
866 write_unlock_bh(&aarp_lock);
867out1:
868 ret = 1;
869out0:
870 kfree_skb(skb);
871 return ret;
872}
873
874static struct notifier_block aarp_notifier = {
875 .notifier_call = aarp_device_event,
876};
877
878static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
879
880void __init aarp_proto_init(void)
881{
882 aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
883 if (!aarp_dl)
884 printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
885 setup_timer(&aarp_timer, aarp_expire_timeout, 0);
886 aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
887 add_timer(&aarp_timer);
888 register_netdevice_notifier(&aarp_notifier);
889}
890
891
892void aarp_device_down(struct net_device *dev)
893{
894 int ct;
895
896 write_lock_bh(&aarp_lock);
897
898 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
899 __aarp_expire_device(&resolved[ct], dev);
900 __aarp_expire_device(&unresolved[ct], dev);
901 __aarp_expire_device(&proxies[ct], dev);
902 }
903
904 write_unlock_bh(&aarp_lock);
905}
906
907#ifdef CONFIG_PROC_FS
908struct aarp_iter_state {
909 int bucket;
910 struct aarp_entry **table;
911};
912
913
914
915
916
917
918
919static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
920{
921 int ct = iter->bucket;
922 struct aarp_entry **table = iter->table;
923 loff_t off = 0;
924 struct aarp_entry *entry;
925
926 rescan:
927 while(ct < AARP_HASH_SIZE) {
928 for (entry = table[ct]; entry; entry = entry->next) {
929 if (!pos || ++off == *pos) {
930 iter->table = table;
931 iter->bucket = ct;
932 return entry;
933 }
934 }
935 ++ct;
936 }
937
938 if (table == resolved) {
939 ct = 0;
940 table = unresolved;
941 goto rescan;
942 }
943 if (table == unresolved) {
944 ct = 0;
945 table = proxies;
946 goto rescan;
947 }
948 return NULL;
949}
950
951static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
952 __acquires(aarp_lock)
953{
954 struct aarp_iter_state *iter = seq->private;
955
956 read_lock_bh(&aarp_lock);
957 iter->table = resolved;
958 iter->bucket = 0;
959
960 return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
961}
962
963static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
964{
965 struct aarp_entry *entry = v;
966 struct aarp_iter_state *iter = seq->private;
967
968 ++*pos;
969
970
971 if (v == SEQ_START_TOKEN)
972 entry = iter_next(iter, NULL);
973
974
975 else if (entry->next)
976 entry = entry->next;
977
978
979 else {
980 ++iter->bucket;
981 entry = iter_next(iter, NULL);
982 }
983 return entry;
984}
985
986static void aarp_seq_stop(struct seq_file *seq, void *v)
987 __releases(aarp_lock)
988{
989 read_unlock_bh(&aarp_lock);
990}
991
992static const char *dt2str(unsigned long ticks)
993{
994 static char buf[32];
995
996 sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100 ) / HZ);
997
998 return buf;
999}
1000
1001static int aarp_seq_show(struct seq_file *seq, void *v)
1002{
1003 struct aarp_iter_state *iter = seq->private;
1004 struct aarp_entry *entry = v;
1005 unsigned long now = jiffies;
1006
1007 if (v == SEQ_START_TOKEN)
1008 seq_puts(seq,
1009 "Address Interface Hardware Address"
1010 " Expires LastSend Retry Status\n");
1011 else {
1012 seq_printf(seq, "%04X:%02X %-12s",
1013 ntohs(entry->target_addr.s_net),
1014 (unsigned int) entry->target_addr.s_node,
1015 entry->dev ? entry->dev->name : "????");
1016 seq_printf(seq, "%pM", entry->hwaddr);
1017 seq_printf(seq, " %8s",
1018 dt2str((long)entry->expires_at - (long)now));
1019 if (iter->table == unresolved)
1020 seq_printf(seq, " %8s %6hu",
1021 dt2str(now - entry->last_sent),
1022 entry->xmit_count);
1023 else
1024 seq_puts(seq, " ");
1025 seq_printf(seq, " %s\n",
1026 (iter->table == resolved) ? "resolved"
1027 : (iter->table == unresolved) ? "unresolved"
1028 : (iter->table == proxies) ? "proxies"
1029 : "unknown");
1030 }
1031 return 0;
1032}
1033
1034static const struct seq_operations aarp_seq_ops = {
1035 .start = aarp_seq_start,
1036 .next = aarp_seq_next,
1037 .stop = aarp_seq_stop,
1038 .show = aarp_seq_show,
1039};
1040
1041static int aarp_seq_open(struct inode *inode, struct file *file)
1042{
1043 return seq_open_private(file, &aarp_seq_ops,
1044 sizeof(struct aarp_iter_state));
1045}
1046
1047const struct file_operations atalk_seq_arp_fops = {
1048 .owner = THIS_MODULE,
1049 .open = aarp_seq_open,
1050 .read = seq_read,
1051 .llseek = seq_lseek,
1052 .release = seq_release_private,
1053};
1054#endif
1055
1056
1057void aarp_cleanup_module(void)
1058{
1059 del_timer_sync(&aarp_timer);
1060 unregister_netdevice_notifier(&aarp_notifier);
1061 unregister_snap_client(aarp_dl);
1062 aarp_purge();
1063}
1064