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