1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * AARP: An implementation of the AppleTalk AARP protocol for
4 * Ethernet 'ELAP'.
5 *
6 * Alan Cox <[email protected]>
7 *
8 * This doesn't fit cleanly with the IP arp. Potentially we can use
9 * the generic neighbour discovery code to clean this up.
10 *
11 * FIXME:
12 * We ought to handle the retransmits with a single list and a
13 * separate fast timer for when it is needed.
14 * Use neighbour discovery code.
15 * Token Ring Support.
16 *
17 * References:
18 * Inside AppleTalk (2nd Ed).
19 * Fixes:
20 * Jaume Grau - flush caches on AARP_PROBE
21 * Rob Newberry - Added proxy AARP and AARP proc fs,
22 * moved probing from DDP module.
23 * Arnaldo C. Melo - don't mangle rx packets
24 */
25
26 #include <linux/if_arp.h>
27 #include <linux/slab.h>
28 #include <net/sock.h>
29 #include <net/datalink.h>
30 #include <net/psnap.h>
31 #include <linux/atalk.h>
32 #include <linux/delay.h>
33 #include <linux/init.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/export.h>
37 #include <linux/etherdevice.h>
38
39 int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME;
40 int sysctl_aarp_tick_time = AARP_TICK_TIME;
41 int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT;
42 int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
43
44 /* Lists of aarp entries */
45 /**
46 * struct aarp_entry - AARP entry
47 * @last_sent: Last time we xmitted the aarp request
48 * @packet_queue: Queue of frames wait for resolution
49 * @status: Used for proxy AARP
50 * @expires_at: Entry expiry time
51 * @target_addr: DDP Address
52 * @dev: Device to use
53 * @hwaddr: Physical i/f address of target/router
54 * @xmit_count: When this hits 10 we give up
55 * @next: Next entry in chain
56 */
57 struct aarp_entry {
58 /* These first two are only used for unresolved entries */
59 unsigned long last_sent;
60 struct sk_buff_head packet_queue;
61 int status;
62 unsigned long expires_at;
63 struct atalk_addr target_addr;
64 struct net_device *dev;
65 char hwaddr[ETH_ALEN];
66 unsigned short xmit_count;
67 struct aarp_entry *next;
68 };
69
70 /* Hashed list of resolved, unresolved and proxy entries */
71 static struct aarp_entry *resolved[AARP_HASH_SIZE];
72 static struct aarp_entry *unresolved[AARP_HASH_SIZE];
73 static struct aarp_entry *proxies[AARP_HASH_SIZE];
74 static int unresolved_count;
75
76 /* One lock protects it all. */
77 static DEFINE_RWLOCK(aarp_lock);
78
79 /* Used to walk the list and purge/kick entries. */
80 static struct timer_list aarp_timer;
81
82 /*
83 * Delete an aarp queue
84 *
85 * Must run under aarp_lock.
86 */
__aarp_expire(struct aarp_entry * a)87 static void __aarp_expire(struct aarp_entry *a)
88 {
89 skb_queue_purge(&a->packet_queue);
90 kfree(a);
91 }
92
93 /*
94 * Send an aarp queue entry request
95 *
96 * Must run under aarp_lock.
97 */
__aarp_send_query(struct aarp_entry * a)98 static void __aarp_send_query(struct aarp_entry *a)
99 {
100 static unsigned char aarp_eth_multicast[ETH_ALEN] =
101 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
102 struct net_device *dev = a->dev;
103 struct elapaarp *eah;
104 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
105 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
106 struct atalk_addr *sat = atalk_find_dev_addr(dev);
107
108 if (!skb)
109 return;
110
111 if (!sat) {
112 kfree_skb(skb);
113 return;
114 }
115
116 /* Set up the buffer */
117 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
118 skb_reset_network_header(skb);
119 skb_reset_transport_header(skb);
120 skb_put(skb, sizeof(*eah));
121 skb->protocol = htons(ETH_P_ATALK);
122 skb->dev = dev;
123 eah = aarp_hdr(skb);
124
125 /* Set up the ARP */
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 ether_addr_copy(eah->hw_src, dev->dev_addr);
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 eth_zero_addr(eah->hw_dst);
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 /* Send it */
145 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
146 /* Update the sending count */
147 a->xmit_count++;
148 a->last_sent = jiffies;
149 }
150
151 /* This runs under aarp_lock and in softint context, so only atomic memory
152 * allocations can be used. */
aarp_send_reply(struct net_device * dev,struct atalk_addr * us,struct atalk_addr * them,unsigned char * sha)153 static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
154 struct atalk_addr *them, unsigned char *sha)
155 {
156 struct elapaarp *eah;
157 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
158 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
159
160 if (!skb)
161 return;
162
163 /* Set up the buffer */
164 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
165 skb_reset_network_header(skb);
166 skb_reset_transport_header(skb);
167 skb_put(skb, sizeof(*eah));
168 skb->protocol = htons(ETH_P_ATALK);
169 skb->dev = dev;
170 eah = aarp_hdr(skb);
171
172 /* Set up the ARP */
173 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
174 eah->pa_type = htons(ETH_P_ATALK);
175 eah->hw_len = ETH_ALEN;
176 eah->pa_len = AARP_PA_ALEN;
177 eah->function = htons(AARP_REPLY);
178
179 ether_addr_copy(eah->hw_src, dev->dev_addr);
180
181 eah->pa_src_zero = 0;
182 eah->pa_src_net = us->s_net;
183 eah->pa_src_node = us->s_node;
184
185 if (!sha)
186 eth_zero_addr(eah->hw_dst);
187 else
188 ether_addr_copy(eah->hw_dst, sha);
189
190 eah->pa_dst_zero = 0;
191 eah->pa_dst_net = them->s_net;
192 eah->pa_dst_node = them->s_node;
193
194 /* Send it */
195 aarp_dl->request(aarp_dl, skb, sha);
196 }
197
198 /*
199 * Send probe frames. Called from aarp_probe_network and
200 * aarp_proxy_probe_network.
201 */
202
aarp_send_probe(struct net_device * dev,struct atalk_addr * us)203 static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
204 {
205 struct elapaarp *eah;
206 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
207 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
208 static unsigned char aarp_eth_multicast[ETH_ALEN] =
209 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
210
211 if (!skb)
212 return;
213
214 /* Set up the buffer */
215 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
216 skb_reset_network_header(skb);
217 skb_reset_transport_header(skb);
218 skb_put(skb, sizeof(*eah));
219 skb->protocol = htons(ETH_P_ATALK);
220 skb->dev = dev;
221 eah = aarp_hdr(skb);
222
223 /* Set up the ARP */
224 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
225 eah->pa_type = htons(ETH_P_ATALK);
226 eah->hw_len = ETH_ALEN;
227 eah->pa_len = AARP_PA_ALEN;
228 eah->function = htons(AARP_PROBE);
229
230 ether_addr_copy(eah->hw_src, dev->dev_addr);
231
232 eah->pa_src_zero = 0;
233 eah->pa_src_net = us->s_net;
234 eah->pa_src_node = us->s_node;
235
236 eth_zero_addr(eah->hw_dst);
237
238 eah->pa_dst_zero = 0;
239 eah->pa_dst_net = us->s_net;
240 eah->pa_dst_node = us->s_node;
241
242 /* Send it */
243 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
244 }
245
246 /*
247 * Handle an aarp timer expire
248 *
249 * Must run under the aarp_lock.
250 */
251
__aarp_expire_timer(struct aarp_entry ** n)252 static void __aarp_expire_timer(struct aarp_entry **n)
253 {
254 struct aarp_entry *t;
255
256 while (*n)
257 /* Expired ? */
258 if (time_after(jiffies, (*n)->expires_at)) {
259 t = *n;
260 *n = (*n)->next;
261 __aarp_expire(t);
262 } else
263 n = &((*n)->next);
264 }
265
266 /*
267 * Kick all pending requests 5 times a second.
268 *
269 * Must run under the aarp_lock.
270 */
__aarp_kick(struct aarp_entry ** n)271 static void __aarp_kick(struct aarp_entry **n)
272 {
273 struct aarp_entry *t;
274
275 while (*n)
276 /* Expired: if this will be the 11th tx, we delete instead. */
277 if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
278 t = *n;
279 *n = (*n)->next;
280 __aarp_expire(t);
281 } else {
282 __aarp_send_query(*n);
283 n = &((*n)->next);
284 }
285 }
286
287 /*
288 * A device has gone down. Take all entries referring to the device
289 * and remove them.
290 *
291 * Must run under the aarp_lock.
292 */
__aarp_expire_device(struct aarp_entry ** n,struct net_device * dev)293 static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
294 {
295 struct aarp_entry *t;
296
297 while (*n)
298 if ((*n)->dev == dev) {
299 t = *n;
300 *n = (*n)->next;
301 __aarp_expire(t);
302 } else
303 n = &((*n)->next);
304 }
305
306 /* Handle the timer event */
aarp_expire_timeout(struct timer_list * unused)307 static void aarp_expire_timeout(struct timer_list *unused)
308 {
309 int ct;
310
311 write_lock_bh(&aarp_lock);
312
313 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
314 __aarp_expire_timer(&resolved[ct]);
315 __aarp_kick(&unresolved[ct]);
316 __aarp_expire_timer(&unresolved[ct]);
317 __aarp_expire_timer(&proxies[ct]);
318 }
319
320 write_unlock_bh(&aarp_lock);
321 mod_timer(&aarp_timer, jiffies +
322 (unresolved_count ? sysctl_aarp_tick_time :
323 sysctl_aarp_expiry_time));
324 }
325
326 /* Network device notifier chain handler. */
aarp_device_event(struct notifier_block * this,unsigned long event,void * ptr)327 static int aarp_device_event(struct notifier_block *this, unsigned long event,
328 void *ptr)
329 {
330 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
331 int ct;
332
333 if (!net_eq(dev_net(dev), &init_net))
334 return NOTIFY_DONE;
335
336 if (event == NETDEV_DOWN) {
337 write_lock_bh(&aarp_lock);
338
339 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
340 __aarp_expire_device(&resolved[ct], dev);
341 __aarp_expire_device(&unresolved[ct], dev);
342 __aarp_expire_device(&proxies[ct], dev);
343 }
344
345 write_unlock_bh(&aarp_lock);
346 }
347 return NOTIFY_DONE;
348 }
349
350 /* Expire all entries in a hash chain */
__aarp_expire_all(struct aarp_entry ** n)351 static void __aarp_expire_all(struct aarp_entry **n)
352 {
353 struct aarp_entry *t;
354
355 while (*n) {
356 t = *n;
357 *n = (*n)->next;
358 __aarp_expire(t);
359 }
360 }
361
362 /* Cleanup all hash chains -- module unloading */
aarp_purge(void)363 static void aarp_purge(void)
364 {
365 int ct;
366
367 write_lock_bh(&aarp_lock);
368 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
369 __aarp_expire_all(&resolved[ct]);
370 __aarp_expire_all(&unresolved[ct]);
371 __aarp_expire_all(&proxies[ct]);
372 }
373 write_unlock_bh(&aarp_lock);
374 }
375
376 /*
377 * Create a new aarp entry. This must use GFP_ATOMIC because it
378 * runs while holding spinlocks.
379 */
aarp_alloc(void)380 static struct aarp_entry *aarp_alloc(void)
381 {
382 struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
383
384 if (a)
385 skb_queue_head_init(&a->packet_queue);
386 return a;
387 }
388
389 /*
390 * Find an entry. We might return an expired but not yet purged entry. We
391 * don't care as it will do no harm.
392 *
393 * This must run under the aarp_lock.
394 */
__aarp_find_entry(struct aarp_entry * list,struct net_device * dev,struct atalk_addr * sat)395 static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
396 struct net_device *dev,
397 struct atalk_addr *sat)
398 {
399 while (list) {
400 if (list->target_addr.s_net == sat->s_net &&
401 list->target_addr.s_node == sat->s_node &&
402 list->dev == dev)
403 break;
404 list = list->next;
405 }
406
407 return list;
408 }
409
410 /* Called from the DDP code, and thus must be exported. */
aarp_proxy_remove(struct net_device * dev,struct atalk_addr * sa)411 void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
412 {
413 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
414 struct aarp_entry *a;
415
416 write_lock_bh(&aarp_lock);
417
418 a = __aarp_find_entry(proxies[hash], dev, sa);
419 if (a)
420 a->expires_at = jiffies - 1;
421
422 write_unlock_bh(&aarp_lock);
423 }
424
425 /* This must run under aarp_lock. */
__aarp_proxy_find(struct net_device * dev,struct atalk_addr * sa)426 static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
427 struct atalk_addr *sa)
428 {
429 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
430 struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
431
432 return a ? sa : NULL;
433 }
434
aarp_probe_network(struct atalk_iface * atif)435 void aarp_probe_network(struct atalk_iface *atif)
436 {
437 unsigned int count;
438
439 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
440 aarp_send_probe(atif->dev, &atif->address);
441
442 /* Defer 1/10th */
443 msleep(100);
444
445 if (atif->status & ATIF_PROBE_FAIL)
446 break;
447 }
448 }
449
aarp_proxy_probe_network(struct atalk_iface * atif,struct atalk_addr * sa)450 int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
451 {
452 int hash, retval = -EPROTONOSUPPORT;
453 struct aarp_entry *entry;
454 unsigned int count;
455
456 /*
457 * we don't currently support LocalTalk or PPP for proxy AARP;
458 * if someone wants to try and add it, have fun
459 */
460 if (atif->dev->type == ARPHRD_LOCALTLK ||
461 atif->dev->type == ARPHRD_PPP)
462 goto out;
463
464 /*
465 * create a new AARP entry with the flags set to be published --
466 * we need this one to hang around even if it's in use
467 */
468 entry = aarp_alloc();
469 retval = -ENOMEM;
470 if (!entry)
471 goto out;
472
473 entry->expires_at = -1;
474 entry->status = ATIF_PROBE;
475 entry->target_addr.s_node = sa->s_node;
476 entry->target_addr.s_net = sa->s_net;
477 entry->dev = atif->dev;
478
479 write_lock_bh(&aarp_lock);
480
481 hash = sa->s_node % (AARP_HASH_SIZE - 1);
482 entry->next = proxies[hash];
483 proxies[hash] = entry;
484
485 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
486 aarp_send_probe(atif->dev, sa);
487
488 /* Defer 1/10th */
489 write_unlock_bh(&aarp_lock);
490 msleep(100);
491 write_lock_bh(&aarp_lock);
492
493 if (entry->status & ATIF_PROBE_FAIL)
494 break;
495 }
496
497 if (entry->status & ATIF_PROBE_FAIL) {
498 entry->expires_at = jiffies - 1; /* free the entry */
499 retval = -EADDRINUSE; /* return network full */
500 } else { /* clear the probing flag */
501 entry->status &= ~ATIF_PROBE;
502 retval = 1;
503 }
504
505 write_unlock_bh(&aarp_lock);
506 out:
507 return retval;
508 }
509
510 /* Send a DDP frame */
aarp_send_ddp(struct net_device * dev,struct sk_buff * skb,struct atalk_addr * sa,void * hwaddr)511 int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
512 struct atalk_addr *sa, void *hwaddr)
513 {
514 static char ddp_eth_multicast[ETH_ALEN] =
515 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
516 int hash;
517 struct aarp_entry *a;
518
519 skb_reset_network_header(skb);
520
521 /* Check for LocalTalk first */
522 if (dev->type == ARPHRD_LOCALTLK) {
523 struct atalk_addr *at = atalk_find_dev_addr(dev);
524 struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
525 int ft = 2;
526
527 /*
528 * Compressible ?
529 *
530 * IFF: src_net == dest_net == device_net
531 * (zero matches anything)
532 */
533
534 if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
535 (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
536 skb_pull(skb, sizeof(*ddp) - 4);
537
538 /*
539 * The upper two remaining bytes are the port
540 * numbers we just happen to need. Now put the
541 * length in the lower two.
542 */
543 *((__be16 *)skb->data) = htons(skb->len);
544 ft = 1;
545 }
546 /*
547 * Nice and easy. No AARP type protocols occur here so we can
548 * just shovel it out with a 3 byte LLAP header
549 */
550
551 skb_push(skb, 3);
552 skb->data[0] = sa->s_node;
553 skb->data[1] = at->s_node;
554 skb->data[2] = ft;
555 skb->dev = dev;
556 goto sendit;
557 }
558
559 /* On a PPP link we neither compress nor aarp. */
560 if (dev->type == ARPHRD_PPP) {
561 skb->protocol = htons(ETH_P_PPPTALK);
562 skb->dev = dev;
563 goto sendit;
564 }
565
566 /* Non ELAP we cannot do. */
567 if (dev->type != ARPHRD_ETHER)
568 goto free_it;
569
570 skb->dev = dev;
571 skb->protocol = htons(ETH_P_ATALK);
572 hash = sa->s_node % (AARP_HASH_SIZE - 1);
573
574 /* Do we have a resolved entry? */
575 if (sa->s_node == ATADDR_BCAST) {
576 /* Send it */
577 ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
578 goto sent;
579 }
580
581 write_lock_bh(&aarp_lock);
582 a = __aarp_find_entry(resolved[hash], dev, sa);
583
584 if (a) { /* Return 1 and fill in the address */
585 a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
586 ddp_dl->request(ddp_dl, skb, a->hwaddr);
587 write_unlock_bh(&aarp_lock);
588 goto sent;
589 }
590
591 /* Do we have an unresolved entry: This is the less common path */
592 a = __aarp_find_entry(unresolved[hash], dev, sa);
593 if (a) { /* Queue onto the unresolved queue */
594 skb_queue_tail(&a->packet_queue, skb);
595 goto out_unlock;
596 }
597
598 /* Allocate a new entry */
599 a = aarp_alloc();
600 if (!a) {
601 /* Whoops slipped... good job it's an unreliable protocol 8) */
602 write_unlock_bh(&aarp_lock);
603 goto free_it;
604 }
605
606 /* Set up the queue */
607 skb_queue_tail(&a->packet_queue, skb);
608 a->expires_at = jiffies + sysctl_aarp_resolve_time;
609 a->dev = dev;
610 a->next = unresolved[hash];
611 a->target_addr = *sa;
612 a->xmit_count = 0;
613 unresolved[hash] = a;
614 unresolved_count++;
615
616 /* Send an initial request for the address */
617 __aarp_send_query(a);
618
619 /*
620 * Switch to fast timer if needed (That is if this is the first
621 * unresolved entry to get added)
622 */
623
624 if (unresolved_count == 1)
625 mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
626
627 /* Now finally, it is safe to drop the lock. */
628 out_unlock:
629 write_unlock_bh(&aarp_lock);
630
631 /* Tell the ddp layer we have taken over for this frame. */
632 goto sent;
633
634 sendit:
635 if (skb->sk)
636 skb->priority = READ_ONCE(skb->sk->sk_priority);
637 if (dev_queue_xmit(skb))
638 goto drop;
639 sent:
640 return NET_XMIT_SUCCESS;
641 free_it:
642 kfree_skb(skb);
643 drop:
644 return NET_XMIT_DROP;
645 }
646 EXPORT_SYMBOL(aarp_send_ddp);
647
648 /*
649 * An entry in the aarp unresolved queue has become resolved. Send
650 * all the frames queued under it.
651 *
652 * Must run under aarp_lock.
653 */
__aarp_resolved(struct aarp_entry ** list,struct aarp_entry * a,int hash)654 static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
655 int hash)
656 {
657 struct sk_buff *skb;
658
659 while (*list)
660 if (*list == a) {
661 unresolved_count--;
662 *list = a->next;
663
664 /* Move into the resolved list */
665 a->next = resolved[hash];
666 resolved[hash] = a;
667
668 /* Kick frames off */
669 while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
670 a->expires_at = jiffies +
671 sysctl_aarp_expiry_time * 10;
672 ddp_dl->request(ddp_dl, skb, a->hwaddr);
673 }
674 } else
675 list = &((*list)->next);
676 }
677
678 /*
679 * This is called by the SNAP driver whenever we see an AARP SNAP
680 * frame. We currently only support Ethernet.
681 */
aarp_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt,struct net_device * orig_dev)682 static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
683 struct packet_type *pt, struct net_device *orig_dev)
684 {
685 struct elapaarp *ea = aarp_hdr(skb);
686 int hash, ret = 0;
687 __u16 function;
688 struct aarp_entry *a;
689 struct atalk_addr sa, *ma, da;
690 struct atalk_iface *ifa;
691
692 if (!net_eq(dev_net(dev), &init_net))
693 goto out0;
694
695 /* We only do Ethernet SNAP AARP. */
696 if (dev->type != ARPHRD_ETHER)
697 goto out0;
698
699 /* Frame size ok? */
700 if (!skb_pull(skb, sizeof(*ea)))
701 goto out0;
702
703 function = ntohs(ea->function);
704
705 /* Sanity check fields. */
706 if (function < AARP_REQUEST || function > AARP_PROBE ||
707 ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
708 ea->pa_src_zero || ea->pa_dst_zero)
709 goto out0;
710
711 /* Looks good. */
712 hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
713
714 /* Build an address. */
715 sa.s_node = ea->pa_src_node;
716 sa.s_net = ea->pa_src_net;
717
718 /* Process the packet. Check for replies of me. */
719 ifa = atalk_find_dev(dev);
720 if (!ifa)
721 goto out1;
722
723 if (ifa->status & ATIF_PROBE &&
724 ifa->address.s_node == ea->pa_dst_node &&
725 ifa->address.s_net == ea->pa_dst_net) {
726 ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
727 goto out1;
728 }
729
730 /* Check for replies of proxy AARP entries */
731 da.s_node = ea->pa_dst_node;
732 da.s_net = ea->pa_dst_net;
733
734 write_lock_bh(&aarp_lock);
735 a = __aarp_find_entry(proxies[hash], dev, &da);
736
737 if (a && a->status & ATIF_PROBE) {
738 a->status |= ATIF_PROBE_FAIL;
739 /*
740 * we do not respond to probe or request packets of
741 * this address while we are probing this address
742 */
743 goto unlock;
744 }
745
746 switch (function) {
747 case AARP_REPLY:
748 if (!unresolved_count) /* Speed up */
749 break;
750
751 /* Find the entry. */
752 a = __aarp_find_entry(unresolved[hash], dev, &sa);
753 if (!a || dev != a->dev)
754 break;
755
756 /* We can fill one in - this is good. */
757 ether_addr_copy(a->hwaddr, ea->hw_src);
758 __aarp_resolved(&unresolved[hash], a, hash);
759 if (!unresolved_count)
760 mod_timer(&aarp_timer,
761 jiffies + sysctl_aarp_expiry_time);
762 break;
763
764 case AARP_REQUEST:
765 case AARP_PROBE:
766
767 /*
768 * If it is my address set ma to my address and reply.
769 * We can treat probe and request the same. Probe
770 * simply means we shouldn't cache the querying host,
771 * as in a probe they are proposing an address not
772 * using one.
773 *
774 * Support for proxy-AARP added. We check if the
775 * address is one of our proxies before we toss the
776 * packet out.
777 */
778
779 sa.s_node = ea->pa_dst_node;
780 sa.s_net = ea->pa_dst_net;
781
782 /* See if we have a matching proxy. */
783 ma = __aarp_proxy_find(dev, &sa);
784 if (!ma)
785 ma = &ifa->address;
786 else { /* We need to make a copy of the entry. */
787 da.s_node = sa.s_node;
788 da.s_net = sa.s_net;
789 ma = &da;
790 }
791
792 if (function == AARP_PROBE) {
793 /*
794 * A probe implies someone trying to get an
795 * address. So as a precaution flush any
796 * entries we have for this address.
797 */
798 a = __aarp_find_entry(resolved[sa.s_node %
799 (AARP_HASH_SIZE - 1)],
800 skb->dev, &sa);
801
802 /*
803 * Make it expire next tick - that avoids us
804 * getting into a probe/flush/learn/probe/
805 * flush/learn cycle during probing of a slow
806 * to respond host addr.
807 */
808 if (a) {
809 a->expires_at = jiffies - 1;
810 mod_timer(&aarp_timer, jiffies +
811 sysctl_aarp_tick_time);
812 }
813 }
814
815 if (sa.s_node != ma->s_node)
816 break;
817
818 if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
819 break;
820
821 sa.s_node = ea->pa_src_node;
822 sa.s_net = ea->pa_src_net;
823
824 /* aarp_my_address has found the address to use for us.
825 */
826 aarp_send_reply(dev, ma, &sa, ea->hw_src);
827 break;
828 }
829
830 unlock:
831 write_unlock_bh(&aarp_lock);
832 out1:
833 ret = 1;
834 out0:
835 kfree_skb(skb);
836 return ret;
837 }
838
839 static struct notifier_block aarp_notifier = {
840 .notifier_call = aarp_device_event,
841 };
842
843 static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
844
aarp_proto_init(void)845 int __init aarp_proto_init(void)
846 {
847 int rc;
848
849 aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
850 if (!aarp_dl) {
851 printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
852 return -ENOMEM;
853 }
854 timer_setup(&aarp_timer, aarp_expire_timeout, 0);
855 aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
856 add_timer(&aarp_timer);
857 rc = register_netdevice_notifier(&aarp_notifier);
858 if (rc) {
859 del_timer_sync(&aarp_timer);
860 unregister_snap_client(aarp_dl);
861 }
862 return rc;
863 }
864
865 /* Remove the AARP entries associated with a device. */
aarp_device_down(struct net_device * dev)866 void aarp_device_down(struct net_device *dev)
867 {
868 int ct;
869
870 write_lock_bh(&aarp_lock);
871
872 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
873 __aarp_expire_device(&resolved[ct], dev);
874 __aarp_expire_device(&unresolved[ct], dev);
875 __aarp_expire_device(&proxies[ct], dev);
876 }
877
878 write_unlock_bh(&aarp_lock);
879 }
880
881 #ifdef CONFIG_PROC_FS
882 /*
883 * Get the aarp entry that is in the chain described
884 * by the iterator.
885 * If pos is set then skip till that index.
886 * pos = 1 is the first entry
887 */
iter_next(struct aarp_iter_state * iter,loff_t * pos)888 static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
889 {
890 int ct = iter->bucket;
891 struct aarp_entry **table = iter->table;
892 loff_t off = 0;
893 struct aarp_entry *entry;
894
895 rescan:
896 while (ct < AARP_HASH_SIZE) {
897 for (entry = table[ct]; entry; entry = entry->next) {
898 if (!pos || ++off == *pos) {
899 iter->table = table;
900 iter->bucket = ct;
901 return entry;
902 }
903 }
904 ++ct;
905 }
906
907 if (table == resolved) {
908 ct = 0;
909 table = unresolved;
910 goto rescan;
911 }
912 if (table == unresolved) {
913 ct = 0;
914 table = proxies;
915 goto rescan;
916 }
917 return NULL;
918 }
919
aarp_seq_start(struct seq_file * seq,loff_t * pos)920 static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
921 __acquires(aarp_lock)
922 {
923 struct aarp_iter_state *iter = seq->private;
924
925 read_lock_bh(&aarp_lock);
926 iter->table = resolved;
927 iter->bucket = 0;
928
929 return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
930 }
931
aarp_seq_next(struct seq_file * seq,void * v,loff_t * pos)932 static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
933 {
934 struct aarp_entry *entry = v;
935 struct aarp_iter_state *iter = seq->private;
936
937 ++*pos;
938
939 /* first line after header */
940 if (v == SEQ_START_TOKEN)
941 entry = iter_next(iter, NULL);
942
943 /* next entry in current bucket */
944 else if (entry->next)
945 entry = entry->next;
946
947 /* next bucket or table */
948 else {
949 ++iter->bucket;
950 entry = iter_next(iter, NULL);
951 }
952 return entry;
953 }
954
aarp_seq_stop(struct seq_file * seq,void * v)955 static void aarp_seq_stop(struct seq_file *seq, void *v)
956 __releases(aarp_lock)
957 {
958 read_unlock_bh(&aarp_lock);
959 }
960
dt2str(unsigned long ticks)961 static const char *dt2str(unsigned long ticks)
962 {
963 static char buf[32];
964
965 sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100) / HZ);
966
967 return buf;
968 }
969
aarp_seq_show(struct seq_file * seq,void * v)970 static int aarp_seq_show(struct seq_file *seq, void *v)
971 {
972 struct aarp_iter_state *iter = seq->private;
973 struct aarp_entry *entry = v;
974 unsigned long now = jiffies;
975
976 if (v == SEQ_START_TOKEN)
977 seq_puts(seq,
978 "Address Interface Hardware Address"
979 " Expires LastSend Retry Status\n");
980 else {
981 seq_printf(seq, "%04X:%02X %-12s",
982 ntohs(entry->target_addr.s_net),
983 (unsigned int) entry->target_addr.s_node,
984 entry->dev ? entry->dev->name : "????");
985 seq_printf(seq, "%pM", entry->hwaddr);
986 seq_printf(seq, " %8s",
987 dt2str((long)entry->expires_at - (long)now));
988 if (iter->table == unresolved)
989 seq_printf(seq, " %8s %6hu",
990 dt2str(now - entry->last_sent),
991 entry->xmit_count);
992 else
993 seq_puts(seq, " ");
994 seq_printf(seq, " %s\n",
995 (iter->table == resolved) ? "resolved"
996 : (iter->table == unresolved) ? "unresolved"
997 : (iter->table == proxies) ? "proxies"
998 : "unknown");
999 }
1000 return 0;
1001 }
1002
1003 const struct seq_operations aarp_seq_ops = {
1004 .start = aarp_seq_start,
1005 .next = aarp_seq_next,
1006 .stop = aarp_seq_stop,
1007 .show = aarp_seq_show,
1008 };
1009 #endif
1010
1011 /* General module cleanup. Called from cleanup_module() in ddp.c. */
aarp_cleanup_module(void)1012 void aarp_cleanup_module(void)
1013 {
1014 del_timer_sync(&aarp_timer);
1015 unregister_netdevice_notifier(&aarp_notifier);
1016 unregister_snap_client(aarp_dl);
1017 aarp_purge();
1018 }
1019