blob: 8ad3ec2610b6499b92b2f3bc97ac02d2d043dd45 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * AARP: An implementation of the AppleTalk AARP protocol for
3 * Ethernet 'ELAP'.
4 *
5 * Alan Cox <Alan.Cox@linux.org>
6 *
7 * This doesn't fit cleanly with the IP arp. Potentially we can use
8 * the generic neighbour discovery code to clean this up.
9 *
10 * FIXME:
11 * We ought to handle the retransmits with a single list and a
12 * separate fast timer for when it is needed.
13 * Use neighbour discovery code.
14 * Token Ring Support.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
20 *
21 *
22 * References:
23 * Inside AppleTalk (2nd Ed).
24 * Fixes:
25 * Jaume Grau - flush caches on AARP_PROBE
26 * Rob Newberry - Added proxy AARP and AARP proc fs,
27 * moved probing from DDP module.
28 * Arnaldo C. Melo - don't mangle rx packets
29 *
30 */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <net/sock.h>
35#include <net/datalink.h>
36#include <net/psnap.h>
37#include <linux/atalk.h>
Nishanth Aravamudan285b3af2005-06-22 22:11:44 -070038#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/init.h>
40#include <linux/proc_fs.h>
41#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040042#include <linux/export.h>
David S. Miller9be68c12014-01-21 18:55:22 -080043#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45int sysctl_aarp_expiry_time = AARP_EXPIRY_TIME;
46int sysctl_aarp_tick_time = AARP_TICK_TIME;
47int sysctl_aarp_retransmit_limit = AARP_RETRANSMIT_LIMIT;
48int sysctl_aarp_resolve_time = AARP_RESOLVE_TIME;
49
50/* Lists of aarp entries */
51/**
52 * struct aarp_entry - AARP entry
53 * @last_sent - Last time we xmitted the aarp request
54 * @packet_queue - Queue of frames wait for resolution
55 * @status - Used for proxy AARP
56 * expires_at - Entry expiry time
57 * target_addr - DDP Address
58 * dev - Device to use
59 * hwaddr - Physical i/f address of target/router
60 * xmit_count - When this hits 10 we give up
61 * next - Next entry in chain
62 */
63struct aarp_entry {
64 /* These first two are only used for unresolved entries */
65 unsigned long last_sent;
66 struct sk_buff_head packet_queue;
67 int status;
68 unsigned long expires_at;
69 struct atalk_addr target_addr;
70 struct net_device *dev;
Joe Perches90ccb6a2014-01-20 09:52:15 -080071 char hwaddr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unsigned short xmit_count;
73 struct aarp_entry *next;
74};
75
76/* Hashed list of resolved, unresolved and proxy entries */
77static struct aarp_entry *resolved[AARP_HASH_SIZE];
78static struct aarp_entry *unresolved[AARP_HASH_SIZE];
79static struct aarp_entry *proxies[AARP_HASH_SIZE];
80static int unresolved_count;
81
82/* One lock protects it all. */
83static DEFINE_RWLOCK(aarp_lock);
84
85/* Used to walk the list and purge/kick entries. */
86static struct timer_list aarp_timer;
87
88/*
89 * Delete an aarp queue
90 *
91 * Must run under aarp_lock.
92 */
93static void __aarp_expire(struct aarp_entry *a)
94{
95 skb_queue_purge(&a->packet_queue);
96 kfree(a);
97}
98
99/*
100 * Send an aarp queue entry request
101 *
102 * Must run under aarp_lock.
103 */
104static void __aarp_send_query(struct aarp_entry *a)
105{
106 static unsigned char aarp_eth_multicast[ETH_ALEN] =
107 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
108 struct net_device *dev = a->dev;
109 struct elapaarp *eah;
110 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
111 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
112 struct atalk_addr *sat = atalk_find_dev_addr(dev);
113
114 if (!skb)
115 return;
116
117 if (!sat) {
118 kfree_skb(skb);
119 return;
120 }
121
122 /* Set up the buffer */
123 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300124 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300125 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300126 skb_put(skb, sizeof(*eah));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 skb->protocol = htons(ETH_P_ATALK);
128 skb->dev = dev;
129 eah = aarp_hdr(skb);
130
131 /* Set up the ARP */
132 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
133 eah->pa_type = htons(ETH_P_ATALK);
134 eah->hw_len = ETH_ALEN;
135 eah->pa_len = AARP_PA_ALEN;
136 eah->function = htons(AARP_REQUEST);
137
Joe Perches90ccb6a2014-01-20 09:52:15 -0800138 ether_addr_copy(eah->hw_src, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 eah->pa_src_zero = 0;
141 eah->pa_src_net = sat->s_net;
142 eah->pa_src_node = sat->s_node;
143
Joe Perches1a73de02015-03-02 19:54:53 -0800144 eth_zero_addr(eah->hw_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 eah->pa_dst_zero = 0;
147 eah->pa_dst_net = a->target_addr.s_net;
148 eah->pa_dst_node = a->target_addr.s_node;
149
150 /* Send it */
151 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
152 /* Update the sending count */
153 a->xmit_count++;
154 a->last_sent = jiffies;
155}
156
157/* This runs under aarp_lock and in softint context, so only atomic memory
158 * allocations can be used. */
159static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
160 struct atalk_addr *them, unsigned char *sha)
161{
162 struct elapaarp *eah;
163 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
164 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
165
166 if (!skb)
167 return;
168
169 /* Set up the buffer */
170 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300171 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300172 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300173 skb_put(skb, sizeof(*eah));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 skb->protocol = htons(ETH_P_ATALK);
175 skb->dev = dev;
176 eah = aarp_hdr(skb);
177
178 /* Set up the ARP */
179 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
180 eah->pa_type = htons(ETH_P_ATALK);
181 eah->hw_len = ETH_ALEN;
182 eah->pa_len = AARP_PA_ALEN;
183 eah->function = htons(AARP_REPLY);
184
Joe Perches90ccb6a2014-01-20 09:52:15 -0800185 ether_addr_copy(eah->hw_src, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 eah->pa_src_zero = 0;
188 eah->pa_src_net = us->s_net;
189 eah->pa_src_node = us->s_node;
190
191 if (!sha)
Joe Perches1a73de02015-03-02 19:54:53 -0800192 eth_zero_addr(eah->hw_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 else
Joe Perches90ccb6a2014-01-20 09:52:15 -0800194 ether_addr_copy(eah->hw_dst, sha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 eah->pa_dst_zero = 0;
197 eah->pa_dst_net = them->s_net;
198 eah->pa_dst_node = them->s_node;
199
200 /* Send it */
201 aarp_dl->request(aarp_dl, skb, sha);
202}
203
204/*
205 * Send probe frames. Called from aarp_probe_network and
206 * aarp_proxy_probe_network.
207 */
208
209static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
210{
211 struct elapaarp *eah;
212 int len = dev->hard_header_len + sizeof(*eah) + aarp_dl->header_length;
213 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
214 static unsigned char aarp_eth_multicast[ETH_ALEN] =
215 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
216
217 if (!skb)
218 return;
219
220 /* Set up the buffer */
221 skb_reserve(skb, dev->hard_header_len + aarp_dl->header_length);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300222 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300223 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300224 skb_put(skb, sizeof(*eah));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 skb->protocol = htons(ETH_P_ATALK);
226 skb->dev = dev;
227 eah = aarp_hdr(skb);
228
229 /* Set up the ARP */
230 eah->hw_type = htons(AARP_HW_TYPE_ETHERNET);
231 eah->pa_type = htons(ETH_P_ATALK);
232 eah->hw_len = ETH_ALEN;
233 eah->pa_len = AARP_PA_ALEN;
234 eah->function = htons(AARP_PROBE);
235
Joe Perches90ccb6a2014-01-20 09:52:15 -0800236 ether_addr_copy(eah->hw_src, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 eah->pa_src_zero = 0;
239 eah->pa_src_net = us->s_net;
240 eah->pa_src_node = us->s_node;
241
Joe Perches1a73de02015-03-02 19:54:53 -0800242 eth_zero_addr(eah->hw_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 eah->pa_dst_zero = 0;
245 eah->pa_dst_net = us->s_net;
246 eah->pa_dst_node = us->s_node;
247
248 /* Send it */
249 aarp_dl->request(aarp_dl, skb, aarp_eth_multicast);
250}
251
252/*
253 * Handle an aarp timer expire
254 *
255 * Must run under the aarp_lock.
256 */
257
258static void __aarp_expire_timer(struct aarp_entry **n)
259{
260 struct aarp_entry *t;
261
262 while (*n)
263 /* Expired ? */
264 if (time_after(jiffies, (*n)->expires_at)) {
265 t = *n;
266 *n = (*n)->next;
267 __aarp_expire(t);
268 } else
269 n = &((*n)->next);
270}
271
272/*
273 * Kick all pending requests 5 times a second.
274 *
275 * Must run under the aarp_lock.
276 */
277static void __aarp_kick(struct aarp_entry **n)
278{
279 struct aarp_entry *t;
280
281 while (*n)
282 /* Expired: if this will be the 11th tx, we delete instead. */
283 if ((*n)->xmit_count >= sysctl_aarp_retransmit_limit) {
284 t = *n;
285 *n = (*n)->next;
286 __aarp_expire(t);
287 } else {
288 __aarp_send_query(*n);
289 n = &((*n)->next);
290 }
291}
292
293/*
294 * A device has gone down. Take all entries referring to the device
295 * and remove them.
296 *
297 * Must run under the aarp_lock.
298 */
299static void __aarp_expire_device(struct aarp_entry **n, struct net_device *dev)
300{
301 struct aarp_entry *t;
302
303 while (*n)
304 if ((*n)->dev == dev) {
305 t = *n;
306 *n = (*n)->next;
307 __aarp_expire(t);
308 } else
309 n = &((*n)->next);
310}
311
312/* Handle the timer event */
313static void aarp_expire_timeout(unsigned long unused)
314{
315 int ct;
316
317 write_lock_bh(&aarp_lock);
318
319 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
320 __aarp_expire_timer(&resolved[ct]);
321 __aarp_kick(&unresolved[ct]);
322 __aarp_expire_timer(&unresolved[ct]);
323 __aarp_expire_timer(&proxies[ct]);
324 }
325
326 write_unlock_bh(&aarp_lock);
327 mod_timer(&aarp_timer, jiffies +
328 (unresolved_count ? sysctl_aarp_tick_time :
329 sysctl_aarp_expiry_time));
330}
331
332/* Network device notifier chain handler. */
333static int aarp_device_event(struct notifier_block *this, unsigned long event,
334 void *ptr)
335{
Jiri Pirko351638e2013-05-28 01:30:21 +0000336 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int ct;
338
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700339 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200340 return NOTIFY_DONE;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (event == NETDEV_DOWN) {
343 write_lock_bh(&aarp_lock);
344
345 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
Eric W. Biederman890d52d2007-09-12 11:26:59 +0200346 __aarp_expire_device(&resolved[ct], dev);
347 __aarp_expire_device(&unresolved[ct], dev);
348 __aarp_expire_device(&proxies[ct], dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 write_unlock_bh(&aarp_lock);
352 }
353 return NOTIFY_DONE;
354}
355
356/* Expire all entries in a hash chain */
357static void __aarp_expire_all(struct aarp_entry **n)
358{
359 struct aarp_entry *t;
360
361 while (*n) {
362 t = *n;
363 *n = (*n)->next;
364 __aarp_expire(t);
365 }
366}
367
368/* Cleanup all hash chains -- module unloading */
369static void aarp_purge(void)
370{
371 int ct;
372
373 write_lock_bh(&aarp_lock);
374 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
375 __aarp_expire_all(&resolved[ct]);
376 __aarp_expire_all(&unresolved[ct]);
377 __aarp_expire_all(&proxies[ct]);
378 }
379 write_unlock_bh(&aarp_lock);
380}
381
382/*
383 * Create a new aarp entry. This must use GFP_ATOMIC because it
384 * runs while holding spinlocks.
385 */
386static struct aarp_entry *aarp_alloc(void)
387{
388 struct aarp_entry *a = kmalloc(sizeof(*a), GFP_ATOMIC);
389
390 if (a)
391 skb_queue_head_init(&a->packet_queue);
392 return a;
393}
394
395/*
396 * Find an entry. We might return an expired but not yet purged entry. We
397 * don't care as it will do no harm.
398 *
399 * This must run under the aarp_lock.
400 */
401static struct aarp_entry *__aarp_find_entry(struct aarp_entry *list,
402 struct net_device *dev,
403 struct atalk_addr *sat)
404{
405 while (list) {
406 if (list->target_addr.s_net == sat->s_net &&
407 list->target_addr.s_node == sat->s_node &&
408 list->dev == dev)
409 break;
410 list = list->next;
411 }
412
413 return list;
414}
415
416/* Called from the DDP code, and thus must be exported. */
417void aarp_proxy_remove(struct net_device *dev, struct atalk_addr *sa)
418{
419 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
420 struct aarp_entry *a;
421
422 write_lock_bh(&aarp_lock);
423
424 a = __aarp_find_entry(proxies[hash], dev, sa);
425 if (a)
426 a->expires_at = jiffies - 1;
427
428 write_unlock_bh(&aarp_lock);
429}
430
431/* This must run under aarp_lock. */
432static struct atalk_addr *__aarp_proxy_find(struct net_device *dev,
433 struct atalk_addr *sa)
434{
435 int hash = sa->s_node % (AARP_HASH_SIZE - 1);
436 struct aarp_entry *a = __aarp_find_entry(proxies[hash], dev, sa);
437
438 return a ? sa : NULL;
439}
440
441/*
442 * Probe a Phase 1 device or a device that requires its Net:Node to
443 * be set via an ioctl.
444 */
445static void aarp_send_probe_phase1(struct atalk_iface *iface)
446{
447 struct ifreq atreq;
448 struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr;
Stephen Hemminger03b35ccb2009-01-07 17:21:44 -0800449 const struct net_device_ops *ops = iface->dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 sa->sat_addr.s_node = iface->address.s_node;
452 sa->sat_addr.s_net = ntohs(iface->address.s_net);
453
454 /* We pass the Net:Node to the drivers/cards by a Device ioctl. */
Stephen Hemminger03b35ccb2009-01-07 17:21:44 -0800455 if (!(ops->ndo_do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) {
456 ops->ndo_do_ioctl(iface->dev, &atreq, SIOCGIFADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (iface->address.s_net != htons(sa->sat_addr.s_net) ||
458 iface->address.s_node != sa->sat_addr.s_node)
459 iface->status |= ATIF_PROBE_FAIL;
460
461 iface->address.s_net = htons(sa->sat_addr.s_net);
462 iface->address.s_node = sa->sat_addr.s_node;
463 }
464}
465
466
467void aarp_probe_network(struct atalk_iface *atif)
468{
469 if (atif->dev->type == ARPHRD_LOCALTLK ||
470 atif->dev->type == ARPHRD_PPP)
471 aarp_send_probe_phase1(atif);
472 else {
473 unsigned int count;
474
475 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
476 aarp_send_probe(atif->dev, &atif->address);
477
478 /* Defer 1/10th */
Nishanth Aravamudan285b3af2005-06-22 22:11:44 -0700479 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (atif->status & ATIF_PROBE_FAIL)
482 break;
483 }
484 }
485}
486
487int aarp_proxy_probe_network(struct atalk_iface *atif, struct atalk_addr *sa)
488{
489 int hash, retval = -EPROTONOSUPPORT;
490 struct aarp_entry *entry;
491 unsigned int count;
492
493 /*
494 * we don't currently support LocalTalk or PPP for proxy AARP;
495 * if someone wants to try and add it, have fun
496 */
497 if (atif->dev->type == ARPHRD_LOCALTLK ||
498 atif->dev->type == ARPHRD_PPP)
499 goto out;
500
501 /*
502 * create a new AARP entry with the flags set to be published --
503 * we need this one to hang around even if it's in use
504 */
505 entry = aarp_alloc();
506 retval = -ENOMEM;
507 if (!entry)
508 goto out;
509
510 entry->expires_at = -1;
511 entry->status = ATIF_PROBE;
512 entry->target_addr.s_node = sa->s_node;
513 entry->target_addr.s_net = sa->s_net;
514 entry->dev = atif->dev;
515
516 write_lock_bh(&aarp_lock);
517
518 hash = sa->s_node % (AARP_HASH_SIZE - 1);
519 entry->next = proxies[hash];
520 proxies[hash] = entry;
521
522 for (count = 0; count < AARP_RETRANSMIT_LIMIT; count++) {
523 aarp_send_probe(atif->dev, sa);
524
525 /* Defer 1/10th */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 write_unlock_bh(&aarp_lock);
Nishanth Aravamudan285b3af2005-06-22 22:11:44 -0700527 msleep(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 write_lock_bh(&aarp_lock);
529
530 if (entry->status & ATIF_PROBE_FAIL)
531 break;
532 }
533
534 if (entry->status & ATIF_PROBE_FAIL) {
535 entry->expires_at = jiffies - 1; /* free the entry */
536 retval = -EADDRINUSE; /* return network full */
537 } else { /* clear the probing flag */
538 entry->status &= ~ATIF_PROBE;
539 retval = 1;
540 }
541
542 write_unlock_bh(&aarp_lock);
543out:
544 return retval;
545}
546
547/* Send a DDP frame */
548int aarp_send_ddp(struct net_device *dev, struct sk_buff *skb,
549 struct atalk_addr *sa, void *hwaddr)
550{
551 static char ddp_eth_multicast[ETH_ALEN] =
552 { 0x09, 0x00, 0x07, 0xFF, 0xFF, 0xFF };
553 int hash;
554 struct aarp_entry *a;
555
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700556 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* Check for LocalTalk first */
559 if (dev->type == ARPHRD_LOCALTLK) {
560 struct atalk_addr *at = atalk_find_dev_addr(dev);
561 struct ddpehdr *ddp = (struct ddpehdr *)skb->data;
562 int ft = 2;
563
564 /*
565 * Compressible ?
566 *
567 * IFF: src_net == dest_net == device_net
568 * (zero matches anything)
569 */
570
571 if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) &&
572 (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) {
573 skb_pull(skb, sizeof(*ddp) - 4);
574
575 /*
576 * The upper two remaining bytes are the port
577 * numbers we just happen to need. Now put the
578 * length in the lower two.
579 */
Alexey Dobriyanf6e276ee2005-06-20 13:32:05 -0700580 *((__be16 *)skb->data) = htons(skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 ft = 1;
582 }
583 /*
584 * Nice and easy. No AARP type protocols occur here so we can
585 * just shovel it out with a 3 byte LLAP header
586 */
587
588 skb_push(skb, 3);
589 skb->data[0] = sa->s_node;
590 skb->data[1] = at->s_node;
591 skb->data[2] = ft;
592 skb->dev = dev;
593 goto sendit;
594 }
595
596 /* On a PPP link we neither compress nor aarp. */
597 if (dev->type == ARPHRD_PPP) {
598 skb->protocol = htons(ETH_P_PPPTALK);
599 skb->dev = dev;
600 goto sendit;
601 }
602
603 /* Non ELAP we cannot do. */
604 if (dev->type != ARPHRD_ETHER)
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -0700605 goto free_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 skb->dev = dev;
608 skb->protocol = htons(ETH_P_ATALK);
609 hash = sa->s_node % (AARP_HASH_SIZE - 1);
610
611 /* Do we have a resolved entry? */
612 if (sa->s_node == ATADDR_BCAST) {
613 /* Send it */
614 ddp_dl->request(ddp_dl, skb, ddp_eth_multicast);
615 goto sent;
616 }
617
618 write_lock_bh(&aarp_lock);
619 a = __aarp_find_entry(resolved[hash], dev, sa);
620
621 if (a) { /* Return 1 and fill in the address */
622 a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10);
623 ddp_dl->request(ddp_dl, skb, a->hwaddr);
624 write_unlock_bh(&aarp_lock);
625 goto sent;
626 }
627
628 /* Do we have an unresolved entry: This is the less common path */
629 a = __aarp_find_entry(unresolved[hash], dev, sa);
630 if (a) { /* Queue onto the unresolved queue */
631 skb_queue_tail(&a->packet_queue, skb);
632 goto out_unlock;
633 }
634
635 /* Allocate a new entry */
636 a = aarp_alloc();
637 if (!a) {
638 /* Whoops slipped... good job it's an unreliable protocol 8) */
639 write_unlock_bh(&aarp_lock);
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -0700640 goto free_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
643 /* Set up the queue */
644 skb_queue_tail(&a->packet_queue, skb);
645 a->expires_at = jiffies + sysctl_aarp_resolve_time;
646 a->dev = dev;
647 a->next = unresolved[hash];
648 a->target_addr = *sa;
649 a->xmit_count = 0;
650 unresolved[hash] = a;
651 unresolved_count++;
652
653 /* Send an initial request for the address */
654 __aarp_send_query(a);
655
656 /*
657 * Switch to fast timer if needed (That is if this is the first
658 * unresolved entry to get added)
659 */
660
661 if (unresolved_count == 1)
662 mod_timer(&aarp_timer, jiffies + sysctl_aarp_tick_time);
663
664 /* Now finally, it is safe to drop the lock. */
665out_unlock:
666 write_unlock_bh(&aarp_lock);
667
668 /* Tell the ddp layer we have taken over for this frame. */
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -0700669 goto sent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671sendit:
672 if (skb->sk)
673 skb->priority = skb->sk->sk_priority;
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -0700674 if (dev_queue_xmit(skb))
675 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676sent:
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -0700677 return NET_XMIT_SUCCESS;
678free_it:
679 kfree_skb(skb);
680drop:
681 return NET_XMIT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
Arnaldo Carvalho de Meloffcfb8d2009-09-11 11:35:22 -0700683EXPORT_SYMBOL(aarp_send_ddp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685/*
686 * An entry in the aarp unresolved queue has become resolved. Send
687 * all the frames queued under it.
688 *
689 * Must run under aarp_lock.
690 */
691static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a,
692 int hash)
693{
694 struct sk_buff *skb;
695
696 while (*list)
697 if (*list == a) {
698 unresolved_count--;
699 *list = a->next;
700
701 /* Move into the resolved list */
702 a->next = resolved[hash];
703 resolved[hash] = a;
704
705 /* Kick frames off */
706 while ((skb = skb_dequeue(&a->packet_queue)) != NULL) {
707 a->expires_at = jiffies +
708 sysctl_aarp_expiry_time * 10;
709 ddp_dl->request(ddp_dl, skb, a->hwaddr);
710 }
711 } else
712 list = &((*list)->next);
713}
714
715/*
716 * This is called by the SNAP driver whenever we see an AARP SNAP
717 * frame. We currently only support Ethernet.
718 */
719static int aarp_rcv(struct sk_buff *skb, struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700720 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 struct elapaarp *ea = aarp_hdr(skb);
723 int hash, ret = 0;
724 __u16 function;
725 struct aarp_entry *a;
726 struct atalk_addr sa, *ma, da;
727 struct atalk_iface *ifa;
728
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700729 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane730c152007-09-17 11:53:39 -0700730 goto out0;
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /* We only do Ethernet SNAP AARP. */
733 if (dev->type != ARPHRD_ETHER)
734 goto out0;
735
736 /* Frame size ok? */
737 if (!skb_pull(skb, sizeof(*ea)))
738 goto out0;
739
740 function = ntohs(ea->function);
741
742 /* Sanity check fields. */
743 if (function < AARP_REQUEST || function > AARP_PROBE ||
744 ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN ||
745 ea->pa_src_zero || ea->pa_dst_zero)
746 goto out0;
747
748 /* Looks good. */
749 hash = ea->pa_src_node % (AARP_HASH_SIZE - 1);
750
751 /* Build an address. */
752 sa.s_node = ea->pa_src_node;
753 sa.s_net = ea->pa_src_net;
754
755 /* Process the packet. Check for replies of me. */
756 ifa = atalk_find_dev(dev);
757 if (!ifa)
758 goto out1;
759
760 if (ifa->status & ATIF_PROBE &&
761 ifa->address.s_node == ea->pa_dst_node &&
762 ifa->address.s_net == ea->pa_dst_net) {
763 ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */
764 goto out1;
765 }
766
767 /* Check for replies of proxy AARP entries */
768 da.s_node = ea->pa_dst_node;
769 da.s_net = ea->pa_dst_net;
770
771 write_lock_bh(&aarp_lock);
772 a = __aarp_find_entry(proxies[hash], dev, &da);
773
774 if (a && a->status & ATIF_PROBE) {
775 a->status |= ATIF_PROBE_FAIL;
776 /*
777 * we do not respond to probe or request packets for
778 * this address while we are probing this address
779 */
780 goto unlock;
781 }
782
783 switch (function) {
Joe Perches4a9e4b02011-07-01 09:43:02 +0000784 case AARP_REPLY:
785 if (!unresolved_count) /* Speed up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 break;
787
Joe Perches4a9e4b02011-07-01 09:43:02 +0000788 /* Find the entry. */
789 a = __aarp_find_entry(unresolved[hash], dev, &sa);
790 if (!a || dev != a->dev)
791 break;
792
793 /* We can fill one in - this is good. */
Joe Perches90ccb6a2014-01-20 09:52:15 -0800794 ether_addr_copy(a->hwaddr, ea->hw_src);
Joe Perches4a9e4b02011-07-01 09:43:02 +0000795 __aarp_resolved(&unresolved[hash], a, hash);
796 if (!unresolved_count)
797 mod_timer(&aarp_timer,
798 jiffies + sysctl_aarp_expiry_time);
799 break;
800
801 case AARP_REQUEST:
802 case AARP_PROBE:
803
804 /*
805 * If it is my address set ma to my address and reply.
806 * We can treat probe and request the same. Probe
807 * simply means we shouldn't cache the querying host,
808 * as in a probe they are proposing an address not
809 * using one.
810 *
811 * Support for proxy-AARP added. We check if the
812 * address is one of our proxies before we toss the
813 * packet out.
814 */
815
816 sa.s_node = ea->pa_dst_node;
817 sa.s_net = ea->pa_dst_net;
818
819 /* See if we have a matching proxy. */
820 ma = __aarp_proxy_find(dev, &sa);
821 if (!ma)
822 ma = &ifa->address;
823 else { /* We need to make a copy of the entry. */
824 da.s_node = sa.s_node;
825 da.s_net = sa.s_net;
826 ma = &da;
827 }
828
829 if (function == AARP_PROBE) {
830 /*
831 * A probe implies someone trying to get an
832 * address. So as a precaution flush any
833 * entries we have for this address.
834 */
835 a = __aarp_find_entry(resolved[sa.s_node %
836 (AARP_HASH_SIZE - 1)],
837 skb->dev, &sa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /*
Joe Perches4a9e4b02011-07-01 09:43:02 +0000840 * Make it expire next tick - that avoids us
841 * getting into a probe/flush/learn/probe/
842 * flush/learn cycle during probing of a slow
843 * to respond host addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 */
Joe Perches4a9e4b02011-07-01 09:43:02 +0000845 if (a) {
846 a->expires_at = jiffies - 1;
847 mod_timer(&aarp_timer, jiffies +
848 sysctl_aarp_tick_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Joe Perches4a9e4b02011-07-01 09:43:02 +0000850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Joe Perches4a9e4b02011-07-01 09:43:02 +0000852 if (sa.s_node != ma->s_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 break;
Joe Perches4a9e4b02011-07-01 09:43:02 +0000854
855 if (sa.s_net && ma->s_net && sa.s_net != ma->s_net)
856 break;
857
858 sa.s_node = ea->pa_src_node;
859 sa.s_net = ea->pa_src_net;
860
861 /* aarp_my_address has found the address to use for us.
862 */
863 aarp_send_reply(dev, ma, &sa, ea->hw_src);
864 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
867unlock:
868 write_unlock_bh(&aarp_lock);
869out1:
870 ret = 1;
871out0:
872 kfree_skb(skb);
873 return ret;
874}
875
876static struct notifier_block aarp_notifier = {
877 .notifier_call = aarp_device_event,
878};
879
880static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 };
881
882void __init aarp_proto_init(void)
883{
884 aarp_dl = register_snap_client(aarp_snap_id, aarp_rcv);
885 if (!aarp_dl)
886 printk(KERN_CRIT "Unable to register AARP with SNAP.\n");
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800887 setup_timer(&aarp_timer, aarp_expire_timeout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 aarp_timer.expires = jiffies + sysctl_aarp_expiry_time;
889 add_timer(&aarp_timer);
890 register_netdevice_notifier(&aarp_notifier);
891}
892
893/* Remove the AARP entries associated with a device. */
894void aarp_device_down(struct net_device *dev)
895{
896 int ct;
897
898 write_lock_bh(&aarp_lock);
899
900 for (ct = 0; ct < AARP_HASH_SIZE; ct++) {
901 __aarp_expire_device(&resolved[ct], dev);
902 __aarp_expire_device(&unresolved[ct], dev);
903 __aarp_expire_device(&proxies[ct], dev);
904 }
905
906 write_unlock_bh(&aarp_lock);
907}
908
909#ifdef CONFIG_PROC_FS
910struct aarp_iter_state {
911 int bucket;
912 struct aarp_entry **table;
913};
914
915/*
916 * Get the aarp entry that is in the chain described
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900917 * by the iterator.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * If pos is set then skip till that index.
919 * pos = 1 is the first entry
920 */
921static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos)
922{
923 int ct = iter->bucket;
924 struct aarp_entry **table = iter->table;
925 loff_t off = 0;
926 struct aarp_entry *entry;
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 rescan:
wangweidongfd1dc262014-02-14 15:43:43 +0800929 while (ct < AARP_HASH_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 for (entry = table[ct]; entry; entry = entry->next) {
931 if (!pos || ++off == *pos) {
932 iter->table = table;
933 iter->bucket = ct;
934 return entry;
935 }
936 }
937 ++ct;
938 }
939
940 if (table == resolved) {
941 ct = 0;
942 table = unresolved;
943 goto rescan;
944 }
945 if (table == unresolved) {
946 ct = 0;
947 table = proxies;
948 goto rescan;
949 }
950 return NULL;
951}
952
953static void *aarp_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetca629f242008-01-15 03:28:43 -0800954 __acquires(aarp_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 struct aarp_iter_state *iter = seq->private;
957
958 read_lock_bh(&aarp_lock);
959 iter->table = resolved;
960 iter->bucket = 0;
961
962 return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN;
963}
964
965static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
966{
967 struct aarp_entry *entry = v;
968 struct aarp_iter_state *iter = seq->private;
969
970 ++*pos;
971
972 /* first line after header */
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900973 if (v == SEQ_START_TOKEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 entry = iter_next(iter, NULL);
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +0900975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* next entry in current bucket */
977 else if (entry->next)
978 entry = entry->next;
979
980 /* next bucket or table */
981 else {
982 ++iter->bucket;
983 entry = iter_next(iter, NULL);
984 }
985 return entry;
986}
987
988static void aarp_seq_stop(struct seq_file *seq, void *v)
Eric Dumazetca629f242008-01-15 03:28:43 -0800989 __releases(aarp_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 read_unlock_bh(&aarp_lock);
992}
993
994static const char *dt2str(unsigned long ticks)
995{
996 static char buf[32];
997
wangweidongfd1dc262014-02-14 15:43:43 +0800998 sprintf(buf, "%ld.%02ld", ticks / HZ, ((ticks % HZ) * 100) / HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 return buf;
1001}
1002
1003static int aarp_seq_show(struct seq_file *seq, void *v)
1004{
1005 struct aarp_iter_state *iter = seq->private;
1006 struct aarp_entry *entry = v;
1007 unsigned long now = jiffies;
1008
1009 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001010 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 "Address Interface Hardware Address"
1012 " Expires LastSend Retry Status\n");
1013 else {
1014 seq_printf(seq, "%04X:%02X %-12s",
1015 ntohs(entry->target_addr.s_net),
1016 (unsigned int) entry->target_addr.s_node,
1017 entry->dev ? entry->dev->name : "????");
Johannes Berge1749612008-10-27 15:59:26 -07001018 seq_printf(seq, "%pM", entry->hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 seq_printf(seq, " %8s",
1020 dt2str((long)entry->expires_at - (long)now));
1021 if (iter->table == unresolved)
1022 seq_printf(seq, " %8s %6hu",
1023 dt2str(now - entry->last_sent),
1024 entry->xmit_count);
1025 else
1026 seq_puts(seq, " ");
1027 seq_printf(seq, " %s\n",
1028 (iter->table == resolved) ? "resolved"
1029 : (iter->table == unresolved) ? "unresolved"
1030 : (iter->table == proxies) ? "proxies"
1031 : "unknown");
YOSHIFUJI Hideakied4477b2007-02-09 23:24:27 +09001032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return 0;
1034}
1035
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001036static const struct seq_operations aarp_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 .start = aarp_seq_start,
1038 .next = aarp_seq_next,
1039 .stop = aarp_seq_stop,
1040 .show = aarp_seq_show,
1041};
1042
1043static int aarp_seq_open(struct inode *inode, struct file *file)
1044{
Pavel Emelyanovc20932d22008-02-29 11:38:24 -08001045 return seq_open_private(file, &aarp_seq_ops,
1046 sizeof(struct aarp_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
Arjan van de Ven9a321442007-02-12 00:55:35 -08001049const struct file_operations atalk_seq_arp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 .owner = THIS_MODULE,
1051 .open = aarp_seq_open,
1052 .read = seq_read,
1053 .llseek = seq_lseek,
1054 .release = seq_release_private,
1055};
1056#endif
1057
1058/* General module cleanup. Called from cleanup_module() in ddp.c. */
1059void aarp_cleanup_module(void)
1060{
1061 del_timer_sync(&aarp_timer);
1062 unregister_netdevice_notifier(&aarp_notifier);
1063 unregister_snap_client(aarp_dl);
1064 aarp_purge();
1065}