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