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