1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * DDP: An implementation of the AppleTalk DDP protocol for 4 * Ethernet 'ELAP'. 5 * 6 * Alan Cox <alan@lxorguk.ukuu.org.uk> 7 * 8 * With more than a little assistance from 9 * 10 * Wesley Craig <netatalk@umich.edu> 11 * 12 * Fixes: 13 * Neil Horman : Added missing device ioctls 14 * Michael Callahan : Made routing work 15 * Wesley Craig : Fix probing to listen to a 16 * passed node id. 17 * Alan Cox : Added send/recvmsg support 18 * Alan Cox : Moved at. to protinfo in 19 * socket. 20 * Alan Cox : Added firewall hooks. 21 * Alan Cox : Supports new ARPHRD_LOOPBACK 22 * Christer Weinigel : Routing and /proc fixes. 23 * Bradford Johnson : LocalTalk. 24 * Tom Dyas : Module support. 25 * Alan Cox : Hooks for PPP (based on the 26 * LocalTalk hook). 27 * Alan Cox : Posix bits 28 * Alan Cox/Mike Freeman : Possible fix to NBP problems 29 * Bradford Johnson : IP-over-DDP (experimental) 30 * Jay Schulist : Moved IP-over-DDP to its own 31 * driver file. (ipddp.c & ipddp.h) 32 * Jay Schulist : Made work as module with 33 * AppleTalk drivers, cleaned it. 34 * Rob Newberry : Added proxy AARP and AARP 35 * procfs, moved probing to AARP 36 * module. 37 * Adrian Sun/ 38 * Michael Zuelsdorff : fix for net.0 packets. don't 39 * allow illegal ether/tokentalk 40 * port assignment. we lose a 41 * valid localtalk port as a 42 * result. 43 * Arnaldo C. de Melo : Cleanup, in preparation for 44 * shared skb support 8) 45 * Arnaldo C. de Melo : Move proc stuff to atalk_proc.c, 46 * use seq_file 47 */ 48 49 #include <linux/capability.h> 50 #include <linux/module.h> 51 #include <linux/if_arp.h> 52 #include <linux/termios.h> /* For TIOCOUTQ/INQ */ 53 #include <linux/compat.h> 54 #include <linux/slab.h> 55 #include <net/datalink.h> 56 #include <net/psnap.h> 57 #include <net/sock.h> 58 #include <net/tcp_states.h> 59 #include <net/route.h> 60 #include <net/compat.h> 61 #include <linux/atalk.h> 62 #include <linux/highmem.h> 63 64 struct datalink_proto *ddp_dl, *aarp_dl; 65 static const struct proto_ops atalk_dgram_ops; 66 67 /**************************************************************************\ 68 * * 69 * Handlers for the socket list. * 70 * * 71 \**************************************************************************/ 72 73 HLIST_HEAD(atalk_sockets); 74 DEFINE_RWLOCK(atalk_sockets_lock); 75 76 static inline void __atalk_insert_socket(struct sock *sk) 77 { 78 sk_add_node(sk, &atalk_sockets); 79 } 80 81 static inline void atalk_remove_socket(struct sock *sk) 82 { 83 write_lock_bh(&atalk_sockets_lock); 84 sk_del_node_init(sk); 85 write_unlock_bh(&atalk_sockets_lock); 86 } 87 88 static struct sock *atalk_search_socket(struct sockaddr_at *to, 89 struct atalk_iface *atif) 90 { 91 struct sock *def_socket = NULL; 92 struct sock *s; 93 94 read_lock_bh(&atalk_sockets_lock); 95 sk_for_each(s, &atalk_sockets) { 96 struct atalk_sock *at = at_sk(s); 97 98 if (to->sat_port != at->src_port) 99 continue; 100 101 if (to->sat_addr.s_net == ATADDR_ANYNET && 102 to->sat_addr.s_node == ATADDR_BCAST) { 103 if (atif->address.s_node == at->src_node && 104 atif->address.s_net == at->src_net) { 105 /* This socket's address matches the address of the interface 106 * that received the packet -- use it 107 */ 108 goto found; 109 } 110 111 /* Continue searching for a socket matching the interface address, 112 * but use this socket by default if no other one is found 113 */ 114 def_socket = s; 115 } 116 117 if (to->sat_addr.s_net == at->src_net && 118 (to->sat_addr.s_node == at->src_node || 119 to->sat_addr.s_node == ATADDR_BCAST || 120 to->sat_addr.s_node == ATADDR_ANYNODE)) 121 goto found; 122 123 /* XXXX.0 -- we got a request for this router. make sure 124 * that the node is appropriately set. */ 125 if (to->sat_addr.s_node == ATADDR_ANYNODE && 126 to->sat_addr.s_net != ATADDR_ANYNET && 127 atif->address.s_node == at->src_node) { 128 to->sat_addr.s_node = atif->address.s_node; 129 goto found; 130 } 131 } 132 s = def_socket; 133 found: 134 read_unlock_bh(&atalk_sockets_lock); 135 return s; 136 } 137 138 /** 139 * atalk_find_or_insert_socket - Try to find a socket matching ADDR 140 * @sk: socket to insert in the list if it is not there already 141 * @sat: address to search for 142 * 143 * Try to find a socket matching ADDR in the socket list, if found then return 144 * it. If not, insert SK into the socket list. 145 * 146 * This entire operation must execute atomically. 147 */ 148 static struct sock *atalk_find_or_insert_socket(struct sock *sk, 149 struct sockaddr_at *sat) 150 { 151 struct sock *s; 152 struct atalk_sock *at; 153 154 write_lock_bh(&atalk_sockets_lock); 155 sk_for_each(s, &atalk_sockets) { 156 at = at_sk(s); 157 158 if (at->src_net == sat->sat_addr.s_net && 159 at->src_node == sat->sat_addr.s_node && 160 at->src_port == sat->sat_port) 161 goto found; 162 } 163 s = NULL; 164 __atalk_insert_socket(sk); /* Wheee, it's free, assign and insert. */ 165 found: 166 write_unlock_bh(&atalk_sockets_lock); 167 return s; 168 } 169 170 static void atalk_destroy_timer(struct timer_list *t) 171 { 172 struct sock *sk = from_timer(sk, t, sk_timer); 173 174 if (sk_has_allocations(sk)) { 175 sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME; 176 add_timer(&sk->sk_timer); 177 } else 178 sock_put(sk); 179 } 180 181 static inline void atalk_destroy_socket(struct sock *sk) 182 { 183 atalk_remove_socket(sk); 184 skb_queue_purge(&sk->sk_receive_queue); 185 186 if (sk_has_allocations(sk)) { 187 timer_setup(&sk->sk_timer, atalk_destroy_timer, 0); 188 sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME; 189 add_timer(&sk->sk_timer); 190 } else 191 sock_put(sk); 192 } 193 194 /**************************************************************************\ 195 * * 196 * Routing tables for the AppleTalk socket layer. * 197 * * 198 \**************************************************************************/ 199 200 /* Anti-deadlock ordering is atalk_routes_lock --> iface_lock -DaveM */ 201 struct atalk_route *atalk_routes; 202 DEFINE_RWLOCK(atalk_routes_lock); 203 204 struct atalk_iface *atalk_interfaces; 205 DEFINE_RWLOCK(atalk_interfaces_lock); 206 207 /* For probing devices or in a routerless network */ 208 struct atalk_route atrtr_default; 209 210 /* AppleTalk interface control */ 211 /* 212 * Drop a device. Doesn't drop any of its routes - that is the caller's 213 * problem. Called when we down the interface or delete the address. 214 */ 215 static void atif_drop_device(struct net_device *dev) 216 { 217 struct atalk_iface **iface = &atalk_interfaces; 218 struct atalk_iface *tmp; 219 220 write_lock_bh(&atalk_interfaces_lock); 221 while ((tmp = *iface) != NULL) { 222 if (tmp->dev == dev) { 223 *iface = tmp->next; 224 dev_put(dev); 225 kfree(tmp); 226 dev->atalk_ptr = NULL; 227 } else 228 iface = &tmp->next; 229 } 230 write_unlock_bh(&atalk_interfaces_lock); 231 } 232 233 static struct atalk_iface *atif_add_device(struct net_device *dev, 234 struct atalk_addr *sa) 235 { 236 struct atalk_iface *iface = kzalloc(sizeof(*iface), GFP_KERNEL); 237 238 if (!iface) 239 goto out; 240 241 dev_hold(dev); 242 iface->dev = dev; 243 dev->atalk_ptr = iface; 244 iface->address = *sa; 245 iface->status = 0; 246 247 write_lock_bh(&atalk_interfaces_lock); 248 iface->next = atalk_interfaces; 249 atalk_interfaces = iface; 250 write_unlock_bh(&atalk_interfaces_lock); 251 out: 252 return iface; 253 } 254 255 /* Perform phase 2 AARP probing on our tentative address */ 256 static int atif_probe_device(struct atalk_iface *atif) 257 { 258 int netrange = ntohs(atif->nets.nr_lastnet) - 259 ntohs(atif->nets.nr_firstnet) + 1; 260 int probe_net = ntohs(atif->address.s_net); 261 int probe_node = atif->address.s_node; 262 int netct, nodect; 263 264 /* Offset the network we start probing with */ 265 if (probe_net == ATADDR_ANYNET) { 266 probe_net = ntohs(atif->nets.nr_firstnet); 267 if (netrange) 268 probe_net += jiffies % netrange; 269 } 270 if (probe_node == ATADDR_ANYNODE) 271 probe_node = jiffies & 0xFF; 272 273 /* Scan the networks */ 274 atif->status |= ATIF_PROBE; 275 for (netct = 0; netct <= netrange; netct++) { 276 /* Sweep the available nodes from a given start */ 277 atif->address.s_net = htons(probe_net); 278 for (nodect = 0; nodect < 256; nodect++) { 279 atif->address.s_node = (nodect + probe_node) & 0xFF; 280 if (atif->address.s_node > 0 && 281 atif->address.s_node < 254) { 282 /* Probe a proposed address */ 283 aarp_probe_network(atif); 284 285 if (!(atif->status & ATIF_PROBE_FAIL)) { 286 atif->status &= ~ATIF_PROBE; 287 return 0; 288 } 289 } 290 atif->status &= ~ATIF_PROBE_FAIL; 291 } 292 probe_net++; 293 if (probe_net > ntohs(atif->nets.nr_lastnet)) 294 probe_net = ntohs(atif->nets.nr_firstnet); 295 } 296 atif->status &= ~ATIF_PROBE; 297 298 return -EADDRINUSE; /* Network is full... */ 299 } 300 301 302 /* Perform AARP probing for a proxy address */ 303 static int atif_proxy_probe_device(struct atalk_iface *atif, 304 struct atalk_addr *proxy_addr) 305 { 306 int netrange = ntohs(atif->nets.nr_lastnet) - 307 ntohs(atif->nets.nr_firstnet) + 1; 308 /* we probe the interface's network */ 309 int probe_net = ntohs(atif->address.s_net); 310 int probe_node = ATADDR_ANYNODE; /* we'll take anything */ 311 int netct, nodect; 312 313 /* Offset the network we start probing with */ 314 if (probe_net == ATADDR_ANYNET) { 315 probe_net = ntohs(atif->nets.nr_firstnet); 316 if (netrange) 317 probe_net += jiffies % netrange; 318 } 319 320 if (probe_node == ATADDR_ANYNODE) 321 probe_node = jiffies & 0xFF; 322 323 /* Scan the networks */ 324 for (netct = 0; netct <= netrange; netct++) { 325 /* Sweep the available nodes from a given start */ 326 proxy_addr->s_net = htons(probe_net); 327 for (nodect = 0; nodect < 256; nodect++) { 328 proxy_addr->s_node = (nodect + probe_node) & 0xFF; 329 if (proxy_addr->s_node > 0 && 330 proxy_addr->s_node < 254) { 331 /* Tell AARP to probe a proposed address */ 332 int ret = aarp_proxy_probe_network(atif, 333 proxy_addr); 334 335 if (ret != -EADDRINUSE) 336 return ret; 337 } 338 } 339 probe_net++; 340 if (probe_net > ntohs(atif->nets.nr_lastnet)) 341 probe_net = ntohs(atif->nets.nr_firstnet); 342 } 343 344 return -EADDRINUSE; /* Network is full... */ 345 } 346 347 348 struct atalk_addr *atalk_find_dev_addr(struct net_device *dev) 349 { 350 struct atalk_iface *iface = dev->atalk_ptr; 351 return iface ? &iface->address : NULL; 352 } 353 354 static struct atalk_addr *atalk_find_primary(void) 355 { 356 struct atalk_iface *fiface = NULL; 357 struct atalk_addr *retval; 358 struct atalk_iface *iface; 359 360 /* 361 * Return a point-to-point interface only if 362 * there is no non-ptp interface available. 363 */ 364 read_lock_bh(&atalk_interfaces_lock); 365 for (iface = atalk_interfaces; iface; iface = iface->next) { 366 if (!fiface && !(iface->dev->flags & IFF_LOOPBACK)) 367 fiface = iface; 368 if (!(iface->dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) { 369 retval = &iface->address; 370 goto out; 371 } 372 } 373 374 if (fiface) 375 retval = &fiface->address; 376 else if (atalk_interfaces) 377 retval = &atalk_interfaces->address; 378 else 379 retval = NULL; 380 out: 381 read_unlock_bh(&atalk_interfaces_lock); 382 return retval; 383 } 384 385 /* 386 * Find a match for 'any network' - ie any of our interfaces with that 387 * node number will do just nicely. 388 */ 389 static struct atalk_iface *atalk_find_anynet(int node, struct net_device *dev) 390 { 391 struct atalk_iface *iface = dev->atalk_ptr; 392 393 if (!iface || iface->status & ATIF_PROBE) 394 goto out_err; 395 396 if (node != ATADDR_BCAST && 397 iface->address.s_node != node && 398 node != ATADDR_ANYNODE) 399 goto out_err; 400 out: 401 return iface; 402 out_err: 403 iface = NULL; 404 goto out; 405 } 406 407 /* Find a match for a specific network:node pair */ 408 static struct atalk_iface *atalk_find_interface(__be16 net, int node) 409 { 410 struct atalk_iface *iface; 411 412 read_lock_bh(&atalk_interfaces_lock); 413 for (iface = atalk_interfaces; iface; iface = iface->next) { 414 if ((node == ATADDR_BCAST || 415 node == ATADDR_ANYNODE || 416 iface->address.s_node == node) && 417 iface->address.s_net == net && 418 !(iface->status & ATIF_PROBE)) 419 break; 420 421 /* XXXX.0 -- net.0 returns the iface associated with net */ 422 if (node == ATADDR_ANYNODE && net != ATADDR_ANYNET && 423 ntohs(iface->nets.nr_firstnet) <= ntohs(net) && 424 ntohs(net) <= ntohs(iface->nets.nr_lastnet)) 425 break; 426 } 427 read_unlock_bh(&atalk_interfaces_lock); 428 return iface; 429 } 430 431 432 /* 433 * Find a route for an AppleTalk packet. This ought to get cached in 434 * the socket (later on...). We know about host routes and the fact 435 * that a route must be direct to broadcast. 436 */ 437 static struct atalk_route *atrtr_find(struct atalk_addr *target) 438 { 439 /* 440 * we must search through all routes unless we find a 441 * host route, because some host routes might overlap 442 * network routes 443 */ 444 struct atalk_route *net_route = NULL; 445 struct atalk_route *r; 446 447 read_lock_bh(&atalk_routes_lock); 448 for (r = atalk_routes; r; r = r->next) { 449 if (!(r->flags & RTF_UP)) 450 continue; 451 452 if (r->target.s_net == target->s_net) { 453 if (r->flags & RTF_HOST) { 454 /* 455 * if this host route is for the target, 456 * the we're done 457 */ 458 if (r->target.s_node == target->s_node) 459 goto out; 460 } else 461 /* 462 * this route will work if there isn't a 463 * direct host route, so cache it 464 */ 465 net_route = r; 466 } 467 } 468 469 /* 470 * if we found a network route but not a direct host 471 * route, then return it 472 */ 473 if (net_route) 474 r = net_route; 475 else if (atrtr_default.dev) 476 r = &atrtr_default; 477 else /* No route can be found */ 478 r = NULL; 479 out: 480 read_unlock_bh(&atalk_routes_lock); 481 return r; 482 } 483 484 485 /* 486 * Given an AppleTalk network, find the device to use. This can be 487 * a simple lookup. 488 */ 489 struct net_device *atrtr_get_dev(struct atalk_addr *sa) 490 { 491 struct atalk_route *atr = atrtr_find(sa); 492 return atr ? atr->dev : NULL; 493 } 494 495 /* Set up a default router */ 496 static void atrtr_set_default(struct net_device *dev) 497 { 498 atrtr_default.dev = dev; 499 atrtr_default.flags = RTF_UP; 500 atrtr_default.gateway.s_net = htons(0); 501 atrtr_default.gateway.s_node = 0; 502 } 503 504 /* 505 * Add a router. Basically make sure it looks valid and stuff the 506 * entry in the list. While it uses netranges we always set them to one 507 * entry to work like netatalk. 508 */ 509 static int atrtr_create(struct rtentry *r, struct net_device *devhint) 510 { 511 struct sockaddr_at *ta = (struct sockaddr_at *)&r->rt_dst; 512 struct sockaddr_at *ga = (struct sockaddr_at *)&r->rt_gateway; 513 struct atalk_route *rt; 514 struct atalk_iface *iface, *riface; 515 int retval = -EINVAL; 516 517 /* 518 * Fixme: Raise/Lower a routing change semaphore for these 519 * operations. 520 */ 521 522 /* Validate the request */ 523 if (ta->sat_family != AF_APPLETALK || 524 (!devhint && ga->sat_family != AF_APPLETALK)) 525 goto out; 526 527 /* Now walk the routing table and make our decisions */ 528 write_lock_bh(&atalk_routes_lock); 529 for (rt = atalk_routes; rt; rt = rt->next) { 530 if (r->rt_flags != rt->flags) 531 continue; 532 533 if (ta->sat_addr.s_net == rt->target.s_net) { 534 if (!(rt->flags & RTF_HOST)) 535 break; 536 if (ta->sat_addr.s_node == rt->target.s_node) 537 break; 538 } 539 } 540 541 if (!devhint) { 542 riface = NULL; 543 544 read_lock_bh(&atalk_interfaces_lock); 545 for (iface = atalk_interfaces; iface; iface = iface->next) { 546 if (!riface && 547 ntohs(ga->sat_addr.s_net) >= 548 ntohs(iface->nets.nr_firstnet) && 549 ntohs(ga->sat_addr.s_net) <= 550 ntohs(iface->nets.nr_lastnet)) 551 riface = iface; 552 553 if (ga->sat_addr.s_net == iface->address.s_net && 554 ga->sat_addr.s_node == iface->address.s_node) 555 riface = iface; 556 } 557 read_unlock_bh(&atalk_interfaces_lock); 558 559 retval = -ENETUNREACH; 560 if (!riface) 561 goto out_unlock; 562 563 devhint = riface->dev; 564 } 565 566 if (!rt) { 567 rt = kzalloc(sizeof(*rt), GFP_ATOMIC); 568 569 retval = -ENOBUFS; 570 if (!rt) 571 goto out_unlock; 572 573 rt->next = atalk_routes; 574 atalk_routes = rt; 575 } 576 577 /* Fill in the routing entry */ 578 rt->target = ta->sat_addr; 579 dev_hold(devhint); 580 rt->dev = devhint; 581 rt->flags = r->rt_flags; 582 rt->gateway = ga->sat_addr; 583 584 retval = 0; 585 out_unlock: 586 write_unlock_bh(&atalk_routes_lock); 587 out: 588 return retval; 589 } 590 591 /* Delete a route. Find it and discard it */ 592 static int atrtr_delete(struct atalk_addr *addr) 593 { 594 struct atalk_route **r = &atalk_routes; 595 int retval = 0; 596 struct atalk_route *tmp; 597 598 write_lock_bh(&atalk_routes_lock); 599 while ((tmp = *r) != NULL) { 600 if (tmp->target.s_net == addr->s_net && 601 (!(tmp->flags&RTF_GATEWAY) || 602 tmp->target.s_node == addr->s_node)) { 603 *r = tmp->next; 604 dev_put(tmp->dev); 605 kfree(tmp); 606 goto out; 607 } 608 r = &tmp->next; 609 } 610 retval = -ENOENT; 611 out: 612 write_unlock_bh(&atalk_routes_lock); 613 return retval; 614 } 615 616 /* 617 * Called when a device is downed. Just throw away any routes 618 * via it. 619 */ 620 static void atrtr_device_down(struct net_device *dev) 621 { 622 struct atalk_route **r = &atalk_routes; 623 struct atalk_route *tmp; 624 625 write_lock_bh(&atalk_routes_lock); 626 while ((tmp = *r) != NULL) { 627 if (tmp->dev == dev) { 628 *r = tmp->next; 629 dev_put(dev); 630 kfree(tmp); 631 } else 632 r = &tmp->next; 633 } 634 write_unlock_bh(&atalk_routes_lock); 635 636 if (atrtr_default.dev == dev) 637 atrtr_set_default(NULL); 638 } 639 640 /* Actually down the interface */ 641 static inline void atalk_dev_down(struct net_device *dev) 642 { 643 atrtr_device_down(dev); /* Remove all routes for the device */ 644 aarp_device_down(dev); /* Remove AARP entries for the device */ 645 atif_drop_device(dev); /* Remove the device */ 646 } 647 648 /* 649 * A device event has occurred. Watch for devices going down and 650 * delete our use of them (iface and route). 651 */ 652 static int ddp_device_event(struct notifier_block *this, unsigned long event, 653 void *ptr) 654 { 655 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 656 657 if (!net_eq(dev_net(dev), &init_net)) 658 return NOTIFY_DONE; 659 660 if (event == NETDEV_DOWN) 661 /* Discard any use of this */ 662 atalk_dev_down(dev); 663 664 return NOTIFY_DONE; 665 } 666 667 /* ioctl calls. Shouldn't even need touching */ 668 /* Device configuration ioctl calls */ 669 static int atif_ioctl(int cmd, void __user *arg) 670 { 671 static char aarp_mcast[6] = { 0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF }; 672 struct ifreq atreq; 673 struct atalk_netrange *nr; 674 struct sockaddr_at *sa; 675 struct net_device *dev; 676 struct atalk_iface *atif; 677 int ct; 678 int limit; 679 struct rtentry rtdef; 680 int add_route; 681 682 if (get_user_ifreq(&atreq, NULL, arg)) 683 return -EFAULT; 684 685 dev = __dev_get_by_name(&init_net, atreq.ifr_name); 686 if (!dev) 687 return -ENODEV; 688 689 sa = (struct sockaddr_at *)&atreq.ifr_addr; 690 atif = atalk_find_dev(dev); 691 692 switch (cmd) { 693 case SIOCSIFADDR: 694 if (!capable(CAP_NET_ADMIN)) 695 return -EPERM; 696 if (sa->sat_family != AF_APPLETALK) 697 return -EINVAL; 698 if (dev->type != ARPHRD_ETHER && 699 dev->type != ARPHRD_LOOPBACK && 700 dev->type != ARPHRD_LOCALTLK && 701 dev->type != ARPHRD_PPP) 702 return -EPROTONOSUPPORT; 703 704 nr = (struct atalk_netrange *)&sa->sat_zero[0]; 705 add_route = 1; 706 707 /* 708 * if this is a point-to-point iface, and we already 709 * have an iface for this AppleTalk address, then we 710 * should not add a route 711 */ 712 if ((dev->flags & IFF_POINTOPOINT) && 713 atalk_find_interface(sa->sat_addr.s_net, 714 sa->sat_addr.s_node)) { 715 printk(KERN_DEBUG "AppleTalk: point-to-point " 716 "interface added with " 717 "existing address\n"); 718 add_route = 0; 719 } 720 721 /* 722 * Phase 1 is fine on LocalTalk but we don't do 723 * EtherTalk phase 1. Anyone wanting to add it, go ahead. 724 */ 725 if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2) 726 return -EPROTONOSUPPORT; 727 if (sa->sat_addr.s_node == ATADDR_BCAST || 728 sa->sat_addr.s_node == 254) 729 return -EINVAL; 730 if (atif) { 731 /* Already setting address */ 732 if (atif->status & ATIF_PROBE) 733 return -EBUSY; 734 735 atif->address.s_net = sa->sat_addr.s_net; 736 atif->address.s_node = sa->sat_addr.s_node; 737 atrtr_device_down(dev); /* Flush old routes */ 738 } else { 739 atif = atif_add_device(dev, &sa->sat_addr); 740 if (!atif) 741 return -ENOMEM; 742 } 743 atif->nets = *nr; 744 745 /* 746 * Check if the chosen address is used. If so we 747 * error and atalkd will try another. 748 */ 749 750 if (!(dev->flags & IFF_LOOPBACK) && 751 !(dev->flags & IFF_POINTOPOINT) && 752 atif_probe_device(atif) < 0) { 753 atif_drop_device(dev); 754 return -EADDRINUSE; 755 } 756 757 /* Hey it worked - add the direct routes */ 758 sa = (struct sockaddr_at *)&rtdef.rt_gateway; 759 sa->sat_family = AF_APPLETALK; 760 sa->sat_addr.s_net = atif->address.s_net; 761 sa->sat_addr.s_node = atif->address.s_node; 762 sa = (struct sockaddr_at *)&rtdef.rt_dst; 763 rtdef.rt_flags = RTF_UP; 764 sa->sat_family = AF_APPLETALK; 765 sa->sat_addr.s_node = ATADDR_ANYNODE; 766 if (dev->flags & IFF_LOOPBACK || 767 dev->flags & IFF_POINTOPOINT) 768 rtdef.rt_flags |= RTF_HOST; 769 770 /* Routerless initial state */ 771 if (nr->nr_firstnet == htons(0) && 772 nr->nr_lastnet == htons(0xFFFE)) { 773 sa->sat_addr.s_net = atif->address.s_net; 774 atrtr_create(&rtdef, dev); 775 atrtr_set_default(dev); 776 } else { 777 limit = ntohs(nr->nr_lastnet); 778 if (limit - ntohs(nr->nr_firstnet) > 4096) { 779 printk(KERN_WARNING "Too many routes/" 780 "iface.\n"); 781 return -EINVAL; 782 } 783 if (add_route) 784 for (ct = ntohs(nr->nr_firstnet); 785 ct <= limit; ct++) { 786 sa->sat_addr.s_net = htons(ct); 787 atrtr_create(&rtdef, dev); 788 } 789 } 790 dev_mc_add_global(dev, aarp_mcast); 791 return 0; 792 793 case SIOCGIFADDR: 794 if (!atif) 795 return -EADDRNOTAVAIL; 796 797 sa->sat_family = AF_APPLETALK; 798 sa->sat_addr = atif->address; 799 break; 800 801 case SIOCGIFBRDADDR: 802 if (!atif) 803 return -EADDRNOTAVAIL; 804 805 sa->sat_family = AF_APPLETALK; 806 sa->sat_addr.s_net = atif->address.s_net; 807 sa->sat_addr.s_node = ATADDR_BCAST; 808 break; 809 810 case SIOCATALKDIFADDR: 811 case SIOCDIFADDR: 812 if (!capable(CAP_NET_ADMIN)) 813 return -EPERM; 814 if (sa->sat_family != AF_APPLETALK) 815 return -EINVAL; 816 atalk_dev_down(dev); 817 break; 818 819 case SIOCSARP: 820 if (!capable(CAP_NET_ADMIN)) 821 return -EPERM; 822 if (sa->sat_family != AF_APPLETALK) 823 return -EINVAL; 824 /* 825 * for now, we only support proxy AARP on ELAP; 826 * we should be able to do it for LocalTalk, too. 827 */ 828 if (dev->type != ARPHRD_ETHER) 829 return -EPROTONOSUPPORT; 830 831 /* 832 * atif points to the current interface on this network; 833 * we aren't concerned about its current status (at 834 * least for now), but it has all the settings about 835 * the network we're going to probe. Consequently, it 836 * must exist. 837 */ 838 if (!atif) 839 return -EADDRNOTAVAIL; 840 841 nr = (struct atalk_netrange *)&(atif->nets); 842 /* 843 * Phase 1 is fine on Localtalk but we don't do 844 * Ethertalk phase 1. Anyone wanting to add it, go ahead. 845 */ 846 if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2) 847 return -EPROTONOSUPPORT; 848 849 if (sa->sat_addr.s_node == ATADDR_BCAST || 850 sa->sat_addr.s_node == 254) 851 return -EINVAL; 852 853 /* 854 * Check if the chosen address is used. If so we 855 * error and ATCP will try another. 856 */ 857 if (atif_proxy_probe_device(atif, &(sa->sat_addr)) < 0) 858 return -EADDRINUSE; 859 860 /* 861 * We now have an address on the local network, and 862 * the AARP code will defend it for us until we take it 863 * down. We don't set up any routes right now, because 864 * ATCP will install them manually via SIOCADDRT. 865 */ 866 break; 867 868 case SIOCDARP: 869 if (!capable(CAP_NET_ADMIN)) 870 return -EPERM; 871 if (sa->sat_family != AF_APPLETALK) 872 return -EINVAL; 873 if (!atif) 874 return -EADDRNOTAVAIL; 875 876 /* give to aarp module to remove proxy entry */ 877 aarp_proxy_remove(atif->dev, &(sa->sat_addr)); 878 return 0; 879 } 880 881 return put_user_ifreq(&atreq, arg); 882 } 883 884 static int atrtr_ioctl_addrt(struct rtentry *rt) 885 { 886 struct net_device *dev = NULL; 887 888 if (rt->rt_dev) { 889 char name[IFNAMSIZ]; 890 891 if (copy_from_user(name, rt->rt_dev, IFNAMSIZ-1)) 892 return -EFAULT; 893 name[IFNAMSIZ-1] = '\0'; 894 895 dev = __dev_get_by_name(&init_net, name); 896 if (!dev) 897 return -ENODEV; 898 } 899 return atrtr_create(rt, dev); 900 } 901 902 /* Routing ioctl() calls */ 903 static int atrtr_ioctl(unsigned int cmd, void __user *arg) 904 { 905 struct rtentry rt; 906 907 if (copy_from_user(&rt, arg, sizeof(rt))) 908 return -EFAULT; 909 910 switch (cmd) { 911 case SIOCDELRT: 912 if (rt.rt_dst.sa_family != AF_APPLETALK) 913 return -EINVAL; 914 return atrtr_delete(&((struct sockaddr_at *) 915 &rt.rt_dst)->sat_addr); 916 917 case SIOCADDRT: 918 return atrtr_ioctl_addrt(&rt); 919 } 920 return -EINVAL; 921 } 922 923 /**************************************************************************\ 924 * * 925 * Handling for system calls applied via the various interfaces to an * 926 * AppleTalk socket object. * 927 * * 928 \**************************************************************************/ 929 930 /* 931 * Checksum: This is 'optional'. It's quite likely also a good 932 * candidate for assembler hackery 8) 933 */ 934 static unsigned long atalk_sum_partial(const unsigned char *data, 935 int len, unsigned long sum) 936 { 937 /* This ought to be unwrapped neatly. I'll trust gcc for now */ 938 while (len--) { 939 sum += *data++; 940 sum = rol16(sum, 1); 941 } 942 return sum; 943 } 944 945 /* Checksum skb data -- similar to skb_checksum */ 946 static unsigned long atalk_sum_skb(const struct sk_buff *skb, int offset, 947 int len, unsigned long sum) 948 { 949 int start = skb_headlen(skb); 950 struct sk_buff *frag_iter; 951 int i, copy; 952 953 /* checksum stuff in header space */ 954 if ((copy = start - offset) > 0) { 955 if (copy > len) 956 copy = len; 957 sum = atalk_sum_partial(skb->data + offset, copy, sum); 958 if ((len -= copy) == 0) 959 return sum; 960 961 offset += copy; 962 } 963 964 /* checksum stuff in frags */ 965 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 966 int end; 967 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 968 WARN_ON(start > offset + len); 969 970 end = start + skb_frag_size(frag); 971 if ((copy = end - offset) > 0) { 972 u8 *vaddr; 973 974 if (copy > len) 975 copy = len; 976 vaddr = kmap_atomic(skb_frag_page(frag)); 977 sum = atalk_sum_partial(vaddr + skb_frag_off(frag) + 978 offset - start, copy, sum); 979 kunmap_atomic(vaddr); 980 981 if (!(len -= copy)) 982 return sum; 983 offset += copy; 984 } 985 start = end; 986 } 987 988 skb_walk_frags(skb, frag_iter) { 989 int end; 990 991 WARN_ON(start > offset + len); 992 993 end = start + frag_iter->len; 994 if ((copy = end - offset) > 0) { 995 if (copy > len) 996 copy = len; 997 sum = atalk_sum_skb(frag_iter, offset - start, 998 copy, sum); 999 if ((len -= copy) == 0) 1000 return sum; 1001 offset += copy; 1002 } 1003 start = end; 1004 } 1005 1006 BUG_ON(len > 0); 1007 1008 return sum; 1009 } 1010 1011 static __be16 atalk_checksum(const struct sk_buff *skb, int len) 1012 { 1013 unsigned long sum; 1014 1015 /* skip header 4 bytes */ 1016 sum = atalk_sum_skb(skb, 4, len-4, 0); 1017 1018 /* Use 0xFFFF for 0. 0 itself means none */ 1019 return sum ? htons((unsigned short)sum) : htons(0xFFFF); 1020 } 1021 1022 static struct proto ddp_proto = { 1023 .name = "DDP", 1024 .owner = THIS_MODULE, 1025 .obj_size = sizeof(struct atalk_sock), 1026 }; 1027 1028 /* 1029 * Create a socket. Initialise the socket, blank the addresses 1030 * set the state. 1031 */ 1032 static int atalk_create(struct net *net, struct socket *sock, int protocol, 1033 int kern) 1034 { 1035 struct sock *sk; 1036 int rc = -ESOCKTNOSUPPORT; 1037 1038 if (!net_eq(net, &init_net)) 1039 return -EAFNOSUPPORT; 1040 1041 /* 1042 * We permit SOCK_DGRAM and RAW is an extension. It is trivial to do 1043 * and gives you the full ELAP frame. Should be handy for CAP 8) 1044 */ 1045 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM) 1046 goto out; 1047 1048 rc = -EPERM; 1049 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW)) 1050 goto out; 1051 1052 rc = -ENOMEM; 1053 sk = sk_alloc(net, PF_APPLETALK, GFP_KERNEL, &ddp_proto, kern); 1054 if (!sk) 1055 goto out; 1056 rc = 0; 1057 sock->ops = &atalk_dgram_ops; 1058 sock_init_data(sock, sk); 1059 1060 /* Checksums on by default */ 1061 sock_set_flag(sk, SOCK_ZAPPED); 1062 out: 1063 return rc; 1064 } 1065 1066 /* Free a socket. No work needed */ 1067 static int atalk_release(struct socket *sock) 1068 { 1069 struct sock *sk = sock->sk; 1070 1071 if (sk) { 1072 sock_hold(sk); 1073 lock_sock(sk); 1074 1075 sock_orphan(sk); 1076 sock->sk = NULL; 1077 atalk_destroy_socket(sk); 1078 1079 release_sock(sk); 1080 sock_put(sk); 1081 } 1082 return 0; 1083 } 1084 1085 /** 1086 * atalk_pick_and_bind_port - Pick a source port when one is not given 1087 * @sk: socket to insert into the tables 1088 * @sat: address to search for 1089 * 1090 * Pick a source port when one is not given. If we can find a suitable free 1091 * one, we insert the socket into the tables using it. 1092 * 1093 * This whole operation must be atomic. 1094 */ 1095 static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat) 1096 { 1097 int retval; 1098 1099 write_lock_bh(&atalk_sockets_lock); 1100 1101 for (sat->sat_port = ATPORT_RESERVED; 1102 sat->sat_port < ATPORT_LAST; 1103 sat->sat_port++) { 1104 struct sock *s; 1105 1106 sk_for_each(s, &atalk_sockets) { 1107 struct atalk_sock *at = at_sk(s); 1108 1109 if (at->src_net == sat->sat_addr.s_net && 1110 at->src_node == sat->sat_addr.s_node && 1111 at->src_port == sat->sat_port) 1112 goto try_next_port; 1113 } 1114 1115 /* Wheee, it's free, assign and insert. */ 1116 __atalk_insert_socket(sk); 1117 at_sk(sk)->src_port = sat->sat_port; 1118 retval = 0; 1119 goto out; 1120 1121 try_next_port:; 1122 } 1123 1124 retval = -EBUSY; 1125 out: 1126 write_unlock_bh(&atalk_sockets_lock); 1127 return retval; 1128 } 1129 1130 static int atalk_autobind(struct sock *sk) 1131 { 1132 struct atalk_sock *at = at_sk(sk); 1133 struct sockaddr_at sat; 1134 struct atalk_addr *ap = atalk_find_primary(); 1135 int n = -EADDRNOTAVAIL; 1136 1137 if (!ap || ap->s_net == htons(ATADDR_ANYNET)) 1138 goto out; 1139 1140 at->src_net = sat.sat_addr.s_net = ap->s_net; 1141 at->src_node = sat.sat_addr.s_node = ap->s_node; 1142 1143 n = atalk_pick_and_bind_port(sk, &sat); 1144 if (!n) 1145 sock_reset_flag(sk, SOCK_ZAPPED); 1146 out: 1147 return n; 1148 } 1149 1150 /* Set the address 'our end' of the connection */ 1151 static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 1152 { 1153 struct sockaddr_at *addr = (struct sockaddr_at *)uaddr; 1154 struct sock *sk = sock->sk; 1155 struct atalk_sock *at = at_sk(sk); 1156 int err; 1157 1158 if (!sock_flag(sk, SOCK_ZAPPED) || 1159 addr_len != sizeof(struct sockaddr_at)) 1160 return -EINVAL; 1161 1162 if (addr->sat_family != AF_APPLETALK) 1163 return -EAFNOSUPPORT; 1164 1165 lock_sock(sk); 1166 if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) { 1167 struct atalk_addr *ap = atalk_find_primary(); 1168 1169 err = -EADDRNOTAVAIL; 1170 if (!ap) 1171 goto out; 1172 1173 at->src_net = addr->sat_addr.s_net = ap->s_net; 1174 at->src_node = addr->sat_addr.s_node = ap->s_node; 1175 } else { 1176 err = -EADDRNOTAVAIL; 1177 if (!atalk_find_interface(addr->sat_addr.s_net, 1178 addr->sat_addr.s_node)) 1179 goto out; 1180 1181 at->src_net = addr->sat_addr.s_net; 1182 at->src_node = addr->sat_addr.s_node; 1183 } 1184 1185 if (addr->sat_port == ATADDR_ANYPORT) { 1186 err = atalk_pick_and_bind_port(sk, addr); 1187 1188 if (err < 0) 1189 goto out; 1190 } else { 1191 at->src_port = addr->sat_port; 1192 1193 err = -EADDRINUSE; 1194 if (atalk_find_or_insert_socket(sk, addr)) 1195 goto out; 1196 } 1197 1198 sock_reset_flag(sk, SOCK_ZAPPED); 1199 err = 0; 1200 out: 1201 release_sock(sk); 1202 return err; 1203 } 1204 1205 /* Set the address we talk to */ 1206 static int atalk_connect(struct socket *sock, struct sockaddr *uaddr, 1207 int addr_len, int flags) 1208 { 1209 struct sock *sk = sock->sk; 1210 struct atalk_sock *at = at_sk(sk); 1211 struct sockaddr_at *addr; 1212 int err; 1213 1214 sk->sk_state = TCP_CLOSE; 1215 sock->state = SS_UNCONNECTED; 1216 1217 if (addr_len != sizeof(*addr)) 1218 return -EINVAL; 1219 1220 addr = (struct sockaddr_at *)uaddr; 1221 1222 if (addr->sat_family != AF_APPLETALK) 1223 return -EAFNOSUPPORT; 1224 1225 if (addr->sat_addr.s_node == ATADDR_BCAST && 1226 !sock_flag(sk, SOCK_BROADCAST)) { 1227 #if 1 1228 pr_warn("atalk_connect: %s is broken and did not set SO_BROADCAST.\n", 1229 current->comm); 1230 #else 1231 return -EACCES; 1232 #endif 1233 } 1234 1235 lock_sock(sk); 1236 err = -EBUSY; 1237 if (sock_flag(sk, SOCK_ZAPPED)) 1238 if (atalk_autobind(sk) < 0) 1239 goto out; 1240 1241 err = -ENETUNREACH; 1242 if (!atrtr_get_dev(&addr->sat_addr)) 1243 goto out; 1244 1245 at->dest_port = addr->sat_port; 1246 at->dest_net = addr->sat_addr.s_net; 1247 at->dest_node = addr->sat_addr.s_node; 1248 1249 sock->state = SS_CONNECTED; 1250 sk->sk_state = TCP_ESTABLISHED; 1251 err = 0; 1252 out: 1253 release_sock(sk); 1254 return err; 1255 } 1256 1257 /* 1258 * Find the name of an AppleTalk socket. Just copy the right 1259 * fields into the sockaddr. 1260 */ 1261 static int atalk_getname(struct socket *sock, struct sockaddr *uaddr, 1262 int peer) 1263 { 1264 struct sockaddr_at sat; 1265 struct sock *sk = sock->sk; 1266 struct atalk_sock *at = at_sk(sk); 1267 int err; 1268 1269 lock_sock(sk); 1270 err = -ENOBUFS; 1271 if (sock_flag(sk, SOCK_ZAPPED)) 1272 if (atalk_autobind(sk) < 0) 1273 goto out; 1274 1275 memset(&sat, 0, sizeof(sat)); 1276 1277 if (peer) { 1278 err = -ENOTCONN; 1279 if (sk->sk_state != TCP_ESTABLISHED) 1280 goto out; 1281 1282 sat.sat_addr.s_net = at->dest_net; 1283 sat.sat_addr.s_node = at->dest_node; 1284 sat.sat_port = at->dest_port; 1285 } else { 1286 sat.sat_addr.s_net = at->src_net; 1287 sat.sat_addr.s_node = at->src_node; 1288 sat.sat_port = at->src_port; 1289 } 1290 1291 sat.sat_family = AF_APPLETALK; 1292 memcpy(uaddr, &sat, sizeof(sat)); 1293 err = sizeof(struct sockaddr_at); 1294 1295 out: 1296 release_sock(sk); 1297 return err; 1298 } 1299 1300 static int atalk_route_packet(struct sk_buff *skb, struct net_device *dev, 1301 struct ddpehdr *ddp, __u16 len_hops, int origlen) 1302 { 1303 struct atalk_route *rt; 1304 struct atalk_addr ta; 1305 1306 /* 1307 * Don't route multicast, etc., packets, or packets sent to "this 1308 * network" 1309 */ 1310 if (skb->pkt_type != PACKET_HOST || !ddp->deh_dnet) { 1311 /* 1312 * FIXME: 1313 * 1314 * Can it ever happen that a packet is from a PPP iface and 1315 * needs to be broadcast onto the default network? 1316 */ 1317 if (dev->type == ARPHRD_PPP) 1318 printk(KERN_DEBUG "AppleTalk: didn't forward broadcast " 1319 "packet received from PPP iface\n"); 1320 goto free_it; 1321 } 1322 1323 ta.s_net = ddp->deh_dnet; 1324 ta.s_node = ddp->deh_dnode; 1325 1326 /* Route the packet */ 1327 rt = atrtr_find(&ta); 1328 /* increment hops count */ 1329 len_hops += 1 << 10; 1330 if (!rt || !(len_hops & (15 << 10))) 1331 goto free_it; 1332 1333 /* FIXME: use skb->cb to be able to use shared skbs */ 1334 1335 /* 1336 * Route goes through another gateway, so set the target to the 1337 * gateway instead. 1338 */ 1339 1340 if (rt->flags & RTF_GATEWAY) { 1341 ta.s_net = rt->gateway.s_net; 1342 ta.s_node = rt->gateway.s_node; 1343 } 1344 1345 /* Fix up skb->len field */ 1346 skb_trim(skb, min_t(unsigned int, origlen, 1347 (rt->dev->hard_header_len + 1348 ddp_dl->header_length + (len_hops & 1023)))); 1349 1350 /* FIXME: use skb->cb to be able to use shared skbs */ 1351 ddp->deh_len_hops = htons(len_hops); 1352 1353 /* 1354 * Send the buffer onwards 1355 * 1356 * Now we must always be careful. If it's come from LocalTalk to 1357 * EtherTalk it might not fit 1358 * 1359 * Order matters here: If a packet has to be copied to make a new 1360 * headroom (rare hopefully) then it won't need unsharing. 1361 * 1362 * Note. ddp-> becomes invalid at the realloc. 1363 */ 1364 if (skb_headroom(skb) < 22) { 1365 /* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */ 1366 struct sk_buff *nskb = skb_realloc_headroom(skb, 32); 1367 kfree_skb(skb); 1368 skb = nskb; 1369 } else 1370 skb = skb_unshare(skb, GFP_ATOMIC); 1371 1372 /* 1373 * If the buffer didn't vanish into the lack of space bitbucket we can 1374 * send it. 1375 */ 1376 if (skb == NULL) 1377 goto drop; 1378 1379 if (aarp_send_ddp(rt->dev, skb, &ta, NULL) == NET_XMIT_DROP) 1380 return NET_RX_DROP; 1381 return NET_RX_SUCCESS; 1382 free_it: 1383 kfree_skb(skb); 1384 drop: 1385 return NET_RX_DROP; 1386 } 1387 1388 /** 1389 * atalk_rcv - Receive a packet (in skb) from device dev 1390 * @skb: packet received 1391 * @dev: network device where the packet comes from 1392 * @pt: packet type 1393 * @orig_dev: the original receive net device 1394 * 1395 * Receive a packet (in skb) from device dev. This has come from the SNAP 1396 * decoder, and on entry skb->transport_header is the DDP header, skb->len 1397 * is the DDP header, skb->len is the DDP length. The physical headers 1398 * have been extracted. PPP should probably pass frames marked as for this 1399 * layer. [ie ARPHRD_ETHERTALK] 1400 */ 1401 static int atalk_rcv(struct sk_buff *skb, struct net_device *dev, 1402 struct packet_type *pt, struct net_device *orig_dev) 1403 { 1404 struct ddpehdr *ddp; 1405 struct sock *sock; 1406 struct atalk_iface *atif; 1407 struct sockaddr_at tosat; 1408 int origlen; 1409 __u16 len_hops; 1410 1411 if (!net_eq(dev_net(dev), &init_net)) 1412 goto drop; 1413 1414 /* Don't mangle buffer if shared */ 1415 if (!(skb = skb_share_check(skb, GFP_ATOMIC))) 1416 goto out; 1417 1418 /* Size check and make sure header is contiguous */ 1419 if (!pskb_may_pull(skb, sizeof(*ddp))) 1420 goto drop; 1421 1422 ddp = ddp_hdr(skb); 1423 1424 len_hops = ntohs(ddp->deh_len_hops); 1425 1426 /* Trim buffer in case of stray trailing data */ 1427 origlen = skb->len; 1428 skb_trim(skb, min_t(unsigned int, skb->len, len_hops & 1023)); 1429 1430 /* 1431 * Size check to see if ddp->deh_len was crap 1432 * (Otherwise we'll detonate most spectacularly 1433 * in the middle of atalk_checksum() or recvmsg()). 1434 */ 1435 if (skb->len < sizeof(*ddp) || skb->len < (len_hops & 1023)) { 1436 pr_debug("AppleTalk: dropping corrupted frame (deh_len=%u, " 1437 "skb->len=%u)\n", len_hops & 1023, skb->len); 1438 goto drop; 1439 } 1440 1441 /* 1442 * Any checksums. Note we don't do htons() on this == is assumed to be 1443 * valid for net byte orders all over the networking code... 1444 */ 1445 if (ddp->deh_sum && 1446 atalk_checksum(skb, len_hops & 1023) != ddp->deh_sum) 1447 /* Not a valid AppleTalk frame - dustbin time */ 1448 goto drop; 1449 1450 /* Check the packet is aimed at us */ 1451 if (!ddp->deh_dnet) /* Net 0 is 'this network' */ 1452 atif = atalk_find_anynet(ddp->deh_dnode, dev); 1453 else 1454 atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode); 1455 1456 if (!atif) { 1457 /* Not ours, so we route the packet via the correct 1458 * AppleTalk iface 1459 */ 1460 return atalk_route_packet(skb, dev, ddp, len_hops, origlen); 1461 } 1462 1463 /* 1464 * Which socket - atalk_search_socket() looks for a *full match* 1465 * of the <net, node, port> tuple. 1466 */ 1467 tosat.sat_addr.s_net = ddp->deh_dnet; 1468 tosat.sat_addr.s_node = ddp->deh_dnode; 1469 tosat.sat_port = ddp->deh_dport; 1470 1471 sock = atalk_search_socket(&tosat, atif); 1472 if (!sock) /* But not one of our sockets */ 1473 goto drop; 1474 1475 /* Queue packet (standard) */ 1476 if (sock_queue_rcv_skb(sock, skb) < 0) 1477 goto drop; 1478 1479 return NET_RX_SUCCESS; 1480 1481 drop: 1482 kfree_skb(skb); 1483 out: 1484 return NET_RX_DROP; 1485 1486 } 1487 1488 /* 1489 * Receive a LocalTalk frame. We make some demands on the caller here. 1490 * Caller must provide enough headroom on the packet to pull the short 1491 * header and append a long one. 1492 */ 1493 static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev, 1494 struct packet_type *pt, struct net_device *orig_dev) 1495 { 1496 if (!net_eq(dev_net(dev), &init_net)) 1497 goto freeit; 1498 1499 /* Expand any short form frames */ 1500 if (skb_mac_header(skb)[2] == 1) { 1501 struct ddpehdr *ddp; 1502 /* Find our address */ 1503 struct atalk_addr *ap = atalk_find_dev_addr(dev); 1504 1505 if (!ap || skb->len < sizeof(__be16) || skb->len > 1023) 1506 goto freeit; 1507 1508 /* Don't mangle buffer if shared */ 1509 if (!(skb = skb_share_check(skb, GFP_ATOMIC))) 1510 return 0; 1511 1512 /* 1513 * The push leaves us with a ddephdr not an shdr, and 1514 * handily the port bytes in the right place preset. 1515 */ 1516 ddp = skb_push(skb, sizeof(*ddp) - 4); 1517 1518 /* Now fill in the long header */ 1519 1520 /* 1521 * These two first. The mac overlays the new source/dest 1522 * network information so we MUST copy these before 1523 * we write the network numbers ! 1524 */ 1525 1526 ddp->deh_dnode = skb_mac_header(skb)[0]; /* From physical header */ 1527 ddp->deh_snode = skb_mac_header(skb)[1]; /* From physical header */ 1528 1529 ddp->deh_dnet = ap->s_net; /* Network number */ 1530 ddp->deh_snet = ap->s_net; 1531 ddp->deh_sum = 0; /* No checksum */ 1532 /* 1533 * Not sure about this bit... 1534 */ 1535 /* Non routable, so force a drop if we slip up later */ 1536 ddp->deh_len_hops = htons(skb->len + (DDP_MAXHOPS << 10)); 1537 } 1538 skb_reset_transport_header(skb); 1539 1540 return atalk_rcv(skb, dev, pt, orig_dev); 1541 freeit: 1542 kfree_skb(skb); 1543 return 0; 1544 } 1545 1546 static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) 1547 { 1548 struct sock *sk = sock->sk; 1549 struct atalk_sock *at = at_sk(sk); 1550 DECLARE_SOCKADDR(struct sockaddr_at *, usat, msg->msg_name); 1551 int flags = msg->msg_flags; 1552 int loopback = 0; 1553 struct sockaddr_at local_satalk, gsat; 1554 struct sk_buff *skb; 1555 struct net_device *dev; 1556 struct ddpehdr *ddp; 1557 int size, hard_header_len; 1558 struct atalk_route *rt, *rt_lo = NULL; 1559 int err; 1560 1561 if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT)) 1562 return -EINVAL; 1563 1564 if (len > DDP_MAXSZ) 1565 return -EMSGSIZE; 1566 1567 lock_sock(sk); 1568 if (usat) { 1569 err = -EBUSY; 1570 if (sock_flag(sk, SOCK_ZAPPED)) 1571 if (atalk_autobind(sk) < 0) 1572 goto out; 1573 1574 err = -EINVAL; 1575 if (msg->msg_namelen < sizeof(*usat) || 1576 usat->sat_family != AF_APPLETALK) 1577 goto out; 1578 1579 err = -EPERM; 1580 /* netatalk didn't implement this check */ 1581 if (usat->sat_addr.s_node == ATADDR_BCAST && 1582 !sock_flag(sk, SOCK_BROADCAST)) { 1583 goto out; 1584 } 1585 } else { 1586 err = -ENOTCONN; 1587 if (sk->sk_state != TCP_ESTABLISHED) 1588 goto out; 1589 usat = &local_satalk; 1590 usat->sat_family = AF_APPLETALK; 1591 usat->sat_port = at->dest_port; 1592 usat->sat_addr.s_node = at->dest_node; 1593 usat->sat_addr.s_net = at->dest_net; 1594 } 1595 1596 /* Build a packet */ 1597 net_dbg_ratelimited("SK %p: Got address.\n", sk); 1598 1599 /* For headers */ 1600 size = sizeof(struct ddpehdr) + len + ddp_dl->header_length; 1601 1602 if (usat->sat_addr.s_net || usat->sat_addr.s_node == ATADDR_ANYNODE) { 1603 rt = atrtr_find(&usat->sat_addr); 1604 } else { 1605 struct atalk_addr at_hint; 1606 1607 at_hint.s_node = 0; 1608 at_hint.s_net = at->src_net; 1609 1610 rt = atrtr_find(&at_hint); 1611 } 1612 err = -ENETUNREACH; 1613 if (!rt) 1614 goto out; 1615 1616 dev = rt->dev; 1617 1618 net_dbg_ratelimited("SK %p: Size needed %d, device %s\n", 1619 sk, size, dev->name); 1620 1621 hard_header_len = dev->hard_header_len; 1622 /* Leave room for loopback hardware header if necessary */ 1623 if (usat->sat_addr.s_node == ATADDR_BCAST && 1624 (dev->flags & IFF_LOOPBACK || !(rt->flags & RTF_GATEWAY))) { 1625 struct atalk_addr at_lo; 1626 1627 at_lo.s_node = 0; 1628 at_lo.s_net = 0; 1629 1630 rt_lo = atrtr_find(&at_lo); 1631 1632 if (rt_lo && rt_lo->dev->hard_header_len > hard_header_len) 1633 hard_header_len = rt_lo->dev->hard_header_len; 1634 } 1635 1636 size += hard_header_len; 1637 release_sock(sk); 1638 skb = sock_alloc_send_skb(sk, size, (flags & MSG_DONTWAIT), &err); 1639 lock_sock(sk); 1640 if (!skb) 1641 goto out; 1642 1643 skb_reserve(skb, ddp_dl->header_length); 1644 skb_reserve(skb, hard_header_len); 1645 skb->dev = dev; 1646 1647 net_dbg_ratelimited("SK %p: Begin build.\n", sk); 1648 1649 ddp = skb_put(skb, sizeof(struct ddpehdr)); 1650 ddp->deh_len_hops = htons(len + sizeof(*ddp)); 1651 ddp->deh_dnet = usat->sat_addr.s_net; 1652 ddp->deh_snet = at->src_net; 1653 ddp->deh_dnode = usat->sat_addr.s_node; 1654 ddp->deh_snode = at->src_node; 1655 ddp->deh_dport = usat->sat_port; 1656 ddp->deh_sport = at->src_port; 1657 1658 net_dbg_ratelimited("SK %p: Copy user data (%zd bytes).\n", sk, len); 1659 1660 err = memcpy_from_msg(skb_put(skb, len), msg, len); 1661 if (err) { 1662 kfree_skb(skb); 1663 err = -EFAULT; 1664 goto out; 1665 } 1666 1667 if (sk->sk_no_check_tx) 1668 ddp->deh_sum = 0; 1669 else 1670 ddp->deh_sum = atalk_checksum(skb, len + sizeof(*ddp)); 1671 1672 /* 1673 * Loopback broadcast packets to non gateway targets (ie routes 1674 * to group we are in) 1675 */ 1676 if (ddp->deh_dnode == ATADDR_BCAST && 1677 !(rt->flags & RTF_GATEWAY) && !(dev->flags & IFF_LOOPBACK)) { 1678 struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL); 1679 1680 if (skb2) { 1681 loopback = 1; 1682 net_dbg_ratelimited("SK %p: send out(copy).\n", sk); 1683 /* 1684 * If it fails it is queued/sent above in the aarp queue 1685 */ 1686 aarp_send_ddp(dev, skb2, &usat->sat_addr, NULL); 1687 } 1688 } 1689 1690 if (dev->flags & IFF_LOOPBACK || loopback) { 1691 net_dbg_ratelimited("SK %p: Loop back.\n", sk); 1692 /* loop back */ 1693 skb_orphan(skb); 1694 if (ddp->deh_dnode == ATADDR_BCAST) { 1695 if (!rt_lo) { 1696 kfree_skb(skb); 1697 err = -ENETUNREACH; 1698 goto out; 1699 } 1700 dev = rt_lo->dev; 1701 skb->dev = dev; 1702 } 1703 ddp_dl->request(ddp_dl, skb, dev->dev_addr); 1704 } else { 1705 net_dbg_ratelimited("SK %p: send out.\n", sk); 1706 if (rt->flags & RTF_GATEWAY) { 1707 gsat.sat_addr = rt->gateway; 1708 usat = &gsat; 1709 } 1710 1711 /* 1712 * If it fails it is queued/sent above in the aarp queue 1713 */ 1714 aarp_send_ddp(dev, skb, &usat->sat_addr, NULL); 1715 } 1716 net_dbg_ratelimited("SK %p: Done write (%zd).\n", sk, len); 1717 1718 out: 1719 release_sock(sk); 1720 return err ? : len; 1721 } 1722 1723 static int atalk_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, 1724 int flags) 1725 { 1726 struct sock *sk = sock->sk; 1727 struct ddpehdr *ddp; 1728 int copied = 0; 1729 int offset = 0; 1730 int err = 0; 1731 struct sk_buff *skb; 1732 1733 skb = skb_recv_datagram(sk, flags, &err); 1734 lock_sock(sk); 1735 1736 if (!skb) 1737 goto out; 1738 1739 /* FIXME: use skb->cb to be able to use shared skbs */ 1740 ddp = ddp_hdr(skb); 1741 copied = ntohs(ddp->deh_len_hops) & 1023; 1742 1743 if (sk->sk_type != SOCK_RAW) { 1744 offset = sizeof(*ddp); 1745 copied -= offset; 1746 } 1747 1748 if (copied > size) { 1749 copied = size; 1750 msg->msg_flags |= MSG_TRUNC; 1751 } 1752 err = skb_copy_datagram_msg(skb, offset, msg, copied); 1753 1754 if (!err && msg->msg_name) { 1755 DECLARE_SOCKADDR(struct sockaddr_at *, sat, msg->msg_name); 1756 sat->sat_family = AF_APPLETALK; 1757 sat->sat_port = ddp->deh_sport; 1758 sat->sat_addr.s_node = ddp->deh_snode; 1759 sat->sat_addr.s_net = ddp->deh_snet; 1760 msg->msg_namelen = sizeof(*sat); 1761 } 1762 1763 skb_free_datagram(sk, skb); /* Free the datagram. */ 1764 1765 out: 1766 release_sock(sk); 1767 return err ? : copied; 1768 } 1769 1770 1771 /* 1772 * AppleTalk ioctl calls. 1773 */ 1774 static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 1775 { 1776 int rc = -ENOIOCTLCMD; 1777 struct sock *sk = sock->sk; 1778 void __user *argp = (void __user *)arg; 1779 1780 switch (cmd) { 1781 /* Protocol layer */ 1782 case TIOCOUTQ: { 1783 long amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk); 1784 1785 if (amount < 0) 1786 amount = 0; 1787 rc = put_user(amount, (int __user *)argp); 1788 break; 1789 } 1790 case TIOCINQ: { 1791 struct sk_buff *skb; 1792 long amount = 0; 1793 1794 spin_lock_irq(&sk->sk_receive_queue.lock); 1795 skb = skb_peek(&sk->sk_receive_queue); 1796 if (skb) 1797 amount = skb->len - sizeof(struct ddpehdr); 1798 spin_unlock_irq(&sk->sk_receive_queue.lock); 1799 rc = put_user(amount, (int __user *)argp); 1800 break; 1801 } 1802 /* Routing */ 1803 case SIOCADDRT: 1804 case SIOCDELRT: 1805 rc = -EPERM; 1806 if (capable(CAP_NET_ADMIN)) 1807 rc = atrtr_ioctl(cmd, argp); 1808 break; 1809 /* Interface */ 1810 case SIOCGIFADDR: 1811 case SIOCSIFADDR: 1812 case SIOCGIFBRDADDR: 1813 case SIOCATALKDIFADDR: 1814 case SIOCDIFADDR: 1815 case SIOCSARP: /* proxy AARP */ 1816 case SIOCDARP: /* proxy AARP */ 1817 rtnl_lock(); 1818 rc = atif_ioctl(cmd, argp); 1819 rtnl_unlock(); 1820 break; 1821 } 1822 1823 return rc; 1824 } 1825 1826 1827 #ifdef CONFIG_COMPAT 1828 static int atalk_compat_routing_ioctl(struct sock *sk, unsigned int cmd, 1829 struct compat_rtentry __user *ur) 1830 { 1831 compat_uptr_t rtdev; 1832 struct rtentry rt; 1833 1834 if (copy_from_user(&rt.rt_dst, &ur->rt_dst, 1835 3 * sizeof(struct sockaddr)) || 1836 get_user(rt.rt_flags, &ur->rt_flags) || 1837 get_user(rt.rt_metric, &ur->rt_metric) || 1838 get_user(rt.rt_mtu, &ur->rt_mtu) || 1839 get_user(rt.rt_window, &ur->rt_window) || 1840 get_user(rt.rt_irtt, &ur->rt_irtt) || 1841 get_user(rtdev, &ur->rt_dev)) 1842 return -EFAULT; 1843 1844 switch (cmd) { 1845 case SIOCDELRT: 1846 if (rt.rt_dst.sa_family != AF_APPLETALK) 1847 return -EINVAL; 1848 return atrtr_delete(&((struct sockaddr_at *) 1849 &rt.rt_dst)->sat_addr); 1850 1851 case SIOCADDRT: 1852 rt.rt_dev = compat_ptr(rtdev); 1853 return atrtr_ioctl_addrt(&rt); 1854 default: 1855 return -EINVAL; 1856 } 1857 } 1858 static int atalk_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 1859 { 1860 void __user *argp = compat_ptr(arg); 1861 struct sock *sk = sock->sk; 1862 1863 switch (cmd) { 1864 case SIOCADDRT: 1865 case SIOCDELRT: 1866 return atalk_compat_routing_ioctl(sk, cmd, argp); 1867 /* 1868 * SIOCATALKDIFADDR is a SIOCPROTOPRIVATE ioctl number, so we 1869 * cannot handle it in common code. The data we access if ifreq 1870 * here is compatible, so we can simply call the native 1871 * handler. 1872 */ 1873 case SIOCATALKDIFADDR: 1874 return atalk_ioctl(sock, cmd, (unsigned long)argp); 1875 default: 1876 return -ENOIOCTLCMD; 1877 } 1878 } 1879 #endif /* CONFIG_COMPAT */ 1880 1881 1882 static const struct net_proto_family atalk_family_ops = { 1883 .family = PF_APPLETALK, 1884 .create = atalk_create, 1885 .owner = THIS_MODULE, 1886 }; 1887 1888 static const struct proto_ops atalk_dgram_ops = { 1889 .family = PF_APPLETALK, 1890 .owner = THIS_MODULE, 1891 .release = atalk_release, 1892 .bind = atalk_bind, 1893 .connect = atalk_connect, 1894 .socketpair = sock_no_socketpair, 1895 .accept = sock_no_accept, 1896 .getname = atalk_getname, 1897 .poll = datagram_poll, 1898 .ioctl = atalk_ioctl, 1899 .gettstamp = sock_gettstamp, 1900 #ifdef CONFIG_COMPAT 1901 .compat_ioctl = atalk_compat_ioctl, 1902 #endif 1903 .listen = sock_no_listen, 1904 .shutdown = sock_no_shutdown, 1905 .sendmsg = atalk_sendmsg, 1906 .recvmsg = atalk_recvmsg, 1907 .mmap = sock_no_mmap, 1908 }; 1909 1910 static struct notifier_block ddp_notifier = { 1911 .notifier_call = ddp_device_event, 1912 }; 1913 1914 static struct packet_type ltalk_packet_type __read_mostly = { 1915 .type = cpu_to_be16(ETH_P_LOCALTALK), 1916 .func = ltalk_rcv, 1917 }; 1918 1919 static struct packet_type ppptalk_packet_type __read_mostly = { 1920 .type = cpu_to_be16(ETH_P_PPPTALK), 1921 .func = atalk_rcv, 1922 }; 1923 1924 static unsigned char ddp_snap_id[] = { 0x08, 0x00, 0x07, 0x80, 0x9B }; 1925 1926 /* Export symbols for use by drivers when AppleTalk is a module */ 1927 EXPORT_SYMBOL(atrtr_get_dev); 1928 EXPORT_SYMBOL(atalk_find_dev_addr); 1929 1930 /* Called by proto.c on kernel start up */ 1931 static int __init atalk_init(void) 1932 { 1933 int rc; 1934 1935 rc = proto_register(&ddp_proto, 0); 1936 if (rc) 1937 goto out; 1938 1939 rc = sock_register(&atalk_family_ops); 1940 if (rc) 1941 goto out_proto; 1942 1943 ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv); 1944 if (!ddp_dl) { 1945 pr_crit("Unable to register DDP with SNAP.\n"); 1946 rc = -ENOMEM; 1947 goto out_sock; 1948 } 1949 1950 dev_add_pack(<alk_packet_type); 1951 dev_add_pack(&ppptalk_packet_type); 1952 1953 rc = register_netdevice_notifier(&ddp_notifier); 1954 if (rc) 1955 goto out_snap; 1956 1957 rc = aarp_proto_init(); 1958 if (rc) 1959 goto out_dev; 1960 1961 rc = atalk_proc_init(); 1962 if (rc) 1963 goto out_aarp; 1964 1965 rc = atalk_register_sysctl(); 1966 if (rc) 1967 goto out_proc; 1968 out: 1969 return rc; 1970 out_proc: 1971 atalk_proc_exit(); 1972 out_aarp: 1973 aarp_cleanup_module(); 1974 out_dev: 1975 unregister_netdevice_notifier(&ddp_notifier); 1976 out_snap: 1977 dev_remove_pack(&ppptalk_packet_type); 1978 dev_remove_pack(<alk_packet_type); 1979 unregister_snap_client(ddp_dl); 1980 out_sock: 1981 sock_unregister(PF_APPLETALK); 1982 out_proto: 1983 proto_unregister(&ddp_proto); 1984 goto out; 1985 } 1986 module_init(atalk_init); 1987 1988 /* 1989 * No explicit module reference count manipulation is needed in the 1990 * protocol. Socket layer sets module reference count for us 1991 * and interfaces reference counting is done 1992 * by the network device layer. 1993 * 1994 * Ergo, before the AppleTalk module can be removed, all AppleTalk 1995 * sockets should be closed from user space. 1996 */ 1997 static void __exit atalk_exit(void) 1998 { 1999 #ifdef CONFIG_SYSCTL 2000 atalk_unregister_sysctl(); 2001 #endif /* CONFIG_SYSCTL */ 2002 atalk_proc_exit(); 2003 aarp_cleanup_module(); /* General aarp clean-up. */ 2004 unregister_netdevice_notifier(&ddp_notifier); 2005 dev_remove_pack(<alk_packet_type); 2006 dev_remove_pack(&ppptalk_packet_type); 2007 unregister_snap_client(ddp_dl); 2008 sock_unregister(PF_APPLETALK); 2009 proto_unregister(&ddp_proto); 2010 } 2011 module_exit(atalk_exit); 2012 2013 MODULE_LICENSE("GPL"); 2014 MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>"); 2015 MODULE_DESCRIPTION("AppleTalk 0.20\n"); 2016 MODULE_ALIAS_NETPROTO(PF_APPLETALK); 2017