2 * af_llc.c - LLC User Interface SAPs
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
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
13 * Copyright (c) 2001 by Jay Schulist <jschlst@samba.org>
14 * 2002-2003 by Arnaldo Carvalho de Melo <acme@conectiva.com.br>
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.
21 * See the GNU General Public License for more details.
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>
30 #include <net/llc_sap.h>
31 #include <net/llc_pdu.h>
32 #include <net/llc_conn.h>
33 #include <net/tcp_states.h>
35 /* remember: uninitialized global data is zeroed because its in .bss */
36 static u16 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
37 static u16 llc_ui_sap_link_no_max[256];
38 static struct sockaddr_llc llc_ui_addrnull;
39 static const struct proto_ops llc_ui_ops;
41 static bool llc_ui_wait_for_conn(struct sock *sk, long timeout);
42 static int llc_ui_wait_for_disc(struct sock *sk, long timeout);
43 static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout);
46 #define dprintk(args...) printk(KERN_DEBUG args)
48 #define dprintk(args...)
51 /* Maybe we'll add some more in the future. */
52 #define LLC_CMSG_PKTINFO 1
56 * llc_ui_next_link_no - return the next unused link number for a sap
57 * @sap: Address of sap to get link number from.
59 * Return the next unused link number for a given sap.
61 static inline u16 llc_ui_next_link_no(int sap)
63 return llc_ui_sap_link_no_max[sap]++;
67 * llc_proto_type - return eth protocol for ARP header type
68 * @arphrd: ARP header type.
70 * Given an ARP header type return the corresponding ethernet protocol.
72 static inline __be16 llc_proto_type(u16 arphrd)
74 return htons(ETH_P_802_2);
78 * llc_ui_addr_null - determines if a address structure is null
79 * @addr: Address to test if null.
81 static inline u8 llc_ui_addr_null(struct sockaddr_llc *addr)
83 return !memcmp(addr, &llc_ui_addrnull, sizeof(*addr));
87 * llc_ui_header_len - return length of llc header based on operation
88 * @sk: Socket which contains a valid llc socket type.
89 * @addr: Complete sockaddr_llc structure received from the user.
91 * Provide the length of the llc header depending on what kind of
92 * operation the user would like to perform and the type of socket.
93 * Returns the correct llc header length.
95 static inline u8 llc_ui_header_len(struct sock *sk, struct sockaddr_llc *addr)
97 u8 rc = LLC_PDU_LEN_U;
101 else if (addr->sllc_xid)
102 /* We need to expand header to sizeof(struct llc_xid_info)
103 * since llc_pdu_init_as_xid_cmd() sets 4,5,6 bytes of LLC header
104 * as XID PDU. In llc_ui_sendmsg() we reserved header size and then
105 * filled all other space with user data. If we won't reserve this
106 * bytes, llc_pdu_init_as_xid_cmd() will overwrite user data
108 rc = LLC_PDU_LEN_U_XID;
109 else if (sk->sk_type == SOCK_STREAM)
115 * llc_ui_send_data - send data via reliable llc2 connection
116 * @sk: Connection the socket is using.
117 * @skb: Data the user wishes to send.
118 * @noblock: can we block waiting for data?
120 * Send data via reliable llc2 connection.
121 * Returns 0 upon success, non-zero if action did not succeed.
123 * This function always consumes a reference to the skb.
125 static int llc_ui_send_data(struct sock* sk, struct sk_buff *skb, int noblock)
127 struct llc_sock* llc = llc_sk(sk);
129 if (unlikely(llc_data_accept_state(llc->state) ||
130 llc->remote_busy_flag ||
132 long timeout = sock_sndtimeo(sk, noblock);
135 rc = llc_ui_wait_for_busy_core(sk, timeout);
141 return llc_build_and_send_pkt(sk, skb);
144 static void llc_ui_sk_init(struct socket *sock, struct sock *sk)
146 sock_graft(sk, sock);
147 sk->sk_type = sock->type;
148 sock->ops = &llc_ui_ops;
151 static struct proto llc_proto = {
153 .owner = THIS_MODULE,
154 .obj_size = sizeof(struct llc_sock),
155 .slab_flags = SLAB_DESTROY_BY_RCU,
159 * llc_ui_create - alloc and init a new llc_ui socket
160 * @net: network namespace (must be default network)
161 * @sock: Socket to initialize and attach allocated sk to.
163 * @kern: on behalf of kernel or userspace
165 * Allocate and initialize a new llc_ui socket, validate the user wants a
166 * socket type we have available.
167 * Returns 0 upon success, negative upon failure.
169 static int llc_ui_create(struct net *net, struct socket *sock, int protocol,
173 int rc = -ESOCKTNOSUPPORT;
175 if (!ns_capable(net->user_ns, CAP_NET_RAW))
178 if (!net_eq(net, &init_net))
179 return -EAFNOSUPPORT;
181 if (likely(sock->type == SOCK_DGRAM || sock->type == SOCK_STREAM)) {
183 sk = llc_sk_alloc(net, PF_LLC, GFP_KERNEL, &llc_proto, kern);
186 llc_ui_sk_init(sock, sk);
193 * llc_ui_release - shutdown socket
194 * @sock: Socket to release.
196 * Shutdown and deallocate an existing socket.
198 static int llc_ui_release(struct socket *sock)
200 struct sock *sk = sock->sk;
201 struct llc_sock *llc;
203 if (unlikely(sk == NULL))
208 dprintk("%s: closing local(%02X) remote(%02X)\n", __func__,
209 llc->laddr.lsap, llc->daddr.lsap);
210 if (!llc_send_disc(sk))
211 llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
212 if (!sock_flag(sk, SOCK_ZAPPED)) {
213 struct llc_sap *sap = llc->sap;
215 /* Hold this for release_sock(), so that llc_backlog_rcv()
216 * could still use it.
219 llc_sap_remove_socket(llc->sap, sk);
234 * llc_ui_autoport - provide dynamically allocate SAP number
236 * Provide the caller with a dynamically allocated SAP number according
237 * to the rules that are set in this function. Returns: 0, upon failure,
238 * SAP number otherwise.
240 static int llc_ui_autoport(void)
245 while (tries < LLC_SAP_DYN_TRIES) {
246 for (i = llc_ui_sap_last_autoport;
247 i < LLC_SAP_DYN_STOP; i += 2) {
248 sap = llc_sap_find(i);
250 llc_ui_sap_last_autoport = i + 2;
255 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
264 * llc_ui_autobind - automatically bind a socket to a sap
265 * @sock: socket to bind
266 * @addr: address to connect to
268 * Used by llc_ui_connect and llc_ui_sendmsg when the user hasn't
269 * specifically used llc_ui_bind to bind to an specific address/sap
271 * Returns: 0 upon success, negative otherwise.
273 static int llc_ui_autobind(struct socket *sock, struct sockaddr_llc *addr)
275 struct sock *sk = sock->sk;
276 struct llc_sock *llc = llc_sk(sk);
277 struct net_device *dev = NULL;
281 if (!sock_flag(sk, SOCK_ZAPPED))
283 if (!addr->sllc_arphrd)
284 addr->sllc_arphrd = ARPHRD_ETHER;
285 if (addr->sllc_arphrd != ARPHRD_ETHER)
288 if (sk->sk_bound_dev_if) {
289 dev = dev_get_by_index(&init_net, sk->sk_bound_dev_if);
290 if (dev && addr->sllc_arphrd != dev->type) {
295 dev = dev_getfirstbyhwtype(&init_net, addr->sllc_arphrd);
299 llc->laddr.lsap = llc_ui_autoport();
300 if (!llc->laddr.lsap)
302 rc = -EBUSY; /* some other network layer is using the sap */
303 sap = llc_sap_open(llc->laddr.lsap, NULL);
307 /* Note: We do not expect errors from this point. */
311 memcpy(llc->laddr.mac, llc->dev->dev_addr, IFHWADDRLEN);
312 memcpy(&llc->addr, addr, sizeof(llc->addr));
313 /* assign new connection to its SAP */
314 llc_sap_add_socket(sap, sk);
315 sock_reset_flag(sk, SOCK_ZAPPED);
323 * llc_ui_bind - bind a socket to a specific address.
324 * @sock: Socket to bind an address to.
325 * @uaddr: Address the user wants the socket bound to.
326 * @addrlen: Length of the uaddr structure.
328 * Bind a socket to a specific address. For llc a user is able to bind to
329 * a specific sap only or mac + sap.
330 * If the user desires to bind to a specific mac + sap, it is possible to
331 * have multiple sap connections via multiple macs.
332 * Bind and autobind for that matter must enforce the correct sap usage
333 * otherwise all hell will break loose.
334 * Returns: 0 upon success, negative otherwise.
336 static int llc_ui_bind(struct socket *sock, struct sockaddr *uaddr, int addrlen)
338 struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
339 struct sock *sk = sock->sk;
340 struct llc_sock *llc = llc_sk(sk);
341 struct net_device *dev = NULL;
345 dprintk("%s: binding %02X\n", __func__, addr->sllc_sap);
348 if (unlikely(!sock_flag(sk, SOCK_ZAPPED) || addrlen != sizeof(*addr)))
351 if (!addr->sllc_arphrd)
352 addr->sllc_arphrd = ARPHRD_ETHER;
353 if (unlikely(addr->sllc_family != AF_LLC || addr->sllc_arphrd != ARPHRD_ETHER))
357 if (sk->sk_bound_dev_if) {
358 dev = dev_get_by_index_rcu(&init_net, sk->sk_bound_dev_if);
360 if (is_zero_ether_addr(addr->sllc_mac))
361 memcpy(addr->sllc_mac, dev->dev_addr,
363 if (addr->sllc_arphrd != dev->type ||
364 !ether_addr_equal(addr->sllc_mac,
371 dev = dev_getbyhwaddr_rcu(&init_net, addr->sllc_arphrd,
379 if (!addr->sllc_sap) {
381 addr->sllc_sap = llc_ui_autoport();
385 sap = llc_sap_find(addr->sllc_sap);
387 sap = llc_sap_open(addr->sllc_sap, NULL);
388 rc = -EBUSY; /* some other network layer is using the sap */
392 struct llc_addr laddr, daddr;
395 memset(&laddr, 0, sizeof(laddr));
396 memset(&daddr, 0, sizeof(daddr));
398 * FIXME: check if the address is multicast,
399 * only SOCK_DGRAM can do this.
401 memcpy(laddr.mac, addr->sllc_mac, IFHWADDRLEN);
402 laddr.lsap = addr->sllc_sap;
403 rc = -EADDRINUSE; /* mac + sap clash. */
404 ask = llc_lookup_established(sap, &daddr, &laddr);
411 /* Note: We do not expect errors from this point. */
415 llc->laddr.lsap = addr->sllc_sap;
416 memcpy(llc->laddr.mac, addr->sllc_mac, IFHWADDRLEN);
417 memcpy(&llc->addr, addr, sizeof(llc->addr));
418 /* assign new connection to its SAP */
419 llc_sap_add_socket(sap, sk);
420 sock_reset_flag(sk, SOCK_ZAPPED);
431 * llc_ui_shutdown - shutdown a connect llc2 socket.
432 * @sock: Socket to shutdown.
433 * @how: What part of the socket to shutdown.
435 * Shutdown a connected llc2 socket. Currently this function only supports
436 * shutting down both sends and receives (2), we could probably make this
437 * function such that a user can shutdown only half the connection but not
439 * Returns: 0 upon success, negative otherwise.
441 static int llc_ui_shutdown(struct socket *sock, int how)
443 struct sock *sk = sock->sk;
447 if (unlikely(sk->sk_state != TCP_ESTABLISHED))
452 rc = llc_send_disc(sk);
454 rc = llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
455 /* Wake up anyone sleeping in poll */
456 sk->sk_state_change(sk);
463 * llc_ui_connect - Connect to a remote llc2 mac + sap.
464 * @sock: Socket which will be connected to the remote destination.
465 * @uaddr: Remote and possibly the local address of the new connection.
466 * @addrlen: Size of uaddr structure.
467 * @flags: Operational flags specified by the user.
469 * Connect to a remote llc2 mac + sap. The caller must specify the
470 * destination mac and address to connect to. If the user hasn't previously
471 * called bind(2) with a smac the address of the first interface of the
472 * specified arp type will be used.
473 * This function will autobind if user did not previously call bind.
474 * Returns: 0 upon success, negative otherwise.
476 static int llc_ui_connect(struct socket *sock, struct sockaddr *uaddr,
477 int addrlen, int flags)
479 struct sock *sk = sock->sk;
480 struct llc_sock *llc = llc_sk(sk);
481 struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr;
485 if (unlikely(addrlen != sizeof(*addr)))
488 if (unlikely(addr->sllc_family != AF_LLC))
490 if (unlikely(sk->sk_type != SOCK_STREAM))
493 if (unlikely(sock->state == SS_CONNECTING))
495 /* bind connection to sap if user hasn't done it. */
496 if (sock_flag(sk, SOCK_ZAPPED)) {
497 /* bind to sap with null dev, exclusive */
498 rc = llc_ui_autobind(sock, addr);
502 llc->daddr.lsap = addr->sllc_sap;
503 memcpy(llc->daddr.mac, addr->sllc_mac, IFHWADDRLEN);
504 sock->state = SS_CONNECTING;
505 sk->sk_state = TCP_SYN_SENT;
506 llc->link = llc_ui_next_link_no(llc->sap->laddr.lsap);
507 rc = llc_establish_connection(sk, llc->dev->dev_addr,
508 addr->sllc_mac, addr->sllc_sap);
510 dprintk("%s: llc_ui_send_conn failed :-(\n", __func__);
511 sock->state = SS_UNCONNECTED;
512 sk->sk_state = TCP_CLOSE;
516 if (sk->sk_state == TCP_SYN_SENT) {
517 const long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
519 if (!timeo || !llc_ui_wait_for_conn(sk, timeo))
522 rc = sock_intr_errno(timeo);
523 if (signal_pending(current))
527 if (sk->sk_state == TCP_CLOSE)
530 sock->state = SS_CONNECTED;
536 rc = sock_error(sk) ? : -ECONNABORTED;
537 sock->state = SS_UNCONNECTED;
542 * llc_ui_listen - allow a normal socket to accept incoming connections
543 * @sock: Socket to allow incoming connections on.
544 * @backlog: Number of connections to queue.
546 * Allow a normal socket to accept incoming connections.
547 * Returns 0 upon success, negative otherwise.
549 static int llc_ui_listen(struct socket *sock, int backlog)
551 struct sock *sk = sock->sk;
555 if (unlikely(sock->state != SS_UNCONNECTED))
558 if (unlikely(sk->sk_type != SOCK_STREAM))
561 if (sock_flag(sk, SOCK_ZAPPED))
564 if (!(unsigned int)backlog) /* BSDism */
566 sk->sk_max_ack_backlog = backlog;
567 if (sk->sk_state != TCP_LISTEN) {
568 sk->sk_ack_backlog = 0;
569 sk->sk_state = TCP_LISTEN;
571 sk->sk_socket->flags |= __SO_ACCEPTCON;
577 static int llc_ui_wait_for_disc(struct sock *sk, long timeout)
583 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
584 if (sk_wait_event(sk, &timeout, sk->sk_state == TCP_CLOSE))
587 if (signal_pending(current))
594 finish_wait(sk_sleep(sk), &wait);
598 static bool llc_ui_wait_for_conn(struct sock *sk, long timeout)
603 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
604 if (sk_wait_event(sk, &timeout, sk->sk_state != TCP_SYN_SENT))
606 if (signal_pending(current) || !timeout)
609 finish_wait(sk_sleep(sk), &wait);
613 static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout)
616 struct llc_sock *llc = llc_sk(sk);
620 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
622 if (sk_wait_event(sk, &timeout,
623 (sk->sk_shutdown & RCV_SHUTDOWN) ||
624 (!llc_data_accept_state(llc->state) &&
625 !llc->remote_busy_flag &&
629 if (signal_pending(current))
635 finish_wait(sk_sleep(sk), &wait);
639 static int llc_wait_data(struct sock *sk, long timeo)
645 * POSIX 1003.1g mandates this order.
651 if (sk->sk_shutdown & RCV_SHUTDOWN)
656 rc = sock_intr_errno(timeo);
657 if (signal_pending(current))
660 if (sk_wait_data(sk, &timeo, NULL))
666 static void llc_cmsg_rcv(struct msghdr *msg, struct sk_buff *skb)
668 struct llc_sock *llc = llc_sk(skb->sk);
670 if (llc->cmsg_flags & LLC_CMSG_PKTINFO) {
671 struct llc_pktinfo info;
673 memset(&info, 0, sizeof(info));
674 info.lpi_ifindex = llc_sk(skb->sk)->dev->ifindex;
675 llc_pdu_decode_dsap(skb, &info.lpi_sap);
676 llc_pdu_decode_da(skb, info.lpi_mac);
677 put_cmsg(msg, SOL_LLC, LLC_OPT_PKTINFO, sizeof(info), &info);
682 * llc_ui_accept - accept a new incoming connection.
683 * @sock: Socket which connections arrive on.
684 * @newsock: Socket to move incoming connection to.
685 * @flags: User specified operational flags.
687 * Accept a new incoming connection.
688 * Returns 0 upon success, negative otherwise.
690 static int llc_ui_accept(struct socket *sock, struct socket *newsock, int flags)
692 struct sock *sk = sock->sk, *newsk;
693 struct llc_sock *llc, *newllc;
695 int rc = -EOPNOTSUPP;
697 dprintk("%s: accepting on %02X\n", __func__,
698 llc_sk(sk)->laddr.lsap);
700 if (unlikely(sk->sk_type != SOCK_STREAM))
703 if (unlikely(sock->state != SS_UNCONNECTED ||
704 sk->sk_state != TCP_LISTEN))
706 /* wait for a connection to arrive. */
707 if (skb_queue_empty(&sk->sk_receive_queue)) {
708 rc = llc_wait_data(sk, sk->sk_rcvtimeo);
712 dprintk("%s: got a new connection on %02X\n", __func__,
713 llc_sk(sk)->laddr.lsap);
714 skb = skb_dequeue(&sk->sk_receive_queue);
720 /* attach connection to a new socket. */
721 llc_ui_sk_init(newsock, newsk);
722 sock_reset_flag(newsk, SOCK_ZAPPED);
723 newsk->sk_state = TCP_ESTABLISHED;
724 newsock->state = SS_CONNECTED;
726 newllc = llc_sk(newsk);
727 memcpy(&newllc->addr, &llc->addr, sizeof(newllc->addr));
728 newllc->link = llc_ui_next_link_no(newllc->laddr.lsap);
730 /* put original socket back into a clean listen state. */
731 sk->sk_state = TCP_LISTEN;
732 sk->sk_ack_backlog--;
733 dprintk("%s: ok success on %02X, client on %02X\n", __func__,
734 llc_sk(sk)->addr.sllc_sap, newllc->daddr.lsap);
743 * llc_ui_recvmsg - copy received data to the socket user.
744 * @sock: Socket to copy data from.
745 * @msg: Various user space related information.
746 * @len: Size of user buffer.
747 * @flags: User specified flags.
749 * Copy received data to the socket user.
750 * Returns non-negative upon success, negative otherwise.
752 static int llc_ui_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
755 DECLARE_SOCKADDR(struct sockaddr_llc *, uaddr, msg->msg_name);
756 const int nonblock = flags & MSG_DONTWAIT;
757 struct sk_buff *skb = NULL;
758 struct sock *sk = sock->sk;
759 struct llc_sock *llc = llc_sk(sk);
764 int target; /* Read at least this many bytes */
769 if (unlikely(sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN))
772 timeo = sock_rcvtimeo(sk, nonblock);
774 seq = &llc->copied_seq;
775 if (flags & MSG_PEEK) {
776 peek_seq = llc->copied_seq;
780 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
787 * We need to check signals first, to get correct SIGURG
788 * handling. FIXME: Need to check this doesn't impact 1003.1g
789 * and move it down to the bottom of the loop
791 if (signal_pending(current)) {
794 copied = timeo ? sock_intr_errno(timeo) : -EAGAIN;
798 /* Next get a buffer. */
800 skb = skb_peek(&sk->sk_receive_queue);
805 /* Well, if we have backlog, try to process it now yet. */
807 if (copied >= target && !sk->sk_backlog.tail)
812 sk->sk_state == TCP_CLOSE ||
813 (sk->sk_shutdown & RCV_SHUTDOWN) ||
818 if (sock_flag(sk, SOCK_DONE))
822 copied = sock_error(sk);
825 if (sk->sk_shutdown & RCV_SHUTDOWN)
828 if (sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_CLOSE) {
829 if (!sock_flag(sk, SOCK_DONE)) {
831 * This occurs when user tries to read
832 * from never connected socket.
845 if (copied >= target) { /* Do not sleep, just process backlog. */
849 sk_wait_data(sk, &timeo, NULL);
851 if ((flags & MSG_PEEK) && peek_seq != llc->copied_seq) {
852 net_dbg_ratelimited("LLC(%s:%d): Application bug, race in MSG_PEEK\n",
854 task_pid_nr(current));
855 peek_seq = llc->copied_seq;
860 /* Ok so how much can we use? */
861 used = skb->len - offset;
865 if (!(flags & MSG_TRUNC)) {
866 int rc = skb_copy_datagram_msg(skb, offset, msg, used);
868 /* Exception. Bailout! */
879 /* For non stream protcols we get one packet per recvmsg call */
880 if (sk->sk_type != SOCK_STREAM)
883 if (!(flags & MSG_PEEK)) {
884 skb_unlink(skb, &sk->sk_receive_queue);
890 if (used + offset < skb_len)
898 if (uaddr != NULL && skb != NULL) {
899 memcpy(uaddr, llc_ui_skb_cb(skb), sizeof(*uaddr));
900 msg->msg_namelen = sizeof(*uaddr);
902 if (llc_sk(sk)->cmsg_flags)
903 llc_cmsg_rcv(msg, skb);
905 if (!(flags & MSG_PEEK)) {
906 skb_unlink(skb, &sk->sk_receive_queue);
915 * llc_ui_sendmsg - Transmit data provided by the socket user.
916 * @sock: Socket to transmit data from.
917 * @msg: Various user related information.
918 * @len: Length of data to transmit.
920 * Transmit data provided by the socket user.
921 * Returns non-negative upon success, negative otherwise.
923 static int llc_ui_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
925 struct sock *sk = sock->sk;
926 struct llc_sock *llc = llc_sk(sk);
927 DECLARE_SOCKADDR(struct sockaddr_llc *, addr, msg->msg_name);
928 int flags = msg->msg_flags;
929 int noblock = flags & MSG_DONTWAIT;
930 struct sk_buff *skb = NULL;
932 int rc = -EINVAL, copied = 0, hdrlen;
934 dprintk("%s: sending from %02X to %02X\n", __func__,
935 llc->laddr.lsap, llc->daddr.lsap);
938 if (msg->msg_namelen < sizeof(*addr))
941 if (llc_ui_addr_null(&llc->addr))
945 /* must bind connection to sap if user hasn't done it. */
946 if (sock_flag(sk, SOCK_ZAPPED)) {
947 /* bind to sap with null dev, exclusive. */
948 rc = llc_ui_autobind(sock, addr);
952 hdrlen = llc->dev->hard_header_len + llc_ui_header_len(sk, addr);
954 if (size > llc->dev->mtu)
955 size = llc->dev->mtu;
956 copied = size - hdrlen;
961 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
966 skb->protocol = llc_proto_type(addr->sllc_arphrd);
967 skb_reserve(skb, hdrlen);
968 rc = memcpy_from_msg(skb_put(skb, copied), msg, copied);
971 if (sk->sk_type == SOCK_DGRAM || addr->sllc_ua) {
972 llc_build_and_send_ui_pkt(llc->sap, skb, addr->sllc_mac,
977 if (addr->sllc_test) {
978 llc_build_and_send_test_pkt(llc->sap, skb, addr->sllc_mac,
983 if (addr->sllc_xid) {
984 llc_build_and_send_xid_pkt(llc->sap, skb, addr->sllc_mac,
990 if (!(sk->sk_type == SOCK_STREAM && !addr->sllc_ua))
992 rc = llc_ui_send_data(sk, skb, noblock);
997 dprintk("%s: failed sending from %02X to %02X: %d\n",
998 __func__, llc->laddr.lsap, llc->daddr.lsap, rc);
1000 return rc ? : copied;
1004 * llc_ui_getname - return the address info of a socket
1005 * @sock: Socket to get address of.
1006 * @uaddr: Address structure to return information.
1007 * @uaddrlen: Length of address structure.
1008 * @peer: Does user want local or remote address information.
1010 * Return the address information of a socket.
1012 static int llc_ui_getname(struct socket *sock, struct sockaddr *uaddr,
1013 int *uaddrlen, int peer)
1015 struct sockaddr_llc sllc;
1016 struct sock *sk = sock->sk;
1017 struct llc_sock *llc = llc_sk(sk);
1020 memset(&sllc, 0, sizeof(sllc));
1022 if (sock_flag(sk, SOCK_ZAPPED))
1024 *uaddrlen = sizeof(sllc);
1027 if (sk->sk_state != TCP_ESTABLISHED)
1030 sllc.sllc_arphrd = llc->dev->type;
1031 sllc.sllc_sap = llc->daddr.lsap;
1032 memcpy(&sllc.sllc_mac, &llc->daddr.mac, IFHWADDRLEN);
1037 sllc.sllc_sap = llc->sap->laddr.lsap;
1040 sllc.sllc_arphrd = llc->dev->type;
1041 memcpy(&sllc.sllc_mac, llc->dev->dev_addr,
1046 sllc.sllc_family = AF_LLC;
1047 memcpy(uaddr, &sllc, sizeof(sllc));
1054 * llc_ui_ioctl - io controls for PF_LLC
1055 * @sock: Socket to get/set info
1057 * @arg: optional argument for cmd
1059 * get/set info on llc sockets
1061 static int llc_ui_ioctl(struct socket *sock, unsigned int cmd,
1064 return -ENOIOCTLCMD;
1068 * llc_ui_setsockopt - set various connection specific parameters.
1069 * @sock: Socket to set options on.
1070 * @level: Socket level user is requesting operations on.
1071 * @optname: Operation name.
1072 * @optval: User provided operation data.
1073 * @optlen: Length of optval.
1075 * Set various connection specific parameters.
1077 static int llc_ui_setsockopt(struct socket *sock, int level, int optname,
1078 char __user *optval, unsigned int optlen)
1080 struct sock *sk = sock->sk;
1081 struct llc_sock *llc = llc_sk(sk);
1086 if (unlikely(level != SOL_LLC || optlen != sizeof(int)))
1088 rc = get_user(opt, (int __user *)optval);
1094 if (opt > LLC_OPT_MAX_RETRY)
1099 if (opt > LLC_OPT_MAX_SIZE)
1103 case LLC_OPT_ACK_TMR_EXP:
1104 if (opt > LLC_OPT_MAX_ACK_TMR_EXP)
1106 llc->ack_timer.expire = opt * HZ;
1108 case LLC_OPT_P_TMR_EXP:
1109 if (opt > LLC_OPT_MAX_P_TMR_EXP)
1111 llc->pf_cycle_timer.expire = opt * HZ;
1113 case LLC_OPT_REJ_TMR_EXP:
1114 if (opt > LLC_OPT_MAX_REJ_TMR_EXP)
1116 llc->rej_sent_timer.expire = opt * HZ;
1118 case LLC_OPT_BUSY_TMR_EXP:
1119 if (opt > LLC_OPT_MAX_BUSY_TMR_EXP)
1121 llc->busy_state_timer.expire = opt * HZ;
1123 case LLC_OPT_TX_WIN:
1124 if (opt > LLC_OPT_MAX_WIN)
1128 case LLC_OPT_RX_WIN:
1129 if (opt > LLC_OPT_MAX_WIN)
1133 case LLC_OPT_PKTINFO:
1135 llc->cmsg_flags |= LLC_CMSG_PKTINFO;
1137 llc->cmsg_flags &= ~LLC_CMSG_PKTINFO;
1150 * llc_ui_getsockopt - get connection specific socket info
1151 * @sock: Socket to get information from.
1152 * @level: Socket level user is requesting operations on.
1153 * @optname: Operation name.
1154 * @optval: Variable to return operation data in.
1155 * @optlen: Length of optval.
1157 * Get connection specific socket information.
1159 static int llc_ui_getsockopt(struct socket *sock, int level, int optname,
1160 char __user *optval, int __user *optlen)
1162 struct sock *sk = sock->sk;
1163 struct llc_sock *llc = llc_sk(sk);
1164 int val = 0, len = 0, rc = -EINVAL;
1167 if (unlikely(level != SOL_LLC))
1169 rc = get_user(len, optlen);
1173 if (len != sizeof(int))
1177 val = llc->n2; break;
1179 val = llc->n1; break;
1180 case LLC_OPT_ACK_TMR_EXP:
1181 val = llc->ack_timer.expire / HZ; break;
1182 case LLC_OPT_P_TMR_EXP:
1183 val = llc->pf_cycle_timer.expire / HZ; break;
1184 case LLC_OPT_REJ_TMR_EXP:
1185 val = llc->rej_sent_timer.expire / HZ; break;
1186 case LLC_OPT_BUSY_TMR_EXP:
1187 val = llc->busy_state_timer.expire / HZ; break;
1188 case LLC_OPT_TX_WIN:
1189 val = llc->k; break;
1190 case LLC_OPT_RX_WIN:
1191 val = llc->rw; break;
1192 case LLC_OPT_PKTINFO:
1193 val = (llc->cmsg_flags & LLC_CMSG_PKTINFO) != 0;
1200 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
1207 static const struct net_proto_family llc_ui_family_ops = {
1209 .create = llc_ui_create,
1210 .owner = THIS_MODULE,
1213 static const struct proto_ops llc_ui_ops = {
1215 .owner = THIS_MODULE,
1216 .release = llc_ui_release,
1217 .bind = llc_ui_bind,
1218 .connect = llc_ui_connect,
1219 .socketpair = sock_no_socketpair,
1220 .accept = llc_ui_accept,
1221 .getname = llc_ui_getname,
1222 .poll = datagram_poll,
1223 .ioctl = llc_ui_ioctl,
1224 .listen = llc_ui_listen,
1225 .shutdown = llc_ui_shutdown,
1226 .setsockopt = llc_ui_setsockopt,
1227 .getsockopt = llc_ui_getsockopt,
1228 .sendmsg = llc_ui_sendmsg,
1229 .recvmsg = llc_ui_recvmsg,
1230 .mmap = sock_no_mmap,
1231 .sendpage = sock_no_sendpage,
1234 static const char llc_proc_err_msg[] __initconst =
1235 KERN_CRIT "LLC: Unable to register the proc_fs entries\n";
1236 static const char llc_sysctl_err_msg[] __initconst =
1237 KERN_CRIT "LLC: Unable to register the sysctl entries\n";
1238 static const char llc_sock_err_msg[] __initconst =
1239 KERN_CRIT "LLC: Unable to register the network family\n";
1241 static int __init llc2_init(void)
1243 int rc = proto_register(&llc_proto, 0);
1248 llc_build_offset_table();
1250 llc_ui_sap_last_autoport = LLC_SAP_DYN_START;
1251 rc = llc_proc_init();
1253 printk(llc_proc_err_msg);
1256 rc = llc_sysctl_init();
1258 printk(llc_sysctl_err_msg);
1261 rc = sock_register(&llc_ui_family_ops);
1263 printk(llc_sock_err_msg);
1266 llc_add_pack(LLC_DEST_SAP, llc_sap_handler);
1267 llc_add_pack(LLC_DEST_CONN, llc_conn_handler);
1276 proto_unregister(&llc_proto);
1280 static void __exit llc2_exit(void)
1283 llc_remove_pack(LLC_DEST_SAP);
1284 llc_remove_pack(LLC_DEST_CONN);
1285 sock_unregister(PF_LLC);
1288 proto_unregister(&llc_proto);
1291 module_init(llc2_init);
1292 module_exit(llc2_exit);
1294 MODULE_LICENSE("GPL");
1295 MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003");
1296 MODULE_DESCRIPTION("IEEE 802.2 PF_LLC support");
1297 MODULE_ALIAS_NETPROTO(PF_LLC);