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