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