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