| 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(r: &a->refcnt); |
| 88 | } |
| 89 | |
| 90 | static inline void aarp_entry_put(struct aarp_entry *a) |
| 91 | { |
| 92 | if (refcount_dec_and_test(r: &a->refcnt)) |
| 93 | kfree(objp: 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(list: &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(size: 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, len: dev->hard_header_len + aarp_dl->header_length); |
| 132 | skb_reset_network_header(skb); |
| 133 | skb_reset_transport_header(skb); |
| 134 | skb_put(skb, len: 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(dst: eah->hw_src, 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(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(size: len, GFP_ATOMIC); |
| 173 | |
| 174 | if (!skb) |
| 175 | return; |
| 176 | |
| 177 | /* Set up the buffer */ |
| 178 | skb_reserve(skb, len: dev->hard_header_len + aarp_dl->header_length); |
| 179 | skb_reset_network_header(skb); |
| 180 | skb_reset_transport_header(skb); |
| 181 | skb_put(skb, len: 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(dst: eah->hw_src, 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(addr: eah->hw_dst); |
| 201 | else |
| 202 | ether_addr_copy(dst: eah->hw_dst, src: 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(size: 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, len: dev->hard_header_len + aarp_dl->header_length); |
| 230 | skb_reset_network_header(skb); |
| 231 | skb_reset_transport_header(skb); |
| 232 | skb_put(skb, len: 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(dst: eah->hw_src, 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(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(a: 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(a: t); |
| 295 | } else { |
| 296 | __aarp_send_query(a: *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(a: 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(n: &resolved[ct]); |
| 329 | __aarp_kick(n: &unresolved[ct]); |
| 330 | __aarp_expire_timer(n: &unresolved[ct]); |
| 331 | __aarp_expire_timer(n: &proxies[ct]); |
| 332 | } |
| 333 | |
| 334 | write_unlock_bh(&aarp_lock); |
| 335 | mod_timer(timer: &aarp_timer, expires: 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(info: ptr); |
| 345 | int ct; |
| 346 | |
| 347 | if (!net_eq(net1: dev_net(dev), net2: &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(n: &resolved[ct], dev); |
| 355 | __aarp_expire_device(n: &unresolved[ct], dev); |
| 356 | __aarp_expire_device(n: &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(a: 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(n: &resolved[ct]); |
| 384 | __aarp_expire_all(n: &unresolved[ct]); |
| 385 | __aarp_expire_all(n: &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(sizeof(*a), GFP_ATOMIC); |
| 397 | if (!a) |
| 398 | return NULL; |
| 399 | |
| 400 | refcount_set(r: &a->refcnt, n: 1); |
| 401 | skb_queue_head_init(list: &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(list: proxies[hash], dev, sat: 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(list: proxies[hash], dev, sat: 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(dev: atif->dev, us: &atif->address); |
| 457 | |
| 458 | /* Defer 1/10th */ |
| 459 | msleep(msecs: 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(a: 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(dev: atif->dev, us: sa); |
| 504 | |
| 505 | /* Defer 1/10th */ |
| 506 | write_unlock_bh(&aarp_lock); |
| 507 | msleep(msecs: 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(a: 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 | /* |
| 546 | * Compressible ? |
| 547 | * |
| 548 | * IFF: src_net == dest_net == device_net |
| 549 | * (zero matches anything) |
| 550 | */ |
| 551 | |
| 552 | if ((!ddp->deh_snet || at->s_net == ddp->deh_snet) && |
| 553 | (!ddp->deh_dnet || at->s_net == ddp->deh_dnet)) { |
| 554 | skb_pull(skb, len: sizeof(*ddp) - 4); |
| 555 | |
| 556 | /* |
| 557 | * The upper two remaining bytes are the port |
| 558 | * numbers we just happen to need. Now put the |
| 559 | * length in the lower two. |
| 560 | */ |
| 561 | *((__be16 *)skb->data) = htons(skb->len); |
| 562 | ft = 1; |
| 563 | } |
| 564 | /* |
| 565 | * Nice and easy. No AARP type protocols occur here so we can |
| 566 | * just shovel it out with a 3 byte LLAP header |
| 567 | */ |
| 568 | |
| 569 | skb_push(skb, len: 3); |
| 570 | skb->data[0] = sa->s_node; |
| 571 | skb->data[1] = at->s_node; |
| 572 | skb->data[2] = ft; |
| 573 | skb->dev = dev; |
| 574 | goto sendit; |
| 575 | } |
| 576 | |
| 577 | /* On a PPP link we neither compress nor aarp. */ |
| 578 | if (dev->type == ARPHRD_PPP) { |
| 579 | skb->protocol = htons(ETH_P_PPPTALK); |
| 580 | skb->dev = dev; |
| 581 | goto sendit; |
| 582 | } |
| 583 | |
| 584 | /* Non ELAP we cannot do. */ |
| 585 | if (dev->type != ARPHRD_ETHER) |
| 586 | goto free_it; |
| 587 | |
| 588 | skb->dev = dev; |
| 589 | skb->protocol = htons(ETH_P_ATALK); |
| 590 | hash = sa->s_node % (AARP_HASH_SIZE - 1); |
| 591 | |
| 592 | /* Do we have a resolved entry? */ |
| 593 | if (sa->s_node == ATADDR_BCAST) { |
| 594 | /* Send it */ |
| 595 | ddp_dl->request(ddp_dl, skb, ddp_eth_multicast); |
| 596 | goto sent; |
| 597 | } |
| 598 | |
| 599 | write_lock_bh(&aarp_lock); |
| 600 | a = __aarp_find_entry(list: resolved[hash], dev, sat: sa); |
| 601 | |
| 602 | if (a) { /* Return 1 and fill in the address */ |
| 603 | a->expires_at = jiffies + (sysctl_aarp_expiry_time * 10); |
| 604 | ddp_dl->request(ddp_dl, skb, a->hwaddr); |
| 605 | write_unlock_bh(&aarp_lock); |
| 606 | goto sent; |
| 607 | } |
| 608 | |
| 609 | /* Do we have an unresolved entry: This is the less common path */ |
| 610 | a = __aarp_find_entry(list: unresolved[hash], dev, sat: sa); |
| 611 | if (a) { /* Queue onto the unresolved queue */ |
| 612 | skb_queue_tail(list: &a->packet_queue, newsk: skb); |
| 613 | goto out_unlock; |
| 614 | } |
| 615 | |
| 616 | /* Allocate a new entry */ |
| 617 | a = aarp_alloc(); |
| 618 | if (!a) { |
| 619 | /* Whoops slipped... good job it's an unreliable protocol 8) */ |
| 620 | write_unlock_bh(&aarp_lock); |
| 621 | goto free_it; |
| 622 | } |
| 623 | |
| 624 | /* Set up the queue */ |
| 625 | skb_queue_tail(list: &a->packet_queue, newsk: skb); |
| 626 | a->expires_at = jiffies + sysctl_aarp_resolve_time; |
| 627 | a->dev = dev; |
| 628 | a->next = unresolved[hash]; |
| 629 | a->target_addr = *sa; |
| 630 | a->xmit_count = 0; |
| 631 | unresolved[hash] = a; |
| 632 | unresolved_count++; |
| 633 | |
| 634 | /* Send an initial request for the address */ |
| 635 | __aarp_send_query(a); |
| 636 | |
| 637 | /* |
| 638 | * Switch to fast timer if needed (That is if this is the first |
| 639 | * unresolved entry to get added) |
| 640 | */ |
| 641 | |
| 642 | if (unresolved_count == 1) |
| 643 | mod_timer(timer: &aarp_timer, expires: jiffies + sysctl_aarp_tick_time); |
| 644 | |
| 645 | /* Now finally, it is safe to drop the lock. */ |
| 646 | out_unlock: |
| 647 | write_unlock_bh(&aarp_lock); |
| 648 | |
| 649 | /* Tell the ddp layer we have taken over for this frame. */ |
| 650 | goto sent; |
| 651 | |
| 652 | sendit: |
| 653 | if (skb->sk) |
| 654 | skb->priority = READ_ONCE(skb->sk->sk_priority); |
| 655 | if (dev_queue_xmit(skb)) |
| 656 | goto drop; |
| 657 | sent: |
| 658 | return NET_XMIT_SUCCESS; |
| 659 | free_it: |
| 660 | kfree_skb(skb); |
| 661 | drop: |
| 662 | return NET_XMIT_DROP; |
| 663 | } |
| 664 | EXPORT_SYMBOL(aarp_send_ddp); |
| 665 | |
| 666 | /* |
| 667 | * An entry in the aarp unresolved queue has become resolved. Send |
| 668 | * all the frames queued under it. |
| 669 | * |
| 670 | * Must run under aarp_lock. |
| 671 | */ |
| 672 | static void __aarp_resolved(struct aarp_entry **list, struct aarp_entry *a, |
| 673 | int hash) |
| 674 | { |
| 675 | struct sk_buff *skb; |
| 676 | |
| 677 | while (*list) |
| 678 | if (*list == a) { |
| 679 | unresolved_count--; |
| 680 | *list = a->next; |
| 681 | |
| 682 | /* Move into the resolved list */ |
| 683 | a->next = resolved[hash]; |
| 684 | resolved[hash] = a; |
| 685 | |
| 686 | /* Kick frames off */ |
| 687 | while ((skb = skb_dequeue(list: &a->packet_queue)) != NULL) { |
| 688 | a->expires_at = jiffies + |
| 689 | sysctl_aarp_expiry_time * 10; |
| 690 | ddp_dl->request(ddp_dl, skb, a->hwaddr); |
| 691 | } |
| 692 | } else |
| 693 | list = &((*list)->next); |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * This is called by the SNAP driver whenever we see an AARP SNAP |
| 698 | * frame. We currently only support Ethernet. |
| 699 | */ |
| 700 | static int aarp_rcv(struct sk_buff *skb, struct net_device *dev, |
| 701 | struct packet_type *pt, struct net_device *orig_dev) |
| 702 | { |
| 703 | struct elapaarp *ea = aarp_hdr(skb); |
| 704 | int hash, ret = 0; |
| 705 | __u16 function; |
| 706 | struct aarp_entry *a; |
| 707 | struct atalk_addr sa, *ma, da; |
| 708 | struct atalk_iface *ifa; |
| 709 | |
| 710 | if (!net_eq(net1: dev_net(dev), net2: &init_net)) |
| 711 | goto out0; |
| 712 | |
| 713 | /* We only do Ethernet SNAP AARP. */ |
| 714 | if (dev->type != ARPHRD_ETHER) |
| 715 | goto out0; |
| 716 | |
| 717 | /* Frame size ok? */ |
| 718 | if (!skb_pull(skb, len: sizeof(*ea))) |
| 719 | goto out0; |
| 720 | |
| 721 | function = ntohs(ea->function); |
| 722 | |
| 723 | /* Sanity check fields. */ |
| 724 | if (function < AARP_REQUEST || function > AARP_PROBE || |
| 725 | ea->hw_len != ETH_ALEN || ea->pa_len != AARP_PA_ALEN || |
| 726 | ea->pa_src_zero || ea->pa_dst_zero) |
| 727 | goto out0; |
| 728 | |
| 729 | /* Looks good. */ |
| 730 | hash = ea->pa_src_node % (AARP_HASH_SIZE - 1); |
| 731 | |
| 732 | /* Build an address. */ |
| 733 | sa.s_node = ea->pa_src_node; |
| 734 | sa.s_net = ea->pa_src_net; |
| 735 | |
| 736 | /* Process the packet. Check for replies of me. */ |
| 737 | ifa = atalk_find_dev(dev); |
| 738 | if (!ifa) |
| 739 | goto out1; |
| 740 | |
| 741 | if (ifa->status & ATIF_PROBE && |
| 742 | ifa->address.s_node == ea->pa_dst_node && |
| 743 | ifa->address.s_net == ea->pa_dst_net) { |
| 744 | ifa->status |= ATIF_PROBE_FAIL; /* Fail the probe (in use) */ |
| 745 | goto out1; |
| 746 | } |
| 747 | |
| 748 | /* Check for replies of proxy AARP entries */ |
| 749 | da.s_node = ea->pa_dst_node; |
| 750 | da.s_net = ea->pa_dst_net; |
| 751 | |
| 752 | write_lock_bh(&aarp_lock); |
| 753 | a = __aarp_find_entry(list: proxies[hash], dev, sat: &da); |
| 754 | |
| 755 | if (a && a->status & ATIF_PROBE) { |
| 756 | a->status |= ATIF_PROBE_FAIL; |
| 757 | /* |
| 758 | * we do not respond to probe or request packets of |
| 759 | * this address while we are probing this address |
| 760 | */ |
| 761 | goto unlock; |
| 762 | } |
| 763 | |
| 764 | switch (function) { |
| 765 | case AARP_REPLY: |
| 766 | if (!unresolved_count) /* Speed up */ |
| 767 | break; |
| 768 | |
| 769 | /* Find the entry. */ |
| 770 | a = __aarp_find_entry(list: unresolved[hash], dev, sat: &sa); |
| 771 | if (!a || dev != a->dev) |
| 772 | break; |
| 773 | |
| 774 | /* We can fill one in - this is good. */ |
| 775 | ether_addr_copy(dst: a->hwaddr, src: ea->hw_src); |
| 776 | __aarp_resolved(list: &unresolved[hash], a, hash); |
| 777 | if (!unresolved_count) |
| 778 | mod_timer(timer: &aarp_timer, |
| 779 | expires: jiffies + sysctl_aarp_expiry_time); |
| 780 | break; |
| 781 | |
| 782 | case AARP_REQUEST: |
| 783 | case AARP_PROBE: |
| 784 | |
| 785 | /* |
| 786 | * If it is my address set ma to my address and reply. |
| 787 | * We can treat probe and request the same. Probe |
| 788 | * simply means we shouldn't cache the querying host, |
| 789 | * as in a probe they are proposing an address not |
| 790 | * using one. |
| 791 | * |
| 792 | * Support for proxy-AARP added. We check if the |
| 793 | * address is one of our proxies before we toss the |
| 794 | * packet out. |
| 795 | */ |
| 796 | |
| 797 | sa.s_node = ea->pa_dst_node; |
| 798 | sa.s_net = ea->pa_dst_net; |
| 799 | |
| 800 | /* See if we have a matching proxy. */ |
| 801 | ma = __aarp_proxy_find(dev, sa: &sa); |
| 802 | if (!ma) |
| 803 | ma = &ifa->address; |
| 804 | else { /* We need to make a copy of the entry. */ |
| 805 | da.s_node = sa.s_node; |
| 806 | da.s_net = sa.s_net; |
| 807 | ma = &da; |
| 808 | } |
| 809 | |
| 810 | if (function == AARP_PROBE) { |
| 811 | /* |
| 812 | * A probe implies someone trying to get an |
| 813 | * address. So as a precaution flush any |
| 814 | * entries we have for this address. |
| 815 | */ |
| 816 | a = __aarp_find_entry(list: resolved[sa.s_node % |
| 817 | (AARP_HASH_SIZE - 1)], |
| 818 | dev: skb->dev, sat: &sa); |
| 819 | |
| 820 | /* |
| 821 | * Make it expire next tick - that avoids us |
| 822 | * getting into a probe/flush/learn/probe/ |
| 823 | * flush/learn cycle during probing of a slow |
| 824 | * to respond host addr. |
| 825 | */ |
| 826 | if (a) { |
| 827 | a->expires_at = jiffies - 1; |
| 828 | mod_timer(timer: &aarp_timer, expires: jiffies + |
| 829 | sysctl_aarp_tick_time); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | if (sa.s_node != ma->s_node) |
| 834 | break; |
| 835 | |
| 836 | if (sa.s_net && ma->s_net && sa.s_net != ma->s_net) |
| 837 | break; |
| 838 | |
| 839 | sa.s_node = ea->pa_src_node; |
| 840 | sa.s_net = ea->pa_src_net; |
| 841 | |
| 842 | /* aarp_my_address has found the address to use for us. |
| 843 | */ |
| 844 | aarp_send_reply(dev, us: ma, them: &sa, sha: ea->hw_src); |
| 845 | break; |
| 846 | } |
| 847 | |
| 848 | unlock: |
| 849 | write_unlock_bh(&aarp_lock); |
| 850 | out1: |
| 851 | ret = 1; |
| 852 | out0: |
| 853 | kfree_skb(skb); |
| 854 | return ret; |
| 855 | } |
| 856 | |
| 857 | static struct notifier_block aarp_notifier = { |
| 858 | .notifier_call = aarp_device_event, |
| 859 | }; |
| 860 | |
| 861 | static unsigned char aarp_snap_id[] = { 0x00, 0x00, 0x00, 0x80, 0xF3 }; |
| 862 | |
| 863 | int __init aarp_proto_init(void) |
| 864 | { |
| 865 | int rc; |
| 866 | |
| 867 | aarp_dl = register_snap_client(desc: aarp_snap_id, rcvfunc: aarp_rcv); |
| 868 | if (!aarp_dl) { |
| 869 | printk(KERN_CRIT "Unable to register AARP with SNAP.\n" ); |
| 870 | return -ENOMEM; |
| 871 | } |
| 872 | timer_setup(&aarp_timer, aarp_expire_timeout, 0); |
| 873 | aarp_timer.expires = jiffies + sysctl_aarp_expiry_time; |
| 874 | add_timer(timer: &aarp_timer); |
| 875 | rc = register_netdevice_notifier(nb: &aarp_notifier); |
| 876 | if (rc) { |
| 877 | timer_delete_sync(timer: &aarp_timer); |
| 878 | unregister_snap_client(proto: aarp_dl); |
| 879 | } |
| 880 | return rc; |
| 881 | } |
| 882 | |
| 883 | /* Remove the AARP entries associated with a device. */ |
| 884 | void aarp_device_down(struct net_device *dev) |
| 885 | { |
| 886 | int ct; |
| 887 | |
| 888 | write_lock_bh(&aarp_lock); |
| 889 | |
| 890 | for (ct = 0; ct < AARP_HASH_SIZE; ct++) { |
| 891 | __aarp_expire_device(n: &resolved[ct], dev); |
| 892 | __aarp_expire_device(n: &unresolved[ct], dev); |
| 893 | __aarp_expire_device(n: &proxies[ct], dev); |
| 894 | } |
| 895 | |
| 896 | write_unlock_bh(&aarp_lock); |
| 897 | } |
| 898 | |
| 899 | #ifdef CONFIG_PROC_FS |
| 900 | /* |
| 901 | * Get the aarp entry that is in the chain described |
| 902 | * by the iterator. |
| 903 | * If pos is set then skip till that index. |
| 904 | * pos = 1 is the first entry |
| 905 | */ |
| 906 | static struct aarp_entry *iter_next(struct aarp_iter_state *iter, loff_t *pos) |
| 907 | { |
| 908 | int ct = iter->bucket; |
| 909 | struct aarp_entry **table = iter->table; |
| 910 | loff_t off = 0; |
| 911 | struct aarp_entry *entry; |
| 912 | |
| 913 | rescan: |
| 914 | while (ct < AARP_HASH_SIZE) { |
| 915 | for (entry = table[ct]; entry; entry = entry->next) { |
| 916 | if (!pos || ++off == *pos) { |
| 917 | iter->table = table; |
| 918 | iter->bucket = ct; |
| 919 | return entry; |
| 920 | } |
| 921 | } |
| 922 | ++ct; |
| 923 | } |
| 924 | |
| 925 | if (table == resolved) { |
| 926 | ct = 0; |
| 927 | table = unresolved; |
| 928 | goto rescan; |
| 929 | } |
| 930 | if (table == unresolved) { |
| 931 | ct = 0; |
| 932 | table = proxies; |
| 933 | goto rescan; |
| 934 | } |
| 935 | return NULL; |
| 936 | } |
| 937 | |
| 938 | static void *aarp_seq_start(struct seq_file *seq, loff_t *pos) |
| 939 | __acquires(aarp_lock) |
| 940 | { |
| 941 | struct aarp_iter_state *iter = seq->private; |
| 942 | |
| 943 | read_lock_bh(&aarp_lock); |
| 944 | iter->table = resolved; |
| 945 | iter->bucket = 0; |
| 946 | |
| 947 | return *pos ? iter_next(iter, pos) : SEQ_START_TOKEN; |
| 948 | } |
| 949 | |
| 950 | static void *aarp_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 951 | { |
| 952 | struct aarp_entry *entry = v; |
| 953 | struct aarp_iter_state *iter = seq->private; |
| 954 | |
| 955 | ++*pos; |
| 956 | |
| 957 | /* first line after header */ |
| 958 | if (v == SEQ_START_TOKEN) |
| 959 | entry = iter_next(iter, NULL); |
| 960 | |
| 961 | /* next entry in current bucket */ |
| 962 | else if (entry->next) |
| 963 | entry = entry->next; |
| 964 | |
| 965 | /* next bucket or table */ |
| 966 | else { |
| 967 | ++iter->bucket; |
| 968 | entry = iter_next(iter, NULL); |
| 969 | } |
| 970 | return entry; |
| 971 | } |
| 972 | |
| 973 | static void aarp_seq_stop(struct seq_file *seq, void *v) |
| 974 | __releases(aarp_lock) |
| 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, fmt: "%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) |
| 995 | seq_puts(m: seq, |
| 996 | s: "Address Interface Hardware Address" |
| 997 | " Expires LastSend Retry Status\n" ); |
| 998 | else { |
| 999 | seq_printf(m: seq, fmt: "%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(m: seq, fmt: "%pM" , entry->hwaddr); |
| 1004 | seq_printf(m: seq, fmt: " %8s" , |
| 1005 | dt2str(ticks: (long)entry->expires_at - (long)now)); |
| 1006 | if (iter->table == unresolved) |
| 1007 | seq_printf(m: seq, fmt: " %8s %6hu" , |
| 1008 | dt2str(ticks: now - entry->last_sent), |
| 1009 | entry->xmit_count); |
| 1010 | else |
| 1011 | seq_puts(m: seq, s: " " ); |
| 1012 | seq_printf(m: seq, fmt: " %s\n" , |
| 1013 | (iter->table == resolved) ? "resolved" |
| 1014 | : (iter->table == unresolved) ? "unresolved" |
| 1015 | : (iter->table == proxies) ? "proxies" |
| 1016 | : "unknown" ); |
| 1017 | } |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | const struct seq_operations aarp_seq_ops = { |
| 1022 | .start = aarp_seq_start, |
| 1023 | .next = aarp_seq_next, |
| 1024 | .stop = aarp_seq_stop, |
| 1025 | .show = aarp_seq_show, |
| 1026 | }; |
| 1027 | #endif |
| 1028 | |
| 1029 | /* General module cleanup. Called from cleanup_module() in ddp.c. */ |
| 1030 | void aarp_cleanup_module(void) |
| 1031 | { |
| 1032 | timer_delete_sync(timer: &aarp_timer); |
| 1033 | unregister_netdevice_notifier(nb: &aarp_notifier); |
| 1034 | unregister_snap_client(proto: aarp_dl); |
| 1035 | aarp_purge(); |
| 1036 | } |
| 1037 | |