xref: /linux/net/llc/af_llc.c (revision 54fb7f25f19a4539d3ec012e410439913650dc06)
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/config.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/init.h>
28 #include <net/llc.h>
29 #include <net/llc_sap.h>
30 #include <net/llc_pdu.h>
31 #include <net/llc_conn.h>
32 #include <net/tcp_states.h>
33 
34 /* remember: uninitialized global data is zeroed because its in .bss */
35 static u16 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
36 static u16 llc_ui_sap_link_no_max[256];
37 static struct sockaddr_llc llc_ui_addrnull;
38 static struct proto_ops llc_ui_ops;
39 
40 static int llc_ui_wait_for_conn(struct sock *sk, long timeout);
41 static int llc_ui_wait_for_disc(struct sock *sk, long timeout);
42 static int llc_ui_wait_for_data(struct sock *sk, long timeout);
43 static 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 /**
52  *	llc_ui_next_link_no - return the next unused link number for a sap
53  *	@sap: Address of sap to get link number from.
54  *
55  *	Return the next unused link number for a given sap.
56  */
57 static __inline__ u16 llc_ui_next_link_no(int sap)
58 {
59 	return llc_ui_sap_link_no_max[sap]++;
60 }
61 
62 /**
63  *	llc_proto_type - return eth protocol for ARP header type
64  *	@arphrd: ARP header type.
65  *
66  *	Given an ARP header type return the corresponding ethernet protocol.
67  */
68 static __inline__ u16 llc_proto_type(u16 arphrd)
69 {
70 	return arphrd == ARPHRD_IEEE802_TR ?
71 		         htons(ETH_P_TR_802_2) : htons(ETH_P_802_2);
72 }
73 
74 /**
75  *	llc_ui_addr_null - determines if a address structure is null
76  *	@addr: Address to test if null.
77  */
78 static __inline__ u8 llc_ui_addr_null(struct sockaddr_llc *addr)
79 {
80 	return !memcmp(addr, &llc_ui_addrnull, sizeof(*addr));
81 }
82 
83 /**
84  *	llc_ui_header_len - return length of llc header based on operation
85  *	@sk: Socket which contains a valid llc socket type.
86  *	@addr: Complete sockaddr_llc structure received from the user.
87  *
88  *	Provide the length of the llc header depending on what kind of
89  *	operation the user would like to perform and the type of socket.
90  *	Returns the correct llc header length.
91  */
92 static __inline__ u8 llc_ui_header_len(struct sock *sk,
93 				       struct sockaddr_llc *addr)
94 {
95 	u8 rc = LLC_PDU_LEN_U;
96 
97 	if (addr->sllc_test || addr->sllc_xid)
98 		rc = LLC_PDU_LEN_U;
99 	else if (sk->sk_type == SOCK_STREAM)
100 		rc = LLC_PDU_LEN_I;
101 	return rc;
102 }
103 
104 /**
105  *	llc_ui_send_data - send data via reliable llc2 connection
106  *	@sk: Connection the socket is using.
107  *	@skb: Data the user wishes to send.
108  *	@addr: Source and destination fields provided by the user.
109  *	@noblock: can we block waiting for data?
110  *
111  *	Send data via reliable llc2 connection.
112  *	Returns 0 upon success, non-zero if action did not succeed.
113  */
114 static int llc_ui_send_data(struct sock* sk, struct sk_buff *skb, int noblock)
115 {
116 	struct llc_sock* llc = llc_sk(sk);
117 	int rc = 0;
118 
119 	if (llc_data_accept_state(llc->state) || llc->p_flag) {
120 		long timeout = sock_sndtimeo(sk, noblock);
121 
122 		rc = llc_ui_wait_for_busy_core(sk, timeout);
123 	}
124 	if (!rc)
125 		rc = llc_build_and_send_pkt(sk, skb);
126 	return rc;
127 }
128 
129 static void llc_ui_sk_init(struct socket *sock, struct sock *sk)
130 {
131 	sk->sk_type	= sock->type;
132 	sk->sk_sleep	= &sock->wait;
133 	sk->sk_socket	= sock;
134 	sock->sk	= sk;
135 	sock->ops	= &llc_ui_ops;
136 }
137 
138 static struct proto llc_proto = {
139 	.name	  = "DDP",
140 	.owner	  = THIS_MODULE,
141 	.obj_size = sizeof(struct llc_sock),
142 };
143 
144 /**
145  *	llc_ui_create - alloc and init a new llc_ui socket
146  *	@sock: Socket to initialize and attach allocated sk to.
147  *	@protocol: Unused.
148  *
149  *	Allocate and initialize a new llc_ui socket, validate the user wants a
150  *	socket type we have available.
151  *	Returns 0 upon success, negative upon failure.
152  */
153 static int llc_ui_create(struct socket *sock, int protocol)
154 {
155 	struct sock *sk;
156 	int rc = -ESOCKTNOSUPPORT;
157 
158 	if (likely(sock->type == SOCK_DGRAM || sock->type == SOCK_STREAM)) {
159 		rc = -ENOMEM;
160 		sk = llc_sk_alloc(PF_LLC, GFP_KERNEL, &llc_proto);
161 		if (sk) {
162 			rc = 0;
163 			llc_ui_sk_init(sock, sk);
164 		}
165 	}
166 	return rc;
167 }
168 
169 /**
170  *	llc_ui_release - shutdown socket
171  *	@sock: Socket to release.
172  *
173  *	Shutdown and deallocate an existing socket.
174  */
175 static int llc_ui_release(struct socket *sock)
176 {
177 	struct sock *sk = sock->sk;
178 	struct llc_sock *llc;
179 
180 	if (unlikely(sk == NULL))
181 		goto out;
182 	sock_hold(sk);
183 	lock_sock(sk);
184 	llc = llc_sk(sk);
185 	dprintk("%s: closing local(%02X) remote(%02X)\n", __FUNCTION__,
186 		llc->laddr.lsap, llc->daddr.lsap);
187 	if (!llc_send_disc(sk))
188 		llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
189 	if (!sock_flag(sk, SOCK_ZAPPED))
190 		llc_sap_remove_socket(llc->sap, sk);
191 	release_sock(sk);
192 	if (llc->sap && hlist_empty(&llc->sap->sk_list.list)) {
193 		llc_release_sockets(llc->sap);
194 		llc_sap_close(llc->sap);
195 	}
196 	if (llc->dev)
197 		dev_put(llc->dev);
198 	sock_put(sk);
199 	llc_sk_free(sk);
200 out:
201 	return 0;
202 }
203 
204 /**
205  *	llc_ui_autoport - provide dynamically allocate SAP number
206  *
207  *	Provide the caller with a dynamically allocated SAP number according
208  *	to the rules that are set in this function. Returns: 0, upon failure,
209  *	SAP number otherwise.
210  */
211 static int llc_ui_autoport(void)
212 {
213 	struct llc_sap *sap;
214 	int i, tries = 0;
215 
216 	while (tries < LLC_SAP_DYN_TRIES) {
217 		for (i = llc_ui_sap_last_autoport;
218 		     i < LLC_SAP_DYN_STOP; i += 2) {
219 			sap = llc_sap_find(i);
220 			if (!sap) {
221 				llc_ui_sap_last_autoport = i + 2;
222 				goto out;
223 			}
224 		}
225 		llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
226 		tries++;
227 	}
228 	i = 0;
229 out:
230 	return i;
231 }
232 
233 /**
234  *	llc_ui_autobind - automatically bind a socket to a sap
235  *	@sock: socket to bind
236  *	@addr: address to connect to
237  *
238  * 	Used by llc_ui_connect and llc_ui_sendmsg when the user hasn't
239  * 	specifically used llc_ui_bind to bind to an specific address/sap
240  *
241  *	Returns: 0 upon success, negative otherwise.
242  */
243 static int llc_ui_autobind(struct socket *sock, struct sockaddr_llc *addr)
244 {
245 	struct sock *sk = sock->sk;
246 	struct llc_sock *llc = llc_sk(sk);
247 	struct llc_sap *sap;
248 	int rc = -EINVAL;
249 
250 	if (!sock_flag(sk, SOCK_ZAPPED))
251 		goto out;
252 	rc = -ENODEV;
253 	llc->dev = dev_getfirstbyhwtype(addr->sllc_arphrd);
254 	if (!llc->dev)
255 		goto out;
256 	rc = -EUSERS;
257 	llc->laddr.lsap = llc_ui_autoport();
258 	if (!llc->laddr.lsap)
259 		goto out;
260 	rc = -EBUSY; /* some other network layer is using the sap */
261 	sap = llc_sap_open(llc->laddr.lsap, NULL);
262 	if (!sap)
263 		goto out;
264 	memcpy(llc->laddr.mac, llc->dev->dev_addr, IFHWADDRLEN);
265 	memcpy(&llc->addr, addr, sizeof(llc->addr));
266 	/* assign new connection to its SAP */
267 	llc_sap_add_socket(sap, sk);
268 	sock_reset_flag(sk, SOCK_ZAPPED);
269 	rc = 0;
270 out:
271 	return rc;
272 }
273 
274 /**
275  *	llc_ui_bind - bind a socket to a specific address.
276  *	@sock: Socket to bind an address to.
277  *	@uaddr: Address the user wants the socket bound to.
278  *	@addrlen: Length of the uaddr structure.
279  *
280  *	Bind a socket to a specific address. For llc a user is able to bind to
281  *	a specific sap only or mac + sap.
282  *	If the user desires to bind to a specific mac + sap, it is possible to
283  *	have multiple sap connections via multiple macs.
284  *	Bind and autobind for that matter must enforce the correct sap usage
285  *	otherwise all hell will break loose.
286  *	Returns: 0 upon success, negative otherwise.
287  */
288 static int llc_ui_bind(struct socket *sock, struct sockaddr *uaddr, int addrlen)
289 {
290 	struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
291 	struct sock *sk = sock->sk;
292 	struct llc_sock *llc = llc_sk(sk);
293 	struct llc_sap *sap;
294 	int rc = -EINVAL;
295 
296 	dprintk("%s: binding %02X\n", __FUNCTION__, addr->sllc_sap);
297 	if (unlikely(!sock_flag(sk, SOCK_ZAPPED) || addrlen != sizeof(*addr)))
298 		goto out;
299 	rc = -EAFNOSUPPORT;
300 	if (unlikely(addr->sllc_family != AF_LLC))
301 		goto out;
302 	if (!addr->sllc_sap) {
303 		rc = -EUSERS;
304 		addr->sllc_sap = llc_ui_autoport();
305 		if (!addr->sllc_sap)
306 			goto out;
307 	}
308 	sap = llc_sap_find(addr->sllc_sap);
309 	if (!sap) {
310 		sap = llc_sap_open(addr->sllc_sap, NULL);
311 		rc = -EBUSY; /* some other network layer is using the sap */
312 		if (!sap)
313 			goto out;
314 	} else {
315 		struct llc_addr laddr, daddr;
316 		struct sock *ask;
317 
318 		memset(&laddr, 0, sizeof(laddr));
319 		memset(&daddr, 0, sizeof(daddr));
320 		/*
321 		 * FIXME: check if the the address is multicast,
322 		 * 	  only SOCK_DGRAM can do this.
323 		 */
324 		memcpy(laddr.mac, addr->sllc_mac, IFHWADDRLEN);
325 		laddr.lsap = addr->sllc_sap;
326 		rc = -EADDRINUSE; /* mac + sap clash. */
327 		ask = llc_lookup_established(sap, &daddr, &laddr);
328 		if (ask) {
329 			sock_put(ask);
330 			goto out;
331 		}
332 	}
333 	llc->laddr.lsap = addr->sllc_sap;
334 	memcpy(llc->laddr.mac, addr->sllc_mac, IFHWADDRLEN);
335 	memcpy(&llc->addr, addr, sizeof(llc->addr));
336 	/* assign new connection to its SAP */
337 	llc_sap_add_socket(sap, sk);
338 	sock_reset_flag(sk, SOCK_ZAPPED);
339 	rc = 0;
340 out:
341 	return rc;
342 }
343 
344 /**
345  *	llc_ui_shutdown - shutdown a connect llc2 socket.
346  *	@sock: Socket to shutdown.
347  *	@how: What part of the socket to shutdown.
348  *
349  *	Shutdown a connected llc2 socket. Currently this function only supports
350  *	shutting down both sends and receives (2), we could probably make this
351  *	function such that a user can shutdown only half the connection but not
352  *	right now.
353  *	Returns: 0 upon success, negative otherwise.
354  */
355 static int llc_ui_shutdown(struct socket *sock, int how)
356 {
357 	struct sock *sk = sock->sk;
358 	int rc = -ENOTCONN;
359 
360 	lock_sock(sk);
361 	if (unlikely(sk->sk_state != TCP_ESTABLISHED))
362 		goto out;
363 	rc = -EINVAL;
364 	if (how != 2)
365 		goto out;
366 	rc = llc_send_disc(sk);
367 	if (!rc)
368 		rc = llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
369 	/* Wake up anyone sleeping in poll */
370 	sk->sk_state_change(sk);
371 out:
372 	release_sock(sk);
373 	return rc;
374 }
375 
376 /**
377  *	llc_ui_connect - Connect to a remote llc2 mac + sap.
378  *	@sock: Socket which will be connected to the remote destination.
379  *	@uaddr: Remote and possibly the local address of the new connection.
380  *	@addrlen: Size of uaddr structure.
381  *	@flags: Operational flags specified by the user.
382  *
383  *	Connect to a remote llc2 mac + sap. The caller must specify the
384  *	destination mac and address to connect to. If the user hasn't previously
385  *	called bind(2) with a smac the address of the first interface of the
386  *	specified arp type will be used.
387  *	This function will autobind if user did not previously call bind.
388  *	Returns: 0 upon success, negative otherwise.
389  */
390 static int llc_ui_connect(struct socket *sock, struct sockaddr *uaddr,
391 			  int addrlen, int flags)
392 {
393 	struct sock *sk = sock->sk;
394 	struct llc_sock *llc = llc_sk(sk);
395 	struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
396 	int rc = -EINVAL;
397 
398 	lock_sock(sk);
399 	if (unlikely(addrlen != sizeof(*addr)))
400 		goto out;
401 	rc = -EAFNOSUPPORT;
402 	if (unlikely(addr->sllc_family != AF_LLC))
403 		goto out;
404 	if (unlikely(sk->sk_type != SOCK_STREAM))
405 		goto out;
406 	rc = -EALREADY;
407 	if (unlikely(sock->state == SS_CONNECTING))
408 		goto out;
409 	/* bind connection to sap if user hasn't done it. */
410 	if (sock_flag(sk, SOCK_ZAPPED)) {
411 		/* bind to sap with null dev, exclusive */
412 		rc = llc_ui_autobind(sock, addr);
413 		if (rc)
414 			goto out;
415 		llc->daddr.lsap = addr->sllc_sap;
416 		memcpy(llc->daddr.mac, addr->sllc_mac, IFHWADDRLEN);
417 	}
418 	sock->state = SS_CONNECTING;
419 	sk->sk_state   = TCP_SYN_SENT;
420 	llc->link   = llc_ui_next_link_no(llc->sap->laddr.lsap);
421 	rc = llc_establish_connection(sk, llc->dev->dev_addr,
422 				      addr->sllc_mac, addr->sllc_sap);
423 	if (rc) {
424 		dprintk("%s: llc_ui_send_conn failed :-(\n", __FUNCTION__);
425 		sock->state  = SS_UNCONNECTED;
426 		sk->sk_state = TCP_CLOSE;
427 		goto out;
428 	}
429 
430 	if (sk->sk_state == TCP_SYN_SENT) {
431 		const long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
432 
433 		if (!timeo || !llc_ui_wait_for_conn(sk, timeo))
434 			goto out;
435 
436 		rc = sock_intr_errno(timeo);
437 		if (signal_pending(current))
438 			goto out;
439 	}
440 
441 	if (sk->sk_state == TCP_CLOSE)
442 		goto sock_error;
443 
444 	sock->state = SS_CONNECTED;
445 	rc = 0;
446 out:
447 	release_sock(sk);
448 	return rc;
449 sock_error:
450 	rc = sock_error(sk) ? : -ECONNABORTED;
451 	sock->state = SS_UNCONNECTED;
452 	goto out;
453 }
454 
455 /**
456  *	llc_ui_listen - allow a normal socket to accept incoming connections
457  *	@sock: Socket to allow incoming connections on.
458  *	@backlog: Number of connections to queue.
459  *
460  *	Allow a normal socket to accept incoming connections.
461  *	Returns 0 upon success, negative otherwise.
462  */
463 static int llc_ui_listen(struct socket *sock, int backlog)
464 {
465 	struct sock *sk = sock->sk;
466 	int rc = -EINVAL;
467 
468 	lock_sock(sk);
469 	if (unlikely(sock->state != SS_UNCONNECTED))
470 		goto out;
471 	rc = -EOPNOTSUPP;
472 	if (unlikely(sk->sk_type != SOCK_STREAM))
473 		goto out;
474 	rc = -EAGAIN;
475 	if (sock_flag(sk, SOCK_ZAPPED))
476 		goto out;
477 	rc = 0;
478 	if (!(unsigned)backlog)	/* BSDism */
479 		backlog = 1;
480 	sk->sk_max_ack_backlog = backlog;
481 	if (sk->sk_state != TCP_LISTEN) {
482 		sk->sk_ack_backlog = 0;
483 		sk->sk_state	   = TCP_LISTEN;
484 	}
485 	sk->sk_socket->flags |= __SO_ACCEPTCON;
486 out:
487 	release_sock(sk);
488 	return rc;
489 }
490 
491 static int llc_ui_wait_for_disc(struct sock *sk, long timeout)
492 {
493 	DEFINE_WAIT(wait);
494 	int rc = 0;
495 
496 	while (1) {
497 		prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
498 		if (sk_wait_event(sk, &timeout, sk->sk_state == TCP_CLOSE))
499 			break;
500 		rc = -ERESTARTSYS;
501 		if (signal_pending(current))
502 			break;
503 		rc = -EAGAIN;
504 		if (!timeout)
505 			break;
506 		rc = 0;
507 	}
508 	finish_wait(sk->sk_sleep, &wait);
509 	return rc;
510 }
511 
512 static int llc_ui_wait_for_conn(struct sock *sk, long timeout)
513 {
514 	DEFINE_WAIT(wait);
515 
516 	while (1) {
517 		prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
518 		if (sk_wait_event(sk, &timeout, sk->sk_state != TCP_SYN_SENT))
519 			break;
520 		if (signal_pending(current) || !timeout)
521 			break;
522 	}
523 	finish_wait(sk->sk_sleep, &wait);
524 	return timeout;
525 }
526 
527 static int llc_ui_wait_for_data(struct sock *sk, long timeout)
528 {
529 	DEFINE_WAIT(wait);
530 	int rc = 0;
531 
532 	while (1) {
533 		prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
534 		if (sk_wait_event(sk, &timeout,
535 				  (sk->sk_shutdown & RCV_SHUTDOWN) ||
536 				  (!skb_queue_empty(&sk->sk_receive_queue))))
537 			break;
538 		rc = -ERESTARTSYS;
539 		if (signal_pending(current))
540 			break;
541 		rc = -EAGAIN;
542 		if (!timeout)
543 			break;
544 		rc = 0;
545 	}
546 	finish_wait(sk->sk_sleep, &wait);
547 	return rc;
548 }
549 
550 static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout)
551 {
552 	DEFINE_WAIT(wait);
553 	struct llc_sock *llc = llc_sk(sk);
554 	int rc;
555 
556 	while (1) {
557 		prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
558 		rc = 0;
559 		if (sk_wait_event(sk, &timeout,
560 				  (sk->sk_shutdown & RCV_SHUTDOWN) ||
561 				  (!llc_data_accept_state(llc->state) &&
562 				   !llc->p_flag)))
563 			break;
564 		rc = -ERESTARTSYS;
565 		if (signal_pending(current))
566 			break;
567 		rc = -EAGAIN;
568 		if (!timeout)
569 			break;
570 	}
571 	finish_wait(sk->sk_sleep, &wait);
572 	return rc;
573 }
574 
575 /**
576  *	llc_ui_accept - accept a new incoming connection.
577  *	@sock: Socket which connections arrive on.
578  *	@newsock: Socket to move incoming connection to.
579  *	@flags: User specified operational flags.
580  *
581  *	Accept a new incoming connection.
582  *	Returns 0 upon success, negative otherwise.
583  */
584 static int llc_ui_accept(struct socket *sock, struct socket *newsock, int flags)
585 {
586 	struct sock *sk = sock->sk, *newsk;
587 	struct llc_sock *llc, *newllc;
588 	struct sk_buff *skb;
589 	int rc = -EOPNOTSUPP;
590 
591 	dprintk("%s: accepting on %02X\n", __FUNCTION__,
592 	        llc_sk(sk)->laddr.lsap);
593 	lock_sock(sk);
594 	if (unlikely(sk->sk_type != SOCK_STREAM))
595 		goto out;
596 	rc = -EINVAL;
597 	if (unlikely(sock->state != SS_UNCONNECTED ||
598 		     sk->sk_state != TCP_LISTEN))
599 		goto out;
600 	/* wait for a connection to arrive. */
601 	if (skb_queue_empty(&sk->sk_receive_queue)) {
602 		rc = llc_ui_wait_for_data(sk, sk->sk_rcvtimeo);
603 		if (rc)
604 			goto out;
605 	}
606 	dprintk("%s: got a new connection on %02X\n", __FUNCTION__,
607 	        llc_sk(sk)->laddr.lsap);
608 	skb = skb_dequeue(&sk->sk_receive_queue);
609 	rc = -EINVAL;
610 	if (!skb->sk)
611 		goto frees;
612 	rc = 0;
613 	newsk = skb->sk;
614 	/* attach connection to a new socket. */
615 	llc_ui_sk_init(newsock, newsk);
616 	sock_reset_flag(newsk, SOCK_ZAPPED);
617 	newsk->sk_state		= TCP_ESTABLISHED;
618 	newsock->state		= SS_CONNECTED;
619 	llc			= llc_sk(sk);
620 	newllc			= llc_sk(newsk);
621 	memcpy(&newllc->addr, &llc->addr, sizeof(newllc->addr));
622 	newllc->link = llc_ui_next_link_no(newllc->laddr.lsap);
623 
624 	/* put original socket back into a clean listen state. */
625 	sk->sk_state = TCP_LISTEN;
626 	sk->sk_ack_backlog--;
627 	skb->sk = NULL;
628 	dprintk("%s: ok success on %02X, client on %02X\n", __FUNCTION__,
629 		llc_sk(sk)->addr.sllc_sap, newllc->daddr.lsap);
630 frees:
631 	kfree_skb(skb);
632 out:
633 	release_sock(sk);
634 	return rc;
635 }
636 
637 /**
638  *	llc_ui_recvmsg - copy received data to the socket user.
639  *	@sock: Socket to copy data from.
640  *	@msg: Various user space related information.
641  *	@size: Size of user buffer.
642  *	@flags: User specified flags.
643  *
644  *	Copy received data to the socket user.
645  *	Returns non-negative upon success, negative otherwise.
646  */
647 static int llc_ui_recvmsg(struct kiocb *iocb, struct socket *sock,
648 			  struct msghdr *msg, size_t size, int flags)
649 {
650 	struct sock *sk = sock->sk;
651 	struct sockaddr_llc *uaddr = (struct sockaddr_llc *)msg->msg_name;
652 	struct sk_buff *skb;
653 	size_t copied = 0;
654 	int rc = -ENOMEM;
655 	int noblock = flags & MSG_DONTWAIT;
656 
657 	dprintk("%s: receiving in %02X from %02X\n", __FUNCTION__,
658 		llc_sk(sk)->laddr.lsap, llc_sk(sk)->daddr.lsap);
659 	lock_sock(sk);
660 	if (skb_queue_empty(&sk->sk_receive_queue)) {
661 		rc = llc_ui_wait_for_data(sk, sock_rcvtimeo(sk, noblock));
662 		if (rc)
663 			goto out;
664 	}
665 	skb = skb_dequeue(&sk->sk_receive_queue);
666 	if (!skb) /* shutdown */
667 		goto out;
668 	copied = skb->len;
669 	if (copied > size)
670 		copied = size;
671 	rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
672 	if (rc)
673 		goto dgram_free;
674 	if (skb->len > copied) {
675 		skb_pull(skb, copied);
676 		skb_queue_head(&sk->sk_receive_queue, skb);
677 	}
678 	if (uaddr)
679 		memcpy(uaddr, llc_ui_skb_cb(skb), sizeof(*uaddr));
680 	msg->msg_namelen = sizeof(*uaddr);
681 	if (!skb->next) {
682 dgram_free:
683 		kfree_skb(skb);
684 	}
685 out:
686 	release_sock(sk);
687 	return rc ? : copied;
688 }
689 
690 /**
691  *	llc_ui_sendmsg - Transmit data provided by the socket user.
692  *	@sock: Socket to transmit data from.
693  *	@msg: Various user related information.
694  *	@len: Length of data to transmit.
695  *
696  *	Transmit data provided by the socket user.
697  *	Returns non-negative upon success, negative otherwise.
698  */
699 static int llc_ui_sendmsg(struct kiocb *iocb, struct socket *sock,
700 			  struct msghdr *msg, size_t len)
701 {
702 	struct sock *sk = sock->sk;
703 	struct llc_sock *llc = llc_sk(sk);
704 	struct sockaddr_llc *addr = (struct sockaddr_llc *)msg->msg_name;
705 	int flags = msg->msg_flags;
706 	int noblock = flags & MSG_DONTWAIT;
707 	struct sk_buff *skb;
708 	size_t size = 0;
709 	int rc = -EINVAL, copied = 0, hdrlen;
710 
711 	dprintk("%s: sending from %02X to %02X\n", __FUNCTION__,
712 		llc->laddr.lsap, llc->daddr.lsap);
713 	lock_sock(sk);
714 	if (addr) {
715 		if (msg->msg_namelen < sizeof(*addr))
716 			goto release;
717 	} else {
718 		if (llc_ui_addr_null(&llc->addr))
719 			goto release;
720 		addr = &llc->addr;
721 	}
722 	/* must bind connection to sap if user hasn't done it. */
723 	if (sock_flag(sk, SOCK_ZAPPED)) {
724 		/* bind to sap with null dev, exclusive. */
725 		rc = llc_ui_autobind(sock, addr);
726 		if (rc)
727 			goto release;
728 	}
729 	hdrlen = llc->dev->hard_header_len + llc_ui_header_len(sk, addr);
730 	size = hdrlen + len;
731 	if (size > llc->dev->mtu)
732 		size = llc->dev->mtu;
733 	copied = size - hdrlen;
734 	release_sock(sk);
735 	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
736 	lock_sock(sk);
737 	if (!skb)
738 		goto release;
739 	skb->sk	      = sk;
740 	skb->dev      = llc->dev;
741 	skb->protocol = llc_proto_type(addr->sllc_arphrd);
742 	skb_reserve(skb, hdrlen);
743 	rc = memcpy_fromiovec(skb_put(skb, copied), msg->msg_iov, copied);
744 	if (rc)
745 		goto out;
746 	if (sk->sk_type == SOCK_DGRAM || addr->sllc_ua) {
747 		llc_build_and_send_ui_pkt(llc->sap, skb, addr->sllc_mac,
748 					  addr->sllc_sap);
749 		goto out;
750 	}
751 	if (addr->sllc_test) {
752 		llc_build_and_send_test_pkt(llc->sap, skb, addr->sllc_mac,
753 					    addr->sllc_sap);
754 		goto out;
755 	}
756 	if (addr->sllc_xid) {
757 		llc_build_and_send_xid_pkt(llc->sap, skb, addr->sllc_mac,
758 					   addr->sllc_sap);
759 		goto out;
760 	}
761 	rc = -ENOPROTOOPT;
762 	if (!(sk->sk_type == SOCK_STREAM && !addr->sllc_ua))
763 		goto out;
764 	rc = llc_ui_send_data(sk, skb, noblock);
765 	if (rc)
766 		dprintk("%s: llc_ui_send_data failed: %d\n", __FUNCTION__, rc);
767 out:
768 	if (rc)
769 		kfree_skb(skb);
770 release:
771 	if (rc)
772 		dprintk("%s: failed sending from %02X to %02X: %d\n",
773 			__FUNCTION__, llc->laddr.lsap, llc->daddr.lsap, rc);
774 	release_sock(sk);
775 	return rc ? : copied;
776 }
777 
778 /**
779  *	llc_ui_getname - return the address info of a socket
780  *	@sock: Socket to get address of.
781  *	@uaddr: Address structure to return information.
782  *	@uaddrlen: Length of address structure.
783  *	@peer: Does user want local or remote address information.
784  *
785  *	Return the address information of a socket.
786  */
787 static int llc_ui_getname(struct socket *sock, struct sockaddr *uaddr,
788 			  int *uaddrlen, int peer)
789 {
790 	struct sockaddr_llc sllc;
791 	struct sock *sk = sock->sk;
792 	struct llc_sock *llc = llc_sk(sk);
793 	int rc = 0;
794 
795 	lock_sock(sk);
796 	if (sock_flag(sk, SOCK_ZAPPED))
797 		goto out;
798 	*uaddrlen = sizeof(sllc);
799 	memset(uaddr, 0, *uaddrlen);
800 	if (peer) {
801 		rc = -ENOTCONN;
802 		if (sk->sk_state != TCP_ESTABLISHED)
803 			goto out;
804 		if(llc->dev)
805 			sllc.sllc_arphrd = llc->dev->type;
806 		sllc.sllc_sap = llc->daddr.lsap;
807 		memcpy(&sllc.sllc_mac, &llc->daddr.mac, IFHWADDRLEN);
808 	} else {
809 		rc = -EINVAL;
810 		if (!llc->sap)
811 			goto out;
812 		sllc.sllc_sap = llc->sap->laddr.lsap;
813 
814 		if (llc->dev) {
815 			sllc.sllc_arphrd = llc->dev->type;
816 			memcpy(&sllc.sllc_mac, &llc->dev->dev_addr,
817 			       IFHWADDRLEN);
818 		}
819 	}
820 	rc = 0;
821 	sllc.sllc_family = AF_LLC;
822 	memcpy(uaddr, &sllc, sizeof(sllc));
823 out:
824 	release_sock(sk);
825 	return rc;
826 }
827 
828 /**
829  *	llc_ui_ioctl - io controls for PF_LLC
830  *	@sock: Socket to get/set info
831  *	@cmd: command
832  *	@arg: optional argument for cmd
833  *
834  *	get/set info on llc sockets
835  */
836 static int llc_ui_ioctl(struct socket *sock, unsigned int cmd,
837 			unsigned long arg)
838 {
839 	return dev_ioctl(cmd, (void __user *)arg);
840 }
841 
842 /**
843  *	llc_ui_setsockopt - set various connection specific parameters.
844  *	@sock: Socket to set options on.
845  *	@level: Socket level user is requesting operations on.
846  *	@optname: Operation name.
847  *	@optval User provided operation data.
848  *	@optlen: Length of optval.
849  *
850  *	Set various connection specific parameters.
851  */
852 static int llc_ui_setsockopt(struct socket *sock, int level, int optname,
853 			     char __user *optval, int optlen)
854 {
855 	struct sock *sk = sock->sk;
856 	struct llc_sock *llc = llc_sk(sk);
857 	int rc = -EINVAL, opt;
858 
859 	lock_sock(sk);
860 	if (unlikely(level != SOL_LLC || optlen != sizeof(int)))
861 		goto out;
862 	rc = get_user(opt, (int __user *)optval);
863 	if (rc)
864 		goto out;
865 	rc = -EINVAL;
866 	switch (optname) {
867 	case LLC_OPT_RETRY:
868 		if (opt > LLC_OPT_MAX_RETRY)
869 			goto out;
870 		llc->n2 = opt;
871 		break;
872 	case LLC_OPT_SIZE:
873 		if (opt > LLC_OPT_MAX_SIZE)
874 			goto out;
875 		llc->n1 = opt;
876 		break;
877 	case LLC_OPT_ACK_TMR_EXP:
878 		if (opt > LLC_OPT_MAX_ACK_TMR_EXP)
879 			goto out;
880 		llc->ack_timer.expire = opt;
881 		break;
882 	case LLC_OPT_P_TMR_EXP:
883 		if (opt > LLC_OPT_MAX_P_TMR_EXP)
884 			goto out;
885 		llc->pf_cycle_timer.expire = opt;
886 		break;
887 	case LLC_OPT_REJ_TMR_EXP:
888 		if (opt > LLC_OPT_MAX_REJ_TMR_EXP)
889 			goto out;
890 		llc->rej_sent_timer.expire = opt;
891 		break;
892 	case LLC_OPT_BUSY_TMR_EXP:
893 		if (opt > LLC_OPT_MAX_BUSY_TMR_EXP)
894 			goto out;
895 		llc->busy_state_timer.expire = opt;
896 		break;
897 	case LLC_OPT_TX_WIN:
898 		if (opt > LLC_OPT_MAX_WIN)
899 			goto out;
900 		llc->k = opt;
901 		break;
902 	case LLC_OPT_RX_WIN:
903 		if (opt > LLC_OPT_MAX_WIN)
904 			goto out;
905 		llc->rw = opt;
906 		break;
907 	default:
908 		rc = -ENOPROTOOPT;
909 		goto out;
910 	}
911 	rc = 0;
912 out:
913 	release_sock(sk);
914 	return rc;
915 }
916 
917 /**
918  *	llc_ui_getsockopt - get connection specific socket info
919  *	@sock: Socket to get information from.
920  *	@level: Socket level user is requesting operations on.
921  *	@optname: Operation name.
922  *	@optval: Variable to return operation data in.
923  *	@optlen: Length of optval.
924  *
925  *	Get connection specific socket information.
926  */
927 static int llc_ui_getsockopt(struct socket *sock, int level, int optname,
928 			     char __user *optval, int __user *optlen)
929 {
930 	struct sock *sk = sock->sk;
931 	struct llc_sock *llc = llc_sk(sk);
932 	int val = 0, len = 0, rc = -EINVAL;
933 
934 	lock_sock(sk);
935 	if (unlikely(level != SOL_LLC))
936 		goto out;
937 	rc = get_user(len, optlen);
938 	if (rc)
939 		goto out;
940 	rc = -EINVAL;
941 	if (len != sizeof(int))
942 		goto out;
943 	switch (optname) {
944 	case LLC_OPT_RETRY:
945 		val = llc->n2;				break;
946 	case LLC_OPT_SIZE:
947 		val = llc->n1;				break;
948 	case LLC_OPT_ACK_TMR_EXP:
949 		val = llc->ack_timer.expire;		break;
950 	case LLC_OPT_P_TMR_EXP:
951 		val = llc->pf_cycle_timer.expire;	break;
952 	case LLC_OPT_REJ_TMR_EXP:
953 		val = llc->rej_sent_timer.expire;	break;
954 	case LLC_OPT_BUSY_TMR_EXP:
955 		val = llc->busy_state_timer.expire;	break;
956 	case LLC_OPT_TX_WIN:
957 		val = llc->k;				break;
958 	case LLC_OPT_RX_WIN:
959 		val = llc->rw;				break;
960 	default:
961 		rc = -ENOPROTOOPT;
962 		goto out;
963 	}
964 	rc = 0;
965 	if (put_user(len, optlen) || copy_to_user(optval, &val, len))
966 		rc = -EFAULT;
967 out:
968 	release_sock(sk);
969 	return rc;
970 }
971 
972 static struct net_proto_family llc_ui_family_ops = {
973 	.family = PF_LLC,
974 	.create = llc_ui_create,
975 	.owner	= THIS_MODULE,
976 };
977 
978 static struct proto_ops llc_ui_ops = {
979 	.family	     = PF_LLC,
980 	.owner       = THIS_MODULE,
981 	.release     = llc_ui_release,
982 	.bind	     = llc_ui_bind,
983 	.connect     = llc_ui_connect,
984 	.socketpair  = sock_no_socketpair,
985 	.accept      = llc_ui_accept,
986 	.getname     = llc_ui_getname,
987 	.poll	     = datagram_poll,
988 	.ioctl       = llc_ui_ioctl,
989 	.listen      = llc_ui_listen,
990 	.shutdown    = llc_ui_shutdown,
991 	.setsockopt  = llc_ui_setsockopt,
992 	.getsockopt  = llc_ui_getsockopt,
993 	.sendmsg     = llc_ui_sendmsg,
994 	.recvmsg     = llc_ui_recvmsg,
995 	.mmap	     = sock_no_mmap,
996 	.sendpage    = sock_no_sendpage,
997 };
998 
999 extern void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb);
1000 extern void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb);
1001 
1002 static int __init llc2_init(void)
1003 {
1004 	int rc = proto_register(&llc_proto, 0);
1005 
1006 	if (rc != 0)
1007 		goto out;
1008 
1009 	llc_build_offset_table();
1010 	llc_station_init();
1011 	llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
1012 	rc = llc_proc_init();
1013 	if (rc != 0)
1014 		goto out_unregister_llc_proto;
1015 	sock_register(&llc_ui_family_ops);
1016 	llc_add_pack(LLC_DEST_SAP, llc_sap_handler);
1017 	llc_add_pack(LLC_DEST_CONN, llc_conn_handler);
1018 out:
1019 	return rc;
1020 out_unregister_llc_proto:
1021 	proto_unregister(&llc_proto);
1022 	goto out;
1023 }
1024 
1025 static void __exit llc2_exit(void)
1026 {
1027 	llc_station_exit();
1028 	llc_remove_pack(LLC_DEST_SAP);
1029 	llc_remove_pack(LLC_DEST_CONN);
1030 	sock_unregister(PF_LLC);
1031 	llc_proc_exit();
1032 	proto_unregister(&llc_proto);
1033 }
1034 
1035 module_init(llc2_init);
1036 module_exit(llc2_exit);
1037 
1038 MODULE_LICENSE("GPL");
1039 MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
1040 MODULE_DESCRIPTION("IEEE 802.2 PF_LLC support");
1041 MODULE_ALIAS_NETPROTO(PF_LLC);
1042