ddp.c 47.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 *	DDP:	An implementation of the AppleTalk DDP protocol for
 *		Ethernet 'ELAP'.
 *
5
 *		Alan Cox  <alan@lxorguk.ukuu.org.uk>
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 *		With more than a little assistance from
 *
 *		Wesley Craig <netatalk@umich.edu>
 *
 *	Fixes:
 *		Neil Horman		:	Added missing device ioctls
 *		Michael Callahan	:	Made routing work
 *		Wesley Craig		:	Fix probing to listen to a
 *						passed node id.
 *		Alan Cox		:	Added send/recvmsg support
 *		Alan Cox		:	Moved at. to protinfo in
 *						socket.
 *		Alan Cox		:	Added firewall hooks.
 *		Alan Cox		:	Supports new ARPHRD_LOOPBACK
 *		Christer Weinigel	: 	Routing and /proc fixes.
 *		Bradford Johnson	:	LocalTalk.
 *		Tom Dyas		:	Module support.
 *		Alan Cox		:	Hooks for PPP (based on the
 *						LocalTalk hook).
 *		Alan Cox		:	Posix bits
 *		Alan Cox/Mike Freeman	:	Possible fix to NBP problems
 *		Bradford Johnson	:	IP-over-DDP (experimental)
 *		Jay Schulist		:	Moved IP-over-DDP to its own
 *						driver file. (ipddp.c & ipddp.h)
31
 *		Jay Schulist		:	Made work as module with
L
Linus Torvalds 已提交
32 33 34 35
 *						AppleTalk drivers, cleaned it.
 *		Rob Newberry		:	Added proxy AARP and AARP
 *						procfs, moved probing to AARP
 *						module.
36 37
 *              Adrian Sun/
 *              Michael Zuelsdorff      :       fix for net.0 packets. don't
L
Linus Torvalds 已提交
38
 *                                              allow illegal ether/tokentalk
39 40
 *                                              port assignment. we lose a
 *                                              valid localtalk port as a
L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48 49 50
 *                                              result.
 *		Arnaldo C. de Melo	:	Cleanup, in preparation for
 *						shared skb support 8)
 *		Arnaldo C. de Melo	:	Move proc stuff to atalk_proc.c,
 *						use seq_file
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
51
 *
L
Linus Torvalds 已提交
52 53
 */

54
#include <linux/capability.h>
L
Linus Torvalds 已提交
55 56 57
#include <linux/module.h>
#include <linux/if_arp.h>
#include <linux/termios.h>	/* For TIOCOUTQ/INQ */
58
#include <linux/compat.h>
59
#include <linux/slab.h>
L
Linus Torvalds 已提交
60 61 62
#include <net/datalink.h>
#include <net/psnap.h>
#include <net/sock.h>
63
#include <net/tcp_states.h>
L
Linus Torvalds 已提交
64 65
#include <net/route.h>
#include <linux/atalk.h>
E
Eric Dumazet 已提交
66
#include <linux/highmem.h>
L
Linus Torvalds 已提交
67 68

struct datalink_proto *ddp_dl, *aarp_dl;
69
static const struct proto_ops atalk_dgram_ops;
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

/**************************************************************************\
*                                                                          *
* Handlers for the socket list.                                            *
*                                                                          *
\**************************************************************************/

HLIST_HEAD(atalk_sockets);
DEFINE_RWLOCK(atalk_sockets_lock);

static inline void __atalk_insert_socket(struct sock *sk)
{
	sk_add_node(sk, &atalk_sockets);
}

static inline void atalk_remove_socket(struct sock *sk)
{
	write_lock_bh(&atalk_sockets_lock);
	sk_del_node_init(sk);
	write_unlock_bh(&atalk_sockets_lock);
}

static struct sock *atalk_search_socket(struct sockaddr_at *to,
					struct atalk_iface *atif)
{
	struct sock *s;

	read_lock_bh(&atalk_sockets_lock);
98
	sk_for_each(s, &atalk_sockets) {
L
Linus Torvalds 已提交
99 100 101 102 103
		struct atalk_sock *at = at_sk(s);

		if (to->sat_port != at->src_port)
			continue;

104
		if (to->sat_addr.s_net == ATADDR_ANYNET &&
O
Oliver Dawid 已提交
105
		    to->sat_addr.s_node == ATADDR_BCAST)
L
Linus Torvalds 已提交
106 107
			goto found;

108
		if (to->sat_addr.s_net == at->src_net &&
L
Linus Torvalds 已提交
109 110 111 112 113
		    (to->sat_addr.s_node == at->src_node ||
		     to->sat_addr.s_node == ATADDR_BCAST ||
		     to->sat_addr.s_node == ATADDR_ANYNODE))
			goto found;

114
		/* XXXX.0 -- we got a request for this router. make sure
L
Linus Torvalds 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		 * that the node is appropriately set. */
		if (to->sat_addr.s_node == ATADDR_ANYNODE &&
		    to->sat_addr.s_net != ATADDR_ANYNET &&
		    atif->address.s_node == at->src_node) {
			to->sat_addr.s_node = atif->address.s_node;
			goto found;
		}
	}
	s = NULL;
found:
	read_unlock_bh(&atalk_sockets_lock);
	return s;
}

/**
 * atalk_find_or_insert_socket - Try to find a socket matching ADDR
131 132
 * @sk: socket to insert in the list if it is not there already
 * @sat: address to search for
L
Linus Torvalds 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145
 *
 * Try to find a socket matching ADDR in the socket list, if found then return
 * it. If not, insert SK into the socket list.
 *
 * This entire operation must execute atomically.
 */
static struct sock *atalk_find_or_insert_socket(struct sock *sk,
						struct sockaddr_at *sat)
{
	struct sock *s;
	struct atalk_sock *at;

	write_lock_bh(&atalk_sockets_lock);
146
	sk_for_each(s, &atalk_sockets) {
L
Linus Torvalds 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160
		at = at_sk(s);

		if (at->src_net == sat->sat_addr.s_net &&
		    at->src_node == sat->sat_addr.s_node &&
		    at->src_port == sat->sat_port)
			goto found;
	}
	s = NULL;
	__atalk_insert_socket(sk); /* Wheee, it's free, assign and insert. */
found:
	write_unlock_bh(&atalk_sockets_lock);
	return s;
}

161
static void atalk_destroy_timer(struct timer_list *t)
L
Linus Torvalds 已提交
162
{
163
	struct sock *sk = from_timer(sk, t, sk_timer);
L
Linus Torvalds 已提交
164

165
	if (sk_has_allocations(sk)) {
L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173 174 175 176
		sk->sk_timer.expires = jiffies + SOCK_DESTROY_TIME;
		add_timer(&sk->sk_timer);
	} else
		sock_put(sk);
}

static inline void atalk_destroy_socket(struct sock *sk)
{
	atalk_remove_socket(sk);
	skb_queue_purge(&sk->sk_receive_queue);

177
	if (sk_has_allocations(sk)) {
178
		timer_setup(&sk->sk_timer, atalk_destroy_timer, 0);
L
Linus Torvalds 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
		sk->sk_timer.expires	= jiffies + SOCK_DESTROY_TIME;
		add_timer(&sk->sk_timer);
	} else
		sock_put(sk);
}

/**************************************************************************\
*                                                                          *
* Routing tables for the AppleTalk socket layer.                           *
*                                                                          *
\**************************************************************************/

/* Anti-deadlock ordering is atalk_routes_lock --> iface_lock -DaveM */
struct atalk_route *atalk_routes;
DEFINE_RWLOCK(atalk_routes_lock);

struct atalk_iface *atalk_interfaces;
DEFINE_RWLOCK(atalk_interfaces_lock);

/* For probing devices or in a routerless network */
struct atalk_route atrtr_default;

/* AppleTalk interface control */
/*
 * Drop a device. Doesn't drop any of its routes - that is the caller's
 * problem. Called when we down the interface or delete the address.
 */
static void atif_drop_device(struct net_device *dev)
{
	struct atalk_iface **iface = &atalk_interfaces;
	struct atalk_iface *tmp;

	write_lock_bh(&atalk_interfaces_lock);
	while ((tmp = *iface) != NULL) {
		if (tmp->dev == dev) {
			*iface = tmp->next;
			dev_put(dev);
			kfree(tmp);
			dev->atalk_ptr = NULL;
		} else
			iface = &tmp->next;
	}
	write_unlock_bh(&atalk_interfaces_lock);
}

static struct atalk_iface *atif_add_device(struct net_device *dev,
					   struct atalk_addr *sa)
{
227
	struct atalk_iface *iface = kzalloc(sizeof(*iface), GFP_KERNEL);
L
Linus Torvalds 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

	if (!iface)
		goto out;

	dev_hold(dev);
	iface->dev = dev;
	dev->atalk_ptr = iface;
	iface->address = *sa;
	iface->status = 0;

	write_lock_bh(&atalk_interfaces_lock);
	iface->next = atalk_interfaces;
	atalk_interfaces = iface;
	write_unlock_bh(&atalk_interfaces_lock);
out:
	return iface;
}

/* Perform phase 2 AARP probing on our tentative address */
static int atif_probe_device(struct atalk_iface *atif)
{
	int netrange = ntohs(atif->nets.nr_lastnet) -
			ntohs(atif->nets.nr_firstnet) + 1;
	int probe_net = ntohs(atif->address.s_net);
	int probe_node = atif->address.s_node;
	int netct, nodect;

	/* Offset the network we start probing with */
	if (probe_net == ATADDR_ANYNET) {
		probe_net = ntohs(atif->nets.nr_firstnet);
		if (netrange)
			probe_net += jiffies % netrange;
	}
	if (probe_node == ATADDR_ANYNODE)
		probe_node = jiffies & 0xFF;

	/* Scan the networks */
	atif->status |= ATIF_PROBE;
	for (netct = 0; netct <= netrange; netct++) {
		/* Sweep the available nodes from a given start */
		atif->address.s_net = htons(probe_net);
		for (nodect = 0; nodect < 256; nodect++) {
			atif->address.s_node = (nodect + probe_node) & 0xFF;
			if (atif->address.s_node > 0 &&
			    atif->address.s_node < 254) {
				/* Probe a proposed address */
				aarp_probe_network(atif);

				if (!(atif->status & ATIF_PROBE_FAIL)) {
					atif->status &= ~ATIF_PROBE;
					return 0;
				}
			}
			atif->status &= ~ATIF_PROBE_FAIL;
		}
		probe_net++;
		if (probe_net > ntohs(atif->nets.nr_lastnet))
			probe_net = ntohs(atif->nets.nr_firstnet);
	}
	atif->status &= ~ATIF_PROBE;

	return -EADDRINUSE;	/* Network is full... */
}


/* Perform AARP probing for a proxy address */
static int atif_proxy_probe_device(struct atalk_iface *atif,
295
				   struct atalk_addr *proxy_addr)
L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
{
	int netrange = ntohs(atif->nets.nr_lastnet) -
			ntohs(atif->nets.nr_firstnet) + 1;
	/* we probe the interface's network */
	int probe_net = ntohs(atif->address.s_net);
	int probe_node = ATADDR_ANYNODE;	    /* we'll take anything */
	int netct, nodect;

	/* Offset the network we start probing with */
	if (probe_net == ATADDR_ANYNET) {
		probe_net = ntohs(atif->nets.nr_firstnet);
		if (netrange)
			probe_net += jiffies % netrange;
	}

	if (probe_node == ATADDR_ANYNODE)
		probe_node = jiffies & 0xFF;
313

L
Linus Torvalds 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	/* Scan the networks */
	for (netct = 0; netct <= netrange; netct++) {
		/* Sweep the available nodes from a given start */
		proxy_addr->s_net = htons(probe_net);
		for (nodect = 0; nodect < 256; nodect++) {
			proxy_addr->s_node = (nodect + probe_node) & 0xFF;
			if (proxy_addr->s_node > 0 &&
			    proxy_addr->s_node < 254) {
				/* Tell AARP to probe a proposed address */
				int ret = aarp_proxy_probe_network(atif,
								    proxy_addr);

				if (ret != -EADDRINUSE)
					return ret;
			}
		}
		probe_net++;
		if (probe_net > ntohs(atif->nets.nr_lastnet))
			probe_net = ntohs(atif->nets.nr_firstnet);
	}

	return -EADDRINUSE;	/* Network is full... */
}


struct atalk_addr *atalk_find_dev_addr(struct net_device *dev)
{
	struct atalk_iface *iface = dev->atalk_ptr;
	return iface ? &iface->address : NULL;
}

static struct atalk_addr *atalk_find_primary(void)
{
	struct atalk_iface *fiface = NULL;
	struct atalk_addr *retval;
	struct atalk_iface *iface;

	/*
	 * Return a point-to-point interface only if
	 * there is no non-ptp interface available.
	 */
	read_lock_bh(&atalk_interfaces_lock);
	for (iface = atalk_interfaces; iface; iface = iface->next) {
		if (!fiface && !(iface->dev->flags & IFF_LOOPBACK))
			fiface = iface;
		if (!(iface->dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) {
			retval = &iface->address;
			goto out;
		}
	}

	if (fiface)
		retval = &fiface->address;
	else if (atalk_interfaces)
		retval = &atalk_interfaces->address;
	else
		retval = NULL;
out:
	read_unlock_bh(&atalk_interfaces_lock);
	return retval;
}

/*
 * Find a match for 'any network' - ie any of our interfaces with that
 * node number will do just nicely.
 */
static struct atalk_iface *atalk_find_anynet(int node, struct net_device *dev)
{
	struct atalk_iface *iface = dev->atalk_ptr;

	if (!iface || iface->status & ATIF_PROBE)
		goto out_err;

	if (node != ATADDR_BCAST &&
	    iface->address.s_node != node &&
	    node != ATADDR_ANYNODE)
		goto out_err;
out:
	return iface;
out_err:
	iface = NULL;
	goto out;
}

/* Find a match for a specific network:node pair */
A
Alexey Dobriyan 已提交
399
static struct atalk_iface *atalk_find_interface(__be16 net, int node)
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
{
	struct atalk_iface *iface;

	read_lock_bh(&atalk_interfaces_lock);
	for (iface = atalk_interfaces; iface; iface = iface->next) {
		if ((node == ATADDR_BCAST ||
		     node == ATADDR_ANYNODE ||
		     iface->address.s_node == node) &&
		    iface->address.s_net == net &&
		    !(iface->status & ATIF_PROBE))
			break;

		/* XXXX.0 -- net.0 returns the iface associated with net */
		if (node == ATADDR_ANYNODE && net != ATADDR_ANYNET &&
		    ntohs(iface->nets.nr_firstnet) <= ntohs(net) &&
		    ntohs(net) <= ntohs(iface->nets.nr_lastnet))
416
			break;
L
Linus Torvalds 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430
	}
	read_unlock_bh(&atalk_interfaces_lock);
	return iface;
}


/*
 * Find a route for an AppleTalk packet. This ought to get cached in
 * the socket (later on...). We know about host routes and the fact
 * that a route must be direct to broadcast.
 */
static struct atalk_route *atrtr_find(struct atalk_addr *target)
{
	/*
431
	 * we must search through all routes unless we find a
L
Linus Torvalds 已提交
432 433 434 435 436
	 * host route, because some host routes might overlap
	 * network routes
	 */
	struct atalk_route *net_route = NULL;
	struct atalk_route *r;
437

L
Linus Torvalds 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
	read_lock_bh(&atalk_routes_lock);
	for (r = atalk_routes; r; r = r->next) {
		if (!(r->flags & RTF_UP))
			continue;

		if (r->target.s_net == target->s_net) {
			if (r->flags & RTF_HOST) {
				/*
				 * if this host route is for the target,
				 * the we're done
				 */
				if (r->target.s_node == target->s_node)
					goto out;
			} else
				/*
				 * this route will work if there isn't a
				 * direct host route, so cache it
				 */
				net_route = r;
		}
	}
459 460

	/*
L
Linus Torvalds 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
	 * if we found a network route but not a direct host
	 * route, then return it
	 */
	if (net_route)
		r = net_route;
	else if (atrtr_default.dev)
		r = &atrtr_default;
	else /* No route can be found */
		r = NULL;
out:
	read_unlock_bh(&atalk_routes_lock);
	return r;
}


/*
 * Given an AppleTalk network, find the device to use. This can be
 * a simple lookup.
 */
struct net_device *atrtr_get_dev(struct atalk_addr *sa)
{
	struct atalk_route *atr = atrtr_find(sa);
	return atr ? atr->dev : NULL;
}

/* Set up a default router */
static void atrtr_set_default(struct net_device *dev)
{
	atrtr_default.dev	     = dev;
	atrtr_default.flags	     = RTF_UP;
	atrtr_default.gateway.s_net  = htons(0);
	atrtr_default.gateway.s_node = 0;
}

/*
 * Add a router. Basically make sure it looks valid and stuff the
 * entry in the list. While it uses netranges we always set them to one
 * entry to work like netatalk.
 */
static int atrtr_create(struct rtentry *r, struct net_device *devhint)
{
	struct sockaddr_at *ta = (struct sockaddr_at *)&r->rt_dst;
	struct sockaddr_at *ga = (struct sockaddr_at *)&r->rt_gateway;
	struct atalk_route *rt;
	struct atalk_iface *iface, *riface;
	int retval = -EINVAL;

	/*
	 * Fixme: Raise/Lower a routing change semaphore for these
	 * operations.
	 */

	/* Validate the request */
	if (ta->sat_family != AF_APPLETALK ||
	    (!devhint && ga->sat_family != AF_APPLETALK))
		goto out;

	/* Now walk the routing table and make our decisions */
	write_lock_bh(&atalk_routes_lock);
	for (rt = atalk_routes; rt; rt = rt->next) {
		if (r->rt_flags != rt->flags)
			continue;

		if (ta->sat_addr.s_net == rt->target.s_net) {
			if (!(rt->flags & RTF_HOST))
				break;
			if (ta->sat_addr.s_node == rt->target.s_node)
				break;
		}
	}

	if (!devhint) {
		riface = NULL;

		read_lock_bh(&atalk_interfaces_lock);
		for (iface = atalk_interfaces; iface; iface = iface->next) {
			if (!riface &&
			    ntohs(ga->sat_addr.s_net) >=
539
					ntohs(iface->nets.nr_firstnet) &&
L
Linus Torvalds 已提交
540
			    ntohs(ga->sat_addr.s_net) <=
541
					ntohs(iface->nets.nr_lastnet))
L
Linus Torvalds 已提交
542 543 544 545 546
				riface = iface;

			if (ga->sat_addr.s_net == iface->address.s_net &&
			    ga->sat_addr.s_node == iface->address.s_node)
				riface = iface;
547
		}
L
Linus Torvalds 已提交
548 549 550 551 552 553 554 555 556 557
		read_unlock_bh(&atalk_interfaces_lock);

		retval = -ENETUNREACH;
		if (!riface)
			goto out_unlock;

		devhint = riface->dev;
	}

	if (!rt) {
558
		rt = kzalloc(sizeof(*rt), GFP_ATOMIC);
L
Linus Torvalds 已提交
559 560 561 562 563 564 565 566 567 568 569

		retval = -ENOBUFS;
		if (!rt)
			goto out_unlock;

		rt->next = atalk_routes;
		atalk_routes = rt;
	}

	/* Fill in the routing entry */
	rt->target  = ta->sat_addr;
570
	dev_hold(devhint);
L
Linus Torvalds 已提交
571 572 573 574 575 576 577 578 579 580 581 582
	rt->dev     = devhint;
	rt->flags   = r->rt_flags;
	rt->gateway = ga->sat_addr;

	retval = 0;
out_unlock:
	write_unlock_bh(&atalk_routes_lock);
out:
	return retval;
}

/* Delete a route. Find it and discard it */
583
static int atrtr_delete(struct atalk_addr *addr)
L
Linus Torvalds 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
{
	struct atalk_route **r = &atalk_routes;
	int retval = 0;
	struct atalk_route *tmp;

	write_lock_bh(&atalk_routes_lock);
	while ((tmp = *r) != NULL) {
		if (tmp->target.s_net == addr->s_net &&
		    (!(tmp->flags&RTF_GATEWAY) ||
		     tmp->target.s_node == addr->s_node)) {
			*r = tmp->next;
			dev_put(tmp->dev);
			kfree(tmp);
			goto out;
		}
		r = &tmp->next;
	}
	retval = -ENOENT;
out:
	write_unlock_bh(&atalk_routes_lock);
	return retval;
}

/*
 * Called when a device is downed. Just throw away any routes
 * via it.
 */
static void atrtr_device_down(struct net_device *dev)
{
	struct atalk_route **r = &atalk_routes;
	struct atalk_route *tmp;

	write_lock_bh(&atalk_routes_lock);
	while ((tmp = *r) != NULL) {
		if (tmp->dev == dev) {
			*r = tmp->next;
			dev_put(dev);
			kfree(tmp);
		} else
			r = &tmp->next;
	}
	write_unlock_bh(&atalk_routes_lock);

	if (atrtr_default.dev == dev)
		atrtr_set_default(NULL);
}

/* Actually down the interface */
static inline void atalk_dev_down(struct net_device *dev)
{
	atrtr_device_down(dev);	/* Remove all routes for the device */
	aarp_device_down(dev);	/* Remove AARP entries for the device */
	atif_drop_device(dev);	/* Remove the device */
}

/*
 * A device event has occurred. Watch for devices going down and
 * delete our use of them (iface and route).
 */
static int ddp_device_event(struct notifier_block *this, unsigned long event,
			    void *ptr)
{
646
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
647

648
	if (!net_eq(dev_net(dev), &init_net))
649 650
		return NOTIFY_DONE;

L
Linus Torvalds 已提交
651 652
	if (event == NETDEV_DOWN)
		/* Discard any use of this */
653
		atalk_dev_down(dev);
L
Linus Torvalds 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675

	return NOTIFY_DONE;
}

/* ioctl calls. Shouldn't even need touching */
/* Device configuration ioctl calls */
static int atif_ioctl(int cmd, void __user *arg)
{
	static char aarp_mcast[6] = { 0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF };
	struct ifreq atreq;
	struct atalk_netrange *nr;
	struct sockaddr_at *sa;
	struct net_device *dev;
	struct atalk_iface *atif;
	int ct;
	int limit;
	struct rtentry rtdef;
	int add_route;

	if (copy_from_user(&atreq, arg, sizeof(atreq)))
		return -EFAULT;

676
	dev = __dev_get_by_name(&init_net, atreq.ifr_name);
L
Linus Torvalds 已提交
677 678 679 680 681 682 683
	if (!dev)
		return -ENODEV;

	sa = (struct sockaddr_at *)&atreq.ifr_addr;
	atif = atalk_find_dev(dev);

	switch (cmd) {
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
	case SIOCSIFADDR:
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		if (sa->sat_family != AF_APPLETALK)
			return -EINVAL;
		if (dev->type != ARPHRD_ETHER &&
		    dev->type != ARPHRD_LOOPBACK &&
		    dev->type != ARPHRD_LOCALTLK &&
		    dev->type != ARPHRD_PPP)
			return -EPROTONOSUPPORT;

		nr = (struct atalk_netrange *)&sa->sat_zero[0];
		add_route = 1;

		/*
		 * if this is a point-to-point iface, and we already
		 * have an iface for this AppleTalk address, then we
		 * should not add a route
		 */
		if ((dev->flags & IFF_POINTOPOINT) &&
		    atalk_find_interface(sa->sat_addr.s_net,
					 sa->sat_addr.s_node)) {
			printk(KERN_DEBUG "AppleTalk: point-to-point "
			       "interface added with "
			       "existing address\n");
			add_route = 0;
		}
L
Linus Torvalds 已提交
711

712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
		/*
		 * Phase 1 is fine on LocalTalk but we don't do
		 * EtherTalk phase 1. Anyone wanting to add it go ahead.
		 */
		if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
			return -EPROTONOSUPPORT;
		if (sa->sat_addr.s_node == ATADDR_BCAST ||
		    sa->sat_addr.s_node == 254)
			return -EINVAL;
		if (atif) {
			/* Already setting address */
			if (atif->status & ATIF_PROBE)
				return -EBUSY;

			atif->address.s_net  = sa->sat_addr.s_net;
			atif->address.s_node = sa->sat_addr.s_node;
			atrtr_device_down(dev);	/* Flush old routes */
		} else {
			atif = atif_add_device(dev, &sa->sat_addr);
L
Linus Torvalds 已提交
731
			if (!atif)
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
				return -ENOMEM;
		}
		atif->nets = *nr;

		/*
		 * Check if the chosen address is used. If so we
		 * error and atalkd will try another.
		 */

		if (!(dev->flags & IFF_LOOPBACK) &&
		    !(dev->flags & IFF_POINTOPOINT) &&
		    atif_probe_device(atif) < 0) {
			atif_drop_device(dev);
			return -EADDRINUSE;
		}
L
Linus Torvalds 已提交
747

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
		/* Hey it worked - add the direct routes */
		sa = (struct sockaddr_at *)&rtdef.rt_gateway;
		sa->sat_family = AF_APPLETALK;
		sa->sat_addr.s_net  = atif->address.s_net;
		sa->sat_addr.s_node = atif->address.s_node;
		sa = (struct sockaddr_at *)&rtdef.rt_dst;
		rtdef.rt_flags = RTF_UP;
		sa->sat_family = AF_APPLETALK;
		sa->sat_addr.s_node = ATADDR_ANYNODE;
		if (dev->flags & IFF_LOOPBACK ||
		    dev->flags & IFF_POINTOPOINT)
			rtdef.rt_flags |= RTF_HOST;

		/* Routerless initial state */
		if (nr->nr_firstnet == htons(0) &&
		    nr->nr_lastnet == htons(0xFFFE)) {
L
Linus Torvalds 已提交
764
			sa->sat_addr.s_net = atif->address.s_net;
765 766 767 768 769 770 771
			atrtr_create(&rtdef, dev);
			atrtr_set_default(dev);
		} else {
			limit = ntohs(nr->nr_lastnet);
			if (limit - ntohs(nr->nr_firstnet) > 4096) {
				printk(KERN_WARNING "Too many routes/"
				       "iface.\n");
L
Linus Torvalds 已提交
772
				return -EINVAL;
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
			}
			if (add_route)
				for (ct = ntohs(nr->nr_firstnet);
				     ct <= limit; ct++) {
					sa->sat_addr.s_net = htons(ct);
					atrtr_create(&rtdef, dev);
				}
		}
		dev_mc_add_global(dev, aarp_mcast);
		return 0;

	case SIOCGIFADDR:
		if (!atif)
			return -EADDRNOTAVAIL;

		sa->sat_family = AF_APPLETALK;
		sa->sat_addr = atif->address;
		break;

	case SIOCGIFBRDADDR:
		if (!atif)
			return -EADDRNOTAVAIL;

		sa->sat_family = AF_APPLETALK;
		sa->sat_addr.s_net = atif->address.s_net;
		sa->sat_addr.s_node = ATADDR_BCAST;
		break;

	case SIOCATALKDIFADDR:
	case SIOCDIFADDR:
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		if (sa->sat_family != AF_APPLETALK)
			return -EINVAL;
		atalk_dev_down(dev);
		break;

	case SIOCSARP:
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		if (sa->sat_family != AF_APPLETALK)
			return -EINVAL;
		/*
		 * for now, we only support proxy AARP on ELAP;
		 * we should be able to do it for LocalTalk, too.
		 */
		if (dev->type != ARPHRD_ETHER)
			return -EPROTONOSUPPORT;

		/*
		 * atif points to the current interface on this network;
		 * we aren't concerned about its current status (at
		 * least for now), but it has all the settings about
		 * the network we're going to probe. Consequently, it
		 * must exist.
		 */
		if (!atif)
			return -EADDRNOTAVAIL;

		nr = (struct atalk_netrange *)&(atif->nets);
		/*
		 * Phase 1 is fine on Localtalk but we don't do
		 * Ethertalk phase 1. Anyone wanting to add it go ahead.
		 */
		if (dev->type == ARPHRD_ETHER && nr->nr_phase != 2)
			return -EPROTONOSUPPORT;

		if (sa->sat_addr.s_node == ATADDR_BCAST ||
		    sa->sat_addr.s_node == 254)
			return -EINVAL;

		/*
		 * Check if the chosen address is used. If so we
		 * error and ATCP will try another.
		 */
		if (atif_proxy_probe_device(atif, &(sa->sat_addr)) < 0)
			return -EADDRINUSE;

		/*
		 * We now have an address on the local network, and
		 * the AARP code will defend it for us until we take it
		 * down. We don't set up any routes right now, because
		 * ATCP will install them manually via SIOCADDRT.
		 */
		break;

	case SIOCDARP:
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		if (sa->sat_family != AF_APPLETALK)
			return -EINVAL;
		if (!atif)
			return -EADDRNOTAVAIL;

		/* give to aarp module to remove proxy entry */
		aarp_proxy_remove(atif->dev, &(sa->sat_addr));
		return 0;
L
Linus Torvalds 已提交
870 871 872 873 874 875 876 877 878 879 880 881 882 883
	}

	return copy_to_user(arg, &atreq, sizeof(atreq)) ? -EFAULT : 0;
}

/* Routing ioctl() calls */
static int atrtr_ioctl(unsigned int cmd, void __user *arg)
{
	struct rtentry rt;

	if (copy_from_user(&rt, arg, sizeof(rt)))
		return -EFAULT;

	switch (cmd) {
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
	case SIOCDELRT:
		if (rt.rt_dst.sa_family != AF_APPLETALK)
			return -EINVAL;
		return atrtr_delete(&((struct sockaddr_at *)
				      &rt.rt_dst)->sat_addr);

	case SIOCADDRT: {
		struct net_device *dev = NULL;
		if (rt.rt_dev) {
			char name[IFNAMSIZ];
			if (copy_from_user(name, rt.rt_dev, IFNAMSIZ-1))
				return -EFAULT;
			name[IFNAMSIZ-1] = '\0';
			dev = __dev_get_by_name(&init_net, name);
			if (!dev)
				return -ENODEV;
L
Linus Torvalds 已提交
900
		}
901 902
		return atrtr_create(&rt, dev);
	}
L
Linus Torvalds 已提交
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
	}
	return -EINVAL;
}

/**************************************************************************\
*                                                                          *
* Handling for system calls applied via the various interfaces to an       *
* AppleTalk socket object.                                                 *
*                                                                          *
\**************************************************************************/

/*
 * Checksum: This is 'optional'. It's quite likely also a good
 * candidate for assembler hackery 8)
 */
918
static unsigned long atalk_sum_partial(const unsigned char *data,
L
Linus Torvalds 已提交
919 920 921 922
				       int len, unsigned long sum)
{
	/* This ought to be unwrapped neatly. I'll trust gcc for now */
	while (len--) {
923 924
		sum += *data++;
		sum = rol16(sum, 1);
L
Linus Torvalds 已提交
925 926 927 928 929 930 931 932
	}
	return sum;
}

/*  Checksum skb data --  similar to skb_checksum  */
static unsigned long atalk_sum_skb(const struct sk_buff *skb, int offset,
				   int len, unsigned long sum)
{
933
	int start = skb_headlen(skb);
934
	struct sk_buff *frag_iter;
L
Linus Torvalds 已提交
935 936 937
	int i, copy;

	/* checksum stuff in header space */
938
	if ((copy = start - offset) > 0) {
L
Linus Torvalds 已提交
939 940 941
		if (copy > len)
			copy = len;
		sum = atalk_sum_partial(skb->data + offset, copy, sum);
942
		if ((len -= copy) == 0)
L
Linus Torvalds 已提交
943 944 945 946 947 948 949
			return sum;

		offset += copy;
	}

	/* checksum stuff in frags */
	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
950
		int end;
E
Eric Dumazet 已提交
951
		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
952
		WARN_ON(start > offset + len);
953

E
Eric Dumazet 已提交
954
		end = start + skb_frag_size(frag);
L
Linus Torvalds 已提交
955 956 957 958 959
		if ((copy = end - offset) > 0) {
			u8 *vaddr;

			if (copy > len)
				copy = len;
E
Eric Dumazet 已提交
960
			vaddr = kmap_atomic(skb_frag_page(frag));
961 962
			sum = atalk_sum_partial(vaddr + frag->page_offset +
						  offset - start, copy, sum);
E
Eric Dumazet 已提交
963
			kunmap_atomic(vaddr);
L
Linus Torvalds 已提交
964 965 966 967 968

			if (!(len -= copy))
				return sum;
			offset += copy;
		}
969
		start = end;
L
Linus Torvalds 已提交
970 971
	}

972 973
	skb_walk_frags(skb, frag_iter) {
		int end;
974

975
		WARN_ON(start > offset + len);
L
Linus Torvalds 已提交
976

977 978 979 980 981 982 983 984 985
		end = start + frag_iter->len;
		if ((copy = end - offset) > 0) {
			if (copy > len)
				copy = len;
			sum = atalk_sum_skb(frag_iter, offset - start,
					    copy, sum);
			if ((len -= copy) == 0)
				return sum;
			offset += copy;
L
Linus Torvalds 已提交
986
		}
987
		start = end;
L
Linus Torvalds 已提交
988 989 990 991 992 993 994
	}

	BUG_ON(len > 0);

	return sum;
}

A
Al Viro 已提交
995
static __be16 atalk_checksum(const struct sk_buff *skb, int len)
L
Linus Torvalds 已提交
996 997 998 999 1000 1001 1002
{
	unsigned long sum;

	/* skip header 4 bytes */
	sum = atalk_sum_skb(skb, 4, len-4, 0);

	/* Use 0xFFFF for 0. 0 itself means none */
A
Al Viro 已提交
1003
	return sum ? htons((unsigned short)sum) : htons(0xFFFF);
L
Linus Torvalds 已提交
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
}

static struct proto ddp_proto = {
	.name	  = "DDP",
	.owner	  = THIS_MODULE,
	.obj_size = sizeof(struct atalk_sock),
};

/*
 * Create a socket. Initialise the socket, blank the addresses
 * set the state.
 */
1016 1017
static int atalk_create(struct net *net, struct socket *sock, int protocol,
			int kern)
L
Linus Torvalds 已提交
1018 1019 1020 1021
{
	struct sock *sk;
	int rc = -ESOCKTNOSUPPORT;

O
Octavian Purdila 已提交
1022
	if (!net_eq(net, &init_net))
1023 1024
		return -EAFNOSUPPORT;

L
Linus Torvalds 已提交
1025 1026
	/*
	 * We permit SOCK_DGRAM and RAW is an extension. It is trivial to do
1027
	 * and gives you the full ELAP frame. Should be handy for CAP 8)
L
Linus Torvalds 已提交
1028 1029 1030 1031
	 */
	if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
		goto out;
	rc = -ENOMEM;
1032
	sk = sk_alloc(net, PF_APPLETALK, GFP_KERNEL, &ddp_proto, kern);
L
Linus Torvalds 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
	if (!sk)
		goto out;
	rc = 0;
	sock->ops = &atalk_dgram_ops;
	sock_init_data(sock, sk);

	/* Checksums on by default */
	sock_set_flag(sk, SOCK_ZAPPED);
out:
	return rc;
}

/* Free a socket. No work needed */
static int atalk_release(struct socket *sock)
{
	struct sock *sk = sock->sk;

	if (sk) {
1051 1052 1053
		sock_hold(sk);
		lock_sock(sk);

L
Linus Torvalds 已提交
1054 1055 1056
		sock_orphan(sk);
		sock->sk = NULL;
		atalk_destroy_socket(sk);
1057

1058 1059 1060
		release_sock(sk);
		sock_put(sk);
	}
L
Linus Torvalds 已提交
1061 1062 1063 1064 1065
	return 0;
}

/**
 * atalk_pick_and_bind_port - Pick a source port when one is not given
1066 1067
 * @sk: socket to insert into the tables
 * @sat: address to search for
L
Linus Torvalds 已提交
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
 *
 * Pick a source port when one is not given. If we can find a suitable free
 * one, we insert the socket into the tables using it.
 *
 * This whole operation must be atomic.
 */
static int atalk_pick_and_bind_port(struct sock *sk, struct sockaddr_at *sat)
{
	int retval;

	write_lock_bh(&atalk_sockets_lock);

	for (sat->sat_port = ATPORT_RESERVED;
	     sat->sat_port < ATPORT_LAST;
	     sat->sat_port++) {
		struct sock *s;

1085
		sk_for_each(s, &atalk_sockets) {
L
Linus Torvalds 已提交
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
			struct atalk_sock *at = at_sk(s);

			if (at->src_net == sat->sat_addr.s_net &&
			    at->src_node == sat->sat_addr.s_node &&
			    at->src_port == sat->sat_port)
				goto try_next_port;
		}

		/* Wheee, it's free, assign and insert. */
		__atalk_insert_socket(sk);
		at_sk(sk)->src_port = sat->sat_port;
		retval = 0;
		goto out;

try_next_port:;
	}

	retval = -EBUSY;
out:
	write_unlock_bh(&atalk_sockets_lock);
	return retval;
}

static int atalk_autobind(struct sock *sk)
{
	struct atalk_sock *at = at_sk(sk);
	struct sockaddr_at sat;
	struct atalk_addr *ap = atalk_find_primary();
	int n = -EADDRNOTAVAIL;

	if (!ap || ap->s_net == htons(ATADDR_ANYNET))
		goto out;

	at->src_net  = sat.sat_addr.s_net  = ap->s_net;
	at->src_node = sat.sat_addr.s_node = ap->s_node;

	n = atalk_pick_and_bind_port(sk, &sat);
	if (!n)
		sock_reset_flag(sk, SOCK_ZAPPED);
out:
	return n;
}

/* Set the address 'our end' of the connection */
static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
{
	struct sockaddr_at *addr = (struct sockaddr_at *)uaddr;
	struct sock *sk = sock->sk;
	struct atalk_sock *at = at_sk(sk);
1135
	int err;
L
Linus Torvalds 已提交
1136 1137 1138 1139 1140 1141 1142 1143

	if (!sock_flag(sk, SOCK_ZAPPED) ||
	    addr_len != sizeof(struct sockaddr_at))
		return -EINVAL;

	if (addr->sat_family != AF_APPLETALK)
		return -EAFNOSUPPORT;

A
Arnd Bergmann 已提交
1144
	lock_sock(sk);
L
Linus Torvalds 已提交
1145 1146 1147
	if (addr->sat_addr.s_net == htons(ATADDR_ANYNET)) {
		struct atalk_addr *ap = atalk_find_primary();

1148
		err = -EADDRNOTAVAIL;
L
Linus Torvalds 已提交
1149
		if (!ap)
1150
			goto out;
L
Linus Torvalds 已提交
1151 1152

		at->src_net  = addr->sat_addr.s_net = ap->s_net;
1153
		at->src_node = addr->sat_addr.s_node = ap->s_node;
L
Linus Torvalds 已提交
1154
	} else {
1155
		err = -EADDRNOTAVAIL;
L
Linus Torvalds 已提交
1156 1157
		if (!atalk_find_interface(addr->sat_addr.s_net,
					  addr->sat_addr.s_node))
1158
			goto out;
L
Linus Torvalds 已提交
1159 1160 1161 1162 1163 1164

		at->src_net  = addr->sat_addr.s_net;
		at->src_node = addr->sat_addr.s_node;
	}

	if (addr->sat_port == ATADDR_ANYPORT) {
1165
		err = atalk_pick_and_bind_port(sk, addr);
L
Linus Torvalds 已提交
1166

1167 1168
		if (err < 0)
			goto out;
L
Linus Torvalds 已提交
1169 1170 1171
	} else {
		at->src_port = addr->sat_port;

1172
		err = -EADDRINUSE;
L
Linus Torvalds 已提交
1173
		if (atalk_find_or_insert_socket(sk, addr))
1174
			goto out;
L
Linus Torvalds 已提交
1175 1176 1177
	}

	sock_reset_flag(sk, SOCK_ZAPPED);
1178 1179
	err = 0;
out:
A
Arnd Bergmann 已提交
1180
	release_sock(sk);
1181
	return err;
L
Linus Torvalds 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190
}

/* Set the address we talk to */
static int atalk_connect(struct socket *sock, struct sockaddr *uaddr,
			 int addr_len, int flags)
{
	struct sock *sk = sock->sk;
	struct atalk_sock *at = at_sk(sk);
	struct sockaddr_at *addr;
1191
	int err;
L
Linus Torvalds 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205

	sk->sk_state   = TCP_CLOSE;
	sock->state = SS_UNCONNECTED;

	if (addr_len != sizeof(*addr))
		return -EINVAL;

	addr = (struct sockaddr_at *)uaddr;

	if (addr->sat_family != AF_APPLETALK)
		return -EAFNOSUPPORT;

	if (addr->sat_addr.s_node == ATADDR_BCAST &&
	    !sock_flag(sk, SOCK_BROADCAST)) {
1206
#if 1
1207
		pr_warn("atalk_connect: %s is broken and did not set SO_BROADCAST.\n",
L
Linus Torvalds 已提交
1208 1209 1210
			current->comm);
#else
		return -EACCES;
1211
#endif
L
Linus Torvalds 已提交
1212 1213
	}

A
Arnd Bergmann 已提交
1214
	lock_sock(sk);
1215
	err = -EBUSY;
L
Linus Torvalds 已提交
1216 1217
	if (sock_flag(sk, SOCK_ZAPPED))
		if (atalk_autobind(sk) < 0)
1218
			goto out;
L
Linus Torvalds 已提交
1219

1220
	err = -ENETUNREACH;
L
Linus Torvalds 已提交
1221
	if (!atrtr_get_dev(&addr->sat_addr))
1222
		goto out;
L
Linus Torvalds 已提交
1223 1224 1225 1226 1227 1228 1229

	at->dest_port = addr->sat_port;
	at->dest_net  = addr->sat_addr.s_net;
	at->dest_node = addr->sat_addr.s_node;

	sock->state  = SS_CONNECTED;
	sk->sk_state = TCP_ESTABLISHED;
1230 1231
	err = 0;
out:
A
Arnd Bergmann 已提交
1232
	release_sock(sk);
1233
	return err;
L
Linus Torvalds 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
}

/*
 * Find the name of an AppleTalk socket. Just copy the right
 * fields into the sockaddr.
 */
static int atalk_getname(struct socket *sock, struct sockaddr *uaddr,
			 int *uaddr_len, int peer)
{
	struct sockaddr_at sat;
	struct sock *sk = sock->sk;
	struct atalk_sock *at = at_sk(sk);
1246
	int err;
L
Linus Torvalds 已提交
1247

A
Arnd Bergmann 已提交
1248
	lock_sock(sk);
1249
	err = -ENOBUFS;
L
Linus Torvalds 已提交
1250 1251
	if (sock_flag(sk, SOCK_ZAPPED))
		if (atalk_autobind(sk) < 0)
1252
			goto out;
L
Linus Torvalds 已提交
1253 1254

	*uaddr_len = sizeof(struct sockaddr_at);
1255
	memset(&sat, 0, sizeof(sat));
L
Linus Torvalds 已提交
1256 1257

	if (peer) {
1258
		err = -ENOTCONN;
L
Linus Torvalds 已提交
1259
		if (sk->sk_state != TCP_ESTABLISHED)
1260
			goto out;
L
Linus Torvalds 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270

		sat.sat_addr.s_net  = at->dest_net;
		sat.sat_addr.s_node = at->dest_node;
		sat.sat_port	    = at->dest_port;
	} else {
		sat.sat_addr.s_net  = at->src_net;
		sat.sat_addr.s_node = at->src_node;
		sat.sat_port	    = at->src_port;
	}

1271
	err = 0;
L
Linus Torvalds 已提交
1272 1273
	sat.sat_family = AF_APPLETALK;
	memcpy(uaddr, &sat, sizeof(sat));
1274 1275

out:
A
Arnd Bergmann 已提交
1276
	release_sock(sk);
1277
	return err;
L
Linus Torvalds 已提交
1278 1279
}

1280
#if IS_ENABLED(CONFIG_IPDDP)
L
Linus Torvalds 已提交
1281 1282
static __inline__ int is_ip_over_ddp(struct sk_buff *skb)
{
1283
	return skb->data[12] == 22;
L
Linus Torvalds 已提交
1284 1285 1286 1287
}

static int handle_ip_over_ddp(struct sk_buff *skb)
{
1288
	struct net_device *dev = __dev_get_by_name(&init_net, "ipddp0");
L
Linus Torvalds 已提交
1289 1290 1291
	struct net_device_stats *stats;

	/* This needs to be able to handle ipddp"N" devices */
1292 1293 1294 1295
	if (!dev) {
		kfree_skb(skb);
		return NET_RX_DROP;
	}
L
Linus Torvalds 已提交
1296

1297 1298 1299
	skb->protocol = htons(ETH_P_IP);
	skb_pull(skb, 13);
	skb->dev   = dev;
1300
	skb_reset_transport_header(skb);
L
Linus Torvalds 已提交
1301

1302
	stats = netdev_priv(dev);
1303 1304
	stats->rx_packets++;
	stats->rx_bytes += skb->len + 13;
1305
	return netif_rx(skb);  /* Send the SKB up to a higher place. */
L
Linus Torvalds 已提交
1306 1307 1308 1309 1310 1311 1312
}
#else
/* make it easy for gcc to optimize this test out, i.e. kill the code */
#define is_ip_over_ddp(skb) 0
#define handle_ip_over_ddp(skb) 0
#endif

1313 1314
static int atalk_route_packet(struct sk_buff *skb, struct net_device *dev,
			      struct ddpehdr *ddp, __u16 len_hops, int origlen)
L
Linus Torvalds 已提交
1315 1316 1317 1318 1319 1320
{
	struct atalk_route *rt;
	struct atalk_addr ta;

	/*
	 * Don't route multicast, etc., packets, or packets sent to "this
1321
	 * network"
L
Linus Torvalds 已提交
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
	 */
	if (skb->pkt_type != PACKET_HOST || !ddp->deh_dnet) {
		/*
		 * FIXME:
		 *
		 * Can it ever happen that a packet is from a PPP iface and
		 * needs to be broadcast onto the default network?
		 */
		if (dev->type == ARPHRD_PPP)
			printk(KERN_DEBUG "AppleTalk: didn't forward broadcast "
					  "packet received from PPP iface\n");
		goto free_it;
	}

	ta.s_net  = ddp->deh_dnet;
	ta.s_node = ddp->deh_dnode;

	/* Route the packet */
	rt = atrtr_find(&ta);
A
Al Viro 已提交
1341 1342 1343
	/* increment hops count */
	len_hops += 1 << 10;
	if (!rt || !(len_hops & (15 << 10)))
L
Linus Torvalds 已提交
1344
		goto free_it;
A
Al Viro 已提交
1345

L
Linus Torvalds 已提交
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
	/* FIXME: use skb->cb to be able to use shared skbs */

	/*
	 * Route goes through another gateway, so set the target to the
	 * gateway instead.
	 */

	if (rt->flags & RTF_GATEWAY) {
		ta.s_net  = rt->gateway.s_net;
		ta.s_node = rt->gateway.s_node;
	}

1358 1359
	/* Fix up skb->len field */
	skb_trim(skb, min_t(unsigned int, origlen,
L
Linus Torvalds 已提交
1360
			    (rt->dev->hard_header_len +
A
Al Viro 已提交
1361
			     ddp_dl->header_length + (len_hops & 1023))));
L
Linus Torvalds 已提交
1362 1363

	/* FIXME: use skb->cb to be able to use shared skbs */
A
Al Viro 已提交
1364
	ddp->deh_len_hops = htons(len_hops);
L
Linus Torvalds 已提交
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383

	/*
	 * Send the buffer onwards
	 *
	 * Now we must always be careful. If it's come from LocalTalk to
	 * EtherTalk it might not fit
	 *
	 * Order matters here: If a packet has to be copied to make a new
	 * headroom (rare hopefully) then it won't need unsharing.
	 *
	 * Note. ddp-> becomes invalid at the realloc.
	 */
	if (skb_headroom(skb) < 22) {
		/* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */
		struct sk_buff *nskb = skb_realloc_headroom(skb, 32);
		kfree_skb(skb);
		skb = nskb;
	} else
		skb = skb_unshare(skb, GFP_ATOMIC);
1384

L
Linus Torvalds 已提交
1385 1386 1387 1388
	/*
	 * If the buffer didn't vanish into the lack of space bitbucket we can
	 * send it.
	 */
1389 1390 1391 1392 1393
	if (skb == NULL)
		goto drop;

	if (aarp_send_ddp(rt->dev, skb, &ta, NULL) == NET_XMIT_DROP)
		return NET_RX_DROP;
1394
	return NET_RX_SUCCESS;
L
Linus Torvalds 已提交
1395 1396
free_it:
	kfree_skb(skb);
1397 1398
drop:
	return NET_RX_DROP;
L
Linus Torvalds 已提交
1399 1400 1401 1402 1403 1404 1405 1406 1407
}

/**
 *	atalk_rcv - Receive a packet (in skb) from device dev
 *	@skb - packet received
 *	@dev - network device where the packet comes from
 *	@pt - packet type
 *
 *	Receive a packet (in skb) from device dev. This has come from the SNAP
1408 1409 1410 1411
 *	decoder, and on entry skb->transport_header is the DDP header, skb->len
 *	is the DDP header, skb->len is the DDP length. The physical headers
 *	have been extracted. PPP should probably pass frames marked as for this
 *	layer.  [ie ARPHRD_ETHERTALK]
L
Linus Torvalds 已提交
1412 1413
 */
static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
D
David S. Miller 已提交
1414
		     struct packet_type *pt, struct net_device *orig_dev)
L
Linus Torvalds 已提交
1415 1416 1417 1418 1419
{
	struct ddpehdr *ddp;
	struct sock *sock;
	struct atalk_iface *atif;
	struct sockaddr_at tosat;
1420
	int origlen;
A
Al Viro 已提交
1421
	__u16 len_hops;
L
Linus Torvalds 已提交
1422

1423
	if (!net_eq(dev_net(dev), &init_net))
1424
		goto drop;
1425

L
Linus Torvalds 已提交
1426
	/* Don't mangle buffer if shared */
1427
	if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
L
Linus Torvalds 已提交
1428
		goto out;
1429

L
Linus Torvalds 已提交
1430 1431
	/* Size check and make sure header is contiguous */
	if (!pskb_may_pull(skb, sizeof(*ddp)))
1432
		goto drop;
L
Linus Torvalds 已提交
1433 1434 1435

	ddp = ddp_hdr(skb);

A
Al Viro 已提交
1436
	len_hops = ntohs(ddp->deh_len_hops);
L
Linus Torvalds 已提交
1437 1438 1439

	/* Trim buffer in case of stray trailing data */
	origlen = skb->len;
A
Al Viro 已提交
1440
	skb_trim(skb, min_t(unsigned int, skb->len, len_hops & 1023));
L
Linus Torvalds 已提交
1441 1442 1443 1444

	/*
	 * Size check to see if ddp->deh_len was crap
	 * (Otherwise we'll detonate most spectacularly
1445
	 * in the middle of atalk_checksum() or recvmsg()).
L
Linus Torvalds 已提交
1446
	 */
1447 1448 1449
	if (skb->len < sizeof(*ddp) || skb->len < (len_hops & 1023)) {
		pr_debug("AppleTalk: dropping corrupted frame (deh_len=%u, "
			 "skb->len=%u)\n", len_hops & 1023, skb->len);
1450
		goto drop;
1451
	}
L
Linus Torvalds 已提交
1452 1453 1454 1455 1456 1457

	/*
	 * Any checksums. Note we don't do htons() on this == is assumed to be
	 * valid for net byte orders all over the networking code...
	 */
	if (ddp->deh_sum &&
A
Al Viro 已提交
1458
	    atalk_checksum(skb, len_hops & 1023) != ddp->deh_sum)
L
Linus Torvalds 已提交
1459
		/* Not a valid AppleTalk frame - dustbin time */
1460
		goto drop;
L
Linus Torvalds 已提交
1461 1462 1463 1464 1465 1466 1467 1468

	/* Check the packet is aimed at us */
	if (!ddp->deh_dnet)	/* Net 0 is 'this network' */
		atif = atalk_find_anynet(ddp->deh_dnode, dev);
	else
		atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode);

	if (!atif) {
O
Oliver Dawid 已提交
1469 1470 1471
		/* Not ours, so we route the packet via the correct
		 * AppleTalk iface
		 */
1472
		return atalk_route_packet(skb, dev, ddp, len_hops, origlen);
L
Linus Torvalds 已提交
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
	}

	/* if IP over DDP is not selected this code will be optimized out */
	if (is_ip_over_ddp(skb))
		return handle_ip_over_ddp(skb);
	/*
	 * Which socket - atalk_search_socket() looks for a *full match*
	 * of the <net, node, port> tuple.
	 */
	tosat.sat_addr.s_net  = ddp->deh_dnet;
	tosat.sat_addr.s_node = ddp->deh_dnode;
	tosat.sat_port	      = ddp->deh_dport;

	sock = atalk_search_socket(&tosat, atif);
	if (!sock) /* But not one of our sockets */
1488
		goto drop;
L
Linus Torvalds 已提交
1489 1490 1491

	/* Queue packet (standard) */
	if (sock_queue_rcv_skb(sock, skb) < 0)
1492 1493 1494 1495 1496
		goto drop;

	return NET_RX_SUCCESS;

drop:
L
Linus Torvalds 已提交
1497
	kfree_skb(skb);
1498 1499 1500
out:
	return NET_RX_DROP;

L
Linus Torvalds 已提交
1501 1502 1503 1504 1505 1506 1507 1508
}

/*
 * Receive a LocalTalk frame. We make some demands on the caller here.
 * Caller must provide enough headroom on the packet to pull the short
 * header and append a long one.
 */
static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev,
D
David S. Miller 已提交
1509
		     struct packet_type *pt, struct net_device *orig_dev)
L
Linus Torvalds 已提交
1510
{
1511
	if (!net_eq(dev_net(dev), &init_net))
1512 1513
		goto freeit;

L
Linus Torvalds 已提交
1514
	/* Expand any short form frames */
1515
	if (skb_mac_header(skb)[2] == 1) {
L
Linus Torvalds 已提交
1516 1517 1518 1519
		struct ddpehdr *ddp;
		/* Find our address */
		struct atalk_addr *ap = atalk_find_dev_addr(dev);

A
Al Viro 已提交
1520
		if (!ap || skb->len < sizeof(__be16) || skb->len > 1023)
L
Linus Torvalds 已提交
1521 1522 1523
			goto freeit;

		/* Don't mangle buffer if shared */
1524
		if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
L
Linus Torvalds 已提交
1525 1526 1527 1528 1529 1530
			return 0;

		/*
		 * The push leaves us with a ddephdr not an shdr, and
		 * handily the port bytes in the right place preset.
		 */
1531
		ddp = skb_push(skb, sizeof(*ddp) - 4);
L
Linus Torvalds 已提交
1532 1533 1534

		/* Now fill in the long header */

1535 1536 1537 1538 1539
		/*
		 * These two first. The mac overlays the new source/dest
		 * network information so we MUST copy these before
		 * we write the network numbers !
		 */
L
Linus Torvalds 已提交
1540

1541 1542
		ddp->deh_dnode = skb_mac_header(skb)[0];     /* From physical header */
		ddp->deh_snode = skb_mac_header(skb)[1];     /* From physical header */
L
Linus Torvalds 已提交
1543 1544 1545 1546 1547 1548 1549

		ddp->deh_dnet  = ap->s_net;	/* Network number */
		ddp->deh_snet  = ap->s_net;
		ddp->deh_sum   = 0;		/* No checksum */
		/*
		 * Not sure about this bit...
		 */
A
Al Viro 已提交
1550 1551
		/* Non routable, so force a drop if we slip up later */
		ddp->deh_len_hops = htons(skb->len + (DDP_MAXHOPS << 10));
L
Linus Torvalds 已提交
1552
	}
1553
	skb_reset_transport_header(skb);
L
Linus Torvalds 已提交
1554

D
David S. Miller 已提交
1555
	return atalk_rcv(skb, dev, pt, orig_dev);
L
Linus Torvalds 已提交
1556 1557 1558 1559 1560
freeit:
	kfree_skb(skb);
	return 0;
}

1561
static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
L
Linus Torvalds 已提交
1562 1563 1564
{
	struct sock *sk = sock->sk;
	struct atalk_sock *at = at_sk(sk);
1565
	DECLARE_SOCKADDR(struct sockaddr_at *, usat, msg->msg_name);
L
Linus Torvalds 已提交
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
	int flags = msg->msg_flags;
	int loopback = 0;
	struct sockaddr_at local_satalk, gsat;
	struct sk_buff *skb;
	struct net_device *dev;
	struct ddpehdr *ddp;
	int size;
	struct atalk_route *rt;
	int err;

	if (flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
		return -EINVAL;

	if (len > DDP_MAXSZ)
		return -EMSGSIZE;

A
Arnd Bergmann 已提交
1582
	lock_sock(sk);
L
Linus Torvalds 已提交
1583
	if (usat) {
1584
		err = -EBUSY;
L
Linus Torvalds 已提交
1585 1586
		if (sock_flag(sk, SOCK_ZAPPED))
			if (atalk_autobind(sk) < 0)
1587
				goto out;
L
Linus Torvalds 已提交
1588

1589
		err = -EINVAL;
L
Linus Torvalds 已提交
1590 1591
		if (msg->msg_namelen < sizeof(*usat) ||
		    usat->sat_family != AF_APPLETALK)
1592
			goto out;
L
Linus Torvalds 已提交
1593

1594
		err = -EPERM;
1595
		/* netatalk didn't implement this check */
L
Linus Torvalds 已提交
1596 1597
		if (usat->sat_addr.s_node == ATADDR_BCAST &&
		    !sock_flag(sk, SOCK_BROADCAST)) {
1598
			goto out;
L
Linus Torvalds 已提交
1599 1600
		}
	} else {
1601
		err = -ENOTCONN;
L
Linus Torvalds 已提交
1602
		if (sk->sk_state != TCP_ESTABLISHED)
1603
			goto out;
L
Linus Torvalds 已提交
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
		usat = &local_satalk;
		usat->sat_family      = AF_APPLETALK;
		usat->sat_port	      = at->dest_port;
		usat->sat_addr.s_node = at->dest_node;
		usat->sat_addr.s_net  = at->dest_net;
	}

	/* Build a packet */
	SOCK_DEBUG(sk, "SK %p: Got address.\n", sk);

	/* For headers */
	size = sizeof(struct ddpehdr) + len + ddp_dl->header_length;

	if (usat->sat_addr.s_net || usat->sat_addr.s_node == ATADDR_ANYNODE) {
		rt = atrtr_find(&usat->sat_addr);
	} else {
		struct atalk_addr at_hint;

		at_hint.s_node = 0;
		at_hint.s_net  = at->src_net;

		rt = atrtr_find(&at_hint);
	}
1627
	err = -ENETUNREACH;
O
Oliver Dawid 已提交
1628
	if (!rt)
1629
		goto out;
O
Oliver Dawid 已提交
1630 1631

	dev = rt->dev;
L
Linus Torvalds 已提交
1632 1633 1634 1635 1636

	SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n",
			sk, size, dev->name);

	size += dev->hard_header_len;
A
Arnd Bergmann 已提交
1637
	release_sock(sk);
L
Linus Torvalds 已提交
1638
	skb = sock_alloc_send_skb(sk, size, (flags & MSG_DONTWAIT), &err);
A
Arnd Bergmann 已提交
1639
	lock_sock(sk);
L
Linus Torvalds 已提交
1640
	if (!skb)
1641
		goto out;
1642

L
Linus Torvalds 已提交
1643 1644 1645 1646 1647 1648
	skb_reserve(skb, ddp_dl->header_length);
	skb_reserve(skb, dev->hard_header_len);
	skb->dev = dev;

	SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk);

1649
	ddp = skb_put(skb, sizeof(struct ddpehdr));
A
Al Viro 已提交
1650
	ddp->deh_len_hops  = htons(len + sizeof(*ddp));
L
Linus Torvalds 已提交
1651 1652 1653 1654 1655 1656 1657
	ddp->deh_dnet  = usat->sat_addr.s_net;
	ddp->deh_snet  = at->src_net;
	ddp->deh_dnode = usat->sat_addr.s_node;
	ddp->deh_snode = at->src_node;
	ddp->deh_dport = usat->sat_port;
	ddp->deh_sport = at->src_port;

1658
	SOCK_DEBUG(sk, "SK %p: Copy user data (%zd bytes).\n", sk, len);
L
Linus Torvalds 已提交
1659

A
Al Viro 已提交
1660
	err = memcpy_from_msg(skb_put(skb, len), msg, len);
L
Linus Torvalds 已提交
1661 1662
	if (err) {
		kfree_skb(skb);
1663 1664
		err = -EFAULT;
		goto out;
L
Linus Torvalds 已提交
1665 1666
	}

1667
	if (sk->sk_no_check_tx)
L
Linus Torvalds 已提交
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
		ddp->deh_sum = 0;
	else
		ddp->deh_sum = atalk_checksum(skb, len + sizeof(*ddp));

	/*
	 * Loopback broadcast packets to non gateway targets (ie routes
	 * to group we are in)
	 */
	if (ddp->deh_dnode == ATADDR_BCAST &&
	    !(rt->flags & RTF_GATEWAY) && !(dev->flags & IFF_LOOPBACK)) {
		struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL);

		if (skb2) {
			loopback = 1;
			SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk);
1683 1684 1685 1686
			/*
			 * If it fails it is queued/sent above in the aarp queue
			 */
			aarp_send_ddp(dev, skb2, &usat->sat_addr, NULL);
L
Linus Torvalds 已提交
1687 1688 1689 1690 1691 1692 1693
		}
	}

	if (dev->flags & IFF_LOOPBACK || loopback) {
		SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk);
		/* loop back */
		skb_orphan(skb);
O
Oliver Dawid 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702
		if (ddp->deh_dnode == ATADDR_BCAST) {
			struct atalk_addr at_lo;

			at_lo.s_node = 0;
			at_lo.s_net  = 0;

			rt = atrtr_find(&at_lo);
			if (!rt) {
				kfree_skb(skb);
1703 1704
				err = -ENETUNREACH;
				goto out;
O
Oliver Dawid 已提交
1705 1706 1707 1708
			}
			dev = rt->dev;
			skb->dev = dev;
		}
L
Linus Torvalds 已提交
1709 1710 1711 1712 1713 1714 1715 1716
		ddp_dl->request(ddp_dl, skb, dev->dev_addr);
	} else {
		SOCK_DEBUG(sk, "SK %p: send out.\n", sk);
		if (rt->flags & RTF_GATEWAY) {
		    gsat.sat_addr = rt->gateway;
		    usat = &gsat;
		}

1717 1718 1719 1720
		/*
		 * If it fails it is queued/sent above in the aarp queue
		 */
		aarp_send_ddp(dev, skb, &usat->sat_addr, NULL);
L
Linus Torvalds 已提交
1721
	}
1722
	SOCK_DEBUG(sk, "SK %p: Done write (%zd).\n", sk, len);
L
Linus Torvalds 已提交
1723

1724
out:
A
Arnd Bergmann 已提交
1725
	release_sock(sk);
1726
	return err ? : len;
L
Linus Torvalds 已提交
1727 1728
}

1729 1730
static int atalk_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
			 int flags)
L
Linus Torvalds 已提交
1731 1732 1733 1734
{
	struct sock *sk = sock->sk;
	struct ddpehdr *ddp;
	int copied = 0;
A
Al Viro 已提交
1735
	int offset = 0;
L
Linus Torvalds 已提交
1736
	int err = 0;
1737 1738 1739
	struct sk_buff *skb;

	skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
L
Linus Torvalds 已提交
1740
						flags & MSG_DONTWAIT, &err);
A
Arnd Bergmann 已提交
1741 1742
	lock_sock(sk);

L
Linus Torvalds 已提交
1743
	if (!skb)
1744
		goto out;
L
Linus Torvalds 已提交
1745 1746 1747

	/* FIXME: use skb->cb to be able to use shared skbs */
	ddp = ddp_hdr(skb);
A
Al Viro 已提交
1748
	copied = ntohs(ddp->deh_len_hops) & 1023;
L
Linus Torvalds 已提交
1749

A
Al Viro 已提交
1750 1751 1752 1753
	if (sk->sk_type != SOCK_RAW) {
		offset = sizeof(*ddp);
		copied -= offset;
	}
L
Linus Torvalds 已提交
1754

A
Al Viro 已提交
1755 1756 1757
	if (copied > size) {
		copied = size;
		msg->msg_flags |= MSG_TRUNC;
L
Linus Torvalds 已提交
1758
	}
1759
	err = skb_copy_datagram_msg(skb, offset, msg, copied);
L
Linus Torvalds 已提交
1760

1761
	if (!err && msg->msg_name) {
1762
		DECLARE_SOCKADDR(struct sockaddr_at *, sat, msg->msg_name);
1763 1764 1765 1766 1767
		sat->sat_family      = AF_APPLETALK;
		sat->sat_port        = ddp->deh_sport;
		sat->sat_addr.s_node = ddp->deh_snode;
		sat->sat_addr.s_net  = ddp->deh_snet;
		msg->msg_namelen     = sizeof(*sat);
L
Linus Torvalds 已提交
1768 1769 1770
	}

	skb_free_datagram(sk, skb);	/* Free the datagram. */
1771 1772

out:
A
Arnd Bergmann 已提交
1773
	release_sock(sk);
L
Linus Torvalds 已提交
1774 1775 1776 1777 1778 1779 1780 1781 1782
	return err ? : copied;
}


/*
 * AppleTalk ioctl calls.
 */
static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
1783
	int rc = -ENOIOCTLCMD;
L
Linus Torvalds 已提交
1784 1785 1786 1787
	struct sock *sk = sock->sk;
	void __user *argp = (void __user *)arg;

	switch (cmd) {
1788 1789 1790
	/* Protocol layer */
	case TIOCOUTQ: {
		long amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
L
Linus Torvalds 已提交
1791

1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
		if (amount < 0)
			amount = 0;
		rc = put_user(amount, (int __user *)argp);
		break;
	}
	case TIOCINQ: {
		/*
		 * These two are safe on a single CPU system as only
		 * user tasks fiddle here
		 */
		struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
		long amount = 0;
L
Linus Torvalds 已提交
1804

1805
		if (skb)
1806
			amount = skb->len - sizeof(struct ddpehdr);
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
		rc = put_user(amount, (int __user *)argp);
		break;
	}
	case SIOCGSTAMP:
		rc = sock_get_timestamp(sk, argp);
		break;
	case SIOCGSTAMPNS:
		rc = sock_get_timestampns(sk, argp);
		break;
	/* Routing */
	case SIOCADDRT:
	case SIOCDELRT:
		rc = -EPERM;
		if (capable(CAP_NET_ADMIN))
			rc = atrtr_ioctl(cmd, argp);
		break;
	/* Interface */
	case SIOCGIFADDR:
	case SIOCSIFADDR:
	case SIOCGIFBRDADDR:
	case SIOCATALKDIFADDR:
	case SIOCDIFADDR:
	case SIOCSARP:		/* proxy AARP */
	case SIOCDARP:		/* proxy AARP */
		rtnl_lock();
		rc = atif_ioctl(cmd, argp);
		rtnl_unlock();
		break;
L
Linus Torvalds 已提交
1835 1836 1837 1838 1839
	}

	return rc;
}

1840 1841 1842 1843 1844

#ifdef CONFIG_COMPAT
static int atalk_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
	/*
1845 1846 1847 1848
	 * SIOCATALKDIFADDR is a SIOCPROTOPRIVATE ioctl number, so we
	 * cannot handle it in common code. The data we access if ifreq
	 * here is compatible, so we can simply call the native
	 * handler.
1849
	 */
1850 1851 1852
	if (cmd == SIOCATALKDIFADDR)
		return atalk_ioctl(sock, cmd, (unsigned long)compat_ptr(arg));

1853 1854 1855 1856 1857
	return -ENOIOCTLCMD;
}
#endif


1858
static const struct net_proto_family atalk_family_ops = {
L
Linus Torvalds 已提交
1859 1860 1861 1862 1863
	.family		= PF_APPLETALK,
	.create		= atalk_create,
	.owner		= THIS_MODULE,
};

1864
static const struct proto_ops atalk_dgram_ops = {
L
Linus Torvalds 已提交
1865 1866 1867 1868 1869 1870 1871 1872
	.family		= PF_APPLETALK,
	.owner		= THIS_MODULE,
	.release	= atalk_release,
	.bind		= atalk_bind,
	.connect	= atalk_connect,
	.socketpair	= sock_no_socketpair,
	.accept		= sock_no_accept,
	.getname	= atalk_getname,
A
Arnd Bergmann 已提交
1873
	.poll		= datagram_poll,
L
Linus Torvalds 已提交
1874
	.ioctl		= atalk_ioctl,
1875 1876 1877
#ifdef CONFIG_COMPAT
	.compat_ioctl	= atalk_compat_ioctl,
#endif
L
Linus Torvalds 已提交
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
	.listen		= sock_no_listen,
	.shutdown	= sock_no_shutdown,
	.setsockopt	= sock_no_setsockopt,
	.getsockopt	= sock_no_getsockopt,
	.sendmsg	= atalk_sendmsg,
	.recvmsg	= atalk_recvmsg,
	.mmap		= sock_no_mmap,
	.sendpage	= sock_no_sendpage,
};

static struct notifier_block ddp_notifier = {
	.notifier_call	= ddp_device_event,
};

1892
static struct packet_type ltalk_packet_type __read_mostly = {
1893
	.type		= cpu_to_be16(ETH_P_LOCALTALK),
L
Linus Torvalds 已提交
1894 1895 1896
	.func		= ltalk_rcv,
};

1897
static struct packet_type ppptalk_packet_type __read_mostly = {
1898
	.type		= cpu_to_be16(ETH_P_PPPTALK),
L
Linus Torvalds 已提交
1899 1900 1901 1902 1903 1904 1905 1906 1907
	.func		= atalk_rcv,
};

static unsigned char ddp_snap_id[] = { 0x08, 0x00, 0x07, 0x80, 0x9B };

/* Export symbols for use by drivers when AppleTalk is a module */
EXPORT_SYMBOL(atrtr_get_dev);
EXPORT_SYMBOL(atalk_find_dev_addr);

1908
static const char atalk_err_snap[] __initconst =
L
Linus Torvalds 已提交
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
	KERN_CRIT "Unable to register DDP with SNAP.\n";

/* Called by proto.c on kernel start up */
static int __init atalk_init(void)
{
	int rc = proto_register(&ddp_proto, 0);

	if (rc != 0)
		goto out;

	(void)sock_register(&atalk_family_ops);
	ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv);
	if (!ddp_dl)
		printk(atalk_err_snap);

	dev_add_pack(&ltalk_packet_type);
	dev_add_pack(&ppptalk_packet_type);

	register_netdevice_notifier(&ddp_notifier);
	aarp_proto_init();
	atalk_proc_init();
	atalk_register_sysctl();
out:
	return rc;
}
module_init(atalk_init);

/*
 * No explicit module reference count manipulation is needed in the
 * protocol. Socket layer sets module reference count for us
 * and interfaces reference counting is done
 * by the network device layer.
 *
 * Ergo, before the AppleTalk module can be removed, all AppleTalk
 * sockets be closed from user space.
 */
static void __exit atalk_exit(void)
{
#ifdef CONFIG_SYSCTL
	atalk_unregister_sysctl();
#endif /* CONFIG_SYSCTL */
	atalk_proc_exit();
	aarp_cleanup_module();	/* General aarp clean-up. */
	unregister_netdevice_notifier(&ddp_notifier);
	dev_remove_pack(&ltalk_packet_type);
	dev_remove_pack(&ppptalk_packet_type);
	unregister_snap_client(ddp_dl);
	sock_unregister(PF_APPLETALK);
	proto_unregister(&ddp_proto);
}
module_exit(atalk_exit);

MODULE_LICENSE("GPL");
1962
MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
L
Linus Torvalds 已提交
1963 1964
MODULE_DESCRIPTION("AppleTalk 0.20\n");
MODULE_ALIAS_NETPROTO(PF_APPLETALK);