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