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