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