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