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