1 /* 2 * net/tipc/socket.c: TIPC socket API 3 * 4 * Copyright (c) 2001-2007, Ericsson AB 5 * Copyright (c) 2004-2008, Wind River Systems 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the names of the copyright holders nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * Alternatively, this software may be distributed under the terms of the 21 * GNU General Public License ("GPL") version 2 as published by the Free 22 * Software Foundation. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 #include <linux/module.h> 38 #include <linux/types.h> 39 #include <linux/net.h> 40 #include <linux/socket.h> 41 #include <linux/errno.h> 42 #include <linux/mm.h> 43 #include <linux/poll.h> 44 #include <linux/fcntl.h> 45 #include <linux/gfp.h> 46 #include <asm/string.h> 47 #include <asm/atomic.h> 48 #include <net/sock.h> 49 50 #include <linux/tipc.h> 51 #include <linux/tipc_config.h> 52 #include <net/tipc/tipc_msg.h> 53 #include <net/tipc/tipc_port.h> 54 55 #include "core.h" 56 57 #define SS_LISTENING -1 /* socket is listening */ 58 #define SS_READY -2 /* socket is connectionless */ 59 60 #define OVERLOAD_LIMIT_BASE 5000 61 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */ 62 63 struct tipc_sock { 64 struct sock sk; 65 struct tipc_port *p; 66 struct tipc_portid peer_name; 67 long conn_timeout; 68 }; 69 70 #define tipc_sk(sk) ((struct tipc_sock *)(sk)) 71 #define tipc_sk_port(sk) ((struct tipc_port *)(tipc_sk(sk)->p)) 72 73 static int backlog_rcv(struct sock *sk, struct sk_buff *skb); 74 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf); 75 static void wakeupdispatch(struct tipc_port *tport); 76 77 static const struct proto_ops packet_ops; 78 static const struct proto_ops stream_ops; 79 static const struct proto_ops msg_ops; 80 81 static struct proto tipc_proto; 82 83 static int sockets_enabled = 0; 84 85 static atomic_t tipc_queue_size = ATOMIC_INIT(0); 86 87 /* 88 * Revised TIPC socket locking policy: 89 * 90 * Most socket operations take the standard socket lock when they start 91 * and hold it until they finish (or until they need to sleep). Acquiring 92 * this lock grants the owner exclusive access to the fields of the socket 93 * data structures, with the exception of the backlog queue. A few socket 94 * operations can be done without taking the socket lock because they only 95 * read socket information that never changes during the life of the socket. 96 * 97 * Socket operations may acquire the lock for the associated TIPC port if they 98 * need to perform an operation on the port. If any routine needs to acquire 99 * both the socket lock and the port lock it must take the socket lock first 100 * to avoid the risk of deadlock. 101 * 102 * The dispatcher handling incoming messages cannot grab the socket lock in 103 * the standard fashion, since invoked it runs at the BH level and cannot block. 104 * Instead, it checks to see if the socket lock is currently owned by someone, 105 * and either handles the message itself or adds it to the socket's backlog 106 * queue; in the latter case the queued message is processed once the process 107 * owning the socket lock releases it. 108 * 109 * NOTE: Releasing the socket lock while an operation is sleeping overcomes 110 * the problem of a blocked socket operation preventing any other operations 111 * from occurring. However, applications must be careful if they have 112 * multiple threads trying to send (or receive) on the same socket, as these 113 * operations might interfere with each other. For example, doing a connect 114 * and a receive at the same time might allow the receive to consume the 115 * ACK message meant for the connect. While additional work could be done 116 * to try and overcome this, it doesn't seem to be worthwhile at the present. 117 * 118 * NOTE: Releasing the socket lock while an operation is sleeping also ensures 119 * that another operation that must be performed in a non-blocking manner is 120 * not delayed for very long because the lock has already been taken. 121 * 122 * NOTE: This code assumes that certain fields of a port/socket pair are 123 * constant over its lifetime; such fields can be examined without taking 124 * the socket lock and/or port lock, and do not need to be re-read even 125 * after resuming processing after waiting. These fields include: 126 * - socket type 127 * - pointer to socket sk structure (aka tipc_sock structure) 128 * - pointer to port structure 129 * - port reference 130 */ 131 132 /** 133 * advance_rx_queue - discard first buffer in socket receive queue 134 * 135 * Caller must hold socket lock 136 */ 137 138 static void advance_rx_queue(struct sock *sk) 139 { 140 buf_discard(__skb_dequeue(&sk->sk_receive_queue)); 141 atomic_dec(&tipc_queue_size); 142 } 143 144 /** 145 * discard_rx_queue - discard all buffers in socket receive queue 146 * 147 * Caller must hold socket lock 148 */ 149 150 static void discard_rx_queue(struct sock *sk) 151 { 152 struct sk_buff *buf; 153 154 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) { 155 atomic_dec(&tipc_queue_size); 156 buf_discard(buf); 157 } 158 } 159 160 /** 161 * reject_rx_queue - reject all buffers in socket receive queue 162 * 163 * Caller must hold socket lock 164 */ 165 166 static void reject_rx_queue(struct sock *sk) 167 { 168 struct sk_buff *buf; 169 170 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) { 171 tipc_reject_msg(buf, TIPC_ERR_NO_PORT); 172 atomic_dec(&tipc_queue_size); 173 } 174 } 175 176 /** 177 * tipc_create - create a TIPC socket 178 * @net: network namespace (must be default network) 179 * @sock: pre-allocated socket structure 180 * @protocol: protocol indicator (must be 0) 181 * @kern: caused by kernel or by userspace? 182 * 183 * This routine creates additional data structures used by the TIPC socket, 184 * initializes them, and links them together. 185 * 186 * Returns 0 on success, errno otherwise 187 */ 188 189 static int tipc_create(struct net *net, struct socket *sock, int protocol, 190 int kern) 191 { 192 const struct proto_ops *ops; 193 socket_state state; 194 struct sock *sk; 195 struct tipc_port *tp_ptr; 196 197 /* Validate arguments */ 198 199 if (!net_eq(net, &init_net)) 200 return -EAFNOSUPPORT; 201 202 if (unlikely(protocol != 0)) 203 return -EPROTONOSUPPORT; 204 205 switch (sock->type) { 206 case SOCK_STREAM: 207 ops = &stream_ops; 208 state = SS_UNCONNECTED; 209 break; 210 case SOCK_SEQPACKET: 211 ops = &packet_ops; 212 state = SS_UNCONNECTED; 213 break; 214 case SOCK_DGRAM: 215 case SOCK_RDM: 216 ops = &msg_ops; 217 state = SS_READY; 218 break; 219 default: 220 return -EPROTOTYPE; 221 } 222 223 /* Allocate socket's protocol area */ 224 225 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto); 226 if (sk == NULL) 227 return -ENOMEM; 228 229 /* Allocate TIPC port for socket to use */ 230 231 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch, 232 TIPC_LOW_IMPORTANCE); 233 if (unlikely(!tp_ptr)) { 234 sk_free(sk); 235 return -ENOMEM; 236 } 237 238 /* Finish initializing socket data structures */ 239 240 sock->ops = ops; 241 sock->state = state; 242 243 sock_init_data(sock, sk); 244 sk->sk_backlog_rcv = backlog_rcv; 245 tipc_sk(sk)->p = tp_ptr; 246 tipc_sk(sk)->conn_timeout = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT); 247 248 spin_unlock_bh(tp_ptr->lock); 249 250 if (sock->state == SS_READY) { 251 tipc_set_portunreturnable(tp_ptr->ref, 1); 252 if (sock->type == SOCK_DGRAM) 253 tipc_set_portunreliable(tp_ptr->ref, 1); 254 } 255 256 atomic_inc(&tipc_user_count); 257 return 0; 258 } 259 260 /** 261 * release - destroy a TIPC socket 262 * @sock: socket to destroy 263 * 264 * This routine cleans up any messages that are still queued on the socket. 265 * For DGRAM and RDM socket types, all queued messages are rejected. 266 * For SEQPACKET and STREAM socket types, the first message is rejected 267 * and any others are discarded. (If the first message on a STREAM socket 268 * is partially-read, it is discarded and the next one is rejected instead.) 269 * 270 * NOTE: Rejected messages are not necessarily returned to the sender! They 271 * are returned or discarded according to the "destination droppable" setting 272 * specified for the message by the sender. 273 * 274 * Returns 0 on success, errno otherwise 275 */ 276 277 static int release(struct socket *sock) 278 { 279 struct sock *sk = sock->sk; 280 struct tipc_port *tport; 281 struct sk_buff *buf; 282 int res; 283 284 /* 285 * Exit if socket isn't fully initialized (occurs when a failed accept() 286 * releases a pre-allocated child socket that was never used) 287 */ 288 289 if (sk == NULL) 290 return 0; 291 292 tport = tipc_sk_port(sk); 293 lock_sock(sk); 294 295 /* 296 * Reject all unreceived messages, except on an active connection 297 * (which disconnects locally & sends a 'FIN+' to peer) 298 */ 299 300 while (sock->state != SS_DISCONNECTING) { 301 buf = __skb_dequeue(&sk->sk_receive_queue); 302 if (buf == NULL) 303 break; 304 atomic_dec(&tipc_queue_size); 305 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) 306 buf_discard(buf); 307 else { 308 if ((sock->state == SS_CONNECTING) || 309 (sock->state == SS_CONNECTED)) { 310 sock->state = SS_DISCONNECTING; 311 tipc_disconnect(tport->ref); 312 } 313 tipc_reject_msg(buf, TIPC_ERR_NO_PORT); 314 } 315 } 316 317 /* 318 * Delete TIPC port; this ensures no more messages are queued 319 * (also disconnects an active connection & sends a 'FIN-' to peer) 320 */ 321 322 res = tipc_deleteport(tport->ref); 323 324 /* Discard any remaining (connection-based) messages in receive queue */ 325 326 discard_rx_queue(sk); 327 328 /* Reject any messages that accumulated in backlog queue */ 329 330 sock->state = SS_DISCONNECTING; 331 release_sock(sk); 332 333 sock_put(sk); 334 sock->sk = NULL; 335 336 atomic_dec(&tipc_user_count); 337 return res; 338 } 339 340 /** 341 * bind - associate or disassocate TIPC name(s) with a socket 342 * @sock: socket structure 343 * @uaddr: socket address describing name(s) and desired operation 344 * @uaddr_len: size of socket address data structure 345 * 346 * Name and name sequence binding is indicated using a positive scope value; 347 * a negative scope value unbinds the specified name. Specifying no name 348 * (i.e. a socket address length of 0) unbinds all names from the socket. 349 * 350 * Returns 0 on success, errno otherwise 351 * 352 * NOTE: This routine doesn't need to take the socket lock since it doesn't 353 * access any non-constant socket information. 354 */ 355 356 static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len) 357 { 358 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; 359 u32 portref = tipc_sk_port(sock->sk)->ref; 360 361 if (unlikely(!uaddr_len)) 362 return tipc_withdraw(portref, 0, NULL); 363 364 if (uaddr_len < sizeof(struct sockaddr_tipc)) 365 return -EINVAL; 366 if (addr->family != AF_TIPC) 367 return -EAFNOSUPPORT; 368 369 if (addr->addrtype == TIPC_ADDR_NAME) 370 addr->addr.nameseq.upper = addr->addr.nameseq.lower; 371 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) 372 return -EAFNOSUPPORT; 373 374 return (addr->scope > 0) ? 375 tipc_publish(portref, addr->scope, &addr->addr.nameseq) : 376 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq); 377 } 378 379 /** 380 * get_name - get port ID of socket or peer socket 381 * @sock: socket structure 382 * @uaddr: area for returned socket address 383 * @uaddr_len: area for returned length of socket address 384 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID 385 * 386 * Returns 0 on success, errno otherwise 387 * 388 * NOTE: This routine doesn't need to take the socket lock since it only 389 * accesses socket information that is unchanging (or which changes in 390 * a completely predictable manner). 391 */ 392 393 static int get_name(struct socket *sock, struct sockaddr *uaddr, 394 int *uaddr_len, int peer) 395 { 396 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; 397 struct tipc_sock *tsock = tipc_sk(sock->sk); 398 399 memset(addr, 0, sizeof(*addr)); 400 if (peer) { 401 if ((sock->state != SS_CONNECTED) && 402 ((peer != 2) || (sock->state != SS_DISCONNECTING))) 403 return -ENOTCONN; 404 addr->addr.id.ref = tsock->peer_name.ref; 405 addr->addr.id.node = tsock->peer_name.node; 406 } else { 407 tipc_ownidentity(tsock->p->ref, &addr->addr.id); 408 } 409 410 *uaddr_len = sizeof(*addr); 411 addr->addrtype = TIPC_ADDR_ID; 412 addr->family = AF_TIPC; 413 addr->scope = 0; 414 addr->addr.name.domain = 0; 415 416 return 0; 417 } 418 419 /** 420 * poll - read and possibly block on pollmask 421 * @file: file structure associated with the socket 422 * @sock: socket for which to calculate the poll bits 423 * @wait: ??? 424 * 425 * Returns pollmask value 426 * 427 * COMMENTARY: 428 * It appears that the usual socket locking mechanisms are not useful here 429 * since the pollmask info is potentially out-of-date the moment this routine 430 * exits. TCP and other protocols seem to rely on higher level poll routines 431 * to handle any preventable race conditions, so TIPC will do the same ... 432 * 433 * TIPC sets the returned events as follows: 434 * 435 * socket state flags set 436 * ------------ --------- 437 * unconnected no read flags 438 * no write flags 439 * 440 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue 441 * no write flags 442 * 443 * connected POLLIN/POLLRDNORM if data in rx queue 444 * POLLOUT if port is not congested 445 * 446 * disconnecting POLLIN/POLLRDNORM/POLLHUP 447 * no write flags 448 * 449 * listening POLLIN if SYN in rx queue 450 * no write flags 451 * 452 * ready POLLIN/POLLRDNORM if data in rx queue 453 * [connectionless] POLLOUT (since port cannot be congested) 454 * 455 * IMPORTANT: The fact that a read or write operation is indicated does NOT 456 * imply that the operation will succeed, merely that it should be performed 457 * and will not block. 458 */ 459 460 static unsigned int poll(struct file *file, struct socket *sock, 461 poll_table *wait) 462 { 463 struct sock *sk = sock->sk; 464 u32 mask = 0; 465 466 poll_wait(file, sk_sleep(sk), wait); 467 468 switch ((int)sock->state) { 469 case SS_READY: 470 case SS_CONNECTED: 471 if (!tipc_sk_port(sk)->congested) 472 mask |= POLLOUT; 473 /* fall thru' */ 474 case SS_CONNECTING: 475 case SS_LISTENING: 476 if (!skb_queue_empty(&sk->sk_receive_queue)) 477 mask |= (POLLIN | POLLRDNORM); 478 break; 479 case SS_DISCONNECTING: 480 mask = (POLLIN | POLLRDNORM | POLLHUP); 481 break; 482 } 483 484 return mask; 485 } 486 487 /** 488 * dest_name_check - verify user is permitted to send to specified port name 489 * @dest: destination address 490 * @m: descriptor for message to be sent 491 * 492 * Prevents restricted configuration commands from being issued by 493 * unauthorized users. 494 * 495 * Returns 0 if permission is granted, otherwise errno 496 */ 497 498 static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m) 499 { 500 struct tipc_cfg_msg_hdr hdr; 501 502 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES)) 503 return 0; 504 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV)) 505 return 0; 506 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV)) 507 return -EACCES; 508 509 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr))) 510 return -EFAULT; 511 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN))) 512 return -EACCES; 513 514 return 0; 515 } 516 517 /** 518 * send_msg - send message in connectionless manner 519 * @iocb: if NULL, indicates that socket lock is already held 520 * @sock: socket structure 521 * @m: message to send 522 * @total_len: length of message 523 * 524 * Message must have an destination specified explicitly. 525 * Used for SOCK_RDM and SOCK_DGRAM messages, 526 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections. 527 * (Note: 'SYN+' is prohibited on SOCK_STREAM.) 528 * 529 * Returns the number of bytes sent on success, or errno otherwise 530 */ 531 532 static int send_msg(struct kiocb *iocb, struct socket *sock, 533 struct msghdr *m, size_t total_len) 534 { 535 struct sock *sk = sock->sk; 536 struct tipc_port *tport = tipc_sk_port(sk); 537 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name; 538 int needs_conn; 539 int res = -EINVAL; 540 541 if (unlikely(!dest)) 542 return -EDESTADDRREQ; 543 if (unlikely((m->msg_namelen < sizeof(*dest)) || 544 (dest->family != AF_TIPC))) 545 return -EINVAL; 546 547 if (iocb) 548 lock_sock(sk); 549 550 needs_conn = (sock->state != SS_READY); 551 if (unlikely(needs_conn)) { 552 if (sock->state == SS_LISTENING) { 553 res = -EPIPE; 554 goto exit; 555 } 556 if (sock->state != SS_UNCONNECTED) { 557 res = -EISCONN; 558 goto exit; 559 } 560 if ((tport->published) || 561 ((sock->type == SOCK_STREAM) && (total_len != 0))) { 562 res = -EOPNOTSUPP; 563 goto exit; 564 } 565 if (dest->addrtype == TIPC_ADDR_NAME) { 566 tport->conn_type = dest->addr.name.name.type; 567 tport->conn_instance = dest->addr.name.name.instance; 568 } 569 570 /* Abort any pending connection attempts (very unlikely) */ 571 572 reject_rx_queue(sk); 573 } 574 575 do { 576 if (dest->addrtype == TIPC_ADDR_NAME) { 577 if ((res = dest_name_check(dest, m))) 578 break; 579 res = tipc_send2name(tport->ref, 580 &dest->addr.name.name, 581 dest->addr.name.domain, 582 m->msg_iovlen, 583 m->msg_iov); 584 } 585 else if (dest->addrtype == TIPC_ADDR_ID) { 586 res = tipc_send2port(tport->ref, 587 &dest->addr.id, 588 m->msg_iovlen, 589 m->msg_iov); 590 } 591 else if (dest->addrtype == TIPC_ADDR_MCAST) { 592 if (needs_conn) { 593 res = -EOPNOTSUPP; 594 break; 595 } 596 if ((res = dest_name_check(dest, m))) 597 break; 598 res = tipc_multicast(tport->ref, 599 &dest->addr.nameseq, 600 0, 601 m->msg_iovlen, 602 m->msg_iov); 603 } 604 if (likely(res != -ELINKCONG)) { 605 if (needs_conn && (res >= 0)) { 606 sock->state = SS_CONNECTING; 607 } 608 break; 609 } 610 if (m->msg_flags & MSG_DONTWAIT) { 611 res = -EWOULDBLOCK; 612 break; 613 } 614 release_sock(sk); 615 res = wait_event_interruptible(*sk_sleep(sk), 616 !tport->congested); 617 lock_sock(sk); 618 if (res) 619 break; 620 } while (1); 621 622 exit: 623 if (iocb) 624 release_sock(sk); 625 return res; 626 } 627 628 /** 629 * send_packet - send a connection-oriented message 630 * @iocb: if NULL, indicates that socket lock is already held 631 * @sock: socket structure 632 * @m: message to send 633 * @total_len: length of message 634 * 635 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data. 636 * 637 * Returns the number of bytes sent on success, or errno otherwise 638 */ 639 640 static int send_packet(struct kiocb *iocb, struct socket *sock, 641 struct msghdr *m, size_t total_len) 642 { 643 struct sock *sk = sock->sk; 644 struct tipc_port *tport = tipc_sk_port(sk); 645 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name; 646 int res; 647 648 /* Handle implied connection establishment */ 649 650 if (unlikely(dest)) 651 return send_msg(iocb, sock, m, total_len); 652 653 if (iocb) 654 lock_sock(sk); 655 656 do { 657 if (unlikely(sock->state != SS_CONNECTED)) { 658 if (sock->state == SS_DISCONNECTING) 659 res = -EPIPE; 660 else 661 res = -ENOTCONN; 662 break; 663 } 664 665 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov); 666 if (likely(res != -ELINKCONG)) { 667 break; 668 } 669 if (m->msg_flags & MSG_DONTWAIT) { 670 res = -EWOULDBLOCK; 671 break; 672 } 673 release_sock(sk); 674 res = wait_event_interruptible(*sk_sleep(sk), 675 (!tport->congested || !tport->connected)); 676 lock_sock(sk); 677 if (res) 678 break; 679 } while (1); 680 681 if (iocb) 682 release_sock(sk); 683 return res; 684 } 685 686 /** 687 * send_stream - send stream-oriented data 688 * @iocb: (unused) 689 * @sock: socket structure 690 * @m: data to send 691 * @total_len: total length of data to be sent 692 * 693 * Used for SOCK_STREAM data. 694 * 695 * Returns the number of bytes sent on success (or partial success), 696 * or errno if no data sent 697 */ 698 699 static int send_stream(struct kiocb *iocb, struct socket *sock, 700 struct msghdr *m, size_t total_len) 701 { 702 struct sock *sk = sock->sk; 703 struct tipc_port *tport = tipc_sk_port(sk); 704 struct msghdr my_msg; 705 struct iovec my_iov; 706 struct iovec *curr_iov; 707 int curr_iovlen; 708 char __user *curr_start; 709 u32 hdr_size; 710 int curr_left; 711 int bytes_to_send; 712 int bytes_sent; 713 int res; 714 715 lock_sock(sk); 716 717 /* Handle special cases where there is no connection */ 718 719 if (unlikely(sock->state != SS_CONNECTED)) { 720 if (sock->state == SS_UNCONNECTED) { 721 res = send_packet(NULL, sock, m, total_len); 722 goto exit; 723 } else if (sock->state == SS_DISCONNECTING) { 724 res = -EPIPE; 725 goto exit; 726 } else { 727 res = -ENOTCONN; 728 goto exit; 729 } 730 } 731 732 if (unlikely(m->msg_name)) { 733 res = -EISCONN; 734 goto exit; 735 } 736 737 /* 738 * Send each iovec entry using one or more messages 739 * 740 * Note: This algorithm is good for the most likely case 741 * (i.e. one large iovec entry), but could be improved to pass sets 742 * of small iovec entries into send_packet(). 743 */ 744 745 curr_iov = m->msg_iov; 746 curr_iovlen = m->msg_iovlen; 747 my_msg.msg_iov = &my_iov; 748 my_msg.msg_iovlen = 1; 749 my_msg.msg_flags = m->msg_flags; 750 my_msg.msg_name = NULL; 751 bytes_sent = 0; 752 753 hdr_size = msg_hdr_sz(&tport->phdr); 754 755 while (curr_iovlen--) { 756 curr_start = curr_iov->iov_base; 757 curr_left = curr_iov->iov_len; 758 759 while (curr_left) { 760 bytes_to_send = tport->max_pkt - hdr_size; 761 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE) 762 bytes_to_send = TIPC_MAX_USER_MSG_SIZE; 763 if (curr_left < bytes_to_send) 764 bytes_to_send = curr_left; 765 my_iov.iov_base = curr_start; 766 my_iov.iov_len = bytes_to_send; 767 if ((res = send_packet(NULL, sock, &my_msg, 0)) < 0) { 768 if (bytes_sent) 769 res = bytes_sent; 770 goto exit; 771 } 772 curr_left -= bytes_to_send; 773 curr_start += bytes_to_send; 774 bytes_sent += bytes_to_send; 775 } 776 777 curr_iov++; 778 } 779 res = bytes_sent; 780 exit: 781 release_sock(sk); 782 return res; 783 } 784 785 /** 786 * auto_connect - complete connection setup to a remote port 787 * @sock: socket structure 788 * @msg: peer's response message 789 * 790 * Returns 0 on success, errno otherwise 791 */ 792 793 static int auto_connect(struct socket *sock, struct tipc_msg *msg) 794 { 795 struct tipc_sock *tsock = tipc_sk(sock->sk); 796 797 if (msg_errcode(msg)) { 798 sock->state = SS_DISCONNECTING; 799 return -ECONNREFUSED; 800 } 801 802 tsock->peer_name.ref = msg_origport(msg); 803 tsock->peer_name.node = msg_orignode(msg); 804 tipc_connect2port(tsock->p->ref, &tsock->peer_name); 805 tipc_set_portimportance(tsock->p->ref, msg_importance(msg)); 806 sock->state = SS_CONNECTED; 807 return 0; 808 } 809 810 /** 811 * set_orig_addr - capture sender's address for received message 812 * @m: descriptor for message info 813 * @msg: received message header 814 * 815 * Note: Address is not captured if not requested by receiver. 816 */ 817 818 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg) 819 { 820 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name; 821 822 if (addr) { 823 addr->family = AF_TIPC; 824 addr->addrtype = TIPC_ADDR_ID; 825 addr->addr.id.ref = msg_origport(msg); 826 addr->addr.id.node = msg_orignode(msg); 827 addr->addr.name.domain = 0; /* could leave uninitialized */ 828 addr->scope = 0; /* could leave uninitialized */ 829 m->msg_namelen = sizeof(struct sockaddr_tipc); 830 } 831 } 832 833 /** 834 * anc_data_recv - optionally capture ancillary data for received message 835 * @m: descriptor for message info 836 * @msg: received message header 837 * @tport: TIPC port associated with message 838 * 839 * Note: Ancillary data is not captured if not requested by receiver. 840 * 841 * Returns 0 if successful, otherwise errno 842 */ 843 844 static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg, 845 struct tipc_port *tport) 846 { 847 u32 anc_data[3]; 848 u32 err; 849 u32 dest_type; 850 int has_name; 851 int res; 852 853 if (likely(m->msg_controllen == 0)) 854 return 0; 855 856 /* Optionally capture errored message object(s) */ 857 858 err = msg ? msg_errcode(msg) : 0; 859 if (unlikely(err)) { 860 anc_data[0] = err; 861 anc_data[1] = msg_data_sz(msg); 862 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data))) 863 return res; 864 if (anc_data[1] && 865 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1], 866 msg_data(msg)))) 867 return res; 868 } 869 870 /* Optionally capture message destination object */ 871 872 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG; 873 switch (dest_type) { 874 case TIPC_NAMED_MSG: 875 has_name = 1; 876 anc_data[0] = msg_nametype(msg); 877 anc_data[1] = msg_namelower(msg); 878 anc_data[2] = msg_namelower(msg); 879 break; 880 case TIPC_MCAST_MSG: 881 has_name = 1; 882 anc_data[0] = msg_nametype(msg); 883 anc_data[1] = msg_namelower(msg); 884 anc_data[2] = msg_nameupper(msg); 885 break; 886 case TIPC_CONN_MSG: 887 has_name = (tport->conn_type != 0); 888 anc_data[0] = tport->conn_type; 889 anc_data[1] = tport->conn_instance; 890 anc_data[2] = tport->conn_instance; 891 break; 892 default: 893 has_name = 0; 894 } 895 if (has_name && 896 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data))) 897 return res; 898 899 return 0; 900 } 901 902 /** 903 * recv_msg - receive packet-oriented message 904 * @iocb: (unused) 905 * @m: descriptor for message info 906 * @buf_len: total size of user buffer area 907 * @flags: receive flags 908 * 909 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages. 910 * If the complete message doesn't fit in user area, truncate it. 911 * 912 * Returns size of returned message data, errno otherwise 913 */ 914 915 static int recv_msg(struct kiocb *iocb, struct socket *sock, 916 struct msghdr *m, size_t buf_len, int flags) 917 { 918 struct sock *sk = sock->sk; 919 struct tipc_port *tport = tipc_sk_port(sk); 920 struct sk_buff *buf; 921 struct tipc_msg *msg; 922 unsigned int sz; 923 u32 err; 924 int res; 925 926 /* Catch invalid receive requests */ 927 928 if (m->msg_iovlen != 1) 929 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */ 930 931 if (unlikely(!buf_len)) 932 return -EINVAL; 933 934 lock_sock(sk); 935 936 if (unlikely(sock->state == SS_UNCONNECTED)) { 937 res = -ENOTCONN; 938 goto exit; 939 } 940 941 restart: 942 943 /* Look for a message in receive queue; wait if necessary */ 944 945 while (skb_queue_empty(&sk->sk_receive_queue)) { 946 if (sock->state == SS_DISCONNECTING) { 947 res = -ENOTCONN; 948 goto exit; 949 } 950 if (flags & MSG_DONTWAIT) { 951 res = -EWOULDBLOCK; 952 goto exit; 953 } 954 release_sock(sk); 955 res = wait_event_interruptible(*sk_sleep(sk), 956 (!skb_queue_empty(&sk->sk_receive_queue) || 957 (sock->state == SS_DISCONNECTING))); 958 lock_sock(sk); 959 if (res) 960 goto exit; 961 } 962 963 /* Look at first message in receive queue */ 964 965 buf = skb_peek(&sk->sk_receive_queue); 966 msg = buf_msg(buf); 967 sz = msg_data_sz(msg); 968 err = msg_errcode(msg); 969 970 /* Complete connection setup for an implied connect */ 971 972 if (unlikely(sock->state == SS_CONNECTING)) { 973 res = auto_connect(sock, msg); 974 if (res) 975 goto exit; 976 } 977 978 /* Discard an empty non-errored message & try again */ 979 980 if ((!sz) && (!err)) { 981 advance_rx_queue(sk); 982 goto restart; 983 } 984 985 /* Capture sender's address (optional) */ 986 987 set_orig_addr(m, msg); 988 989 /* Capture ancillary data (optional) */ 990 991 res = anc_data_recv(m, msg, tport); 992 if (res) 993 goto exit; 994 995 /* Capture message data (if valid) & compute return value (always) */ 996 997 if (!err) { 998 if (unlikely(buf_len < sz)) { 999 sz = buf_len; 1000 m->msg_flags |= MSG_TRUNC; 1001 } 1002 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg), 1003 sz))) { 1004 res = -EFAULT; 1005 goto exit; 1006 } 1007 res = sz; 1008 } else { 1009 if ((sock->state == SS_READY) || 1010 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)) 1011 res = 0; 1012 else 1013 res = -ECONNRESET; 1014 } 1015 1016 /* Consume received message (optional) */ 1017 1018 if (likely(!(flags & MSG_PEEK))) { 1019 if ((sock->state != SS_READY) && 1020 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN)) 1021 tipc_acknowledge(tport->ref, tport->conn_unacked); 1022 advance_rx_queue(sk); 1023 } 1024 exit: 1025 release_sock(sk); 1026 return res; 1027 } 1028 1029 /** 1030 * recv_stream - receive stream-oriented data 1031 * @iocb: (unused) 1032 * @m: descriptor for message info 1033 * @buf_len: total size of user buffer area 1034 * @flags: receive flags 1035 * 1036 * Used for SOCK_STREAM messages only. If not enough data is available 1037 * will optionally wait for more; never truncates data. 1038 * 1039 * Returns size of returned message data, errno otherwise 1040 */ 1041 1042 static int recv_stream(struct kiocb *iocb, struct socket *sock, 1043 struct msghdr *m, size_t buf_len, int flags) 1044 { 1045 struct sock *sk = sock->sk; 1046 struct tipc_port *tport = tipc_sk_port(sk); 1047 struct sk_buff *buf; 1048 struct tipc_msg *msg; 1049 unsigned int sz; 1050 int sz_to_copy, target, needed; 1051 int sz_copied = 0; 1052 char __user *crs = m->msg_iov->iov_base; 1053 unsigned char *buf_crs; 1054 u32 err; 1055 int res = 0; 1056 1057 /* Catch invalid receive attempts */ 1058 1059 if (m->msg_iovlen != 1) 1060 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */ 1061 1062 if (unlikely(!buf_len)) 1063 return -EINVAL; 1064 1065 lock_sock(sk); 1066 1067 if (unlikely((sock->state == SS_UNCONNECTED) || 1068 (sock->state == SS_CONNECTING))) { 1069 res = -ENOTCONN; 1070 goto exit; 1071 } 1072 1073 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len); 1074 1075 restart: 1076 1077 /* Look for a message in receive queue; wait if necessary */ 1078 1079 while (skb_queue_empty(&sk->sk_receive_queue)) { 1080 if (sock->state == SS_DISCONNECTING) { 1081 res = -ENOTCONN; 1082 goto exit; 1083 } 1084 if (flags & MSG_DONTWAIT) { 1085 res = -EWOULDBLOCK; 1086 goto exit; 1087 } 1088 release_sock(sk); 1089 res = wait_event_interruptible(*sk_sleep(sk), 1090 (!skb_queue_empty(&sk->sk_receive_queue) || 1091 (sock->state == SS_DISCONNECTING))); 1092 lock_sock(sk); 1093 if (res) 1094 goto exit; 1095 } 1096 1097 /* Look at first message in receive queue */ 1098 1099 buf = skb_peek(&sk->sk_receive_queue); 1100 msg = buf_msg(buf); 1101 sz = msg_data_sz(msg); 1102 err = msg_errcode(msg); 1103 1104 /* Discard an empty non-errored message & try again */ 1105 1106 if ((!sz) && (!err)) { 1107 advance_rx_queue(sk); 1108 goto restart; 1109 } 1110 1111 /* Optionally capture sender's address & ancillary data of first msg */ 1112 1113 if (sz_copied == 0) { 1114 set_orig_addr(m, msg); 1115 res = anc_data_recv(m, msg, tport); 1116 if (res) 1117 goto exit; 1118 } 1119 1120 /* Capture message data (if valid) & compute return value (always) */ 1121 1122 if (!err) { 1123 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle); 1124 sz = (unsigned char *)msg + msg_size(msg) - buf_crs; 1125 1126 needed = (buf_len - sz_copied); 1127 sz_to_copy = (sz <= needed) ? sz : needed; 1128 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) { 1129 res = -EFAULT; 1130 goto exit; 1131 } 1132 sz_copied += sz_to_copy; 1133 1134 if (sz_to_copy < sz) { 1135 if (!(flags & MSG_PEEK)) 1136 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy; 1137 goto exit; 1138 } 1139 1140 crs += sz_to_copy; 1141 } else { 1142 if (sz_copied != 0) 1143 goto exit; /* can't add error msg to valid data */ 1144 1145 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control) 1146 res = 0; 1147 else 1148 res = -ECONNRESET; 1149 } 1150 1151 /* Consume received message (optional) */ 1152 1153 if (likely(!(flags & MSG_PEEK))) { 1154 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN)) 1155 tipc_acknowledge(tport->ref, tport->conn_unacked); 1156 advance_rx_queue(sk); 1157 } 1158 1159 /* Loop around if more data is required */ 1160 1161 if ((sz_copied < buf_len) && /* didn't get all requested data */ 1162 (!skb_queue_empty(&sk->sk_receive_queue) || 1163 (sz_copied < target)) && /* and more is ready or required */ 1164 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */ 1165 (!err)) /* and haven't reached a FIN */ 1166 goto restart; 1167 1168 exit: 1169 release_sock(sk); 1170 return sz_copied ? sz_copied : res; 1171 } 1172 1173 /** 1174 * rx_queue_full - determine if receive queue can accept another message 1175 * @msg: message to be added to queue 1176 * @queue_size: current size of queue 1177 * @base: nominal maximum size of queue 1178 * 1179 * Returns 1 if queue is unable to accept message, 0 otherwise 1180 */ 1181 1182 static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base) 1183 { 1184 u32 threshold; 1185 u32 imp = msg_importance(msg); 1186 1187 if (imp == TIPC_LOW_IMPORTANCE) 1188 threshold = base; 1189 else if (imp == TIPC_MEDIUM_IMPORTANCE) 1190 threshold = base * 2; 1191 else if (imp == TIPC_HIGH_IMPORTANCE) 1192 threshold = base * 100; 1193 else 1194 return 0; 1195 1196 if (msg_connected(msg)) 1197 threshold *= 4; 1198 1199 return queue_size >= threshold; 1200 } 1201 1202 /** 1203 * filter_rcv - validate incoming message 1204 * @sk: socket 1205 * @buf: message 1206 * 1207 * Enqueues message on receive queue if acceptable; optionally handles 1208 * disconnect indication for a connected socket. 1209 * 1210 * Called with socket lock already taken; port lock may also be taken. 1211 * 1212 * Returns TIPC error status code (TIPC_OK if message is not to be rejected) 1213 */ 1214 1215 static u32 filter_rcv(struct sock *sk, struct sk_buff *buf) 1216 { 1217 struct socket *sock = sk->sk_socket; 1218 struct tipc_msg *msg = buf_msg(buf); 1219 u32 recv_q_len; 1220 1221 /* Reject message if it is wrong sort of message for socket */ 1222 1223 /* 1224 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD? 1225 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY 1226 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC 1227 */ 1228 1229 if (sock->state == SS_READY) { 1230 if (msg_connected(msg)) { 1231 msg_dbg(msg, "dispatch filter 1\n"); 1232 return TIPC_ERR_NO_PORT; 1233 } 1234 } else { 1235 if (msg_mcast(msg)) { 1236 msg_dbg(msg, "dispatch filter 2\n"); 1237 return TIPC_ERR_NO_PORT; 1238 } 1239 if (sock->state == SS_CONNECTED) { 1240 if (!msg_connected(msg)) { 1241 msg_dbg(msg, "dispatch filter 3\n"); 1242 return TIPC_ERR_NO_PORT; 1243 } 1244 } 1245 else if (sock->state == SS_CONNECTING) { 1246 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) { 1247 msg_dbg(msg, "dispatch filter 4\n"); 1248 return TIPC_ERR_NO_PORT; 1249 } 1250 } 1251 else if (sock->state == SS_LISTENING) { 1252 if (msg_connected(msg) || msg_errcode(msg)) { 1253 msg_dbg(msg, "dispatch filter 5\n"); 1254 return TIPC_ERR_NO_PORT; 1255 } 1256 } 1257 else if (sock->state == SS_DISCONNECTING) { 1258 msg_dbg(msg, "dispatch filter 6\n"); 1259 return TIPC_ERR_NO_PORT; 1260 } 1261 else /* (sock->state == SS_UNCONNECTED) */ { 1262 if (msg_connected(msg) || msg_errcode(msg)) { 1263 msg_dbg(msg, "dispatch filter 7\n"); 1264 return TIPC_ERR_NO_PORT; 1265 } 1266 } 1267 } 1268 1269 /* Reject message if there isn't room to queue it */ 1270 1271 recv_q_len = (u32)atomic_read(&tipc_queue_size); 1272 if (unlikely(recv_q_len >= OVERLOAD_LIMIT_BASE)) { 1273 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE)) 1274 return TIPC_ERR_OVERLOAD; 1275 } 1276 recv_q_len = skb_queue_len(&sk->sk_receive_queue); 1277 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) { 1278 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2)) 1279 return TIPC_ERR_OVERLOAD; 1280 } 1281 1282 /* Enqueue message (finally!) */ 1283 1284 msg_dbg(msg, "<DISP<: "); 1285 TIPC_SKB_CB(buf)->handle = msg_data(msg); 1286 atomic_inc(&tipc_queue_size); 1287 __skb_queue_tail(&sk->sk_receive_queue, buf); 1288 1289 /* Initiate connection termination for an incoming 'FIN' */ 1290 1291 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) { 1292 sock->state = SS_DISCONNECTING; 1293 tipc_disconnect_port(tipc_sk_port(sk)); 1294 } 1295 1296 if (waitqueue_active(sk_sleep(sk))) 1297 wake_up_interruptible(sk_sleep(sk)); 1298 return TIPC_OK; 1299 } 1300 1301 /** 1302 * backlog_rcv - handle incoming message from backlog queue 1303 * @sk: socket 1304 * @buf: message 1305 * 1306 * Caller must hold socket lock, but not port lock. 1307 * 1308 * Returns 0 1309 */ 1310 1311 static int backlog_rcv(struct sock *sk, struct sk_buff *buf) 1312 { 1313 u32 res; 1314 1315 res = filter_rcv(sk, buf); 1316 if (res) 1317 tipc_reject_msg(buf, res); 1318 return 0; 1319 } 1320 1321 /** 1322 * dispatch - handle incoming message 1323 * @tport: TIPC port that received message 1324 * @buf: message 1325 * 1326 * Called with port lock already taken. 1327 * 1328 * Returns TIPC error status code (TIPC_OK if message is not to be rejected) 1329 */ 1330 1331 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf) 1332 { 1333 struct sock *sk = (struct sock *)tport->usr_handle; 1334 u32 res; 1335 1336 /* 1337 * Process message if socket is unlocked; otherwise add to backlog queue 1338 * 1339 * This code is based on sk_receive_skb(), but must be distinct from it 1340 * since a TIPC-specific filter/reject mechanism is utilized 1341 */ 1342 1343 bh_lock_sock(sk); 1344 if (!sock_owned_by_user(sk)) { 1345 res = filter_rcv(sk, buf); 1346 } else { 1347 if (sk_add_backlog(sk, buf)) 1348 res = TIPC_ERR_OVERLOAD; 1349 else 1350 res = TIPC_OK; 1351 } 1352 bh_unlock_sock(sk); 1353 1354 return res; 1355 } 1356 1357 /** 1358 * wakeupdispatch - wake up port after congestion 1359 * @tport: port to wakeup 1360 * 1361 * Called with port lock already taken. 1362 */ 1363 1364 static void wakeupdispatch(struct tipc_port *tport) 1365 { 1366 struct sock *sk = (struct sock *)tport->usr_handle; 1367 1368 if (waitqueue_active(sk_sleep(sk))) 1369 wake_up_interruptible(sk_sleep(sk)); 1370 } 1371 1372 /** 1373 * connect - establish a connection to another TIPC port 1374 * @sock: socket structure 1375 * @dest: socket address for destination port 1376 * @destlen: size of socket address data structure 1377 * @flags: file-related flags associated with socket 1378 * 1379 * Returns 0 on success, errno otherwise 1380 */ 1381 1382 static int connect(struct socket *sock, struct sockaddr *dest, int destlen, 1383 int flags) 1384 { 1385 struct sock *sk = sock->sk; 1386 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; 1387 struct msghdr m = {NULL,}; 1388 struct sk_buff *buf; 1389 struct tipc_msg *msg; 1390 long timeout; 1391 int res; 1392 1393 lock_sock(sk); 1394 1395 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */ 1396 1397 if (sock->state == SS_READY) { 1398 res = -EOPNOTSUPP; 1399 goto exit; 1400 } 1401 1402 /* For now, TIPC does not support the non-blocking form of connect() */ 1403 1404 if (flags & O_NONBLOCK) { 1405 res = -EOPNOTSUPP; 1406 goto exit; 1407 } 1408 1409 /* Issue Posix-compliant error code if socket is in the wrong state */ 1410 1411 if (sock->state == SS_LISTENING) { 1412 res = -EOPNOTSUPP; 1413 goto exit; 1414 } 1415 if (sock->state == SS_CONNECTING) { 1416 res = -EALREADY; 1417 goto exit; 1418 } 1419 if (sock->state != SS_UNCONNECTED) { 1420 res = -EISCONN; 1421 goto exit; 1422 } 1423 1424 /* 1425 * Reject connection attempt using multicast address 1426 * 1427 * Note: send_msg() validates the rest of the address fields, 1428 * so there's no need to do it here 1429 */ 1430 1431 if (dst->addrtype == TIPC_ADDR_MCAST) { 1432 res = -EINVAL; 1433 goto exit; 1434 } 1435 1436 /* Reject any messages already in receive queue (very unlikely) */ 1437 1438 reject_rx_queue(sk); 1439 1440 /* Send a 'SYN-' to destination */ 1441 1442 m.msg_name = dest; 1443 m.msg_namelen = destlen; 1444 res = send_msg(NULL, sock, &m, 0); 1445 if (res < 0) { 1446 goto exit; 1447 } 1448 1449 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */ 1450 1451 timeout = tipc_sk(sk)->conn_timeout; 1452 release_sock(sk); 1453 res = wait_event_interruptible_timeout(*sk_sleep(sk), 1454 (!skb_queue_empty(&sk->sk_receive_queue) || 1455 (sock->state != SS_CONNECTING)), 1456 timeout ? timeout : MAX_SCHEDULE_TIMEOUT); 1457 lock_sock(sk); 1458 1459 if (res > 0) { 1460 buf = skb_peek(&sk->sk_receive_queue); 1461 if (buf != NULL) { 1462 msg = buf_msg(buf); 1463 res = auto_connect(sock, msg); 1464 if (!res) { 1465 if (!msg_data_sz(msg)) 1466 advance_rx_queue(sk); 1467 } 1468 } else { 1469 if (sock->state == SS_CONNECTED) { 1470 res = -EISCONN; 1471 } else { 1472 res = -ECONNREFUSED; 1473 } 1474 } 1475 } else { 1476 if (res == 0) 1477 res = -ETIMEDOUT; 1478 else 1479 ; /* leave "res" unchanged */ 1480 sock->state = SS_DISCONNECTING; 1481 } 1482 1483 exit: 1484 release_sock(sk); 1485 return res; 1486 } 1487 1488 /** 1489 * listen - allow socket to listen for incoming connections 1490 * @sock: socket structure 1491 * @len: (unused) 1492 * 1493 * Returns 0 on success, errno otherwise 1494 */ 1495 1496 static int listen(struct socket *sock, int len) 1497 { 1498 struct sock *sk = sock->sk; 1499 int res; 1500 1501 lock_sock(sk); 1502 1503 if (sock->state == SS_READY) 1504 res = -EOPNOTSUPP; 1505 else if (sock->state != SS_UNCONNECTED) 1506 res = -EINVAL; 1507 else { 1508 sock->state = SS_LISTENING; 1509 res = 0; 1510 } 1511 1512 release_sock(sk); 1513 return res; 1514 } 1515 1516 /** 1517 * accept - wait for connection request 1518 * @sock: listening socket 1519 * @newsock: new socket that is to be connected 1520 * @flags: file-related flags associated with socket 1521 * 1522 * Returns 0 on success, errno otherwise 1523 */ 1524 1525 static int accept(struct socket *sock, struct socket *new_sock, int flags) 1526 { 1527 struct sock *sk = sock->sk; 1528 struct sk_buff *buf; 1529 int res; 1530 1531 lock_sock(sk); 1532 1533 if (sock->state == SS_READY) { 1534 res = -EOPNOTSUPP; 1535 goto exit; 1536 } 1537 if (sock->state != SS_LISTENING) { 1538 res = -EINVAL; 1539 goto exit; 1540 } 1541 1542 while (skb_queue_empty(&sk->sk_receive_queue)) { 1543 if (flags & O_NONBLOCK) { 1544 res = -EWOULDBLOCK; 1545 goto exit; 1546 } 1547 release_sock(sk); 1548 res = wait_event_interruptible(*sk_sleep(sk), 1549 (!skb_queue_empty(&sk->sk_receive_queue))); 1550 lock_sock(sk); 1551 if (res) 1552 goto exit; 1553 } 1554 1555 buf = skb_peek(&sk->sk_receive_queue); 1556 1557 res = tipc_create(sock_net(sock->sk), new_sock, 0, 0); 1558 if (!res) { 1559 struct sock *new_sk = new_sock->sk; 1560 struct tipc_sock *new_tsock = tipc_sk(new_sk); 1561 struct tipc_port *new_tport = new_tsock->p; 1562 u32 new_ref = new_tport->ref; 1563 struct tipc_msg *msg = buf_msg(buf); 1564 1565 lock_sock(new_sk); 1566 1567 /* 1568 * Reject any stray messages received by new socket 1569 * before the socket lock was taken (very, very unlikely) 1570 */ 1571 1572 reject_rx_queue(new_sk); 1573 1574 /* Connect new socket to it's peer */ 1575 1576 new_tsock->peer_name.ref = msg_origport(msg); 1577 new_tsock->peer_name.node = msg_orignode(msg); 1578 tipc_connect2port(new_ref, &new_tsock->peer_name); 1579 new_sock->state = SS_CONNECTED; 1580 1581 tipc_set_portimportance(new_ref, msg_importance(msg)); 1582 if (msg_named(msg)) { 1583 new_tport->conn_type = msg_nametype(msg); 1584 new_tport->conn_instance = msg_nameinst(msg); 1585 } 1586 1587 /* 1588 * Respond to 'SYN-' by discarding it & returning 'ACK'-. 1589 * Respond to 'SYN+' by queuing it on new socket. 1590 */ 1591 1592 msg_dbg(msg,"<ACC<: "); 1593 if (!msg_data_sz(msg)) { 1594 struct msghdr m = {NULL,}; 1595 1596 advance_rx_queue(sk); 1597 send_packet(NULL, new_sock, &m, 0); 1598 } else { 1599 __skb_dequeue(&sk->sk_receive_queue); 1600 __skb_queue_head(&new_sk->sk_receive_queue, buf); 1601 } 1602 release_sock(new_sk); 1603 } 1604 exit: 1605 release_sock(sk); 1606 return res; 1607 } 1608 1609 /** 1610 * shutdown - shutdown socket connection 1611 * @sock: socket structure 1612 * @how: direction to close (must be SHUT_RDWR) 1613 * 1614 * Terminates connection (if necessary), then purges socket's receive queue. 1615 * 1616 * Returns 0 on success, errno otherwise 1617 */ 1618 1619 static int shutdown(struct socket *sock, int how) 1620 { 1621 struct sock *sk = sock->sk; 1622 struct tipc_port *tport = tipc_sk_port(sk); 1623 struct sk_buff *buf; 1624 int res; 1625 1626 if (how != SHUT_RDWR) 1627 return -EINVAL; 1628 1629 lock_sock(sk); 1630 1631 switch (sock->state) { 1632 case SS_CONNECTING: 1633 case SS_CONNECTED: 1634 1635 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */ 1636 restart: 1637 buf = __skb_dequeue(&sk->sk_receive_queue); 1638 if (buf) { 1639 atomic_dec(&tipc_queue_size); 1640 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) { 1641 buf_discard(buf); 1642 goto restart; 1643 } 1644 tipc_disconnect(tport->ref); 1645 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN); 1646 } else { 1647 tipc_shutdown(tport->ref); 1648 } 1649 1650 sock->state = SS_DISCONNECTING; 1651 1652 /* fall through */ 1653 1654 case SS_DISCONNECTING: 1655 1656 /* Discard any unreceived messages; wake up sleeping tasks */ 1657 1658 discard_rx_queue(sk); 1659 if (waitqueue_active(sk_sleep(sk))) 1660 wake_up_interruptible(sk_sleep(sk)); 1661 res = 0; 1662 break; 1663 1664 default: 1665 res = -ENOTCONN; 1666 } 1667 1668 release_sock(sk); 1669 return res; 1670 } 1671 1672 /** 1673 * setsockopt - set socket option 1674 * @sock: socket structure 1675 * @lvl: option level 1676 * @opt: option identifier 1677 * @ov: pointer to new option value 1678 * @ol: length of option value 1679 * 1680 * For stream sockets only, accepts and ignores all IPPROTO_TCP options 1681 * (to ease compatibility). 1682 * 1683 * Returns 0 on success, errno otherwise 1684 */ 1685 1686 static int setsockopt(struct socket *sock, 1687 int lvl, int opt, char __user *ov, unsigned int ol) 1688 { 1689 struct sock *sk = sock->sk; 1690 struct tipc_port *tport = tipc_sk_port(sk); 1691 u32 value; 1692 int res; 1693 1694 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) 1695 return 0; 1696 if (lvl != SOL_TIPC) 1697 return -ENOPROTOOPT; 1698 if (ol < sizeof(value)) 1699 return -EINVAL; 1700 if ((res = get_user(value, (u32 __user *)ov))) 1701 return res; 1702 1703 lock_sock(sk); 1704 1705 switch (opt) { 1706 case TIPC_IMPORTANCE: 1707 res = tipc_set_portimportance(tport->ref, value); 1708 break; 1709 case TIPC_SRC_DROPPABLE: 1710 if (sock->type != SOCK_STREAM) 1711 res = tipc_set_portunreliable(tport->ref, value); 1712 else 1713 res = -ENOPROTOOPT; 1714 break; 1715 case TIPC_DEST_DROPPABLE: 1716 res = tipc_set_portunreturnable(tport->ref, value); 1717 break; 1718 case TIPC_CONN_TIMEOUT: 1719 tipc_sk(sk)->conn_timeout = msecs_to_jiffies(value); 1720 /* no need to set "res", since already 0 at this point */ 1721 break; 1722 default: 1723 res = -EINVAL; 1724 } 1725 1726 release_sock(sk); 1727 1728 return res; 1729 } 1730 1731 /** 1732 * getsockopt - get socket option 1733 * @sock: socket structure 1734 * @lvl: option level 1735 * @opt: option identifier 1736 * @ov: receptacle for option value 1737 * @ol: receptacle for length of option value 1738 * 1739 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options 1740 * (to ease compatibility). 1741 * 1742 * Returns 0 on success, errno otherwise 1743 */ 1744 1745 static int getsockopt(struct socket *sock, 1746 int lvl, int opt, char __user *ov, int __user *ol) 1747 { 1748 struct sock *sk = sock->sk; 1749 struct tipc_port *tport = tipc_sk_port(sk); 1750 int len; 1751 u32 value; 1752 int res; 1753 1754 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) 1755 return put_user(0, ol); 1756 if (lvl != SOL_TIPC) 1757 return -ENOPROTOOPT; 1758 if ((res = get_user(len, ol))) 1759 return res; 1760 1761 lock_sock(sk); 1762 1763 switch (opt) { 1764 case TIPC_IMPORTANCE: 1765 res = tipc_portimportance(tport->ref, &value); 1766 break; 1767 case TIPC_SRC_DROPPABLE: 1768 res = tipc_portunreliable(tport->ref, &value); 1769 break; 1770 case TIPC_DEST_DROPPABLE: 1771 res = tipc_portunreturnable(tport->ref, &value); 1772 break; 1773 case TIPC_CONN_TIMEOUT: 1774 value = jiffies_to_msecs(tipc_sk(sk)->conn_timeout); 1775 /* no need to set "res", since already 0 at this point */ 1776 break; 1777 case TIPC_NODE_RECVQ_DEPTH: 1778 value = (u32)atomic_read(&tipc_queue_size); 1779 break; 1780 case TIPC_SOCK_RECVQ_DEPTH: 1781 value = skb_queue_len(&sk->sk_receive_queue); 1782 break; 1783 default: 1784 res = -EINVAL; 1785 } 1786 1787 release_sock(sk); 1788 1789 if (res) { 1790 /* "get" failed */ 1791 } 1792 else if (len < sizeof(value)) { 1793 res = -EINVAL; 1794 } 1795 else if (copy_to_user(ov, &value, sizeof(value))) { 1796 res = -EFAULT; 1797 } 1798 else { 1799 res = put_user(sizeof(value), ol); 1800 } 1801 1802 return res; 1803 } 1804 1805 /** 1806 * Protocol switches for the various types of TIPC sockets 1807 */ 1808 1809 static const struct proto_ops msg_ops = { 1810 .owner = THIS_MODULE, 1811 .family = AF_TIPC, 1812 .release = release, 1813 .bind = bind, 1814 .connect = connect, 1815 .socketpair = sock_no_socketpair, 1816 .accept = accept, 1817 .getname = get_name, 1818 .poll = poll, 1819 .ioctl = sock_no_ioctl, 1820 .listen = listen, 1821 .shutdown = shutdown, 1822 .setsockopt = setsockopt, 1823 .getsockopt = getsockopt, 1824 .sendmsg = send_msg, 1825 .recvmsg = recv_msg, 1826 .mmap = sock_no_mmap, 1827 .sendpage = sock_no_sendpage 1828 }; 1829 1830 static const struct proto_ops packet_ops = { 1831 .owner = THIS_MODULE, 1832 .family = AF_TIPC, 1833 .release = release, 1834 .bind = bind, 1835 .connect = connect, 1836 .socketpair = sock_no_socketpair, 1837 .accept = accept, 1838 .getname = get_name, 1839 .poll = poll, 1840 .ioctl = sock_no_ioctl, 1841 .listen = listen, 1842 .shutdown = shutdown, 1843 .setsockopt = setsockopt, 1844 .getsockopt = getsockopt, 1845 .sendmsg = send_packet, 1846 .recvmsg = recv_msg, 1847 .mmap = sock_no_mmap, 1848 .sendpage = sock_no_sendpage 1849 }; 1850 1851 static const struct proto_ops stream_ops = { 1852 .owner = THIS_MODULE, 1853 .family = AF_TIPC, 1854 .release = release, 1855 .bind = bind, 1856 .connect = connect, 1857 .socketpair = sock_no_socketpair, 1858 .accept = accept, 1859 .getname = get_name, 1860 .poll = poll, 1861 .ioctl = sock_no_ioctl, 1862 .listen = listen, 1863 .shutdown = shutdown, 1864 .setsockopt = setsockopt, 1865 .getsockopt = getsockopt, 1866 .sendmsg = send_stream, 1867 .recvmsg = recv_stream, 1868 .mmap = sock_no_mmap, 1869 .sendpage = sock_no_sendpage 1870 }; 1871 1872 static const struct net_proto_family tipc_family_ops = { 1873 .owner = THIS_MODULE, 1874 .family = AF_TIPC, 1875 .create = tipc_create 1876 }; 1877 1878 static struct proto tipc_proto = { 1879 .name = "TIPC", 1880 .owner = THIS_MODULE, 1881 .obj_size = sizeof(struct tipc_sock) 1882 }; 1883 1884 /** 1885 * tipc_socket_init - initialize TIPC socket interface 1886 * 1887 * Returns 0 on success, errno otherwise 1888 */ 1889 int tipc_socket_init(void) 1890 { 1891 int res; 1892 1893 res = proto_register(&tipc_proto, 1); 1894 if (res) { 1895 err("Failed to register TIPC protocol type\n"); 1896 goto out; 1897 } 1898 1899 res = sock_register(&tipc_family_ops); 1900 if (res) { 1901 err("Failed to register TIPC socket type\n"); 1902 proto_unregister(&tipc_proto); 1903 goto out; 1904 } 1905 1906 sockets_enabled = 1; 1907 out: 1908 return res; 1909 } 1910 1911 /** 1912 * tipc_socket_stop - stop TIPC socket interface 1913 */ 1914 1915 void tipc_socket_stop(void) 1916 { 1917 if (!sockets_enabled) 1918 return; 1919 1920 sockets_enabled = 0; 1921 sock_unregister(tipc_family_ops.family); 1922 proto_unregister(&tipc_proto); 1923 } 1924 1925