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> 24 #include <linux/compiler.h> 25 #include <linux/kernel.h> 26 #include <linux/module.h> 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> 33 #include <net/tcp_states.h> 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 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); 43 static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout); 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 119 if (unlikely(llc_data_accept_state(llc->state) || llc->p_flag)) { 120 long timeout = sock_sndtimeo(sk, noblock); 121 122 rc = llc_ui_wait_for_busy_core(sk, timeout); 123 } 124 if (unlikely(!rc)) 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 158 if (likely(sock->type == SOCK_DGRAM || sock->type == SOCK_STREAM)) { 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 180 if (unlikely(sk == NULL)) 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); 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 } 220 llc_sap_put(sap); 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 /** 231 * llc_ui_autobind - automatically bind a socket to a sap 232 * @sock: socket to bind 233 * @addr: address to connect to 234 * 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 * 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 278 * a specific sap only or mac + sap. 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); 294 if (unlikely(!sock_flag(sk, SOCK_ZAPPED) || addrlen != sizeof(*addr))) 295 goto out; 296 rc = -EAFNOSUPPORT; 297 if (unlikely(addr->sllc_family != AF_LLC)) 298 goto out; 299 if (!addr->sllc_sap) { 300 rc = -EUSERS; 301 addr->sllc_sap = llc_ui_autoport(); 302 if (!addr->sllc_sap) 303 goto out; 304 } 305 sap = llc_sap_find(addr->sllc_sap); 306 if (!sap) { 307 sap = llc_sap_open(addr->sllc_sap, NULL); 308 rc = -EBUSY; /* some other network layer is using the sap */ 309 if (!sap) 310 goto out; 311 llc_sap_hold(sap); 312 } else { 313 struct llc_addr laddr, daddr; 314 struct sock *ask; 315 316 memset(&laddr, 0, sizeof(laddr)); 317 memset(&daddr, 0, sizeof(daddr)); 318 /* 319 * FIXME: check if the the address is multicast, 320 * only SOCK_DGRAM can do this. 321 */ 322 memcpy(laddr.mac, addr->sllc_mac, IFHWADDRLEN); 323 laddr.lsap = addr->sllc_sap; 324 rc = -EADDRINUSE; /* mac + sap clash. */ 325 ask = llc_lookup_established(sap, &daddr, &laddr); 326 if (ask) { 327 sock_put(ask); 328 goto out_put; 329 } 330 } 331 llc->laddr.lsap = addr->sllc_sap; 332 memcpy(llc->laddr.mac, addr->sllc_mac, IFHWADDRLEN); 333 memcpy(&llc->addr, addr, sizeof(llc->addr)); 334 /* assign new connection to its SAP */ 335 llc_sap_add_socket(sap, sk); 336 sock_reset_flag(sk, SOCK_ZAPPED); 337 rc = 0; 338 out_put: 339 llc_sap_put(sap); 340 out: 341 return rc; 342 } 343 344 /** 345 * llc_ui_shutdown - shutdown a connect llc2 socket. 346 * @sock: Socket to shutdown. 347 * @how: What part of the socket to shutdown. 348 * 349 * Shutdown a connected llc2 socket. Currently this function only supports 350 * shutting down both sends and receives (2), we could probably make this 351 * function such that a user can shutdown only half the connection but not 352 * right now. 353 * Returns: 0 upon success, negative otherwise. 354 */ 355 static int llc_ui_shutdown(struct socket *sock, int how) 356 { 357 struct sock *sk = sock->sk; 358 int rc = -ENOTCONN; 359 360 lock_sock(sk); 361 if (unlikely(sk->sk_state != TCP_ESTABLISHED)) 362 goto out; 363 rc = -EINVAL; 364 if (how != 2) 365 goto out; 366 rc = llc_send_disc(sk); 367 if (!rc) 368 rc = llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo); 369 /* Wake up anyone sleeping in poll */ 370 sk->sk_state_change(sk); 371 out: 372 release_sock(sk); 373 return rc; 374 } 375 376 /** 377 * llc_ui_connect - Connect to a remote llc2 mac + sap. 378 * @sock: Socket which will be connected to the remote destination. 379 * @uaddr: Remote and possibly the local address of the new connection. 380 * @addrlen: Size of uaddr structure. 381 * @flags: Operational flags specified by the user. 382 * 383 * Connect to a remote llc2 mac + sap. The caller must specify the 384 * destination mac and address to connect to. If the user hasn't previously 385 * called bind(2) with a smac the address of the first interface of the 386 * specified arp type will be used. 387 * This function will autobind if user did not previously call bind. 388 * Returns: 0 upon success, negative otherwise. 389 */ 390 static int llc_ui_connect(struct socket *sock, struct sockaddr *uaddr, 391 int addrlen, int flags) 392 { 393 struct sock *sk = sock->sk; 394 struct llc_sock *llc = llc_sk(sk); 395 struct sockaddr_llc *addr = (struct sockaddr_llc *)uaddr; 396 int rc = -EINVAL; 397 398 lock_sock(sk); 399 if (unlikely(addrlen != sizeof(*addr))) 400 goto out; 401 rc = -EAFNOSUPPORT; 402 if (unlikely(addr->sllc_family != AF_LLC)) 403 goto out; 404 if (unlikely(sk->sk_type != SOCK_STREAM)) 405 goto out; 406 rc = -EALREADY; 407 if (unlikely(sock->state == SS_CONNECTING)) 408 goto out; 409 /* bind connection to sap if user hasn't done it. */ 410 if (sock_flag(sk, SOCK_ZAPPED)) { 411 /* bind to sap with null dev, exclusive */ 412 rc = llc_ui_autobind(sock, addr); 413 if (rc) 414 goto out; 415 llc->daddr.lsap = addr->sllc_sap; 416 memcpy(llc->daddr.mac, addr->sllc_mac, IFHWADDRLEN); 417 } 418 sock->state = SS_CONNECTING; 419 sk->sk_state = TCP_SYN_SENT; 420 llc->link = llc_ui_next_link_no(llc->sap->laddr.lsap); 421 rc = llc_establish_connection(sk, llc->dev->dev_addr, 422 addr->sllc_mac, addr->sllc_sap); 423 if (rc) { 424 dprintk("%s: llc_ui_send_conn failed :-(\n", __FUNCTION__); 425 sock->state = SS_UNCONNECTED; 426 sk->sk_state = TCP_CLOSE; 427 goto out; 428 } 429 430 if (sk->sk_state == TCP_SYN_SENT) { 431 const long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); 432 433 if (!timeo || !llc_ui_wait_for_conn(sk, timeo)) 434 goto out; 435 436 rc = sock_intr_errno(timeo); 437 if (signal_pending(current)) 438 goto out; 439 } 440 441 if (sk->sk_state == TCP_CLOSE) 442 goto sock_error; 443 444 sock->state = SS_CONNECTED; 445 rc = 0; 446 out: 447 release_sock(sk); 448 return rc; 449 sock_error: 450 rc = sock_error(sk) ? : -ECONNABORTED; 451 sock->state = SS_UNCONNECTED; 452 goto out; 453 } 454 455 /** 456 * llc_ui_listen - allow a normal socket to accept incoming connections 457 * @sock: Socket to allow incoming connections on. 458 * @backlog: Number of connections to queue. 459 * 460 * Allow a normal socket to accept incoming connections. 461 * Returns 0 upon success, negative otherwise. 462 */ 463 static int llc_ui_listen(struct socket *sock, int backlog) 464 { 465 struct sock *sk = sock->sk; 466 int rc = -EINVAL; 467 468 lock_sock(sk); 469 if (unlikely(sock->state != SS_UNCONNECTED)) 470 goto out; 471 rc = -EOPNOTSUPP; 472 if (unlikely(sk->sk_type != SOCK_STREAM)) 473 goto out; 474 rc = -EAGAIN; 475 if (sock_flag(sk, SOCK_ZAPPED)) 476 goto out; 477 rc = 0; 478 if (!(unsigned)backlog) /* BSDism */ 479 backlog = 1; 480 sk->sk_max_ack_backlog = backlog; 481 if (sk->sk_state != TCP_LISTEN) { 482 sk->sk_ack_backlog = 0; 483 sk->sk_state = TCP_LISTEN; 484 } 485 sk->sk_socket->flags |= __SO_ACCEPTCON; 486 out: 487 release_sock(sk); 488 return rc; 489 } 490 491 static int llc_ui_wait_for_disc(struct sock *sk, long timeout) 492 { 493 DEFINE_WAIT(wait); 494 int rc = 0; 495 496 while (1) { 497 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); 498 if (sk_wait_event(sk, &timeout, sk->sk_state == TCP_CLOSE)) 499 break; 500 rc = -ERESTARTSYS; 501 if (signal_pending(current)) 502 break; 503 rc = -EAGAIN; 504 if (!timeout) 505 break; 506 rc = 0; 507 } 508 finish_wait(sk->sk_sleep, &wait); 509 return rc; 510 } 511 512 static int llc_ui_wait_for_conn(struct sock *sk, long timeout) 513 { 514 DEFINE_WAIT(wait); 515 516 while (1) { 517 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); 518 if (sk_wait_event(sk, &timeout, sk->sk_state != TCP_SYN_SENT)) 519 break; 520 if (signal_pending(current) || !timeout) 521 break; 522 } 523 finish_wait(sk->sk_sleep, &wait); 524 return timeout; 525 } 526 527 static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout) 528 { 529 DEFINE_WAIT(wait); 530 struct llc_sock *llc = llc_sk(sk); 531 int rc; 532 533 while (1) { 534 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE); 535 rc = 0; 536 if (sk_wait_event(sk, &timeout, 537 (sk->sk_shutdown & RCV_SHUTDOWN) || 538 (!llc_data_accept_state(llc->state) && 539 !llc->p_flag))) 540 break; 541 rc = -ERESTARTSYS; 542 if (signal_pending(current)) 543 break; 544 rc = -EAGAIN; 545 if (!timeout) 546 break; 547 } 548 finish_wait(sk->sk_sleep, &wait); 549 return rc; 550 } 551 552 int llc_wait_data(struct sock *sk, long timeo) 553 { 554 int rc; 555 556 while (1) { 557 /* 558 * POSIX 1003.1g mandates this order. 559 */ 560 if (sk->sk_err) { 561 rc = sock_error(sk); 562 break; 563 } 564 rc = 0; 565 if (sk->sk_shutdown & RCV_SHUTDOWN) 566 break; 567 rc = -EAGAIN; 568 if (!timeo) 569 break; 570 rc = sock_intr_errno(timeo); 571 if (signal_pending(current)) 572 break; 573 rc = 0; 574 if (sk_wait_data(sk, &timeo)) 575 break; 576 } 577 return rc; 578 } 579 580 /** 581 * llc_ui_accept - accept a new incoming connection. 582 * @sock: Socket which connections arrive on. 583 * @newsock: Socket to move incoming connection to. 584 * @flags: User specified operational flags. 585 * 586 * Accept a new incoming connection. 587 * Returns 0 upon success, negative otherwise. 588 */ 589 static int llc_ui_accept(struct socket *sock, struct socket *newsock, int flags) 590 { 591 struct sock *sk = sock->sk, *newsk; 592 struct llc_sock *llc, *newllc; 593 struct sk_buff *skb; 594 int rc = -EOPNOTSUPP; 595 596 dprintk("%s: accepting on %02X\n", __FUNCTION__, 597 llc_sk(sk)->laddr.lsap); 598 lock_sock(sk); 599 if (unlikely(sk->sk_type != SOCK_STREAM)) 600 goto out; 601 rc = -EINVAL; 602 if (unlikely(sock->state != SS_UNCONNECTED || 603 sk->sk_state != TCP_LISTEN)) 604 goto out; 605 /* wait for a connection to arrive. */ 606 if (skb_queue_empty(&sk->sk_receive_queue)) { 607 rc = llc_wait_data(sk, sk->sk_rcvtimeo); 608 if (rc) 609 goto out; 610 } 611 dprintk("%s: got a new connection on %02X\n", __FUNCTION__, 612 llc_sk(sk)->laddr.lsap); 613 skb = skb_dequeue(&sk->sk_receive_queue); 614 rc = -EINVAL; 615 if (!skb->sk) 616 goto frees; 617 rc = 0; 618 newsk = skb->sk; 619 /* attach connection to a new socket. */ 620 llc_ui_sk_init(newsock, newsk); 621 sock_reset_flag(newsk, SOCK_ZAPPED); 622 newsk->sk_state = TCP_ESTABLISHED; 623 newsock->state = SS_CONNECTED; 624 llc = llc_sk(sk); 625 newllc = llc_sk(newsk); 626 memcpy(&newllc->addr, &llc->addr, sizeof(newllc->addr)); 627 newllc->link = llc_ui_next_link_no(newllc->laddr.lsap); 628 629 /* put original socket back into a clean listen state. */ 630 sk->sk_state = TCP_LISTEN; 631 sk->sk_ack_backlog--; 632 dprintk("%s: ok success on %02X, client on %02X\n", __FUNCTION__, 633 llc_sk(sk)->addr.sllc_sap, newllc->daddr.lsap); 634 frees: 635 kfree_skb(skb); 636 out: 637 release_sock(sk); 638 return rc; 639 } 640 641 /** 642 * llc_ui_recvmsg - copy received data to the socket user. 643 * @sock: Socket to copy data from. 644 * @msg: Various user space related information. 645 * @size: Size of user buffer. 646 * @flags: User specified flags. 647 * 648 * Copy received data to the socket user. 649 * Returns non-negative upon success, negative otherwise. 650 */ 651 static int llc_ui_recvmsg(struct kiocb *iocb, struct socket *sock, 652 struct msghdr *msg, size_t size, int flags) 653 { 654 struct sock *sk = sock->sk; 655 struct sockaddr_llc *uaddr = (struct sockaddr_llc *)msg->msg_name; 656 struct sk_buff *skb; 657 size_t copied = 0; 658 int rc = -ENOMEM; 659 int noblock = flags & MSG_DONTWAIT; 660 661 dprintk("%s: receiving in %02X from %02X\n", __FUNCTION__, 662 llc_sk(sk)->laddr.lsap, llc_sk(sk)->daddr.lsap); 663 lock_sock(sk); 664 if (skb_queue_empty(&sk->sk_receive_queue)) { 665 rc = llc_wait_data(sk, sock_rcvtimeo(sk, noblock)); 666 if (rc) 667 goto out; 668 } 669 skb = skb_dequeue(&sk->sk_receive_queue); 670 if (!skb) /* shutdown */ 671 goto out; 672 copied = skb->len; 673 if (copied > size) 674 copied = size; 675 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 676 if (rc) 677 goto dgram_free; 678 if (skb->len > copied) { 679 skb_pull(skb, copied); 680 skb_queue_head(&sk->sk_receive_queue, skb); 681 } 682 if (uaddr) 683 memcpy(uaddr, llc_ui_skb_cb(skb), sizeof(*uaddr)); 684 msg->msg_namelen = sizeof(*uaddr); 685 if (!skb->next) { 686 dgram_free: 687 kfree_skb(skb); 688 } 689 out: 690 release_sock(sk); 691 return rc ? : copied; 692 } 693 694 /** 695 * llc_ui_sendmsg - Transmit data provided by the socket user. 696 * @sock: Socket to transmit data from. 697 * @msg: Various user related information. 698 * @len: Length of data to transmit. 699 * 700 * Transmit data provided by the socket user. 701 * Returns non-negative upon success, negative otherwise. 702 */ 703 static int llc_ui_sendmsg(struct kiocb *iocb, struct socket *sock, 704 struct msghdr *msg, size_t len) 705 { 706 struct sock *sk = sock->sk; 707 struct llc_sock *llc = llc_sk(sk); 708 struct sockaddr_llc *addr = (struct sockaddr_llc *)msg->msg_name; 709 int flags = msg->msg_flags; 710 int noblock = flags & MSG_DONTWAIT; 711 struct sk_buff *skb; 712 size_t size = 0; 713 int rc = -EINVAL, copied = 0, hdrlen; 714 715 dprintk("%s: sending from %02X to %02X\n", __FUNCTION__, 716 llc->laddr.lsap, llc->daddr.lsap); 717 lock_sock(sk); 718 if (addr) { 719 if (msg->msg_namelen < sizeof(*addr)) 720 goto release; 721 } else { 722 if (llc_ui_addr_null(&llc->addr)) 723 goto release; 724 addr = &llc->addr; 725 } 726 /* must bind connection to sap if user hasn't done it. */ 727 if (sock_flag(sk, SOCK_ZAPPED)) { 728 /* bind to sap with null dev, exclusive. */ 729 rc = llc_ui_autobind(sock, addr); 730 if (rc) 731 goto release; 732 } 733 hdrlen = llc->dev->hard_header_len + llc_ui_header_len(sk, addr); 734 size = hdrlen + len; 735 if (size > llc->dev->mtu) 736 size = llc->dev->mtu; 737 copied = size - hdrlen; 738 release_sock(sk); 739 skb = sock_alloc_send_skb(sk, size, noblock, &rc); 740 lock_sock(sk); 741 if (!skb) 742 goto release; 743 skb->dev = llc->dev; 744 skb->protocol = llc_proto_type(addr->sllc_arphrd); 745 skb_reserve(skb, hdrlen); 746 rc = memcpy_fromiovec(skb_put(skb, copied), msg->msg_iov, copied); 747 if (rc) 748 goto out; 749 if (sk->sk_type == SOCK_DGRAM || addr->sllc_ua) { 750 llc_build_and_send_ui_pkt(llc->sap, skb, addr->sllc_mac, 751 addr->sllc_sap); 752 goto out; 753 } 754 if (addr->sllc_test) { 755 llc_build_and_send_test_pkt(llc->sap, skb, addr->sllc_mac, 756 addr->sllc_sap); 757 goto out; 758 } 759 if (addr->sllc_xid) { 760 llc_build_and_send_xid_pkt(llc->sap, skb, addr->sllc_mac, 761 addr->sllc_sap); 762 goto out; 763 } 764 rc = -ENOPROTOOPT; 765 if (!(sk->sk_type == SOCK_STREAM && !addr->sllc_ua)) 766 goto out; 767 rc = llc_ui_send_data(sk, skb, noblock); 768 out: 769 if (rc) { 770 kfree_skb(skb); 771 release: 772 dprintk("%s: failed sending from %02X to %02X: %d\n", 773 __FUNCTION__, llc->laddr.lsap, llc->daddr.lsap, rc); 774 } 775 release_sock(sk); 776 return rc ? : copied; 777 } 778 779 /** 780 * llc_ui_getname - return the address info of a socket 781 * @sock: Socket to get address of. 782 * @uaddr: Address structure to return information. 783 * @uaddrlen: Length of address structure. 784 * @peer: Does user want local or remote address information. 785 * 786 * Return the address information of a socket. 787 */ 788 static int llc_ui_getname(struct socket *sock, struct sockaddr *uaddr, 789 int *uaddrlen, int peer) 790 { 791 struct sockaddr_llc sllc; 792 struct sock *sk = sock->sk; 793 struct llc_sock *llc = llc_sk(sk); 794 int rc = 0; 795 796 lock_sock(sk); 797 if (sock_flag(sk, SOCK_ZAPPED)) 798 goto out; 799 *uaddrlen = sizeof(sllc); 800 memset(uaddr, 0, *uaddrlen); 801 if (peer) { 802 rc = -ENOTCONN; 803 if (sk->sk_state != TCP_ESTABLISHED) 804 goto out; 805 if(llc->dev) 806 sllc.sllc_arphrd = llc->dev->type; 807 sllc.sllc_sap = llc->daddr.lsap; 808 memcpy(&sllc.sllc_mac, &llc->daddr.mac, IFHWADDRLEN); 809 } else { 810 rc = -EINVAL; 811 if (!llc->sap) 812 goto out; 813 sllc.sllc_sap = llc->sap->laddr.lsap; 814 815 if (llc->dev) { 816 sllc.sllc_arphrd = llc->dev->type; 817 memcpy(&sllc.sllc_mac, &llc->dev->dev_addr, 818 IFHWADDRLEN); 819 } 820 } 821 rc = 0; 822 sllc.sllc_family = AF_LLC; 823 memcpy(uaddr, &sllc, sizeof(sllc)); 824 out: 825 release_sock(sk); 826 return rc; 827 } 828 829 /** 830 * llc_ui_ioctl - io controls for PF_LLC 831 * @sock: Socket to get/set info 832 * @cmd: command 833 * @arg: optional argument for cmd 834 * 835 * get/set info on llc sockets 836 */ 837 static int llc_ui_ioctl(struct socket *sock, unsigned int cmd, 838 unsigned long arg) 839 { 840 return dev_ioctl(cmd, (void __user *)arg); 841 } 842 843 /** 844 * llc_ui_setsockopt - set various connection specific parameters. 845 * @sock: Socket to set options on. 846 * @level: Socket level user is requesting operations on. 847 * @optname: Operation name. 848 * @optval User provided operation data. 849 * @optlen: Length of optval. 850 * 851 * Set various connection specific parameters. 852 */ 853 static int llc_ui_setsockopt(struct socket *sock, int level, int optname, 854 char __user *optval, int optlen) 855 { 856 struct sock *sk = sock->sk; 857 struct llc_sock *llc = llc_sk(sk); 858 int rc = -EINVAL, opt; 859 860 lock_sock(sk); 861 if (unlikely(level != SOL_LLC || optlen != sizeof(int))) 862 goto out; 863 rc = get_user(opt, (int __user *)optval); 864 if (rc) 865 goto out; 866 rc = -EINVAL; 867 switch (optname) { 868 case LLC_OPT_RETRY: 869 if (opt > LLC_OPT_MAX_RETRY) 870 goto out; 871 llc->n2 = opt; 872 break; 873 case LLC_OPT_SIZE: 874 if (opt > LLC_OPT_MAX_SIZE) 875 goto out; 876 llc->n1 = opt; 877 break; 878 case LLC_OPT_ACK_TMR_EXP: 879 if (opt > LLC_OPT_MAX_ACK_TMR_EXP) 880 goto out; 881 llc->ack_timer.expire = opt * HZ; 882 break; 883 case LLC_OPT_P_TMR_EXP: 884 if (opt > LLC_OPT_MAX_P_TMR_EXP) 885 goto out; 886 llc->pf_cycle_timer.expire = opt * HZ; 887 break; 888 case LLC_OPT_REJ_TMR_EXP: 889 if (opt > LLC_OPT_MAX_REJ_TMR_EXP) 890 goto out; 891 llc->rej_sent_timer.expire = opt * HZ; 892 break; 893 case LLC_OPT_BUSY_TMR_EXP: 894 if (opt > LLC_OPT_MAX_BUSY_TMR_EXP) 895 goto out; 896 llc->busy_state_timer.expire = opt * HZ; 897 break; 898 case LLC_OPT_TX_WIN: 899 if (opt > LLC_OPT_MAX_WIN) 900 goto out; 901 llc->k = opt; 902 break; 903 case LLC_OPT_RX_WIN: 904 if (opt > LLC_OPT_MAX_WIN) 905 goto out; 906 llc->rw = opt; 907 break; 908 default: 909 rc = -ENOPROTOOPT; 910 goto out; 911 } 912 rc = 0; 913 out: 914 release_sock(sk); 915 return rc; 916 } 917 918 /** 919 * llc_ui_getsockopt - get connection specific socket info 920 * @sock: Socket to get information from. 921 * @level: Socket level user is requesting operations on. 922 * @optname: Operation name. 923 * @optval: Variable to return operation data in. 924 * @optlen: Length of optval. 925 * 926 * Get connection specific socket information. 927 */ 928 static int llc_ui_getsockopt(struct socket *sock, int level, int optname, 929 char __user *optval, int __user *optlen) 930 { 931 struct sock *sk = sock->sk; 932 struct llc_sock *llc = llc_sk(sk); 933 int val = 0, len = 0, rc = -EINVAL; 934 935 lock_sock(sk); 936 if (unlikely(level != SOL_LLC)) 937 goto out; 938 rc = get_user(len, optlen); 939 if (rc) 940 goto out; 941 rc = -EINVAL; 942 if (len != sizeof(int)) 943 goto out; 944 switch (optname) { 945 case LLC_OPT_RETRY: 946 val = llc->n2; break; 947 case LLC_OPT_SIZE: 948 val = llc->n1; break; 949 case LLC_OPT_ACK_TMR_EXP: 950 val = llc->ack_timer.expire / HZ; break; 951 case LLC_OPT_P_TMR_EXP: 952 val = llc->pf_cycle_timer.expire / HZ; break; 953 case LLC_OPT_REJ_TMR_EXP: 954 val = llc->rej_sent_timer.expire / HZ; break; 955 case LLC_OPT_BUSY_TMR_EXP: 956 val = llc->busy_state_timer.expire / HZ; break; 957 case LLC_OPT_TX_WIN: 958 val = llc->k; break; 959 case LLC_OPT_RX_WIN: 960 val = llc->rw; break; 961 default: 962 rc = -ENOPROTOOPT; 963 goto out; 964 } 965 rc = 0; 966 if (put_user(len, optlen) || copy_to_user(optval, &val, len)) 967 rc = -EFAULT; 968 out: 969 release_sock(sk); 970 return rc; 971 } 972 973 static struct net_proto_family llc_ui_family_ops = { 974 .family = PF_LLC, 975 .create = llc_ui_create, 976 .owner = THIS_MODULE, 977 }; 978 979 static struct proto_ops llc_ui_ops = { 980 .family = PF_LLC, 981 .owner = THIS_MODULE, 982 .release = llc_ui_release, 983 .bind = llc_ui_bind, 984 .connect = llc_ui_connect, 985 .socketpair = sock_no_socketpair, 986 .accept = llc_ui_accept, 987 .getname = llc_ui_getname, 988 .poll = datagram_poll, 989 .ioctl = llc_ui_ioctl, 990 .listen = llc_ui_listen, 991 .shutdown = llc_ui_shutdown, 992 .setsockopt = llc_ui_setsockopt, 993 .getsockopt = llc_ui_getsockopt, 994 .sendmsg = llc_ui_sendmsg, 995 .recvmsg = llc_ui_recvmsg, 996 .mmap = sock_no_mmap, 997 .sendpage = sock_no_sendpage, 998 }; 999 1000 extern void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb); 1001 extern void llc_conn_handler(struct llc_sap *sap, struct sk_buff *skb); 1002 1003 static char llc_proc_err_msg[] __initdata = 1004 KERN_CRIT "LLC: Unable to register the proc_fs entries\n"; 1005 static char llc_sysctl_err_msg[] __initdata = 1006 KERN_CRIT "LLC: Unable to register the sysctl entries\n"; 1007 static char llc_sock_err_msg[] __initdata = 1008 KERN_CRIT "LLC: Unable to register the network family\n"; 1009 1010 static int __init llc2_init(void) 1011 { 1012 int rc = proto_register(&llc_proto, 0); 1013 1014 if (rc != 0) 1015 goto out; 1016 1017 llc_build_offset_table(); 1018 llc_station_init(); 1019 llc_ui_sap_last_autoport = LLC_SAP_DYN_START; 1020 rc = llc_proc_init(); 1021 if (rc != 0) { 1022 printk(llc_proc_err_msg); 1023 goto out_unregister_llc_proto; 1024 } 1025 rc = llc_sysctl_init(); 1026 if (rc) { 1027 printk(llc_sysctl_err_msg); 1028 goto out_proc; 1029 } 1030 rc = sock_register(&llc_ui_family_ops); 1031 if (rc) { 1032 printk(llc_sock_err_msg); 1033 goto out_sysctl; 1034 } 1035 llc_add_pack(LLC_DEST_SAP, llc_sap_handler); 1036 llc_add_pack(LLC_DEST_CONN, llc_conn_handler); 1037 out: 1038 return rc; 1039 out_sysctl: 1040 llc_sysctl_exit(); 1041 out_proc: 1042 llc_proc_exit(); 1043 out_unregister_llc_proto: 1044 proto_unregister(&llc_proto); 1045 goto out; 1046 } 1047 1048 static void __exit llc2_exit(void) 1049 { 1050 llc_station_exit(); 1051 llc_remove_pack(LLC_DEST_SAP); 1052 llc_remove_pack(LLC_DEST_CONN); 1053 sock_unregister(PF_LLC); 1054 llc_proc_exit(); 1055 llc_sysctl_exit(); 1056 proto_unregister(&llc_proto); 1057 } 1058 1059 module_init(llc2_init); 1060 module_exit(llc2_exit); 1061 1062 MODULE_LICENSE("GPL"); 1063 MODULE_AUTHOR("Procom 1997, Jay Schullist 2001, Arnaldo C. Melo 2001-2003"); 1064 MODULE_DESCRIPTION("IEEE 802.2 PF_LLC support"); 1065 MODULE_ALIAS_NETPROTO(PF_LLC); 1066