1/*
2 * af_llc.c - LLC User Interface SAPs
3 * Description:
4 *   Functions in this module are implementation of socket based llc
5 *   communications for the Linux operating system. Support of llc class
6 *   one and class two is provided via SOCK_DGRAM and SOCK_STREAM
7 *   respectively.
8 *
9 *   An llc2 connection is (mac + sap), only one llc2 sap connection
10 *   is allowed per mac. Though one sap may have multiple mac + sap
11 *   connections.
12 *
13 * Copyright (c) 2001 by Jay Schulist <jschlst@samba.org>
14 *		 2002-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
15 *
16 * This program can be redistributed or modified under the terms of the
17 * GNU General Public License as published by the Free Software Foundation.
18 * This program is distributed without any warranty or implied warranty
19 * of merchantability or fitness for a particular purpose.
20 *
21 * See the GNU General Public License for more details.
22 */
23#include <linux/compiler.h>
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/rtnetlink.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <net/llc.h>
30#include <net/llc_sap.h>
31#include <net/llc_pdu.h>
32#include <net/llc_conn.h>
33#include <net/tcp_states.h>
34
35/* remember: uninitialized global data is zeroed because its in .bss */
36static u16 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
37static u16 llc_ui_sap_link_no_max[256];
38static struct sockaddr_llc llc_ui_addrnull;
39static const struct proto_ops llc_ui_ops;
40
41static int llc_ui_wait_for_conn(struct sock *sk, long timeout);
42static int llc_ui_wait_for_disc(struct sock *sk, long timeout);
43static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout);
44
45#if 0
46#define dprintk(args...) printk(KERN_DEBUG args)
47#else
48#define dprintk(args...)
49#endif
50
51/* Maybe we'll add some more in the future. */
52#define LLC_CMSG_PKTINFO	1
53
54
55/**
56 *	llc_ui_next_link_no - return the next unused link number for a sap
57 *	@sap: Address of sap to get link number from.
58 *
59 *	Return the next unused link number for a given sap.
60 */
61static inline u16 llc_ui_next_link_no(int sap)
62{
63	return llc_ui_sap_link_no_max[sap]++;
64}
65
66/**
67 *	llc_proto_type - return eth protocol for ARP header type
68 *	@arphrd: ARP header type.
69 *
70 *	Given an ARP header type return the corresponding ethernet protocol.
71 */
72static inline __be16 llc_proto_type(u16 arphrd)
73{
74	return htons(ETH_P_802_2);
75}
76
77/**
78 *	llc_ui_addr_null - determines if a address structure is null
79 *	@addr: Address to test if null.
80 */
81static inline u8 llc_ui_addr_null(struct sockaddr_llc *addr)
82{
83	return !memcmp(addr, &llc_ui_addrnull, sizeof(*addr));
84}
85
86/**
87 *	llc_ui_header_len - return length of llc header based on operation
88 *	@sk: Socket which contains a valid llc socket type.
89 *	@addr: Complete sockaddr_llc structure received from the user.
90 *
91 *	Provide the length of the llc header depending on what kind of
92 *	operation the user would like to perform and the type of socket.
93 *	Returns the correct llc header length.
94 */
95static inline u8 llc_ui_header_len(struct sock *sk, struct sockaddr_llc *addr)
96{
97	u8 rc = LLC_PDU_LEN_U;
98
99	if (addr->sllc_test || addr->sllc_xid)
100		rc = LLC_PDU_LEN_U;
101	else if (sk->sk_type == SOCK_STREAM)
102		rc = LLC_PDU_LEN_I;
103	return rc;
104}
105
106/**
107 *	llc_ui_send_data - send data via reliable llc2 connection
108 *	@sk: Connection the socket is using.
109 *	@skb: Data the user wishes to send.
110 *	@noblock: can we block waiting for data?
111 *
112 *	Send data via reliable llc2 connection.
113 *	Returns 0 upon success, non-zero if action did not succeed.
114 */
115static int llc_ui_send_data(struct sock* sk, struct sk_buff *skb, int noblock)
116{
117	struct llc_sock* llc = llc_sk(sk);
118	int rc = 0;
119
120	if (unlikely(llc_data_accept_state(llc->state) ||
121		     llc->remote_busy_flag ||
122		     llc->p_flag)) {
123		long timeout = sock_sndtimeo(sk, noblock);
124
125		rc = llc_ui_wait_for_busy_core(sk, timeout);
126	}
127	if (unlikely(!rc))
128		rc = llc_build_and_send_pkt(sk, skb);
129	return rc;
130}
131
132static void llc_ui_sk_init(struct socket *sock, struct sock *sk)
133{
134	sock_graft(sk, sock);
135	sk->sk_type	= sock->type;
136	sock->ops	= &llc_ui_ops;
137}
138
139static struct proto llc_proto = {
140	.name	  = "LLC",
141	.owner	  = THIS_MODULE,
142	.obj_size = sizeof(struct llc_sock),
143	.slab_flags = SLAB_DESTROY_BY_RCU,
144};
145
146/**
147 *	llc_ui_create - alloc and init a new llc_ui socket
148 *	@net: network namespace (must be default network)
149 *	@sock: Socket to initialize and attach allocated sk to.
150 *	@protocol: Unused.
151 *	@kern: on behalf of kernel or userspace
152 *
153 *	Allocate and initialize a new llc_ui socket, validate the user wants a
154 *	socket type we have available.
155 *	Returns 0 upon success, negative upon failure.
156 */
157static int llc_ui_create(struct net *net, struct socket *sock, int protocol,
158			 int kern)
159{
160	struct sock *sk;
161	int rc = -ESOCKTNOSUPPORT;
162
163	if (!ns_capable(net->user_ns, CAP_NET_RAW))
164		return -EPERM;
165
166	if (!net_eq(net, &init_net))
167		return -EAFNOSUPPORT;
168
169	if (likely(sock->type == SOCK_DGRAM || sock->type == SOCK_STREAM)) {
170		rc = -ENOMEM;
171		sk = llc_sk_alloc(net, PF_LLC, GFP_KERNEL, &llc_proto);
172		if (sk) {
173			rc = 0;
174			llc_ui_sk_init(sock, sk);
175		}
176	}
177	return rc;
178}
179
180/**
181 *	llc_ui_release - shutdown socket
182 *	@sock: Socket to release.
183 *
184 *	Shutdown and deallocate an existing socket.
185 */
186static int llc_ui_release(struct socket *sock)
187{
188	struct sock *sk = sock->sk;
189	struct llc_sock *llc;
190
191	if (unlikely(sk == NULL))
192		goto out;
193	sock_hold(sk);
194	lock_sock(sk);
195	llc = llc_sk(sk);
196	dprintk("%s: closing local(%02X) remote(%02X)\n", __func__,
197		llc->laddr.lsap, llc->daddr.lsap);
198	if (!llc_send_disc(sk))
199		llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
200	if (!sock_flag(sk, SOCK_ZAPPED))
201		llc_sap_remove_socket(llc->sap, sk);
202	release_sock(sk);
203	if (llc->dev)
204		dev_put(llc->dev);
205	sock_put(sk);
206	llc_sk_free(sk);
207out:
208	return 0;
209}
210
211/**
212 *	llc_ui_autoport - provide dynamically allocate SAP number
213 *
214 *	Provide the caller with a dynamically allocated SAP number according
215 *	to the rules that are set in this function. Returns: 0, upon failure,
216 *	SAP number otherwise.
217 */
218static int llc_ui_autoport(void)
219{
220	struct llc_sap *sap;
221	int i, tries = 0;
222
223	while (tries < LLC_SAP_DYN_TRIES) {
224		for (i = llc_ui_sap_last_autoport;
225		     i < LLC_SAP_DYN_STOP; i += 2) {
226			sap = llc_sap_find(i);
227			if (!sap) {
228				llc_ui_sap_last_autoport = i + 2;
229				goto out;
230			}
231			llc_sap_put(sap);
232		}
233		llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
234		tries++;
235	}
236	i = 0;
237out:
238	return i;
239}
240
241/**
242 *	llc_ui_autobind - automatically bind a socket to a sap
243 *	@sock: socket to bind
244 *	@addr: address to connect to
245 *
246 * 	Used by llc_ui_connect and llc_ui_sendmsg when the user hasn't
247 * 	specifically used llc_ui_bind to bind to an specific address/sap
248 *
249 *	Returns: 0 upon success, negative otherwise.
250 */
251static int llc_ui_autobind(struct socket *sock, struct sockaddr_llc *addr)
252{
253	struct sock *sk = sock->sk;
254	struct llc_sock *llc = llc_sk(sk);
255	struct llc_sap *sap;
256	int rc = -EINVAL;
257
258	if (!sock_flag(sk, SOCK_ZAPPED))
259		goto out;
260	rc = -ENODEV;
261	if (sk->sk_bound_dev_if) {
262		llc->dev = dev_get_by_index(&init_net, sk->sk_bound_dev_if);
263		if (llc->dev && addr->sllc_arphrd != llc->dev->type) {
264			dev_put(llc->dev);
265			llc->dev = NULL;
266		}
267	} else
268		llc->dev = dev_getfirstbyhwtype(&init_net, addr->sllc_arphrd);
269	if (!llc->dev)
270		goto out;
271	rc = -EUSERS;
272	llc->laddr.lsap = llc_ui_autoport();
273	if (!llc->laddr.lsap)
274		goto out;
275	rc = -EBUSY; /* some other network layer is using the sap */
276	sap = llc_sap_open(llc->laddr.lsap, NULL);
277	if (!sap)
278		goto out;
279	memcpy(llc->laddr.mac, llc->dev->dev_addr, IFHWADDRLEN);
280	memcpy(&llc->addr, addr, sizeof(llc->addr));
281	/* assign new connection to its SAP */
282	llc_sap_add_socket(sap, sk);
283	sock_reset_flag(sk, SOCK_ZAPPED);
284	rc = 0;
285out:
286	return rc;
287}
288
289/**
290 *	llc_ui_bind - bind a socket to a specific address.
291 *	@sock: Socket to bind an address to.
292 *	@uaddr: Address the user wants the socket bound to.
293 *	@addrlen: Length of the uaddr structure.
294 *
295 *	Bind a socket to a specific address. For llc a user is able to bind to
296 *	a specific sap only or mac + sap.
297 *	If the user desires to bind to a specific mac + sap, it is possible to
298 *	have multiple sap connections via multiple macs.
299 *	Bind and autobind for that matter must enforce the correct sap usage
300 *	otherwise all hell will break loose.
301 *	Returns: 0 upon success, negative otherwise.
302 */
303static int llc_ui_bind(struct socket *sock, struct sockaddr *uaddr, int addrlen)
304{
305	struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
306	struct sock *sk = sock->sk;
307	struct llc_sock *llc = llc_sk(sk);
308	struct llc_sap *sap;
309	int rc = -EINVAL;
310
311	dprintk("%s: binding %02X\n", __func__, addr->sllc_sap);
312	if (unlikely(!sock_flag(sk, SOCK_ZAPPED) || addrlen != sizeof(*addr)))
313		goto out;
314	rc = -EAFNOSUPPORT;
315	if (unlikely(addr->sllc_family != AF_LLC))
316		goto out;
317	rc = -ENODEV;
318	rcu_read_lock();
319	if (sk->sk_bound_dev_if) {
320		llc->dev = dev_get_by_index_rcu(&init_net, sk->sk_bound_dev_if);
321		if (llc->dev) {
322			if (!addr->sllc_arphrd)
323				addr->sllc_arphrd = llc->dev->type;
324			if (is_zero_ether_addr(addr->sllc_mac))
325				memcpy(addr->sllc_mac, llc->dev->dev_addr,
326				       IFHWADDRLEN);
327			if (addr->sllc_arphrd != llc->dev->type ||
328			    !ether_addr_equal(addr->sllc_mac,
329					      llc->dev->dev_addr)) {
330				rc = -EINVAL;
331				llc->dev = NULL;
332			}
333		}
334	} else
335		llc->dev = dev_getbyhwaddr_rcu(&init_net, addr->sllc_arphrd,
336					   addr->sllc_mac);
337	if (llc->dev)
338		dev_hold(llc->dev);
339	rcu_read_unlock();
340	if (!llc->dev)
341		goto out;
342	if (!addr->sllc_sap) {
343		rc = -EUSERS;
344		addr->sllc_sap = llc_ui_autoport();
345		if (!addr->sllc_sap)
346			goto out;
347	}
348	sap = llc_sap_find(addr->sllc_sap);
349	if (!sap) {
350		sap = llc_sap_open(addr->sllc_sap, NULL);
351		rc = -EBUSY; /* some other network layer is using the sap */
352		if (!sap)
353			goto out;
354	} else {
355		struct llc_addr laddr, daddr;
356		struct sock *ask;
357
358		memset(&laddr, 0, sizeof(laddr));
359		memset(&daddr, 0, sizeof(daddr));
360		/*
361		 * FIXME: check if the address is multicast,
362		 * 	  only SOCK_DGRAM can do this.
363		 */
364		memcpy(laddr.mac, addr->sllc_mac, IFHWADDRLEN);
365		laddr.lsap = addr->sllc_sap;
366		rc = -EADDRINUSE; /* mac + sap clash. */
367		ask = llc_lookup_established(sap, &daddr, &laddr);
368		if (ask) {
369			sock_put(ask);
370			goto out_put;
371		}
372	}
373	llc->laddr.lsap = addr->sllc_sap;
374	memcpy(llc->laddr.mac, addr->sllc_mac, IFHWADDRLEN);
375	memcpy(&llc->addr, addr, sizeof(llc->addr));
376	/* assign new connection to its SAP */
377	llc_sap_add_socket(sap, sk);
378	sock_reset_flag(sk, SOCK_ZAPPED);
379	rc = 0;
380out_put:
381	llc_sap_put(sap);
382out:
383	return rc;
384}
385
386/**
387 *	llc_ui_shutdown - shutdown a connect llc2 socket.
388 *	@sock: Socket to shutdown.
389 *	@how: What part of the socket to shutdown.
390 *
391 *	Shutdown a connected llc2 socket. Currently this function only supports
392 *	shutting down both sends and receives (2), we could probably make this
393 *	function such that a user can shutdown only half the connection but not
394 *	right now.
395 *	Returns: 0 upon success, negative otherwise.
396 */
397static int llc_ui_shutdown(struct socket *sock, int how)
398{
399	struct sock *sk = sock->sk;
400	int rc = -ENOTCONN;
401
402	lock_sock(sk);
403	if (unlikely(sk->sk_state != TCP_ESTABLISHED))
404		goto out;
405	rc = -EINVAL;
406	if (how != 2)
407		goto out;
408	rc = llc_send_disc(sk);
409	if (!rc)
410		rc = llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
411	/* Wake up anyone sleeping in poll */
412	sk->sk_state_change(sk);
413out:
414	release_sock(sk);
415	return rc;
416}
417
418/**
419 *	llc_ui_connect - Connect to a remote llc2 mac + sap.
420 *	@sock: Socket which will be connected to the remote destination.
421 *	@uaddr: Remote and possibly the local address of the new connection.
422 *	@addrlen: Size of uaddr structure.
423 *	@flags: Operational flags specified by the user.
424 *
425 *	Connect to a remote llc2 mac + sap. The caller must specify the
426 *	destination mac and address to connect to. If the user hasn't previously
427 *	called bind(2) with a smac the address of the first interface of the
428 *	specified arp type will be used.
429 *	This function will autobind if user did not previously call bind.
430 *	Returns: 0 upon success, negative otherwise.
431 */
432static int llc_ui_connect(struct socket *sock, struct sockaddr *uaddr,
433			  int addrlen, int flags)
434{
435	struct sock *sk = sock->sk;
436	struct llc_sock *llc = llc_sk(sk);
437	struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
438	int rc = -EINVAL;
439
440	lock_sock(sk);
441	if (unlikely(addrlen != sizeof(*addr)))
442		goto out;
443	rc = -EAFNOSUPPORT;
444	if (unlikely(addr->sllc_family != AF_LLC))
445		goto out;
446	if (unlikely(sk->sk_type != SOCK_STREAM))
447		goto out;
448	rc = -EALREADY;
449	if (unlikely(sock->state == SS_CONNECTING))
450		goto out;
451	/* bind connection to sap if user hasn't done it. */
452	if (sock_flag(sk, SOCK_ZAPPED)) {
453		/* bind to sap with null dev, exclusive */
454		rc = llc_ui_autobind(sock, addr);
455		if (rc)
456			goto out;
457	}
458	llc->daddr.lsap = addr->sllc_sap;
459	memcpy(llc->daddr.mac, addr->sllc_mac, IFHWADDRLEN);
460	sock->state = SS_CONNECTING;
461	sk->sk_state   = TCP_SYN_SENT;
462	llc->link   = llc_ui_next_link_no(llc->sap->laddr.lsap);
463	rc = llc_establish_connection(sk, llc->dev->dev_addr,
464				      addr->sllc_mac, addr->sllc_sap);
465	if (rc) {
466		dprintk("%s: llc_ui_send_conn failed :-(\n", __func__);
467		sock->state  = SS_UNCONNECTED;
468		sk->sk_state = TCP_CLOSE;
469		goto out;
470	}
471
472	if (sk->sk_state == TCP_SYN_SENT) {
473		const long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
474
475		if (!timeo || !llc_ui_wait_for_conn(sk, timeo))
476			goto out;
477
478		rc = sock_intr_errno(timeo);
479		if (signal_pending(current))
480			goto out;
481	}
482
483	if (sk->sk_state == TCP_CLOSE)
484		goto sock_error;
485
486	sock->state = SS_CONNECTED;
487	rc = 0;
488out:
489	release_sock(sk);
490	return rc;
491sock_error:
492	rc = sock_error(sk) ? : -ECONNABORTED;
493	sock->state = SS_UNCONNECTED;
494	goto out;
495}
496
497/**
498 *	llc_ui_listen - allow a normal socket to accept incoming connections
499 *	@sock: Socket to allow incoming connections on.
500 *	@backlog: Number of connections to queue.
501 *
502 *	Allow a normal socket to accept incoming connections.
503 *	Returns 0 upon success, negative otherwise.
504 */
505static int llc_ui_listen(struct socket *sock, int backlog)
506{
507	struct sock *sk = sock->sk;
508	int rc = -EINVAL;
509
510	lock_sock(sk);
511	if (unlikely(sock->state != SS_UNCONNECTED))
512		goto out;
513	rc = -EOPNOTSUPP;
514	if (unlikely(sk->sk_type != SOCK_STREAM))
515		goto out;
516	rc = -EAGAIN;
517	if (sock_flag(sk, SOCK_ZAPPED))
518		goto out;
519	rc = 0;
520	if (!(unsigned int)backlog)	/* BSDism */
521		backlog = 1;
522	sk->sk_max_ack_backlog = backlog;
523	if (sk->sk_state != TCP_LISTEN) {
524		sk->sk_ack_backlog = 0;
525		sk->sk_state	   = TCP_LISTEN;
526	}
527	sk->sk_socket->flags |= __SO_ACCEPTCON;
528out:
529	release_sock(sk);
530	return rc;
531}
532
533static int llc_ui_wait_for_disc(struct sock *sk, long timeout)
534{
535	DEFINE_WAIT(wait);
536	int rc = 0;
537
538	while (1) {
539		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
540		if (sk_wait_event(sk, &timeout, sk->sk_state == TCP_CLOSE))
541			break;
542		rc = -ERESTARTSYS;
543		if (signal_pending(current))
544			break;
545		rc = -EAGAIN;
546		if (!timeout)
547			break;
548		rc = 0;
549	}
550	finish_wait(sk_sleep(sk), &wait);
551	return rc;
552}
553
554static int llc_ui_wait_for_conn(struct sock *sk, long timeout)
555{
556	DEFINE_WAIT(wait);
557
558	while (1) {
559		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
560		if (sk_wait_event(sk, &timeout, sk->sk_state != TCP_SYN_SENT))
561			break;
562		if (signal_pending(current) || !timeout)
563			break;
564	}
565	finish_wait(sk_sleep(sk), &wait);
566	return timeout;
567}
568
569static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout)
570{
571	DEFINE_WAIT(wait);
572	struct llc_sock *llc = llc_sk(sk);
573	int rc;
574
575	while (1) {
576		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
577		rc = 0;
578		if (sk_wait_event(sk, &timeout,
579				  (sk->sk_shutdown & RCV_SHUTDOWN) ||
580				  (!llc_data_accept_state(llc->state) &&
581				   !llc->remote_busy_flag &&
582				   !llc->p_flag)))
583			break;
584		rc = -ERESTARTSYS;
585		if (signal_pending(current))
586			break;
587		rc = -EAGAIN;
588		if (!timeout)
589			break;
590	}
591	finish_wait(sk_sleep(sk), &wait);
592	return rc;
593}
594
595static int llc_wait_data(struct sock *sk, long timeo)
596{
597	int rc;
598
599	while (1) {
600		/*
601		 * POSIX 1003.1g mandates this order.
602		 */
603		rc = sock_error(sk);
604		if (rc)
605			break;
606		rc = 0;
607		if (sk->sk_shutdown & RCV_SHUTDOWN)
608			break;
609		rc = -EAGAIN;
610		if (!timeo)
611			break;
612		rc = sock_intr_errno(timeo);
613		if (signal_pending(current))
614			break;
615		rc = 0;
616		if (sk_wait_data(sk, &timeo))
617			break;
618	}
619	return rc;
620}
621
622static void llc_cmsg_rcv(struct msghdr *msg, struct sk_buff *skb)
623{
624	struct llc_sock *llc = llc_sk(skb->sk);
625
626	if (llc->cmsg_flags & LLC_CMSG_PKTINFO) {
627		struct llc_pktinfo info;
628
629		info.lpi_ifindex = llc_sk(skb->sk)->dev->ifindex;
630		llc_pdu_decode_dsap(skb, &info.lpi_sap);
631		llc_pdu_decode_da(skb, info.lpi_mac);
632		put_cmsg(msg, SOL_LLC, LLC_OPT_PKTINFO, sizeof(info), &info);
633	}
634}
635
636/**
637 *	llc_ui_accept - accept a new incoming connection.
638 *	@sock: Socket which connections arrive on.
639 *	@newsock: Socket to move incoming connection to.
640 *	@flags: User specified operational flags.
641 *
642 *	Accept a new incoming connection.
643 *	Returns 0 upon success, negative otherwise.
644 */
645static int llc_ui_accept(struct socket *sock, struct socket *newsock, int flags)
646{
647	struct sock *sk = sock->sk, *newsk;
648	struct llc_sock *llc, *newllc;
649	struct sk_buff *skb;
650	int rc = -EOPNOTSUPP;
651
652	dprintk("%s: accepting on %02X\n", __func__,
653		llc_sk(sk)->laddr.lsap);
654	lock_sock(sk);
655	if (unlikely(sk->sk_type != SOCK_STREAM))
656		goto out;
657	rc = -EINVAL;
658	if (unlikely(sock->state != SS_UNCONNECTED ||
659		     sk->sk_state != TCP_LISTEN))
660		goto out;
661	/* wait for a connection to arrive. */
662	if (skb_queue_empty(&sk->sk_receive_queue)) {
663		rc = llc_wait_data(sk, sk->sk_rcvtimeo);
664		if (rc)
665			goto out;
666	}
667	dprintk("%s: got a new connection on %02X\n", __func__,
668		llc_sk(sk)->laddr.lsap);
669	skb = skb_dequeue(&sk->sk_receive_queue);
670	rc = -EINVAL;
671	if (!skb->sk)
672		goto frees;
673	rc = 0;
674	newsk = skb->sk;
675	/* attach connection to a new socket. */
676	llc_ui_sk_init(newsock, newsk);
677	sock_reset_flag(newsk, SOCK_ZAPPED);
678	newsk->sk_state		= TCP_ESTABLISHED;
679	newsock->state		= SS_CONNECTED;
680	llc			= llc_sk(sk);
681	newllc			= llc_sk(newsk);
682	memcpy(&newllc->addr, &llc->addr, sizeof(newllc->addr));
683	newllc->link = llc_ui_next_link_no(newllc->laddr.lsap);
684
685	/* put original socket back into a clean listen state. */
686	sk->sk_state = TCP_LISTEN;
687	sk->sk_ack_backlog--;
688	dprintk("%s: ok success on %02X, client on %02X\n", __func__,
689		llc_sk(sk)->addr.sllc_sap, newllc->daddr.lsap);
690frees:
691	kfree_skb(skb);
692out:
693	release_sock(sk);
694	return rc;
695}
696
697/**
698 *	llc_ui_recvmsg - copy received data to the socket user.
699 *	@sock: Socket to copy data from.
700 *	@msg: Various user space related information.
701 *	@len: Size of user buffer.
702 *	@flags: User specified flags.
703 *
704 *	Copy received data to the socket user.
705 *	Returns non-negative upon success, negative otherwise.
706 */
707static int llc_ui_recvmsg(struct kiocb *iocb, struct socket *sock,
708			  struct msghdr *msg, size_t len, int flags)
709{
710	DECLARE_SOCKADDR(struct sockaddr_llc *, uaddr, msg->msg_name);
711	const int nonblock = flags & MSG_DONTWAIT;
712	struct sk_buff *skb = NULL;
713	struct sock *sk = sock->sk;
714	struct llc_sock *llc = llc_sk(sk);
715	unsigned long cpu_flags;
716	size_t copied = 0;
717	u32 peek_seq = 0;
718	u32 *seq, skb_len;
719	unsigned long used;
720	int target;	/* Read at least this many bytes */
721	long timeo;
722
723	lock_sock(sk);
724	copied = -ENOTCONN;
725	if (unlikely(sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN))
726		goto out;
727
728	timeo = sock_rcvtimeo(sk, nonblock);
729
730	seq = &llc->copied_seq;
731	if (flags & MSG_PEEK) {
732		peek_seq = llc->copied_seq;
733		seq = &peek_seq;
734	}
735
736	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
737	copied = 0;
738
739	do {
740		u32 offset;
741
742		/*
743		 * We need to check signals first, to get correct SIGURG
744		 * handling. FIXME: Need to check this doesn't impact 1003.1g
745		 * and move it down to the bottom of the loop
746		 */
747		if (signal_pending(current)) {
748			if (copied)
749				break;
750			copied = timeo ? sock_intr_errno(timeo) : -EAGAIN;
751			break;
752		}
753
754		/* Next get a buffer. */
755
756		skb = skb_peek(&sk->sk_receive_queue);
757		if (skb) {
758			offset = *seq;
759			goto found_ok_skb;
760		}
761		/* Well, if we have backlog, try to process it now yet. */
762
763		if (copied >= target && !sk->sk_backlog.tail)
764			break;
765
766		if (copied) {
767			if (sk->sk_err ||
768			    sk->sk_state == TCP_CLOSE ||
769			    (sk->sk_shutdown & RCV_SHUTDOWN) ||
770			    !timeo ||
771			    (flags & MSG_PEEK))
772				break;
773		} else {
774			if (sock_flag(sk, SOCK_DONE))
775				break;
776
777			if (sk->sk_err) {
778				copied = sock_error(sk);
779				break;
780			}
781			if (sk->sk_shutdown & RCV_SHUTDOWN)
782				break;
783
784			if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSE) {
785				if (!sock_flag(sk, SOCK_DONE)) {
786					/*
787					 * This occurs when user tries to read
788					 * from never connected socket.
789					 */
790					copied = -ENOTCONN;
791					break;
792				}
793				break;
794			}
795			if (!timeo) {
796				copied = -EAGAIN;
797				break;
798			}
799		}
800
801		if (copied >= target) { /* Do not sleep, just process backlog. */
802			release_sock(sk);
803			lock_sock(sk);
804		} else
805			sk_wait_data(sk, &timeo);
806
807		if ((flags & MSG_PEEK) && peek_seq != llc->copied_seq) {
808			net_dbg_ratelimited("LLC(%s:%d): Application bug, race in MSG_PEEK\n",
809					    current->comm,
810					    task_pid_nr(current));
811			peek_seq = llc->copied_seq;
812		}
813		continue;
814	found_ok_skb:
815		skb_len = skb->len;
816		/* Ok so how much can we use? */
817		used = skb->len - offset;
818		if (len < used)
819			used = len;
820
821		if (!(flags & MSG_TRUNC)) {
822			int rc = skb_copy_datagram_iovec(skb, offset,
823							 msg->msg_iov, used);
824			if (rc) {
825				/* Exception. Bailout! */
826				if (!copied)
827					copied = -EFAULT;
828				break;
829			}
830		}
831
832		*seq += used;
833		copied += used;
834		len -= used;
835
836		/* For non stream protcols we get one packet per recvmsg call */
837		if (sk->sk_type != SOCK_STREAM)
838			goto copy_uaddr;
839
840		if (!(flags & MSG_PEEK)) {
841			spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags);
842			sk_eat_skb(sk, skb);
843			spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags);
844			*seq = 0;
845		}
846
847		/* Partial read */
848		if (used + offset < skb_len)
849			continue;
850	} while (len > 0);
851
852out:
853	release_sock(sk);
854	return copied;
855copy_uaddr:
856	if (uaddr != NULL && skb != NULL) {
857		memcpy(uaddr, llc_ui_skb_cb(skb), sizeof(*uaddr));
858		msg->msg_namelen = sizeof(*uaddr);
859	}
860	if (llc_sk(sk)->cmsg_flags)
861		llc_cmsg_rcv(msg, skb);
862
863	if (!(flags & MSG_PEEK)) {
864		spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags);
865		sk_eat_skb(sk, skb);
866		spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags);
867		*seq = 0;
868	}
869
870	goto out;
871}
872
873/**
874 *	llc_ui_sendmsg - Transmit data provided by the socket user.
875 *	@sock: Socket to transmit data from.
876 *	@msg: Various user related information.
877 *	@len: Length of data to transmit.
878 *
879 *	Transmit data provided by the socket user.
880 *	Returns non-negative upon success, negative otherwise.
881 */
882static int llc_ui_sendmsg(struct kiocb *iocb, struct socket *sock,
883			  struct msghdr *msg, size_t len)
884{
885	struct sock *sk = sock->sk;
886	struct llc_sock *llc = llc_sk(sk);
887	DECLARE_SOCKADDR(struct sockaddr_llc *, addr, msg->msg_name);
888	int flags = msg->msg_flags;
889	int noblock = flags & MSG_DONTWAIT;
890	struct sk_buff *skb;
891	size_t size = 0;
892	int rc = -EINVAL, copied = 0, hdrlen;
893
894	dprintk("%s: sending from %02X to %02X\n", __func__,
895		llc->laddr.lsap, llc->daddr.lsap);
896	lock_sock(sk);
897	if (addr) {
898		if (msg->msg_namelen < sizeof(*addr))
899			goto release;
900	} else {
901		if (llc_ui_addr_null(&llc->addr))
902			goto release;
903		addr = &llc->addr;
904	}
905	/* must bind connection to sap if user hasn't done it. */
906	if (sock_flag(sk, SOCK_ZAPPED)) {
907		/* bind to sap with null dev, exclusive. */
908		rc = llc_ui_autobind(sock, addr);
909		if (rc)
910			goto release;
911	}
912	hdrlen = llc->dev->hard_header_len + llc_ui_header_len(sk, addr);
913	size = hdrlen + len;
914	if (size > llc->dev->mtu)
915		size = llc->dev->mtu;
916	copied = size - hdrlen;
917	release_sock(sk);
918	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
919	lock_sock(sk);
920	if (!skb)
921		goto release;
922	skb->dev      = llc->dev;
923	skb->protocol = llc_proto_type(addr->sllc_arphrd);
924	skb_reserve(skb, hdrlen);
925	rc = memcpy_fromiovec(skb_put(skb, copied), msg->msg_iov, copied);
926	if (rc)
927		goto out;
928	if (sk->sk_type == SOCK_DGRAM || addr->sllc_ua) {
929		llc_build_and_send_ui_pkt(llc->sap, skb, addr->sllc_mac,
930					  addr->sllc_sap);
931		goto out;
932	}
933	if (addr->sllc_test) {
934		llc_build_and_send_test_pkt(llc->sap, skb, addr->sllc_mac,
935					    addr->sllc_sap);
936		goto out;
937	}
938	if (addr->sllc_xid) {
939		llc_build_and_send_xid_pkt(llc->sap, skb, addr->sllc_mac,
940					   addr->sllc_sap);
941		goto out;
942	}
943	rc = -ENOPROTOOPT;
944	if (!(sk->sk_type == SOCK_STREAM && !addr->sllc_ua))
945		goto out;
946	rc = llc_ui_send_data(sk, skb, noblock);
947out:
948	if (rc) {
949		kfree_skb(skb);
950release:
951		dprintk("%s: failed sending from %02X to %02X: %d\n",
952			__func__, llc->laddr.lsap, llc->daddr.lsap, rc);
953	}
954	release_sock(sk);
955	return rc ? : copied;
956}
957
958/**
959 *	llc_ui_getname - return the address info of a socket
960 *	@sock: Socket to get address of.
961 *	@uaddr: Address structure to return information.
962 *	@uaddrlen: Length of address structure.
963 *	@peer: Does user want local or remote address information.
964 *
965 *	Return the address information of a socket.
966 */
967static int llc_ui_getname(struct socket *sock, struct sockaddr *uaddr,
968			  int *uaddrlen, int peer)
969{
970	struct sockaddr_llc sllc;
971	struct sock *sk = sock->sk;
972	struct llc_sock *llc = llc_sk(sk);
973	int rc = -EBADF;
974
975	memset(&sllc, 0, sizeof(sllc));
976	lock_sock(sk);
977	if (sock_flag(sk, SOCK_ZAPPED))
978		goto out;
979	*uaddrlen = sizeof(sllc);
980	if (peer) {
981		rc = -ENOTCONN;
982		if (sk->sk_state != TCP_ESTABLISHED)
983			goto out;
984		if(llc->dev)
985			sllc.sllc_arphrd = llc->dev->type;
986		sllc.sllc_sap = llc->daddr.lsap;
987		memcpy(&sllc.sllc_mac, &llc->daddr.mac, IFHWADDRLEN);
988	} else {
989		rc = -EINVAL;
990		if (!llc->sap)
991			goto out;
992		sllc.sllc_sap = llc->sap->laddr.lsap;
993
994		if (llc->dev) {
995			sllc.sllc_arphrd = llc->dev->type;
996			memcpy(&sllc.sllc_mac, llc->dev->dev_addr,
997			       IFHWADDRLEN);
998		}
999	}
1000	rc = 0;
1001	sllc.sllc_family = AF_LLC;
1002	memcpy(uaddr, &sllc, sizeof(sllc));
1003out:
1004	release_sock(sk);
1005	return rc;
1006}
1007
1008/**
1009 *	llc_ui_ioctl - io controls for PF_LLC
1010 *	@sock: Socket to get/set info
1011 *	@cmd: command
1012 *	@arg: optional argument for cmd
1013 *
1014 *	get/set info on llc sockets
1015 */
1016static int llc_ui_ioctl(struct socket *sock, unsigned int cmd,
1017			unsigned long arg)
1018{
1019	return -ENOIOCTLCMD;
1020}
1021
1022/**
1023 *	llc_ui_setsockopt - set various connection specific parameters.
1024 *	@sock: Socket to set options on.
1025 *	@level: Socket level user is requesting operations on.
1026 *	@optname: Operation name.
1027 *	@optval: User provided operation data.
1028 *	@optlen: Length of optval.
1029 *
1030 *	Set various connection specific parameters.
1031 */
1032static int llc_ui_setsockopt(struct socket *sock, int level, int optname,
1033			     char __user *optval, unsigned int optlen)
1034{
1035	struct sock *sk = sock->sk;
1036	struct llc_sock *llc = llc_sk(sk);
1037	unsigned int opt;
1038	int rc = -EINVAL;
1039
1040	lock_sock(sk);
1041	if (unlikely(level != SOL_LLC || optlen != sizeof(int)))
1042		goto out;
1043	rc = get_user(opt, (int __user *)optval);
1044	if (rc)
1045		goto out;
1046	rc = -EINVAL;
1047	switch (optname) {
1048	case LLC_OPT_RETRY:
1049		if (opt > LLC_OPT_MAX_RETRY)
1050			goto out;
1051		llc->n2 = opt;
1052		break;
1053	case LLC_OPT_SIZE:
1054		if (opt > LLC_OPT_MAX_SIZE)
1055			goto out;
1056		llc->n1 = opt;
1057		break;
1058	case LLC_OPT_ACK_TMR_EXP:
1059		if (opt > LLC_OPT_MAX_ACK_TMR_EXP)
1060			goto out;
1061		llc->ack_timer.expire = opt * HZ;
1062		break;
1063	case LLC_OPT_P_TMR_EXP:
1064		if (opt > LLC_OPT_MAX_P_TMR_EXP)
1065			goto out;
1066		llc->pf_cycle_timer.expire = opt * HZ;
1067		break;
1068	case LLC_OPT_REJ_TMR_EXP:
1069		if (opt > LLC_OPT_MAX_REJ_TMR_EXP)
1070			goto out;
1071		llc->rej_sent_timer.expire = opt * HZ;
1072		break;
1073	case LLC_OPT_BUSY_TMR_EXP:
1074		if (opt > LLC_OPT_MAX_BUSY_TMR_EXP)
1075			goto out;
1076		llc->busy_state_timer.expire = opt * HZ;
1077		break;
1078	case LLC_OPT_TX_WIN:
1079		if (opt > LLC_OPT_MAX_WIN)
1080			goto out;
1081		llc->k = opt;
1082		break;
1083	case LLC_OPT_RX_WIN:
1084		if (opt > LLC_OPT_MAX_WIN)
1085			goto out;
1086		llc->rw = opt;
1087		break;
1088	case LLC_OPT_PKTINFO:
1089		if (opt)
1090			llc->cmsg_flags |= LLC_CMSG_PKTINFO;
1091		else
1092			llc->cmsg_flags &= ~LLC_CMSG_PKTINFO;
1093		break;
1094	default:
1095		rc = -ENOPROTOOPT;
1096		goto out;
1097	}
1098	rc = 0;
1099out:
1100	release_sock(sk);
1101	return rc;
1102}
1103
1104/**
1105 *	llc_ui_getsockopt - get connection specific socket info
1106 *	@sock: Socket to get information from.
1107 *	@level: Socket level user is requesting operations on.
1108 *	@optname: Operation name.
1109 *	@optval: Variable to return operation data in.
1110 *	@optlen: Length of optval.
1111 *
1112 *	Get connection specific socket information.
1113 */
1114static int llc_ui_getsockopt(struct socket *sock, int level, int optname,
1115			     char __user *optval, int __user *optlen)
1116{
1117	struct sock *sk = sock->sk;
1118	struct llc_sock *llc = llc_sk(sk);
1119	int val = 0, len = 0, rc = -EINVAL;
1120
1121	lock_sock(sk);
1122	if (unlikely(level != SOL_LLC))
1123		goto out;
1124	rc = get_user(len, optlen);
1125	if (rc)
1126		goto out;
1127	rc = -EINVAL;
1128	if (len != sizeof(int))
1129		goto out;
1130	switch (optname) {
1131	case LLC_OPT_RETRY:
1132		val = llc->n2;					break;
1133	case LLC_OPT_SIZE:
1134		val = llc->n1;					break;
1135	case LLC_OPT_ACK_TMR_EXP:
1136		val = llc->ack_timer.expire / HZ;		break;
1137	case LLC_OPT_P_TMR_EXP:
1138		val = llc->pf_cycle_timer.expire / HZ;		break;
1139	case LLC_OPT_REJ_TMR_EXP:
1140		val = llc->rej_sent_timer.expire / HZ;		break;
1141	case LLC_OPT_BUSY_TMR_EXP:
1142		val = llc->busy_state_timer.expire / HZ;	break;
1143	case LLC_OPT_TX_WIN:
1144		val = llc->k;				break;
1145	case LLC_OPT_RX_WIN:
1146		val = llc->rw;				break;
1147	case LLC_OPT_PKTINFO:
1148		val = (llc->cmsg_flags & LLC_CMSG_PKTINFO) != 0;
1149		break;
1150	default:
1151		rc = -ENOPROTOOPT;
1152		goto out;
1153	}
1154	rc = 0;
1155	if (put_user(len, optlen) || copy_to_user(optval, &val, len))
1156		rc = -EFAULT;
1157out:
1158	release_sock(sk);
1159	return rc;
1160}
1161
1162static const struct net_proto_family llc_ui_family_ops = {
1163	.family = PF_LLC,
1164	.create = llc_ui_create,
1165	.owner	= THIS_MODULE,
1166};
1167
1168static const struct proto_ops llc_ui_ops = {
1169	.family	     = PF_LLC,
1170	.owner       = THIS_MODULE,
1171	.release     = llc_ui_release,
1172	.bind	     = llc_ui_bind,
1173	.connect     = llc_ui_connect,
1174	.socketpair  = sock_no_socketpair,
1175	.accept      = llc_ui_accept,
1176	.getname     = llc_ui_getname,
1177	.poll	     = datagram_poll,
1178	.ioctl       = llc_ui_ioctl,
1179	.listen      = llc_ui_listen,
1180	.shutdown    = llc_ui_shutdown,
1181	.setsockopt  = llc_ui_setsockopt,
1182	.getsockopt  = llc_ui_getsockopt,
1183	.sendmsg     = llc_ui_sendmsg,
1184	.recvmsg     = llc_ui_recvmsg,
1185	.mmap	     = sock_no_mmap,
1186	.sendpage    = sock_no_sendpage,
1187};
1188
1189static const char llc_proc_err_msg[] __initconst =
1190	KERN_CRIT "LLC: Unable to register the proc_fs entries\n";
1191static const char llc_sysctl_err_msg[] __initconst =
1192	KERN_CRIT "LLC: Unable to register the sysctl entries\n";
1193static const char llc_sock_err_msg[] __initconst =
1194	KERN_CRIT "LLC: Unable to register the network family\n";
1195
1196static int __init llc2_init(void)
1197{
1198	int rc = proto_register(&llc_proto, 0);
1199
1200	if (rc != 0)
1201		goto out;
1202
1203	llc_build_offset_table();
1204	llc_station_init();
1205	llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
1206	rc = llc_proc_init();
1207	if (rc != 0) {
1208		printk(llc_proc_err_msg);
1209		goto out_station;
1210	}
1211	rc = llc_sysctl_init();
1212	if (rc) {
1213		printk(llc_sysctl_err_msg);
1214		goto out_proc;
1215	}
1216	rc = sock_register(&llc_ui_family_ops);
1217	if (rc) {
1218		printk(llc_sock_err_msg);
1219		goto out_sysctl;
1220	}
1221	llc_add_pack(LLC_DEST_SAP, llc_sap_handler);
1222	llc_add_pack(LLC_DEST_CONN, llc_conn_handler);
1223out:
1224	return rc;
1225out_sysctl:
1226	llc_sysctl_exit();
1227out_proc:
1228	llc_proc_exit();
1229out_station:
1230	llc_station_exit();
1231	proto_unregister(&llc_proto);
1232	goto out;
1233}
1234
1235static void __exit llc2_exit(void)
1236{
1237	llc_station_exit();
1238	llc_remove_pack(LLC_DEST_SAP);
1239	llc_remove_pack(LLC_DEST_CONN);
1240	sock_unregister(PF_LLC);
1241	llc_proc_exit();
1242	llc_sysctl_exit();
1243	proto_unregister(&llc_proto);
1244}
1245
1246module_init(llc2_init);
1247module_exit(llc2_exit);
1248
1249MODULE_LICENSE("GPL");
1250MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
1251MODULE_DESCRIPTION("IEEE 802.2 PF_LLC support");
1252MODULE_ALIAS_NETPROTO(PF_LLC);
1253