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