File Index
Files and directories
Sign in
FILE

ddp.c

File preview with syntax highlighting.

linux-6.19.14/net/appletalk/ddp.c C
   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
  64struct datalink_proto *ddp_dl, *aarp_dl;
  65static const struct proto_ops atalk_dgram_ops;
  66
  67/**************************************************************************\
  68*                                                                          *
  69* Handlers for the socket list.                                            *
  70*                                                                          *
  71\**************************************************************************/
  72
  73HLIST_HEAD(atalk_sockets);
  74DEFINE_RWLOCK(atalk_sockets_lock);
  75
  76static inline void __atalk_insert_socket(struct sock *sk)
  77{
  78	sk_add_node(sk, &atalk_sockets);
  79}
  80
  81static 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
  88static 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;
 133found:
 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 */
 148static 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. */
 165found:
 166	write_unlock_bh(&atalk_sockets_lock);
 167	return s;
 168}
 169
 170static void atalk_destroy_timer(struct timer_list *t)
 171{
 172	struct sock *sk = timer_container_of(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
 181static 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 */
 201struct atalk_route *atalk_routes;
 202DEFINE_RWLOCK(atalk_routes_lock);
 203
 204struct atalk_iface *atalk_interfaces;
 205DEFINE_RWLOCK(atalk_interfaces_lock);
 206
 207/* For probing devices or in a routerless network */
 208struct 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 */
 215static 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
 233static 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);
 251out:
 252	return iface;
 253}
 254
 255/* Perform phase 2 AARP probing on our tentative address */
 256static 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 */
 303static 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
 348struct 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
 354static 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;
 380out:
 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 */
 389static 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;
 400out:
 401	return iface;
 402out_err:
 403	iface = NULL;
 404	goto out;
 405}
 406
 407/* Find a match for a specific network:node pair */
 408static 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 */
 437static 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;
 479out:
 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 */
 489struct 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 */
 496static 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 */
 509static 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_put(rt->dev); /* Release old device */
 580	dev_hold(devhint);
 581	rt->dev     = devhint;
 582	rt->flags   = r->rt_flags;
 583	rt->gateway = ga->sat_addr;
 584
 585	retval = 0;
 586out_unlock:
 587	write_unlock_bh(&atalk_routes_lock);
 588out:
 589	return retval;
 590}
 591
 592/* Delete a route. Find it and discard it */
 593static int atrtr_delete(struct atalk_addr *addr)
 594{
 595	struct atalk_route **r = &atalk_routes;
 596	int retval = 0;
 597	struct atalk_route *tmp;
 598
 599	write_lock_bh(&atalk_routes_lock);
 600	while ((tmp = *r) != NULL) {
 601		if (tmp->target.s_net == addr->s_net &&
 602		    (!(tmp->flags&RTF_GATEWAY) ||
 603		     tmp->target.s_node == addr->s_node)) {
 604			*r = tmp->next;
 605			dev_put(tmp->dev);
 606			kfree(tmp);
 607			goto out;
 608		}
 609		r = &tmp->next;
 610	}
 611	retval = -ENOENT;
 612out:
 613	write_unlock_bh(&atalk_routes_lock);
 614	return retval;
 615}
 616
 617/*
 618 * Called when a device is downed. Just throw away any routes
 619 * via it.
 620 */
 621static void atrtr_device_down(struct net_device *dev)
 622{
 623	struct atalk_route **r = &atalk_routes;
 624	struct atalk_route *tmp;
 625
 626	write_lock_bh(&atalk_routes_lock);
 627	while ((tmp = *r) != NULL) {
 628		if (tmp->dev == dev) {
 629			*r = tmp->next;
 630			dev_put(dev);
 631			kfree(tmp);
 632		} else
 633			r = &tmp->next;
 634	}
 635	write_unlock_bh(&atalk_routes_lock);
 636
 637	if (atrtr_default.dev == dev)
 638		atrtr_set_default(NULL);
 639}
 640
 641/* Actually down the interface */
 642static inline void atalk_dev_down(struct net_device *dev)
 643{
 644	atrtr_device_down(dev);	/* Remove all routes for the device */
 645	aarp_device_down(dev);	/* Remove AARP entries for the device */
 646	atif_drop_device(dev);	/* Remove the device */
 647}
 648
 649/*
 650 * A device event has occurred. Watch for devices going down and
 651 * delete our use of them (iface and route).
 652 */
 653static int ddp_device_event(struct notifier_block *this, unsigned long event,
 654			    void *ptr)
 655{
 656	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
 657
 658	if (!net_eq(dev_net(dev), &init_net))
 659		return NOTIFY_DONE;
 660
 661	if (event == NETDEV_DOWN)
 662		/* Discard any use of this */
 663		atalk_dev_down(dev);
 664
 665	return NOTIFY_DONE;
 666}
 667
 668/* ioctl calls. Shouldn't even need touching */
 669/* Device configuration ioctl calls */
 670static int atif_ioctl(int cmd, void __user *arg)
 671{
 672	static char aarp_mcast[6] = { 0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF };
 673	struct ifreq atreq;
 674	struct atalk_netrange *nr;
 675	struct sockaddr_at *sa;
 676	struct net_device *dev;
 677	struct atalk_iface *atif;
 678	int ct;
 679	int limit;
 680	struct rtentry rtdef;
 681	int add_route;
 682
 683	if (get_user_ifreq(&atreq, NULL, arg))
 684		return -EFAULT;
 685
 686	dev = __dev_get_by_name(&init_net, atreq.ifr_name);
 687	if (!dev)
 688		return -ENODEV;
 689
 690	sa = (struct sockaddr_at *)&atreq.ifr_addr;
 691	atif = atalk_find_dev(dev);
 692
 693	switch (cmd) {
 694	case SIOCSIFADDR:
 695		if (!capable(CAP_NET_ADMIN))
 696			return -EPERM;
 697		if (sa->sat_family != AF_APPLETALK)
 698			return -EINVAL;
 699		if (dev->type != ARPHRD_ETHER &&
 700		    dev->type != ARPHRD_LOOPBACK &&
 701		    dev->type != ARPHRD_LOCALTLK &&
 702		    dev->type != ARPHRD_PPP)
 703			return -EPROTONOSUPPORT;
 704
 705		nr = (struct atalk_netrange *)&sa->sat_zero[0];
 706		add_route = 1;
 707
 708		/*
 709		 * if this is a point-to-point iface, and we already
 710		 * have an iface for this AppleTalk address, then we
 711		 * should not add a route
 712		 */
 713		if ((dev->flags & IFF_POINTOPOINT) &&
 714		    atalk_find_interface(sa->sat_addr.s_net,
 715					 sa->sat_addr.s_node)) {
 716			printk(KERN_DEBUG "AppleTalk: point-to-point "
 717			       "interface added with "
 718			       "existing address\n");
 719			add_route = 0;
 720		}
 721
 722		/*
 723		 * Phase 1 is fine on LocalTalk but we don't do
 724		 * EtherTalk phase 1. Anyone wanting to add it, go ahead.
 725		 */
 726		if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
 727			return -EPROTONOSUPPORT;
 728		if (sa->sat_addr.s_node == ATADDR_BCAST ||
 729		    sa->sat_addr.s_node == 254)
 730			return -EINVAL;
 731		if (atif) {
 732			/* Already setting address */
 733			if (atif->status & ATIF_PROBE)
 734				return -EBUSY;
 735
 736			atif->address.s_net  = sa->sat_addr.s_net;
 737			atif->address.s_node = sa->sat_addr.s_node;
 738			atrtr_device_down(dev);	/* Flush old routes */
 739		} else {
 740			atif = atif_add_device(dev, &sa->sat_addr);
 741			if (!atif)
 742				return -ENOMEM;
 743		}
 744		atif->nets = *nr;
 745
 746		/*
 747		 * Check if the chosen address is used. If so we
 748		 * error and atalkd will try another.
 749		 */
 750
 751		if (!(dev->flags & IFF_LOOPBACK) &&
 752		    !(dev->flags & IFF_POINTOPOINT) &&
 753		    atif_probe_device(atif) < 0) {
 754			atif_drop_device(dev);
 755			return -EADDRINUSE;
 756		}
 757
 758		/* Hey it worked - add the direct routes */
 759		sa = (struct sockaddr_at *)&rtdef.rt_gateway;
 760		sa->sat_family = AF_APPLETALK;
 761		sa->sat_addr.s_net  = atif->address.s_net;
 762		sa->sat_addr.s_node = atif->address.s_node;
 763		sa = (struct sockaddr_at *)&rtdef.rt_dst;
 764		rtdef.rt_flags = RTF_UP;
 765		sa->sat_family = AF_APPLETALK;
 766		sa->sat_addr.s_node = ATADDR_ANYNODE;
 767		if (dev->flags & IFF_LOOPBACK ||
 768		    dev->flags & IFF_POINTOPOINT)
 769			rtdef.rt_flags |= RTF_HOST;
 770
 771		/* Routerless initial state */
 772		if (nr->nr_firstnet == htons(0) &&
 773		    nr->nr_lastnet == htons(0xFFFE)) {
 774			sa->sat_addr.s_net = atif->address.s_net;
 775			atrtr_create(&rtdef, dev);
 776			atrtr_set_default(dev);
 777		} else {
 778			limit = ntohs(nr->nr_lastnet);
 779			if (limit - ntohs(nr->nr_firstnet) > 4096) {
 780				printk(KERN_WARNING "Too many routes/"
 781				       "iface.\n");
 782				return -EINVAL;
 783			}
 784			if (add_route)
 785				for (ct = ntohs(nr->nr_firstnet);
 786				     ct <= limit; ct++) {
 787					sa->sat_addr.s_net = htons(ct);
 788					atrtr_create(&rtdef, dev);
 789				}
 790		}
 791		dev_mc_add_global(dev, aarp_mcast);
 792		return 0;
 793
 794	case SIOCGIFADDR:
 795		if (!atif)
 796			return -EADDRNOTAVAIL;
 797
 798		sa->sat_family = AF_APPLETALK;
 799		sa->sat_addr = atif->address;
 800		break;
 801
 802	case SIOCGIFBRDADDR:
 803		if (!atif)
 804			return -EADDRNOTAVAIL;
 805
 806		sa->sat_family = AF_APPLETALK;
 807		sa->sat_addr.s_net = atif->address.s_net;
 808		sa->sat_addr.s_node = ATADDR_BCAST;
 809		break;
 810
 811	case SIOCATALKDIFADDR:
 812	case SIOCDIFADDR:
 813		if (!capable(CAP_NET_ADMIN))
 814			return -EPERM;
 815		if (sa->sat_family != AF_APPLETALK)
 816			return -EINVAL;
 817		atalk_dev_down(dev);
 818		break;
 819
 820	case SIOCSARP:
 821		if (!capable(CAP_NET_ADMIN))
 822			return -EPERM;
 823		if (sa->sat_family != AF_APPLETALK)
 824			return -EINVAL;
 825		/*
 826		 * for now, we only support proxy AARP on ELAP;
 827		 * we should be able to do it for LocalTalk, too.
 828		 */
 829		if (dev->type != ARPHRD_ETHER)
 830			return -EPROTONOSUPPORT;
 831
 832		/*
 833		 * atif points to the current interface on this network;
 834		 * we aren't concerned about its current status (at
 835		 * least for now), but it has all the settings about
 836		 * the network we're going to probe. Consequently, it
 837		 * must exist.
 838		 */
 839		if (!atif)
 840			return -EADDRNOTAVAIL;
 841
 842		nr = (struct atalk_netrange *)&(atif->nets);
 843		/*
 844		 * Phase 1 is fine on Localtalk but we don't do
 845		 * Ethertalk phase 1. Anyone wanting to add it, go ahead.
 846		 */
 847		if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
 848			return -EPROTONOSUPPORT;
 849
 850		if (sa->sat_addr.s_node == ATADDR_BCAST ||
 851		    sa->sat_addr.s_node == 254)
 852			return -EINVAL;
 853
 854		/*
 855		 * Check if the chosen address is used. If so we
 856		 * error and ATCP will try another.
 857		 */
 858		if (atif_proxy_probe_device(atif, &(sa->sat_addr)) < 0)
 859			return -EADDRINUSE;
 860
 861		/*
 862		 * We now have an address on the local network, and
 863		 * the AARP code will defend it for us until we take it
 864		 * down. We don't set up any routes right now, because
 865		 * ATCP will install them manually via SIOCADDRT.
 866		 */
 867		break;
 868
 869	case SIOCDARP:
 870		if (!capable(CAP_NET_ADMIN))
 871			return -EPERM;
 872		if (sa->sat_family != AF_APPLETALK)
 873			return -EINVAL;
 874		if (!atif)
 875			return -EADDRNOTAVAIL;
 876
 877		/* give to aarp module to remove proxy entry */
 878		aarp_proxy_remove(atif->dev, &(sa->sat_addr));
 879		return 0;
 880	}
 881
 882	return put_user_ifreq(&atreq, arg);
 883}
 884
 885static int atrtr_ioctl_addrt(struct rtentry *rt)
 886{
 887	struct net_device *dev = NULL;
 888
 889	if (rt->rt_dev) {
 890		char name[IFNAMSIZ];
 891
 892		if (copy_from_user(name, rt->rt_dev, IFNAMSIZ-1))
 893			return -EFAULT;
 894		name[IFNAMSIZ-1] = '\0';
 895
 896		dev = __dev_get_by_name(&init_net, name);
 897		if (!dev)
 898			return -ENODEV;
 899	}
 900	return atrtr_create(rt, dev);
 901}
 902
 903/* Routing ioctl() calls */
 904static int atrtr_ioctl(unsigned int cmd, void __user *arg)
 905{
 906	struct rtentry rt;
 907
 908	if (copy_from_user(&rt, arg, sizeof(rt)))
 909		return -EFAULT;
 910
 911	switch (cmd) {
 912	case SIOCDELRT:
 913		if (rt.rt_dst.sa_family != AF_APPLETALK)
 914			return -EINVAL;
 915		return atrtr_delete(&((struct sockaddr_at *)
 916				      &rt.rt_dst)->sat_addr);
 917
 918	case SIOCADDRT:
 919		return atrtr_ioctl_addrt(&rt);
 920	}
 921	return -EINVAL;
 922}
 923
 924/**************************************************************************\
 925*                                                                          *
 926* Handling for system calls applied via the various interfaces to an       *
 927* AppleTalk socket object.                                                 *
 928*                                                                          *
 929\**************************************************************************/
 930
 931/*
 932 * Checksum: This is 'optional'. It's quite likely also a good
 933 * candidate for assembler hackery 8)
 934 */
 935static unsigned long atalk_sum_partial(const unsigned char *data,
 936				       int len, unsigned long sum)
 937{
 938	/* This ought to be unwrapped neatly. I'll trust gcc for now */
 939	while (len--) {
 940		sum += *data++;
 941		sum = rol16(sum, 1);
 942	}
 943	return sum;
 944}
 945
 946/*  Checksum skb data --  similar to skb_checksum  */
 947static unsigned long atalk_sum_skb(const struct sk_buff *skb, int offset,
 948				   int len, unsigned long sum)
 949{
 950	int start = skb_headlen(skb);
 951	struct sk_buff *frag_iter;
 952	int i, copy;
 953
 954	/* checksum stuff in header space */
 955	if ((copy = start - offset) > 0) {
 956		if (copy > len)
 957			copy = len;
 958		sum = atalk_sum_partial(skb->data + offset, copy, sum);
 959		if ((len -= copy) == 0)
 960			return sum;
 961
 962		offset += copy;
 963	}
 964
 965	/* checksum stuff in frags */
 966	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
 967		int end;
 968		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
 969		WARN_ON(start > offset + len);
 970
 971		end = start + skb_frag_size(frag);
 972		if ((copy = end - offset) > 0) {
 973			u8 *vaddr;
 974
 975			if (copy > len)
 976				copy = len;
 977			vaddr = kmap_atomic(skb_frag_page(frag));
 978			sum = atalk_sum_partial(vaddr + skb_frag_off(frag) +
 979						offset - start, copy, sum);
 980			kunmap_atomic(vaddr);
 981
 982			if (!(len -= copy))
 983				return sum;
 984			offset += copy;
 985		}
 986		start = end;
 987	}
 988
 989	skb_walk_frags(skb, frag_iter) {
 990		int end;
 991
 992		WARN_ON(start > offset + len);
 993
 994		end = start + frag_iter->len;
 995		if ((copy = end - offset) > 0) {
 996			if (copy > len)
 997				copy = len;
 998			sum = atalk_sum_skb(frag_iter, offset - start,
 999					    copy, sum);
1000			if ((len -= copy) == 0)
1001				return sum;
1002			offset += copy;
1003		}
1004		start = end;
1005	}
1006
1007	BUG_ON(len > 0);
1008
1009	return sum;
1010}
1011
1012static __be16 atalk_checksum(const struct sk_buff *skb, int len)
1013{
1014	unsigned long sum;
1015
1016	/* skip header 4 bytes */
1017	sum = atalk_sum_skb(skb, 4, len-4, 0);
1018
1019	/* Use 0xFFFF for 0. 0 itself means none */
1020	return sum ? htons((unsigned short)sum) : htons(0xFFFF);
1021}
1022
1023static struct proto ddp_proto = {
1024	.name	  = "DDP",
1025	.owner	  = THIS_MODULE,
1026	.obj_size = sizeof(struct atalk_sock),
1027};
1028
1029/*
1030 * Create a socket. Initialise the socket, blank the addresses
1031 * set the state.
1032 */
1033static int atalk_create(struct net *net, struct socket *sock, int protocol,
1034			int kern)
1035{
1036	struct sock *sk;
1037	int rc = -ESOCKTNOSUPPORT;
1038
1039	if (!net_eq(net, &init_net))
1040		return -EAFNOSUPPORT;
1041
1042	/*
1043	 * We permit SOCK_DGRAM and RAW is an extension. It is trivial to do
1044	 * and gives you the full ELAP frame. Should be handy for CAP 8)
1045	 */
1046	if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
1047		goto out;
1048
1049	rc = -EPERM;
1050	if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
1051		goto out;
1052
1053	rc = -ENOMEM;
1054	sk = sk_alloc(net, PF_APPLETALK, GFP_KERNEL, &ddp_proto, kern);
1055	if (!sk)
1056		goto out;
1057	rc = 0;
1058	sock->ops = &atalk_dgram_ops;
1059	sock_init_data(sock, sk);
1060
1061	/* Checksums on by default */
1062	sock_set_flag(sk, SOCK_ZAPPED);
1063out:
1064	return rc;
1065}
1066
1067/* Free a socket. No work needed */
1068static int atalk_release(struct socket *sock)
1069{
1070	struct sock *sk = sock->sk;
1071
1072	if (sk) {
1073		sock_hold(sk);
1074		lock_sock(sk);
1075
1076		sock_orphan(sk);
1077		sock->sk = NULL;
1078		atalk_destroy_socket(sk);
1079
1080		release_sock(sk);
1081		sock_put(sk);
1082	}
1083	return 0;
1084}
1085
1086/**
1087 * atalk_pick_and_bind_port - Pick a source port when one is not given
1088 * @sk: socket to insert into the tables
1089 * @sat: address to search for
1090 *
1091 * Pick a source port when one is not given. If we can find a suitable free
1092 * one, we insert the socket into the tables using it.
1093 *
1094 * This whole operation must be atomic.
1095 */
1096static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat)
1097{
1098	int retval;
1099
1100	write_lock_bh(&atalk_sockets_lock);
1101
1102	for (sat->sat_port = ATPORT_RESERVED;
1103	     sat->sat_port < ATPORT_LAST;
1104	     sat->sat_port++) {
1105		struct sock *s;
1106
1107		sk_for_each(s, &atalk_sockets) {
1108			struct atalk_sock *at = at_sk(s);
1109
1110			if (at->src_net == sat->sat_addr.s_net &&
1111			    at->src_node == sat->sat_addr.s_node &&
1112			    at->src_port == sat->sat_port)
1113				goto try_next_port;
1114		}
1115
1116		/* Wheee, it's free, assign and insert. */
1117		__atalk_insert_socket(sk);
1118		at_sk(sk)->src_port = sat->sat_port;
1119		retval = 0;
1120		goto out;
1121
1122try_next_port:;
1123	}
1124
1125	retval = -EBUSY;
1126out:
1127	write_unlock_bh(&atalk_sockets_lock);
1128	return retval;
1129}
1130
1131static int atalk_autobind(struct sock *sk)
1132{
1133	struct atalk_sock *at = at_sk(sk);
1134	struct sockaddr_at sat;
1135	struct atalk_addr *ap = atalk_find_primary();
1136	int n = -EADDRNOTAVAIL;
1137
1138	if (!ap || ap->s_net == htons(ATADDR_ANYNET))
1139		goto out;
1140
1141	at->src_net  = sat.sat_addr.s_net  = ap->s_net;
1142	at->src_node = sat.sat_addr.s_node = ap->s_node;
1143
1144	n = atalk_pick_and_bind_port(sk, &sat);
1145	if (!n)
1146		sock_reset_flag(sk, SOCK_ZAPPED);
1147out:
1148	return n;
1149}
1150
1151/* Set the address 'our end' of the connection */
1152static int atalk_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int addr_len)
1153{
1154	struct sockaddr_at *addr = (struct sockaddr_at *)uaddr;
1155	struct sock *sk = sock->sk;
1156	struct atalk_sock *at = at_sk(sk);
1157	int err;
1158
1159	if (!sock_flag(sk, SOCK_ZAPPED) ||
1160	    addr_len != sizeof(struct sockaddr_at))
1161		return -EINVAL;
1162
1163	if (addr->sat_family != AF_APPLETALK)
1164		return -EAFNOSUPPORT;
1165
1166	lock_sock(sk);
1167	if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) {
1168		struct atalk_addr *ap = atalk_find_primary();
1169
1170		err = -EADDRNOTAVAIL;
1171		if (!ap)
1172			goto out;
1173
1174		at->src_net  = addr->sat_addr.s_net = ap->s_net;
1175		at->src_node = addr->sat_addr.s_node = ap->s_node;
1176	} else {
1177		err = -EADDRNOTAVAIL;
1178		if (!atalk_find_interface(addr->sat_addr.s_net,
1179					  addr->sat_addr.s_node))
1180			goto out;
1181
1182		at->src_net  = addr->sat_addr.s_net;
1183		at->src_node = addr->sat_addr.s_node;
1184	}
1185
1186	if (addr->sat_port == ATADDR_ANYPORT) {
1187		err = atalk_pick_and_bind_port(sk, addr);
1188
1189		if (err < 0)
1190			goto out;
1191	} else {
1192		at->src_port = addr->sat_port;
1193
1194		err = -EADDRINUSE;
1195		if (atalk_find_or_insert_socket(sk, addr))
1196			goto out;
1197	}
1198
1199	sock_reset_flag(sk, SOCK_ZAPPED);
1200	err = 0;
1201out:
1202	release_sock(sk);
1203	return err;
1204}
1205
1206/* Set the address we talk to */
1207static int atalk_connect(struct socket *sock, struct sockaddr_unsized *uaddr,
1208			 int addr_len, int flags)
1209{
1210	struct sock *sk = sock->sk;
1211	struct atalk_sock *at = at_sk(sk);
1212	struct sockaddr_at *addr;
1213	int err;
1214
1215	sk->sk_state   = TCP_CLOSE;
1216	sock->state = SS_UNCONNECTED;
1217
1218	if (addr_len != sizeof(*addr))
1219		return -EINVAL;
1220
1221	addr = (struct sockaddr_at *)uaddr;
1222
1223	if (addr->sat_family != AF_APPLETALK)
1224		return -EAFNOSUPPORT;
1225
1226	if (addr->sat_addr.s_node == ATADDR_BCAST &&
1227	    !sock_flag(sk, SOCK_BROADCAST)) {
1228#if 1
1229		pr_warn("atalk_connect: %s is broken and did not set SO_BROADCAST.\n",
1230			current->comm);
1231#else
1232		return -EACCES;
1233#endif
1234	}
1235
1236	lock_sock(sk);
1237	err = -EBUSY;
1238	if (sock_flag(sk, SOCK_ZAPPED))
1239		if (atalk_autobind(sk) < 0)
1240			goto out;
1241
1242	err = -ENETUNREACH;
1243	if (!atrtr_get_dev(&addr->sat_addr))
1244		goto out;
1245
1246	at->dest_port = addr->sat_port;
1247	at->dest_net  = addr->sat_addr.s_net;
1248	at->dest_node = addr->sat_addr.s_node;
1249
1250	sock->state  = SS_CONNECTED;
1251	sk->sk_state = TCP_ESTABLISHED;
1252	err = 0;
1253out:
1254	release_sock(sk);
1255	return err;
1256}
1257
1258/*
1259 * Find the name of an AppleTalk socket. Just copy the right
1260 * fields into the sockaddr.
1261 */
1262static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
1263			 int peer)
1264{
1265	struct sockaddr_at sat;
1266	struct sock *sk = sock->sk;
1267	struct atalk_sock *at = at_sk(sk);
1268	int err;
1269
1270	lock_sock(sk);
1271	err = -ENOBUFS;
1272	if (sock_flag(sk, SOCK_ZAPPED))
1273		if (atalk_autobind(sk) < 0)
1274			goto out;
1275
1276	memset(&sat, 0, sizeof(sat));
1277
1278	if (peer) {
1279		err = -ENOTCONN;
1280		if (sk->sk_state != TCP_ESTABLISHED)
1281			goto out;
1282
1283		sat.sat_addr.s_net  = at->dest_net;
1284		sat.sat_addr.s_node = at->dest_node;
1285		sat.sat_port	    = at->dest_port;
1286	} else {
1287		sat.sat_addr.s_net  = at->src_net;
1288		sat.sat_addr.s_node = at->src_node;
1289		sat.sat_port	    = at->src_port;
1290	}
1291
1292	sat.sat_family = AF_APPLETALK;
1293	memcpy(uaddr, &sat, sizeof(sat));
1294	err = sizeof(struct sockaddr_at);
1295
1296out:
1297	release_sock(sk);
1298	return err;
1299}
1300
1301static int atalk_route_packet(struct sk_buff *skb, struct net_device *dev,
1302			      struct ddpehdr *ddp, __u16 len_hops, int origlen)
1303{
1304	struct atalk_route *rt;
1305	struct atalk_addr ta;
1306
1307	/*
1308	 * Don't route multicast, etc., packets, or packets sent to "this
1309	 * network"
1310	 */
1311	if (skb->pkt_type != PACKET_HOST || !ddp->deh_dnet) {
1312		/*
1313		 * FIXME:
1314		 *
1315		 * Can it ever happen that a packet is from a PPP iface and
1316		 * needs to be broadcast onto the default network?
1317		 */
1318		if (dev->type == ARPHRD_PPP)
1319			printk(KERN_DEBUG "AppleTalk: didn't forward broadcast "
1320					  "packet received from PPP iface\n");
1321		goto free_it;
1322	}
1323
1324	ta.s_net  = ddp->deh_dnet;
1325	ta.s_node = ddp->deh_dnode;
1326
1327	/* Route the packet */
1328	rt = atrtr_find(&ta);
1329	/* increment hops count */
1330	len_hops += 1 << 10;
1331	if (!rt || !(len_hops & (15 << 10)))
1332		goto free_it;
1333
1334	/* FIXME: use skb->cb to be able to use shared skbs */
1335
1336	/*
1337	 * Route goes through another gateway, so set the target to the
1338	 * gateway instead.
1339	 */
1340
1341	if (rt->flags & RTF_GATEWAY) {
1342		ta.s_net  = rt->gateway.s_net;
1343		ta.s_node = rt->gateway.s_node;
1344	}
1345
1346	/* Fix up skb->len field */
1347	skb_trim(skb, min_t(unsigned int, origlen,
1348			    (rt->dev->hard_header_len +
1349			     ddp_dl->header_length + (len_hops & 1023))));
1350
1351	/* FIXME: use skb->cb to be able to use shared skbs */
1352	ddp->deh_len_hops = htons(len_hops);
1353
1354	/*
1355	 * Send the buffer onwards
1356	 *
1357	 * Now we must always be careful. If it's come from LocalTalk to
1358	 * EtherTalk it might not fit
1359	 *
1360	 * Order matters here: If a packet has to be copied to make a new
1361	 * headroom (rare hopefully) then it won't need unsharing.
1362	 *
1363	 * Note. ddp-> becomes invalid at the realloc.
1364	 */
1365	if (skb_headroom(skb) < 22) {
1366		/* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */
1367		struct sk_buff *nskb = skb_realloc_headroom(skb, 32);
1368		kfree_skb(skb);
1369		skb = nskb;
1370	} else
1371		skb = skb_unshare(skb, GFP_ATOMIC);
1372
1373	/*
1374	 * If the buffer didn't vanish into the lack of space bitbucket we can
1375	 * send it.
1376	 */
1377	if (skb == NULL)
1378		goto drop;
1379
1380	if (aarp_send_ddp(rt->dev, skb, &ta, NULL) == NET_XMIT_DROP)
1381		return NET_RX_DROP;
1382	return NET_RX_SUCCESS;
1383free_it:
1384	kfree_skb(skb);
1385drop:
1386	return NET_RX_DROP;
1387}
1388
1389/**
1390 *	atalk_rcv - Receive a packet (in skb) from device dev
1391 *	@skb: packet received
1392 *	@dev: network device where the packet comes from
1393 *	@pt: packet type
1394 *	@orig_dev: the original receive net device
1395 *
1396 *	Receive a packet (in skb) from device dev. This has come from the SNAP
1397 *	decoder, and on entry skb->transport_header is the DDP header, skb->len
1398 *	is the DDP header, skb->len is the DDP length. The physical headers
1399 *	have been extracted. PPP should probably pass frames marked as for this
1400 *	layer.  [ie ARPHRD_ETHERTALK]
1401 */
1402static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
1403		     struct packet_type *pt, struct net_device *orig_dev)
1404{
1405	struct ddpehdr *ddp;
1406	struct sock *sock;
1407	struct atalk_iface *atif;
1408	struct sockaddr_at tosat;
1409	int origlen;
1410	__u16 len_hops;
1411
1412	if (!net_eq(dev_net(dev), &init_net))
1413		goto drop;
1414
1415	/* Don't mangle buffer if shared */
1416	if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
1417		goto out;
1418
1419	/* Size check and make sure header is contiguous */
1420	if (!pskb_may_pull(skb, sizeof(*ddp)))
1421		goto drop;
1422
1423	ddp = ddp_hdr(skb);
1424
1425	len_hops = ntohs(ddp->deh_len_hops);
1426
1427	/* Trim buffer in case of stray trailing data */
1428	origlen = skb->len;
1429	skb_trim(skb, min_t(unsigned int, skb->len, len_hops & 1023));
1430
1431	/*
1432	 * Size check to see if ddp->deh_len was crap
1433	 * (Otherwise we'll detonate most spectacularly
1434	 * in the middle of atalk_checksum() or recvmsg()).
1435	 */
1436	if (skb->len < sizeof(*ddp) || skb->len < (len_hops & 1023)) {
1437		pr_debug("AppleTalk: dropping corrupted frame (deh_len=%u, "
1438			 "skb->len=%u)\n", len_hops & 1023, skb->len);
1439		goto drop;
1440	}
1441
1442	/*
1443	 * Any checksums. Note we don't do htons() on this == is assumed to be
1444	 * valid for net byte orders all over the networking code...
1445	 */
1446	if (ddp->deh_sum &&
1447	    atalk_checksum(skb, len_hops & 1023) != ddp->deh_sum)
1448		/* Not a valid AppleTalk frame - dustbin time */
1449		goto drop;
1450
1451	/* Check the packet is aimed at us */
1452	if (!ddp->deh_dnet)	/* Net 0 is 'this network' */
1453		atif = atalk_find_anynet(ddp->deh_dnode, dev);
1454	else
1455		atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode);
1456
1457	if (!atif) {
1458		/* Not ours, so we route the packet via the correct
1459		 * AppleTalk iface
1460		 */
1461		return atalk_route_packet(skb, dev, ddp, len_hops, origlen);
1462	}
1463
1464	/*
1465	 * Which socket - atalk_search_socket() looks for a *full match*
1466	 * of the <net, node, port> tuple.
1467	 */
1468	tosat.sat_addr.s_net  = ddp->deh_dnet;
1469	tosat.sat_addr.s_node = ddp->deh_dnode;
1470	tosat.sat_port	      = ddp->deh_dport;
1471
1472	sock = atalk_search_socket(&tosat, atif);
1473	if (!sock) /* But not one of our sockets */
1474		goto drop;
1475
1476	/* Queue packet (standard) */
1477	if (sock_queue_rcv_skb(sock, skb) < 0)
1478		goto drop;
1479
1480	return NET_RX_SUCCESS;
1481
1482drop:
1483	kfree_skb(skb);
1484out:
1485	return NET_RX_DROP;
1486
1487}
1488
1489/*
1490 * Receive a LocalTalk frame. We make some demands on the caller here.
1491 * Caller must provide enough headroom on the packet to pull the short
1492 * header and append a long one.
1493 */
1494static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev,
1495		     struct packet_type *pt, struct net_device *orig_dev)
1496{
1497	if (!net_eq(dev_net(dev), &init_net))
1498		goto freeit;
1499
1500	/* Expand any short form frames */
1501	if (skb_mac_header(skb)[2] == 1) {
1502		struct ddpehdr *ddp;
1503		/* Find our address */
1504		struct atalk_addr *ap = atalk_find_dev_addr(dev);
1505
1506		if (!ap || skb->len < sizeof(__be16) || skb->len > 1023)
1507			goto freeit;
1508
1509		/* Don't mangle buffer if shared */
1510		if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
1511			return 0;
1512
1513		/*
1514		 * The push leaves us with a ddephdr not an shdr, and
1515		 * handily the port bytes in the right place preset.
1516		 */
1517		ddp = skb_push(skb, sizeof(*ddp) - 4);
1518
1519		/* Now fill in the long header */
1520
1521		/*
1522		 * These two first. The mac overlays the new source/dest
1523		 * network information so we MUST copy these before
1524		 * we write the network numbers !
1525		 */
1526
1527		ddp->deh_dnode = skb_mac_header(skb)[0];     /* From physical header */
1528		ddp->deh_snode = skb_mac_header(skb)[1];     /* From physical header */
1529
1530		ddp->deh_dnet  = ap->s_net;	/* Network number */
1531		ddp->deh_snet  = ap->s_net;
1532		ddp->deh_sum   = 0;		/* No checksum */
1533		/*
1534		 * Not sure about this bit...
1535		 */
1536		/* Non routable, so force a drop if we slip up later */
1537		ddp->deh_len_hops = htons(skb->len + (DDP_MAXHOPS << 10));
1538	}
1539	skb_reset_transport_header(skb);
1540
1541	return atalk_rcv(skb, dev, pt, orig_dev);
1542freeit:
1543	kfree_skb(skb);
1544	return 0;
1545}
1546
1547static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1548{
1549	struct sock *sk = sock->sk;
1550	struct atalk_sock *at = at_sk(sk);
1551	DECLARE_SOCKADDR(struct sockaddr_at *, usat, msg->msg_name);
1552	int flags = msg->msg_flags;
1553	int loopback = 0;
1554	struct sockaddr_at local_satalk, gsat;
1555	struct sk_buff *skb;
1556	struct net_device *dev;
1557	struct ddpehdr *ddp;
1558	int size, hard_header_len;
1559	struct atalk_route *rt, *rt_lo = NULL;
1560	int err;
1561
1562	if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1563		return -EINVAL;
1564
1565	if (len > DDP_MAXSZ)
1566		return -EMSGSIZE;
1567
1568	lock_sock(sk);
1569	if (usat) {
1570		err = -EBUSY;
1571		if (sock_flag(sk, SOCK_ZAPPED))
1572			if (atalk_autobind(sk) < 0)
1573				goto out;
1574
1575		err = -EINVAL;
1576		if (msg->msg_namelen < sizeof(*usat) ||
1577		    usat->sat_family != AF_APPLETALK)
1578			goto out;
1579
1580		err = -EPERM;
1581		/* netatalk didn't implement this check */
1582		if (usat->sat_addr.s_node == ATADDR_BCAST &&
1583		    !sock_flag(sk, SOCK_BROADCAST)) {
1584			goto out;
1585		}
1586	} else {
1587		err = -ENOTCONN;
1588		if (sk->sk_state != TCP_ESTABLISHED)
1589			goto out;
1590		usat = &local_satalk;
1591		usat->sat_family      = AF_APPLETALK;
1592		usat->sat_port	      = at->dest_port;
1593		usat->sat_addr.s_node = at->dest_node;
1594		usat->sat_addr.s_net  = at->dest_net;
1595	}
1596
1597	/* Build a packet */
1598	net_dbg_ratelimited("SK %p: Got address.\n", sk);
1599
1600	/* For headers */
1601	size = sizeof(struct ddpehdr) + len + ddp_dl->header_length;
1602
1603	if (usat->sat_addr.s_net || usat->sat_addr.s_node == ATADDR_ANYNODE) {
1604		rt = atrtr_find(&usat->sat_addr);
1605	} else {
1606		struct atalk_addr at_hint;
1607
1608		at_hint.s_node = 0;
1609		at_hint.s_net  = at->src_net;
1610
1611		rt = atrtr_find(&at_hint);
1612	}
1613	err = -ENETUNREACH;
1614	if (!rt)
1615		goto out;
1616
1617	dev = rt->dev;
1618
1619	net_dbg_ratelimited("SK %p: Size needed %d, device %s\n",
1620			sk, size, dev->name);
1621
1622	hard_header_len = dev->hard_header_len;
1623	/* Leave room for loopback hardware header if necessary */
1624	if (usat->sat_addr.s_node == ATADDR_BCAST &&
1625	    (dev->flags & IFF_LOOPBACK || !(rt->flags & RTF_GATEWAY))) {
1626		struct atalk_addr at_lo;
1627
1628		at_lo.s_node = 0;
1629		at_lo.s_net  = 0;
1630
1631		rt_lo = atrtr_find(&at_lo);
1632
1633		if (rt_lo && rt_lo->dev->hard_header_len > hard_header_len)
1634			hard_header_len = rt_lo->dev->hard_header_len;
1635	}
1636
1637	size += hard_header_len;
1638	release_sock(sk);
1639	skb = sock_alloc_send_skb(sk, size, (flags & MSG_DONTWAIT), &err);
1640	lock_sock(sk);
1641	if (!skb)
1642		goto out;
1643
1644	skb_reserve(skb, ddp_dl->header_length);
1645	skb_reserve(skb, hard_header_len);
1646	skb->dev = dev;
1647
1648	net_dbg_ratelimited("SK %p: Begin build.\n", sk);
1649
1650	ddp = skb_put(skb, sizeof(struct ddpehdr));
1651	ddp->deh_len_hops  = htons(len + sizeof(*ddp));
1652	ddp->deh_dnet  = usat->sat_addr.s_net;
1653	ddp->deh_snet  = at->src_net;
1654	ddp->deh_dnode = usat->sat_addr.s_node;
1655	ddp->deh_snode = at->src_node;
1656	ddp->deh_dport = usat->sat_port;
1657	ddp->deh_sport = at->src_port;
1658
1659	net_dbg_ratelimited("SK %p: Copy user data (%zd bytes).\n", sk, len);
1660
1661	err = memcpy_from_msg(skb_put(skb, len), msg, len);
1662	if (err) {
1663		kfree_skb(skb);
1664		err = -EFAULT;
1665		goto out;
1666	}
1667
1668	if (sk->sk_no_check_tx)
1669		ddp->deh_sum = 0;
1670	else
1671		ddp->deh_sum = atalk_checksum(skb, len + sizeof(*ddp));
1672
1673	/*
1674	 * Loopback broadcast packets to non gateway targets (ie routes
1675	 * to group we are in)
1676	 */
1677	if (ddp->deh_dnode == ATADDR_BCAST &&
1678	    !(rt->flags & RTF_GATEWAY) && !(dev->flags & IFF_LOOPBACK)) {
1679		struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL);
1680
1681		if (skb2) {
1682			loopback = 1;
1683			net_dbg_ratelimited("SK %p: send out(copy).\n", sk);
1684			/*
1685			 * If it fails it is queued/sent above in the aarp queue
1686			 */
1687			aarp_send_ddp(dev, skb2, &usat->sat_addr, NULL);
1688		}
1689	}
1690
1691	if (dev->flags & IFF_LOOPBACK || loopback) {
1692		net_dbg_ratelimited("SK %p: Loop back.\n", sk);
1693		/* loop back */
1694		skb_orphan(skb);
1695		if (ddp->deh_dnode == ATADDR_BCAST) {
1696			if (!rt_lo) {
1697				kfree_skb(skb);
1698				err = -ENETUNREACH;
1699				goto out;
1700			}
1701			dev = rt_lo->dev;
1702			skb->dev = dev;
1703		}
1704		ddp_dl->request(ddp_dl, skb, dev->dev_addr);
1705	} else {
1706		net_dbg_ratelimited("SK %p: send out.\n", sk);
1707		if (rt->flags & RTF_GATEWAY) {
1708		    gsat.sat_addr = rt->gateway;
1709		    usat = &gsat;
1710		}
1711
1712		/*
1713		 * If it fails it is queued/sent above in the aarp queue
1714		 */
1715		aarp_send_ddp(dev, skb, &usat->sat_addr, NULL);
1716	}
1717	net_dbg_ratelimited("SK %p: Done write (%zd).\n", sk, len);
1718
1719out:
1720	release_sock(sk);
1721	return err ? : len;
1722}
1723
1724static int atalk_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1725			 int flags)
1726{
1727	struct sock *sk = sock->sk;
1728	struct ddpehdr *ddp;
1729	int copied = 0;
1730	int offset = 0;
1731	int err = 0;
1732	struct sk_buff *skb;
1733
1734	skb = skb_recv_datagram(sk, flags, &err);
1735	lock_sock(sk);
1736
1737	if (!skb)
1738		goto out;
1739
1740	/* FIXME: use skb->cb to be able to use shared skbs */
1741	ddp = ddp_hdr(skb);
1742	copied = ntohs(ddp->deh_len_hops) & 1023;
1743
1744	if (sk->sk_type != SOCK_RAW) {
1745		offset = sizeof(*ddp);
1746		copied -= offset;
1747	}
1748
1749	if (copied > size) {
1750		copied = size;
1751		msg->msg_flags |= MSG_TRUNC;
1752	}
1753	err = skb_copy_datagram_msg(skb, offset, msg, copied);
1754
1755	if (!err && msg->msg_name) {
1756		DECLARE_SOCKADDR(struct sockaddr_at *, sat, msg->msg_name);
1757		sat->sat_family      = AF_APPLETALK;
1758		sat->sat_port        = ddp->deh_sport;
1759		sat->sat_addr.s_node = ddp->deh_snode;
1760		sat->sat_addr.s_net  = ddp->deh_snet;
1761		msg->msg_namelen     = sizeof(*sat);
1762	}
1763
1764	skb_free_datagram(sk, skb);	/* Free the datagram. */
1765
1766out:
1767	release_sock(sk);
1768	return err ? : copied;
1769}
1770
1771
1772/*
1773 * AppleTalk ioctl calls.
1774 */
1775static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1776{
1777	int rc = -ENOIOCTLCMD;
1778	struct sock *sk = sock->sk;
1779	void __user *argp = (void __user *)arg;
1780
1781	switch (cmd) {
1782	/* Protocol layer */
1783	case TIOCOUTQ: {
1784		long amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1785
1786		if (amount < 0)
1787			amount = 0;
1788		rc = put_user(amount, (int __user *)argp);
1789		break;
1790	}
1791	case TIOCINQ: {
1792		struct sk_buff *skb;
1793		long amount = 0;
1794
1795		spin_lock_irq(&sk->sk_receive_queue.lock);
1796		skb = skb_peek(&sk->sk_receive_queue);
1797		if (skb)
1798			amount = skb->len - sizeof(struct ddpehdr);
1799		spin_unlock_irq(&sk->sk_receive_queue.lock);
1800		rc = put_user(amount, (int __user *)argp);
1801		break;
1802	}
1803	/* Routing */
1804	case SIOCADDRT:
1805	case SIOCDELRT:
1806		rc = -EPERM;
1807		if (capable(CAP_NET_ADMIN))
1808			rc = atrtr_ioctl(cmd, argp);
1809		break;
1810	/* Interface */
1811	case SIOCGIFADDR:
1812	case SIOCSIFADDR:
1813	case SIOCGIFBRDADDR:
1814	case SIOCATALKDIFADDR:
1815	case SIOCDIFADDR:
1816	case SIOCSARP:		/* proxy AARP */
1817	case SIOCDARP:		/* proxy AARP */
1818		rtnl_lock();
1819		rc = atif_ioctl(cmd, argp);
1820		rtnl_unlock();
1821		break;
1822	}
1823
1824	return rc;
1825}
1826
1827
1828#ifdef CONFIG_COMPAT
1829static int atalk_compat_routing_ioctl(struct sock *sk, unsigned int cmd,
1830		struct compat_rtentry __user *ur)
1831{
1832	compat_uptr_t rtdev;
1833	struct rtentry rt;
1834
1835	if (copy_from_user(&rt.rt_dst, &ur->rt_dst,
1836			3 * sizeof(struct sockaddr)) ||
1837	    get_user(rt.rt_flags, &ur->rt_flags) ||
1838	    get_user(rt.rt_metric, &ur->rt_metric) ||
1839	    get_user(rt.rt_mtu, &ur->rt_mtu) ||
1840	    get_user(rt.rt_window, &ur->rt_window) ||
1841	    get_user(rt.rt_irtt, &ur->rt_irtt) ||
1842	    get_user(rtdev, &ur->rt_dev))
1843		return -EFAULT;
1844
1845	switch (cmd) {
1846	case SIOCDELRT:
1847		if (rt.rt_dst.sa_family != AF_APPLETALK)
1848			return -EINVAL;
1849		return atrtr_delete(&((struct sockaddr_at *)
1850				      &rt.rt_dst)->sat_addr);
1851
1852	case SIOCADDRT:
1853		rt.rt_dev = compat_ptr(rtdev);
1854		return atrtr_ioctl_addrt(&rt);
1855	default:
1856		return -EINVAL;
1857	}
1858}
1859static int atalk_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1860{
1861	void __user *argp = compat_ptr(arg);
1862	struct sock *sk = sock->sk;
1863
1864	switch (cmd) {
1865	case SIOCADDRT:
1866	case SIOCDELRT:
1867		return atalk_compat_routing_ioctl(sk, cmd, argp);
1868	/*
1869	 * SIOCATALKDIFADDR is a SIOCPROTOPRIVATE ioctl number, so we
1870	 * cannot handle it in common code. The data we access if ifreq
1871	 * here is compatible, so we can simply call the native
1872	 * handler.
1873	 */
1874	case SIOCATALKDIFADDR:
1875		return atalk_ioctl(sock, cmd, (unsigned long)argp);
1876	default:
1877		return -ENOIOCTLCMD;
1878	}
1879}
1880#endif /* CONFIG_COMPAT */
1881
1882
1883static const struct net_proto_family atalk_family_ops = {
1884	.family		= PF_APPLETALK,
1885	.create		= atalk_create,
1886	.owner		= THIS_MODULE,
1887};
1888
1889static const struct proto_ops atalk_dgram_ops = {
1890	.family		= PF_APPLETALK,
1891	.owner		= THIS_MODULE,
1892	.release	= atalk_release,
1893	.bind		= atalk_bind,
1894	.connect	= atalk_connect,
1895	.socketpair	= sock_no_socketpair,
1896	.accept		= sock_no_accept,
1897	.getname	= atalk_getname,
1898	.poll		= datagram_poll,
1899	.ioctl		= atalk_ioctl,
1900	.gettstamp	= sock_gettstamp,
1901#ifdef CONFIG_COMPAT
1902	.compat_ioctl	= atalk_compat_ioctl,
1903#endif
1904	.listen		= sock_no_listen,
1905	.shutdown	= sock_no_shutdown,
1906	.sendmsg	= atalk_sendmsg,
1907	.recvmsg	= atalk_recvmsg,
1908	.mmap		= sock_no_mmap,
1909};
1910
1911static struct notifier_block ddp_notifier = {
1912	.notifier_call	= ddp_device_event,
1913};
1914
1915static struct packet_type ltalk_packet_type __read_mostly = {
1916	.type		= cpu_to_be16(ETH_P_LOCALTALK),
1917	.func		= ltalk_rcv,
1918};
1919
1920static struct packet_type ppptalk_packet_type __read_mostly = {
1921	.type		= cpu_to_be16(ETH_P_PPPTALK),
1922	.func		= atalk_rcv,
1923};
1924
1925static unsigned char ddp_snap_id[] = { 0x08, 0x00, 0x07, 0x80, 0x9B };
1926
1927/* Export symbols for use by drivers when AppleTalk is a module */
1928EXPORT_SYMBOL(atrtr_get_dev);
1929EXPORT_SYMBOL(atalk_find_dev_addr);
1930
1931/* Called by proto.c on kernel start up */
1932static int __init atalk_init(void)
1933{
1934	int rc;
1935
1936	rc = proto_register(&ddp_proto, 0);
1937	if (rc)
1938		goto out;
1939
1940	rc = sock_register(&atalk_family_ops);
1941	if (rc)
1942		goto out_proto;
1943
1944	ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv);
1945	if (!ddp_dl) {
1946		pr_crit("Unable to register DDP with SNAP.\n");
1947		rc = -ENOMEM;
1948		goto out_sock;
1949	}
1950
1951	dev_add_pack(&ltalk_packet_type);
1952	dev_add_pack(&ppptalk_packet_type);
1953
1954	rc = register_netdevice_notifier(&ddp_notifier);
1955	if (rc)
1956		goto out_snap;
1957
1958	rc = aarp_proto_init();
1959	if (rc)
1960		goto out_dev;
1961
1962	rc = atalk_proc_init();
1963	if (rc)
1964		goto out_aarp;
1965
1966	rc = atalk_register_sysctl();
1967	if (rc)
1968		goto out_proc;
1969out:
1970	return rc;
1971out_proc:
1972	atalk_proc_exit();
1973out_aarp:
1974	aarp_cleanup_module();
1975out_dev:
1976	unregister_netdevice_notifier(&ddp_notifier);
1977out_snap:
1978	dev_remove_pack(&ppptalk_packet_type);
1979	dev_remove_pack(&ltalk_packet_type);
1980	unregister_snap_client(ddp_dl);
1981out_sock:
1982	sock_unregister(PF_APPLETALK);
1983out_proto:
1984	proto_unregister(&ddp_proto);
1985	goto out;
1986}
1987module_init(atalk_init);
1988
1989/*
1990 * No explicit module reference count manipulation is needed in the
1991 * protocol. Socket layer sets module reference count for us
1992 * and interfaces reference counting is done
1993 * by the network device layer.
1994 *
1995 * Ergo, before the AppleTalk module can be removed, all AppleTalk
1996 * sockets should be closed from user space.
1997 */
1998static void __exit atalk_exit(void)
1999{
2000#ifdef CONFIG_SYSCTL
2001	atalk_unregister_sysctl();
2002#endif /* CONFIG_SYSCTL */
2003	atalk_proc_exit();
2004	aarp_cleanup_module();	/* General aarp clean-up. */
2005	unregister_netdevice_notifier(&ddp_notifier);
2006	dev_remove_pack(&ltalk_packet_type);
2007	dev_remove_pack(&ppptalk_packet_type);
2008	unregister_snap_client(ddp_dl);
2009	sock_unregister(PF_APPLETALK);
2010	proto_unregister(&ddp_proto);
2011}
2012module_exit(atalk_exit);
2013
2014MODULE_LICENSE("GPL");
2015MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
2016MODULE_DESCRIPTION("AppleTalk 0.20\n");
2017MODULE_ALIAS_NETPROTO(PF_APPLETALK);
~/ Read-only file preview.