This source file includes following definitions.
- aarp_expire
- aarp_send_query
- aarp_send_reply
- aarp_send_probe
- aarp_expire_timer
- aarp_kick
- aarp_expire_device
- aarp_expire_timeout
- aarp_device_event
- aarp_alloc
- aarp_find_entry
- aarp_send_ddp
- aarp_resolved
- aarp_rcv
- aarp_proto_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 #include <asm/segment.h>
27 #include <asm/system.h>
28 #include <asm/bitops.h>
29 #include <linux/config.h>
30 #include <linux/types.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/string.h>
34 #include <linux/mm.h>
35 #include <linux/socket.h>
36 #include <linux/sockios.h>
37 #include <linux/in.h>
38 #include <linux/errno.h>
39 #include <linux/interrupt.h>
40 #include <linux/if_ether.h>
41 #include <linux/inet.h>
42 #include <linux/notifier.h>
43 #include <linux/netdevice.h>
44 #include <linux/etherdevice.h>
45 #include <linux/skbuff.h>
46 #include <net/sock.h>
47 #include <net/datalink.h>
48 #include <net/psnap.h>
49 #include <linux/atalk.h>
50
51 #ifdef CONFIG_ATALK
52
53
54
55
56 struct aarp_entry
57 {
58
59 unsigned long last_sent;
60 struct sk_buff_head packet_queue;
61 unsigned long expires_at;
62 struct at_addr target_addr;
63 struct device *dev;
64 char hwaddr[6];
65 unsigned short xmit_count;
66 struct aarp_entry *next;
67 };
68
69
70
71
72
73
74 static struct aarp_entry *resolved[AARP_HASH_SIZE], *unresolved[AARP_HASH_SIZE];
75 static int unresolved_count=0;
76
77
78
79
80
81 static struct timer_list aarp_timer;
82
83
84
85
86
87 static void aarp_expire(struct aarp_entry *a)
88 {
89 struct sk_buff *skb;
90
91 while((skb=skb_dequeue(&a->packet_queue))!=NULL)
92 kfree_skb(skb, FREE_WRITE);
93 kfree_s(a,sizeof(*a));
94 }
95
96
97
98
99
100 static void aarp_send_query(struct aarp_entry *a)
101 {
102 static char aarp_eth_multicast[ETH_ALEN]={ 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
103 struct device *dev=a->dev;
104 int len=dev->hard_header_len+sizeof(struct elapaarp)+aarp_dl->header_length;
105 struct sk_buff *skb=alloc_skb(len, GFP_ATOMIC);
106 struct elapaarp *eah;
107 struct at_addr *sat=atalk_find_dev_addr(dev);
108
109 if(skb==NULL || sat==NULL)
110 return;
111
112
113
114
115
116 skb_reserve(skb,dev->hard_header_len+aarp_dl->header_length);
117 eah = (struct elapaarp *)skb_put(skb,sizeof(struct elapaarp));
118 skb->arp = 1;
119 skb->free = 1;
120 skb->dev = a->dev;
121
122
123
124
125
126 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
127 eah->pa_type = htons(ETH_P_ATALK);
128 eah->hw_len = ETH_ALEN;
129 eah->pa_len = AARP_PA_ALEN;
130 eah->function = htons(AARP_REQUEST);
131
132 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
133
134 eah->pa_src_zero= 0;
135 eah->pa_src_net = sat->s_net;
136 eah->pa_src_node= sat->s_node;
137
138 memset(eah->hw_dst, '\0', ETH_ALEN);
139
140 eah->pa_dst_zero= 0;
141 eah->pa_dst_net = a->target_addr.s_net;
142 eah->pa_dst_node= a->target_addr.s_node;
143
144
145
146
147
148 aarp_dl->datalink_header(aarp_dl, skb, aarp_eth_multicast);
149
150
151
152
153
154
155 dev_queue_xmit(skb, dev, SOPRI_NORMAL);
156
157
158
159
160
161 a->xmit_count++;
162 }
163
164 static void aarp_send_reply(struct device *dev, struct at_addr *us, struct at_addr *them, unsigned char *sha)
165 {
166 int len=dev->hard_header_len+sizeof(struct elapaarp)+aarp_dl->header_length;
167 struct sk_buff *skb=alloc_skb(len, GFP_ATOMIC);
168 struct elapaarp *eah;
169
170 if(skb==NULL)
171 return;
172
173
174
175
176
177 skb_reserve(skb,dev->hard_header_len+aarp_dl->header_length);
178 eah = (struct elapaarp *)skb_put(skb,sizeof(struct elapaarp));
179 skb->arp = 1;
180 skb->free = 1;
181 skb->dev = dev;
182
183
184
185
186
187 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
188 eah->pa_type = htons(ETH_P_ATALK);
189 eah->hw_len = ETH_ALEN;
190 eah->pa_len = AARP_PA_ALEN;
191 eah->function = htons(AARP_REPLY);
192
193 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
194
195 eah->pa_src_zero= 0;
196 eah->pa_src_net = us->s_net;
197 eah->pa_src_node= us->s_node;
198
199 if(sha==NULL)
200 memset(eah->hw_dst, '\0', ETH_ALEN);
201 else
202 memcpy(eah->hw_dst, sha, ETH_ALEN);
203
204 eah->pa_dst_zero= 0;
205 eah->pa_dst_net = them->s_net;
206 eah->pa_dst_node= them->s_node;
207
208
209
210
211
212 aarp_dl->datalink_header(aarp_dl, skb, sha);
213
214
215
216
217
218 dev_queue_xmit(skb, dev, SOPRI_NORMAL);
219
220 }
221
222
223
224
225
226 void aarp_send_probe(struct device *dev, struct at_addr *us)
227 {
228 int len=dev->hard_header_len+sizeof(struct elapaarp)+aarp_dl->header_length;
229 struct sk_buff *skb=alloc_skb(len, GFP_ATOMIC);
230 struct elapaarp *eah;
231 static char aarp_eth_multicast[ETH_ALEN]={ 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
232
233 if(skb==NULL)
234 return;
235
236
237
238
239
240 skb_reserve(skb,dev->hard_header_len+aarp_dl->header_length);
241 eah = (struct elapaarp *)skb_put(skb,sizeof(struct elapaarp));
242
243 skb->arp = 1;
244 skb->free = 1;
245 skb->dev = dev;
246
247
248
249
250
251 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
252 eah->pa_type = htons(ETH_P_ATALK);
253 eah->hw_len = ETH_ALEN;
254 eah->pa_len = AARP_PA_ALEN;
255 eah->function = htons(AARP_PROBE);
256
257 memcpy(eah->hw_src, dev->dev_addr, ETH_ALEN);
258
259 eah->pa_src_zero= 0;
260 eah->pa_src_net = us->s_net;
261 eah->pa_src_node= us->s_node;
262
263 memset(eah->hw_dst, '\0', ETH_ALEN);
264
265 eah->pa_dst_zero= 0;
266 eah->pa_dst_net = us->s_net;
267 eah->pa_dst_node= us->s_node;
268
269
270
271
272
273 aarp_dl->datalink_header(aarp_dl, skb, aarp_eth_multicast);
274
275
276
277
278
279 dev_queue_xmit(skb, dev, SOPRI_NORMAL);
280
281 }
282
283
284
285
286
287 static void aarp_expire_timer(struct aarp_entry **n)
288 {
289 struct aarp_entry *t;
290 while((*n)!=NULL)
291 {
292
293 if((*n)->expires_at < jiffies)
294 {
295 t= *n;
296 *n=(*n)->next;
297 aarp_expire(t);
298 }
299 else
300 n=&((*n)->next);
301 }
302 }
303
304
305
306
307
308 static void aarp_kick(struct aarp_entry **n)
309 {
310 struct aarp_entry *t;
311 while((*n)!=NULL)
312 {
313
314
315 if((*n)->xmit_count>=AARP_RETRANSMIT_LIMIT)
316 {
317 t= *n;
318 *n=(*n)->next;
319 aarp_expire(t);
320 }
321 else
322 {
323 aarp_send_query(*n);
324 n=&((*n)->next);
325 }
326 }
327 }
328
329
330
331
332
333
334 static void aarp_expire_device(struct aarp_entry **n, struct device *dev)
335 {
336 struct aarp_entry *t;
337 while((*n)!=NULL)
338 {
339 if((*n)->dev==dev)
340 {
341 t= *n;
342 *n=(*n)->next;
343 aarp_expire(t);
344 }
345 else
346 n=&((*n)->next);
347 }
348 }
349
350
351
352
353
354 static void aarp_expire_timeout(unsigned long unused)
355 {
356 int ct=0;
357 for(ct=0;ct<AARP_HASH_SIZE;ct++)
358 {
359 aarp_expire_timer(&resolved[ct]);
360 aarp_kick(&unresolved[ct]);
361 aarp_expire_timer(&unresolved[ct]);
362 }
363 del_timer(&aarp_timer);
364 if(unresolved_count==0)
365 aarp_timer.expires=AARP_EXPIRY_TIME;
366 else
367 aarp_timer.expires=AARP_TICK_TIME;
368 add_timer(&aarp_timer);
369 }
370
371
372
373
374
375 static int aarp_device_event(unsigned long event, void *ptr)
376 {
377 int ct=0;
378 if(event==NETDEV_DOWN)
379 {
380 for(ct=0;ct<AARP_HASH_SIZE;ct++)
381 {
382 aarp_expire_device(&resolved[ct],ptr);
383 aarp_expire_device(&unresolved[ct],ptr);
384 }
385 }
386 return NOTIFY_DONE;
387 }
388
389
390
391
392
393 static struct aarp_entry *aarp_alloc(void)
394 {
395 struct aarp_entry *a=kmalloc(sizeof(struct aarp_entry), GFP_ATOMIC);
396 if(a==NULL)
397 return NULL;
398 skb_queue_head_init(&a->packet_queue);
399 return a;
400 }
401
402
403
404
405
406
407 static struct aarp_entry *aarp_find_entry(struct aarp_entry *list, struct device *dev, struct at_addr *sat)
408 {
409 unsigned long flags;
410 save_flags(flags);
411 cli();
412 while(list)
413 {
414 if(list->target_addr.s_net==sat->s_net &&
415 list->target_addr.s_node==sat->s_node && list->dev==dev)
416 break;
417 list=list->next;
418 }
419 restore_flags(flags);
420 return list;
421 }
422
423
424
425
426
427 int aarp_send_ddp(struct device *dev,struct sk_buff *skb, struct at_addr *sa, void *hwaddr)
428 {
429 static char ddp_eth_multicast[ETH_ALEN]={ 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
430 int hash;
431 struct aarp_entry *a;
432 unsigned long flags;
433
434
435
436
437 if(dev->type!=ARPHRD_ETHER)
438 {
439 return -1;
440 }
441
442 skb->dev = dev;
443
444 hash=sa->s_node%(AARP_HASH_SIZE-1);
445 save_flags(flags);
446 cli();
447
448
449
450
451
452 if(sa->s_node==ATADDR_BCAST)
453 {
454 ddp_dl->datalink_header(ddp_dl, skb, ddp_eth_multicast);
455 if(skb->sk==NULL)
456 dev_queue_xmit(skb, skb->dev, SOPRI_NORMAL);
457 else
458 dev_queue_xmit(skb, skb->dev, skb->sk->priority);
459 restore_flags(flags);
460 return 1;
461 }
462 a=aarp_find_entry(resolved[hash],dev,sa);
463 if(a!=NULL)
464 {
465
466
467
468 a->expires_at=jiffies+AARP_EXPIRY_TIME*10;
469 ddp_dl->datalink_header(ddp_dl, skb, a->hwaddr);
470 if(skb->sk==NULL)
471 dev_queue_xmit(skb, skb->dev, SOPRI_NORMAL);
472 else
473 dev_queue_xmit(skb, skb->dev, skb->sk->priority);
474 restore_flags(flags);
475 return 1;
476 }
477
478
479
480 a=aarp_find_entry(unresolved[hash],dev,sa);
481 if(a!=NULL)
482 {
483
484
485
486 skb_queue_tail(&a->packet_queue, skb);
487 restore_flags(flags);
488 return 0;
489 }
490
491
492
493 a=aarp_alloc();
494 if(a==NULL)
495 {
496
497
498
499
500 restore_flags(flags);
501 return -1;
502 }
503
504
505
506 skb_queue_tail(&a->packet_queue, skb);
507 a->expires_at=jiffies+AARP_RESOLVE_TIME;
508 a->dev=dev;
509 a->next=unresolved[hash];
510 a->target_addr= *sa;
511 a->xmit_count=0;
512 unresolved[hash]=a;
513 unresolved_count++;
514 restore_flags(flags);
515
516
517
518 aarp_send_query(a);
519
520
521
522
523 if(unresolved_count==1)
524 {
525 del_timer(&aarp_timer);
526 aarp_timer.expires=AARP_TICK_TIME;
527 add_timer(&aarp_timer);
528 }
529
530
531
532 return 0;
533 }
534
535 static void aarp_resolved(struct aarp_entry **list, struct aarp_entry *a, int hash)
536 {
537 struct sk_buff *skb;
538 while(*list!=NULL)
539 {
540 if(*list==a)
541 {
542 unresolved_count--;
543 *list=a->next;
544
545 a->next=resolved[hash];
546 resolved[hash]=a;
547
548 while((skb=skb_dequeue(&a->packet_queue))!=NULL)
549 {
550 a->expires_at=jiffies+AARP_EXPIRY_TIME*10;
551 ddp_dl->datalink_header(ddp_dl,skb,a->hwaddr);
552 if(skb->sk==NULL)
553 dev_queue_xmit(skb, skb->dev, SOPRI_NORMAL);
554 else
555 dev_queue_xmit(skb, skb->dev, skb->sk->priority);
556 }
557 }
558 else
559 list=&((*list)->next);
560 }
561 }
562
563 static int aarp_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
564 {
565 struct elapaarp *ea=(struct elapaarp *)skb->h.raw;
566 struct aarp_entry *a;
567 struct at_addr sa, *ma;
568 unsigned long flags;
569 int hash;
570 struct atalk_iface *ifa;
571
572
573
574
575
576
577 if(dev->type!=ARPHRD_ETHER)
578 {
579 kfree_skb(skb, FREE_READ);
580 return 0;
581 }
582
583
584
585
586
587 if(skb_pull(skb,sizeof(*ea))<sizeof(*ea))
588 {
589 kfree_skb(skb, FREE_READ);
590 return 0;
591 }
592
593 ea->function=ntohs(ea->function);
594
595
596
597
598
599 if(ea->function<AARP_REQUEST || ea->function > AARP_PROBE || ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
600 ea->pa_src_zero != 0 || ea->pa_dst_zero != 0)
601 {
602 kfree_skb(skb, FREE_READ);
603 return 0;
604 }
605
606
607
608
609
610 hash=ea->pa_src_node%(AARP_HASH_SIZE-1);
611
612
613
614
615
616 sa.s_node=ea->pa_src_node;
617 sa.s_net=ea->pa_src_net;
618
619
620
621
622
623 save_flags(flags);
624
625
626
627
628
629 ifa=atalk_find_dev(dev);
630 if(ifa==NULL)
631 {
632 restore_flags(flags);
633 kfree_skb(skb, FREE_READ);
634 return 1;
635 }
636 if(ifa->status&ATIF_PROBE)
637 {
638 if(ifa->address.s_node==ea->pa_dst_node && ifa->address.s_net==ea->pa_dst_net)
639 {
640
641
642
643 ifa->status|=ATIF_PROBE_FAIL;
644 restore_flags(flags);
645 kfree_skb(skb, FREE_READ);
646 return 1;
647 }
648 }
649
650 switch(ea->function)
651 {
652 case AARP_REPLY:
653 if(unresolved_count==0)
654 break;
655
656
657
658
659 cli();
660 if((a=aarp_find_entry(unresolved[hash],dev,&sa))==NULL || dev != a->dev)
661 break;
662
663
664
665 memcpy(a->hwaddr,ea->hw_src,ETH_ALEN);
666 aarp_resolved(&unresolved[hash],a,hash);
667 if(unresolved_count==0)
668 {
669 del_timer(&aarp_timer);
670 aarp_timer.expires=AARP_EXPIRY_TIME;
671 add_timer(&aarp_timer);
672 }
673 break;
674
675 case AARP_REQUEST:
676 case AARP_PROBE:
677
678
679
680
681
682
683 ma=&ifa->address;
684 sa.s_node=ea->pa_dst_node;
685 sa.s_net=ea->pa_dst_net;
686
687 if(sa.s_node!=ma->s_node)
688 break;
689 if(sa.s_net && ma->s_net && sa.s_net!=ma->s_net)
690 break;
691
692 sa.s_node=ea->pa_src_node;
693 sa.s_net=ea->pa_src_net;
694
695
696
697
698 aarp_send_reply(dev,ma,&sa,ea->hw_src);
699 break;
700 }
701 restore_flags(flags);
702 kfree_skb(skb, FREE_READ);
703 return 1;
704 }
705
706 static struct notifier_block aarp_notifier={
707 aarp_device_event,
708 NULL,
709 0
710 };
711
712
713 void aarp_proto_init(void)
714 {
715 static char aarp_snap_id[]={0x00,0x00,0x00,0x80,0xF3};
716 if((aarp_dl=register_snap_client(aarp_snap_id, aarp_rcv))==NULL)
717 printk("Unable to register AARP with SNAP.\n");
718 init_timer(&aarp_timer);
719 aarp_timer.function=aarp_expire_timeout;
720 aarp_timer.data=0;
721 aarp_timer.expires=AARP_EXPIRY_TIME;
722 add_timer(&aarp_timer);
723 register_netdevice_notifier(&aarp_notifier);
724 }
725 #endif