xref: /openbmc/linux/net/llc/af_llc.c (revision 1da177e4)
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/tcp.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/init.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 
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, int timeout);
41 static int llc_ui_wait_for_disc(struct sock *sk, int timeout);
42 static int llc_ui_wait_for_data(struct sock *sk, int timeout);
43 static int llc_ui_wait_for_busy_core(struct sock *sk, int 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 		int 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 (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 (!sk)
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 - Bind a socket to a specific address.
235  *	@sk: Socket to bind an address to.
236  *	@addr: Address the user wants the socket bound to.
237  *
238  *	Bind a socket to a specific address. For llc a user is able to bind to
239  *	a specific sap only or mac + sap. If the user only specifies a sap and
240  *	a null dmac (all zeros) the user is attempting to bind to an entire
241  *	sap. This will stop anyone else on the local system from using that
242  *	sap.  If someone else has a mac + sap open the bind to null + sap will
243  *	fail.
244  *	If the user desires to bind to a specific mac + sap, it is possible to
245  *	have multiple sap connections via multiple macs.
246  *	Bind and autobind for that matter must enforce the correct sap usage
247  *	otherwise all hell will break loose.
248  *	Returns: 0 upon success, negative otherwise.
249  */
250 static int llc_ui_autobind(struct socket *sock, struct sockaddr_llc *addr)
251 {
252 	struct sock *sk = sock->sk;
253 	struct llc_sock *llc = llc_sk(sk);
254 	struct llc_sap *sap;
255 	int rc = -EINVAL;
256 
257 	if (!sock_flag(sk, SOCK_ZAPPED))
258 		goto out;
259 	rc = -ENODEV;
260 	llc->dev = dev_getfirstbyhwtype(addr->sllc_arphrd);
261 	if (!llc->dev)
262 		goto out;
263 	rc = -EUSERS;
264 	llc->laddr.lsap = llc_ui_autoport();
265 	if (!llc->laddr.lsap)
266 		goto out;
267 	rc = -EBUSY; /* some other network layer is using the sap */
268 	sap = llc_sap_open(llc->laddr.lsap, NULL);
269 	if (!sap)
270 		goto out;
271 	memcpy(llc->laddr.mac, llc->dev->dev_addr, IFHWADDRLEN);
272 	memcpy(&llc->addr, addr, sizeof(llc->addr));
273 	/* assign new connection to its SAP */
274 	llc_sap_add_socket(sap, sk);
275 	sock_reset_flag(sk, SOCK_ZAPPED);
276 	rc = 0;
277 out:
278 	return rc;
279 }
280 
281 /**
282  *	llc_ui_bind - bind a socket to a specific address.
283  *	@sock: Socket to bind an address to.
284  *	@uaddr: Address the user wants the socket bound to.
285  *	@addrlen: Length of the uaddr structure.
286  *
287  *	Bind a socket to a specific address. For llc a user is able to bind to
288  *	a specific sap only or mac + sap. If the user only specifies a sap and
289  *	a null dmac (all zeros) the user is attempting to bind to an entire
290  *	sap. This will stop anyone else on the local system from using that
291  *	sap. If someone else has a mac + sap open the bind to null + sap will
292  *	fail.
293  *	If the user desires to bind to a specific mac + sap, it is possible to
294  *	have multiple sap connections via multiple macs.
295  *	Bind and autobind for that matter must enforce the correct sap usage
296  *	otherwise all hell will break loose.
297  *	Returns: 0 upon success, negative otherwise.
298  */
299 static int llc_ui_bind(struct socket *sock, struct sockaddr *uaddr, int addrlen)
300 {
301 	struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
302 	struct sock *sk = sock->sk;
303 	struct llc_sock *llc = llc_sk(sk);
304 	struct llc_sap *sap;
305 	int rc = -EINVAL;
306 
307 	dprintk("%s: binding %02X\n", __FUNCTION__, addr->sllc_sap);
308 	if (!sock_flag(sk, SOCK_ZAPPED) || addrlen != sizeof(*addr))
309 		goto out;
310 	rc = -EAFNOSUPPORT;
311 	if (addr->sllc_family != AF_LLC)
312 		goto out;
313 	if (!addr->sllc_sap) {
314 		rc = -EUSERS;
315 		addr->sllc_sap = llc_ui_autoport();
316 		if (!addr->sllc_sap)
317 			goto out;
318 	}
319 	sap = llc_sap_find(addr->sllc_sap);
320 	if (!sap) {
321 		sap = llc_sap_open(addr->sllc_sap, NULL);
322 		rc = -EBUSY; /* some other network layer is using the sap */
323 		if (!sap)
324 			goto out;
325 	} else {
326 		struct llc_addr laddr, daddr;
327 		struct sock *ask;
328 
329 		memset(&laddr, 0, sizeof(laddr));
330 		memset(&daddr, 0, sizeof(daddr));
331 		/*
332 		 * FIXME: check if the the address is multicast,
333 		 * 	  only SOCK_DGRAM can do this.
334 		 */
335 		memcpy(laddr.mac, addr->sllc_mac, IFHWADDRLEN);
336 		laddr.lsap = addr->sllc_sap;
337 		rc = -EADDRINUSE; /* mac + sap clash. */
338 		ask = llc_lookup_established(sap, &daddr, &laddr);
339 		if (ask) {
340 			sock_put(ask);
341 			goto out;
342 		}
343 	}
344 	llc->laddr.lsap = addr->sllc_sap;
345 	memcpy(llc->laddr.mac, addr->sllc_mac, IFHWADDRLEN);
346 	memcpy(&llc->addr, addr, sizeof(llc->addr));
347 	/* assign new connection to its SAP */
348 	llc_sap_add_socket(sap, sk);
349 	sock_reset_flag(sk, SOCK_ZAPPED);
350 	rc = 0;
351 out:
352 	return rc;
353 }
354 
355 /**
356  *	llc_ui_shutdown - shutdown a connect llc2 socket.
357  *	@sock: Socket to shutdown.
358  *	@how: What part of the socket to shutdown.
359  *
360  *	Shutdown a connected llc2 socket. Currently this function only supports
361  *	shutting down both sends and receives (2), we could probably make this
362  *	function such that a user can shutdown only half the connection but not
363  *	right now.
364  *	Returns: 0 upon success, negative otherwise.
365  */
366 static int llc_ui_shutdown(struct socket *sock, int how)
367 {
368 	struct sock *sk = sock->sk;
369 	int rc = -ENOTCONN;
370 
371 	lock_sock(sk);
372 	if (sk->sk_state != TCP_ESTABLISHED)
373 		goto out;
374 	rc = -EINVAL;
375 	if (how != 2)
376 		goto out;
377 	rc = llc_send_disc(sk);
378 	if (!rc)
379 		rc = llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
380 	/* Wake up anyone sleeping in poll */
381 	sk->sk_state_change(sk);
382 out:
383 	release_sock(sk);
384 	return rc;
385 }
386 
387 /**
388  *	llc_ui_connect - Connect to a remote llc2 mac + sap.
389  *	@sock: Socket which will be connected to the remote destination.
390  *	@uaddr: Remote and possibly the local address of the new connection.
391  *	@addrlen: Size of uaddr structure.
392  *	@flags: Operational flags specified by the user.
393  *
394  *	Connect to a remote llc2 mac + sap. The caller must specify the
395  *	destination mac and address to connect to. If the user hasn't previously
396  *	called bind(2) with a smac the address of the first interface of the
397  *	specified arp type will be used.
398  *	This function will autobind if user did not previously call bind.
399  *	Returns: 0 upon success, negative otherwise.
400  */
401 static int llc_ui_connect(struct socket *sock, struct sockaddr *uaddr,
402 			  int addrlen, int flags)
403 {
404 	struct sock *sk = sock->sk;
405 	struct llc_sock *llc = llc_sk(sk);
406 	struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
407 	struct net_device *dev;
408 	int rc = -EINVAL;
409 
410 	lock_sock(sk);
411 	if (addrlen != sizeof(*addr))
412 		goto out;
413 	rc = -EAFNOSUPPORT;
414 	if (addr->sllc_family != AF_LLC)
415 		goto out;
416 	/* bind connection to sap if user hasn't done it. */
417 	if (sock_flag(sk, SOCK_ZAPPED)) {
418 		/* bind to sap with null dev, exclusive */
419 		rc = llc_ui_autobind(sock, addr);
420 		if (rc)
421 			goto out;
422 		llc->daddr.lsap = addr->sllc_sap;
423 		memcpy(llc->daddr.mac, addr->sllc_mac, IFHWADDRLEN);
424 	}
425 	dev = llc->dev;
426 	if (sk->sk_type != SOCK_STREAM)
427 		goto out;
428 	rc = -EALREADY;
429 	if (sock->state == SS_CONNECTING)
430 		goto out;
431 	sock->state = SS_CONNECTING;
432 	sk->sk_state   = TCP_SYN_SENT;
433 	llc->link   = llc_ui_next_link_no(llc->sap->laddr.lsap);
434 	rc = llc_establish_connection(sk, dev->dev_addr,
435 				      addr->sllc_mac, addr->sllc_sap);
436 	if (rc) {
437 		dprintk("%s: llc_ui_send_conn failed :-(\n", __FUNCTION__);
438 		sock->state  = SS_UNCONNECTED;
439 		sk->sk_state = TCP_CLOSE;
440 		goto out;
441 	}
442 	rc = llc_ui_wait_for_conn(sk, sk->sk_rcvtimeo);
443 	if (rc)
444 		dprintk("%s: llc_ui_wait_for_conn failed=%d\n", __FUNCTION__, rc);
445 out:
446 	release_sock(sk);
447 	return rc;
448 }
449 
450 /**
451  *	llc_ui_listen - allow a normal socket to accept incoming connections
452  *	@sock: Socket to allow incoming connections on.
453  *	@backlog: Number of connections to queue.
454  *
455  *	Allow a normal socket to accept incoming connections.
456  *	Returns 0 upon success, negative otherwise.
457  */
458 static int llc_ui_listen(struct socket *sock, int backlog)
459 {
460 	struct sock *sk = sock->sk;
461 	int rc = -EINVAL;
462 
463 	lock_sock(sk);
464 	if (sock->state != SS_UNCONNECTED)
465 		goto out;
466 	rc = -EOPNOTSUPP;
467 	if (sk->sk_type != SOCK_STREAM)
468 		goto out;
469 	rc = -EAGAIN;
470 	if (sock_flag(sk, SOCK_ZAPPED))
471 		goto out;
472 	rc = 0;
473 	if (!(unsigned)backlog)	/* BSDism */
474 		backlog = 1;
475 	sk->sk_max_ack_backlog = backlog;
476 	if (sk->sk_state != TCP_LISTEN) {
477 		sk->sk_ack_backlog = 0;
478 		sk->sk_state	   = TCP_LISTEN;
479 	}
480 	sk->sk_socket->flags |= __SO_ACCEPTCON;
481 out:
482 	release_sock(sk);
483 	return rc;
484 }
485 
486 static int llc_ui_wait_for_disc(struct sock *sk, int timeout)
487 {
488 	DECLARE_WAITQUEUE(wait, current);
489 	int rc;
490 
491 	add_wait_queue_exclusive(sk->sk_sleep, &wait);
492 	for (;;) {
493 		__set_current_state(TASK_INTERRUPTIBLE);
494 		rc = 0;
495 		if (sk->sk_state != TCP_CLOSE) {
496 			release_sock(sk);
497 			timeout = schedule_timeout(timeout);
498 			lock_sock(sk);
499 		} else
500 			break;
501 		rc = -ERESTARTSYS;
502 		if (signal_pending(current))
503 			break;
504 		rc = -EAGAIN;
505 		if (!timeout)
506 			break;
507 	}
508 	__set_current_state(TASK_RUNNING);
509 	remove_wait_queue(sk->sk_sleep, &wait);
510 	return rc;
511 }
512 
513 static int llc_ui_wait_for_conn(struct sock *sk, int timeout)
514 {
515 	DECLARE_WAITQUEUE(wait, current);
516 	int rc;
517 
518 	add_wait_queue_exclusive(sk->sk_sleep, &wait);
519 	for (;;) {
520 		__set_current_state(TASK_INTERRUPTIBLE);
521 		rc = -EAGAIN;
522 		if (sk->sk_state == TCP_CLOSE)
523 			break;
524 		rc = 0;
525 		if (sk->sk_state != TCP_ESTABLISHED) {
526 			release_sock(sk);
527 			timeout = schedule_timeout(timeout);
528 			lock_sock(sk);
529 		} else
530 			break;
531 		rc = -ERESTARTSYS;
532 		if (signal_pending(current))
533 			break;
534 		rc = -EAGAIN;
535 		if (!timeout)
536 			break;
537 	}
538 	__set_current_state(TASK_RUNNING);
539 	remove_wait_queue(sk->sk_sleep, &wait);
540 	return rc;
541 }
542 
543 static int llc_ui_wait_for_data(struct sock *sk, int timeout)
544 {
545 	DECLARE_WAITQUEUE(wait, current);
546 	int rc = 0;
547 
548 	add_wait_queue_exclusive(sk->sk_sleep, &wait);
549 	for (;;) {
550 		__set_current_state(TASK_INTERRUPTIBLE);
551 		if (sk->sk_shutdown & RCV_SHUTDOWN)
552 			break;
553 		/*
554 		 * Well, if we have backlog, try to process it now.
555 		 */
556                 if (sk->sk_backlog.tail) {
557 			release_sock(sk);
558 			lock_sock(sk);
559 		}
560 		rc = 0;
561 		if (skb_queue_empty(&sk->sk_receive_queue)) {
562 			release_sock(sk);
563 			timeout = schedule_timeout(timeout);
564 			lock_sock(sk);
565 		} else
566 			break;
567 		rc = -ERESTARTSYS;
568 		if (signal_pending(current))
569 			break;
570 		rc = -EAGAIN;
571 		if (!timeout)
572 			break;
573 	}
574 	__set_current_state(TASK_RUNNING);
575 	remove_wait_queue(sk->sk_sleep, &wait);
576 	return rc;
577 }
578 
579 static int llc_ui_wait_for_busy_core(struct sock *sk, int timeout)
580 {
581 	DECLARE_WAITQUEUE(wait, current);
582 	struct llc_sock *llc = llc_sk(sk);
583 	int rc;
584 
585 	add_wait_queue_exclusive(sk->sk_sleep, &wait);
586 	for (;;) {
587 		dprintk("%s: looping...\n", __FUNCTION__);
588 		__set_current_state(TASK_INTERRUPTIBLE);
589 		rc = -ENOTCONN;
590 		if (sk->sk_shutdown & RCV_SHUTDOWN)
591 			break;
592 		rc = 0;
593 		if (llc_data_accept_state(llc->state) || llc->p_flag) {
594 			release_sock(sk);
595 			timeout = schedule_timeout(timeout);
596 			lock_sock(sk);
597 		} else
598 			break;
599 		rc = -ERESTARTSYS;
600 		if (signal_pending(current))
601 			break;
602 		rc = -EAGAIN;
603 		if (!timeout)
604 			break;
605 	}
606 	__set_current_state(TASK_RUNNING);
607 	remove_wait_queue(sk->sk_sleep, &wait);
608 	return rc;
609 }
610 
611 /**
612  *	llc_ui_accept - accept a new incoming connection.
613  *	@sock: Socket which connections arrive on.
614  *	@newsock: Socket to move incoming connection to.
615  *	@flags: User specified operational flags.
616  *
617  *	Accept a new incoming connection.
618  *	Returns 0 upon success, negative otherwise.
619  */
620 static int llc_ui_accept(struct socket *sock, struct socket *newsock, int flags)
621 {
622 	struct sock *sk = sock->sk, *newsk;
623 	struct llc_sock *llc, *newllc;
624 	struct sk_buff *skb;
625 	int rc = -EOPNOTSUPP;
626 
627 	dprintk("%s: accepting on %02X\n", __FUNCTION__,
628 	        llc_sk(sk)->laddr.lsap);
629 	lock_sock(sk);
630 	if (sk->sk_type != SOCK_STREAM)
631 		goto out;
632 	rc = -EINVAL;
633 	if (sock->state != SS_UNCONNECTED || sk->sk_state != TCP_LISTEN)
634 		goto out;
635 	/* wait for a connection to arrive. */
636 	rc = llc_ui_wait_for_data(sk, sk->sk_rcvtimeo);
637 	if (rc)
638 		goto out;
639 	dprintk("%s: got a new connection on %02X\n", __FUNCTION__,
640 	        llc_sk(sk)->laddr.lsap);
641 	skb = skb_dequeue(&sk->sk_receive_queue);
642 	rc = -EINVAL;
643 	if (!skb->sk)
644 		goto frees;
645 	rc = 0;
646 	newsk = skb->sk;
647 	/* attach connection to a new socket. */
648 	llc_ui_sk_init(newsock, newsk);
649 	sock_reset_flag(newsk, SOCK_ZAPPED);
650 	newsk->sk_state		= TCP_ESTABLISHED;
651 	newsock->state		= SS_CONNECTED;
652 	llc			= llc_sk(sk);
653 	newllc			= llc_sk(newsk);
654 	memcpy(&newllc->addr, &llc->addr, sizeof(newllc->addr));
655 	newllc->link = llc_ui_next_link_no(newllc->laddr.lsap);
656 
657 	/* put original socket back into a clean listen state. */
658 	sk->sk_state = TCP_LISTEN;
659 	sk->sk_ack_backlog--;
660 	skb->sk = NULL;
661 	dprintk("%s: ok success on %02X, client on %02X\n", __FUNCTION__,
662 		llc_sk(sk)->addr.sllc_sap, newllc->daddr.lsap);
663 frees:
664 	kfree_skb(skb);
665 out:
666 	release_sock(sk);
667 	return rc;
668 }
669 
670 /**
671  *	llc_ui_recvmsg - copy received data to the socket user.
672  *	@sock: Socket to copy data from.
673  *	@msg: Various user space related information.
674  *	@size: Size of user buffer.
675  *	@flags: User specified flags.
676  *
677  *	Copy received data to the socket user.
678  *	Returns non-negative upon success, negative otherwise.
679  */
680 static int llc_ui_recvmsg(struct kiocb *iocb, struct socket *sock,
681 			  struct msghdr *msg, size_t size, int flags)
682 {
683 	struct sock *sk = sock->sk;
684 	struct sockaddr_llc *uaddr = (struct sockaddr_llc *)msg->msg_name;
685 	struct sk_buff *skb;
686 	size_t copied = 0;
687 	int rc = -ENOMEM, timeout;
688 	int noblock = flags & MSG_DONTWAIT;
689 
690 	dprintk("%s: receiving in %02X from %02X\n", __FUNCTION__,
691 		llc_sk(sk)->laddr.lsap, llc_sk(sk)->daddr.lsap);
692 	lock_sock(sk);
693 	timeout = sock_rcvtimeo(sk, noblock);
694 	rc = llc_ui_wait_for_data(sk, timeout);
695 	if (rc) {
696 		dprintk("%s: llc_ui_wait_for_data failed recv "
697 			"in %02X from %02X\n", __FUNCTION__,
698 			llc_sk(sk)->laddr.lsap, llc_sk(sk)->daddr.lsap);
699 		goto out;
700 	}
701 	skb = skb_dequeue(&sk->sk_receive_queue);
702 	if (!skb) /* shutdown */
703 		goto out;
704 	copied = skb->len;
705 	if (copied > size)
706 		copied = size;
707 	rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
708 	if (rc)
709 		goto dgram_free;
710 	if (skb->len > copied) {
711 		skb_pull(skb, copied);
712 		skb_queue_head(&sk->sk_receive_queue, skb);
713 	}
714 	if (uaddr)
715 		memcpy(uaddr, llc_ui_skb_cb(skb), sizeof(*uaddr));
716 	msg->msg_namelen = sizeof(*uaddr);
717 	if (!skb->list) {
718 dgram_free:
719 		kfree_skb(skb);
720 	}
721 out:
722 	release_sock(sk);
723 	return rc ? : copied;
724 }
725 
726 /**
727  *	llc_ui_sendmsg - Transmit data provided by the socket user.
728  *	@sock: Socket to transmit data from.
729  *	@msg: Various user related information.
730  *	@len: Length of data to transmit.
731  *
732  *	Transmit data provided by the socket user.
733  *	Returns non-negative upon success, negative otherwise.
734  */
735 static int llc_ui_sendmsg(struct kiocb *iocb, struct socket *sock,
736 			  struct msghdr *msg, size_t len)
737 {
738 	struct sock *sk = sock->sk;
739 	struct llc_sock *llc = llc_sk(sk);
740 	struct sockaddr_llc *addr = (struct sockaddr_llc *)msg->msg_name;
741 	int flags = msg->msg_flags;
742 	int noblock = flags & MSG_DONTWAIT;
743 	struct net_device *dev;
744 	struct sk_buff *skb;
745 	size_t size = 0;
746 	int rc = -EINVAL, copied = 0, hdrlen;
747 
748 	dprintk("%s: sending from %02X to %02X\n", __FUNCTION__,
749 		llc->laddr.lsap, llc->daddr.lsap);
750 	lock_sock(sk);
751 	if (addr) {
752 		if (msg->msg_namelen < sizeof(*addr))
753 			goto release;
754 	} else {
755 		if (llc_ui_addr_null(&llc->addr))
756 			goto release;
757 		addr = &llc->addr;
758 	}
759 	/* must bind connection to sap if user hasn't done it. */
760 	if (sock_flag(sk, SOCK_ZAPPED)) {
761 		/* bind to sap with null dev, exclusive. */
762 		rc = llc_ui_autobind(sock, addr);
763 		if (rc)
764 			goto release;
765 	}
766 	dev = llc->dev;
767 	hdrlen = dev->hard_header_len + llc_ui_header_len(sk, addr);
768 	size = hdrlen + len;
769 	if (size > dev->mtu)
770 		size = dev->mtu;
771 	copied = size - hdrlen;
772 	release_sock(sk);
773 	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
774 	lock_sock(sk);
775 	if (!skb)
776 		goto release;
777 	skb->sk	      = sk;
778 	skb->dev      = dev;
779 	skb->protocol = llc_proto_type(addr->sllc_arphrd);
780 	skb_reserve(skb, hdrlen);
781 	rc = memcpy_fromiovec(skb_put(skb, copied), msg->msg_iov, copied);
782 	if (rc)
783 		goto out;
784 	if (sk->sk_type == SOCK_DGRAM || addr->sllc_ua) {
785 		llc_build_and_send_ui_pkt(llc->sap, skb, addr->sllc_mac,
786 					  addr->sllc_sap);
787 		goto out;
788 	}
789 	if (addr->sllc_test) {
790 		llc_build_and_send_test_pkt(llc->sap, skb, addr->sllc_mac,
791 					    addr->sllc_sap);
792 		goto out;
793 	}
794 	if (addr->sllc_xid) {
795 		llc_build_and_send_xid_pkt(llc->sap, skb, addr->sllc_mac,
796 					   addr->sllc_sap);
797 		goto out;
798 	}
799 	rc = -ENOPROTOOPT;
800 	if (!(sk->sk_type == SOCK_STREAM && !addr->sllc_ua))
801 		goto out;
802 	rc = llc_ui_send_data(sk, skb, noblock);
803 	if (rc)
804 		dprintk("%s: llc_ui_send_data failed: %d\n", __FUNCTION__, rc);
805 out:
806 	if (rc)
807 		kfree_skb(skb);
808 release:
809 	if (rc)
810 		dprintk("%s: failed sending from %02X to %02X: %d\n",
811 			__FUNCTION__, llc->laddr.lsap, llc->daddr.lsap, rc);
812 	release_sock(sk);
813 	return rc ? : copied;
814 }
815 
816 /**
817  *	llc_ui_getname - return the address info of a socket
818  *	@sock: Socket to get address of.
819  *	@uaddr: Address structure to return information.
820  *	@uaddrlen: Length of address structure.
821  *	@peer: Does user want local or remote address information.
822  *
823  *	Return the address information of a socket.
824  */
825 static int llc_ui_getname(struct socket *sock, struct sockaddr *uaddr,
826 			  int *uaddrlen, int peer)
827 {
828 	struct sockaddr_llc sllc;
829 	struct sock *sk = sock->sk;
830 	struct llc_sock *llc = llc_sk(sk);
831 	int rc = 0;
832 
833 	lock_sock(sk);
834 	if (sock_flag(sk, SOCK_ZAPPED))
835 		goto out;
836 	*uaddrlen = sizeof(sllc);
837 	memset(uaddr, 0, *uaddrlen);
838 	if (peer) {
839 		rc = -ENOTCONN;
840 		if (sk->sk_state != TCP_ESTABLISHED)
841 			goto out;
842 		if(llc->dev)
843 			sllc.sllc_arphrd = llc->dev->type;
844 		sllc.sllc_sap = llc->daddr.lsap;
845 		memcpy(&sllc.sllc_mac, &llc->daddr.mac, IFHWADDRLEN);
846 	} else {
847 		rc = -EINVAL;
848 		if (!llc->sap)
849 			goto out;
850 		sllc.sllc_sap = llc->sap->laddr.lsap;
851 
852 		if (llc->dev) {
853 			sllc.sllc_arphrd = llc->dev->type;
854 			memcpy(&sllc.sllc_mac, &llc->dev->dev_addr,
855 			       IFHWADDRLEN);
856 		}
857 	}
858 	rc = 0;
859 	sllc.sllc_family = AF_LLC;
860 	memcpy(uaddr, &sllc, sizeof(sllc));
861 out:
862 	release_sock(sk);
863 	return rc;
864 }
865 
866 /**
867  *	llc_ui_ioctl - io controls for PF_LLC
868  *	@sock: Socket to get/set info
869  *	@cmd: command
870  *	@arg: optional argument for cmd
871  *
872  *	get/set info on llc sockets
873  */
874 static int llc_ui_ioctl(struct socket *sock, unsigned int cmd,
875 			unsigned long arg)
876 {
877 	return dev_ioctl(cmd, (void __user *)arg);
878 }
879 
880 /**
881  *	llc_ui_setsockopt - set various connection specific parameters.
882  *	@sock: Socket to set options on.
883  *	@level: Socket level user is requesting operations on.
884  *	@optname: Operation name.
885  *	@optval User provided operation data.
886  *	@optlen: Length of optval.
887  *
888  *	Set various connection specific parameters.
889  */
890 static int llc_ui_setsockopt(struct socket *sock, int level, int optname,
891 			     char __user *optval, int optlen)
892 {
893 	struct sock *sk = sock->sk;
894 	struct llc_sock *llc = llc_sk(sk);
895 	int rc = -EINVAL, opt;
896 
897 	lock_sock(sk);
898 	if (level != SOL_LLC || optlen != sizeof(int))
899 		goto out;
900 	rc = get_user(opt, (int __user *)optval);
901 	if (rc)
902 		goto out;
903 	rc = -EINVAL;
904 	switch (optname) {
905 	case LLC_OPT_RETRY:
906 		if (opt > LLC_OPT_MAX_RETRY)
907 			goto out;
908 		llc->n2 = opt;
909 		break;
910 	case LLC_OPT_SIZE:
911 		if (opt > LLC_OPT_MAX_SIZE)
912 			goto out;
913 		llc->n1 = opt;
914 		break;
915 	case LLC_OPT_ACK_TMR_EXP:
916 		if (opt > LLC_OPT_MAX_ACK_TMR_EXP)
917 			goto out;
918 		llc->ack_timer.expire = opt;
919 		break;
920 	case LLC_OPT_P_TMR_EXP:
921 		if (opt > LLC_OPT_MAX_P_TMR_EXP)
922 			goto out;
923 		llc->pf_cycle_timer.expire = opt;
924 		break;
925 	case LLC_OPT_REJ_TMR_EXP:
926 		if (opt > LLC_OPT_MAX_REJ_TMR_EXP)
927 			goto out;
928 		llc->rej_sent_timer.expire = opt;
929 		break;
930 	case LLC_OPT_BUSY_TMR_EXP:
931 		if (opt > LLC_OPT_MAX_BUSY_TMR_EXP)
932 			goto out;
933 		llc->busy_state_timer.expire = opt;
934 		break;
935 	case LLC_OPT_TX_WIN:
936 		if (opt > LLC_OPT_MAX_WIN)
937 			goto out;
938 		llc->k = opt;
939 		break;
940 	case LLC_OPT_RX_WIN:
941 		if (opt > LLC_OPT_MAX_WIN)
942 			goto out;
943 		llc->rw = opt;
944 		break;
945 	default:
946 		rc = -ENOPROTOOPT;
947 		goto out;
948 	}
949 	rc = 0;
950 out:
951 	release_sock(sk);
952 	return rc;
953 }
954 
955 /**
956  *	llc_ui_getsockopt - get connection specific socket info
957  *	@sock: Socket to get information from.
958  *	@level: Socket level user is requesting operations on.
959  *	@optname: Operation name.
960  *	@optval: Variable to return operation data in.
961  *	@optlen: Length of optval.
962  *
963  *	Get connection specific socket information.
964  */
965 static int llc_ui_getsockopt(struct socket *sock, int level, int optname,
966 			     char __user *optval, int __user *optlen)
967 {
968 	struct sock *sk = sock->sk;
969 	struct llc_sock *llc = llc_sk(sk);
970 	int val = 0, len = 0, rc = -EINVAL;
971 
972 	lock_sock(sk);
973 	if (level != SOL_LLC)
974 		goto out;
975 	rc = get_user(len, optlen);
976 	if (rc)
977 		goto out;
978 	rc = -EINVAL;
979 	if (len != sizeof(int))
980 		goto out;
981 	switch (optname) {
982 	case LLC_OPT_RETRY:
983 		val = llc->n2;				break;
984 	case LLC_OPT_SIZE:
985 		val = llc->n1;				break;
986 	case LLC_OPT_ACK_TMR_EXP:
987 		val = llc->ack_timer.expire;		break;
988 	case LLC_OPT_P_TMR_EXP:
989 		val = llc->pf_cycle_timer.expire;	break;
990 	case LLC_OPT_REJ_TMR_EXP:
991 		val = llc->rej_sent_timer.expire;	break;
992 	case LLC_OPT_BUSY_TMR_EXP:
993 		val = llc->busy_state_timer.expire;	break;
994 	case LLC_OPT_TX_WIN:
995 		val = llc->k;				break;
996 	case LLC_OPT_RX_WIN:
997 		val = llc->rw;				break;
998 	default:
999 		rc = -ENOPROTOOPT;
1000 		goto out;
1001 	}
1002 	rc = 0;
1003 	if (put_user(len, optlen) || copy_to_user(optval, &val, len))
1004 		rc = -EFAULT;
1005 out:
1006 	release_sock(sk);
1007 	return rc;
1008 }
1009 
1010 static struct net_proto_family llc_ui_family_ops = {
1011 	.family = PF_LLC,
1012 	.create = llc_ui_create,
1013 	.owner	= THIS_MODULE,
1014 };
1015 
1016 static struct proto_ops llc_ui_ops = {
1017 	.family	     = PF_LLC,
1018 	.owner       = THIS_MODULE,
1019 	.release     = llc_ui_release,
1020 	.bind	     = llc_ui_bind,
1021 	.connect     = llc_ui_connect,
1022 	.socketpair  = sock_no_socketpair,
1023 	.accept      = llc_ui_accept,
1024 	.getname     = llc_ui_getname,
1025 	.poll	     = datagram_poll,
1026 	.ioctl       = llc_ui_ioctl,
1027 	.listen      = llc_ui_listen,
1028 	.shutdown    = llc_ui_shutdown,
1029 	.setsockopt  = llc_ui_setsockopt,
1030 	.getsockopt  = llc_ui_getsockopt,
1031 	.sendmsg     = llc_ui_sendmsg,
1032 	.recvmsg     = llc_ui_recvmsg,
1033 	.mmap	     = sock_no_mmap,
1034 	.sendpage    = sock_no_sendpage,
1035 };
1036 
1037 extern void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb);
1038 extern void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb);
1039 
1040 static int __init llc2_init(void)
1041 {
1042 	int rc = proto_register(&llc_proto, 0);
1043 
1044 	if (rc != 0)
1045 		goto out;
1046 
1047 	llc_build_offset_table();
1048 	llc_station_init();
1049 	llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
1050 	rc = llc_proc_init();
1051 	if (rc != 0)
1052 		goto out_unregister_llc_proto;
1053 	sock_register(&llc_ui_family_ops);
1054 	llc_add_pack(LLC_DEST_SAP, llc_sap_handler);
1055 	llc_add_pack(LLC_DEST_CONN, llc_conn_handler);
1056 out:
1057 	return rc;
1058 out_unregister_llc_proto:
1059 	proto_unregister(&llc_proto);
1060 	goto out;
1061 }
1062 
1063 static void __exit llc2_exit(void)
1064 {
1065 	llc_station_exit();
1066 	llc_remove_pack(LLC_DEST_SAP);
1067 	llc_remove_pack(LLC_DEST_CONN);
1068 	sock_unregister(PF_LLC);
1069 	llc_proc_exit();
1070 	proto_unregister(&llc_proto);
1071 }
1072 
1073 module_init(llc2_init);
1074 module_exit(llc2_exit);
1075 
1076 MODULE_LICENSE("GPL");
1077 MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
1078 MODULE_DESCRIPTION("IEEE 802.2 PF_LLC support");
1079 MODULE_ALIAS_NETPROTO(PF_LLC);
1080