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