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